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