Blame view

tools/perf/builtin-buildid-cache.c 12.3 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
2
3
4
5
6
7
8
9
  /*
   * builtin-buildid-cache.c
   *
   * Builtin buildid-cache command: Manages build-id cache
   *
   * Copyright (C) 2010, Red Hat Inc.
   * Copyright (C) 2010, Arnaldo Carvalho de Melo <acme@redhat.com>
   */
fc1b691d7   Adrian Hunter   perf buildid-cach...
10
11
12
13
  #include <sys/types.h>
  #include <sys/time.h>
  #include <time.h>
  #include <dirent.h>
a43783aee   Arnaldo Carvalho de Melo   perf tools: Inclu...
14
  #include <errno.h>
fc1b691d7   Adrian Hunter   perf buildid-cach...
15
  #include <unistd.h>
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
16
  #include "builtin.h"
f045b8c4b   Krister Johansen   perf buildid-cach...
17
  #include "namespaces.h"
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
18
19
  #include "util/debug.h"
  #include "util/header.h"
fa0d98462   Arnaldo Carvalho de Melo   perf tools: Remov...
20
  #include <subcmd/pager.h>
4b6ab94ea   Josh Poimboeuf   perf subcmd: Crea...
21
  #include <subcmd/parse-options.h>
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
22
  #include "util/strlist.h"
ebb296c27   Jiri Olsa   perf tools: Move ...
23
  #include "util/build-id.h"
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
24
  #include "util/session.h"
4a3cec849   Arnaldo Carvalho de Melo   perf dsos: Move t...
25
  #include "util/dso.h"
4383db88a   Jiri Olsa   perf tools: Move ...
26
  #include "util/symbol.h"
c5e4027e0   Arnaldo Carvalho de Melo   perf tools: Move ...
27
  #include "util/time-utils.h"
efa73d37c   Arnaldo Carvalho de Melo   perf tools: Remov...
28
  #include "util/util.h"
8e1e0d746   Ravi Bangoria   perf buildid-cach...
29
  #include "util/probe-file.h"
fa0d98462   Arnaldo Carvalho de Melo   perf tools: Remov...
30
  #include <linux/string.h>
6ef81c55a   Mamatha Inamdar   perf session: Ret...
31
  #include <linux/err.h>
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
32

fc1b691d7   Adrian Hunter   perf buildid-cach...
33
34
35
  static int build_id_cache__kcore_buildid(const char *proc_dir, char *sbuildid)
  {
  	char root_dir[PATH_MAX];
fc1b691d7   Adrian Hunter   perf buildid-cach...
36
37
38
39
40
41
42
43
  	char *p;
  
  	strlcpy(root_dir, proc_dir, sizeof(root_dir));
  
  	p = strrchr(root_dir, '/');
  	if (!p)
  		return -1;
  	*p = '\0';
0b5a7935f   Masami Hiramatsu   perf buildid: Int...
44
  	return sysfs__sprintf_build_id(root_dir, sbuildid);
fc1b691d7   Adrian Hunter   perf buildid-cach...
45
46
47
48
  }
  
  static int build_id_cache__kcore_dir(char *dir, size_t sz)
  {
37b20151e   Wang Nan   perf tools: Move ...
49
  	return fetch_current_timestamp(dir, sz);
fc1b691d7   Adrian Hunter   perf buildid-cach...
50
  }
d3b702202   Adrian Hunter   perf buildid-cach...
51
52
53
54
55
56
  static bool same_kallsyms_reloc(const char *from_dir, char *to_dir)
  {
  	char from[PATH_MAX];
  	char to[PATH_MAX];
  	const char *name;
  	u64 addr1 = 0, addr2 = 0;
b843f62ad   Arnaldo Carvalho de Melo   perf symbols: Acc...
57
  	int i, err = -1;
d3b702202   Adrian Hunter   perf buildid-cach...
58
59
60
61
62
  
  	scnprintf(from, sizeof(from), "%s/kallsyms", from_dir);
  	scnprintf(to, sizeof(to), "%s/kallsyms", to_dir);
  
  	for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
b843f62ad   Arnaldo Carvalho de Melo   perf symbols: Acc...
63
64
  		err = kallsyms__get_function_start(from, name, &addr1);
  		if (!err)
d3b702202   Adrian Hunter   perf buildid-cach...
65
66
  			break;
  	}
b843f62ad   Arnaldo Carvalho de Melo   perf symbols: Acc...
67
68
69
70
71
  	if (err)
  		return false;
  
  	if (kallsyms__get_function_start(to, name, &addr2))
  		return false;
d3b702202   Adrian Hunter   perf buildid-cach...
72
73
74
  
  	return addr1 == addr2;
  }
fc1b691d7   Adrian Hunter   perf buildid-cach...
75
76
77
78
79
  static int build_id_cache__kcore_existing(const char *from_dir, char *to_dir,
  					  size_t to_dir_sz)
  {
  	char from[PATH_MAX];
  	char to[PATH_MAX];
d3b702202   Adrian Hunter   perf buildid-cach...
80
  	char to_subdir[PATH_MAX];
fc1b691d7   Adrian Hunter   perf buildid-cach...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  	struct dirent *dent;
  	int ret = -1;
  	DIR *d;
  
  	d = opendir(to_dir);
  	if (!d)
  		return -1;
  
  	scnprintf(from, sizeof(from), "%s/modules", from_dir);
  
  	while (1) {
  		dent = readdir(d);
  		if (!dent)
  			break;
  		if (dent->d_type != DT_DIR)
  			continue;
  		scnprintf(to, sizeof(to), "%s/%s/modules", to_dir,
  			  dent->d_name);
d3b702202   Adrian Hunter   perf buildid-cach...
99
100
101
102
103
  		scnprintf(to_subdir, sizeof(to_subdir), "%s/%s",
  			  to_dir, dent->d_name);
  		if (!compare_proc_modules(from, to) &&
  		    same_kallsyms_reloc(from_dir, to_subdir)) {
  			strlcpy(to_dir, to_subdir, to_dir_sz);
fc1b691d7   Adrian Hunter   perf buildid-cach...
104
105
106
107
108
109
110
111
112
  			ret = 0;
  			break;
  		}
  	}
  
  	closedir(d);
  
  	return ret;
  }
e35f7362b   Masami Hiramatsu   perf buildid-cach...
113
  static int build_id_cache__add_kcore(const char *filename, bool force)
fc1b691d7   Adrian Hunter   perf buildid-cach...
114
  {
d77fac7f9   Masami Hiramatsu   perf buildid: Use...
115
  	char dir[32], sbuildid[SBUILD_ID_SIZE];
fc1b691d7   Adrian Hunter   perf buildid-cach...
116
117
118
119
120
121
122
123
124
  	char from_dir[PATH_MAX], to_dir[PATH_MAX];
  	char *p;
  
  	strlcpy(from_dir, filename, sizeof(from_dir));
  
  	p = strrchr(from_dir, '/');
  	if (!p || strcmp(p + 1, "kcore"))
  		return -1;
  	*p = '\0';
0b5a7935f   Masami Hiramatsu   perf buildid: Int...
125
  	if (build_id_cache__kcore_buildid(from_dir, sbuildid) < 0)
fc1b691d7   Adrian Hunter   perf buildid-cach...
126
  		return -1;
0a77582f0   Masami Hiramatsu   perf symbols: Int...
127
128
  	scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s",
  		  buildid_dir, DSO__NAME_KCORE, sbuildid);
fc1b691d7   Adrian Hunter   perf buildid-cach...
129

5173fbb8a   Adrian Hunter   perf buildid-cach...
130
131
  	if (!force &&
  	    !build_id_cache__kcore_existing(from_dir, to_dir, sizeof(to_dir))) {
fc1b691d7   Adrian Hunter   perf buildid-cach...
132
133
134
135
136
137
138
  		pr_debug("same kcore found in %s
  ", to_dir);
  		return 0;
  	}
  
  	if (build_id_cache__kcore_dir(dir, sizeof(dir)))
  		return -1;
0a77582f0   Masami Hiramatsu   perf symbols: Int...
139
140
  	scnprintf(to_dir, sizeof(to_dir), "%s/%s/%s/%s",
  		  buildid_dir, DSO__NAME_KCORE, sbuildid, dir);
fc1b691d7   Adrian Hunter   perf buildid-cach...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
  
  	if (mkdir_p(to_dir, 0755))
  		return -1;
  
  	if (kcore_copy(from_dir, to_dir)) {
  		/* Remove YYYYmmddHHMMSShh directory */
  		if (!rmdir(to_dir)) {
  			p = strrchr(to_dir, '/');
  			if (p)
  				*p = '\0';
  			/* Try to remove buildid directory */
  			if (!rmdir(to_dir)) {
  				p = strrchr(to_dir, '/');
  				if (p)
  					*p = '\0';
  				/* Try to remove [kernel.kcore] directory */
  				rmdir(to_dir);
  			}
  		}
  		return -1;
  	}
  
  	pr_debug("kcore added to build-id cache directory %s
  ", to_dir);
  
  	return 0;
  }
f045b8c4b   Krister Johansen   perf buildid-cach...
168
  static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
169
  {
d77fac7f9   Masami Hiramatsu   perf buildid: Use...
170
  	char sbuild_id[SBUILD_ID_SIZE];
f766819cd   Jiri Olsa   perf tools: Pass ...
171
  	struct build_id bid;
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
172
  	int err;
f045b8c4b   Krister Johansen   perf buildid-cach...
173
  	struct nscookie nsc;
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
174

f045b8c4b   Krister Johansen   perf buildid-cach...
175
  	nsinfo__mountns_enter(nsi, &nsc);
f766819cd   Jiri Olsa   perf tools: Pass ...
176
  	err = filename__read_build_id(filename, &bid);
f045b8c4b   Krister Johansen   perf buildid-cach...
177
178
  	nsinfo__mountns_exit(&nsc);
  	if (err < 0) {
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
179
180
181
182
  		pr_debug("Couldn't read a build-id in %s
  ", filename);
  		return -1;
  	}
bf5411695   Jiri Olsa   perf tools: Pass ...
183
  	build_id__sprintf(&bid, sbuild_id);
f045b8c4b   Krister Johansen   perf buildid-cach...
184
  	err = build_id_cache__add_s(sbuild_id, filename, nsi,
7dbf4dcfe   Jiri Olsa   perf tools: Back ...
185
  				    false, false);
cc169c7c3   Masami Hiramatsu   perf buildid-cach...
186
187
188
  	pr_debug("Adding %s %s: %s
  ", sbuild_id, filename,
  		 err ? "FAIL" : "Ok");
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
189
190
  	return err;
  }
f045b8c4b   Krister Johansen   perf buildid-cach...
191
  static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
192
  {
d77fac7f9   Masami Hiramatsu   perf buildid: Use...
193
  	char sbuild_id[SBUILD_ID_SIZE];
f766819cd   Jiri Olsa   perf tools: Pass ...
194
  	struct build_id bid;
f045b8c4b   Krister Johansen   perf buildid-cach...
195
  	struct nscookie nsc;
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
196
197
  
  	int err;
f045b8c4b   Krister Johansen   perf buildid-cach...
198
  	nsinfo__mountns_enter(nsi, &nsc);
f766819cd   Jiri Olsa   perf tools: Pass ...
199
  	err = filename__read_build_id(filename, &bid);
f045b8c4b   Krister Johansen   perf buildid-cach...
200
201
  	nsinfo__mountns_exit(&nsc);
  	if (err < 0) {
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
202
203
204
205
  		pr_debug("Couldn't read a build-id in %s
  ", filename);
  		return -1;
  	}
bf5411695   Jiri Olsa   perf tools: Pass ...
206
  	build_id__sprintf(&bid, sbuild_id);
e35f7362b   Masami Hiramatsu   perf buildid-cach...
207
  	err = build_id_cache__remove_s(sbuild_id);
cc169c7c3   Masami Hiramatsu   perf buildid-cach...
208
209
210
  	pr_debug("Removing %s %s: %s
  ", sbuild_id, filename,
  		 err ? "FAIL" : "Ok");
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
211
212
213
  
  	return err;
  }
f045b8c4b   Krister Johansen   perf buildid-cach...
214
  static int build_id_cache__purge_path(const char *pathname, struct nsinfo *nsi)
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
215
216
217
218
  {
  	struct strlist *list;
  	struct str_node *pos;
  	int err;
f045b8c4b   Krister Johansen   perf buildid-cach...
219
  	err = build_id_cache__list_build_ids(pathname, nsi, &list);
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
220
221
  	if (err)
  		goto out;
602a1f4da   Arnaldo Carvalho de Melo   perf tools: Renam...
222
  	strlist__for_each_entry(pos, list) {
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
223
  		err = build_id_cache__remove_s(pos->s);
cc169c7c3   Masami Hiramatsu   perf buildid-cach...
224
225
226
  		pr_debug("Removing %s %s: %s
  ", pos->s, pathname,
  			 err ? "FAIL" : "Ok");
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
227
228
229
230
231
232
  		if (err)
  			break;
  	}
  	strlist__delete(list);
  
  out:
cc169c7c3   Masami Hiramatsu   perf buildid-cach...
233
234
  	pr_debug("Purging %s: %s
  ", pathname, err ? "FAIL" : "Ok");
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
235
236
237
  
  	return err;
  }
9a73c3085   Ravi Bangoria   perf buildid-cach...
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  static int build_id_cache__purge_all(void)
  {
  	struct strlist *list;
  	struct str_node *pos;
  	int err = 0;
  	char *buf;
  
  	list = build_id_cache__list_all(false);
  	if (!list) {
  		pr_debug("Failed to get buildids: -%d
  ", errno);
  		return -EINVAL;
  	}
  
  	strlist__for_each_entry(pos, list) {
  		buf = build_id_cache__origname(pos->s);
  		err = build_id_cache__remove_s(pos->s);
  		pr_debug("Removing %s (%s): %s
  ", buf, pos->s,
  			 err ? "FAIL" : "Ok");
  		free(buf);
  		if (err)
  			break;
  	}
  	strlist__delete(list);
  
  	pr_debug("Purged all: %s
  ", err ? "FAIL" : "Ok");
  	return err;
  }
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
268
269
270
  static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)
  {
  	char filename[PATH_MAX];
f766819cd   Jiri Olsa   perf tools: Pass ...
271
  	struct build_id bid;
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
272

d2396999c   Krister Johansen   perf buildid-cach...
273
  	if (dso__build_id_filename(dso, filename, sizeof(filename), false) &&
f766819cd   Jiri Olsa   perf tools: Pass ...
274
  	    filename__read_build_id(filename, &bid) == -1) {
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
275
276
  		if (errno == ENOENT)
  			return false;
48000a1ae   Arnaldo Carvalho de Melo   perf tools: Remov...
277
278
  		pr_warning("Problems with %s file, consider removing it from the cache
  ",
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
279
  			   filename);
f766819cd   Jiri Olsa   perf tools: Pass ...
280
  	} else if (memcmp(dso->bid.data, bid.data, bid.size)) {
48000a1ae   Arnaldo Carvalho de Melo   perf tools: Remov...
281
282
  		pr_warning("Problems with %s file, consider removing it from the cache
  ",
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
283
284
285
286
287
  			   filename);
  	}
  
  	return true;
  }
e3ed75bb5   Namhyung Kim   perf buildid-cach...
288
  static int build_id_cache__fprintf_missing(struct perf_session *session, FILE *fp)
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
289
  {
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
290
  	perf_session__fprintf_dsos_buildid(session, fp, dso__missing_buildid_cache, 0);
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
291
292
  	return 0;
  }
f045b8c4b   Krister Johansen   perf buildid-cach...
293
  static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
eeb498454   Namhyung Kim   perf buildid-cach...
294
  {
d77fac7f9   Masami Hiramatsu   perf buildid: Use...
295
  	char sbuild_id[SBUILD_ID_SIZE];
f766819cd   Jiri Olsa   perf tools: Pass ...
296
  	struct build_id bid;
f045b8c4b   Krister Johansen   perf buildid-cach...
297
  	struct nscookie nsc;
eeb498454   Namhyung Kim   perf buildid-cach...
298

f045b8c4b   Krister Johansen   perf buildid-cach...
299
  	int err;
eeb498454   Namhyung Kim   perf buildid-cach...
300

f045b8c4b   Krister Johansen   perf buildid-cach...
301
  	nsinfo__mountns_enter(nsi, &nsc);
f766819cd   Jiri Olsa   perf tools: Pass ...
302
  	err = filename__read_build_id(filename, &bid);
f045b8c4b   Krister Johansen   perf buildid-cach...
303
304
  	nsinfo__mountns_exit(&nsc);
  	if (err < 0) {
eeb498454   Namhyung Kim   perf buildid-cach...
305
306
307
308
  		pr_debug("Couldn't read a build-id in %s
  ", filename);
  		return -1;
  	}
f045b8c4b   Krister Johansen   perf buildid-cach...
309
  	err = 0;
eeb498454   Namhyung Kim   perf buildid-cach...
310

bf5411695   Jiri Olsa   perf tools: Pass ...
311
  	build_id__sprintf(&bid, sbuild_id);
a50d11a10   Masami Hiramatsu   perf buildid-cach...
312
313
  	if (build_id_cache__cached(sbuild_id))
  		err = build_id_cache__remove_s(sbuild_id);
e35f7362b   Masami Hiramatsu   perf buildid-cach...
314
  	if (!err)
f045b8c4b   Krister Johansen   perf buildid-cach...
315
316
  		err = build_id_cache__add_s(sbuild_id, filename, nsi, false,
  					    false);
e35f7362b   Masami Hiramatsu   perf buildid-cach...
317

cc169c7c3   Masami Hiramatsu   perf buildid-cach...
318
319
320
  	pr_debug("Updating %s %s: %s
  ", sbuild_id, filename,
  		 err ? "FAIL" : "Ok");
eeb498454   Namhyung Kim   perf buildid-cach...
321
322
323
  
  	return err;
  }
8e1e0d746   Ravi Bangoria   perf buildid-cach...
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  static int build_id_cache__show_all(void)
  {
  	struct strlist *bidlist;
  	struct str_node *nd;
  	char *buf;
  
  	bidlist = build_id_cache__list_all(true);
  	if (!bidlist) {
  		pr_debug("Failed to get buildids: -%d
  ", errno);
  		return -1;
  	}
  	strlist__for_each_entry(nd, bidlist) {
  		buf = build_id_cache__origname(nd->s);
  		fprintf(stdout, "%s %s
  ", nd->s, buf);
  		free(buf);
  	}
  	strlist__delete(bidlist);
  	return 0;
  }
b0ad8ea66   Arnaldo Carvalho de Melo   perf tools: Remov...
345
  int cmd_buildid_cache(int argc, const char **argv)
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
346
347
348
  {
  	struct strlist *list;
  	struct str_node *pos;
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
349
  	int ret = 0;
f045b8c4b   Krister Johansen   perf buildid-cach...
350
  	int ns_id = -1;
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
351
  	bool force = false;
8e1e0d746   Ravi Bangoria   perf buildid-cach...
352
353
  	bool list_files = false;
  	bool opts_flag = false;
9a73c3085   Ravi Bangoria   perf buildid-cach...
354
  	bool purge_all = false;
472cc83c3   Arnaldo Carvalho de Melo   perf buildid-cach...
355
  	char const *add_name_list_str = NULL,
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
356
  		   *remove_name_list_str = NULL,
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
357
  		   *purge_name_list_str = NULL,
eeb498454   Namhyung Kim   perf buildid-cach...
358
  		   *missing_filename = NULL,
fc1b691d7   Adrian Hunter   perf buildid-cach...
359
  		   *update_name_list_str = NULL,
eec5a688f   Jiri Olsa   perf buildid cach...
360
  		   *kcore_filename = NULL;
340481ada   Masami Hiramatsu   perf buildid-cach...
361
  	char sbuf[STRERR_BUFSIZE];
eeb498454   Namhyung Kim   perf buildid-cach...
362

8ceb41d7e   Jiri Olsa   perf tools: Renam...
363
  	struct perf_data data = {
e3ed75bb5   Namhyung Kim   perf buildid-cach...
364
365
366
  		.mode  = PERF_DATA_MODE_READ,
  	};
  	struct perf_session *session = NULL;
f045b8c4b   Krister Johansen   perf buildid-cach...
367
  	struct nsinfo *nsi = NULL;
e3ed75bb5   Namhyung Kim   perf buildid-cach...
368

472cc83c3   Arnaldo Carvalho de Melo   perf buildid-cach...
369
370
371
  	const struct option buildid_cache_options[] = {
  	OPT_STRING('a', "add", &add_name_list_str,
  		   "file list", "file(s) to add"),
fc1b691d7   Adrian Hunter   perf buildid-cach...
372
373
  	OPT_STRING('k', "kcore", &kcore_filename,
  		   "file", "kcore file to add"),
472cc83c3   Arnaldo Carvalho de Melo   perf buildid-cach...
374
375
  	OPT_STRING('r', "remove", &remove_name_list_str, "file list",
  		    "file(s) to remove"),
4359dd88a   Thomas-Mich Richter   perf buildid-cach...
376
377
  	OPT_STRING('p', "purge", &purge_name_list_str, "file list",
  		    "file(s) to remove (remove old caches too)"),
9a73c3085   Ravi Bangoria   perf buildid-cach...
378
  	OPT_BOOLEAN('P', "purge-all", &purge_all, "purge all cached files"),
8e1e0d746   Ravi Bangoria   perf buildid-cach...
379
  	OPT_BOOLEAN('l', "list", &list_files, "list all cached files"),
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
380
381
382
  	OPT_STRING('M', "missing", &missing_filename, "file",
  		   "to find missing build ids in the cache"),
  	OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
eeb498454   Namhyung Kim   perf buildid-cach...
383
384
  	OPT_STRING('u', "update", &update_name_list_str, "file list",
  		    "file(s) to update"),
472cc83c3   Arnaldo Carvalho de Melo   perf buildid-cach...
385
  	OPT_INCR('v', "verbose", &verbose, "be more verbose"),
f045b8c4b   Krister Johansen   perf buildid-cach...
386
  	OPT_INTEGER(0, "target-ns", &ns_id, "target pid for namespace context"),
472cc83c3   Arnaldo Carvalho de Melo   perf buildid-cach...
387
388
389
390
391
392
393
394
395
  	OPT_END()
  	};
  	const char * const buildid_cache_usage[] = {
  		"perf buildid-cache [<options>]",
  		NULL
  	};
  
  	argc = parse_options(argc, argv, buildid_cache_options,
  			     buildid_cache_usage, 0);
8e1e0d746   Ravi Bangoria   perf buildid-cach...
396
397
  	opts_flag = add_name_list_str || kcore_filename ||
  		remove_name_list_str || purge_name_list_str ||
9a73c3085   Ravi Bangoria   perf buildid-cach...
398
399
  		missing_filename || update_name_list_str ||
  		purge_all;
8e1e0d746   Ravi Bangoria   perf buildid-cach...
400
401
  
  	if (argc || !(list_files || opts_flag))
0497d0a82   Masami Hiramatsu   perf buildid-cach...
402
  		usage_with_options(buildid_cache_usage, buildid_cache_options);
8e1e0d746   Ravi Bangoria   perf buildid-cach...
403
404
405
406
407
408
  	/* -l is exclusive. It can not be used with other options. */
  	if (list_files && opts_flag) {
  		usage_with_options_msg(buildid_cache_usage,
  			buildid_cache_options, "-l is exclusive.
  ");
  	}
f045b8c4b   Krister Johansen   perf buildid-cach...
409
410
  	if (ns_id > 0)
  		nsi = nsinfo__new(ns_id);
e3ed75bb5   Namhyung Kim   perf buildid-cach...
411
  	if (missing_filename) {
2d4f27999   Jiri Olsa   perf data: Add gl...
412
413
  		data.path  = missing_filename;
  		data.force = force;
e3ed75bb5   Namhyung Kim   perf buildid-cach...
414

8ceb41d7e   Jiri Olsa   perf tools: Renam...
415
  		session = perf_session__new(&data, false, NULL);
6ef81c55a   Mamatha Inamdar   perf session: Ret...
416
417
  		if (IS_ERR(session))
  			return PTR_ERR(session);
e3ed75bb5   Namhyung Kim   perf buildid-cach...
418
  	}
0a7e6d1b6   Namhyung Kim   perf tools: Check...
419
  	if (symbol__init(session ? &session->header.env : NULL) < 0)
e3ed75bb5   Namhyung Kim   perf buildid-cach...
420
  		goto out;
472cc83c3   Arnaldo Carvalho de Melo   perf buildid-cach...
421
422
  
  	setup_pager();
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
423

8e1e0d746   Ravi Bangoria   perf buildid-cach...
424
425
426
427
  	if (list_files) {
  		ret = build_id_cache__show_all();
  		goto out;
  	}
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
428
  	if (add_name_list_str) {
4a77e2183   Arnaldo Carvalho de Melo   perf strlist: Mak...
429
  		list = strlist__new(add_name_list_str, NULL);
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
430
  		if (list) {
602a1f4da   Arnaldo Carvalho de Melo   perf tools: Renam...
431
  			strlist__for_each_entry(pos, list)
f045b8c4b   Krister Johansen   perf buildid-cach...
432
  				if (build_id_cache__add_file(pos->s, nsi)) {
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
433
434
435
436
437
438
439
440
  					if (errno == EEXIST) {
  						pr_debug("%s already in the cache
  ",
  							 pos->s);
  						continue;
  					}
  					pr_warning("Couldn't add %s: %s
  ",
c8b5f2c96   Arnaldo Carvalho de Melo   tools: Introduce ...
441
  						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
442
443
444
445
446
447
448
  				}
  
  			strlist__delete(list);
  		}
  	}
  
  	if (remove_name_list_str) {
4a77e2183   Arnaldo Carvalho de Melo   perf strlist: Mak...
449
  		list = strlist__new(remove_name_list_str, NULL);
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
450
  		if (list) {
602a1f4da   Arnaldo Carvalho de Melo   perf tools: Renam...
451
  			strlist__for_each_entry(pos, list)
f045b8c4b   Krister Johansen   perf buildid-cach...
452
  				if (build_id_cache__remove_file(pos->s, nsi)) {
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
453
454
455
456
457
458
459
460
  					if (errno == ENOENT) {
  						pr_debug("%s wasn't in the cache
  ",
  							 pos->s);
  						continue;
  					}
  					pr_warning("Couldn't remove %s: %s
  ",
c8b5f2c96   Arnaldo Carvalho de Melo   tools: Introduce ...
461
  						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
462
463
464
465
466
  				}
  
  			strlist__delete(list);
  		}
  	}
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
467
  	if (purge_name_list_str) {
4a77e2183   Arnaldo Carvalho de Melo   perf strlist: Mak...
468
  		list = strlist__new(purge_name_list_str, NULL);
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
469
  		if (list) {
602a1f4da   Arnaldo Carvalho de Melo   perf tools: Renam...
470
  			strlist__for_each_entry(pos, list)
f045b8c4b   Krister Johansen   perf buildid-cach...
471
  				if (build_id_cache__purge_path(pos->s, nsi)) {
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
472
473
474
475
476
477
478
479
  					if (errno == ENOENT) {
  						pr_debug("%s wasn't in the cache
  ",
  							 pos->s);
  						continue;
  					}
  					pr_warning("Couldn't remove %s: %s
  ",
c8b5f2c96   Arnaldo Carvalho de Melo   tools: Introduce ...
480
  						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
8d8c8e4cb   Masami Hiramatsu   perf buildid-cach...
481
482
483
484
485
  				}
  
  			strlist__delete(list);
  		}
  	}
d8ed87bc1   Ravi Bangoria   perf buildid-cach...
486
487
488
489
490
491
492
  	if (purge_all) {
  		if (build_id_cache__purge_all()) {
  			pr_warning("Couldn't remove some caches. Error: %s.
  ",
  				str_error_r(errno, sbuf, sizeof(sbuf)));
  		}
  	}
9a73c3085   Ravi Bangoria   perf buildid-cach...
493

fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
494
  	if (missing_filename)
e3ed75bb5   Namhyung Kim   perf buildid-cach...
495
  		ret = build_id_cache__fprintf_missing(session, stdout);
fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
496

eeb498454   Namhyung Kim   perf buildid-cach...
497
  	if (update_name_list_str) {
4a77e2183   Arnaldo Carvalho de Melo   perf strlist: Mak...
498
  		list = strlist__new(update_name_list_str, NULL);
eeb498454   Namhyung Kim   perf buildid-cach...
499
  		if (list) {
602a1f4da   Arnaldo Carvalho de Melo   perf tools: Renam...
500
  			strlist__for_each_entry(pos, list)
f045b8c4b   Krister Johansen   perf buildid-cach...
501
  				if (build_id_cache__update_file(pos->s, nsi)) {
eeb498454   Namhyung Kim   perf buildid-cach...
502
503
504
505
506
507
508
509
  					if (errno == ENOENT) {
  						pr_debug("%s wasn't in the cache
  ",
  							 pos->s);
  						continue;
  					}
  					pr_warning("Couldn't update %s: %s
  ",
c8b5f2c96   Arnaldo Carvalho de Melo   tools: Introduce ...
510
  						   pos->s, str_error_r(errno, sbuf, sizeof(sbuf)));
eeb498454   Namhyung Kim   perf buildid-cach...
511
512
513
514
515
  				}
  
  			strlist__delete(list);
  		}
  	}
e35f7362b   Masami Hiramatsu   perf buildid-cach...
516
  	if (kcore_filename && build_id_cache__add_kcore(kcore_filename, force))
fc1b691d7   Adrian Hunter   perf buildid-cach...
517
518
  		pr_warning("Couldn't add %s
  ", kcore_filename);
e3ed75bb5   Namhyung Kim   perf buildid-cach...
519
  out:
e1446551e   Arnaldo Carvalho de Melo   perf session: Des...
520
  	perf_session__delete(session);
f045b8c4b   Krister Johansen   perf buildid-cach...
521
  	nsinfo__zput(nsi);
e3ed75bb5   Namhyung Kim   perf buildid-cach...
522

fbb6976c2   Arnaldo Carvalho de Melo   perf buildid-cach...
523
  	return ret;
ef12a1413   Arnaldo Carvalho de Melo   perf buildid-cach...
524
  }