blob: 78abbe8d9d5ff0a8c2c396619d1566f2c00def44 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -02002/*
3 * builtin-buildid-list.c
4 *
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -03005 * Builtin buildid-list command: list buildids in perf.data, in the running
6 * kernel and in ELF files.
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -02007 *
8 * Copyright (C) 2009, Red Hat Inc.
9 * Copyright (C) 2009, Arnaldo Carvalho de Melo <acme@redhat.com>
10 */
11#include "builtin.h"
12#include "perf.h"
Arnaldo Carvalho de Melo7b2567c2010-02-03 16:52:04 -020013#include "util/build-id.h"
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -020014#include "util/cache.h"
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -020015#include "util/debug.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"
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030020#include <errno.h>
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -020021
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030022static int sysfs__fprintf_build_id(FILE *fp)
23{
Masami Hiramatsud77fac72015-07-15 18:14:28 +090024 char sbuild_id[SBUILD_ID_SIZE];
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090025 int ret;
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030026
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090027 ret = sysfs__sprintf_build_id("/", sbuild_id);
28 if (ret != sizeof(sbuild_id))
29 return ret < 0 ? ret : -EINVAL;
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030030
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090031 return fprintf(fp, "%s\n", sbuild_id);
Arnaldo Carvalho de Melof2add9c2011-08-29 08:07:22 -030032}
33
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030034static int filename__fprintf_build_id(const char *name, FILE *fp)
35{
Masami Hiramatsud77fac72015-07-15 18:14:28 +090036 char sbuild_id[SBUILD_ID_SIZE];
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090037 int ret;
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030038
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090039 ret = filename__sprintf_build_id(name, sbuild_id);
40 if (ret != sizeof(sbuild_id))
41 return ret < 0 ? ret : -EINVAL;
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030042
Arnaldo Carvalho de Melo7a6f2052011-08-29 08:33:17 -030043 return fprintf(fp, "%s\n", sbuild_id);
44}
45
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -030046static bool dso__skip_buildid(struct dso *dso, int with_hits)
47{
48 return with_hits && !dso->hit;
49}
50
Feng Tang70cb4e92012-10-30 11:56:02 +080051static int perf_session__list_build_ids(bool force, bool with_hits)
Robert Richter1b549502011-12-07 10:02:53 +010052{
53 struct perf_session *session;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010054 struct perf_data data = {
Jiri Olsaeae8ad82017-01-23 22:25:41 +010055 .file = {
56 .path = input_name,
57 },
58 .mode = PERF_DATA_MODE_READ,
59 .force = force,
Jiri Olsaf5fc14122013-10-15 16:27:32 +020060 };
Robert Richter1b549502011-12-07 10:02:53 +010061
Namhyung Kim166ccc92012-08-06 13:41:19 +090062 symbol__elf_init();
Arnaldo Carvalho de Melof0bf9102012-12-05 16:24:05 -030063 /*
64 * See if this is an ELF file first:
65 */
Masami Hiramatsu0b5a7932015-08-15 20:42:59 +090066 if (filename__fprintf_build_id(input_name, stdout) > 0)
Arnaldo Carvalho de Melof0bf9102012-12-05 16:24:05 -030067 goto out;
Robert Richterefad1412011-12-07 10:02:54 +010068
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010069 session = perf_session__new(&data, false, &build_id__mark_dso_hit_ops);
Robert Richter1b549502011-12-07 10:02:53 +010070 if (session == NULL)
71 return -1;
Adrian Huntercd10b282015-04-30 17:37:26 +030072
73 /*
74 * We take all buildids when the file contains AUX area tracing data
75 * because we do not decode the trace because it would take too long.
76 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010077 if (!perf_data__is_pipe(&data) &&
Adrian Huntercd10b282015-04-30 17:37:26 +030078 perf_header__has_feat(&session->header, HEADER_AUXTRACE))
79 with_hits = false;
80
Stephane Eranian299c3452012-05-15 13:28:15 +020081 /*
82 * in pipe-mode, the only way to get the buildids is to parse
83 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
84 */
Jiri Olsa8ceb41d2017-01-23 22:07:59 +010085 if (with_hits || perf_data__is_pipe(&data))
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -030086 perf_session__process_events(session);
Robert Richter1b549502011-12-07 10:02:53 +010087
Arnaldo Carvalho de Melo417c2ff2012-12-07 09:53:58 -030088 perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
Robert Richter1b549502011-12-07 10:02:53 +010089 perf_session__delete(session);
Arnaldo Carvalho de Melof0bf9102012-12-05 16:24:05 -030090out:
Robert Richter1b549502011-12-07 10:02:53 +010091 return 0;
92}
93
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -030094int cmd_buildid_list(int argc, const char **argv)
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -020095{
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -030096 bool show_kernel = false;
97 bool with_hits = false;
98 bool force = false;
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -030099 const struct option options[] = {
100 OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
101 OPT_STRING('i', "input", &input_name, "file", "input file name"),
102 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
103 OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
104 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
105 OPT_END()
106 };
107 const char * const buildid_list_usage[] = {
108 "perf buildid-list [<options>]",
109 NULL
110 };
111
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200112 argc = parse_options(argc, argv, options, buildid_list_usage, 0);
113 setup_pager();
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300114
115 if (show_kernel)
Michael Petlanc8319c92015-12-02 19:27:51 +0100116 return !(sysfs__fprintf_build_id(stdout) > 0);
Arnaldo Carvalho de Melo6ee41492012-10-01 15:20:58 -0300117
Feng Tang70cb4e92012-10-30 11:56:02 +0800118 return perf_session__list_build_ids(force, with_hits);
Arnaldo Carvalho de Meloc34984b2009-11-16 16:32:45 -0200119}