perf buildid-list: Add option to show the running kernel build id
[pandora-kernel.git] / tools / perf / builtin-buildid-list.c
1 /*
2  * builtin-buildid-list.c
3  *
4  * Builtin buildid-list command: list buildids in perf.data or in the running
5  * kernel.
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 char const *input_name = "perf.data";
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 perf_session__list_build_ids(void)
41 {
42         struct perf_session *session;
43
44         session = perf_session__new(input_name, O_RDONLY, force, false,
45                                     &build_id__mark_dso_hit_ops);
46         if (session == NULL)
47                 return -1;
48
49         if (with_hits)
50                 perf_session__process_events(session, &build_id__mark_dso_hit_ops);
51
52         perf_session__fprintf_dsos_buildid(session, stdout, with_hits);
53
54         perf_session__delete(session);
55         return 0;
56 }
57
58 static int sysfs__fprintf_build_id(FILE *fp)
59 {
60         u8 kallsyms_build_id[BUILD_ID_SIZE];
61         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
62
63         if (sysfs__read_build_id("/sys/kernel/notes", kallsyms_build_id,
64                                  sizeof(kallsyms_build_id)) != 0)
65                 return -1;
66
67         build_id__sprintf(kallsyms_build_id, sizeof(kallsyms_build_id),
68                           sbuild_id);
69         fprintf(fp, "%s\n", sbuild_id);
70         return 0;
71 }
72
73 static int __cmd_buildid_list(void)
74 {
75
76         if (show_kernel)
77                 return sysfs__fprintf_build_id(stdout);
78
79         return perf_session__list_build_ids();
80 }
81
82 int cmd_buildid_list(int argc, const char **argv, const char *prefix __used)
83 {
84         argc = parse_options(argc, argv, options, buildid_list_usage, 0);
85         setup_pager();
86         return __cmd_buildid_list();
87 }