Skip to contentSkip to conversation

perf script: add support for dumping events other than trace

PATCH Superseded David Ahern 2 participants 6 messages Mar 6, 2011 – Mar 7, 2011 perf events

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover perf script: add support for dumping events other than trace

v1 -> v2:
- fixed process_event handler in trace-event-perl
- removed unneeded call to callchain_register_param
- corrected walking of callchains in perf_session__print_symbols
- complete rewrite for handling non-tracepoint events

David Ahern (2):
perf script: change process_event prototype perf script: support custom field selection for output

 tools/perf/Documentation/perf-script.txt           |   24 ++
 tools/perf/builtin-script.c                        |  234 ++++++++++++++++++--
 tools/perf/util/output.h                           |   18 ++
 .../perf/util/scripting-engines/trace-event-perl.c |   12 +-
 .../util/scripting-engines/trace-event-python.c    |   12 +-
 tools/perf/util/session.c                          |   75 +++++++
 tools/perf/util/session.h                          |    5 +
 tools/perf/util/trace-event-parse.c                |   71 +++++--
 tools/perf/util/trace-event-scripting.c            |   10 +-
 tools/perf/util/trace-event.h                      |    8 +-
 10 files changed, 420 insertions(+), 49 deletions(-)
 create mode 100644 tools/perf/util/output.h
Signature · 1 line
1.7.4
#1 lore raw

PATCH v2 1/2 perf script: change process_event prototype

Prepare for handling of samples for any event type.

---
 tools/perf/builtin-script.c                        |   35 ++++++++++++++-----
 .../perf/util/scripting-engines/trace-event-perl.c |   12 +++++--
 .../util/scripting-engines/trace-event-python.c    |   12 +++++--
 tools/perf/util/trace-event-scripting.c            |   10 +++---
 tools/perf/util/trace-event.h                      |    8 +++-
 5 files changed, 55 insertions(+), 22 deletions(-)
tools/perf/builtin-script.c +26 -9
index 5f40df6..48bdf94 100644
@@ -20,6 +20,21 @@ static u64			last_timestamp;
 static u64			nr_unordered;
 extern const struct option	record_options[];
 
+static void process_event(union perf_event *event __unused,
+			  struct perf_sample *sample,
+			  struct perf_session *session __unused,
+			  struct perf_event_attr *attr __unused,
+			  struct thread *thread)
+{
+	/*
+	 * FIXME: better resolve from pid from the struct trace_entry
+	 * field, although it should be the same than this perf
+	 * event pid
+	 */
+	print_event(sample->cpu, sample->raw_data, sample->raw_size,
+		    sample->time, thread->comm);
+}
+
 static int default_start_script(const char *script __unused,
 				int argc __unused,
 				const char **argv __unused)
@@ -40,7 +55,7 @@ static int default_generate_script(const char *outfile __unused)
 static struct scripting_ops default_scripting_ops = {
 	.start_script		= default_start_script,
 	.stop_script		= default_stop_script,
-	.process_event		= print_event,
+	.process_event		= process_event,
 	.generate_script	= default_generate_script,
 };
 
@@ -67,6 +82,7 @@ static int process_sample_event(union perf_event *event,
 				struct perf_sample *sample,
 				struct perf_session *session)
 {
+	struct perf_event_attr *attr;
 	struct thread *thread = perf_session__findnew(session, event->ip.pid);
 
 	if (thread == NULL) {
@@ -75,6 +91,13 @@ static int process_sample_event(union perf_event *event,
 		return -1;
 	}
 
+	attr = perf_header__find_attr(sample->id, &session->header);
+	if (!attr) {
+		pr_debug("No attributes found for sample id %" PRId64 ". "
+			 "skipping it.\n", sample->id);
+		return -1;
+	}
+
 	if (session->sample_type & PERF_SAMPLE_RAW) {
 		if (debug_mode) {
 			if (sample->time < last_timestamp) {
@@ -86,14 +109,8 @@ static int process_sample_event(union perf_event *event,
 			last_timestamp = sample->time;
 			return 0;
 		}
-		/*
-		 * FIXME: better resolve from pid from the struct trace_entry
-		 * field, although it should be the same than this perf
-		 * event pid
-		 */
-		scripting_ops->process_event(sample->cpu, sample->raw_data,
-					     sample->raw_size,
-					     sample->time, thread->comm);
+		scripting_ops->process_event(event, sample, session,
+					     attr, thread);
 	}
 
 	session->hists.stats.total_period += sample->period;
tools/perf/util/scripting-engines/trace-event-perl.c +9 -3
index 9368081..ed69d4c 100644
@@ -245,9 +245,11 @@ static inline struct event *find_cache_event(int type)
 	return event;
 }
 
-static void perl_process_event(int cpu, void *data,
-			       int size __unused,
-			       unsigned long long nsecs, char *comm)
+static void perl_process_event(union perf_event *pevent __unused,
+			       struct perf_sample *sample,
+			       struct perf_session *session __unused,
+			       struct perf_event_attr *attr __unused,
+			       struct thread *thread)
 {
 	struct format_field *field;
 	static char handler[256];
@@ -256,6 +258,10 @@ static void perl_process_event(int cpu, void *data,
 	struct event *event;
 	int type;
 	int pid;
+	int cpu = sample->cpu;
+	void *data = sample->raw_data;
+	unsigned long long nsecs = sample->time;
+	char *comm = thread->comm;
 
 	dSP;
 
tools/perf/util/scripting-engines/trace-event-python.c +9 -3
index 2040b85..9fded61 100644
@@ -204,9 +204,11 @@ static inline struct event *find_cache_event(int type)
 	return event;
 }
 
-static void python_process_event(int cpu, void *data,
-				 int size __unused,
-				 unsigned long long nsecs, char *comm)
+static void python_process_event(union perf_event *pevent __unused,
+				 struct perf_sample *sample,
+				 struct perf_session *session __unused,
+				 struct perf_event_attr *attr __unused,
+				 struct thread *thread)
 {
 	PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
 	static char handler_name[256];
@@ -217,6 +219,10 @@ static void python_process_event(int cpu, void *data,
 	unsigned n = 0;
 	int type;
 	int pid;
+	int cpu = sample->cpu;
+	void *data = sample->raw_data;
+	unsigned long long nsecs = sample->time;
+	char *comm = thread->comm;
 
 	t = PyTuple_New(MAX_FIELDS);
 	if (!t)
tools/perf/util/trace-event-scripting.c +5 -5
index f7af2fc..6f91d3c 100644
@@ -36,11 +36,11 @@ static int stop_script_unsupported(void)
 	return 0;
 }
 
-static void process_event_unsupported(int cpu __unused,
-				      void *data __unused,
-				      int size __unused,
-				      unsigned long long nsecs __unused,
-				      char *comm __unused)
+static void process_event_unsupported(union perf_event *event __unused,
+				      struct perf_sample *sample __unused,
+				      struct perf_session *session __unused,
+				      struct perf_event_attr *attr __unused,
+				      struct thread *thread __unused)
 {
 }
 
tools/perf/util/trace-event.h +6 -2
index b5f12ca..b4b939d 100644
@@ -3,6 +3,7 @@
 
 #include <stdbool.h>
 #include "parse-events.h"
+#include "session.h"
 
 #define __unused __attribute__((unused))
 
@@ -278,8 +279,11 @@ struct scripting_ops {
 	const char *name;
 	int (*start_script) (const char *script, int argc, const char **argv);
 	int (*stop_script) (void);
-	void (*process_event) (int cpu, void *data, int size,
-			       unsigned long long nsecs, char *comm);
+	void (*process_event) (union perf_event *event,
+			       struct perf_sample *sample,
+			       struct perf_session *session,
+			       struct perf_event_attr *attr,
+			       struct thread *thread);
 	int (*generate_script) (const char *outfile);
 };
 
Signature · 1 line
1.7.4
#2 lore raw

PATCH v2 2/2 perf script: support custom field selection for output

In reply to David Ahern

Allow a user to select which fields to print to stdout for event data. Options include comm (command name), tid (thread id), pid (process id), time (perf timestamp), cpu, event (for event name), trace (for trace data), and sym (for IP, symbol name and DSO name).

This options allows dumping software events with symbols -- my primary goal -- and also symbols for trace events if a so desired.

Default is set to maintain compatibility with current output.

Thanks to Frederic Weisbecker for detailed suggestions on this approach.

Examples (output compressed)

1. trace, default format

perf record -ga -e sched:sched_switch
perf script

swapper-0 [000]  534.955086: sched_switch: prev_comm=swapper prev_pid=0 ...
sshd-1678 [000]  534.955093: sched_switch: prev_comm=sshd prev_pid=1678 ...
sshd-1675 [001]  534.960353: sched_switch: prev_comm=sshd prev_pid=1675 ...

2. trace, custom format

perf record -ga -e sched:sched_switch
perf script -f comm,pid,time,trace,sym

swapper-0 534.955086: prev_comm=swapper prev_pid=0 prev_prio=120 ...

        ffffffff81030350 perf_trace_sched_switch ([kernel.kallsyms])
        ffffffff81382ac5 schedule ([kernel.kallsyms])
        ffffffff8100134a cpu_idle ([kernel.kallsyms])
        ffffffff81370b39 rest_init ([kernel.kallsyms])
        ffffffff81696c23 start_kernel ([kernel.kallsyms].init.text)
        ffffffff816962af x86_64_start_reservations ([kernel.kallsyms].init.text)
        ffffffff816963b9 x86_64_start_kernel ([kernel.kallsyms].init.text)

sshd-1678 534.955093: prev_comm=sshd prev_pid=1678 prev_prio=120 ...
ffffffff81030350 perf_trace_sched_switch ([kernel.kallsyms]) ffffffff81382ac5 schedule ([kernel.kallsyms]) ffffffff81382fa0 schedule_timeout ([kernel.kallsyms]) ffffffff81363f16 unix_stream_recvmsg ([kernel.kallsyms]) ffffffff812e35b4 __sock_recvmsg ([kernel.kallsyms]) ffffffff812e368b sock_aio_read ([kernel.kallsyms]) ffffffff810fd2f2 do_sync_read ([kernel.kallsyms]) ffffffff810fd978 vfs_read ([kernel.kallsyms]) ffffffff810fda02 sys_read ([kernel.kallsyms]) ffffffff81002bc2 system_call ([kernel.kallsyms])
7f1647e51210 __GI___libc_read (/lib64/libc-2.12.90.so)

sshd-1675 534.960353: prev_comm=sshd prev_pid=1675 prev_prio=120 ...
ffffffff81030350 perf_trace_sched_switch ([kernel.kallsyms]) ffffffff81382ac5 schedule ([kernel.kallsyms]) ffffffff81382fa0 schedule_timeout ([kernel.kallsyms]) ffffffff81363f16 unix_stream_recvmsg ([kernel.kallsyms]) ffffffff812e35b4 __sock_recvmsg ([kernel.kallsyms]) ffffffff812e368b sock_aio_read ([kernel.kallsyms]) ffffffff810fd2f2 do_sync_read ([kernel.kallsyms]) ffffffff810fd978 vfs_read ([kernel.kallsyms]) ffffffff810fda02 sys_read ([kernel.kallsyms]) ffffffff81002bc2 system_call ([kernel.kallsyms])
7f1647e51210 __GI___libc_read (/lib64/libc-2.12.90.so)

3. Software events

perf record -e cs -c 1 -gaT
perf script -f comm,tid,pid,time,sym

sshd 1015/1015 328.654782
ffffffff810355de perf_event_task_sched_out ([kernel.kallsyms]) ffffffff81382aa1 schedule ([kernel.kallsyms]) ffffffff813837aa schedule_hrtimeout_range_clock ([kernel.kallsyms]) ffffffff81383886 schedule_hrtimeout_range ([kernel.kallsyms]) ffffffff8110c4f9 poll_schedule_timeout ([kernel.kallsyms]) ffffffff8110cd20 do_select ([kernel.kallsyms]) ffffffff8110ced8 core_sys_select ([kernel.kallsyms]) ffffffff8110d00d sys_select ([kernel.kallsyms]) ffffffff81002bc2 system_call ([kernel.kallsyms])
7fd685d30e93 __GI_select (/lib64/libc-2.12.90.so)

sshd 1629/1629 328.655460

        ffffffff810355de perf_event_task_sched_out ([kernel.kallsyms])
        ffffffff81382aa1 schedule ([kernel.kallsyms])
        ffffffff8103d4d7 __cond_resched ([kernel.kallsyms])
        ffffffff81382cc7 _cond_resched ([kernel.kallsyms])
        ffffffff81382d0a wait_for_common ([kernel.kallsyms])
        ffffffff81382e90 wait_for_completion ([kernel.kallsyms])
        ffffffff8108092e stop_one_cpu ([kernel.kallsyms])
        ffffffff8103b41d sched_exec ([kernel.kallsyms])
        ffffffff811035e0 do_execve ([kernel.kallsyms])
        ffffffff8100a5bd sys_execve ([kernel.kallsyms])
        ffffffff8100301c stub_execve ([kernel.kallsyms])
            7fd685d01f67 __execve (/lib64/libc-2.12.90.so)

4. generate a process list from a recorded session:

perf script -f comm,pid

---
 tools/perf/Documentation/perf-script.txt |   24 ++++
 tools/perf/builtin-script.c              |  207 +++++++++++++++++++++++++++---
 tools/perf/util/output.h                 |   18 +++
 tools/perf/util/session.c                |   75 +++++++++++
 tools/perf/util/session.h                |    5 +
 tools/perf/util/trace-event-parse.c      |   71 ++++++++--
 6 files changed, 369 insertions(+), 31 deletions(-)
 create mode 100644 tools/perf/util/output.h
tools/perf/Documentation/perf-script.txt +24 -0
index 29ad942..f527287 100644
@@ -112,6 +112,30 @@ OPTIONS
 --debug-mode::
         Do various checks like samples ordering and lost events.
 
+-k::
+--vmlinux=<file>::
+        vmlinux pathname
+
+--kallsyms=<file>::
+        kallsyms pathname
+
+--symfs=<directory>::
+        Look for files with symbols relative to this directory.
+
+-U::
+--show-unresolved::
+        Display all addresses including unresolved to a symbol.
+
+-G::
+--hide-call-graph::
+        When printing symbols do not display call chain.
+
+-f::
+--fields
+        Comma separated list of fields to print. Options are:
+        comm, tid, pid, time, cpu, sym, trace, event
+
+--
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-script-perl[1],
tools/perf/builtin-script.c +191 -16

275 lines not on this page · raw message

tools/perf/util/output.h new file +18 -0
new file mode 100644
index 0000000..9d79852
@@ -0,0 +1,18 @@
+#ifndef _PERF_OUTPUT_H
+#define _PERF_OUTPUT_H
+
+enum perf_output_field {
+    PERF_OUTPUT_COMM            = 1U << 0,
+    PERF_OUTPUT_TID             = 1U << 1,
+    PERF_OUTPUT_PID             = 1U << 2,
+    PERF_OUTPUT_TIME            = 1U << 3,
+    PERF_OUTPUT_CPU             = 1U << 4,
+    PERF_OUTPUT_SYM             = 1U << 5,
+    PERF_OUTPUT_TRACE           = 1U << 6,
+    PERF_OUTPUT_EVNAME          = 1U << 7,
+};
+
+extern u64 output_fields;
+#define PRINT_FIELD(x)  (output_fields & PERF_OUTPUT_##x)
+
+#endif
tools/perf/util/session.c +75 -0

84 lines not on this page · raw message

tools/perf/util/session.h +5 -0
index 977b3a1..d77a2fa 100644
@@ -165,4 +165,9 @@ static inline int perf_session__parse_sample(struct perf_session *session,
 					session->sample_id_all, sample);
 }
 
+void perf_session__print_symbols(union perf_event *event,
+				struct perf_sample *sample,
+				struct perf_session *session,
+				bool show_unresolved);
+
 #endif /* __PERF_SESSION_H */
tools/perf/util/trace-event-parse.c +56 -15

122 lines not on this page · raw message

Signature · 1 line
1.7.4
#3 lore raw

Re: [PATCH 2/2] perf script: support custom field selection for output

Quoted 20 lines · David Ahern

Allow a user to select which fields to print to stdout for event data. Options include comm (command name), tid (thread id), pid (process id), time (perf timestamp), cpu, event (for event name), trace (for trace data), and sym (for IP, symbol name and DSO name).

This options allows dumping software events with symbols -- my primary goal -- and also symbols for trace events if a so desired.

Default is set to maintain compatibility with current output.

Thanks to Frederic Weisbecker for detailed suggestions on this approach.

Examples (output compressed)

1. trace, default format

perf record -ga -e sched:sched_switch
perf script

Quoted 8 lines
swapper-0 [000]  534.955086: sched_switch: prev_comm=swapper prev_pid=0 ...
sshd-1678 [000]  534.955093: sched_switch: prev_comm=sshd prev_pid=1678 ...
sshd-1675 [001]  534.960353: sched_switch: prev_comm=sshd prev_pid=1675 ...

2. trace, custom format

perf record -ga -e sched:sched_switch
perf script -f comm,pid,time,trace,sym

Nice.

Quoted 11 lines
tools/perf/Documentation/perf-script.txt +3 -0
index 29ad942..f527287 100644
@@ -112,6 +112,30 @@ OPTIONS
 --debug-mode::
         Do various checks like samples ordering and lost events.
 
+-k::
+--vmlinux=<file>::
+        vmlinux pathname

tools/perf/Documentation/perf-script.txt OPTIONS · line 127

Quoted 10 lines
tools/perf/Documentation/perf-script.txt +10 -0
+
+--kallsyms=<file>::
+        kallsyms pathname
+
+--symfs=<directory>::
+        Look for files with symbols relative to this directory.
+
+-U::
+--show-unresolved::
+        Display all addresses including unresolved to a symbol.

We should always do it I think. As long as the sym format is asked, try to resolve it, otherwise print the raw address.

Quoted 43 lines
tools/perf/Documentation/perf-script.txt +11 -0
+
+-G::
+--hide-call-graph::
+        When printing symbols do not display call chain.
+
+-f::
+--fields
+        Comma separated list of fields to print. Options are:
+        comm, tid, pid, time, cpu, sym, trace, event
+
+--
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-script-perl[1],
tools/perf/builtin-script.c +14 -0
index 48bdf94..c6b9db1 100644
@@ -12,6 +12,7 @@
 #include "util/trace-event.h"
 #include "util/parse-options.h"
 #include "util/util.h"
+#include "util/output.h"
 
 static char const		*script_name;
 static char const		*generate_script_lang;
@@ -19,6 +20,81 @@ static bool			debug_mode;
 static u64			last_timestamp;
 static u64			nr_unordered;
 extern const struct option	record_options[];
+static bool			show_unresolved;
+static bool			no_callchain;
+
+struct output_option {
+	const char *str;
+	enum perf_output_field field;
+} all_output_options [] = {
+	{.str = "comm",  .field = PERF_OUTPUT_COMM},
+	{.str = "tid",   .field = PERF_OUTPUT_TID},
+	{.str = "pid",   .field = PERF_OUTPUT_PID},
+	{.str = "time",  .field = PERF_OUTPUT_TIME},
+	{.str = "cpu",   .field = PERF_OUTPUT_CPU},
+	{.str = "sym",   .field = PERF_OUTPUT_SYM},

tools/perf/builtin-script.c static bool debug_mode; · line 45

Quoted 10 lines
tools/perf/builtin-script.c +10 -0
+	{.str = "trace", .field = PERF_OUTPUT_TRACE},
+	{.str = "event", .field = PERF_OUTPUT_EVNAME},
+};
+
+/* default set to maintain compatibility with current format */
+#define output_fields_default   (PERF_OUTPUT_COMM | PERF_OUTPUT_PID | \
+				PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
+				PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE)
+
+u64 output_fields = output_fields_default;

Hm, we should have one default for tracepoint events and one for others. For example dso and sym make sense for hardware, breakpoint and software event, but it makes few sense for tracepoints.

Quoted 56 lines
tools/perf/builtin-script.c +52 -0
+
+static void print_sample_oneline(struct perf_sample *sample,
+			  struct perf_event_attr *attr,
+			  struct thread *thread)
+{
+	const char *evname;
+
+	if (PRINT_FIELD(COMM)) {
+		int n = 16;
+		if (PRINT_FIELD(SYM) && !no_callchain)
+			n = 1;
+		printf("%*s ", n, thread->comm);
+	}
+
+	if (PRINT_FIELD(PID) && PRINT_FIELD(TID))
+		printf("%5d/%-5d ", sample->pid, sample->tid);
+	else if (PRINT_FIELD(PID))
+		printf("%5d ", sample->pid);
+	else if (PRINT_FIELD(TID))
+		printf("%5d ", sample->tid);
+
+	if (PRINT_FIELD(CPU))
+		printf("[%03d] ", sample->cpu);
+
+	if (PRINT_FIELD(TIME)) {
+		unsigned long secs, usecs, nsecs;
+
+		nsecs = sample->time;
+		secs = nsecs / NSECS_PER_SEC;
+		nsecs -= secs * NSECS_PER_SEC;
+		usecs = nsecs / NSECS_PER_USEC;
+		printf("%8lu.%06lu ", secs, usecs);
+	}
+
+	if (PRINT_FIELD(EVNAME)) {
+		if (attr->type == PERF_TYPE_TRACEPOINT) {
+			struct event *event;
+			int type;
+
+			type = trace_parse_common_type(sample->raw_data);
+			event = trace_find_event(type);
+			if (event)
+				evname = event->name;
+		} else
+			evname = __event_name(attr->type, attr->config);
+
+		printf("%s ", evname ? evname : "(unknown)");
+	}
+
+	if (!no_callchain)
+		printf("\n");
+}
 
 static void process_event(union perf_event *event __unused,
 			  struct perf_sample *sample,

tools/perf/builtin-script.c static void process_event(union perf_event *event __unused, · line 113

Quoted 9 lines
tools/perf/builtin-script.c +4 -2
@@ -31,8 +107,15 @@ static void process_event(union perf_event *event __unused,
 	 * field, although it should be the same than this perf
 	 * event pid
 	 */
-	print_event(sample->cpu, sample->raw_data, sample->raw_size,
-		    sample->time, thread->comm);
+	if (PRINT_FIELD(TRACE))
+		print_event(sample->cpu, sample->raw_data,
+			    sample->raw_size, sample->time,
+			    thread->comm);

The print_event() thing we have in trace-event-parse.c should really handle only the raw trace itself. More on that below when I reach that file.

Quoted 13 lines
tools/perf/builtin-script.c +5 -0
+	else
+		print_sample_oneline(sample, attr, thread);
+
+	if (PRINT_FIELD(SYM))
+		perf_session__print_symbols(event, sample, session, show_unresolved);
 }
 
 struct script_spec {
tools/perf/builtin-script.c +0 -0
 	struct list_head	node;
 	struct scripting_ops	*ops;
@@ -297,6 +413,43 @@ static int parse_scriptname(const struct option *opt __used,
 	return 0;
 }

[...]

Quoted 59 lines
tools/perf/util/output.h new file +18 -0
new file mode 100644
index 0000000..9d79852
@@ -0,0 +1,18 @@
+#ifndef _PERF_OUTPUT_H
+#define _PERF_OUTPUT_H
+
+enum perf_output_field {
+    PERF_OUTPUT_COMM            = 1U << 0,
+    PERF_OUTPUT_TID             = 1U << 1,
+    PERF_OUTPUT_PID             = 1U << 2,
+    PERF_OUTPUT_TIME            = 1U << 3,
+    PERF_OUTPUT_CPU             = 1U << 4,
+    PERF_OUTPUT_SYM             = 1U << 5,
+    PERF_OUTPUT_TRACE           = 1U << 6,
+    PERF_OUTPUT_EVNAME          = 1U << 7,
+};
+
+extern u64 output_fields;
+#define PRINT_FIELD(x)  (output_fields & PERF_OUTPUT_##x)
+
+#endif
tools/perf/util/session.c +20 -0
index a3a871f..c862a71 100644
@@ -10,6 +10,7 @@
 #include "session.h"
 #include "sort.h"
 #include "util.h"
+#include "trace-event.h"
 
 static int perf_session__open(struct perf_session *self, bool force)
 {
@@ -1137,3 +1138,77 @@ size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp,
 	size_t ret = machine__fprintf_dsos_buildid(&self->host_machine, fp, with_hits);
 	return ret + machines__fprintf_dsos_buildid(&self->machines, fp, with_hits);
 }
+
+void perf_session__print_symbols(union perf_event *event,
+				struct perf_sample *sample,
+				struct perf_session *session,
+				bool show_unresolved)
+{
+	struct addr_location al;
+	const char *symname, *dsoname;
+	struct callchain_cursor *cursor = &session->callchain_cursor;
+	struct callchain_cursor_node *node;
+
+	if (perf_event__preprocess_sample(event, session, &al, sample,
+					  NULL) < 0) {
+		error("problem processing %d event, skipping it.\n",
+			event->header.type);
+		return;
+	}
+
+	if (symbol_conf.use_callchain && sample->callchain) {

tools/perf/util/session.c size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, · line 1169

Quoted 10 lines
tools/perf/util/session.c +10 -0
+
+		if (perf_session__resolve_callchain(session, al.thread,
+						sample->callchain, NULL) != 0) {
+			if (verbose)
+				error("Failed to resolve callchain. Skipping\n");
+			return;
+		}
+		callchain_cursor_commit(cursor);
+		if (cursor->nr == 0)
+			return;

I guess you don't need the above check.

Quoted 79 lines
tools/perf/util/session.c +45 -0
+
+		while (1) {
+			node = callchain_cursor_current(cursor);
+			if (!node)
+				break;
+
+			if (node->sym && node->sym->name)
+				symname = node->sym->name;
+			else if (show_unresolved)
+				symname = "";
+			else
+				goto next;
+
+			if (node->map && node->map->dso && node->map->dso->name)
+				dsoname = node->map->dso->name;
+			else if (show_unresolved)
+				dsoname = "";
+			else
+				goto next;
+
+			printf("\t%16" PRIx64 " %s (%s)\n", node->ip, symname, dsoname);
+
+next:
+			callchain_cursor_advance(cursor);
+		}
+		printf("\n");
+
+	} else {
+		if (al.sym && al.sym->name)
+			symname = al.sym->name;
+		else if (show_unresolved)
+			symname = "";
+		else
+			return;
+
+		if (al.map && al.map->dso && al.map->dso->name)
+			dsoname = al.map->dso->name;
+		else if (show_unresolved)
+			dsoname = "";
+		else
+			return;
+
+		printf("%16" PRIx64 " %s (%s)\n", al.addr, symname, dsoname);
+	}
+}
tools/perf/util/session.h +5 -0
index 977b3a1..d77a2fa 100644
@@ -165,4 +165,9 @@ static inline int perf_session__parse_sample(struct perf_session *session,
 					session->sample_id_all, sample);
 }
 
+void perf_session__print_symbols(union perf_event *event,
+				struct perf_sample *sample,
+				struct perf_session *session,
+				bool show_unresolved);
+
 #endif /* __PERF_SESSION_H */
tools/perf/util/trace-event-parse.c +2 -1
index d8e622d..9ac6524 100644
@@ -32,6 +32,7 @@
 #include "../perf.h"
 #include "util.h"
 #include "trace-event.h"
+#include "output.h"
 
 int header_page_ts_offset;
 int header_page_ts_size;
@@ -2683,13 +2684,18 @@ static void print_graph_proc(int pid, const char *comm)
 	/* sign + log10(MAX_INT) + '\0' */
 	char pid_str[11];
 	int spaces = 0;
-	int len;
+	int len = 0;
 	int i;
 

tools/perf/util/trace-event-parse.c static void print_graph_proc(int pid, const char *comm) · line 2698

Quoted 10 lines
tools/perf/util/trace-event-parse.c +6 -1
 	sprintf(pid_str, "%d", pid);
 
 	/* 1 stands for the "-" character */
-	len = strlen(comm) + strlen(pid_str) + 1;
+	if (PRINT_FIELD(COMM) && PRINT_FIELD(PID))
+		len = strlen(comm) + strlen(pid_str) + 1;
+	else if (PRINT_FIELD(COMM))
+		len = strlen(comm);
+	else if (PRINT_FIELD(PID))
+		len = strlen(pid_str);

For now we don't use that function because the function graph tracer is not yet supported by perf.

Just forget about that, we'll take care of that later. Consider pretty_print_func_graph() can't be called.

trace-event-parse.c and its print_event() pretty printing should not care about the output format. It only needs to print the raw trace itself. All the comm, time, etc... things should be handled from perf script core.

In fact, print_event() should really be renamed print_trace() or something.

Other than that, looks like a right direction.

Thanks!

#4 lore raw

Re: [PATCH 2/2] perf script: support custom field selection for output

Quoted 6 lines · Frederic Weisbecker
Quoted 3 lines
+-U::
+--show-unresolved::
+        Display all addresses including unresolved to a symbol.

We should always do it I think. As long as the sym format is asked, try to resolve it, otherwise print the raw address.

Ok. I'll remove.

Quoted 10 lines

tools/perf/builtin-script.c static bool debug_mode; · line 45

Quoted 6 lines
tools/perf/builtin-script.c +6 -0
+/* default set to maintain compatibility with current format */
+#define output_fields_default   (PERF_OUTPUT_COMM | PERF_OUTPUT_PID | \
+				PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
+				PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE)
+
+u64 output_fields = output_fields_default;

Hm, we should have one default for tracepoint events and one for others. For example dso and sym make sense for hardware, breakpoint and software event, but it makes few sense for tracepoints.

That default was strictly to maintain backwards compatibility with existing output. So you would like to see:

out_fields_def_trace = <the above default>

out_fields_def_sw = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | \
PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
PERF_OUTPUT_EVNAM | PERF_OUTPUT_SYM

If user does not specify custom option, set output_fields to default based on event type - which conceptually can change sample to sample (though perf currently can't handle a S/W and a trace event in the same file).

As for H/W events, the cycles format should be the same as the S/W format. After that perf-record does not mix well with H/W events.

Quoted 7 lines
Quoted 7 lines

static void process_event(union perf_event *event __unused,

tools/perf/builtin-script.c +0 -1
 			  struct perf_sample *sample,
@@ -31,8 +107,15 @@ static void process_event(union perf_event *event __unused,
 	 * field, although it should be the same than this perf
 	 * event pid
 	 */
-	print_event(sample->cpu, sample->raw_data, sample->raw_size,
Quoted 8 lines

tools/perf/builtin-script.c static void process_event(union perf_event *event __unused, · line 113

Quoted 5 lines
tools/perf/builtin-script.c +4 -1
-		    sample->time, thread->comm);
+	if (PRINT_FIELD(TRACE))
+		print_event(sample->cpu, sample->raw_data,
+			    sample->raw_size, sample->time,
+			    thread->comm);

The print_event() thing we have in trace-event-parse.c should really handle only the raw trace itself. More on that below when I reach that file.

Ok

Quoted 5 lines

tools/perf/util/session.c size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, · line 1169

Quoted 3 lines
tools/perf/util/session.c +3 -0
+		callchain_cursor_commit(cursor);
+		if (cursor->nr == 0)
+			return;

I guess you don't need the above check.

Ok. I'll remove.

Quoted 34 lines
Quoted 20 lines
tools/perf/util/trace-event-parse.c +2 -1
index d8e622d..9ac6524 100644
@@ -32,6 +32,7 @@
 #include "../perf.h"
 #include "util.h"
 #include "trace-event.h"
+#include "output.h"
 
 int header_page_ts_offset;
 int header_page_ts_size;
@@ -2683,13 +2684,18 @@ static void print_graph_proc(int pid, const char *comm)
 	/* sign + log10(MAX_INT) + '\0' */
 	char pid_str[11];
 	int spaces = 0;
-	int len;
+	int len = 0;
 	int i;
 

tools/perf/util/trace-event-parse.c static void print_graph_proc(int pid, const char *comm) · line 2698

Quoted 10 lines
tools/perf/util/trace-event-parse.c +6 -1
 	sprintf(pid_str, "%d", pid);
 
 	/* 1 stands for the "-" character */
-	len = strlen(comm) + strlen(pid_str) + 1;
+	if (PRINT_FIELD(COMM) && PRINT_FIELD(PID))
+		len = strlen(comm) + strlen(pid_str) + 1;
+	else if (PRINT_FIELD(COMM))
+		len = strlen(comm);
+	else if (PRINT_FIELD(PID))
+		len = strlen(pid_str);

For now we don't use that function because the function graph tracer is not yet supported by perf.

Quoted 2 lines

Just forget about that, we'll take care of that later. Consider pretty_print_func_graph() can't be called.

That certainly helps make it simpler. I left the printing of those fields in that file b/c of that function.

Quoted 7 lines

trace-event-parse.c and its print_event() pretty printing should not care about the output format. It only needs to print the raw trace itself. All the comm, time, etc... things should be handled from perf script core.

In fact, print_event() should really be renamed print_trace() or something.

Ok. I'll pull the comm, pid, time options into builtin-script's version and rename the function.

David

Quoted 4 lines

Other than that, looks like a right direction.

Thanks!

#5 lore raw

Re: [PATCH 2/2] perf script: support custom field selection for output

Quoted 12 lines · David Ahern
Quoted 10 lines · Frederic Weisbecker

tools/perf/builtin-script.c static bool debug_mode; · line 45

Quoted 6 lines
tools/perf/builtin-script.c +6 -0
+/* default set to maintain compatibility with current format */
+#define output_fields_default   (PERF_OUTPUT_COMM | PERF_OUTPUT_PID | \
+				PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
+				PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE)
+
+u64 output_fields = output_fields_default;

Hm, we should have one default for tracepoint events and one for others. For example dso and sym make sense for hardware, breakpoint and software event, but it makes few sense for tracepoints.

Quoted 2 lines

That default was strictly to maintain backwards compatibility with existing output.

Well, we don't necessarily need to keep a backward compatibility. If one day we realize the comm is not needed on the traces output, we can just remove it right away. We only want to keep the exisiting default because it (seems to) make sense. Now the default necessary headers that make sense for tracepoint may not be the same than for hardware events.

Quoted 7 lines

So you would like to see:

out_fields_def_trace = <the above default>

out_fields_def_sw = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | \
PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | \
PERF_OUTPUT_EVNAM | PERF_OUTPUT_SYM

Yep, I guess that would be sensible.

Quoted 3 lines

If user does not specify custom option, set output_fields to default based on event type - which conceptually can change sample to sample

Exactly. Which means that a global output_fields wouldn't make much sense anymore.

Quoted 2 lines

(though perf currently can't handle a S/W and a trace event in the same file).

Yeah, but let's prepare the ground for that.

Quoted 2 lines

As for H/W events, the cycles format should be the same as the S/W format.

Probably. Unless we find later that H/W event need some special treatment over software ones.

Quoted 1 line

After that perf-record does not mix well with H/W events.

Hm?

Quoted 74 lines
Quoted 7 lines
Quoted 7 lines

static void process_event(union perf_event *event __unused,

tools/perf/builtin-script.c +0 -1
 			  struct perf_sample *sample,
@@ -31,8 +107,15 @@ static void process_event(union perf_event *event __unused,
 	 * field, although it should be the same than this perf
 	 * event pid
 	 */
-	print_event(sample->cpu, sample->raw_data, sample->raw_size,
Quoted 8 lines

tools/perf/builtin-script.c static void process_event(union perf_event *event __unused, · line 113

Quoted 5 lines
tools/perf/builtin-script.c +4 -1
-		    sample->time, thread->comm);
+	if (PRINT_FIELD(TRACE))
+		print_event(sample->cpu, sample->raw_data,
+			    sample->raw_size, sample->time,
+			    thread->comm);

The print_event() thing we have in trace-event-parse.c should really handle only the raw trace itself. More on that below when I reach that file.

Ok

Quoted 5 lines

tools/perf/util/session.c size_t perf_session__fprintf_dsos_buildid(struct perf_session *self, FILE *fp, · line 1169

Quoted 3 lines
tools/perf/util/session.c +3 -0
+		callchain_cursor_commit(cursor);
+		if (cursor->nr == 0)
+			return;

I guess you don't need the above check.

Ok. I'll remove.

Quoted 34 lines
Quoted 20 lines
tools/perf/util/trace-event-parse.c +2 -1
index d8e622d..9ac6524 100644
@@ -32,6 +32,7 @@
 #include "../perf.h"
 #include "util.h"
 #include "trace-event.h"
+#include "output.h"
 
 int header_page_ts_offset;
 int header_page_ts_size;
@@ -2683,13 +2684,18 @@ static void print_graph_proc(int pid, const char *comm)
 	/* sign + log10(MAX_INT) + '\0' */
 	char pid_str[11];
 	int spaces = 0;
-	int len;
+	int len = 0;
 	int i;
 

tools/perf/util/trace-event-parse.c static void print_graph_proc(int pid, const char *comm) · line 2698

Quoted 10 lines
tools/perf/util/trace-event-parse.c +6 -1
 	sprintf(pid_str, "%d", pid);
 
 	/* 1 stands for the "-" character */
-	len = strlen(comm) + strlen(pid_str) + 1;
+	if (PRINT_FIELD(COMM) && PRINT_FIELD(PID))
+		len = strlen(comm) + strlen(pid_str) + 1;
+	else if (PRINT_FIELD(COMM))
+		len = strlen(comm);
+	else if (PRINT_FIELD(PID))
+		len = strlen(pid_str);

For now we don't use that function because the function graph tracer is not yet supported by perf.

Quoted 2 lines

Just forget about that, we'll take care of that later. Consider pretty_print_func_graph() can't be called.

That certainly helps make it simpler. I left the printing of those fields in that file b/c of that function.

Quoted 6 lines

trace-event-parse.c and its print_event() pretty printing should not care about the output format. It only needs to print the raw trace itself. All the comm, time, etc... things should be handled from perf script core.

Quoted 4 lines
Quoted 1 line

In fact, print_event() should really be renamed print_trace() or something.

Ok. I'll pull the comm, pid, time options into builtin-script's version and rename the function.

Thanks!