perf tools: Remove unused PYRF_OBJS variable on Makefile
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / tools / perf / builtin-buildid-list.c
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 "perf.h"
12 #include "util/build-id.h"
13 #include "util/cache.h"
14 #include "util/debug.h"
15 #include "util/parse-options.h"
16 #include "util/session.h"
17 #include "util/symbol.h"
18
19 static const char *input_name;
20 static bool force;
21 static bool show_kernel;
22 static bool with_hits;
23
24 static const char * const buildid_list_usage[] = {
25 "perf buildid-list [<options>]",
26 NULL
27 };
28
29 static const struct option options[] = {
30 OPT_BOOLEAN('H', "with-hits", &with_hits, "Show only DSOs with hits"),
31 OPT_STRING('i', "input", &input_name, "file",
32 "input file name"),
33 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
34 OPT_BOOLEAN('k', "kernel", &show_kernel, "Show current kernel build id"),
35 OPT_INCR('v', "verbose", &verbose,
36 "be more verbose"),
37 OPT_END()
38 };
39
40 static int sysfs__fprintf_build_id(FILE *fp)
41 {
42 u8 kallsyms_build_id[BUILD_ID_SIZE];
43 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
44
45 if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
46 sizeof(kallsyms_build_id)) != 0)
47 return -1;
48
49 build_id__sprintf(kallsyms_build_id, sizeof(kallsyms_build_id),
50 sbuild_id);
51 fprintf(fp, "%s\n", sbuild_id);
52 return 0;
53 }
54
55 static int filename__fprintf_build_id(const char *name, FILE *fp)
56 {
57 u8 build_id[BUILD_ID_SIZE];
58 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
59
60 if (filename__read_build_id(name, build_id,
61 sizeof(build_id)) != sizeof(build_id))
62 return 0;
63
64 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
65 return fprintf(fp, "%s\n", sbuild_id);
66 }
67
68 static int perf_session__list_build_ids(void)
69 {
70 struct perf_session *session;
71
72 symbol__elf_init();
73
74 session = perf_session__new(input_name, O_RDONLY, force, false,
75 &build_id__mark_dso_hit_ops);
76 if (session == NULL)
77 return -1;
78
79 /*
80 * See if this is an ELF file first:
81 */
82 if (filename__fprintf_build_id(session->filename, stdout))
83 goto out;
84
85 /*
86 * in pipe-mode, the only way to get the buildids is to parse
87 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
88 */
89 if (with_hits || session->fd_pipe)
90 perf_session__process_events(session, &build_id__mark_dso_hit_ops);
91
92 perf_session__fprintf_dsos_buildid(session, stdout, with_hits);
93 out:
94 perf_session__delete(session);
95 return 0;
96 }
97
98 static int __cmd_buildid_list(void)
99 {
100 if (show_kernel)
101 return sysfs__fprintf_build_id(stdout);
102
103 return perf_session__list_build_ids();
104 }
105
106 int cmd_buildid_list(int argc, const char **argv,
107 const char *prefix __maybe_unused)
108 {
109 argc = parse_options(argc, argv, options, buildid_list_usage, 0);
110 setup_pager();
111 return __cmd_buildid_list();
112 }