Commit | Line | Data |
---|---|---|
abaff32a | 1 | /* |
bf9e1876 IM |
2 | * builtin-record.c |
3 | * | |
4 | * Builtin record command: Record the profile of a workload | |
5 | * (or a CPU, or a PID) into the perf.data output file - for | |
6 | * later analysis via perf report. | |
abaff32a | 7 | */ |
16f762a2 | 8 | #include "builtin.h" |
bf9e1876 IM |
9 | |
10 | #include "perf.h" | |
11 | ||
6122e4e4 | 12 | #include "util/build-id.h" |
6eda5838 | 13 | #include "util/util.h" |
4b6ab94e | 14 | #include <subcmd/parse-options.h> |
8ad8db37 | 15 | #include "util/parse-events.h" |
6eda5838 | 16 | |
8f651eae | 17 | #include "util/callchain.h" |
f14d5707 | 18 | #include "util/cgroup.h" |
7c6a1c65 | 19 | #include "util/header.h" |
66e274f3 | 20 | #include "util/event.h" |
361c99a6 | 21 | #include "util/evlist.h" |
69aad6f1 | 22 | #include "util/evsel.h" |
8f28827a | 23 | #include "util/debug.h" |
94c744b6 | 24 | #include "util/session.h" |
45694aa7 | 25 | #include "util/tool.h" |
8d06367f | 26 | #include "util/symbol.h" |
a12b51c4 | 27 | #include "util/cpumap.h" |
fd78260b | 28 | #include "util/thread_map.h" |
f5fc1412 | 29 | #include "util/data.h" |
bcc84ec6 | 30 | #include "util/perf_regs.h" |
ef149c25 | 31 | #include "util/auxtrace.h" |
46bc29b9 | 32 | #include "util/tsc.h" |
f00898f4 | 33 | #include "util/parse-branch-options.h" |
bcc84ec6 | 34 | #include "util/parse-regs-options.h" |
71dc2326 | 35 | #include "util/llvm-utils.h" |
8690a2a7 | 36 | #include "util/bpf-loader.h" |
d8871ea7 | 37 | #include "asm/bug.h" |
7c6a1c65 | 38 | |
97124d5e | 39 | #include <unistd.h> |
de9ac07b | 40 | #include <sched.h> |
a41794cd | 41 | #include <sys/mman.h> |
de9ac07b | 42 | |
78da39fa | 43 | |
8c6f45a7 | 44 | struct record { |
45694aa7 | 45 | struct perf_tool tool; |
b4006796 | 46 | struct record_opts opts; |
d20deb64 | 47 | u64 bytes_written; |
f5fc1412 | 48 | struct perf_data_file file; |
ef149c25 | 49 | struct auxtrace_record *itr; |
d20deb64 ACM |
50 | struct perf_evlist *evlist; |
51 | struct perf_session *session; | |
52 | const char *progname; | |
d20deb64 | 53 | int realtime_prio; |
d20deb64 | 54 | bool no_buildid; |
d2db9a98 | 55 | bool no_buildid_set; |
d20deb64 | 56 | bool no_buildid_cache; |
d2db9a98 | 57 | bool no_buildid_cache_set; |
6156681b | 58 | bool buildid_all; |
9f065194 | 59 | unsigned long long samples; |
0f82ebc4 | 60 | }; |
a21ca2ca | 61 | |
8c6f45a7 | 62 | static int record__write(struct record *rec, void *bf, size_t size) |
f5970550 | 63 | { |
cf8b2e69 | 64 | if (perf_data_file__write(rec->session->file, bf, size) < 0) { |
50a9b868 JO |
65 | pr_err("failed to write perf data, error: %m\n"); |
66 | return -1; | |
f5970550 | 67 | } |
8d3eca20 | 68 | |
cf8b2e69 | 69 | rec->bytes_written += size; |
8d3eca20 | 70 | return 0; |
f5970550 PZ |
71 | } |
72 | ||
45694aa7 | 73 | static int process_synthesized_event(struct perf_tool *tool, |
d20deb64 | 74 | union perf_event *event, |
1d037ca1 IT |
75 | struct perf_sample *sample __maybe_unused, |
76 | struct machine *machine __maybe_unused) | |
234fbbf5 | 77 | { |
8c6f45a7 ACM |
78 | struct record *rec = container_of(tool, struct record, tool); |
79 | return record__write(rec, event, event->header.size); | |
234fbbf5 ACM |
80 | } |
81 | ||
e5685730 | 82 | static int record__mmap_read(struct record *rec, int idx) |
de9ac07b | 83 | { |
e5685730 | 84 | struct perf_mmap *md = &rec->evlist->mmap[idx]; |
7b8283b5 DA |
85 | u64 head = perf_mmap__read_head(md); |
86 | u64 old = md->prev; | |
918512b4 | 87 | unsigned char *data = md->base + page_size; |
de9ac07b PZ |
88 | unsigned long size; |
89 | void *buf; | |
8d3eca20 | 90 | int rc = 0; |
de9ac07b | 91 | |
dc82009a | 92 | if (old == head) |
8d3eca20 | 93 | return 0; |
dc82009a | 94 | |
d20deb64 | 95 | rec->samples++; |
de9ac07b PZ |
96 | |
97 | size = head - old; | |
98 | ||
99 | if ((old & md->mask) + size != (head & md->mask)) { | |
100 | buf = &data[old & md->mask]; | |
101 | size = md->mask + 1 - (old & md->mask); | |
102 | old += size; | |
021e9f47 | 103 | |
8c6f45a7 | 104 | if (record__write(rec, buf, size) < 0) { |
8d3eca20 DA |
105 | rc = -1; |
106 | goto out; | |
107 | } | |
de9ac07b PZ |
108 | } |
109 | ||
110 | buf = &data[old & md->mask]; | |
111 | size = head - old; | |
112 | old += size; | |
021e9f47 | 113 | |
8c6f45a7 | 114 | if (record__write(rec, buf, size) < 0) { |
8d3eca20 DA |
115 | rc = -1; |
116 | goto out; | |
117 | } | |
de9ac07b PZ |
118 | |
119 | md->prev = old; | |
e5685730 | 120 | perf_evlist__mmap_consume(rec->evlist, idx); |
8d3eca20 DA |
121 | out: |
122 | return rc; | |
de9ac07b PZ |
123 | } |
124 | ||
2dd6d8a1 AH |
125 | static volatile int done; |
126 | static volatile int signr = -1; | |
127 | static volatile int child_finished; | |
c0bdc1c4 WN |
128 | |
129 | static volatile enum { | |
130 | AUXTRACE_SNAPSHOT_OFF = -1, | |
131 | AUXTRACE_SNAPSHOT_DISABLED = 0, | |
132 | AUXTRACE_SNAPSHOT_ENABLED = 1, | |
133 | } auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_OFF; | |
134 | ||
135 | static inline void | |
136 | auxtrace_snapshot_on(void) | |
137 | { | |
138 | auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_DISABLED; | |
139 | } | |
140 | ||
141 | static inline void | |
142 | auxtrace_snapshot_enable(void) | |
143 | { | |
144 | if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF) | |
145 | return; | |
146 | auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_ENABLED; | |
147 | } | |
148 | ||
149 | static inline void | |
150 | auxtrace_snapshot_disable(void) | |
151 | { | |
152 | if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF) | |
153 | return; | |
154 | auxtrace_snapshot_state = AUXTRACE_SNAPSHOT_DISABLED; | |
155 | } | |
156 | ||
157 | static inline bool | |
158 | auxtrace_snapshot_is_enabled(void) | |
159 | { | |
160 | if (auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_OFF) | |
161 | return false; | |
162 | return auxtrace_snapshot_state == AUXTRACE_SNAPSHOT_ENABLED; | |
163 | } | |
164 | ||
2dd6d8a1 AH |
165 | static volatile int auxtrace_snapshot_err; |
166 | static volatile int auxtrace_record__snapshot_started; | |
167 | ||
168 | static void sig_handler(int sig) | |
169 | { | |
170 | if (sig == SIGCHLD) | |
171 | child_finished = 1; | |
172 | else | |
173 | signr = sig; | |
174 | ||
175 | done = 1; | |
176 | } | |
177 | ||
178 | static void record__sig_exit(void) | |
179 | { | |
180 | if (signr == -1) | |
181 | return; | |
182 | ||
183 | signal(signr, SIG_DFL); | |
184 | raise(signr); | |
185 | } | |
186 | ||
e31f0d01 AH |
187 | #ifdef HAVE_AUXTRACE_SUPPORT |
188 | ||
ef149c25 AH |
189 | static int record__process_auxtrace(struct perf_tool *tool, |
190 | union perf_event *event, void *data1, | |
191 | size_t len1, void *data2, size_t len2) | |
192 | { | |
193 | struct record *rec = container_of(tool, struct record, tool); | |
99fa2984 | 194 | struct perf_data_file *file = &rec->file; |
ef149c25 AH |
195 | size_t padding; |
196 | u8 pad[8] = {0}; | |
197 | ||
99fa2984 AH |
198 | if (!perf_data_file__is_pipe(file)) { |
199 | off_t file_offset; | |
200 | int fd = perf_data_file__fd(file); | |
201 | int err; | |
202 | ||
203 | file_offset = lseek(fd, 0, SEEK_CUR); | |
204 | if (file_offset == -1) | |
205 | return -1; | |
206 | err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, | |
207 | event, file_offset); | |
208 | if (err) | |
209 | return err; | |
210 | } | |
211 | ||
ef149c25 AH |
212 | /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ |
213 | padding = (len1 + len2) & 7; | |
214 | if (padding) | |
215 | padding = 8 - padding; | |
216 | ||
217 | record__write(rec, event, event->header.size); | |
218 | record__write(rec, data1, len1); | |
219 | if (len2) | |
220 | record__write(rec, data2, len2); | |
221 | record__write(rec, &pad, padding); | |
222 | ||
223 | return 0; | |
224 | } | |
225 | ||
226 | static int record__auxtrace_mmap_read(struct record *rec, | |
227 | struct auxtrace_mmap *mm) | |
228 | { | |
229 | int ret; | |
230 | ||
231 | ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool, | |
232 | record__process_auxtrace); | |
233 | if (ret < 0) | |
234 | return ret; | |
235 | ||
236 | if (ret) | |
237 | rec->samples++; | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
2dd6d8a1 AH |
242 | static int record__auxtrace_mmap_read_snapshot(struct record *rec, |
243 | struct auxtrace_mmap *mm) | |
244 | { | |
245 | int ret; | |
246 | ||
247 | ret = auxtrace_mmap__read_snapshot(mm, rec->itr, &rec->tool, | |
248 | record__process_auxtrace, | |
249 | rec->opts.auxtrace_snapshot_size); | |
250 | if (ret < 0) | |
251 | return ret; | |
252 | ||
253 | if (ret) | |
254 | rec->samples++; | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
259 | static int record__auxtrace_read_snapshot_all(struct record *rec) | |
260 | { | |
261 | int i; | |
262 | int rc = 0; | |
263 | ||
264 | for (i = 0; i < rec->evlist->nr_mmaps; i++) { | |
265 | struct auxtrace_mmap *mm = | |
266 | &rec->evlist->mmap[i].auxtrace_mmap; | |
267 | ||
268 | if (!mm->base) | |
269 | continue; | |
270 | ||
271 | if (record__auxtrace_mmap_read_snapshot(rec, mm) != 0) { | |
272 | rc = -1; | |
273 | goto out; | |
274 | } | |
275 | } | |
276 | out: | |
277 | return rc; | |
278 | } | |
279 | ||
280 | static void record__read_auxtrace_snapshot(struct record *rec) | |
281 | { | |
282 | pr_debug("Recording AUX area tracing snapshot\n"); | |
283 | if (record__auxtrace_read_snapshot_all(rec) < 0) { | |
284 | auxtrace_snapshot_err = -1; | |
285 | } else { | |
286 | auxtrace_snapshot_err = auxtrace_record__snapshot_finish(rec->itr); | |
287 | if (!auxtrace_snapshot_err) | |
c0bdc1c4 | 288 | auxtrace_snapshot_enable(); |
2dd6d8a1 AH |
289 | } |
290 | } | |
291 | ||
e31f0d01 AH |
292 | #else |
293 | ||
294 | static inline | |
295 | int record__auxtrace_mmap_read(struct record *rec __maybe_unused, | |
296 | struct auxtrace_mmap *mm __maybe_unused) | |
297 | { | |
298 | return 0; | |
299 | } | |
300 | ||
2dd6d8a1 AH |
301 | static inline |
302 | void record__read_auxtrace_snapshot(struct record *rec __maybe_unused) | |
de9ac07b | 303 | { |
f7b7c26e PZ |
304 | } |
305 | ||
2dd6d8a1 AH |
306 | static inline |
307 | int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) | |
f7b7c26e | 308 | { |
2dd6d8a1 | 309 | return 0; |
de9ac07b PZ |
310 | } |
311 | ||
2dd6d8a1 AH |
312 | #endif |
313 | ||
8c6f45a7 | 314 | static int record__open(struct record *rec) |
dd7927f4 | 315 | { |
56e52e85 | 316 | char msg[512]; |
6a4bb04c | 317 | struct perf_evsel *pos; |
d20deb64 ACM |
318 | struct perf_evlist *evlist = rec->evlist; |
319 | struct perf_session *session = rec->session; | |
b4006796 | 320 | struct record_opts *opts = &rec->opts; |
8d3eca20 | 321 | int rc = 0; |
dd7927f4 | 322 | |
e68ae9cf | 323 | perf_evlist__config(evlist, opts, &callchain_param); |
cac21425 | 324 | |
0050f7aa | 325 | evlist__for_each(evlist, pos) { |
dd7927f4 | 326 | try_again: |
d988d5ee | 327 | if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) { |
56e52e85 | 328 | if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { |
d6d901c2 | 329 | if (verbose) |
c0a54341 | 330 | ui__warning("%s\n", msg); |
d6d901c2 ZY |
331 | goto try_again; |
332 | } | |
ca6a4258 | 333 | |
56e52e85 ACM |
334 | rc = -errno; |
335 | perf_evsel__open_strerror(pos, &opts->target, | |
336 | errno, msg, sizeof(msg)); | |
337 | ui__error("%s\n", msg); | |
8d3eca20 | 338 | goto out; |
c171b552 LZ |
339 | } |
340 | } | |
a43d3f08 | 341 | |
23d4aad4 ACM |
342 | if (perf_evlist__apply_filters(evlist, &pos)) { |
343 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", | |
344 | pos->filter, perf_evsel__name(pos), errno, | |
35550da3 | 345 | strerror_r(errno, msg, sizeof(msg))); |
8d3eca20 DA |
346 | rc = -1; |
347 | goto out; | |
0a102479 FW |
348 | } |
349 | ||
ef149c25 | 350 | if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false, |
2dd6d8a1 AH |
351 | opts->auxtrace_mmap_pages, |
352 | opts->auxtrace_snapshot_mode) < 0) { | |
8d3eca20 DA |
353 | if (errno == EPERM) { |
354 | pr_err("Permission error mapping pages.\n" | |
355 | "Consider increasing " | |
356 | "/proc/sys/kernel/perf_event_mlock_kb,\n" | |
357 | "or try again with a smaller value of -m/--mmap_pages.\n" | |
ef149c25 AH |
358 | "(current value: %u,%u)\n", |
359 | opts->mmap_pages, opts->auxtrace_mmap_pages); | |
8d3eca20 | 360 | rc = -errno; |
8d3eca20 | 361 | } else { |
35550da3 MH |
362 | pr_err("failed to mmap with %d (%s)\n", errno, |
363 | strerror_r(errno, msg, sizeof(msg))); | |
95c36561 WN |
364 | if (errno) |
365 | rc = -errno; | |
366 | else | |
367 | rc = -EINVAL; | |
8d3eca20 DA |
368 | } |
369 | goto out; | |
18e60939 | 370 | } |
0a27d7f9 | 371 | |
563aecb2 | 372 | session->evlist = evlist; |
7b56cce2 | 373 | perf_session__set_id_hdr_size(session); |
8d3eca20 DA |
374 | out: |
375 | return rc; | |
16c8a109 PZ |
376 | } |
377 | ||
e3d59112 NK |
378 | static int process_sample_event(struct perf_tool *tool, |
379 | union perf_event *event, | |
380 | struct perf_sample *sample, | |
381 | struct perf_evsel *evsel, | |
382 | struct machine *machine) | |
383 | { | |
384 | struct record *rec = container_of(tool, struct record, tool); | |
385 | ||
386 | rec->samples++; | |
387 | ||
388 | return build_id__mark_dso_hit(tool, event, sample, evsel, machine); | |
389 | } | |
390 | ||
8c6f45a7 | 391 | static int process_buildids(struct record *rec) |
6122e4e4 | 392 | { |
f5fc1412 JO |
393 | struct perf_data_file *file = &rec->file; |
394 | struct perf_session *session = rec->session; | |
6122e4e4 | 395 | |
457ae94a | 396 | if (file->size == 0) |
9f591fd7 ACM |
397 | return 0; |
398 | ||
00dc8657 NK |
399 | /* |
400 | * During this process, it'll load kernel map and replace the | |
401 | * dso->long_name to a real pathname it found. In this case | |
402 | * we prefer the vmlinux path like | |
403 | * /lib/modules/3.16.4/build/vmlinux | |
404 | * | |
405 | * rather than build-id path (in debug directory). | |
406 | * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 | |
407 | */ | |
408 | symbol_conf.ignore_vmlinux_buildid = true; | |
409 | ||
6156681b NK |
410 | /* |
411 | * If --buildid-all is given, it marks all DSO regardless of hits, | |
412 | * so no need to process samples. | |
413 | */ | |
414 | if (rec->buildid_all) | |
415 | rec->tool.sample = NULL; | |
416 | ||
b7b61cbe | 417 | return perf_session__process_events(session); |
6122e4e4 ACM |
418 | } |
419 | ||
8115d60c | 420 | static void perf_event__synthesize_guest_os(struct machine *machine, void *data) |
a1645ce1 ZY |
421 | { |
422 | int err; | |
45694aa7 | 423 | struct perf_tool *tool = data; |
a1645ce1 ZY |
424 | /* |
425 | *As for guest kernel when processing subcommand record&report, | |
426 | *we arrange module mmap prior to guest kernel mmap and trigger | |
427 | *a preload dso because default guest module symbols are loaded | |
428 | *from guest kallsyms instead of /lib/modules/XXX/XXX. This | |
429 | *method is used to avoid symbol missing when the first addr is | |
430 | *in module instead of in guest kernel. | |
431 | */ | |
45694aa7 | 432 | err = perf_event__synthesize_modules(tool, process_synthesized_event, |
743eb868 | 433 | machine); |
a1645ce1 ZY |
434 | if (err < 0) |
435 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 436 | " relocation symbol.\n", machine->pid); |
a1645ce1 | 437 | |
a1645ce1 ZY |
438 | /* |
439 | * We use _stext for guest kernel because guest kernel's /proc/kallsyms | |
440 | * have no _text sometimes. | |
441 | */ | |
45694aa7 | 442 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, |
0ae617be | 443 | machine); |
a1645ce1 ZY |
444 | if (err < 0) |
445 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 446 | " relocation symbol.\n", machine->pid); |
a1645ce1 ZY |
447 | } |
448 | ||
98402807 FW |
449 | static struct perf_event_header finished_round_event = { |
450 | .size = sizeof(struct perf_event_header), | |
451 | .type = PERF_RECORD_FINISHED_ROUND, | |
452 | }; | |
453 | ||
8c6f45a7 | 454 | static int record__mmap_read_all(struct record *rec) |
98402807 | 455 | { |
dcabb507 | 456 | u64 bytes_written = rec->bytes_written; |
0e2e63dd | 457 | int i; |
8d3eca20 | 458 | int rc = 0; |
98402807 | 459 | |
d20deb64 | 460 | for (i = 0; i < rec->evlist->nr_mmaps; i++) { |
ef149c25 AH |
461 | struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap; |
462 | ||
8d3eca20 | 463 | if (rec->evlist->mmap[i].base) { |
e5685730 | 464 | if (record__mmap_read(rec, i) != 0) { |
8d3eca20 DA |
465 | rc = -1; |
466 | goto out; | |
467 | } | |
468 | } | |
ef149c25 | 469 | |
2dd6d8a1 | 470 | if (mm->base && !rec->opts.auxtrace_snapshot_mode && |
ef149c25 AH |
471 | record__auxtrace_mmap_read(rec, mm) != 0) { |
472 | rc = -1; | |
473 | goto out; | |
474 | } | |
98402807 FW |
475 | } |
476 | ||
dcabb507 JO |
477 | /* |
478 | * Mark the round finished in case we wrote | |
479 | * at least one event. | |
480 | */ | |
481 | if (bytes_written != rec->bytes_written) | |
482 | rc = record__write(rec, &finished_round_event, sizeof(finished_round_event)); | |
8d3eca20 DA |
483 | |
484 | out: | |
485 | return rc; | |
98402807 FW |
486 | } |
487 | ||
8c6f45a7 | 488 | static void record__init_features(struct record *rec) |
57706abc | 489 | { |
57706abc DA |
490 | struct perf_session *session = rec->session; |
491 | int feat; | |
492 | ||
493 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) | |
494 | perf_header__set_feat(&session->header, feat); | |
495 | ||
496 | if (rec->no_buildid) | |
497 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); | |
498 | ||
3e2be2da | 499 | if (!have_tracepoints(&rec->evlist->entries)) |
57706abc DA |
500 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); |
501 | ||
502 | if (!rec->opts.branch_stack) | |
503 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); | |
ef149c25 AH |
504 | |
505 | if (!rec->opts.full_auxtrace) | |
506 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); | |
ffa517ad JO |
507 | |
508 | perf_header__clear_feat(&session->header, HEADER_STAT); | |
57706abc DA |
509 | } |
510 | ||
e1ab48ba WN |
511 | static void |
512 | record__finish_output(struct record *rec) | |
513 | { | |
514 | struct perf_data_file *file = &rec->file; | |
515 | int fd = perf_data_file__fd(file); | |
516 | ||
517 | if (file->is_pipe) | |
518 | return; | |
519 | ||
520 | rec->session->header.data_size += rec->bytes_written; | |
521 | file->size = lseek(perf_data_file__fd(file), 0, SEEK_CUR); | |
522 | ||
523 | if (!rec->no_buildid) { | |
524 | process_buildids(rec); | |
525 | ||
526 | if (rec->buildid_all) | |
527 | dsos__hit_all(rec->session); | |
528 | } | |
529 | perf_session__write_header(rec->session, rec->evlist, fd, true); | |
530 | ||
531 | return; | |
532 | } | |
533 | ||
f33cbe72 ACM |
534 | static volatile int workload_exec_errno; |
535 | ||
536 | /* | |
537 | * perf_evlist__prepare_workload will send a SIGUSR1 | |
538 | * if the fork fails, since we asked by setting its | |
539 | * want_signal to true. | |
540 | */ | |
45604710 NK |
541 | static void workload_exec_failed_signal(int signo __maybe_unused, |
542 | siginfo_t *info, | |
f33cbe72 ACM |
543 | void *ucontext __maybe_unused) |
544 | { | |
545 | workload_exec_errno = info->si_value.sival_int; | |
546 | done = 1; | |
f33cbe72 ACM |
547 | child_finished = 1; |
548 | } | |
549 | ||
2dd6d8a1 AH |
550 | static void snapshot_sig_handler(int sig); |
551 | ||
46bc29b9 AH |
552 | int __weak |
553 | perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused, | |
554 | struct perf_tool *tool __maybe_unused, | |
555 | perf_event__handler_t process __maybe_unused, | |
556 | struct machine *machine __maybe_unused) | |
557 | { | |
558 | return 0; | |
559 | } | |
560 | ||
c45c86eb WN |
561 | static int record__synthesize(struct record *rec) |
562 | { | |
563 | struct perf_session *session = rec->session; | |
564 | struct machine *machine = &session->machines.host; | |
565 | struct perf_data_file *file = &rec->file; | |
566 | struct record_opts *opts = &rec->opts; | |
567 | struct perf_tool *tool = &rec->tool; | |
568 | int fd = perf_data_file__fd(file); | |
569 | int err = 0; | |
570 | ||
571 | if (file->is_pipe) { | |
572 | err = perf_event__synthesize_attrs(tool, session, | |
573 | process_synthesized_event); | |
574 | if (err < 0) { | |
575 | pr_err("Couldn't synthesize attrs.\n"); | |
576 | goto out; | |
577 | } | |
578 | ||
579 | if (have_tracepoints(&rec->evlist->entries)) { | |
580 | /* | |
581 | * FIXME err <= 0 here actually means that | |
582 | * there were no tracepoints so its not really | |
583 | * an error, just that we don't need to | |
584 | * synthesize anything. We really have to | |
585 | * return this more properly and also | |
586 | * propagate errors that now are calling die() | |
587 | */ | |
588 | err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, | |
589 | process_synthesized_event); | |
590 | if (err <= 0) { | |
591 | pr_err("Couldn't record tracing data.\n"); | |
592 | goto out; | |
593 | } | |
594 | rec->bytes_written += err; | |
595 | } | |
596 | } | |
597 | ||
46bc29b9 AH |
598 | err = perf_event__synth_time_conv(rec->evlist->mmap[0].base, tool, |
599 | process_synthesized_event, machine); | |
600 | if (err) | |
601 | goto out; | |
602 | ||
c45c86eb WN |
603 | if (rec->opts.full_auxtrace) { |
604 | err = perf_event__synthesize_auxtrace_info(rec->itr, tool, | |
605 | session, process_synthesized_event); | |
606 | if (err) | |
607 | goto out; | |
608 | } | |
609 | ||
610 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, | |
611 | machine); | |
612 | WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" | |
613 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
614 | "Check /proc/kallsyms permission or run as root.\n"); | |
615 | ||
616 | err = perf_event__synthesize_modules(tool, process_synthesized_event, | |
617 | machine); | |
618 | WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" | |
619 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
620 | "Check /proc/modules permission or run as root.\n"); | |
621 | ||
622 | if (perf_guest) { | |
623 | machines__process_guests(&session->machines, | |
624 | perf_event__synthesize_guest_os, tool); | |
625 | } | |
626 | ||
627 | err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, | |
628 | process_synthesized_event, opts->sample_address, | |
629 | opts->proc_map_timeout); | |
630 | out: | |
631 | return err; | |
632 | } | |
633 | ||
8c6f45a7 | 634 | static int __cmd_record(struct record *rec, int argc, const char **argv) |
16c8a109 | 635 | { |
57706abc | 636 | int err; |
45604710 | 637 | int status = 0; |
8b412664 | 638 | unsigned long waking = 0; |
46be604b | 639 | const bool forks = argc > 0; |
23346f21 | 640 | struct machine *machine; |
45694aa7 | 641 | struct perf_tool *tool = &rec->tool; |
b4006796 | 642 | struct record_opts *opts = &rec->opts; |
f5fc1412 | 643 | struct perf_data_file *file = &rec->file; |
d20deb64 | 644 | struct perf_session *session; |
6dcf45ef | 645 | bool disabled = false, draining = false; |
42aa276f | 646 | int fd; |
de9ac07b | 647 | |
d20deb64 | 648 | rec->progname = argv[0]; |
33e49ea7 | 649 | |
45604710 | 650 | atexit(record__sig_exit); |
f5970550 PZ |
651 | signal(SIGCHLD, sig_handler); |
652 | signal(SIGINT, sig_handler); | |
804f7ac7 | 653 | signal(SIGTERM, sig_handler); |
c0bdc1c4 WN |
654 | |
655 | if (rec->opts.auxtrace_snapshot_mode) { | |
2dd6d8a1 | 656 | signal(SIGUSR2, snapshot_sig_handler); |
c0bdc1c4 WN |
657 | auxtrace_snapshot_on(); |
658 | } else { | |
2dd6d8a1 | 659 | signal(SIGUSR2, SIG_IGN); |
c0bdc1c4 | 660 | } |
f5970550 | 661 | |
b7b61cbe | 662 | session = perf_session__new(file, false, tool); |
94c744b6 | 663 | if (session == NULL) { |
ffa91880 | 664 | pr_err("Perf session creation failed.\n"); |
a9a70bbc ACM |
665 | return -1; |
666 | } | |
667 | ||
42aa276f | 668 | fd = perf_data_file__fd(file); |
d20deb64 ACM |
669 | rec->session = session; |
670 | ||
8c6f45a7 | 671 | record__init_features(rec); |
330aa675 | 672 | |
d4db3f16 | 673 | if (forks) { |
3e2be2da | 674 | err = perf_evlist__prepare_workload(rec->evlist, &opts->target, |
f5fc1412 | 675 | argv, file->is_pipe, |
735f7e0b | 676 | workload_exec_failed_signal); |
35b9d88e ACM |
677 | if (err < 0) { |
678 | pr_err("Couldn't run the workload!\n"); | |
45604710 | 679 | status = err; |
35b9d88e | 680 | goto out_delete_session; |
856e9660 | 681 | } |
856e9660 PZ |
682 | } |
683 | ||
8c6f45a7 | 684 | if (record__open(rec) != 0) { |
8d3eca20 | 685 | err = -1; |
45604710 | 686 | goto out_child; |
8d3eca20 | 687 | } |
de9ac07b | 688 | |
8690a2a7 WN |
689 | err = bpf__apply_obj_config(); |
690 | if (err) { | |
691 | char errbuf[BUFSIZ]; | |
692 | ||
693 | bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); | |
694 | pr_err("ERROR: Apply config to BPF failed: %s\n", | |
695 | errbuf); | |
696 | goto out_child; | |
697 | } | |
698 | ||
cca8482c AH |
699 | /* |
700 | * Normally perf_session__new would do this, but it doesn't have the | |
701 | * evlist. | |
702 | */ | |
703 | if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) { | |
704 | pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); | |
705 | rec->tool.ordered_events = false; | |
706 | } | |
707 | ||
3e2be2da | 708 | if (!rec->evlist->nr_groups) |
a8bb559b NK |
709 | perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); |
710 | ||
f5fc1412 | 711 | if (file->is_pipe) { |
42aa276f | 712 | err = perf_header__write_pipe(fd); |
529870e3 | 713 | if (err < 0) |
45604710 | 714 | goto out_child; |
563aecb2 | 715 | } else { |
42aa276f | 716 | err = perf_session__write_header(session, rec->evlist, fd, false); |
d5eed904 | 717 | if (err < 0) |
45604710 | 718 | goto out_child; |
56b03f3c ACM |
719 | } |
720 | ||
d3665498 | 721 | if (!rec->no_buildid |
e20960c0 | 722 | && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { |
d3665498 | 723 | pr_err("Couldn't generate buildids. " |
e20960c0 | 724 | "Use --no-buildid to profile anyway.\n"); |
8d3eca20 | 725 | err = -1; |
45604710 | 726 | goto out_child; |
e20960c0 RR |
727 | } |
728 | ||
34ba5122 | 729 | machine = &session->machines.host; |
743eb868 | 730 | |
c45c86eb WN |
731 | err = record__synthesize(rec); |
732 | if (err < 0) | |
45604710 | 733 | goto out_child; |
8d3eca20 | 734 | |
d20deb64 | 735 | if (rec->realtime_prio) { |
de9ac07b PZ |
736 | struct sched_param param; |
737 | ||
d20deb64 | 738 | param.sched_priority = rec->realtime_prio; |
de9ac07b | 739 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
6beba7ad | 740 | pr_err("Could not set realtime priority.\n"); |
8d3eca20 | 741 | err = -1; |
45604710 | 742 | goto out_child; |
de9ac07b PZ |
743 | } |
744 | } | |
745 | ||
774cb499 JO |
746 | /* |
747 | * When perf is starting the traced process, all the events | |
748 | * (apart from group members) have enable_on_exec=1 set, | |
749 | * so don't spoil it by prematurely enabling them. | |
750 | */ | |
6619a53e | 751 | if (!target__none(&opts->target) && !opts->initial_delay) |
3e2be2da | 752 | perf_evlist__enable(rec->evlist); |
764e16a3 | 753 | |
856e9660 PZ |
754 | /* |
755 | * Let the child rip | |
756 | */ | |
e803cf97 | 757 | if (forks) { |
e5bed564 NK |
758 | union perf_event *event; |
759 | ||
760 | event = malloc(sizeof(event->comm) + machine->id_hdr_size); | |
761 | if (event == NULL) { | |
762 | err = -ENOMEM; | |
763 | goto out_child; | |
764 | } | |
765 | ||
e803cf97 NK |
766 | /* |
767 | * Some H/W events are generated before COMM event | |
768 | * which is emitted during exec(), so perf script | |
769 | * cannot see a correct process name for those events. | |
770 | * Synthesize COMM event to prevent it. | |
771 | */ | |
e5bed564 | 772 | perf_event__synthesize_comm(tool, event, |
e803cf97 NK |
773 | rec->evlist->workload.pid, |
774 | process_synthesized_event, | |
775 | machine); | |
e5bed564 | 776 | free(event); |
e803cf97 | 777 | |
3e2be2da | 778 | perf_evlist__start_workload(rec->evlist); |
e803cf97 | 779 | } |
856e9660 | 780 | |
6619a53e AK |
781 | if (opts->initial_delay) { |
782 | usleep(opts->initial_delay * 1000); | |
783 | perf_evlist__enable(rec->evlist); | |
784 | } | |
785 | ||
c0bdc1c4 | 786 | auxtrace_snapshot_enable(); |
649c48a9 | 787 | for (;;) { |
9f065194 | 788 | unsigned long long hits = rec->samples; |
de9ac07b | 789 | |
8c6f45a7 | 790 | if (record__mmap_read_all(rec) < 0) { |
c0bdc1c4 | 791 | auxtrace_snapshot_disable(); |
8d3eca20 | 792 | err = -1; |
45604710 | 793 | goto out_child; |
8d3eca20 | 794 | } |
de9ac07b | 795 | |
2dd6d8a1 AH |
796 | if (auxtrace_record__snapshot_started) { |
797 | auxtrace_record__snapshot_started = 0; | |
798 | if (!auxtrace_snapshot_err) | |
799 | record__read_auxtrace_snapshot(rec); | |
800 | if (auxtrace_snapshot_err) { | |
801 | pr_err("AUX area tracing snapshot failed\n"); | |
802 | err = -1; | |
803 | goto out_child; | |
804 | } | |
805 | } | |
806 | ||
d20deb64 | 807 | if (hits == rec->samples) { |
6dcf45ef | 808 | if (done || draining) |
649c48a9 | 809 | break; |
f66a889d | 810 | err = perf_evlist__poll(rec->evlist, -1); |
a515114f JO |
811 | /* |
812 | * Propagate error, only if there's any. Ignore positive | |
813 | * number of returned events and interrupt error. | |
814 | */ | |
815 | if (err > 0 || (err < 0 && errno == EINTR)) | |
45604710 | 816 | err = 0; |
8b412664 | 817 | waking++; |
6dcf45ef ACM |
818 | |
819 | if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) | |
820 | draining = true; | |
8b412664 PZ |
821 | } |
822 | ||
774cb499 JO |
823 | /* |
824 | * When perf is starting the traced process, at the end events | |
825 | * die with the process and we wait for that. Thus no need to | |
826 | * disable events in this case. | |
827 | */ | |
602ad878 | 828 | if (done && !disabled && !target__none(&opts->target)) { |
c0bdc1c4 | 829 | auxtrace_snapshot_disable(); |
3e2be2da | 830 | perf_evlist__disable(rec->evlist); |
2711926a JO |
831 | disabled = true; |
832 | } | |
de9ac07b | 833 | } |
c0bdc1c4 | 834 | auxtrace_snapshot_disable(); |
de9ac07b | 835 | |
f33cbe72 | 836 | if (forks && workload_exec_errno) { |
35550da3 | 837 | char msg[STRERR_BUFSIZE]; |
f33cbe72 ACM |
838 | const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg)); |
839 | pr_err("Workload failed: %s\n", emsg); | |
840 | err = -1; | |
45604710 | 841 | goto out_child; |
f33cbe72 ACM |
842 | } |
843 | ||
e3d59112 | 844 | if (!quiet) |
45604710 | 845 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
b44308f5 | 846 | |
45604710 NK |
847 | out_child: |
848 | if (forks) { | |
849 | int exit_status; | |
addc2785 | 850 | |
45604710 NK |
851 | if (!child_finished) |
852 | kill(rec->evlist->workload.pid, SIGTERM); | |
853 | ||
854 | wait(&exit_status); | |
855 | ||
856 | if (err < 0) | |
857 | status = err; | |
858 | else if (WIFEXITED(exit_status)) | |
859 | status = WEXITSTATUS(exit_status); | |
860 | else if (WIFSIGNALED(exit_status)) | |
861 | signr = WTERMSIG(exit_status); | |
862 | } else | |
863 | status = err; | |
864 | ||
e3d59112 NK |
865 | /* this will be recalculated during process_buildids() */ |
866 | rec->samples = 0; | |
867 | ||
e1ab48ba WN |
868 | if (!err) |
869 | record__finish_output(rec); | |
39d17dac | 870 | |
e3d59112 NK |
871 | if (!err && !quiet) { |
872 | char samples[128]; | |
873 | ||
ef149c25 | 874 | if (rec->samples && !rec->opts.full_auxtrace) |
e3d59112 NK |
875 | scnprintf(samples, sizeof(samples), |
876 | " (%" PRIu64 " samples)", rec->samples); | |
877 | else | |
878 | samples[0] = '\0'; | |
879 | ||
880 | fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s ]\n", | |
881 | perf_data_file__size(file) / 1024.0 / 1024.0, | |
882 | file->path, samples); | |
883 | } | |
884 | ||
39d17dac ACM |
885 | out_delete_session: |
886 | perf_session__delete(session); | |
45604710 | 887 | return status; |
de9ac07b | 888 | } |
0e9b20b8 | 889 | |
72a128aa | 890 | static void callchain_debug(void) |
09b0fd45 | 891 | { |
aad2b21c | 892 | static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; |
a601fdff | 893 | |
72a128aa | 894 | pr_debug("callchain: type %s\n", str[callchain_param.record_mode]); |
26d33022 | 895 | |
72a128aa | 896 | if (callchain_param.record_mode == CALLCHAIN_DWARF) |
09b0fd45 | 897 | pr_debug("callchain: stack dump size %d\n", |
72a128aa | 898 | callchain_param.dump_size); |
09b0fd45 JO |
899 | } |
900 | ||
c421e80b | 901 | int record_parse_callchain_opt(const struct option *opt, |
09b0fd45 JO |
902 | const char *arg, |
903 | int unset) | |
904 | { | |
09b0fd45 | 905 | int ret; |
c421e80b | 906 | struct record_opts *record = (struct record_opts *)opt->value; |
09b0fd45 | 907 | |
c421e80b | 908 | record->callgraph_set = true; |
72a128aa | 909 | callchain_param.enabled = !unset; |
eb853e80 | 910 | |
09b0fd45 JO |
911 | /* --no-call-graph */ |
912 | if (unset) { | |
72a128aa | 913 | callchain_param.record_mode = CALLCHAIN_NONE; |
09b0fd45 JO |
914 | pr_debug("callchain: disabled\n"); |
915 | return 0; | |
916 | } | |
917 | ||
c3a6a8c4 | 918 | ret = parse_callchain_record_opt(arg, &callchain_param); |
5c0cf224 JO |
919 | if (!ret) { |
920 | /* Enable data address sampling for DWARF unwind. */ | |
921 | if (callchain_param.record_mode == CALLCHAIN_DWARF) | |
922 | record->sample_address = true; | |
72a128aa | 923 | callchain_debug(); |
5c0cf224 | 924 | } |
26d33022 JO |
925 | |
926 | return ret; | |
927 | } | |
928 | ||
c421e80b | 929 | int record_callchain_opt(const struct option *opt, |
09b0fd45 JO |
930 | const char *arg __maybe_unused, |
931 | int unset __maybe_unused) | |
932 | { | |
c421e80b KL |
933 | struct record_opts *record = (struct record_opts *)opt->value; |
934 | ||
935 | record->callgraph_set = true; | |
72a128aa | 936 | callchain_param.enabled = true; |
09b0fd45 | 937 | |
72a128aa NK |
938 | if (callchain_param.record_mode == CALLCHAIN_NONE) |
939 | callchain_param.record_mode = CALLCHAIN_FP; | |
eb853e80 | 940 | |
72a128aa | 941 | callchain_debug(); |
09b0fd45 JO |
942 | return 0; |
943 | } | |
944 | ||
eb853e80 JO |
945 | static int perf_record_config(const char *var, const char *value, void *cb) |
946 | { | |
7a29c087 NK |
947 | struct record *rec = cb; |
948 | ||
949 | if (!strcmp(var, "record.build-id")) { | |
950 | if (!strcmp(value, "cache")) | |
951 | rec->no_buildid_cache = false; | |
952 | else if (!strcmp(value, "no-cache")) | |
953 | rec->no_buildid_cache = true; | |
954 | else if (!strcmp(value, "skip")) | |
955 | rec->no_buildid = true; | |
956 | else | |
957 | return -1; | |
958 | return 0; | |
959 | } | |
eb853e80 | 960 | if (!strcmp(var, "record.call-graph")) |
5a2e5e85 | 961 | var = "call-graph.record-mode"; /* fall-through */ |
eb853e80 JO |
962 | |
963 | return perf_default_config(var, value, cb); | |
964 | } | |
965 | ||
814c8c38 PZ |
966 | struct clockid_map { |
967 | const char *name; | |
968 | int clockid; | |
969 | }; | |
970 | ||
971 | #define CLOCKID_MAP(n, c) \ | |
972 | { .name = n, .clockid = (c), } | |
973 | ||
974 | #define CLOCKID_END { .name = NULL, } | |
975 | ||
976 | ||
977 | /* | |
978 | * Add the missing ones, we need to build on many distros... | |
979 | */ | |
980 | #ifndef CLOCK_MONOTONIC_RAW | |
981 | #define CLOCK_MONOTONIC_RAW 4 | |
982 | #endif | |
983 | #ifndef CLOCK_BOOTTIME | |
984 | #define CLOCK_BOOTTIME 7 | |
985 | #endif | |
986 | #ifndef CLOCK_TAI | |
987 | #define CLOCK_TAI 11 | |
988 | #endif | |
989 | ||
990 | static const struct clockid_map clockids[] = { | |
991 | /* available for all events, NMI safe */ | |
992 | CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), | |
993 | CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), | |
994 | ||
995 | /* available for some events */ | |
996 | CLOCKID_MAP("realtime", CLOCK_REALTIME), | |
997 | CLOCKID_MAP("boottime", CLOCK_BOOTTIME), | |
998 | CLOCKID_MAP("tai", CLOCK_TAI), | |
999 | ||
1000 | /* available for the lazy */ | |
1001 | CLOCKID_MAP("mono", CLOCK_MONOTONIC), | |
1002 | CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), | |
1003 | CLOCKID_MAP("real", CLOCK_REALTIME), | |
1004 | CLOCKID_MAP("boot", CLOCK_BOOTTIME), | |
1005 | ||
1006 | CLOCKID_END, | |
1007 | }; | |
1008 | ||
1009 | static int parse_clockid(const struct option *opt, const char *str, int unset) | |
1010 | { | |
1011 | struct record_opts *opts = (struct record_opts *)opt->value; | |
1012 | const struct clockid_map *cm; | |
1013 | const char *ostr = str; | |
1014 | ||
1015 | if (unset) { | |
1016 | opts->use_clockid = 0; | |
1017 | return 0; | |
1018 | } | |
1019 | ||
1020 | /* no arg passed */ | |
1021 | if (!str) | |
1022 | return 0; | |
1023 | ||
1024 | /* no setting it twice */ | |
1025 | if (opts->use_clockid) | |
1026 | return -1; | |
1027 | ||
1028 | opts->use_clockid = true; | |
1029 | ||
1030 | /* if its a number, we're done */ | |
1031 | if (sscanf(str, "%d", &opts->clockid) == 1) | |
1032 | return 0; | |
1033 | ||
1034 | /* allow a "CLOCK_" prefix to the name */ | |
1035 | if (!strncasecmp(str, "CLOCK_", 6)) | |
1036 | str += 6; | |
1037 | ||
1038 | for (cm = clockids; cm->name; cm++) { | |
1039 | if (!strcasecmp(str, cm->name)) { | |
1040 | opts->clockid = cm->clockid; | |
1041 | return 0; | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | opts->use_clockid = false; | |
1046 | ui__warning("unknown clockid %s, check man page\n", ostr); | |
1047 | return -1; | |
1048 | } | |
1049 | ||
e9db1310 AH |
1050 | static int record__parse_mmap_pages(const struct option *opt, |
1051 | const char *str, | |
1052 | int unset __maybe_unused) | |
1053 | { | |
1054 | struct record_opts *opts = opt->value; | |
1055 | char *s, *p; | |
1056 | unsigned int mmap_pages; | |
1057 | int ret; | |
1058 | ||
1059 | if (!str) | |
1060 | return -EINVAL; | |
1061 | ||
1062 | s = strdup(str); | |
1063 | if (!s) | |
1064 | return -ENOMEM; | |
1065 | ||
1066 | p = strchr(s, ','); | |
1067 | if (p) | |
1068 | *p = '\0'; | |
1069 | ||
1070 | if (*s) { | |
1071 | ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); | |
1072 | if (ret) | |
1073 | goto out_free; | |
1074 | opts->mmap_pages = mmap_pages; | |
1075 | } | |
1076 | ||
1077 | if (!p) { | |
1078 | ret = 0; | |
1079 | goto out_free; | |
1080 | } | |
1081 | ||
1082 | ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); | |
1083 | if (ret) | |
1084 | goto out_free; | |
1085 | ||
1086 | opts->auxtrace_mmap_pages = mmap_pages; | |
1087 | ||
1088 | out_free: | |
1089 | free(s); | |
1090 | return ret; | |
1091 | } | |
1092 | ||
e5b2c207 | 1093 | static const char * const __record_usage[] = { |
9e096753 MG |
1094 | "perf record [<options>] [<command>]", |
1095 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
1096 | NULL |
1097 | }; | |
e5b2c207 | 1098 | const char * const *record_usage = __record_usage; |
0e9b20b8 | 1099 | |
d20deb64 | 1100 | /* |
8c6f45a7 ACM |
1101 | * XXX Ideally would be local to cmd_record() and passed to a record__new |
1102 | * because we need to have access to it in record__exit, that is called | |
d20deb64 ACM |
1103 | * after cmd_record() exits, but since record_options need to be accessible to |
1104 | * builtin-script, leave it here. | |
1105 | * | |
1106 | * At least we don't ouch it in all the other functions here directly. | |
1107 | * | |
1108 | * Just say no to tons of global variables, sigh. | |
1109 | */ | |
8c6f45a7 | 1110 | static struct record record = { |
d20deb64 | 1111 | .opts = { |
8affc2b8 | 1112 | .sample_time = true, |
d20deb64 ACM |
1113 | .mmap_pages = UINT_MAX, |
1114 | .user_freq = UINT_MAX, | |
1115 | .user_interval = ULLONG_MAX, | |
447a6013 | 1116 | .freq = 4000, |
d1cb9fce NK |
1117 | .target = { |
1118 | .uses_mmap = true, | |
3aa5939d | 1119 | .default_per_cpu = true, |
d1cb9fce | 1120 | }, |
9d9cad76 | 1121 | .proc_map_timeout = 500, |
d20deb64 | 1122 | }, |
e3d59112 NK |
1123 | .tool = { |
1124 | .sample = process_sample_event, | |
1125 | .fork = perf_event__process_fork, | |
cca8482c | 1126 | .exit = perf_event__process_exit, |
e3d59112 NK |
1127 | .comm = perf_event__process_comm, |
1128 | .mmap = perf_event__process_mmap, | |
1129 | .mmap2 = perf_event__process_mmap2, | |
cca8482c | 1130 | .ordered_events = true, |
e3d59112 | 1131 | }, |
d20deb64 | 1132 | }; |
7865e817 | 1133 | |
76a26549 NK |
1134 | const char record_callchain_help[] = CALLCHAIN_RECORD_HELP |
1135 | "\n\t\t\t\tDefault: fp"; | |
61eaa3be | 1136 | |
d20deb64 ACM |
1137 | /* |
1138 | * XXX Will stay a global variable till we fix builtin-script.c to stop messing | |
1139 | * with it and switch to use the library functions in perf_evlist that came | |
b4006796 | 1140 | * from builtin-record.c, i.e. use record_opts, |
d20deb64 ACM |
1141 | * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', |
1142 | * using pipes, etc. | |
1143 | */ | |
e5b2c207 | 1144 | struct option __record_options[] = { |
d20deb64 | 1145 | OPT_CALLBACK('e', "event", &record.evlist, "event", |
86847b62 | 1146 | "event selector. use 'perf list' to list available events", |
f120f9d5 | 1147 | parse_events_option), |
d20deb64 | 1148 | OPT_CALLBACK(0, "filter", &record.evlist, "filter", |
c171b552 | 1149 | "event filter", parse_filter), |
4ba1faa1 WN |
1150 | OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, |
1151 | NULL, "don't record events from perf itself", | |
1152 | exclude_perf), | |
bea03405 | 1153 | OPT_STRING('p', "pid", &record.opts.target.pid, "pid", |
d6d901c2 | 1154 | "record events on existing process id"), |
bea03405 | 1155 | OPT_STRING('t', "tid", &record.opts.target.tid, "tid", |
d6d901c2 | 1156 | "record events on existing thread id"), |
d20deb64 | 1157 | OPT_INTEGER('r', "realtime", &record.realtime_prio, |
0e9b20b8 | 1158 | "collect data with this RT SCHED_FIFO priority"), |
509051ea | 1159 | OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, |
acac03fa | 1160 | "collect data without buffering"), |
d20deb64 | 1161 | OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, |
daac07b2 | 1162 | "collect raw sample records from all opened counters"), |
bea03405 | 1163 | OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, |
0e9b20b8 | 1164 | "system-wide collection from all CPUs"), |
bea03405 | 1165 | OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", |
c45c6ea2 | 1166 | "list of cpus to monitor"), |
d20deb64 | 1167 | OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), |
f5fc1412 | 1168 | OPT_STRING('o', "output", &record.file.path, "file", |
abaff32a | 1169 | "output file name"), |
69e7e5b0 AH |
1170 | OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, |
1171 | &record.opts.no_inherit_set, | |
1172 | "child tasks do not inherit counters"), | |
d20deb64 | 1173 | OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"), |
e9db1310 AH |
1174 | OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", |
1175 | "number of mmap data pages and AUX area tracing mmap pages", | |
1176 | record__parse_mmap_pages), | |
d20deb64 | 1177 | OPT_BOOLEAN(0, "group", &record.opts.group, |
43bece79 | 1178 | "put the counters into a counter group"), |
09b0fd45 JO |
1179 | OPT_CALLBACK_NOOPT('g', NULL, &record.opts, |
1180 | NULL, "enables call-graph recording" , | |
1181 | &record_callchain_opt), | |
1182 | OPT_CALLBACK(0, "call-graph", &record.opts, | |
76a26549 | 1183 | "record_mode[,record_size]", record_callchain_help, |
09b0fd45 | 1184 | &record_parse_callchain_opt), |
c0555642 | 1185 | OPT_INCR('v', "verbose", &verbose, |
3da297a6 | 1186 | "be more verbose (show counter open errors, etc)"), |
b44308f5 | 1187 | OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), |
d20deb64 | 1188 | OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, |
649c48a9 | 1189 | "per thread counts"), |
56100321 | 1190 | OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), |
3abebc55 AH |
1191 | OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, |
1192 | &record.opts.sample_time_set, | |
1193 | "Record the sample timestamps"), | |
56100321 | 1194 | OPT_BOOLEAN('P', "period", &record.opts.period, "Record the sample period"), |
d20deb64 | 1195 | OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, |
649c48a9 | 1196 | "don't sample"), |
d2db9a98 WN |
1197 | OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, |
1198 | &record.no_buildid_cache_set, | |
1199 | "do not update the buildid cache"), | |
1200 | OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, | |
1201 | &record.no_buildid_set, | |
1202 | "do not collect buildids in perf.data"), | |
d20deb64 | 1203 | OPT_CALLBACK('G', "cgroup", &record.evlist, "name", |
023695d9 SE |
1204 | "monitor event in cgroup name only", |
1205 | parse_cgroups), | |
a6205a35 | 1206 | OPT_UINTEGER('D', "delay", &record.opts.initial_delay, |
6619a53e | 1207 | "ms to wait before starting measurement after program start"), |
bea03405 NK |
1208 | OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", |
1209 | "user to profile"), | |
a5aabdac SE |
1210 | |
1211 | OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, | |
1212 | "branch any", "sample any taken branches", | |
1213 | parse_branch_stack), | |
1214 | ||
1215 | OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, | |
1216 | "branch filter mask", "branch stack filter modes", | |
bdfebd84 | 1217 | parse_branch_stack), |
05484298 AK |
1218 | OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, |
1219 | "sample by weight (on special events only)"), | |
475eeab9 AK |
1220 | OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, |
1221 | "sample transaction flags (special events only)"), | |
3aa5939d AH |
1222 | OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, |
1223 | "use per-thread mmaps"), | |
bcc84ec6 SE |
1224 | OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", |
1225 | "sample selected machine registers on interrupt," | |
1226 | " use -I ? to list register names", parse_regs), | |
85c273d2 AK |
1227 | OPT_BOOLEAN(0, "running-time", &record.opts.running_time, |
1228 | "Record running/enabled time of read (:S) events"), | |
814c8c38 PZ |
1229 | OPT_CALLBACK('k', "clockid", &record.opts, |
1230 | "clockid", "clockid to use for events, see clock_gettime()", | |
1231 | parse_clockid), | |
2dd6d8a1 AH |
1232 | OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, |
1233 | "opts", "AUX area tracing Snapshot Mode", ""), | |
9d9cad76 KL |
1234 | OPT_UINTEGER(0, "proc-map-timeout", &record.opts.proc_map_timeout, |
1235 | "per thread proc mmap processing timeout in ms"), | |
b757bb09 AH |
1236 | OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events, |
1237 | "Record context switch events"), | |
85723885 JO |
1238 | OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, |
1239 | "Configure all used events to run in kernel space.", | |
1240 | PARSE_OPT_EXCLUSIVE), | |
1241 | OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, | |
1242 | "Configure all used events to run in user space.", | |
1243 | PARSE_OPT_EXCLUSIVE), | |
71dc2326 WN |
1244 | OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", |
1245 | "clang binary to use for compiling BPF scriptlets"), | |
1246 | OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", | |
1247 | "options passed to clang when compiling BPF scriptlets"), | |
7efe0e03 HK |
1248 | OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, |
1249 | "file", "vmlinux pathname"), | |
6156681b NK |
1250 | OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, |
1251 | "Record build-id of all DSOs regardless of hits"), | |
0e9b20b8 IM |
1252 | OPT_END() |
1253 | }; | |
1254 | ||
e5b2c207 NK |
1255 | struct option *record_options = __record_options; |
1256 | ||
1d037ca1 | 1257 | int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused) |
0e9b20b8 | 1258 | { |
ef149c25 | 1259 | int err; |
8c6f45a7 | 1260 | struct record *rec = &record; |
16ad2ffb | 1261 | char errbuf[BUFSIZ]; |
0e9b20b8 | 1262 | |
48e1cab1 WN |
1263 | #ifndef HAVE_LIBBPF_SUPPORT |
1264 | # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) | |
1265 | set_nobuild('\0', "clang-path", true); | |
1266 | set_nobuild('\0', "clang-opt", true); | |
1267 | # undef set_nobuild | |
7efe0e03 HK |
1268 | #endif |
1269 | ||
1270 | #ifndef HAVE_BPF_PROLOGUE | |
1271 | # if !defined (HAVE_DWARF_SUPPORT) | |
1272 | # define REASON "NO_DWARF=1" | |
1273 | # elif !defined (HAVE_LIBBPF_SUPPORT) | |
1274 | # define REASON "NO_LIBBPF=1" | |
1275 | # else | |
1276 | # define REASON "this architecture doesn't support BPF prologue" | |
1277 | # endif | |
1278 | # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) | |
1279 | set_nobuild('\0', "vmlinux", true); | |
1280 | # undef set_nobuild | |
1281 | # undef REASON | |
48e1cab1 WN |
1282 | #endif |
1283 | ||
3e2be2da ACM |
1284 | rec->evlist = perf_evlist__new(); |
1285 | if (rec->evlist == NULL) | |
361c99a6 ACM |
1286 | return -ENOMEM; |
1287 | ||
eb853e80 JO |
1288 | perf_config(perf_record_config, rec); |
1289 | ||
bca647aa | 1290 | argc = parse_options(argc, argv, record_options, record_usage, |
655000e7 | 1291 | PARSE_OPT_STOP_AT_NON_OPTION); |
602ad878 | 1292 | if (!argc && target__none(&rec->opts.target)) |
bca647aa | 1293 | usage_with_options(record_usage, record_options); |
0e9b20b8 | 1294 | |
bea03405 | 1295 | if (nr_cgroups && !rec->opts.target.system_wide) { |
c7118369 NK |
1296 | usage_with_options_msg(record_usage, record_options, |
1297 | "cgroup monitoring only available in system-wide mode"); | |
1298 | ||
023695d9 | 1299 | } |
b757bb09 AH |
1300 | if (rec->opts.record_switch_events && |
1301 | !perf_can_record_switch_events()) { | |
c7118369 NK |
1302 | ui__error("kernel does not support recording context switch events\n"); |
1303 | parse_options_usage(record_usage, record_options, "switch-events", 0); | |
1304 | return -EINVAL; | |
b757bb09 | 1305 | } |
023695d9 | 1306 | |
ef149c25 AH |
1307 | if (!rec->itr) { |
1308 | rec->itr = auxtrace_record__init(rec->evlist, &err); | |
1309 | if (err) | |
1310 | return err; | |
1311 | } | |
1312 | ||
2dd6d8a1 AH |
1313 | err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, |
1314 | rec->opts.auxtrace_snapshot_opts); | |
1315 | if (err) | |
1316 | return err; | |
1317 | ||
d7888573 WN |
1318 | err = bpf__setup_stdout(rec->evlist); |
1319 | if (err) { | |
1320 | bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); | |
1321 | pr_err("ERROR: Setup BPF stdout failed: %s\n", | |
1322 | errbuf); | |
1323 | return err; | |
1324 | } | |
1325 | ||
ef149c25 AH |
1326 | err = -ENOMEM; |
1327 | ||
0a7e6d1b | 1328 | symbol__init(NULL); |
baa2f6ce | 1329 | |
ec80fde7 | 1330 | if (symbol_conf.kptr_restrict) |
646aaea6 ACM |
1331 | pr_warning( |
1332 | "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" | |
1333 | "check /proc/sys/kernel/kptr_restrict.\n\n" | |
1334 | "Samples in kernel functions may not be resolved if a suitable vmlinux\n" | |
1335 | "file is not found in the buildid cache or in the vmlinux path.\n\n" | |
1336 | "Samples in kernel modules won't be resolved at all.\n\n" | |
1337 | "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" | |
1338 | "even with a suitable vmlinux or kallsyms file.\n\n"); | |
ec80fde7 | 1339 | |
d20deb64 | 1340 | if (rec->no_buildid_cache || rec->no_buildid) |
a1ac1d3c | 1341 | disable_buildid_cache(); |
655000e7 | 1342 | |
3e2be2da ACM |
1343 | if (rec->evlist->nr_entries == 0 && |
1344 | perf_evlist__add_default(rec->evlist) < 0) { | |
69aad6f1 ACM |
1345 | pr_err("Not enough memory for event selector list\n"); |
1346 | goto out_symbol_exit; | |
bbd36e5e | 1347 | } |
0e9b20b8 | 1348 | |
69e7e5b0 AH |
1349 | if (rec->opts.target.tid && !rec->opts.no_inherit_set) |
1350 | rec->opts.no_inherit = true; | |
1351 | ||
602ad878 | 1352 | err = target__validate(&rec->opts.target); |
16ad2ffb | 1353 | if (err) { |
602ad878 | 1354 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
16ad2ffb NK |
1355 | ui__warning("%s", errbuf); |
1356 | } | |
1357 | ||
602ad878 | 1358 | err = target__parse_uid(&rec->opts.target); |
16ad2ffb NK |
1359 | if (err) { |
1360 | int saved_errno = errno; | |
4bd0f2d2 | 1361 | |
602ad878 | 1362 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
3780f488 | 1363 | ui__error("%s", errbuf); |
16ad2ffb NK |
1364 | |
1365 | err = -saved_errno; | |
8fa60e1f | 1366 | goto out_symbol_exit; |
16ad2ffb | 1367 | } |
0d37aa34 | 1368 | |
16ad2ffb | 1369 | err = -ENOMEM; |
3e2be2da | 1370 | if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) |
dd7927f4 | 1371 | usage_with_options(record_usage, record_options); |
69aad6f1 | 1372 | |
ef149c25 AH |
1373 | err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); |
1374 | if (err) | |
1375 | goto out_symbol_exit; | |
1376 | ||
6156681b NK |
1377 | /* |
1378 | * We take all buildids when the file contains | |
1379 | * AUX area tracing data because we do not decode the | |
1380 | * trace because it would take too long. | |
1381 | */ | |
1382 | if (rec->opts.full_auxtrace) | |
1383 | rec->buildid_all = true; | |
1384 | ||
b4006796 | 1385 | if (record_opts__config(&rec->opts)) { |
39d17dac | 1386 | err = -EINVAL; |
03ad9747 | 1387 | goto out_symbol_exit; |
7e4ff9e3 MG |
1388 | } |
1389 | ||
d20deb64 | 1390 | err = __cmd_record(&record, argc, argv); |
d65a458b | 1391 | out_symbol_exit: |
45604710 | 1392 | perf_evlist__delete(rec->evlist); |
d65a458b | 1393 | symbol__exit(); |
ef149c25 | 1394 | auxtrace_record__free(rec->itr); |
39d17dac | 1395 | return err; |
0e9b20b8 | 1396 | } |
2dd6d8a1 AH |
1397 | |
1398 | static void snapshot_sig_handler(int sig __maybe_unused) | |
1399 | { | |
c0bdc1c4 | 1400 | if (!auxtrace_snapshot_is_enabled()) |
2dd6d8a1 | 1401 | return; |
c0bdc1c4 | 1402 | auxtrace_snapshot_disable(); |
2dd6d8a1 AH |
1403 | auxtrace_snapshot_err = auxtrace_record__snapshot_start(record.itr); |
1404 | auxtrace_record__snapshot_started = 1; | |
1405 | } |