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