2 * builtin-buildid-cache.c
4 * Builtin buildid-cache command: Manages build-id cache
6 * Copyright (C) 2010, Red Hat Inc.
7 * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
16 #include "util/cache.h"
17 #include "util/debug.h"
18 #include "util/header.h"
19 #include <subcmd/parse-options.h>
20 #include "util/strlist.h"
21 #include "util/build-id.h"
22 #include "util/session.h"
23 #include "util/symbol.h"
25 static int build_id_cache__kcore_buildid(const char *proc_dir
, char *sbuildid
)
27 char root_dir
[PATH_MAX
];
30 strlcpy(root_dir
, proc_dir
, sizeof(root_dir
));
32 p
= strrchr(root_dir
, '/');
36 return sysfs__sprintf_build_id(root_dir
, sbuildid
);
39 static int build_id_cache__kcore_dir(char *dir
, size_t sz
)
45 if (gettimeofday(&tv
, NULL
) || !localtime_r(&tv
.tv_sec
, &tm
))
48 if (!strftime(dt
, sizeof(dt
), "%Y%m%d%H%M%S", &tm
))
51 scnprintf(dir
, sz
, "%s%02u", dt
, (unsigned)tv
.tv_usec
/ 10000);
56 static bool same_kallsyms_reloc(const char *from_dir
, char *to_dir
)
61 u64 addr1
= 0, addr2
= 0;
64 scnprintf(from
, sizeof(from
), "%s/kallsyms", from_dir
);
65 scnprintf(to
, sizeof(to
), "%s/kallsyms", to_dir
);
67 for (i
= 0; (name
= ref_reloc_sym_names
[i
]) != NULL
; i
++) {
68 addr1
= kallsyms__get_function_start(from
, name
);
74 addr2
= kallsyms__get_function_start(to
, name
);
76 return addr1
== addr2
;
79 static int build_id_cache__kcore_existing(const char *from_dir
, char *to_dir
,
84 char to_subdir
[PATH_MAX
];
93 scnprintf(from
, sizeof(from
), "%s/modules", from_dir
);
99 if (dent
->d_type
!= DT_DIR
)
101 scnprintf(to
, sizeof(to
), "%s/%s/modules", to_dir
,
103 scnprintf(to_subdir
, sizeof(to_subdir
), "%s/%s",
104 to_dir
, dent
->d_name
);
105 if (!compare_proc_modules(from
, to
) &&
106 same_kallsyms_reloc(from_dir
, to_subdir
)) {
107 strlcpy(to_dir
, to_subdir
, to_dir_sz
);
118 static int build_id_cache__add_kcore(const char *filename
, bool force
)
120 char dir
[32], sbuildid
[SBUILD_ID_SIZE
];
121 char from_dir
[PATH_MAX
], to_dir
[PATH_MAX
];
124 strlcpy(from_dir
, filename
, sizeof(from_dir
));
126 p
= strrchr(from_dir
, '/');
127 if (!p
|| strcmp(p
+ 1, "kcore"))
131 if (build_id_cache__kcore_buildid(from_dir
, sbuildid
) < 0)
134 scnprintf(to_dir
, sizeof(to_dir
), "%s/[kernel.kcore]/%s",
135 buildid_dir
, sbuildid
);
138 !build_id_cache__kcore_existing(from_dir
, to_dir
, sizeof(to_dir
))) {
139 pr_debug("same kcore found in %s\n", to_dir
);
143 if (build_id_cache__kcore_dir(dir
, sizeof(dir
)))
146 scnprintf(to_dir
, sizeof(to_dir
), "%s/[kernel.kcore]/%s/%s",
147 buildid_dir
, sbuildid
, dir
);
149 if (mkdir_p(to_dir
, 0755))
152 if (kcore_copy(from_dir
, to_dir
)) {
153 /* Remove YYYYmmddHHMMSShh directory */
154 if (!rmdir(to_dir
)) {
155 p
= strrchr(to_dir
, '/');
158 /* Try to remove buildid directory */
159 if (!rmdir(to_dir
)) {
160 p
= strrchr(to_dir
, '/');
163 /* Try to remove [kernel.kcore] directory */
170 pr_debug("kcore added to build-id cache directory %s\n", to_dir
);
175 static int build_id_cache__add_file(const char *filename
)
177 char sbuild_id
[SBUILD_ID_SIZE
];
178 u8 build_id
[BUILD_ID_SIZE
];
181 if (filename__read_build_id(filename
, &build_id
, sizeof(build_id
)) < 0) {
182 pr_debug("Couldn't read a build-id in %s\n", filename
);
186 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
187 err
= build_id_cache__add_s(sbuild_id
, filename
,
189 pr_debug("Adding %s %s: %s\n", sbuild_id
, filename
,
190 err
? "FAIL" : "Ok");
194 static int build_id_cache__remove_file(const char *filename
)
196 u8 build_id
[BUILD_ID_SIZE
];
197 char sbuild_id
[SBUILD_ID_SIZE
];
201 if (filename__read_build_id(filename
, &build_id
, sizeof(build_id
)) < 0) {
202 pr_debug("Couldn't read a build-id in %s\n", filename
);
206 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
207 err
= build_id_cache__remove_s(sbuild_id
);
208 pr_debug("Removing %s %s: %s\n", sbuild_id
, filename
,
209 err
? "FAIL" : "Ok");
214 static int build_id_cache__purge_path(const char *pathname
)
216 struct strlist
*list
;
217 struct str_node
*pos
;
220 err
= build_id_cache__list_build_ids(pathname
, &list
);
224 strlist__for_each(pos
, list
) {
225 err
= build_id_cache__remove_s(pos
->s
);
226 pr_debug("Removing %s %s: %s\n", pos
->s
, pathname
,
227 err
? "FAIL" : "Ok");
231 strlist__delete(list
);
234 pr_debug("Purging %s: %s\n", pathname
, err
? "FAIL" : "Ok");
239 static bool dso__missing_buildid_cache(struct dso
*dso
, int parm __maybe_unused
)
241 char filename
[PATH_MAX
];
242 u8 build_id
[BUILD_ID_SIZE
];
244 if (dso__build_id_filename(dso
, filename
, sizeof(filename
)) &&
245 filename__read_build_id(filename
, build_id
,
246 sizeof(build_id
)) != sizeof(build_id
)) {
250 pr_warning("Problems with %s file, consider removing it from the cache\n",
252 } else if (memcmp(dso
->build_id
, build_id
, sizeof(dso
->build_id
))) {
253 pr_warning("Problems with %s file, consider removing it from the cache\n",
260 static int build_id_cache__fprintf_missing(struct perf_session
*session
, FILE *fp
)
262 perf_session__fprintf_dsos_buildid(session
, fp
, dso__missing_buildid_cache
, 0);
266 static int build_id_cache__update_file(const char *filename
)
268 u8 build_id
[BUILD_ID_SIZE
];
269 char sbuild_id
[SBUILD_ID_SIZE
];
273 if (filename__read_build_id(filename
, &build_id
, sizeof(build_id
)) < 0) {
274 pr_debug("Couldn't read a build-id in %s\n", filename
);
278 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
279 if (build_id_cache__cached(sbuild_id
))
280 err
= build_id_cache__remove_s(sbuild_id
);
283 err
= build_id_cache__add_s(sbuild_id
, filename
, false, false);
285 pr_debug("Updating %s %s: %s\n", sbuild_id
, filename
,
286 err
? "FAIL" : "Ok");
291 int cmd_buildid_cache(int argc
, const char **argv
,
292 const char *prefix __maybe_unused
)
294 struct strlist
*list
;
295 struct str_node
*pos
;
298 char const *add_name_list_str
= NULL
,
299 *remove_name_list_str
= NULL
,
300 *purge_name_list_str
= NULL
,
301 *missing_filename
= NULL
,
302 *update_name_list_str
= NULL
,
303 *kcore_filename
= NULL
;
304 char sbuf
[STRERR_BUFSIZE
];
306 struct perf_data_file file
= {
307 .mode
= PERF_DATA_MODE_READ
,
309 struct perf_session
*session
= NULL
;
311 const struct option buildid_cache_options
[] = {
312 OPT_STRING('a', "add", &add_name_list_str
,
313 "file list", "file(s) to add"),
314 OPT_STRING('k', "kcore", &kcore_filename
,
315 "file", "kcore file to add"),
316 OPT_STRING('r', "remove", &remove_name_list_str
, "file list",
317 "file(s) to remove"),
318 OPT_STRING('p', "purge", &purge_name_list_str
, "path list",
319 "path(s) to remove (remove old caches too)"),
320 OPT_STRING('M', "missing", &missing_filename
, "file",
321 "to find missing build ids in the cache"),
322 OPT_BOOLEAN('f', "force", &force
, "don't complain, do it"),
323 OPT_STRING('u', "update", &update_name_list_str
, "file list",
324 "file(s) to update"),
325 OPT_INCR('v', "verbose", &verbose
, "be more verbose"),
328 const char * const buildid_cache_usage
[] = {
329 "perf buildid-cache [<options>]",
333 argc
= parse_options(argc
, argv
, buildid_cache_options
,
334 buildid_cache_usage
, 0);
336 if (argc
|| (!add_name_list_str
&& !kcore_filename
&&
337 !remove_name_list_str
&& !purge_name_list_str
&&
338 !missing_filename
&& !update_name_list_str
))
339 usage_with_options(buildid_cache_usage
, buildid_cache_options
);
341 if (missing_filename
) {
342 file
.path
= missing_filename
;
345 session
= perf_session__new(&file
, false, NULL
);
350 if (symbol__init(session
? &session
->header
.env
: NULL
) < 0)
355 if (add_name_list_str
) {
356 list
= strlist__new(add_name_list_str
, NULL
);
358 strlist__for_each(pos
, list
)
359 if (build_id_cache__add_file(pos
->s
)) {
360 if (errno
== EEXIST
) {
361 pr_debug("%s already in the cache\n",
365 pr_warning("Couldn't add %s: %s\n",
366 pos
->s
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));
369 strlist__delete(list
);
373 if (remove_name_list_str
) {
374 list
= strlist__new(remove_name_list_str
, NULL
);
376 strlist__for_each(pos
, list
)
377 if (build_id_cache__remove_file(pos
->s
)) {
378 if (errno
== ENOENT
) {
379 pr_debug("%s wasn't in the cache\n",
383 pr_warning("Couldn't remove %s: %s\n",
384 pos
->s
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));
387 strlist__delete(list
);
391 if (purge_name_list_str
) {
392 list
= strlist__new(purge_name_list_str
, NULL
);
394 strlist__for_each(pos
, list
)
395 if (build_id_cache__purge_path(pos
->s
)) {
396 if (errno
== ENOENT
) {
397 pr_debug("%s wasn't in the cache\n",
401 pr_warning("Couldn't remove %s: %s\n",
402 pos
->s
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));
405 strlist__delete(list
);
409 if (missing_filename
)
410 ret
= build_id_cache__fprintf_missing(session
, stdout
);
412 if (update_name_list_str
) {
413 list
= strlist__new(update_name_list_str
, NULL
);
415 strlist__for_each(pos
, list
)
416 if (build_id_cache__update_file(pos
->s
)) {
417 if (errno
== ENOENT
) {
418 pr_debug("%s wasn't in the cache\n",
422 pr_warning("Couldn't update %s: %s\n",
423 pos
->s
, strerror_r(errno
, sbuf
, sizeof(sbuf
)));
426 strlist__delete(list
);
430 if (kcore_filename
&& build_id_cache__add_kcore(kcore_filename
, force
))
431 pr_warning("Couldn't add %s\n", kcore_filename
);
435 perf_session__delete(session
);