Skip to content

Commit 22ca2f7

Browse files
captain5050acmel
authored andcommitted
perf script: Allow the generated script to be a path
Allow the script generated by "perf script -g <language>" to be a file path and the language determined by the file extension. This is useful in testing so that the generated script file can be written to a test directory. Committer testing: $ perf record ls a.a ls: cannot access 'a.a': No such file or directory [ perf record: Woken up 2 times to write data ] [ perf record: Captured and wrote 0.003 MB perf.data (7 samples) ] $ perf script -g python generated Python script: perf-script.py $ perf script -g myscript.py generated Python script: myscript.py $ diff -u perf-script.py myscript.py $ tail myscript.py def trace_unhandled(event_name, context, event_fields_dict, perf_sample_dict): print(get_dict_as_string(event_fields_dict)) print('Sample: {'+get_dict_as_string(perf_sample_dict['sample'], ', ')+'}') def print_header(event_name, cpu, secs, nsecs, pid, comm): print("%-20s %5u %05u.%09u %8u %-20s " % \ (event_name, cpu, secs, nsecs, pid, comm), end="") def get_dict_as_string(a_dict, delimiter=' '): return delimiter.join(['%s=%s'%(k,str(v))for k,v in sorted(a_dict.items())]) $ Signed-off-by: Ian Rogers <irogers@google.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@linaro.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Leo Yan <leo.yan@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Sandipan Das <sandipan.das@amd.com> Cc: Yujie Liu <yujie.liu@intel.com> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 9083ce5 commit 22ca2f7

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

tools/perf/Documentation/perf-script.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ OPTIONS
9898

9999
-g::
100100
--gen-script=::
101-
Generate perf-script.[ext] starter script for given language,
102-
using current perf.data.
101+
Generate a starter script. If a language is given then the
102+
script is named perf-script.[ext] according to the
103+
language. If a file path is given then python is used for
104+
files ending '.py' and perl used for files ending '.pl'.
103105

104106
--dlfilter=<file>::
105107
Filter sample events using the given shared object file.

tools/perf/builtin-script.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4489,6 +4489,7 @@ int cmd_script(int argc, const char **argv)
44894489
if (generate_script_lang) {
44904490
struct stat perf_stat;
44914491
int input;
4492+
char *filename = strdup("perf-script");
44924493

44934494
if (output_set_by_user()) {
44944495
fprintf(stderr,
@@ -4516,17 +4517,32 @@ int cmd_script(int argc, const char **argv)
45164517
}
45174518

45184519
scripting_ops = script_spec__lookup(generate_script_lang);
4520+
if (!scripting_ops && ends_with(generate_script_lang, ".py")) {
4521+
scripting_ops = script_spec__lookup("python");
4522+
free(filename);
4523+
filename = strdup(generate_script_lang);
4524+
filename[strlen(filename) - 3] = '\0';
4525+
} else if (!scripting_ops && ends_with(generate_script_lang, ".pl")) {
4526+
scripting_ops = script_spec__lookup("perl");
4527+
free(filename);
4528+
filename = strdup(generate_script_lang);
4529+
filename[strlen(filename) - 3] = '\0';
4530+
}
45194531
if (!scripting_ops) {
4520-
fprintf(stderr, "invalid language specifier");
4532+
fprintf(stderr, "invalid language specifier '%s'\n", generate_script_lang);
45214533
err = -ENOENT;
45224534
goto out_delete;
45234535
}
4536+
if (!filename) {
4537+
err = -ENOMEM;
4538+
goto out_delete;
4539+
}
45244540
#ifdef HAVE_LIBTRACEEVENT
4525-
err = scripting_ops->generate_script(session->tevent.pevent,
4526-
"perf-script");
4541+
err = scripting_ops->generate_script(session->tevent.pevent, filename);
45274542
#else
4528-
err = scripting_ops->generate_script(NULL, "perf-script");
4543+
err = scripting_ops->generate_script(NULL, filename);
45294544
#endif
4545+
free(filename);
45304546
goto out_delete;
45314547
}
45324548

0 commit comments

Comments
 (0)