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