blob: a336014e02865192c81a49ec5bd54b5d418ee1a9 [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"
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -030017#include "util/session.h"
Jiri Olsa4383db82012-10-27 23:18:29 +020018#include "util/symbol.h"
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020019
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020020static 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);
Jiri Olsa7dbf4dc2012-09-10 18:50:19 +020032 err = build_id_cache__add_s(sbuild_id, debugdir, filename,
33 false, false);
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020034 if (verbose)
35 pr_info("Adding %s %s: %s\n", sbuild_id, filename,
36 err ? "FAIL" : "Ok");
37 return err;
38}
39
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -030040static int build_id_cache__remove_file(const char *filename,
41 const char *debugdir)
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020042{
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
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -030062static 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
83static 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
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -030096int cmd_buildid_cache(int argc, const char **argv,
97 const char *prefix __maybe_unused)
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -020098{
99 struct strlist *list;
100 struct str_node *pos;
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300101 int ret = 0;
102 bool force = false;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200103 char debugdir[PATH_MAX];
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300104 char const *add_name_list_str = NULL,
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300105 *remove_name_list_str = NULL,
106 *missing_filename = NULL;
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300107 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"),
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300112 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"),
Arnaldo Carvalho de Melo472cc832012-10-01 15:20:58 -0300115 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();
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200130
Stephane Eranian45de34b2010-06-01 21:25:01 +0200131 snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir);
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200132
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
Arnaldo Carvalho de Melofbb69762012-12-07 16:28:27 -0300169 if (missing_filename)
170 ret = build_id_cache__fprintf_missing(missing_filename, force, stdout);
171
172 return ret;
Arnaldo Carvalho de Meloef12a142010-01-20 15:28:45 -0200173}