Skip to content

Commit f766148

Browse files
olsajirigregkh
authored andcommitted
perf record: Fix crash in pipe mode
[ Upstream commit ad46e48 ] Currently we can crash perf record when running in pipe mode, like: $ perf record ls | perf report # To display the perf.data header info, please use --header/--header-only options. # perf: Segmentation fault Error: The - file has no samples! The callstack of the crash is: 0x0000000000515242 in perf_event__synthesize_event_update_name 3513 ev = event_update_event__new(len + 1, PERF_EVENT_UPDATE__NAME, evsel->id[0]); (gdb) bt #0 0x0000000000515242 in perf_event__synthesize_event_update_name #1 0x00000000005158a4 in perf_event__synthesize_extra_attr #2 0x0000000000443347 in record__synthesize #3 0x00000000004438e3 in __cmd_record #4 0x000000000044514e in cmd_record #5 0x00000000004cbc95 in run_builtin #6 0x00000000004cbf02 in handle_internal_command #7 0x00000000004cc054 in run_argv #8 0x00000000004cc422 in main The reason of the crash is that the evsel does not have ids array allocated and the pipe's synthesize code tries to access it. We don't force evsel ids allocation when we have single event, because it's not needed. However we need it when we are in pipe mode even for single event as a key for evsel update event. Fixing this by forcing evsel ids allocation event for single event, when we are in pipe mode. Signed-off-by: Jiri Olsa <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: David Ahern <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 8997115 commit f766148

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

tools/perf/builtin-record.c

+9
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,15 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
926926
}
927927
}
928928

929+
/*
930+
* If we have just single event and are sending data
931+
* through pipe, we need to force the ids allocation,
932+
* because we synthesize event name through the pipe
933+
* and need the id for that.
934+
*/
935+
if (data->is_pipe && rec->evlist->nr_entries == 1)
936+
rec->opts.sample_id = true;
937+
929938
if (record__open(rec) != 0) {
930939
err = -1;
931940
goto out_child;

tools/perf/perf.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct record_opts {
6161
bool tail_synthesize;
6262
bool overwrite;
6363
bool ignore_missing_thread;
64+
bool sample_id;
6465
unsigned int freq;
6566
unsigned int mmap_pages;
6667
unsigned int auxtrace_mmap_pages;

tools/perf/util/record.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
137137
struct perf_evsel *evsel;
138138
bool use_sample_identifier = false;
139139
bool use_comm_exec;
140+
bool sample_id = opts->sample_id;
140141

141142
/*
142143
* Set the evsel leader links before we configure attributes,
@@ -163,8 +164,7 @@ void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
163164
* match the id.
164165
*/
165166
use_sample_identifier = perf_can_sample_identifier();
166-
evlist__for_each_entry(evlist, evsel)
167-
perf_evsel__set_sample_id(evsel, use_sample_identifier);
167+
sample_id = true;
168168
} else if (evlist->nr_entries > 1) {
169169
struct perf_evsel *first = perf_evlist__first(evlist);
170170

@@ -174,6 +174,10 @@ void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
174174
use_sample_identifier = perf_can_sample_identifier();
175175
break;
176176
}
177+
sample_id = true;
178+
}
179+
180+
if (sample_id) {
177181
evlist__for_each_entry(evlist, evsel)
178182
perf_evsel__set_sample_id(evsel, use_sample_identifier);
179183
}

0 commit comments

Comments
 (0)