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