blob: fae8b250b2ca711a8581fd70ab2142a1ddd5e9e5 [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 */
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"
Jiri Olsaebb296c2012-10-27 23:18:28 +020016#include "util/build-id.h"
Jiri Olsa4383db82012-10-27 23:18:29 +020017#include "util/symbol.h"
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020018
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020019static int build_id_cache__add_file(const char *filename, const char *debugdir)
20{
21 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
22 u8 build_id[BUILD_ID_SIZE];
23 int err;
24
25 if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
26 pr_debug("Couldn't read a build-id in %s\n", filename);
27 return -1;
28 }
29
30 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020031 err = build_id_cache__add_s(sbuild_id, debugdir, filename,
32 false, false);
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020033 if (verbose)
34 pr_info("Adding %s %s: %s\n", sbuild_id, filename,
35 err ? "FAIL" : "Ok");
36 return err;
37}
38
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -030039static int build_id_cache__remove_file(const char *filename,
40 const char *debugdir)
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020041{
42 u8 build_id[BUILD_ID_SIZE];
43 char sbuild_id[BUILD_ID_SIZE * 2 + 1];
44
45 int err;
46
47 if (filename__read_build_id(filename, &build_id, sizeof(build_id)) < 0) {
48 pr_debug("Couldn't read a build-id in %s\n", filename);
49 return -1;
50 }
51
52 build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
53 err = build_id_cache__remove_s(sbuild_id, debugdir);
54 if (verbose)
55 pr_info("Removing %s %s: %s\n", sbuild_id, filename,
56 err ? "FAIL" : "Ok");
57
58 return err;
59}
60
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -030061int cmd_buildid_cache(int argc, const char **argv,
62 const char *prefix __maybe_unused)
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020063{
64 struct strlist *list;
65 struct str_node *pos;
66 char debugdir[PATH_MAX];
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -030067 char const *add_name_list_str = NULL,
68 *remove_name_list_str = NULL;
69 const struct option buildid_cache_options[] = {
70 OPT_STRING('a', "add", &add_name_list_str,
71 "file list", "file(s) to add"),
72 OPT_STRING('r', "remove", &remove_name_list_str, "file list",
73 "file(s) to remove"),
74 OPT_INCR('v', "verbose", &verbose, "be more verbose"),
75 OPT_END()
76 };
77 const char * const buildid_cache_usage[] = {
78 "perf buildid-cache [<options>]",
79 NULL
80 };
81
82 argc = parse_options(argc, argv, buildid_cache_options,
83 buildid_cache_usage, 0);
84
85 if (symbol__init() < 0)
86 return -1;
87
88 setup_pager();
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020089
Stephane Eranian45de34b2010-06-01 21:25:01 +020090 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020091
92 if (add_name_list_str) {
93 list = strlist__new(true, add_name_list_str);
94 if (list) {
95 strlist__for_each(pos, list)
96 if (build_id_cache__add_file(pos->s, debugdir)) {
97 if (errno == EEXIST) {
98 pr_debug("%s already in the cache\n",
99 pos->s);
100 continue;
101 }
102 pr_warning("Couldn't add %s: %s\n",
103 pos->s, strerror(errno));
104 }
105
106 strlist__delete(list);
107 }
108 }
109
110 if (remove_name_list_str) {
111 list = strlist__new(true, remove_name_list_str);
112 if (list) {
113 strlist__for_each(pos, list)
114 if (build_id_cache__remove_file(pos->s, debugdir)) {
115 if (errno == ENOENT) {
116 pr_debug("%s wasn't in the cache\n",
117 pos->s);
118 continue;
119 }
120 pr_warning("Couldn't remove %s: %s\n",
121 pos->s, strerror(errno));
122 }
123
124 strlist__delete(list);
125 }
126 }
127
128 return 0;
129}