1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 286470930SIngo Molnar /* 386470930SIngo Molnar * builtin-record.c 486470930SIngo Molnar * 586470930SIngo Molnar * Builtin record command: Record the profile of a workload 686470930SIngo Molnar * (or a CPU, or a PID) into the perf.data output file - for 786470930SIngo Molnar * later analysis via perf report. 886470930SIngo Molnar */ 986470930SIngo Molnar #include "builtin.h" 1086470930SIngo Molnar 1186470930SIngo Molnar #include "perf.h" 1286470930SIngo Molnar 136122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h" 1486470930SIngo Molnar #include "util/util.h" 154b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h> 1686470930SIngo Molnar #include "util/parse-events.h" 1741840d21STaeung Song #include "util/config.h" 1886470930SIngo Molnar 198f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h" 20f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h" 217c6a1c65SPeter Zijlstra #include "util/header.h" 2266e274f3SFrederic Weisbecker #include "util/event.h" 23361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h" 2469aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h" 258f28827aSFrederic Weisbecker #include "util/debug.h" 265d8bb1ecSMathieu Poirier #include "util/drv_configs.h" 2794c744b6SArnaldo Carvalho de Melo #include "util/session.h" 2845694aa7SArnaldo Carvalho de Melo #include "util/tool.h" 298d06367fSArnaldo Carvalho de Melo #include "util/symbol.h" 30a12b51c4SPaul Mackerras #include "util/cpumap.h" 31fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h" 32f5fc1412SJiri Olsa #include "util/data.h" 33bcc84ec6SStephane Eranian #include "util/perf_regs.h" 34ef149c25SAdrian Hunter #include "util/auxtrace.h" 3546bc29b9SAdrian Hunter #include "util/tsc.h" 36f00898f4SAndi Kleen #include "util/parse-branch-options.h" 37bcc84ec6SStephane Eranian #include "util/parse-regs-options.h" 3871dc2326SWang Nan #include "util/llvm-utils.h" 398690a2a7SWang Nan #include "util/bpf-loader.h" 405f9cf599SWang Nan #include "util/trigger.h" 41a074865eSWang Nan #include "util/perf-hooks.h" 42c5e4027eSArnaldo Carvalho de Melo #include "util/time-utils.h" 4358db1d6eSArnaldo Carvalho de Melo #include "util/units.h" 44d8871ea7SWang Nan #include "asm/bug.h" 457c6a1c65SPeter Zijlstra 46a43783aeSArnaldo Carvalho de Melo #include <errno.h> 47fd20e811SArnaldo Carvalho de Melo #include <inttypes.h> 484208735dSArnaldo Carvalho de Melo #include <poll.h> 4986470930SIngo Molnar #include <unistd.h> 5086470930SIngo Molnar #include <sched.h> 519607ad3aSArnaldo Carvalho de Melo #include <signal.h> 52a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h> 534208735dSArnaldo Carvalho de Melo #include <sys/wait.h> 540693e680SArnaldo Carvalho de Melo #include <linux/time64.h> 5578da39faSBernhard Rosenkraenzer 561b43b704SJiri Olsa struct switch_output { 57dc0c6127SJiri Olsa bool enabled; 581b43b704SJiri Olsa bool signal; 59dc0c6127SJiri Olsa unsigned long size; 60bfacbe3bSJiri Olsa unsigned long time; 61cb4e1ebbSJiri Olsa const char *str; 62cb4e1ebbSJiri Olsa bool set; 631b43b704SJiri Olsa }; 641b43b704SJiri Olsa 658c6f45a7SArnaldo Carvalho de Melo struct record { 6645694aa7SArnaldo Carvalho de Melo struct perf_tool tool; 67b4006796SArnaldo Carvalho de Melo struct record_opts opts; 68d20deb64SArnaldo Carvalho de Melo u64 bytes_written; 698ceb41d7SJiri Olsa struct perf_data data; 70ef149c25SAdrian Hunter struct auxtrace_record *itr; 71d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist; 72d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 73d20deb64SArnaldo Carvalho de Melo const char *progname; 74d20deb64SArnaldo Carvalho de Melo int realtime_prio; 75d20deb64SArnaldo Carvalho de Melo bool no_buildid; 76d2db9a98SWang Nan bool no_buildid_set; 77d20deb64SArnaldo Carvalho de Melo bool no_buildid_cache; 78d2db9a98SWang Nan bool no_buildid_cache_set; 796156681bSNamhyung Kim bool buildid_all; 80ecfd7a9cSWang Nan bool timestamp_filename; 811b43b704SJiri Olsa struct switch_output switch_output; 829f065194SYang Shi unsigned long long samples; 830f82ebc4SArnaldo Carvalho de Melo }; 8486470930SIngo Molnar 85dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started; 86dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger); 87dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger); 88dc0c6127SJiri Olsa 89dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec) 90dc0c6127SJiri Olsa { 91dc0c6127SJiri Olsa return rec->switch_output.signal && 92dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger); 93dc0c6127SJiri Olsa } 94dc0c6127SJiri Olsa 95dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec) 96dc0c6127SJiri Olsa { 97dc0c6127SJiri Olsa return rec->switch_output.size && 98dc0c6127SJiri Olsa trigger_is_ready(&switch_output_trigger) && 99dc0c6127SJiri Olsa (rec->bytes_written >= rec->switch_output.size); 100dc0c6127SJiri Olsa } 101dc0c6127SJiri Olsa 102bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec) 103bfacbe3bSJiri Olsa { 104bfacbe3bSJiri Olsa return rec->switch_output.time && 105bfacbe3bSJiri Olsa trigger_is_ready(&switch_output_trigger); 106bfacbe3bSJiri Olsa } 107bfacbe3bSJiri Olsa 1088c6f45a7SArnaldo Carvalho de Melo static int record__write(struct record *rec, void *bf, size_t size) 109f5970550SPeter Zijlstra { 1108ceb41d7SJiri Olsa if (perf_data__write(rec->session->data, bf, size) < 0) { 1114f624685SAdrian Hunter pr_err("failed to write perf data, error: %m\n"); 1128d3eca20SDavid Ahern return -1; 1138d3eca20SDavid Ahern } 114f5970550SPeter Zijlstra 115cf8b2e69SArnaldo Carvalho de Melo rec->bytes_written += size; 116dc0c6127SJiri Olsa 117dc0c6127SJiri Olsa if (switch_output_size(rec)) 118dc0c6127SJiri Olsa trigger_hit(&switch_output_trigger); 119dc0c6127SJiri Olsa 1208d3eca20SDavid Ahern return 0; 121f5970550SPeter Zijlstra } 122f5970550SPeter Zijlstra 12345694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool, 124d20deb64SArnaldo Carvalho de Melo union perf_event *event, 1251d037ca1SIrina Tirdea struct perf_sample *sample __maybe_unused, 1261d037ca1SIrina Tirdea struct machine *machine __maybe_unused) 127234fbbf5SArnaldo Carvalho de Melo { 1288c6f45a7SArnaldo Carvalho de Melo struct record *rec = container_of(tool, struct record, tool); 1298c6f45a7SArnaldo Carvalho de Melo return record__write(rec, event, event->header.size); 130234fbbf5SArnaldo Carvalho de Melo } 131234fbbf5SArnaldo Carvalho de Melo 132d37f1586SArnaldo Carvalho de Melo static int record__pushfn(void *to, void *bf, size_t size) 133d37f1586SArnaldo Carvalho de Melo { 134d37f1586SArnaldo Carvalho de Melo struct record *rec = to; 135d37f1586SArnaldo Carvalho de Melo 136d37f1586SArnaldo Carvalho de Melo rec->samples++; 137d37f1586SArnaldo Carvalho de Melo return record__write(rec, bf, size); 138d37f1586SArnaldo Carvalho de Melo } 139d37f1586SArnaldo Carvalho de Melo 1402dd6d8a1SAdrian Hunter static volatile int done; 1412dd6d8a1SAdrian Hunter static volatile int signr = -1; 1422dd6d8a1SAdrian Hunter static volatile int child_finished; 143c0bdc1c4SWang Nan 1442dd6d8a1SAdrian Hunter static void sig_handler(int sig) 1452dd6d8a1SAdrian Hunter { 1462dd6d8a1SAdrian Hunter if (sig == SIGCHLD) 1472dd6d8a1SAdrian Hunter child_finished = 1; 1482dd6d8a1SAdrian Hunter else 1492dd6d8a1SAdrian Hunter signr = sig; 1502dd6d8a1SAdrian Hunter 1512dd6d8a1SAdrian Hunter done = 1; 1522dd6d8a1SAdrian Hunter } 1532dd6d8a1SAdrian Hunter 154a074865eSWang Nan static void sigsegv_handler(int sig) 155a074865eSWang Nan { 156a074865eSWang Nan perf_hooks__recover(); 157a074865eSWang Nan sighandler_dump_stack(sig); 158a074865eSWang Nan } 159a074865eSWang Nan 1602dd6d8a1SAdrian Hunter static void record__sig_exit(void) 1612dd6d8a1SAdrian Hunter { 1622dd6d8a1SAdrian Hunter if (signr == -1) 1632dd6d8a1SAdrian Hunter return; 1642dd6d8a1SAdrian Hunter 1652dd6d8a1SAdrian Hunter signal(signr, SIG_DFL); 1662dd6d8a1SAdrian Hunter raise(signr); 1672dd6d8a1SAdrian Hunter } 1682dd6d8a1SAdrian Hunter 169e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT 170e31f0d01SAdrian Hunter 171ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool, 172ef149c25SAdrian Hunter union perf_event *event, void *data1, 173ef149c25SAdrian Hunter size_t len1, void *data2, size_t len2) 174ef149c25SAdrian Hunter { 175ef149c25SAdrian Hunter struct record *rec = container_of(tool, struct record, tool); 1768ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 177ef149c25SAdrian Hunter size_t padding; 178ef149c25SAdrian Hunter u8 pad[8] = {0}; 179ef149c25SAdrian Hunter 1808ceb41d7SJiri Olsa if (!perf_data__is_pipe(data)) { 18199fa2984SAdrian Hunter off_t file_offset; 1828ceb41d7SJiri Olsa int fd = perf_data__fd(data); 18399fa2984SAdrian Hunter int err; 18499fa2984SAdrian Hunter 18599fa2984SAdrian Hunter file_offset = lseek(fd, 0, SEEK_CUR); 18699fa2984SAdrian Hunter if (file_offset == -1) 18799fa2984SAdrian Hunter return -1; 18899fa2984SAdrian Hunter err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index, 18999fa2984SAdrian Hunter event, file_offset); 19099fa2984SAdrian Hunter if (err) 19199fa2984SAdrian Hunter return err; 19299fa2984SAdrian Hunter } 19399fa2984SAdrian Hunter 194ef149c25SAdrian Hunter /* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */ 195ef149c25SAdrian Hunter padding = (len1 + len2) & 7; 196ef149c25SAdrian Hunter if (padding) 197ef149c25SAdrian Hunter padding = 8 - padding; 198ef149c25SAdrian Hunter 199ef149c25SAdrian Hunter record__write(rec, event, event->header.size); 200ef149c25SAdrian Hunter record__write(rec, data1, len1); 201ef149c25SAdrian Hunter if (len2) 202ef149c25SAdrian Hunter record__write(rec, data2, len2); 203ef149c25SAdrian Hunter record__write(rec, &pad, padding); 204ef149c25SAdrian Hunter 205ef149c25SAdrian Hunter return 0; 206ef149c25SAdrian Hunter } 207ef149c25SAdrian Hunter 208ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec, 209ef149c25SAdrian Hunter struct auxtrace_mmap *mm) 210ef149c25SAdrian Hunter { 211ef149c25SAdrian Hunter int ret; 212ef149c25SAdrian Hunter 213ef149c25SAdrian Hunter ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool, 214ef149c25SAdrian Hunter record__process_auxtrace); 215ef149c25SAdrian Hunter if (ret < 0) 216ef149c25SAdrian Hunter return ret; 217ef149c25SAdrian Hunter 218ef149c25SAdrian Hunter if (ret) 219ef149c25SAdrian Hunter rec->samples++; 220ef149c25SAdrian Hunter 221ef149c25SAdrian Hunter return 0; 222ef149c25SAdrian Hunter } 223ef149c25SAdrian Hunter 2242dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec, 2252dd6d8a1SAdrian Hunter struct auxtrace_mmap *mm) 2262dd6d8a1SAdrian Hunter { 2272dd6d8a1SAdrian Hunter int ret; 2282dd6d8a1SAdrian Hunter 2292dd6d8a1SAdrian Hunter ret = auxtrace_mmap__read_snapshot(mm, rec->itr, &rec->tool, 2302dd6d8a1SAdrian Hunter record__process_auxtrace, 2312dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_size); 2322dd6d8a1SAdrian Hunter if (ret < 0) 2332dd6d8a1SAdrian Hunter return ret; 2342dd6d8a1SAdrian Hunter 2352dd6d8a1SAdrian Hunter if (ret) 2362dd6d8a1SAdrian Hunter rec->samples++; 2372dd6d8a1SAdrian Hunter 2382dd6d8a1SAdrian Hunter return 0; 2392dd6d8a1SAdrian Hunter } 2402dd6d8a1SAdrian Hunter 2412dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec) 2422dd6d8a1SAdrian Hunter { 2432dd6d8a1SAdrian Hunter int i; 2442dd6d8a1SAdrian Hunter int rc = 0; 2452dd6d8a1SAdrian Hunter 2462dd6d8a1SAdrian Hunter for (i = 0; i < rec->evlist->nr_mmaps; i++) { 2472dd6d8a1SAdrian Hunter struct auxtrace_mmap *mm = 2482dd6d8a1SAdrian Hunter &rec->evlist->mmap[i].auxtrace_mmap; 2492dd6d8a1SAdrian Hunter 2502dd6d8a1SAdrian Hunter if (!mm->base) 2512dd6d8a1SAdrian Hunter continue; 2522dd6d8a1SAdrian Hunter 2532dd6d8a1SAdrian Hunter if (record__auxtrace_mmap_read_snapshot(rec, mm) != 0) { 2542dd6d8a1SAdrian Hunter rc = -1; 2552dd6d8a1SAdrian Hunter goto out; 2562dd6d8a1SAdrian Hunter } 2572dd6d8a1SAdrian Hunter } 2582dd6d8a1SAdrian Hunter out: 2592dd6d8a1SAdrian Hunter return rc; 2602dd6d8a1SAdrian Hunter } 2612dd6d8a1SAdrian Hunter 2622dd6d8a1SAdrian Hunter static void record__read_auxtrace_snapshot(struct record *rec) 2632dd6d8a1SAdrian Hunter { 2642dd6d8a1SAdrian Hunter pr_debug("Recording AUX area tracing snapshot\n"); 2652dd6d8a1SAdrian Hunter if (record__auxtrace_read_snapshot_all(rec) < 0) { 2665f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 2672dd6d8a1SAdrian Hunter } else { 2685f9cf599SWang Nan if (auxtrace_record__snapshot_finish(rec->itr)) 2695f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 2705f9cf599SWang Nan else 2715f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 2722dd6d8a1SAdrian Hunter } 2732dd6d8a1SAdrian Hunter } 2742dd6d8a1SAdrian Hunter 275e31f0d01SAdrian Hunter #else 276e31f0d01SAdrian Hunter 277e31f0d01SAdrian Hunter static inline 278e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused, 279e31f0d01SAdrian Hunter struct auxtrace_mmap *mm __maybe_unused) 280e31f0d01SAdrian Hunter { 281e31f0d01SAdrian Hunter return 0; 282e31f0d01SAdrian Hunter } 283e31f0d01SAdrian Hunter 2842dd6d8a1SAdrian Hunter static inline 2852dd6d8a1SAdrian Hunter void record__read_auxtrace_snapshot(struct record *rec __maybe_unused) 2862dd6d8a1SAdrian Hunter { 2872dd6d8a1SAdrian Hunter } 2882dd6d8a1SAdrian Hunter 2892dd6d8a1SAdrian Hunter static inline 2902dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused) 2912dd6d8a1SAdrian Hunter { 2922dd6d8a1SAdrian Hunter return 0; 2932dd6d8a1SAdrian Hunter } 2942dd6d8a1SAdrian Hunter 295e31f0d01SAdrian Hunter #endif 296e31f0d01SAdrian Hunter 297cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec, 298cda57a8cSWang Nan struct perf_evlist *evlist) 299cda57a8cSWang Nan { 300cda57a8cSWang Nan struct record_opts *opts = &rec->opts; 301cda57a8cSWang Nan char msg[512]; 302cda57a8cSWang Nan 3037a276ff6SWang Nan if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, 304cda57a8cSWang Nan opts->auxtrace_mmap_pages, 305cda57a8cSWang Nan opts->auxtrace_snapshot_mode) < 0) { 306cda57a8cSWang Nan if (errno == EPERM) { 307cda57a8cSWang Nan pr_err("Permission error mapping pages.\n" 308cda57a8cSWang Nan "Consider increasing " 309cda57a8cSWang Nan "/proc/sys/kernel/perf_event_mlock_kb,\n" 310cda57a8cSWang Nan "or try again with a smaller value of -m/--mmap_pages.\n" 311cda57a8cSWang Nan "(current value: %u,%u)\n", 312cda57a8cSWang Nan opts->mmap_pages, opts->auxtrace_mmap_pages); 313cda57a8cSWang Nan return -errno; 314cda57a8cSWang Nan } else { 315cda57a8cSWang Nan pr_err("failed to mmap with %d (%s)\n", errno, 316c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 317cda57a8cSWang Nan if (errno) 318cda57a8cSWang Nan return -errno; 319cda57a8cSWang Nan else 320cda57a8cSWang Nan return -EINVAL; 321cda57a8cSWang Nan } 322cda57a8cSWang Nan } 323cda57a8cSWang Nan return 0; 324cda57a8cSWang Nan } 325cda57a8cSWang Nan 326cda57a8cSWang Nan static int record__mmap(struct record *rec) 327cda57a8cSWang Nan { 328cda57a8cSWang Nan return record__mmap_evlist(rec, rec->evlist); 329cda57a8cSWang Nan } 330cda57a8cSWang Nan 3318c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec) 332dd7927f4SArnaldo Carvalho de Melo { 333d6195a6aSArnaldo Carvalho de Melo char msg[BUFSIZ]; 3346a4bb04cSJiri Olsa struct perf_evsel *pos; 335d20deb64SArnaldo Carvalho de Melo struct perf_evlist *evlist = rec->evlist; 336d20deb64SArnaldo Carvalho de Melo struct perf_session *session = rec->session; 337b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 3385d8bb1ecSMathieu Poirier struct perf_evsel_config_term *err_term; 3398d3eca20SDavid Ahern int rc = 0; 340dd7927f4SArnaldo Carvalho de Melo 341d3dbf43cSArnaldo Carvalho de Melo /* 342d3dbf43cSArnaldo Carvalho de Melo * For initial_delay we need to add a dummy event so that we can track 343d3dbf43cSArnaldo Carvalho de Melo * PERF_RECORD_MMAP while we wait for the initial delay to enable the 344d3dbf43cSArnaldo Carvalho de Melo * real events, the ones asked by the user. 345d3dbf43cSArnaldo Carvalho de Melo */ 346d3dbf43cSArnaldo Carvalho de Melo if (opts->initial_delay) { 347d3dbf43cSArnaldo Carvalho de Melo if (perf_evlist__add_dummy(evlist)) 348d3dbf43cSArnaldo Carvalho de Melo return -ENOMEM; 349d3dbf43cSArnaldo Carvalho de Melo 350d3dbf43cSArnaldo Carvalho de Melo pos = perf_evlist__first(evlist); 351d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 0; 352d3dbf43cSArnaldo Carvalho de Melo pos = perf_evlist__last(evlist); 353d3dbf43cSArnaldo Carvalho de Melo pos->tracking = 1; 354d3dbf43cSArnaldo Carvalho de Melo pos->attr.enable_on_exec = 1; 355d3dbf43cSArnaldo Carvalho de Melo } 356d3dbf43cSArnaldo Carvalho de Melo 357e68ae9cfSArnaldo Carvalho de Melo perf_evlist__config(evlist, opts, &callchain_param); 358cac21425SJiri Olsa 359e5cadb93SArnaldo Carvalho de Melo evlist__for_each_entry(evlist, pos) { 3603da297a6SIngo Molnar try_again: 361d988d5eeSKan Liang if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) { 36256e52e85SArnaldo Carvalho de Melo if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) { 363bb963e16SNamhyung Kim if (verbose > 0) 364c0a54341SArnaldo Carvalho de Melo ui__warning("%s\n", msg); 3653da297a6SIngo Molnar goto try_again; 3663da297a6SIngo Molnar } 367ca6a4258SDavid Ahern 36856e52e85SArnaldo Carvalho de Melo rc = -errno; 36956e52e85SArnaldo Carvalho de Melo perf_evsel__open_strerror(pos, &opts->target, 37056e52e85SArnaldo Carvalho de Melo errno, msg, sizeof(msg)); 37156e52e85SArnaldo Carvalho de Melo ui__error("%s\n", msg); 3728d3eca20SDavid Ahern goto out; 3737c6a1c65SPeter Zijlstra } 374bfd8f72cSAndi Kleen 375bfd8f72cSAndi Kleen pos->supported = true; 3767c6a1c65SPeter Zijlstra } 3777c6a1c65SPeter Zijlstra 37823d4aad4SArnaldo Carvalho de Melo if (perf_evlist__apply_filters(evlist, &pos)) { 37962d94b00SArnaldo Carvalho de Melo pr_err("failed to set filter \"%s\" on event %s with %d (%s)\n", 38023d4aad4SArnaldo Carvalho de Melo pos->filter, perf_evsel__name(pos), errno, 381c8b5f2c9SArnaldo Carvalho de Melo str_error_r(errno, msg, sizeof(msg))); 3828d3eca20SDavid Ahern rc = -1; 3838d3eca20SDavid Ahern goto out; 3840a102479SFrederic Weisbecker } 3850a102479SFrederic Weisbecker 3865d8bb1ecSMathieu Poirier if (perf_evlist__apply_drv_configs(evlist, &pos, &err_term)) { 38762d94b00SArnaldo Carvalho de Melo pr_err("failed to set config \"%s\" on event %s with %d (%s)\n", 3885d8bb1ecSMathieu Poirier err_term->val.drv_cfg, perf_evsel__name(pos), errno, 3895d8bb1ecSMathieu Poirier str_error_r(errno, msg, sizeof(msg))); 3905d8bb1ecSMathieu Poirier rc = -1; 3915d8bb1ecSMathieu Poirier goto out; 3925d8bb1ecSMathieu Poirier } 3935d8bb1ecSMathieu Poirier 394cda57a8cSWang Nan rc = record__mmap(rec); 395cda57a8cSWang Nan if (rc) 3968d3eca20SDavid Ahern goto out; 3970a27d7f9SArnaldo Carvalho de Melo 398a91e5431SArnaldo Carvalho de Melo session->evlist = evlist; 3997b56cce2SArnaldo Carvalho de Melo perf_session__set_id_hdr_size(session); 4008d3eca20SDavid Ahern out: 4018d3eca20SDavid Ahern return rc; 402a91e5431SArnaldo Carvalho de Melo } 403a91e5431SArnaldo Carvalho de Melo 404e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool, 405e3d59112SNamhyung Kim union perf_event *event, 406e3d59112SNamhyung Kim struct perf_sample *sample, 407e3d59112SNamhyung Kim struct perf_evsel *evsel, 408e3d59112SNamhyung Kim struct machine *machine) 409e3d59112SNamhyung Kim { 410e3d59112SNamhyung Kim struct record *rec = container_of(tool, struct record, tool); 411e3d59112SNamhyung Kim 412e3d59112SNamhyung Kim rec->samples++; 413e3d59112SNamhyung Kim 414e3d59112SNamhyung Kim return build_id__mark_dso_hit(tool, event, sample, evsel, machine); 415e3d59112SNamhyung Kim } 416e3d59112SNamhyung Kim 4178c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec) 4186122e4e4SArnaldo Carvalho de Melo { 4198ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 420f5fc1412SJiri Olsa struct perf_session *session = rec->session; 4216122e4e4SArnaldo Carvalho de Melo 4228ceb41d7SJiri Olsa if (data->size == 0) 4239f591fd7SArnaldo Carvalho de Melo return 0; 4249f591fd7SArnaldo Carvalho de Melo 42500dc8657SNamhyung Kim /* 42600dc8657SNamhyung Kim * During this process, it'll load kernel map and replace the 42700dc8657SNamhyung Kim * dso->long_name to a real pathname it found. In this case 42800dc8657SNamhyung Kim * we prefer the vmlinux path like 42900dc8657SNamhyung Kim * /lib/modules/3.16.4/build/vmlinux 43000dc8657SNamhyung Kim * 43100dc8657SNamhyung Kim * rather than build-id path (in debug directory). 43200dc8657SNamhyung Kim * $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551 43300dc8657SNamhyung Kim */ 43400dc8657SNamhyung Kim symbol_conf.ignore_vmlinux_buildid = true; 43500dc8657SNamhyung Kim 4366156681bSNamhyung Kim /* 4376156681bSNamhyung Kim * If --buildid-all is given, it marks all DSO regardless of hits, 4386156681bSNamhyung Kim * so no need to process samples. 4396156681bSNamhyung Kim */ 4406156681bSNamhyung Kim if (rec->buildid_all) 4416156681bSNamhyung Kim rec->tool.sample = NULL; 4426156681bSNamhyung Kim 443b7b61cbeSArnaldo Carvalho de Melo return perf_session__process_events(session); 4446122e4e4SArnaldo Carvalho de Melo } 4456122e4e4SArnaldo Carvalho de Melo 4468115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data) 447a1645ce1SZhang, Yanmin { 448a1645ce1SZhang, Yanmin int err; 44945694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = data; 450a1645ce1SZhang, Yanmin /* 451a1645ce1SZhang, Yanmin *As for guest kernel when processing subcommand record&report, 452a1645ce1SZhang, Yanmin *we arrange module mmap prior to guest kernel mmap and trigger 453a1645ce1SZhang, Yanmin *a preload dso because default guest module symbols are loaded 454a1645ce1SZhang, Yanmin *from guest kallsyms instead of /lib/modules/XXX/XXX. This 455a1645ce1SZhang, Yanmin *method is used to avoid symbol missing when the first addr is 456a1645ce1SZhang, Yanmin *in module instead of in guest kernel. 457a1645ce1SZhang, Yanmin */ 45845694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_modules(tool, process_synthesized_event, 459743eb868SArnaldo Carvalho de Melo machine); 460a1645ce1SZhang, Yanmin if (err < 0) 461a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 46223346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 463a1645ce1SZhang, Yanmin 464a1645ce1SZhang, Yanmin /* 465a1645ce1SZhang, Yanmin * We use _stext for guest kernel because guest kernel's /proc/kallsyms 466a1645ce1SZhang, Yanmin * have no _text sometimes. 467a1645ce1SZhang, Yanmin */ 46845694aa7SArnaldo Carvalho de Melo err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 4690ae617beSAdrian Hunter machine); 470a1645ce1SZhang, Yanmin if (err < 0) 471a1645ce1SZhang, Yanmin pr_err("Couldn't record guest kernel [%d]'s reference" 47223346f21SArnaldo Carvalho de Melo " relocation symbol.\n", machine->pid); 473a1645ce1SZhang, Yanmin } 474a1645ce1SZhang, Yanmin 47598402807SFrederic Weisbecker static struct perf_event_header finished_round_event = { 47698402807SFrederic Weisbecker .size = sizeof(struct perf_event_header), 47798402807SFrederic Weisbecker .type = PERF_RECORD_FINISHED_ROUND, 47898402807SFrederic Weisbecker }; 47998402807SFrederic Weisbecker 480a4ea0ec4SWang Nan static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist, 4810b72d69aSWang Nan bool overwrite) 48298402807SFrederic Weisbecker { 483dcabb507SJiri Olsa u64 bytes_written = rec->bytes_written; 4840e2e63ddSPeter Zijlstra int i; 4858d3eca20SDavid Ahern int rc = 0; 486a4ea0ec4SWang Nan struct perf_mmap *maps; 48798402807SFrederic Weisbecker 488cb21686bSWang Nan if (!evlist) 489cb21686bSWang Nan return 0; 490ef149c25SAdrian Hunter 4910b72d69aSWang Nan maps = overwrite ? evlist->overwrite_mmap : evlist->mmap; 492a4ea0ec4SWang Nan if (!maps) 493a4ea0ec4SWang Nan return 0; 494cb21686bSWang Nan 4950b72d69aSWang Nan if (overwrite && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING) 49654cc54deSWang Nan return 0; 49754cc54deSWang Nan 498a4ea0ec4SWang Nan for (i = 0; i < evlist->nr_mmaps; i++) { 499a4ea0ec4SWang Nan struct auxtrace_mmap *mm = &maps[i].auxtrace_mmap; 500a4ea0ec4SWang Nan 501a4ea0ec4SWang Nan if (maps[i].base) { 5020b72d69aSWang Nan if (perf_mmap__push(&maps[i], overwrite, rec, record__pushfn) != 0) { 5038d3eca20SDavid Ahern rc = -1; 5048d3eca20SDavid Ahern goto out; 5058d3eca20SDavid Ahern } 5068d3eca20SDavid Ahern } 507ef149c25SAdrian Hunter 5082dd6d8a1SAdrian Hunter if (mm->base && !rec->opts.auxtrace_snapshot_mode && 509ef149c25SAdrian Hunter record__auxtrace_mmap_read(rec, mm) != 0) { 510ef149c25SAdrian Hunter rc = -1; 511ef149c25SAdrian Hunter goto out; 512ef149c25SAdrian Hunter } 51398402807SFrederic Weisbecker } 51498402807SFrederic Weisbecker 515dcabb507SJiri Olsa /* 516dcabb507SJiri Olsa * Mark the round finished in case we wrote 517dcabb507SJiri Olsa * at least one event. 518dcabb507SJiri Olsa */ 519dcabb507SJiri Olsa if (bytes_written != rec->bytes_written) 5208c6f45a7SArnaldo Carvalho de Melo rc = record__write(rec, &finished_round_event, sizeof(finished_round_event)); 5218d3eca20SDavid Ahern 5220b72d69aSWang Nan if (overwrite) 52354cc54deSWang Nan perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY); 5248d3eca20SDavid Ahern out: 5258d3eca20SDavid Ahern return rc; 52698402807SFrederic Weisbecker } 52798402807SFrederic Weisbecker 528cb21686bSWang Nan static int record__mmap_read_all(struct record *rec) 529cb21686bSWang Nan { 530cb21686bSWang Nan int err; 531cb21686bSWang Nan 532a4ea0ec4SWang Nan err = record__mmap_read_evlist(rec, rec->evlist, false); 533cb21686bSWang Nan if (err) 534cb21686bSWang Nan return err; 535cb21686bSWang Nan 53605737464SWang Nan return record__mmap_read_evlist(rec, rec->evlist, true); 537cb21686bSWang Nan } 538cb21686bSWang Nan 5398c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec) 54057706abcSDavid Ahern { 54157706abcSDavid Ahern struct perf_session *session = rec->session; 54257706abcSDavid Ahern int feat; 54357706abcSDavid Ahern 54457706abcSDavid Ahern for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++) 54557706abcSDavid Ahern perf_header__set_feat(&session->header, feat); 54657706abcSDavid Ahern 54757706abcSDavid Ahern if (rec->no_buildid) 54857706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BUILD_ID); 54957706abcSDavid Ahern 5503e2be2daSArnaldo Carvalho de Melo if (!have_tracepoints(&rec->evlist->entries)) 55157706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_TRACING_DATA); 55257706abcSDavid Ahern 55357706abcSDavid Ahern if (!rec->opts.branch_stack) 55457706abcSDavid Ahern perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK); 555ef149c25SAdrian Hunter 556ef149c25SAdrian Hunter if (!rec->opts.full_auxtrace) 557ef149c25SAdrian Hunter perf_header__clear_feat(&session->header, HEADER_AUXTRACE); 558ffa517adSJiri Olsa 559ffa517adSJiri Olsa perf_header__clear_feat(&session->header, HEADER_STAT); 56057706abcSDavid Ahern } 56157706abcSDavid Ahern 562e1ab48baSWang Nan static void 563e1ab48baSWang Nan record__finish_output(struct record *rec) 564e1ab48baSWang Nan { 5658ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 5668ceb41d7SJiri Olsa int fd = perf_data__fd(data); 567e1ab48baSWang Nan 5688ceb41d7SJiri Olsa if (data->is_pipe) 569e1ab48baSWang Nan return; 570e1ab48baSWang Nan 571e1ab48baSWang Nan rec->session->header.data_size += rec->bytes_written; 5728ceb41d7SJiri Olsa data->size = lseek(perf_data__fd(data), 0, SEEK_CUR); 573e1ab48baSWang Nan 574e1ab48baSWang Nan if (!rec->no_buildid) { 575e1ab48baSWang Nan process_buildids(rec); 576e1ab48baSWang Nan 577e1ab48baSWang Nan if (rec->buildid_all) 578e1ab48baSWang Nan dsos__hit_all(rec->session); 579e1ab48baSWang Nan } 580e1ab48baSWang Nan perf_session__write_header(rec->session, rec->evlist, fd, true); 581e1ab48baSWang Nan 582e1ab48baSWang Nan return; 583e1ab48baSWang Nan } 584e1ab48baSWang Nan 5854ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail) 586be7b0c9eSWang Nan { 5879d6aae72SArnaldo Carvalho de Melo int err; 5889d6aae72SArnaldo Carvalho de Melo struct thread_map *thread_map; 589be7b0c9eSWang Nan 5904ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 5914ea648aeSWang Nan return 0; 5924ea648aeSWang Nan 5939d6aae72SArnaldo Carvalho de Melo thread_map = thread_map__new_by_tid(rec->evlist->workload.pid); 5949d6aae72SArnaldo Carvalho de Melo if (thread_map == NULL) 5959d6aae72SArnaldo Carvalho de Melo return -1; 5969d6aae72SArnaldo Carvalho de Melo 5979d6aae72SArnaldo Carvalho de Melo err = perf_event__synthesize_thread_map(&rec->tool, thread_map, 598be7b0c9eSWang Nan process_synthesized_event, 599be7b0c9eSWang Nan &rec->session->machines.host, 600be7b0c9eSWang Nan rec->opts.sample_address, 601be7b0c9eSWang Nan rec->opts.proc_map_timeout); 6029d6aae72SArnaldo Carvalho de Melo thread_map__put(thread_map); 6039d6aae72SArnaldo Carvalho de Melo return err; 604be7b0c9eSWang Nan } 605be7b0c9eSWang Nan 6064ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail); 6073c1cb7e3SWang Nan 608ecfd7a9cSWang Nan static int 609ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit) 610ecfd7a9cSWang Nan { 6118ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 612ecfd7a9cSWang Nan int fd, err; 613ecfd7a9cSWang Nan 614ecfd7a9cSWang Nan /* Same Size: "2015122520103046"*/ 615ecfd7a9cSWang Nan char timestamp[] = "InvalidTimestamp"; 616ecfd7a9cSWang Nan 6174ea648aeSWang Nan record__synthesize(rec, true); 6184ea648aeSWang Nan if (target__none(&rec->opts.target)) 6194ea648aeSWang Nan record__synthesize_workload(rec, true); 6204ea648aeSWang Nan 621ecfd7a9cSWang Nan rec->samples = 0; 622ecfd7a9cSWang Nan record__finish_output(rec); 623ecfd7a9cSWang Nan err = fetch_current_timestamp(timestamp, sizeof(timestamp)); 624ecfd7a9cSWang Nan if (err) { 625ecfd7a9cSWang Nan pr_err("Failed to get current timestamp\n"); 626ecfd7a9cSWang Nan return -EINVAL; 627ecfd7a9cSWang Nan } 628ecfd7a9cSWang Nan 6298ceb41d7SJiri Olsa fd = perf_data__switch(data, timestamp, 630ecfd7a9cSWang Nan rec->session->header.data_offset, 631ecfd7a9cSWang Nan at_exit); 632ecfd7a9cSWang Nan if (fd >= 0 && !at_exit) { 633ecfd7a9cSWang Nan rec->bytes_written = 0; 634ecfd7a9cSWang Nan rec->session->header.data_size = 0; 635ecfd7a9cSWang Nan } 636ecfd7a9cSWang Nan 637ecfd7a9cSWang Nan if (!quiet) 638ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Dump %s.%s ]\n", 639eae8ad80SJiri Olsa data->file.path, timestamp); 6403c1cb7e3SWang Nan 6413c1cb7e3SWang Nan /* Output tracking events */ 642be7b0c9eSWang Nan if (!at_exit) { 6434ea648aeSWang Nan record__synthesize(rec, false); 6443c1cb7e3SWang Nan 645be7b0c9eSWang Nan /* 646be7b0c9eSWang Nan * In 'perf record --switch-output' without -a, 647be7b0c9eSWang Nan * record__synthesize() in record__switch_output() won't 648be7b0c9eSWang Nan * generate tracking events because there's no thread_map 649be7b0c9eSWang Nan * in evlist. Which causes newly created perf.data doesn't 650be7b0c9eSWang Nan * contain map and comm information. 651be7b0c9eSWang Nan * Create a fake thread_map and directly call 652be7b0c9eSWang Nan * perf_event__synthesize_thread_map() for those events. 653be7b0c9eSWang Nan */ 654be7b0c9eSWang Nan if (target__none(&rec->opts.target)) 6554ea648aeSWang Nan record__synthesize_workload(rec, false); 656be7b0c9eSWang Nan } 657ecfd7a9cSWang Nan return fd; 658ecfd7a9cSWang Nan } 659ecfd7a9cSWang Nan 660f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno; 661f33cbe72SArnaldo Carvalho de Melo 662f33cbe72SArnaldo Carvalho de Melo /* 663f33cbe72SArnaldo Carvalho de Melo * perf_evlist__prepare_workload will send a SIGUSR1 664f33cbe72SArnaldo Carvalho de Melo * if the fork fails, since we asked by setting its 665f33cbe72SArnaldo Carvalho de Melo * want_signal to true. 666f33cbe72SArnaldo Carvalho de Melo */ 66745604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused, 66845604710SNamhyung Kim siginfo_t *info, 669f33cbe72SArnaldo Carvalho de Melo void *ucontext __maybe_unused) 670f33cbe72SArnaldo Carvalho de Melo { 671f33cbe72SArnaldo Carvalho de Melo workload_exec_errno = info->si_value.sival_int; 672f33cbe72SArnaldo Carvalho de Melo done = 1; 673f33cbe72SArnaldo Carvalho de Melo child_finished = 1; 674f33cbe72SArnaldo Carvalho de Melo } 675f33cbe72SArnaldo Carvalho de Melo 6762dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig); 677bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig); 6782dd6d8a1SAdrian Hunter 67946bc29b9SAdrian Hunter int __weak 68046bc29b9SAdrian Hunter perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused, 68146bc29b9SAdrian Hunter struct perf_tool *tool __maybe_unused, 68246bc29b9SAdrian Hunter perf_event__handler_t process __maybe_unused, 68346bc29b9SAdrian Hunter struct machine *machine __maybe_unused) 68446bc29b9SAdrian Hunter { 68546bc29b9SAdrian Hunter return 0; 68646bc29b9SAdrian Hunter } 68746bc29b9SAdrian Hunter 688ee667f94SWang Nan static const struct perf_event_mmap_page * 689ee667f94SWang Nan perf_evlist__pick_pc(struct perf_evlist *evlist) 690ee667f94SWang Nan { 691b2cb615dSWang Nan if (evlist) { 692b2cb615dSWang Nan if (evlist->mmap && evlist->mmap[0].base) 693ee667f94SWang Nan return evlist->mmap[0].base; 6940b72d69aSWang Nan if (evlist->overwrite_mmap && evlist->overwrite_mmap[0].base) 6950b72d69aSWang Nan return evlist->overwrite_mmap[0].base; 696b2cb615dSWang Nan } 697ee667f94SWang Nan return NULL; 698ee667f94SWang Nan } 699ee667f94SWang Nan 700c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec) 701c45628b0SWang Nan { 702ee667f94SWang Nan const struct perf_event_mmap_page *pc; 703ee667f94SWang Nan 704ee667f94SWang Nan pc = perf_evlist__pick_pc(rec->evlist); 705ee667f94SWang Nan if (pc) 706ee667f94SWang Nan return pc; 707c45628b0SWang Nan return NULL; 708c45628b0SWang Nan } 709c45628b0SWang Nan 7104ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail) 711c45c86ebSWang Nan { 712c45c86ebSWang Nan struct perf_session *session = rec->session; 713c45c86ebSWang Nan struct machine *machine = &session->machines.host; 7148ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 715c45c86ebSWang Nan struct record_opts *opts = &rec->opts; 716c45c86ebSWang Nan struct perf_tool *tool = &rec->tool; 7178ceb41d7SJiri Olsa int fd = perf_data__fd(data); 718c45c86ebSWang Nan int err = 0; 719c45c86ebSWang Nan 7204ea648aeSWang Nan if (rec->opts.tail_synthesize != tail) 7214ea648aeSWang Nan return 0; 7224ea648aeSWang Nan 7238ceb41d7SJiri Olsa if (data->is_pipe) { 724e9def1b2SDavid Carrillo-Cisneros err = perf_event__synthesize_features( 725e9def1b2SDavid Carrillo-Cisneros tool, session, rec->evlist, process_synthesized_event); 726e9def1b2SDavid Carrillo-Cisneros if (err < 0) { 727e9def1b2SDavid Carrillo-Cisneros pr_err("Couldn't synthesize features.\n"); 728e9def1b2SDavid Carrillo-Cisneros return err; 729e9def1b2SDavid Carrillo-Cisneros } 730e9def1b2SDavid Carrillo-Cisneros 731c45c86ebSWang Nan err = perf_event__synthesize_attrs(tool, session, 732c45c86ebSWang Nan process_synthesized_event); 733c45c86ebSWang Nan if (err < 0) { 734c45c86ebSWang Nan pr_err("Couldn't synthesize attrs.\n"); 735c45c86ebSWang Nan goto out; 736c45c86ebSWang Nan } 737c45c86ebSWang Nan 738c45c86ebSWang Nan if (have_tracepoints(&rec->evlist->entries)) { 739c45c86ebSWang Nan /* 740c45c86ebSWang Nan * FIXME err <= 0 here actually means that 741c45c86ebSWang Nan * there were no tracepoints so its not really 742c45c86ebSWang Nan * an error, just that we don't need to 743c45c86ebSWang Nan * synthesize anything. We really have to 744c45c86ebSWang Nan * return this more properly and also 745c45c86ebSWang Nan * propagate errors that now are calling die() 746c45c86ebSWang Nan */ 747c45c86ebSWang Nan err = perf_event__synthesize_tracing_data(tool, fd, rec->evlist, 748c45c86ebSWang Nan process_synthesized_event); 749c45c86ebSWang Nan if (err <= 0) { 750c45c86ebSWang Nan pr_err("Couldn't record tracing data.\n"); 751c45c86ebSWang Nan goto out; 752c45c86ebSWang Nan } 753c45c86ebSWang Nan rec->bytes_written += err; 754c45c86ebSWang Nan } 755c45c86ebSWang Nan } 756c45c86ebSWang Nan 757c45628b0SWang Nan err = perf_event__synth_time_conv(record__pick_pc(rec), tool, 75846bc29b9SAdrian Hunter process_synthesized_event, machine); 75946bc29b9SAdrian Hunter if (err) 76046bc29b9SAdrian Hunter goto out; 76146bc29b9SAdrian Hunter 762c45c86ebSWang Nan if (rec->opts.full_auxtrace) { 763c45c86ebSWang Nan err = perf_event__synthesize_auxtrace_info(rec->itr, tool, 764c45c86ebSWang Nan session, process_synthesized_event); 765c45c86ebSWang Nan if (err) 766c45c86ebSWang Nan goto out; 767c45c86ebSWang Nan } 768c45c86ebSWang Nan 7696c443954SArnaldo Carvalho de Melo if (!perf_evlist__exclude_kernel(rec->evlist)) { 770c45c86ebSWang Nan err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event, 771c45c86ebSWang Nan machine); 772c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n" 773c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 774c45c86ebSWang Nan "Check /proc/kallsyms permission or run as root.\n"); 775c45c86ebSWang Nan 776c45c86ebSWang Nan err = perf_event__synthesize_modules(tool, process_synthesized_event, 777c45c86ebSWang Nan machine); 778c45c86ebSWang Nan WARN_ONCE(err < 0, "Couldn't record kernel module information.\n" 779c45c86ebSWang Nan "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n" 780c45c86ebSWang Nan "Check /proc/modules permission or run as root.\n"); 7816c443954SArnaldo Carvalho de Melo } 782c45c86ebSWang Nan 783c45c86ebSWang Nan if (perf_guest) { 784c45c86ebSWang Nan machines__process_guests(&session->machines, 785c45c86ebSWang Nan perf_event__synthesize_guest_os, tool); 786c45c86ebSWang Nan } 787c45c86ebSWang Nan 788bfd8f72cSAndi Kleen err = perf_event__synthesize_extra_attr(&rec->tool, 789bfd8f72cSAndi Kleen rec->evlist, 790bfd8f72cSAndi Kleen process_synthesized_event, 791bfd8f72cSAndi Kleen data->is_pipe); 792bfd8f72cSAndi Kleen if (err) 793bfd8f72cSAndi Kleen goto out; 794bfd8f72cSAndi Kleen 795373565d2SAndi Kleen err = perf_event__synthesize_thread_map2(&rec->tool, rec->evlist->threads, 796373565d2SAndi Kleen process_synthesized_event, 797373565d2SAndi Kleen NULL); 798373565d2SAndi Kleen if (err < 0) { 799373565d2SAndi Kleen pr_err("Couldn't synthesize thread map.\n"); 800373565d2SAndi Kleen return err; 801373565d2SAndi Kleen } 802373565d2SAndi Kleen 803373565d2SAndi Kleen err = perf_event__synthesize_cpu_map(&rec->tool, rec->evlist->cpus, 804373565d2SAndi Kleen process_synthesized_event, NULL); 805373565d2SAndi Kleen if (err < 0) { 806373565d2SAndi Kleen pr_err("Couldn't synthesize cpu map.\n"); 807373565d2SAndi Kleen return err; 808373565d2SAndi Kleen } 809373565d2SAndi Kleen 810c45c86ebSWang Nan err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads, 811c45c86ebSWang Nan process_synthesized_event, opts->sample_address, 812340b47f5SKan Liang opts->proc_map_timeout, 1); 813c45c86ebSWang Nan out: 814c45c86ebSWang Nan return err; 815c45c86ebSWang Nan } 816c45c86ebSWang Nan 8178c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv) 81886470930SIngo Molnar { 81957706abcSDavid Ahern int err; 82045604710SNamhyung Kim int status = 0; 8218b412664SPeter Zijlstra unsigned long waking = 0; 82246be604bSZhang, Yanmin const bool forks = argc > 0; 82323346f21SArnaldo Carvalho de Melo struct machine *machine; 82445694aa7SArnaldo Carvalho de Melo struct perf_tool *tool = &rec->tool; 825b4006796SArnaldo Carvalho de Melo struct record_opts *opts = &rec->opts; 8268ceb41d7SJiri Olsa struct perf_data *data = &rec->data; 827d20deb64SArnaldo Carvalho de Melo struct perf_session *session; 8286dcf45efSArnaldo Carvalho de Melo bool disabled = false, draining = false; 82942aa276fSNamhyung Kim int fd; 83086470930SIngo Molnar 831d20deb64SArnaldo Carvalho de Melo rec->progname = argv[0]; 83233e49ea7SAndi Kleen 83345604710SNamhyung Kim atexit(record__sig_exit); 834f5970550SPeter Zijlstra signal(SIGCHLD, sig_handler); 835f5970550SPeter Zijlstra signal(SIGINT, sig_handler); 836804f7ac7SDavid Ahern signal(SIGTERM, sig_handler); 837a074865eSWang Nan signal(SIGSEGV, sigsegv_handler); 838c0bdc1c4SWang Nan 839f3b3614aSHari Bathini if (rec->opts.record_namespaces) 840f3b3614aSHari Bathini tool->namespace_events = true; 841f3b3614aSHari Bathini 842dc0c6127SJiri Olsa if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) { 8432dd6d8a1SAdrian Hunter signal(SIGUSR2, snapshot_sig_handler); 8443c1cb7e3SWang Nan if (rec->opts.auxtrace_snapshot_mode) 8455f9cf599SWang Nan trigger_on(&auxtrace_snapshot_trigger); 846dc0c6127SJiri Olsa if (rec->switch_output.enabled) 8473c1cb7e3SWang Nan trigger_on(&switch_output_trigger); 848c0bdc1c4SWang Nan } else { 8492dd6d8a1SAdrian Hunter signal(SIGUSR2, SIG_IGN); 850c0bdc1c4SWang Nan } 851f5970550SPeter Zijlstra 8528ceb41d7SJiri Olsa session = perf_session__new(data, false, tool); 85394c744b6SArnaldo Carvalho de Melo if (session == NULL) { 854ffa91880SAdrien BAK pr_err("Perf session creation failed.\n"); 855a9a70bbcSArnaldo Carvalho de Melo return -1; 856a9a70bbcSArnaldo Carvalho de Melo } 857a9a70bbcSArnaldo Carvalho de Melo 8588ceb41d7SJiri Olsa fd = perf_data__fd(data); 859d20deb64SArnaldo Carvalho de Melo rec->session = session; 860d20deb64SArnaldo Carvalho de Melo 8618c6f45a7SArnaldo Carvalho de Melo record__init_features(rec); 862330aa675SStephane Eranian 863d4db3f16SArnaldo Carvalho de Melo if (forks) { 8643e2be2daSArnaldo Carvalho de Melo err = perf_evlist__prepare_workload(rec->evlist, &opts->target, 8658ceb41d7SJiri Olsa argv, data->is_pipe, 866735f7e0bSArnaldo Carvalho de Melo workload_exec_failed_signal); 86735b9d88eSArnaldo Carvalho de Melo if (err < 0) { 86835b9d88eSArnaldo Carvalho de Melo pr_err("Couldn't run the workload!\n"); 86945604710SNamhyung Kim status = err; 87035b9d88eSArnaldo Carvalho de Melo goto out_delete_session; 871856e9660SPeter Zijlstra } 872856e9660SPeter Zijlstra } 873856e9660SPeter Zijlstra 8748c6f45a7SArnaldo Carvalho de Melo if (record__open(rec) != 0) { 8758d3eca20SDavid Ahern err = -1; 87645604710SNamhyung Kim goto out_child; 8778d3eca20SDavid Ahern } 87886470930SIngo Molnar 8798690a2a7SWang Nan err = bpf__apply_obj_config(); 8808690a2a7SWang Nan if (err) { 8818690a2a7SWang Nan char errbuf[BUFSIZ]; 8828690a2a7SWang Nan 8838690a2a7SWang Nan bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf)); 8848690a2a7SWang Nan pr_err("ERROR: Apply config to BPF failed: %s\n", 8858690a2a7SWang Nan errbuf); 8868690a2a7SWang Nan goto out_child; 8878690a2a7SWang Nan } 8888690a2a7SWang Nan 889cca8482cSAdrian Hunter /* 890cca8482cSAdrian Hunter * Normally perf_session__new would do this, but it doesn't have the 891cca8482cSAdrian Hunter * evlist. 892cca8482cSAdrian Hunter */ 893cca8482cSAdrian Hunter if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) { 894cca8482cSAdrian Hunter pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n"); 895cca8482cSAdrian Hunter rec->tool.ordered_events = false; 896cca8482cSAdrian Hunter } 897cca8482cSAdrian Hunter 8983e2be2daSArnaldo Carvalho de Melo if (!rec->evlist->nr_groups) 899a8bb559bSNamhyung Kim perf_header__clear_feat(&session->header, HEADER_GROUP_DESC); 900a8bb559bSNamhyung Kim 9018ceb41d7SJiri Olsa if (data->is_pipe) { 90242aa276fSNamhyung Kim err = perf_header__write_pipe(fd); 903529870e3STom Zanussi if (err < 0) 90445604710SNamhyung Kim goto out_child; 905563aecb2SJiri Olsa } else { 90642aa276fSNamhyung Kim err = perf_session__write_header(session, rec->evlist, fd, false); 907d5eed904SArnaldo Carvalho de Melo if (err < 0) 90845604710SNamhyung Kim goto out_child; 909d5eed904SArnaldo Carvalho de Melo } 9107c6a1c65SPeter Zijlstra 911d3665498SDavid Ahern if (!rec->no_buildid 912e20960c0SRobert Richter && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) { 913d3665498SDavid Ahern pr_err("Couldn't generate buildids. " 914e20960c0SRobert Richter "Use --no-buildid to profile anyway.\n"); 9158d3eca20SDavid Ahern err = -1; 91645604710SNamhyung Kim goto out_child; 917e20960c0SRobert Richter } 918e20960c0SRobert Richter 91934ba5122SArnaldo Carvalho de Melo machine = &session->machines.host; 920743eb868SArnaldo Carvalho de Melo 9214ea648aeSWang Nan err = record__synthesize(rec, false); 922c45c86ebSWang Nan if (err < 0) 92345604710SNamhyung Kim goto out_child; 9248d3eca20SDavid Ahern 925d20deb64SArnaldo Carvalho de Melo if (rec->realtime_prio) { 92686470930SIngo Molnar struct sched_param param; 92786470930SIngo Molnar 928d20deb64SArnaldo Carvalho de Melo param.sched_priority = rec->realtime_prio; 92986470930SIngo Molnar if (sched_setscheduler(0, SCHED_FIFO, ¶m)) { 9306beba7adSArnaldo Carvalho de Melo pr_err("Could not set realtime priority.\n"); 9318d3eca20SDavid Ahern err = -1; 93245604710SNamhyung Kim goto out_child; 93386470930SIngo Molnar } 93486470930SIngo Molnar } 93586470930SIngo Molnar 936774cb499SJiri Olsa /* 937774cb499SJiri Olsa * When perf is starting the traced process, all the events 938774cb499SJiri Olsa * (apart from group members) have enable_on_exec=1 set, 939774cb499SJiri Olsa * so don't spoil it by prematurely enabling them. 940774cb499SJiri Olsa */ 9416619a53eSAndi Kleen if (!target__none(&opts->target) && !opts->initial_delay) 9423e2be2daSArnaldo Carvalho de Melo perf_evlist__enable(rec->evlist); 943764e16a3SDavid Ahern 944856e9660SPeter Zijlstra /* 945856e9660SPeter Zijlstra * Let the child rip 946856e9660SPeter Zijlstra */ 947e803cf97SNamhyung Kim if (forks) { 948e5bed564SNamhyung Kim union perf_event *event; 949e907caf3SHari Bathini pid_t tgid; 950e5bed564SNamhyung Kim 951e5bed564SNamhyung Kim event = malloc(sizeof(event->comm) + machine->id_hdr_size); 952e5bed564SNamhyung Kim if (event == NULL) { 953e5bed564SNamhyung Kim err = -ENOMEM; 954e5bed564SNamhyung Kim goto out_child; 955e5bed564SNamhyung Kim } 956e5bed564SNamhyung Kim 957e803cf97SNamhyung Kim /* 958e803cf97SNamhyung Kim * Some H/W events are generated before COMM event 959e803cf97SNamhyung Kim * which is emitted during exec(), so perf script 960e803cf97SNamhyung Kim * cannot see a correct process name for those events. 961e803cf97SNamhyung Kim * Synthesize COMM event to prevent it. 962e803cf97SNamhyung Kim */ 963e907caf3SHari Bathini tgid = perf_event__synthesize_comm(tool, event, 964e803cf97SNamhyung Kim rec->evlist->workload.pid, 965e803cf97SNamhyung Kim process_synthesized_event, 966e803cf97SNamhyung Kim machine); 967e5bed564SNamhyung Kim free(event); 968e803cf97SNamhyung Kim 969e907caf3SHari Bathini if (tgid == -1) 970e907caf3SHari Bathini goto out_child; 971e907caf3SHari Bathini 972e907caf3SHari Bathini event = malloc(sizeof(event->namespaces) + 973e907caf3SHari Bathini (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) + 974e907caf3SHari Bathini machine->id_hdr_size); 975e907caf3SHari Bathini if (event == NULL) { 976e907caf3SHari Bathini err = -ENOMEM; 977e907caf3SHari Bathini goto out_child; 978e907caf3SHari Bathini } 979e907caf3SHari Bathini 980e907caf3SHari Bathini /* 981e907caf3SHari Bathini * Synthesize NAMESPACES event for the command specified. 982e907caf3SHari Bathini */ 983e907caf3SHari Bathini perf_event__synthesize_namespaces(tool, event, 984e907caf3SHari Bathini rec->evlist->workload.pid, 985e907caf3SHari Bathini tgid, process_synthesized_event, 986e907caf3SHari Bathini machine); 987e907caf3SHari Bathini free(event); 988e907caf3SHari Bathini 9893e2be2daSArnaldo Carvalho de Melo perf_evlist__start_workload(rec->evlist); 990e803cf97SNamhyung Kim } 991856e9660SPeter Zijlstra 9926619a53eSAndi Kleen if (opts->initial_delay) { 9930693e680SArnaldo Carvalho de Melo usleep(opts->initial_delay * USEC_PER_MSEC); 9946619a53eSAndi Kleen perf_evlist__enable(rec->evlist); 9956619a53eSAndi Kleen } 9966619a53eSAndi Kleen 9975f9cf599SWang Nan trigger_ready(&auxtrace_snapshot_trigger); 9983c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 999a074865eSWang Nan perf_hooks__invoke_record_start(); 1000649c48a9SPeter Zijlstra for (;;) { 10019f065194SYang Shi unsigned long long hits = rec->samples; 100286470930SIngo Molnar 100305737464SWang Nan /* 100405737464SWang Nan * rec->evlist->bkw_mmap_state is possible to be 100505737464SWang Nan * BKW_MMAP_EMPTY here: when done == true and 100605737464SWang Nan * hits != rec->samples in previous round. 100705737464SWang Nan * 100805737464SWang Nan * perf_evlist__toggle_bkw_mmap ensure we never 100905737464SWang Nan * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING. 101005737464SWang Nan */ 101105737464SWang Nan if (trigger_is_hit(&switch_output_trigger) || done || draining) 101205737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING); 101305737464SWang Nan 10148c6f45a7SArnaldo Carvalho de Melo if (record__mmap_read_all(rec) < 0) { 10155f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 10163c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 10178d3eca20SDavid Ahern err = -1; 101845604710SNamhyung Kim goto out_child; 10198d3eca20SDavid Ahern } 102086470930SIngo Molnar 10212dd6d8a1SAdrian Hunter if (auxtrace_record__snapshot_started) { 10222dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 0; 10235f9cf599SWang Nan if (!trigger_is_error(&auxtrace_snapshot_trigger)) 10242dd6d8a1SAdrian Hunter record__read_auxtrace_snapshot(rec); 10255f9cf599SWang Nan if (trigger_is_error(&auxtrace_snapshot_trigger)) { 10262dd6d8a1SAdrian Hunter pr_err("AUX area tracing snapshot failed\n"); 10272dd6d8a1SAdrian Hunter err = -1; 10282dd6d8a1SAdrian Hunter goto out_child; 10292dd6d8a1SAdrian Hunter } 10302dd6d8a1SAdrian Hunter } 10312dd6d8a1SAdrian Hunter 10323c1cb7e3SWang Nan if (trigger_is_hit(&switch_output_trigger)) { 103305737464SWang Nan /* 103405737464SWang Nan * If switch_output_trigger is hit, the data in 103505737464SWang Nan * overwritable ring buffer should have been collected, 103605737464SWang Nan * so bkw_mmap_state should be set to BKW_MMAP_EMPTY. 103705737464SWang Nan * 103805737464SWang Nan * If SIGUSR2 raise after or during record__mmap_read_all(), 103905737464SWang Nan * record__mmap_read_all() didn't collect data from 104005737464SWang Nan * overwritable ring buffer. Read again. 104105737464SWang Nan */ 104205737464SWang Nan if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING) 104305737464SWang Nan continue; 10443c1cb7e3SWang Nan trigger_ready(&switch_output_trigger); 10453c1cb7e3SWang Nan 104605737464SWang Nan /* 104705737464SWang Nan * Reenable events in overwrite ring buffer after 104805737464SWang Nan * record__mmap_read_all(): we should have collected 104905737464SWang Nan * data from it. 105005737464SWang Nan */ 105105737464SWang Nan perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING); 105205737464SWang Nan 10533c1cb7e3SWang Nan if (!quiet) 10543c1cb7e3SWang Nan fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n", 10553c1cb7e3SWang Nan waking); 10563c1cb7e3SWang Nan waking = 0; 10573c1cb7e3SWang Nan fd = record__switch_output(rec, false); 10583c1cb7e3SWang Nan if (fd < 0) { 10593c1cb7e3SWang Nan pr_err("Failed to switch to new file\n"); 10603c1cb7e3SWang Nan trigger_error(&switch_output_trigger); 10613c1cb7e3SWang Nan err = fd; 10623c1cb7e3SWang Nan goto out_child; 10633c1cb7e3SWang Nan } 1064bfacbe3bSJiri Olsa 1065bfacbe3bSJiri Olsa /* re-arm the alarm */ 1066bfacbe3bSJiri Olsa if (rec->switch_output.time) 1067bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 10683c1cb7e3SWang Nan } 10693c1cb7e3SWang Nan 1070d20deb64SArnaldo Carvalho de Melo if (hits == rec->samples) { 10716dcf45efSArnaldo Carvalho de Melo if (done || draining) 1072649c48a9SPeter Zijlstra break; 1073f66a889dSArnaldo Carvalho de Melo err = perf_evlist__poll(rec->evlist, -1); 1074a515114fSJiri Olsa /* 1075a515114fSJiri Olsa * Propagate error, only if there's any. Ignore positive 1076a515114fSJiri Olsa * number of returned events and interrupt error. 1077a515114fSJiri Olsa */ 1078a515114fSJiri Olsa if (err > 0 || (err < 0 && errno == EINTR)) 107945604710SNamhyung Kim err = 0; 10808b412664SPeter Zijlstra waking++; 10816dcf45efSArnaldo Carvalho de Melo 10826dcf45efSArnaldo Carvalho de Melo if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0) 10836dcf45efSArnaldo Carvalho de Melo draining = true; 10848b412664SPeter Zijlstra } 10858b412664SPeter Zijlstra 1086774cb499SJiri Olsa /* 1087774cb499SJiri Olsa * When perf is starting the traced process, at the end events 1088774cb499SJiri Olsa * die with the process and we wait for that. Thus no need to 1089774cb499SJiri Olsa * disable events in this case. 1090774cb499SJiri Olsa */ 1091602ad878SArnaldo Carvalho de Melo if (done && !disabled && !target__none(&opts->target)) { 10925f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 10933e2be2daSArnaldo Carvalho de Melo perf_evlist__disable(rec->evlist); 10942711926aSJiri Olsa disabled = true; 10952711926aSJiri Olsa } 10968b412664SPeter Zijlstra } 10975f9cf599SWang Nan trigger_off(&auxtrace_snapshot_trigger); 10983c1cb7e3SWang Nan trigger_off(&switch_output_trigger); 10998b412664SPeter Zijlstra 1100f33cbe72SArnaldo Carvalho de Melo if (forks && workload_exec_errno) { 110135550da3SMasami Hiramatsu char msg[STRERR_BUFSIZE]; 1102c8b5f2c9SArnaldo Carvalho de Melo const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 1103f33cbe72SArnaldo Carvalho de Melo pr_err("Workload failed: %s\n", emsg); 1104f33cbe72SArnaldo Carvalho de Melo err = -1; 110545604710SNamhyung Kim goto out_child; 1106f33cbe72SArnaldo Carvalho de Melo } 1107f33cbe72SArnaldo Carvalho de Melo 1108e3d59112SNamhyung Kim if (!quiet) 11098b412664SPeter Zijlstra fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking); 111086470930SIngo Molnar 11114ea648aeSWang Nan if (target__none(&rec->opts.target)) 11124ea648aeSWang Nan record__synthesize_workload(rec, true); 11134ea648aeSWang Nan 111445604710SNamhyung Kim out_child: 111545604710SNamhyung Kim if (forks) { 111645604710SNamhyung Kim int exit_status; 111745604710SNamhyung Kim 111845604710SNamhyung Kim if (!child_finished) 111945604710SNamhyung Kim kill(rec->evlist->workload.pid, SIGTERM); 112045604710SNamhyung Kim 112145604710SNamhyung Kim wait(&exit_status); 112245604710SNamhyung Kim 112345604710SNamhyung Kim if (err < 0) 112445604710SNamhyung Kim status = err; 112545604710SNamhyung Kim else if (WIFEXITED(exit_status)) 112645604710SNamhyung Kim status = WEXITSTATUS(exit_status); 112745604710SNamhyung Kim else if (WIFSIGNALED(exit_status)) 112845604710SNamhyung Kim signr = WTERMSIG(exit_status); 112945604710SNamhyung Kim } else 113045604710SNamhyung Kim status = err; 113145604710SNamhyung Kim 11324ea648aeSWang Nan record__synthesize(rec, true); 1133e3d59112SNamhyung Kim /* this will be recalculated during process_buildids() */ 1134e3d59112SNamhyung Kim rec->samples = 0; 1135e3d59112SNamhyung Kim 1136ecfd7a9cSWang Nan if (!err) { 1137ecfd7a9cSWang Nan if (!rec->timestamp_filename) { 1138e1ab48baSWang Nan record__finish_output(rec); 1139ecfd7a9cSWang Nan } else { 1140ecfd7a9cSWang Nan fd = record__switch_output(rec, true); 1141ecfd7a9cSWang Nan if (fd < 0) { 1142ecfd7a9cSWang Nan status = fd; 1143ecfd7a9cSWang Nan goto out_delete_session; 1144ecfd7a9cSWang Nan } 1145ecfd7a9cSWang Nan } 1146ecfd7a9cSWang Nan } 114739d17dacSArnaldo Carvalho de Melo 1148a074865eSWang Nan perf_hooks__invoke_record_end(); 1149a074865eSWang Nan 1150e3d59112SNamhyung Kim if (!err && !quiet) { 1151e3d59112SNamhyung Kim char samples[128]; 1152ecfd7a9cSWang Nan const char *postfix = rec->timestamp_filename ? 1153ecfd7a9cSWang Nan ".<timestamp>" : ""; 1154e3d59112SNamhyung Kim 1155ef149c25SAdrian Hunter if (rec->samples && !rec->opts.full_auxtrace) 1156e3d59112SNamhyung Kim scnprintf(samples, sizeof(samples), 1157e3d59112SNamhyung Kim " (%" PRIu64 " samples)", rec->samples); 1158e3d59112SNamhyung Kim else 1159e3d59112SNamhyung Kim samples[0] = '\0'; 1160e3d59112SNamhyung Kim 1161ecfd7a9cSWang Nan fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s%s%s ]\n", 11628ceb41d7SJiri Olsa perf_data__size(data) / 1024.0 / 1024.0, 1163eae8ad80SJiri Olsa data->file.path, postfix, samples); 1164e3d59112SNamhyung Kim } 1165e3d59112SNamhyung Kim 116639d17dacSArnaldo Carvalho de Melo out_delete_session: 116739d17dacSArnaldo Carvalho de Melo perf_session__delete(session); 116845604710SNamhyung Kim return status; 116986470930SIngo Molnar } 117086470930SIngo Molnar 11710883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain) 117209b0fd45SJiri Olsa { 1173aad2b21cSKan Liang static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" }; 1174a601fdffSJiri Olsa 11750883e820SArnaldo Carvalho de Melo pr_debug("callchain: type %s\n", str[callchain->record_mode]); 117626d33022SJiri Olsa 11770883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 117809b0fd45SJiri Olsa pr_debug("callchain: stack dump size %d\n", 11790883e820SArnaldo Carvalho de Melo callchain->dump_size); 11800883e820SArnaldo Carvalho de Melo } 11810883e820SArnaldo Carvalho de Melo 11820883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record, 11830883e820SArnaldo Carvalho de Melo struct callchain_param *callchain, 11840883e820SArnaldo Carvalho de Melo const char *arg, bool unset) 11850883e820SArnaldo Carvalho de Melo { 11860883e820SArnaldo Carvalho de Melo int ret; 11870883e820SArnaldo Carvalho de Melo callchain->enabled = !unset; 11880883e820SArnaldo Carvalho de Melo 11890883e820SArnaldo Carvalho de Melo /* --no-call-graph */ 11900883e820SArnaldo Carvalho de Melo if (unset) { 11910883e820SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_NONE; 11920883e820SArnaldo Carvalho de Melo pr_debug("callchain: disabled\n"); 11930883e820SArnaldo Carvalho de Melo return 0; 11940883e820SArnaldo Carvalho de Melo } 11950883e820SArnaldo Carvalho de Melo 11960883e820SArnaldo Carvalho de Melo ret = parse_callchain_record_opt(arg, callchain); 11970883e820SArnaldo Carvalho de Melo if (!ret) { 11980883e820SArnaldo Carvalho de Melo /* Enable data address sampling for DWARF unwind. */ 11990883e820SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_DWARF) 12000883e820SArnaldo Carvalho de Melo record->sample_address = true; 12010883e820SArnaldo Carvalho de Melo callchain_debug(callchain); 12020883e820SArnaldo Carvalho de Melo } 12030883e820SArnaldo Carvalho de Melo 12040883e820SArnaldo Carvalho de Melo return ret; 120509b0fd45SJiri Olsa } 120609b0fd45SJiri Olsa 1207c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt, 120809b0fd45SJiri Olsa const char *arg, 120909b0fd45SJiri Olsa int unset) 121009b0fd45SJiri Olsa { 12110883e820SArnaldo Carvalho de Melo return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset); 121226d33022SJiri Olsa } 121326d33022SJiri Olsa 1214c421e80bSKan Liang int record_callchain_opt(const struct option *opt, 121509b0fd45SJiri Olsa const char *arg __maybe_unused, 121609b0fd45SJiri Olsa int unset __maybe_unused) 121709b0fd45SJiri Olsa { 12182ddd5c04SArnaldo Carvalho de Melo struct callchain_param *callchain = opt->value; 1219c421e80bSKan Liang 12202ddd5c04SArnaldo Carvalho de Melo callchain->enabled = true; 122109b0fd45SJiri Olsa 12222ddd5c04SArnaldo Carvalho de Melo if (callchain->record_mode == CALLCHAIN_NONE) 12232ddd5c04SArnaldo Carvalho de Melo callchain->record_mode = CALLCHAIN_FP; 1224eb853e80SJiri Olsa 12252ddd5c04SArnaldo Carvalho de Melo callchain_debug(callchain); 122609b0fd45SJiri Olsa return 0; 122709b0fd45SJiri Olsa } 122809b0fd45SJiri Olsa 1229eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb) 1230eb853e80SJiri Olsa { 12317a29c087SNamhyung Kim struct record *rec = cb; 12327a29c087SNamhyung Kim 12337a29c087SNamhyung Kim if (!strcmp(var, "record.build-id")) { 12347a29c087SNamhyung Kim if (!strcmp(value, "cache")) 12357a29c087SNamhyung Kim rec->no_buildid_cache = false; 12367a29c087SNamhyung Kim else if (!strcmp(value, "no-cache")) 12377a29c087SNamhyung Kim rec->no_buildid_cache = true; 12387a29c087SNamhyung Kim else if (!strcmp(value, "skip")) 12397a29c087SNamhyung Kim rec->no_buildid = true; 12407a29c087SNamhyung Kim else 12417a29c087SNamhyung Kim return -1; 12427a29c087SNamhyung Kim return 0; 12437a29c087SNamhyung Kim } 1244eb853e80SJiri Olsa if (!strcmp(var, "record.call-graph")) 12455a2e5e85SNamhyung Kim var = "call-graph.record-mode"; /* fall-through */ 1246eb853e80SJiri Olsa 1247eb853e80SJiri Olsa return perf_default_config(var, value, cb); 1248eb853e80SJiri Olsa } 1249eb853e80SJiri Olsa 1250814c8c38SPeter Zijlstra struct clockid_map { 1251814c8c38SPeter Zijlstra const char *name; 1252814c8c38SPeter Zijlstra int clockid; 1253814c8c38SPeter Zijlstra }; 1254814c8c38SPeter Zijlstra 1255814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c) \ 1256814c8c38SPeter Zijlstra { .name = n, .clockid = (c), } 1257814c8c38SPeter Zijlstra 1258814c8c38SPeter Zijlstra #define CLOCKID_END { .name = NULL, } 1259814c8c38SPeter Zijlstra 1260814c8c38SPeter Zijlstra 1261814c8c38SPeter Zijlstra /* 1262814c8c38SPeter Zijlstra * Add the missing ones, we need to build on many distros... 1263814c8c38SPeter Zijlstra */ 1264814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW 1265814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4 1266814c8c38SPeter Zijlstra #endif 1267814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME 1268814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7 1269814c8c38SPeter Zijlstra #endif 1270814c8c38SPeter Zijlstra #ifndef CLOCK_TAI 1271814c8c38SPeter Zijlstra #define CLOCK_TAI 11 1272814c8c38SPeter Zijlstra #endif 1273814c8c38SPeter Zijlstra 1274814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = { 1275814c8c38SPeter Zijlstra /* available for all events, NMI safe */ 1276814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic", CLOCK_MONOTONIC), 1277814c8c38SPeter Zijlstra CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW), 1278814c8c38SPeter Zijlstra 1279814c8c38SPeter Zijlstra /* available for some events */ 1280814c8c38SPeter Zijlstra CLOCKID_MAP("realtime", CLOCK_REALTIME), 1281814c8c38SPeter Zijlstra CLOCKID_MAP("boottime", CLOCK_BOOTTIME), 1282814c8c38SPeter Zijlstra CLOCKID_MAP("tai", CLOCK_TAI), 1283814c8c38SPeter Zijlstra 1284814c8c38SPeter Zijlstra /* available for the lazy */ 1285814c8c38SPeter Zijlstra CLOCKID_MAP("mono", CLOCK_MONOTONIC), 1286814c8c38SPeter Zijlstra CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW), 1287814c8c38SPeter Zijlstra CLOCKID_MAP("real", CLOCK_REALTIME), 1288814c8c38SPeter Zijlstra CLOCKID_MAP("boot", CLOCK_BOOTTIME), 1289814c8c38SPeter Zijlstra 1290814c8c38SPeter Zijlstra CLOCKID_END, 1291814c8c38SPeter Zijlstra }; 1292814c8c38SPeter Zijlstra 1293814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset) 1294814c8c38SPeter Zijlstra { 1295814c8c38SPeter Zijlstra struct record_opts *opts = (struct record_opts *)opt->value; 1296814c8c38SPeter Zijlstra const struct clockid_map *cm; 1297814c8c38SPeter Zijlstra const char *ostr = str; 1298814c8c38SPeter Zijlstra 1299814c8c38SPeter Zijlstra if (unset) { 1300814c8c38SPeter Zijlstra opts->use_clockid = 0; 1301814c8c38SPeter Zijlstra return 0; 1302814c8c38SPeter Zijlstra } 1303814c8c38SPeter Zijlstra 1304814c8c38SPeter Zijlstra /* no arg passed */ 1305814c8c38SPeter Zijlstra if (!str) 1306814c8c38SPeter Zijlstra return 0; 1307814c8c38SPeter Zijlstra 1308814c8c38SPeter Zijlstra /* no setting it twice */ 1309814c8c38SPeter Zijlstra if (opts->use_clockid) 1310814c8c38SPeter Zijlstra return -1; 1311814c8c38SPeter Zijlstra 1312814c8c38SPeter Zijlstra opts->use_clockid = true; 1313814c8c38SPeter Zijlstra 1314814c8c38SPeter Zijlstra /* if its a number, we're done */ 1315814c8c38SPeter Zijlstra if (sscanf(str, "%d", &opts->clockid) == 1) 1316814c8c38SPeter Zijlstra return 0; 1317814c8c38SPeter Zijlstra 1318814c8c38SPeter Zijlstra /* allow a "CLOCK_" prefix to the name */ 1319814c8c38SPeter Zijlstra if (!strncasecmp(str, "CLOCK_", 6)) 1320814c8c38SPeter Zijlstra str += 6; 1321814c8c38SPeter Zijlstra 1322814c8c38SPeter Zijlstra for (cm = clockids; cm->name; cm++) { 1323814c8c38SPeter Zijlstra if (!strcasecmp(str, cm->name)) { 1324814c8c38SPeter Zijlstra opts->clockid = cm->clockid; 1325814c8c38SPeter Zijlstra return 0; 1326814c8c38SPeter Zijlstra } 1327814c8c38SPeter Zijlstra } 1328814c8c38SPeter Zijlstra 1329814c8c38SPeter Zijlstra opts->use_clockid = false; 1330814c8c38SPeter Zijlstra ui__warning("unknown clockid %s, check man page\n", ostr); 1331814c8c38SPeter Zijlstra return -1; 1332814c8c38SPeter Zijlstra } 1333814c8c38SPeter Zijlstra 1334e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt, 1335e9db1310SAdrian Hunter const char *str, 1336e9db1310SAdrian Hunter int unset __maybe_unused) 1337e9db1310SAdrian Hunter { 1338e9db1310SAdrian Hunter struct record_opts *opts = opt->value; 1339e9db1310SAdrian Hunter char *s, *p; 1340e9db1310SAdrian Hunter unsigned int mmap_pages; 1341e9db1310SAdrian Hunter int ret; 1342e9db1310SAdrian Hunter 1343e9db1310SAdrian Hunter if (!str) 1344e9db1310SAdrian Hunter return -EINVAL; 1345e9db1310SAdrian Hunter 1346e9db1310SAdrian Hunter s = strdup(str); 1347e9db1310SAdrian Hunter if (!s) 1348e9db1310SAdrian Hunter return -ENOMEM; 1349e9db1310SAdrian Hunter 1350e9db1310SAdrian Hunter p = strchr(s, ','); 1351e9db1310SAdrian Hunter if (p) 1352e9db1310SAdrian Hunter *p = '\0'; 1353e9db1310SAdrian Hunter 1354e9db1310SAdrian Hunter if (*s) { 1355e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s); 1356e9db1310SAdrian Hunter if (ret) 1357e9db1310SAdrian Hunter goto out_free; 1358e9db1310SAdrian Hunter opts->mmap_pages = mmap_pages; 1359e9db1310SAdrian Hunter } 1360e9db1310SAdrian Hunter 1361e9db1310SAdrian Hunter if (!p) { 1362e9db1310SAdrian Hunter ret = 0; 1363e9db1310SAdrian Hunter goto out_free; 1364e9db1310SAdrian Hunter } 1365e9db1310SAdrian Hunter 1366e9db1310SAdrian Hunter ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1); 1367e9db1310SAdrian Hunter if (ret) 1368e9db1310SAdrian Hunter goto out_free; 1369e9db1310SAdrian Hunter 1370e9db1310SAdrian Hunter opts->auxtrace_mmap_pages = mmap_pages; 1371e9db1310SAdrian Hunter 1372e9db1310SAdrian Hunter out_free: 1373e9db1310SAdrian Hunter free(s); 1374e9db1310SAdrian Hunter return ret; 1375e9db1310SAdrian Hunter } 1376e9db1310SAdrian Hunter 13770c582449SJiri Olsa static void switch_output_size_warn(struct record *rec) 13780c582449SJiri Olsa { 13790c582449SJiri Olsa u64 wakeup_size = perf_evlist__mmap_size(rec->opts.mmap_pages); 13800c582449SJiri Olsa struct switch_output *s = &rec->switch_output; 13810c582449SJiri Olsa 13820c582449SJiri Olsa wakeup_size /= 2; 13830c582449SJiri Olsa 13840c582449SJiri Olsa if (s->size < wakeup_size) { 13850c582449SJiri Olsa char buf[100]; 13860c582449SJiri Olsa 13870c582449SJiri Olsa unit_number__scnprintf(buf, sizeof(buf), wakeup_size); 13880c582449SJiri Olsa pr_warning("WARNING: switch-output data size lower than " 13890c582449SJiri Olsa "wakeup kernel buffer size (%s) " 13900c582449SJiri Olsa "expect bigger perf.data sizes\n", buf); 13910c582449SJiri Olsa } 13920c582449SJiri Olsa } 13930c582449SJiri Olsa 1394cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec) 1395cb4e1ebbSJiri Olsa { 1396cb4e1ebbSJiri Olsa struct switch_output *s = &rec->switch_output; 1397dc0c6127SJiri Olsa static struct parse_tag tags_size[] = { 1398dc0c6127SJiri Olsa { .tag = 'B', .mult = 1 }, 1399dc0c6127SJiri Olsa { .tag = 'K', .mult = 1 << 10 }, 1400dc0c6127SJiri Olsa { .tag = 'M', .mult = 1 << 20 }, 1401dc0c6127SJiri Olsa { .tag = 'G', .mult = 1 << 30 }, 1402dc0c6127SJiri Olsa { .tag = 0 }, 1403dc0c6127SJiri Olsa }; 1404bfacbe3bSJiri Olsa static struct parse_tag tags_time[] = { 1405bfacbe3bSJiri Olsa { .tag = 's', .mult = 1 }, 1406bfacbe3bSJiri Olsa { .tag = 'm', .mult = 60 }, 1407bfacbe3bSJiri Olsa { .tag = 'h', .mult = 60*60 }, 1408bfacbe3bSJiri Olsa { .tag = 'd', .mult = 60*60*24 }, 1409bfacbe3bSJiri Olsa { .tag = 0 }, 1410bfacbe3bSJiri Olsa }; 1411dc0c6127SJiri Olsa unsigned long val; 1412cb4e1ebbSJiri Olsa 1413cb4e1ebbSJiri Olsa if (!s->set) 1414cb4e1ebbSJiri Olsa return 0; 1415cb4e1ebbSJiri Olsa 1416cb4e1ebbSJiri Olsa if (!strcmp(s->str, "signal")) { 1417cb4e1ebbSJiri Olsa s->signal = true; 1418cb4e1ebbSJiri Olsa pr_debug("switch-output with SIGUSR2 signal\n"); 1419dc0c6127SJiri Olsa goto enabled; 1420dc0c6127SJiri Olsa } 1421dc0c6127SJiri Olsa 1422dc0c6127SJiri Olsa val = parse_tag_value(s->str, tags_size); 1423dc0c6127SJiri Olsa if (val != (unsigned long) -1) { 1424dc0c6127SJiri Olsa s->size = val; 1425dc0c6127SJiri Olsa pr_debug("switch-output with %s size threshold\n", s->str); 1426dc0c6127SJiri Olsa goto enabled; 1427cb4e1ebbSJiri Olsa } 1428cb4e1ebbSJiri Olsa 1429bfacbe3bSJiri Olsa val = parse_tag_value(s->str, tags_time); 1430bfacbe3bSJiri Olsa if (val != (unsigned long) -1) { 1431bfacbe3bSJiri Olsa s->time = val; 1432bfacbe3bSJiri Olsa pr_debug("switch-output with %s time threshold (%lu seconds)\n", 1433bfacbe3bSJiri Olsa s->str, s->time); 1434bfacbe3bSJiri Olsa goto enabled; 1435bfacbe3bSJiri Olsa } 1436bfacbe3bSJiri Olsa 1437cb4e1ebbSJiri Olsa return -1; 1438dc0c6127SJiri Olsa 1439dc0c6127SJiri Olsa enabled: 1440dc0c6127SJiri Olsa rec->timestamp_filename = true; 1441dc0c6127SJiri Olsa s->enabled = true; 14420c582449SJiri Olsa 14430c582449SJiri Olsa if (s->size && !rec->opts.no_buffering) 14440c582449SJiri Olsa switch_output_size_warn(rec); 14450c582449SJiri Olsa 1446dc0c6127SJiri Olsa return 0; 1447cb4e1ebbSJiri Olsa } 1448cb4e1ebbSJiri Olsa 1449e5b2c207SNamhyung Kim static const char * const __record_usage[] = { 145086470930SIngo Molnar "perf record [<options>] [<command>]", 145186470930SIngo Molnar "perf record [<options>] -- <command> [<options>]", 145286470930SIngo Molnar NULL 145386470930SIngo Molnar }; 1454e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage; 145586470930SIngo Molnar 1456d20deb64SArnaldo Carvalho de Melo /* 14578c6f45a7SArnaldo Carvalho de Melo * XXX Ideally would be local to cmd_record() and passed to a record__new 14588c6f45a7SArnaldo Carvalho de Melo * because we need to have access to it in record__exit, that is called 1459d20deb64SArnaldo Carvalho de Melo * after cmd_record() exits, but since record_options need to be accessible to 1460d20deb64SArnaldo Carvalho de Melo * builtin-script, leave it here. 1461d20deb64SArnaldo Carvalho de Melo * 1462d20deb64SArnaldo Carvalho de Melo * At least we don't ouch it in all the other functions here directly. 1463d20deb64SArnaldo Carvalho de Melo * 1464d20deb64SArnaldo Carvalho de Melo * Just say no to tons of global variables, sigh. 1465d20deb64SArnaldo Carvalho de Melo */ 14668c6f45a7SArnaldo Carvalho de Melo static struct record record = { 1467d20deb64SArnaldo Carvalho de Melo .opts = { 14688affc2b8SAndi Kleen .sample_time = true, 1469d20deb64SArnaldo Carvalho de Melo .mmap_pages = UINT_MAX, 1470d20deb64SArnaldo Carvalho de Melo .user_freq = UINT_MAX, 1471d20deb64SArnaldo Carvalho de Melo .user_interval = ULLONG_MAX, 1472447a6013SArnaldo Carvalho de Melo .freq = 4000, 1473d1cb9fceSNamhyung Kim .target = { 1474d1cb9fceSNamhyung Kim .uses_mmap = true, 14753aa5939dSAdrian Hunter .default_per_cpu = true, 1476d1cb9fceSNamhyung Kim }, 14779d9cad76SKan Liang .proc_map_timeout = 500, 1478d20deb64SArnaldo Carvalho de Melo }, 1479e3d59112SNamhyung Kim .tool = { 1480e3d59112SNamhyung Kim .sample = process_sample_event, 1481e3d59112SNamhyung Kim .fork = perf_event__process_fork, 1482cca8482cSAdrian Hunter .exit = perf_event__process_exit, 1483e3d59112SNamhyung Kim .comm = perf_event__process_comm, 1484f3b3614aSHari Bathini .namespaces = perf_event__process_namespaces, 1485e3d59112SNamhyung Kim .mmap = perf_event__process_mmap, 1486e3d59112SNamhyung Kim .mmap2 = perf_event__process_mmap2, 1487cca8482cSAdrian Hunter .ordered_events = true, 1488e3d59112SNamhyung Kim }, 1489d20deb64SArnaldo Carvalho de Melo }; 14907865e817SFrederic Weisbecker 149176a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP 149276a26549SNamhyung Kim "\n\t\t\t\tDefault: fp"; 149361eaa3beSArnaldo Carvalho de Melo 14940aab2136SWang Nan static bool dry_run; 14950aab2136SWang Nan 1496d20deb64SArnaldo Carvalho de Melo /* 1497d20deb64SArnaldo Carvalho de Melo * XXX Will stay a global variable till we fix builtin-script.c to stop messing 1498d20deb64SArnaldo Carvalho de Melo * with it and switch to use the library functions in perf_evlist that came 1499b4006796SArnaldo Carvalho de Melo * from builtin-record.c, i.e. use record_opts, 1500d20deb64SArnaldo Carvalho de Melo * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record', 1501d20deb64SArnaldo Carvalho de Melo * using pipes, etc. 1502d20deb64SArnaldo Carvalho de Melo */ 1503efd21307SJiri Olsa static struct option __record_options[] = { 1504d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('e', "event", &record.evlist, "event", 150586470930SIngo Molnar "event selector. use 'perf list' to list available events", 1506f120f9d5SJiri Olsa parse_events_option), 1507d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK(0, "filter", &record.evlist, "filter", 1508c171b552SLi Zefan "event filter", parse_filter), 15094ba1faa1SWang Nan OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist, 15104ba1faa1SWang Nan NULL, "don't record events from perf itself", 15114ba1faa1SWang Nan exclude_perf), 1512bea03405SNamhyung Kim OPT_STRING('p', "pid", &record.opts.target.pid, "pid", 1513d6d901c2SZhang, Yanmin "record events on existing process id"), 1514bea03405SNamhyung Kim OPT_STRING('t', "tid", &record.opts.target.tid, "tid", 1515d6d901c2SZhang, Yanmin "record events on existing thread id"), 1516d20deb64SArnaldo Carvalho de Melo OPT_INTEGER('r', "realtime", &record.realtime_prio, 151786470930SIngo Molnar "collect data with this RT SCHED_FIFO priority"), 1518509051eaSArnaldo Carvalho de Melo OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering, 1519acac03faSKirill Smelkov "collect data without buffering"), 1520d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples, 1521daac07b2SFrederic Weisbecker "collect raw sample records from all opened counters"), 1522bea03405SNamhyung Kim OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide, 152386470930SIngo Molnar "system-wide collection from all CPUs"), 1524bea03405SNamhyung Kim OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu", 1525c45c6ea2SStephane Eranian "list of cpus to monitor"), 1526d20deb64SArnaldo Carvalho de Melo OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"), 1527eae8ad80SJiri Olsa OPT_STRING('o', "output", &record.data.file.path, "file", 152886470930SIngo Molnar "output file name"), 152969e7e5b0SAdrian Hunter OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit, 153069e7e5b0SAdrian Hunter &record.opts.no_inherit_set, 15312e6cdf99SStephane Eranian "child tasks do not inherit counters"), 15324ea648aeSWang Nan OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize, 15334ea648aeSWang Nan "synthesize non-sample events at the end of output"), 1534626a6b78SWang Nan OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"), 1535d20deb64SArnaldo Carvalho de Melo OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"), 1536e9db1310SAdrian Hunter OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]", 1537e9db1310SAdrian Hunter "number of mmap data pages and AUX area tracing mmap pages", 1538e9db1310SAdrian Hunter record__parse_mmap_pages), 1539d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN(0, "group", &record.opts.group, 154043bece79SLin Ming "put the counters into a counter group"), 15412ddd5c04SArnaldo Carvalho de Melo OPT_CALLBACK_NOOPT('g', NULL, &callchain_param, 154209b0fd45SJiri Olsa NULL, "enables call-graph recording" , 154309b0fd45SJiri Olsa &record_callchain_opt), 154409b0fd45SJiri Olsa OPT_CALLBACK(0, "call-graph", &record.opts, 154576a26549SNamhyung Kim "record_mode[,record_size]", record_callchain_help, 154609b0fd45SJiri Olsa &record_parse_callchain_opt), 1547c0555642SIan Munsie OPT_INCR('v', "verbose", &verbose, 15483da297a6SIngo Molnar "be more verbose (show counter open errors, etc)"), 1549b44308f5SArnaldo Carvalho de Melo OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"), 1550d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat, 1551649c48a9SPeter Zijlstra "per thread counts"), 155256100321SPeter Zijlstra OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"), 15533b0a5daaSKan Liang OPT_BOOLEAN(0, "phys-data", &record.opts.sample_phys_addr, 15543b0a5daaSKan Liang "Record the sample physical addresses"), 1555b6f35ed7SJiri Olsa OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"), 15563abebc55SAdrian Hunter OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time, 15573abebc55SAdrian Hunter &record.opts.sample_time_set, 15583abebc55SAdrian Hunter "Record the sample timestamps"), 155956100321SPeter Zijlstra OPT_BOOLEAN('P', "period", &record.opts.period, "Record the sample period"), 1560d20deb64SArnaldo Carvalho de Melo OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples, 1561649c48a9SPeter Zijlstra "don't sample"), 1562d2db9a98SWang Nan OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache, 1563d2db9a98SWang Nan &record.no_buildid_cache_set, 1564a1ac1d3cSStephane Eranian "do not update the buildid cache"), 1565d2db9a98SWang Nan OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid, 1566d2db9a98SWang Nan &record.no_buildid_set, 1567baa2f6ceSArnaldo Carvalho de Melo "do not collect buildids in perf.data"), 1568d20deb64SArnaldo Carvalho de Melo OPT_CALLBACK('G', "cgroup", &record.evlist, "name", 1569023695d9SStephane Eranian "monitor event in cgroup name only", 1570023695d9SStephane Eranian parse_cgroups), 1571a6205a35SArnaldo Carvalho de Melo OPT_UINTEGER('D', "delay", &record.opts.initial_delay, 15726619a53eSAndi Kleen "ms to wait before starting measurement after program start"), 1573bea03405SNamhyung Kim OPT_STRING('u', "uid", &record.opts.target.uid_str, "user", 1574bea03405SNamhyung Kim "user to profile"), 1575a5aabdacSStephane Eranian 1576a5aabdacSStephane Eranian OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack, 1577a5aabdacSStephane Eranian "branch any", "sample any taken branches", 1578a5aabdacSStephane Eranian parse_branch_stack), 1579a5aabdacSStephane Eranian 1580a5aabdacSStephane Eranian OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack, 1581a5aabdacSStephane Eranian "branch filter mask", "branch stack filter modes", 1582bdfebd84SRoberto Agostino Vitillo parse_branch_stack), 158305484298SAndi Kleen OPT_BOOLEAN('W', "weight", &record.opts.sample_weight, 158405484298SAndi Kleen "sample by weight (on special events only)"), 1585475eeab9SAndi Kleen OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction, 1586475eeab9SAndi Kleen "sample transaction flags (special events only)"), 15873aa5939dSAdrian Hunter OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread, 15883aa5939dSAdrian Hunter "use per-thread mmaps"), 1589bcc84ec6SStephane Eranian OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register", 1590bcc84ec6SStephane Eranian "sample selected machine registers on interrupt," 1591bcc84ec6SStephane Eranian " use -I ? to list register names", parse_regs), 159284c41742SAndi Kleen OPT_CALLBACK_OPTARG(0, "user-regs", &record.opts.sample_user_regs, NULL, "any register", 159384c41742SAndi Kleen "sample selected machine registers on interrupt," 159484c41742SAndi Kleen " use -I ? to list register names", parse_regs), 159585c273d2SAndi Kleen OPT_BOOLEAN(0, "running-time", &record.opts.running_time, 159685c273d2SAndi Kleen "Record running/enabled time of read (:S) events"), 1597814c8c38SPeter Zijlstra OPT_CALLBACK('k', "clockid", &record.opts, 1598814c8c38SPeter Zijlstra "clockid", "clockid to use for events, see clock_gettime()", 1599814c8c38SPeter Zijlstra parse_clockid), 16002dd6d8a1SAdrian Hunter OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts, 16012dd6d8a1SAdrian Hunter "opts", "AUX area tracing Snapshot Mode", ""), 16029d9cad76SKan Liang OPT_UINTEGER(0, "proc-map-timeout", &record.opts.proc_map_timeout, 16039d9cad76SKan Liang "per thread proc mmap processing timeout in ms"), 1604f3b3614aSHari Bathini OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces, 1605f3b3614aSHari Bathini "Record namespaces events"), 1606b757bb09SAdrian Hunter OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events, 1607b757bb09SAdrian Hunter "Record context switch events"), 160885723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel, 160985723885SJiri Olsa "Configure all used events to run in kernel space.", 161085723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 161185723885SJiri Olsa OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user, 161285723885SJiri Olsa "Configure all used events to run in user space.", 161385723885SJiri Olsa PARSE_OPT_EXCLUSIVE), 161471dc2326SWang Nan OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path", 161571dc2326SWang Nan "clang binary to use for compiling BPF scriptlets"), 161671dc2326SWang Nan OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options", 161771dc2326SWang Nan "options passed to clang when compiling BPF scriptlets"), 16187efe0e03SHe Kuang OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name, 16197efe0e03SHe Kuang "file", "vmlinux pathname"), 16206156681bSNamhyung Kim OPT_BOOLEAN(0, "buildid-all", &record.buildid_all, 16216156681bSNamhyung Kim "Record build-id of all DSOs regardless of hits"), 1622ecfd7a9cSWang Nan OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename, 1623ecfd7a9cSWang Nan "append timestamp to output filename"), 1624cb4e1ebbSJiri Olsa OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str, 1625bfacbe3bSJiri Olsa &record.switch_output.set, "signal,size,time", 1626bfacbe3bSJiri Olsa "Switch output when receive SIGUSR2 or cross size,time threshold", 1627dc0c6127SJiri Olsa "signal"), 16280aab2136SWang Nan OPT_BOOLEAN(0, "dry-run", &dry_run, 16290aab2136SWang Nan "Parse options then exit"), 163086470930SIngo Molnar OPT_END() 163186470930SIngo Molnar }; 163286470930SIngo Molnar 1633e5b2c207SNamhyung Kim struct option *record_options = __record_options; 1634e5b2c207SNamhyung Kim 1635b0ad8ea6SArnaldo Carvalho de Melo int cmd_record(int argc, const char **argv) 163686470930SIngo Molnar { 1637ef149c25SAdrian Hunter int err; 16388c6f45a7SArnaldo Carvalho de Melo struct record *rec = &record; 163916ad2ffbSNamhyung Kim char errbuf[BUFSIZ]; 164086470930SIngo Molnar 164148e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT 164248e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c) 164348e1cab1SWang Nan set_nobuild('\0', "clang-path", true); 164448e1cab1SWang Nan set_nobuild('\0', "clang-opt", true); 164548e1cab1SWang Nan # undef set_nobuild 164648e1cab1SWang Nan #endif 164748e1cab1SWang Nan 16487efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE 16497efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT) 16507efe0e03SHe Kuang # define REASON "NO_DWARF=1" 16517efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT) 16527efe0e03SHe Kuang # define REASON "NO_LIBBPF=1" 16537efe0e03SHe Kuang # else 16547efe0e03SHe Kuang # define REASON "this architecture doesn't support BPF prologue" 16557efe0e03SHe Kuang # endif 16567efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c) 16577efe0e03SHe Kuang set_nobuild('\0', "vmlinux", true); 16587efe0e03SHe Kuang # undef set_nobuild 16597efe0e03SHe Kuang # undef REASON 16607efe0e03SHe Kuang #endif 16617efe0e03SHe Kuang 16623e2be2daSArnaldo Carvalho de Melo rec->evlist = perf_evlist__new(); 16633e2be2daSArnaldo Carvalho de Melo if (rec->evlist == NULL) 1664361c99a6SArnaldo Carvalho de Melo return -ENOMEM; 1665361c99a6SArnaldo Carvalho de Melo 1666ecc4c561SArnaldo Carvalho de Melo err = perf_config(perf_record_config, rec); 1667ecc4c561SArnaldo Carvalho de Melo if (err) 1668ecc4c561SArnaldo Carvalho de Melo return err; 1669eb853e80SJiri Olsa 1670bca647aaSTom Zanussi argc = parse_options(argc, argv, record_options, record_usage, 1671a0541234SAnton Blanchard PARSE_OPT_STOP_AT_NON_OPTION); 167268ba3235SNamhyung Kim if (quiet) 167368ba3235SNamhyung Kim perf_quiet_option(); 1674483635a9SJiri Olsa 1675483635a9SJiri Olsa /* Make system wide (-a) the default target. */ 1676602ad878SArnaldo Carvalho de Melo if (!argc && target__none(&rec->opts.target)) 1677483635a9SJiri Olsa rec->opts.target.system_wide = true; 167886470930SIngo Molnar 1679bea03405SNamhyung Kim if (nr_cgroups && !rec->opts.target.system_wide) { 1680c7118369SNamhyung Kim usage_with_options_msg(record_usage, record_options, 1681c7118369SNamhyung Kim "cgroup monitoring only available in system-wide mode"); 1682c7118369SNamhyung Kim 1683023695d9SStephane Eranian } 1684b757bb09SAdrian Hunter if (rec->opts.record_switch_events && 1685b757bb09SAdrian Hunter !perf_can_record_switch_events()) { 1686c7118369SNamhyung Kim ui__error("kernel does not support recording context switch events\n"); 1687c7118369SNamhyung Kim parse_options_usage(record_usage, record_options, "switch-events", 0); 1688c7118369SNamhyung Kim return -EINVAL; 1689b757bb09SAdrian Hunter } 1690023695d9SStephane Eranian 1691cb4e1ebbSJiri Olsa if (switch_output_setup(rec)) { 1692cb4e1ebbSJiri Olsa parse_options_usage(record_usage, record_options, "switch-output", 0); 1693cb4e1ebbSJiri Olsa return -EINVAL; 1694cb4e1ebbSJiri Olsa } 1695cb4e1ebbSJiri Olsa 1696bfacbe3bSJiri Olsa if (rec->switch_output.time) { 1697bfacbe3bSJiri Olsa signal(SIGALRM, alarm_sig_handler); 1698bfacbe3bSJiri Olsa alarm(rec->switch_output.time); 1699bfacbe3bSJiri Olsa } 1700bfacbe3bSJiri Olsa 1701ef149c25SAdrian Hunter if (!rec->itr) { 1702ef149c25SAdrian Hunter rec->itr = auxtrace_record__init(rec->evlist, &err); 1703ef149c25SAdrian Hunter if (err) 17045c01ad60SAdrian Hunter goto out; 1705ef149c25SAdrian Hunter } 1706ef149c25SAdrian Hunter 17072dd6d8a1SAdrian Hunter err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts, 17082dd6d8a1SAdrian Hunter rec->opts.auxtrace_snapshot_opts); 17092dd6d8a1SAdrian Hunter if (err) 17105c01ad60SAdrian Hunter goto out; 17112dd6d8a1SAdrian Hunter 17121b36c03eSAdrian Hunter /* 17131b36c03eSAdrian Hunter * Allow aliases to facilitate the lookup of symbols for address 17141b36c03eSAdrian Hunter * filters. Refer to auxtrace_parse_filters(). 17151b36c03eSAdrian Hunter */ 17161b36c03eSAdrian Hunter symbol_conf.allow_aliases = true; 17171b36c03eSAdrian Hunter 17181b36c03eSAdrian Hunter symbol__init(NULL); 17191b36c03eSAdrian Hunter 17201b36c03eSAdrian Hunter err = auxtrace_parse_filters(rec->evlist); 17211b36c03eSAdrian Hunter if (err) 17221b36c03eSAdrian Hunter goto out; 17231b36c03eSAdrian Hunter 17240aab2136SWang Nan if (dry_run) 17255c01ad60SAdrian Hunter goto out; 17260aab2136SWang Nan 1727d7888573SWang Nan err = bpf__setup_stdout(rec->evlist); 1728d7888573SWang Nan if (err) { 1729d7888573SWang Nan bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf)); 1730d7888573SWang Nan pr_err("ERROR: Setup BPF stdout failed: %s\n", 1731d7888573SWang Nan errbuf); 17325c01ad60SAdrian Hunter goto out; 1733d7888573SWang Nan } 1734d7888573SWang Nan 1735ef149c25SAdrian Hunter err = -ENOMEM; 1736ef149c25SAdrian Hunter 17376c443954SArnaldo Carvalho de Melo if (symbol_conf.kptr_restrict && !perf_evlist__exclude_kernel(rec->evlist)) 1738646aaea6SArnaldo Carvalho de Melo pr_warning( 1739646aaea6SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n" 1740ec80fde7SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict.\n\n" 1741646aaea6SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n" 1742646aaea6SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n" 1743646aaea6SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n" 1744646aaea6SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n" 1745646aaea6SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n"); 1746ec80fde7SArnaldo Carvalho de Melo 17470c1d46a8SWang Nan if (rec->no_buildid_cache || rec->no_buildid) { 1748a1ac1d3cSStephane Eranian disable_buildid_cache(); 1749dc0c6127SJiri Olsa } else if (rec->switch_output.enabled) { 17500c1d46a8SWang Nan /* 17510c1d46a8SWang Nan * In 'perf record --switch-output', disable buildid 17520c1d46a8SWang Nan * generation by default to reduce data file switching 17530c1d46a8SWang Nan * overhead. Still generate buildid if they are required 17540c1d46a8SWang Nan * explicitly using 17550c1d46a8SWang Nan * 175660437ac0SJiri Olsa * perf record --switch-output --no-no-buildid \ 17570c1d46a8SWang Nan * --no-no-buildid-cache 17580c1d46a8SWang Nan * 17590c1d46a8SWang Nan * Following code equals to: 17600c1d46a8SWang Nan * 17610c1d46a8SWang Nan * if ((rec->no_buildid || !rec->no_buildid_set) && 17620c1d46a8SWang Nan * (rec->no_buildid_cache || !rec->no_buildid_cache_set)) 17630c1d46a8SWang Nan * disable_buildid_cache(); 17640c1d46a8SWang Nan */ 17650c1d46a8SWang Nan bool disable = true; 17660c1d46a8SWang Nan 17670c1d46a8SWang Nan if (rec->no_buildid_set && !rec->no_buildid) 17680c1d46a8SWang Nan disable = false; 17690c1d46a8SWang Nan if (rec->no_buildid_cache_set && !rec->no_buildid_cache) 17700c1d46a8SWang Nan disable = false; 17710c1d46a8SWang Nan if (disable) { 17720c1d46a8SWang Nan rec->no_buildid = true; 17730c1d46a8SWang Nan rec->no_buildid_cache = true; 17740c1d46a8SWang Nan disable_buildid_cache(); 17750c1d46a8SWang Nan } 17760c1d46a8SWang Nan } 1777655000e7SArnaldo Carvalho de Melo 17784ea648aeSWang Nan if (record.opts.overwrite) 17794ea648aeSWang Nan record.opts.tail_synthesize = true; 17804ea648aeSWang Nan 17813e2be2daSArnaldo Carvalho de Melo if (rec->evlist->nr_entries == 0 && 17824b4cd503SArnaldo Carvalho de Melo __perf_evlist__add_default(rec->evlist, !record.opts.no_samples) < 0) { 178369aad6f1SArnaldo Carvalho de Melo pr_err("Not enough memory for event selector list\n"); 1784394c01edSAdrian Hunter goto out; 1785bbd36e5eSPeter Zijlstra } 178686470930SIngo Molnar 178769e7e5b0SAdrian Hunter if (rec->opts.target.tid && !rec->opts.no_inherit_set) 178869e7e5b0SAdrian Hunter rec->opts.no_inherit = true; 178969e7e5b0SAdrian Hunter 1790602ad878SArnaldo Carvalho de Melo err = target__validate(&rec->opts.target); 179116ad2ffbSNamhyung Kim if (err) { 1792602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 179316ad2ffbSNamhyung Kim ui__warning("%s", errbuf); 179416ad2ffbSNamhyung Kim } 17954bd0f2d2SNamhyung Kim 1796602ad878SArnaldo Carvalho de Melo err = target__parse_uid(&rec->opts.target); 179716ad2ffbSNamhyung Kim if (err) { 179816ad2ffbSNamhyung Kim int saved_errno = errno; 179916ad2ffbSNamhyung Kim 1800602ad878SArnaldo Carvalho de Melo target__strerror(&rec->opts.target, err, errbuf, BUFSIZ); 18013780f488SNamhyung Kim ui__error("%s", errbuf); 180216ad2ffbSNamhyung Kim 180316ad2ffbSNamhyung Kim err = -saved_errno; 1804394c01edSAdrian Hunter goto out; 180516ad2ffbSNamhyung Kim } 18060d37aa34SArnaldo Carvalho de Melo 1807*ca800068SMengting Zhang /* Enable ignoring missing threads when -u/-p option is defined. */ 1808*ca800068SMengting Zhang rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX || rec->opts.target.pid; 180923dc4f15SJiri Olsa 181016ad2ffbSNamhyung Kim err = -ENOMEM; 18113e2be2daSArnaldo Carvalho de Melo if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0) 1812dd7927f4SArnaldo Carvalho de Melo usage_with_options(record_usage, record_options); 181369aad6f1SArnaldo Carvalho de Melo 1814ef149c25SAdrian Hunter err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts); 1815ef149c25SAdrian Hunter if (err) 1816394c01edSAdrian Hunter goto out; 1817ef149c25SAdrian Hunter 18186156681bSNamhyung Kim /* 18196156681bSNamhyung Kim * We take all buildids when the file contains 18206156681bSNamhyung Kim * AUX area tracing data because we do not decode the 18216156681bSNamhyung Kim * trace because it would take too long. 18226156681bSNamhyung Kim */ 18236156681bSNamhyung Kim if (rec->opts.full_auxtrace) 18246156681bSNamhyung Kim rec->buildid_all = true; 18256156681bSNamhyung Kim 1826b4006796SArnaldo Carvalho de Melo if (record_opts__config(&rec->opts)) { 182739d17dacSArnaldo Carvalho de Melo err = -EINVAL; 1828394c01edSAdrian Hunter goto out; 18297e4ff9e3SMike Galbraith } 18307e4ff9e3SMike Galbraith 1831d20deb64SArnaldo Carvalho de Melo err = __cmd_record(&record, argc, argv); 1832394c01edSAdrian Hunter out: 183345604710SNamhyung Kim perf_evlist__delete(rec->evlist); 1834d65a458bSArnaldo Carvalho de Melo symbol__exit(); 1835ef149c25SAdrian Hunter auxtrace_record__free(rec->itr); 183639d17dacSArnaldo Carvalho de Melo return err; 183786470930SIngo Molnar } 18382dd6d8a1SAdrian Hunter 18392dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused) 18402dd6d8a1SAdrian Hunter { 1841dc0c6127SJiri Olsa struct record *rec = &record; 1842dc0c6127SJiri Olsa 18435f9cf599SWang Nan if (trigger_is_ready(&auxtrace_snapshot_trigger)) { 18445f9cf599SWang Nan trigger_hit(&auxtrace_snapshot_trigger); 18452dd6d8a1SAdrian Hunter auxtrace_record__snapshot_started = 1; 18465f9cf599SWang Nan if (auxtrace_record__snapshot_start(record.itr)) 18475f9cf599SWang Nan trigger_error(&auxtrace_snapshot_trigger); 18485f9cf599SWang Nan } 18493c1cb7e3SWang Nan 1850dc0c6127SJiri Olsa if (switch_output_signal(rec)) 18513c1cb7e3SWang Nan trigger_hit(&switch_output_trigger); 18522dd6d8a1SAdrian Hunter } 1853bfacbe3bSJiri Olsa 1854bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused) 1855bfacbe3bSJiri Olsa { 1856bfacbe3bSJiri Olsa struct record *rec = &record; 1857bfacbe3bSJiri Olsa 1858bfacbe3bSJiri Olsa if (switch_output_time(rec)) 1859bfacbe3bSJiri Olsa trigger_hit(&switch_output_trigger); 1860bfacbe3bSJiri Olsa } 1861