]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - tools/perf/builtin-buildid-cache.c
perf tools: Remove needless evlist.h include directives
[mirror_ubuntu-hirsute-kernel.git] / tools / perf / builtin-buildid-cache.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
ef12a141
ACM
2/*
3 * builtin-buildid-cache.c
4 *
5 * Builtin buildid-cache command: Manages build-id cache
6 *
7 * Copyright (C) 2010, Red Hat Inc.
8 * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
9 */
fc1b691d
AH
10#include <sys/types.h>
11#include <sys/time.h>
12#include <time.h>
13#include <dirent.h>
a43783ae 14#include <errno.h>
fc1b691d 15#include <unistd.h>
ef12a141 16#include "builtin.h"
f045b8c4 17#include "namespaces.h"
ef12a141
ACM
18#include "util/cache.h"
19#include "util/debug.h"
20#include "util/header.h"
4b6ab94e 21#include <subcmd/parse-options.h>
ef12a141 22#include "util/strlist.h"
ebb296c2 23#include "util/build-id.h"
fbb6976c 24#include "util/session.h"
4a3cec84 25#include "util/dso.h"
4383db88 26#include "util/symbol.h"
c5e4027e 27#include "util/time-utils.h"
efa73d37 28#include "util/util.h"
8e1e0d74 29#include "util/probe-file.h"
ef12a141 30
fc1b691d
AH
31static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
32{
33 char root_dir[PATH_MAX];
fc1b691d
AH
34 char *p;
35
36 strlcpy(root_dir, proc_dir, sizeof(root_dir));
37
38 p = strrchr(root_dir, '/');
39 if (!p)
40 return -1;
41 *p = '\0';
0b5a7935 42 return sysfs__sprintf_build_id(root_dir, sbuildid);
fc1b691d
AH
43}
44
45static int build_id_cache__kcore_dir(char *dir, size_t sz)
46{
37b20151 47 return fetch_current_timestamp(dir, sz);
fc1b691d
AH
48}
49
d3b70220
AH
50static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
51{
52 char from[PATH_MAX];
53 char to[PATH_MAX];
54 const char *name;
55 u64 addr1 = 0, addr2 = 0;
b843f62a 56 int i, err = -1;
d3b70220
AH
57
58 scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
59 scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
60
61 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
b843f62a
ACM
62 err = kallsyms__get_function_start(from, name, &addr1);
63 if (!err)
d3b70220
AH
64 break;
65 }
66
b843f62a
ACM
67 if (err)
68 return false;
69
70 if (kallsyms__get_function_start(to, name, &addr2))
71 return false;
d3b70220
AH
72
73 return addr1 == addr2;
74}
75
fc1b691d
AH
76static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
77 size_t to_dir_sz)
78{
79 char from[PATH_MAX];
80 char to[PATH_MAX];
d3b70220 81 char to_subdir[PATH_MAX];
fc1b691d
AH
82 struct dirent *dent;
83 int ret = -1;
84 DIR *d;
85
86 d = opendir(to_dir);
87 if (!d)
88 return -1;
89
90 scnprintf(from, sizeof(from), "%s/modules", from_dir);
91
92 while (1) {
93 dent = readdir(d);
94 if (!dent)
95 break;
96 if (dent->d_type != DT_DIR)
97 continue;
98 scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
99 dent->d_name);
d3b70220
AH
100 scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
101 to_dir, dent->d_name);
102 if (!compare_proc_modules(from, to) &&
103 same_kallsyms_reloc(from_dir, to_subdir)) {
104 strlcpy(to_dir, to_subdir, to_dir_sz);
fc1b691d
AH
105 ret = 0;
106 break;
107 }
108 }
109
110 closedir(d);
111
112 return ret;
113}
114
e35f7362 115static int build_id_cache__add_kcore(const char *filename, bool force)
fc1b691d 116{
d77fac7f 117 char dir[32], sbuildid[SBUILD_ID_SIZE];
fc1b691d
AH
118 char from_dir[PATH_MAX], to_dir[PATH_MAX];
119 char *p;
120
121 strlcpy(from_dir, filename, sizeof(from_dir));
122
123 p = strrchr(from_dir, '/');
124 if (!p || strcmp(p + 1, "kcore"))
125 return -1;
126 *p = '\0';
127
0b5a7935 128 if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
fc1b691d
AH
129 return -1;
130
0a77582f
MH
131 scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s",
132 buildid_dir, DSO__NAME_KCORE, sbuildid);
fc1b691d 133
5173fbb8
AH
134 if (!force &&
135 !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
fc1b691d
AH
136 pr_debug("same kcore found in %s\n", to_dir);
137 return 0;
138 }
139
140 if (build_id_cache__kcore_dir(dir, sizeof(dir)))
141 return -1;
142
0a77582f
MH
143 scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s/%s",
144 buildid_dir, DSO__NAME_KCORE, sbuildid, dir);
fc1b691d
AH
145
146 if (mkdir_p(to_dir, 0755))
147 return -1;
148
149 if (kcore_copy(from_dir, to_dir)) {
150 /* Remove YYYYmmddHHMMSShh directory */
151 if (!rmdir(to_dir)) {
152 p = strrchr(to_dir, '/');
153 if (p)
154 *p = '\0';
155 /* Try to remove buildid directory */
156 if (!rmdir(to_dir)) {
157 p = strrchr(to_dir, '/');
158 if (p)
159 *p = '\0';
160 /* Try to remove [kernel.kcore] directory */
161 rmdir(to_dir);
162 }
163 }
164 return -1;
165 }
166
167 pr_debug("kcore added to build-id cache directory %s\n", to_dir);
168
169 return 0;
170}
171
f045b8c4 172static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
ef12a141 173{
d77fac7f 174 char sbuild_id[SBUILD_ID_SIZE];
ef12a141
ACM
175 u8 build_id[BUILD_ID_SIZE];
176 int err;
f045b8c4 177 struct nscookie nsc;
ef12a141 178
f045b8c4
KJ
179 nsinfo__mountns_enter(nsi, &nsc);
180 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
181 nsinfo__mountns_exit(&nsc);
182 if (err < 0) {
ef12a141
ACM
183 pr_debug("Couldn't read a build-id in %s\n", filename);
184 return -1;
185 }
186
187 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
f045b8c4 188 err = build_id_cache__add_s(sbuild_id, filename, nsi,
7dbf4dcf 189 false, false);
cc169c7c
MH
190 pr_debug("Adding %s %s: %s\n", sbuild_id, filename,
191 err ? "FAIL" : "Ok");
ef12a141
ACM
192 return err;
193}
194
f045b8c4 195static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
ef12a141
ACM
196{
197 u8 build_id[BUILD_ID_SIZE];
d77fac7f 198 char sbuild_id[SBUILD_ID_SIZE];
f045b8c4 199 struct nscookie nsc;
ef12a141
ACM
200
201 int err;
202
f045b8c4
KJ
203 nsinfo__mountns_enter(nsi, &nsc);
204 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
205 nsinfo__mountns_exit(&nsc);
206 if (err < 0) {
ef12a141
ACM
207 pr_debug("Couldn't read a build-id in %s\n", filename);
208 return -1;
209 }
210
211 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
e35f7362 212 err = build_id_cache__remove_s(sbuild_id);
cc169c7c
MH
213 pr_debug("Removing %s %s: %s\n", sbuild_id, filename,
214 err ? "FAIL" : "Ok");
ef12a141
ACM
215
216 return err;
217}
218
f045b8c4 219static int build_id_cache__purge_path(const char *pathname, struct nsinfo *nsi)
8d8c8e4c
MH
220{
221 struct strlist *list;
222 struct str_node *pos;
223 int err;
224
f045b8c4 225 err = build_id_cache__list_build_ids(pathname, nsi, &list);
8d8c8e4c
MH
226 if (err)
227 goto out;
228
602a1f4d 229 strlist__for_each_entry(pos, list) {
8d8c8e4c 230 err = build_id_cache__remove_s(pos->s);
cc169c7c
MH
231 pr_debug("Removing %s %s: %s\n", pos->s, pathname,
232 err ? "FAIL" : "Ok");
8d8c8e4c
MH
233 if (err)
234 break;
235 }
236 strlist__delete(list);
237
238out:
cc169c7c 239 pr_debug("Purging %s: %s\n", pathname, err ? "FAIL" : "Ok");
8d8c8e4c
MH
240
241 return err;
242}
243
9a73c308
RB
244static int build_id_cache__purge_all(void)
245{
246 struct strlist *list;
247 struct str_node *pos;
248 int err = 0;
249 char *buf;
250
251 list = build_id_cache__list_all(false);
252 if (!list) {
253 pr_debug("Failed to get buildids: -%d\n", errno);
254 return -EINVAL;
255 }
256
257 strlist__for_each_entry(pos, list) {
258 buf = build_id_cache__origname(pos->s);
259 err = build_id_cache__remove_s(pos->s);
260 pr_debug("Removing %s (%s): %s\n", buf, pos->s,
261 err ? "FAIL" : "Ok");
262 free(buf);
263 if (err)
264 break;
265 }
266 strlist__delete(list);
267
268 pr_debug("Purged all: %s\n", err ? "FAIL" : "Ok");
269 return err;
270}
271
fbb6976c
ACM
272static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
273{
274 char filename[PATH_MAX];
275 u8 build_id[BUILD_ID_SIZE];
276
d2396999 277 if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
fbb6976c
ACM
278 filename__read_build_id(filename, build_id,
279 sizeof(build_id)) != sizeof(build_id)) {
280 if (errno == ENOENT)
281 return false;
282
48000a1a 283 pr_warning("Problems with %s file, consider removing it from the cache\n",
fbb6976c
ACM
284 filename);
285 } else if (memcmp(dso->build_id, build_id, sizeof(dso->build_id))) {
48000a1a 286 pr_warning("Problems with %s file, consider removing it from the cache\n",
fbb6976c
ACM
287 filename);
288 }
289
290 return true;
291}
292
e3ed75bb 293static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
fbb6976c 294{
fbb6976c 295 perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
fbb6976c
ACM
296 return 0;
297}
298
f045b8c4 299static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
eeb49845
NK
300{
301 u8 build_id[BUILD_ID_SIZE];
d77fac7f 302 char sbuild_id[SBUILD_ID_SIZE];
f045b8c4 303 struct nscookie nsc;
eeb49845 304
f045b8c4 305 int err;
eeb49845 306
f045b8c4
KJ
307 nsinfo__mountns_enter(nsi, &nsc);
308 err = filename__read_build_id(filename, &build_id, sizeof(build_id));
309 nsinfo__mountns_exit(&nsc);
310 if (err < 0) {
eeb49845
NK
311 pr_debug("Couldn't read a build-id in %s\n", filename);
312 return -1;
313 }
f045b8c4 314 err = 0;
eeb49845
NK
315
316 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
a50d11a1
MH
317 if (build_id_cache__cached(sbuild_id))
318 err = build_id_cache__remove_s(sbuild_id);
319
e35f7362 320 if (!err)
f045b8c4
KJ
321 err = build_id_cache__add_s(sbuild_id, filename, nsi, false,
322 false);
e35f7362 323
cc169c7c
MH
324 pr_debug("Updating %s %s: %s\n", sbuild_id, filename,
325 err ? "FAIL" : "Ok");
eeb49845
NK
326
327 return err;
328}
329
8e1e0d74
RB
330static int build_id_cache__show_all(void)
331{
332 struct strlist *bidlist;
333 struct str_node *nd;
334 char *buf;
335
336 bidlist = build_id_cache__list_all(true);
337 if (!bidlist) {
338 pr_debug("Failed to get buildids: -%d\n", errno);
339 return -1;
340 }
341 strlist__for_each_entry(nd, bidlist) {
342 buf = build_id_cache__origname(nd->s);
343 fprintf(stdout, "%s %s\n", nd->s, buf);
344 free(buf);
345 }
346 strlist__delete(bidlist);
347 return 0;
348}
349
b0ad8ea6 350int cmd_buildid_cache(int argc, const char **argv)
ef12a141
ACM
351{
352 struct strlist *list;
353 struct str_node *pos;
fbb6976c 354 int ret = 0;
f045b8c4 355 int ns_id = -1;
fbb6976c 356 bool force = false;
8e1e0d74
RB
357 bool list_files = false;
358 bool opts_flag = false;
9a73c308 359 bool purge_all = false;
472cc83c 360 char const *add_name_list_str = NULL,
fbb6976c 361 *remove_name_list_str = NULL,
8d8c8e4c 362 *purge_name_list_str = NULL,
eeb49845 363 *missing_filename = NULL,
fc1b691d 364 *update_name_list_str = NULL,
eec5a688 365 *kcore_filename = NULL;
340481ad 366 char sbuf[STRERR_BUFSIZE];
eeb49845 367
8ceb41d7 368 struct perf_data data = {
e3ed75bb
NK
369 .mode = PERF_DATA_MODE_READ,
370 };
371 struct perf_session *session = NULL;
f045b8c4 372 struct nsinfo *nsi = NULL;
e3ed75bb 373
472cc83c
ACM
374 const struct option buildid_cache_options[] = {
375 OPT_STRING('a', "add", &add_name_list_str,
376 "file list", "file(s) to add"),
fc1b691d
AH
377 OPT_STRING('k', "kcore", &kcore_filename,
378 "file", "kcore file to add"),
472cc83c
ACM
379 OPT_STRING('r', "remove", &remove_name_list_str, "file list",
380 "file(s) to remove"),
4359dd88
TMR
381 OPT_STRING('p', "purge", &purge_name_list_str, "file list",
382 "file(s) to remove (remove old caches too)"),
9a73c308 383 OPT_BOOLEAN('P', "purge-all", &purge_all, "purge all cached files"),
8e1e0d74 384 OPT_BOOLEAN('l', "list", &list_files, "list all cached files"),
fbb6976c
ACM
385 OPT_STRING('M', "missing", &missing_filename, "file",
386 "to find missing build ids in the cache"),
387 OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
eeb49845
NK
388 OPT_STRING('u', "update", &update_name_list_str, "file list",
389 "file(s) to update"),
472cc83c 390 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
f045b8c4 391 OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"),
472cc83c
ACM
392 OPT_END()
393 };
394 const char * const buildid_cache_usage[] = {
395 "perf buildid-cache [<options>]",
396 NULL
397 };
398
399 argc = parse_options(argc, argv, buildid_cache_options,
400 buildid_cache_usage, 0);
401
8e1e0d74
RB
402 opts_flag = add_name_list_str || kcore_filename ||
403 remove_name_list_str || purge_name_list_str ||
9a73c308
RB
404 missing_filename || update_name_list_str ||
405 purge_all;
8e1e0d74
RB
406
407 if (argc || !(list_files || opts_flag))
0497d0a8
MH
408 usage_with_options(buildid_cache_usage, buildid_cache_options);
409
8e1e0d74
RB
410 /* -l is exclusive. It can not be used with other options. */
411 if (list_files && opts_flag) {
412 usage_with_options_msg(buildid_cache_usage,
413 buildid_cache_options, "-l is exclusive.\n");
414 }
415
f045b8c4
KJ
416 if (ns_id > 0)
417 nsi = nsinfo__new(ns_id);
418
e3ed75bb 419 if (missing_filename) {
2d4f2799
JO
420 data.path = missing_filename;
421 data.force = force;
e3ed75bb 422
8ceb41d7 423 session = perf_session__new(&data, false, NULL);
e3ed75bb
NK
424 if (session == NULL)
425 return -1;
426 }
427
0a7e6d1b 428 if (symbol__init(session ? &session->header.env : NULL) < 0)
e3ed75bb 429 goto out;
472cc83c
ACM
430
431 setup_pager();
ef12a141 432
8e1e0d74
RB
433 if (list_files) {
434 ret = build_id_cache__show_all();
435 goto out;
436 }
437
ef12a141 438 if (add_name_list_str) {
4a77e218 439 list = strlist__new(add_name_list_str, NULL);
ef12a141 440 if (list) {
602a1f4d 441 strlist__for_each_entry(pos, list)
f045b8c4 442 if (build_id_cache__add_file(pos->s, nsi)) {
ef12a141
ACM
443 if (errno == EEXIST) {
444 pr_debug("%s already in the cache\n",
445 pos->s);
446 continue;
447 }
448 pr_warning("Couldn't add %s: %s\n",
c8b5f2c9 449 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
ef12a141
ACM
450 }
451
452 strlist__delete(list);
453 }
454 }
455
456 if (remove_name_list_str) {
4a77e218 457 list = strlist__new(remove_name_list_str, NULL);
ef12a141 458 if (list) {
602a1f4d 459 strlist__for_each_entry(pos, list)
f045b8c4 460 if (build_id_cache__remove_file(pos->s, nsi)) {
ef12a141
ACM
461 if (errno == ENOENT) {
462 pr_debug("%s wasn't in the cache\n",
463 pos->s);
464 continue;
465 }
466 pr_warning("Couldn't remove %s: %s\n",
c8b5f2c9 467 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
ef12a141
ACM
468 }
469
470 strlist__delete(list);
471 }
472 }
473
8d8c8e4c 474 if (purge_name_list_str) {
4a77e218 475 list = strlist__new(purge_name_list_str, NULL);
8d8c8e4c 476 if (list) {
602a1f4d 477 strlist__for_each_entry(pos, list)
f045b8c4 478 if (build_id_cache__purge_path(pos->s, nsi)) {
8d8c8e4c
MH
479 if (errno == ENOENT) {
480 pr_debug("%s wasn't in the cache\n",
481 pos->s);
482 continue;
483 }
484 pr_warning("Couldn't remove %s: %s\n",
c8b5f2c9 485 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
8d8c8e4c
MH
486 }
487
488 strlist__delete(list);
489 }
490 }
491
d8ed87bc
RB
492 if (purge_all) {
493 if (build_id_cache__purge_all()) {
494 pr_warning("Couldn't remove some caches. Error: %s.\n",
495 str_error_r(errno, sbuf, sizeof(sbuf)));
496 }
497 }
9a73c308 498
fbb6976c 499 if (missing_filename)
e3ed75bb 500 ret = build_id_cache__fprintf_missing(session, stdout);
fbb6976c 501
eeb49845 502 if (update_name_list_str) {
4a77e218 503 list = strlist__new(update_name_list_str, NULL);
eeb49845 504 if (list) {
602a1f4d 505 strlist__for_each_entry(pos, list)
f045b8c4 506 if (build_id_cache__update_file(pos->s, nsi)) {
eeb49845
NK
507 if (errno == ENOENT) {
508 pr_debug("%s wasn't in the cache\n",
509 pos->s);
510 continue;
511 }
512 pr_warning("Couldn't update %s: %s\n",
c8b5f2c9 513 pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
eeb49845
NK
514 }
515
516 strlist__delete(list);
517 }
518 }
519
e35f7362 520 if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
fc1b691d
AH
521 pr_warning("Couldn't add %s\n", kcore_filename);
522
e3ed75bb 523out:
e1446551 524 perf_session__delete(session);
f045b8c4 525 nsinfo__zput(nsi);
e3ed75bb 526
fbb6976c 527 return ret;
ef12a141 528}