xref: /linux/tools/perf/builtin-buildid-list.c (revision 6ef81c55a2b6584cb642917f5fdf3632ef44b670)
1c34984b2SArnaldo Carvalho de Melo /*
2c34984b2SArnaldo Carvalho de Melo  * builtin-buildid-list.c
3c34984b2SArnaldo Carvalho de Melo  *
47a6f205dSArnaldo Carvalho de Melo  * Builtin buildid-list command: list buildids in perf.data, in the running
57a6f205dSArnaldo Carvalho de Melo  * kernel and in ELF files.
6c34984b2SArnaldo Carvalho de Melo  *
7c34984b2SArnaldo Carvalho de Melo  * Copyright (C) 2009, Red Hat Inc.
8c34984b2SArnaldo Carvalho de Melo  * Copyright (C) 2009, Arnaldo Carvalho de Melo <acme@redhat.com>
9c34984b2SArnaldo Carvalho de Melo  */
10c34984b2SArnaldo Carvalho de Melo #include "builtin.h"
11c34984b2SArnaldo Carvalho de Melo #include "perf.h"
127b2567c1SArnaldo Carvalho de Melo #include "util/build-id.h"
13c34984b2SArnaldo Carvalho de Melo #include "util/debug.h"
144a3cec84SArnaldo Carvalho de Melo #include "util/dso.h"
15fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
164b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1794c744b6SArnaldo Carvalho de Melo #include "util/session.h"
18c34984b2SArnaldo Carvalho de Melo #include "util/symbol.h"
19f5fc1412SJiri Olsa #include "util/data.h"
20a43783aeSArnaldo Carvalho de Melo #include <errno.h>
21*6ef81c55SMamatha Inamdar #include <linux/err.h>
22c34984b2SArnaldo Carvalho de Melo 
23f2add9cdSArnaldo Carvalho de Melo static int sysfs__fprintf_build_id(FILE *fp)
24f2add9cdSArnaldo Carvalho de Melo {
25d77fac7fSMasami Hiramatsu 	char sbuild_id[SBUILD_ID_SIZE];
260b5a7935SMasami Hiramatsu 	int ret;
27f2add9cdSArnaldo Carvalho de Melo 
280b5a7935SMasami Hiramatsu 	ret = sysfs__sprintf_build_id("/", sbuild_id);
290b5a7935SMasami Hiramatsu 	if (ret != sizeof(sbuild_id))
300b5a7935SMasami Hiramatsu 		return ret < 0 ? ret : -EINVAL;
31f2add9cdSArnaldo Carvalho de Melo 
320b5a7935SMasami Hiramatsu 	return fprintf(fp, "%s\n", sbuild_id);
33f2add9cdSArnaldo Carvalho de Melo }
34f2add9cdSArnaldo Carvalho de Melo 
357a6f205dSArnaldo Carvalho de Melo static int filename__fprintf_build_id(const char *name, FILE *fp)
367a6f205dSArnaldo Carvalho de Melo {
37d77fac7fSMasami Hiramatsu 	char sbuild_id[SBUILD_ID_SIZE];
380b5a7935SMasami Hiramatsu 	int ret;
397a6f205dSArnaldo Carvalho de Melo 
400b5a7935SMasami Hiramatsu 	ret = filename__sprintf_build_id(name, sbuild_id);
410b5a7935SMasami Hiramatsu 	if (ret != sizeof(sbuild_id))
420b5a7935SMasami Hiramatsu 		return ret < 0 ? ret : -EINVAL;
437a6f205dSArnaldo Carvalho de Melo 
447a6f205dSArnaldo Carvalho de Melo 	return fprintf(fp, "%s\n", sbuild_id);
457a6f205dSArnaldo Carvalho de Melo }
467a6f205dSArnaldo Carvalho de Melo 
47417c2ff6SArnaldo Carvalho de Melo static bool dso__skip_buildid(struct dso *dso, int with_hits)
48417c2ff6SArnaldo Carvalho de Melo {
49417c2ff6SArnaldo Carvalho de Melo 	return with_hits && !dso->hit;
50417c2ff6SArnaldo Carvalho de Melo }
51417c2ff6SArnaldo Carvalho de Melo 
5270cb4e96SFeng Tang static int perf_session__list_build_ids(bool force, bool with_hits)
531b549504SRobert Richter {
541b549504SRobert Richter 	struct perf_session *session;
558ceb41d7SJiri Olsa 	struct perf_data data = {
56f5fc1412SJiri Olsa 		.path  = input_name,
57f5fc1412SJiri Olsa 		.mode  = PERF_DATA_MODE_READ,
58f5fc1412SJiri Olsa 		.force = force,
59f5fc1412SJiri Olsa 	};
601b549504SRobert Richter 
61166ccc9cSNamhyung Kim 	symbol__elf_init();
62f0bf9107SArnaldo Carvalho de Melo 	/*
63f0bf9107SArnaldo Carvalho de Melo 	 * See if this is an ELF file first:
64f0bf9107SArnaldo Carvalho de Melo 	 */
650b5a7935SMasami Hiramatsu 	if (filename__fprintf_build_id(input_name, stdout) > 0)
66f0bf9107SArnaldo Carvalho de Melo 		goto out;
67efad1415SRobert Richter 
688ceb41d7SJiri Olsa 	session = perf_session__new(&data, false, &build_id__mark_dso_hit_ops);
69*6ef81c55SMamatha Inamdar 	if (IS_ERR(session))
70*6ef81c55SMamatha Inamdar 		return PTR_ERR(session);
71cd10b289SAdrian Hunter 
72cd10b289SAdrian Hunter 	/*
73cd10b289SAdrian Hunter 	 * We take all buildids when the file contains AUX area tracing data
74cd10b289SAdrian Hunter 	 * because we do not decode the trace because it would take too long.
75cd10b289SAdrian Hunter 	 */
768ceb41d7SJiri Olsa 	if (!perf_data__is_pipe(&data) &&
77cd10b289SAdrian Hunter 	    perf_header__has_feat(&session->header, HEADER_AUXTRACE))
78cd10b289SAdrian Hunter 		with_hits = false;
79cd10b289SAdrian Hunter 
80299c3452SStephane Eranian 	/*
81299c3452SStephane Eranian 	 * in pipe-mode, the only way to get the buildids is to parse
82299c3452SStephane Eranian 	 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
83299c3452SStephane Eranian 	 */
848ceb41d7SJiri Olsa 	if (with_hits || perf_data__is_pipe(&data))
85b7b61cbeSArnaldo Carvalho de Melo 		perf_session__process_events(session);
861b549504SRobert Richter 
87417c2ff6SArnaldo Carvalho de Melo 	perf_session__fprintf_dsos_buildid(session, stdout, dso__skip_buildid, with_hits);
881b549504SRobert Richter 	perf_session__delete(session);
89f0bf9107SArnaldo Carvalho de Melo out:
901b549504SRobert Richter 	return 0;
911b549504SRobert Richter }
921b549504SRobert Richter 
93b0ad8ea6SArnaldo Carvalho de Melo int cmd_buildid_list(int argc, const char **argv)
94c34984b2SArnaldo Carvalho de Melo {
956ee41497SArnaldo Carvalho de Melo 	bool show_kernel = false;
966ee41497SArnaldo Carvalho de Melo 	bool with_hits = false;
976ee41497SArnaldo Carvalho de Melo 	bool force = false;
986ee41497SArnaldo Carvalho de Melo 	const struct option options[] = {
996ee41497SArnaldo Carvalho de Melo 	OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
1006ee41497SArnaldo Carvalho de Melo 	OPT_STRING('i', "input", &input_name, "file", "input file name"),
1016ee41497SArnaldo Carvalho de Melo 	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
1026ee41497SArnaldo Carvalho de Melo 	OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
1036ee41497SArnaldo Carvalho de Melo 	OPT_INCR('v', "verbose", &verbose, "be more verbose"),
1046ee41497SArnaldo Carvalho de Melo 	OPT_END()
1056ee41497SArnaldo Carvalho de Melo 	};
1066ee41497SArnaldo Carvalho de Melo 	const char * const buildid_list_usage[] = {
1076ee41497SArnaldo Carvalho de Melo 		"perf buildid-list [<options>]",
1086ee41497SArnaldo Carvalho de Melo 		NULL
1096ee41497SArnaldo Carvalho de Melo 	};
1106ee41497SArnaldo Carvalho de Melo 
111c34984b2SArnaldo Carvalho de Melo 	argc = parse_options(argc, argv, options, buildid_list_usage, 0);
112c34984b2SArnaldo Carvalho de Melo 	setup_pager();
1136ee41497SArnaldo Carvalho de Melo 
1146ee41497SArnaldo Carvalho de Melo 	if (show_kernel)
115c8319c9dSMichael Petlan 		return !(sysfs__fprintf_build_id(stdout) > 0);
1166ee41497SArnaldo Carvalho de Melo 
11770cb4e96SFeng Tang 	return perf_session__list_build_ids(force, with_hits);
118c34984b2SArnaldo Carvalho de Melo }
119