Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 1 | #include <asm/bug.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 3 | #include <sys/time.h> |
| 4 | #include <sys/resource.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 5 | #include <errno.h> |
Arnaldo Carvalho de Melo | 611f0af | 2017-04-19 16:29:38 -0300 | [diff] [blame^] | 6 | #include "compress.h" |
Arnaldo Carvalho de Melo | 9a3993d | 2017-04-18 11:33:48 -0300 | [diff] [blame] | 7 | #include "path.h" |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 8 | #include "symbol.h" |
| 9 | #include "dso.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 10 | #include "machine.h" |
Adrian Hunter | cfe9174 | 2015-04-09 18:53:55 +0300 | [diff] [blame] | 11 | #include "auxtrace.h" |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 12 | #include "util.h" |
| 13 | #include "debug.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 14 | #include "string2.h" |
He Kuang | 6ae98ba | 2016-05-12 08:43:11 +0000 | [diff] [blame] | 15 | #include "vdso.h" |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 16 | |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 17 | static const char * const debuglink_paths[] = { |
| 18 | "%.0s%s", |
| 19 | "%s/%s", |
| 20 | "%s/.debug/%s", |
| 21 | "/usr/lib/debug%s/%s" |
| 22 | }; |
| 23 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 24 | char dso__symtab_origin(const struct dso *dso) |
| 25 | { |
| 26 | static const char origin[] = { |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 27 | [DSO_BINARY_TYPE__KALLSYMS] = 'k', |
| 28 | [DSO_BINARY_TYPE__VMLINUX] = 'v', |
| 29 | [DSO_BINARY_TYPE__JAVA_JIT] = 'j', |
| 30 | [DSO_BINARY_TYPE__DEBUGLINK] = 'l', |
| 31 | [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B', |
| 32 | [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f', |
| 33 | [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u', |
| 34 | [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o', |
| 35 | [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b', |
| 36 | [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd', |
| 37 | [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K', |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 38 | [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm', |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 39 | [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g', |
| 40 | [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G', |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 41 | [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M', |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 42 | [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V', |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND) |
| 46 | return '!'; |
| 47 | return origin[dso->symtab_type]; |
| 48 | } |
| 49 | |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 50 | int dso__read_binary_type_filename(const struct dso *dso, |
| 51 | enum dso_binary_type type, |
| 52 | char *root_dir, char *filename, size_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 53 | { |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 54 | char build_id_hex[SBUILD_ID_SIZE]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 55 | int ret = 0; |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 56 | size_t len; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 57 | |
| 58 | switch (type) { |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 59 | case DSO_BINARY_TYPE__DEBUGLINK: |
| 60 | { |
| 61 | const char *last_slash; |
| 62 | char dso_dir[PATH_MAX]; |
| 63 | char symfile[PATH_MAX]; |
| 64 | unsigned int i; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 65 | |
Victor Kamensky | dc6254c | 2015-01-26 22:34:02 -0800 | [diff] [blame] | 66 | len = __symbol__join_symfs(filename, size, dso->long_name); |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 67 | last_slash = filename + len; |
| 68 | while (last_slash != filename && *last_slash != '/') |
| 69 | last_slash--; |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 70 | |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 71 | strncpy(dso_dir, filename, last_slash - filename); |
| 72 | dso_dir[last_slash-filename] = '\0'; |
| 73 | |
| 74 | if (!is_regular_file(filename)) { |
| 75 | ret = -1; |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | ret = filename__read_debuglink(filename, symfile, PATH_MAX); |
| 80 | if (ret) |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 81 | break; |
| 82 | |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 83 | /* Check predefined locations where debug file might reside */ |
| 84 | ret = -1; |
| 85 | for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) { |
| 86 | snprintf(filename, size, |
| 87 | debuglink_paths[i], dso_dir, symfile); |
| 88 | if (is_regular_file(filename)) { |
| 89 | ret = 0; |
| 90 | break; |
| 91 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 92 | } |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 93 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 94 | break; |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 95 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 96 | case DSO_BINARY_TYPE__BUILD_ID_CACHE: |
He Kuang | a706670 | 2016-05-19 11:47:37 +0000 | [diff] [blame] | 97 | if (dso__build_id_filename(dso, filename, size) == NULL) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 98 | ret = -1; |
| 99 | break; |
| 100 | |
| 101 | case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 102 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); |
| 103 | snprintf(filename + len, size - len, "%s.debug", dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 104 | break; |
| 105 | |
| 106 | case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 107 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); |
| 108 | snprintf(filename + len, size - len, "%s", dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 109 | break; |
| 110 | |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 111 | case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: |
| 112 | { |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 113 | const char *last_slash; |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 114 | size_t dir_size; |
| 115 | |
| 116 | last_slash = dso->long_name + dso->long_name_len; |
| 117 | while (last_slash != dso->long_name && *last_slash != '/') |
| 118 | last_slash--; |
| 119 | |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 120 | len = __symbol__join_symfs(filename, size, ""); |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 121 | dir_size = last_slash - dso->long_name + 2; |
| 122 | if (dir_size > (size - len)) { |
| 123 | ret = -1; |
| 124 | break; |
| 125 | } |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 126 | len += scnprintf(filename + len, dir_size, "%s", dso->long_name); |
| 127 | len += scnprintf(filename + len , size - len, ".debug%s", |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 128 | last_slash); |
| 129 | break; |
| 130 | } |
| 131 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 132 | case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: |
| 133 | if (!dso->has_build_id) { |
| 134 | ret = -1; |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | build_id__sprintf(dso->build_id, |
| 139 | sizeof(dso->build_id), |
| 140 | build_id_hex); |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 141 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/"); |
| 142 | snprintf(filename + len, size - len, "%.2s/%s.debug", |
| 143 | build_id_hex, build_id_hex + 2); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 144 | break; |
| 145 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 146 | case DSO_BINARY_TYPE__VMLINUX: |
| 147 | case DSO_BINARY_TYPE__GUEST_VMLINUX: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 148 | case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 149 | __symbol__join_symfs(filename, size, dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 150 | break; |
| 151 | |
| 152 | case DSO_BINARY_TYPE__GUEST_KMODULE: |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 153 | case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 154 | path__join3(filename, size, symbol_conf.symfs, |
| 155 | root_dir, dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 156 | break; |
| 157 | |
| 158 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 159 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 160 | __symbol__join_symfs(filename, size, dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 161 | break; |
| 162 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 163 | case DSO_BINARY_TYPE__KCORE: |
| 164 | case DSO_BINARY_TYPE__GUEST_KCORE: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 165 | snprintf(filename, size, "%s", dso->long_name); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 166 | break; |
| 167 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 168 | default: |
| 169 | case DSO_BINARY_TYPE__KALLSYMS: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 170 | case DSO_BINARY_TYPE__GUEST_KALLSYMS: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 171 | case DSO_BINARY_TYPE__JAVA_JIT: |
| 172 | case DSO_BINARY_TYPE__NOT_FOUND: |
| 173 | ret = -1; |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 180 | static const struct { |
| 181 | const char *fmt; |
| 182 | int (*decompress)(const char *input, int output); |
| 183 | } compressions[] = { |
Namhyung Kim | e92ce12 | 2014-10-31 16:51:38 +0900 | [diff] [blame] | 184 | #ifdef HAVE_ZLIB_SUPPORT |
| 185 | { "gz", gzip_decompress_to_file }, |
| 186 | #endif |
Jiri Olsa | 80a32e5b | 2015-01-29 13:29:39 +0100 | [diff] [blame] | 187 | #ifdef HAVE_LZMA_SUPPORT |
| 188 | { "xz", lzma_decompress_to_file }, |
| 189 | #endif |
Namhyung Kim | e92ce12 | 2014-10-31 16:51:38 +0900 | [diff] [blame] | 190 | { NULL, NULL }, |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | bool is_supported_compression(const char *ext) |
| 194 | { |
| 195 | unsigned i; |
| 196 | |
| 197 | for (i = 0; compressions[i].fmt; i++) { |
| 198 | if (!strcmp(ext, compressions[i].fmt)) |
| 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 204 | bool is_kernel_module(const char *pathname, int cpumode) |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 205 | { |
Jiri Olsa | 8dee9ff11 | 2015-02-12 15:56:21 +0100 | [diff] [blame] | 206 | struct kmod_path m; |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 207 | int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK; |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 208 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 209 | WARN_ONCE(mode != cpumode, |
| 210 | "Internal error: passing unmasked cpumode (%x) to is_kernel_module", |
| 211 | cpumode); |
| 212 | |
| 213 | switch (mode) { |
| 214 | case PERF_RECORD_MISC_USER: |
| 215 | case PERF_RECORD_MISC_HYPERVISOR: |
| 216 | case PERF_RECORD_MISC_GUEST_USER: |
| 217 | return false; |
| 218 | /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */ |
| 219 | default: |
| 220 | if (kmod_path__parse(&m, pathname)) { |
| 221 | pr_err("Failed to check whether %s is a kernel module or not. Assume it is.", |
| 222 | pathname); |
| 223 | return true; |
| 224 | } |
| 225 | } |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 226 | |
Jiri Olsa | 8dee9ff11 | 2015-02-12 15:56:21 +0100 | [diff] [blame] | 227 | return m.kmod; |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | bool decompress_to_file(const char *ext, const char *filename, int output_fd) |
| 231 | { |
| 232 | unsigned i; |
| 233 | |
| 234 | for (i = 0; compressions[i].fmt; i++) { |
| 235 | if (!strcmp(ext, compressions[i].fmt)) |
| 236 | return !compressions[i].decompress(filename, |
| 237 | output_fd); |
| 238 | } |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | bool dso__needs_decompress(struct dso *dso) |
| 243 | { |
| 244 | return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || |
| 245 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; |
| 246 | } |
| 247 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 248 | /* |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 249 | * Parses kernel module specified in @path and updates |
| 250 | * @m argument like: |
| 251 | * |
| 252 | * @comp - true if @path contains supported compression suffix, |
| 253 | * false otherwise |
| 254 | * @kmod - true if @path contains '.ko' suffix in right position, |
| 255 | * false otherwise |
| 256 | * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name |
| 257 | * of the kernel module without suffixes, otherwise strudup-ed |
| 258 | * base name of @path |
| 259 | * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string |
| 260 | * the compression suffix |
| 261 | * |
| 262 | * Returns 0 if there's no strdup error, -ENOMEM otherwise. |
| 263 | */ |
| 264 | int __kmod_path__parse(struct kmod_path *m, const char *path, |
| 265 | bool alloc_name, bool alloc_ext) |
| 266 | { |
| 267 | const char *name = strrchr(path, '/'); |
| 268 | const char *ext = strrchr(path, '.'); |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 269 | bool is_simple_name = false; |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 270 | |
| 271 | memset(m, 0x0, sizeof(*m)); |
| 272 | name = name ? name + 1 : path; |
| 273 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 274 | /* |
| 275 | * '.' is also a valid character for module name. For example: |
| 276 | * [aaa.bbb] is a valid module name. '[' should have higher |
| 277 | * priority than '.ko' suffix. |
| 278 | * |
| 279 | * The kernel names are from machine__mmap_name. Such |
| 280 | * name should belong to kernel itself, not kernel module. |
| 281 | */ |
| 282 | if (name[0] == '[') { |
| 283 | is_simple_name = true; |
| 284 | if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) || |
| 285 | (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) || |
| 286 | (strncmp(name, "[vdso]", 6) == 0) || |
| 287 | (strncmp(name, "[vsyscall]", 10) == 0)) { |
| 288 | m->kmod = false; |
| 289 | |
| 290 | } else |
| 291 | m->kmod = true; |
| 292 | } |
| 293 | |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 294 | /* No extension, just return name. */ |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 295 | if ((ext == NULL) || is_simple_name) { |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 296 | if (alloc_name) { |
| 297 | m->name = strdup(name); |
| 298 | return m->name ? 0 : -ENOMEM; |
| 299 | } |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | if (is_supported_compression(ext + 1)) { |
| 304 | m->comp = true; |
| 305 | ext -= 3; |
| 306 | } |
| 307 | |
| 308 | /* Check .ko extension only if there's enough name left. */ |
| 309 | if (ext > name) |
| 310 | m->kmod = !strncmp(ext, ".ko", 3); |
| 311 | |
| 312 | if (alloc_name) { |
| 313 | if (m->kmod) { |
| 314 | if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1) |
| 315 | return -ENOMEM; |
| 316 | } else { |
| 317 | if (asprintf(&m->name, "%s", name) == -1) |
| 318 | return -ENOMEM; |
| 319 | } |
| 320 | |
| 321 | strxfrchar(m->name, '-', '_'); |
| 322 | } |
| 323 | |
| 324 | if (alloc_ext && m->comp) { |
| 325 | m->ext = strdup(ext + 4); |
| 326 | if (!m->ext) { |
| 327 | free((void *) m->name); |
| 328 | return -ENOMEM; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /* |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 336 | * Global list of open DSOs and the counter. |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 337 | */ |
| 338 | static LIST_HEAD(dso__data_open); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 339 | static long dso__data_open_cnt; |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 340 | static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 341 | |
| 342 | static void dso__list_add(struct dso *dso) |
| 343 | { |
| 344 | list_add_tail(&dso->data.open_entry, &dso__data_open); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 345 | dso__data_open_cnt++; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static void dso__list_del(struct dso *dso) |
| 349 | { |
| 350 | list_del(&dso->data.open_entry); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 351 | WARN_ONCE(dso__data_open_cnt <= 0, |
| 352 | "DSO data fd counter out of bounds."); |
| 353 | dso__data_open_cnt--; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 356 | static void close_first_dso(void); |
| 357 | |
| 358 | static int do_open(char *name) |
| 359 | { |
| 360 | int fd; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 361 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 362 | |
| 363 | do { |
| 364 | fd = open(name, O_RDONLY); |
| 365 | if (fd >= 0) |
| 366 | return fd; |
| 367 | |
Namhyung Kim | a3c0cc2 | 2015-01-30 11:33:29 +0900 | [diff] [blame] | 368 | pr_debug("dso open failed: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 369 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 370 | if (!dso__data_open_cnt || errno != EMFILE) |
| 371 | break; |
| 372 | |
| 373 | close_first_dso(); |
| 374 | } while (1); |
| 375 | |
| 376 | return -1; |
| 377 | } |
| 378 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 379 | static int __open_dso(struct dso *dso, struct machine *machine) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 380 | { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 381 | int fd; |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 382 | char *root_dir = (char *)""; |
| 383 | char *name = malloc(PATH_MAX); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 384 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 385 | if (!name) |
| 386 | return -ENOMEM; |
| 387 | |
| 388 | if (machine) |
| 389 | root_dir = machine->root_dir; |
| 390 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 391 | if (dso__read_binary_type_filename(dso, dso->binary_type, |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 392 | root_dir, name, PATH_MAX)) { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 393 | free(name); |
| 394 | return -EINVAL; |
| 395 | } |
| 396 | |
Jiri Olsa | 3c028a0 | 2016-09-20 18:12:45 +0200 | [diff] [blame] | 397 | if (!is_regular_file(name)) |
| 398 | return -EINVAL; |
| 399 | |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 400 | fd = do_open(name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 401 | free(name); |
| 402 | return fd; |
| 403 | } |
| 404 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 405 | static void check_data_close(void); |
| 406 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 407 | /** |
| 408 | * dso_close - Open DSO data file |
| 409 | * @dso: dso object |
| 410 | * |
| 411 | * Open @dso's data file descriptor and updates |
| 412 | * list/count of open DSO objects. |
| 413 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 414 | static int open_dso(struct dso *dso, struct machine *machine) |
| 415 | { |
| 416 | int fd = __open_dso(dso, machine); |
| 417 | |
Adrian Hunter | a6f6ae9 | 2014-07-17 11:43:09 +0300 | [diff] [blame] | 418 | if (fd >= 0) { |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 419 | dso__list_add(dso); |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 420 | /* |
| 421 | * Check if we crossed the allowed number |
| 422 | * of opened DSOs and close one if needed. |
| 423 | */ |
| 424 | check_data_close(); |
| 425 | } |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 426 | |
| 427 | return fd; |
| 428 | } |
| 429 | |
| 430 | static void close_data_fd(struct dso *dso) |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 431 | { |
| 432 | if (dso->data.fd >= 0) { |
| 433 | close(dso->data.fd); |
| 434 | dso->data.fd = -1; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 435 | dso->data.file_size = 0; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 436 | dso__list_del(dso); |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 440 | /** |
| 441 | * dso_close - Close DSO data file |
| 442 | * @dso: dso object |
| 443 | * |
| 444 | * Close @dso's data file descriptor and updates |
| 445 | * list/count of open DSO objects. |
| 446 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 447 | static void close_dso(struct dso *dso) |
| 448 | { |
| 449 | close_data_fd(dso); |
| 450 | } |
| 451 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 452 | static void close_first_dso(void) |
| 453 | { |
| 454 | struct dso *dso; |
| 455 | |
| 456 | dso = list_first_entry(&dso__data_open, struct dso, data.open_entry); |
| 457 | close_dso(dso); |
| 458 | } |
| 459 | |
| 460 | static rlim_t get_fd_limit(void) |
| 461 | { |
| 462 | struct rlimit l; |
| 463 | rlim_t limit = 0; |
| 464 | |
| 465 | /* Allow half of the current open fd limit. */ |
| 466 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { |
| 467 | if (l.rlim_cur == RLIM_INFINITY) |
| 468 | limit = l.rlim_cur; |
| 469 | else |
| 470 | limit = l.rlim_cur / 2; |
| 471 | } else { |
| 472 | pr_err("failed to get fd limit\n"); |
| 473 | limit = 1; |
| 474 | } |
| 475 | |
| 476 | return limit; |
| 477 | } |
| 478 | |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 479 | static rlim_t fd_limit; |
| 480 | |
| 481 | /* |
| 482 | * Used only by tests/dso-data.c to reset the environment |
| 483 | * for tests. I dont expect we should change this during |
| 484 | * standard runtime. |
| 485 | */ |
| 486 | void reset_fd_limit(void) |
| 487 | { |
| 488 | fd_limit = 0; |
| 489 | } |
| 490 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 491 | static bool may_cache_fd(void) |
| 492 | { |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 493 | if (!fd_limit) |
| 494 | fd_limit = get_fd_limit(); |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 495 | |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 496 | if (fd_limit == RLIM_INFINITY) |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 497 | return true; |
| 498 | |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 499 | return fd_limit > (rlim_t) dso__data_open_cnt; |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 502 | /* |
| 503 | * Check and close LRU dso if we crossed allowed limit |
| 504 | * for opened dso file descriptors. The limit is half |
| 505 | * of the RLIMIT_NOFILE files opened. |
| 506 | */ |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 507 | static void check_data_close(void) |
| 508 | { |
| 509 | bool cache_fd = may_cache_fd(); |
| 510 | |
| 511 | if (!cache_fd) |
| 512 | close_first_dso(); |
| 513 | } |
| 514 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 515 | /** |
| 516 | * dso__data_close - Close DSO data file |
| 517 | * @dso: dso object |
| 518 | * |
| 519 | * External interface to close @dso's data file descriptor. |
| 520 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 521 | void dso__data_close(struct dso *dso) |
| 522 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 523 | pthread_mutex_lock(&dso__data_open_lock); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 524 | close_dso(dso); |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 525 | pthread_mutex_unlock(&dso__data_open_lock); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 528 | static void try_to_open_dso(struct dso *dso, struct machine *machine) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 529 | { |
Arnaldo Carvalho de Melo | 631d34b | 2013-12-16 16:57:43 -0300 | [diff] [blame] | 530 | enum dso_binary_type binary_type_data[] = { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 531 | DSO_BINARY_TYPE__BUILD_ID_CACHE, |
| 532 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, |
| 533 | DSO_BINARY_TYPE__NOT_FOUND, |
| 534 | }; |
| 535 | int i = 0; |
| 536 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 537 | if (dso->data.fd >= 0) |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 538 | return; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 539 | |
| 540 | if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) { |
| 541 | dso->data.fd = open_dso(dso, machine); |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 542 | goto out; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 543 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 544 | |
| 545 | do { |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 546 | dso->binary_type = binary_type_data[i++]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 547 | |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 548 | dso->data.fd = open_dso(dso, machine); |
| 549 | if (dso->data.fd >= 0) |
| 550 | goto out; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 551 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 552 | } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND); |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 553 | out: |
| 554 | if (dso->data.fd >= 0) |
| 555 | dso->data.status = DSO_DATA_STATUS_OK; |
| 556 | else |
| 557 | dso->data.status = DSO_DATA_STATUS_ERROR; |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 558 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 559 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 560 | /** |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 561 | * dso__data_get_fd - Get dso's data file descriptor |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 562 | * @dso: dso object |
| 563 | * @machine: machine object |
| 564 | * |
| 565 | * External interface to find dso's file, open it and |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 566 | * returns file descriptor. It should be paired with |
| 567 | * dso__data_put_fd() if it returns non-negative value. |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 568 | */ |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 569 | int dso__data_get_fd(struct dso *dso, struct machine *machine) |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 570 | { |
| 571 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
| 572 | return -1; |
| 573 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 574 | if (pthread_mutex_lock(&dso__data_open_lock) < 0) |
| 575 | return -1; |
| 576 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 577 | try_to_open_dso(dso, machine); |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 578 | |
| 579 | if (dso->data.fd < 0) |
| 580 | pthread_mutex_unlock(&dso__data_open_lock); |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 581 | |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 582 | return dso->data.fd; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 583 | } |
| 584 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 585 | void dso__data_put_fd(struct dso *dso __maybe_unused) |
| 586 | { |
| 587 | pthread_mutex_unlock(&dso__data_open_lock); |
| 588 | } |
| 589 | |
Adrian Hunter | 288be94 | 2014-07-22 16:17:19 +0300 | [diff] [blame] | 590 | bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) |
| 591 | { |
| 592 | u32 flag = 1 << by; |
| 593 | |
| 594 | if (dso->data.status_seen & flag) |
| 595 | return true; |
| 596 | |
| 597 | dso->data.status_seen |= flag; |
| 598 | |
| 599 | return false; |
| 600 | } |
| 601 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 602 | static void |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 603 | dso_cache__free(struct dso *dso) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 604 | { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 605 | struct rb_root *root = &dso->data.cache; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 606 | struct rb_node *next = rb_first(root); |
| 607 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 608 | pthread_mutex_lock(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 609 | while (next) { |
| 610 | struct dso_cache *cache; |
| 611 | |
| 612 | cache = rb_entry(next, struct dso_cache, rb_node); |
| 613 | next = rb_next(&cache->rb_node); |
| 614 | rb_erase(&cache->rb_node, root); |
| 615 | free(cache); |
| 616 | } |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 617 | pthread_mutex_unlock(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 620 | static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 621 | { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 622 | const struct rb_root *root = &dso->data.cache; |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 623 | struct rb_node * const *p = &root->rb_node; |
| 624 | const struct rb_node *parent = NULL; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 625 | struct dso_cache *cache; |
| 626 | |
| 627 | while (*p != NULL) { |
| 628 | u64 end; |
| 629 | |
| 630 | parent = *p; |
| 631 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 632 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 633 | |
| 634 | if (offset < cache->offset) |
| 635 | p = &(*p)->rb_left; |
| 636 | else if (offset >= end) |
| 637 | p = &(*p)->rb_right; |
| 638 | else |
| 639 | return cache; |
| 640 | } |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 641 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 642 | return NULL; |
| 643 | } |
| 644 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 645 | static struct dso_cache * |
| 646 | dso_cache__insert(struct dso *dso, struct dso_cache *new) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 647 | { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 648 | struct rb_root *root = &dso->data.cache; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 649 | struct rb_node **p = &root->rb_node; |
| 650 | struct rb_node *parent = NULL; |
| 651 | struct dso_cache *cache; |
| 652 | u64 offset = new->offset; |
| 653 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 654 | pthread_mutex_lock(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 655 | while (*p != NULL) { |
| 656 | u64 end; |
| 657 | |
| 658 | parent = *p; |
| 659 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 660 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 661 | |
| 662 | if (offset < cache->offset) |
| 663 | p = &(*p)->rb_left; |
| 664 | else if (offset >= end) |
| 665 | p = &(*p)->rb_right; |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 666 | else |
| 667 | goto out; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | rb_link_node(&new->rb_node, parent, p); |
| 671 | rb_insert_color(&new->rb_node, root); |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 672 | |
| 673 | cache = NULL; |
| 674 | out: |
| 675 | pthread_mutex_unlock(&dso->lock); |
| 676 | return cache; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | static ssize_t |
| 680 | dso_cache__memcpy(struct dso_cache *cache, u64 offset, |
| 681 | u8 *data, u64 size) |
| 682 | { |
| 683 | u64 cache_offset = offset - cache->offset; |
| 684 | u64 cache_size = min(cache->size - cache_offset, size); |
| 685 | |
| 686 | memcpy(data, cache->data + cache_offset, cache_size); |
| 687 | return cache_size; |
| 688 | } |
| 689 | |
| 690 | static ssize_t |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 691 | dso_cache__read(struct dso *dso, struct machine *machine, |
| 692 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 693 | { |
| 694 | struct dso_cache *cache; |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 695 | struct dso_cache *old; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 696 | ssize_t ret; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 697 | |
| 698 | do { |
| 699 | u64 cache_offset; |
| 700 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 701 | cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); |
| 702 | if (!cache) |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 703 | return -ENOMEM; |
| 704 | |
| 705 | pthread_mutex_lock(&dso__data_open_lock); |
| 706 | |
| 707 | /* |
| 708 | * dso->data.fd might be closed if other thread opened another |
| 709 | * file (dso) due to open file limit (RLIMIT_NOFILE). |
| 710 | */ |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 711 | try_to_open_dso(dso, machine); |
| 712 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 713 | if (dso->data.fd < 0) { |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 714 | ret = -errno; |
| 715 | dso->data.status = DSO_DATA_STATUS_ERROR; |
| 716 | break; |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 717 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 718 | |
| 719 | cache_offset = offset & DSO__DATA_CACHE_MASK; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 720 | |
Namhyung Kim | c52686f | 2015-01-29 17:02:01 -0300 | [diff] [blame] | 721 | ret = pread(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE, cache_offset); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 722 | if (ret <= 0) |
| 723 | break; |
| 724 | |
| 725 | cache->offset = cache_offset; |
| 726 | cache->size = ret; |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 727 | } while (0); |
| 728 | |
| 729 | pthread_mutex_unlock(&dso__data_open_lock); |
| 730 | |
| 731 | if (ret > 0) { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 732 | old = dso_cache__insert(dso, cache); |
| 733 | if (old) { |
| 734 | /* we lose the race */ |
| 735 | free(cache); |
| 736 | cache = old; |
| 737 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 738 | |
| 739 | ret = dso_cache__memcpy(cache, offset, data, size); |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 740 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 741 | |
| 742 | if (ret <= 0) |
| 743 | free(cache); |
| 744 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 745 | return ret; |
| 746 | } |
| 747 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 748 | static ssize_t dso_cache_read(struct dso *dso, struct machine *machine, |
| 749 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 750 | { |
| 751 | struct dso_cache *cache; |
| 752 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 753 | cache = dso_cache__find(dso, offset); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 754 | if (cache) |
| 755 | return dso_cache__memcpy(cache, offset, data, size); |
| 756 | else |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 757 | return dso_cache__read(dso, machine, offset, data, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 760 | /* |
| 761 | * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks |
| 762 | * in the rb_tree. Any read to already cached data is served |
| 763 | * by cached data. |
| 764 | */ |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 765 | static ssize_t cached_read(struct dso *dso, struct machine *machine, |
| 766 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 767 | { |
| 768 | ssize_t r = 0; |
| 769 | u8 *p = data; |
| 770 | |
| 771 | do { |
| 772 | ssize_t ret; |
| 773 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 774 | ret = dso_cache_read(dso, machine, offset, p, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 775 | if (ret < 0) |
| 776 | return ret; |
| 777 | |
| 778 | /* Reached EOF, return what we have. */ |
| 779 | if (!ret) |
| 780 | break; |
| 781 | |
| 782 | BUG_ON(ret > size); |
| 783 | |
| 784 | r += ret; |
| 785 | p += ret; |
| 786 | offset += ret; |
| 787 | size -= ret; |
| 788 | |
| 789 | } while (size); |
| 790 | |
| 791 | return r; |
| 792 | } |
| 793 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 794 | static int data_file_size(struct dso *dso, struct machine *machine) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 795 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 796 | int ret = 0; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 797 | struct stat st; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 798 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 799 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 800 | if (dso->data.file_size) |
| 801 | return 0; |
| 802 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 803 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
| 804 | return -1; |
| 805 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 806 | pthread_mutex_lock(&dso__data_open_lock); |
| 807 | |
| 808 | /* |
| 809 | * dso->data.fd might be closed if other thread opened another |
| 810 | * file (dso) due to open file limit (RLIMIT_NOFILE). |
| 811 | */ |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 812 | try_to_open_dso(dso, machine); |
| 813 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 814 | if (dso->data.fd < 0) { |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 815 | ret = -errno; |
| 816 | dso->data.status = DSO_DATA_STATUS_ERROR; |
| 817 | goto out; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 818 | } |
| 819 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 820 | if (fstat(dso->data.fd, &st) < 0) { |
| 821 | ret = -errno; |
| 822 | pr_err("dso cache fstat failed: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 823 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 824 | dso->data.status = DSO_DATA_STATUS_ERROR; |
| 825 | goto out; |
| 826 | } |
| 827 | dso->data.file_size = st.st_size; |
| 828 | |
| 829 | out: |
| 830 | pthread_mutex_unlock(&dso__data_open_lock); |
| 831 | return ret; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 832 | } |
| 833 | |
Adrian Hunter | 6d36345 | 2014-07-22 16:17:35 +0300 | [diff] [blame] | 834 | /** |
| 835 | * dso__data_size - Return dso data size |
| 836 | * @dso: dso object |
| 837 | * @machine: machine object |
| 838 | * |
| 839 | * Return: dso data size |
| 840 | */ |
| 841 | off_t dso__data_size(struct dso *dso, struct machine *machine) |
| 842 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 843 | if (data_file_size(dso, machine)) |
Adrian Hunter | 6d36345 | 2014-07-22 16:17:35 +0300 | [diff] [blame] | 844 | return -1; |
| 845 | |
| 846 | /* For now just estimate dso data size is close to file size */ |
| 847 | return dso->data.file_size; |
| 848 | } |
| 849 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 850 | static ssize_t data_read_offset(struct dso *dso, struct machine *machine, |
| 851 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 852 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 853 | if (data_file_size(dso, machine)) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 854 | return -1; |
| 855 | |
| 856 | /* Check the offset sanity. */ |
| 857 | if (offset > dso->data.file_size) |
| 858 | return -1; |
| 859 | |
| 860 | if (offset + size < offset) |
| 861 | return -1; |
| 862 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 863 | return cached_read(dso, machine, offset, data, size); |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 864 | } |
| 865 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 866 | /** |
| 867 | * dso__data_read_offset - Read data from dso file offset |
| 868 | * @dso: dso object |
| 869 | * @machine: machine object |
| 870 | * @offset: file offset |
| 871 | * @data: buffer to store data |
| 872 | * @size: size of the @data buffer |
| 873 | * |
| 874 | * External interface to read data from dso file offset. Open |
| 875 | * dso data file and use cached_read to get the data. |
| 876 | */ |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 877 | ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, |
| 878 | u64 offset, u8 *data, ssize_t size) |
| 879 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 880 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 881 | return -1; |
| 882 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 883 | return data_read_offset(dso, machine, offset, data, size); |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 886 | /** |
| 887 | * dso__data_read_addr - Read data from dso address |
| 888 | * @dso: dso object |
| 889 | * @machine: machine object |
| 890 | * @add: virtual memory address |
| 891 | * @data: buffer to store data |
| 892 | * @size: size of the @data buffer |
| 893 | * |
| 894 | * External interface to read data from dso address. |
| 895 | */ |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 896 | ssize_t dso__data_read_addr(struct dso *dso, struct map *map, |
| 897 | struct machine *machine, u64 addr, |
| 898 | u8 *data, ssize_t size) |
| 899 | { |
| 900 | u64 offset = map->map_ip(map, addr); |
| 901 | return dso__data_read_offset(dso, machine, offset, data, size); |
| 902 | } |
| 903 | |
| 904 | struct map *dso__new_map(const char *name) |
| 905 | { |
| 906 | struct map *map = NULL; |
| 907 | struct dso *dso = dso__new(name); |
| 908 | |
| 909 | if (dso) |
| 910 | map = map__new2(0, dso, MAP__FUNCTION); |
| 911 | |
| 912 | return map; |
| 913 | } |
| 914 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 915 | struct dso *machine__findnew_kernel(struct machine *machine, const char *name, |
| 916 | const char *short_name, int dso_type) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 917 | { |
| 918 | /* |
| 919 | * The kernel dso could be created by build_id processing. |
| 920 | */ |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 921 | struct dso *dso = machine__findnew_dso(machine, name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 922 | |
| 923 | /* |
| 924 | * We need to run this in all cases, since during the build_id |
| 925 | * processing we had no idea this was the kernel dso. |
| 926 | */ |
| 927 | if (dso != NULL) { |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 928 | dso__set_short_name(dso, short_name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 929 | dso->kernel = dso_type; |
| 930 | } |
| 931 | |
| 932 | return dso; |
| 933 | } |
| 934 | |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 935 | /* |
| 936 | * Find a matching entry and/or link current entry to RB tree. |
| 937 | * Either one of the dso or name parameter must be non-NULL or the |
| 938 | * function will not work. |
| 939 | */ |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 940 | static struct dso *__dso__findlink_by_longname(struct rb_root *root, |
| 941 | struct dso *dso, const char *name) |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 942 | { |
| 943 | struct rb_node **p = &root->rb_node; |
| 944 | struct rb_node *parent = NULL; |
| 945 | |
| 946 | if (!name) |
| 947 | name = dso->long_name; |
| 948 | /* |
| 949 | * Find node with the matching name |
| 950 | */ |
| 951 | while (*p) { |
| 952 | struct dso *this = rb_entry(*p, struct dso, rb_node); |
| 953 | int rc = strcmp(name, this->long_name); |
| 954 | |
| 955 | parent = *p; |
| 956 | if (rc == 0) { |
| 957 | /* |
| 958 | * In case the new DSO is a duplicate of an existing |
Masahiro Yamada | 0f5e155 | 2017-02-27 14:28:52 -0800 | [diff] [blame] | 959 | * one, print a one-time warning & put the new entry |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 960 | * at the end of the list of duplicates. |
| 961 | */ |
| 962 | if (!dso || (dso == this)) |
| 963 | return this; /* Find matching dso */ |
| 964 | /* |
| 965 | * The core kernel DSOs may have duplicated long name. |
| 966 | * In this case, the short name should be different. |
| 967 | * Comparing the short names to differentiate the DSOs. |
| 968 | */ |
| 969 | rc = strcmp(dso->short_name, this->short_name); |
| 970 | if (rc == 0) { |
| 971 | pr_err("Duplicated dso name: %s\n", name); |
| 972 | return NULL; |
| 973 | } |
| 974 | } |
| 975 | if (rc < 0) |
| 976 | p = &parent->rb_left; |
| 977 | else |
| 978 | p = &parent->rb_right; |
| 979 | } |
| 980 | if (dso) { |
| 981 | /* Add new node and rebalance tree */ |
| 982 | rb_link_node(&dso->rb_node, parent, p); |
| 983 | rb_insert_color(&dso->rb_node, root); |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 984 | dso->root = root; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 985 | } |
| 986 | return NULL; |
| 987 | } |
| 988 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 989 | static inline struct dso *__dso__find_by_longname(struct rb_root *root, |
| 990 | const char *name) |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 991 | { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 992 | return __dso__findlink_by_longname(root, NULL, name); |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 993 | } |
| 994 | |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 995 | void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 996 | { |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 997 | struct rb_root *root = dso->root; |
| 998 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 999 | if (name == NULL) |
| 1000 | return; |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1001 | |
| 1002 | if (dso->long_name_allocated) |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 1003 | free((char *)dso->long_name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1004 | |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1005 | if (root) { |
| 1006 | rb_erase(&dso->rb_node, root); |
| 1007 | /* |
| 1008 | * __dso__findlink_by_longname() isn't guaranteed to add it |
| 1009 | * back, so a clean removal is required here. |
| 1010 | */ |
| 1011 | RB_CLEAR_NODE(&dso->rb_node); |
| 1012 | dso->root = NULL; |
| 1013 | } |
| 1014 | |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1015 | dso->long_name = name; |
| 1016 | dso->long_name_len = strlen(name); |
| 1017 | dso->long_name_allocated = name_allocated; |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1018 | |
| 1019 | if (root) |
| 1020 | __dso__findlink_by_longname(root, dso, NULL); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1023 | void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1024 | { |
| 1025 | if (name == NULL) |
| 1026 | return; |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1027 | |
| 1028 | if (dso->short_name_allocated) |
| 1029 | free((char *)dso->short_name); |
| 1030 | |
| 1031 | dso->short_name = name; |
| 1032 | dso->short_name_len = strlen(name); |
| 1033 | dso->short_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static void dso__set_basename(struct dso *dso) |
| 1037 | { |
Stephane Eranian | ac5e7f8 | 2013-12-05 19:26:42 +0100 | [diff] [blame] | 1038 | /* |
| 1039 | * basename() may modify path buffer, so we must pass |
| 1040 | * a copy. |
| 1041 | */ |
| 1042 | char *base, *lname = strdup(dso->long_name); |
| 1043 | |
| 1044 | if (!lname) |
| 1045 | return; |
| 1046 | |
| 1047 | /* |
| 1048 | * basename() may return a pointer to internal |
| 1049 | * storage which is reused in subsequent calls |
| 1050 | * so copy the result. |
| 1051 | */ |
| 1052 | base = strdup(basename(lname)); |
| 1053 | |
| 1054 | free(lname); |
| 1055 | |
| 1056 | if (!base) |
| 1057 | return; |
| 1058 | |
| 1059 | dso__set_short_name(dso, base, true); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | int dso__name_len(const struct dso *dso) |
| 1063 | { |
| 1064 | if (!dso) |
| 1065 | return strlen("[unknown]"); |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 1066 | if (verbose > 0) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1067 | return dso->long_name_len; |
| 1068 | |
| 1069 | return dso->short_name_len; |
| 1070 | } |
| 1071 | |
| 1072 | bool dso__loaded(const struct dso *dso, enum map_type type) |
| 1073 | { |
| 1074 | return dso->loaded & (1 << type); |
| 1075 | } |
| 1076 | |
| 1077 | bool dso__sorted_by_name(const struct dso *dso, enum map_type type) |
| 1078 | { |
| 1079 | return dso->sorted_by_name & (1 << type); |
| 1080 | } |
| 1081 | |
| 1082 | void dso__set_sorted_by_name(struct dso *dso, enum map_type type) |
| 1083 | { |
| 1084 | dso->sorted_by_name |= (1 << type); |
| 1085 | } |
| 1086 | |
| 1087 | struct dso *dso__new(const char *name) |
| 1088 | { |
| 1089 | struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1); |
| 1090 | |
| 1091 | if (dso != NULL) { |
| 1092 | int i; |
| 1093 | strcpy(dso->name, name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1094 | dso__set_long_name(dso, dso->name, false); |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1095 | dso__set_short_name(dso, dso->name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1096 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1097 | dso->symbols[i] = dso->symbol_names[i] = RB_ROOT; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 1098 | dso->data.cache = RB_ROOT; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 1099 | dso->data.fd = -1; |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 1100 | dso->data.status = DSO_DATA_STATUS_UNKNOWN; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1101 | dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1102 | dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 1103 | dso->is_64_bit = (sizeof(void *) == 8); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1104 | dso->loaded = 0; |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 1105 | dso->rel = 0; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1106 | dso->sorted_by_name = 0; |
| 1107 | dso->has_build_id = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 1108 | dso->has_srcline = 1; |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 1109 | dso->a2l_fails = 1; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1110 | dso->kernel = DSO_TYPE_USER; |
| 1111 | dso->needs_swap = DSO_SWAP__UNSET; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1112 | RB_CLEAR_NODE(&dso->rb_node); |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1113 | dso->root = NULL; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1114 | INIT_LIST_HEAD(&dso->node); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 1115 | INIT_LIST_HEAD(&dso->data.open_entry); |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1116 | pthread_mutex_init(&dso->lock, NULL); |
Elena Reshetova | 7100810 | 2017-02-21 17:34:58 +0200 | [diff] [blame] | 1117 | refcount_set(&dso->refcnt, 1); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | return dso; |
| 1121 | } |
| 1122 | |
| 1123 | void dso__delete(struct dso *dso) |
| 1124 | { |
| 1125 | int i; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1126 | |
| 1127 | if (!RB_EMPTY_NODE(&dso->rb_node)) |
| 1128 | pr_err("DSO %s is still in rbtree when being deleted!\n", |
| 1129 | dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1130 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1131 | symbols__delete(&dso->symbols[i]); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 1132 | |
| 1133 | if (dso->short_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1134 | zfree((char **)&dso->short_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 1135 | dso->short_name_allocated = false; |
| 1136 | } |
| 1137 | |
| 1138 | if (dso->long_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1139 | zfree((char **)&dso->long_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 1140 | dso->long_name_allocated = false; |
| 1141 | } |
| 1142 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 1143 | dso__data_close(dso); |
Adrian Hunter | cfe9174 | 2015-04-09 18:53:55 +0300 | [diff] [blame] | 1144 | auxtrace_cache__free(dso->auxtrace_cache); |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 1145 | dso_cache__free(dso); |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 1146 | dso__free_a2l(dso); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1147 | zfree(&dso->symsrc_filename); |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1148 | pthread_mutex_destroy(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1149 | free(dso); |
| 1150 | } |
| 1151 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1152 | struct dso *dso__get(struct dso *dso) |
| 1153 | { |
| 1154 | if (dso) |
Elena Reshetova | 7100810 | 2017-02-21 17:34:58 +0200 | [diff] [blame] | 1155 | refcount_inc(&dso->refcnt); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1156 | return dso; |
| 1157 | } |
| 1158 | |
| 1159 | void dso__put(struct dso *dso) |
| 1160 | { |
Elena Reshetova | 7100810 | 2017-02-21 17:34:58 +0200 | [diff] [blame] | 1161 | if (dso && refcount_dec_and_test(&dso->refcnt)) |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1162 | dso__delete(dso); |
| 1163 | } |
| 1164 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1165 | void dso__set_build_id(struct dso *dso, void *build_id) |
| 1166 | { |
| 1167 | memcpy(dso->build_id, build_id, sizeof(dso->build_id)); |
| 1168 | dso->has_build_id = 1; |
| 1169 | } |
| 1170 | |
| 1171 | bool dso__build_id_equal(const struct dso *dso, u8 *build_id) |
| 1172 | { |
| 1173 | return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0; |
| 1174 | } |
| 1175 | |
| 1176 | void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) |
| 1177 | { |
| 1178 | char path[PATH_MAX]; |
| 1179 | |
| 1180 | if (machine__is_default_guest(machine)) |
| 1181 | return; |
| 1182 | sprintf(path, "%s/sys/kernel/notes", machine->root_dir); |
| 1183 | if (sysfs__read_build_id(path, dso->build_id, |
| 1184 | sizeof(dso->build_id)) == 0) |
| 1185 | dso->has_build_id = true; |
| 1186 | } |
| 1187 | |
| 1188 | int dso__kernel_module_get_build_id(struct dso *dso, |
| 1189 | const char *root_dir) |
| 1190 | { |
| 1191 | char filename[PATH_MAX]; |
| 1192 | /* |
| 1193 | * kernel module short names are of the form "[module]" and |
| 1194 | * we need just "module" here. |
| 1195 | */ |
| 1196 | const char *name = dso->short_name + 1; |
| 1197 | |
| 1198 | snprintf(filename, sizeof(filename), |
| 1199 | "%s/sys/module/%.*s/notes/.note.gnu.build-id", |
| 1200 | root_dir, (int)strlen(name) - 1, name); |
| 1201 | |
| 1202 | if (sysfs__read_build_id(filename, dso->build_id, |
| 1203 | sizeof(dso->build_id)) == 0) |
| 1204 | dso->has_build_id = true; |
| 1205 | |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | bool __dsos__read_build_ids(struct list_head *head, bool with_hits) |
| 1210 | { |
| 1211 | bool have_build_id = false; |
| 1212 | struct dso *pos; |
| 1213 | |
| 1214 | list_for_each_entry(pos, head, node) { |
He Kuang | 6ae98ba | 2016-05-12 08:43:11 +0000 | [diff] [blame] | 1215 | if (with_hits && !pos->hit && !dso__is_vdso(pos)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1216 | continue; |
| 1217 | if (pos->has_build_id) { |
| 1218 | have_build_id = true; |
| 1219 | continue; |
| 1220 | } |
| 1221 | if (filename__read_build_id(pos->long_name, pos->build_id, |
| 1222 | sizeof(pos->build_id)) > 0) { |
| 1223 | have_build_id = true; |
| 1224 | pos->has_build_id = true; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | return have_build_id; |
| 1229 | } |
| 1230 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1231 | void __dsos__add(struct dsos *dsos, struct dso *dso) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1232 | { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 1233 | list_add_tail(&dso->node, &dsos->head); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1234 | __dso__findlink_by_longname(&dsos->root, dso, NULL); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1235 | /* |
| 1236 | * It is now in the linked list, grab a reference, then garbage collect |
| 1237 | * this when needing memory, by looking at LRU dso instances in the |
| 1238 | * list with atomic_read(&dso->refcnt) == 1, i.e. no references |
| 1239 | * anywhere besides the one for the list, do, under a lock for the |
| 1240 | * list: remove it from the list, then a dso__put(), that probably will |
| 1241 | * be the last and will then call dso__delete(), end of life. |
| 1242 | * |
| 1243 | * That, or at the end of the 'struct machine' lifetime, when all |
| 1244 | * 'struct dso' instances will be removed from the list, in |
| 1245 | * dsos__exit(), if they have no other reference from some other data |
| 1246 | * structure. |
| 1247 | * |
| 1248 | * E.g.: after processing a 'perf.data' file and storing references |
| 1249 | * to objects instantiated while processing events, we will have |
| 1250 | * references to the 'thread', 'map', 'dso' structs all from 'struct |
| 1251 | * hist_entry' instances, but we may not need anything not referenced, |
| 1252 | * so we might as well call machines__exit()/machines__delete() and |
| 1253 | * garbage collect it. |
| 1254 | */ |
| 1255 | dso__get(dso); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1256 | } |
| 1257 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1258 | void dsos__add(struct dsos *dsos, struct dso *dso) |
| 1259 | { |
| 1260 | pthread_rwlock_wrlock(&dsos->lock); |
| 1261 | __dsos__add(dsos, dso); |
| 1262 | pthread_rwlock_unlock(&dsos->lock); |
| 1263 | } |
| 1264 | |
| 1265 | struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1266 | { |
| 1267 | struct dso *pos; |
| 1268 | |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 1269 | if (cmp_short) { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 1270 | list_for_each_entry(pos, &dsos->head, node) |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 1271 | if (strcmp(pos->short_name, name) == 0) |
| 1272 | return pos; |
| 1273 | return NULL; |
| 1274 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1275 | return __dso__find_by_longname(&dsos->root, name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1276 | } |
| 1277 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1278 | struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) |
| 1279 | { |
| 1280 | struct dso *dso; |
| 1281 | pthread_rwlock_rdlock(&dsos->lock); |
| 1282 | dso = __dsos__find(dsos, name, cmp_short); |
| 1283 | pthread_rwlock_unlock(&dsos->lock); |
| 1284 | return dso; |
| 1285 | } |
| 1286 | |
| 1287 | struct dso *__dsos__addnew(struct dsos *dsos, const char *name) |
Jiri Olsa | 701d8d7 | 2015-02-12 22:06:09 +0100 | [diff] [blame] | 1288 | { |
| 1289 | struct dso *dso = dso__new(name); |
| 1290 | |
| 1291 | if (dso != NULL) { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1292 | __dsos__add(dsos, dso); |
Jiri Olsa | 701d8d7 | 2015-02-12 22:06:09 +0100 | [diff] [blame] | 1293 | dso__set_basename(dso); |
Masami Hiramatsu | 82de26a | 2015-11-18 15:40:31 +0900 | [diff] [blame] | 1294 | /* Put dso here because __dsos_add already got it */ |
| 1295 | dso__put(dso); |
Jiri Olsa | 701d8d7 | 2015-02-12 22:06:09 +0100 | [diff] [blame] | 1296 | } |
| 1297 | return dso; |
| 1298 | } |
| 1299 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 1300 | struct dso *__dsos__findnew(struct dsos *dsos, const char *name) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1301 | { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1302 | struct dso *dso = __dsos__find(dsos, name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1303 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1304 | return dso ? dso : __dsos__addnew(dsos, name); |
| 1305 | } |
| 1306 | |
| 1307 | struct dso *dsos__findnew(struct dsos *dsos, const char *name) |
| 1308 | { |
| 1309 | struct dso *dso; |
| 1310 | pthread_rwlock_wrlock(&dsos->lock); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1311 | dso = dso__get(__dsos__findnew(dsos, name)); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1312 | pthread_rwlock_unlock(&dsos->lock); |
| 1313 | return dso; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | size_t __dsos__fprintf_buildid(struct list_head *head, FILE *fp, |
Arnaldo Carvalho de Melo | 417c2ff | 2012-12-07 09:53:58 -0300 | [diff] [blame] | 1317 | bool (skip)(struct dso *dso, int parm), int parm) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1318 | { |
| 1319 | struct dso *pos; |
| 1320 | size_t ret = 0; |
| 1321 | |
| 1322 | list_for_each_entry(pos, head, node) { |
Arnaldo Carvalho de Melo | 417c2ff | 2012-12-07 09:53:58 -0300 | [diff] [blame] | 1323 | if (skip && skip(pos, parm)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1324 | continue; |
| 1325 | ret += dso__fprintf_buildid(pos, fp); |
| 1326 | ret += fprintf(fp, " %s\n", pos->long_name); |
| 1327 | } |
| 1328 | return ret; |
| 1329 | } |
| 1330 | |
| 1331 | size_t __dsos__fprintf(struct list_head *head, FILE *fp) |
| 1332 | { |
| 1333 | struct dso *pos; |
| 1334 | size_t ret = 0; |
| 1335 | |
| 1336 | list_for_each_entry(pos, head, node) { |
| 1337 | int i; |
| 1338 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1339 | ret += dso__fprintf(pos, i, fp); |
| 1340 | } |
| 1341 | |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
| 1345 | size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) |
| 1346 | { |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 1347 | char sbuild_id[SBUILD_ID_SIZE]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1348 | |
| 1349 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 1350 | return fprintf(fp, "%s", sbuild_id); |
| 1351 | } |
| 1352 | |
| 1353 | size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp) |
| 1354 | { |
| 1355 | struct rb_node *nd; |
| 1356 | size_t ret = fprintf(fp, "dso: %s (", dso->short_name); |
| 1357 | |
| 1358 | if (dso->short_name != dso->long_name) |
| 1359 | ret += fprintf(fp, "%s, ", dso->long_name); |
| 1360 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], |
Stephane Eranian | 919d590 | 2012-11-20 10:51:02 +0100 | [diff] [blame] | 1361 | dso__loaded(dso, type) ? "" : "NOT "); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1362 | ret += dso__fprintf_buildid(dso, fp); |
| 1363 | ret += fprintf(fp, ")\n"); |
| 1364 | for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) { |
| 1365 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 1366 | ret += symbol__fprintf(pos, fp); |
| 1367 | } |
| 1368 | |
| 1369 | return ret; |
| 1370 | } |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1371 | |
| 1372 | enum dso_type dso__type(struct dso *dso, struct machine *machine) |
| 1373 | { |
| 1374 | int fd; |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 1375 | enum dso_type type = DSO__TYPE_UNKNOWN; |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1376 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 1377 | fd = dso__data_get_fd(dso, machine); |
| 1378 | if (fd >= 0) { |
| 1379 | type = dso__type_fd(fd); |
| 1380 | dso__data_put_fd(dso); |
| 1381 | } |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1382 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 1383 | return type; |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1384 | } |
Arnaldo Carvalho de Melo | 18425f1 | 2015-03-24 11:49:02 -0300 | [diff] [blame] | 1385 | |
| 1386 | int dso__strerror_load(struct dso *dso, char *buf, size_t buflen) |
| 1387 | { |
| 1388 | int idx, errnum = dso->load_errno; |
| 1389 | /* |
| 1390 | * This must have a same ordering as the enum dso_load_errno. |
| 1391 | */ |
| 1392 | static const char *dso_load__error_str[] = { |
| 1393 | "Internal tools/perf/ library error", |
| 1394 | "Invalid ELF file", |
| 1395 | "Can not read build id", |
| 1396 | "Mismatching build id", |
| 1397 | "Decompression failure", |
| 1398 | }; |
| 1399 | |
| 1400 | BUG_ON(buflen == 0); |
| 1401 | |
| 1402 | if (errnum >= 0) { |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 1403 | const char *err = str_error_r(errnum, buf, buflen); |
Arnaldo Carvalho de Melo | 18425f1 | 2015-03-24 11:49:02 -0300 | [diff] [blame] | 1404 | |
| 1405 | if (err != buf) |
| 1406 | scnprintf(buf, buflen, "%s", err); |
| 1407 | |
| 1408 | return 0; |
| 1409 | } |
| 1410 | |
| 1411 | if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END) |
| 1412 | return -1; |
| 1413 | |
| 1414 | idx = errnum - __DSO_LOAD_ERRNO__START; |
| 1415 | scnprintf(buf, buflen, "%s", dso_load__error_str[idx]); |
| 1416 | return 0; |
| 1417 | } |