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