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 | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 208 | static int open_dso(struct dso *dso, struct machine *machine) |
| 209 | { |
| 210 | int fd = __open_dso(dso, machine); |
| 211 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 212 | if (fd > 0) { |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 213 | dso__list_add(dso); |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 214 | /* |
| 215 | * Check if we crossed the allowed number |
| 216 | * of opened DSOs and close one if needed. |
| 217 | */ |
| 218 | check_data_close(); |
| 219 | } |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 220 | |
| 221 | return fd; |
| 222 | } |
| 223 | |
| 224 | static void close_data_fd(struct dso *dso) |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 225 | { |
| 226 | if (dso->data.fd >= 0) { |
| 227 | close(dso->data.fd); |
| 228 | dso->data.fd = -1; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 229 | dso->data.file_size = 0; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 230 | dso__list_del(dso); |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 234 | static void close_dso(struct dso *dso) |
| 235 | { |
| 236 | close_data_fd(dso); |
| 237 | } |
| 238 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 239 | static void close_first_dso(void) |
| 240 | { |
| 241 | struct dso *dso; |
| 242 | |
| 243 | dso = list_first_entry(&dso__data_open, struct dso, data.open_entry); |
| 244 | close_dso(dso); |
| 245 | } |
| 246 | |
| 247 | static rlim_t get_fd_limit(void) |
| 248 | { |
| 249 | struct rlimit l; |
| 250 | rlim_t limit = 0; |
| 251 | |
| 252 | /* Allow half of the current open fd limit. */ |
| 253 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { |
| 254 | if (l.rlim_cur == RLIM_INFINITY) |
| 255 | limit = l.rlim_cur; |
| 256 | else |
| 257 | limit = l.rlim_cur / 2; |
| 258 | } else { |
| 259 | pr_err("failed to get fd limit\n"); |
| 260 | limit = 1; |
| 261 | } |
| 262 | |
| 263 | return limit; |
| 264 | } |
| 265 | |
| 266 | static bool may_cache_fd(void) |
| 267 | { |
| 268 | static rlim_t limit; |
| 269 | |
| 270 | if (!limit) |
| 271 | limit = get_fd_limit(); |
| 272 | |
| 273 | if (limit == RLIM_INFINITY) |
| 274 | return true; |
| 275 | |
| 276 | return limit > (rlim_t) dso__data_open_cnt; |
| 277 | } |
| 278 | |
| 279 | static void check_data_close(void) |
| 280 | { |
| 281 | bool cache_fd = may_cache_fd(); |
| 282 | |
| 283 | if (!cache_fd) |
| 284 | close_first_dso(); |
| 285 | } |
| 286 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 287 | void dso__data_close(struct dso *dso) |
| 288 | { |
| 289 | close_dso(dso); |
| 290 | } |
| 291 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 292 | int dso__data_fd(struct dso *dso, struct machine *machine) |
| 293 | { |
Arnaldo Carvalho de Melo | 631d34b | 2013-12-16 16:57:43 -0300 | [diff] [blame] | 294 | enum dso_binary_type binary_type_data[] = { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 295 | DSO_BINARY_TYPE__BUILD_ID_CACHE, |
| 296 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, |
| 297 | DSO_BINARY_TYPE__NOT_FOUND, |
| 298 | }; |
| 299 | int i = 0; |
| 300 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 301 | if (dso->data.fd >= 0) |
| 302 | return dso->data.fd; |
| 303 | |
| 304 | if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) { |
| 305 | dso->data.fd = open_dso(dso, machine); |
| 306 | return dso->data.fd; |
| 307 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 308 | |
| 309 | do { |
| 310 | int fd; |
| 311 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 312 | dso->binary_type = binary_type_data[i++]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 313 | |
| 314 | fd = open_dso(dso, machine); |
| 315 | if (fd >= 0) |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 316 | return dso->data.fd = fd; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 317 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 318 | } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 319 | |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | |
| 323 | static void |
| 324 | dso_cache__free(struct rb_root *root) |
| 325 | { |
| 326 | struct rb_node *next = rb_first(root); |
| 327 | |
| 328 | while (next) { |
| 329 | struct dso_cache *cache; |
| 330 | |
| 331 | cache = rb_entry(next, struct dso_cache, rb_node); |
| 332 | next = rb_next(&cache->rb_node); |
| 333 | rb_erase(&cache->rb_node, root); |
| 334 | free(cache); |
| 335 | } |
| 336 | } |
| 337 | |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 338 | 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] | 339 | { |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 340 | struct rb_node * const *p = &root->rb_node; |
| 341 | const struct rb_node *parent = NULL; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 342 | struct dso_cache *cache; |
| 343 | |
| 344 | while (*p != NULL) { |
| 345 | u64 end; |
| 346 | |
| 347 | parent = *p; |
| 348 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 349 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 350 | |
| 351 | if (offset < cache->offset) |
| 352 | p = &(*p)->rb_left; |
| 353 | else if (offset >= end) |
| 354 | p = &(*p)->rb_right; |
| 355 | else |
| 356 | return cache; |
| 357 | } |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | static void |
| 362 | dso_cache__insert(struct rb_root *root, struct dso_cache *new) |
| 363 | { |
| 364 | struct rb_node **p = &root->rb_node; |
| 365 | struct rb_node *parent = NULL; |
| 366 | struct dso_cache *cache; |
| 367 | u64 offset = new->offset; |
| 368 | |
| 369 | while (*p != NULL) { |
| 370 | u64 end; |
| 371 | |
| 372 | parent = *p; |
| 373 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 374 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 375 | |
| 376 | if (offset < cache->offset) |
| 377 | p = &(*p)->rb_left; |
| 378 | else if (offset >= end) |
| 379 | p = &(*p)->rb_right; |
| 380 | } |
| 381 | |
| 382 | rb_link_node(&new->rb_node, parent, p); |
| 383 | rb_insert_color(&new->rb_node, root); |
| 384 | } |
| 385 | |
| 386 | static ssize_t |
| 387 | dso_cache__memcpy(struct dso_cache *cache, u64 offset, |
| 388 | u8 *data, u64 size) |
| 389 | { |
| 390 | u64 cache_offset = offset - cache->offset; |
| 391 | u64 cache_size = min(cache->size - cache_offset, size); |
| 392 | |
| 393 | memcpy(data, cache->data + cache_offset, cache_size); |
| 394 | return cache_size; |
| 395 | } |
| 396 | |
| 397 | static ssize_t |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 398 | 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] | 399 | { |
| 400 | struct dso_cache *cache; |
| 401 | ssize_t ret; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 402 | |
| 403 | do { |
| 404 | u64 cache_offset; |
| 405 | |
| 406 | ret = -ENOMEM; |
| 407 | |
| 408 | cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); |
| 409 | if (!cache) |
| 410 | break; |
| 411 | |
| 412 | cache_offset = offset & DSO__DATA_CACHE_MASK; |
| 413 | ret = -EINVAL; |
| 414 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 415 | if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 416 | break; |
| 417 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 418 | ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 419 | if (ret <= 0) |
| 420 | break; |
| 421 | |
| 422 | cache->offset = cache_offset; |
| 423 | cache->size = ret; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 424 | dso_cache__insert(&dso->data.cache, cache); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 425 | |
| 426 | ret = dso_cache__memcpy(cache, offset, data, size); |
| 427 | |
| 428 | } while (0); |
| 429 | |
| 430 | if (ret <= 0) |
| 431 | free(cache); |
| 432 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 433 | return ret; |
| 434 | } |
| 435 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 436 | static ssize_t dso_cache_read(struct dso *dso, u64 offset, |
| 437 | u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 438 | { |
| 439 | struct dso_cache *cache; |
| 440 | |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 441 | cache = dso_cache__find(&dso->data.cache, offset); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 442 | if (cache) |
| 443 | return dso_cache__memcpy(cache, offset, data, size); |
| 444 | else |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 445 | return dso_cache__read(dso, offset, data, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 448 | 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] | 449 | { |
| 450 | ssize_t r = 0; |
| 451 | u8 *p = data; |
| 452 | |
| 453 | do { |
| 454 | ssize_t ret; |
| 455 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 456 | ret = dso_cache_read(dso, offset, p, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 457 | if (ret < 0) |
| 458 | return ret; |
| 459 | |
| 460 | /* Reached EOF, return what we have. */ |
| 461 | if (!ret) |
| 462 | break; |
| 463 | |
| 464 | BUG_ON(ret > size); |
| 465 | |
| 466 | r += ret; |
| 467 | p += ret; |
| 468 | offset += ret; |
| 469 | size -= ret; |
| 470 | |
| 471 | } while (size); |
| 472 | |
| 473 | return r; |
| 474 | } |
| 475 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 476 | static int data_file_size(struct dso *dso) |
| 477 | { |
| 478 | struct stat st; |
| 479 | |
| 480 | if (!dso->data.file_size) { |
| 481 | if (fstat(dso->data.fd, &st)) { |
| 482 | pr_err("dso mmap failed, fstat: %s\n", strerror(errno)); |
| 483 | return -1; |
| 484 | } |
| 485 | dso->data.file_size = st.st_size; |
| 486 | } |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | static ssize_t data_read_offset(struct dso *dso, u64 offset, |
| 492 | u8 *data, ssize_t size) |
| 493 | { |
| 494 | if (data_file_size(dso)) |
| 495 | return -1; |
| 496 | |
| 497 | /* Check the offset sanity. */ |
| 498 | if (offset > dso->data.file_size) |
| 499 | return -1; |
| 500 | |
| 501 | if (offset + size < offset) |
| 502 | return -1; |
| 503 | |
| 504 | return cached_read(dso, offset, data, size); |
| 505 | } |
| 506 | |
| 507 | ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, |
| 508 | u64 offset, u8 *data, ssize_t size) |
| 509 | { |
| 510 | if (dso__data_fd(dso, machine) < 0) |
| 511 | return -1; |
| 512 | |
| 513 | return data_read_offset(dso, offset, data, size); |
| 514 | } |
| 515 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 516 | ssize_t dso__data_read_addr(struct dso *dso, struct map *map, |
| 517 | struct machine *machine, u64 addr, |
| 518 | u8 *data, ssize_t size) |
| 519 | { |
| 520 | u64 offset = map->map_ip(map, addr); |
| 521 | return dso__data_read_offset(dso, machine, offset, data, size); |
| 522 | } |
| 523 | |
| 524 | struct map *dso__new_map(const char *name) |
| 525 | { |
| 526 | struct map *map = NULL; |
| 527 | struct dso *dso = dso__new(name); |
| 528 | |
| 529 | if (dso) |
| 530 | map = map__new2(0, dso, MAP__FUNCTION); |
| 531 | |
| 532 | return map; |
| 533 | } |
| 534 | |
| 535 | struct dso *dso__kernel_findnew(struct machine *machine, const char *name, |
| 536 | const char *short_name, int dso_type) |
| 537 | { |
| 538 | /* |
| 539 | * The kernel dso could be created by build_id processing. |
| 540 | */ |
| 541 | struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name); |
| 542 | |
| 543 | /* |
| 544 | * We need to run this in all cases, since during the build_id |
| 545 | * processing we had no idea this was the kernel dso. |
| 546 | */ |
| 547 | if (dso != NULL) { |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 548 | dso__set_short_name(dso, short_name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 549 | dso->kernel = dso_type; |
| 550 | } |
| 551 | |
| 552 | return dso; |
| 553 | } |
| 554 | |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 555 | 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] | 556 | { |
| 557 | if (name == NULL) |
| 558 | return; |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 559 | |
| 560 | if (dso->long_name_allocated) |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 561 | free((char *)dso->long_name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 562 | |
| 563 | dso->long_name = name; |
| 564 | dso->long_name_len = strlen(name); |
| 565 | dso->long_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 568 | 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] | 569 | { |
| 570 | if (name == NULL) |
| 571 | return; |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 572 | |
| 573 | if (dso->short_name_allocated) |
| 574 | free((char *)dso->short_name); |
| 575 | |
| 576 | dso->short_name = name; |
| 577 | dso->short_name_len = strlen(name); |
| 578 | dso->short_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static void dso__set_basename(struct dso *dso) |
| 582 | { |
Stephane Eranian | ac5e7f8 | 2013-12-05 19:26:42 +0100 | [diff] [blame] | 583 | /* |
| 584 | * basename() may modify path buffer, so we must pass |
| 585 | * a copy. |
| 586 | */ |
| 587 | char *base, *lname = strdup(dso->long_name); |
| 588 | |
| 589 | if (!lname) |
| 590 | return; |
| 591 | |
| 592 | /* |
| 593 | * basename() may return a pointer to internal |
| 594 | * storage which is reused in subsequent calls |
| 595 | * so copy the result. |
| 596 | */ |
| 597 | base = strdup(basename(lname)); |
| 598 | |
| 599 | free(lname); |
| 600 | |
| 601 | if (!base) |
| 602 | return; |
| 603 | |
| 604 | dso__set_short_name(dso, base, true); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | int dso__name_len(const struct dso *dso) |
| 608 | { |
| 609 | if (!dso) |
| 610 | return strlen("[unknown]"); |
| 611 | if (verbose) |
| 612 | return dso->long_name_len; |
| 613 | |
| 614 | return dso->short_name_len; |
| 615 | } |
| 616 | |
| 617 | bool dso__loaded(const struct dso *dso, enum map_type type) |
| 618 | { |
| 619 | return dso->loaded & (1 << type); |
| 620 | } |
| 621 | |
| 622 | bool dso__sorted_by_name(const struct dso *dso, enum map_type type) |
| 623 | { |
| 624 | return dso->sorted_by_name & (1 << type); |
| 625 | } |
| 626 | |
| 627 | void dso__set_sorted_by_name(struct dso *dso, enum map_type type) |
| 628 | { |
| 629 | dso->sorted_by_name |= (1 << type); |
| 630 | } |
| 631 | |
| 632 | struct dso *dso__new(const char *name) |
| 633 | { |
| 634 | struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1); |
| 635 | |
| 636 | if (dso != NULL) { |
| 637 | int i; |
| 638 | strcpy(dso->name, name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 639 | dso__set_long_name(dso, dso->name, false); |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 640 | dso__set_short_name(dso, dso->name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 641 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 642 | dso->symbols[i] = dso->symbol_names[i] = RB_ROOT; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 643 | dso->data.cache = RB_ROOT; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 644 | dso->data.fd = -1; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 645 | dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 646 | dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 647 | dso->loaded = 0; |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 648 | dso->rel = 0; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 649 | dso->sorted_by_name = 0; |
| 650 | dso->has_build_id = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 651 | dso->has_srcline = 1; |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 652 | dso->a2l_fails = 1; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 653 | dso->kernel = DSO_TYPE_USER; |
| 654 | dso->needs_swap = DSO_SWAP__UNSET; |
| 655 | INIT_LIST_HEAD(&dso->node); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 656 | INIT_LIST_HEAD(&dso->data.open_entry); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | return dso; |
| 660 | } |
| 661 | |
| 662 | void dso__delete(struct dso *dso) |
| 663 | { |
| 664 | int i; |
| 665 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 666 | symbols__delete(&dso->symbols[i]); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 667 | |
| 668 | if (dso->short_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 669 | zfree((char **)&dso->short_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 670 | dso->short_name_allocated = false; |
| 671 | } |
| 672 | |
| 673 | if (dso->long_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 674 | zfree((char **)&dso->long_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 675 | dso->long_name_allocated = false; |
| 676 | } |
| 677 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 678 | dso__data_close(dso); |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 679 | dso_cache__free(&dso->data.cache); |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 680 | dso__free_a2l(dso); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 681 | zfree(&dso->symsrc_filename); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 682 | free(dso); |
| 683 | } |
| 684 | |
| 685 | void dso__set_build_id(struct dso *dso, void *build_id) |
| 686 | { |
| 687 | memcpy(dso->build_id, build_id, sizeof(dso->build_id)); |
| 688 | dso->has_build_id = 1; |
| 689 | } |
| 690 | |
| 691 | bool dso__build_id_equal(const struct dso *dso, u8 *build_id) |
| 692 | { |
| 693 | return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0; |
| 694 | } |
| 695 | |
| 696 | void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) |
| 697 | { |
| 698 | char path[PATH_MAX]; |
| 699 | |
| 700 | if (machine__is_default_guest(machine)) |
| 701 | return; |
| 702 | sprintf(path, "%s/sys/kernel/notes", machine->root_dir); |
| 703 | if (sysfs__read_build_id(path, dso->build_id, |
| 704 | sizeof(dso->build_id)) == 0) |
| 705 | dso->has_build_id = true; |
| 706 | } |
| 707 | |
| 708 | int dso__kernel_module_get_build_id(struct dso *dso, |
| 709 | const char *root_dir) |
| 710 | { |
| 711 | char filename[PATH_MAX]; |
| 712 | /* |
| 713 | * kernel module short names are of the form "[module]" and |
| 714 | * we need just "module" here. |
| 715 | */ |
| 716 | const char *name = dso->short_name + 1; |
| 717 | |
| 718 | snprintf(filename, sizeof(filename), |
| 719 | "%s/sys/module/%.*s/notes/.note.gnu.build-id", |
| 720 | root_dir, (int)strlen(name) - 1, name); |
| 721 | |
| 722 | if (sysfs__read_build_id(filename, dso->build_id, |
| 723 | sizeof(dso->build_id)) == 0) |
| 724 | dso->has_build_id = true; |
| 725 | |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | bool __dsos__read_build_ids(struct list_head *head, bool with_hits) |
| 730 | { |
| 731 | bool have_build_id = false; |
| 732 | struct dso *pos; |
| 733 | |
| 734 | list_for_each_entry(pos, head, node) { |
| 735 | if (with_hits && !pos->hit) |
| 736 | continue; |
| 737 | if (pos->has_build_id) { |
| 738 | have_build_id = true; |
| 739 | continue; |
| 740 | } |
| 741 | if (filename__read_build_id(pos->long_name, pos->build_id, |
| 742 | sizeof(pos->build_id)) > 0) { |
| 743 | have_build_id = true; |
| 744 | pos->has_build_id = true; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | return have_build_id; |
| 749 | } |
| 750 | |
| 751 | void dsos__add(struct list_head *head, struct dso *dso) |
| 752 | { |
| 753 | list_add_tail(&dso->node, head); |
| 754 | } |
| 755 | |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 756 | 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] | 757 | { |
| 758 | struct dso *pos; |
| 759 | |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 760 | if (cmp_short) { |
| 761 | list_for_each_entry(pos, head, node) |
| 762 | if (strcmp(pos->short_name, name) == 0) |
| 763 | return pos; |
| 764 | return NULL; |
| 765 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 766 | list_for_each_entry(pos, head, node) |
| 767 | if (strcmp(pos->long_name, name) == 0) |
| 768 | return pos; |
| 769 | return NULL; |
| 770 | } |
| 771 | |
| 772 | struct dso *__dsos__findnew(struct list_head *head, const char *name) |
| 773 | { |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 774 | struct dso *dso = dsos__find(head, name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 775 | |
| 776 | if (!dso) { |
| 777 | dso = dso__new(name); |
| 778 | if (dso != NULL) { |
| 779 | dsos__add(head, dso); |
| 780 | dso__set_basename(dso); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | return dso; |
| 785 | } |
| 786 | |
| 787 | 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] | 788 | bool (skip)(struct dso *dso, int parm), int parm) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 789 | { |
| 790 | struct dso *pos; |
| 791 | size_t ret = 0; |
| 792 | |
| 793 | list_for_each_entry(pos, head, node) { |
Arnaldo Carvalho de Melo | 417c2ff | 2012-12-07 09:53:58 -0300 | [diff] [blame] | 794 | if (skip && skip(pos, parm)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 795 | continue; |
| 796 | ret += dso__fprintf_buildid(pos, fp); |
| 797 | ret += fprintf(fp, " %s\n", pos->long_name); |
| 798 | } |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | size_t __dsos__fprintf(struct list_head *head, FILE *fp) |
| 803 | { |
| 804 | struct dso *pos; |
| 805 | size_t ret = 0; |
| 806 | |
| 807 | list_for_each_entry(pos, head, node) { |
| 808 | int i; |
| 809 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 810 | ret += dso__fprintf(pos, i, fp); |
| 811 | } |
| 812 | |
| 813 | return ret; |
| 814 | } |
| 815 | |
| 816 | size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) |
| 817 | { |
| 818 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 819 | |
| 820 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 821 | return fprintf(fp, "%s", sbuild_id); |
| 822 | } |
| 823 | |
| 824 | size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp) |
| 825 | { |
| 826 | struct rb_node *nd; |
| 827 | size_t ret = fprintf(fp, "dso: %s (", dso->short_name); |
| 828 | |
| 829 | if (dso->short_name != dso->long_name) |
| 830 | ret += fprintf(fp, "%s, ", dso->long_name); |
| 831 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], |
Stephane Eranian | 919d590 | 2012-11-20 10:51:02 +0100 | [diff] [blame] | 832 | dso__loaded(dso, type) ? "" : "NOT "); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 833 | ret += dso__fprintf_buildid(dso, fp); |
| 834 | ret += fprintf(fp, ")\n"); |
| 835 | for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) { |
| 836 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 837 | ret += symbol__fprintf(pos, fp); |
| 838 | } |
| 839 | |
| 840 | return ret; |
| 841 | } |