builtin-record.c 39 KB

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