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