Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/*
2 * builtin-buildid-list.c
3 *
4 * Builtin buildid-list command: list buildids in perf.data, in the running
5 * kernel and in ELF files.
6 *
7 * Copyright (C) 2009, Red Hat Inc.
8 * Copyright (C) 2009, Arnaldo Carvalho de Melo <acme@redhat.com>
9 */
10#include "builtin.h"
11#include "util/build-id.h"
12#include "util/debug.h"
13#include "util/dso.h"
14#include "util/map.h"
15#include <subcmd/pager.h>
16#include <subcmd/parse-options.h>
17#include "util/session.h"
18#include "util/symbol.h"
19#include "util/data.h"
20#include "util/util.h"
21#include <errno.h>
22#include <inttypes.h>
23#include <linux/err.h>
24
25static int buildid__map_cb(struct map *map, void *arg __maybe_unused)
26{
27 const struct dso *dso = map__dso(map);
28 char bid_buf[SBUILD_ID_SIZE];
29 const char *dso_long_name = dso__long_name(dso);
30 const char *dso_short_name = dso__short_name(dso);
31
32 memset(bid_buf, 0, sizeof(bid_buf));
33 if (dso__has_build_id(dso))
34 build_id__snprintf(dso__bid(dso), bid_buf, sizeof(bid_buf));
35 printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map__start(map), map__end(map));
36 if (dso_long_name != NULL)
37 printf(" %s", dso_long_name);
38 else if (dso_short_name != NULL)
39 printf(" %s", dso_short_name);
40
41 printf("\n");
42
43 return 0;
44}
45
46static void buildid__show_kernel_maps(void)
47{
48 struct perf_env host_env;
49 struct machine *machine;
50
51 perf_env__init(&host_env);
52 machine = machine__new_host(&host_env);
53 machine__for_each_kernel_map(machine, buildid__map_cb, NULL);
54 machine__delete(machine);
55 perf_env__exit(&host_env);
56}
57
58static int sysfs__fprintf_build_id(FILE *fp)
59{
60 char sbuild_id[SBUILD_ID_SIZE];
61 int ret;
62
63 ret = sysfs__snprintf_build_id("/", sbuild_id, sizeof(sbuild_id));
64 if (ret != sizeof(sbuild_id))
65 return ret < 0 ? ret : -EINVAL;
66
67 return fprintf(fp, "%s\n", sbuild_id);
68}
69
70static int filename__fprintf_build_id(const char *name, FILE *fp)
71{
72 char sbuild_id[SBUILD_ID_SIZE];
73 int ret;
74
75 ret = filename__snprintf_build_id(name, sbuild_id, sizeof(sbuild_id));
76 if (ret != sizeof(sbuild_id))
77 return ret < 0 ? ret : -EINVAL;
78
79 return fprintf(fp, "%s\n", sbuild_id);
80}
81
82static bool dso__skip_buildid(struct dso *dso, int with_hits)
83{
84 return with_hits && !dso__hit(dso);
85}
86
87static int perf_session__list_build_ids(bool force, bool with_hits)
88{
89 struct perf_session *session;
90 struct perf_data data = {
91 .path = input_name,
92 .mode = PERF_DATA_MODE_READ,
93 .force = force,
94 };
95 struct perf_tool build_id__mark_dso_hit_ops;
96
97 symbol__elf_init();
98 /*
99 * See if this is an ELF file first:
100 */
101 if (filename__fprintf_build_id(input_name, stdout) > 0)
102 goto out;
103
104 perf_tool__init(&build_id__mark_dso_hit_ops, /*ordered_events=*/true);
105 build_id__mark_dso_hit_ops.sample = build_id__mark_dso_hit;
106 build_id__mark_dso_hit_ops.mmap = perf_event__process_mmap;
107 build_id__mark_dso_hit_ops.mmap2 = perf_event__process_mmap2;
108 build_id__mark_dso_hit_ops.fork = perf_event__process_fork;
109 build_id__mark_dso_hit_ops.exit = perf_event__exit_del_thread;
110 build_id__mark_dso_hit_ops.attr = perf_event__process_attr;
111 build_id__mark_dso_hit_ops.build_id = perf_event__process_build_id;
112
113 session = perf_session__new(&data, &build_id__mark_dso_hit_ops);
114 if (IS_ERR(session))
115 return PTR_ERR(session);
116
117 /*
118 * We take all buildids when the file contains AUX area tracing data
119 * because we do not decode the trace because it would take too long.
120 */
121 if (!perf_data__is_pipe(&data) &&
122 perf_header__has_feat(&session->header, HEADER_AUXTRACE))
123 with_hits = false;
124
125 if (!perf_header__has_feat(&session->header, HEADER_BUILD_ID))
126 with_hits = true;
127
128 if (zstd_init(&(session->zstd_data), 0) < 0)
129 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
130
131 /*
132 * in pipe-mode, the only way to get the buildids is to parse
133 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
134 */
135 if (with_hits || perf_data__is_pipe(&data))
136 perf_session__process_events(session);
137
138 perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
139 perf_session__delete(session);
140out:
141 return 0;
142}
143
144int cmd_buildid_list(int argc, const char **argv)
145{
146 bool show_kernel = false;
147 bool show_kernel_maps = false;
148 bool with_hits = false;
149 bool force = false;
150 const struct option options[] = {
151 OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
152 OPT_STRING('i', "input", &input_name, "file", "input file name"),
153 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
154 OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
155 OPT_BOOLEAN('m', "kernel-maps", &show_kernel_maps,
156 "Show build id of current kernel + modules"),
157 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
158 OPT_END()
159 };
160 const char * const buildid_list_usage[] = {
161 "perf buildid-list [<options>]",
162 NULL
163 };
164
165 argc = parse_options(argc, argv, options, buildid_list_usage, 0);
166 setup_pager();
167
168 if (show_kernel) {
169 return !(sysfs__fprintf_build_id(stdout) > 0);
170 } else if (show_kernel_maps) {
171 buildid__show_kernel_maps();
172 return 0;
173 }
174
175 return perf_session__list_build_ids(force, with_hits);
176}