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