blob: c9037477865a79aa9ad7e6f9df8053e8c3b778ff [file] [log] [blame]
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -02001/*
2 * builtin-buildid-list.c
3 *
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -03004 * Builtin buildid-list command: list buildids in perf.data, in the running
5 * kernel and in ELF files.
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -02006 *
7 * Copyright (C) 2009, Red Hat Inc.
8 * Copyright (C) 2009, Arnaldo Carvalho de Melo <acme@redhat.com>
9 */
10#include "builtin.h"
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020011#include "util/build-id.h"
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -020012#include "util/debug.h"
Arnaldo Carvalho de Melo4a3cec82019-08-30 11:11:01 -030013#include "util/dso.h"
Blake Jonesa6bd98c2022-06-29 14:36:32 -070014#include "util/map.h"
Arnaldo Carvalho de Melofa0d9842019-08-30 12:52:25 -030015#include <subcmd/pager.h>
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060016#include <subcmd/parse-options.h>
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020017#include "util/session.h"
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -020018#include "util/symbol.h"
Jiri Olsaf5fc14122013-10-15 16:27:32 +020019#include "util/data.h"
Ian Rogersf12ad272023-04-10 09:25:10 -070020#include "util/util.h"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030021#include <errno.h>
Blake Jonesa6bd98c2022-06-29 14:36:32 -070022#include <inttypes.h>
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053023#include <linux/err.h>
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -020024
Blake Jonesa6bd98c2022-06-29 14:36:32 -070025static int buildid__map_cb(struct map *map, void *arg __maybe_unused)
26{
Ian Rogers63df0e42023-03-20 14:22:35 -070027 const struct dso *dso = map__dso(map);
Blake Jonesa6bd98c2022-06-29 14:36:32 -070028 char bid_buf[SBUILD_ID_SIZE];
29
30 memset(bid_buf, 0, sizeof(bid_buf));
31 if (dso->has_build_id)
32 build_id__sprintf(&dso->bid, bid_buf);
Ian Rogerse5116f42023-03-20 14:22:36 -070033 printf("%s %16" PRIx64 " %16" PRIx64, bid_buf, map__start(map), map__end(map));
Blake Jonesa6bd98c2022-06-29 14:36:32 -070034 if (dso->long_name != NULL) {
35 printf(" %s", dso->long_name);
36 } else if (dso->short_name != NULL) {
37 printf(" %s", dso->short_name);
38 }
39 printf("\n");
40
41 return 0;
42}
43
44static void buildid__show_kernel_maps(void)
45{
46 struct machine *machine;
47
48 machine = machine__new_host();
49 machine__for_each_kernel_map(machine, buildid__map_cb, NULL);
50 machine__delete(machine);
51}
52
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030053static int sysfs__fprintf_build_id(FILE *fp)
54{
Masami Hiramatsud77fac72015-07-15 18:14:28 +090055 char sbuild_id[SBUILD_ID_SIZE];
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090056 int ret;
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030057
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090058 ret = sysfs__sprintf_build_id("/", sbuild_id);
59 if (ret != sizeof(sbuild_id))
60 return ret < 0 ? ret : -EINVAL;
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030061
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090062 return fprintf(fp, "%s\n", sbuild_id);
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030063}
64
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030065static int filename__fprintf_build_id(const char *name, FILE *fp)
66{
Masami Hiramatsud77fac72015-07-15 18:14:28 +090067 char sbuild_id[SBUILD_ID_SIZE];
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090068 int ret;
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030069
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090070 ret = filename__sprintf_build_id(name, sbuild_id);
71 if (ret != sizeof(sbuild_id))
72 return ret < 0 ? ret : -EINVAL;
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030073
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030074 return fprintf(fp, "%s\n", sbuild_id);
75}
76
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -030077static bool dso__skip_buildid(struct dso *dso, int with_hits)
78{
79 return with_hits && !dso->hit;
80}
81
Feng Tang70cb4e92012-10-30 11:56:02 +080082static int perf_session__list_build_ids(bool force, bool with_hits)
Robert Richter1b549502011-12-07 10:02:53 +010083{
84 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010085 struct perf_data data = {
Jiri Olsa2d4f2792019-02-21 10:41:30 +010086 .path = input_name,
87 .mode = PERF_DATA_MODE_READ,
88 .force = force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +020089 };
Robert Richter1b549502011-12-07 10:02:53 +010090
Namhyung Kim166ccc92012-08-06 13:41:19 +090091 symbol__elf_init();
Arnaldo Carvalho de Melof0bf9102012-12-05 16:24:05 -030092 /*
93 * See if this is an ELF file first:
94 */
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090095 if (filename__fprintf_build_id(input_name, stdout) > 0)
Arnaldo Carvalho de Melof0bf9102012-12-05 16:24:05 -030096 goto out;
Robert Richterefad1412011-12-07 10:02:54 +010097
Namhyung Kim2681bd82021-07-19 15:31:49 -070098 session = perf_session__new(&data, &build_id__mark_dso_hit_ops);
Mamatha Inamdar6ef81c52019-08-22 12:50:49 +053099 if (IS_ERR(session))
100 return PTR_ERR(session);
Adrian Huntercd10b282015-04-30 17:37:26 +0300101
102 /*
103 * We take all buildids when the file contains AUX area tracing data
104 * because we do not decode the trace because it would take too long.
105 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100106 if (!perf_data__is_pipe(&data) &&
Adrian Huntercd10b282015-04-30 17:37:26 +0300107 perf_header__has_feat(&session->header, HEADER_AUXTRACE))
108 with_hits = false;
109
Jiri Olsad176db92020-12-14 11:54:56 +0100110 if (!perf_header__has_feat(&session->header, HEADER_BUILD_ID))
111 with_hits = true;
112
Milian Wolffc67d7342021-04-29 20:57:59 +0200113 if (zstd_init(&(session->zstd_data), 0) < 0)
114 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
115
Stephane Eranian299c3452012-05-15 13:28:15 +0200116 /*
117 * in pipe-mode, the only way to get the buildids is to parse
118 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
119 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100120 if (with_hits || perf_data__is_pipe(&data))
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300121 perf_session__process_events(session);
Robert Richter1b549502011-12-07 10:02:53 +0100122
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -0300123 perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
Robert Richter1b549502011-12-07 10:02:53 +0100124 perf_session__delete(session);
Arnaldo Carvalho de Melof0bf9102012-12-05 16:24:05 -0300125out:
Robert Richter1b549502011-12-07 10:02:53 +0100126 return 0;
127}
128
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300129int cmd_buildid_list(int argc, const char **argv)
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200130{
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300131 bool show_kernel = false;
Blake Jonesa6bd98c2022-06-29 14:36:32 -0700132 bool show_kernel_maps = false;
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300133 bool with_hits = false;
134 bool force = false;
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300135 const struct option options[] = {
136 OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
137 OPT_STRING('i', "input", &input_name, "file", "input file name"),
138 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
139 OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
Blake Jonesa6bd98c2022-06-29 14:36:32 -0700140 OPT_BOOLEAN('m', "kernel-maps", &show_kernel_maps,
141 "Show build id of current kernel + modules"),
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300142 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
143 OPT_END()
144 };
145 const char * const buildid_list_usage[] = {
146 "perf buildid-list [<options>]",
147 NULL
148 };
149
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200150 argc = parse_options(argc, argv, options, buildid_list_usage, 0);
151 setup_pager();
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300152
Blake Jonesa6bd98c2022-06-29 14:36:32 -0700153 if (show_kernel) {
Michael Petlanc8319c92015-12-02 19:27:51 +0100154 return !(sysfs__fprintf_build_id(stdout) > 0);
Blake Jonesa6bd98c2022-06-29 14:36:32 -0700155 } else if (show_kernel_maps) {
156 buildid__show_kernel_maps();
157 return 0;
158 }
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300159
Feng Tang70cb4e92012-10-30 11:56:02 +0800160 return perf_session__list_build_ids(force, with_hits);
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200161}