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/symbol.h"
18 static char const *add_name_list_str
, *remove_name_list_str
;
20 static const char * const buildid_cache_usage
[] = {
21 "perf buildid-cache [<options>]",
25 static const struct option buildid_cache_options
[] = {
26 OPT_STRING('a', "add", &add_name_list_str
,
27 "file list", "file(s) to add"),
28 OPT_STRING('r', "remove", &remove_name_list_str
, "file list",
30 OPT_INCR('v', "verbose", &verbose
, "be more verbose"),
34 static int build_id_cache__add_file(const char *filename
, const char *debugdir
)
36 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
37 u8 build_id
[BUILD_ID_SIZE
];
40 if (filename__read_build_id(filename
, &build_id
, sizeof(build_id
)) < 0) {
41 pr_debug("Couldn't read a build-id in %s\n", filename
);
45 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
46 err
= build_id_cache__add_s(sbuild_id
, debugdir
, filename
, false);
48 pr_info("Adding %s %s: %s\n", sbuild_id
, filename
,
53 static int build_id_cache__remove_file(const char *filename __used
,
54 const char *debugdir __used
)
56 u8 build_id
[BUILD_ID_SIZE
];
57 char sbuild_id
[BUILD_ID_SIZE
* 2 + 1];
61 if (filename__read_build_id(filename
, &build_id
, sizeof(build_id
)) < 0) {
62 pr_debug("Couldn't read a build-id in %s\n", filename
);
66 build_id__sprintf(build_id
, sizeof(build_id
), sbuild_id
);
67 err
= build_id_cache__remove_s(sbuild_id
, debugdir
);
69 pr_info("Removing %s %s: %s\n", sbuild_id
, filename
,
75 static int __cmd_buildid_cache(void)
79 char debugdir
[PATH_MAX
];
81 snprintf(debugdir
, sizeof(debugdir
), "%s", buildid_dir
);
83 if (add_name_list_str
) {
84 list
= strlist__new(true, add_name_list_str
);
86 strlist__for_each(pos
, list
)
87 if (build_id_cache__add_file(pos
->s
, debugdir
)) {
88 if (errno
== EEXIST
) {
89 pr_debug("%s already in the cache\n",
93 pr_warning("Couldn't add %s: %s\n",
94 pos
->s
, strerror(errno
));
97 strlist__delete(list
);
101 if (remove_name_list_str
) {
102 list
= strlist__new(true, remove_name_list_str
);
104 strlist__for_each(pos
, list
)
105 if (build_id_cache__remove_file(pos
->s
, debugdir
)) {
106 if (errno
== ENOENT
) {
107 pr_debug("%s wasn't in the cache\n",
111 pr_warning("Couldn't remove %s: %s\n",
112 pos
->s
, strerror(errno
));
115 strlist__delete(list
);
122 int cmd_buildid_cache(int argc
, const char **argv
, const char *prefix __used
)
124 argc
= parse_options(argc
, argv
, buildid_cache_options
,
125 buildid_cache_usage
, 0);
127 if (symbol__init() < 0)
131 return __cmd_buildid_cache();