blob: cfede86161d8ae9410c123745bb60def09785331 [file] [log] [blame]
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -02001/*
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 */
Adrian Hunterfc1b6912013-10-14 16:57:29 +03009#include <sys/types.h>
10#include <sys/time.h>
11#include <time.h>
12#include <dirent.h>
13#include <unistd.h>
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020014#include "builtin.h"
15#include "perf.h"
16#include "util/cache.h"
17#include "util/debug.h"
18#include "util/header.h"
19#include "util/parse-options.h"
20#include "util/strlist.h"
Jiri Olsaebb296c2012-10-27 23:18:28 +020021#include "util/build-id.h"
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -030022#include "util/session.h"
Jiri Olsa4383db82012-10-27 23:18:29 +020023#include "util/symbol.h"
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020024
Adrian Hunterfc1b6912013-10-14 16:57:29 +030025static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
26{
27 char root_dir[PATH_MAX];
28 char notes[PATH_MAX];
29 u8 build_id[BUILD_ID_SIZE];
30 char *p;
31
32 strlcpy(root_dir, proc_dir, sizeof(root_dir));
33
34 p = strrchr(root_dir, '/');
35 if (!p)
36 return -1;
37 *p = '\0';
38
39 scnprintf(notes, sizeof(notes), "%s/sys/kernel/notes", root_dir);
40
41 if (sysfs__read_build_id(notes, build_id, sizeof(build_id)))
42 return -1;
43
44 build_id__sprintf(build_id, sizeof(build_id), sbuildid);
45
46 return 0;
47}
48
49static int build_id_cache__kcore_dir(char *dir, size_t sz)
50{
51 struct timeval tv;
52 struct tm tm;
53 char dt[32];
54
55 if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
56 return -1;
57
58 if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
59 return -1;
60
61 scnprintf(dir, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
62
63 return 0;
64}
65
66static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
67 size_t to_dir_sz)
68{
69 char from[PATH_MAX];
70 char to[PATH_MAX];
71 struct dirent *dent;
72 int ret = -1;
73 DIR *d;
74
75 d = opendir(to_dir);
76 if (!d)
77 return -1;
78
79 scnprintf(from, sizeof(from), "%s/modules", from_dir);
80
81 while (1) {
82 dent = readdir(d);
83 if (!dent)
84 break;
85 if (dent->d_type != DT_DIR)
86 continue;
87 scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
88 dent->d_name);
89 if (!compare_proc_modules(from, to)) {
90 scnprintf(to, sizeof(to), "%s/%s", to_dir,
91 dent->d_name);
92 strlcpy(to_dir, to, to_dir_sz);
93 ret = 0;
94 break;
95 }
96 }
97
98 closedir(d);
99
100 return ret;
101}
102
103static int build_id_cache__add_kcore(const char *filename, const char *debugdir)
104{
105 char dir[32], sbuildid[BUILD_ID_SIZE * 2 + 1];
106 char from_dir[PATH_MAX], to_dir[PATH_MAX];
107 char *p;
108
109 strlcpy(from_dir, filename, sizeof(from_dir));
110
111 p = strrchr(from_dir, '/');
112 if (!p || strcmp(p + 1, "kcore"))
113 return -1;
114 *p = '\0';
115
116 if (build_id_cache__kcore_buildid(from_dir, sbuildid))
117 return -1;
118
119 scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s",
120 debugdir, sbuildid);
121
122 if (!build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
123 pr_debug("same kcore found in %s\n", to_dir);
124 return 0;
125 }
126
127 if (build_id_cache__kcore_dir(dir, sizeof(dir)))
128 return -1;
129
130 scnprintf(to_dir, sizeof(to_dir), "%s/[kernel.kcore]/%s/%s",
131 debugdir, sbuildid, dir);
132
133 if (mkdir_p(to_dir, 0755))
134 return -1;
135
136 if (kcore_copy(from_dir, to_dir)) {
137 /* Remove YYYYmmddHHMMSShh directory */
138 if (!rmdir(to_dir)) {
139 p = strrchr(to_dir, '/');
140 if (p)
141 *p = '\0';
142 /* Try to remove buildid directory */
143 if (!rmdir(to_dir)) {
144 p = strrchr(to_dir, '/');
145 if (p)
146 *p = '\0';
147 /* Try to remove [kernel.kcore] directory */
148 rmdir(to_dir);
149 }
150 }
151 return -1;
152 }
153
154 pr_debug("kcore added to build-id cache directory %s\n", to_dir);
155
156 return 0;
157}
158
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200159static int build_id_cache__add_file(const char *filename, const char *debugdir)
160{
161 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
162 u8 build_id[BUILD_ID_SIZE];
163 int err;
164
165 if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
166 pr_debug("Couldn't read a build-id in %s\n", filename);
167 return -1;
168 }
169
170 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +0200171 err = build_id_cache__add_s(sbuild_id, debugdir, filename,
172 false, false);
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200173 if (verbose)
174 pr_info("Adding %s %s: %s\n", sbuild_id, filename,
175 err ? "FAIL" : "Ok");
176 return err;
177}
178
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300179static int build_id_cache__remove_file(const char *filename,
180 const char *debugdir)
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200181{
182 u8 build_id[BUILD_ID_SIZE];
183 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
184
185 int err;
186
187 if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
188 pr_debug("Couldn't read a build-id in %s\n", filename);
189 return -1;
190 }
191
192 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
193 err = build_id_cache__remove_s(sbuild_id, debugdir);
194 if (verbose)
195 pr_info("Removing %s %s: %s\n", sbuild_id, filename,
196 err ? "FAIL" : "Ok");
197
198 return err;
199}
200
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300201static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
202{
203 char filename[PATH_MAX];
204 u8 build_id[BUILD_ID_SIZE];
205
206 if (dso__build_id_filename(dso, filename, sizeof(filename)) &&
207 filename__read_build_id(filename, build_id,
208 sizeof(build_id)) != sizeof(build_id)) {
209 if (errno == ENOENT)
210 return false;
211
212 pr_warning("Problems with %s file, consider removing it from the cache\n",
213 filename);
214 } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
215 pr_warning("Problems with %s file, consider removing it from the cache\n",
216 filename);
217 }
218
219 return true;
220}
221
222static int build_id_cache__fprintf_missing(const char *filename, bool force, FILE *fp)
223{
Jiri Olsaf5fc1412013-10-15 16:27:32 +0200224 struct perf_data_file file = {
225 .path = filename,
226 .mode = PERF_DATA_MODE_READ,
227 .force = force,
228 };
229 struct perf_session *session = perf_session__new(&file, false, NULL);
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300230 if (session == NULL)
231 return -1;
232
233 perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
234 perf_session__delete(session);
235
236 return 0;
237}
238
Namhyung Kimeeb49842013-02-07 18:02:11 +0900239static int build_id_cache__update_file(const char *filename,
240 const char *debugdir)
241{
242 u8 build_id[BUILD_ID_SIZE];
243 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
244
245 int err;
246
247 if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
248 pr_debug("Couldn't read a build-id in %s\n", filename);
249 return -1;
250 }
251
252 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
253 err = build_id_cache__remove_s(sbuild_id, debugdir);
254 if (!err) {
255 err = build_id_cache__add_s(sbuild_id, debugdir, filename,
256 false, false);
257 }
258 if (verbose)
259 pr_info("Updating %s %s: %s\n", sbuild_id, filename,
260 err ? "FAIL" : "Ok");
261
262 return err;
263}
264
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300265int cmd_buildid_cache(int argc, const char **argv,
266 const char *prefix __maybe_unused)
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200267{
268 struct strlist *list;
269 struct str_node *pos;
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300270 int ret = 0;
271 bool force = false;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200272 char debugdir[PATH_MAX];
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300273 char const *add_name_list_str = NULL,
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300274 *remove_name_list_str = NULL,
Namhyung Kimeeb49842013-02-07 18:02:11 +0900275 *missing_filename = NULL,
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300276 *update_name_list_str = NULL,
277 *kcore_filename;
Namhyung Kimeeb49842013-02-07 18:02:11 +0900278
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300279 const struct option buildid_cache_options[] = {
280 OPT_STRING('a', "add", &add_name_list_str,
281 "file list", "file(s) to add"),
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300282 OPT_STRING('k', "kcore", &kcore_filename,
283 "file", "kcore file to add"),
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300284 OPT_STRING('r', "remove", &remove_name_list_str, "file list",
285 "file(s) to remove"),
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300286 OPT_STRING('M', "missing", &missing_filename, "file",
287 "to find missing build ids in the cache"),
288 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
Namhyung Kimeeb49842013-02-07 18:02:11 +0900289 OPT_STRING('u', "update", &update_name_list_str, "file list",
290 "file(s) to update"),
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300291 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
292 OPT_END()
293 };
294 const char * const buildid_cache_usage[] = {
295 "perf buildid-cache [<options>]",
296 NULL
297 };
298
299 argc = parse_options(argc, argv, buildid_cache_options,
300 buildid_cache_usage, 0);
301
302 if (symbol__init() < 0)
303 return -1;
304
305 setup_pager();
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200306
Stephane Eranian45de34b2010-06-01 21:25:01 +0200307 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200308
309 if (add_name_list_str) {
310 list = strlist__new(true, add_name_list_str);
311 if (list) {
312 strlist__for_each(pos, list)
313 if (build_id_cache__add_file(pos->s, debugdir)) {
314 if (errno == EEXIST) {
315 pr_debug("%s already in the cache\n",
316 pos->s);
317 continue;
318 }
319 pr_warning("Couldn't add %s: %s\n",
320 pos->s, strerror(errno));
321 }
322
323 strlist__delete(list);
324 }
325 }
326
327 if (remove_name_list_str) {
328 list = strlist__new(true, remove_name_list_str);
329 if (list) {
330 strlist__for_each(pos, list)
331 if (build_id_cache__remove_file(pos->s, debugdir)) {
332 if (errno == ENOENT) {
333 pr_debug("%s wasn't in the cache\n",
334 pos->s);
335 continue;
336 }
337 pr_warning("Couldn't remove %s: %s\n",
338 pos->s, strerror(errno));
339 }
340
341 strlist__delete(list);
342 }
343 }
344
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300345 if (missing_filename)
346 ret = build_id_cache__fprintf_missing(missing_filename, force, stdout);
347
Namhyung Kimeeb49842013-02-07 18:02:11 +0900348 if (update_name_list_str) {
349 list = strlist__new(true, update_name_list_str);
350 if (list) {
351 strlist__for_each(pos, list)
352 if (build_id_cache__update_file(pos->s, debugdir)) {
353 if (errno == ENOENT) {
354 pr_debug("%s wasn't in the cache\n",
355 pos->s);
356 continue;
357 }
358 pr_warning("Couldn't update %s: %s\n",
359 pos->s, strerror(errno));
360 }
361
362 strlist__delete(list);
363 }
364 }
365
Adrian Hunterfc1b6912013-10-14 16:57:29 +0300366 if (kcore_filename &&
367 build_id_cache__add_kcore(kcore_filename, debugdir))
368 pr_warning("Couldn't add %s\n", kcore_filename);
369
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300370 return ret;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200371}