xref: /openbmc/linux/tools/perf/builtin-record.c (revision e907caf3a07ee42ef08ba689a436fd1eb99fbf62)
186470930SIngo Molnar /*
286470930SIngo Molnar  * builtin-record.c
386470930SIngo Molnar  *
486470930SIngo Molnar  * Builtin record command: Record the profile of a workload
586470930SIngo Molnar  * (or a CPU, or a PID) into the perf.data output file - for
686470930SIngo Molnar  * later analysis via perf report.
786470930SIngo Molnar  */
886470930SIngo Molnar #include "builtin.h"
986470930SIngo Molnar 
1086470930SIngo Molnar #include "perf.h"
1186470930SIngo Molnar 
126122e4e4SArnaldo Carvalho de Melo #include "util/build-id.h"
1386470930SIngo Molnar #include "util/util.h"
144b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1586470930SIngo Molnar #include "util/parse-events.h"
1641840d21STaeung Song #include "util/config.h"
1786470930SIngo Molnar 
188f651eaeSArnaldo Carvalho de Melo #include "util/callchain.h"
19f14d5707SArnaldo Carvalho de Melo #include "util/cgroup.h"
207c6a1c65SPeter Zijlstra #include "util/header.h"
2166e274f3SFrederic Weisbecker #include "util/event.h"
22361c99a6SArnaldo Carvalho de Melo #include "util/evlist.h"
2369aad6f1SArnaldo Carvalho de Melo #include "util/evsel.h"
248f28827aSFrederic Weisbecker #include "util/debug.h"
255d8bb1ecSMathieu Poirier #include "util/drv_configs.h"
2694c744b6SArnaldo Carvalho de Melo #include "util/session.h"
2745694aa7SArnaldo Carvalho de Melo #include "util/tool.h"
288d06367fSArnaldo Carvalho de Melo #include "util/symbol.h"
29a12b51c4SPaul Mackerras #include "util/cpumap.h"
30fd78260bSArnaldo Carvalho de Melo #include "util/thread_map.h"
31f5fc1412SJiri Olsa #include "util/data.h"
32bcc84ec6SStephane Eranian #include "util/perf_regs.h"
33ef149c25SAdrian Hunter #include "util/auxtrace.h"
3446bc29b9SAdrian Hunter #include "util/tsc.h"
35f00898f4SAndi Kleen #include "util/parse-branch-options.h"
36bcc84ec6SStephane Eranian #include "util/parse-regs-options.h"
3771dc2326SWang Nan #include "util/llvm-utils.h"
388690a2a7SWang Nan #include "util/bpf-loader.h"
395f9cf599SWang Nan #include "util/trigger.h"
40a074865eSWang Nan #include "util/perf-hooks.h"
41d8871ea7SWang Nan #include "asm/bug.h"
427c6a1c65SPeter Zijlstra 
4386470930SIngo Molnar #include <unistd.h>
4486470930SIngo Molnar #include <sched.h>
45a41794cdSArnaldo Carvalho de Melo #include <sys/mman.h>
462d11c650SWang Nan #include <asm/bug.h>
470693e680SArnaldo Carvalho de Melo #include <linux/time64.h>
4878da39faSBernhard Rosenkraenzer 
491b43b704SJiri Olsa struct switch_output {
50dc0c6127SJiri Olsa 	bool		 enabled;
511b43b704SJiri Olsa 	bool		 signal;
52dc0c6127SJiri Olsa 	unsigned long	 size;
53bfacbe3bSJiri Olsa 	unsigned long	 time;
54cb4e1ebbSJiri Olsa 	const char	*str;
55cb4e1ebbSJiri Olsa 	bool		 set;
561b43b704SJiri Olsa };
571b43b704SJiri Olsa 
588c6f45a7SArnaldo Carvalho de Melo struct record {
5945694aa7SArnaldo Carvalho de Melo 	struct perf_tool	tool;
60b4006796SArnaldo Carvalho de Melo 	struct record_opts	opts;
61d20deb64SArnaldo Carvalho de Melo 	u64			bytes_written;
62f5fc1412SJiri Olsa 	struct perf_data_file	file;
63ef149c25SAdrian Hunter 	struct auxtrace_record	*itr;
64d20deb64SArnaldo Carvalho de Melo 	struct perf_evlist	*evlist;
65d20deb64SArnaldo Carvalho de Melo 	struct perf_session	*session;
66d20deb64SArnaldo Carvalho de Melo 	const char		*progname;
67d20deb64SArnaldo Carvalho de Melo 	int			realtime_prio;
68d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid;
69d2db9a98SWang Nan 	bool			no_buildid_set;
70d20deb64SArnaldo Carvalho de Melo 	bool			no_buildid_cache;
71d2db9a98SWang Nan 	bool			no_buildid_cache_set;
726156681bSNamhyung Kim 	bool			buildid_all;
73ecfd7a9cSWang Nan 	bool			timestamp_filename;
741b43b704SJiri Olsa 	struct switch_output	switch_output;
759f065194SYang Shi 	unsigned long long	samples;
760f82ebc4SArnaldo Carvalho de Melo };
7786470930SIngo Molnar 
78dc0c6127SJiri Olsa static volatile int auxtrace_record__snapshot_started;
79dc0c6127SJiri Olsa static DEFINE_TRIGGER(auxtrace_snapshot_trigger);
80dc0c6127SJiri Olsa static DEFINE_TRIGGER(switch_output_trigger);
81dc0c6127SJiri Olsa 
82dc0c6127SJiri Olsa static bool switch_output_signal(struct record *rec)
83dc0c6127SJiri Olsa {
84dc0c6127SJiri Olsa 	return rec->switch_output.signal &&
85dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
86dc0c6127SJiri Olsa }
87dc0c6127SJiri Olsa 
88dc0c6127SJiri Olsa static bool switch_output_size(struct record *rec)
89dc0c6127SJiri Olsa {
90dc0c6127SJiri Olsa 	return rec->switch_output.size &&
91dc0c6127SJiri Olsa 	       trigger_is_ready(&switch_output_trigger) &&
92dc0c6127SJiri Olsa 	       (rec->bytes_written >= rec->switch_output.size);
93dc0c6127SJiri Olsa }
94dc0c6127SJiri Olsa 
95bfacbe3bSJiri Olsa static bool switch_output_time(struct record *rec)
96bfacbe3bSJiri Olsa {
97bfacbe3bSJiri Olsa 	return rec->switch_output.time &&
98bfacbe3bSJiri Olsa 	       trigger_is_ready(&switch_output_trigger);
99bfacbe3bSJiri Olsa }
100bfacbe3bSJiri Olsa 
1018c6f45a7SArnaldo Carvalho de Melo static int record__write(struct record *rec, void *bf, size_t size)
102f5970550SPeter Zijlstra {
103cf8b2e69SArnaldo Carvalho de Melo 	if (perf_data_file__write(rec->session->file, bf, size) < 0) {
1044f624685SAdrian Hunter 		pr_err("failed to write perf data, error: %m\n");
1058d3eca20SDavid Ahern 		return -1;
1068d3eca20SDavid Ahern 	}
107f5970550SPeter Zijlstra 
108cf8b2e69SArnaldo Carvalho de Melo 	rec->bytes_written += size;
109dc0c6127SJiri Olsa 
110dc0c6127SJiri Olsa 	if (switch_output_size(rec))
111dc0c6127SJiri Olsa 		trigger_hit(&switch_output_trigger);
112dc0c6127SJiri Olsa 
1138d3eca20SDavid Ahern 	return 0;
114f5970550SPeter Zijlstra }
115f5970550SPeter Zijlstra 
11645694aa7SArnaldo Carvalho de Melo static int process_synthesized_event(struct perf_tool *tool,
117d20deb64SArnaldo Carvalho de Melo 				     union perf_event *event,
1181d037ca1SIrina Tirdea 				     struct perf_sample *sample __maybe_unused,
1191d037ca1SIrina Tirdea 				     struct machine *machine __maybe_unused)
120234fbbf5SArnaldo Carvalho de Melo {
1218c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = container_of(tool, struct record, tool);
1228c6f45a7SArnaldo Carvalho de Melo 	return record__write(rec, event, event->header.size);
123234fbbf5SArnaldo Carvalho de Melo }
124234fbbf5SArnaldo Carvalho de Melo 
1253a62a7b8SWang Nan static int
1263a62a7b8SWang Nan backward_rb_find_range(void *buf, int mask, u64 head, u64 *start, u64 *end)
1273a62a7b8SWang Nan {
1283a62a7b8SWang Nan 	struct perf_event_header *pheader;
1293a62a7b8SWang Nan 	u64 evt_head = head;
1303a62a7b8SWang Nan 	int size = mask + 1;
1313a62a7b8SWang Nan 
1323a62a7b8SWang Nan 	pr_debug2("backward_rb_find_range: buf=%p, head=%"PRIx64"\n", buf, head);
1333a62a7b8SWang Nan 	pheader = (struct perf_event_header *)(buf + (head & mask));
1343a62a7b8SWang Nan 	*start = head;
1353a62a7b8SWang Nan 	while (true) {
1363a62a7b8SWang Nan 		if (evt_head - head >= (unsigned int)size) {
1375e30d55cSColin Ian King 			pr_debug("Finished reading backward ring buffer: rewind\n");
1383a62a7b8SWang Nan 			if (evt_head - head > (unsigned int)size)
1393a62a7b8SWang Nan 				evt_head -= pheader->size;
1403a62a7b8SWang Nan 			*end = evt_head;
1413a62a7b8SWang Nan 			return 0;
1423a62a7b8SWang Nan 		}
1433a62a7b8SWang Nan 
1443a62a7b8SWang Nan 		pheader = (struct perf_event_header *)(buf + (evt_head & mask));
1453a62a7b8SWang Nan 
1463a62a7b8SWang Nan 		if (pheader->size == 0) {
1475e30d55cSColin Ian King 			pr_debug("Finished reading backward ring buffer: get start\n");
1483a62a7b8SWang Nan 			*end = evt_head;
1493a62a7b8SWang Nan 			return 0;
1503a62a7b8SWang Nan 		}
1513a62a7b8SWang Nan 
1523a62a7b8SWang Nan 		evt_head += pheader->size;
1533a62a7b8SWang Nan 		pr_debug3("move evt_head: %"PRIx64"\n", evt_head);
1543a62a7b8SWang Nan 	}
1553a62a7b8SWang Nan 	WARN_ONCE(1, "Shouldn't get here\n");
1563a62a7b8SWang Nan 	return -1;
1573a62a7b8SWang Nan }
1583a62a7b8SWang Nan 
1593a62a7b8SWang Nan static int
160a4ea0ec4SWang Nan rb_find_range(void *data, int mask, u64 head, u64 old,
161a4ea0ec4SWang Nan 	      u64 *start, u64 *end, bool backward)
1623a62a7b8SWang Nan {
163a4ea0ec4SWang Nan 	if (!backward) {
1643a62a7b8SWang Nan 		*start = old;
1653a62a7b8SWang Nan 		*end = head;
1663a62a7b8SWang Nan 		return 0;
1673a62a7b8SWang Nan 	}
1683a62a7b8SWang Nan 
1693a62a7b8SWang Nan 	return backward_rb_find_range(data, mask, head, start, end);
1703a62a7b8SWang Nan }
1713a62a7b8SWang Nan 
172a4ea0ec4SWang Nan static int
173a4ea0ec4SWang Nan record__mmap_read(struct record *rec, struct perf_mmap *md,
174a4ea0ec4SWang Nan 		  bool overwrite, bool backward)
17586470930SIngo Molnar {
1767b8283b5SDavid Ahern 	u64 head = perf_mmap__read_head(md);
1777b8283b5SDavid Ahern 	u64 old = md->prev;
17809fa4f40SWang Nan 	u64 end = head, start = old;
179918512b4SJiri Olsa 	unsigned char *data = md->base + page_size;
18086470930SIngo Molnar 	unsigned long size;
18186470930SIngo Molnar 	void *buf;
1828d3eca20SDavid Ahern 	int rc = 0;
18386470930SIngo Molnar 
184a4ea0ec4SWang Nan 	if (rb_find_range(data, md->mask, head,
185a4ea0ec4SWang Nan 			  old, &start, &end, backward))
1863a62a7b8SWang Nan 		return -1;
1873a62a7b8SWang Nan 
18809fa4f40SWang Nan 	if (start == end)
1898d3eca20SDavid Ahern 		return 0;
19086470930SIngo Molnar 
191d20deb64SArnaldo Carvalho de Melo 	rec->samples++;
19286470930SIngo Molnar 
19309fa4f40SWang Nan 	size = end - start;
1942d11c650SWang Nan 	if (size > (unsigned long)(md->mask) + 1) {
1952d11c650SWang Nan 		WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
1962d11c650SWang Nan 
1972d11c650SWang Nan 		md->prev = head;
198a4ea0ec4SWang Nan 		perf_mmap__consume(md, overwrite || backward);
1992d11c650SWang Nan 		return 0;
2002d11c650SWang Nan 	}
20186470930SIngo Molnar 
20209fa4f40SWang Nan 	if ((start & md->mask) + size != (end & md->mask)) {
20309fa4f40SWang Nan 		buf = &data[start & md->mask];
20409fa4f40SWang Nan 		size = md->mask + 1 - (start & md->mask);
20509fa4f40SWang Nan 		start += size;
20686470930SIngo Molnar 
2078c6f45a7SArnaldo Carvalho de Melo 		if (record__write(rec, buf, size) < 0) {
2088d3eca20SDavid Ahern 			rc = -1;
2098d3eca20SDavid Ahern 			goto out;
2108d3eca20SDavid Ahern 		}
21186470930SIngo Molnar 	}
21286470930SIngo Molnar 
21309fa4f40SWang Nan 	buf = &data[start & md->mask];
21409fa4f40SWang Nan 	size = end - start;
21509fa4f40SWang Nan 	start += size;
21686470930SIngo Molnar 
2178c6f45a7SArnaldo Carvalho de Melo 	if (record__write(rec, buf, size) < 0) {
2188d3eca20SDavid Ahern 		rc = -1;
2198d3eca20SDavid Ahern 		goto out;
2208d3eca20SDavid Ahern 	}
22186470930SIngo Molnar 
22209fa4f40SWang Nan 	md->prev = head;
223a4ea0ec4SWang Nan 	perf_mmap__consume(md, overwrite || backward);
2248d3eca20SDavid Ahern out:
2258d3eca20SDavid Ahern 	return rc;
22686470930SIngo Molnar }
22786470930SIngo Molnar 
2282dd6d8a1SAdrian Hunter static volatile int done;
2292dd6d8a1SAdrian Hunter static volatile int signr = -1;
2302dd6d8a1SAdrian Hunter static volatile int child_finished;
231c0bdc1c4SWang Nan 
2322dd6d8a1SAdrian Hunter static void sig_handler(int sig)
2332dd6d8a1SAdrian Hunter {
2342dd6d8a1SAdrian Hunter 	if (sig == SIGCHLD)
2352dd6d8a1SAdrian Hunter 		child_finished = 1;
2362dd6d8a1SAdrian Hunter 	else
2372dd6d8a1SAdrian Hunter 		signr = sig;
2382dd6d8a1SAdrian Hunter 
2392dd6d8a1SAdrian Hunter 	done = 1;
2402dd6d8a1SAdrian Hunter }
2412dd6d8a1SAdrian Hunter 
242a074865eSWang Nan static void sigsegv_handler(int sig)
243a074865eSWang Nan {
244a074865eSWang Nan 	perf_hooks__recover();
245a074865eSWang Nan 	sighandler_dump_stack(sig);
246a074865eSWang Nan }
247a074865eSWang Nan 
2482dd6d8a1SAdrian Hunter static void record__sig_exit(void)
2492dd6d8a1SAdrian Hunter {
2502dd6d8a1SAdrian Hunter 	if (signr == -1)
2512dd6d8a1SAdrian Hunter 		return;
2522dd6d8a1SAdrian Hunter 
2532dd6d8a1SAdrian Hunter 	signal(signr, SIG_DFL);
2542dd6d8a1SAdrian Hunter 	raise(signr);
2552dd6d8a1SAdrian Hunter }
2562dd6d8a1SAdrian Hunter 
257e31f0d01SAdrian Hunter #ifdef HAVE_AUXTRACE_SUPPORT
258e31f0d01SAdrian Hunter 
259ef149c25SAdrian Hunter static int record__process_auxtrace(struct perf_tool *tool,
260ef149c25SAdrian Hunter 				    union perf_event *event, void *data1,
261ef149c25SAdrian Hunter 				    size_t len1, void *data2, size_t len2)
262ef149c25SAdrian Hunter {
263ef149c25SAdrian Hunter 	struct record *rec = container_of(tool, struct record, tool);
26499fa2984SAdrian Hunter 	struct perf_data_file *file = &rec->file;
265ef149c25SAdrian Hunter 	size_t padding;
266ef149c25SAdrian Hunter 	u8 pad[8] = {0};
267ef149c25SAdrian Hunter 
26899fa2984SAdrian Hunter 	if (!perf_data_file__is_pipe(file)) {
26999fa2984SAdrian Hunter 		off_t file_offset;
27099fa2984SAdrian Hunter 		int fd = perf_data_file__fd(file);
27199fa2984SAdrian Hunter 		int err;
27299fa2984SAdrian Hunter 
27399fa2984SAdrian Hunter 		file_offset = lseek(fd, 0, SEEK_CUR);
27499fa2984SAdrian Hunter 		if (file_offset == -1)
27599fa2984SAdrian Hunter 			return -1;
27699fa2984SAdrian Hunter 		err = auxtrace_index__auxtrace_event(&rec->session->auxtrace_index,
27799fa2984SAdrian Hunter 						     event, file_offset);
27899fa2984SAdrian Hunter 		if (err)
27999fa2984SAdrian Hunter 			return err;
28099fa2984SAdrian Hunter 	}
28199fa2984SAdrian Hunter 
282ef149c25SAdrian Hunter 	/* event.auxtrace.size includes padding, see __auxtrace_mmap__read() */
283ef149c25SAdrian Hunter 	padding = (len1 + len2) & 7;
284ef149c25SAdrian Hunter 	if (padding)
285ef149c25SAdrian Hunter 		padding = 8 - padding;
286ef149c25SAdrian Hunter 
287ef149c25SAdrian Hunter 	record__write(rec, event, event->header.size);
288ef149c25SAdrian Hunter 	record__write(rec, data1, len1);
289ef149c25SAdrian Hunter 	if (len2)
290ef149c25SAdrian Hunter 		record__write(rec, data2, len2);
291ef149c25SAdrian Hunter 	record__write(rec, &pad, padding);
292ef149c25SAdrian Hunter 
293ef149c25SAdrian Hunter 	return 0;
294ef149c25SAdrian Hunter }
295ef149c25SAdrian Hunter 
296ef149c25SAdrian Hunter static int record__auxtrace_mmap_read(struct record *rec,
297ef149c25SAdrian Hunter 				      struct auxtrace_mmap *mm)
298ef149c25SAdrian Hunter {
299ef149c25SAdrian Hunter 	int ret;
300ef149c25SAdrian Hunter 
301ef149c25SAdrian Hunter 	ret = auxtrace_mmap__read(mm, rec->itr, &rec->tool,
302ef149c25SAdrian Hunter 				  record__process_auxtrace);
303ef149c25SAdrian Hunter 	if (ret < 0)
304ef149c25SAdrian Hunter 		return ret;
305ef149c25SAdrian Hunter 
306ef149c25SAdrian Hunter 	if (ret)
307ef149c25SAdrian Hunter 		rec->samples++;
308ef149c25SAdrian Hunter 
309ef149c25SAdrian Hunter 	return 0;
310ef149c25SAdrian Hunter }
311ef149c25SAdrian Hunter 
3122dd6d8a1SAdrian Hunter static int record__auxtrace_mmap_read_snapshot(struct record *rec,
3132dd6d8a1SAdrian Hunter 					       struct auxtrace_mmap *mm)
3142dd6d8a1SAdrian Hunter {
3152dd6d8a1SAdrian Hunter 	int ret;
3162dd6d8a1SAdrian Hunter 
3172dd6d8a1SAdrian Hunter 	ret = auxtrace_mmap__read_snapshot(mm, rec->itr, &rec->tool,
3182dd6d8a1SAdrian Hunter 					   record__process_auxtrace,
3192dd6d8a1SAdrian Hunter 					   rec->opts.auxtrace_snapshot_size);
3202dd6d8a1SAdrian Hunter 	if (ret < 0)
3212dd6d8a1SAdrian Hunter 		return ret;
3222dd6d8a1SAdrian Hunter 
3232dd6d8a1SAdrian Hunter 	if (ret)
3242dd6d8a1SAdrian Hunter 		rec->samples++;
3252dd6d8a1SAdrian Hunter 
3262dd6d8a1SAdrian Hunter 	return 0;
3272dd6d8a1SAdrian Hunter }
3282dd6d8a1SAdrian Hunter 
3292dd6d8a1SAdrian Hunter static int record__auxtrace_read_snapshot_all(struct record *rec)
3302dd6d8a1SAdrian Hunter {
3312dd6d8a1SAdrian Hunter 	int i;
3322dd6d8a1SAdrian Hunter 	int rc = 0;
3332dd6d8a1SAdrian Hunter 
3342dd6d8a1SAdrian Hunter 	for (i = 0; i < rec->evlist->nr_mmaps; i++) {
3352dd6d8a1SAdrian Hunter 		struct auxtrace_mmap *mm =
3362dd6d8a1SAdrian Hunter 				&rec->evlist->mmap[i].auxtrace_mmap;
3372dd6d8a1SAdrian Hunter 
3382dd6d8a1SAdrian Hunter 		if (!mm->base)
3392dd6d8a1SAdrian Hunter 			continue;
3402dd6d8a1SAdrian Hunter 
3412dd6d8a1SAdrian Hunter 		if (record__auxtrace_mmap_read_snapshot(rec, mm) != 0) {
3422dd6d8a1SAdrian Hunter 			rc = -1;
3432dd6d8a1SAdrian Hunter 			goto out;
3442dd6d8a1SAdrian Hunter 		}
3452dd6d8a1SAdrian Hunter 	}
3462dd6d8a1SAdrian Hunter out:
3472dd6d8a1SAdrian Hunter 	return rc;
3482dd6d8a1SAdrian Hunter }
3492dd6d8a1SAdrian Hunter 
3502dd6d8a1SAdrian Hunter static void record__read_auxtrace_snapshot(struct record *rec)
3512dd6d8a1SAdrian Hunter {
3522dd6d8a1SAdrian Hunter 	pr_debug("Recording AUX area tracing snapshot\n");
3532dd6d8a1SAdrian Hunter 	if (record__auxtrace_read_snapshot_all(rec) < 0) {
3545f9cf599SWang Nan 		trigger_error(&auxtrace_snapshot_trigger);
3552dd6d8a1SAdrian Hunter 	} else {
3565f9cf599SWang Nan 		if (auxtrace_record__snapshot_finish(rec->itr))
3575f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
3585f9cf599SWang Nan 		else
3595f9cf599SWang Nan 			trigger_ready(&auxtrace_snapshot_trigger);
3602dd6d8a1SAdrian Hunter 	}
3612dd6d8a1SAdrian Hunter }
3622dd6d8a1SAdrian Hunter 
363e31f0d01SAdrian Hunter #else
364e31f0d01SAdrian Hunter 
365e31f0d01SAdrian Hunter static inline
366e31f0d01SAdrian Hunter int record__auxtrace_mmap_read(struct record *rec __maybe_unused,
367e31f0d01SAdrian Hunter 			       struct auxtrace_mmap *mm __maybe_unused)
368e31f0d01SAdrian Hunter {
369e31f0d01SAdrian Hunter 	return 0;
370e31f0d01SAdrian Hunter }
371e31f0d01SAdrian Hunter 
3722dd6d8a1SAdrian Hunter static inline
3732dd6d8a1SAdrian Hunter void record__read_auxtrace_snapshot(struct record *rec __maybe_unused)
3742dd6d8a1SAdrian Hunter {
3752dd6d8a1SAdrian Hunter }
3762dd6d8a1SAdrian Hunter 
3772dd6d8a1SAdrian Hunter static inline
3782dd6d8a1SAdrian Hunter int auxtrace_record__snapshot_start(struct auxtrace_record *itr __maybe_unused)
3792dd6d8a1SAdrian Hunter {
3802dd6d8a1SAdrian Hunter 	return 0;
3812dd6d8a1SAdrian Hunter }
3822dd6d8a1SAdrian Hunter 
383e31f0d01SAdrian Hunter #endif
384e31f0d01SAdrian Hunter 
385cda57a8cSWang Nan static int record__mmap_evlist(struct record *rec,
386cda57a8cSWang Nan 			       struct perf_evlist *evlist)
387cda57a8cSWang Nan {
388cda57a8cSWang Nan 	struct record_opts *opts = &rec->opts;
389cda57a8cSWang Nan 	char msg[512];
390cda57a8cSWang Nan 
391cda57a8cSWang Nan 	if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false,
392cda57a8cSWang Nan 				 opts->auxtrace_mmap_pages,
393cda57a8cSWang Nan 				 opts->auxtrace_snapshot_mode) < 0) {
394cda57a8cSWang Nan 		if (errno == EPERM) {
395cda57a8cSWang Nan 			pr_err("Permission error mapping pages.\n"
396cda57a8cSWang Nan 			       "Consider increasing "
397cda57a8cSWang Nan 			       "/proc/sys/kernel/perf_event_mlock_kb,\n"
398cda57a8cSWang Nan 			       "or try again with a smaller value of -m/--mmap_pages.\n"
399cda57a8cSWang Nan 			       "(current value: %u,%u)\n",
400cda57a8cSWang Nan 			       opts->mmap_pages, opts->auxtrace_mmap_pages);
401cda57a8cSWang Nan 			return -errno;
402cda57a8cSWang Nan 		} else {
403cda57a8cSWang Nan 			pr_err("failed to mmap with %d (%s)\n", errno,
404c8b5f2c9SArnaldo Carvalho de Melo 				str_error_r(errno, msg, sizeof(msg)));
405cda57a8cSWang Nan 			if (errno)
406cda57a8cSWang Nan 				return -errno;
407cda57a8cSWang Nan 			else
408cda57a8cSWang Nan 				return -EINVAL;
409cda57a8cSWang Nan 		}
410cda57a8cSWang Nan 	}
411cda57a8cSWang Nan 	return 0;
412cda57a8cSWang Nan }
413cda57a8cSWang Nan 
414cda57a8cSWang Nan static int record__mmap(struct record *rec)
415cda57a8cSWang Nan {
416cda57a8cSWang Nan 	return record__mmap_evlist(rec, rec->evlist);
417cda57a8cSWang Nan }
418cda57a8cSWang Nan 
4198c6f45a7SArnaldo Carvalho de Melo static int record__open(struct record *rec)
420dd7927f4SArnaldo Carvalho de Melo {
421d6195a6aSArnaldo Carvalho de Melo 	char msg[BUFSIZ];
4226a4bb04cSJiri Olsa 	struct perf_evsel *pos;
423d20deb64SArnaldo Carvalho de Melo 	struct perf_evlist *evlist = rec->evlist;
424d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session = rec->session;
425b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
4265d8bb1ecSMathieu Poirier 	struct perf_evsel_config_term *err_term;
4278d3eca20SDavid Ahern 	int rc = 0;
428dd7927f4SArnaldo Carvalho de Melo 
429e68ae9cfSArnaldo Carvalho de Melo 	perf_evlist__config(evlist, opts, &callchain_param);
430cac21425SJiri Olsa 
431e5cadb93SArnaldo Carvalho de Melo 	evlist__for_each_entry(evlist, pos) {
4323da297a6SIngo Molnar try_again:
433d988d5eeSKan Liang 		if (perf_evsel__open(pos, pos->cpus, pos->threads) < 0) {
43456e52e85SArnaldo Carvalho de Melo 			if (perf_evsel__fallback(pos, errno, msg, sizeof(msg))) {
435bb963e16SNamhyung Kim 				if (verbose > 0)
436c0a54341SArnaldo Carvalho de Melo 					ui__warning("%s\n", msg);
4373da297a6SIngo Molnar 				goto try_again;
4383da297a6SIngo Molnar 			}
439ca6a4258SDavid Ahern 
44056e52e85SArnaldo Carvalho de Melo 			rc = -errno;
44156e52e85SArnaldo Carvalho de Melo 			perf_evsel__open_strerror(pos, &opts->target,
44256e52e85SArnaldo Carvalho de Melo 						  errno, msg, sizeof(msg));
44356e52e85SArnaldo Carvalho de Melo 			ui__error("%s\n", msg);
4448d3eca20SDavid Ahern 			goto out;
4457c6a1c65SPeter Zijlstra 		}
4467c6a1c65SPeter Zijlstra 	}
4477c6a1c65SPeter Zijlstra 
44823d4aad4SArnaldo Carvalho de Melo 	if (perf_evlist__apply_filters(evlist, &pos)) {
44923d4aad4SArnaldo Carvalho de Melo 		error("failed to set filter \"%s\" on event %s with %d (%s)\n",
45023d4aad4SArnaldo Carvalho de Melo 			pos->filter, perf_evsel__name(pos), errno,
451c8b5f2c9SArnaldo Carvalho de Melo 			str_error_r(errno, msg, sizeof(msg)));
4528d3eca20SDavid Ahern 		rc = -1;
4538d3eca20SDavid Ahern 		goto out;
4540a102479SFrederic Weisbecker 	}
4550a102479SFrederic Weisbecker 
4565d8bb1ecSMathieu Poirier 	if (perf_evlist__apply_drv_configs(evlist, &pos, &err_term)) {
4575d8bb1ecSMathieu Poirier 		error("failed to set config \"%s\" on event %s with %d (%s)\n",
4585d8bb1ecSMathieu Poirier 		      err_term->val.drv_cfg, perf_evsel__name(pos), errno,
4595d8bb1ecSMathieu Poirier 		      str_error_r(errno, msg, sizeof(msg)));
4605d8bb1ecSMathieu Poirier 		rc = -1;
4615d8bb1ecSMathieu Poirier 		goto out;
4625d8bb1ecSMathieu Poirier 	}
4635d8bb1ecSMathieu Poirier 
464cda57a8cSWang Nan 	rc = record__mmap(rec);
465cda57a8cSWang Nan 	if (rc)
4668d3eca20SDavid Ahern 		goto out;
4670a27d7f9SArnaldo Carvalho de Melo 
468a91e5431SArnaldo Carvalho de Melo 	session->evlist = evlist;
4697b56cce2SArnaldo Carvalho de Melo 	perf_session__set_id_hdr_size(session);
4708d3eca20SDavid Ahern out:
4718d3eca20SDavid Ahern 	return rc;
472a91e5431SArnaldo Carvalho de Melo }
473a91e5431SArnaldo Carvalho de Melo 
474e3d59112SNamhyung Kim static int process_sample_event(struct perf_tool *tool,
475e3d59112SNamhyung Kim 				union perf_event *event,
476e3d59112SNamhyung Kim 				struct perf_sample *sample,
477e3d59112SNamhyung Kim 				struct perf_evsel *evsel,
478e3d59112SNamhyung Kim 				struct machine *machine)
479e3d59112SNamhyung Kim {
480e3d59112SNamhyung Kim 	struct record *rec = container_of(tool, struct record, tool);
481e3d59112SNamhyung Kim 
482e3d59112SNamhyung Kim 	rec->samples++;
483e3d59112SNamhyung Kim 
484e3d59112SNamhyung Kim 	return build_id__mark_dso_hit(tool, event, sample, evsel, machine);
485e3d59112SNamhyung Kim }
486e3d59112SNamhyung Kim 
4878c6f45a7SArnaldo Carvalho de Melo static int process_buildids(struct record *rec)
4886122e4e4SArnaldo Carvalho de Melo {
489f5fc1412SJiri Olsa 	struct perf_data_file *file  = &rec->file;
490f5fc1412SJiri Olsa 	struct perf_session *session = rec->session;
4916122e4e4SArnaldo Carvalho de Melo 
492457ae94aSHe Kuang 	if (file->size == 0)
4939f591fd7SArnaldo Carvalho de Melo 		return 0;
4949f591fd7SArnaldo Carvalho de Melo 
49500dc8657SNamhyung Kim 	/*
49600dc8657SNamhyung Kim 	 * During this process, it'll load kernel map and replace the
49700dc8657SNamhyung Kim 	 * dso->long_name to a real pathname it found.  In this case
49800dc8657SNamhyung Kim 	 * we prefer the vmlinux path like
49900dc8657SNamhyung Kim 	 *   /lib/modules/3.16.4/build/vmlinux
50000dc8657SNamhyung Kim 	 *
50100dc8657SNamhyung Kim 	 * rather than build-id path (in debug directory).
50200dc8657SNamhyung Kim 	 *   $HOME/.debug/.build-id/f0/6e17aa50adf4d00b88925e03775de107611551
50300dc8657SNamhyung Kim 	 */
50400dc8657SNamhyung Kim 	symbol_conf.ignore_vmlinux_buildid = true;
50500dc8657SNamhyung Kim 
5066156681bSNamhyung Kim 	/*
5076156681bSNamhyung Kim 	 * If --buildid-all is given, it marks all DSO regardless of hits,
5086156681bSNamhyung Kim 	 * so no need to process samples.
5096156681bSNamhyung Kim 	 */
5106156681bSNamhyung Kim 	if (rec->buildid_all)
5116156681bSNamhyung Kim 		rec->tool.sample = NULL;
5126156681bSNamhyung Kim 
513b7b61cbeSArnaldo Carvalho de Melo 	return perf_session__process_events(session);
5146122e4e4SArnaldo Carvalho de Melo }
5156122e4e4SArnaldo Carvalho de Melo 
5168115d60cSArnaldo Carvalho de Melo static void perf_event__synthesize_guest_os(struct machine *machine, void *data)
517a1645ce1SZhang, Yanmin {
518a1645ce1SZhang, Yanmin 	int err;
51945694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = data;
520a1645ce1SZhang, Yanmin 	/*
521a1645ce1SZhang, Yanmin 	 *As for guest kernel when processing subcommand record&report,
522a1645ce1SZhang, Yanmin 	 *we arrange module mmap prior to guest kernel mmap and trigger
523a1645ce1SZhang, Yanmin 	 *a preload dso because default guest module symbols are loaded
524a1645ce1SZhang, Yanmin 	 *from guest kallsyms instead of /lib/modules/XXX/XXX. This
525a1645ce1SZhang, Yanmin 	 *method is used to avoid symbol missing when the first addr is
526a1645ce1SZhang, Yanmin 	 *in module instead of in guest kernel.
527a1645ce1SZhang, Yanmin 	 */
52845694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_modules(tool, process_synthesized_event,
529743eb868SArnaldo Carvalho de Melo 					     machine);
530a1645ce1SZhang, Yanmin 	if (err < 0)
531a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
53223346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
533a1645ce1SZhang, Yanmin 
534a1645ce1SZhang, Yanmin 	/*
535a1645ce1SZhang, Yanmin 	 * We use _stext for guest kernel because guest kernel's /proc/kallsyms
536a1645ce1SZhang, Yanmin 	 * have no _text sometimes.
537a1645ce1SZhang, Yanmin 	 */
53845694aa7SArnaldo Carvalho de Melo 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
5390ae617beSAdrian Hunter 						 machine);
540a1645ce1SZhang, Yanmin 	if (err < 0)
541a1645ce1SZhang, Yanmin 		pr_err("Couldn't record guest kernel [%d]'s reference"
54223346f21SArnaldo Carvalho de Melo 		       " relocation symbol.\n", machine->pid);
543a1645ce1SZhang, Yanmin }
544a1645ce1SZhang, Yanmin 
54598402807SFrederic Weisbecker static struct perf_event_header finished_round_event = {
54698402807SFrederic Weisbecker 	.size = sizeof(struct perf_event_header),
54798402807SFrederic Weisbecker 	.type = PERF_RECORD_FINISHED_ROUND,
54898402807SFrederic Weisbecker };
54998402807SFrederic Weisbecker 
550a4ea0ec4SWang Nan static int record__mmap_read_evlist(struct record *rec, struct perf_evlist *evlist,
551a4ea0ec4SWang Nan 				    bool backward)
55298402807SFrederic Weisbecker {
553dcabb507SJiri Olsa 	u64 bytes_written = rec->bytes_written;
5540e2e63ddSPeter Zijlstra 	int i;
5558d3eca20SDavid Ahern 	int rc = 0;
556a4ea0ec4SWang Nan 	struct perf_mmap *maps;
55798402807SFrederic Weisbecker 
558cb21686bSWang Nan 	if (!evlist)
559cb21686bSWang Nan 		return 0;
560ef149c25SAdrian Hunter 
561b2cb615dSWang Nan 	maps = backward ? evlist->backward_mmap : evlist->mmap;
562a4ea0ec4SWang Nan 	if (!maps)
563a4ea0ec4SWang Nan 		return 0;
564cb21686bSWang Nan 
56554cc54deSWang Nan 	if (backward && evlist->bkw_mmap_state != BKW_MMAP_DATA_PENDING)
56654cc54deSWang Nan 		return 0;
56754cc54deSWang Nan 
568a4ea0ec4SWang Nan 	for (i = 0; i < evlist->nr_mmaps; i++) {
569a4ea0ec4SWang Nan 		struct auxtrace_mmap *mm = &maps[i].auxtrace_mmap;
570a4ea0ec4SWang Nan 
571a4ea0ec4SWang Nan 		if (maps[i].base) {
572a4ea0ec4SWang Nan 			if (record__mmap_read(rec, &maps[i],
573a4ea0ec4SWang Nan 					      evlist->overwrite, backward) != 0) {
5748d3eca20SDavid Ahern 				rc = -1;
5758d3eca20SDavid Ahern 				goto out;
5768d3eca20SDavid Ahern 			}
5778d3eca20SDavid Ahern 		}
578ef149c25SAdrian Hunter 
5792dd6d8a1SAdrian Hunter 		if (mm->base && !rec->opts.auxtrace_snapshot_mode &&
580ef149c25SAdrian Hunter 		    record__auxtrace_mmap_read(rec, mm) != 0) {
581ef149c25SAdrian Hunter 			rc = -1;
582ef149c25SAdrian Hunter 			goto out;
583ef149c25SAdrian Hunter 		}
58498402807SFrederic Weisbecker 	}
58598402807SFrederic Weisbecker 
586dcabb507SJiri Olsa 	/*
587dcabb507SJiri Olsa 	 * Mark the round finished in case we wrote
588dcabb507SJiri Olsa 	 * at least one event.
589dcabb507SJiri Olsa 	 */
590dcabb507SJiri Olsa 	if (bytes_written != rec->bytes_written)
5918c6f45a7SArnaldo Carvalho de Melo 		rc = record__write(rec, &finished_round_event, sizeof(finished_round_event));
5928d3eca20SDavid Ahern 
59354cc54deSWang Nan 	if (backward)
59454cc54deSWang Nan 		perf_evlist__toggle_bkw_mmap(evlist, BKW_MMAP_EMPTY);
5958d3eca20SDavid Ahern out:
5968d3eca20SDavid Ahern 	return rc;
59798402807SFrederic Weisbecker }
59898402807SFrederic Weisbecker 
599cb21686bSWang Nan static int record__mmap_read_all(struct record *rec)
600cb21686bSWang Nan {
601cb21686bSWang Nan 	int err;
602cb21686bSWang Nan 
603a4ea0ec4SWang Nan 	err = record__mmap_read_evlist(rec, rec->evlist, false);
604cb21686bSWang Nan 	if (err)
605cb21686bSWang Nan 		return err;
606cb21686bSWang Nan 
60705737464SWang Nan 	return record__mmap_read_evlist(rec, rec->evlist, true);
608cb21686bSWang Nan }
609cb21686bSWang Nan 
6108c6f45a7SArnaldo Carvalho de Melo static void record__init_features(struct record *rec)
61157706abcSDavid Ahern {
61257706abcSDavid Ahern 	struct perf_session *session = rec->session;
61357706abcSDavid Ahern 	int feat;
61457706abcSDavid Ahern 
61557706abcSDavid Ahern 	for (feat = HEADER_FIRST_FEATURE; feat < HEADER_LAST_FEATURE; feat++)
61657706abcSDavid Ahern 		perf_header__set_feat(&session->header, feat);
61757706abcSDavid Ahern 
61857706abcSDavid Ahern 	if (rec->no_buildid)
61957706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BUILD_ID);
62057706abcSDavid Ahern 
6213e2be2daSArnaldo Carvalho de Melo 	if (!have_tracepoints(&rec->evlist->entries))
62257706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_TRACING_DATA);
62357706abcSDavid Ahern 
62457706abcSDavid Ahern 	if (!rec->opts.branch_stack)
62557706abcSDavid Ahern 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
626ef149c25SAdrian Hunter 
627ef149c25SAdrian Hunter 	if (!rec->opts.full_auxtrace)
628ef149c25SAdrian Hunter 		perf_header__clear_feat(&session->header, HEADER_AUXTRACE);
629ffa517adSJiri Olsa 
630ffa517adSJiri Olsa 	perf_header__clear_feat(&session->header, HEADER_STAT);
63157706abcSDavid Ahern }
63257706abcSDavid Ahern 
633e1ab48baSWang Nan static void
634e1ab48baSWang Nan record__finish_output(struct record *rec)
635e1ab48baSWang Nan {
636e1ab48baSWang Nan 	struct perf_data_file *file = &rec->file;
637e1ab48baSWang Nan 	int fd = perf_data_file__fd(file);
638e1ab48baSWang Nan 
639e1ab48baSWang Nan 	if (file->is_pipe)
640e1ab48baSWang Nan 		return;
641e1ab48baSWang Nan 
642e1ab48baSWang Nan 	rec->session->header.data_size += rec->bytes_written;
643e1ab48baSWang Nan 	file->size = lseek(perf_data_file__fd(file), 0, SEEK_CUR);
644e1ab48baSWang Nan 
645e1ab48baSWang Nan 	if (!rec->no_buildid) {
646e1ab48baSWang Nan 		process_buildids(rec);
647e1ab48baSWang Nan 
648e1ab48baSWang Nan 		if (rec->buildid_all)
649e1ab48baSWang Nan 			dsos__hit_all(rec->session);
650e1ab48baSWang Nan 	}
651e1ab48baSWang Nan 	perf_session__write_header(rec->session, rec->evlist, fd, true);
652e1ab48baSWang Nan 
653e1ab48baSWang Nan 	return;
654e1ab48baSWang Nan }
655e1ab48baSWang Nan 
6564ea648aeSWang Nan static int record__synthesize_workload(struct record *rec, bool tail)
657be7b0c9eSWang Nan {
6589d6aae72SArnaldo Carvalho de Melo 	int err;
6599d6aae72SArnaldo Carvalho de Melo 	struct thread_map *thread_map;
660be7b0c9eSWang Nan 
6614ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
6624ea648aeSWang Nan 		return 0;
6634ea648aeSWang Nan 
6649d6aae72SArnaldo Carvalho de Melo 	thread_map = thread_map__new_by_tid(rec->evlist->workload.pid);
6659d6aae72SArnaldo Carvalho de Melo 	if (thread_map == NULL)
6669d6aae72SArnaldo Carvalho de Melo 		return -1;
6679d6aae72SArnaldo Carvalho de Melo 
6689d6aae72SArnaldo Carvalho de Melo 	err = perf_event__synthesize_thread_map(&rec->tool, thread_map,
669be7b0c9eSWang Nan 						 process_synthesized_event,
670be7b0c9eSWang Nan 						 &rec->session->machines.host,
671be7b0c9eSWang Nan 						 rec->opts.sample_address,
672be7b0c9eSWang Nan 						 rec->opts.proc_map_timeout);
6739d6aae72SArnaldo Carvalho de Melo 	thread_map__put(thread_map);
6749d6aae72SArnaldo Carvalho de Melo 	return err;
675be7b0c9eSWang Nan }
676be7b0c9eSWang Nan 
6774ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail);
6783c1cb7e3SWang Nan 
679ecfd7a9cSWang Nan static int
680ecfd7a9cSWang Nan record__switch_output(struct record *rec, bool at_exit)
681ecfd7a9cSWang Nan {
682ecfd7a9cSWang Nan 	struct perf_data_file *file = &rec->file;
683ecfd7a9cSWang Nan 	int fd, err;
684ecfd7a9cSWang Nan 
685ecfd7a9cSWang Nan 	/* Same Size:      "2015122520103046"*/
686ecfd7a9cSWang Nan 	char timestamp[] = "InvalidTimestamp";
687ecfd7a9cSWang Nan 
6884ea648aeSWang Nan 	record__synthesize(rec, true);
6894ea648aeSWang Nan 	if (target__none(&rec->opts.target))
6904ea648aeSWang Nan 		record__synthesize_workload(rec, true);
6914ea648aeSWang Nan 
692ecfd7a9cSWang Nan 	rec->samples = 0;
693ecfd7a9cSWang Nan 	record__finish_output(rec);
694ecfd7a9cSWang Nan 	err = fetch_current_timestamp(timestamp, sizeof(timestamp));
695ecfd7a9cSWang Nan 	if (err) {
696ecfd7a9cSWang Nan 		pr_err("Failed to get current timestamp\n");
697ecfd7a9cSWang Nan 		return -EINVAL;
698ecfd7a9cSWang Nan 	}
699ecfd7a9cSWang Nan 
700ecfd7a9cSWang Nan 	fd = perf_data_file__switch(file, timestamp,
701ecfd7a9cSWang Nan 				    rec->session->header.data_offset,
702ecfd7a9cSWang Nan 				    at_exit);
703ecfd7a9cSWang Nan 	if (fd >= 0 && !at_exit) {
704ecfd7a9cSWang Nan 		rec->bytes_written = 0;
705ecfd7a9cSWang Nan 		rec->session->header.data_size = 0;
706ecfd7a9cSWang Nan 	}
707ecfd7a9cSWang Nan 
708ecfd7a9cSWang Nan 	if (!quiet)
709ecfd7a9cSWang Nan 		fprintf(stderr, "[ perf record: Dump %s.%s ]\n",
710ecfd7a9cSWang Nan 			file->path, timestamp);
7113c1cb7e3SWang Nan 
7123c1cb7e3SWang Nan 	/* Output tracking events */
713be7b0c9eSWang Nan 	if (!at_exit) {
7144ea648aeSWang Nan 		record__synthesize(rec, false);
7153c1cb7e3SWang Nan 
716be7b0c9eSWang Nan 		/*
717be7b0c9eSWang Nan 		 * In 'perf record --switch-output' without -a,
718be7b0c9eSWang Nan 		 * record__synthesize() in record__switch_output() won't
719be7b0c9eSWang Nan 		 * generate tracking events because there's no thread_map
720be7b0c9eSWang Nan 		 * in evlist. Which causes newly created perf.data doesn't
721be7b0c9eSWang Nan 		 * contain map and comm information.
722be7b0c9eSWang Nan 		 * Create a fake thread_map and directly call
723be7b0c9eSWang Nan 		 * perf_event__synthesize_thread_map() for those events.
724be7b0c9eSWang Nan 		 */
725be7b0c9eSWang Nan 		if (target__none(&rec->opts.target))
7264ea648aeSWang Nan 			record__synthesize_workload(rec, false);
727be7b0c9eSWang Nan 	}
728ecfd7a9cSWang Nan 	return fd;
729ecfd7a9cSWang Nan }
730ecfd7a9cSWang Nan 
731f33cbe72SArnaldo Carvalho de Melo static volatile int workload_exec_errno;
732f33cbe72SArnaldo Carvalho de Melo 
733f33cbe72SArnaldo Carvalho de Melo /*
734f33cbe72SArnaldo Carvalho de Melo  * perf_evlist__prepare_workload will send a SIGUSR1
735f33cbe72SArnaldo Carvalho de Melo  * if the fork fails, since we asked by setting its
736f33cbe72SArnaldo Carvalho de Melo  * want_signal to true.
737f33cbe72SArnaldo Carvalho de Melo  */
73845604710SNamhyung Kim static void workload_exec_failed_signal(int signo __maybe_unused,
73945604710SNamhyung Kim 					siginfo_t *info,
740f33cbe72SArnaldo Carvalho de Melo 					void *ucontext __maybe_unused)
741f33cbe72SArnaldo Carvalho de Melo {
742f33cbe72SArnaldo Carvalho de Melo 	workload_exec_errno = info->si_value.sival_int;
743f33cbe72SArnaldo Carvalho de Melo 	done = 1;
744f33cbe72SArnaldo Carvalho de Melo 	child_finished = 1;
745f33cbe72SArnaldo Carvalho de Melo }
746f33cbe72SArnaldo Carvalho de Melo 
7472dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig);
748bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig);
7492dd6d8a1SAdrian Hunter 
75046bc29b9SAdrian Hunter int __weak
75146bc29b9SAdrian Hunter perf_event__synth_time_conv(const struct perf_event_mmap_page *pc __maybe_unused,
75246bc29b9SAdrian Hunter 			    struct perf_tool *tool __maybe_unused,
75346bc29b9SAdrian Hunter 			    perf_event__handler_t process __maybe_unused,
75446bc29b9SAdrian Hunter 			    struct machine *machine __maybe_unused)
75546bc29b9SAdrian Hunter {
75646bc29b9SAdrian Hunter 	return 0;
75746bc29b9SAdrian Hunter }
75846bc29b9SAdrian Hunter 
759ee667f94SWang Nan static const struct perf_event_mmap_page *
760ee667f94SWang Nan perf_evlist__pick_pc(struct perf_evlist *evlist)
761ee667f94SWang Nan {
762b2cb615dSWang Nan 	if (evlist) {
763b2cb615dSWang Nan 		if (evlist->mmap && evlist->mmap[0].base)
764ee667f94SWang Nan 			return evlist->mmap[0].base;
765b2cb615dSWang Nan 		if (evlist->backward_mmap && evlist->backward_mmap[0].base)
766b2cb615dSWang Nan 			return evlist->backward_mmap[0].base;
767b2cb615dSWang Nan 	}
768ee667f94SWang Nan 	return NULL;
769ee667f94SWang Nan }
770ee667f94SWang Nan 
771c45628b0SWang Nan static const struct perf_event_mmap_page *record__pick_pc(struct record *rec)
772c45628b0SWang Nan {
773ee667f94SWang Nan 	const struct perf_event_mmap_page *pc;
774ee667f94SWang Nan 
775ee667f94SWang Nan 	pc = perf_evlist__pick_pc(rec->evlist);
776ee667f94SWang Nan 	if (pc)
777ee667f94SWang Nan 		return pc;
778c45628b0SWang Nan 	return NULL;
779c45628b0SWang Nan }
780c45628b0SWang Nan 
7814ea648aeSWang Nan static int record__synthesize(struct record *rec, bool tail)
782c45c86ebSWang Nan {
783c45c86ebSWang Nan 	struct perf_session *session = rec->session;
784c45c86ebSWang Nan 	struct machine *machine = &session->machines.host;
785c45c86ebSWang Nan 	struct perf_data_file *file = &rec->file;
786c45c86ebSWang Nan 	struct record_opts *opts = &rec->opts;
787c45c86ebSWang Nan 	struct perf_tool *tool = &rec->tool;
788c45c86ebSWang Nan 	int fd = perf_data_file__fd(file);
789c45c86ebSWang Nan 	int err = 0;
790c45c86ebSWang Nan 
7914ea648aeSWang Nan 	if (rec->opts.tail_synthesize != tail)
7924ea648aeSWang Nan 		return 0;
7934ea648aeSWang Nan 
794c45c86ebSWang Nan 	if (file->is_pipe) {
795c45c86ebSWang Nan 		err = perf_event__synthesize_attrs(tool, session,
796c45c86ebSWang Nan 						   process_synthesized_event);
797c45c86ebSWang Nan 		if (err < 0) {
798c45c86ebSWang Nan 			pr_err("Couldn't synthesize attrs.\n");
799c45c86ebSWang Nan 			goto out;
800c45c86ebSWang Nan 		}
801c45c86ebSWang Nan 
802c45c86ebSWang Nan 		if (have_tracepoints(&rec->evlist->entries)) {
803c45c86ebSWang Nan 			/*
804c45c86ebSWang Nan 			 * FIXME err <= 0 here actually means that
805c45c86ebSWang Nan 			 * there were no tracepoints so its not really
806c45c86ebSWang Nan 			 * an error, just that we don't need to
807c45c86ebSWang Nan 			 * synthesize anything.  We really have to
808c45c86ebSWang Nan 			 * return this more properly and also
809c45c86ebSWang Nan 			 * propagate errors that now are calling die()
810c45c86ebSWang Nan 			 */
811c45c86ebSWang Nan 			err = perf_event__synthesize_tracing_data(tool,	fd, rec->evlist,
812c45c86ebSWang Nan 								  process_synthesized_event);
813c45c86ebSWang Nan 			if (err <= 0) {
814c45c86ebSWang Nan 				pr_err("Couldn't record tracing data.\n");
815c45c86ebSWang Nan 				goto out;
816c45c86ebSWang Nan 			}
817c45c86ebSWang Nan 			rec->bytes_written += err;
818c45c86ebSWang Nan 		}
819c45c86ebSWang Nan 	}
820c45c86ebSWang Nan 
821c45628b0SWang Nan 	err = perf_event__synth_time_conv(record__pick_pc(rec), tool,
82246bc29b9SAdrian Hunter 					  process_synthesized_event, machine);
82346bc29b9SAdrian Hunter 	if (err)
82446bc29b9SAdrian Hunter 		goto out;
82546bc29b9SAdrian Hunter 
826c45c86ebSWang Nan 	if (rec->opts.full_auxtrace) {
827c45c86ebSWang Nan 		err = perf_event__synthesize_auxtrace_info(rec->itr, tool,
828c45c86ebSWang Nan 					session, process_synthesized_event);
829c45c86ebSWang Nan 		if (err)
830c45c86ebSWang Nan 			goto out;
831c45c86ebSWang Nan 	}
832c45c86ebSWang Nan 
833c45c86ebSWang Nan 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
834c45c86ebSWang Nan 						 machine);
835c45c86ebSWang Nan 	WARN_ONCE(err < 0, "Couldn't record kernel reference relocation symbol\n"
836c45c86ebSWang Nan 			   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
837c45c86ebSWang Nan 			   "Check /proc/kallsyms permission or run as root.\n");
838c45c86ebSWang Nan 
839c45c86ebSWang Nan 	err = perf_event__synthesize_modules(tool, process_synthesized_event,
840c45c86ebSWang Nan 					     machine);
841c45c86ebSWang Nan 	WARN_ONCE(err < 0, "Couldn't record kernel module information.\n"
842c45c86ebSWang Nan 			   "Symbol resolution may be skewed if relocation was used (e.g. kexec).\n"
843c45c86ebSWang Nan 			   "Check /proc/modules permission or run as root.\n");
844c45c86ebSWang Nan 
845c45c86ebSWang Nan 	if (perf_guest) {
846c45c86ebSWang Nan 		machines__process_guests(&session->machines,
847c45c86ebSWang Nan 					 perf_event__synthesize_guest_os, tool);
848c45c86ebSWang Nan 	}
849c45c86ebSWang Nan 
850c45c86ebSWang Nan 	err = __machine__synthesize_threads(machine, tool, &opts->target, rec->evlist->threads,
851c45c86ebSWang Nan 					    process_synthesized_event, opts->sample_address,
852c45c86ebSWang Nan 					    opts->proc_map_timeout);
853c45c86ebSWang Nan out:
854c45c86ebSWang Nan 	return err;
855c45c86ebSWang Nan }
856c45c86ebSWang Nan 
8578c6f45a7SArnaldo Carvalho de Melo static int __cmd_record(struct record *rec, int argc, const char **argv)
85886470930SIngo Molnar {
85957706abcSDavid Ahern 	int err;
86045604710SNamhyung Kim 	int status = 0;
8618b412664SPeter Zijlstra 	unsigned long waking = 0;
86246be604bSZhang, Yanmin 	const bool forks = argc > 0;
86323346f21SArnaldo Carvalho de Melo 	struct machine *machine;
86445694aa7SArnaldo Carvalho de Melo 	struct perf_tool *tool = &rec->tool;
865b4006796SArnaldo Carvalho de Melo 	struct record_opts *opts = &rec->opts;
866f5fc1412SJiri Olsa 	struct perf_data_file *file = &rec->file;
867d20deb64SArnaldo Carvalho de Melo 	struct perf_session *session;
8686dcf45efSArnaldo Carvalho de Melo 	bool disabled = false, draining = false;
86942aa276fSNamhyung Kim 	int fd;
87086470930SIngo Molnar 
871d20deb64SArnaldo Carvalho de Melo 	rec->progname = argv[0];
87233e49ea7SAndi Kleen 
87345604710SNamhyung Kim 	atexit(record__sig_exit);
874f5970550SPeter Zijlstra 	signal(SIGCHLD, sig_handler);
875f5970550SPeter Zijlstra 	signal(SIGINT, sig_handler);
876804f7ac7SDavid Ahern 	signal(SIGTERM, sig_handler);
877a074865eSWang Nan 	signal(SIGSEGV, sigsegv_handler);
878c0bdc1c4SWang Nan 
879f3b3614aSHari Bathini 	if (rec->opts.record_namespaces)
880f3b3614aSHari Bathini 		tool->namespace_events = true;
881f3b3614aSHari Bathini 
882dc0c6127SJiri Olsa 	if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
8832dd6d8a1SAdrian Hunter 		signal(SIGUSR2, snapshot_sig_handler);
8843c1cb7e3SWang Nan 		if (rec->opts.auxtrace_snapshot_mode)
8855f9cf599SWang Nan 			trigger_on(&auxtrace_snapshot_trigger);
886dc0c6127SJiri Olsa 		if (rec->switch_output.enabled)
8873c1cb7e3SWang Nan 			trigger_on(&switch_output_trigger);
888c0bdc1c4SWang Nan 	} else {
8892dd6d8a1SAdrian Hunter 		signal(SIGUSR2, SIG_IGN);
890c0bdc1c4SWang Nan 	}
891f5970550SPeter Zijlstra 
892b7b61cbeSArnaldo Carvalho de Melo 	session = perf_session__new(file, false, tool);
89394c744b6SArnaldo Carvalho de Melo 	if (session == NULL) {
894ffa91880SAdrien BAK 		pr_err("Perf session creation failed.\n");
895a9a70bbcSArnaldo Carvalho de Melo 		return -1;
896a9a70bbcSArnaldo Carvalho de Melo 	}
897a9a70bbcSArnaldo Carvalho de Melo 
89842aa276fSNamhyung Kim 	fd = perf_data_file__fd(file);
899d20deb64SArnaldo Carvalho de Melo 	rec->session = session;
900d20deb64SArnaldo Carvalho de Melo 
9018c6f45a7SArnaldo Carvalho de Melo 	record__init_features(rec);
902330aa675SStephane Eranian 
903d4db3f16SArnaldo Carvalho de Melo 	if (forks) {
9043e2be2daSArnaldo Carvalho de Melo 		err = perf_evlist__prepare_workload(rec->evlist, &opts->target,
905f5fc1412SJiri Olsa 						    argv, file->is_pipe,
906735f7e0bSArnaldo Carvalho de Melo 						    workload_exec_failed_signal);
90735b9d88eSArnaldo Carvalho de Melo 		if (err < 0) {
90835b9d88eSArnaldo Carvalho de Melo 			pr_err("Couldn't run the workload!\n");
90945604710SNamhyung Kim 			status = err;
91035b9d88eSArnaldo Carvalho de Melo 			goto out_delete_session;
911856e9660SPeter Zijlstra 		}
912856e9660SPeter Zijlstra 	}
913856e9660SPeter Zijlstra 
9148c6f45a7SArnaldo Carvalho de Melo 	if (record__open(rec) != 0) {
9158d3eca20SDavid Ahern 		err = -1;
91645604710SNamhyung Kim 		goto out_child;
9178d3eca20SDavid Ahern 	}
91886470930SIngo Molnar 
9198690a2a7SWang Nan 	err = bpf__apply_obj_config();
9208690a2a7SWang Nan 	if (err) {
9218690a2a7SWang Nan 		char errbuf[BUFSIZ];
9228690a2a7SWang Nan 
9238690a2a7SWang Nan 		bpf__strerror_apply_obj_config(err, errbuf, sizeof(errbuf));
9248690a2a7SWang Nan 		pr_err("ERROR: Apply config to BPF failed: %s\n",
9258690a2a7SWang Nan 			 errbuf);
9268690a2a7SWang Nan 		goto out_child;
9278690a2a7SWang Nan 	}
9288690a2a7SWang Nan 
929cca8482cSAdrian Hunter 	/*
930cca8482cSAdrian Hunter 	 * Normally perf_session__new would do this, but it doesn't have the
931cca8482cSAdrian Hunter 	 * evlist.
932cca8482cSAdrian Hunter 	 */
933cca8482cSAdrian Hunter 	if (rec->tool.ordered_events && !perf_evlist__sample_id_all(rec->evlist)) {
934cca8482cSAdrian Hunter 		pr_warning("WARNING: No sample_id_all support, falling back to unordered processing\n");
935cca8482cSAdrian Hunter 		rec->tool.ordered_events = false;
936cca8482cSAdrian Hunter 	}
937cca8482cSAdrian Hunter 
9383e2be2daSArnaldo Carvalho de Melo 	if (!rec->evlist->nr_groups)
939a8bb559bSNamhyung Kim 		perf_header__clear_feat(&session->header, HEADER_GROUP_DESC);
940a8bb559bSNamhyung Kim 
941f5fc1412SJiri Olsa 	if (file->is_pipe) {
94242aa276fSNamhyung Kim 		err = perf_header__write_pipe(fd);
943529870e3STom Zanussi 		if (err < 0)
94445604710SNamhyung Kim 			goto out_child;
945563aecb2SJiri Olsa 	} else {
94642aa276fSNamhyung Kim 		err = perf_session__write_header(session, rec->evlist, fd, false);
947d5eed904SArnaldo Carvalho de Melo 		if (err < 0)
94845604710SNamhyung Kim 			goto out_child;
949d5eed904SArnaldo Carvalho de Melo 	}
9507c6a1c65SPeter Zijlstra 
951d3665498SDavid Ahern 	if (!rec->no_buildid
952e20960c0SRobert Richter 	    && !perf_header__has_feat(&session->header, HEADER_BUILD_ID)) {
953d3665498SDavid Ahern 		pr_err("Couldn't generate buildids. "
954e20960c0SRobert Richter 		       "Use --no-buildid to profile anyway.\n");
9558d3eca20SDavid Ahern 		err = -1;
95645604710SNamhyung Kim 		goto out_child;
957e20960c0SRobert Richter 	}
958e20960c0SRobert Richter 
95934ba5122SArnaldo Carvalho de Melo 	machine = &session->machines.host;
960743eb868SArnaldo Carvalho de Melo 
9614ea648aeSWang Nan 	err = record__synthesize(rec, false);
962c45c86ebSWang Nan 	if (err < 0)
96345604710SNamhyung Kim 		goto out_child;
9648d3eca20SDavid Ahern 
965d20deb64SArnaldo Carvalho de Melo 	if (rec->realtime_prio) {
96686470930SIngo Molnar 		struct sched_param param;
96786470930SIngo Molnar 
968d20deb64SArnaldo Carvalho de Melo 		param.sched_priority = rec->realtime_prio;
96986470930SIngo Molnar 		if (sched_setscheduler(0, SCHED_FIFO, &param)) {
9706beba7adSArnaldo Carvalho de Melo 			pr_err("Could not set realtime priority.\n");
9718d3eca20SDavid Ahern 			err = -1;
97245604710SNamhyung Kim 			goto out_child;
97386470930SIngo Molnar 		}
97486470930SIngo Molnar 	}
97586470930SIngo Molnar 
976774cb499SJiri Olsa 	/*
977774cb499SJiri Olsa 	 * When perf is starting the traced process, all the events
978774cb499SJiri Olsa 	 * (apart from group members) have enable_on_exec=1 set,
979774cb499SJiri Olsa 	 * so don't spoil it by prematurely enabling them.
980774cb499SJiri Olsa 	 */
9816619a53eSAndi Kleen 	if (!target__none(&opts->target) && !opts->initial_delay)
9823e2be2daSArnaldo Carvalho de Melo 		perf_evlist__enable(rec->evlist);
983764e16a3SDavid Ahern 
984856e9660SPeter Zijlstra 	/*
985856e9660SPeter Zijlstra 	 * Let the child rip
986856e9660SPeter Zijlstra 	 */
987e803cf97SNamhyung Kim 	if (forks) {
988e5bed564SNamhyung Kim 		union perf_event *event;
989*e907caf3SHari Bathini 		pid_t tgid;
990e5bed564SNamhyung Kim 
991e5bed564SNamhyung Kim 		event = malloc(sizeof(event->comm) + machine->id_hdr_size);
992e5bed564SNamhyung Kim 		if (event == NULL) {
993e5bed564SNamhyung Kim 			err = -ENOMEM;
994e5bed564SNamhyung Kim 			goto out_child;
995e5bed564SNamhyung Kim 		}
996e5bed564SNamhyung Kim 
997e803cf97SNamhyung Kim 		/*
998e803cf97SNamhyung Kim 		 * Some H/W events are generated before COMM event
999e803cf97SNamhyung Kim 		 * which is emitted during exec(), so perf script
1000e803cf97SNamhyung Kim 		 * cannot see a correct process name for those events.
1001e803cf97SNamhyung Kim 		 * Synthesize COMM event to prevent it.
1002e803cf97SNamhyung Kim 		 */
1003*e907caf3SHari Bathini 		tgid = perf_event__synthesize_comm(tool, event,
1004e803cf97SNamhyung Kim 						   rec->evlist->workload.pid,
1005e803cf97SNamhyung Kim 						   process_synthesized_event,
1006e803cf97SNamhyung Kim 						   machine);
1007e5bed564SNamhyung Kim 		free(event);
1008e803cf97SNamhyung Kim 
1009*e907caf3SHari Bathini 		if (tgid == -1)
1010*e907caf3SHari Bathini 			goto out_child;
1011*e907caf3SHari Bathini 
1012*e907caf3SHari Bathini 		event = malloc(sizeof(event->namespaces) +
1013*e907caf3SHari Bathini 			       (NR_NAMESPACES * sizeof(struct perf_ns_link_info)) +
1014*e907caf3SHari Bathini 			       machine->id_hdr_size);
1015*e907caf3SHari Bathini 		if (event == NULL) {
1016*e907caf3SHari Bathini 			err = -ENOMEM;
1017*e907caf3SHari Bathini 			goto out_child;
1018*e907caf3SHari Bathini 		}
1019*e907caf3SHari Bathini 
1020*e907caf3SHari Bathini 		/*
1021*e907caf3SHari Bathini 		 * Synthesize NAMESPACES event for the command specified.
1022*e907caf3SHari Bathini 		 */
1023*e907caf3SHari Bathini 		perf_event__synthesize_namespaces(tool, event,
1024*e907caf3SHari Bathini 						  rec->evlist->workload.pid,
1025*e907caf3SHari Bathini 						  tgid, process_synthesized_event,
1026*e907caf3SHari Bathini 						  machine);
1027*e907caf3SHari Bathini 		free(event);
1028*e907caf3SHari Bathini 
10293e2be2daSArnaldo Carvalho de Melo 		perf_evlist__start_workload(rec->evlist);
1030e803cf97SNamhyung Kim 	}
1031856e9660SPeter Zijlstra 
10326619a53eSAndi Kleen 	if (opts->initial_delay) {
10330693e680SArnaldo Carvalho de Melo 		usleep(opts->initial_delay * USEC_PER_MSEC);
10346619a53eSAndi Kleen 		perf_evlist__enable(rec->evlist);
10356619a53eSAndi Kleen 	}
10366619a53eSAndi Kleen 
10375f9cf599SWang Nan 	trigger_ready(&auxtrace_snapshot_trigger);
10383c1cb7e3SWang Nan 	trigger_ready(&switch_output_trigger);
1039a074865eSWang Nan 	perf_hooks__invoke_record_start();
1040649c48a9SPeter Zijlstra 	for (;;) {
10419f065194SYang Shi 		unsigned long long hits = rec->samples;
104286470930SIngo Molnar 
104305737464SWang Nan 		/*
104405737464SWang Nan 		 * rec->evlist->bkw_mmap_state is possible to be
104505737464SWang Nan 		 * BKW_MMAP_EMPTY here: when done == true and
104605737464SWang Nan 		 * hits != rec->samples in previous round.
104705737464SWang Nan 		 *
104805737464SWang Nan 		 * perf_evlist__toggle_bkw_mmap ensure we never
104905737464SWang Nan 		 * convert BKW_MMAP_EMPTY to BKW_MMAP_DATA_PENDING.
105005737464SWang Nan 		 */
105105737464SWang Nan 		if (trigger_is_hit(&switch_output_trigger) || done || draining)
105205737464SWang Nan 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_DATA_PENDING);
105305737464SWang Nan 
10548c6f45a7SArnaldo Carvalho de Melo 		if (record__mmap_read_all(rec) < 0) {
10555f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
10563c1cb7e3SWang Nan 			trigger_error(&switch_output_trigger);
10578d3eca20SDavid Ahern 			err = -1;
105845604710SNamhyung Kim 			goto out_child;
10598d3eca20SDavid Ahern 		}
106086470930SIngo Molnar 
10612dd6d8a1SAdrian Hunter 		if (auxtrace_record__snapshot_started) {
10622dd6d8a1SAdrian Hunter 			auxtrace_record__snapshot_started = 0;
10635f9cf599SWang Nan 			if (!trigger_is_error(&auxtrace_snapshot_trigger))
10642dd6d8a1SAdrian Hunter 				record__read_auxtrace_snapshot(rec);
10655f9cf599SWang Nan 			if (trigger_is_error(&auxtrace_snapshot_trigger)) {
10662dd6d8a1SAdrian Hunter 				pr_err("AUX area tracing snapshot failed\n");
10672dd6d8a1SAdrian Hunter 				err = -1;
10682dd6d8a1SAdrian Hunter 				goto out_child;
10692dd6d8a1SAdrian Hunter 			}
10702dd6d8a1SAdrian Hunter 		}
10712dd6d8a1SAdrian Hunter 
10723c1cb7e3SWang Nan 		if (trigger_is_hit(&switch_output_trigger)) {
107305737464SWang Nan 			/*
107405737464SWang Nan 			 * If switch_output_trigger is hit, the data in
107505737464SWang Nan 			 * overwritable ring buffer should have been collected,
107605737464SWang Nan 			 * so bkw_mmap_state should be set to BKW_MMAP_EMPTY.
107705737464SWang Nan 			 *
107805737464SWang Nan 			 * If SIGUSR2 raise after or during record__mmap_read_all(),
107905737464SWang Nan 			 * record__mmap_read_all() didn't collect data from
108005737464SWang Nan 			 * overwritable ring buffer. Read again.
108105737464SWang Nan 			 */
108205737464SWang Nan 			if (rec->evlist->bkw_mmap_state == BKW_MMAP_RUNNING)
108305737464SWang Nan 				continue;
10843c1cb7e3SWang Nan 			trigger_ready(&switch_output_trigger);
10853c1cb7e3SWang Nan 
108605737464SWang Nan 			/*
108705737464SWang Nan 			 * Reenable events in overwrite ring buffer after
108805737464SWang Nan 			 * record__mmap_read_all(): we should have collected
108905737464SWang Nan 			 * data from it.
109005737464SWang Nan 			 */
109105737464SWang Nan 			perf_evlist__toggle_bkw_mmap(rec->evlist, BKW_MMAP_RUNNING);
109205737464SWang Nan 
10933c1cb7e3SWang Nan 			if (!quiet)
10943c1cb7e3SWang Nan 				fprintf(stderr, "[ perf record: dump data: Woken up %ld times ]\n",
10953c1cb7e3SWang Nan 					waking);
10963c1cb7e3SWang Nan 			waking = 0;
10973c1cb7e3SWang Nan 			fd = record__switch_output(rec, false);
10983c1cb7e3SWang Nan 			if (fd < 0) {
10993c1cb7e3SWang Nan 				pr_err("Failed to switch to new file\n");
11003c1cb7e3SWang Nan 				trigger_error(&switch_output_trigger);
11013c1cb7e3SWang Nan 				err = fd;
11023c1cb7e3SWang Nan 				goto out_child;
11033c1cb7e3SWang Nan 			}
1104bfacbe3bSJiri Olsa 
1105bfacbe3bSJiri Olsa 			/* re-arm the alarm */
1106bfacbe3bSJiri Olsa 			if (rec->switch_output.time)
1107bfacbe3bSJiri Olsa 				alarm(rec->switch_output.time);
11083c1cb7e3SWang Nan 		}
11093c1cb7e3SWang Nan 
1110d20deb64SArnaldo Carvalho de Melo 		if (hits == rec->samples) {
11116dcf45efSArnaldo Carvalho de Melo 			if (done || draining)
1112649c48a9SPeter Zijlstra 				break;
1113f66a889dSArnaldo Carvalho de Melo 			err = perf_evlist__poll(rec->evlist, -1);
1114a515114fSJiri Olsa 			/*
1115a515114fSJiri Olsa 			 * Propagate error, only if there's any. Ignore positive
1116a515114fSJiri Olsa 			 * number of returned events and interrupt error.
1117a515114fSJiri Olsa 			 */
1118a515114fSJiri Olsa 			if (err > 0 || (err < 0 && errno == EINTR))
111945604710SNamhyung Kim 				err = 0;
11208b412664SPeter Zijlstra 			waking++;
11216dcf45efSArnaldo Carvalho de Melo 
11226dcf45efSArnaldo Carvalho de Melo 			if (perf_evlist__filter_pollfd(rec->evlist, POLLERR | POLLHUP) == 0)
11236dcf45efSArnaldo Carvalho de Melo 				draining = true;
11248b412664SPeter Zijlstra 		}
11258b412664SPeter Zijlstra 
1126774cb499SJiri Olsa 		/*
1127774cb499SJiri Olsa 		 * When perf is starting the traced process, at the end events
1128774cb499SJiri Olsa 		 * die with the process and we wait for that. Thus no need to
1129774cb499SJiri Olsa 		 * disable events in this case.
1130774cb499SJiri Olsa 		 */
1131602ad878SArnaldo Carvalho de Melo 		if (done && !disabled && !target__none(&opts->target)) {
11325f9cf599SWang Nan 			trigger_off(&auxtrace_snapshot_trigger);
11333e2be2daSArnaldo Carvalho de Melo 			perf_evlist__disable(rec->evlist);
11342711926aSJiri Olsa 			disabled = true;
11352711926aSJiri Olsa 		}
11368b412664SPeter Zijlstra 	}
11375f9cf599SWang Nan 	trigger_off(&auxtrace_snapshot_trigger);
11383c1cb7e3SWang Nan 	trigger_off(&switch_output_trigger);
11398b412664SPeter Zijlstra 
1140f33cbe72SArnaldo Carvalho de Melo 	if (forks && workload_exec_errno) {
114135550da3SMasami Hiramatsu 		char msg[STRERR_BUFSIZE];
1142c8b5f2c9SArnaldo Carvalho de Melo 		const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
1143f33cbe72SArnaldo Carvalho de Melo 		pr_err("Workload failed: %s\n", emsg);
1144f33cbe72SArnaldo Carvalho de Melo 		err = -1;
114545604710SNamhyung Kim 		goto out_child;
1146f33cbe72SArnaldo Carvalho de Melo 	}
1147f33cbe72SArnaldo Carvalho de Melo 
1148e3d59112SNamhyung Kim 	if (!quiet)
11498b412664SPeter Zijlstra 		fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
115086470930SIngo Molnar 
11514ea648aeSWang Nan 	if (target__none(&rec->opts.target))
11524ea648aeSWang Nan 		record__synthesize_workload(rec, true);
11534ea648aeSWang Nan 
115445604710SNamhyung Kim out_child:
115545604710SNamhyung Kim 	if (forks) {
115645604710SNamhyung Kim 		int exit_status;
115745604710SNamhyung Kim 
115845604710SNamhyung Kim 		if (!child_finished)
115945604710SNamhyung Kim 			kill(rec->evlist->workload.pid, SIGTERM);
116045604710SNamhyung Kim 
116145604710SNamhyung Kim 		wait(&exit_status);
116245604710SNamhyung Kim 
116345604710SNamhyung Kim 		if (err < 0)
116445604710SNamhyung Kim 			status = err;
116545604710SNamhyung Kim 		else if (WIFEXITED(exit_status))
116645604710SNamhyung Kim 			status = WEXITSTATUS(exit_status);
116745604710SNamhyung Kim 		else if (WIFSIGNALED(exit_status))
116845604710SNamhyung Kim 			signr = WTERMSIG(exit_status);
116945604710SNamhyung Kim 	} else
117045604710SNamhyung Kim 		status = err;
117145604710SNamhyung Kim 
11724ea648aeSWang Nan 	record__synthesize(rec, true);
1173e3d59112SNamhyung Kim 	/* this will be recalculated during process_buildids() */
1174e3d59112SNamhyung Kim 	rec->samples = 0;
1175e3d59112SNamhyung Kim 
1176ecfd7a9cSWang Nan 	if (!err) {
1177ecfd7a9cSWang Nan 		if (!rec->timestamp_filename) {
1178e1ab48baSWang Nan 			record__finish_output(rec);
1179ecfd7a9cSWang Nan 		} else {
1180ecfd7a9cSWang Nan 			fd = record__switch_output(rec, true);
1181ecfd7a9cSWang Nan 			if (fd < 0) {
1182ecfd7a9cSWang Nan 				status = fd;
1183ecfd7a9cSWang Nan 				goto out_delete_session;
1184ecfd7a9cSWang Nan 			}
1185ecfd7a9cSWang Nan 		}
1186ecfd7a9cSWang Nan 	}
118739d17dacSArnaldo Carvalho de Melo 
1188a074865eSWang Nan 	perf_hooks__invoke_record_end();
1189a074865eSWang Nan 
1190e3d59112SNamhyung Kim 	if (!err && !quiet) {
1191e3d59112SNamhyung Kim 		char samples[128];
1192ecfd7a9cSWang Nan 		const char *postfix = rec->timestamp_filename ?
1193ecfd7a9cSWang Nan 					".<timestamp>" : "";
1194e3d59112SNamhyung Kim 
1195ef149c25SAdrian Hunter 		if (rec->samples && !rec->opts.full_auxtrace)
1196e3d59112SNamhyung Kim 			scnprintf(samples, sizeof(samples),
1197e3d59112SNamhyung Kim 				  " (%" PRIu64 " samples)", rec->samples);
1198e3d59112SNamhyung Kim 		else
1199e3d59112SNamhyung Kim 			samples[0] = '\0';
1200e3d59112SNamhyung Kim 
1201ecfd7a9cSWang Nan 		fprintf(stderr,	"[ perf record: Captured and wrote %.3f MB %s%s%s ]\n",
1202e3d59112SNamhyung Kim 			perf_data_file__size(file) / 1024.0 / 1024.0,
1203ecfd7a9cSWang Nan 			file->path, postfix, samples);
1204e3d59112SNamhyung Kim 	}
1205e3d59112SNamhyung Kim 
120639d17dacSArnaldo Carvalho de Melo out_delete_session:
120739d17dacSArnaldo Carvalho de Melo 	perf_session__delete(session);
120845604710SNamhyung Kim 	return status;
120986470930SIngo Molnar }
121086470930SIngo Molnar 
12110883e820SArnaldo Carvalho de Melo static void callchain_debug(struct callchain_param *callchain)
121209b0fd45SJiri Olsa {
1213aad2b21cSKan Liang 	static const char *str[CALLCHAIN_MAX] = { "NONE", "FP", "DWARF", "LBR" };
1214a601fdffSJiri Olsa 
12150883e820SArnaldo Carvalho de Melo 	pr_debug("callchain: type %s\n", str[callchain->record_mode]);
121626d33022SJiri Olsa 
12170883e820SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_DWARF)
121809b0fd45SJiri Olsa 		pr_debug("callchain: stack dump size %d\n",
12190883e820SArnaldo Carvalho de Melo 			 callchain->dump_size);
12200883e820SArnaldo Carvalho de Melo }
12210883e820SArnaldo Carvalho de Melo 
12220883e820SArnaldo Carvalho de Melo int record_opts__parse_callchain(struct record_opts *record,
12230883e820SArnaldo Carvalho de Melo 				 struct callchain_param *callchain,
12240883e820SArnaldo Carvalho de Melo 				 const char *arg, bool unset)
12250883e820SArnaldo Carvalho de Melo {
12260883e820SArnaldo Carvalho de Melo 	int ret;
12270883e820SArnaldo Carvalho de Melo 	callchain->enabled = !unset;
12280883e820SArnaldo Carvalho de Melo 
12290883e820SArnaldo Carvalho de Melo 	/* --no-call-graph */
12300883e820SArnaldo Carvalho de Melo 	if (unset) {
12310883e820SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_NONE;
12320883e820SArnaldo Carvalho de Melo 		pr_debug("callchain: disabled\n");
12330883e820SArnaldo Carvalho de Melo 		return 0;
12340883e820SArnaldo Carvalho de Melo 	}
12350883e820SArnaldo Carvalho de Melo 
12360883e820SArnaldo Carvalho de Melo 	ret = parse_callchain_record_opt(arg, callchain);
12370883e820SArnaldo Carvalho de Melo 	if (!ret) {
12380883e820SArnaldo Carvalho de Melo 		/* Enable data address sampling for DWARF unwind. */
12390883e820SArnaldo Carvalho de Melo 		if (callchain->record_mode == CALLCHAIN_DWARF)
12400883e820SArnaldo Carvalho de Melo 			record->sample_address = true;
12410883e820SArnaldo Carvalho de Melo 		callchain_debug(callchain);
12420883e820SArnaldo Carvalho de Melo 	}
12430883e820SArnaldo Carvalho de Melo 
12440883e820SArnaldo Carvalho de Melo 	return ret;
124509b0fd45SJiri Olsa }
124609b0fd45SJiri Olsa 
1247c421e80bSKan Liang int record_parse_callchain_opt(const struct option *opt,
124809b0fd45SJiri Olsa 			       const char *arg,
124909b0fd45SJiri Olsa 			       int unset)
125009b0fd45SJiri Olsa {
12510883e820SArnaldo Carvalho de Melo 	return record_opts__parse_callchain(opt->value, &callchain_param, arg, unset);
125226d33022SJiri Olsa }
125326d33022SJiri Olsa 
1254c421e80bSKan Liang int record_callchain_opt(const struct option *opt,
125509b0fd45SJiri Olsa 			 const char *arg __maybe_unused,
125609b0fd45SJiri Olsa 			 int unset __maybe_unused)
125709b0fd45SJiri Olsa {
12582ddd5c04SArnaldo Carvalho de Melo 	struct callchain_param *callchain = opt->value;
1259c421e80bSKan Liang 
12602ddd5c04SArnaldo Carvalho de Melo 	callchain->enabled = true;
126109b0fd45SJiri Olsa 
12622ddd5c04SArnaldo Carvalho de Melo 	if (callchain->record_mode == CALLCHAIN_NONE)
12632ddd5c04SArnaldo Carvalho de Melo 		callchain->record_mode = CALLCHAIN_FP;
1264eb853e80SJiri Olsa 
12652ddd5c04SArnaldo Carvalho de Melo 	callchain_debug(callchain);
126609b0fd45SJiri Olsa 	return 0;
126709b0fd45SJiri Olsa }
126809b0fd45SJiri Olsa 
1269eb853e80SJiri Olsa static int perf_record_config(const char *var, const char *value, void *cb)
1270eb853e80SJiri Olsa {
12717a29c087SNamhyung Kim 	struct record *rec = cb;
12727a29c087SNamhyung Kim 
12737a29c087SNamhyung Kim 	if (!strcmp(var, "record.build-id")) {
12747a29c087SNamhyung Kim 		if (!strcmp(value, "cache"))
12757a29c087SNamhyung Kim 			rec->no_buildid_cache = false;
12767a29c087SNamhyung Kim 		else if (!strcmp(value, "no-cache"))
12777a29c087SNamhyung Kim 			rec->no_buildid_cache = true;
12787a29c087SNamhyung Kim 		else if (!strcmp(value, "skip"))
12797a29c087SNamhyung Kim 			rec->no_buildid = true;
12807a29c087SNamhyung Kim 		else
12817a29c087SNamhyung Kim 			return -1;
12827a29c087SNamhyung Kim 		return 0;
12837a29c087SNamhyung Kim 	}
1284eb853e80SJiri Olsa 	if (!strcmp(var, "record.call-graph"))
12855a2e5e85SNamhyung Kim 		var = "call-graph.record-mode"; /* fall-through */
1286eb853e80SJiri Olsa 
1287eb853e80SJiri Olsa 	return perf_default_config(var, value, cb);
1288eb853e80SJiri Olsa }
1289eb853e80SJiri Olsa 
1290814c8c38SPeter Zijlstra struct clockid_map {
1291814c8c38SPeter Zijlstra 	const char *name;
1292814c8c38SPeter Zijlstra 	int clockid;
1293814c8c38SPeter Zijlstra };
1294814c8c38SPeter Zijlstra 
1295814c8c38SPeter Zijlstra #define CLOCKID_MAP(n, c)	\
1296814c8c38SPeter Zijlstra 	{ .name = n, .clockid = (c), }
1297814c8c38SPeter Zijlstra 
1298814c8c38SPeter Zijlstra #define CLOCKID_END	{ .name = NULL, }
1299814c8c38SPeter Zijlstra 
1300814c8c38SPeter Zijlstra 
1301814c8c38SPeter Zijlstra /*
1302814c8c38SPeter Zijlstra  * Add the missing ones, we need to build on many distros...
1303814c8c38SPeter Zijlstra  */
1304814c8c38SPeter Zijlstra #ifndef CLOCK_MONOTONIC_RAW
1305814c8c38SPeter Zijlstra #define CLOCK_MONOTONIC_RAW 4
1306814c8c38SPeter Zijlstra #endif
1307814c8c38SPeter Zijlstra #ifndef CLOCK_BOOTTIME
1308814c8c38SPeter Zijlstra #define CLOCK_BOOTTIME 7
1309814c8c38SPeter Zijlstra #endif
1310814c8c38SPeter Zijlstra #ifndef CLOCK_TAI
1311814c8c38SPeter Zijlstra #define CLOCK_TAI 11
1312814c8c38SPeter Zijlstra #endif
1313814c8c38SPeter Zijlstra 
1314814c8c38SPeter Zijlstra static const struct clockid_map clockids[] = {
1315814c8c38SPeter Zijlstra 	/* available for all events, NMI safe */
1316814c8c38SPeter Zijlstra 	CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
1317814c8c38SPeter Zijlstra 	CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
1318814c8c38SPeter Zijlstra 
1319814c8c38SPeter Zijlstra 	/* available for some events */
1320814c8c38SPeter Zijlstra 	CLOCKID_MAP("realtime", CLOCK_REALTIME),
1321814c8c38SPeter Zijlstra 	CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
1322814c8c38SPeter Zijlstra 	CLOCKID_MAP("tai", CLOCK_TAI),
1323814c8c38SPeter Zijlstra 
1324814c8c38SPeter Zijlstra 	/* available for the lazy */
1325814c8c38SPeter Zijlstra 	CLOCKID_MAP("mono", CLOCK_MONOTONIC),
1326814c8c38SPeter Zijlstra 	CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
1327814c8c38SPeter Zijlstra 	CLOCKID_MAP("real", CLOCK_REALTIME),
1328814c8c38SPeter Zijlstra 	CLOCKID_MAP("boot", CLOCK_BOOTTIME),
1329814c8c38SPeter Zijlstra 
1330814c8c38SPeter Zijlstra 	CLOCKID_END,
1331814c8c38SPeter Zijlstra };
1332814c8c38SPeter Zijlstra 
1333814c8c38SPeter Zijlstra static int parse_clockid(const struct option *opt, const char *str, int unset)
1334814c8c38SPeter Zijlstra {
1335814c8c38SPeter Zijlstra 	struct record_opts *opts = (struct record_opts *)opt->value;
1336814c8c38SPeter Zijlstra 	const struct clockid_map *cm;
1337814c8c38SPeter Zijlstra 	const char *ostr = str;
1338814c8c38SPeter Zijlstra 
1339814c8c38SPeter Zijlstra 	if (unset) {
1340814c8c38SPeter Zijlstra 		opts->use_clockid = 0;
1341814c8c38SPeter Zijlstra 		return 0;
1342814c8c38SPeter Zijlstra 	}
1343814c8c38SPeter Zijlstra 
1344814c8c38SPeter Zijlstra 	/* no arg passed */
1345814c8c38SPeter Zijlstra 	if (!str)
1346814c8c38SPeter Zijlstra 		return 0;
1347814c8c38SPeter Zijlstra 
1348814c8c38SPeter Zijlstra 	/* no setting it twice */
1349814c8c38SPeter Zijlstra 	if (opts->use_clockid)
1350814c8c38SPeter Zijlstra 		return -1;
1351814c8c38SPeter Zijlstra 
1352814c8c38SPeter Zijlstra 	opts->use_clockid = true;
1353814c8c38SPeter Zijlstra 
1354814c8c38SPeter Zijlstra 	/* if its a number, we're done */
1355814c8c38SPeter Zijlstra 	if (sscanf(str, "%d", &opts->clockid) == 1)
1356814c8c38SPeter Zijlstra 		return 0;
1357814c8c38SPeter Zijlstra 
1358814c8c38SPeter Zijlstra 	/* allow a "CLOCK_" prefix to the name */
1359814c8c38SPeter Zijlstra 	if (!strncasecmp(str, "CLOCK_", 6))
1360814c8c38SPeter Zijlstra 		str += 6;
1361814c8c38SPeter Zijlstra 
1362814c8c38SPeter Zijlstra 	for (cm = clockids; cm->name; cm++) {
1363814c8c38SPeter Zijlstra 		if (!strcasecmp(str, cm->name)) {
1364814c8c38SPeter Zijlstra 			opts->clockid = cm->clockid;
1365814c8c38SPeter Zijlstra 			return 0;
1366814c8c38SPeter Zijlstra 		}
1367814c8c38SPeter Zijlstra 	}
1368814c8c38SPeter Zijlstra 
1369814c8c38SPeter Zijlstra 	opts->use_clockid = false;
1370814c8c38SPeter Zijlstra 	ui__warning("unknown clockid %s, check man page\n", ostr);
1371814c8c38SPeter Zijlstra 	return -1;
1372814c8c38SPeter Zijlstra }
1373814c8c38SPeter Zijlstra 
1374e9db1310SAdrian Hunter static int record__parse_mmap_pages(const struct option *opt,
1375e9db1310SAdrian Hunter 				    const char *str,
1376e9db1310SAdrian Hunter 				    int unset __maybe_unused)
1377e9db1310SAdrian Hunter {
1378e9db1310SAdrian Hunter 	struct record_opts *opts = opt->value;
1379e9db1310SAdrian Hunter 	char *s, *p;
1380e9db1310SAdrian Hunter 	unsigned int mmap_pages;
1381e9db1310SAdrian Hunter 	int ret;
1382e9db1310SAdrian Hunter 
1383e9db1310SAdrian Hunter 	if (!str)
1384e9db1310SAdrian Hunter 		return -EINVAL;
1385e9db1310SAdrian Hunter 
1386e9db1310SAdrian Hunter 	s = strdup(str);
1387e9db1310SAdrian Hunter 	if (!s)
1388e9db1310SAdrian Hunter 		return -ENOMEM;
1389e9db1310SAdrian Hunter 
1390e9db1310SAdrian Hunter 	p = strchr(s, ',');
1391e9db1310SAdrian Hunter 	if (p)
1392e9db1310SAdrian Hunter 		*p = '\0';
1393e9db1310SAdrian Hunter 
1394e9db1310SAdrian Hunter 	if (*s) {
1395e9db1310SAdrian Hunter 		ret = __perf_evlist__parse_mmap_pages(&mmap_pages, s);
1396e9db1310SAdrian Hunter 		if (ret)
1397e9db1310SAdrian Hunter 			goto out_free;
1398e9db1310SAdrian Hunter 		opts->mmap_pages = mmap_pages;
1399e9db1310SAdrian Hunter 	}
1400e9db1310SAdrian Hunter 
1401e9db1310SAdrian Hunter 	if (!p) {
1402e9db1310SAdrian Hunter 		ret = 0;
1403e9db1310SAdrian Hunter 		goto out_free;
1404e9db1310SAdrian Hunter 	}
1405e9db1310SAdrian Hunter 
1406e9db1310SAdrian Hunter 	ret = __perf_evlist__parse_mmap_pages(&mmap_pages, p + 1);
1407e9db1310SAdrian Hunter 	if (ret)
1408e9db1310SAdrian Hunter 		goto out_free;
1409e9db1310SAdrian Hunter 
1410e9db1310SAdrian Hunter 	opts->auxtrace_mmap_pages = mmap_pages;
1411e9db1310SAdrian Hunter 
1412e9db1310SAdrian Hunter out_free:
1413e9db1310SAdrian Hunter 	free(s);
1414e9db1310SAdrian Hunter 	return ret;
1415e9db1310SAdrian Hunter }
1416e9db1310SAdrian Hunter 
14170c582449SJiri Olsa static void switch_output_size_warn(struct record *rec)
14180c582449SJiri Olsa {
14190c582449SJiri Olsa 	u64 wakeup_size = perf_evlist__mmap_size(rec->opts.mmap_pages);
14200c582449SJiri Olsa 	struct switch_output *s = &rec->switch_output;
14210c582449SJiri Olsa 
14220c582449SJiri Olsa 	wakeup_size /= 2;
14230c582449SJiri Olsa 
14240c582449SJiri Olsa 	if (s->size < wakeup_size) {
14250c582449SJiri Olsa 		char buf[100];
14260c582449SJiri Olsa 
14270c582449SJiri Olsa 		unit_number__scnprintf(buf, sizeof(buf), wakeup_size);
14280c582449SJiri Olsa 		pr_warning("WARNING: switch-output data size lower than "
14290c582449SJiri Olsa 			   "wakeup kernel buffer size (%s) "
14300c582449SJiri Olsa 			   "expect bigger perf.data sizes\n", buf);
14310c582449SJiri Olsa 	}
14320c582449SJiri Olsa }
14330c582449SJiri Olsa 
1434cb4e1ebbSJiri Olsa static int switch_output_setup(struct record *rec)
1435cb4e1ebbSJiri Olsa {
1436cb4e1ebbSJiri Olsa 	struct switch_output *s = &rec->switch_output;
1437dc0c6127SJiri Olsa 	static struct parse_tag tags_size[] = {
1438dc0c6127SJiri Olsa 		{ .tag  = 'B', .mult = 1       },
1439dc0c6127SJiri Olsa 		{ .tag  = 'K', .mult = 1 << 10 },
1440dc0c6127SJiri Olsa 		{ .tag  = 'M', .mult = 1 << 20 },
1441dc0c6127SJiri Olsa 		{ .tag  = 'G', .mult = 1 << 30 },
1442dc0c6127SJiri Olsa 		{ .tag  = 0 },
1443dc0c6127SJiri Olsa 	};
1444bfacbe3bSJiri Olsa 	static struct parse_tag tags_time[] = {
1445bfacbe3bSJiri Olsa 		{ .tag  = 's', .mult = 1        },
1446bfacbe3bSJiri Olsa 		{ .tag  = 'm', .mult = 60       },
1447bfacbe3bSJiri Olsa 		{ .tag  = 'h', .mult = 60*60    },
1448bfacbe3bSJiri Olsa 		{ .tag  = 'd', .mult = 60*60*24 },
1449bfacbe3bSJiri Olsa 		{ .tag  = 0 },
1450bfacbe3bSJiri Olsa 	};
1451dc0c6127SJiri Olsa 	unsigned long val;
1452cb4e1ebbSJiri Olsa 
1453cb4e1ebbSJiri Olsa 	if (!s->set)
1454cb4e1ebbSJiri Olsa 		return 0;
1455cb4e1ebbSJiri Olsa 
1456cb4e1ebbSJiri Olsa 	if (!strcmp(s->str, "signal")) {
1457cb4e1ebbSJiri Olsa 		s->signal = true;
1458cb4e1ebbSJiri Olsa 		pr_debug("switch-output with SIGUSR2 signal\n");
1459dc0c6127SJiri Olsa 		goto enabled;
1460dc0c6127SJiri Olsa 	}
1461dc0c6127SJiri Olsa 
1462dc0c6127SJiri Olsa 	val = parse_tag_value(s->str, tags_size);
1463dc0c6127SJiri Olsa 	if (val != (unsigned long) -1) {
1464dc0c6127SJiri Olsa 		s->size = val;
1465dc0c6127SJiri Olsa 		pr_debug("switch-output with %s size threshold\n", s->str);
1466dc0c6127SJiri Olsa 		goto enabled;
1467cb4e1ebbSJiri Olsa 	}
1468cb4e1ebbSJiri Olsa 
1469bfacbe3bSJiri Olsa 	val = parse_tag_value(s->str, tags_time);
1470bfacbe3bSJiri Olsa 	if (val != (unsigned long) -1) {
1471bfacbe3bSJiri Olsa 		s->time = val;
1472bfacbe3bSJiri Olsa 		pr_debug("switch-output with %s time threshold (%lu seconds)\n",
1473bfacbe3bSJiri Olsa 			 s->str, s->time);
1474bfacbe3bSJiri Olsa 		goto enabled;
1475bfacbe3bSJiri Olsa 	}
1476bfacbe3bSJiri Olsa 
1477cb4e1ebbSJiri Olsa 	return -1;
1478dc0c6127SJiri Olsa 
1479dc0c6127SJiri Olsa enabled:
1480dc0c6127SJiri Olsa 	rec->timestamp_filename = true;
1481dc0c6127SJiri Olsa 	s->enabled              = true;
14820c582449SJiri Olsa 
14830c582449SJiri Olsa 	if (s->size && !rec->opts.no_buffering)
14840c582449SJiri Olsa 		switch_output_size_warn(rec);
14850c582449SJiri Olsa 
1486dc0c6127SJiri Olsa 	return 0;
1487cb4e1ebbSJiri Olsa }
1488cb4e1ebbSJiri Olsa 
1489e5b2c207SNamhyung Kim static const char * const __record_usage[] = {
149086470930SIngo Molnar 	"perf record [<options>] [<command>]",
149186470930SIngo Molnar 	"perf record [<options>] -- <command> [<options>]",
149286470930SIngo Molnar 	NULL
149386470930SIngo Molnar };
1494e5b2c207SNamhyung Kim const char * const *record_usage = __record_usage;
149586470930SIngo Molnar 
1496d20deb64SArnaldo Carvalho de Melo /*
14978c6f45a7SArnaldo Carvalho de Melo  * XXX Ideally would be local to cmd_record() and passed to a record__new
14988c6f45a7SArnaldo Carvalho de Melo  * because we need to have access to it in record__exit, that is called
1499d20deb64SArnaldo Carvalho de Melo  * after cmd_record() exits, but since record_options need to be accessible to
1500d20deb64SArnaldo Carvalho de Melo  * builtin-script, leave it here.
1501d20deb64SArnaldo Carvalho de Melo  *
1502d20deb64SArnaldo Carvalho de Melo  * At least we don't ouch it in all the other functions here directly.
1503d20deb64SArnaldo Carvalho de Melo  *
1504d20deb64SArnaldo Carvalho de Melo  * Just say no to tons of global variables, sigh.
1505d20deb64SArnaldo Carvalho de Melo  */
15068c6f45a7SArnaldo Carvalho de Melo static struct record record = {
1507d20deb64SArnaldo Carvalho de Melo 	.opts = {
15088affc2b8SAndi Kleen 		.sample_time	     = true,
1509d20deb64SArnaldo Carvalho de Melo 		.mmap_pages	     = UINT_MAX,
1510d20deb64SArnaldo Carvalho de Melo 		.user_freq	     = UINT_MAX,
1511d20deb64SArnaldo Carvalho de Melo 		.user_interval	     = ULLONG_MAX,
1512447a6013SArnaldo Carvalho de Melo 		.freq		     = 4000,
1513d1cb9fceSNamhyung Kim 		.target		     = {
1514d1cb9fceSNamhyung Kim 			.uses_mmap   = true,
15153aa5939dSAdrian Hunter 			.default_per_cpu = true,
1516d1cb9fceSNamhyung Kim 		},
15179d9cad76SKan Liang 		.proc_map_timeout     = 500,
1518d20deb64SArnaldo Carvalho de Melo 	},
1519e3d59112SNamhyung Kim 	.tool = {
1520e3d59112SNamhyung Kim 		.sample		= process_sample_event,
1521e3d59112SNamhyung Kim 		.fork		= perf_event__process_fork,
1522cca8482cSAdrian Hunter 		.exit		= perf_event__process_exit,
1523e3d59112SNamhyung Kim 		.comm		= perf_event__process_comm,
1524f3b3614aSHari Bathini 		.namespaces	= perf_event__process_namespaces,
1525e3d59112SNamhyung Kim 		.mmap		= perf_event__process_mmap,
1526e3d59112SNamhyung Kim 		.mmap2		= perf_event__process_mmap2,
1527cca8482cSAdrian Hunter 		.ordered_events	= true,
1528e3d59112SNamhyung Kim 	},
1529d20deb64SArnaldo Carvalho de Melo };
15307865e817SFrederic Weisbecker 
153176a26549SNamhyung Kim const char record_callchain_help[] = CALLCHAIN_RECORD_HELP
153276a26549SNamhyung Kim 	"\n\t\t\t\tDefault: fp";
153361eaa3beSArnaldo Carvalho de Melo 
15340aab2136SWang Nan static bool dry_run;
15350aab2136SWang Nan 
1536d20deb64SArnaldo Carvalho de Melo /*
1537d20deb64SArnaldo Carvalho de Melo  * XXX Will stay a global variable till we fix builtin-script.c to stop messing
1538d20deb64SArnaldo Carvalho de Melo  * with it and switch to use the library functions in perf_evlist that came
1539b4006796SArnaldo Carvalho de Melo  * from builtin-record.c, i.e. use record_opts,
1540d20deb64SArnaldo Carvalho de Melo  * perf_evlist__prepare_workload, etc instead of fork+exec'in 'perf record',
1541d20deb64SArnaldo Carvalho de Melo  * using pipes, etc.
1542d20deb64SArnaldo Carvalho de Melo  */
1543efd21307SJiri Olsa static struct option __record_options[] = {
1544d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('e', "event", &record.evlist, "event",
154586470930SIngo Molnar 		     "event selector. use 'perf list' to list available events",
1546f120f9d5SJiri Olsa 		     parse_events_option),
1547d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK(0, "filter", &record.evlist, "filter",
1548c171b552SLi Zefan 		     "event filter", parse_filter),
15494ba1faa1SWang Nan 	OPT_CALLBACK_NOOPT(0, "exclude-perf", &record.evlist,
15504ba1faa1SWang Nan 			   NULL, "don't record events from perf itself",
15514ba1faa1SWang Nan 			   exclude_perf),
1552bea03405SNamhyung Kim 	OPT_STRING('p', "pid", &record.opts.target.pid, "pid",
1553d6d901c2SZhang, Yanmin 		    "record events on existing process id"),
1554bea03405SNamhyung Kim 	OPT_STRING('t', "tid", &record.opts.target.tid, "tid",
1555d6d901c2SZhang, Yanmin 		    "record events on existing thread id"),
1556d20deb64SArnaldo Carvalho de Melo 	OPT_INTEGER('r', "realtime", &record.realtime_prio,
155786470930SIngo Molnar 		    "collect data with this RT SCHED_FIFO priority"),
1558509051eaSArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "no-buffering", &record.opts.no_buffering,
1559acac03faSKirill Smelkov 		    "collect data without buffering"),
1560d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('R', "raw-samples", &record.opts.raw_samples,
1561daac07b2SFrederic Weisbecker 		    "collect raw sample records from all opened counters"),
1562bea03405SNamhyung Kim 	OPT_BOOLEAN('a', "all-cpus", &record.opts.target.system_wide,
156386470930SIngo Molnar 			    "system-wide collection from all CPUs"),
1564bea03405SNamhyung Kim 	OPT_STRING('C', "cpu", &record.opts.target.cpu_list, "cpu",
1565c45c6ea2SStephane Eranian 		    "list of cpus to monitor"),
1566d20deb64SArnaldo Carvalho de Melo 	OPT_U64('c', "count", &record.opts.user_interval, "event period to sample"),
1567f5fc1412SJiri Olsa 	OPT_STRING('o', "output", &record.file.path, "file",
156886470930SIngo Molnar 		    "output file name"),
156969e7e5b0SAdrian Hunter 	OPT_BOOLEAN_SET('i', "no-inherit", &record.opts.no_inherit,
157069e7e5b0SAdrian Hunter 			&record.opts.no_inherit_set,
15712e6cdf99SStephane Eranian 			"child tasks do not inherit counters"),
15724ea648aeSWang Nan 	OPT_BOOLEAN(0, "tail-synthesize", &record.opts.tail_synthesize,
15734ea648aeSWang Nan 		    "synthesize non-sample events at the end of output"),
1574626a6b78SWang Nan 	OPT_BOOLEAN(0, "overwrite", &record.opts.overwrite, "use overwrite mode"),
1575d20deb64SArnaldo Carvalho de Melo 	OPT_UINTEGER('F', "freq", &record.opts.user_freq, "profile at this frequency"),
1576e9db1310SAdrian Hunter 	OPT_CALLBACK('m', "mmap-pages", &record.opts, "pages[,pages]",
1577e9db1310SAdrian Hunter 		     "number of mmap data pages and AUX area tracing mmap pages",
1578e9db1310SAdrian Hunter 		     record__parse_mmap_pages),
1579d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN(0, "group", &record.opts.group,
158043bece79SLin Ming 		    "put the counters into a counter group"),
15812ddd5c04SArnaldo Carvalho de Melo 	OPT_CALLBACK_NOOPT('g', NULL, &callchain_param,
158209b0fd45SJiri Olsa 			   NULL, "enables call-graph recording" ,
158309b0fd45SJiri Olsa 			   &record_callchain_opt),
158409b0fd45SJiri Olsa 	OPT_CALLBACK(0, "call-graph", &record.opts,
158576a26549SNamhyung Kim 		     "record_mode[,record_size]", record_callchain_help,
158609b0fd45SJiri Olsa 		     &record_parse_callchain_opt),
1587c0555642SIan Munsie 	OPT_INCR('v', "verbose", &verbose,
15883da297a6SIngo Molnar 		    "be more verbose (show counter open errors, etc)"),
1589b44308f5SArnaldo Carvalho de Melo 	OPT_BOOLEAN('q', "quiet", &quiet, "don't print any message"),
1590d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('s', "stat", &record.opts.inherit_stat,
1591649c48a9SPeter Zijlstra 		    "per thread counts"),
159256100321SPeter Zijlstra 	OPT_BOOLEAN('d', "data", &record.opts.sample_address, "Record the sample addresses"),
1593b6f35ed7SJiri Olsa 	OPT_BOOLEAN(0, "sample-cpu", &record.opts.sample_cpu, "Record the sample cpu"),
15943abebc55SAdrian Hunter 	OPT_BOOLEAN_SET('T', "timestamp", &record.opts.sample_time,
15953abebc55SAdrian Hunter 			&record.opts.sample_time_set,
15963abebc55SAdrian Hunter 			"Record the sample timestamps"),
159756100321SPeter Zijlstra 	OPT_BOOLEAN('P', "period", &record.opts.period, "Record the sample period"),
1598d20deb64SArnaldo Carvalho de Melo 	OPT_BOOLEAN('n', "no-samples", &record.opts.no_samples,
1599649c48a9SPeter Zijlstra 		    "don't sample"),
1600d2db9a98SWang Nan 	OPT_BOOLEAN_SET('N', "no-buildid-cache", &record.no_buildid_cache,
1601d2db9a98SWang Nan 			&record.no_buildid_cache_set,
1602a1ac1d3cSStephane Eranian 			"do not update the buildid cache"),
1603d2db9a98SWang Nan 	OPT_BOOLEAN_SET('B', "no-buildid", &record.no_buildid,
1604d2db9a98SWang Nan 			&record.no_buildid_set,
1605baa2f6ceSArnaldo Carvalho de Melo 			"do not collect buildids in perf.data"),
1606d20deb64SArnaldo Carvalho de Melo 	OPT_CALLBACK('G', "cgroup", &record.evlist, "name",
1607023695d9SStephane Eranian 		     "monitor event in cgroup name only",
1608023695d9SStephane Eranian 		     parse_cgroups),
1609a6205a35SArnaldo Carvalho de Melo 	OPT_UINTEGER('D', "delay", &record.opts.initial_delay,
16106619a53eSAndi Kleen 		  "ms to wait before starting measurement after program start"),
1611bea03405SNamhyung Kim 	OPT_STRING('u', "uid", &record.opts.target.uid_str, "user",
1612bea03405SNamhyung Kim 		   "user to profile"),
1613a5aabdacSStephane Eranian 
1614a5aabdacSStephane Eranian 	OPT_CALLBACK_NOOPT('b', "branch-any", &record.opts.branch_stack,
1615a5aabdacSStephane Eranian 		     "branch any", "sample any taken branches",
1616a5aabdacSStephane Eranian 		     parse_branch_stack),
1617a5aabdacSStephane Eranian 
1618a5aabdacSStephane Eranian 	OPT_CALLBACK('j', "branch-filter", &record.opts.branch_stack,
1619a5aabdacSStephane Eranian 		     "branch filter mask", "branch stack filter modes",
1620bdfebd84SRoberto Agostino Vitillo 		     parse_branch_stack),
162105484298SAndi Kleen 	OPT_BOOLEAN('W', "weight", &record.opts.sample_weight,
162205484298SAndi Kleen 		    "sample by weight (on special events only)"),
1623475eeab9SAndi Kleen 	OPT_BOOLEAN(0, "transaction", &record.opts.sample_transaction,
1624475eeab9SAndi Kleen 		    "sample transaction flags (special events only)"),
16253aa5939dSAdrian Hunter 	OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
16263aa5939dSAdrian Hunter 		    "use per-thread mmaps"),
1627bcc84ec6SStephane Eranian 	OPT_CALLBACK_OPTARG('I', "intr-regs", &record.opts.sample_intr_regs, NULL, "any register",
1628bcc84ec6SStephane Eranian 		    "sample selected machine registers on interrupt,"
1629bcc84ec6SStephane Eranian 		    " use -I ? to list register names", parse_regs),
163085c273d2SAndi Kleen 	OPT_BOOLEAN(0, "running-time", &record.opts.running_time,
163185c273d2SAndi Kleen 		    "Record running/enabled time of read (:S) events"),
1632814c8c38SPeter Zijlstra 	OPT_CALLBACK('k', "clockid", &record.opts,
1633814c8c38SPeter Zijlstra 	"clockid", "clockid to use for events, see clock_gettime()",
1634814c8c38SPeter Zijlstra 	parse_clockid),
16352dd6d8a1SAdrian Hunter 	OPT_STRING_OPTARG('S', "snapshot", &record.opts.auxtrace_snapshot_opts,
16362dd6d8a1SAdrian Hunter 			  "opts", "AUX area tracing Snapshot Mode", ""),
16379d9cad76SKan Liang 	OPT_UINTEGER(0, "proc-map-timeout", &record.opts.proc_map_timeout,
16389d9cad76SKan Liang 			"per thread proc mmap processing timeout in ms"),
1639f3b3614aSHari Bathini 	OPT_BOOLEAN(0, "namespaces", &record.opts.record_namespaces,
1640f3b3614aSHari Bathini 		    "Record namespaces events"),
1641b757bb09SAdrian Hunter 	OPT_BOOLEAN(0, "switch-events", &record.opts.record_switch_events,
1642b757bb09SAdrian Hunter 		    "Record context switch events"),
164385723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-kernel", &record.opts.all_kernel,
164485723885SJiri Olsa 			 "Configure all used events to run in kernel space.",
164585723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
164685723885SJiri Olsa 	OPT_BOOLEAN_FLAG(0, "all-user", &record.opts.all_user,
164785723885SJiri Olsa 			 "Configure all used events to run in user space.",
164885723885SJiri Olsa 			 PARSE_OPT_EXCLUSIVE),
164971dc2326SWang Nan 	OPT_STRING(0, "clang-path", &llvm_param.clang_path, "clang path",
165071dc2326SWang Nan 		   "clang binary to use for compiling BPF scriptlets"),
165171dc2326SWang Nan 	OPT_STRING(0, "clang-opt", &llvm_param.clang_opt, "clang options",
165271dc2326SWang Nan 		   "options passed to clang when compiling BPF scriptlets"),
16537efe0e03SHe Kuang 	OPT_STRING(0, "vmlinux", &symbol_conf.vmlinux_name,
16547efe0e03SHe Kuang 		   "file", "vmlinux pathname"),
16556156681bSNamhyung Kim 	OPT_BOOLEAN(0, "buildid-all", &record.buildid_all,
16566156681bSNamhyung Kim 		    "Record build-id of all DSOs regardless of hits"),
1657ecfd7a9cSWang Nan 	OPT_BOOLEAN(0, "timestamp-filename", &record.timestamp_filename,
1658ecfd7a9cSWang Nan 		    "append timestamp to output filename"),
1659cb4e1ebbSJiri Olsa 	OPT_STRING_OPTARG_SET(0, "switch-output", &record.switch_output.str,
1660bfacbe3bSJiri Olsa 			  &record.switch_output.set, "signal,size,time",
1661bfacbe3bSJiri Olsa 			  "Switch output when receive SIGUSR2 or cross size,time threshold",
1662dc0c6127SJiri Olsa 			  "signal"),
16630aab2136SWang Nan 	OPT_BOOLEAN(0, "dry-run", &dry_run,
16640aab2136SWang Nan 		    "Parse options then exit"),
166586470930SIngo Molnar 	OPT_END()
166686470930SIngo Molnar };
166786470930SIngo Molnar 
1668e5b2c207SNamhyung Kim struct option *record_options = __record_options;
1669e5b2c207SNamhyung Kim 
16701d037ca1SIrina Tirdea int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
167186470930SIngo Molnar {
1672ef149c25SAdrian Hunter 	int err;
16738c6f45a7SArnaldo Carvalho de Melo 	struct record *rec = &record;
167416ad2ffbSNamhyung Kim 	char errbuf[BUFSIZ];
167586470930SIngo Molnar 
167648e1cab1SWang Nan #ifndef HAVE_LIBBPF_SUPPORT
167748e1cab1SWang Nan # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, "NO_LIBBPF=1", c)
167848e1cab1SWang Nan 	set_nobuild('\0', "clang-path", true);
167948e1cab1SWang Nan 	set_nobuild('\0', "clang-opt", true);
168048e1cab1SWang Nan # undef set_nobuild
168148e1cab1SWang Nan #endif
168248e1cab1SWang Nan 
16837efe0e03SHe Kuang #ifndef HAVE_BPF_PROLOGUE
16847efe0e03SHe Kuang # if !defined (HAVE_DWARF_SUPPORT)
16857efe0e03SHe Kuang #  define REASON  "NO_DWARF=1"
16867efe0e03SHe Kuang # elif !defined (HAVE_LIBBPF_SUPPORT)
16877efe0e03SHe Kuang #  define REASON  "NO_LIBBPF=1"
16887efe0e03SHe Kuang # else
16897efe0e03SHe Kuang #  define REASON  "this architecture doesn't support BPF prologue"
16907efe0e03SHe Kuang # endif
16917efe0e03SHe Kuang # define set_nobuild(s, l, c) set_option_nobuild(record_options, s, l, REASON, c)
16927efe0e03SHe Kuang 	set_nobuild('\0', "vmlinux", true);
16937efe0e03SHe Kuang # undef set_nobuild
16947efe0e03SHe Kuang # undef REASON
16957efe0e03SHe Kuang #endif
16967efe0e03SHe Kuang 
16973e2be2daSArnaldo Carvalho de Melo 	rec->evlist = perf_evlist__new();
16983e2be2daSArnaldo Carvalho de Melo 	if (rec->evlist == NULL)
1699361c99a6SArnaldo Carvalho de Melo 		return -ENOMEM;
1700361c99a6SArnaldo Carvalho de Melo 
1701ecc4c561SArnaldo Carvalho de Melo 	err = perf_config(perf_record_config, rec);
1702ecc4c561SArnaldo Carvalho de Melo 	if (err)
1703ecc4c561SArnaldo Carvalho de Melo 		return err;
1704eb853e80SJiri Olsa 
1705bca647aaSTom Zanussi 	argc = parse_options(argc, argv, record_options, record_usage,
1706a0541234SAnton Blanchard 			    PARSE_OPT_STOP_AT_NON_OPTION);
170768ba3235SNamhyung Kim 	if (quiet)
170868ba3235SNamhyung Kim 		perf_quiet_option();
1709483635a9SJiri Olsa 
1710483635a9SJiri Olsa 	/* Make system wide (-a) the default target. */
1711602ad878SArnaldo Carvalho de Melo 	if (!argc && target__none(&rec->opts.target))
1712483635a9SJiri Olsa 		rec->opts.target.system_wide = true;
171386470930SIngo Molnar 
1714bea03405SNamhyung Kim 	if (nr_cgroups && !rec->opts.target.system_wide) {
1715c7118369SNamhyung Kim 		usage_with_options_msg(record_usage, record_options,
1716c7118369SNamhyung Kim 			"cgroup monitoring only available in system-wide mode");
1717c7118369SNamhyung Kim 
1718023695d9SStephane Eranian 	}
1719b757bb09SAdrian Hunter 	if (rec->opts.record_switch_events &&
1720b757bb09SAdrian Hunter 	    !perf_can_record_switch_events()) {
1721c7118369SNamhyung Kim 		ui__error("kernel does not support recording context switch events\n");
1722c7118369SNamhyung Kim 		parse_options_usage(record_usage, record_options, "switch-events", 0);
1723c7118369SNamhyung Kim 		return -EINVAL;
1724b757bb09SAdrian Hunter 	}
1725023695d9SStephane Eranian 
1726cb4e1ebbSJiri Olsa 	if (switch_output_setup(rec)) {
1727cb4e1ebbSJiri Olsa 		parse_options_usage(record_usage, record_options, "switch-output", 0);
1728cb4e1ebbSJiri Olsa 		return -EINVAL;
1729cb4e1ebbSJiri Olsa 	}
1730cb4e1ebbSJiri Olsa 
1731bfacbe3bSJiri Olsa 	if (rec->switch_output.time) {
1732bfacbe3bSJiri Olsa 		signal(SIGALRM, alarm_sig_handler);
1733bfacbe3bSJiri Olsa 		alarm(rec->switch_output.time);
1734bfacbe3bSJiri Olsa 	}
1735bfacbe3bSJiri Olsa 
1736ef149c25SAdrian Hunter 	if (!rec->itr) {
1737ef149c25SAdrian Hunter 		rec->itr = auxtrace_record__init(rec->evlist, &err);
1738ef149c25SAdrian Hunter 		if (err)
17395c01ad60SAdrian Hunter 			goto out;
1740ef149c25SAdrian Hunter 	}
1741ef149c25SAdrian Hunter 
17422dd6d8a1SAdrian Hunter 	err = auxtrace_parse_snapshot_options(rec->itr, &rec->opts,
17432dd6d8a1SAdrian Hunter 					      rec->opts.auxtrace_snapshot_opts);
17442dd6d8a1SAdrian Hunter 	if (err)
17455c01ad60SAdrian Hunter 		goto out;
17462dd6d8a1SAdrian Hunter 
17471b36c03eSAdrian Hunter 	/*
17481b36c03eSAdrian Hunter 	 * Allow aliases to facilitate the lookup of symbols for address
17491b36c03eSAdrian Hunter 	 * filters. Refer to auxtrace_parse_filters().
17501b36c03eSAdrian Hunter 	 */
17511b36c03eSAdrian Hunter 	symbol_conf.allow_aliases = true;
17521b36c03eSAdrian Hunter 
17531b36c03eSAdrian Hunter 	symbol__init(NULL);
17541b36c03eSAdrian Hunter 
17551b36c03eSAdrian Hunter 	err = auxtrace_parse_filters(rec->evlist);
17561b36c03eSAdrian Hunter 	if (err)
17571b36c03eSAdrian Hunter 		goto out;
17581b36c03eSAdrian Hunter 
17590aab2136SWang Nan 	if (dry_run)
17605c01ad60SAdrian Hunter 		goto out;
17610aab2136SWang Nan 
1762d7888573SWang Nan 	err = bpf__setup_stdout(rec->evlist);
1763d7888573SWang Nan 	if (err) {
1764d7888573SWang Nan 		bpf__strerror_setup_stdout(rec->evlist, err, errbuf, sizeof(errbuf));
1765d7888573SWang Nan 		pr_err("ERROR: Setup BPF stdout failed: %s\n",
1766d7888573SWang Nan 			 errbuf);
17675c01ad60SAdrian Hunter 		goto out;
1768d7888573SWang Nan 	}
1769d7888573SWang Nan 
1770ef149c25SAdrian Hunter 	err = -ENOMEM;
1771ef149c25SAdrian Hunter 
1772ec80fde7SArnaldo Carvalho de Melo 	if (symbol_conf.kptr_restrict)
1773646aaea6SArnaldo Carvalho de Melo 		pr_warning(
1774646aaea6SArnaldo Carvalho de Melo "WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted,\n"
1775ec80fde7SArnaldo Carvalho de Melo "check /proc/sys/kernel/kptr_restrict.\n\n"
1776646aaea6SArnaldo Carvalho de Melo "Samples in kernel functions may not be resolved if a suitable vmlinux\n"
1777646aaea6SArnaldo Carvalho de Melo "file is not found in the buildid cache or in the vmlinux path.\n\n"
1778646aaea6SArnaldo Carvalho de Melo "Samples in kernel modules won't be resolved at all.\n\n"
1779646aaea6SArnaldo Carvalho de Melo "If some relocation was applied (e.g. kexec) symbols may be misresolved\n"
1780646aaea6SArnaldo Carvalho de Melo "even with a suitable vmlinux or kallsyms file.\n\n");
1781ec80fde7SArnaldo Carvalho de Melo 
17820c1d46a8SWang Nan 	if (rec->no_buildid_cache || rec->no_buildid) {
1783a1ac1d3cSStephane Eranian 		disable_buildid_cache();
1784dc0c6127SJiri Olsa 	} else if (rec->switch_output.enabled) {
17850c1d46a8SWang Nan 		/*
17860c1d46a8SWang Nan 		 * In 'perf record --switch-output', disable buildid
17870c1d46a8SWang Nan 		 * generation by default to reduce data file switching
17880c1d46a8SWang Nan 		 * overhead. Still generate buildid if they are required
17890c1d46a8SWang Nan 		 * explicitly using
17900c1d46a8SWang Nan 		 *
179160437ac0SJiri Olsa 		 *  perf record --switch-output --no-no-buildid \
17920c1d46a8SWang Nan 		 *              --no-no-buildid-cache
17930c1d46a8SWang Nan 		 *
17940c1d46a8SWang Nan 		 * Following code equals to:
17950c1d46a8SWang Nan 		 *
17960c1d46a8SWang Nan 		 * if ((rec->no_buildid || !rec->no_buildid_set) &&
17970c1d46a8SWang Nan 		 *     (rec->no_buildid_cache || !rec->no_buildid_cache_set))
17980c1d46a8SWang Nan 		 *         disable_buildid_cache();
17990c1d46a8SWang Nan 		 */
18000c1d46a8SWang Nan 		bool disable = true;
18010c1d46a8SWang Nan 
18020c1d46a8SWang Nan 		if (rec->no_buildid_set && !rec->no_buildid)
18030c1d46a8SWang Nan 			disable = false;
18040c1d46a8SWang Nan 		if (rec->no_buildid_cache_set && !rec->no_buildid_cache)
18050c1d46a8SWang Nan 			disable = false;
18060c1d46a8SWang Nan 		if (disable) {
18070c1d46a8SWang Nan 			rec->no_buildid = true;
18080c1d46a8SWang Nan 			rec->no_buildid_cache = true;
18090c1d46a8SWang Nan 			disable_buildid_cache();
18100c1d46a8SWang Nan 		}
18110c1d46a8SWang Nan 	}
1812655000e7SArnaldo Carvalho de Melo 
18134ea648aeSWang Nan 	if (record.opts.overwrite)
18144ea648aeSWang Nan 		record.opts.tail_synthesize = true;
18154ea648aeSWang Nan 
18163e2be2daSArnaldo Carvalho de Melo 	if (rec->evlist->nr_entries == 0 &&
18173e2be2daSArnaldo Carvalho de Melo 	    perf_evlist__add_default(rec->evlist) < 0) {
181869aad6f1SArnaldo Carvalho de Melo 		pr_err("Not enough memory for event selector list\n");
1819394c01edSAdrian Hunter 		goto out;
1820bbd36e5eSPeter Zijlstra 	}
182186470930SIngo Molnar 
182269e7e5b0SAdrian Hunter 	if (rec->opts.target.tid && !rec->opts.no_inherit_set)
182369e7e5b0SAdrian Hunter 		rec->opts.no_inherit = true;
182469e7e5b0SAdrian Hunter 
1825602ad878SArnaldo Carvalho de Melo 	err = target__validate(&rec->opts.target);
182616ad2ffbSNamhyung Kim 	if (err) {
1827602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
182816ad2ffbSNamhyung Kim 		ui__warning("%s", errbuf);
182916ad2ffbSNamhyung Kim 	}
18304bd0f2d2SNamhyung Kim 
1831602ad878SArnaldo Carvalho de Melo 	err = target__parse_uid(&rec->opts.target);
183216ad2ffbSNamhyung Kim 	if (err) {
183316ad2ffbSNamhyung Kim 		int saved_errno = errno;
183416ad2ffbSNamhyung Kim 
1835602ad878SArnaldo Carvalho de Melo 		target__strerror(&rec->opts.target, err, errbuf, BUFSIZ);
18363780f488SNamhyung Kim 		ui__error("%s", errbuf);
183716ad2ffbSNamhyung Kim 
183816ad2ffbSNamhyung Kim 		err = -saved_errno;
1839394c01edSAdrian Hunter 		goto out;
184016ad2ffbSNamhyung Kim 	}
18410d37aa34SArnaldo Carvalho de Melo 
184223dc4f15SJiri Olsa 	/* Enable ignoring missing threads when -u option is defined. */
184323dc4f15SJiri Olsa 	rec->opts.ignore_missing_thread = rec->opts.target.uid != UINT_MAX;
184423dc4f15SJiri Olsa 
184516ad2ffbSNamhyung Kim 	err = -ENOMEM;
18463e2be2daSArnaldo Carvalho de Melo 	if (perf_evlist__create_maps(rec->evlist, &rec->opts.target) < 0)
1847dd7927f4SArnaldo Carvalho de Melo 		usage_with_options(record_usage, record_options);
184869aad6f1SArnaldo Carvalho de Melo 
1849ef149c25SAdrian Hunter 	err = auxtrace_record__options(rec->itr, rec->evlist, &rec->opts);
1850ef149c25SAdrian Hunter 	if (err)
1851394c01edSAdrian Hunter 		goto out;
1852ef149c25SAdrian Hunter 
18536156681bSNamhyung Kim 	/*
18546156681bSNamhyung Kim 	 * We take all buildids when the file contains
18556156681bSNamhyung Kim 	 * AUX area tracing data because we do not decode the
18566156681bSNamhyung Kim 	 * trace because it would take too long.
18576156681bSNamhyung Kim 	 */
18586156681bSNamhyung Kim 	if (rec->opts.full_auxtrace)
18596156681bSNamhyung Kim 		rec->buildid_all = true;
18606156681bSNamhyung Kim 
1861b4006796SArnaldo Carvalho de Melo 	if (record_opts__config(&rec->opts)) {
186239d17dacSArnaldo Carvalho de Melo 		err = -EINVAL;
1863394c01edSAdrian Hunter 		goto out;
18647e4ff9e3SMike Galbraith 	}
18657e4ff9e3SMike Galbraith 
1866d20deb64SArnaldo Carvalho de Melo 	err = __cmd_record(&record, argc, argv);
1867394c01edSAdrian Hunter out:
186845604710SNamhyung Kim 	perf_evlist__delete(rec->evlist);
1869d65a458bSArnaldo Carvalho de Melo 	symbol__exit();
1870ef149c25SAdrian Hunter 	auxtrace_record__free(rec->itr);
187139d17dacSArnaldo Carvalho de Melo 	return err;
187286470930SIngo Molnar }
18732dd6d8a1SAdrian Hunter 
18742dd6d8a1SAdrian Hunter static void snapshot_sig_handler(int sig __maybe_unused)
18752dd6d8a1SAdrian Hunter {
1876dc0c6127SJiri Olsa 	struct record *rec = &record;
1877dc0c6127SJiri Olsa 
18785f9cf599SWang Nan 	if (trigger_is_ready(&auxtrace_snapshot_trigger)) {
18795f9cf599SWang Nan 		trigger_hit(&auxtrace_snapshot_trigger);
18802dd6d8a1SAdrian Hunter 		auxtrace_record__snapshot_started = 1;
18815f9cf599SWang Nan 		if (auxtrace_record__snapshot_start(record.itr))
18825f9cf599SWang Nan 			trigger_error(&auxtrace_snapshot_trigger);
18835f9cf599SWang Nan 	}
18843c1cb7e3SWang Nan 
1885dc0c6127SJiri Olsa 	if (switch_output_signal(rec))
18863c1cb7e3SWang Nan 		trigger_hit(&switch_output_trigger);
18872dd6d8a1SAdrian Hunter }
1888bfacbe3bSJiri Olsa 
1889bfacbe3bSJiri Olsa static void alarm_sig_handler(int sig __maybe_unused)
1890bfacbe3bSJiri Olsa {
1891bfacbe3bSJiri Olsa 	struct record *rec = &record;
1892bfacbe3bSJiri Olsa 
1893bfacbe3bSJiri Olsa 	if (switch_output_time(rec))
1894bfacbe3bSJiri Olsa 		trigger_hit(&switch_output_trigger);
1895bfacbe3bSJiri Olsa }
1896