Skip to content

Commit ffeb883

Browse files
He Kuangacmel
authored andcommitted
perf tools: Show proper error message for wrong terms of hw/sw events
Show proper error message and show valid terms when wrong config terms is specified for hw/sw type perf events. This patch makes the original error format function formats_error_string() more generic, which only outputs the static config terms for hw/sw perf events, and prepends pmu formats for pmu events. Before this patch: $ perf record -e 'cpu-clock/freqx=200/' -a sleep 1 invalid or unsupported event: 'cpu-clock/freqx=200/' Run 'perf list' for a list of valid events usage: perf record [<options>] [<command>] or: perf record [<options>] -- <command> [<options>] -e, --event <event> event selector. use 'perf list' to list available events After this patch: $ perf record -e 'cpu-clock/freqx=200/' -a sleep 1 event syntax error: 'cpu-clock/freqx=200/' \___ unknown term valid terms: config,config1,config2,name,period,freq,branch_type,time,call-graph,stack-size Run 'perf list' for a list of valid events usage: perf record [<options>] [<command>] or: perf record [<options>] -- <command> [<options>] -e, --event <event> event selector. use 'perf list' to list available events Signed-off-by: He Kuang <hekuang@huawei.com> Acked-by: Jiri Olsa <jolsa@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Kan Liang <kan.liang@intel.com> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Wang Nan <wangnan0@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1443412336-120050-2-git-send-email-hekuang@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 0b8891a commit ffeb883

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

tools/perf/util/parse-events.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ do { \
656656
CHECK_TYPE_VAL(STR);
657657
break;
658658
default:
659+
err->str = strdup("unknown term");
660+
err->idx = term->err_term;
661+
err->help = parse_events_formats_error_string(NULL);
659662
return -EINVAL;
660663
}
661664

@@ -1875,3 +1878,29 @@ void parse_events_evlist_error(struct parse_events_evlist *data,
18751878
err->str = strdup(str);
18761879
WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
18771880
}
1881+
1882+
/*
1883+
* Return string contains valid config terms of an event.
1884+
* @additional_terms: For terms such as PMU sysfs terms.
1885+
*/
1886+
char *parse_events_formats_error_string(char *additional_terms)
1887+
{
1888+
char *str;
1889+
static const char *static_terms = "config,config1,config2,name,"
1890+
"period,freq,branch_type,time,"
1891+
"call-graph,stack-size\n";
1892+
1893+
/* valid terms */
1894+
if (additional_terms) {
1895+
if (!asprintf(&str, "valid terms: %s,%s",
1896+
additional_terms, static_terms))
1897+
goto fail;
1898+
} else {
1899+
if (!asprintf(&str, "valid terms: %s", static_terms))
1900+
goto fail;
1901+
}
1902+
return str;
1903+
1904+
fail:
1905+
return NULL;
1906+
}

tools/perf/util/parse-events.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,6 @@ int print_hwcache_events(const char *event_glob, bool name_only);
156156
extern int is_valid_tracepoint(const char *event_string);
157157

158158
int valid_event_mount(const char *eventfs);
159+
char *parse_events_formats_error_string(char *additional_terms);
159160

160161
#endif /* __PERF_PARSE_EVENTS_H */

tools/perf/util/parse-events.l

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ modifier_bp [rwx]{1,3}
174174

175175
<config>{
176176
/*
177-
* Please update formats_error_string any time
177+
* Please update parse_events_formats_error_string any time
178178
* new static term is added.
179179
*/
180180
config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }

tools/perf/util/pmu.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -626,38 +626,26 @@ static int pmu_resolve_param_term(struct parse_events_term *term,
626626
return -1;
627627
}
628628

629-
static char *formats_error_string(struct list_head *formats)
629+
static char *pmu_formats_string(struct list_head *formats)
630630
{
631631
struct perf_pmu_format *format;
632-
char *err, *str;
633-
static const char *static_terms = "config,config1,config2,name,"
634-
"period,freq,branch_type,time,"
635-
"call-graph,stack-size\n";
632+
char *str;
633+
struct strbuf buf;
636634
unsigned i = 0;
637635

638-
if (!asprintf(&str, "valid terms:"))
636+
if (!formats)
639637
return NULL;
640638

639+
strbuf_init(&buf, 0);
641640
/* sysfs exported terms */
642-
list_for_each_entry(format, formats, list) {
643-
char c = i++ ? ',' : ' ';
644-
645-
err = str;
646-
if (!asprintf(&str, "%s%c%s", err, c, format->name))
647-
goto fail;
648-
free(err);
649-
}
641+
list_for_each_entry(format, formats, list)
642+
strbuf_addf(&buf, i++ ? ",%s" : "%s",
643+
format->name);
650644

651-
/* static terms */
652-
err = str;
653-
if (!asprintf(&str, "%s,%s", err, static_terms))
654-
goto fail;
645+
str = strbuf_detach(&buf, NULL);
646+
strbuf_release(&buf);
655647

656-
free(err);
657648
return str;
658-
fail:
659-
free(err);
660-
return NULL;
661649
}
662650

663651
/*
@@ -693,9 +681,12 @@ static int pmu_config_term(struct list_head *formats,
693681
if (verbose)
694682
printf("Invalid event/parameter '%s'\n", term->config);
695683
if (err) {
684+
char *pmu_term = pmu_formats_string(formats);
685+
696686
err->idx = term->err_term;
697687
err->str = strdup("unknown term");
698-
err->help = formats_error_string(formats);
688+
err->help = parse_events_formats_error_string(pmu_term);
689+
free(pmu_term);
699690
}
700691
return -EINVAL;
701692
}

0 commit comments

Comments
 (0)