Merge tag 'perf-core-for-mingo' of git://git.kernel.org/pub/scm/linux/kernel/git...
[~shefty/rdma-dev.git] / tools / perf / builtin-buildid-cache.c
1 /*
2  * builtin-buildid-cache.c
3  *
4  * Builtin buildid-cache command: Manages build-id cache
5  *
6  * Copyright (C) 2010, Red Hat Inc.
7  * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
8  */
9 #include "builtin.h"
10 #include "perf.h"
11 #include "util/cache.h"
12 #include "util/debug.h"
13 #include "util/header.h"
14 #include "util/parse-options.h"
15 #include "util/strlist.h"
16 #include "util/build-id.h"
17 #include "util/session.h"
18 #include "util/symbol.h"
19
20 static int build_id_cache__add_file(const char *filename, const char *debugdir)
21 {
22         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
23         u8 build_id[BUILD_ID_SIZE];
24         int err;
25
26         if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
27                 pr_debug("Couldn't read a build-id in %s\n", filename);
28                 return -1;
29         }
30
31         build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
32         err = build_id_cache__add_s(sbuild_id, debugdir, filename,
33                                     false, false);
34         if (verbose)
35                 pr_info("Adding %s %s: %s\n", sbuild_id, filename,
36                         err ? "FAIL" : "Ok");
37         return err;
38 }
39
40 static int build_id_cache__remove_file(const char *filename,
41                                        const char *debugdir)
42 {
43         u8 build_id[BUILD_ID_SIZE];
44         char sbuild_id[BUILD_ID_SIZE * 2 + 1];
45
46         int err;
47
48         if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
49                 pr_debug("Couldn't read a build-id in %s\n", filename);
50                 return -1;
51         }
52
53         build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
54         err = build_id_cache__remove_s(sbuild_id, debugdir);
55         if (verbose)
56                 pr_info("Removing %s %s: %s\n", sbuild_id, filename,
57                         err ? "FAIL" : "Ok");
58
59         return err;
60 }
61
62 static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
63 {
64         char filename[PATH_MAX];
65         u8 build_id[BUILD_ID_SIZE];
66
67         if (dso__build_id_filename(dso, filename, sizeof(filename)) &&
68             filename__read_build_id(filename, build_id,
69                                     sizeof(build_id)) != sizeof(build_id)) {
70                 if (errno == ENOENT)
71                         return false;
72
73                 pr_warning("Problems with %s file, consider removing it from the cache\n", 
74                            filename);
75         } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
76                 pr_warning("Problems with %s file, consider removing it from the cache\n", 
77                            filename);
78         }
79
80         return true;
81 }
82
83 static int build_id_cache__fprintf_missing(const char *filename, bool force, FILE *fp)
84 {
85         struct perf_session *session = perf_session__new(filename, O_RDONLY,
86                                                          force, false, NULL);
87         if (session == NULL)
88                 return -1;
89
90         perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
91         perf_session__delete(session);
92
93         return 0;
94 }
95
96 int cmd_buildid_cache(int argc, const char **argv,
97                       const char *prefix __maybe_unused)
98 {
99         struct strlist *list;
100         struct str_node *pos;
101         int ret = 0;
102         bool force = false;
103         char debugdir[PATH_MAX];
104         char const *add_name_list_str = NULL,
105                    *remove_name_list_str = NULL,
106                    *missing_filename = NULL;
107         const struct option buildid_cache_options[] = {
108         OPT_STRING('a', "add", &add_name_list_str,
109                    "file list", "file(s) to add"),
110         OPT_STRING('r', "remove", &remove_name_list_str, "file list",
111                     "file(s) to remove"),
112         OPT_STRING('M', "missing", &missing_filename, "file",
113                    "to find missing build ids in the cache"),
114         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
115         OPT_INCR('v', "verbose", &verbose, "be more verbose"),
116         OPT_END()
117         };
118         const char * const buildid_cache_usage[] = {
119                 "perf buildid-cache [<options>]",
120                 NULL
121         };
122
123         argc = parse_options(argc, argv, buildid_cache_options,
124                              buildid_cache_usage, 0);
125
126         if (symbol__init() < 0)
127                 return -1;
128
129         setup_pager();
130
131         snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
132
133         if (add_name_list_str) {
134                 list = strlist__new(true, add_name_list_str);
135                 if (list) {
136                         strlist__for_each(pos, list)
137                                 if (build_id_cache__add_file(pos->s, debugdir)) {
138                                         if (errno == EEXIST) {
139                                                 pr_debug("%s already in the cache\n",
140                                                          pos->s);
141                                                 continue;
142                                         }
143                                         pr_warning("Couldn't add %s: %s\n",
144                                                    pos->s, strerror(errno));
145                                 }
146
147                         strlist__delete(list);
148                 }
149         }
150
151         if (remove_name_list_str) {
152                 list = strlist__new(true, remove_name_list_str);
153                 if (list) {
154                         strlist__for_each(pos, list)
155                                 if (build_id_cache__remove_file(pos->s, debugdir)) {
156                                         if (errno == ENOENT) {
157                                                 pr_debug("%s wasn't in the cache\n",
158                                                          pos->s);
159                                                 continue;
160                                         }
161                                         pr_warning("Couldn't remove %s: %s\n",
162                                                    pos->s, strerror(errno));
163                                 }
164
165                         strlist__delete(list);
166                 }
167         }
168
169         if (missing_filename)
170                 ret = build_id_cache__fprintf_missing(missing_filename, force, stdout);
171
172         return ret;
173 }