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