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>
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"
16 #include "util/build-id.h"
17 #include "util/symbol.h"
19 static int build_id_cache__add_file(const char *filename
, const char *debugdir
)
21 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
22 u8 build_id
[BUILD_ID_SIZE
];
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
);
30 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
31 err
= build_id_cache__add_s(sbuild_id
, debugdir
, filename
,
34 pr_info("Adding %s %s: %s\n", sbuild_id
, filename
,
39 static int build_id_cache__remove_file(const char *filename
,
42 u8 build_id
[BUILD_ID_SIZE
];
43 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
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
);
52 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
53 err
= build_id_cache__remove_s(sbuild_id
, debugdir
);
55 pr_info("Removing %s %s: %s\n", sbuild_id
, filename
,
61 int cmd_buildid_cache(int argc
, const char **argv
,
62 const char *prefix __maybe_unused
)
66 char debugdir
[PATH_MAX
];
67 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",
74 OPT_INCR('v', "verbose", &verbose
, "be more verbose"),
77 const char * const buildid_cache_usage
[] = {
78 "perf buildid-cache [<options>]",
82 argc
= parse_options(argc
, argv
, buildid_cache_options
,
83 buildid_cache_usage
, 0);
85 if (symbol__init() < 0)
90 snprintf(debugdir
, sizeof(debugdir
), "%s", buildid_dir
);
92 if (add_name_list_str
) {
93 list
= strlist__new(true, add_name_list_str
);
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",
102 pr_warning("Couldn't add %s: %s\n",
103 pos
->s
, strerror(errno
));
106 strlist__delete(list
);
110 if (remove_name_list_str
) {
111 list
= strlist__new(true, remove_name_list_str
);
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",
120 pr_warning("Couldn't remove %s: %s\n",
121 pos
->s
, strerror(errno
));
124 strlist__delete(list
);