186470930SIngo Molnar /* 286470930SIngo Molnar * builtin-record.c 386470930SIngo Molnar * 486470930SIngo Molnar * Builtin record command: Record the profile of a workload 586470930SIngo Molnar * (or a CPU, or a PID) into the perf.data output file - for 686470930SIngo Molnar * later analysis via perf report. 786470930SIngo Molnar */ 886470930SIngo Molnar #include "builtin.h" 986470930SIngo Molnar 1086470930SIngo Molnar #include "perf.h" 1186470930SIngo Molnar 126122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h" 1386470930SIngo Molnar #include "util/util.h" 144b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1586470930SIngo Molnar #include "util/parse-events.h" 1641840d21STaeung Song #include "util/config.h" 1786470930SIngo Molnar 188f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h" 19f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h" 207c6a1c65SPeter Zijlstra #include "util/header.h" 2166e274f3SFrederic Weisbecker #include "util/event.h" 22361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2369aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 248f28827aSFrederic Weisbecker #include "util/debug.h" 255d8bb1ecSMathieu Poirier #include "util/drv_configs.h" 2694c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2745694aa7SArnaldo Carvalho de Melo #include "util/tool.h" 288d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 29a12b51c4SPaul Mackerras #include "util/cpumap.h" 30fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h" 31f5fc1412SJiri Olsa #include "util/data.h" 32bcc84ec6SStephane Eranian #include "util/perf_regs.h" 33ef149c25SAdrian Hunter #include "util/auxtrace.h" 3446bc29b9SAdrian Hunter #include "util/tsc.h" 35f00898f4SAndi Kleen #include "util/parse-branch-options.h" 36bcc84ec6SStephane Eranian #include "util/parse-regs-options.h" 3771dc2326SWang Nan #include "util/llvm-utils.h" 388690a2a7SWang Nan #include "util/bpf-loader.h" 395f9cf599SWang Nan #include "util/trigger.h" 40a074865eSWang Nan #include "util/perf-hooks.h" 41*c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h" 4258db1d6eSArnaldo Carvalho de Melo #include "util/units.h" 43d8871ea7SWang Nan #include "asm/bug.h" 447c6a1c65SPeter Zijlstra 45a43783aeSArnaldo Carvalho de Melo #include <errno.h> 46fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 4786470930SIngo Molnar #include <unistd.h> 4886470930SIngo Molnar #include <sched.h> 499607ad3aSArnaldo Carvalho de Melo #include <signal.h> 50a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 512d11c650SWang Nan #include <asm/bug.h> 520693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 5378da39faSBernhard Rosenkraenzer 541b43b704SJiri Olsa struct switch_output { 55dc0c6127SJiri Olsa bool enabled; 561b43b704SJiri Olsa bool signal; 57dc0c6127SJiri Olsa unsigned long size; 58bfacbe3bSJiri Olsa unsigned long time; 59cb4e1ebbSJiri Olsa const char *str; 60cb4e1ebbSJiri Olsa bool set; 611b43b704SJiri Olsa }; 621b43b704SJiri Olsa 638c6f45a7SArnaldo Carvalho de Melo struct record { 6445694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 65b4006796SArnaldo Carvalho de Melo struct record_opts opts; 66d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 67f5fc1412SJiri Olsa struct perf_data_file file; 68ef149c25SAdrian Hunter struct auxtrace_record *itr; 69d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist; 70d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 71d20deb64SArnaldo Carvalho de Melo const char *progname; 72d20deb64SArnaldo Carvalho de Melo int realtime_prio; 73d20deb64SArnaldo Carvalho de Melo bool no_buildid; 74d2db9a98SWang Nan bool no_buildid_set; 75d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 76d2db9a98SWang Nan bool no_buildid_cache_set; 776156681bSNamhyung Kim bool buildid_all; 78ecfd7a9cSWang Nan bool timestamp_filename; 791b43b704SJiri Olsa struct switch_output switch_output; 809f065194SYang Shi unsigned long long samples; 810f82ebc4SArnaldo Carvalho de Melo }; 8286470930SIngo Molnar 83dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 84dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 85dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 86dc0c6127SJiri Olsa 87dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 88dc0c6127SJiri Olsa { 89dc0c6127SJiri Olsa return rec->switch_output.signal && 90dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 91dc0c6127SJiri Olsa } 92dc0c6127SJiri Olsa 93dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 94dc0c6127SJiri Olsa { 95dc0c6127SJiri Olsa return rec->switch_output.size && 96dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 97dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 98dc0c6127SJiri Olsa } 99dc0c6127SJiri Olsa 100bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 101bfacbe3bSJiri Olsa { 102bfacbe3bSJiri Olsa return rec->switch_output.time && 103bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 104bfacbe3bSJiri Olsa } 105bfacbe3bSJiri Olsa 1068c6f45a7SArnaldo Carvalho de Melo static int record__write(struct record *rec, void *bf, size_t size) 107f5970550SPeter Zijlstra { 108cf8b2e69SArnaldo Carvalho de Melo if (perf_data_file__write(rec->session->file, bf, size) < 0) { 1094f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 1108d3eca20SDavid Ahern return -1; 1118d3eca20SDavid Ahern } 112f5970550SPeter Zijlstra 113cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 114dc0c6127SJiri Olsa 115dc0c6127SJiri Olsa if (switch_output_size(rec)) 116dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 117dc0c6127SJiri Olsa 1188d3eca20SDavid Ahern return 0; 119f5970550SPeter Zijlstra } 120f5970550SPeter Zijlstra 12145694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 122d20deb64SArnaldo Carvalho de Melo union perf_event *event, 1231d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 1241d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 125234fbbf5SArnaldo Carvalho de Melo { 1268c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 1278c6f45a7SArnaldo Carvalho de Melo return record__write(rec, event, event->header.size); 128234fbbf5SArnaldo Carvalho de Melo } 129234fbbf5SArnaldo Carvalho de Melo 1303a62a7b8SWang Nan static int 1313a62a7b8SWang Nan backward_rb_find_range(void *buf, int mask, u64 head, u64 *start, u64 *end) 1323a62a7b8SWang Nan { 1333a62a7b8SWang Nan struct perf_event_header *pheader; 1343a62a7b8SWang Nan u64 evt_head = head; 1353a62a7b8SWang Nan int size = mask + 1; 1363a62a7b8SWang Nan 1373a62a7b8SWang Nan pr_debug2("backward_rb_find_range: buf=%p, head=%"PRIx64"\n", buf, head); 1383a62a7b8SWang Nan pheader = (struct perf_event_header *)(buf + (head & mask)); 1393a62a7b8SWang Nan *start = head; 1403a62a7b8SWang Nan while (true) { 1413a62a7b8SWang Nan if (evt_head - head >= (unsigned int)size) { 1425e30d55cSColin Ian King pr_debug("Finished reading backward ring buffer: rewind\n"); 1433a62a7b8SWang Nan if (evt_head - head > (unsigned int)size) 1443a62a7b8SWang Nan evt_head -= pheader->size; 1453a62a7b8SWang Nan *end = evt_head; 1463a62a7b8SWang Nan return 0; 1473a62a7b8SWang Nan } 1483a62a7b8SWang Nan 1493a62a7b8SWang Nan pheader = (struct perf_event_header *)(buf + (evt_head & mask)); 1503a62a7b8SWang Nan 1513a62a7b8SWang Nan if (pheader->size == 0) { 1525e30d55cSColin Ian King pr_debug("Finished reading backward ring buffer: get start\n"); 1533a62a7b8SWang Nan *end = evt_head; 1543a62a7b8SWang Nan return 0; 1553a62a7b8SWang Nan } 1563a62a7b8SWang Nan 1573a62a7b8SWang Nan evt_head += pheader->size; 1583a62a7b8SWang Nan pr_debug3("move evt_head: %"PRIx64"\n", evt_head); 1593a62a7b8SWang Nan } 1603a62a7b8SWang Nan WARN_ONCE(1, "Shouldn't get here\n"); 1613a62a7b8SWang Nan return -1; 1623a62a7b8SWang Nan } 1633a62a7b8SWang Nan 1643a62a7b8SWang Nan static int 165a4ea0ec4SWang Nan rb_find_range(void *data, int mask, u64 head, u64 old, 166a4ea0ec4SWang Nan u64 *start, u64 *end, bool backward) 1673a62a7b8SWang Nan { 168a4ea0ec4SWang Nan if (!backward) { 1693a62a7b8SWang Nan *start = old; 1703a62a7b8SWang Nan *end = head; 1713a62a7b8SWang Nan return 0; 1723a62a7b8SWang Nan } 1733a62a7b8SWang Nan 1743a62a7b8SWang Nan return backward_rb_find_range(data, mask, head, start, end); 1753a62a7b8SWang Nan } 1763a62a7b8SWang Nan 177a4ea0ec4SWang Nan static int 178a4ea0ec4SWang Nan record__mmap_read(struct record *rec, struct perf_mmap *md, 179a4ea0ec4SWang Nan bool overwrite, bool backward) 18086470930SIngo Molnar { 1817b8283b5SDavid Ahern u64 head = perf_mmap__read_head(md); 1827b8283b5SDavid Ahern u64 old = md->prev; 18309fa4f40SWang Nan u64 end = head, start = old; 184918512b4SJiri Olsa unsigned char *data = md->base + page_size; 18586470930SIngo Molnar unsigned long size; 18686470930SIngo Molnar void *buf; 1878d3eca20SDavid Ahern int rc = 0; 18886470930SIngo Molnar 189a4ea0ec4SWang Nan if (rb_find_range(data, md->mask, head, 190a4ea0ec4SWang Nan old, &start, &end, backward)) 1913a62a7b8SWang Nan return -1; 1923a62a7b8SWang Nan 19309fa4f40SWang Nan if (start == end) 1948d3eca20SDavid Ahern return 0; 19586470930SIngo Molnar 196d20deb64SArnaldo Carvalho de Melo rec->samples++; 19786470930SIngo Molnar 19809fa4f40SWang Nan size = end - start; 1992d11c650SWang Nan if (size > (unsigned long)(md->mask) + 1) { 2002d11c650SWang Nan WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n"); 2012d11c650SWang Nan 2022d11c650SWang Nan md->prev = head; 203a4ea0ec4SWang Nan perf_mmap__consume(md, overwrite || backward); 2042d11c650SWang Nan return 0; 2052d11c650SWang Nan } 20686470930SIngo Molnar 20709fa4f40SWang Nan if ((start & md->mask) + size != (end & md->mask)) { 20809fa4f40SWang Nan buf = &data[start & md->mask]; 20909fa4f40SWang Nan size = md->mask + 1 - (start & md->mask); 21009fa4f40SWang Nan start += size; 21186470930SIngo Molnar 2128c6f45a7SArnaldo Carvalho de Melo if (record__write(rec, buf, size) < 0) { 2138d3eca20SDavid Ahern rc = -1; 2148d3eca20SDavid Ahern goto out; 2158d3eca20SDavid Ahern } 21686470930SIngo Molnar } 21786470930SIngo Molnar 21809fa4f40SWang Nan buf = &data[start & md->mask]; 21909fa4f40SWang Nan size = end - start; 22009fa4f40SWang Nan start += size; 22186470930SIngo Molnar 2228c6f45a7SArnaldo Carvalho de Melo if (record__write(rec, buf, size) < 0) { 2238d3eca20SDavid Ahern rc = -1; 2248d3eca20SDavid Ahern goto out; 2258d3eca20SDavid Ahern } 22686470930SIngo Molnar 22709fa4f40SWang Nan md->prev = head; 228a4ea0ec4SWang Nan perf_mmap__consume(md, overwrite || backward); 2298d3eca20SDavid Ahern out: 2308d3eca20SDavid Ahern return rc; 23186470930SIngo Molnar } 23286470930SIngo Molnar 2332dd6d8a1SAdrian Hunter static volatile int done; 2342dd6d8a1SAdrian Hunter static volatile int signr = -1; 2352dd6d8a1SAdrian Hunter static volatile int child_finished; 236c0bdc1c4SWang Nan 2372dd6d8a1SAdrian Hunter static void sig_handler(int sig) 2382dd6d8a1SAdrian Hunter { 2392dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 2402dd6d8a1SAdrian Hunter child_finished = 1; 2412dd6d8a1SAdrian Hunter else 2422dd6d8a1SAdrian Hunter signr = sig; 2432dd6d8a1SAdrian Hunter 2442dd6d8a1SAdrian Hunter done = 1; 2452dd6d8a1SAdrian Hunter } 2462dd6d8a1SAdrian Hunter 247a074865eSWang Nan static void sigsegv_handler(int sig) 248a074865eSWang Nan { 249a074865eSWang Nan perf_hooks__recover(); 250a074865eSWang Nan sighandler_dump_stack(sig); 251a074865eSWang Nan } 252a074865eSWang Nan 2532dd6d8a1SAdrian Hunter static void record__sig_exit(void) 2542dd6d8a1SAdrian Hunter { 2552dd6d8a1SAdrian Hunter if (signr == -1) 2562dd6d8a1SAdrian Hunter return; 2572dd6d8a1SAdrian Hunter 2582dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 2592dd6d8a1SAdrian Hunter raise(signr); 2602dd6d8a1SAdrian Hunter } 2612dd6d8a1SAdrian Hunter 262e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 263e31f0d01SAdrian Hunter 264ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 265ef149c25SAdrian Hunter union perf_event *event, void *data1, 266ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 267ef149c25SAdrian Hunter { 268ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 26999fa2984SAdrian Hunter struct perf_data_file *file = &rec->file; 270ef149c25SAdrian Hunter size_t padding; 271ef149c25SAdrian Hunter u8 pad[8] = {0}; 272ef149c25SAdrian Hunter 27399fa2984SAdrian Hunter if (!perf_data_file__is_pipe(file)) { 27499fa2984SAdrian Hunter off_t file_offset; 27599fa2984SAdrian Hunter int fd = perf_data_file__fd(file); 27699fa2984SAdrian Hunter int err; 27799fa2984SAdrian Hunter 27899fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 27999fa2984SAdrian Hunter if (file_offset == -1) 28099fa2984SAdrian Hunter return -1; 28199fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 28299fa2984SAdrian Hunter event, file_offset); 28399fa2984SAdrian Hunter if (err) 28499fa2984SAdrian Hunter return err; 28599fa2984SAdrian Hunter } 28699fa2984SAdrian Hunter 287ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 288ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 289ef149c25SAdrian Hunter if (padding) 290ef149c25SAdrian Hunter padding = 8 - padding; 291ef149c25SAdrian Hunter 292ef149c25SAdrian Hunter record__write(rec, event, event->header.size); 293ef149c25SAdrian Hunter record__write(rec, data1, len1); 294ef149c25SAdrian Hunter if (len2) 295ef149c25SAdrian Hunter record__write(rec, data2, len2); 296ef149c25SAdrian Hunter record__write(rec, &pad, padding); 297ef149c25SAdrian Hunter 298ef149c25SAdrian Hunter return 0; 299ef149c25SAdrian Hunter } 300ef149c25SAdrian Hunter 301ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 302ef149c25SAdrian Hunter struct auxtrace_mmap *mm) 303ef149c25SAdrian Hunter { 304ef149c25SAdrian Hunter int ret; 305ef149c25SAdrian Hunter 306ef149c25SAdrian Hunter ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool, 307ef149c25SAdrian Hunter record__process_auxtrace); 308ef149c25SAdrian Hunter if (ret < 0) 309ef149c25SAdrian Hunter return ret; 310ef149c25SAdrian Hunter 311ef149c25SAdrian Hunter if (ret) 312ef149c25SAdrian Hunter rec->samples++; 313ef149c25SAdrian Hunter 314ef149c25SAdrian Hunter return 0; 315ef149c25SAdrian Hunter } 316ef149c25SAdrian Hunter 3172dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 3182dd6d8a1SAdrian Hunter struct auxtrace_mmap *mm) 3192dd6d8a1SAdrian Hunter { 3202dd6d8a1SAdrian Hunter int ret; 3212dd6d8a1SAdrian Hunter 3222dd6d8a1SAdrian Hunter ret = auxtrace_mmap__read_snapshot(mm, rec->itr, &rec->tool, 3232dd6d8a1SAdrian Hunter record__process_auxtrace, 3242dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 3252dd6d8a1SAdrian Hunter if (ret < 0) 3262dd6d8a1SAdrian Hunter return ret; 3272dd6d8a1SAdrian Hunter 3282dd6d8a1SAdrian Hunter if (ret) 3292dd6d8a1SAdrian Hunter rec->samples++; 3302dd6d8a1SAdrian Hunter 3312dd6d8a1SAdrian Hunter return 0; 3322dd6d8a1SAdrian Hunter } 3332dd6d8a1SAdrian Hunter 3342dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 3352dd6d8a1SAdrian Hunter { 3362dd6d8a1SAdrian Hunter int i; 3372dd6d8a1SAdrian Hunter int rc = 0; 3382dd6d8a1SAdrian Hunter 3392dd6d8a1SAdrian Hunter for (i = 0; i < rec->evlist->nr_mmaps; i++) { 3402dd6d8a1SAdrian Hunter struct auxtrace_mmap *mm = 3412dd6d8a1SAdrian Hunter &rec->evlist->mmap[i].auxtrace_mmap; 3422dd6d8a1SAdrian Hunter 3432dd6d8a1SAdrian Hunter if (!mm->base) 3442dd6d8a1SAdrian Hunter continue; 3452dd6d8a1SAdrian Hunter 3462dd6d8a1SAdrian Hunter if (record__auxtrace_mmap_read_snapshot(rec, mm) != 0) { 3472dd6d8a1SAdrian Hunter rc = -1; 3482dd6d8a1SAdrian Hunter goto out; 3492dd6d8a1SAdrian Hunter } 3502dd6d8a1SAdrian Hunter } 3512dd6d8a1SAdrian Hunter out: 3522dd6d8a1SAdrian Hunter return rc; 3532dd6d8a1SAdrian Hunter } 3542dd6d8a1SAdrian Hunter 3552dd6d8a1SAdrian Hunter static void record__read_auxtrace_snapshot(struct record *rec) 3562dd6d8a1SAdrian Hunter { 3572dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 3582dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 3595f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 3602dd6d8a1SAdrian Hunter } else { 3615f9cf599SWang Nan if (auxtrace_record__snapshot_finish(rec->itr)) 3625f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 3635f9cf599SWang Nan else 3645f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 3652dd6d8a1SAdrian Hunter } 3662dd6d8a1SAdrian Hunter } 3672dd6d8a1SAdrian Hunter 368e31f0d01SAdrian Hunter #else 369e31f0d01SAdrian Hunter 370e31f0d01SAdrian Hunter static inline 371e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 372e31f0d01SAdrian Hunter struct auxtrace_mmap *mm __maybe_unused) 373e31f0d01SAdrian Hunter { 374e31f0d01SAdrian Hunter return 0; 375e31f0d01SAdrian Hunter } 376e31f0d01SAdrian Hunter 3772dd6d8a1SAdrian Hunter static inline 3782dd6d8a1SAdrian Hunter void record__read_auxtrace_snapshot(struct record *rec __maybe_unused) 3792dd6d8a1SAdrian Hunter { 3802dd6d8a1SAdrian Hunter } 3812dd6d8a1SAdrian Hunter 3822dd6d8a1SAdrian Hunter static inline 3832dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 3842dd6d8a1SAdrian Hunter { 3852dd6d8a1SAdrian Hunter return 0; 3862dd6d8a1SAdrian Hunter } 3872dd6d8a1SAdrian Hunter 388e31f0d01SAdrian Hunter #endif 389e31f0d01SAdrian Hunter 390cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 391cda57a8cSWang Nan struct perf_evlist *evlist) 392cda57a8cSWang Nan { 393cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 394cda57a8cSWang Nan char msg[512]; 395cda57a8cSWang Nan 396cda57a8cSWang Nan if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false, 397cda57a8cSWang Nan opts->auxtrace_mmap_pages, 398cda57a8cSWang Nan opts->auxtrace_snapshot_mode) < 0) { 399cda57a8cSWang Nan if (errno == EPERM) { 400cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 401cda57a8cSWang Nan "Consider increasing " 402cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 403cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 404cda57a8cSWang Nan "(current value: %u,%u)\n", 405cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 406cda57a8cSWang Nan return -errno; 407cda57a8cSWang Nan } else { 408cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 409c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 410cda57a8cSWang Nan if (errno) 411cda57a8cSWang Nan return -errno; 412cda57a8cSWang Nan else 413cda57a8cSWang Nan return -EINVAL; 414cda57a8cSWang Nan } 415cda57a8cSWang Nan } 416cda57a8cSWang Nan return 0; 417cda57a8cSWang Nan } 418cda57a8cSWang Nan 419cda57a8cSWang Nan static int record__mmap(struct record *rec) 420cda57a8cSWang Nan { 421cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 422cda57a8cSWang Nan } 423cda57a8cSWang Nan 4248c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 425dd7927f4SArnaldo Carvalho de Melo { 426d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 4276a4bb04cSJiri Olsa struct perf_evsel *pos; 428d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist = rec->evlist; 429d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 430b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 4315d8bb1ecSMathieu Poirier struct perf_evsel_config_term *err_term; 4328d3eca20SDavid Ahern int rc = 0; 433dd7927f4SArnaldo Carvalho de Melo 434e68ae9cfSArnaldo Carvalho de Melo perf_evlist__config(evlist, opts, &callchain_param); 435cac21425SJiri Olsa 436e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 4373da297a6SIngo Molnar try_again: 438d988d5eeSKan Liang if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) { 43956e52e85SArnaldo Carvalho de Melo if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { 440bb963e16SNamhyung Kim if (verbose > 0) 441c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 4423da297a6SIngo Molnar goto try_again; 4433da297a6SIngo Molnar } 444ca6a4258SDavid Ahern 44556e52e85SArnaldo Carvalho de Melo rc = -errno; 44656e52e85SArnaldo Carvalho de Melo perf_evsel__open_strerror(pos, &opts->target, 44756e52e85SArnaldo Carvalho de Melo errno, msg, sizeof(msg)); 44856e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 4498d3eca20SDavid Ahern goto out; 4507c6a1c65SPeter Zijlstra } 4517c6a1c65SPeter Zijlstra } 4527c6a1c65SPeter Zijlstra 45323d4aad4SArnaldo Carvalho de Melo if (perf_evlist__apply_filters(evlist, &pos)) { 45423d4aad4SArnaldo Carvalho de Melo error("failed to set filter \"%s\" on event %s with %d (%s)\n", 45523d4aad4SArnaldo Carvalho de Melo pos->filter, perf_evsel__name(pos), errno, 456c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 4578d3eca20SDavid Ahern rc = -1; 4588d3eca20SDavid Ahern goto out; 4590a102479SFrederic Weisbecker } 4600a102479SFrederic Weisbecker 4615d8bb1ecSMathieu Poirier if (perf_evlist__apply_drv_configs(evlist, &pos, &err_term)) { 4625d8bb1ecSMathieu Poirier error("failed to set config \"%s\" on event %s with %d (%s)\n", 4635d8bb1ecSMathieu Poirier err_term->val.drv_cfg, perf_evsel__name(pos), errno, 4645d8bb1ecSMathieu Poirier str_error_r(errno, msg, sizeof(msg))); 4655d8bb1ecSMathieu Poirier rc = -1; 4665d8bb1ecSMathieu Poirier goto out; 4675d8bb1ecSMathieu Poirier } 4685d8bb1ecSMathieu Poirier 469cda57a8cSWang Nan rc = record__mmap(rec); 470cda57a8cSWang Nan if (rc) 4718d3eca20SDavid Ahern goto out; 4720a27d7f9SArnaldo Carvalho de Melo 473a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 4747b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 4758d3eca20SDavid Ahern out: 4768d3eca20SDavid Ahern return rc; 477a91e5431SArnaldo Carvalho de Melo } 478a91e5431SArnaldo Carvalho de Melo 479e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 480e3d59112SNamhyung Kim union perf_event *event, 481e3d59112SNamhyung Kim struct perf_sample *sample, 482e3d59112SNamhyung Kim struct perf_evsel *evsel, 483e3d59112SNamhyung Kim struct machine *machine) 484e3d59112SNamhyung Kim { 485e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 486e3d59112SNamhyung Kim 487e3d59112SNamhyung Kim rec->samples++; 488e3d59112SNamhyung Kim 489e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 490e3d59112SNamhyung Kim } 491e3d59112SNamhyung Kim 4928c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 4936122e4e4SArnaldo Carvalho de Melo { 494f5fc1412SJiri Olsa struct perf_data_file *file = &rec->file; 495f5fc1412SJiri Olsa struct perf_session *session = rec->session; 4966122e4e4SArnaldo Carvalho de Melo 497457ae94aSHe Kuang if (file->size == 0) 4989f591fd7SArnaldo Carvalho de Melo return 0; 4999f591fd7SArnaldo Carvalho de Melo 50000dc8657SNamhyung Kim /* 50100dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 50200dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 50300dc8657SNamhyung Kim * we prefer the vmlinux path like 50400dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 50500dc8657SNamhyung Kim * 50600dc8657SNamhyung Kim * rather than build-id path (in debug directory). 50700dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 50800dc8657SNamhyung Kim */ 50900dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 51000dc8657SNamhyung Kim 5116156681bSNamhyung Kim /* 5126156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 5136156681bSNamhyung Kim * so no need to process samples. 5146156681bSNamhyung Kim */ 5156156681bSNamhyung Kim if (rec->buildid_all) 5166156681bSNamhyung Kim rec->tool.sample = NULL; 5176156681bSNamhyung Kim 518b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 5196122e4e4SArnaldo Carvalho de Melo } 5206122e4e4SArnaldo Carvalho de Melo 5218115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 522a1645ce1SZhang, Yanmin { 523a1645ce1SZhang, Yanmin int err; 52445694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 525a1645ce1SZhang, Yanmin /* 526a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 527a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 528a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 529a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 530a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 531a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 532a1645ce1SZhang, Yanmin */ 53345694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 534743eb868SArnaldo Carvalho de Melo machine); 535a1645ce1SZhang, Yanmin if (err < 0) 536a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 53723346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 538a1645ce1SZhang, Yanmin 539a1645ce1SZhang, Yanmin /* 540a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 541a1645ce1SZhang, Yanmin * have no _text sometimes. 542a1645ce1SZhang, Yanmin */ 54345694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 5440ae617beSAdrian Hunter machine); 545a1645ce1SZhang, Yanmin if (err < 0) 546a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 54723346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 548a1645ce1SZhang, Yanmin } 549a1645ce1SZhang, Yanmin 55098402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 55198402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 55298402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 55398402807SFrederic Weisbecker }; 55498402807SFrederic Weisbecker 555a4ea0ec4SWang Nan static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist, 556a4ea0ec4SWang Nan bool backward) 55798402807SFrederic Weisbecker { 558dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 5590e2e63ddSPeter Zijlstra int i; 5608d3eca20SDavid Ahern int rc = 0; 561a4ea0ec4SWang Nan struct perf_mmap *maps; 56298402807SFrederic Weisbecker 563cb21686bSWang Nan if (!evlist) 564cb21686bSWang Nan return 0; 565ef149c25SAdrian Hunter 566b2cb615dSWang Nan maps = backward ? evlist->backward_mmap : evlist->mmap; 567a4ea0ec4SWang Nan if (!maps) 568a4ea0ec4SWang Nan return 0; 569cb21686bSWang Nan 57054cc54deSWang Nan if (backward && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 57154cc54deSWang Nan return 0; 57254cc54deSWang Nan 573a4ea0ec4SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 574a4ea0ec4SWang Nan struct auxtrace_mmap *mm = &maps[i].auxtrace_mmap; 575a4ea0ec4SWang Nan 576a4ea0ec4SWang Nan if (maps[i].base) { 577a4ea0ec4SWang Nan if (record__mmap_read(rec, &maps[i], 578a4ea0ec4SWang Nan evlist->overwrite, backward) != 0) { 5798d3eca20SDavid Ahern rc = -1; 5808d3eca20SDavid Ahern goto out; 5818d3eca20SDavid Ahern } 5828d3eca20SDavid Ahern } 583ef149c25SAdrian Hunter 5842dd6d8a1SAdrian Hunter if (mm->base && !rec->opts.auxtrace_snapshot_mode && 585ef149c25SAdrian Hunter record__auxtrace_mmap_read(rec, mm) != 0) { 586ef149c25SAdrian Hunter rc = -1; 587ef149c25SAdrian Hunter goto out; 588ef149c25SAdrian Hunter } 58998402807SFrederic Weisbecker } 59098402807SFrederic Weisbecker 591dcabb507SJiri Olsa /* 592dcabb507SJiri Olsa * Mark the round finished in case we wrote 593dcabb507SJiri Olsa * at least one event. 594dcabb507SJiri Olsa */ 595dcabb507SJiri Olsa if (bytes_written != rec->bytes_written) 5968c6f45a7SArnaldo Carvalho de Melo rc = record__write(rec, &finished_round_event, sizeof(finished_round_event)); 5978d3eca20SDavid Ahern 59854cc54deSWang Nan if (backward) 59954cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 6008d3eca20SDavid Ahern out: 6018d3eca20SDavid Ahern return rc; 60298402807SFrederic Weisbecker } 60398402807SFrederic Weisbecker 604cb21686bSWang Nan static int record__mmap_read_all(struct record *rec) 605cb21686bSWang Nan { 606cb21686bSWang Nan int err; 607cb21686bSWang Nan 608a4ea0ec4SWang Nan err = record__mmap_read_evlist(rec, rec->evlist, false); 609cb21686bSWang Nan if (err) 610cb21686bSWang Nan return err; 611cb21686bSWang Nan 61205737464SWang Nan return record__mmap_read_evlist(rec, rec->evlist, true); 613cb21686bSWang Nan } 614cb21686bSWang Nan 6158c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 61657706abcSDavid Ahern { 61757706abcSDavid Ahern struct perf_session *session = rec->session; 61857706abcSDavid Ahern int feat; 61957706abcSDavid Ahern 62057706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 62157706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 62257706abcSDavid Ahern 62357706abcSDavid Ahern if (rec->no_buildid) 62457706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 62557706abcSDavid Ahern 6263e2be2daSArnaldo Carvalho de Melo if (!have_tracepoints(&rec->evlist->entries)) 62757706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 62857706abcSDavid Ahern 62957706abcSDavid Ahern if (!rec->opts.branch_stack) 63057706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 631ef149c25SAdrian Hunter 632ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 633ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 634ffa517adSJiri Olsa 635ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 63657706abcSDavid Ahern } 63757706abcSDavid Ahern 638e1ab48baSWang Nan static void 639e1ab48baSWang Nan record__finish_output(struct record *rec) 640e1ab48baSWang Nan { 641e1ab48baSWang Nan struct perf_data_file *file = &rec->file; 642e1ab48baSWang Nan int fd = perf_data_file__fd(file); 643e1ab48baSWang Nan 644e1ab48baSWang Nan if (file->is_pipe) 645e1ab48baSWang Nan return; 646e1ab48baSWang Nan 647e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 648e1ab48baSWang Nan file->size = lseek(perf_data_file__fd(file), 0, SEEK_CUR); 649e1ab48baSWang Nan 650e1ab48baSWang Nan if (!rec->no_buildid) { 651e1ab48baSWang Nan process_buildids(rec); 652e1ab48baSWang Nan 653e1ab48baSWang Nan if (rec->buildid_all) 654e1ab48baSWang Nan dsos__hit_all(rec->session); 655e1ab48baSWang Nan } 656e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 657e1ab48baSWang Nan 658e1ab48baSWang Nan return; 659e1ab48baSWang Nan } 660e1ab48baSWang Nan 6614ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 662be7b0c9eSWang Nan { 6639d6aae72SArnaldo Carvalho de Melo int err; 6649d6aae72SArnaldo Carvalho de Melo struct thread_map *thread_map; 665be7b0c9eSWang Nan 6664ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 6674ea648aeSWang Nan return 0; 6684ea648aeSWang Nan 6699d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 6709d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 6719d6aae72SArnaldo Carvalho de Melo return -1; 6729d6aae72SArnaldo Carvalho de Melo 6739d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 674be7b0c9eSWang Nan process_synthesized_event, 675be7b0c9eSWang Nan &rec->session->machines.host, 676be7b0c9eSWang Nan rec->opts.sample_address, 677be7b0c9eSWang Nan rec->opts.proc_map_timeout); 6789d6aae72SArnaldo Carvalho de Melo thread_map__put(thread_map); 6799d6aae72SArnaldo Carvalho de Melo return err; 680be7b0c9eSWang Nan } 681be7b0c9eSWang Nan 6824ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 6833c1cb7e3SWang Nan 684ecfd7a9cSWang Nan static int 685ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 686ecfd7a9cSWang Nan { 687ecfd7a9cSWang Nan struct perf_data_file *file = &rec->file; 688ecfd7a9cSWang Nan int fd, err; 689ecfd7a9cSWang Nan 690ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 691ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 692ecfd7a9cSWang Nan 6934ea648aeSWang Nan record__synthesize(rec, true); 6944ea648aeSWang Nan if (target__none(&rec->opts.target)) 6954ea648aeSWang Nan record__synthesize_workload(rec, true); 6964ea648aeSWang Nan 697ecfd7a9cSWang Nan rec->samples = 0; 698ecfd7a9cSWang Nan record__finish_output(rec); 699ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 700ecfd7a9cSWang Nan if (err) { 701ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 702ecfd7a9cSWang Nan return -EINVAL; 703ecfd7a9cSWang Nan } 704ecfd7a9cSWang Nan 705ecfd7a9cSWang Nan fd = perf_data_file__switch(file, timestamp, 706ecfd7a9cSWang Nan rec->session->header.data_offset, 707ecfd7a9cSWang Nan at_exit); 708ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 709ecfd7a9cSWang Nan rec->bytes_written = 0; 710ecfd7a9cSWang Nan rec->session->header.data_size = 0; 711ecfd7a9cSWang Nan } 712ecfd7a9cSWang Nan 713ecfd7a9cSWang Nan if (!quiet) 714ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 715ecfd7a9cSWang Nan file->path, timestamp); 7163c1cb7e3SWang Nan 7173c1cb7e3SWang Nan /* Output tracking events */ 718be7b0c9eSWang Nan if (!at_exit) { 7194ea648aeSWang Nan record__synthesize(rec, false); 7203c1cb7e3SWang Nan 721be7b0c9eSWang Nan /* 722be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 723be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 724be7b0c9eSWang Nan * generate tracking events because there's no thread_map 725be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 726be7b0c9eSWang Nan * contain map and comm information. 727be7b0c9eSWang Nan * Create a fake thread_map and directly call 728be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 729be7b0c9eSWang Nan */ 730be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 7314ea648aeSWang Nan record__synthesize_workload(rec, false); 732be7b0c9eSWang Nan } 733ecfd7a9cSWang Nan return fd; 734ecfd7a9cSWang Nan } 735ecfd7a9cSWang Nan 736f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 737f33cbe72SArnaldo Carvalho de Melo 738f33cbe72SArnaldo Carvalho de Melo /* 739f33cbe72SArnaldo Carvalho de Melo * perf_evlist__prepare_workload will send a SIGUSR1 740f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 741f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 742f33cbe72SArnaldo Carvalho de Melo */ 74345604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 74445604710SNamhyung Kim siginfo_t *info, 745f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 746f33cbe72SArnaldo Carvalho de Melo { 747f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 748f33cbe72SArnaldo Carvalho de Melo done = 1; 749f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 750f33cbe72SArnaldo Carvalho de Melo } 751f33cbe72SArnaldo Carvalho de Melo 7522dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 753bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 7542dd6d8a1SAdrian Hunter 75546bc29b9SAdrian Hunter int __weak 75646bc29b9SAdrian Hunter perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused, 75746bc29b9SAdrian Hunter struct perf_tool *tool __maybe_unused, 75846bc29b9SAdrian Hunter perf_event__handler_t process __maybe_unused, 75946bc29b9SAdrian Hunter struct machine *machine __maybe_unused) 76046bc29b9SAdrian Hunter { 76146bc29b9SAdrian Hunter return 0; 76246bc29b9SAdrian Hunter } 76346bc29b9SAdrian Hunter 764ee667f94SWang Nan static const struct perf_event_mmap_page * 765ee667f94SWang Nan perf_evlist__pick_pc(struct perf_evlist *evlist) 766ee667f94SWang Nan { 767b2cb615dSWang Nan if (evlist) { 768b2cb615dSWang Nan if (evlist->mmap && evlist->mmap[0].base) 769ee667f94SWang Nan return evlist->mmap[0].base; 770b2cb615dSWang Nan if (evlist->backward_mmap && evlist->backward_mmap[0].base) 771b2cb615dSWang Nan return evlist->backward_mmap[0].base; 772b2cb615dSWang Nan } 773ee667f94SWang Nan return NULL; 774ee667f94SWang Nan } 775ee667f94SWang Nan 776c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 777c45628b0SWang Nan { 778ee667f94SWang Nan const struct perf_event_mmap_page *pc; 779ee667f94SWang Nan 780ee667f94SWang Nan pc = perf_evlist__pick_pc(rec->evlist); 781ee667f94SWang Nan if (pc) 782ee667f94SWang Nan return pc; 783c45628b0SWang Nan return NULL; 784c45628b0SWang Nan } 785c45628b0SWang Nan 7864ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 787c45c86ebSWang Nan { 788c45c86ebSWang Nan struct perf_session *session = rec->session; 789c45c86ebSWang Nan struct machine *machine = &session->machines.host; 790c45c86ebSWang Nan struct perf_data_file *file = &rec->file; 791c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 792c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 793c45c86ebSWang Nan int fd = perf_data_file__fd(file); 794c45c86ebSWang Nan int err = 0; 795c45c86ebSWang Nan 7964ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 7974ea648aeSWang Nan return 0; 7984ea648aeSWang Nan 799c45c86ebSWang Nan if (file->is_pipe) { 800c45c86ebSWang Nan err = perf_event__synthesize_attrs(tool, session, 801c45c86ebSWang Nan process_synthesized_event); 802c45c86ebSWang Nan if (err < 0) { 803c45c86ebSWang Nan pr_err("Couldn't synthesize attrs.\n"); 804c45c86ebSWang Nan goto out; 805c45c86ebSWang Nan } 806c45c86ebSWang Nan 807c45c86ebSWang Nan if (have_tracepoints(&rec->evlist->entries)) { 808c45c86ebSWang Nan /* 809c45c86ebSWang Nan * FIXME err <= 0 here actually means that 810c45c86ebSWang Nan * there were no tracepoints so its not really 811c45c86ebSWang Nan * an error, just that we don't need to 812c45c86ebSWang Nan * synthesize anything. We really have to 813c45c86ebSWang Nan * return this more properly and also 814c45c86ebSWang Nan * propagate errors that now are calling die() 815c45c86ebSWang Nan */ 816c45c86ebSWang Nan err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, 817c45c86ebSWang Nan process_synthesized_event); 818c45c86ebSWang Nan if (err <= 0) { 819c45c86ebSWang Nan pr_err("Couldn't record tracing data.\n"); 820c45c86ebSWang Nan goto out; 821c45c86ebSWang Nan } 822c45c86ebSWang Nan rec->bytes_written += err; 823c45c86ebSWang Nan } 824c45c86ebSWang Nan } 825c45c86ebSWang Nan 826c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 82746bc29b9SAdrian Hunter process_synthesized_event, machine); 82846bc29b9SAdrian Hunter if (err) 82946bc29b9SAdrian Hunter goto out; 83046bc29b9SAdrian Hunter 831c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 832c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 833c45c86ebSWang Nan session, process_synthesized_event); 834c45c86ebSWang Nan if (err) 835c45c86ebSWang Nan goto out; 836c45c86ebSWang Nan } 837c45c86ebSWang Nan 838c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 839c45c86ebSWang Nan machine); 840c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 841c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 842c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 843c45c86ebSWang Nan 844c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 845c45c86ebSWang Nan machine); 846c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 847c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 848c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 849c45c86ebSWang Nan 850c45c86ebSWang Nan if (perf_guest) { 851c45c86ebSWang Nan machines__process_guests(&session->machines, 852c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 853c45c86ebSWang Nan } 854c45c86ebSWang Nan 855c45c86ebSWang Nan err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, 856c45c86ebSWang Nan process_synthesized_event, opts->sample_address, 857c45c86ebSWang Nan opts->proc_map_timeout); 858c45c86ebSWang Nan out: 859c45c86ebSWang Nan return err; 860c45c86ebSWang Nan } 861c45c86ebSWang Nan 8628c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 86386470930SIngo Molnar { 86457706abcSDavid Ahern int err; 86545604710SNamhyung Kim int status = 0; 8668b412664SPeter Zijlstra unsigned long waking = 0; 86746be604bSZhang, Yanmin const bool forks = argc > 0; 86823346f21SArnaldo Carvalho de Melo struct machine *machine; 86945694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 870b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 871f5fc1412SJiri Olsa struct perf_data_file *file = &rec->file; 872d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 8736dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 87442aa276fSNamhyung Kim int fd; 87586470930SIngo Molnar 876d20deb64SArnaldo Carvalho de Melo rec->progname = argv[0]; 87733e49ea7SAndi Kleen 87845604710SNamhyung Kim atexit(record__sig_exit); 879f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 880f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 881804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 882a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 883c0bdc1c4SWang Nan 884f3b3614aSHari Bathini if (rec->opts.record_namespaces) 885f3b3614aSHari Bathini tool->namespace_events = true; 886f3b3614aSHari Bathini 887dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 8882dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 8893c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 8905f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 891dc0c6127SJiri Olsa if (rec->switch_output.enabled) 8923c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 893c0bdc1c4SWang Nan } else { 8942dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 895c0bdc1c4SWang Nan } 896f5970550SPeter Zijlstra 897b7b61cbeSArnaldo Carvalho de Melo session = perf_session__new(file, false, tool); 89894c744b6SArnaldo Carvalho de Melo if (session == NULL) { 899ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 900a9a70bbcSArnaldo Carvalho de Melo return -1; 901a9a70bbcSArnaldo Carvalho de Melo } 902a9a70bbcSArnaldo Carvalho de Melo 90342aa276fSNamhyung Kim fd = perf_data_file__fd(file); 904d20deb64SArnaldo Carvalho de Melo rec->session = session; 905d20deb64SArnaldo Carvalho de Melo 9068c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 907330aa675SStephane Eranian 908d4db3f16SArnaldo Carvalho de Melo if (forks) { 9093e2be2daSArnaldo Carvalho de Melo err = perf_evlist__prepare_workload(rec->evlist, &opts->target, 910f5fc1412SJiri Olsa argv, file->is_pipe, 911735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 91235b9d88eSArnaldo Carvalho de Melo if (err < 0) { 91335b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 91445604710SNamhyung Kim status = err; 91535b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 916856e9660SPeter Zijlstra } 917856e9660SPeter Zijlstra } 918856e9660SPeter Zijlstra 9198c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 9208d3eca20SDavid Ahern err = -1; 92145604710SNamhyung Kim goto out_child; 9228d3eca20SDavid Ahern } 92386470930SIngo Molnar 9248690a2a7SWang Nan err = bpf__apply_obj_config(); 9258690a2a7SWang Nan if (err) { 9268690a2a7SWang Nan char errbuf[BUFSIZ]; 9278690a2a7SWang Nan 9288690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 9298690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 9308690a2a7SWang Nan errbuf); 9318690a2a7SWang Nan goto out_child; 9328690a2a7SWang Nan } 9338690a2a7SWang Nan 934cca8482cSAdrian Hunter /* 935cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 936cca8482cSAdrian Hunter * evlist. 937cca8482cSAdrian Hunter */ 938cca8482cSAdrian Hunter if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) { 939cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 940cca8482cSAdrian Hunter rec->tool.ordered_events = false; 941cca8482cSAdrian Hunter } 942cca8482cSAdrian Hunter 9433e2be2daSArnaldo Carvalho de Melo if (!rec->evlist->nr_groups) 944a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 945a8bb559bSNamhyung Kim 946f5fc1412SJiri Olsa if (file->is_pipe) { 94742aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 948529870e3STom Zanussi if (err < 0) 94945604710SNamhyung Kim goto out_child; 950563aecb2SJiri Olsa } else { 95142aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 952d5eed904SArnaldo Carvalho de Melo if (err < 0) 95345604710SNamhyung Kim goto out_child; 954d5eed904SArnaldo Carvalho de Melo } 9557c6a1c65SPeter Zijlstra 956d3665498SDavid Ahern if (!rec->no_buildid 957e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 958d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 959e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 9608d3eca20SDavid Ahern err = -1; 96145604710SNamhyung Kim goto out_child; 962e20960c0SRobert Richter } 963e20960c0SRobert Richter 96434ba5122SArnaldo Carvalho de Melo machine = &session->machines.host; 965743eb868SArnaldo Carvalho de Melo 9664ea648aeSWang Nan err = record__synthesize(rec, false); 967c45c86ebSWang Nan if (err < 0) 96845604710SNamhyung Kim goto out_child; 9698d3eca20SDavid Ahern 970d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 97186470930SIngo Molnar struct sched_param param; 97286470930SIngo Molnar 973d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 97486470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 9756beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 9768d3eca20SDavid Ahern err = -1; 97745604710SNamhyung Kim goto out_child; 97886470930SIngo Molnar } 97986470930SIngo Molnar } 98086470930SIngo Molnar 981774cb499SJiri Olsa /* 982774cb499SJiri Olsa * When perf is starting the traced process, all the events 983774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 984774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 985774cb499SJiri Olsa */ 9866619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 9873e2be2daSArnaldo Carvalho de Melo perf_evlist__enable(rec->evlist); 988764e16a3SDavid Ahern 989856e9660SPeter Zijlstra /* 990856e9660SPeter Zijlstra * Let the child rip 991856e9660SPeter Zijlstra */ 992e803cf97SNamhyung Kim if (forks) { 993e5bed564SNamhyung Kim union perf_event *event; 994e907caf3SHari Bathini pid_t tgid; 995e5bed564SNamhyung Kim 996e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 997e5bed564SNamhyung Kim if (event == NULL) { 998e5bed564SNamhyung Kim err = -ENOMEM; 999e5bed564SNamhyung Kim goto out_child; 1000e5bed564SNamhyung Kim } 1001e5bed564SNamhyung Kim 1002e803cf97SNamhyung Kim /* 1003e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 1004e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 1005e803cf97SNamhyung Kim * cannot see a correct process name for those events. 1006e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 1007e803cf97SNamhyung Kim */ 1008e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 1009e803cf97SNamhyung Kim rec->evlist->workload.pid, 1010e803cf97SNamhyung Kim process_synthesized_event, 1011e803cf97SNamhyung Kim machine); 1012e5bed564SNamhyung Kim free(event); 1013e803cf97SNamhyung Kim 1014e907caf3SHari Bathini if (tgid == -1) 1015e907caf3SHari Bathini goto out_child; 1016e907caf3SHari Bathini 1017e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 1018e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 1019e907caf3SHari Bathini machine->id_hdr_size); 1020e907caf3SHari Bathini if (event == NULL) { 1021e907caf3SHari Bathini err = -ENOMEM; 1022e907caf3SHari Bathini goto out_child; 1023e907caf3SHari Bathini } 1024e907caf3SHari Bathini 1025e907caf3SHari Bathini /* 1026e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 1027e907caf3SHari Bathini */ 1028e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 1029e907caf3SHari Bathini rec->evlist->workload.pid, 1030e907caf3SHari Bathini tgid, process_synthesized_event, 1031e907caf3SHari Bathini machine); 1032e907caf3SHari Bathini free(event); 1033e907caf3SHari Bathini 10343e2be2daSArnaldo Carvalho de Melo perf_evlist__start_workload(rec->evlist); 1035e803cf97SNamhyung Kim } 1036856e9660SPeter Zijlstra 10376619a53eSAndi Kleen if (opts->initial_delay) { 10380693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 10396619a53eSAndi Kleen perf_evlist__enable(rec->evlist); 10406619a53eSAndi Kleen } 10416619a53eSAndi Kleen 10425f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 10433c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 1044a074865eSWang Nan perf_hooks__invoke_record_start(); 1045649c48a9SPeter Zijlstra for (;;) { 10469f065194SYang Shi unsigned long long hits = rec->samples; 104786470930SIngo Molnar 104805737464SWang Nan /* 104905737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 105005737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 105105737464SWang Nan * hits != rec->samples in previous round. 105205737464SWang Nan * 105305737464SWang Nan * perf_evlist__toggle_bkw_mmap ensure we never 105405737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 105505737464SWang Nan */ 105605737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 105705737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 105805737464SWang Nan 10598c6f45a7SArnaldo Carvalho de Melo if (record__mmap_read_all(rec) < 0) { 10605f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 10613c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 10628d3eca20SDavid Ahern err = -1; 106345604710SNamhyung Kim goto out_child; 10648d3eca20SDavid Ahern } 106586470930SIngo Molnar 10662dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 10672dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 10685f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 10692dd6d8a1SAdrian Hunter record__read_auxtrace_snapshot(rec); 10705f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 10712dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 10722dd6d8a1SAdrian Hunter err = -1; 10732dd6d8a1SAdrian Hunter goto out_child; 10742dd6d8a1SAdrian Hunter } 10752dd6d8a1SAdrian Hunter } 10762dd6d8a1SAdrian Hunter 10773c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 107805737464SWang Nan /* 107905737464SWang Nan * If switch_output_trigger is hit, the data in 108005737464SWang Nan * overwritable ring buffer should have been collected, 108105737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 108205737464SWang Nan * 108305737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 108405737464SWang Nan * record__mmap_read_all() didn't collect data from 108505737464SWang Nan * overwritable ring buffer. Read again. 108605737464SWang Nan */ 108705737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 108805737464SWang Nan continue; 10893c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 10903c1cb7e3SWang Nan 109105737464SWang Nan /* 109205737464SWang Nan * Reenable events in overwrite ring buffer after 109305737464SWang Nan * record__mmap_read_all(): we should have collected 109405737464SWang Nan * data from it. 109505737464SWang Nan */ 109605737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 109705737464SWang Nan 10983c1cb7e3SWang Nan if (!quiet) 10993c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 11003c1cb7e3SWang Nan waking); 11013c1cb7e3SWang Nan waking = 0; 11023c1cb7e3SWang Nan fd = record__switch_output(rec, false); 11033c1cb7e3SWang Nan if (fd < 0) { 11043c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 11053c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 11063c1cb7e3SWang Nan err = fd; 11073c1cb7e3SWang Nan goto out_child; 11083c1cb7e3SWang Nan } 1109bfacbe3bSJiri Olsa 1110bfacbe3bSJiri Olsa /* re-arm the alarm */ 1111bfacbe3bSJiri Olsa if (rec->switch_output.time) 1112bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 11133c1cb7e3SWang Nan } 11143c1cb7e3SWang Nan 1115d20deb64SArnaldo Carvalho de Melo if (hits == rec->samples) { 11166dcf45efSArnaldo Carvalho de Melo if (done || draining) 1117649c48a9SPeter Zijlstra break; 1118f66a889dSArnaldo Carvalho de Melo err = perf_evlist__poll(rec->evlist, -1); 1119a515114fSJiri Olsa /* 1120a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 1121a515114fSJiri Olsa * number of returned events and interrupt error. 1122a515114fSJiri Olsa */ 1123a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 112445604710SNamhyung Kim err = 0; 11258b412664SPeter Zijlstra waking++; 11266dcf45efSArnaldo Carvalho de Melo 11276dcf45efSArnaldo Carvalho de Melo if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) 11286dcf45efSArnaldo Carvalho de Melo draining = true; 11298b412664SPeter Zijlstra } 11308b412664SPeter Zijlstra 1131774cb499SJiri Olsa /* 1132774cb499SJiri Olsa * When perf is starting the traced process, at the end events 1133774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 1134774cb499SJiri Olsa * disable events in this case. 1135774cb499SJiri Olsa */ 1136602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 11375f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 11383e2be2daSArnaldo Carvalho de Melo perf_evlist__disable(rec->evlist); 11392711926aSJiri Olsa disabled = true; 11402711926aSJiri Olsa } 11418b412664SPeter Zijlstra } 11425f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 11433c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 11448b412664SPeter Zijlstra 1145f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 114635550da3SMasami Hiramatsu char msg[STRERR_BUFSIZE]; 1147c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 1148f33cbe72SArnaldo Carvalho de Melo pr_err("Workload failed: %s\n", emsg); 1149f33cbe72SArnaldo Carvalho de Melo err = -1; 115045604710SNamhyung Kim goto out_child; 1151f33cbe72SArnaldo Carvalho de Melo } 1152f33cbe72SArnaldo Carvalho de Melo 1153e3d59112SNamhyung Kim if (!quiet) 11548b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 115586470930SIngo Molnar 11564ea648aeSWang Nan if (target__none(&rec->opts.target)) 11574ea648aeSWang Nan record__synthesize_workload(rec, true); 11584ea648aeSWang Nan 115945604710SNamhyung Kim out_child: 116045604710SNamhyung Kim if (forks) { 116145604710SNamhyung Kim int exit_status; 116245604710SNamhyung Kim 116345604710SNamhyung Kim if (!child_finished) 116445604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 116545604710SNamhyung Kim 116645604710SNamhyung Kim wait(&exit_status); 116745604710SNamhyung Kim 116845604710SNamhyung Kim if (err < 0) 116945604710SNamhyung Kim status = err; 117045604710SNamhyung Kim else if (WIFEXITED(exit_status)) 117145604710SNamhyung Kim status = WEXITSTATUS(exit_status); 117245604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 117345604710SNamhyung Kim signr = WTERMSIG(exit_status); 117445604710SNamhyung Kim } else 117545604710SNamhyung Kim status = err; 117645604710SNamhyung Kim 11774ea648aeSWang Nan record__synthesize(rec, true); 1178e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 1179e3d59112SNamhyung Kim rec->samples = 0; 1180e3d59112SNamhyung Kim 1181ecfd7a9cSWang Nan if (!err) { 1182ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 1183e1ab48baSWang Nan record__finish_output(rec); 1184ecfd7a9cSWang Nan } else { 1185ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 1186ecfd7a9cSWang Nan if (fd < 0) { 1187ecfd7a9cSWang Nan status = fd; 1188ecfd7a9cSWang Nan goto out_delete_session; 1189ecfd7a9cSWang Nan } 1190ecfd7a9cSWang Nan } 1191ecfd7a9cSWang Nan } 119239d17dacSArnaldo Carvalho de Melo 1193a074865eSWang Nan perf_hooks__invoke_record_end(); 1194a074865eSWang Nan 1195e3d59112SNamhyung Kim if (!err && !quiet) { 1196e3d59112SNamhyung Kim char samples[128]; 1197ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 1198ecfd7a9cSWang Nan ".<timestamp>" : ""; 1199e3d59112SNamhyung Kim 1200ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 1201e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 1202e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 1203e3d59112SNamhyung Kim else 1204e3d59112SNamhyung Kim samples[0] = '\0'; 1205e3d59112SNamhyung Kim 1206ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n", 1207e3d59112SNamhyung Kim perf_data_file__size(file) / 1024.0 / 1024.0, 1208ecfd7a9cSWang Nan file->path, postfix, samples); 1209e3d59112SNamhyung Kim } 1210e3d59112SNamhyung Kim 121139d17dacSArnaldo Carvalho de Melo out_delete_session: 121239d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 121345604710SNamhyung Kim return status; 121486470930SIngo Molnar } 121586470930SIngo Molnar 12160883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 121709b0fd45SJiri Olsa { 1218aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 1219a601fdffSJiri Olsa 12200883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 122126d33022SJiri Olsa 12220883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 122309b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 12240883e820SArnaldo Carvalho de Melo callchain->dump_size); 12250883e820SArnaldo Carvalho de Melo } 12260883e820SArnaldo Carvalho de Melo 12270883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 12280883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 12290883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 12300883e820SArnaldo Carvalho de Melo { 12310883e820SArnaldo Carvalho de Melo int ret; 12320883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 12330883e820SArnaldo Carvalho de Melo 12340883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 12350883e820SArnaldo Carvalho de Melo if (unset) { 12360883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 12370883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 12380883e820SArnaldo Carvalho de Melo return 0; 12390883e820SArnaldo Carvalho de Melo } 12400883e820SArnaldo Carvalho de Melo 12410883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 12420883e820SArnaldo Carvalho de Melo if (!ret) { 12430883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 12440883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 12450883e820SArnaldo Carvalho de Melo record->sample_address = true; 12460883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 12470883e820SArnaldo Carvalho de Melo } 12480883e820SArnaldo Carvalho de Melo 12490883e820SArnaldo Carvalho de Melo return ret; 125009b0fd45SJiri Olsa } 125109b0fd45SJiri Olsa 1252c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 125309b0fd45SJiri Olsa const char *arg, 125409b0fd45SJiri Olsa int unset) 125509b0fd45SJiri Olsa { 12560883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 125726d33022SJiri Olsa } 125826d33022SJiri Olsa 1259c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 126009b0fd45SJiri Olsa const char *arg __maybe_unused, 126109b0fd45SJiri Olsa int unset __maybe_unused) 126209b0fd45SJiri Olsa { 12632ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 1264c421e80bSKan Liang 12652ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 126609b0fd45SJiri Olsa 12672ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 12682ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 1269eb853e80SJiri Olsa 12702ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 127109b0fd45SJiri Olsa return 0; 127209b0fd45SJiri Olsa } 127309b0fd45SJiri Olsa 1274eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 1275eb853e80SJiri Olsa { 12767a29c087SNamhyung Kim struct record *rec = cb; 12777a29c087SNamhyung Kim 12787a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 12797a29c087SNamhyung Kim if (!strcmp(value, "cache")) 12807a29c087SNamhyung Kim rec->no_buildid_cache = false; 12817a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 12827a29c087SNamhyung Kim rec->no_buildid_cache = true; 12837a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 12847a29c087SNamhyung Kim rec->no_buildid = true; 12857a29c087SNamhyung Kim else 12867a29c087SNamhyung Kim return -1; 12877a29c087SNamhyung Kim return 0; 12887a29c087SNamhyung Kim } 1289eb853e80SJiri Olsa if (!strcmp(var, "record.call-graph")) 12905a2e5e85SNamhyung Kim var = "call-graph.record-mode"; /* fall-through */ 1291eb853e80SJiri Olsa 1292eb853e80SJiri Olsa return perf_default_config(var, value, cb); 1293eb853e80SJiri Olsa } 1294eb853e80SJiri Olsa 1295814c8c38SPeter Zijlstra struct clockid_map { 1296814c8c38SPeter Zijlstra const char *name; 1297814c8c38SPeter Zijlstra int clockid; 1298814c8c38SPeter Zijlstra }; 1299814c8c38SPeter Zijlstra 1300814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c) \ 1301814c8c38SPeter Zijlstra { .name = n, .clockid = (c), } 1302814c8c38SPeter Zijlstra 1303814c8c38SPeter Zijlstra #define CLOCKID_END { .name = NULL, } 1304814c8c38SPeter Zijlstra 1305814c8c38SPeter Zijlstra 1306814c8c38SPeter Zijlstra /* 1307814c8c38SPeter Zijlstra * Add the missing ones, we need to build on many distros... 1308814c8c38SPeter Zijlstra */ 1309814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW 1310814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4 1311814c8c38SPeter Zijlstra #endif 1312814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME 1313814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7 1314814c8c38SPeter Zijlstra #endif 1315814c8c38SPeter Zijlstra #ifndef CLOCK_TAI 1316814c8c38SPeter Zijlstra #define CLOCK_TAI 11 1317814c8c38SPeter Zijlstra #endif 1318814c8c38SPeter Zijlstra 1319814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = { 1320814c8c38SPeter Zijlstra /* available for all events, NMI safe */ 1321814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), 1322814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), 1323814c8c38SPeter Zijlstra 1324814c8c38SPeter Zijlstra /* available for some events */ 1325814c8c38SPeter Zijlstra CLOCKID_MAP("realtime", CLOCK_REALTIME), 1326814c8c38SPeter Zijlstra CLOCKID_MAP("boottime", CLOCK_BOOTTIME), 1327814c8c38SPeter Zijlstra CLOCKID_MAP("tai", CLOCK_TAI), 1328814c8c38SPeter Zijlstra 1329814c8c38SPeter Zijlstra /* available for the lazy */ 1330814c8c38SPeter Zijlstra CLOCKID_MAP("mono", CLOCK_MONOTONIC), 1331814c8c38SPeter Zijlstra CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), 1332814c8c38SPeter Zijlstra CLOCKID_MAP("real", CLOCK_REALTIME), 1333814c8c38SPeter Zijlstra CLOCKID_MAP("boot", CLOCK_BOOTTIME), 1334814c8c38SPeter Zijlstra 1335814c8c38SPeter Zijlstra CLOCKID_END, 1336814c8c38SPeter Zijlstra }; 1337814c8c38SPeter Zijlstra 1338814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset) 1339814c8c38SPeter Zijlstra { 1340814c8c38SPeter Zijlstra struct record_opts *opts = (struct record_opts *)opt->value; 1341814c8c38SPeter Zijlstra const struct clockid_map *cm; 1342814c8c38SPeter Zijlstra const char *ostr = str; 1343814c8c38SPeter Zijlstra 1344814c8c38SPeter Zijlstra if (unset) { 1345814c8c38SPeter Zijlstra opts->use_clockid = 0; 1346814c8c38SPeter Zijlstra return 0; 1347814c8c38SPeter Zijlstra } 1348814c8c38SPeter Zijlstra 1349814c8c38SPeter Zijlstra /* no arg passed */ 1350814c8c38SPeter Zijlstra if (!str) 1351814c8c38SPeter Zijlstra return 0; 1352814c8c38SPeter Zijlstra 1353814c8c38SPeter Zijlstra /* no setting it twice */ 1354814c8c38SPeter Zijlstra if (opts->use_clockid) 1355814c8c38SPeter Zijlstra return -1; 1356814c8c38SPeter Zijlstra 1357814c8c38SPeter Zijlstra opts->use_clockid = true; 1358814c8c38SPeter Zijlstra 1359814c8c38SPeter Zijlstra /* if its a number, we're done */ 1360814c8c38SPeter Zijlstra if (sscanf(str, "%d", &opts->clockid) == 1) 1361814c8c38SPeter Zijlstra return 0; 1362814c8c38SPeter Zijlstra 1363814c8c38SPeter Zijlstra /* allow a "CLOCK_" prefix to the name */ 1364814c8c38SPeter Zijlstra if (!strncasecmp(str, "CLOCK_", 6)) 1365814c8c38SPeter Zijlstra str += 6; 1366814c8c38SPeter Zijlstra 1367814c8c38SPeter Zijlstra for (cm = clockids; cm->name; cm++) { 1368814c8c38SPeter Zijlstra if (!strcasecmp(str, cm->name)) { 1369814c8c38SPeter Zijlstra opts->clockid = cm->clockid; 1370814c8c38SPeter Zijlstra return 0; 1371814c8c38SPeter Zijlstra } 1372814c8c38SPeter Zijlstra } 1373814c8c38SPeter Zijlstra 1374814c8c38SPeter Zijlstra opts->use_clockid = false; 1375814c8c38SPeter Zijlstra ui__warning("unknown clockid %s, check man page\n", ostr); 1376814c8c38SPeter Zijlstra return -1; 1377814c8c38SPeter Zijlstra } 1378814c8c38SPeter Zijlstra 1379e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 1380e9db1310SAdrian Hunter const char *str, 1381e9db1310SAdrian Hunter int unset __maybe_unused) 1382e9db1310SAdrian Hunter { 1383e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 1384e9db1310SAdrian Hunter char *s, *p; 1385e9db1310SAdrian Hunter unsigned int mmap_pages; 1386e9db1310SAdrian Hunter int ret; 1387e9db1310SAdrian Hunter 1388e9db1310SAdrian Hunter if (!str) 1389e9db1310SAdrian Hunter return -EINVAL; 1390e9db1310SAdrian Hunter 1391e9db1310SAdrian Hunter s = strdup(str); 1392e9db1310SAdrian Hunter if (!s) 1393e9db1310SAdrian Hunter return -ENOMEM; 1394e9db1310SAdrian Hunter 1395e9db1310SAdrian Hunter p = strchr(s, ','); 1396e9db1310SAdrian Hunter if (p) 1397e9db1310SAdrian Hunter *p = '\0'; 1398e9db1310SAdrian Hunter 1399e9db1310SAdrian Hunter if (*s) { 1400e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); 1401e9db1310SAdrian Hunter if (ret) 1402e9db1310SAdrian Hunter goto out_free; 1403e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 1404e9db1310SAdrian Hunter } 1405e9db1310SAdrian Hunter 1406e9db1310SAdrian Hunter if (!p) { 1407e9db1310SAdrian Hunter ret = 0; 1408e9db1310SAdrian Hunter goto out_free; 1409e9db1310SAdrian Hunter } 1410e9db1310SAdrian Hunter 1411e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); 1412e9db1310SAdrian Hunter if (ret) 1413e9db1310SAdrian Hunter goto out_free; 1414e9db1310SAdrian Hunter 1415e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 1416e9db1310SAdrian Hunter 1417e9db1310SAdrian Hunter out_free: 1418e9db1310SAdrian Hunter free(s); 1419e9db1310SAdrian Hunter return ret; 1420e9db1310SAdrian Hunter } 1421e9db1310SAdrian Hunter 14220c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 14230c582449SJiri Olsa { 14240c582449SJiri Olsa u64 wakeup_size = perf_evlist__mmap_size(rec->opts.mmap_pages); 14250c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 14260c582449SJiri Olsa 14270c582449SJiri Olsa wakeup_size /= 2; 14280c582449SJiri Olsa 14290c582449SJiri Olsa if (s->size < wakeup_size) { 14300c582449SJiri Olsa char buf[100]; 14310c582449SJiri Olsa 14320c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 14330c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 14340c582449SJiri Olsa "wakeup kernel buffer size (%s) " 14350c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 14360c582449SJiri Olsa } 14370c582449SJiri Olsa } 14380c582449SJiri Olsa 1439cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 1440cb4e1ebbSJiri Olsa { 1441cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 1442dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 1443dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 1444dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 1445dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 1446dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 1447dc0c6127SJiri Olsa { .tag = 0 }, 1448dc0c6127SJiri Olsa }; 1449bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 1450bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 1451bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 1452bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 1453bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 1454bfacbe3bSJiri Olsa { .tag = 0 }, 1455bfacbe3bSJiri Olsa }; 1456dc0c6127SJiri Olsa unsigned long val; 1457cb4e1ebbSJiri Olsa 1458cb4e1ebbSJiri Olsa if (!s->set) 1459cb4e1ebbSJiri Olsa return 0; 1460cb4e1ebbSJiri Olsa 1461cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 1462cb4e1ebbSJiri Olsa s->signal = true; 1463cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 1464dc0c6127SJiri Olsa goto enabled; 1465dc0c6127SJiri Olsa } 1466dc0c6127SJiri Olsa 1467dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 1468dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 1469dc0c6127SJiri Olsa s->size = val; 1470dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 1471dc0c6127SJiri Olsa goto enabled; 1472cb4e1ebbSJiri Olsa } 1473cb4e1ebbSJiri Olsa 1474bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 1475bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 1476bfacbe3bSJiri Olsa s->time = val; 1477bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 1478bfacbe3bSJiri Olsa s->str, s->time); 1479bfacbe3bSJiri Olsa goto enabled; 1480bfacbe3bSJiri Olsa } 1481bfacbe3bSJiri Olsa 1482cb4e1ebbSJiri Olsa return -1; 1483dc0c6127SJiri Olsa 1484dc0c6127SJiri Olsa enabled: 1485dc0c6127SJiri Olsa rec->timestamp_filename = true; 1486dc0c6127SJiri Olsa s->enabled = true; 14870c582449SJiri Olsa 14880c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 14890c582449SJiri Olsa switch_output_size_warn(rec); 14900c582449SJiri Olsa 1491dc0c6127SJiri Olsa return 0; 1492cb4e1ebbSJiri Olsa } 1493cb4e1ebbSJiri Olsa 1494e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 149586470930SIngo Molnar "perf record [<options>] [<command>]", 149686470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 149786470930SIngo Molnar NULL 149886470930SIngo Molnar }; 1499e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 150086470930SIngo Molnar 1501d20deb64SArnaldo Carvalho de Melo /* 15028c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 15038c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 1504d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 1505d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 1506d20deb64SArnaldo Carvalho de Melo * 1507d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 1508d20deb64SArnaldo Carvalho de Melo * 1509d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 1510d20deb64SArnaldo Carvalho de Melo */ 15118c6f45a7SArnaldo Carvalho de Melo static struct record record = { 1512d20deb64SArnaldo Carvalho de Melo .opts = { 15138affc2b8SAndi Kleen .sample_time = true, 1514d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 1515d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 1516d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 1517447a6013SArnaldo Carvalho de Melo .freq = 4000, 1518d1cb9fceSNamhyung Kim .target = { 1519d1cb9fceSNamhyung Kim .uses_mmap = true, 15203aa5939dSAdrian Hunter .default_per_cpu = true, 1521d1cb9fceSNamhyung Kim }, 15229d9cad76SKan Liang .proc_map_timeout = 500, 1523d20deb64SArnaldo Carvalho de Melo }, 1524e3d59112SNamhyung Kim .tool = { 1525e3d59112SNamhyung Kim .sample = process_sample_event, 1526e3d59112SNamhyung Kim .fork = perf_event__process_fork, 1527cca8482cSAdrian Hunter .exit = perf_event__process_exit, 1528e3d59112SNamhyung Kim .comm = perf_event__process_comm, 1529f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 1530e3d59112SNamhyung Kim .mmap = perf_event__process_mmap, 1531e3d59112SNamhyung Kim .mmap2 = perf_event__process_mmap2, 1532cca8482cSAdrian Hunter .ordered_events = true, 1533e3d59112SNamhyung Kim }, 1534d20deb64SArnaldo Carvalho de Melo }; 15357865e817SFrederic Weisbecker 153676a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 153776a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 153861eaa3beSArnaldo Carvalho de Melo 15390aab2136SWang Nan static bool dry_run; 15400aab2136SWang Nan 1541d20deb64SArnaldo Carvalho de Melo /* 1542d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 1543d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 1544b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 1545d20deb64SArnaldo Carvalho de Melo * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 1546d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 1547d20deb64SArnaldo Carvalho de Melo */ 1548efd21307SJiri Olsa static struct option __record_options[] = { 1549d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 155086470930SIngo Molnar "event selector. use 'perf list' to list available events", 1551f120f9d5SJiri Olsa parse_events_option), 1552d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 1553c171b552SLi Zefan "event filter", parse_filter), 15544ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 15554ba1faa1SWang Nan NULL, "don't record events from perf itself", 15564ba1faa1SWang Nan exclude_perf), 1557bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 1558d6d901c2SZhang, Yanmin "record events on existing process id"), 1559bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 1560d6d901c2SZhang, Yanmin "record events on existing thread id"), 1561d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 156286470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 1563509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 1564acac03faSKirill Smelkov "collect data without buffering"), 1565d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 1566daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 1567bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 156886470930SIngo Molnar "system-wide collection from all CPUs"), 1569bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 1570c45c6ea2SStephane Eranian "list of cpus to monitor"), 1571d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 1572f5fc1412SJiri Olsa OPT_STRING('o', "output", &record.file.path, "file", 157386470930SIngo Molnar "output file name"), 157469e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 157569e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 15762e6cdf99SStephane Eranian "child tasks do not inherit counters"), 15774ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 15784ea648aeSWang Nan "synthesize non-sample events at the end of output"), 1579626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 1580d20deb64SArnaldo Carvalho de Melo OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"), 1581e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 1582e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 1583e9db1310SAdrian Hunter record__parse_mmap_pages), 1584d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 158543bece79SLin Ming "put the counters into a counter group"), 15862ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 158709b0fd45SJiri Olsa NULL, "enables call-graph recording" , 158809b0fd45SJiri Olsa &record_callchain_opt), 158909b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 159076a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 159109b0fd45SJiri Olsa &record_parse_callchain_opt), 1592c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 15933da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 1594b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 1595d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 1596649c48a9SPeter Zijlstra "per thread counts"), 159756100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 1598b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 15993abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 16003abebc55SAdrian Hunter &record.opts.sample_time_set, 16013abebc55SAdrian Hunter "Record the sample timestamps"), 160256100321SPeter Zijlstra OPT_BOOLEAN('P', "period", &record.opts.period, "Record the sample period"), 1603d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 1604649c48a9SPeter Zijlstra "don't sample"), 1605d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 1606d2db9a98SWang Nan &record.no_buildid_cache_set, 1607a1ac1d3cSStephane Eranian "do not update the buildid cache"), 1608d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 1609d2db9a98SWang Nan &record.no_buildid_set, 1610baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 1611d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 1612023695d9SStephane Eranian "monitor event in cgroup name only", 1613023695d9SStephane Eranian parse_cgroups), 1614a6205a35SArnaldo Carvalho de Melo OPT_UINTEGER('D', "delay", &record.opts.initial_delay, 16156619a53eSAndi Kleen "ms to wait before starting measurement after program start"), 1616bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 1617bea03405SNamhyung Kim "user to profile"), 1618a5aabdacSStephane Eranian 1619a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 1620a5aabdacSStephane Eranian "branch any", "sample any taken branches", 1621a5aabdacSStephane Eranian parse_branch_stack), 1622a5aabdacSStephane Eranian 1623a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 1624a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 1625bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 162605484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 162705484298SAndi Kleen "sample by weight (on special events only)"), 1628475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 1629475eeab9SAndi Kleen "sample transaction flags (special events only)"), 16303aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 16313aa5939dSAdrian Hunter "use per-thread mmaps"), 1632bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 1633bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 1634bcc84ec6SStephane Eranian " use -I ? to list register names", parse_regs), 163585c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 163685c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 1637814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 1638814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 1639814c8c38SPeter Zijlstra parse_clockid), 16402dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 16412dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 16429d9cad76SKan Liang OPT_UINTEGER(0, "proc-map-timeout", &record.opts.proc_map_timeout, 16439d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 1644f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 1645f3b3614aSHari Bathini "Record namespaces events"), 1646b757bb09SAdrian Hunter OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events, 1647b757bb09SAdrian Hunter "Record context switch events"), 164885723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 164985723885SJiri Olsa "Configure all used events to run in kernel space.", 165085723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 165185723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 165285723885SJiri Olsa "Configure all used events to run in user space.", 165385723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 165471dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 165571dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 165671dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 165771dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 16587efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 16597efe0e03SHe Kuang "file", "vmlinux pathname"), 16606156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 16616156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 1662ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 1663ecfd7a9cSWang Nan "append timestamp to output filename"), 1664cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 1665bfacbe3bSJiri Olsa &record.switch_output.set, "signal,size,time", 1666bfacbe3bSJiri Olsa "Switch output when receive SIGUSR2 or cross size,time threshold", 1667dc0c6127SJiri Olsa "signal"), 16680aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 16690aab2136SWang Nan "Parse options then exit"), 167086470930SIngo Molnar OPT_END() 167186470930SIngo Molnar }; 167286470930SIngo Molnar 1673e5b2c207SNamhyung Kim struct option *record_options = __record_options; 1674e5b2c207SNamhyung Kim 1675b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 167686470930SIngo Molnar { 1677ef149c25SAdrian Hunter int err; 16788c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 167916ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 168086470930SIngo Molnar 168148e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 168248e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 168348e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 168448e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 168548e1cab1SWang Nan # undef set_nobuild 168648e1cab1SWang Nan #endif 168748e1cab1SWang Nan 16887efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 16897efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 16907efe0e03SHe Kuang # define REASON "NO_DWARF=1" 16917efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 16927efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 16937efe0e03SHe Kuang # else 16947efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 16957efe0e03SHe Kuang # endif 16967efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 16977efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 16987efe0e03SHe Kuang # undef set_nobuild 16997efe0e03SHe Kuang # undef REASON 17007efe0e03SHe Kuang #endif 17017efe0e03SHe Kuang 17023e2be2daSArnaldo Carvalho de Melo rec->evlist = perf_evlist__new(); 17033e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 1704361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 1705361c99a6SArnaldo Carvalho de Melo 1706ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 1707ecc4c561SArnaldo Carvalho de Melo if (err) 1708ecc4c561SArnaldo Carvalho de Melo return err; 1709eb853e80SJiri Olsa 1710bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 1711a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 171268ba3235SNamhyung Kim if (quiet) 171368ba3235SNamhyung Kim perf_quiet_option(); 1714483635a9SJiri Olsa 1715483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 1716602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 1717483635a9SJiri Olsa rec->opts.target.system_wide = true; 171886470930SIngo Molnar 1719bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 1720c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 1721c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 1722c7118369SNamhyung Kim 1723023695d9SStephane Eranian } 1724b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 1725b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 1726c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 1727c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 1728c7118369SNamhyung Kim return -EINVAL; 1729b757bb09SAdrian Hunter } 1730023695d9SStephane Eranian 1731cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 1732cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 1733cb4e1ebbSJiri Olsa return -EINVAL; 1734cb4e1ebbSJiri Olsa } 1735cb4e1ebbSJiri Olsa 1736bfacbe3bSJiri Olsa if (rec->switch_output.time) { 1737bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 1738bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 1739bfacbe3bSJiri Olsa } 1740bfacbe3bSJiri Olsa 1741ef149c25SAdrian Hunter if (!rec->itr) { 1742ef149c25SAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 1743ef149c25SAdrian Hunter if (err) 17445c01ad60SAdrian Hunter goto out; 1745ef149c25SAdrian Hunter } 1746ef149c25SAdrian Hunter 17472dd6d8a1SAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 17482dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_opts); 17492dd6d8a1SAdrian Hunter if (err) 17505c01ad60SAdrian Hunter goto out; 17512dd6d8a1SAdrian Hunter 17521b36c03eSAdrian Hunter /* 17531b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 17541b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 17551b36c03eSAdrian Hunter */ 17561b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 17571b36c03eSAdrian Hunter 17581b36c03eSAdrian Hunter symbol__init(NULL); 17591b36c03eSAdrian Hunter 17601b36c03eSAdrian Hunter err = auxtrace_parse_filters(rec->evlist); 17611b36c03eSAdrian Hunter if (err) 17621b36c03eSAdrian Hunter goto out; 17631b36c03eSAdrian Hunter 17640aab2136SWang Nan if (dry_run) 17655c01ad60SAdrian Hunter goto out; 17660aab2136SWang Nan 1767d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 1768d7888573SWang Nan if (err) { 1769d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 1770d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 1771d7888573SWang Nan errbuf); 17725c01ad60SAdrian Hunter goto out; 1773d7888573SWang Nan } 1774d7888573SWang Nan 1775ef149c25SAdrian Hunter err = -ENOMEM; 1776ef149c25SAdrian Hunter 1777ec80fde7SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict) 1778646aaea6SArnaldo Carvalho de Melo pr_warning( 1779646aaea6SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 1780ec80fde7SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict.\n\n" 1781646aaea6SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 1782646aaea6SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 1783646aaea6SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 1784646aaea6SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 1785646aaea6SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 1786ec80fde7SArnaldo Carvalho de Melo 17870c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 1788a1ac1d3cSStephane Eranian disable_buildid_cache(); 1789dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 17900c1d46a8SWang Nan /* 17910c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 17920c1d46a8SWang Nan * generation by default to reduce data file switching 17930c1d46a8SWang Nan * overhead. Still generate buildid if they are required 17940c1d46a8SWang Nan * explicitly using 17950c1d46a8SWang Nan * 179660437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 17970c1d46a8SWang Nan * --no-no-buildid-cache 17980c1d46a8SWang Nan * 17990c1d46a8SWang Nan * Following code equals to: 18000c1d46a8SWang Nan * 18010c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 18020c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 18030c1d46a8SWang Nan * disable_buildid_cache(); 18040c1d46a8SWang Nan */ 18050c1d46a8SWang Nan bool disable = true; 18060c1d46a8SWang Nan 18070c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 18080c1d46a8SWang Nan disable = false; 18090c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 18100c1d46a8SWang Nan disable = false; 18110c1d46a8SWang Nan if (disable) { 18120c1d46a8SWang Nan rec->no_buildid = true; 18130c1d46a8SWang Nan rec->no_buildid_cache = true; 18140c1d46a8SWang Nan disable_buildid_cache(); 18150c1d46a8SWang Nan } 18160c1d46a8SWang Nan } 1817655000e7SArnaldo Carvalho de Melo 18184ea648aeSWang Nan if (record.opts.overwrite) 18194ea648aeSWang Nan record.opts.tail_synthesize = true; 18204ea648aeSWang Nan 18213e2be2daSArnaldo Carvalho de Melo if (rec->evlist->nr_entries == 0 && 18223e2be2daSArnaldo Carvalho de Melo perf_evlist__add_default(rec->evlist) < 0) { 182369aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 1824394c01edSAdrian Hunter goto out; 1825bbd36e5eSPeter Zijlstra } 182686470930SIngo Molnar 182769e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 182869e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 182969e7e5b0SAdrian Hunter 1830602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 183116ad2ffbSNamhyung Kim if (err) { 1832602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 183316ad2ffbSNamhyung Kim ui__warning("%s", errbuf); 183416ad2ffbSNamhyung Kim } 18354bd0f2d2SNamhyung Kim 1836602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 183716ad2ffbSNamhyung Kim if (err) { 183816ad2ffbSNamhyung Kim int saved_errno = errno; 183916ad2ffbSNamhyung Kim 1840602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 18413780f488SNamhyung Kim ui__error("%s", errbuf); 184216ad2ffbSNamhyung Kim 184316ad2ffbSNamhyung Kim err = -saved_errno; 1844394c01edSAdrian Hunter goto out; 184516ad2ffbSNamhyung Kim } 18460d37aa34SArnaldo Carvalho de Melo 184723dc4f15SJiri Olsa /* Enable ignoring missing threads when -u option is defined. */ 184823dc4f15SJiri Olsa rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX; 184923dc4f15SJiri Olsa 185016ad2ffbSNamhyung Kim err = -ENOMEM; 18513e2be2daSArnaldo Carvalho de Melo if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 1852dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 185369aad6f1SArnaldo Carvalho de Melo 1854ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 1855ef149c25SAdrian Hunter if (err) 1856394c01edSAdrian Hunter goto out; 1857ef149c25SAdrian Hunter 18586156681bSNamhyung Kim /* 18596156681bSNamhyung Kim * We take all buildids when the file contains 18606156681bSNamhyung Kim * AUX area tracing data because we do not decode the 18616156681bSNamhyung Kim * trace because it would take too long. 18626156681bSNamhyung Kim */ 18636156681bSNamhyung Kim if (rec->opts.full_auxtrace) 18646156681bSNamhyung Kim rec->buildid_all = true; 18656156681bSNamhyung Kim 1866b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 186739d17dacSArnaldo Carvalho de Melo err = -EINVAL; 1868394c01edSAdrian Hunter goto out; 18697e4ff9e3SMike Galbraith } 18707e4ff9e3SMike Galbraith 1871d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 1872394c01edSAdrian Hunter out: 187345604710SNamhyung Kim perf_evlist__delete(rec->evlist); 1874d65a458bSArnaldo Carvalho de Melo symbol__exit(); 1875ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 187639d17dacSArnaldo Carvalho de Melo return err; 187786470930SIngo Molnar } 18782dd6d8a1SAdrian Hunter 18792dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 18802dd6d8a1SAdrian Hunter { 1881dc0c6127SJiri Olsa struct record *rec = &record; 1882dc0c6127SJiri Olsa 18835f9cf599SWang Nan if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 18845f9cf599SWang Nan trigger_hit(&auxtrace_snapshot_trigger); 18852dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 1; 18865f9cf599SWang Nan if (auxtrace_record__snapshot_start(record.itr)) 18875f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 18885f9cf599SWang Nan } 18893c1cb7e3SWang Nan 1890dc0c6127SJiri Olsa if (switch_output_signal(rec)) 18913c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 18922dd6d8a1SAdrian Hunter } 1893bfacbe3bSJiri Olsa 1894bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 1895bfacbe3bSJiri Olsa { 1896bfacbe3bSJiri Olsa struct record *rec = &record; 1897bfacbe3bSJiri Olsa 1898bfacbe3bSJiri Olsa if (switch_output_time(rec)) 1899bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 1900bfacbe3bSJiri Olsa } 1901