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" |
0e9b20b8 | 14 | #include "util/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" |
ef149c25 | 30 | #include "util/auxtrace.h" |
7c6a1c65 | 31 | |
97124d5e | 32 | #include <unistd.h> |
de9ac07b | 33 | #include <sched.h> |
a41794cd | 34 | #include <sys/mman.h> |
de9ac07b | 35 | |
78da39fa | 36 | |
8c6f45a7 | 37 | struct record { |
45694aa7 | 38 | struct perf_tool tool; |
b4006796 | 39 | struct record_opts opts; |
d20deb64 | 40 | u64 bytes_written; |
f5fc1412 | 41 | struct perf_data_file file; |
ef149c25 | 42 | struct auxtrace_record *itr; |
d20deb64 ACM |
43 | struct perf_evlist *evlist; |
44 | struct perf_session *session; | |
45 | const char *progname; | |
d20deb64 | 46 | int realtime_prio; |
d20deb64 ACM |
47 | bool no_buildid; |
48 | bool no_buildid_cache; | |
d20deb64 | 49 | long samples; |
0f82ebc4 | 50 | }; |
a21ca2ca | 51 | |
8c6f45a7 | 52 | static int record__write(struct record *rec, void *bf, size_t size) |
f5970550 | 53 | { |
cf8b2e69 | 54 | if (perf_data_file__write(rec->session->file, bf, size) < 0) { |
50a9b868 JO |
55 | pr_err("failed to write perf data, error: %m\n"); |
56 | return -1; | |
f5970550 | 57 | } |
8d3eca20 | 58 | |
cf8b2e69 | 59 | rec->bytes_written += size; |
8d3eca20 | 60 | return 0; |
f5970550 PZ |
61 | } |
62 | ||
45694aa7 | 63 | static int process_synthesized_event(struct perf_tool *tool, |
d20deb64 | 64 | union perf_event *event, |
1d037ca1 IT |
65 | struct perf_sample *sample __maybe_unused, |
66 | struct machine *machine __maybe_unused) | |
234fbbf5 | 67 | { |
8c6f45a7 ACM |
68 | struct record *rec = container_of(tool, struct record, tool); |
69 | return record__write(rec, event, event->header.size); | |
234fbbf5 ACM |
70 | } |
71 | ||
e5685730 | 72 | static int record__mmap_read(struct record *rec, int idx) |
de9ac07b | 73 | { |
e5685730 | 74 | struct perf_mmap *md = &rec->evlist->mmap[idx]; |
7b8283b5 DA |
75 | u64 head = perf_mmap__read_head(md); |
76 | u64 old = md->prev; | |
918512b4 | 77 | unsigned char *data = md->base + page_size; |
de9ac07b PZ |
78 | unsigned long size; |
79 | void *buf; | |
8d3eca20 | 80 | int rc = 0; |
de9ac07b | 81 | |
dc82009a | 82 | if (old == head) |
8d3eca20 | 83 | return 0; |
dc82009a | 84 | |
d20deb64 | 85 | rec->samples++; |
de9ac07b PZ |
86 | |
87 | size = head - old; | |
88 | ||
89 | if ((old & md->mask) + size != (head & md->mask)) { | |
90 | buf = &data[old & md->mask]; | |
91 | size = md->mask + 1 - (old & md->mask); | |
92 | old += size; | |
021e9f47 | 93 | |
8c6f45a7 | 94 | if (record__write(rec, buf, size) < 0) { |
8d3eca20 DA |
95 | rc = -1; |
96 | goto out; | |
97 | } | |
de9ac07b PZ |
98 | } |
99 | ||
100 | buf = &data[old & md->mask]; | |
101 | size = head - old; | |
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 | md->prev = old; | |
e5685730 | 110 | perf_evlist__mmap_consume(rec->evlist, idx); |
8d3eca20 DA |
111 | out: |
112 | return rc; | |
de9ac07b PZ |
113 | } |
114 | ||
ef149c25 AH |
115 | static int record__process_auxtrace(struct perf_tool *tool, |
116 | union perf_event *event, void *data1, | |
117 | size_t len1, void *data2, size_t len2) | |
118 | { | |
119 | struct record *rec = container_of(tool, struct record, tool); | |
120 | size_t padding; | |
121 | u8 pad[8] = {0}; | |
122 | ||
123 | /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ | |
124 | padding = (len1 + len2) & 7; | |
125 | if (padding) | |
126 | padding = 8 - padding; | |
127 | ||
128 | record__write(rec, event, event->header.size); | |
129 | record__write(rec, data1, len1); | |
130 | if (len2) | |
131 | record__write(rec, data2, len2); | |
132 | record__write(rec, &pad, padding); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int record__auxtrace_mmap_read(struct record *rec, | |
138 | struct auxtrace_mmap *mm) | |
139 | { | |
140 | int ret; | |
141 | ||
142 | ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool, | |
143 | record__process_auxtrace); | |
144 | if (ret < 0) | |
145 | return ret; | |
146 | ||
147 | if (ret) | |
148 | rec->samples++; | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
de9ac07b | 153 | static volatile int done = 0; |
f7b7c26e | 154 | static volatile int signr = -1; |
33e49ea7 | 155 | static volatile int child_finished = 0; |
de9ac07b | 156 | |
16c8a109 | 157 | static void sig_handler(int sig) |
de9ac07b | 158 | { |
33e49ea7 AK |
159 | if (sig == SIGCHLD) |
160 | child_finished = 1; | |
45604710 NK |
161 | else |
162 | signr = sig; | |
33e49ea7 | 163 | |
16c8a109 | 164 | done = 1; |
f7b7c26e PZ |
165 | } |
166 | ||
45604710 | 167 | static void record__sig_exit(void) |
f7b7c26e | 168 | { |
45604710 | 169 | if (signr == -1) |
f7b7c26e PZ |
170 | return; |
171 | ||
172 | signal(signr, SIG_DFL); | |
45604710 | 173 | raise(signr); |
de9ac07b PZ |
174 | } |
175 | ||
8c6f45a7 | 176 | static int record__open(struct record *rec) |
dd7927f4 | 177 | { |
56e52e85 | 178 | char msg[512]; |
6a4bb04c | 179 | struct perf_evsel *pos; |
d20deb64 ACM |
180 | struct perf_evlist *evlist = rec->evlist; |
181 | struct perf_session *session = rec->session; | |
b4006796 | 182 | struct record_opts *opts = &rec->opts; |
8d3eca20 | 183 | int rc = 0; |
dd7927f4 | 184 | |
f77a9518 | 185 | perf_evlist__config(evlist, opts); |
cac21425 | 186 | |
0050f7aa | 187 | evlist__for_each(evlist, pos) { |
dd7927f4 | 188 | try_again: |
6a4bb04c | 189 | if (perf_evsel__open(pos, evlist->cpus, evlist->threads) < 0) { |
56e52e85 | 190 | if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { |
d6d901c2 | 191 | if (verbose) |
c0a54341 | 192 | ui__warning("%s\n", msg); |
d6d901c2 ZY |
193 | goto try_again; |
194 | } | |
ca6a4258 | 195 | |
56e52e85 ACM |
196 | rc = -errno; |
197 | perf_evsel__open_strerror(pos, &opts->target, | |
198 | errno, msg, sizeof(msg)); | |
199 | ui__error("%s\n", msg); | |
8d3eca20 | 200 | goto out; |
c171b552 LZ |
201 | } |
202 | } | |
a43d3f08 | 203 | |
23d4aad4 ACM |
204 | if (perf_evlist__apply_filters(evlist, &pos)) { |
205 | error("failed to set filter \"%s\" on event %s with %d (%s)\n", | |
206 | pos->filter, perf_evsel__name(pos), errno, | |
35550da3 | 207 | strerror_r(errno, msg, sizeof(msg))); |
8d3eca20 DA |
208 | rc = -1; |
209 | goto out; | |
0a102479 FW |
210 | } |
211 | ||
ef149c25 AH |
212 | if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false, |
213 | opts->auxtrace_mmap_pages, false) < 0) { | |
8d3eca20 DA |
214 | if (errno == EPERM) { |
215 | pr_err("Permission error mapping pages.\n" | |
216 | "Consider increasing " | |
217 | "/proc/sys/kernel/perf_event_mlock_kb,\n" | |
218 | "or try again with a smaller value of -m/--mmap_pages.\n" | |
ef149c25 AH |
219 | "(current value: %u,%u)\n", |
220 | opts->mmap_pages, opts->auxtrace_mmap_pages); | |
8d3eca20 | 221 | rc = -errno; |
8d3eca20 | 222 | } else { |
35550da3 MH |
223 | pr_err("failed to mmap with %d (%s)\n", errno, |
224 | strerror_r(errno, msg, sizeof(msg))); | |
8d3eca20 DA |
225 | rc = -errno; |
226 | } | |
227 | goto out; | |
18e60939 | 228 | } |
0a27d7f9 | 229 | |
563aecb2 | 230 | session->evlist = evlist; |
7b56cce2 | 231 | perf_session__set_id_hdr_size(session); |
8d3eca20 DA |
232 | out: |
233 | return rc; | |
16c8a109 PZ |
234 | } |
235 | ||
e3d59112 NK |
236 | static int process_sample_event(struct perf_tool *tool, |
237 | union perf_event *event, | |
238 | struct perf_sample *sample, | |
239 | struct perf_evsel *evsel, | |
240 | struct machine *machine) | |
241 | { | |
242 | struct record *rec = container_of(tool, struct record, tool); | |
243 | ||
244 | rec->samples++; | |
245 | ||
246 | return build_id__mark_dso_hit(tool, event, sample, evsel, machine); | |
247 | } | |
248 | ||
8c6f45a7 | 249 | static int process_buildids(struct record *rec) |
6122e4e4 | 250 | { |
f5fc1412 JO |
251 | struct perf_data_file *file = &rec->file; |
252 | struct perf_session *session = rec->session; | |
6122e4e4 | 253 | |
42aa276f | 254 | u64 size = lseek(perf_data_file__fd(file), 0, SEEK_CUR); |
9f591fd7 ACM |
255 | if (size == 0) |
256 | return 0; | |
257 | ||
4ac30cf7 NK |
258 | file->size = size; |
259 | ||
00dc8657 NK |
260 | /* |
261 | * During this process, it'll load kernel map and replace the | |
262 | * dso->long_name to a real pathname it found. In this case | |
263 | * we prefer the vmlinux path like | |
264 | * /lib/modules/3.16.4/build/vmlinux | |
265 | * | |
266 | * rather than build-id path (in debug directory). | |
267 | * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 | |
268 | */ | |
269 | symbol_conf.ignore_vmlinux_buildid = true; | |
270 | ||
b7b61cbe | 271 | return perf_session__process_events(session); |
6122e4e4 ACM |
272 | } |
273 | ||
8115d60c | 274 | static void perf_event__synthesize_guest_os(struct machine *machine, void *data) |
a1645ce1 ZY |
275 | { |
276 | int err; | |
45694aa7 | 277 | struct perf_tool *tool = data; |
a1645ce1 ZY |
278 | /* |
279 | *As for guest kernel when processing subcommand record&report, | |
280 | *we arrange module mmap prior to guest kernel mmap and trigger | |
281 | *a preload dso because default guest module symbols are loaded | |
282 | *from guest kallsyms instead of /lib/modules/XXX/XXX. This | |
283 | *method is used to avoid symbol missing when the first addr is | |
284 | *in module instead of in guest kernel. | |
285 | */ | |
45694aa7 | 286 | err = perf_event__synthesize_modules(tool, process_synthesized_event, |
743eb868 | 287 | machine); |
a1645ce1 ZY |
288 | if (err < 0) |
289 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 290 | " relocation symbol.\n", machine->pid); |
a1645ce1 | 291 | |
a1645ce1 ZY |
292 | /* |
293 | * We use _stext for guest kernel because guest kernel's /proc/kallsyms | |
294 | * have no _text sometimes. | |
295 | */ | |
45694aa7 | 296 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, |
0ae617be | 297 | machine); |
a1645ce1 ZY |
298 | if (err < 0) |
299 | pr_err("Couldn't record guest kernel [%d]'s reference" | |
23346f21 | 300 | " relocation symbol.\n", machine->pid); |
a1645ce1 ZY |
301 | } |
302 | ||
98402807 FW |
303 | static struct perf_event_header finished_round_event = { |
304 | .size = sizeof(struct perf_event_header), | |
305 | .type = PERF_RECORD_FINISHED_ROUND, | |
306 | }; | |
307 | ||
8c6f45a7 | 308 | static int record__mmap_read_all(struct record *rec) |
98402807 | 309 | { |
dcabb507 | 310 | u64 bytes_written = rec->bytes_written; |
0e2e63dd | 311 | int i; |
8d3eca20 | 312 | int rc = 0; |
98402807 | 313 | |
d20deb64 | 314 | for (i = 0; i < rec->evlist->nr_mmaps; i++) { |
ef149c25 AH |
315 | struct auxtrace_mmap *mm = &rec->evlist->mmap[i].auxtrace_mmap; |
316 | ||
8d3eca20 | 317 | if (rec->evlist->mmap[i].base) { |
e5685730 | 318 | if (record__mmap_read(rec, i) != 0) { |
8d3eca20 DA |
319 | rc = -1; |
320 | goto out; | |
321 | } | |
322 | } | |
ef149c25 AH |
323 | |
324 | if (mm->base && | |
325 | record__auxtrace_mmap_read(rec, mm) != 0) { | |
326 | rc = -1; | |
327 | goto out; | |
328 | } | |
98402807 FW |
329 | } |
330 | ||
dcabb507 JO |
331 | /* |
332 | * Mark the round finished in case we wrote | |
333 | * at least one event. | |
334 | */ | |
335 | if (bytes_written != rec->bytes_written) | |
336 | rc = record__write(rec, &finished_round_event, sizeof(finished_round_event)); | |
8d3eca20 DA |
337 | |
338 | out: | |
339 | return rc; | |
98402807 FW |
340 | } |
341 | ||
8c6f45a7 | 342 | static void record__init_features(struct record *rec) |
57706abc | 343 | { |
57706abc DA |
344 | struct perf_session *session = rec->session; |
345 | int feat; | |
346 | ||
347 | for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) | |
348 | perf_header__set_feat(&session->header, feat); | |
349 | ||
350 | if (rec->no_buildid) | |
351 | perf_header__clear_feat(&session->header, HEADER_BUILD_ID); | |
352 | ||
3e2be2da | 353 | if (!have_tracepoints(&rec->evlist->entries)) |
57706abc DA |
354 | perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); |
355 | ||
356 | if (!rec->opts.branch_stack) | |
357 | perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); | |
ef149c25 AH |
358 | |
359 | if (!rec->opts.full_auxtrace) | |
360 | perf_header__clear_feat(&session->header, HEADER_AUXTRACE); | |
57706abc DA |
361 | } |
362 | ||
f33cbe72 ACM |
363 | static volatile int workload_exec_errno; |
364 | ||
365 | /* | |
366 | * perf_evlist__prepare_workload will send a SIGUSR1 | |
367 | * if the fork fails, since we asked by setting its | |
368 | * want_signal to true. | |
369 | */ | |
45604710 NK |
370 | static void workload_exec_failed_signal(int signo __maybe_unused, |
371 | siginfo_t *info, | |
f33cbe72 ACM |
372 | void *ucontext __maybe_unused) |
373 | { | |
374 | workload_exec_errno = info->si_value.sival_int; | |
375 | done = 1; | |
f33cbe72 ACM |
376 | child_finished = 1; |
377 | } | |
378 | ||
8c6f45a7 | 379 | static int __cmd_record(struct record *rec, int argc, const char **argv) |
16c8a109 | 380 | { |
57706abc | 381 | int err; |
45604710 | 382 | int status = 0; |
8b412664 | 383 | unsigned long waking = 0; |
46be604b | 384 | const bool forks = argc > 0; |
23346f21 | 385 | struct machine *machine; |
45694aa7 | 386 | struct perf_tool *tool = &rec->tool; |
b4006796 | 387 | struct record_opts *opts = &rec->opts; |
f5fc1412 | 388 | struct perf_data_file *file = &rec->file; |
d20deb64 | 389 | struct perf_session *session; |
6dcf45ef | 390 | bool disabled = false, draining = false; |
42aa276f | 391 | int fd; |
de9ac07b | 392 | |
d20deb64 | 393 | rec->progname = argv[0]; |
33e49ea7 | 394 | |
45604710 | 395 | atexit(record__sig_exit); |
f5970550 PZ |
396 | signal(SIGCHLD, sig_handler); |
397 | signal(SIGINT, sig_handler); | |
804f7ac7 | 398 | signal(SIGTERM, sig_handler); |
f5970550 | 399 | |
b7b61cbe | 400 | session = perf_session__new(file, false, tool); |
94c744b6 | 401 | if (session == NULL) { |
ffa91880 | 402 | pr_err("Perf session creation failed.\n"); |
a9a70bbc ACM |
403 | return -1; |
404 | } | |
405 | ||
42aa276f | 406 | fd = perf_data_file__fd(file); |
d20deb64 ACM |
407 | rec->session = session; |
408 | ||
8c6f45a7 | 409 | record__init_features(rec); |
330aa675 | 410 | |
d4db3f16 | 411 | if (forks) { |
3e2be2da | 412 | err = perf_evlist__prepare_workload(rec->evlist, &opts->target, |
f5fc1412 | 413 | argv, file->is_pipe, |
735f7e0b | 414 | workload_exec_failed_signal); |
35b9d88e ACM |
415 | if (err < 0) { |
416 | pr_err("Couldn't run the workload!\n"); | |
45604710 | 417 | status = err; |
35b9d88e | 418 | goto out_delete_session; |
856e9660 | 419 | } |
856e9660 PZ |
420 | } |
421 | ||
8c6f45a7 | 422 | if (record__open(rec) != 0) { |
8d3eca20 | 423 | err = -1; |
45604710 | 424 | goto out_child; |
8d3eca20 | 425 | } |
de9ac07b | 426 | |
3e2be2da | 427 | if (!rec->evlist->nr_groups) |
a8bb559b NK |
428 | perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); |
429 | ||
f5fc1412 | 430 | if (file->is_pipe) { |
42aa276f | 431 | err = perf_header__write_pipe(fd); |
529870e3 | 432 | if (err < 0) |
45604710 | 433 | goto out_child; |
563aecb2 | 434 | } else { |
42aa276f | 435 | err = perf_session__write_header(session, rec->evlist, fd, false); |
d5eed904 | 436 | if (err < 0) |
45604710 | 437 | goto out_child; |
56b03f3c ACM |
438 | } |
439 | ||
d3665498 | 440 | if (!rec->no_buildid |
e20960c0 | 441 | && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { |
d3665498 | 442 | pr_err("Couldn't generate buildids. " |
e20960c0 | 443 | "Use --no-buildid to profile anyway.\n"); |
8d3eca20 | 444 | err = -1; |
45604710 | 445 | goto out_child; |
e20960c0 RR |
446 | } |
447 | ||
34ba5122 | 448 | machine = &session->machines.host; |
743eb868 | 449 | |
f5fc1412 | 450 | if (file->is_pipe) { |
45694aa7 | 451 | err = perf_event__synthesize_attrs(tool, session, |
d20deb64 | 452 | process_synthesized_event); |
2c46dbb5 TZ |
453 | if (err < 0) { |
454 | pr_err("Couldn't synthesize attrs.\n"); | |
45604710 | 455 | goto out_child; |
2c46dbb5 | 456 | } |
cd19a035 | 457 | |
3e2be2da | 458 | if (have_tracepoints(&rec->evlist->entries)) { |
63e0c771 TZ |
459 | /* |
460 | * FIXME err <= 0 here actually means that | |
461 | * there were no tracepoints so its not really | |
462 | * an error, just that we don't need to | |
463 | * synthesize anything. We really have to | |
464 | * return this more properly and also | |
465 | * propagate errors that now are calling die() | |
466 | */ | |
42aa276f | 467 | err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, |
743eb868 | 468 | process_synthesized_event); |
63e0c771 TZ |
469 | if (err <= 0) { |
470 | pr_err("Couldn't record tracing data.\n"); | |
45604710 | 471 | goto out_child; |
63e0c771 | 472 | } |
f34b9001 | 473 | rec->bytes_written += err; |
63e0c771 | 474 | } |
2c46dbb5 TZ |
475 | } |
476 | ||
ef149c25 AH |
477 | if (rec->opts.full_auxtrace) { |
478 | err = perf_event__synthesize_auxtrace_info(rec->itr, tool, | |
479 | session, process_synthesized_event); | |
480 | if (err) | |
481 | goto out_delete_session; | |
482 | } | |
483 | ||
45694aa7 | 484 | err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, |
0ae617be | 485 | machine); |
c1a3a4b9 ACM |
486 | if (err < 0) |
487 | pr_err("Couldn't record kernel reference relocation symbol\n" | |
488 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
489 | "Check /proc/kallsyms permission or run as root.\n"); | |
b7cece76 | 490 | |
45694aa7 | 491 | err = perf_event__synthesize_modules(tool, process_synthesized_event, |
743eb868 | 492 | machine); |
c1a3a4b9 ACM |
493 | if (err < 0) |
494 | pr_err("Couldn't record kernel module information.\n" | |
495 | "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" | |
496 | "Check /proc/modules permission or run as root.\n"); | |
497 | ||
7e383de4 | 498 | if (perf_guest) { |
876650e6 ACM |
499 | machines__process_guests(&session->machines, |
500 | perf_event__synthesize_guest_os, tool); | |
7e383de4 | 501 | } |
7c6a1c65 | 502 | |
3e2be2da | 503 | err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, |
a33fbd56 | 504 | process_synthesized_event, opts->sample_address); |
8d3eca20 | 505 | if (err != 0) |
45604710 | 506 | goto out_child; |
8d3eca20 | 507 | |
d20deb64 | 508 | if (rec->realtime_prio) { |
de9ac07b PZ |
509 | struct sched_param param; |
510 | ||
d20deb64 | 511 | param.sched_priority = rec->realtime_prio; |
de9ac07b | 512 | if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { |
6beba7ad | 513 | pr_err("Could not set realtime priority.\n"); |
8d3eca20 | 514 | err = -1; |
45604710 | 515 | goto out_child; |
de9ac07b PZ |
516 | } |
517 | } | |
518 | ||
774cb499 JO |
519 | /* |
520 | * When perf is starting the traced process, all the events | |
521 | * (apart from group members) have enable_on_exec=1 set, | |
522 | * so don't spoil it by prematurely enabling them. | |
523 | */ | |
6619a53e | 524 | if (!target__none(&opts->target) && !opts->initial_delay) |
3e2be2da | 525 | perf_evlist__enable(rec->evlist); |
764e16a3 | 526 | |
856e9660 PZ |
527 | /* |
528 | * Let the child rip | |
529 | */ | |
735f7e0b | 530 | if (forks) |
3e2be2da | 531 | perf_evlist__start_workload(rec->evlist); |
856e9660 | 532 | |
6619a53e AK |
533 | if (opts->initial_delay) { |
534 | usleep(opts->initial_delay * 1000); | |
535 | perf_evlist__enable(rec->evlist); | |
536 | } | |
537 | ||
649c48a9 | 538 | for (;;) { |
d20deb64 | 539 | int hits = rec->samples; |
de9ac07b | 540 | |
8c6f45a7 | 541 | if (record__mmap_read_all(rec) < 0) { |
8d3eca20 | 542 | err = -1; |
45604710 | 543 | goto out_child; |
8d3eca20 | 544 | } |
de9ac07b | 545 | |
d20deb64 | 546 | if (hits == rec->samples) { |
6dcf45ef | 547 | if (done || draining) |
649c48a9 | 548 | break; |
f66a889d | 549 | err = perf_evlist__poll(rec->evlist, -1); |
a515114f JO |
550 | /* |
551 | * Propagate error, only if there's any. Ignore positive | |
552 | * number of returned events and interrupt error. | |
553 | */ | |
554 | if (err > 0 || (err < 0 && errno == EINTR)) | |
45604710 | 555 | err = 0; |
8b412664 | 556 | waking++; |
6dcf45ef ACM |
557 | |
558 | if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) | |
559 | draining = true; | |
8b412664 PZ |
560 | } |
561 | ||
774cb499 JO |
562 | /* |
563 | * When perf is starting the traced process, at the end events | |
564 | * die with the process and we wait for that. Thus no need to | |
565 | * disable events in this case. | |
566 | */ | |
602ad878 | 567 | if (done && !disabled && !target__none(&opts->target)) { |
3e2be2da | 568 | perf_evlist__disable(rec->evlist); |
2711926a JO |
569 | disabled = true; |
570 | } | |
de9ac07b PZ |
571 | } |
572 | ||
f33cbe72 | 573 | if (forks && workload_exec_errno) { |
35550da3 | 574 | char msg[STRERR_BUFSIZE]; |
f33cbe72 ACM |
575 | const char *emsg = strerror_r(workload_exec_errno, msg, sizeof(msg)); |
576 | pr_err("Workload failed: %s\n", emsg); | |
577 | err = -1; | |
45604710 | 578 | goto out_child; |
f33cbe72 ACM |
579 | } |
580 | ||
e3d59112 | 581 | if (!quiet) |
45604710 | 582 | fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); |
b44308f5 | 583 | |
45604710 NK |
584 | out_child: |
585 | if (forks) { | |
586 | int exit_status; | |
addc2785 | 587 | |
45604710 NK |
588 | if (!child_finished) |
589 | kill(rec->evlist->workload.pid, SIGTERM); | |
590 | ||
591 | wait(&exit_status); | |
592 | ||
593 | if (err < 0) | |
594 | status = err; | |
595 | else if (WIFEXITED(exit_status)) | |
596 | status = WEXITSTATUS(exit_status); | |
597 | else if (WIFSIGNALED(exit_status)) | |
598 | signr = WTERMSIG(exit_status); | |
599 | } else | |
600 | status = err; | |
601 | ||
e3d59112 NK |
602 | /* this will be recalculated during process_buildids() */ |
603 | rec->samples = 0; | |
604 | ||
45604710 NK |
605 | if (!err && !file->is_pipe) { |
606 | rec->session->header.data_size += rec->bytes_written; | |
607 | ||
608 | if (!rec->no_buildid) | |
609 | process_buildids(rec); | |
42aa276f | 610 | perf_session__write_header(rec->session, rec->evlist, fd, true); |
45604710 | 611 | } |
39d17dac | 612 | |
e3d59112 NK |
613 | if (!err && !quiet) { |
614 | char samples[128]; | |
615 | ||
ef149c25 | 616 | if (rec->samples && !rec->opts.full_auxtrace) |
e3d59112 NK |
617 | scnprintf(samples, sizeof(samples), |
618 | " (%" PRIu64 " samples)", rec->samples); | |
619 | else | |
620 | samples[0] = '\0'; | |
621 | ||
622 | fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s ]\n", | |
623 | perf_data_file__size(file) / 1024.0 / 1024.0, | |
624 | file->path, samples); | |
625 | } | |
626 | ||
39d17dac ACM |
627 | out_delete_session: |
628 | perf_session__delete(session); | |
45604710 | 629 | return status; |
de9ac07b | 630 | } |
0e9b20b8 | 631 | |
bdfebd84 RAV |
632 | #define BRANCH_OPT(n, m) \ |
633 | { .name = n, .mode = (m) } | |
634 | ||
635 | #define BRANCH_END { .name = NULL } | |
636 | ||
637 | struct branch_mode { | |
638 | const char *name; | |
639 | int mode; | |
640 | }; | |
641 | ||
642 | static const struct branch_mode branch_modes[] = { | |
643 | BRANCH_OPT("u", PERF_SAMPLE_BRANCH_USER), | |
644 | BRANCH_OPT("k", PERF_SAMPLE_BRANCH_KERNEL), | |
645 | BRANCH_OPT("hv", PERF_SAMPLE_BRANCH_HV), | |
646 | BRANCH_OPT("any", PERF_SAMPLE_BRANCH_ANY), | |
647 | BRANCH_OPT("any_call", PERF_SAMPLE_BRANCH_ANY_CALL), | |
648 | BRANCH_OPT("any_ret", PERF_SAMPLE_BRANCH_ANY_RETURN), | |
649 | BRANCH_OPT("ind_call", PERF_SAMPLE_BRANCH_IND_CALL), | |
0126d493 AK |
650 | BRANCH_OPT("abort_tx", PERF_SAMPLE_BRANCH_ABORT_TX), |
651 | BRANCH_OPT("in_tx", PERF_SAMPLE_BRANCH_IN_TX), | |
652 | BRANCH_OPT("no_tx", PERF_SAMPLE_BRANCH_NO_TX), | |
0fffa5df | 653 | BRANCH_OPT("cond", PERF_SAMPLE_BRANCH_COND), |
bdfebd84 RAV |
654 | BRANCH_END |
655 | }; | |
656 | ||
657 | static int | |
a5aabdac | 658 | parse_branch_stack(const struct option *opt, const char *str, int unset) |
bdfebd84 RAV |
659 | { |
660 | #define ONLY_PLM \ | |
661 | (PERF_SAMPLE_BRANCH_USER |\ | |
662 | PERF_SAMPLE_BRANCH_KERNEL |\ | |
663 | PERF_SAMPLE_BRANCH_HV) | |
664 | ||
665 | uint64_t *mode = (uint64_t *)opt->value; | |
666 | const struct branch_mode *br; | |
a5aabdac | 667 | char *s, *os = NULL, *p; |
bdfebd84 RAV |
668 | int ret = -1; |
669 | ||
a5aabdac SE |
670 | if (unset) |
671 | return 0; | |
bdfebd84 | 672 | |
a5aabdac SE |
673 | /* |
674 | * cannot set it twice, -b + --branch-filter for instance | |
675 | */ | |
676 | if (*mode) | |
bdfebd84 RAV |
677 | return -1; |
678 | ||
a5aabdac SE |
679 | /* str may be NULL in case no arg is passed to -b */ |
680 | if (str) { | |
681 | /* because str is read-only */ | |
682 | s = os = strdup(str); | |
683 | if (!s) | |
684 | return -1; | |
685 | ||
686 | for (;;) { | |
687 | p = strchr(s, ','); | |
688 | if (p) | |
689 | *p = '\0'; | |
690 | ||
691 | for (br = branch_modes; br->name; br++) { | |
692 | if (!strcasecmp(s, br->name)) | |
693 | break; | |
694 | } | |
695 | if (!br->name) { | |
696 | ui__warning("unknown branch filter %s," | |
697 | " check man page\n", s); | |
698 | goto error; | |
699 | } | |
bdfebd84 | 700 | |
a5aabdac | 701 | *mode |= br->mode; |
bdfebd84 | 702 | |
a5aabdac SE |
703 | if (!p) |
704 | break; | |
bdfebd84 | 705 | |
a5aabdac SE |
706 | s = p + 1; |
707 | } | |
bdfebd84 RAV |
708 | } |
709 | ret = 0; | |
710 | ||
a5aabdac | 711 | /* default to any branch */ |
bdfebd84 | 712 | if ((*mode & ~ONLY_PLM) == 0) { |
a5aabdac | 713 | *mode = PERF_SAMPLE_BRANCH_ANY; |
bdfebd84 RAV |
714 | } |
715 | error: | |
716 | free(os); | |
717 | return ret; | |
718 | } | |
719 | ||
72a128aa | 720 | static void callchain_debug(void) |
09b0fd45 | 721 | { |
aad2b21c | 722 | static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; |
a601fdff | 723 | |
72a128aa | 724 | pr_debug("callchain: type %s\n", str[callchain_param.record_mode]); |
26d33022 | 725 | |
72a128aa | 726 | if (callchain_param.record_mode == CALLCHAIN_DWARF) |
09b0fd45 | 727 | pr_debug("callchain: stack dump size %d\n", |
72a128aa | 728 | callchain_param.dump_size); |
09b0fd45 JO |
729 | } |
730 | ||
72a128aa | 731 | int record_parse_callchain_opt(const struct option *opt __maybe_unused, |
09b0fd45 JO |
732 | const char *arg, |
733 | int unset) | |
734 | { | |
09b0fd45 JO |
735 | int ret; |
736 | ||
72a128aa | 737 | callchain_param.enabled = !unset; |
eb853e80 | 738 | |
09b0fd45 JO |
739 | /* --no-call-graph */ |
740 | if (unset) { | |
72a128aa | 741 | callchain_param.record_mode = CALLCHAIN_NONE; |
09b0fd45 JO |
742 | pr_debug("callchain: disabled\n"); |
743 | return 0; | |
744 | } | |
745 | ||
f7f084f4 | 746 | ret = parse_callchain_record_opt(arg); |
26d33022 | 747 | if (!ret) |
72a128aa | 748 | callchain_debug(); |
26d33022 JO |
749 | |
750 | return ret; | |
751 | } | |
752 | ||
72a128aa | 753 | int record_callchain_opt(const struct option *opt __maybe_unused, |
09b0fd45 JO |
754 | const char *arg __maybe_unused, |
755 | int unset __maybe_unused) | |
756 | { | |
72a128aa | 757 | callchain_param.enabled = true; |
09b0fd45 | 758 | |
72a128aa NK |
759 | if (callchain_param.record_mode == CALLCHAIN_NONE) |
760 | callchain_param.record_mode = CALLCHAIN_FP; | |
eb853e80 | 761 | |
72a128aa | 762 | callchain_debug(); |
09b0fd45 JO |
763 | return 0; |
764 | } | |
765 | ||
eb853e80 JO |
766 | static int perf_record_config(const char *var, const char *value, void *cb) |
767 | { | |
eb853e80 | 768 | if (!strcmp(var, "record.call-graph")) |
5a2e5e85 | 769 | var = "call-graph.record-mode"; /* fall-through */ |
eb853e80 JO |
770 | |
771 | return perf_default_config(var, value, cb); | |
772 | } | |
773 | ||
814c8c38 PZ |
774 | struct clockid_map { |
775 | const char *name; | |
776 | int clockid; | |
777 | }; | |
778 | ||
779 | #define CLOCKID_MAP(n, c) \ | |
780 | { .name = n, .clockid = (c), } | |
781 | ||
782 | #define CLOCKID_END { .name = NULL, } | |
783 | ||
784 | ||
785 | /* | |
786 | * Add the missing ones, we need to build on many distros... | |
787 | */ | |
788 | #ifndef CLOCK_MONOTONIC_RAW | |
789 | #define CLOCK_MONOTONIC_RAW 4 | |
790 | #endif | |
791 | #ifndef CLOCK_BOOTTIME | |
792 | #define CLOCK_BOOTTIME 7 | |
793 | #endif | |
794 | #ifndef CLOCK_TAI | |
795 | #define CLOCK_TAI 11 | |
796 | #endif | |
797 | ||
798 | static const struct clockid_map clockids[] = { | |
799 | /* available for all events, NMI safe */ | |
800 | CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), | |
801 | CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), | |
802 | ||
803 | /* available for some events */ | |
804 | CLOCKID_MAP("realtime", CLOCK_REALTIME), | |
805 | CLOCKID_MAP("boottime", CLOCK_BOOTTIME), | |
806 | CLOCKID_MAP("tai", CLOCK_TAI), | |
807 | ||
808 | /* available for the lazy */ | |
809 | CLOCKID_MAP("mono", CLOCK_MONOTONIC), | |
810 | CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), | |
811 | CLOCKID_MAP("real", CLOCK_REALTIME), | |
812 | CLOCKID_MAP("boot", CLOCK_BOOTTIME), | |
813 | ||
814 | CLOCKID_END, | |
815 | }; | |
816 | ||
817 | static int parse_clockid(const struct option *opt, const char *str, int unset) | |
818 | { | |
819 | struct record_opts *opts = (struct record_opts *)opt->value; | |
820 | const struct clockid_map *cm; | |
821 | const char *ostr = str; | |
822 | ||
823 | if (unset) { | |
824 | opts->use_clockid = 0; | |
825 | return 0; | |
826 | } | |
827 | ||
828 | /* no arg passed */ | |
829 | if (!str) | |
830 | return 0; | |
831 | ||
832 | /* no setting it twice */ | |
833 | if (opts->use_clockid) | |
834 | return -1; | |
835 | ||
836 | opts->use_clockid = true; | |
837 | ||
838 | /* if its a number, we're done */ | |
839 | if (sscanf(str, "%d", &opts->clockid) == 1) | |
840 | return 0; | |
841 | ||
842 | /* allow a "CLOCK_" prefix to the name */ | |
843 | if (!strncasecmp(str, "CLOCK_", 6)) | |
844 | str += 6; | |
845 | ||
846 | for (cm = clockids; cm->name; cm++) { | |
847 | if (!strcasecmp(str, cm->name)) { | |
848 | opts->clockid = cm->clockid; | |
849 | return 0; | |
850 | } | |
851 | } | |
852 | ||
853 | opts->use_clockid = false; | |
854 | ui__warning("unknown clockid %s, check man page\n", ostr); | |
855 | return -1; | |
856 | } | |
857 | ||
e5b2c207 | 858 | static const char * const __record_usage[] = { |
9e096753 MG |
859 | "perf record [<options>] [<command>]", |
860 | "perf record [<options>] -- <command> [<options>]", | |
0e9b20b8 IM |
861 | NULL |
862 | }; | |
e5b2c207 | 863 | const char * const *record_usage = __record_usage; |
0e9b20b8 | 864 | |
d20deb64 | 865 | /* |
8c6f45a7 ACM |
866 | * XXX Ideally would be local to cmd_record() and passed to a record__new |
867 | * because we need to have access to it in record__exit, that is called | |
d20deb64 ACM |
868 | * after cmd_record() exits, but since record_options need to be accessible to |
869 | * builtin-script, leave it here. | |
870 | * | |
871 | * At least we don't ouch it in all the other functions here directly. | |
872 | * | |
873 | * Just say no to tons of global variables, sigh. | |
874 | */ | |
8c6f45a7 | 875 | static struct record record = { |
d20deb64 | 876 | .opts = { |
8affc2b8 | 877 | .sample_time = true, |
d20deb64 ACM |
878 | .mmap_pages = UINT_MAX, |
879 | .user_freq = UINT_MAX, | |
880 | .user_interval = ULLONG_MAX, | |
447a6013 | 881 | .freq = 4000, |
d1cb9fce NK |
882 | .target = { |
883 | .uses_mmap = true, | |
3aa5939d | 884 | .default_per_cpu = true, |
d1cb9fce | 885 | }, |
d20deb64 | 886 | }, |
e3d59112 NK |
887 | .tool = { |
888 | .sample = process_sample_event, | |
889 | .fork = perf_event__process_fork, | |
890 | .comm = perf_event__process_comm, | |
891 | .mmap = perf_event__process_mmap, | |
892 | .mmap2 = perf_event__process_mmap2, | |
893 | }, | |
d20deb64 | 894 | }; |
7865e817 | 895 | |
09b0fd45 | 896 | #define CALLCHAIN_HELP "setup and enables call-graph (stack chain/backtrace) recording: " |
61eaa3be | 897 | |
9ff125d1 | 898 | #ifdef HAVE_DWARF_UNWIND_SUPPORT |
aad2b21c | 899 | const char record_callchain_help[] = CALLCHAIN_HELP "fp dwarf lbr"; |
61eaa3be | 900 | #else |
aad2b21c | 901 | const char record_callchain_help[] = CALLCHAIN_HELP "fp lbr"; |
61eaa3be ACM |
902 | #endif |
903 | ||
d20deb64 ACM |
904 | /* |
905 | * XXX Will stay a global variable till we fix builtin-script.c to stop messing | |
906 | * with it and switch to use the library functions in perf_evlist that came | |
b4006796 | 907 | * from builtin-record.c, i.e. use record_opts, |
d20deb64 ACM |
908 | * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', |
909 | * using pipes, etc. | |
910 | */ | |
e5b2c207 | 911 | struct option __record_options[] = { |
d20deb64 | 912 | OPT_CALLBACK('e', "event", &record.evlist, "event", |
86847b62 | 913 | "event selector. use 'perf list' to list available events", |
f120f9d5 | 914 | parse_events_option), |
d20deb64 | 915 | OPT_CALLBACK(0, "filter", &record.evlist, "filter", |
c171b552 | 916 | "event filter", parse_filter), |
bea03405 | 917 | OPT_STRING('p', "pid", &record.opts.target.pid, "pid", |
d6d901c2 | 918 | "record events on existing process id"), |
bea03405 | 919 | OPT_STRING('t', "tid", &record.opts.target.tid, "tid", |
d6d901c2 | 920 | "record events on existing thread id"), |
d20deb64 | 921 | OPT_INTEGER('r', "realtime", &record.realtime_prio, |
0e9b20b8 | 922 | "collect data with this RT SCHED_FIFO priority"), |
509051ea | 923 | OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, |
acac03fa | 924 | "collect data without buffering"), |
d20deb64 | 925 | OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, |
daac07b2 | 926 | "collect raw sample records from all opened counters"), |
bea03405 | 927 | OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, |
0e9b20b8 | 928 | "system-wide collection from all CPUs"), |
bea03405 | 929 | OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", |
c45c6ea2 | 930 | "list of cpus to monitor"), |
d20deb64 | 931 | OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), |
f5fc1412 | 932 | OPT_STRING('o', "output", &record.file.path, "file", |
abaff32a | 933 | "output file name"), |
69e7e5b0 AH |
934 | OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, |
935 | &record.opts.no_inherit_set, | |
936 | "child tasks do not inherit counters"), | |
d20deb64 | 937 | OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"), |
994a1f78 JO |
938 | OPT_CALLBACK('m', "mmap-pages", &record.opts.mmap_pages, "pages", |
939 | "number of mmap data pages", | |
940 | perf_evlist__parse_mmap_pages), | |
d20deb64 | 941 | OPT_BOOLEAN(0, "group", &record.opts.group, |
43bece79 | 942 | "put the counters into a counter group"), |
09b0fd45 JO |
943 | OPT_CALLBACK_NOOPT('g', NULL, &record.opts, |
944 | NULL, "enables call-graph recording" , | |
945 | &record_callchain_opt), | |
946 | OPT_CALLBACK(0, "call-graph", &record.opts, | |
947 | "mode[,dump_size]", record_callchain_help, | |
948 | &record_parse_callchain_opt), | |
c0555642 | 949 | OPT_INCR('v', "verbose", &verbose, |
3da297a6 | 950 | "be more verbose (show counter open errors, etc)"), |
b44308f5 | 951 | OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), |
d20deb64 | 952 | OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, |
649c48a9 | 953 | "per thread counts"), |
d20deb64 | 954 | OPT_BOOLEAN('d', "data", &record.opts.sample_address, |
4bba828d | 955 | "Sample addresses"), |
d20deb64 | 956 | OPT_BOOLEAN('T', "timestamp", &record.opts.sample_time, "Sample timestamps"), |
3e76ac78 | 957 | OPT_BOOLEAN('P', "period", &record.opts.period, "Sample period"), |
d20deb64 | 958 | OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, |
649c48a9 | 959 | "don't sample"), |
d20deb64 | 960 | OPT_BOOLEAN('N', "no-buildid-cache", &record.no_buildid_cache, |
a1ac1d3c | 961 | "do not update the buildid cache"), |
d20deb64 | 962 | OPT_BOOLEAN('B', "no-buildid", &record.no_buildid, |
baa2f6ce | 963 | "do not collect buildids in perf.data"), |
d20deb64 | 964 | OPT_CALLBACK('G', "cgroup", &record.evlist, "name", |
023695d9 SE |
965 | "monitor event in cgroup name only", |
966 | parse_cgroups), | |
a6205a35 | 967 | OPT_UINTEGER('D', "delay", &record.opts.initial_delay, |
6619a53e | 968 | "ms to wait before starting measurement after program start"), |
bea03405 NK |
969 | OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", |
970 | "user to profile"), | |
a5aabdac SE |
971 | |
972 | OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, | |
973 | "branch any", "sample any taken branches", | |
974 | parse_branch_stack), | |
975 | ||
976 | OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, | |
977 | "branch filter mask", "branch stack filter modes", | |
bdfebd84 | 978 | parse_branch_stack), |
05484298 AK |
979 | OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, |
980 | "sample by weight (on special events only)"), | |
475eeab9 AK |
981 | OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, |
982 | "sample transaction flags (special events only)"), | |
3aa5939d AH |
983 | OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, |
984 | "use per-thread mmaps"), | |
4b6c5177 SE |
985 | OPT_BOOLEAN('I', "intr-regs", &record.opts.sample_intr_regs, |
986 | "Sample machine registers on interrupt"), | |
85c273d2 AK |
987 | OPT_BOOLEAN(0, "running-time", &record.opts.running_time, |
988 | "Record running/enabled time of read (:S) events"), | |
814c8c38 PZ |
989 | OPT_CALLBACK('k', "clockid", &record.opts, |
990 | "clockid", "clockid to use for events, see clock_gettime()", | |
991 | parse_clockid), | |
0e9b20b8 IM |
992 | OPT_END() |
993 | }; | |
994 | ||
e5b2c207 NK |
995 | struct option *record_options = __record_options; |
996 | ||
1d037ca1 | 997 | int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused) |
0e9b20b8 | 998 | { |
ef149c25 | 999 | int err; |
8c6f45a7 | 1000 | struct record *rec = &record; |
16ad2ffb | 1001 | char errbuf[BUFSIZ]; |
0e9b20b8 | 1002 | |
3e2be2da ACM |
1003 | rec->evlist = perf_evlist__new(); |
1004 | if (rec->evlist == NULL) | |
361c99a6 ACM |
1005 | return -ENOMEM; |
1006 | ||
eb853e80 JO |
1007 | perf_config(perf_record_config, rec); |
1008 | ||
bca647aa | 1009 | argc = parse_options(argc, argv, record_options, record_usage, |
655000e7 | 1010 | PARSE_OPT_STOP_AT_NON_OPTION); |
602ad878 | 1011 | if (!argc && target__none(&rec->opts.target)) |
bca647aa | 1012 | usage_with_options(record_usage, record_options); |
0e9b20b8 | 1013 | |
bea03405 | 1014 | if (nr_cgroups && !rec->opts.target.system_wide) { |
3780f488 NK |
1015 | ui__error("cgroup monitoring only available in" |
1016 | " system-wide mode\n"); | |
023695d9 SE |
1017 | usage_with_options(record_usage, record_options); |
1018 | } | |
1019 | ||
ef149c25 AH |
1020 | if (!rec->itr) { |
1021 | rec->itr = auxtrace_record__init(rec->evlist, &err); | |
1022 | if (err) | |
1023 | return err; | |
1024 | } | |
1025 | ||
1026 | err = -ENOMEM; | |
1027 | ||
0a7e6d1b | 1028 | symbol__init(NULL); |
baa2f6ce | 1029 | |
ec80fde7 | 1030 | if (symbol_conf.kptr_restrict) |
646aaea6 ACM |
1031 | pr_warning( |
1032 | "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" | |
1033 | "check /proc/sys/kernel/kptr_restrict.\n\n" | |
1034 | "Samples in kernel functions may not be resolved if a suitable vmlinux\n" | |
1035 | "file is not found in the buildid cache or in the vmlinux path.\n\n" | |
1036 | "Samples in kernel modules won't be resolved at all.\n\n" | |
1037 | "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" | |
1038 | "even with a suitable vmlinux or kallsyms file.\n\n"); | |
ec80fde7 | 1039 | |
d20deb64 | 1040 | if (rec->no_buildid_cache || rec->no_buildid) |
a1ac1d3c | 1041 | disable_buildid_cache(); |
655000e7 | 1042 | |
3e2be2da ACM |
1043 | if (rec->evlist->nr_entries == 0 && |
1044 | perf_evlist__add_default(rec->evlist) < 0) { | |
69aad6f1 ACM |
1045 | pr_err("Not enough memory for event selector list\n"); |
1046 | goto out_symbol_exit; | |
bbd36e5e | 1047 | } |
0e9b20b8 | 1048 | |
69e7e5b0 AH |
1049 | if (rec->opts.target.tid && !rec->opts.no_inherit_set) |
1050 | rec->opts.no_inherit = true; | |
1051 | ||
602ad878 | 1052 | err = target__validate(&rec->opts.target); |
16ad2ffb | 1053 | if (err) { |
602ad878 | 1054 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
16ad2ffb NK |
1055 | ui__warning("%s", errbuf); |
1056 | } | |
1057 | ||
602ad878 | 1058 | err = target__parse_uid(&rec->opts.target); |
16ad2ffb NK |
1059 | if (err) { |
1060 | int saved_errno = errno; | |
4bd0f2d2 | 1061 | |
602ad878 | 1062 | target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); |
3780f488 | 1063 | ui__error("%s", errbuf); |
16ad2ffb NK |
1064 | |
1065 | err = -saved_errno; | |
8fa60e1f | 1066 | goto out_symbol_exit; |
16ad2ffb | 1067 | } |
0d37aa34 | 1068 | |
16ad2ffb | 1069 | err = -ENOMEM; |
3e2be2da | 1070 | if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) |
dd7927f4 | 1071 | usage_with_options(record_usage, record_options); |
69aad6f1 | 1072 | |
ef149c25 AH |
1073 | err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); |
1074 | if (err) | |
1075 | goto out_symbol_exit; | |
1076 | ||
b4006796 | 1077 | if (record_opts__config(&rec->opts)) { |
39d17dac | 1078 | err = -EINVAL; |
03ad9747 | 1079 | goto out_symbol_exit; |
7e4ff9e3 MG |
1080 | } |
1081 | ||
d20deb64 | 1082 | err = __cmd_record(&record, argc, argv); |
d65a458b | 1083 | out_symbol_exit: |
45604710 | 1084 | perf_evlist__delete(rec->evlist); |
d65a458b | 1085 | symbol__exit(); |
ef149c25 | 1086 | auxtrace_record__free(rec->itr); |
39d17dac | 1087 | return err; |
0e9b20b8 | 1088 | } |