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