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; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 165 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 166 | |
| 167 | do { |
| 168 | fd = open(name, O_RDONLY); |
| 169 | if (fd >= 0) |
| 170 | return fd; |
| 171 | |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 172 | pr_debug("dso open failed, mmap: %s\n", |
| 173 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 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 | |
Adrian Hunter | 288be94 | 2014-07-22 16:17:19 +0300 | [diff] [blame] | 362 | bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) |
| 363 | { |
| 364 | u32 flag = 1 << by; |
| 365 | |
| 366 | if (dso->data.status_seen & flag) |
| 367 | return true; |
| 368 | |
| 369 | dso->data.status_seen |= flag; |
| 370 | |
| 371 | return false; |
| 372 | } |
| 373 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 374 | static void |
| 375 | dso_cache__free(struct rb_root *root) |
| 376 | { |
| 377 | struct rb_node *next = rb_first(root); |
| 378 | |
| 379 | while (next) { |
| 380 | struct dso_cache *cache; |
| 381 | |
| 382 | cache = rb_entry(next, struct dso_cache, rb_node); |
| 383 | next = rb_next(&cache->rb_node); |
| 384 | rb_erase(&cache->rb_node, root); |
| 385 | free(cache); |
| 386 | } |
| 387 | } |
| 388 | |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 389 | 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] | 390 | { |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 391 | struct rb_node * const *p = &root->rb_node; |
| 392 | const struct rb_node *parent = NULL; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 393 | struct dso_cache *cache; |
| 394 | |
| 395 | while (*p != NULL) { |
| 396 | u64 end; |
| 397 | |
| 398 | parent = *p; |
| 399 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 400 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 401 | |
| 402 | if (offset < cache->offset) |
| 403 | p = &(*p)->rb_left; |
| 404 | else if (offset >= end) |
| 405 | p = &(*p)->rb_right; |
| 406 | else |
| 407 | return cache; |
| 408 | } |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | static void |
| 413 | dso_cache__insert(struct rb_root *root, struct dso_cache *new) |
| 414 | { |
| 415 | struct rb_node **p = &root->rb_node; |
| 416 | struct rb_node *parent = NULL; |
| 417 | struct dso_cache *cache; |
| 418 | u64 offset = new->offset; |
| 419 | |
| 420 | while (*p != NULL) { |
| 421 | u64 end; |
| 422 | |
| 423 | parent = *p; |
| 424 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 425 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 426 | |
| 427 | if (offset < cache->offset) |
| 428 | p = &(*p)->rb_left; |
| 429 | else if (offset >= end) |
| 430 | p = &(*p)->rb_right; |
| 431 | } |
| 432 | |
| 433 | rb_link_node(&new->rb_node, parent, p); |
| 434 | rb_insert_color(&new->rb_node, root); |
| 435 | } |
| 436 | |
| 437 | static ssize_t |
| 438 | dso_cache__memcpy(struct dso_cache *cache, u64 offset, |
| 439 | u8 *data, u64 size) |
| 440 | { |
| 441 | u64 cache_offset = offset - cache->offset; |
| 442 | u64 cache_size = min(cache->size - cache_offset, size); |
| 443 | |
| 444 | memcpy(data, cache->data + cache_offset, cache_size); |
| 445 | return cache_size; |
| 446 | } |
| 447 | |
| 448 | static ssize_t |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 449 | 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] | 450 | { |
| 451 | struct dso_cache *cache; |
| 452 | ssize_t ret; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 453 | |
| 454 | do { |
| 455 | u64 cache_offset; |
| 456 | |
| 457 | ret = -ENOMEM; |
| 458 | |
| 459 | cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); |
| 460 | if (!cache) |
| 461 | break; |
| 462 | |
| 463 | cache_offset = offset & DSO__DATA_CACHE_MASK; |
| 464 | ret = -EINVAL; |
| 465 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 466 | if (-1 == lseek(dso->data.fd, cache_offset, SEEK_SET)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 467 | break; |
| 468 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 469 | ret = read(dso->data.fd, cache->data, DSO__DATA_CACHE_SIZE); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 470 | if (ret <= 0) |
| 471 | break; |
| 472 | |
| 473 | cache->offset = cache_offset; |
| 474 | cache->size = ret; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 475 | dso_cache__insert(&dso->data.cache, cache); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 476 | |
| 477 | ret = dso_cache__memcpy(cache, offset, data, size); |
| 478 | |
| 479 | } while (0); |
| 480 | |
| 481 | if (ret <= 0) |
| 482 | free(cache); |
| 483 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 484 | return ret; |
| 485 | } |
| 486 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 487 | static ssize_t dso_cache_read(struct dso *dso, u64 offset, |
| 488 | u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 489 | { |
| 490 | struct dso_cache *cache; |
| 491 | |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 492 | cache = dso_cache__find(&dso->data.cache, offset); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 493 | if (cache) |
| 494 | return dso_cache__memcpy(cache, offset, data, size); |
| 495 | else |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 496 | return dso_cache__read(dso, offset, data, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 499 | /* |
| 500 | * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks |
| 501 | * in the rb_tree. Any read to already cached data is served |
| 502 | * by cached data. |
| 503 | */ |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 504 | 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] | 505 | { |
| 506 | ssize_t r = 0; |
| 507 | u8 *p = data; |
| 508 | |
| 509 | do { |
| 510 | ssize_t ret; |
| 511 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 512 | ret = dso_cache_read(dso, offset, p, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 513 | if (ret < 0) |
| 514 | return ret; |
| 515 | |
| 516 | /* Reached EOF, return what we have. */ |
| 517 | if (!ret) |
| 518 | break; |
| 519 | |
| 520 | BUG_ON(ret > size); |
| 521 | |
| 522 | r += ret; |
| 523 | p += ret; |
| 524 | offset += ret; |
| 525 | size -= ret; |
| 526 | |
| 527 | } while (size); |
| 528 | |
| 529 | return r; |
| 530 | } |
| 531 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 532 | static int data_file_size(struct dso *dso) |
| 533 | { |
| 534 | struct stat st; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 535 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 536 | |
| 537 | if (!dso->data.file_size) { |
| 538 | if (fstat(dso->data.fd, &st)) { |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 539 | pr_err("dso mmap failed, fstat: %s\n", |
| 540 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | dso->data.file_size = st.st_size; |
| 544 | } |
| 545 | |
| 546 | return 0; |
| 547 | } |
| 548 | |
Adrian Hunter | 6d36345 | 2014-07-22 16:17:35 +0300 | [diff] [blame] | 549 | /** |
| 550 | * dso__data_size - Return dso data size |
| 551 | * @dso: dso object |
| 552 | * @machine: machine object |
| 553 | * |
| 554 | * Return: dso data size |
| 555 | */ |
| 556 | off_t dso__data_size(struct dso *dso, struct machine *machine) |
| 557 | { |
| 558 | int fd; |
| 559 | |
| 560 | fd = dso__data_fd(dso, machine); |
| 561 | if (fd < 0) |
| 562 | return fd; |
| 563 | |
| 564 | if (data_file_size(dso)) |
| 565 | return -1; |
| 566 | |
| 567 | /* For now just estimate dso data size is close to file size */ |
| 568 | return dso->data.file_size; |
| 569 | } |
| 570 | |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 571 | static ssize_t data_read_offset(struct dso *dso, u64 offset, |
| 572 | u8 *data, ssize_t size) |
| 573 | { |
| 574 | if (data_file_size(dso)) |
| 575 | return -1; |
| 576 | |
| 577 | /* Check the offset sanity. */ |
| 578 | if (offset > dso->data.file_size) |
| 579 | return -1; |
| 580 | |
| 581 | if (offset + size < offset) |
| 582 | return -1; |
| 583 | |
| 584 | return cached_read(dso, offset, data, size); |
| 585 | } |
| 586 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 587 | /** |
| 588 | * dso__data_read_offset - Read data from dso file offset |
| 589 | * @dso: dso object |
| 590 | * @machine: machine object |
| 591 | * @offset: file offset |
| 592 | * @data: buffer to store data |
| 593 | * @size: size of the @data buffer |
| 594 | * |
| 595 | * External interface to read data from dso file offset. Open |
| 596 | * dso data file and use cached_read to get the data. |
| 597 | */ |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 598 | ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, |
| 599 | u64 offset, u8 *data, ssize_t size) |
| 600 | { |
| 601 | if (dso__data_fd(dso, machine) < 0) |
| 602 | return -1; |
| 603 | |
| 604 | return data_read_offset(dso, offset, data, size); |
| 605 | } |
| 606 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 607 | /** |
| 608 | * dso__data_read_addr - Read data from dso address |
| 609 | * @dso: dso object |
| 610 | * @machine: machine object |
| 611 | * @add: virtual memory address |
| 612 | * @data: buffer to store data |
| 613 | * @size: size of the @data buffer |
| 614 | * |
| 615 | * External interface to read data from dso address. |
| 616 | */ |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 617 | ssize_t dso__data_read_addr(struct dso *dso, struct map *map, |
| 618 | struct machine *machine, u64 addr, |
| 619 | u8 *data, ssize_t size) |
| 620 | { |
| 621 | u64 offset = map->map_ip(map, addr); |
| 622 | return dso__data_read_offset(dso, machine, offset, data, size); |
| 623 | } |
| 624 | |
| 625 | struct map *dso__new_map(const char *name) |
| 626 | { |
| 627 | struct map *map = NULL; |
| 628 | struct dso *dso = dso__new(name); |
| 629 | |
| 630 | if (dso) |
| 631 | map = map__new2(0, dso, MAP__FUNCTION); |
| 632 | |
| 633 | return map; |
| 634 | } |
| 635 | |
| 636 | struct dso *dso__kernel_findnew(struct machine *machine, const char *name, |
| 637 | const char *short_name, int dso_type) |
| 638 | { |
| 639 | /* |
| 640 | * The kernel dso could be created by build_id processing. |
| 641 | */ |
| 642 | struct dso *dso = __dsos__findnew(&machine->kernel_dsos, name); |
| 643 | |
| 644 | /* |
| 645 | * We need to run this in all cases, since during the build_id |
| 646 | * processing we had no idea this was the kernel dso. |
| 647 | */ |
| 648 | if (dso != NULL) { |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 649 | dso__set_short_name(dso, short_name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 650 | dso->kernel = dso_type; |
| 651 | } |
| 652 | |
| 653 | return dso; |
| 654 | } |
| 655 | |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame^] | 656 | /* |
| 657 | * Find a matching entry and/or link current entry to RB tree. |
| 658 | * Either one of the dso or name parameter must be non-NULL or the |
| 659 | * function will not work. |
| 660 | */ |
| 661 | static struct dso *dso__findlink_by_longname(struct rb_root *root, |
| 662 | struct dso *dso, const char *name) |
| 663 | { |
| 664 | struct rb_node **p = &root->rb_node; |
| 665 | struct rb_node *parent = NULL; |
| 666 | |
| 667 | if (!name) |
| 668 | name = dso->long_name; |
| 669 | /* |
| 670 | * Find node with the matching name |
| 671 | */ |
| 672 | while (*p) { |
| 673 | struct dso *this = rb_entry(*p, struct dso, rb_node); |
| 674 | int rc = strcmp(name, this->long_name); |
| 675 | |
| 676 | parent = *p; |
| 677 | if (rc == 0) { |
| 678 | /* |
| 679 | * In case the new DSO is a duplicate of an existing |
| 680 | * one, print an one-time warning & put the new entry |
| 681 | * at the end of the list of duplicates. |
| 682 | */ |
| 683 | if (!dso || (dso == this)) |
| 684 | return this; /* Find matching dso */ |
| 685 | /* |
| 686 | * The core kernel DSOs may have duplicated long name. |
| 687 | * In this case, the short name should be different. |
| 688 | * Comparing the short names to differentiate the DSOs. |
| 689 | */ |
| 690 | rc = strcmp(dso->short_name, this->short_name); |
| 691 | if (rc == 0) { |
| 692 | pr_err("Duplicated dso name: %s\n", name); |
| 693 | return NULL; |
| 694 | } |
| 695 | } |
| 696 | if (rc < 0) |
| 697 | p = &parent->rb_left; |
| 698 | else |
| 699 | p = &parent->rb_right; |
| 700 | } |
| 701 | if (dso) { |
| 702 | /* Add new node and rebalance tree */ |
| 703 | rb_link_node(&dso->rb_node, parent, p); |
| 704 | rb_insert_color(&dso->rb_node, root); |
| 705 | } |
| 706 | return NULL; |
| 707 | } |
| 708 | |
| 709 | static inline struct dso * |
| 710 | dso__find_by_longname(const struct rb_root *root, const char *name) |
| 711 | { |
| 712 | return dso__findlink_by_longname((struct rb_root *)root, NULL, name); |
| 713 | } |
| 714 | |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 715 | 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] | 716 | { |
| 717 | if (name == NULL) |
| 718 | return; |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 719 | |
| 720 | if (dso->long_name_allocated) |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 721 | free((char *)dso->long_name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 722 | |
| 723 | dso->long_name = name; |
| 724 | dso->long_name_len = strlen(name); |
| 725 | dso->long_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 728 | 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] | 729 | { |
| 730 | if (name == NULL) |
| 731 | return; |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 732 | |
| 733 | if (dso->short_name_allocated) |
| 734 | free((char *)dso->short_name); |
| 735 | |
| 736 | dso->short_name = name; |
| 737 | dso->short_name_len = strlen(name); |
| 738 | dso->short_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | static void dso__set_basename(struct dso *dso) |
| 742 | { |
Stephane Eranian | ac5e7f8 | 2013-12-05 19:26:42 +0100 | [diff] [blame] | 743 | /* |
| 744 | * basename() may modify path buffer, so we must pass |
| 745 | * a copy. |
| 746 | */ |
| 747 | char *base, *lname = strdup(dso->long_name); |
| 748 | |
| 749 | if (!lname) |
| 750 | return; |
| 751 | |
| 752 | /* |
| 753 | * basename() may return a pointer to internal |
| 754 | * storage which is reused in subsequent calls |
| 755 | * so copy the result. |
| 756 | */ |
| 757 | base = strdup(basename(lname)); |
| 758 | |
| 759 | free(lname); |
| 760 | |
| 761 | if (!base) |
| 762 | return; |
| 763 | |
| 764 | dso__set_short_name(dso, base, true); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | int dso__name_len(const struct dso *dso) |
| 768 | { |
| 769 | if (!dso) |
| 770 | return strlen("[unknown]"); |
| 771 | if (verbose) |
| 772 | return dso->long_name_len; |
| 773 | |
| 774 | return dso->short_name_len; |
| 775 | } |
| 776 | |
| 777 | bool dso__loaded(const struct dso *dso, enum map_type type) |
| 778 | { |
| 779 | return dso->loaded & (1 << type); |
| 780 | } |
| 781 | |
| 782 | bool dso__sorted_by_name(const struct dso *dso, enum map_type type) |
| 783 | { |
| 784 | return dso->sorted_by_name & (1 << type); |
| 785 | } |
| 786 | |
| 787 | void dso__set_sorted_by_name(struct dso *dso, enum map_type type) |
| 788 | { |
| 789 | dso->sorted_by_name |= (1 << type); |
| 790 | } |
| 791 | |
| 792 | struct dso *dso__new(const char *name) |
| 793 | { |
| 794 | struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1); |
| 795 | |
| 796 | if (dso != NULL) { |
| 797 | int i; |
| 798 | strcpy(dso->name, name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 799 | dso__set_long_name(dso, dso->name, false); |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 800 | dso__set_short_name(dso, dso->name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 801 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 802 | dso->symbols[i] = dso->symbol_names[i] = RB_ROOT; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 803 | dso->data.cache = RB_ROOT; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 804 | dso->data.fd = -1; |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 805 | dso->data.status = DSO_DATA_STATUS_UNKNOWN; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 806 | dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 807 | dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 808 | dso->is_64_bit = (sizeof(void *) == 8); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 809 | dso->loaded = 0; |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 810 | dso->rel = 0; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 811 | dso->sorted_by_name = 0; |
| 812 | dso->has_build_id = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 813 | dso->has_srcline = 1; |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 814 | dso->a2l_fails = 1; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 815 | dso->kernel = DSO_TYPE_USER; |
| 816 | dso->needs_swap = DSO_SWAP__UNSET; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame^] | 817 | RB_CLEAR_NODE(&dso->rb_node); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 818 | INIT_LIST_HEAD(&dso->node); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 819 | INIT_LIST_HEAD(&dso->data.open_entry); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | return dso; |
| 823 | } |
| 824 | |
| 825 | void dso__delete(struct dso *dso) |
| 826 | { |
| 827 | int i; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame^] | 828 | |
| 829 | if (!RB_EMPTY_NODE(&dso->rb_node)) |
| 830 | pr_err("DSO %s is still in rbtree when being deleted!\n", |
| 831 | dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 832 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 833 | symbols__delete(&dso->symbols[i]); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 834 | |
| 835 | if (dso->short_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 836 | zfree((char **)&dso->short_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 837 | dso->short_name_allocated = false; |
| 838 | } |
| 839 | |
| 840 | if (dso->long_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 841 | zfree((char **)&dso->long_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 842 | dso->long_name_allocated = false; |
| 843 | } |
| 844 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 845 | dso__data_close(dso); |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 846 | dso_cache__free(&dso->data.cache); |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 847 | dso__free_a2l(dso); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 848 | zfree(&dso->symsrc_filename); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 849 | free(dso); |
| 850 | } |
| 851 | |
| 852 | void dso__set_build_id(struct dso *dso, void *build_id) |
| 853 | { |
| 854 | memcpy(dso->build_id, build_id, sizeof(dso->build_id)); |
| 855 | dso->has_build_id = 1; |
| 856 | } |
| 857 | |
| 858 | bool dso__build_id_equal(const struct dso *dso, u8 *build_id) |
| 859 | { |
| 860 | return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0; |
| 861 | } |
| 862 | |
| 863 | void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) |
| 864 | { |
| 865 | char path[PATH_MAX]; |
| 866 | |
| 867 | if (machine__is_default_guest(machine)) |
| 868 | return; |
| 869 | sprintf(path, "%s/sys/kernel/notes", machine->root_dir); |
| 870 | if (sysfs__read_build_id(path, dso->build_id, |
| 871 | sizeof(dso->build_id)) == 0) |
| 872 | dso->has_build_id = true; |
| 873 | } |
| 874 | |
| 875 | int dso__kernel_module_get_build_id(struct dso *dso, |
| 876 | const char *root_dir) |
| 877 | { |
| 878 | char filename[PATH_MAX]; |
| 879 | /* |
| 880 | * kernel module short names are of the form "[module]" and |
| 881 | * we need just "module" here. |
| 882 | */ |
| 883 | const char *name = dso->short_name + 1; |
| 884 | |
| 885 | snprintf(filename, sizeof(filename), |
| 886 | "%s/sys/module/%.*s/notes/.note.gnu.build-id", |
| 887 | root_dir, (int)strlen(name) - 1, name); |
| 888 | |
| 889 | if (sysfs__read_build_id(filename, dso->build_id, |
| 890 | sizeof(dso->build_id)) == 0) |
| 891 | dso->has_build_id = true; |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | bool __dsos__read_build_ids(struct list_head *head, bool with_hits) |
| 897 | { |
| 898 | bool have_build_id = false; |
| 899 | struct dso *pos; |
| 900 | |
| 901 | list_for_each_entry(pos, head, node) { |
| 902 | if (with_hits && !pos->hit) |
| 903 | continue; |
| 904 | if (pos->has_build_id) { |
| 905 | have_build_id = true; |
| 906 | continue; |
| 907 | } |
| 908 | if (filename__read_build_id(pos->long_name, pos->build_id, |
| 909 | sizeof(pos->build_id)) > 0) { |
| 910 | have_build_id = true; |
| 911 | pos->has_build_id = true; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | return have_build_id; |
| 916 | } |
| 917 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 918 | void dsos__add(struct dsos *dsos, struct dso *dso) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 919 | { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 920 | list_add_tail(&dso->node, &dsos->head); |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame^] | 921 | dso__findlink_by_longname(&dsos->root, dso, NULL); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 924 | struct dso *dsos__find(const struct dsos *dsos, const char *name, |
| 925 | bool cmp_short) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 926 | { |
| 927 | struct dso *pos; |
| 928 | |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 929 | if (cmp_short) { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 930 | list_for_each_entry(pos, &dsos->head, node) |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 931 | if (strcmp(pos->short_name, name) == 0) |
| 932 | return pos; |
| 933 | return NULL; |
| 934 | } |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame^] | 935 | return dso__find_by_longname(&dsos->root, name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 938 | struct dso *__dsos__findnew(struct dsos *dsos, const char *name) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 939 | { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 940 | struct dso *dso = dsos__find(dsos, name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 941 | |
| 942 | if (!dso) { |
| 943 | dso = dso__new(name); |
| 944 | if (dso != NULL) { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 945 | dsos__add(dsos, dso); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 946 | dso__set_basename(dso); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | return dso; |
| 951 | } |
| 952 | |
| 953 | 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] | 954 | bool (skip)(struct dso *dso, int parm), int parm) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 955 | { |
| 956 | struct dso *pos; |
| 957 | size_t ret = 0; |
| 958 | |
| 959 | list_for_each_entry(pos, head, node) { |
Arnaldo Carvalho de Melo | 417c2ff | 2012-12-07 09:53:58 -0300 | [diff] [blame] | 960 | if (skip && skip(pos, parm)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 961 | continue; |
| 962 | ret += dso__fprintf_buildid(pos, fp); |
| 963 | ret += fprintf(fp, " %s\n", pos->long_name); |
| 964 | } |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | size_t __dsos__fprintf(struct list_head *head, FILE *fp) |
| 969 | { |
| 970 | struct dso *pos; |
| 971 | size_t ret = 0; |
| 972 | |
| 973 | list_for_each_entry(pos, head, node) { |
| 974 | int i; |
| 975 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 976 | ret += dso__fprintf(pos, i, fp); |
| 977 | } |
| 978 | |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) |
| 983 | { |
| 984 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 985 | |
| 986 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 987 | return fprintf(fp, "%s", sbuild_id); |
| 988 | } |
| 989 | |
| 990 | size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp) |
| 991 | { |
| 992 | struct rb_node *nd; |
| 993 | size_t ret = fprintf(fp, "dso: %s (", dso->short_name); |
| 994 | |
| 995 | if (dso->short_name != dso->long_name) |
| 996 | ret += fprintf(fp, "%s, ", dso->long_name); |
| 997 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], |
Stephane Eranian | 919d590 | 2012-11-20 10:51:02 +0100 | [diff] [blame] | 998 | dso__loaded(dso, type) ? "" : "NOT "); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 999 | ret += dso__fprintf_buildid(dso, fp); |
| 1000 | ret += fprintf(fp, ")\n"); |
| 1001 | for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) { |
| 1002 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 1003 | ret += symbol__fprintf(pos, fp); |
| 1004 | } |
| 1005 | |
| 1006 | return ret; |
| 1007 | } |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1008 | |
| 1009 | enum dso_type dso__type(struct dso *dso, struct machine *machine) |
| 1010 | { |
| 1011 | int fd; |
| 1012 | |
| 1013 | fd = dso__data_fd(dso, machine); |
| 1014 | if (fd < 0) |
| 1015 | return DSO__TYPE_UNKNOWN; |
| 1016 | |
| 1017 | return dso__type_fd(fd); |
| 1018 | } |