Commit | Line | Data |
---|---|---|
ef12a141 ACM |
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 | */ | |
fc1b691d AH |
9 | #include <sys/types.h> |
10 | #include <sys/time.h> | |
11 | #include <time.h> | |
12 | #include <dirent.h> | |
13 | #include <unistd.h> | |
ef12a141 ACM |
14 | #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" | |
ebb296c2 | 21 | #include "util/build-id.h" |
fbb6976c | 22 | #include "util/session.h" |
4383db88 | 23 | #include "util/symbol.h" |
ef12a141 | 24 | |
fc1b691d AH |
25 | static 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 | ||
49 | static 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 | ||
66 | static 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 | ||
103 | static 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 | ||
ef12a141 ACM |
159 | static 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); | |
7dbf4dcf JO |
171 | err = build_id_cache__add_s(sbuild_id, debugdir, filename, |
172 | false, false); | |
ef12a141 ACM |
173 | if (verbose) |
174 | pr_info("Adding %s %s: %s\n", sbuild_id, filename, | |
175 | err ? "FAIL" : "Ok"); | |
176 | return err; | |
177 | } | |
178 | ||
472cc83c ACM |
179 | static int build_id_cache__remove_file(const char *filename, |
180 | const char *debugdir) | |
ef12a141 ACM |
181 | { |
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 | ||
fbb6976c ACM |
201 | static 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 | ||
222 | static int build_id_cache__fprintf_missing(const char *filename, bool force, FILE *fp) | |
223 | { | |
f5fc1412 JO |
224 | 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); | |
fbb6976c ACM |
230 | 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 | ||
eeb49845 NK |
239 | static 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 | ||
472cc83c ACM |
265 | int cmd_buildid_cache(int argc, const char **argv, |
266 | const char *prefix __maybe_unused) | |
ef12a141 ACM |
267 | { |
268 | struct strlist *list; | |
269 | struct str_node *pos; | |
fbb6976c ACM |
270 | int ret = 0; |
271 | bool force = false; | |
ef12a141 | 272 | char debugdir[PATH_MAX]; |
472cc83c | 273 | char const *add_name_list_str = NULL, |
fbb6976c | 274 | *remove_name_list_str = NULL, |
eeb49845 | 275 | *missing_filename = NULL, |
fc1b691d AH |
276 | *update_name_list_str = NULL, |
277 | *kcore_filename; | |
eeb49845 | 278 | |
472cc83c ACM |
279 | const struct option buildid_cache_options[] = { |
280 | OPT_STRING('a', "add", &add_name_list_str, | |
281 | "file list", "file(s) to add"), | |
fc1b691d AH |
282 | OPT_STRING('k', "kcore", &kcore_filename, |
283 | "file", "kcore file to add"), | |
472cc83c ACM |
284 | OPT_STRING('r', "remove", &remove_name_list_str, "file list", |
285 | "file(s) to remove"), | |
fbb6976c ACM |
286 | 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"), | |
eeb49845 NK |
289 | OPT_STRING('u', "update", &update_name_list_str, "file list", |
290 | "file(s) to update"), | |
472cc83c ACM |
291 | 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(); | |
ef12a141 | 306 | |
45de34bb | 307 | snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); |
ef12a141 ACM |
308 | |
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 | ||
fbb6976c ACM |
345 | if (missing_filename) |
346 | ret = build_id_cache__fprintf_missing(missing_filename, force, stdout); | |
347 | ||
eeb49845 NK |
348 | 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 | ||
fc1b691d AH |
366 | if (kcore_filename && |
367 | build_id_cache__add_kcore(kcore_filename, debugdir)) | |
368 | pr_warning("Couldn't add %s\n", kcore_filename); | |
369 | ||
fbb6976c | 370 | return ret; |
ef12a141 | 371 | } |