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', |
| 24 | [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g', |
| 25 | [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G', |
| 26 | [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V', |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND) |
| 30 | return '!'; |
| 31 | return origin[dso->symtab_type]; |
| 32 | } |
| 33 | |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 34 | int dso__read_binary_type_filename(const struct dso *dso, |
| 35 | enum dso_binary_type type, |
| 36 | char *root_dir, char *filename, size_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 37 | { |
| 38 | char build_id_hex[BUILD_ID_SIZE * 2 + 1]; |
| 39 | int ret = 0; |
| 40 | |
| 41 | switch (type) { |
| 42 | case DSO_BINARY_TYPE__DEBUGLINK: { |
| 43 | char *debuglink; |
| 44 | |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 45 | strncpy(filename, dso->long_name, size); |
| 46 | debuglink = filename + dso->long_name_len; |
| 47 | while (debuglink != filename && *debuglink != '/') |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 48 | debuglink--; |
| 49 | if (*debuglink == '/') |
| 50 | debuglink++; |
Stephane Eranian | 0d3dc5e | 2014-02-20 10:32:55 +0900 | [diff] [blame] | 51 | ret = filename__read_debuglink(dso->long_name, debuglink, |
| 52 | size - (debuglink - filename)); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 53 | } |
| 54 | break; |
| 55 | case DSO_BINARY_TYPE__BUILD_ID_CACHE: |
| 56 | /* skip the locally configured cache if a symfs is given */ |
| 57 | if (symbol_conf.symfs[0] || |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 58 | (dso__build_id_filename(dso, filename, size) == NULL)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 59 | ret = -1; |
| 60 | break; |
| 61 | |
| 62 | case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 63 | snprintf(filename, size, "%s/usr/lib/debug%s.debug", |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 64 | symbol_conf.symfs, dso->long_name); |
| 65 | break; |
| 66 | |
| 67 | case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 68 | snprintf(filename, size, "%s/usr/lib/debug%s", |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 69 | symbol_conf.symfs, dso->long_name); |
| 70 | break; |
| 71 | |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 72 | case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: |
| 73 | { |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 74 | const char *last_slash; |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 75 | size_t len; |
| 76 | size_t dir_size; |
| 77 | |
| 78 | last_slash = dso->long_name + dso->long_name_len; |
| 79 | while (last_slash != dso->long_name && *last_slash != '/') |
| 80 | last_slash--; |
| 81 | |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 82 | len = scnprintf(filename, size, "%s", symbol_conf.symfs); |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 83 | dir_size = last_slash - dso->long_name + 2; |
| 84 | if (dir_size > (size - len)) { |
| 85 | ret = -1; |
| 86 | break; |
| 87 | } |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 88 | len += scnprintf(filename + len, dir_size, "%s", dso->long_name); |
| 89 | len += scnprintf(filename + len , size - len, ".debug%s", |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 90 | last_slash); |
| 91 | break; |
| 92 | } |
| 93 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 94 | case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: |
| 95 | if (!dso->has_build_id) { |
| 96 | ret = -1; |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | build_id__sprintf(dso->build_id, |
| 101 | sizeof(dso->build_id), |
| 102 | build_id_hex); |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 103 | snprintf(filename, size, |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 104 | "%s/usr/lib/debug/.build-id/%.2s/%s.debug", |
| 105 | symbol_conf.symfs, build_id_hex, build_id_hex + 2); |
| 106 | break; |
| 107 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 108 | case DSO_BINARY_TYPE__VMLINUX: |
| 109 | case DSO_BINARY_TYPE__GUEST_VMLINUX: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 110 | case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 111 | snprintf(filename, size, "%s%s", |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 112 | symbol_conf.symfs, dso->long_name); |
| 113 | break; |
| 114 | |
| 115 | case DSO_BINARY_TYPE__GUEST_KMODULE: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 116 | snprintf(filename, size, "%s%s%s", symbol_conf.symfs, |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 117 | root_dir, dso->long_name); |
| 118 | break; |
| 119 | |
| 120 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 121 | snprintf(filename, size, "%s%s", symbol_conf.symfs, |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 122 | dso->long_name); |
| 123 | break; |
| 124 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 125 | case DSO_BINARY_TYPE__KCORE: |
| 126 | case DSO_BINARY_TYPE__GUEST_KCORE: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 127 | snprintf(filename, size, "%s", dso->long_name); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 128 | break; |
| 129 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 130 | default: |
| 131 | case DSO_BINARY_TYPE__KALLSYMS: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 132 | case DSO_BINARY_TYPE__GUEST_KALLSYMS: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 133 | case DSO_BINARY_TYPE__JAVA_JIT: |
| 134 | case DSO_BINARY_TYPE__NOT_FOUND: |
| 135 | ret = -1; |
| 136 | break; |
| 137 | } |
| 138 | |
| 139 | return ret; |
| 140 | } |
| 141 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 142 | /* |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 143 | * Global list of open DSOs and the counter. |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 144 | */ |
| 145 | static LIST_HEAD(dso__data_open); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 146 | static long dso__data_open_cnt; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 147 | |
| 148 | static void dso__list_add(struct dso *dso) |
| 149 | { |
| 150 | list_add_tail(&dso->data.open_entry, &dso__data_open); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 151 | dso__data_open_cnt++; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void dso__list_del(struct dso *dso) |
| 155 | { |
| 156 | list_del(&dso->data.open_entry); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 157 | WARN_ONCE(dso__data_open_cnt <= 0, |
| 158 | "DSO data fd counter out of bounds."); |
| 159 | dso__data_open_cnt--; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 162 | static void close_first_dso(void); |
| 163 | |
| 164 | static int do_open(char *name) |
| 165 | { |
| 166 | int fd; |
| 167 | |
| 168 | do { |
| 169 | fd = open(name, O_RDONLY); |
| 170 | if (fd >= 0) |
| 171 | return fd; |
| 172 | |
| 173 | pr_debug("dso open failed, mmap: %s\n", strerror(errno)); |
| 174 | if (!dso__data_open_cnt || errno != EMFILE) |
| 175 | break; |
| 176 | |
| 177 | close_first_dso(); |
| 178 | } while (1); |
| 179 | |
| 180 | return -1; |
| 181 | } |
| 182 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 183 | static int __open_dso(struct dso *dso, struct machine *machine) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 184 | { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 185 | int fd; |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 186 | char *root_dir = (char *)""; |
| 187 | char *name = malloc(PATH_MAX); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 188 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 189 | if (!name) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | if (machine) |
| 193 | root_dir = machine->root_dir; |
| 194 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 195 | if (dso__read_binary_type_filename(dso, dso->binary_type, |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 196 | root_dir, name, PATH_MAX)) { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 197 | free(name); |
| 198 | return -EINVAL; |
| 199 | } |
| 200 | |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 201 | fd = do_open(name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 202 | free(name); |
| 203 | return fd; |
| 204 | } |
| 205 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 206 | static void check_data_close(void); |
| 207 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 208 | /** |
| 209 | * dso_close - Open DSO data file |
| 210 | * @dso: dso object |
| 211 | * |
| 212 | * Open @dso's data file descriptor and updates |
| 213 | * list/count of open DSO objects. |
| 214 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 215 | static int open_dso(struct dso *dso, struct machine *machine) |
| 216 | { |
| 217 | int fd = __open_dso(dso, machine); |
| 218 | |
Adrian Hunter | a6f6ae9 | 2014-07-17 11:43:09 +0300 | [diff] [blame] | 219 | if (fd >= 0) { |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 220 | dso__list_add(dso); |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 221 | /* |
| 222 | * Check if we crossed the allowed number |
| 223 | * of opened DSOs and close one if needed. |
| 224 | */ |
| 225 | check_data_close(); |
| 226 | } |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 227 | |
| 228 | return fd; |
| 229 | } |
| 230 | |
| 231 | static void close_data_fd(struct dso *dso) |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 232 | { |
| 233 | if (dso->data.fd >= 0) { |
| 234 | close(dso->data.fd); |
| 235 | dso->data.fd = -1; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 236 | dso->data.file_size = 0; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 237 | dso__list_del(dso); |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 241 | /** |
| 242 | * dso_close - Close DSO data file |
| 243 | * @dso: dso object |
| 244 | * |
| 245 | * Close @dso's data file descriptor and updates |
| 246 | * list/count of open DSO objects. |
| 247 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 248 | static void close_dso(struct dso *dso) |
| 249 | { |
| 250 | close_data_fd(dso); |
| 251 | } |
| 252 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 253 | static void close_first_dso(void) |
| 254 | { |
| 255 | struct dso *dso; |
| 256 | |
| 257 | dso = list_first_entry(&dso__data_open, struct dso, data.open_entry); |
| 258 | close_dso(dso); |
| 259 | } |
| 260 | |
| 261 | static rlim_t get_fd_limit(void) |
| 262 | { |
| 263 | struct rlimit l; |
| 264 | rlim_t limit = 0; |
| 265 | |
| 266 | /* Allow half of the current open fd limit. */ |
| 267 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { |
| 268 | if (l.rlim_cur == RLIM_INFINITY) |
| 269 | limit = l.rlim_cur; |
| 270 | else |
| 271 | limit = l.rlim_cur / 2; |
| 272 | } else { |
| 273 | pr_err("failed to get fd limit\n"); |
| 274 | limit = 1; |
| 275 | } |
| 276 | |
| 277 | return limit; |
| 278 | } |
| 279 | |
| 280 | static bool may_cache_fd(void) |
| 281 | { |
| 282 | static rlim_t limit; |
| 283 | |
| 284 | if (!limit) |
| 285 | limit = get_fd_limit(); |
| 286 | |
| 287 | if (limit == RLIM_INFINITY) |
| 288 | return true; |
| 289 | |
| 290 | return limit > (rlim_t) dso__data_open_cnt; |
| 291 | } |
| 292 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 293 | /* |
| 294 | * Check and close LRU dso if we crossed allowed limit |
| 295 | * for opened dso file descriptors. The limit is half |
| 296 | * of the RLIMIT_NOFILE files opened. |
| 297 | */ |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 298 | static void check_data_close(void) |
| 299 | { |
| 300 | bool cache_fd = may_cache_fd(); |
| 301 | |
| 302 | if (!cache_fd) |
| 303 | close_first_dso(); |
| 304 | } |
| 305 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 306 | /** |
| 307 | * dso__data_close - Close DSO data file |
| 308 | * @dso: dso object |
| 309 | * |
| 310 | * External interface to close @dso's data file descriptor. |
| 311 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 312 | void dso__data_close(struct dso *dso) |
| 313 | { |
| 314 | close_dso(dso); |
| 315 | } |
| 316 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 317 | /** |
| 318 | * dso__data_fd - Get dso's data file descriptor |
| 319 | * @dso: dso object |
| 320 | * @machine: machine object |
| 321 | * |
| 322 | * External interface to find dso's file, open it and |
| 323 | * returns file descriptor. |
| 324 | */ |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 325 | int dso__data_fd(struct dso *dso, struct machine *machine) |
| 326 | { |
Arnaldo Carvalho de Melo | 631d34b | 2013-12-16 16:57:43 -0300 | [diff] [blame] | 327 | enum dso_binary_type binary_type_data[] = { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 328 | DSO_BINARY_TYPE__BUILD_ID_CACHE, |
| 329 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, |
| 330 | DSO_BINARY_TYPE__NOT_FOUND, |
| 331 | }; |
| 332 | int i = 0; |
| 333 | |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame^] | 334 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
| 335 | return -1; |
| 336 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 337 | if (dso->data.fd >= 0) |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame^] | 338 | goto out; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 339 | |
| 340 | if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) { |
| 341 | dso->data.fd = open_dso(dso, machine); |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame^] | 342 | goto out; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 343 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 344 | |
| 345 | do { |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 346 | dso->binary_type = binary_type_data[i++]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 347 | |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame^] | 348 | dso->data.fd = open_dso(dso, machine); |
| 349 | if (dso->data.fd >= 0) |
| 350 | goto out; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 351 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 352 | } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND); |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame^] | 353 | out: |
| 354 | if (dso->data.fd >= 0) |
| 355 | dso->data.status = DSO_DATA_STATUS_OK; |
| 356 | else |
| 357 | dso->data.status = DSO_DATA_STATUS_ERROR; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 358 | |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame^] | 359 | return dso->data.fd; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void |
| 363 | dso_cache__free(struct rb_root *root) |
| 364 | { |
| 365 | struct rb_node *next = rb_first(root); |
| 366 | |
| 367 | while (next) { |
| 368 | struct dso_cache *cache; |
| 369 | |
| 370 | cache = rb_entry(next, struct dso_cache, rb_node); |
| 371 | next = rb_next(&cache->rb_node); |
| 372 | rb_erase(&cache->rb_node, root); |
| 373 | free(cache); |
| 374 | } |
| 375 | } |
| 376 | |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 377 | 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] | 378 | { |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 379 | struct rb_node * const *p = &root->rb_node; |
| 380 | const struct rb_node *parent = NULL; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 381 | struct dso_cache *cache; |
| 382 | |
| 383 | while (*p != NULL) { |
| 384 | u64 end; |
| 385 | |
| 386 | parent = *p; |
| 387 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 388 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 389 | |
| 390 | if (offset < cache->offset) |
| 391 | p = &(*p)->rb_left; |
| 392 | else if (offset >= end) |
| 393 | p = &(*p)->rb_right; |
| 394 | else |
| 395 | return cache; |
| 396 | } |
| 397 | return NULL; |
| 398 | } |
| 399 | |
| 400 | static void |
| 401 | dso_cache__insert(struct rb_root *root, struct dso_cache *new) |
| 402 | { |
| 403 | struct rb_node **p = &root->rb_node; |
| 404 | struct rb_node *parent = NULL; |
| 405 | struct dso_cache *cache; |
| 406 | u64 offset = new->offset; |
| 407 | |
| 408 | while (*p != NULL) { |
| 409 | u64 end; |
| 410 | |
| 411 | parent = *p; |
| 412 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 413 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 414 | |
| 415 | if (offset < cache->offset) |
| 416 | p = &(*p)->rb_left; |
| 417 | else if (offset >= end) |
| 418 | p = &(*p)->rb_right; |
| 419 | } |
| 420 | |
| 421 | rb_link_node(&new->rb_node, parent, p); |
| 422 | rb_insert_color(&new->rb_node, root); |
| 423 | } |
| 424 | |
| 425 | static ssize_t |
| 426 | dso_cache__memcpy(struct dso_cache *cache, u64 offset, |
| 427 | u8 *data, u64 size) |
| 428 | { |
| 429 | u64 cache_offset = offset - cache->offset; |
| 430 | u64 cache_size = min(cache->size - cache_offset, size); |
| 431 | |
| 432 | memcpy(data, cache->data + cache_offset, cache_size); |
| 433 | return cache_size; |
| 434 | } |
| 435 | |
| 436 | static ssize_t |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 437 | 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] | 438 | { |
| 439 | struct dso_cache *cache; |
| 440 | ssize_t ret; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 441 | |
| 442 | do { |
| 443 | u64 cache_offset; |
| 444 | |
| 445 | ret = -ENOMEM; |
| 446 | |
| 447 | cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); |
| 448 | if (!cache) |
| 449 | break; |
| 450 | |
| 451 | cache_offset = offset & DSO__DATA_CACHE_MASK; |
| 452 | ret = -EINVAL; |
| 453 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 454 | if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 455 | break; |
| 456 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 457 | ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 458 | if (ret <= 0) |
| 459 | break; |
| 460 | |
| 461 | cache->offset = cache_offset; |
| 462 | cache->size = ret; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 463 | dso_cache__insert(&dso->data.cache, cache); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 464 | |
| 465 | ret = dso_cache__memcpy(cache, offset, data, size); |
| 466 | |
| 467 | } while (0); |
| 468 | |
| 469 | if (ret <= 0) |
| 470 | free(cache); |
| 471 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 472 | return ret; |
| 473 | } |
| 474 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 475 | static ssize_t dso_cache_read(struct dso *dso, u64 offset, |
| 476 | u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 477 | { |
| 478 | struct dso_cache *cache; |
| 479 | |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 480 | cache = dso_cache__find(&dso->data.cache, offset); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 481 | if (cache) |
| 482 | return dso_cache__memcpy(cache, offset, data, size); |
| 483 | else |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 484 | return dso_cache__read(dso, offset, data, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 485 | } |
| 486 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 487 | /* |
| 488 | * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks |
| 489 | * in the rb_tree. Any read to already cached data is served |
| 490 | * by cached data. |
| 491 | */ |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 492 | 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] | 493 | { |
| 494 | ssize_t r = 0; |
| 495 | u8 *p = data; |
| 496 | |
| 497 | do { |
| 498 | ssize_t ret; |
| 499 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 500 | ret = dso_cache_read(dso, offset, p, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 501 | if (ret < 0) |
| 502 | return ret; |
| 503 | |
| 504 | /* Reached EOF, return what we have. */ |
| 505 | if (!ret) |
| 506 | break; |
| 507 | |
| 508 | BUG_ON(ret > size); |
| 509 | |
| 510 | r += ret; |
| 511 | p += ret; |
| 512 | offset += ret; |
| 513 | size -= ret; |
| 514 | |
| 515 | } while (size); |
| 516 | |
| 517 | return r; |
| 518 | } |
| 519 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 520 | static int data_file_size(struct dso *dso) |
| 521 | { |
| 522 | struct stat st; |
| 523 | |
| 524 | if (!dso->data.file_size) { |
| 525 | if (fstat(dso->data.fd, &st)) { |
| 526 | pr_err("dso mmap failed, fstat: %s\n", strerror(errno)); |
| 527 | return -1; |
| 528 | } |
| 529 | dso->data.file_size = st.st_size; |
| 530 | } |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static ssize_t data_read_offset(struct dso *dso, u64 offset, |
| 536 | u8 *data, ssize_t size) |
| 537 | { |
| 538 | if (data_file_size(dso)) |
| 539 | return -1; |
| 540 | |
| 541 | /* Check the offset sanity. */ |
| 542 | if (offset > dso->data.file_size) |
| 543 | return -1; |
| 544 | |
| 545 | if (offset + size < offset) |
| 546 | return -1; |
| 547 | |
| 548 | return cached_read(dso, offset, data, size); |
| 549 | } |
| 550 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 551 | /** |
| 552 | * dso__data_read_offset - Read data from dso file offset |
| 553 | * @dso: dso object |
| 554 | * @machine: machine object |
| 555 | * @offset: file offset |
| 556 | * @data: buffer to store data |
| 557 | * @size: size of the @data buffer |
| 558 | * |
| 559 | * External interface to read data from dso file offset. Open |
| 560 | * dso data file and use cached_read to get the data. |
| 561 | */ |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 562 | ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, |
| 563 | u64 offset, u8 *data, ssize_t size) |
| 564 | { |
| 565 | if (dso__data_fd(dso, machine) < 0) |
| 566 | return -1; |
| 567 | |
| 568 | return data_read_offset(dso, offset, data, size); |
| 569 | } |
| 570 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 571 | /** |
| 572 | * dso__data_read_addr - Read data from dso address |
| 573 | * @dso: dso object |
| 574 | * @machine: machine object |
| 575 | * @add: virtual memory address |
| 576 | * @data: buffer to store data |
| 577 | * @size: size of the @data buffer |
| 578 | * |
| 579 | * External interface to read data from dso address. |
| 580 | */ |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 581 | ssize_t dso__data_read_addr(struct dso *dso, struct map *map, |
| 582 | struct machine *machine, u64 addr, |
| 583 | u8 *data, ssize_t size) |
| 584 | { |
| 585 | u64 offset = map->map_ip(map, addr); |
| 586 | return dso__data_read_offset(dso, machine, offset, data, size); |
| 587 | } |
| 588 | |
| 589 | struct map *dso__new_map(const char *name) |
| 590 | { |
| 591 | struct map *map = NULL; |
| 592 | struct dso *dso = dso__new(name); |
| 593 | |
| 594 | if (dso) |
| 595 | map = map__new2(0, dso, MAP__FUNCTION); |
| 596 | |
| 597 | return map; |
| 598 | } |
| 599 | |
| 600 | struct dso *dso__kernel_findnew(struct machine *machine, const char *name, |
| 601 | const char *short_name, int dso_type) |
| 602 | { |
| 603 | /* |
| 604 | * The kernel dso could be created by build_id processing. |
| 605 | */ |
| 606 | struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name); |
| 607 | |
| 608 | /* |
| 609 | * We need to run this in all cases, since during the build_id |
| 610 | * processing we had no idea this was the kernel dso. |
| 611 | */ |
| 612 | if (dso != NULL) { |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 613 | dso__set_short_name(dso, short_name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 614 | dso->kernel = dso_type; |
| 615 | } |
| 616 | |
| 617 | return dso; |
| 618 | } |
| 619 | |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 620 | 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] | 621 | { |
| 622 | if (name == NULL) |
| 623 | return; |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 624 | |
| 625 | if (dso->long_name_allocated) |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 626 | free((char *)dso->long_name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 627 | |
| 628 | dso->long_name = name; |
| 629 | dso->long_name_len = strlen(name); |
| 630 | dso->long_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 631 | } |
| 632 | |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 633 | 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] | 634 | { |
| 635 | if (name == NULL) |
| 636 | return; |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 637 | |
| 638 | if (dso->short_name_allocated) |
| 639 | free((char *)dso->short_name); |
| 640 | |
| 641 | dso->short_name = name; |
| 642 | dso->short_name_len = strlen(name); |
| 643 | dso->short_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static void dso__set_basename(struct dso *dso) |
| 647 | { |
Stephane Eranian | ac5e7f8 | 2013-12-05 19:26:42 +0100 | [diff] [blame] | 648 | /* |
| 649 | * basename() may modify path buffer, so we must pass |
| 650 | * a copy. |
| 651 | */ |
| 652 | char *base, *lname = strdup(dso->long_name); |
| 653 | |
| 654 | if (!lname) |
| 655 | return; |
| 656 | |
| 657 | /* |
| 658 | * basename() may return a pointer to internal |
| 659 | * storage which is reused in subsequent calls |
| 660 | * so copy the result. |
| 661 | */ |
| 662 | base = strdup(basename(lname)); |
| 663 | |
| 664 | free(lname); |
| 665 | |
| 666 | if (!base) |
| 667 | return; |
| 668 | |
| 669 | dso__set_short_name(dso, base, true); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | int dso__name_len(const struct dso *dso) |
| 673 | { |
| 674 | if (!dso) |
| 675 | return strlen("[unknown]"); |
| 676 | if (verbose) |
| 677 | return dso->long_name_len; |
| 678 | |
| 679 | return dso->short_name_len; |
| 680 | } |
| 681 | |
| 682 | bool dso__loaded(const struct dso *dso, enum map_type type) |
| 683 | { |
| 684 | return dso->loaded & (1 << type); |
| 685 | } |
| 686 | |
| 687 | bool dso__sorted_by_name(const struct dso *dso, enum map_type type) |
| 688 | { |
| 689 | return dso->sorted_by_name & (1 << type); |
| 690 | } |
| 691 | |
| 692 | void dso__set_sorted_by_name(struct dso *dso, enum map_type type) |
| 693 | { |
| 694 | dso->sorted_by_name |= (1 << type); |
| 695 | } |
| 696 | |
| 697 | struct dso *dso__new(const char *name) |
| 698 | { |
| 699 | struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1); |
| 700 | |
| 701 | if (dso != NULL) { |
| 702 | int i; |
| 703 | strcpy(dso->name, name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 704 | dso__set_long_name(dso, dso->name, false); |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 705 | dso__set_short_name(dso, dso->name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 706 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 707 | dso->symbols[i] = dso->symbol_names[i] = RB_ROOT; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 708 | dso->data.cache = RB_ROOT; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 709 | dso->data.fd = -1; |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame^] | 710 | dso->data.status = DSO_DATA_STATUS_UNKNOWN; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 711 | dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 712 | dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 713 | dso->is_64_bit = (sizeof(void *) == 8); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 714 | dso->loaded = 0; |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 715 | dso->rel = 0; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 716 | dso->sorted_by_name = 0; |
| 717 | dso->has_build_id = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 718 | dso->has_srcline = 1; |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 719 | dso->a2l_fails = 1; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 720 | dso->kernel = DSO_TYPE_USER; |
| 721 | dso->needs_swap = DSO_SWAP__UNSET; |
| 722 | INIT_LIST_HEAD(&dso->node); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 723 | INIT_LIST_HEAD(&dso->data.open_entry); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | return dso; |
| 727 | } |
| 728 | |
| 729 | void dso__delete(struct dso *dso) |
| 730 | { |
| 731 | int i; |
| 732 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 733 | symbols__delete(&dso->symbols[i]); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 734 | |
| 735 | if (dso->short_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 736 | zfree((char **)&dso->short_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 737 | dso->short_name_allocated = false; |
| 738 | } |
| 739 | |
| 740 | if (dso->long_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 741 | zfree((char **)&dso->long_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 742 | dso->long_name_allocated = false; |
| 743 | } |
| 744 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 745 | dso__data_close(dso); |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 746 | dso_cache__free(&dso->data.cache); |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 747 | dso__free_a2l(dso); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 748 | zfree(&dso->symsrc_filename); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 749 | free(dso); |
| 750 | } |
| 751 | |
| 752 | void dso__set_build_id(struct dso *dso, void *build_id) |
| 753 | { |
| 754 | memcpy(dso->build_id, build_id, sizeof(dso->build_id)); |
| 755 | dso->has_build_id = 1; |
| 756 | } |
| 757 | |
| 758 | bool dso__build_id_equal(const struct dso *dso, u8 *build_id) |
| 759 | { |
| 760 | return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0; |
| 761 | } |
| 762 | |
| 763 | void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) |
| 764 | { |
| 765 | char path[PATH_MAX]; |
| 766 | |
| 767 | if (machine__is_default_guest(machine)) |
| 768 | return; |
| 769 | sprintf(path, "%s/sys/kernel/notes", machine->root_dir); |
| 770 | if (sysfs__read_build_id(path, dso->build_id, |
| 771 | sizeof(dso->build_id)) == 0) |
| 772 | dso->has_build_id = true; |
| 773 | } |
| 774 | |
| 775 | int dso__kernel_module_get_build_id(struct dso *dso, |
| 776 | const char *root_dir) |
| 777 | { |
| 778 | char filename[PATH_MAX]; |
| 779 | /* |
| 780 | * kernel module short names are of the form "[module]" and |
| 781 | * we need just "module" here. |
| 782 | */ |
| 783 | const char *name = dso->short_name + 1; |
| 784 | |
| 785 | snprintf(filename, sizeof(filename), |
| 786 | "%s/sys/module/%.*s/notes/.note.gnu.build-id", |
| 787 | root_dir, (int)strlen(name) - 1, name); |
| 788 | |
| 789 | if (sysfs__read_build_id(filename, dso->build_id, |
| 790 | sizeof(dso->build_id)) == 0) |
| 791 | dso->has_build_id = true; |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | bool __dsos__read_build_ids(struct list_head *head, bool with_hits) |
| 797 | { |
| 798 | bool have_build_id = false; |
| 799 | struct dso *pos; |
| 800 | |
| 801 | list_for_each_entry(pos, head, node) { |
| 802 | if (with_hits && !pos->hit) |
| 803 | continue; |
| 804 | if (pos->has_build_id) { |
| 805 | have_build_id = true; |
| 806 | continue; |
| 807 | } |
| 808 | if (filename__read_build_id(pos->long_name, pos->build_id, |
| 809 | sizeof(pos->build_id)) > 0) { |
| 810 | have_build_id = true; |
| 811 | pos->has_build_id = true; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | return have_build_id; |
| 816 | } |
| 817 | |
| 818 | void dsos__add(struct list_head *head, struct dso *dso) |
| 819 | { |
| 820 | list_add_tail(&dso->node, head); |
| 821 | } |
| 822 | |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 823 | struct dso *dsos__find(const struct list_head *head, const char *name, bool cmp_short) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 824 | { |
| 825 | struct dso *pos; |
| 826 | |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 827 | if (cmp_short) { |
| 828 | list_for_each_entry(pos, head, node) |
| 829 | if (strcmp(pos->short_name, name) == 0) |
| 830 | return pos; |
| 831 | return NULL; |
| 832 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 833 | list_for_each_entry(pos, head, node) |
| 834 | if (strcmp(pos->long_name, name) == 0) |
| 835 | return pos; |
| 836 | return NULL; |
| 837 | } |
| 838 | |
| 839 | struct dso *__dsos__findnew(struct list_head *head, const char *name) |
| 840 | { |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 841 | struct dso *dso = dsos__find(head, name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 842 | |
| 843 | if (!dso) { |
| 844 | dso = dso__new(name); |
| 845 | if (dso != NULL) { |
| 846 | dsos__add(head, dso); |
| 847 | dso__set_basename(dso); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | return dso; |
| 852 | } |
| 853 | |
| 854 | 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] | 855 | bool (skip)(struct dso *dso, int parm), int parm) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 856 | { |
| 857 | struct dso *pos; |
| 858 | size_t ret = 0; |
| 859 | |
| 860 | list_for_each_entry(pos, head, node) { |
Arnaldo Carvalho de Melo | 417c2ff | 2012-12-07 09:53:58 -0300 | [diff] [blame] | 861 | if (skip && skip(pos, parm)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 862 | continue; |
| 863 | ret += dso__fprintf_buildid(pos, fp); |
| 864 | ret += fprintf(fp, " %s\n", pos->long_name); |
| 865 | } |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | size_t __dsos__fprintf(struct list_head *head, FILE *fp) |
| 870 | { |
| 871 | struct dso *pos; |
| 872 | size_t ret = 0; |
| 873 | |
| 874 | list_for_each_entry(pos, head, node) { |
| 875 | int i; |
| 876 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 877 | ret += dso__fprintf(pos, i, fp); |
| 878 | } |
| 879 | |
| 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) |
| 884 | { |
| 885 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 886 | |
| 887 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 888 | return fprintf(fp, "%s", sbuild_id); |
| 889 | } |
| 890 | |
| 891 | size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp) |
| 892 | { |
| 893 | struct rb_node *nd; |
| 894 | size_t ret = fprintf(fp, "dso: %s (", dso->short_name); |
| 895 | |
| 896 | if (dso->short_name != dso->long_name) |
| 897 | ret += fprintf(fp, "%s, ", dso->long_name); |
| 898 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], |
Stephane Eranian | 919d590 | 2012-11-20 10:51:02 +0100 | [diff] [blame] | 899 | dso__loaded(dso, type) ? "" : "NOT "); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 900 | ret += dso__fprintf_buildid(dso, fp); |
| 901 | ret += fprintf(fp, ")\n"); |
| 902 | for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) { |
| 903 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 904 | ret += symbol__fprintf(pos, fp); |
| 905 | } |
| 906 | |
| 907 | return ret; |
| 908 | } |