Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 1 | #include <asm/bug.h> |
Arnaldo Carvalho de Melo | 877a7a1 | 2017-04-17 11:39:06 -0300 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 3 | #include <sys/time.h> |
| 4 | #include <sys/resource.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 5 | #include <sys/types.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <unistd.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 8 | #include <errno.h> |
Arnaldo Carvalho de Melo | 611f0af | 2017-04-19 16:29:38 -0300 | [diff] [blame] | 9 | #include "compress.h" |
Arnaldo Carvalho de Melo | 9a3993d | 2017-04-18 11:33:48 -0300 | [diff] [blame] | 10 | #include "path.h" |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 11 | #include "symbol.h" |
| 12 | #include "dso.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 13 | #include "machine.h" |
Adrian Hunter | cfe9174 | 2015-04-09 18:53:55 +0300 | [diff] [blame] | 14 | #include "auxtrace.h" |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 15 | #include "util.h" |
| 16 | #include "debug.h" |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 17 | #include "string2.h" |
He Kuang | 6ae98ba | 2016-05-12 08:43:11 +0000 | [diff] [blame] | 18 | #include "vdso.h" |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 19 | |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 20 | static const char * const debuglink_paths[] = { |
| 21 | "%.0s%s", |
| 22 | "%s/%s", |
| 23 | "%s/.debug/%s", |
| 24 | "/usr/lib/debug%s/%s" |
| 25 | }; |
| 26 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 27 | char dso__symtab_origin(const struct dso *dso) |
| 28 | { |
| 29 | static const char origin[] = { |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 30 | [DSO_BINARY_TYPE__KALLSYMS] = 'k', |
| 31 | [DSO_BINARY_TYPE__VMLINUX] = 'v', |
| 32 | [DSO_BINARY_TYPE__JAVA_JIT] = 'j', |
| 33 | [DSO_BINARY_TYPE__DEBUGLINK] = 'l', |
| 34 | [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B', |
Krister Johansen | d239699 | 2017-07-05 18:48:13 -0700 | [diff] [blame] | 35 | [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D', |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 36 | [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f', |
| 37 | [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u', |
| 38 | [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o', |
| 39 | [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b', |
| 40 | [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd', |
| 41 | [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K', |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 42 | [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm', |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 43 | [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g', |
| 44 | [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G', |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 45 | [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M', |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 46 | [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V', |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND) |
| 50 | return '!'; |
| 51 | return origin[dso->symtab_type]; |
| 52 | } |
| 53 | |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 54 | int dso__read_binary_type_filename(const struct dso *dso, |
| 55 | enum dso_binary_type type, |
| 56 | char *root_dir, char *filename, size_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 57 | { |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 58 | char build_id_hex[SBUILD_ID_SIZE]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 59 | int ret = 0; |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 60 | size_t len; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 61 | |
| 62 | switch (type) { |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 63 | case DSO_BINARY_TYPE__DEBUGLINK: |
| 64 | { |
| 65 | const char *last_slash; |
| 66 | char dso_dir[PATH_MAX]; |
| 67 | char symfile[PATH_MAX]; |
| 68 | unsigned int i; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 69 | |
Victor Kamensky | dc6254c | 2015-01-26 22:34:02 -0800 | [diff] [blame] | 70 | len = __symbol__join_symfs(filename, size, dso->long_name); |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 71 | last_slash = filename + len; |
| 72 | while (last_slash != filename && *last_slash != '/') |
| 73 | last_slash--; |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 74 | |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 75 | strncpy(dso_dir, filename, last_slash - filename); |
| 76 | dso_dir[last_slash-filename] = '\0'; |
| 77 | |
| 78 | if (!is_regular_file(filename)) { |
| 79 | ret = -1; |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | ret = filename__read_debuglink(filename, symfile, PATH_MAX); |
| 84 | if (ret) |
Jiri Olsa | 4035672 | 2016-01-20 12:56:32 +0100 | [diff] [blame] | 85 | break; |
| 86 | |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 87 | /* Check predefined locations where debug file might reside */ |
| 88 | ret = -1; |
| 89 | for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) { |
| 90 | snprintf(filename, size, |
| 91 | debuglink_paths[i], dso_dir, symfile); |
| 92 | if (is_regular_file(filename)) { |
| 93 | ret = 0; |
| 94 | break; |
| 95 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 96 | } |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 97 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 98 | break; |
Matija Glavinic Pecotic | 9343e45 | 2017-01-17 15:50:35 +0100 | [diff] [blame] | 99 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 100 | case DSO_BINARY_TYPE__BUILD_ID_CACHE: |
Krister Johansen | d239699 | 2017-07-05 18:48:13 -0700 | [diff] [blame] | 101 | if (dso__build_id_filename(dso, filename, size, false) == NULL) |
| 102 | ret = -1; |
| 103 | break; |
| 104 | |
| 105 | case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO: |
| 106 | if (dso__build_id_filename(dso, filename, size, true) == NULL) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 107 | ret = -1; |
| 108 | break; |
| 109 | |
| 110 | case DSO_BINARY_TYPE__FEDORA_DEBUGINFO: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 111 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); |
| 112 | snprintf(filename + len, size - len, "%s.debug", dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 113 | break; |
| 114 | |
| 115 | case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 116 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug"); |
| 117 | snprintf(filename + len, size - len, "%s", dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 118 | break; |
| 119 | |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 120 | case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO: |
| 121 | { |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 122 | const char *last_slash; |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 123 | size_t dir_size; |
| 124 | |
| 125 | last_slash = dso->long_name + dso->long_name_len; |
| 126 | while (last_slash != dso->long_name && *last_slash != '/') |
| 127 | last_slash--; |
| 128 | |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 129 | len = __symbol__join_symfs(filename, size, ""); |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 130 | dir_size = last_slash - dso->long_name + 2; |
| 131 | if (dir_size > (size - len)) { |
| 132 | ret = -1; |
| 133 | break; |
| 134 | } |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 135 | len += scnprintf(filename + len, dir_size, "%s", dso->long_name); |
| 136 | len += scnprintf(filename + len , size - len, ".debug%s", |
Ricardo Ribalda Delgado | 9cd0094 | 2013-09-18 15:56:14 +0200 | [diff] [blame] | 137 | last_slash); |
| 138 | break; |
| 139 | } |
| 140 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 141 | case DSO_BINARY_TYPE__BUILDID_DEBUGINFO: |
| 142 | if (!dso->has_build_id) { |
| 143 | ret = -1; |
| 144 | break; |
| 145 | } |
| 146 | |
| 147 | build_id__sprintf(dso->build_id, |
| 148 | sizeof(dso->build_id), |
| 149 | build_id_hex); |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 150 | len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/"); |
| 151 | snprintf(filename + len, size - len, "%.2s/%s.debug", |
| 152 | build_id_hex, build_id_hex + 2); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 153 | break; |
| 154 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 155 | case DSO_BINARY_TYPE__VMLINUX: |
| 156 | case DSO_BINARY_TYPE__GUEST_VMLINUX: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 157 | case DSO_BINARY_TYPE__SYSTEM_PATH_DSO: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 158 | __symbol__join_symfs(filename, size, dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 159 | break; |
| 160 | |
| 161 | case DSO_BINARY_TYPE__GUEST_KMODULE: |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 162 | case DSO_BINARY_TYPE__GUEST_KMODULE_COMP: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 163 | path__join3(filename, size, symbol_conf.symfs, |
| 164 | root_dir, dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 165 | break; |
| 166 | |
| 167 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE: |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 168 | case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP: |
Arnaldo Carvalho de Melo | 972f393 | 2014-07-29 10:21:58 -0300 | [diff] [blame] | 169 | __symbol__join_symfs(filename, size, dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 170 | break; |
| 171 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 172 | case DSO_BINARY_TYPE__KCORE: |
| 173 | case DSO_BINARY_TYPE__GUEST_KCORE: |
Arnaldo Carvalho de Melo | 7d2a512 | 2013-12-10 16:02:50 -0300 | [diff] [blame] | 174 | snprintf(filename, size, "%s", dso->long_name); |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 175 | break; |
| 176 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 177 | default: |
| 178 | case DSO_BINARY_TYPE__KALLSYMS: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 179 | case DSO_BINARY_TYPE__GUEST_KALLSYMS: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 180 | case DSO_BINARY_TYPE__JAVA_JIT: |
| 181 | case DSO_BINARY_TYPE__NOT_FOUND: |
| 182 | ret = -1; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 189 | static const struct { |
| 190 | const char *fmt; |
| 191 | int (*decompress)(const char *input, int output); |
| 192 | } compressions[] = { |
Namhyung Kim | e92ce12 | 2014-10-31 16:51:38 +0900 | [diff] [blame] | 193 | #ifdef HAVE_ZLIB_SUPPORT |
| 194 | { "gz", gzip_decompress_to_file }, |
| 195 | #endif |
Jiri Olsa | 80a32e5b | 2015-01-29 13:29:39 +0100 | [diff] [blame] | 196 | #ifdef HAVE_LZMA_SUPPORT |
| 197 | { "xz", lzma_decompress_to_file }, |
| 198 | #endif |
Namhyung Kim | e92ce12 | 2014-10-31 16:51:38 +0900 | [diff] [blame] | 199 | { NULL, NULL }, |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | bool is_supported_compression(const char *ext) |
| 203 | { |
| 204 | unsigned i; |
| 205 | |
| 206 | for (i = 0; compressions[i].fmt; i++) { |
| 207 | if (!strcmp(ext, compressions[i].fmt)) |
| 208 | return true; |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 213 | bool is_kernel_module(const char *pathname, int cpumode) |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 214 | { |
Jiri Olsa | 8dee9ff11 | 2015-02-12 15:56:21 +0100 | [diff] [blame] | 215 | struct kmod_path m; |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 216 | int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK; |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 217 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 218 | WARN_ONCE(mode != cpumode, |
| 219 | "Internal error: passing unmasked cpumode (%x) to is_kernel_module", |
| 220 | cpumode); |
| 221 | |
| 222 | switch (mode) { |
| 223 | case PERF_RECORD_MISC_USER: |
| 224 | case PERF_RECORD_MISC_HYPERVISOR: |
| 225 | case PERF_RECORD_MISC_GUEST_USER: |
| 226 | return false; |
| 227 | /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */ |
| 228 | default: |
| 229 | if (kmod_path__parse(&m, pathname)) { |
| 230 | pr_err("Failed to check whether %s is a kernel module or not. Assume it is.", |
| 231 | pathname); |
| 232 | return true; |
| 233 | } |
| 234 | } |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 235 | |
Jiri Olsa | 8dee9ff11 | 2015-02-12 15:56:21 +0100 | [diff] [blame] | 236 | return m.kmod; |
Namhyung Kim | c00c48f | 2014-11-04 10:14:27 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | bool decompress_to_file(const char *ext, const char *filename, int output_fd) |
| 240 | { |
| 241 | unsigned i; |
| 242 | |
| 243 | for (i = 0; compressions[i].fmt; i++) { |
| 244 | if (!strcmp(ext, compressions[i].fmt)) |
| 245 | return !compressions[i].decompress(filename, |
| 246 | output_fd); |
| 247 | } |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | bool dso__needs_decompress(struct dso *dso) |
| 252 | { |
| 253 | return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP || |
| 254 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP; |
| 255 | } |
| 256 | |
Namhyung Kim | 42b3fa6 | 2017-06-08 16:31:03 +0900 | [diff] [blame] | 257 | static int decompress_kmodule(struct dso *dso, const char *name, char *tmpbuf) |
| 258 | { |
| 259 | int fd = -1; |
| 260 | struct kmod_path m; |
| 261 | |
| 262 | if (!dso__needs_decompress(dso)) |
| 263 | return -1; |
| 264 | |
| 265 | if (kmod_path__parse_ext(&m, dso->long_name)) |
| 266 | return -1; |
| 267 | |
| 268 | if (!m.comp) |
| 269 | goto out; |
| 270 | |
| 271 | fd = mkstemp(tmpbuf); |
| 272 | if (fd < 0) { |
| 273 | dso->load_errno = errno; |
| 274 | goto out; |
| 275 | } |
| 276 | |
| 277 | if (!decompress_to_file(m.ext, name, fd)) { |
| 278 | dso->load_errno = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE; |
| 279 | close(fd); |
| 280 | fd = -1; |
| 281 | } |
| 282 | |
| 283 | out: |
| 284 | free(m.ext); |
| 285 | return fd; |
| 286 | } |
| 287 | |
| 288 | int dso__decompress_kmodule_fd(struct dso *dso, const char *name) |
| 289 | { |
| 290 | char tmpbuf[] = KMOD_DECOMP_NAME; |
| 291 | int fd; |
| 292 | |
| 293 | fd = decompress_kmodule(dso, name, tmpbuf); |
| 294 | unlink(tmpbuf); |
| 295 | return fd; |
| 296 | } |
| 297 | |
| 298 | int dso__decompress_kmodule_path(struct dso *dso, const char *name, |
| 299 | char *pathname, size_t len) |
| 300 | { |
| 301 | char tmpbuf[] = KMOD_DECOMP_NAME; |
| 302 | int fd; |
| 303 | |
| 304 | fd = decompress_kmodule(dso, name, tmpbuf); |
| 305 | if (fd < 0) { |
| 306 | unlink(tmpbuf); |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | strncpy(pathname, tmpbuf, len); |
| 311 | close(fd); |
| 312 | return 0; |
| 313 | } |
| 314 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 315 | /* |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 316 | * Parses kernel module specified in @path and updates |
| 317 | * @m argument like: |
| 318 | * |
| 319 | * @comp - true if @path contains supported compression suffix, |
| 320 | * false otherwise |
| 321 | * @kmod - true if @path contains '.ko' suffix in right position, |
| 322 | * false otherwise |
| 323 | * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name |
| 324 | * of the kernel module without suffixes, otherwise strudup-ed |
| 325 | * base name of @path |
| 326 | * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string |
| 327 | * the compression suffix |
| 328 | * |
| 329 | * Returns 0 if there's no strdup error, -ENOMEM otherwise. |
| 330 | */ |
| 331 | int __kmod_path__parse(struct kmod_path *m, const char *path, |
| 332 | bool alloc_name, bool alloc_ext) |
| 333 | { |
| 334 | const char *name = strrchr(path, '/'); |
| 335 | const char *ext = strrchr(path, '.'); |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 336 | bool is_simple_name = false; |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 337 | |
| 338 | memset(m, 0x0, sizeof(*m)); |
| 339 | name = name ? name + 1 : path; |
| 340 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 341 | /* |
| 342 | * '.' is also a valid character for module name. For example: |
| 343 | * [aaa.bbb] is a valid module name. '[' should have higher |
| 344 | * priority than '.ko' suffix. |
| 345 | * |
| 346 | * The kernel names are from machine__mmap_name. Such |
| 347 | * name should belong to kernel itself, not kernel module. |
| 348 | */ |
| 349 | if (name[0] == '[') { |
| 350 | is_simple_name = true; |
| 351 | if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) || |
| 352 | (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) || |
| 353 | (strncmp(name, "[vdso]", 6) == 0) || |
| 354 | (strncmp(name, "[vsyscall]", 10) == 0)) { |
| 355 | m->kmod = false; |
| 356 | |
| 357 | } else |
| 358 | m->kmod = true; |
| 359 | } |
| 360 | |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 361 | /* No extension, just return name. */ |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 362 | if ((ext == NULL) || is_simple_name) { |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 363 | if (alloc_name) { |
| 364 | m->name = strdup(name); |
| 365 | return m->name ? 0 : -ENOMEM; |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | if (is_supported_compression(ext + 1)) { |
| 371 | m->comp = true; |
| 372 | ext -= 3; |
| 373 | } |
| 374 | |
| 375 | /* Check .ko extension only if there's enough name left. */ |
| 376 | if (ext > name) |
| 377 | m->kmod = !strncmp(ext, ".ko", 3); |
| 378 | |
| 379 | if (alloc_name) { |
| 380 | if (m->kmod) { |
| 381 | if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1) |
| 382 | return -ENOMEM; |
| 383 | } else { |
| 384 | if (asprintf(&m->name, "%s", name) == -1) |
| 385 | return -ENOMEM; |
| 386 | } |
| 387 | |
| 388 | strxfrchar(m->name, '-', '_'); |
| 389 | } |
| 390 | |
| 391 | if (alloc_ext && m->comp) { |
| 392 | m->ext = strdup(ext + 4); |
| 393 | if (!m->ext) { |
| 394 | free((void *) m->name); |
| 395 | return -ENOMEM; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
Namhyung Kim | 6b335e8 | 2017-05-31 21:01:04 +0900 | [diff] [blame] | 402 | void dso__set_module_info(struct dso *dso, struct kmod_path *m, |
| 403 | struct machine *machine) |
| 404 | { |
| 405 | if (machine__is_host(machine)) |
| 406 | dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE; |
| 407 | else |
| 408 | dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE; |
| 409 | |
| 410 | /* _KMODULE_COMP should be next to _KMODULE */ |
| 411 | if (m->kmod && m->comp) |
| 412 | dso->symtab_type++; |
| 413 | |
| 414 | dso__set_short_name(dso, strdup(m->name), true); |
| 415 | } |
| 416 | |
Jiri Olsa | 3c8a67f | 2015-02-05 15:40:25 +0100 | [diff] [blame] | 417 | /* |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 418 | * Global list of open DSOs and the counter. |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 419 | */ |
| 420 | static LIST_HEAD(dso__data_open); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 421 | static long dso__data_open_cnt; |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 422 | static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 423 | |
| 424 | static void dso__list_add(struct dso *dso) |
| 425 | { |
| 426 | list_add_tail(&dso->data.open_entry, &dso__data_open); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 427 | dso__data_open_cnt++; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static void dso__list_del(struct dso *dso) |
| 431 | { |
| 432 | list_del(&dso->data.open_entry); |
Jiri Olsa | bda6ee4 | 2014-04-30 15:25:10 +0200 | [diff] [blame] | 433 | WARN_ONCE(dso__data_open_cnt <= 0, |
| 434 | "DSO data fd counter out of bounds."); |
| 435 | dso__data_open_cnt--; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 438 | static void close_first_dso(void); |
| 439 | |
| 440 | static int do_open(char *name) |
| 441 | { |
| 442 | int fd; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 443 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 444 | |
| 445 | do { |
| 446 | fd = open(name, O_RDONLY); |
| 447 | if (fd >= 0) |
| 448 | return fd; |
| 449 | |
Namhyung Kim | a3c0cc2 | 2015-01-30 11:33:29 +0900 | [diff] [blame] | 450 | pr_debug("dso open failed: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 451 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 452 | if (!dso__data_open_cnt || errno != EMFILE) |
| 453 | break; |
| 454 | |
| 455 | close_first_dso(); |
| 456 | } while (1); |
| 457 | |
| 458 | return -1; |
| 459 | } |
| 460 | |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 461 | static int __open_dso(struct dso *dso, struct machine *machine) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 462 | { |
Namhyung Kim | 8ba29ad | 2017-06-08 16:31:06 +0900 | [diff] [blame] | 463 | int fd = -EINVAL; |
Arnaldo Carvalho de Melo | ee4e962 | 2013-12-16 17:03:18 -0300 | [diff] [blame] | 464 | char *root_dir = (char *)""; |
| 465 | char *name = malloc(PATH_MAX); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 466 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 467 | if (!name) |
| 468 | return -ENOMEM; |
| 469 | |
| 470 | if (machine) |
| 471 | root_dir = machine->root_dir; |
| 472 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 473 | if (dso__read_binary_type_filename(dso, dso->binary_type, |
Namhyung Kim | 8ba29ad | 2017-06-08 16:31:06 +0900 | [diff] [blame] | 474 | root_dir, name, PATH_MAX)) |
| 475 | goto out; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 476 | |
Namhyung Kim | 8ba29ad | 2017-06-08 16:31:06 +0900 | [diff] [blame] | 477 | if (!is_regular_file(name)) |
| 478 | goto out; |
Jiri Olsa | 3c028a0 | 2016-09-20 18:12:45 +0200 | [diff] [blame] | 479 | |
Namhyung Kim | 1d6b3c9 | 2017-06-08 16:31:05 +0900 | [diff] [blame] | 480 | if (dso__needs_decompress(dso)) { |
| 481 | char newpath[KMOD_DECOMP_LEN]; |
| 482 | size_t len = sizeof(newpath); |
| 483 | |
| 484 | if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) { |
Namhyung Kim | 8ba29ad | 2017-06-08 16:31:06 +0900 | [diff] [blame] | 485 | fd = -dso->load_errno; |
| 486 | goto out; |
Namhyung Kim | 1d6b3c9 | 2017-06-08 16:31:05 +0900 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | strcpy(name, newpath); |
| 490 | } |
| 491 | |
Jiri Olsa | a08cae0 | 2014-05-07 21:35:02 +0200 | [diff] [blame] | 492 | fd = do_open(name); |
Namhyung Kim | 1d6b3c9 | 2017-06-08 16:31:05 +0900 | [diff] [blame] | 493 | |
| 494 | if (dso__needs_decompress(dso)) |
| 495 | unlink(name); |
| 496 | |
Namhyung Kim | 8ba29ad | 2017-06-08 16:31:06 +0900 | [diff] [blame] | 497 | out: |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 498 | free(name); |
| 499 | return fd; |
| 500 | } |
| 501 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 502 | static void check_data_close(void); |
| 503 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 504 | /** |
| 505 | * dso_close - Open DSO data file |
| 506 | * @dso: dso object |
| 507 | * |
| 508 | * Open @dso's data file descriptor and updates |
| 509 | * list/count of open DSO objects. |
| 510 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 511 | static int open_dso(struct dso *dso, struct machine *machine) |
| 512 | { |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 513 | int fd; |
| 514 | struct nscookie nsc; |
| 515 | |
| 516 | if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) |
| 517 | nsinfo__mountns_enter(dso->nsinfo, &nsc); |
| 518 | fd = __open_dso(dso, machine); |
| 519 | if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) |
| 520 | nsinfo__mountns_exit(&nsc); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 521 | |
Adrian Hunter | a6f6ae9 | 2014-07-17 11:43:09 +0300 | [diff] [blame] | 522 | if (fd >= 0) { |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 523 | dso__list_add(dso); |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 524 | /* |
| 525 | * Check if we crossed the allowed number |
| 526 | * of opened DSOs and close one if needed. |
| 527 | */ |
| 528 | check_data_close(); |
| 529 | } |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 530 | |
| 531 | return fd; |
| 532 | } |
| 533 | |
| 534 | static void close_data_fd(struct dso *dso) |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 535 | { |
| 536 | if (dso->data.fd >= 0) { |
| 537 | close(dso->data.fd); |
| 538 | dso->data.fd = -1; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 539 | dso->data.file_size = 0; |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 540 | dso__list_del(dso); |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 544 | /** |
| 545 | * dso_close - Close DSO data file |
| 546 | * @dso: dso object |
| 547 | * |
| 548 | * Close @dso's data file descriptor and updates |
| 549 | * list/count of open DSO objects. |
| 550 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 551 | static void close_dso(struct dso *dso) |
| 552 | { |
| 553 | close_data_fd(dso); |
| 554 | } |
| 555 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 556 | static void close_first_dso(void) |
| 557 | { |
| 558 | struct dso *dso; |
| 559 | |
| 560 | dso = list_first_entry(&dso__data_open, struct dso, data.open_entry); |
| 561 | close_dso(dso); |
| 562 | } |
| 563 | |
| 564 | static rlim_t get_fd_limit(void) |
| 565 | { |
| 566 | struct rlimit l; |
| 567 | rlim_t limit = 0; |
| 568 | |
| 569 | /* Allow half of the current open fd limit. */ |
| 570 | if (getrlimit(RLIMIT_NOFILE, &l) == 0) { |
| 571 | if (l.rlim_cur == RLIM_INFINITY) |
| 572 | limit = l.rlim_cur; |
| 573 | else |
| 574 | limit = l.rlim_cur / 2; |
| 575 | } else { |
| 576 | pr_err("failed to get fd limit\n"); |
| 577 | limit = 1; |
| 578 | } |
| 579 | |
| 580 | return limit; |
| 581 | } |
| 582 | |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 583 | static rlim_t fd_limit; |
| 584 | |
| 585 | /* |
| 586 | * Used only by tests/dso-data.c to reset the environment |
| 587 | * for tests. I dont expect we should change this during |
| 588 | * standard runtime. |
| 589 | */ |
| 590 | void reset_fd_limit(void) |
| 591 | { |
| 592 | fd_limit = 0; |
| 593 | } |
| 594 | |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 595 | static bool may_cache_fd(void) |
| 596 | { |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 597 | if (!fd_limit) |
| 598 | fd_limit = get_fd_limit(); |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 599 | |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 600 | if (fd_limit == RLIM_INFINITY) |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 601 | return true; |
| 602 | |
Jiri Olsa | f306924 | 2016-06-28 13:29:02 +0200 | [diff] [blame] | 603 | return fd_limit > (rlim_t) dso__data_open_cnt; |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 604 | } |
| 605 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 606 | /* |
| 607 | * Check and close LRU dso if we crossed allowed limit |
| 608 | * for opened dso file descriptors. The limit is half |
| 609 | * of the RLIMIT_NOFILE files opened. |
| 610 | */ |
Jiri Olsa | c658045 | 2014-04-30 15:47:27 +0200 | [diff] [blame] | 611 | static void check_data_close(void) |
| 612 | { |
| 613 | bool cache_fd = may_cache_fd(); |
| 614 | |
| 615 | if (!cache_fd) |
| 616 | close_first_dso(); |
| 617 | } |
| 618 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 619 | /** |
| 620 | * dso__data_close - Close DSO data file |
| 621 | * @dso: dso object |
| 622 | * |
| 623 | * External interface to close @dso's data file descriptor. |
| 624 | */ |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 625 | void dso__data_close(struct dso *dso) |
| 626 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 627 | pthread_mutex_lock(&dso__data_open_lock); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 628 | close_dso(dso); |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 629 | pthread_mutex_unlock(&dso__data_open_lock); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 630 | } |
| 631 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 632 | static void try_to_open_dso(struct dso *dso, struct machine *machine) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 633 | { |
Arnaldo Carvalho de Melo | 631d34b | 2013-12-16 16:57:43 -0300 | [diff] [blame] | 634 | enum dso_binary_type binary_type_data[] = { |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 635 | DSO_BINARY_TYPE__BUILD_ID_CACHE, |
| 636 | DSO_BINARY_TYPE__SYSTEM_PATH_DSO, |
| 637 | DSO_BINARY_TYPE__NOT_FOUND, |
| 638 | }; |
| 639 | int i = 0; |
| 640 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 641 | if (dso->data.fd >= 0) |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 642 | return; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 643 | |
| 644 | if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) { |
| 645 | dso->data.fd = open_dso(dso, machine); |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 646 | goto out; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 647 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 648 | |
| 649 | do { |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 650 | dso->binary_type = binary_type_data[i++]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 651 | |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 652 | dso->data.fd = open_dso(dso, machine); |
| 653 | if (dso->data.fd >= 0) |
| 654 | goto out; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 655 | |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 656 | } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND); |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 657 | out: |
| 658 | if (dso->data.fd >= 0) |
| 659 | dso->data.status = DSO_DATA_STATUS_OK; |
| 660 | else |
| 661 | dso->data.status = DSO_DATA_STATUS_ERROR; |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 662 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 663 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 664 | /** |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 665 | * dso__data_get_fd - Get dso's data file descriptor |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 666 | * @dso: dso object |
| 667 | * @machine: machine object |
| 668 | * |
| 669 | * External interface to find dso's file, open it and |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 670 | * returns file descriptor. It should be paired with |
| 671 | * dso__data_put_fd() if it returns non-negative value. |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 672 | */ |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 673 | int dso__data_get_fd(struct dso *dso, struct machine *machine) |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 674 | { |
| 675 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
| 676 | return -1; |
| 677 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 678 | if (pthread_mutex_lock(&dso__data_open_lock) < 0) |
| 679 | return -1; |
| 680 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 681 | try_to_open_dso(dso, machine); |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 682 | |
| 683 | if (dso->data.fd < 0) |
| 684 | pthread_mutex_unlock(&dso__data_open_lock); |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 685 | |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 686 | return dso->data.fd; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 689 | void dso__data_put_fd(struct dso *dso __maybe_unused) |
| 690 | { |
| 691 | pthread_mutex_unlock(&dso__data_open_lock); |
| 692 | } |
| 693 | |
Adrian Hunter | 288be94 | 2014-07-22 16:17:19 +0300 | [diff] [blame] | 694 | bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) |
| 695 | { |
| 696 | u32 flag = 1 << by; |
| 697 | |
| 698 | if (dso->data.status_seen & flag) |
| 699 | return true; |
| 700 | |
| 701 | dso->data.status_seen |= flag; |
| 702 | |
| 703 | return false; |
| 704 | } |
| 705 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 706 | static void |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 707 | dso_cache__free(struct dso *dso) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 708 | { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 709 | struct rb_root *root = &dso->data.cache; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 710 | struct rb_node *next = rb_first(root); |
| 711 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 712 | pthread_mutex_lock(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 713 | while (next) { |
| 714 | struct dso_cache *cache; |
| 715 | |
| 716 | cache = rb_entry(next, struct dso_cache, rb_node); |
| 717 | next = rb_next(&cache->rb_node); |
| 718 | rb_erase(&cache->rb_node, root); |
| 719 | free(cache); |
| 720 | } |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 721 | pthread_mutex_unlock(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 724 | static struct dso_cache *dso_cache__find(struct dso *dso, u64 offset) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 725 | { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 726 | const struct rb_root *root = &dso->data.cache; |
Arnaldo Carvalho de Melo | 3344996 | 2013-12-10 15:46:29 -0300 | [diff] [blame] | 727 | struct rb_node * const *p = &root->rb_node; |
| 728 | const struct rb_node *parent = NULL; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 729 | struct dso_cache *cache; |
| 730 | |
| 731 | while (*p != NULL) { |
| 732 | u64 end; |
| 733 | |
| 734 | parent = *p; |
| 735 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 736 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 737 | |
| 738 | if (offset < cache->offset) |
| 739 | p = &(*p)->rb_left; |
| 740 | else if (offset >= end) |
| 741 | p = &(*p)->rb_right; |
| 742 | else |
| 743 | return cache; |
| 744 | } |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 745 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 746 | return NULL; |
| 747 | } |
| 748 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 749 | static struct dso_cache * |
| 750 | dso_cache__insert(struct dso *dso, struct dso_cache *new) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 751 | { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 752 | struct rb_root *root = &dso->data.cache; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 753 | struct rb_node **p = &root->rb_node; |
| 754 | struct rb_node *parent = NULL; |
| 755 | struct dso_cache *cache; |
| 756 | u64 offset = new->offset; |
| 757 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 758 | pthread_mutex_lock(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 759 | while (*p != NULL) { |
| 760 | u64 end; |
| 761 | |
| 762 | parent = *p; |
| 763 | cache = rb_entry(parent, struct dso_cache, rb_node); |
| 764 | end = cache->offset + DSO__DATA_CACHE_SIZE; |
| 765 | |
| 766 | if (offset < cache->offset) |
| 767 | p = &(*p)->rb_left; |
| 768 | else if (offset >= end) |
| 769 | p = &(*p)->rb_right; |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 770 | else |
| 771 | goto out; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | rb_link_node(&new->rb_node, parent, p); |
| 775 | rb_insert_color(&new->rb_node, root); |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 776 | |
| 777 | cache = NULL; |
| 778 | out: |
| 779 | pthread_mutex_unlock(&dso->lock); |
| 780 | return cache; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static ssize_t |
| 784 | dso_cache__memcpy(struct dso_cache *cache, u64 offset, |
| 785 | u8 *data, u64 size) |
| 786 | { |
| 787 | u64 cache_offset = offset - cache->offset; |
| 788 | u64 cache_size = min(cache->size - cache_offset, size); |
| 789 | |
| 790 | memcpy(data, cache->data + cache_offset, cache_size); |
| 791 | return cache_size; |
| 792 | } |
| 793 | |
| 794 | static ssize_t |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 795 | dso_cache__read(struct dso *dso, struct machine *machine, |
| 796 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 797 | { |
| 798 | struct dso_cache *cache; |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 799 | struct dso_cache *old; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 800 | ssize_t ret; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 801 | |
| 802 | do { |
| 803 | u64 cache_offset; |
| 804 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 805 | cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE); |
| 806 | if (!cache) |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 807 | return -ENOMEM; |
| 808 | |
| 809 | pthread_mutex_lock(&dso__data_open_lock); |
| 810 | |
| 811 | /* |
| 812 | * dso->data.fd might be closed if other thread opened another |
| 813 | * file (dso) due to open file limit (RLIMIT_NOFILE). |
| 814 | */ |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 815 | try_to_open_dso(dso, machine); |
| 816 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 817 | if (dso->data.fd < 0) { |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 818 | ret = -errno; |
| 819 | dso->data.status = DSO_DATA_STATUS_ERROR; |
| 820 | break; |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 821 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 822 | |
| 823 | cache_offset = offset & DSO__DATA_CACHE_MASK; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 824 | |
Namhyung Kim | c52686f | 2015-01-29 17:02:01 -0300 | [diff] [blame] | 825 | 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] | 826 | if (ret <= 0) |
| 827 | break; |
| 828 | |
| 829 | cache->offset = cache_offset; |
| 830 | cache->size = ret; |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 831 | } while (0); |
| 832 | |
| 833 | pthread_mutex_unlock(&dso__data_open_lock); |
| 834 | |
| 835 | if (ret > 0) { |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 836 | old = dso_cache__insert(dso, cache); |
| 837 | if (old) { |
| 838 | /* we lose the race */ |
| 839 | free(cache); |
| 840 | cache = old; |
| 841 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 842 | |
| 843 | ret = dso_cache__memcpy(cache, offset, data, size); |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 844 | } |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 845 | |
| 846 | if (ret <= 0) |
| 847 | free(cache); |
| 848 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 849 | return ret; |
| 850 | } |
| 851 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 852 | static ssize_t dso_cache_read(struct dso *dso, struct machine *machine, |
| 853 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 854 | { |
| 855 | struct dso_cache *cache; |
| 856 | |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 857 | cache = dso_cache__find(dso, offset); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 858 | if (cache) |
| 859 | return dso_cache__memcpy(cache, offset, data, size); |
| 860 | else |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 861 | return dso_cache__read(dso, machine, offset, data, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 864 | /* |
| 865 | * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks |
| 866 | * in the rb_tree. Any read to already cached data is served |
| 867 | * by cached data. |
| 868 | */ |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 869 | static ssize_t cached_read(struct dso *dso, struct machine *machine, |
| 870 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 871 | { |
| 872 | ssize_t r = 0; |
| 873 | u8 *p = data; |
| 874 | |
| 875 | do { |
| 876 | ssize_t ret; |
| 877 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 878 | ret = dso_cache_read(dso, machine, offset, p, size); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 879 | if (ret < 0) |
| 880 | return ret; |
| 881 | |
| 882 | /* Reached EOF, return what we have. */ |
| 883 | if (!ret) |
| 884 | break; |
| 885 | |
| 886 | BUG_ON(ret > size); |
| 887 | |
| 888 | r += ret; |
| 889 | p += ret; |
| 890 | offset += ret; |
| 891 | size -= ret; |
| 892 | |
| 893 | } while (size); |
| 894 | |
| 895 | return r; |
| 896 | } |
| 897 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 898 | static int data_file_size(struct dso *dso, struct machine *machine) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 899 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 900 | int ret = 0; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 901 | struct stat st; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 902 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 903 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 904 | if (dso->data.file_size) |
| 905 | return 0; |
| 906 | |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 907 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
| 908 | return -1; |
| 909 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 910 | pthread_mutex_lock(&dso__data_open_lock); |
| 911 | |
| 912 | /* |
| 913 | * dso->data.fd might be closed if other thread opened another |
| 914 | * file (dso) due to open file limit (RLIMIT_NOFILE). |
| 915 | */ |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 916 | try_to_open_dso(dso, machine); |
| 917 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 918 | if (dso->data.fd < 0) { |
Namhyung Kim | 71ff824 | 2015-05-21 01:03:39 +0900 | [diff] [blame] | 919 | ret = -errno; |
| 920 | dso->data.status = DSO_DATA_STATUS_ERROR; |
| 921 | goto out; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 922 | } |
| 923 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 924 | if (fstat(dso->data.fd, &st) < 0) { |
| 925 | ret = -errno; |
| 926 | pr_err("dso cache fstat failed: %s\n", |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 927 | str_error_r(errno, sbuf, sizeof(sbuf))); |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 928 | dso->data.status = DSO_DATA_STATUS_ERROR; |
| 929 | goto out; |
| 930 | } |
| 931 | dso->data.file_size = st.st_size; |
| 932 | |
| 933 | out: |
| 934 | pthread_mutex_unlock(&dso__data_open_lock); |
| 935 | return ret; |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Adrian Hunter | 6d36345 | 2014-07-22 16:17:35 +0300 | [diff] [blame] | 938 | /** |
| 939 | * dso__data_size - Return dso data size |
| 940 | * @dso: dso object |
| 941 | * @machine: machine object |
| 942 | * |
| 943 | * Return: dso data size |
| 944 | */ |
| 945 | off_t dso__data_size(struct dso *dso, struct machine *machine) |
| 946 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 947 | if (data_file_size(dso, machine)) |
Adrian Hunter | 6d36345 | 2014-07-22 16:17:35 +0300 | [diff] [blame] | 948 | return -1; |
| 949 | |
| 950 | /* For now just estimate dso data size is close to file size */ |
| 951 | return dso->data.file_size; |
| 952 | } |
| 953 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 954 | static ssize_t data_read_offset(struct dso *dso, struct machine *machine, |
| 955 | u64 offset, u8 *data, ssize_t size) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 956 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 957 | if (data_file_size(dso, machine)) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 958 | return -1; |
| 959 | |
| 960 | /* Check the offset sanity. */ |
| 961 | if (offset > dso->data.file_size) |
| 962 | return -1; |
| 963 | |
| 964 | if (offset + size < offset) |
| 965 | return -1; |
| 966 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 967 | return cached_read(dso, machine, offset, data, size); |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 970 | /** |
| 971 | * dso__data_read_offset - Read data from dso file offset |
| 972 | * @dso: dso object |
| 973 | * @machine: machine object |
| 974 | * @offset: file offset |
| 975 | * @data: buffer to store data |
| 976 | * @size: size of the @data buffer |
| 977 | * |
| 978 | * External interface to read data from dso file offset. Open |
| 979 | * dso data file and use cached_read to get the data. |
| 980 | */ |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 981 | ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine, |
| 982 | u64 offset, u8 *data, ssize_t size) |
| 983 | { |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 984 | if (dso->data.status == DSO_DATA_STATUS_ERROR) |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 985 | return -1; |
| 986 | |
Namhyung Kim | 33bdedc | 2015-05-18 09:30:42 +0900 | [diff] [blame] | 987 | return data_read_offset(dso, machine, offset, data, size); |
Jiri Olsa | c3fbd2a | 2014-05-07 18:51:41 +0200 | [diff] [blame] | 988 | } |
| 989 | |
Jiri Olsa | c1f9aa0 | 2014-05-07 21:09:59 +0200 | [diff] [blame] | 990 | /** |
| 991 | * dso__data_read_addr - Read data from dso address |
| 992 | * @dso: dso object |
| 993 | * @machine: machine object |
| 994 | * @add: virtual memory address |
| 995 | * @data: buffer to store data |
| 996 | * @size: size of the @data buffer |
| 997 | * |
| 998 | * External interface to read data from dso address. |
| 999 | */ |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1000 | ssize_t dso__data_read_addr(struct dso *dso, struct map *map, |
| 1001 | struct machine *machine, u64 addr, |
| 1002 | u8 *data, ssize_t size) |
| 1003 | { |
| 1004 | u64 offset = map->map_ip(map, addr); |
| 1005 | return dso__data_read_offset(dso, machine, offset, data, size); |
| 1006 | } |
| 1007 | |
| 1008 | struct map *dso__new_map(const char *name) |
| 1009 | { |
| 1010 | struct map *map = NULL; |
| 1011 | struct dso *dso = dso__new(name); |
| 1012 | |
| 1013 | if (dso) |
| 1014 | map = map__new2(0, dso, MAP__FUNCTION); |
| 1015 | |
| 1016 | return map; |
| 1017 | } |
| 1018 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 1019 | struct dso *machine__findnew_kernel(struct machine *machine, const char *name, |
| 1020 | const char *short_name, int dso_type) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1021 | { |
| 1022 | /* |
| 1023 | * The kernel dso could be created by build_id processing. |
| 1024 | */ |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 1025 | struct dso *dso = machine__findnew_dso(machine, name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1026 | |
| 1027 | /* |
| 1028 | * We need to run this in all cases, since during the build_id |
| 1029 | * processing we had no idea this was the kernel dso. |
| 1030 | */ |
| 1031 | if (dso != NULL) { |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1032 | dso__set_short_name(dso, short_name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1033 | dso->kernel = dso_type; |
| 1034 | } |
| 1035 | |
| 1036 | return dso; |
| 1037 | } |
| 1038 | |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1039 | /* |
| 1040 | * Find a matching entry and/or link current entry to RB tree. |
| 1041 | * Either one of the dso or name parameter must be non-NULL or the |
| 1042 | * function will not work. |
| 1043 | */ |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1044 | static struct dso *__dso__findlink_by_longname(struct rb_root *root, |
| 1045 | struct dso *dso, const char *name) |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1046 | { |
| 1047 | struct rb_node **p = &root->rb_node; |
| 1048 | struct rb_node *parent = NULL; |
| 1049 | |
| 1050 | if (!name) |
| 1051 | name = dso->long_name; |
| 1052 | /* |
| 1053 | * Find node with the matching name |
| 1054 | */ |
| 1055 | while (*p) { |
| 1056 | struct dso *this = rb_entry(*p, struct dso, rb_node); |
| 1057 | int rc = strcmp(name, this->long_name); |
| 1058 | |
| 1059 | parent = *p; |
| 1060 | if (rc == 0) { |
| 1061 | /* |
| 1062 | * In case the new DSO is a duplicate of an existing |
Masahiro Yamada | 0f5e155 | 2017-02-27 14:28:52 -0800 | [diff] [blame] | 1063 | * one, print a one-time warning & put the new entry |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1064 | * at the end of the list of duplicates. |
| 1065 | */ |
| 1066 | if (!dso || (dso == this)) |
| 1067 | return this; /* Find matching dso */ |
| 1068 | /* |
| 1069 | * The core kernel DSOs may have duplicated long name. |
| 1070 | * In this case, the short name should be different. |
| 1071 | * Comparing the short names to differentiate the DSOs. |
| 1072 | */ |
| 1073 | rc = strcmp(dso->short_name, this->short_name); |
| 1074 | if (rc == 0) { |
| 1075 | pr_err("Duplicated dso name: %s\n", name); |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | } |
| 1079 | if (rc < 0) |
| 1080 | p = &parent->rb_left; |
| 1081 | else |
| 1082 | p = &parent->rb_right; |
| 1083 | } |
| 1084 | if (dso) { |
| 1085 | /* Add new node and rebalance tree */ |
| 1086 | rb_link_node(&dso->rb_node, parent, p); |
| 1087 | rb_insert_color(&dso->rb_node, root); |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1088 | dso->root = root; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1089 | } |
| 1090 | return NULL; |
| 1091 | } |
| 1092 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1093 | static inline struct dso *__dso__find_by_longname(struct rb_root *root, |
| 1094 | const char *name) |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1095 | { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1096 | return __dso__findlink_by_longname(root, NULL, name); |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 1099 | 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] | 1100 | { |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1101 | struct rb_root *root = dso->root; |
| 1102 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1103 | if (name == NULL) |
| 1104 | return; |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1105 | |
| 1106 | if (dso->long_name_allocated) |
Arnaldo Carvalho de Melo | bf4414a | 2013-12-10 15:19:23 -0300 | [diff] [blame] | 1107 | free((char *)dso->long_name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1108 | |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1109 | if (root) { |
| 1110 | rb_erase(&dso->rb_node, root); |
| 1111 | /* |
| 1112 | * __dso__findlink_by_longname() isn't guaranteed to add it |
| 1113 | * back, so a clean removal is required here. |
| 1114 | */ |
| 1115 | RB_CLEAR_NODE(&dso->rb_node); |
| 1116 | dso->root = NULL; |
| 1117 | } |
| 1118 | |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1119 | dso->long_name = name; |
| 1120 | dso->long_name_len = strlen(name); |
| 1121 | dso->long_name_allocated = name_allocated; |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1122 | |
| 1123 | if (root) |
| 1124 | __dso__findlink_by_longname(root, dso, NULL); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1127 | 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] | 1128 | { |
| 1129 | if (name == NULL) |
| 1130 | return; |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1131 | |
| 1132 | if (dso->short_name_allocated) |
| 1133 | free((char *)dso->short_name); |
| 1134 | |
| 1135 | dso->short_name = name; |
| 1136 | dso->short_name_len = strlen(name); |
| 1137 | dso->short_name_allocated = name_allocated; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | static void dso__set_basename(struct dso *dso) |
| 1141 | { |
Stephane Eranian | ac5e7f8 | 2013-12-05 19:26:42 +0100 | [diff] [blame] | 1142 | /* |
| 1143 | * basename() may modify path buffer, so we must pass |
| 1144 | * a copy. |
| 1145 | */ |
| 1146 | char *base, *lname = strdup(dso->long_name); |
| 1147 | |
| 1148 | if (!lname) |
| 1149 | return; |
| 1150 | |
| 1151 | /* |
| 1152 | * basename() may return a pointer to internal |
| 1153 | * storage which is reused in subsequent calls |
| 1154 | * so copy the result. |
| 1155 | */ |
| 1156 | base = strdup(basename(lname)); |
| 1157 | |
| 1158 | free(lname); |
| 1159 | |
| 1160 | if (!base) |
| 1161 | return; |
| 1162 | |
| 1163 | dso__set_short_name(dso, base, true); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | int dso__name_len(const struct dso *dso) |
| 1167 | { |
| 1168 | if (!dso) |
| 1169 | return strlen("[unknown]"); |
Namhyung Kim | bb963e1 | 2017-02-17 17:17:38 +0900 | [diff] [blame] | 1170 | if (verbose > 0) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1171 | return dso->long_name_len; |
| 1172 | |
| 1173 | return dso->short_name_len; |
| 1174 | } |
| 1175 | |
| 1176 | bool dso__loaded(const struct dso *dso, enum map_type type) |
| 1177 | { |
| 1178 | return dso->loaded & (1 << type); |
| 1179 | } |
| 1180 | |
| 1181 | bool dso__sorted_by_name(const struct dso *dso, enum map_type type) |
| 1182 | { |
| 1183 | return dso->sorted_by_name & (1 << type); |
| 1184 | } |
| 1185 | |
| 1186 | void dso__set_sorted_by_name(struct dso *dso, enum map_type type) |
| 1187 | { |
| 1188 | dso->sorted_by_name |= (1 << type); |
| 1189 | } |
| 1190 | |
| 1191 | struct dso *dso__new(const char *name) |
| 1192 | { |
| 1193 | struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1); |
| 1194 | |
| 1195 | if (dso != NULL) { |
| 1196 | int i; |
| 1197 | strcpy(dso->name, name); |
Arnaldo Carvalho de Melo | 7e155d4 | 2013-12-10 15:08:44 -0300 | [diff] [blame] | 1198 | dso__set_long_name(dso, dso->name, false); |
Adrian Hunter | 58a98c9 | 2013-12-10 11:11:46 -0300 | [diff] [blame] | 1199 | dso__set_short_name(dso, dso->name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1200 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1201 | dso->symbols[i] = dso->symbol_names[i] = RB_ROOT; |
Jiri Olsa | ca40e2a | 2014-05-07 18:30:45 +0200 | [diff] [blame] | 1202 | dso->data.cache = RB_ROOT; |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 1203 | dso->data.fd = -1; |
Adrian Hunter | c27697d | 2014-07-22 16:17:18 +0300 | [diff] [blame] | 1204 | dso->data.status = DSO_DATA_STATUS_UNKNOWN; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1205 | dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND; |
Arnaldo Carvalho de Melo | 5f70619 | 2013-12-17 16:14:07 -0300 | [diff] [blame] | 1206 | dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND; |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 1207 | dso->is_64_bit = (sizeof(void *) == 8); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1208 | dso->loaded = 0; |
Adrian Hunter | 0131c4e | 2013-08-07 14:38:50 +0300 | [diff] [blame] | 1209 | dso->rel = 0; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1210 | dso->sorted_by_name = 0; |
| 1211 | dso->has_build_id = 0; |
Namhyung Kim | 2cc9d0e | 2013-09-11 14:09:31 +0900 | [diff] [blame] | 1212 | dso->has_srcline = 1; |
Adrian Hunter | 906049c8 | 2013-12-03 09:23:10 +0200 | [diff] [blame] | 1213 | dso->a2l_fails = 1; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1214 | dso->kernel = DSO_TYPE_USER; |
| 1215 | dso->needs_swap = DSO_SWAP__UNSET; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1216 | RB_CLEAR_NODE(&dso->rb_node); |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 1217 | dso->root = NULL; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1218 | INIT_LIST_HEAD(&dso->node); |
Jiri Olsa | eba5102 | 2014-04-30 15:00:59 +0200 | [diff] [blame] | 1219 | INIT_LIST_HEAD(&dso->data.open_entry); |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1220 | pthread_mutex_init(&dso->lock, NULL); |
Elena Reshetova | 7100810 | 2017-02-21 17:34:58 +0200 | [diff] [blame] | 1221 | refcount_set(&dso->refcnt, 1); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | return dso; |
| 1225 | } |
| 1226 | |
| 1227 | void dso__delete(struct dso *dso) |
| 1228 | { |
| 1229 | int i; |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 1230 | |
| 1231 | if (!RB_EMPTY_NODE(&dso->rb_node)) |
| 1232 | pr_err("DSO %s is still in rbtree when being deleted!\n", |
| 1233 | dso->long_name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1234 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1235 | symbols__delete(&dso->symbols[i]); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 1236 | |
| 1237 | if (dso->short_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1238 | zfree((char **)&dso->short_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 1239 | dso->short_name_allocated = false; |
| 1240 | } |
| 1241 | |
| 1242 | if (dso->long_name_allocated) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1243 | zfree((char **)&dso->long_name); |
Arnaldo Carvalho de Melo | ee021d4 | 2013-12-10 15:26:55 -0300 | [diff] [blame] | 1244 | dso->long_name_allocated = false; |
| 1245 | } |
| 1246 | |
Jiri Olsa | 53fa8eaa | 2014-04-28 16:43:43 +0200 | [diff] [blame] | 1247 | dso__data_close(dso); |
Adrian Hunter | cfe9174 | 2015-04-09 18:53:55 +0300 | [diff] [blame] | 1248 | auxtrace_cache__free(dso->auxtrace_cache); |
Namhyung Kim | 8e67b72 | 2015-05-18 09:30:41 +0900 | [diff] [blame] | 1249 | dso_cache__free(dso); |
Adrian Hunter | 454ff00 | 2013-12-03 09:23:07 +0200 | [diff] [blame] | 1250 | dso__free_a2l(dso); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 1251 | zfree(&dso->symsrc_filename); |
Krister Johansen | 843ff37 | 2017-07-05 18:48:08 -0700 | [diff] [blame] | 1252 | nsinfo__zput(dso->nsinfo); |
Namhyung Kim | 4a936ed | 2015-05-18 09:30:40 +0900 | [diff] [blame] | 1253 | pthread_mutex_destroy(&dso->lock); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1254 | free(dso); |
| 1255 | } |
| 1256 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1257 | struct dso *dso__get(struct dso *dso) |
| 1258 | { |
| 1259 | if (dso) |
Elena Reshetova | 7100810 | 2017-02-21 17:34:58 +0200 | [diff] [blame] | 1260 | refcount_inc(&dso->refcnt); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1261 | return dso; |
| 1262 | } |
| 1263 | |
| 1264 | void dso__put(struct dso *dso) |
| 1265 | { |
Elena Reshetova | 7100810 | 2017-02-21 17:34:58 +0200 | [diff] [blame] | 1266 | if (dso && refcount_dec_and_test(&dso->refcnt)) |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1267 | dso__delete(dso); |
| 1268 | } |
| 1269 | |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1270 | void dso__set_build_id(struct dso *dso, void *build_id) |
| 1271 | { |
| 1272 | memcpy(dso->build_id, build_id, sizeof(dso->build_id)); |
| 1273 | dso->has_build_id = 1; |
| 1274 | } |
| 1275 | |
| 1276 | bool dso__build_id_equal(const struct dso *dso, u8 *build_id) |
| 1277 | { |
| 1278 | return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0; |
| 1279 | } |
| 1280 | |
| 1281 | void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine) |
| 1282 | { |
| 1283 | char path[PATH_MAX]; |
| 1284 | |
| 1285 | if (machine__is_default_guest(machine)) |
| 1286 | return; |
| 1287 | sprintf(path, "%s/sys/kernel/notes", machine->root_dir); |
| 1288 | if (sysfs__read_build_id(path, dso->build_id, |
| 1289 | sizeof(dso->build_id)) == 0) |
| 1290 | dso->has_build_id = true; |
| 1291 | } |
| 1292 | |
| 1293 | int dso__kernel_module_get_build_id(struct dso *dso, |
| 1294 | const char *root_dir) |
| 1295 | { |
| 1296 | char filename[PATH_MAX]; |
| 1297 | /* |
| 1298 | * kernel module short names are of the form "[module]" and |
| 1299 | * we need just "module" here. |
| 1300 | */ |
| 1301 | const char *name = dso->short_name + 1; |
| 1302 | |
| 1303 | snprintf(filename, sizeof(filename), |
| 1304 | "%s/sys/module/%.*s/notes/.note.gnu.build-id", |
| 1305 | root_dir, (int)strlen(name) - 1, name); |
| 1306 | |
| 1307 | if (sysfs__read_build_id(filename, dso->build_id, |
| 1308 | sizeof(dso->build_id)) == 0) |
| 1309 | dso->has_build_id = true; |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | bool __dsos__read_build_ids(struct list_head *head, bool with_hits) |
| 1315 | { |
| 1316 | bool have_build_id = false; |
| 1317 | struct dso *pos; |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 1318 | struct nscookie nsc; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1319 | |
| 1320 | list_for_each_entry(pos, head, node) { |
He Kuang | 6ae98ba | 2016-05-12 08:43:11 +0000 | [diff] [blame] | 1321 | if (with_hits && !pos->hit && !dso__is_vdso(pos)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1322 | continue; |
| 1323 | if (pos->has_build_id) { |
| 1324 | have_build_id = true; |
| 1325 | continue; |
| 1326 | } |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 1327 | nsinfo__mountns_enter(pos->nsinfo, &nsc); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1328 | if (filename__read_build_id(pos->long_name, pos->build_id, |
| 1329 | sizeof(pos->build_id)) > 0) { |
| 1330 | have_build_id = true; |
| 1331 | pos->has_build_id = true; |
| 1332 | } |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 1333 | nsinfo__mountns_exit(&nsc); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | return have_build_id; |
| 1337 | } |
| 1338 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1339 | void __dsos__add(struct dsos *dsos, struct dso *dso) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1340 | { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 1341 | list_add_tail(&dso->node, &dsos->head); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1342 | __dso__findlink_by_longname(&dsos->root, dso, NULL); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1343 | /* |
| 1344 | * It is now in the linked list, grab a reference, then garbage collect |
| 1345 | * this when needing memory, by looking at LRU dso instances in the |
| 1346 | * list with atomic_read(&dso->refcnt) == 1, i.e. no references |
| 1347 | * anywhere besides the one for the list, do, under a lock for the |
| 1348 | * list: remove it from the list, then a dso__put(), that probably will |
| 1349 | * be the last and will then call dso__delete(), end of life. |
| 1350 | * |
| 1351 | * That, or at the end of the 'struct machine' lifetime, when all |
| 1352 | * 'struct dso' instances will be removed from the list, in |
| 1353 | * dsos__exit(), if they have no other reference from some other data |
| 1354 | * structure. |
| 1355 | * |
| 1356 | * E.g.: after processing a 'perf.data' file and storing references |
| 1357 | * to objects instantiated while processing events, we will have |
| 1358 | * references to the 'thread', 'map', 'dso' structs all from 'struct |
| 1359 | * hist_entry' instances, but we may not need anything not referenced, |
| 1360 | * so we might as well call machines__exit()/machines__delete() and |
| 1361 | * garbage collect it. |
| 1362 | */ |
| 1363 | dso__get(dso); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1364 | } |
| 1365 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1366 | void dsos__add(struct dsos *dsos, struct dso *dso) |
| 1367 | { |
| 1368 | pthread_rwlock_wrlock(&dsos->lock); |
| 1369 | __dsos__add(dsos, dso); |
| 1370 | pthread_rwlock_unlock(&dsos->lock); |
| 1371 | } |
| 1372 | |
| 1373 | struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_short) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1374 | { |
| 1375 | struct dso *pos; |
| 1376 | |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 1377 | if (cmp_short) { |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 1378 | list_for_each_entry(pos, &dsos->head, node) |
Waiman Long | f9ceffb | 2013-05-09 10:42:48 -0400 | [diff] [blame] | 1379 | if (strcmp(pos->short_name, name) == 0) |
| 1380 | return pos; |
| 1381 | return NULL; |
| 1382 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1383 | return __dso__find_by_longname(&dsos->root, name); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1384 | } |
| 1385 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1386 | struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) |
| 1387 | { |
| 1388 | struct dso *dso; |
| 1389 | pthread_rwlock_rdlock(&dsos->lock); |
| 1390 | dso = __dsos__find(dsos, name, cmp_short); |
| 1391 | pthread_rwlock_unlock(&dsos->lock); |
| 1392 | return dso; |
| 1393 | } |
| 1394 | |
| 1395 | struct dso *__dsos__addnew(struct dsos *dsos, const char *name) |
Jiri Olsa | 701d8d7 | 2015-02-12 22:06:09 +0100 | [diff] [blame] | 1396 | { |
| 1397 | struct dso *dso = dso__new(name); |
| 1398 | |
| 1399 | if (dso != NULL) { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1400 | __dsos__add(dsos, dso); |
Jiri Olsa | 701d8d7 | 2015-02-12 22:06:09 +0100 | [diff] [blame] | 1401 | dso__set_basename(dso); |
Masami Hiramatsu | 82de26a | 2015-11-18 15:40:31 +0900 | [diff] [blame] | 1402 | /* Put dso here because __dsos_add already got it */ |
| 1403 | dso__put(dso); |
Jiri Olsa | 701d8d7 | 2015-02-12 22:06:09 +0100 | [diff] [blame] | 1404 | } |
| 1405 | return dso; |
| 1406 | } |
| 1407 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 1408 | struct dso *__dsos__findnew(struct dsos *dsos, const char *name) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1409 | { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1410 | struct dso *dso = __dsos__find(dsos, name, false); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1411 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1412 | return dso ? dso : __dsos__addnew(dsos, name); |
| 1413 | } |
| 1414 | |
| 1415 | struct dso *dsos__findnew(struct dsos *dsos, const char *name) |
| 1416 | { |
| 1417 | struct dso *dso; |
| 1418 | pthread_rwlock_wrlock(&dsos->lock); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1419 | dso = dso__get(__dsos__findnew(dsos, name)); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1420 | pthread_rwlock_unlock(&dsos->lock); |
| 1421 | return dso; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | 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] | 1425 | bool (skip)(struct dso *dso, int parm), int parm) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1426 | { |
| 1427 | struct dso *pos; |
| 1428 | size_t ret = 0; |
| 1429 | |
| 1430 | list_for_each_entry(pos, head, node) { |
Arnaldo Carvalho de Melo | 417c2ff | 2012-12-07 09:53:58 -0300 | [diff] [blame] | 1431 | if (skip && skip(pos, parm)) |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1432 | continue; |
| 1433 | ret += dso__fprintf_buildid(pos, fp); |
| 1434 | ret += fprintf(fp, " %s\n", pos->long_name); |
| 1435 | } |
| 1436 | return ret; |
| 1437 | } |
| 1438 | |
| 1439 | size_t __dsos__fprintf(struct list_head *head, FILE *fp) |
| 1440 | { |
| 1441 | struct dso *pos; |
| 1442 | size_t ret = 0; |
| 1443 | |
| 1444 | list_for_each_entry(pos, head, node) { |
| 1445 | int i; |
| 1446 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1447 | ret += dso__fprintf(pos, i, fp); |
| 1448 | } |
| 1449 | |
| 1450 | return ret; |
| 1451 | } |
| 1452 | |
| 1453 | size_t dso__fprintf_buildid(struct dso *dso, FILE *fp) |
| 1454 | { |
Masami Hiramatsu | b5d8bbe | 2016-05-11 22:51:59 +0900 | [diff] [blame] | 1455 | char sbuild_id[SBUILD_ID_SIZE]; |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1456 | |
| 1457 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id); |
| 1458 | return fprintf(fp, "%s", sbuild_id); |
| 1459 | } |
| 1460 | |
| 1461 | size_t dso__fprintf(struct dso *dso, enum map_type type, FILE *fp) |
| 1462 | { |
| 1463 | struct rb_node *nd; |
| 1464 | size_t ret = fprintf(fp, "dso: %s (", dso->short_name); |
| 1465 | |
| 1466 | if (dso->short_name != dso->long_name) |
| 1467 | ret += fprintf(fp, "%s, ", dso->long_name); |
| 1468 | ret += fprintf(fp, "%s, %sloaded, ", map_type__name[type], |
Stephane Eranian | 919d590 | 2012-11-20 10:51:02 +0100 | [diff] [blame] | 1469 | dso__loaded(dso, type) ? "" : "NOT "); |
Jiri Olsa | cdd059d | 2012-10-27 23:18:32 +0200 | [diff] [blame] | 1470 | ret += dso__fprintf_buildid(dso, fp); |
| 1471 | ret += fprintf(fp, ")\n"); |
| 1472 | for (nd = rb_first(&dso->symbols[type]); nd; nd = rb_next(nd)) { |
| 1473 | struct symbol *pos = rb_entry(nd, struct symbol, rb_node); |
| 1474 | ret += symbol__fprintf(pos, fp); |
| 1475 | } |
| 1476 | |
| 1477 | return ret; |
| 1478 | } |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1479 | |
| 1480 | enum dso_type dso__type(struct dso *dso, struct machine *machine) |
| 1481 | { |
| 1482 | int fd; |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 1483 | enum dso_type type = DSO__TYPE_UNKNOWN; |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1484 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 1485 | fd = dso__data_get_fd(dso, machine); |
| 1486 | if (fd >= 0) { |
| 1487 | type = dso__type_fd(fd); |
| 1488 | dso__data_put_fd(dso); |
| 1489 | } |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1490 | |
Namhyung Kim | 4bb11d0 | 2015-05-21 01:03:41 +0900 | [diff] [blame] | 1491 | return type; |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 1492 | } |
Arnaldo Carvalho de Melo | 18425f1 | 2015-03-24 11:49:02 -0300 | [diff] [blame] | 1493 | |
| 1494 | int dso__strerror_load(struct dso *dso, char *buf, size_t buflen) |
| 1495 | { |
| 1496 | int idx, errnum = dso->load_errno; |
| 1497 | /* |
| 1498 | * This must have a same ordering as the enum dso_load_errno. |
| 1499 | */ |
| 1500 | static const char *dso_load__error_str[] = { |
| 1501 | "Internal tools/perf/ library error", |
| 1502 | "Invalid ELF file", |
| 1503 | "Can not read build id", |
| 1504 | "Mismatching build id", |
| 1505 | "Decompression failure", |
| 1506 | }; |
| 1507 | |
| 1508 | BUG_ON(buflen == 0); |
| 1509 | |
| 1510 | if (errnum >= 0) { |
Arnaldo Carvalho de Melo | c8b5f2c | 2016-07-06 11:56:20 -0300 | [diff] [blame] | 1511 | const char *err = str_error_r(errnum, buf, buflen); |
Arnaldo Carvalho de Melo | 18425f1 | 2015-03-24 11:49:02 -0300 | [diff] [blame] | 1512 | |
| 1513 | if (err != buf) |
| 1514 | scnprintf(buf, buflen, "%s", err); |
| 1515 | |
| 1516 | return 0; |
| 1517 | } |
| 1518 | |
| 1519 | if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END) |
| 1520 | return -1; |
| 1521 | |
| 1522 | idx = errnum - __DSO_LOAD_ERRNO__START; |
| 1523 | scnprintf(buf, buflen, "%s", dso_load__error_str[idx]); |
| 1524 | return 0; |
| 1525 | } |