Skip to contentSkip to conversation

libtraceevent: perf script -g python segfaults

BUG Applied Arnaldo Carvalho de Melo 3 participants 10 messages Oct 17, 2019 – Nov 6, 2019 perf events

lore
Share & tools
lore
#0 lore raw

I'll try and continue later, but if you guys can take a look...

The first call in that loop:

while ((event = trace_find_next_event(pevent, event)))

works and the event is valid, one of the sched: tracepoints, but then the next call returns this:

struct tep_event *trace_find_next_event(struct tep_handle *pevent,
struct tep_event *event)

{
        static int idx;
        int events_count;
        struct tep_event *all_events;

        all_events = tep_get_first_event(pevent);
        events_count = tep_get_events_count(pevent);
        if (!pevent || !all_events || events_count < 1)
                return NULL;

        if (!event) {
                idx = 0;
                return all_events;
        }

        if (idx < events_count && event == (all_events + idx)) {
                idx++;
                if (idx == events_count)
                        return NULL;
                return (all_events + idx);
        }

        for (idx = 1; idx < events_count; idx++) {
                if (event == (all_events + (idx - 1)))
                        return (all_events + idx);
        }
        return NULL;
}

Oh, static int idx, oops, anyway, the all_events + idx returned for the second call to trace_find_next_event() fails, in a hurry now, will get back to this later.

- Arnaldo

[root@quaco ~]# perf record -e sched:* sleep 1
[ perf record: Woken up 33 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (8 samples) ] [root@quaco ~]# perf script -g python
Segmentation fault (core dumped)
[root@quaco ~]#

Bisected to:

[acme@quaco perf]$ git bisect bad bb3dd7e7c4d5e024d607c0ec06c2a2fb9408cc99 is the first bad commit commit bb3dd7e7c4d5e024d607c0ec06c2a2fb9408cc99

Author: Tzvetomir Stoyanov <[email protected]>
Date:   Fri Oct 5 12:22:25 2018 -0400

tools lib traceevent, perf tools: Move struct tep_handler definition in a local header file

As traceevent is going to be transferred into a proper library, its local data should be protected from the library users. This patch encapsulates struct tep_handler into a local header, not visible outside of the library. It implements also a bunch of new APIs, which library users can use to access tep_handler members.

:040000 040000 7a23aa486fd8ed255b681e6c959d6ab64d16f2f6 d95f601bb03b81bc04070b69635df40b62c70a1a M tools
[acme@quaco perf]$

[root@quaco ~]# gdb perf
GNU gdb (GDB) Fedora 8.3-6.fc30
Reading symbols from perf...
(gdb) run script -g python
Starting program: /root/bin/perf script -g python [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". [Detaching after fork from child process 10352]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff76f55e5 in __strlen_avx2 () from /lib64/libc.so.6
Missing separate debuginfos, use: dnf debuginfo-install bzip2-libs-1.0.6-29.fc30.x86_64 elfutils-libelf-0.177-1.fc30.x86_64 elfutils-libs-0.177-1.fc30.x86_64 glib2-2.60.7-1.fc30.x86_64 libbabeltrace-1.5.6-2.fc30.x86_64 libcap-2.26-5.fc30.x86_64 libgcc-9.2.1-1.fc30.x86_64 libunwind-1.3.1-2.fc30.x86_64 libuuid-2.33.2-2.fc30.x86_64 libxcrypt-4.4.10-1.fc30.x86_64 libzstd-1.4.2-1.fc30.x86_64 numactl-libs-2.0.12-2.fc30.x86_64 pcre-8.43-2.fc30.x86_64 perl-libs-5.28.2-440.fc30.x86_64 popt-1.16-17.fc30.x86_64 slang-2.3.2-5.fc30.x86_64 xz-libs-5.2.4-5.fc30.x86_64 zlib-1.2.11-18.fc30.x86_64 (gdb) bt

#0  0x00007ffff76f55e5 in __strlen_avx2 () from /lib64/libc.so.6
#1  0x00007ffff7601a1e in __vfprintf_internal () from /lib64/libc.so.6
#2  0x00007ffff75ec2ba in fprintf () from /lib64/libc.so.6
#3  0x00000000005ec8db in python_generate_script (pevent=0xbedb10, outfile=0x71f761 "perf-script") at util/scripting-engines/trace-event-python.c:1739
#4  0x000000000046e94a in cmd_script (argc=0, argv=0x7fffffffd680) at builtin-script.c:3848
#5  0x00000000004e0d86 in run_builtin (p=0xa3d238 <commands+408>, argc=3, argv=0x7fffffffd680) at perf.c:312
#6  0x00000000004e0ff3 in handle_internal_command (argc=3, argv=0x7fffffffd680) at perf.c:364
#7  0x00000000004e113a in run_argv (argcp=0x7fffffffd4dc, argv=0x7fffffffd4d0) at perf.c:408
#8  0x00000000004e1506 in main (argc=3, argv=0x7fffffffd680) at perf.c:538

(gdb) fr 3
#3 0x00000000005ec8db in python_generate_script (pevent=0xbedb10, outfile=0x71f761 "perf-script") at util/scripting-engines/trace-event-python.c:1739
1739 fprintf(ofp, "def %s__%s(", event->system, event->name);
(gdb) p event
$1 = (struct tep_event *) 0xbfa298
(gdb) p *event
$2 = {tep = 0x31, name = 0x61775f6465686373 <error: Cannot access memory at address 0x61775f6465686373>, id = 1767859563, flags = 1600482404, format = {nr_common = 1752459639, nr_fields = 1601467759, common_fields = 0x6e6f6d6d00697069,

    fields = 0x93b7367616c665f}, print_fmt = {format = 0x21 <error: Cannot access memory at address 0x21>, args = 0x64656e6769736e75}, system = 0x74726f687320 <error: Cannot access memory at address 0x74726f687320>,

handler = 0x656966090a3b303a, context = 0x51}
(gdb) list -20
1714 fprintf(ofp, "# in the format files. Those fields not available as "
1715 "handler params can\n");
1716
1717 fprintf(ofp, "# be retrieved using Python functions of the form "
1718 "common_*(context).\n");
1719
1720 fprintf(ofp, "# See the perf-script-python Documentation for the list "
1721 "of available functions.\n\n");
1722
1723 fprintf(ofp, "from __future__ import print_function\n\n");
(gdb)
1724 fprintf(ofp, "import os\n");
1725 fprintf(ofp, "import sys\n\n");
1726
1727 fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
1728 fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
1729 fprintf(ofp, "\nfrom perf_trace_context import *\n");
1730 fprintf(ofp, "from Core import *\n\n\n");
1731
1732 fprintf(ofp, "def trace_begin():\n");
1733 fprintf(ofp, "\tprint(\"in trace_begin\")\n\n");
(gdb)
1734
1735 fprintf(ofp, "def trace_end():\n");
1736 fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
1737
1738 while ((event = trace_find_next_event(pevent, event))) {
1739 fprintf(ofp, "def %s__%s(", event->system, event->name);
1740 fprintf(ofp, "event_name, ");
1741 fprintf(ofp, "context, ");
1742 fprintf(ofp, "common_cpu,\n");
1743 fprintf(ofp, "\tcommon_secs, ");

- Arnaldo

#1 lore raw
Quoted 40 lines · On Thu, 17 Oct 2019 12:42:05 -0300 Arnaldo Carvalho de Melo…

I'll try and continue later, but if you guys can take a look...

The first call in that loop:

while ((event = trace_find_next_event(pevent, event)))

works and the event is valid, one of the sched: tracepoints, but then the next call returns this:

struct tep_event *trace_find_next_event(struct tep_handle *pevent,
struct tep_event *event)

{
        static int idx;
        int events_count;
        struct tep_event *all_events;

        all_events = tep_get_first_event(pevent);
        events_count = tep_get_events_count(pevent);
        if (!pevent || !all_events || events_count < 1)
                return NULL;

        if (!event) {
                idx = 0;
                return all_events;
        }

        if (idx < events_count && event == (all_events + idx)) {
                idx++;
                if (idx == events_count)
                        return NULL;
                return (all_events + idx);
        }

        for (idx = 1; idx < events_count; idx++) {
                if (event == (all_events + (idx - 1)))
                        return (all_events + idx);
        }
        return NULL;
}
Quoted 3 lines

Oh, static int idx, oops, anyway, the all_events + idx returned for the second call to trace_find_next_event() fails, in a hurry now, will get back to this later.

Yeah, this is obviously broken. :-(

I'll have a patch later today.

-- Steve

#2 lore raw
In reply to Arnaldo Carvalho de Melo
Quoted 1 line · On Thu, 17 Oct 2019 12:42:05 -0300 Arnaldo Carvalho de Melo…

I'll try and continue later, but if you guys can take a look...

Does this fix it for you?

-- Steve

tools/perf/util/trace-event-parse.c +9 -7
index ad74be1f0e42..227629796e32 100644
@@ -193,30 +193,32 @@ int parse_event_file(struct tep_handle *pevent,
 struct tep_event *trace_find_next_event(struct tep_handle *pevent,
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
 	static int idx;
 	int events_count;
-	struct tep_event *all_events;
 
-	all_events = tep_get_first_event(pevent);
 	events_count = tep_get_events_count(pevent);
 	if (!pevent || !all_events || events_count < 1)
 		return NULL;
 
 	if (!event) {
 		idx = 0;
-		return all_events;
+		all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
+		if (all_events)
+			return all_events[0];
+		return NULL;
 	}
 
-	if (idx < events_count && event == (all_events + idx)) {
+	if (idx < events_count && event == all_events[idx]) {
 		idx++;
 		if (idx == events_count)
 			return NULL;
-		return (all_events + idx);
+		return all_events[idx];
 	}
 
 	for (idx = 1; idx < events_count; idx++) {
-		if (event == (all_events + (idx - 1)))
-			return (all_events + idx);
+		if (event == all_events[idx - 1])
+			return all_events[idx];
 	}
 	return NULL;
 }
#3 lore raw

Review

Quoted 7 lines · On Thu, 17 Oct 2019 14:38:41 -0400 Steven Rostedt <[email protected]> wrote:

struct tep_event *trace_find_next_event(struct tep_handle *pevent,

(quoted diff) +1 -1
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
 	static int idx;
 	int events_count;
-	struct tep_event *all_events;

If we are going to use static variables, let's make them all static and optimize it a little more...

-- Steve

tools/perf/util/trace-event-parse.c +15 -10
index ad74be1f0e42..3f23462517a3 100644
@@ -193,30 +193,35 @@ int parse_event_file(struct tep_handle *pevent,
 struct tep_event *trace_find_next_event(struct tep_handle *pevent,
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
+	static int events_count;
 	static int idx;
-	int events_count;
-	struct tep_event *all_events;
 
-	all_events = tep_get_first_event(pevent);
-	events_count = tep_get_events_count(pevent);
-	if (!pevent || !all_events || events_count < 1)
+	if (!pevent || !all_events)
 		return NULL;
 
 	if (!event) {
 		idx = 0;
-		return all_events;
+		events_count = tep_get_events_count(pevent);
+		if (events_count < 1)
+			return NULL;
+
+		all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
+		if (all_events)
+			return all_events[0];
+		return NULL;
 	}
 
-	if (idx < events_count && event == (all_events + idx)) {
+	if (idx < events_count && event == all_events[idx]) {
 		idx++;
 		if (idx == events_count)
 			return NULL;
-		return (all_events + idx);
+		return all_events[idx];
 	}
 
 	for (idx = 1; idx < events_count; idx++) {
-		if (event == (all_events + (idx - 1)))
-			return (all_events + idx);
+		if (event == all_events[idx - 1])
+			return all_events[idx];
 	}
 	return NULL;
 }
#4 lore raw

Em Thu, Oct 17, 2019 at 02:41:14PM -0400, Steven Rostedt escreveu:

Quoted 13 lines

tools/perf/util/trace-event-parse.c int parse_event_file(struct tep_handle *pevent, · line 198

Quoted 7 lines · On Thu, 17 Oct 2019 14:38:41 -0400 Steven Rostedt <[email protected]> wrote:

struct tep_event *trace_find_next_event(struct tep_handle *pevent,

tools/perf/util/trace-event-parse.c +1 -1
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
 	static int idx;
 	int events_count;
-	struct tep_event *all_events;

If we are going to use static variables, let's make them all static and optimize it a little more...

I'll test it, but can't you have this somewhere else, i.e. at tep_handle perhaps?

- Arnaldo

Quoted 52 lines

-- Steve

tools/perf/util/trace-event-parse.c +15 -10
index ad74be1f0e42..3f23462517a3 100644
@@ -193,30 +193,35 @@ int parse_event_file(struct tep_handle *pevent,
 struct tep_event *trace_find_next_event(struct tep_handle *pevent,
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
+	static int events_count;
 	static int idx;
-	int events_count;
-	struct tep_event *all_events;
 
-	all_events = tep_get_first_event(pevent);
-	events_count = tep_get_events_count(pevent);
-	if (!pevent || !all_events || events_count < 1)
+	if (!pevent || !all_events)
 		return NULL;
 
 	if (!event) {
 		idx = 0;
-		return all_events;
+		events_count = tep_get_events_count(pevent);
+		if (events_count < 1)
+			return NULL;
+
+		all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
+		if (all_events)
+			return all_events[0];
+		return NULL;
 	}
 
-	if (idx < events_count && event == (all_events + idx)) {
+	if (idx < events_count && event == all_events[idx]) {
 		idx++;
 		if (idx == events_count)
 			return NULL;
-		return (all_events + idx);
+		return all_events[idx];
 	}
 
 	for (idx = 1; idx < events_count; idx++) {
-		if (event == (all_events + (idx - 1)))
-			return (all_events + idx);
+		if (event == all_events[idx - 1])
+			return all_events[idx];
 	}
 	return NULL;
 }
Signature · 1 line
- Arnaldo
#5 lore raw
Quoted 15 lines · On Thu, 17 Oct 2019 16:28:32 -0300 Arnaldo Carvalho de Melo…

Em Thu, Oct 17, 2019 at 02:41:14PM -0400, Steven Rostedt escreveu:

Quoted 13 lines

Review

Quoted 7 lines · On Thu, 17 Oct 2019 14:38:41 -0400 Steven Rostedt <[email protected]> wrote:

struct tep_event *trace_find_next_event(struct tep_handle *pevent,

(quoted diff) +1 -1
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
 	static int idx;
 	int events_count;
-	struct tep_event *all_events;  

If we are going to use static variables, let's make them all static and optimize it a little more...

Quoted 4 lines

I'll test it, but can't you have this somewhere else, i.e. at tep_handle perhaps?

Or we can nuke the function entirely, it's a rather silly helper anyway. Just do this:

-- Steve

tools/perf/util/scripting-engines/trace-event-perl.c +6 -2
index b93f36b887b5..f1d5f564aa46 100644
@@ -537,10 +537,11 @@ static int perl_stop_script(void)
 
 static int perl_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.pl", outfile);
@@ -601,8 +602,11 @@ sub print_backtrace\n\
 }\n\n\
 ");
 
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
 		fprintf(ofp, "\tmy (");
 
tools/perf/util/scripting-engines/trace-event-python.c +7 -2
index 87ef16a1b17e..2a148a10d0de 100644
@@ -1590,10 +1590,11 @@ static int python_stop_script(void)
 
 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.py", outfile);
@@ -1638,7 +1639,11 @@ static int python_generate_script(struct tep_handle *pevent, const char *outfile
 	fprintf(ofp, "def trace_end():\n");
 	fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
+
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "def %s__%s(", event->system, event->name);
 		fprintf(ofp, "event_name, ");
 		fprintf(ofp, "context, ");
#6 lore raw

Em Thu, Oct 17, 2019 at 03:37:33PM -0400, Steven Rostedt escreveu:

Quoted 18 lines
Quoted 15 lines · On Thu, 17 Oct 2019 16:28:32 -0300 Arnaldo Carvalho de Melo…

Em Thu, Oct 17, 2019 at 02:41:14PM -0400, Steven Rostedt escreveu:

Quoted 13 lines

Review

Quoted 7 lines · On Thu, 17 Oct 2019 14:38:41 -0400 Steven Rostedt <[email protected]> wrote:

struct tep_event *trace_find_next_event(struct tep_handle *pevent,

(quoted diff) +1 -1
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
 	static int idx;
 	int events_count;
-	struct tep_event *all_events;  

If we are going to use static variables, let's make them all static and optimize it a little more...

Quoted 7 lines
Quoted 4 lines

I'll test it, but can't you have this somewhere else, i.e. at tep_handle perhaps?

Or we can nuke the function entirely, it's a rather silly helper anyway. Just do this:

I like it, that is a good nuke use, nice button to press! :-)

Testing it now...

- Arnaldo

Quoted 64 lines

-- Steve

tools/perf/util/scripting-engines/trace-event-perl.c +6 -2
index b93f36b887b5..f1d5f564aa46 100644
@@ -537,10 +537,11 @@ static int perl_stop_script(void)
 
 static int perl_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.pl", outfile);
@@ -601,8 +602,11 @@ sub print_backtrace\n\
 }\n\n\
 ");
 
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
 		fprintf(ofp, "\tmy (");
 
tools/perf/util/scripting-engines/trace-event-python.c +7 -2
index 87ef16a1b17e..2a148a10d0de 100644
@@ -1590,10 +1590,11 @@ static int python_stop_script(void)
 
 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.py", outfile);
@@ -1638,7 +1639,11 @@ static int python_generate_script(struct tep_handle *pevent, const char *outfile
 	fprintf(ofp, "def trace_end():\n");
 	fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
+
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "def %s__%s(", event->system, event->name);
 		fprintf(ofp, "event_name, ");
 		fprintf(ofp, "context, ");
Signature · 1 line
- Arnaldo
author Tested-by
#7 lore raw

Em Thu, Oct 17, 2019 at 04:49:08PM -0300, Arnaldo Carvalho de Melo escreveu:

Quoted 24 lines

Em Thu, Oct 17, 2019 at 03:37:33PM -0400, Steven Rostedt escreveu:

Quoted 23 lines
Quoted 19 lines · On Thu, 17 Oct 2019 16:28:32 -0300 Arnaldo Carvalho de Melo…

Em Thu, Oct 17, 2019 at 02:41:14PM -0400, Steven Rostedt escreveu:

Quoted 13 lines

Review

Quoted 7 lines · On Thu, 17 Oct 2019 14:38:41 -0400 Steven Rostedt <[email protected]> wrote:

struct tep_event *trace_find_next_event(struct tep_handle *pevent,

(quoted diff) +1 -1
 					struct tep_event *event)
 {
+	static struct tep_event **all_events;
 	static int idx;
 	int events_count;
-	struct tep_event *all_events;  

If we are going to use static variables, let's make them all static and optimize it a little more...

I'll test it, but can't you have this somewhere else, i.e. at tep_handle perhaps?

Quoted 6 lines
Quoted 2 lines

Or we can nuke the function entirely, it's a rather silly helper anyway. Just do this:

I like it, that is a good nuke use, nice button to press! :-)

Testing it now...

Works like a charm, thanks!

- Arnaldo

#8 lore raw

[tip: perf/core] perf scripting engines: Iterate on tep event arrays directly

In reply to Steven Rostedt
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     a5e05abc6b8d81148b35cd8632a4a6252383d968
Gitweb:        https://git.kernel.org/tip/a5e05abc6b8d81148b35cd8632a4a6252383d968
Author:        Steven Rostedt (VMware) <[email protected]>
AuthorDate:    Thu, 17 Oct 2019 17:05:22 -04:00
Committer:     Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Fri, 18 Oct 2019 12:07:46 -03:00

perf scripting engines: Iterate on tep event arrays directly

Instead of calling a useless (and broken) helper function to get the next event of a tep event array, just get the array directly and iterate over it.

Note, the broken part was from trace_find_next_event() which after this will no longer be used, and can be removed.

Committer notes:

This fixes a segfault when generating python scripts from perf.data files with multiple tracepoint events, i.e. the following use case is fixed by this patch:

  # perf record -e sched:* sleep 1
  [ perf record: Woken up 31 times to write data ]
  [ perf record: Captured and wrote 0.031 MB perf.data (9 samples) ]
  # perf script -g python
  Segmentation fault (core dumped)
  #
---
 tools/perf/util/scripting-engines/trace-event-perl.c   |  8 ++++++--
 tools/perf/util/scripting-engines/trace-event-python.c |  9 +++++++--
 2 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/util/scripting-engines/trace-event-perl.c +6 -2
index 1596185..741f040 100644
@@ -539,10 +539,11 @@ static int perl_stop_script(void)
 
 static int perl_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.pl", outfile);
@@ -603,8 +604,11 @@ sub print_backtrace\n\
 }\n\n\
 ");
 
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
 		fprintf(ofp, "\tmy (");
 
tools/perf/util/scripting-engines/trace-event-python.c +7 -2
index 5d341ef..93c03b3 100644
@@ -1687,10 +1687,11 @@ static int python_stop_script(void)
 
 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.py", outfile);
@@ -1735,7 +1736,11 @@ static int python_generate_script(struct tep_handle *pevent, const char *outfile
 	fprintf(ofp, "def trace_end():\n");
 	fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
+
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "def %s__%s(", event->system, event->name);
 		fprintf(ofp, "event_name, ");
 		fprintf(ofp, "context, ");
#9 lore raw

[tip: perf/urgent] perf scripting engines: Iterate on tep event arrays directly

In reply to Steven Rostedt
The following commit has been merged into the perf/urgent branch of tip:

The following commit has been merged into the perf/urgent branch of tip:

Commit-ID:     443b0636ea7386d01dc460b4a4264e125f710b53
Gitweb:        https://git.kernel.org/tip/443b0636ea7386d01dc460b4a4264e125f710b53
Author:        Steven Rostedt (VMware) <[email protected]>
AuthorDate:    Thu, 17 Oct 2019 17:05:22 -04:00
Committer:     Arnaldo Carvalho de Melo <[email protected]>
CommitterDate: Tue, 05 Nov 2019 08:39:26 -03:00

perf scripting engines: Iterate on tep event arrays directly

Instead of calling a useless (and broken) helper function to get the next event of a tep event array, just get the array directly and iterate over it.

Note, the broken part was from trace_find_next_event() which after this will no longer be used, and can be removed.

Committer notes:

This fixes a segfault when generating python scripts from perf.data files with multiple tracepoint events, i.e. the following use case is fixed by this patch:

  # perf record -e sched:* sleep 1
  [ perf record: Woken up 31 times to write data ]
  [ perf record: Captured and wrote 0.031 MB perf.data (9 samples) ]
  # perf script -g python
  Segmentation fault (core dumped)
  #
---
 tools/perf/util/scripting-engines/trace-event-perl.c   |  8 ++++++--
 tools/perf/util/scripting-engines/trace-event-python.c |  9 +++++++--
 2 files changed, 13 insertions(+), 4 deletions(-)
tools/perf/util/scripting-engines/trace-event-perl.c +6 -2
index 1596185..741f040 100644
@@ -539,10 +539,11 @@ static int perl_stop_script(void)
 
 static int perl_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.pl", outfile);
@@ -603,8 +604,11 @@ sub print_backtrace\n\
 }\n\n\
 ");
 
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
 		fprintf(ofp, "\tmy (");
 
tools/perf/util/scripting-engines/trace-event-python.c +7 -2
index 5d341ef..93c03b3 100644
@@ -1687,10 +1687,11 @@ static int python_stop_script(void)
 
 static int python_generate_script(struct tep_handle *pevent, const char *outfile)
 {
+	int i, not_first, count, nr_events;
+	struct tep_event **all_events;
 	struct tep_event *event = NULL;
 	struct tep_format_field *f;
 	char fname[PATH_MAX];
-	int not_first, count;
 	FILE *ofp;
 
 	sprintf(fname, "%s.py", outfile);
@@ -1735,7 +1736,11 @@ static int python_generate_script(struct tep_handle *pevent, const char *outfile
 	fprintf(ofp, "def trace_end():\n");
 	fprintf(ofp, "\tprint(\"in trace_end\")\n\n");
 
-	while ((event = trace_find_next_event(pevent, event))) {
+	nr_events = tep_get_events_count(pevent);
+	all_events = tep_list_events(pevent, TEP_EVENT_SORT_ID);
+
+	for (i = 0; all_events && i < nr_events; i++) {
+		event = all_events[i];
 		fprintf(ofp, "def %s__%s(", event->system, event->name);
 		fprintf(ofp, "event_name, ");
 		fprintf(ofp, "context, ");