1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2bda6ee4aSJiri Olsa #include <asm/bug.h>
3877a7a11SArnaldo Carvalho de Melo #include <linux/kernel.h>
4af0de0c5SArnaldo Carvalho de Melo #include <linux/string.h>
57f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
6c6580451SJiri Olsa #include <sys/time.h>
7c6580451SJiri Olsa #include <sys/resource.h>
87a8ef4c4SArnaldo Carvalho de Melo #include <sys/types.h>
97a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h>
107a8ef4c4SArnaldo Carvalho de Melo #include <unistd.h>
11a43783aeSArnaldo Carvalho de Melo #include <errno.h>
12c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h>
13f2a39fe8SArnaldo Carvalho de Melo #include <stdlib.h>
14ef0580ecSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT
156c398d72SJiri Olsa #include <bpf/libbpf.h>
166c398d72SJiri Olsa #include "bpf-event.h"
176ac22d03SDave Marchevsky #include "bpf-utils.h"
18ef0580ecSArnaldo Carvalho de Melo #endif
19611f0afeSArnaldo Carvalho de Melo #include "compress.h"
20f2a39fe8SArnaldo Carvalho de Melo #include "env.h"
2140f3b2d2SArnaldo Carvalho de Melo #include "namespaces.h"
229a3993d4SArnaldo Carvalho de Melo #include "path.h"
231101f69aSArnaldo Carvalho de Melo #include "map.h"
24cdd059d7SJiri Olsa #include "symbol.h"
2511ea2515SMilian Wolff #include "srcline.h"
26cdd059d7SJiri Olsa #include "dso.h"
274a3cec84SArnaldo Carvalho de Melo #include "dsos.h"
2869d2591aSArnaldo Carvalho de Melo #include "machine.h"
29cfe9174fSAdrian Hunter #include "auxtrace.h"
307f7c536fSArnaldo Carvalho de Melo #include "util.h" /* O_CLOEXEC for older systems */
31cdd059d7SJiri Olsa #include "debug.h"
32a067558eSArnaldo Carvalho de Melo #include "string2.h"
336ae98ba6SHe Kuang #include "vdso.h"
34cdd059d7SJiri Olsa
359343e45bSMatija Glavinic Pecotic static const char * const debuglink_paths[] = {
369343e45bSMatija Glavinic Pecotic "%.0s%s",
379343e45bSMatija Glavinic Pecotic "%s/%s",
389343e45bSMatija Glavinic Pecotic "%s/.debug/%s",
399343e45bSMatija Glavinic Pecotic "/usr/lib/debug%s/%s"
409343e45bSMatija Glavinic Pecotic };
419343e45bSMatija Glavinic Pecotic
dso__symtab_origin(const struct dso * dso)42cdd059d7SJiri Olsa char dso__symtab_origin(const struct dso *dso)
43cdd059d7SJiri Olsa {
44cdd059d7SJiri Olsa static const char origin[] = {
45cdd059d7SJiri Olsa [DSO_BINARY_TYPE__KALLSYMS] = 'k',
46cdd059d7SJiri Olsa [DSO_BINARY_TYPE__VMLINUX] = 'v',
47cdd059d7SJiri Olsa [DSO_BINARY_TYPE__JAVA_JIT] = 'j',
48cdd059d7SJiri Olsa [DSO_BINARY_TYPE__DEBUGLINK] = 'l',
49cdd059d7SJiri Olsa [DSO_BINARY_TYPE__BUILD_ID_CACHE] = 'B',
50d2396999SKrister Johansen [DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO] = 'D',
51cdd059d7SJiri Olsa [DSO_BINARY_TYPE__FEDORA_DEBUGINFO] = 'f',
52cdd059d7SJiri Olsa [DSO_BINARY_TYPE__UBUNTU_DEBUGINFO] = 'u',
5385afd355SAdrian Hunter [DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO] = 'x',
549cd00941SRicardo Ribalda Delgado [DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO] = 'o',
55cdd059d7SJiri Olsa [DSO_BINARY_TYPE__BUILDID_DEBUGINFO] = 'b',
56cdd059d7SJiri Olsa [DSO_BINARY_TYPE__SYSTEM_PATH_DSO] = 'd',
57cdd059d7SJiri Olsa [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE] = 'K',
58c00c48fcSNamhyung Kim [DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP] = 'm',
59cdd059d7SJiri Olsa [DSO_BINARY_TYPE__GUEST_KALLSYMS] = 'g',
60cdd059d7SJiri Olsa [DSO_BINARY_TYPE__GUEST_KMODULE] = 'G',
61c00c48fcSNamhyung Kim [DSO_BINARY_TYPE__GUEST_KMODULE_COMP] = 'M',
62cdd059d7SJiri Olsa [DSO_BINARY_TYPE__GUEST_VMLINUX] = 'V',
63cdd059d7SJiri Olsa };
64cdd059d7SJiri Olsa
65cdd059d7SJiri Olsa if (dso == NULL || dso->symtab_type == DSO_BINARY_TYPE__NOT_FOUND)
66cdd059d7SJiri Olsa return '!';
67cdd059d7SJiri Olsa return origin[dso->symtab_type];
68cdd059d7SJiri Olsa }
69cdd059d7SJiri Olsa
dso__is_object_file(const struct dso * dso)70ee4e9625SArnaldo Carvalho de Melo bool dso__is_object_file(const struct dso *dso)
71ee4e9625SArnaldo Carvalho de Melo {
727d2a5122SArnaldo Carvalho de Melo switch (dso->binary_type) {
73cdd059d7SJiri Olsa case DSO_BINARY_TYPE__KALLSYMS:
74b5d8bbe8SMasami Hiramatsu case DSO_BINARY_TYPE__GUEST_KALLSYMS:
75cdd059d7SJiri Olsa case DSO_BINARY_TYPE__JAVA_JIT:
76972f393bSArnaldo Carvalho de Melo case DSO_BINARY_TYPE__BPF_PROG_INFO:
77cdd059d7SJiri Olsa case DSO_BINARY_TYPE__BPF_IMAGE:
78cdd059d7SJiri Olsa case DSO_BINARY_TYPE__OOL:
799343e45bSMatija Glavinic Pecotic return false;
809343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__VMLINUX:
819343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__GUEST_VMLINUX:
829343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__DEBUGLINK:
839343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__BUILD_ID_CACHE:
849343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
85cdd059d7SJiri Olsa case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
86dc6254cfSVictor Kamensky case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
879343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
889343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
899343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
9040356721SJiri Olsa case DSO_BINARY_TYPE__GUEST_KMODULE:
919343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
929343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
939343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
949343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__KCORE:
9540356721SJiri Olsa case DSO_BINARY_TYPE__GUEST_KCORE:
969343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
979343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__NOT_FOUND:
989343e45bSMatija Glavinic Pecotic default:
999343e45bSMatija Glavinic Pecotic return true;
1009343e45bSMatija Glavinic Pecotic }
10140356721SJiri Olsa }
10240356721SJiri Olsa
dso__read_binary_type_filename(const struct dso * dso,enum dso_binary_type type,char * root_dir,char * filename,size_t size)1039343e45bSMatija Glavinic Pecotic int dso__read_binary_type_filename(const struct dso *dso,
1049343e45bSMatija Glavinic Pecotic enum dso_binary_type type,
1059343e45bSMatija Glavinic Pecotic char *root_dir, char *filename, size_t size)
1069343e45bSMatija Glavinic Pecotic {
1079343e45bSMatija Glavinic Pecotic char build_id_hex[SBUILD_ID_SIZE];
1089343e45bSMatija Glavinic Pecotic int ret = 0;
1099343e45bSMatija Glavinic Pecotic size_t len;
110cdd059d7SJiri Olsa
1119343e45bSMatija Glavinic Pecotic switch (type) {
1129343e45bSMatija Glavinic Pecotic case DSO_BINARY_TYPE__DEBUGLINK:
1139343e45bSMatija Glavinic Pecotic {
1149343e45bSMatija Glavinic Pecotic const char *last_slash;
1159343e45bSMatija Glavinic Pecotic char dso_dir[PATH_MAX];
116cdd059d7SJiri Olsa char symfile[PATH_MAX];
117d2396999SKrister Johansen unsigned int i;
118d2396999SKrister Johansen
119d2396999SKrister Johansen len = __symbol__join_symfs(filename, size, dso->long_name);
120d2396999SKrister Johansen last_slash = filename + len;
121d2396999SKrister Johansen while (last_slash != filename && *last_slash != '/')
122d2396999SKrister Johansen last_slash--;
123cdd059d7SJiri Olsa
124cdd059d7SJiri Olsa strncpy(dso_dir, filename, last_slash - filename);
125cdd059d7SJiri Olsa dso_dir[last_slash-filename] = '\0';
126cdd059d7SJiri Olsa
127972f393bSArnaldo Carvalho de Melo if (!is_regular_file(filename)) {
128972f393bSArnaldo Carvalho de Melo ret = -1;
129cdd059d7SJiri Olsa break;
130cdd059d7SJiri Olsa }
131cdd059d7SJiri Olsa
132972f393bSArnaldo Carvalho de Melo ret = filename__read_debuglink(filename, symfile, PATH_MAX);
133972f393bSArnaldo Carvalho de Melo if (ret)
134cdd059d7SJiri Olsa break;
135cdd059d7SJiri Olsa
13685afd355SAdrian Hunter /* Check predefined locations where debug file might reside */
13785afd355SAdrian Hunter ret = -1;
13885afd355SAdrian Hunter for (i = 0; i < ARRAY_SIZE(debuglink_paths); i++) {
13985afd355SAdrian Hunter snprintf(filename, size,
14085afd355SAdrian Hunter debuglink_paths[i], dso_dir, symfile);
14185afd355SAdrian Hunter if (is_regular_file(filename)) {
14285afd355SAdrian Hunter ret = 0;
14385afd355SAdrian Hunter break;
14485afd355SAdrian Hunter }
14585afd355SAdrian Hunter }
14685afd355SAdrian Hunter
14785afd355SAdrian Hunter break;
14885afd355SAdrian Hunter }
14985afd355SAdrian Hunter case DSO_BINARY_TYPE__BUILD_ID_CACHE:
15085afd355SAdrian Hunter if (dso__build_id_filename(dso, filename, size, false) == NULL)
1519cd00941SRicardo Ribalda Delgado ret = -1;
1529cd00941SRicardo Ribalda Delgado break;
153bf4414aeSArnaldo Carvalho de Melo
1549cd00941SRicardo Ribalda Delgado case DSO_BINARY_TYPE__BUILD_ID_CACHE_DEBUGINFO:
1559cd00941SRicardo Ribalda Delgado if (dso__build_id_filename(dso, filename, size, true) == NULL)
1569cd00941SRicardo Ribalda Delgado ret = -1;
1579cd00941SRicardo Ribalda Delgado break;
1589cd00941SRicardo Ribalda Delgado
1599cd00941SRicardo Ribalda Delgado case DSO_BINARY_TYPE__FEDORA_DEBUGINFO:
160972f393bSArnaldo Carvalho de Melo len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
1619cd00941SRicardo Ribalda Delgado snprintf(filename + len, size - len, "%s.debug", dso->long_name);
1629cd00941SRicardo Ribalda Delgado break;
1639cd00941SRicardo Ribalda Delgado
1649cd00941SRicardo Ribalda Delgado case DSO_BINARY_TYPE__UBUNTU_DEBUGINFO:
1659cd00941SRicardo Ribalda Delgado len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
1667d2a5122SArnaldo Carvalho de Melo snprintf(filename + len, size - len, "%s", dso->long_name);
1677d2a5122SArnaldo Carvalho de Melo break;
1689cd00941SRicardo Ribalda Delgado
1699cd00941SRicardo Ribalda Delgado case DSO_BINARY_TYPE__MIXEDUP_UBUNTU_DEBUGINFO:
1709cd00941SRicardo Ribalda Delgado /*
1719cd00941SRicardo Ribalda Delgado * Ubuntu can mixup /usr/lib with /lib, putting debuginfo in
172cdd059d7SJiri Olsa * /usr/lib/debug/lib when it is expected to be in
173cdd059d7SJiri Olsa * /usr/lib/debug/usr/lib
174cdd059d7SJiri Olsa */
175cdd059d7SJiri Olsa if (strlen(dso->long_name) < 9 ||
176cdd059d7SJiri Olsa strncmp(dso->long_name, "/usr/lib/", 9)) {
177cdd059d7SJiri Olsa ret = -1;
178bf541169SJiri Olsa break;
179972f393bSArnaldo Carvalho de Melo }
180972f393bSArnaldo Carvalho de Melo len = __symbol__join_symfs(filename, size, "/usr/lib/debug");
181972f393bSArnaldo Carvalho de Melo snprintf(filename + len, size - len, "%s", dso->long_name + 4);
182cdd059d7SJiri Olsa break;
183cdd059d7SJiri Olsa
18439b12f78SAdrian Hunter case DSO_BINARY_TYPE__OPENEMBEDDED_DEBUGINFO:
18539b12f78SAdrian Hunter {
186cdd059d7SJiri Olsa const char *last_slash;
187972f393bSArnaldo Carvalho de Melo size_t dir_size;
188cdd059d7SJiri Olsa
189cdd059d7SJiri Olsa last_slash = dso->long_name + dso->long_name_len;
190cdd059d7SJiri Olsa while (last_slash != dso->long_name && *last_slash != '/')
191c00c48fcSNamhyung Kim last_slash--;
192972f393bSArnaldo Carvalho de Melo
193cdd059d7SJiri Olsa len = __symbol__join_symfs(filename, size, "");
194cdd059d7SJiri Olsa dir_size = last_slash - dso->long_name + 2;
195cdd059d7SJiri Olsa if (dir_size > (size - len)) {
196cdd059d7SJiri Olsa ret = -1;
197c00c48fcSNamhyung Kim break;
198972f393bSArnaldo Carvalho de Melo }
199cdd059d7SJiri Olsa len += scnprintf(filename + len, dir_size, "%s", dso->long_name);
200cdd059d7SJiri Olsa len += scnprintf(filename + len , size - len, ".debug%s",
2018e0cf965SAdrian Hunter last_slash);
2028e0cf965SAdrian Hunter break;
2037d2a5122SArnaldo Carvalho de Melo }
2048e0cf965SAdrian Hunter
2058e0cf965SAdrian Hunter case DSO_BINARY_TYPE__BUILDID_DEBUGINFO:
206cdd059d7SJiri Olsa if (!dso->has_build_id) {
207cdd059d7SJiri Olsa ret = -1;
208cdd059d7SJiri Olsa break;
209cdd059d7SJiri Olsa }
2109b86d04dSSong Liu
2113c29d448SJiri Olsa build_id__sprintf(&dso->bid, build_id_hex);
212789e2419SAdrian Hunter len = __symbol__join_symfs(filename, size, "/usr/lib/debug/.build-id/");
213cdd059d7SJiri Olsa snprintf(filename + len, size - len, "%.2s/%s.debug",
214cdd059d7SJiri Olsa build_id_hex, build_id_hex + 2);
215cdd059d7SJiri Olsa break;
216cdd059d7SJiri Olsa
217cdd059d7SJiri Olsa case DSO_BINARY_TYPE__VMLINUX:
218cdd059d7SJiri Olsa case DSO_BINARY_TYPE__GUEST_VMLINUX:
219cdd059d7SJiri Olsa case DSO_BINARY_TYPE__SYSTEM_PATH_DSO:
220cdd059d7SJiri Olsa __symbol__join_symfs(filename, size, dso->long_name);
2214b838b0dSJiri Olsa break;
2224b838b0dSJiri Olsa
2234b838b0dSJiri Olsa case DSO_BINARY_TYPE__GUEST_KMODULE:
2244b838b0dSJiri Olsa case DSO_BINARY_TYPE__GUEST_KMODULE_COMP:
225c00c48fcSNamhyung Kim path__join3(filename, size, symbol_conf.symfs,
226c00c48fcSNamhyung Kim root_dir, dso->long_name);
227c00c48fcSNamhyung Kim break;
2288b42b7e5SJiri Olsa
229c00c48fcSNamhyung Kim case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE:
2304b838b0dSJiri Olsa case DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP:
231e92ce12eSNamhyung Kim __symbol__join_symfs(filename, size, dso->long_name);
2328b42b7e5SJiri Olsa break;
233e92ce12eSNamhyung Kim
23480a32e5bSJiri Olsa case DSO_BINARY_TYPE__KCORE:
2358b42b7e5SJiri Olsa case DSO_BINARY_TYPE__GUEST_KCORE:
23680a32e5bSJiri Olsa snprintf(filename, size, "%s", dso->long_name);
2378b42b7e5SJiri Olsa break;
238c00c48fcSNamhyung Kim
239c00c48fcSNamhyung Kim default:
2404b838b0dSJiri Olsa case DSO_BINARY_TYPE__KALLSYMS:
241c00c48fcSNamhyung Kim case DSO_BINARY_TYPE__GUEST_KALLSYMS:
242c00c48fcSNamhyung Kim case DSO_BINARY_TYPE__JAVA_JIT:
243c00c48fcSNamhyung Kim case DSO_BINARY_TYPE__BPF_PROG_INFO:
2444b838b0dSJiri Olsa case DSO_BINARY_TYPE__BPF_IMAGE:
245c00c48fcSNamhyung Kim case DSO_BINARY_TYPE__OOL:
2464b838b0dSJiri Olsa case DSO_BINARY_TYPE__NOT_FOUND:
247c00c48fcSNamhyung Kim ret = -1;
2484b838b0dSJiri Olsa break;
249c00c48fcSNamhyung Kim }
250c00c48fcSNamhyung Kim
2511f121b03SWang Nan return ret;
252c00c48fcSNamhyung Kim }
2538dee9ff1SJiri Olsa
2541f121b03SWang Nan enum {
255c00c48fcSNamhyung Kim COMP_ID__NONE = 0,
2561f121b03SWang Nan };
2571f121b03SWang Nan
2581f121b03SWang Nan static const struct {
2591f121b03SWang Nan const char *fmt;
2601f121b03SWang Nan int (*decompress)(const char *input, int output);
2611f121b03SWang Nan bool (*is_compressed)(const char *input);
2621f121b03SWang Nan } compressions[] = {
2631f121b03SWang Nan [COMP_ID__NONE] = { .fmt = NULL, },
2641f121b03SWang Nan #ifdef HAVE_ZLIB_SUPPORT
2651f121b03SWang Nan { "gz", gzip_decompress_to_file, gzip_is_compressed },
2661f121b03SWang Nan #endif
2671f121b03SWang Nan #ifdef HAVE_LZMA_SUPPORT
2681f121b03SWang Nan { "xz", lzma_decompress_to_file, lzma_is_compressed },
2691f121b03SWang Nan #endif
2701f121b03SWang Nan { NULL, NULL, NULL },
2711f121b03SWang Nan };
2721f121b03SWang Nan
is_supported_compression(const char * ext)273c00c48fcSNamhyung Kim static int is_supported_compression(const char *ext)
2748dee9ff1SJiri Olsa {
275c00c48fcSNamhyung Kim unsigned i;
276c00c48fcSNamhyung Kim
277c00c48fcSNamhyung Kim for (i = 1; compressions[i].fmt; i++) {
278c00c48fcSNamhyung Kim if (!strcmp(ext, compressions[i].fmt))
279c00c48fcSNamhyung Kim return i;
280c00c48fcSNamhyung Kim }
281c00c48fcSNamhyung Kim return COMP_ID__NONE;
282c00c48fcSNamhyung Kim }
2837ac22b08SJiri Olsa
is_kernel_module(const char * pathname,int cpumode)2847ac22b08SJiri Olsa bool is_kernel_module(const char *pathname, int cpumode)
28542b3fa67SNamhyung Kim {
286c9a8a613SJiri Olsa struct kmod_path m;
28742b3fa67SNamhyung Kim int mode = cpumode & PERF_RECORD_MISC_CPUMODE_MASK;
28842b3fa67SNamhyung Kim
2898b42b7e5SJiri Olsa WARN_ONCE(mode != cpumode,
2908b42b7e5SJiri Olsa "Internal error: passing unmasked cpumode (%x) to is_kernel_module",
2918b42b7e5SJiri Olsa cpumode);
2928b42b7e5SJiri Olsa
2938b42b7e5SJiri Olsa switch (mode) {
2948b42b7e5SJiri Olsa case PERF_RECORD_MISC_USER:
2958b42b7e5SJiri Olsa case PERF_RECORD_MISC_HYPERVISOR:
2968b42b7e5SJiri Olsa case PERF_RECORD_MISC_GUEST_USER:
2978b42b7e5SJiri Olsa return false;
2988b42b7e5SJiri Olsa /* Treat PERF_RECORD_MISC_CPUMODE_UNKNOWN as kernel */
2998b42b7e5SJiri Olsa default:
3008b42b7e5SJiri Olsa if (kmod_path__parse(&m, pathname)) {
3018b42b7e5SJiri Olsa pr_err("Failed to check whether %s is a kernel module or not. Assume it is.",
3027ac22b08SJiri Olsa pathname);
3038b42b7e5SJiri Olsa return true;
3048b42b7e5SJiri Olsa }
30542b3fa67SNamhyung Kim }
30642b3fa67SNamhyung Kim
3077ac22b08SJiri Olsa return m.kmod;
308dde755a9SJiri Olsa }
30942b3fa67SNamhyung Kim
dso__needs_decompress(struct dso * dso)31042b3fa67SNamhyung Kim bool dso__needs_decompress(struct dso *dso)
3117ac22b08SJiri Olsa {
3127ac22b08SJiri Olsa return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE_COMP ||
31342b3fa67SNamhyung Kim dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE_COMP;
31442b3fa67SNamhyung Kim }
31542b3fa67SNamhyung Kim
filename__decompress(const char * name,char * pathname,size_t len,int comp,int * err)31642b3fa67SNamhyung Kim int filename__decompress(const char *name, char *pathname,
317c9a8a613SJiri Olsa size_t len, int comp, int *err)
318c9a8a613SJiri Olsa {
319c9a8a613SJiri Olsa char tmpbuf[] = KMOD_DECOMP_NAME;
320c9a8a613SJiri Olsa int fd = -1;
321fca5085cSArnaldo Carvalho de Melo
322c9a8a613SJiri Olsa /*
32342b3fa67SNamhyung Kim * We have proper compression id for DSO and yet the file
32442b3fa67SNamhyung Kim * behind the 'name' can still be plain uncompressed object.
32542b3fa67SNamhyung Kim *
3267ac22b08SJiri Olsa * The reason is behind the logic we open the DSO object files,
3277ac22b08SJiri Olsa * when we try all possible 'debug' objects until we find the
3287ac22b08SJiri Olsa * data. So even if the DSO is represented by 'krava.xz' module,
3297ac22b08SJiri Olsa * we can end up here opening ~/.debug/....23432432/debug' file
3307ac22b08SJiri Olsa * which is not compressed.
3317ac22b08SJiri Olsa *
3327ac22b08SJiri Olsa * To keep this transparent, we detect this and return the file
3337ac22b08SJiri Olsa * descriptor to the uncompressed file.
3347ac22b08SJiri Olsa */
3357ac22b08SJiri Olsa if (!compressions[comp].is_compressed(name))
3367ac22b08SJiri Olsa return open(name, O_RDONLY);
3377ac22b08SJiri Olsa
3387ac22b08SJiri Olsa fd = mkstemp(tmpbuf);
33942b3fa67SNamhyung Kim if (fd < 0) {
34042b3fa67SNamhyung Kim *err = errno;
341c9a8a613SJiri Olsa return -1;
34242b3fa67SNamhyung Kim }
34342b3fa67SNamhyung Kim
34442b3fa67SNamhyung Kim if (compressions[comp].decompress(name, fd)) {
34542b3fa67SNamhyung Kim *err = DSO_LOAD_ERRNO__DECOMPRESSION_FAILURE;
34642b3fa67SNamhyung Kim close(fd);
347c9a8a613SJiri Olsa fd = -1;
34842b3fa67SNamhyung Kim }
34942b3fa67SNamhyung Kim
350c9a8a613SJiri Olsa if (!pathname || (fd < 0))
35142b3fa67SNamhyung Kim unlink(tmpbuf);
35242b3fa67SNamhyung Kim
353eba5102dSJiri Olsa if (pathname && (fd >= 0))
3543c8a67f5SJiri Olsa strlcpy(pathname, tmpbuf, len);
3553c8a67f5SJiri Olsa
3563c8a67f5SJiri Olsa return fd;
3573c8a67f5SJiri Olsa }
3583c8a67f5SJiri Olsa
decompress_kmodule(struct dso * dso,const char * name,char * pathname,size_t len)3593c8a67f5SJiri Olsa static int decompress_kmodule(struct dso *dso, const char *name,
3603c8a67f5SJiri Olsa char *pathname, size_t len)
3613c8a67f5SJiri Olsa {
3623c8a67f5SJiri Olsa if (!dso__needs_decompress(dso))
3633c8a67f5SJiri Olsa return -1;
3643c8a67f5SJiri Olsa
3653c8a67f5SJiri Olsa if (dso->comp == COMP_ID__NONE)
3663c8a67f5SJiri Olsa return -1;
3673c8a67f5SJiri Olsa
3683c8a67f5SJiri Olsa return filename__decompress(name, pathname, len, dso->comp,
3693c8a67f5SJiri Olsa &dso->load_errno);
370b946cd37SJiri Olsa }
3713c8a67f5SJiri Olsa
dso__decompress_kmodule_fd(struct dso * dso,const char * name)3723c8a67f5SJiri Olsa int dso__decompress_kmodule_fd(struct dso *dso, const char *name)
3733c8a67f5SJiri Olsa {
3741f121b03SWang Nan return decompress_kmodule(dso, name, NULL, 0);
3753c8a67f5SJiri Olsa }
3763c8a67f5SJiri Olsa
dso__decompress_kmodule_path(struct dso * dso,const char * name,char * pathname,size_t len)3773c8a67f5SJiri Olsa int dso__decompress_kmodule_path(struct dso *dso, const char *name,
3783c8a67f5SJiri Olsa char *pathname, size_t len)
3791f121b03SWang Nan {
3801f121b03SWang Nan int fd = decompress_kmodule(dso, name, pathname, len);
3811f121b03SWang Nan
3821f121b03SWang Nan close(fd);
3831f121b03SWang Nan return fd >= 0 ? 0 : -1;
3841f121b03SWang Nan }
3851f121b03SWang Nan
3861f121b03SWang Nan /*
3871f121b03SWang Nan * Parses kernel module specified in @path and updates
3881f121b03SWang Nan * @m argument like:
3891f121b03SWang Nan *
3901f121b03SWang Nan * @comp - true if @path contains supported compression suffix,
3911f121b03SWang Nan * false otherwise
392aef4feacSAdrian Hunter * @kmod - true if @path contains '.ko' suffix in right position,
393aef4feacSAdrian Hunter * false otherwise
3941f121b03SWang Nan * @name - if (@alloc_name && @kmod) is true, it contains strdup-ed base name
3951f121b03SWang Nan * of the kernel module without suffixes, otherwise strudup-ed
3961f121b03SWang Nan * base name of @path
3971f121b03SWang Nan * @ext - if (@alloc_ext && @comp) is true, it contains strdup-ed string
3981f121b03SWang Nan * the compression suffix
3991f121b03SWang Nan *
4001f121b03SWang Nan * Returns 0 if there's no strdup error, -ENOMEM otherwise.
4013c8a67f5SJiri Olsa */
__kmod_path__parse(struct kmod_path * m,const char * path,bool alloc_name)4021f121b03SWang Nan int __kmod_path__parse(struct kmod_path *m, const char *path,
4033c8a67f5SJiri Olsa bool alloc_name)
4043c8a67f5SJiri Olsa {
4053c8a67f5SJiri Olsa const char *name = strrchr(path, '/');
4063c8a67f5SJiri Olsa const char *ext = strrchr(path, '.');
4073c8a67f5SJiri Olsa bool is_simple_name = false;
4083c8a67f5SJiri Olsa
4093c8a67f5SJiri Olsa memset(m, 0x0, sizeof(*m));
4104b838b0dSJiri Olsa name = name ? name + 1 : path;
4114b838b0dSJiri Olsa
4123c8a67f5SJiri Olsa /*
4133c8a67f5SJiri Olsa * '.' is also a valid character for module name. For example:
4143c8a67f5SJiri Olsa * [aaa.bbb] is a valid module name. '[' should have higher
4153c8a67f5SJiri Olsa * priority than '.ko' suffix.
4163c8a67f5SJiri Olsa *
4173c8a67f5SJiri Olsa * The kernel names are from machine__mmap_name. Such
4183c8a67f5SJiri Olsa * name should belong to kernel itself, not kernel module.
4193c8a67f5SJiri Olsa */
4203c8a67f5SJiri Olsa if (name[0] == '[') {
4213c8a67f5SJiri Olsa is_simple_name = true;
4223c8a67f5SJiri Olsa if ((strncmp(name, "[kernel.kallsyms]", 17) == 0) ||
4233c8a67f5SJiri Olsa (strncmp(name, "[guest.kernel.kallsyms", 22) == 0) ||
4243c8a67f5SJiri Olsa (strncmp(name, "[vdso]", 6) == 0) ||
4253c8a67f5SJiri Olsa (strncmp(name, "[vdso32]", 8) == 0) ||
4263c8a67f5SJiri Olsa (strncmp(name, "[vdsox32]", 9) == 0) ||
427af0de0c5SArnaldo Carvalho de Melo (strncmp(name, "[vsyscall]", 10) == 0)) {
4283c8a67f5SJiri Olsa m->kmod = false;
4293c8a67f5SJiri Olsa
4303c8a67f5SJiri Olsa } else
4313c8a67f5SJiri Olsa m->kmod = true;
4323c8a67f5SJiri Olsa }
4336b335e8fSNamhyung Kim
4346b335e8fSNamhyung Kim /* No extension, just return name. */
4356b335e8fSNamhyung Kim if ((ext == NULL) || is_simple_name) {
4366b335e8fSNamhyung Kim if (alloc_name) {
4376b335e8fSNamhyung Kim m->name = strdup(name);
4386b335e8fSNamhyung Kim return m->name ? 0 : -ENOMEM;
4396b335e8fSNamhyung Kim }
4406b335e8fSNamhyung Kim return 0;
4416b335e8fSNamhyung Kim }
4422af52475SJiri Olsa
4436b335e8fSNamhyung Kim m->comp = is_supported_compression(ext + 1);
4442af52475SJiri Olsa if (m->comp > COMP_ID__NONE)
4452af52475SJiri Olsa ext -= 3;
4466b335e8fSNamhyung Kim
4476b335e8fSNamhyung Kim /* Check .ko extension only if there's enough name left. */
4486b335e8fSNamhyung Kim if (ext > name)
4496b335e8fSNamhyung Kim m->kmod = !strncmp(ext, ".ko", 3);
4503c8a67f5SJiri Olsa
451bda6ee4aSJiri Olsa if (alloc_name) {
452eba5102dSJiri Olsa if (m->kmod) {
453eba5102dSJiri Olsa if (asprintf(&m->name, "[%.*s]", (int) (ext - name), name) == -1)
454bda6ee4aSJiri Olsa return -ENOMEM;
45533bdedceSNamhyung Kim } else {
456eba5102dSJiri Olsa if (asprintf(&m->name, "%s", name) == -1)
457eba5102dSJiri Olsa return -ENOMEM;
458eba5102dSJiri Olsa }
459eba5102dSJiri Olsa
460bda6ee4aSJiri Olsa strreplace(m->name, '-', '_');
461eba5102dSJiri Olsa }
462eba5102dSJiri Olsa
463eba5102dSJiri Olsa return 0;
464eba5102dSJiri Olsa }
465e56fbc9dSArnaldo Carvalho de Melo
dso__set_module_info(struct dso * dso,struct kmod_path * m,struct machine * machine)466bda6ee4aSJiri Olsa void dso__set_module_info(struct dso *dso, struct kmod_path *m,
467bda6ee4aSJiri Olsa struct machine *machine)
468bda6ee4aSJiri Olsa {
469eba5102dSJiri Olsa if (machine__is_host(machine))
470eba5102dSJiri Olsa dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
471a08cae03SJiri Olsa else
472a08cae03SJiri Olsa dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
473a08cae03SJiri Olsa
474a08cae03SJiri Olsa /* _KMODULE_COMP should be next to _KMODULE */
475a08cae03SJiri Olsa if (m->kmod && m->comp) {
4766e81c74cSMasami Hiramatsu dso->symtab_type++;
477a08cae03SJiri Olsa dso->comp = m->comp;
478a08cae03SJiri Olsa }
4794c0d8d27SWang YanQing
480a08cae03SJiri Olsa dso__set_short_name(dso, strdup(m->name), true);
481a08cae03SJiri Olsa }
482a08cae03SJiri Olsa
483a3c0cc2aSNamhyung Kim /*
484c8b5f2c9SArnaldo Carvalho de Melo * Global list of open DSOs and the counter.
485a08cae03SJiri Olsa */
486a08cae03SJiri Olsa static LIST_HEAD(dso__data_open);
487a08cae03SJiri Olsa static long dso__data_open_cnt;
488a08cae03SJiri Olsa static pthread_mutex_t dso__data_open_lock = PTHREAD_MUTEX_INITIALIZER;
489a08cae03SJiri Olsa
dso__list_add(struct dso * dso)490a08cae03SJiri Olsa static void dso__list_add(struct dso *dso)
491a08cae03SJiri Olsa {
492a08cae03SJiri Olsa list_add_tail(&dso->data.open_entry, &dso__data_open);
493a08cae03SJiri Olsa dso__data_open_cnt++;
494*7031edacSArnaldo Carvalho de Melo }
495*7031edacSArnaldo Carvalho de Melo
dso__list_del(struct dso * dso)496*7031edacSArnaldo Carvalho de Melo static void dso__list_del(struct dso *dso)
497*7031edacSArnaldo Carvalho de Melo {
498*7031edacSArnaldo Carvalho de Melo list_del_init(&dso->data.open_entry);
499eba5102dSJiri Olsa WARN_ONCE(dso__data_open_cnt <= 0,
500cdd059d7SJiri Olsa "DSO data fd counter out of bounds.");
5018ba29adfSNamhyung Kim dso__data_open_cnt--;
502ee4e9625SArnaldo Carvalho de Melo }
503ee4e9625SArnaldo Carvalho de Melo
504d68a29c2SJiri Olsa static void close_first_dso(void);
505cdd059d7SJiri Olsa
do_open(char * name)506cdd059d7SJiri Olsa static int do_open(char *name)
507cdd059d7SJiri Olsa {
508cdd059d7SJiri Olsa int fd;
509e54dea69SIan Rogers char sbuf[STRERR_BUFSIZE];
510cdd059d7SJiri Olsa
511cdd059d7SJiri Olsa do {
512cdd059d7SJiri Olsa fd = open(name, O_RDONLY|O_CLOEXEC);
5135f70619dSArnaldo Carvalho de Melo if (fd >= 0)
5148ba29adfSNamhyung Kim return fd;
5158ba29adfSNamhyung Kim
516cdd059d7SJiri Olsa pr_debug("dso open failed: %s\n",
51767fd1892SNamhyung Kim str_error_r(errno, sbuf, sizeof(sbuf)));
51867fd1892SNamhyung Kim if (!dso__data_open_cnt || errno != EMFILE)
51967fd1892SNamhyung Kim break;
52067fd1892SNamhyung Kim
5218ba29adfSNamhyung Kim close_first_dso();
5223c028a0cSJiri Olsa } while (1);
523*7031edacSArnaldo Carvalho de Melo
52467fd1892SNamhyung Kim return -1;
52567fd1892SNamhyung Kim }
52667fd1892SNamhyung Kim
dso__filename_with_chroot(const struct dso * dso,const char * filename)52767fd1892SNamhyung Kim char *dso__filename_with_chroot(const struct dso *dso, const char *filename)
52867fd1892SNamhyung Kim {
52967fd1892SNamhyung Kim return filename_with_chroot(nsinfo__pid(dso->nsinfo), filename);
53067fd1892SNamhyung Kim }
5311d6b3c9bSNamhyung Kim
__open_dso(struct dso * dso,struct machine * machine)5321d6b3c9bSNamhyung Kim static int __open_dso(struct dso *dso, struct machine *machine)
5331d6b3c9bSNamhyung Kim {
5341d6b3c9bSNamhyung Kim int fd = -EINVAL;
5351d6b3c9bSNamhyung Kim char *root_dir = (char *)"";
5368ba29adfSNamhyung Kim char *name = malloc(PATH_MAX);
5378ba29adfSNamhyung Kim bool decomp = false;
5381d6b3c9bSNamhyung Kim
5391d6b3c9bSNamhyung Kim if (!name)
540d68a29c2SJiri Olsa return -ENOMEM;
5411d6b3c9bSNamhyung Kim
5421d6b3c9bSNamhyung Kim mutex_lock(&dso->lock);
5431d6b3c9bSNamhyung Kim if (machine)
544a08cae03SJiri Olsa root_dir = machine->root_dir;
5451d6b3c9bSNamhyung Kim
546d68a29c2SJiri Olsa if (dso__read_binary_type_filename(dso, dso->binary_type,
5471d6b3c9bSNamhyung Kim root_dir, name, PATH_MAX))
5481d6b3c9bSNamhyung Kim goto out;
5498ba29adfSNamhyung Kim
550e54dea69SIan Rogers if (!is_regular_file(name)) {
551cdd059d7SJiri Olsa char *new_name;
552cdd059d7SJiri Olsa
553cdd059d7SJiri Olsa if (errno != ENOENT || dso->nsinfo == NULL)
554cdd059d7SJiri Olsa goto out;
555c6580451SJiri Olsa
556c6580451SJiri Olsa new_name = dso__filename_with_chroot(dso, name);
557c1f9aa0aSJiri Olsa if (!new_name)
558c1f9aa0aSJiri Olsa goto out;
559c1f9aa0aSJiri Olsa
560c1f9aa0aSJiri Olsa free(name);
561c1f9aa0aSJiri Olsa name = new_name;
562c1f9aa0aSJiri Olsa }
563c1f9aa0aSJiri Olsa
564eba5102dSJiri Olsa if (dso__needs_decompress(dso)) {
565eba5102dSJiri Olsa char newpath[KMOD_DECOMP_LEN];
566f045b8c4SKrister Johansen size_t len = sizeof(newpath);
567f045b8c4SKrister Johansen
568f045b8c4SKrister Johansen if (dso__decompress_kmodule_path(dso, name, newpath, len) < 0) {
569e54dea69SIan Rogers fd = -dso->load_errno;
570e54dea69SIan Rogers goto out;
571f045b8c4SKrister Johansen }
572e54dea69SIan Rogers
573e54dea69SIan Rogers decomp = true;
574f045b8c4SKrister Johansen strcpy(name, newpath);
575f045b8c4SKrister Johansen }
576f045b8c4SKrister Johansen
577eba5102dSJiri Olsa fd = do_open(name);
578a6f6ae99SAdrian Hunter
579eba5102dSJiri Olsa if (decomp)
580c6580451SJiri Olsa unlink(name);
581c6580451SJiri Olsa
582c6580451SJiri Olsa out:
583c6580451SJiri Olsa mutex_unlock(&dso->lock);
584c6580451SJiri Olsa free(name);
585c6580451SJiri Olsa return fd;
586eba5102dSJiri Olsa }
587eba5102dSJiri Olsa
588eba5102dSJiri Olsa static void check_data_close(void);
589eba5102dSJiri Olsa
590eba5102dSJiri Olsa /**
59153fa8eaaSJiri Olsa * dso_close - Open DSO data file
59253fa8eaaSJiri Olsa * @dso: dso object
59353fa8eaaSJiri Olsa *
59453fa8eaaSJiri Olsa * Open @dso's data file descriptor and updates
595c3fbd2a6SJiri Olsa * list/count of open DSO objects.
596eba5102dSJiri Olsa */
open_dso(struct dso * dso,struct machine * machine)59753fa8eaaSJiri Olsa static int open_dso(struct dso *dso, struct machine *machine)
59853fa8eaaSJiri Olsa {
59953fa8eaaSJiri Olsa int fd;
600c1f9aa0aSJiri Olsa struct nscookie nsc;
601c1f9aa0aSJiri Olsa
602c1f9aa0aSJiri Olsa if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE) {
603c1f9aa0aSJiri Olsa mutex_lock(&dso->lock);
604c1f9aa0aSJiri Olsa nsinfo__mountns_enter(dso->nsinfo, &nsc);
605c1f9aa0aSJiri Olsa mutex_unlock(&dso->lock);
606c1f9aa0aSJiri Olsa }
607eba5102dSJiri Olsa fd = __open_dso(dso, machine);
608eba5102dSJiri Olsa if (dso->binary_type != DSO_BINARY_TYPE__BUILD_ID_CACHE)
609eba5102dSJiri Olsa nsinfo__mountns_exit(&nsc);
610eba5102dSJiri Olsa
611eba5102dSJiri Olsa if (fd >= 0) {
612c6580451SJiri Olsa dso__list_add(dso);
613c6580451SJiri Olsa /*
614c6580451SJiri Olsa * Check if we crossed the allowed number
615c6580451SJiri Olsa * of opened DSOs and close one if needed.
616c6580451SJiri Olsa */
617c6580451SJiri Olsa check_data_close();
618c6580451SJiri Olsa }
619c6580451SJiri Olsa
620c6580451SJiri Olsa return fd;
621c6580451SJiri Olsa }
622c6580451SJiri Olsa
close_data_fd(struct dso * dso)623c6580451SJiri Olsa static void close_data_fd(struct dso *dso)
624c6580451SJiri Olsa {
625c6580451SJiri Olsa if (dso->data.fd >= 0) {
626c6580451SJiri Olsa close(dso->data.fd);
627c6580451SJiri Olsa dso->data.fd = -1;
628c6580451SJiri Olsa dso->data.file_size = 0;
629c6580451SJiri Olsa dso__list_del(dso);
630c6580451SJiri Olsa }
631c6580451SJiri Olsa }
632c6580451SJiri Olsa
633c6580451SJiri Olsa /**
634c6580451SJiri Olsa * dso_close - Close DSO data file
635c6580451SJiri Olsa * @dso: dso object
636c6580451SJiri Olsa *
637c6580451SJiri Olsa * Close @dso's data file descriptor and updates
638c6580451SJiri Olsa * list/count of open DSO objects.
639f3069249SJiri Olsa */
close_dso(struct dso * dso)640f3069249SJiri Olsa static void close_dso(struct dso *dso)
641f3069249SJiri Olsa {
642f3069249SJiri Olsa close_data_fd(dso);
643f3069249SJiri Olsa }
644f3069249SJiri Olsa
close_first_dso(void)645f3069249SJiri Olsa static void close_first_dso(void)
646f3069249SJiri Olsa {
647f3069249SJiri Olsa struct dso *dso;
648f3069249SJiri Olsa
649f3069249SJiri Olsa dso = list_first_entry(&dso__data_open, struct dso, data.open_entry);
650f3069249SJiri Olsa close_dso(dso);
651c6580451SJiri Olsa }
652c6580451SJiri Olsa
get_fd_limit(void)653f3069249SJiri Olsa static rlim_t get_fd_limit(void)
654f3069249SJiri Olsa {
655c6580451SJiri Olsa struct rlimit l;
656f3069249SJiri Olsa rlim_t limit = 0;
657c6580451SJiri Olsa
658c6580451SJiri Olsa /* Allow half of the current open fd limit. */
659f3069249SJiri Olsa if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
660c6580451SJiri Olsa if (l.rlim_cur == RLIM_INFINITY)
661c6580451SJiri Olsa limit = l.rlim_cur;
662c1f9aa0aSJiri Olsa else
663c1f9aa0aSJiri Olsa limit = l.rlim_cur / 2;
664c1f9aa0aSJiri Olsa } else {
665c1f9aa0aSJiri Olsa pr_err("failed to get fd limit\n");
666c1f9aa0aSJiri Olsa limit = 1;
667c6580451SJiri Olsa }
668c6580451SJiri Olsa
669c6580451SJiri Olsa return limit;
670c6580451SJiri Olsa }
671c6580451SJiri Olsa
672c6580451SJiri Olsa static rlim_t fd_limit;
673c6580451SJiri Olsa
674c6580451SJiri Olsa /*
675c1f9aa0aSJiri Olsa * Used only by tests/dso-data.c to reset the environment
676c1f9aa0aSJiri Olsa * for tests. I dont expect we should change this during
677c1f9aa0aSJiri Olsa * standard runtime.
678c1f9aa0aSJiri Olsa */
reset_fd_limit(void)679c1f9aa0aSJiri Olsa void reset_fd_limit(void)
680c1f9aa0aSJiri Olsa {
681eba5102dSJiri Olsa fd_limit = 0;
682eba5102dSJiri Olsa }
68333bdedceSNamhyung Kim
may_cache_fd(void)684eba5102dSJiri Olsa static bool may_cache_fd(void)
68533bdedceSNamhyung Kim {
686eba5102dSJiri Olsa if (!fd_limit)
687eba5102dSJiri Olsa fd_limit = get_fd_limit();
68871ff824aSNamhyung Kim
689cdd059d7SJiri Olsa if (fd_limit == RLIM_INFINITY)
690631d34b5SArnaldo Carvalho de Melo return true;
691cdd059d7SJiri Olsa
692cdd059d7SJiri Olsa return fd_limit > (rlim_t) dso__data_open_cnt;
693cdd059d7SJiri Olsa }
694cdd059d7SJiri Olsa
695cdd059d7SJiri Olsa /*
696cdd059d7SJiri Olsa * Check and close LRU dso if we crossed allowed limit
69753fa8eaaSJiri Olsa * for opened dso file descriptors. The limit is half
69871ff824aSNamhyung Kim * of the RLIMIT_NOFILE files opened.
69953fa8eaaSJiri Olsa */
check_data_close(void)70053fa8eaaSJiri Olsa static void check_data_close(void)
70153fa8eaaSJiri Olsa {
702c27697d6SAdrian Hunter bool cache_fd = may_cache_fd();
70353fa8eaaSJiri Olsa
704cdd059d7SJiri Olsa if (!cache_fd)
705cdd059d7SJiri Olsa close_first_dso();
7065f70619dSArnaldo Carvalho de Melo }
707cdd059d7SJiri Olsa
708c27697d6SAdrian Hunter /**
709c27697d6SAdrian Hunter * dso__data_close - Close DSO data file
710c27697d6SAdrian Hunter * @dso: dso object
711cdd059d7SJiri Olsa *
7125f70619dSArnaldo Carvalho de Melo * External interface to close @dso's data file descriptor.
713c27697d6SAdrian Hunter */
dso__data_close(struct dso * dso)714c27697d6SAdrian Hunter void dso__data_close(struct dso *dso)
715c27697d6SAdrian Hunter {
716c27697d6SAdrian Hunter pthread_mutex_lock(&dso__data_open_lock);
717c27697d6SAdrian Hunter close_dso(dso);
71871ff824aSNamhyung Kim pthread_mutex_unlock(&dso__data_open_lock);
719cdd059d7SJiri Olsa }
72071ff824aSNamhyung Kim
try_to_open_dso(struct dso * dso,struct machine * machine)7214bb11d01SNamhyung Kim static void try_to_open_dso(struct dso *dso, struct machine *machine)
72271ff824aSNamhyung Kim {
72371ff824aSNamhyung Kim enum dso_binary_type binary_type_data[] = {
72471ff824aSNamhyung Kim DSO_BINARY_TYPE__BUILD_ID_CACHE,
72571ff824aSNamhyung Kim DSO_BINARY_TYPE__SYSTEM_PATH_DSO,
7264bb11d01SNamhyung Kim DSO_BINARY_TYPE__NOT_FOUND,
7274bb11d01SNamhyung Kim };
72871ff824aSNamhyung Kim int i = 0;
7294bb11d01SNamhyung Kim
73071ff824aSNamhyung Kim if (dso->data.fd >= 0)
73171ff824aSNamhyung Kim return;
73271ff824aSNamhyung Kim
73371ff824aSNamhyung Kim if (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND) {
7344bb11d01SNamhyung Kim dso->data.fd = open_dso(dso, machine);
7354bb11d01SNamhyung Kim goto out;
7364bb11d01SNamhyung Kim }
73771ff824aSNamhyung Kim
7384bb11d01SNamhyung Kim do {
7394bb11d01SNamhyung Kim dso->binary_type = binary_type_data[i++];
74033bdedceSNamhyung Kim
74171ff824aSNamhyung Kim dso->data.fd = open_dso(dso, machine);
742c27697d6SAdrian Hunter if (dso->data.fd >= 0)
743cdd059d7SJiri Olsa goto out;
744cdd059d7SJiri Olsa
7454bb11d01SNamhyung Kim } while (dso->binary_type != DSO_BINARY_TYPE__NOT_FOUND);
7464bb11d01SNamhyung Kim out:
7474bb11d01SNamhyung Kim if (dso->data.fd >= 0)
7484bb11d01SNamhyung Kim dso->data.status = DSO_DATA_STATUS_OK;
7494bb11d01SNamhyung Kim else
750288be943SAdrian Hunter dso->data.status = DSO_DATA_STATUS_ERROR;
751288be943SAdrian Hunter }
752288be943SAdrian Hunter
753288be943SAdrian Hunter /**
754288be943SAdrian Hunter * dso__data_get_fd - Get dso's data file descriptor
755288be943SAdrian Hunter * @dso: dso object
756288be943SAdrian Hunter * @machine: machine object
757288be943SAdrian Hunter *
758288be943SAdrian Hunter * External interface to find dso's file, open it and
759288be943SAdrian Hunter * returns file descriptor. It should be paired with
760288be943SAdrian Hunter * dso__data_put_fd() if it returns non-negative value.
761288be943SAdrian Hunter */
dso__data_get_fd(struct dso * dso,struct machine * machine)762ef0580ecSArnaldo Carvalho de Melo int dso__data_get_fd(struct dso *dso, struct machine *machine)
7636c398d72SJiri Olsa {
7646c398d72SJiri Olsa if (dso->data.status == DSO_DATA_STATUS_ERROR)
7656c398d72SJiri Olsa return -1;
7666c398d72SJiri Olsa
7676c398d72SJiri Olsa if (pthread_mutex_lock(&dso__data_open_lock) < 0)
7686c398d72SJiri Olsa return -1;
7696c398d72SJiri Olsa
7706c398d72SJiri Olsa try_to_open_dso(dso, machine);
7716c398d72SJiri Olsa
7726c398d72SJiri Olsa if (dso->data.fd < 0)
7736c398d72SJiri Olsa pthread_mutex_unlock(&dso__data_open_lock);
7746c398d72SJiri Olsa
7756c398d72SJiri Olsa return dso->data.fd;
7766c398d72SJiri Olsa }
7776c398d72SJiri Olsa
dso__data_put_fd(struct dso * dso __maybe_unused)7786c398d72SJiri Olsa void dso__data_put_fd(struct dso *dso __maybe_unused)
7796c398d72SJiri Olsa {
7806c398d72SJiri Olsa pthread_mutex_unlock(&dso__data_open_lock);
7816c398d72SJiri Olsa }
7826c398d72SJiri Olsa
dso__data_status_seen(struct dso * dso,enum dso_data_status_seen by)7836c398d72SJiri Olsa bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by)
7846c398d72SJiri Olsa {
7856c398d72SJiri Olsa u32 flag = 1 << by;
7866c398d72SJiri Olsa
7876c398d72SJiri Olsa if (dso->data.status_seen & flag)
7886c398d72SJiri Olsa return true;
7896c398d72SJiri Olsa
7906c398d72SJiri Olsa dso->data.status_seen |= flag;
7916c398d72SJiri Olsa
7926c398d72SJiri Olsa return false;
7936c398d72SJiri Olsa }
7946c398d72SJiri Olsa
7956c398d72SJiri Olsa #ifdef HAVE_LIBBPF_SUPPORT
bpf_read(struct dso * dso,u64 offset,char * data)7966c398d72SJiri Olsa static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)
7976c398d72SJiri Olsa {
7986c398d72SJiri Olsa struct bpf_prog_info_node *node;
7996c398d72SJiri Olsa ssize_t size = DSO__DATA_CACHE_SIZE;
800ef0580ecSArnaldo Carvalho de Melo u64 len;
8016c398d72SJiri Olsa u8 *buf;
802cdd059d7SJiri Olsa
8038e67b725SNamhyung Kim node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
804cdd059d7SJiri Olsa if (!node || !node->info_linear) {
8058e67b725SNamhyung Kim dso->data.status = DSO_DATA_STATUS_ERROR;
806cdd059d7SJiri Olsa return -1;
807cdd059d7SJiri Olsa }
808d9a0d6b8SIan Rogers
809cdd059d7SJiri Olsa len = node->info_linear->info.jited_prog_len;
810cdd059d7SJiri Olsa buf = (u8 *)(uintptr_t)node->info_linear->info.jited_prog_insns;
811cdd059d7SJiri Olsa
812cdd059d7SJiri Olsa if (offset >= len)
813cdd059d7SJiri Olsa return -1;
814cdd059d7SJiri Olsa
815cdd059d7SJiri Olsa size = (ssize_t)min(len - offset, (u64)size);
816cdd059d7SJiri Olsa memcpy(data, buf + offset, size);
817d9a0d6b8SIan Rogers return size;
818cdd059d7SJiri Olsa }
819cdd059d7SJiri Olsa
bpf_size(struct dso * dso)820366df726SAdrian Hunter static int bpf_size(struct dso *dso)
821cdd059d7SJiri Olsa {
8228e67b725SNamhyung Kim struct bpf_prog_info_node *node;
8233344996eSArnaldo Carvalho de Melo
8243344996eSArnaldo Carvalho de Melo node = perf_env__find_bpf_prog_info(dso->bpf_prog.env, dso->bpf_prog.id);
825cdd059d7SJiri Olsa if (!node || !node->info_linear) {
826cdd059d7SJiri Olsa dso->data.status = DSO_DATA_STATUS_ERROR;
827cdd059d7SJiri Olsa return -1;
828cdd059d7SJiri Olsa }
829cdd059d7SJiri Olsa
830cdd059d7SJiri Olsa dso->data.file_size = node->info_linear->info.jited_prog_len;
831cdd059d7SJiri Olsa return 0;
832cdd059d7SJiri Olsa }
833cdd059d7SJiri Olsa #endif // HAVE_LIBBPF_SUPPORT
834cdd059d7SJiri Olsa
835cdd059d7SJiri Olsa static void
dso_cache__free(struct dso * dso)836cdd059d7SJiri Olsa dso_cache__free(struct dso *dso)
837cdd059d7SJiri Olsa {
838cdd059d7SJiri Olsa struct rb_root *root = &dso->data.cache;
839cdd059d7SJiri Olsa struct rb_node *next = rb_first(root);
840cdd059d7SJiri Olsa
8418e67b725SNamhyung Kim mutex_lock(&dso->lock);
842cdd059d7SJiri Olsa while (next) {
843cdd059d7SJiri Olsa struct dso_cache *cache;
844cdd059d7SJiri Olsa
8458e67b725SNamhyung Kim cache = rb_entry(next, struct dso_cache, rb_node);
8468e67b725SNamhyung Kim next = rb_next(&cache->rb_node);
847cdd059d7SJiri Olsa rb_erase(&cache->rb_node, root);
8488e67b725SNamhyung Kim free(cache);
849cdd059d7SJiri Olsa }
850cdd059d7SJiri Olsa mutex_unlock(&dso->lock);
851cdd059d7SJiri Olsa }
852cdd059d7SJiri Olsa
__dso_cache__find(struct dso * dso,u64 offset)853cdd059d7SJiri Olsa static struct dso_cache *__dso_cache__find(struct dso *dso, u64 offset)
854d9a0d6b8SIan Rogers {
855cdd059d7SJiri Olsa const struct rb_root *root = &dso->data.cache;
856cdd059d7SJiri Olsa struct rb_node * const *p = &root->rb_node;
857cdd059d7SJiri Olsa const struct rb_node *parent = NULL;
858cdd059d7SJiri Olsa struct dso_cache *cache;
859cdd059d7SJiri Olsa
860cdd059d7SJiri Olsa while (*p != NULL) {
861cdd059d7SJiri Olsa u64 end;
862cdd059d7SJiri Olsa
863cdd059d7SJiri Olsa parent = *p;
864cdd059d7SJiri Olsa cache = rb_entry(parent, struct dso_cache, rb_node);
865cdd059d7SJiri Olsa end = cache->offset + DSO__DATA_CACHE_SIZE;
8668e67b725SNamhyung Kim
8678e67b725SNamhyung Kim if (offset < cache->offset)
868cdd059d7SJiri Olsa p = &(*p)->rb_left;
869cdd059d7SJiri Olsa else if (offset >= end)
870cdd059d7SJiri Olsa p = &(*p)->rb_right;
871cdd059d7SJiri Olsa else
8728e67b725SNamhyung Kim return cache;
8738e67b725SNamhyung Kim }
8748e67b725SNamhyung Kim
875d9a0d6b8SIan Rogers return NULL;
8768e67b725SNamhyung Kim }
877cdd059d7SJiri Olsa
878cdd059d7SJiri Olsa static struct dso_cache *
dso_cache__insert(struct dso * dso,struct dso_cache * new)879b86a9d91SAdrian Hunter dso_cache__insert(struct dso *dso, struct dso_cache *new)
880b86a9d91SAdrian Hunter {
881cdd059d7SJiri Olsa struct rb_root *root = &dso->data.cache;
882cdd059d7SJiri Olsa struct rb_node **p = &root->rb_node;
883cdd059d7SJiri Olsa struct rb_node *parent = NULL;
884cdd059d7SJiri Olsa struct dso_cache *cache;
885b86a9d91SAdrian Hunter u64 offset = new->offset;
886cdd059d7SJiri Olsa
887b86a9d91SAdrian Hunter mutex_lock(&dso->lock);
888b86a9d91SAdrian Hunter while (*p != NULL) {
889cdd059d7SJiri Olsa u64 end;
890cdd059d7SJiri Olsa
891cdd059d7SJiri Olsa parent = *p;
892ea5db1bdSJiri Olsa cache = rb_entry(parent, struct dso_cache, rb_node);
893ea5db1bdSJiri Olsa end = cache->offset + DSO__DATA_CACHE_SIZE;
894cdd059d7SJiri Olsa
895cdd059d7SJiri Olsa if (offset < cache->offset)
896cdd059d7SJiri Olsa p = &(*p)->rb_left;
89733bdedceSNamhyung Kim else if (offset >= end)
89833bdedceSNamhyung Kim p = &(*p)->rb_right;
89933bdedceSNamhyung Kim else
90033bdedceSNamhyung Kim goto out;
90133bdedceSNamhyung Kim }
90233bdedceSNamhyung Kim
90371ff824aSNamhyung Kim rb_link_node(&new->rb_node, parent, p);
90471ff824aSNamhyung Kim rb_insert_color(&new->rb_node, root);
90533bdedceSNamhyung Kim
90633bdedceSNamhyung Kim cache = NULL;
907ea5db1bdSJiri Olsa out:
908ea5db1bdSJiri Olsa mutex_unlock(&dso->lock);
90933bdedceSNamhyung Kim return cache;
910cdd059d7SJiri Olsa }
911ea5db1bdSJiri Olsa
dso_cache__memcpy(struct dso_cache * cache,u64 offset,u8 * data,u64 size,bool out)912ea5db1bdSJiri Olsa static ssize_t dso_cache__memcpy(struct dso_cache *cache, u64 offset, u8 *data,
913ea5db1bdSJiri Olsa u64 size, bool out)
914ea5db1bdSJiri Olsa {
915ea5db1bdSJiri Olsa u64 cache_offset = offset - cache->offset;
916cdd059d7SJiri Olsa u64 cache_size = min(cache->size - cache_offset, size);
917366df726SAdrian Hunter
918366df726SAdrian Hunter if (out)
919366df726SAdrian Hunter memcpy(data, cache->data + cache_offset, cache_size);
920ea5db1bdSJiri Olsa else
921cacddfe7SJiri Olsa memcpy(cache->data + cache_offset, data, cache_size);
922ea5db1bdSJiri Olsa return cache_size;
923ea5db1bdSJiri Olsa }
924ea5db1bdSJiri Olsa
file_read(struct dso * dso,struct machine * machine,u64 offset,char * data)925ea5db1bdSJiri Olsa static ssize_t file_read(struct dso *dso, struct machine *machine,
926366df726SAdrian Hunter u64 offset, char *data)
927366df726SAdrian Hunter {
928366df726SAdrian Hunter ssize_t ret;
929366df726SAdrian Hunter
930ef0580ecSArnaldo Carvalho de Melo pthread_mutex_lock(&dso__data_open_lock);
9316c398d72SJiri Olsa
932366df726SAdrian Hunter /*
933ef0580ecSArnaldo Carvalho de Melo * dso->data.fd might be closed if other thread opened another
934ef0580ecSArnaldo Carvalho de Melo * file (dso) due to open file limit (RLIMIT_NOFILE).
935ef0580ecSArnaldo Carvalho de Melo */
936789e2419SAdrian Hunter try_to_open_dso(dso, machine);
9376c398d72SJiri Olsa
938366df726SAdrian Hunter if (dso->data.fd < 0) {
9396c398d72SJiri Olsa dso->data.status = DSO_DATA_STATUS_ERROR;
940366df726SAdrian Hunter ret = -errno;
941366df726SAdrian Hunter goto out;
942366df726SAdrian Hunter }
943366df726SAdrian Hunter
944366df726SAdrian Hunter ret = pread(dso->data.fd, data, DSO__DATA_CACHE_SIZE, offset);
945cdd059d7SJiri Olsa out:
946366df726SAdrian Hunter pthread_mutex_unlock(&dso__data_open_lock);
94733bdedceSNamhyung Kim return ret;
9488e67b725SNamhyung Kim }
9498e67b725SNamhyung Kim
dso_cache__populate(struct dso * dso,struct machine * machine,u64 offset,ssize_t * ret)9508e67b725SNamhyung Kim static struct dso_cache *dso_cache__populate(struct dso *dso,
9518e67b725SNamhyung Kim struct machine *machine,
9528e67b725SNamhyung Kim u64 offset, ssize_t *ret)
9538e67b725SNamhyung Kim {
954cdd059d7SJiri Olsa u64 cache_offset = offset & DSO__DATA_CACHE_MASK;
955366df726SAdrian Hunter struct dso_cache *cache;
95633bdedceSNamhyung Kim struct dso_cache *old;
957cdd059d7SJiri Olsa
958366df726SAdrian Hunter cache = zalloc(sizeof(*cache) + DSO__DATA_CACHE_SIZE);
959366df726SAdrian Hunter if (!cache) {
960366df726SAdrian Hunter *ret = -ENOMEM;
961366df726SAdrian Hunter return NULL;
962366df726SAdrian Hunter }
963366df726SAdrian Hunter #ifdef HAVE_LIBBPF_SUPPORT
964cdd059d7SJiri Olsa if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
965366df726SAdrian Hunter *ret = bpf_read(dso, cache_offset, cache->data);
966cdd059d7SJiri Olsa else
967cdd059d7SJiri Olsa #endif
968b86a9d91SAdrian Hunter if (dso->binary_type == DSO_BINARY_TYPE__OOL)
969b86a9d91SAdrian Hunter *ret = DSO__DATA_CACHE_SIZE;
970cdd059d7SJiri Olsa else
971cdd059d7SJiri Olsa *ret = file_read(dso, machine, cache_offset, cache->data);
972366df726SAdrian Hunter
973cdd059d7SJiri Olsa if (*ret <= 0) {
974366df726SAdrian Hunter free(cache);
975366df726SAdrian Hunter return NULL;
976366df726SAdrian Hunter }
977366df726SAdrian Hunter
978b86a9d91SAdrian Hunter cache->offset = cache_offset;
979cdd059d7SJiri Olsa cache->size = *ret;
980cdd059d7SJiri Olsa
981c1f9aa0aSJiri Olsa old = dso_cache__insert(dso, cache);
982c1f9aa0aSJiri Olsa if (old) {
983c1f9aa0aSJiri Olsa /* we lose the race */
984b86a9d91SAdrian Hunter free(cache);
985c1f9aa0aSJiri Olsa cache = old;
986b86a9d91SAdrian Hunter }
987b86a9d91SAdrian Hunter
988cdd059d7SJiri Olsa return cache;
989cdd059d7SJiri Olsa }
990cdd059d7SJiri Olsa
dso_cache__find(struct dso * dso,struct machine * machine,u64 offset,ssize_t * ret)991cdd059d7SJiri Olsa static struct dso_cache *dso_cache__find(struct dso *dso,
992cdd059d7SJiri Olsa struct machine *machine,
993cdd059d7SJiri Olsa u64 offset,
994cdd059d7SJiri Olsa ssize_t *ret)
995b86a9d91SAdrian Hunter {
996cdd059d7SJiri Olsa struct dso_cache *cache = __dso_cache__find(dso, offset);
997cdd059d7SJiri Olsa
998cdd059d7SJiri Olsa return cache ? cache : dso_cache__populate(dso, machine, offset, ret);
999cdd059d7SJiri Olsa }
1000cdd059d7SJiri Olsa
dso_cache_io(struct dso * dso,struct machine * machine,u64 offset,u8 * data,ssize_t size,bool out)1001cdd059d7SJiri Olsa static ssize_t dso_cache_io(struct dso *dso, struct machine *machine,
1002cdd059d7SJiri Olsa u64 offset, u8 *data, ssize_t size, bool out)
1003cdd059d7SJiri Olsa {
1004cdd059d7SJiri Olsa struct dso_cache *cache;
1005cdd059d7SJiri Olsa ssize_t ret = 0;
1006cdd059d7SJiri Olsa
1007cdd059d7SJiri Olsa cache = dso_cache__find(dso, machine, offset, &ret);
1008cdd059d7SJiri Olsa if (!cache)
1009cdd059d7SJiri Olsa return ret;
1010cdd059d7SJiri Olsa
1011cdd059d7SJiri Olsa return dso_cache__memcpy(cache, offset, data, size, out);
1012cdd059d7SJiri Olsa }
1013cdd059d7SJiri Olsa
1014cdd059d7SJiri Olsa /*
10155523769eSJiri Olsa * Reads and caches dso data DSO__DATA_CACHE_SIZE size chunks
1016c3fbd2a6SJiri Olsa * in the rb_tree. Any read to already cached data is served
101733bdedceSNamhyung Kim * by cached data. Writes update the cache only, not the backing file.
1018c3fbd2a6SJiri Olsa */
cached_io(struct dso * dso,struct machine * machine,u64 offset,u8 * data,ssize_t size,bool out)10196e81c74cSMasami Hiramatsu static ssize_t cached_io(struct dso *dso, struct machine *machine,
1020c3fbd2a6SJiri Olsa u64 offset, u8 *data, ssize_t size, bool out)
102133bdedceSNamhyung Kim {
102233bdedceSNamhyung Kim ssize_t r = 0;
102333bdedceSNamhyung Kim u8 *p = data;
102433bdedceSNamhyung Kim
102533bdedceSNamhyung Kim do {
102633bdedceSNamhyung Kim ssize_t ret;
102771ff824aSNamhyung Kim
102871ff824aSNamhyung Kim ret = dso_cache_io(dso, machine, offset, p, size, out);
102933bdedceSNamhyung Kim if (ret < 0)
103033bdedceSNamhyung Kim return ret;
103133bdedceSNamhyung Kim
103233bdedceSNamhyung Kim /* Reached EOF, return what we have. */
1033c3fbd2a6SJiri Olsa if (!ret)
1034c3fbd2a6SJiri Olsa break;
103533bdedceSNamhyung Kim
103633bdedceSNamhyung Kim BUG_ON(ret > size);
103733bdedceSNamhyung Kim
1038c8b5f2c9SArnaldo Carvalho de Melo r += ret;
103933bdedceSNamhyung Kim p += ret;
104033bdedceSNamhyung Kim offset += ret;
104133bdedceSNamhyung Kim size -= ret;
104233bdedceSNamhyung Kim
104333bdedceSNamhyung Kim } while (size);
104433bdedceSNamhyung Kim
104533bdedceSNamhyung Kim return r;
104633bdedceSNamhyung Kim }
1047c3fbd2a6SJiri Olsa
file_size(struct dso * dso,struct machine * machine)1048c3fbd2a6SJiri Olsa static int file_size(struct dso *dso, struct machine *machine)
10495523769eSJiri Olsa {
10505523769eSJiri Olsa int ret = 0;
10515523769eSJiri Olsa struct stat st;
10525523769eSJiri Olsa char sbuf[STRERR_BUFSIZE];
10535523769eSJiri Olsa
10545523769eSJiri Olsa pthread_mutex_lock(&dso__data_open_lock);
10555523769eSJiri Olsa
1056ef0580ecSArnaldo Carvalho de Melo /*
10576c398d72SJiri Olsa * dso->data.fd might be closed if other thread opened another
10586c398d72SJiri Olsa * file (dso) due to open file limit (RLIMIT_NOFILE).
1059ef0580ecSArnaldo Carvalho de Melo */
10605523769eSJiri Olsa try_to_open_dso(dso, machine);
10615523769eSJiri Olsa
10625523769eSJiri Olsa if (dso->data.fd < 0) {
10636d363459SAdrian Hunter ret = -errno;
10646d363459SAdrian Hunter dso->data.status = DSO_DATA_STATUS_ERROR;
10656d363459SAdrian Hunter goto out;
10666d363459SAdrian Hunter }
10676d363459SAdrian Hunter
10686d363459SAdrian Hunter if (fstat(dso->data.fd, &st) < 0) {
10696d363459SAdrian Hunter ret = -errno;
10706d363459SAdrian Hunter pr_err("dso cache fstat failed: %s\n",
10716d363459SAdrian Hunter str_error_r(errno, sbuf, sizeof(sbuf)));
1072b5c2161cSAdrian Hunter dso->data.status = DSO_DATA_STATUS_ERROR;
10736d363459SAdrian Hunter goto out;
10746d363459SAdrian Hunter }
10756d363459SAdrian Hunter dso->data.file_size = st.st_size;
10766d363459SAdrian Hunter
10776d363459SAdrian Hunter out:
10786d363459SAdrian Hunter pthread_mutex_unlock(&dso__data_open_lock);
1079b86a9d91SAdrian Hunter return ret;
1080b86a9d91SAdrian Hunter }
1081b86a9d91SAdrian Hunter
dso__data_file_size(struct dso * dso,struct machine * machine)1082c3fbd2a6SJiri Olsa int dso__data_file_size(struct dso *dso, struct machine *machine)
1083b5c2161cSAdrian Hunter {
1084c3fbd2a6SJiri Olsa if (dso->data.file_size)
1085c3fbd2a6SJiri Olsa return 0;
1086c3fbd2a6SJiri Olsa
1087c3fbd2a6SJiri Olsa if (dso->data.status == DSO_DATA_STATUS_ERROR)
1088c3fbd2a6SJiri Olsa return -1;
1089c3fbd2a6SJiri Olsa #ifdef HAVE_LIBBPF_SUPPORT
1090c3fbd2a6SJiri Olsa if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO)
1091c3fbd2a6SJiri Olsa return bpf_size(dso);
1092c3fbd2a6SJiri Olsa #endif
1093b86a9d91SAdrian Hunter return file_size(dso, machine);
1094c3fbd2a6SJiri Olsa }
1095c3fbd2a6SJiri Olsa
1096c1f9aa0aSJiri Olsa /**
1097c1f9aa0aSJiri Olsa * dso__data_size - Return dso data size
1098c1f9aa0aSJiri Olsa * @dso: dso object
1099c1f9aa0aSJiri Olsa * @machine: machine object
1100c1f9aa0aSJiri Olsa *
1101c1f9aa0aSJiri Olsa * Return: dso data size
1102c1f9aa0aSJiri Olsa */
dso__data_size(struct dso * dso,struct machine * machine)1103c1f9aa0aSJiri Olsa off_t dso__data_size(struct dso *dso, struct machine *machine)
1104c1f9aa0aSJiri Olsa {
1105c1f9aa0aSJiri Olsa if (dso__data_file_size(dso, machine))
1106c1f9aa0aSJiri Olsa return -1;
1107c3fbd2a6SJiri Olsa
1108c3fbd2a6SJiri Olsa /* For now just estimate dso data size is close to file size */
1109c3fbd2a6SJiri Olsa return dso->data.file_size;
111033bdedceSNamhyung Kim }
1111c3fbd2a6SJiri Olsa
data_read_write_offset(struct dso * dso,struct machine * machine,u64 offset,u8 * data,ssize_t size,bool out)1112c3fbd2a6SJiri Olsa static ssize_t data_read_write_offset(struct dso *dso, struct machine *machine,
1113b86a9d91SAdrian Hunter u64 offset, u8 *data, ssize_t size,
1114c3fbd2a6SJiri Olsa bool out)
1115c3fbd2a6SJiri Olsa {
1116c1f9aa0aSJiri Olsa if (dso__data_file_size(dso, machine))
1117c1f9aa0aSJiri Olsa return -1;
1118c1f9aa0aSJiri Olsa
1119c1f9aa0aSJiri Olsa /* Check the offset sanity. */
1120c1f9aa0aSJiri Olsa if (offset > dso->data.file_size)
1121c1f9aa0aSJiri Olsa return -1;
1122c1f9aa0aSJiri Olsa
1123c1f9aa0aSJiri Olsa if (offset + size < offset)
1124c1f9aa0aSJiri Olsa return -1;
1125c1f9aa0aSJiri Olsa
1126cdd059d7SJiri Olsa return cached_io(dso, machine, offset, data, size, out);
1127cdd059d7SJiri Olsa }
1128cdd059d7SJiri Olsa
1129cdd059d7SJiri Olsa /**
113078a1f7cdSIan Rogers * dso__data_read_offset - Read data from dso file offset
113178a1f7cdSIan Rogers * @dso: dso object
1132cdd059d7SJiri Olsa * @machine: machine object
1133cdd059d7SJiri Olsa * @offset: file offset
1134cdd059d7SJiri Olsa * @data: buffer to store data
1135b86a9d91SAdrian Hunter * @size: size of the @data buffer
1136b86a9d91SAdrian Hunter *
1137b86a9d91SAdrian Hunter * External interface to read data from dso file offset. Open
1138b86a9d91SAdrian Hunter * dso data file and use cached_read to get the data.
1139b86a9d91SAdrian Hunter */
dso__data_read_offset(struct dso * dso,struct machine * machine,u64 offset,u8 * data,ssize_t size)1140b86a9d91SAdrian Hunter ssize_t dso__data_read_offset(struct dso *dso, struct machine *machine,
1141b86a9d91SAdrian Hunter u64 offset, u8 *data, ssize_t size)
1142b86a9d91SAdrian Hunter {
1143b86a9d91SAdrian Hunter if (dso->data.status == DSO_DATA_STATUS_ERROR)
1144b86a9d91SAdrian Hunter return -1;
1145b86a9d91SAdrian Hunter
1146b86a9d91SAdrian Hunter return data_read_write_offset(dso, machine, offset, data, size, true);
1147b86a9d91SAdrian Hunter }
1148b86a9d91SAdrian Hunter
1149b86a9d91SAdrian Hunter /**
1150b86a9d91SAdrian Hunter * dso__data_read_addr - Read data from dso address
1151b86a9d91SAdrian Hunter * @dso: dso object
1152b86a9d91SAdrian Hunter * @machine: machine object
1153b86a9d91SAdrian Hunter * @add: virtual memory address
1154b86a9d91SAdrian Hunter * @data: buffer to store data
1155b86a9d91SAdrian Hunter * @size: size of the @data buffer
1156b86a9d91SAdrian Hunter *
1157b86a9d91SAdrian Hunter * External interface to read data from dso address.
1158b86a9d91SAdrian Hunter */
dso__data_read_addr(struct dso * dso,struct map * map,struct machine * machine,u64 addr,u8 * data,ssize_t size)1159b86a9d91SAdrian Hunter ssize_t dso__data_read_addr(struct dso *dso, struct map *map,
1160b86a9d91SAdrian Hunter struct machine *machine, u64 addr,
1161b86a9d91SAdrian Hunter u8 *data, ssize_t size)
1162b86a9d91SAdrian Hunter {
1163b86a9d91SAdrian Hunter u64 offset = map__map_ip(map, addr);
1164b86a9d91SAdrian Hunter
1165b86a9d91SAdrian Hunter return dso__data_read_offset(dso, machine, offset, data, size);
1166b86a9d91SAdrian Hunter }
1167b86a9d91SAdrian Hunter
1168b86a9d91SAdrian Hunter /**
1169b86a9d91SAdrian Hunter * dso__data_write_cache_offs - Write data to dso data cache at file offset
1170b86a9d91SAdrian Hunter * @dso: dso object
117178a1f7cdSIan Rogers * @machine: machine object
117278a1f7cdSIan Rogers * @offset: file offset
1173b86a9d91SAdrian Hunter * @data: buffer to write
1174b86a9d91SAdrian Hunter * @size: size of the @data buffer
1175b86a9d91SAdrian Hunter *
1176cdd059d7SJiri Olsa * Write into the dso file data cache, but do not change the file itself.
1177cdd059d7SJiri Olsa */
dso__data_write_cache_offs(struct dso * dso,struct machine * machine,u64 offset,const u8 * data_in,ssize_t size)1178cdd059d7SJiri Olsa ssize_t dso__data_write_cache_offs(struct dso *dso, struct machine *machine,
1179cdd059d7SJiri Olsa u64 offset, const u8 *data_in, ssize_t size)
1180cdd059d7SJiri Olsa {
1181581e295aSRiccardo Mancini u8 *data = (u8 *)data_in; /* cast away const to use same fns for r/w */
11823183f8caSArnaldo Carvalho de Melo
1183581e295aSRiccardo Mancini if (dso->data.status == DSO_DATA_STATUS_ERROR)
1184581e295aSRiccardo Mancini return -1;
1185cdd059d7SJiri Olsa
1186cdd059d7SJiri Olsa return data_read_write_offset(dso, machine, offset, data, size, false);
1187cdd059d7SJiri Olsa }
1188cdd059d7SJiri Olsa
1189459ce518SArnaldo Carvalho de Melo /**
1190cdd059d7SJiri Olsa * dso__data_write_cache_addr - Write data to dso data cache at dso address
1191cdd059d7SJiri Olsa * @dso: dso object
1192cdd059d7SJiri Olsa * @machine: machine object
1193cdd059d7SJiri Olsa * @add: virtual memory address
1194cdd059d7SJiri Olsa * @data: buffer to write
1195aa7cc2aeSArnaldo Carvalho de Melo * @size: size of the @data buffer
1196cdd059d7SJiri Olsa *
1197cdd059d7SJiri Olsa * External interface to write into the dso file data cache, but do not change
1198cdd059d7SJiri Olsa * the file itself.
1199cdd059d7SJiri Olsa */
dso__data_write_cache_addr(struct dso * dso,struct map * map,struct machine * machine,u64 addr,const u8 * data,ssize_t size)1200cdd059d7SJiri Olsa ssize_t dso__data_write_cache_addr(struct dso *dso, struct map *map,
1201cdd059d7SJiri Olsa struct machine *machine, u64 addr,
120258a98c9cSAdrian Hunter const u8 *data, ssize_t size)
1203cdd059d7SJiri Olsa {
1204cdd059d7SJiri Olsa u64 offset = map__map_ip(map, addr);
1205cdd059d7SJiri Olsa
1206cdd059d7SJiri Olsa return dso__data_write_cache_offs(dso, machine, offset, data, size);
1207cdd059d7SJiri Olsa }
1208cdd059d7SJiri Olsa
dso__new_map(const char * name)12090e3149f8SArnaldo Carvalho de Melo struct map *dso__new_map(const char *name)
1210cdd059d7SJiri Olsa {
1211e266a753SAdrian Hunter struct map *map = NULL;
1212e266a753SAdrian Hunter struct dso *dso = dso__new(name);
1213cdd059d7SJiri Olsa
1214cdd059d7SJiri Olsa if (dso) {
12157e155d4dSArnaldo Carvalho de Melo map = map__new2(0, dso);
12167e155d4dSArnaldo Carvalho de Melo dso__put(dso);
1217bf4414aeSArnaldo Carvalho de Melo }
12187e155d4dSArnaldo Carvalho de Melo
1219e266a753SAdrian Hunter return map;
1220e266a753SAdrian Hunter }
1221e266a753SAdrian Hunter
machine__findnew_kernel(struct machine * machine,const char * name,const char * short_name,int dso_type)12220e3149f8SArnaldo Carvalho de Melo struct dso *machine__findnew_kernel(struct machine *machine, const char *name,
12230e3149f8SArnaldo Carvalho de Melo const char *short_name, int dso_type)
1224e266a753SAdrian Hunter {
1225e266a753SAdrian Hunter /*
1226e266a753SAdrian Hunter * The kernel dso could be created by build_id processing.
1227e266a753SAdrian Hunter */
1228e266a753SAdrian Hunter struct dso *dso = machine__findnew_dso(machine, name);
1229cdd059d7SJiri Olsa
1230cdd059d7SJiri Olsa /*
12317e155d4dSArnaldo Carvalho de Melo * We need to run this in all cases, since during the build_id
1232e266a753SAdrian Hunter * processing we had no idea this was the kernel dso.
1233e266a753SAdrian Hunter */
12340e3149f8SArnaldo Carvalho de Melo if (dso != NULL) {
12350e3149f8SArnaldo Carvalho de Melo dso__set_short_name(dso, short_name, false);
12360e3149f8SArnaldo Carvalho de Melo dso->kernel = dso_type;
12370e3149f8SArnaldo Carvalho de Melo }
12380e3149f8SArnaldo Carvalho de Melo
12390e3149f8SArnaldo Carvalho de Melo return dso;
1240cdd059d7SJiri Olsa }
1241cdd059d7SJiri Olsa
dso__set_long_name_id(struct dso * dso,const char * name,struct dso_id * id,bool name_allocated)124258a98c9cSAdrian Hunter static void dso__set_long_name_id(struct dso *dso, const char *name, struct dso_id *id, bool name_allocated)
1243cdd059d7SJiri Olsa {
1244cdd059d7SJiri Olsa struct rb_root *root = dso->root;
1245cdd059d7SJiri Olsa
124658a98c9cSAdrian Hunter if (name == NULL)
124758a98c9cSAdrian Hunter return;
124858a98c9cSAdrian Hunter
124958a98c9cSAdrian Hunter if (dso->long_name_allocated)
1250cdd059d7SJiri Olsa free((char *)dso->long_name);
1251cdd059d7SJiri Olsa
125258a98c9cSAdrian Hunter if (root) {
1253cdd059d7SJiri Olsa rb_erase(&dso->rb_node, root);
1254cdd059d7SJiri Olsa /*
1255cdd059d7SJiri Olsa * __dsos__findnew_link_by_longname_id() isn't guaranteed to
1256cdd059d7SJiri Olsa * add it back, so a clean removal is required here.
1257cdd059d7SJiri Olsa */
1258cdd059d7SJiri Olsa RB_CLEAR_NODE(&dso->rb_node);
1259bb963e16SNamhyung Kim dso->root = NULL;
1260cdd059d7SJiri Olsa }
1261cdd059d7SJiri Olsa
1262cdd059d7SJiri Olsa dso->long_name = name;
1263cdd059d7SJiri Olsa dso->long_name_len = strlen(name);
1264cdd059d7SJiri Olsa dso->long_name_allocated = name_allocated;
12653183f8caSArnaldo Carvalho de Melo
1266cdd059d7SJiri Olsa if (root)
12673183f8caSArnaldo Carvalho de Melo __dsos__findnew_link_by_longname_id(root, dso, NULL, id);
1268cdd059d7SJiri Olsa }
1269cdd059d7SJiri Olsa
dso__set_long_name(struct dso * dso,const char * name,bool name_allocated)12703183f8caSArnaldo Carvalho de Melo void dso__set_long_name(struct dso *dso, const char *name, bool name_allocated)
1271cdd059d7SJiri Olsa {
12723183f8caSArnaldo Carvalho de Melo dso__set_long_name_id(dso, name, NULL, name_allocated);
1273cdd059d7SJiri Olsa }
1274cdd059d7SJiri Olsa
dso__set_short_name(struct dso * dso,const char * name,bool name_allocated)12753183f8caSArnaldo Carvalho de Melo void dso__set_short_name(struct dso *dso, const char *name, bool name_allocated)
1276cdd059d7SJiri Olsa {
12773183f8caSArnaldo Carvalho de Melo if (name == NULL)
1278cdd059d7SJiri Olsa return;
1279cdd059d7SJiri Olsa
12800e3149f8SArnaldo Carvalho de Melo if (dso->short_name_allocated)
1281cdd059d7SJiri Olsa free((char *)dso->short_name);
1282cdd059d7SJiri Olsa
1283cdd059d7SJiri Olsa dso->short_name = name;
1284cdd059d7SJiri Olsa dso->short_name_len = strlen(name);
1285cdd059d7SJiri Olsa dso->short_name_allocated = name_allocated;
12860e3149f8SArnaldo Carvalho de Melo }
12870e3149f8SArnaldo Carvalho de Melo
dso__name_len(const struct dso * dso)12880e3149f8SArnaldo Carvalho de Melo int dso__name_len(const struct dso *dso)
128958a98c9cSAdrian Hunter {
12907137ff50SDavidlohr Bueso if (!dso)
1291ca40e2afSJiri Olsa return strlen("[unknown]");
129255ecd631SDavidlohr Bueso if (verbose > 0)
129355ecd631SDavidlohr Bueso return dso->long_name_len;
129453fa8eaaSJiri Olsa
1295c27697d6SAdrian Hunter return dso->short_name_len;
1296cdd059d7SJiri Olsa }
12975f70619dSArnaldo Carvalho de Melo
dso__loaded(const struct dso * dso)1298c6d8f2a4SAdrian Hunter bool dso__loaded(const struct dso *dso)
1299cdd059d7SJiri Olsa {
13000131c4ecSAdrian Hunter return dso->loaded;
1301cdd059d7SJiri Olsa }
1302cdd059d7SJiri Olsa
dso__sorted_by_name(const struct dso * dso)13032cc9d0efSNamhyung Kim bool dso__sorted_by_name(const struct dso *dso)
1304906049c8SAdrian Hunter {
13051c695c88SJiri Olsa return dso->sorted_by_name;
1306cdd059d7SJiri Olsa }
13072af52475SJiri Olsa
dso__set_sorted_by_name(struct dso * dso)13084598a0a6SWaiman Long void dso__set_sorted_by_name(struct dso *dso)
1309e266a753SAdrian Hunter {
1310cdd059d7SJiri Olsa dso->sorted_by_name = true;
1311eba5102dSJiri Olsa }
1312d9a0d6b8SIan Rogers
dso__new_id(const char * name,struct dso_id * id)13137100810aSElena Reshetova struct dso *dso__new_id(const char *name, struct dso_id *id)
1314cdd059d7SJiri Olsa {
1315cdd059d7SJiri Olsa struct dso *dso = calloc(1, sizeof(*dso) + strlen(name) + 1);
1316cdd059d7SJiri Olsa
1317cdd059d7SJiri Olsa if (dso != NULL) {
1318cdd059d7SJiri Olsa strcpy(dso->name, name);
13190e3149f8SArnaldo Carvalho de Melo if (id)
13200e3149f8SArnaldo Carvalho de Melo dso->id = *id;
13210e3149f8SArnaldo Carvalho de Melo dso__set_long_name_id(dso, dso->name, id, false);
13220e3149f8SArnaldo Carvalho de Melo dso__set_short_name(dso, dso->name, false);
13230e3149f8SArnaldo Carvalho de Melo dso->symbols = RB_ROOT_CACHED;
1324cdd059d7SJiri Olsa dso->symbol_names = NULL;
1325cdd059d7SJiri Olsa dso->symbol_names_len = 0;
13264598a0a6SWaiman Long dso->data.cache = RB_ROOT;
13274598a0a6SWaiman Long dso->inlined_nodes = RB_ROOT_CACHED;
13284598a0a6SWaiman Long dso->srclines = RB_ROOT_CACHED;
132911ea2515SMilian Wolff dso->data.fd = -1;
133011ea2515SMilian Wolff dso->data.status = DSO_DATA_STATUS_UNKNOWN;
133111ea2515SMilian Wolff dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
133221ac9d54SMilian Wolff dso->binary_type = DSO_BINARY_TYPE__NOT_FOUND;
13333183f8caSArnaldo Carvalho de Melo dso->is_64_bit = (sizeof(void *) == 8);
1334ee021d42SArnaldo Carvalho de Melo dso->loaded = 0;
1335ee021d42SArnaldo Carvalho de Melo dso->rel = 0;
133604662523SArnaldo Carvalho de Melo dso->sorted_by_name = 0;
1337ee021d42SArnaldo Carvalho de Melo dso->has_build_id = 0;
1338ee021d42SArnaldo Carvalho de Melo dso->has_srcline = 1;
1339ee021d42SArnaldo Carvalho de Melo dso->a2l_fails = 1;
1340ee021d42SArnaldo Carvalho de Melo dso->kernel = DSO_SPACE__USER;
134104662523SArnaldo Carvalho de Melo dso->needs_swap = DSO_SWAP__UNSET;
1342ee021d42SArnaldo Carvalho de Melo dso->comp = COMP_ID__NONE;
1343ee021d42SArnaldo Carvalho de Melo RB_CLEAR_NODE(&dso->rb_node);
1344ee021d42SArnaldo Carvalho de Melo dso->root = NULL;
134553fa8eaaSJiri Olsa INIT_LIST_HEAD(&dso->node);
1346cfe9174fSAdrian Hunter INIT_LIST_HEAD(&dso->data.open_entry);
13478e67b725SNamhyung Kim mutex_init(&dso->lock);
1348454ff00fSAdrian Hunter refcount_set(&dso->refcnt, 1);
134904662523SArnaldo Carvalho de Melo }
1350843ff37bSKrister Johansen
1351d9a0d6b8SIan Rogers return dso;
1352cdd059d7SJiri Olsa }
1353cdd059d7SJiri Olsa
dso__new(const char * name)1354cdd059d7SJiri Olsa struct dso *dso__new(const char *name)
1355d3a7c489SArnaldo Carvalho de Melo {
1356d3a7c489SArnaldo Carvalho de Melo return dso__new_id(name, NULL);
1357d3a7c489SArnaldo Carvalho de Melo }
13587100810aSElena Reshetova
dso__delete(struct dso * dso)1359d3a7c489SArnaldo Carvalho de Melo void dso__delete(struct dso *dso)
1360d3a7c489SArnaldo Carvalho de Melo {
1361d3a7c489SArnaldo Carvalho de Melo if (!RB_EMPTY_NODE(&dso->rb_node))
1362d3a7c489SArnaldo Carvalho de Melo pr_err("DSO %s is still in rbtree when being deleted!\n",
1363d3a7c489SArnaldo Carvalho de Melo dso->long_name);
13647100810aSElena Reshetova
1365d3a7c489SArnaldo Carvalho de Melo /* free inlines first, as they reference symbols */
1366d3a7c489SArnaldo Carvalho de Melo inlines__tree_delete(&dso->inlined_nodes);
1367d3a7c489SArnaldo Carvalho de Melo srcline__tree_delete(&dso->srclines);
13688dfdf440SJiri Olsa symbols__delete(&dso->symbols);
1369cdd059d7SJiri Olsa dso->symbol_names_len = 0;
13708dfdf440SJiri Olsa zfree(&dso->symbol_names);
1371cdd059d7SJiri Olsa if (dso->short_name_allocated) {
1372cdd059d7SJiri Olsa zfree((char **)&dso->short_name);
1373cdd059d7SJiri Olsa dso->short_name_allocated = false;
137439be8d01SJiri Olsa }
1375cdd059d7SJiri Olsa
13764a86d414SNamhyung Kim if (dso->long_name_allocated) {
13774a86d414SNamhyung Kim zfree((char **)&dso->long_name);
13784a86d414SNamhyung Kim dso->long_name_allocated = false;
13794a86d414SNamhyung Kim }
13804a86d414SNamhyung Kim
13814a86d414SNamhyung Kim dso__data_close(dso);
13824a86d414SNamhyung Kim auxtrace_cache__free(dso->auxtrace_cache);
13834a86d414SNamhyung Kim dso_cache__free(dso);
13844a86d414SNamhyung Kim dso__free_a2l(dso);
13854a86d414SNamhyung Kim zfree(&dso->symsrc_filename);
138639be8d01SJiri Olsa nsinfo__zput(dso->nsinfo);
138739be8d01SJiri Olsa mutex_destroy(&dso->lock);
1388cdd059d7SJiri Olsa free(dso);
1389cdd059d7SJiri Olsa }
1390cdd059d7SJiri Olsa
dso__get(struct dso * dso)1391cdd059d7SJiri Olsa struct dso *dso__get(struct dso *dso)
1392cdd059d7SJiri Olsa {
1393cdd059d7SJiri Olsa if (dso)
1394cdd059d7SJiri Olsa refcount_inc(&dso->refcnt);
1395cdd059d7SJiri Olsa return dso;
1396cdd059d7SJiri Olsa }
13973ff1b8c8SJiri Olsa
dso__put(struct dso * dso)1398cdd059d7SJiri Olsa void dso__put(struct dso *dso)
1399cdd059d7SJiri Olsa {
1400cdd059d7SJiri Olsa if (dso && refcount_dec_and_test(&dso->refcnt))
1401cdd059d7SJiri Olsa dso__delete(dso);
1402cdd059d7SJiri Olsa }
1403cdd059d7SJiri Olsa
dso__set_build_id(struct dso * dso,struct build_id * bid)1404cdd059d7SJiri Olsa void dso__set_build_id(struct dso *dso, struct build_id *bid)
1405cdd059d7SJiri Olsa {
1406cdd059d7SJiri Olsa dso->bid = *bid;
1407cdd059d7SJiri Olsa dso->has_build_id = 1;
1408cdd059d7SJiri Olsa }
1409cdd059d7SJiri Olsa
dso__build_id_equal(const struct dso * dso,struct build_id * bid)1410cdd059d7SJiri Olsa bool dso__build_id_equal(const struct dso *dso, struct build_id *bid)
1411cdd059d7SJiri Olsa {
1412cdd059d7SJiri Olsa if (dso->bid.size > bid->size && dso->bid.size == BUILD_ID_SIZE) {
1413cdd059d7SJiri Olsa /*
1414cdd059d7SJiri Olsa * For the backward compatibility, it allows a build-id has
14153ff1b8c8SJiri Olsa * trailing zeros.
1416cdd059d7SJiri Olsa */
1417cdd059d7SJiri Olsa return !memcmp(dso->bid.data, bid->data, bid->size) &&
1418cdd059d7SJiri Olsa !memchr_inv(&dso->bid.data[bid->size], 0,
1419cdd059d7SJiri Olsa dso->bid.size - bid->size);
1420cdd059d7SJiri Olsa }
1421e9ad9438SJiri Olsa
1422cdd059d7SJiri Olsa return dso->bid.size == bid->size &&
1423b5d8bbe8SMasami Hiramatsu memcmp(dso->bid.data, bid->data, dso->bid.size) == 0;
1424cdd059d7SJiri Olsa }
1425bf541169SJiri Olsa
dso__read_running_kernel_build_id(struct dso * dso,struct machine * machine)1426cdd059d7SJiri Olsa void dso__read_running_kernel_build_id(struct dso *dso, struct machine *machine)
1427cdd059d7SJiri Olsa {
1428cdd059d7SJiri Olsa char path[PATH_MAX];
14293183f8caSArnaldo Carvalho de Melo
1430cdd059d7SJiri Olsa if (machine__is_default_guest(machine))
1431cdd059d7SJiri Olsa return;
1432cdd059d7SJiri Olsa sprintf(path, "%s/sys/kernel/notes", machine->root_dir);
1433cdd059d7SJiri Olsa if (sysfs__read_build_id(path, &dso->bid) == 0)
1434cdd059d7SJiri Olsa dso->has_build_id = true;
1435cdd059d7SJiri Olsa }
14363183f8caSArnaldo Carvalho de Melo
dso__kernel_module_get_build_id(struct dso * dso,const char * root_dir)1437cdd059d7SJiri Olsa int dso__kernel_module_get_build_id(struct dso *dso,
1438cdd059d7SJiri Olsa const char *root_dir)
14397137ff50SDavidlohr Bueso {
1440cdd059d7SJiri Olsa char filename[PATH_MAX];
1441cdd059d7SJiri Olsa /*
1442cdd059d7SJiri Olsa * kernel module short names are of the form "[module]" and
1443cdd059d7SJiri Olsa * we need just "module" here.
1444cdd059d7SJiri Olsa */
1445cdd059d7SJiri Olsa const char *name = dso->short_name + 1;
14462b5b8bb2SAdrian Hunter
14472b5b8bb2SAdrian Hunter snprintf(filename, sizeof(filename),
14482b5b8bb2SAdrian Hunter "%s/sys/module/%.*s/notes/.note.gnu.build-id",
14492b5b8bb2SAdrian Hunter root_dir, (int)strlen(name) - 1, name);
14504bb11d01SNamhyung Kim
14512b5b8bb2SAdrian Hunter if (sysfs__read_build_id(filename, &dso->bid) == 0)
14524bb11d01SNamhyung Kim dso->has_build_id = true;
14534bb11d01SNamhyung Kim
14544bb11d01SNamhyung Kim return 0;
14554bb11d01SNamhyung Kim }
14564bb11d01SNamhyung Kim
dso__fprintf_buildid(struct dso * dso,FILE * fp)14572b5b8bb2SAdrian Hunter static size_t dso__fprintf_buildid(struct dso *dso, FILE *fp)
14584bb11d01SNamhyung Kim {
14592b5b8bb2SAdrian Hunter char sbuild_id[SBUILD_ID_SIZE];
146018425f13SArnaldo Carvalho de Melo
146118425f13SArnaldo Carvalho de Melo build_id__sprintf(&dso->bid, sbuild_id);
146218425f13SArnaldo Carvalho de Melo return fprintf(fp, "%s", sbuild_id);
146318425f13SArnaldo Carvalho de Melo }
146418425f13SArnaldo Carvalho de Melo
dso__fprintf(struct dso * dso,FILE * fp)146518425f13SArnaldo Carvalho de Melo size_t dso__fprintf(struct dso *dso, FILE *fp)
146618425f13SArnaldo Carvalho de Melo {
146718425f13SArnaldo Carvalho de Melo struct rb_node *nd;
146818425f13SArnaldo Carvalho de Melo size_t ret = fprintf(fp, "dso: %s (", dso->short_name);
146918425f13SArnaldo Carvalho de Melo
147018425f13SArnaldo Carvalho de Melo if (dso->short_name != dso->long_name)
147118425f13SArnaldo Carvalho de Melo ret += fprintf(fp, "%s, ", dso->long_name);
147218425f13SArnaldo Carvalho de Melo ret += fprintf(fp, "%sloaded, ", dso__loaded(dso) ? "" : "NOT ");
147318425f13SArnaldo Carvalho de Melo ret += dso__fprintf_buildid(dso, fp);
147418425f13SArnaldo Carvalho de Melo ret += fprintf(fp, ")\n");
147518425f13SArnaldo Carvalho de Melo for (nd = rb_first_cached(&dso->symbols); nd; nd = rb_next(nd)) {
147618425f13SArnaldo Carvalho de Melo struct symbol *pos = rb_entry(nd, struct symbol, rb_node);
147718425f13SArnaldo Carvalho de Melo ret += symbol__fprintf(pos, fp);
1478c8b5f2c9SArnaldo Carvalho de Melo }
147918425f13SArnaldo Carvalho de Melo
148018425f13SArnaldo Carvalho de Melo return ret;
148118425f13SArnaldo Carvalho de Melo }
148218425f13SArnaldo Carvalho de Melo
dso__type(struct dso * dso,struct machine * machine)148318425f13SArnaldo Carvalho de Melo enum dso_type dso__type(struct dso *dso, struct machine *machine)
148418425f13SArnaldo Carvalho de Melo {
148518425f13SArnaldo Carvalho de Melo int fd;
148618425f13SArnaldo Carvalho de Melo enum dso_type type = DSO__TYPE_UNKNOWN;
148718425f13SArnaldo Carvalho de Melo
148818425f13SArnaldo Carvalho de Melo fd = dso__data_get_fd(dso, machine);
148918425f13SArnaldo Carvalho de Melo if (fd >= 0) {
149018425f13SArnaldo Carvalho de Melo type = dso__type_fd(fd);
149118425f13SArnaldo Carvalho de Melo dso__data_put_fd(dso);
149218425f13SArnaldo Carvalho de Melo }
1493
1494 return type;
1495 }
1496
dso__strerror_load(struct dso * dso,char * buf,size_t buflen)1497 int dso__strerror_load(struct dso *dso, char *buf, size_t buflen)
1498 {
1499 int idx, errnum = dso->load_errno;
1500 /*
1501 * This must have a same ordering as the enum dso_load_errno.
1502 */
1503 static const char *dso_load__error_str[] = {
1504 "Internal tools/perf/ library error",
1505 "Invalid ELF file",
1506 "Can not read build id",
1507 "Mismatching build id",
1508 "Decompression failure",
1509 };
1510
1511 BUG_ON(buflen == 0);
1512
1513 if (errnum >= 0) {
1514 const char *err = str_error_r(errnum, buf, buflen);
1515
1516 if (err != buf)
1517 scnprintf(buf, buflen, "%s", err);
1518
1519 return 0;
1520 }
1521
1522 if (errnum < __DSO_LOAD_ERRNO__START || errnum >= __DSO_LOAD_ERRNO__END)
1523 return -1;
1524
1525 idx = errnum - __DSO_LOAD_ERRNO__START;
1526 scnprintf(buf, buflen, "%s", dso_load__error_str[idx]);
1527 return 0;
1528 }
1529