Skip to content

Commit 9cbdb70

Browse files
David Ahernacmel
authored andcommitted
perf script: improve validation of sample attributes for output fields
Check for required sample attributes using evsel rather than sample_type in the session header. If the attribute for a default field is not present for the event type (e.g., new command operating on file from older kernel) the field is removed from the output list. Expected event types must exist. For example, if a user specifies -f trace:time,trace -f sw:time,cpu,sym the perf.data file must contain both tracepoints and software events (ie., it is an error if either does not exist in the file). Attribute checking is done once at the beginning of perf-script rather than for each sample. v1 -> v2: - addressed comments from acme Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1302148460-570-1-git-send-email-daahern@cisco.com Signed-off-by: David Ahern <daahern@cisco.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 0817a6a commit 9cbdb70

3 files changed

Lines changed: 109 additions & 17 deletions

File tree

‎tools/perf/builtin-script.c‎

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ struct output_option {
5151
/* default set to maintain compatibility with current format */
5252
static struct {
5353
bool user_set;
54+
bool wildcard_set;
5455
u64 fields;
5556
u64 invalid_fields;
5657
} output[PERF_TYPE_MAX] = {
@@ -104,41 +105,113 @@ static bool output_set_by_user(void)
104105
return false;
105106
}
106107

108+
static const char *output_field2str(enum perf_output_field field)
109+
{
110+
int i, imax = ARRAY_SIZE(all_output_options);
111+
const char *str = "";
112+
113+
for (i = 0; i < imax; ++i) {
114+
if (all_output_options[i].field == field) {
115+
str = all_output_options[i].str;
116+
break;
117+
}
118+
}
119+
return str;
120+
}
121+
107122
#define PRINT_FIELD(x) (output[attr->type].fields & PERF_OUTPUT_##x)
108123

109-
static int perf_session__check_attr(struct perf_session *session,
110-
struct perf_event_attr *attr)
124+
static int perf_event_attr__check_stype(struct perf_event_attr *attr,
125+
u64 sample_type, const char *sample_msg,
126+
enum perf_output_field field)
111127
{
128+
int type = attr->type;
129+
const char *evname;
130+
131+
if (attr->sample_type & sample_type)
132+
return 0;
133+
134+
if (output[type].user_set) {
135+
evname = __event_name(attr->type, attr->config);
136+
pr_err("Samples for '%s' event do not have %s attribute set. "
137+
"Cannot print '%s' field.\n",
138+
evname, sample_msg, output_field2str(field));
139+
return -1;
140+
}
141+
142+
/* user did not ask for it explicitly so remove from the default list */
143+
output[type].fields &= ~field;
144+
evname = __event_name(attr->type, attr->config);
145+
pr_debug("Samples for '%s' event do not have %s attribute set. "
146+
"Skipping '%s' field.\n",
147+
evname, sample_msg, output_field2str(field));
148+
149+
return 0;
150+
}
151+
152+
static int perf_evsel__check_attr(struct perf_evsel *evsel,
153+
struct perf_session *session)
154+
{
155+
struct perf_event_attr *attr = &evsel->attr;
156+
112157
if (PRINT_FIELD(TRACE) &&
113158
!perf_session__has_traces(session, "record -R"))
114159
return -EINVAL;
115160

116161
if (PRINT_FIELD(SYM)) {
117-
if (!(session->sample_type & PERF_SAMPLE_IP)) {
118-
pr_err("Samples do not contain IP data.\n");
162+
if (perf_event_attr__check_stype(attr, PERF_SAMPLE_IP, "IP",
163+
PERF_OUTPUT_SYM))
119164
return -EINVAL;
120-
}
165+
121166
if (!no_callchain &&
122-
!(session->sample_type & PERF_SAMPLE_CALLCHAIN))
167+
!(attr->sample_type & PERF_SAMPLE_CALLCHAIN))
123168
symbol_conf.use_callchain = false;
124169
}
125170

126171
if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
127-
!(session->sample_type & PERF_SAMPLE_TID)) {
128-
pr_err("Samples do not contain TID/PID data.\n");
172+
perf_event_attr__check_stype(attr, PERF_SAMPLE_TID, "TID",
173+
PERF_OUTPUT_TID|PERF_OUTPUT_PID))
129174
return -EINVAL;
130-
}
131175

132176
if (PRINT_FIELD(TIME) &&
133-
!(session->sample_type & PERF_SAMPLE_TIME)) {
134-
pr_err("Samples do not contain timestamps.\n");
177+
perf_event_attr__check_stype(attr, PERF_SAMPLE_TIME, "TIME",
178+
PERF_OUTPUT_TIME))
135179
return -EINVAL;
136-
}
137180

138181
if (PRINT_FIELD(CPU) &&
139-
!(session->sample_type & PERF_SAMPLE_CPU)) {
140-
pr_err("Samples do not contain cpu.\n");
182+
perf_event_attr__check_stype(attr, PERF_SAMPLE_CPU, "CPU",
183+
PERF_OUTPUT_CPU))
141184
return -EINVAL;
185+
186+
return 0;
187+
}
188+
189+
/*
190+
* verify all user requested events exist and the samples
191+
* have the expected data
192+
*/
193+
static int perf_session__check_output_opt(struct perf_session *session)
194+
{
195+
int j;
196+
struct perf_evsel *evsel;
197+
198+
for (j = 0; j < PERF_TYPE_MAX; ++j) {
199+
evsel = perf_session__find_first_evtype(session, j);
200+
201+
/*
202+
* even if fields is set to 0 (ie., show nothing) event must
203+
* exist if user explicitly includes it on the command line
204+
*/
205+
if (!evsel && output[j].user_set && !output[j].wildcard_set) {
206+
pr_err("%s events do not exist. "
207+
"Remove corresponding -f option to proceed.\n",
208+
event_type(j));
209+
return -1;
210+
}
211+
212+
if (evsel && output[j].fields &&
213+
perf_evsel__check_attr(evsel, session))
214+
return -1;
142215
}
143216

144217
return 0;
@@ -210,9 +283,6 @@ static void process_event(union perf_event *event __unused,
210283
if (output[attr->type].fields == 0)
211284
return;
212285

213-
if (perf_session__check_attr(session, attr) < 0)
214-
return;
215-
216286
print_sample_start(sample, thread, attr);
217287

218288
if (PRINT_FIELD(TRACE))
@@ -525,6 +595,7 @@ static int parse_output_fields(const struct option *opt __used,
525595

526596
output[type].fields = 0;
527597
output[type].user_set = true;
598+
output[type].wildcard_set = false;
528599

529600
} else {
530601
tok = str;
@@ -541,6 +612,7 @@ static int parse_output_fields(const struct option *opt __used,
541612
for (j = 0; j < PERF_TYPE_MAX; ++j) {
542613
output[j].fields = 0;
543614
output[j].user_set = true;
615+
output[j].wildcard_set = true;
544616
}
545617
}
546618

@@ -1145,6 +1217,11 @@ int cmd_script(int argc, const char **argv, const char *prefix __used)
11451217
pr_debug("perf script started with script %s\n\n", script_name);
11461218
}
11471219

1220+
1221+
err = perf_session__check_output_opt(session);
1222+
if (err < 0)
1223+
goto out;
1224+
11481225
err = __cmd_script(session);
11491226

11501227
perf_session__delete(session);

‎tools/perf/util/session.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,18 @@ size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
11561156
return ret;
11571157
}
11581158

1159+
struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1160+
unsigned int type)
1161+
{
1162+
struct perf_evsel *pos;
1163+
1164+
list_for_each_entry(pos, &session->evlist->entries, node) {
1165+
if (pos->attr.type == type)
1166+
return pos;
1167+
}
1168+
return NULL;
1169+
}
1170+
11591171
void perf_session__print_symbols(union perf_event *event,
11601172
struct perf_sample *sample,
11611173
struct perf_session *session)

‎tools/perf/util/session.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ static inline int perf_session__parse_sample(struct perf_session *session,
162162
session->sample_id_all, sample);
163163
}
164164

165+
struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
166+
unsigned int type);
167+
165168
void perf_session__print_symbols(union perf_event *event,
166169
struct perf_sample *sample,
167170
struct perf_session *session);

0 commit comments

Comments
 (0)