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__sprintf(dso__bid_const(dso), 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 machine *machine;
49
50	machine = machine__new_host();
51	machine__for_each_kernel_map(machine, buildid__map_cb, NULL);
52	machine__delete(machine);
53}
54
55static int sysfs__fprintf_build_id(FILE *fp)
56{
57	char sbuild_id[SBUILD_ID_SIZE];
58	int ret;
59
60	ret = sysfs__sprintf_build_id("/", sbuild_id);
61	if (ret != sizeof(sbuild_id))
62		return ret < 0 ? ret : -EINVAL;
63
64	return fprintf(fp, "%s\n", sbuild_id);
65}
66
67static int filename__fprintf_build_id(const char *name, FILE *fp)
68{
69	char sbuild_id[SBUILD_ID_SIZE];
70	int ret;
71
72	ret = filename__sprintf_build_id(name, sbuild_id);
73	if (ret != sizeof(sbuild_id))
74		return ret < 0 ? ret : -EINVAL;
75
76	return fprintf(fp, "%s\n", sbuild_id);
77}
78
79static bool dso__skip_buildid(struct dso *dso, int with_hits)
80{
81	return with_hits && !dso__hit(dso);
82}
83
84static int perf_session__list_build_ids(bool force, bool with_hits)
85{
86	struct perf_session *session;
87	struct perf_data data = {
88		.path  = input_name,
89		.mode  = PERF_DATA_MODE_READ,
90		.force = force,
91	};
92
93	symbol__elf_init();
94	/*
95	 * See if this is an ELF file first:
96	 */
97	if (filename__fprintf_build_id(input_name, stdout) > 0)
98		goto out;
99
100	session = perf_session__new(&data, &build_id__mark_dso_hit_ops);
101	if (IS_ERR(session))
102		return PTR_ERR(session);
103
104	/*
105	 * We take all buildids when the file contains AUX area tracing data
106	 * because we do not decode the trace because it would take too long.
107	 */
108	if (!perf_data__is_pipe(&data) &&
109	    perf_header__has_feat(&session->header, HEADER_AUXTRACE))
110		with_hits = false;
111
112	if (!perf_header__has_feat(&session->header, HEADER_BUILD_ID))
113		with_hits = true;
114
115	if (zstd_init(&(session->zstd_data), 0) < 0)
116		pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
117
118	/*
119	 * in pipe-mode, the only way to get the buildids is to parse
120	 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
121	 */
122	if (with_hits || perf_data__is_pipe(&data))
123		perf_session__process_events(session);
124
125	perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
126	perf_session__delete(session);
127out:
128	return 0;
129}
130
131int cmd_buildid_list(int argc, const char **argv)
132{
133	bool show_kernel = false;
134	bool show_kernel_maps = false;
135	bool with_hits = false;
136	bool force = false;
137	const struct option options[] = {
138	OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
139	OPT_STRING('i', "input", &input_name, "file", "input file name"),
140	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
141	OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
142	OPT_BOOLEAN('m', "kernel-maps", &show_kernel_maps,
143	    "Show build id of current kernel + modules"),
144	OPT_INCR('v', "verbose", &verbose, "be more verbose"),
145	OPT_END()
146	};
147	const char * const buildid_list_usage[] = {
148		"perf buildid-list [<options>]",
149		NULL
150	};
151
152	argc = parse_options(argc, argv, options, buildid_list_usage, 0);
153	setup_pager();
154
155	if (show_kernel) {
156		return !(sysfs__fprintf_build_id(stdout) > 0);
157	} else if (show_kernel_maps) {
158		buildid__show_kernel_maps();
159		return 0;
160	}
161
162	return perf_session__list_build_ids(force, with_hits);
163}
164