Skip to contentSkip to conversation

perf scripting python: Add insn, srcline and srccode

PATCH Applied Adrian Hunter 2 participants 15 messages May 30, 2021 – Jun 1, 2021 perf events

lore
Share & tools
lore

Adrian Hunter

author reviewer
#0 lore raw

cover perf scripting python: Add insn, srcline and srccode

Hi

Here are some patches to add insn, srcline and srccode to python scripting. In addition, it is made possible for a script to set itrace options.

The first 2 patches are minor tidy-ups. The next 3 are additions to scripting_context. The next 5 add new methods that python scripts can call.

Then there is a patch to the perf scripting python documentation.

And finally 2 patches add to the intel-pt-events.py script as an example.

Adrian Hunter (13):
perf scripting python: Remove unnecessary 'static'
perf scripting python: Simplify perf-trace-context module functions perf scripting: Add scripting_context__update()
perf scripting: Add perf_session to scripting_context
perf scripting python: Assign perf_script_context
perf script: Factor out script_fetch_insn()
perf scripting python: Add perf_sample_insn()
perf auxtrace: Factor out itrace_do_parse_synth_opts()
perf scripting python: Add perf_set_itrace_options()
perf scripting python: Add perf_sample_srcline() and perf_sample_srccode() perf scripting python: Update documentation for srcline etc perf scripting python: exported-sql-viewer.py: Factor out libxed.py perf scripting python: intel-pt-events.py: Add --insn-trace and --src-trace

 tools/perf/Documentation/perf-intel-pt.txt         |   6 +-
 tools/perf/Documentation/perf-script-python.txt    |  46 +++++-
 tools/perf/builtin-script.c                        |  12 +-
 .../perf/scripts/python/Perf-Trace-Util/Context.c  | 168 +++++++++++++++++---
 tools/perf/scripts/python/exported-sql-viewer.py   |  89 +----------
 tools/perf/scripts/python/intel-pt-events.py       | 176 ++++++++++++++++++---
 tools/perf/scripts/python/libxed.py                | 107 +++++++++++++
 tools/perf/util/auxtrace.c                         |  10 +-
 tools/perf/util/auxtrace.h                         |  10 ++
 .../perf/util/scripting-engines/trace-event-perl.c |  11 +-
 .../util/scripting-engines/trace-event-python.c    |  37 ++++-
 tools/perf/util/trace-event-scripting.c            |  27 +++-
 tools/perf/util/trace-event.h                      |  20 ++-
 13 files changed, 568 insertions(+), 151 deletions(-)
 create mode 100644 tools/perf/scripts/python/libxed.py

Regards
Adrian

author reviewer
#1 lore raw

PATCH 1/13 perf scripting python: Remove unnecessary 'static'

The variables are always assigned before use, making the 'static' storage class unnecessary.

---
 tools/perf/scripts/python/Perf-Trace-Util/Context.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/scripts/python/Perf-Trace-Util/Context.c +3 -3
index 0b7096847991..fdf692d1e8f3 100644
@@ -22,7 +22,7 @@ PyMODINIT_FUNC PyInit_perf_trace_context(void);
 
 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
 {
-	static struct scripting_context *scripting_context;
+	struct scripting_context *scripting_context;
 	PyObject *context;
 	int retval;
 
@@ -38,7 +38,7 @@ static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
 static PyObject *perf_trace_context_common_flags(PyObject *obj,
 						 PyObject *args)
 {
-	static struct scripting_context *scripting_context;
+	struct scripting_context *scripting_context;
 	PyObject *context;
 	int retval;
 
@@ -54,7 +54,7 @@ static PyObject *perf_trace_context_common_flags(PyObject *obj,
 static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
 						      PyObject *args)
 {
-	static struct scripting_context *scripting_context;
+	struct scripting_context *scripting_context;
 	PyObject *context;
 	int retval;
 
Signature · 1 line
2.17.1
author reviewer
#2 lore raw

PATCH 2/13 perf scripting python: Simplify perf-trace-context module functions

In reply to Adrian Hunter

Simplify perf-trace-context module functions by factoring out some common code.

---
 .../scripts/python/Perf-Trace-Util/Context.c  | 39 ++++++++-----------
 1 file changed, 17 insertions(+), 22 deletions(-)
tools/perf/scripts/python/Perf-Trace-Util/Context.c +17 -22
index fdf692d1e8f3..7cef02d75bc7 100644
@@ -20,51 +20,46 @@ PyMODINIT_FUNC initperf_trace_context(void);
 PyMODINIT_FUNC PyInit_perf_trace_context(void);
 #endif
 
-static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
+static struct scripting_context *get_scripting_context(PyObject *args)
 {
-	struct scripting_context *scripting_context;
 	PyObject *context;
-	int retval;
 
 	if (!PyArg_ParseTuple(args, "O", &context))
 		return NULL;
 
-	scripting_context = _PyCapsule_GetPointer(context, NULL);
-	retval = common_pc(scripting_context);
+	return _PyCapsule_GetPointer(context, NULL);
+}
+
+static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
+{
+	struct scripting_context *c = get_scripting_context(args);
+
+	if (!c)
+		return NULL;
 
-	return Py_BuildValue("i", retval);
+	return Py_BuildValue("i", common_pc(c));
 }
 
 static PyObject *perf_trace_context_common_flags(PyObject *obj,
 						 PyObject *args)
 {
-	struct scripting_context *scripting_context;
-	PyObject *context;
-	int retval;
+	struct scripting_context *c = get_scripting_context(args);
 
-	if (!PyArg_ParseTuple(args, "O", &context))
+	if (!c)
 		return NULL;
 
-	scripting_context = _PyCapsule_GetPointer(context, NULL);
-	retval = common_flags(scripting_context);
-
-	return Py_BuildValue("i", retval);
+	return Py_BuildValue("i", common_flags(c));
 }
 
 static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
 						      PyObject *args)
 {
-	struct scripting_context *scripting_context;
-	PyObject *context;
-	int retval;
+	struct scripting_context *c = get_scripting_context(args);
 
-	if (!PyArg_ParseTuple(args, "O", &context))
+	if (!c)
 		return NULL;
 
-	scripting_context = _PyCapsule_GetPointer(context, NULL);
-	retval = common_lock_depth(scripting_context);
-
-	return Py_BuildValue("i", retval);
+	return Py_BuildValue("i", common_lock_depth(c));
 }
 
 static PyMethodDef ContextMethods[] = {
Signature · 1 line
2.17.1
author reviewer
#3 lore raw

PATCH 3/13 perf scripting: Add scripting_context__update()

In reply to Adrian Hunter

Move scripting_context update to a separate function and add the arguments of ->process_event() to it.

This prepares the way for adding more methods to the perf_trace_context module, by providing the context information that they will need.

---
 .../util/scripting-engines/trace-event-perl.c |  6 ++----
 .../scripting-engines/trace-event-python.c    |  5 ++---
 tools/perf/util/trace-event-scripting.c       | 21 +++++++++++++++++++
 tools/perf/util/trace-event.h                 | 13 ++++++++++++
 4 files changed, 38 insertions(+), 7 deletions(-)
tools/perf/util/scripting-engines/trace-event-perl.c +2 -4
index a837aee24674..5bbf00c1179f 100644
@@ -371,9 +371,6 @@ static void perl_process_tracepoint(struct perf_sample *sample,
 	s = nsecs / NSEC_PER_SEC;
 	ns = nsecs - s * NSEC_PER_SEC;
 
-	scripting_context->event_data = data;
-	scripting_context->pevent = evsel->tp_format->tep;
-
 	ENTER;
 	SAVETMPS;
 	PUSHMARK(SP);
@@ -457,8 +454,9 @@ static void perl_process_event(union perf_event *event,
 			       struct perf_sample *sample,
 			       struct evsel *evsel,
 			       struct addr_location *al,
-			       struct addr_location *addr_al __maybe_unused)
+			       struct addr_location *addr_al)
 {
+	scripting_context__update(scripting_context, event, sample, evsel, al, addr_al);
 	perl_process_tracepoint(sample, evsel, al);
 	perl_process_event_generic(event, sample, evsel);
 }
tools/perf/util/scripting-engines/trace-event-python.c +2 -3
index ffc5f4cffdba..d99f71916af7 100644
@@ -897,9 +897,6 @@ static void python_process_tracepoint(struct perf_sample *sample,
 	s = nsecs / NSEC_PER_SEC;
 	ns = nsecs - s * NSEC_PER_SEC;
 
-	scripting_context->event_data = data;
-	scripting_context->pevent = evsel->tp_format->tep;
-
 	context = _PyCapsule_New(scripting_context, NULL, NULL);
 
 	PyTuple_SetItem(t, n++, _PyUnicode_FromString(handler_name));
@@ -1403,6 +1400,8 @@ static void python_process_event(union perf_event *event,
 {
 	struct tables *tables = &tables_global;
 
+	scripting_context__update(scripting_context, event, sample, evsel, al, addr_al);
+
 	switch (evsel->core.attr.type) {
 	case PERF_TYPE_TRACEPOINT:
 		python_process_tracepoint(sample, evsel, al, addr_al);
tools/perf/util/trace-event-scripting.c +21 -0
index 6c51fba70d49..a2f0c1e460a2 100644
@@ -12,10 +12,31 @@
 
 #include "debug.h"
 #include "trace-event.h"
+#include "event.h"
+#include "evsel.h"
 #include <linux/zalloc.h>
 
 struct scripting_context *scripting_context;
 
+void scripting_context__update(struct scripting_context *c,
+			       union perf_event *event,
+			       struct perf_sample *sample,
+			       struct evsel *evsel,
+			       struct addr_location *al,
+			       struct addr_location *addr_al)
+{
+	c->event_data = sample->raw_data;
+	if (evsel->tp_format)
+		c->pevent = evsel->tp_format->tep;
+	else
+		c->pevent = NULL;
+	c->event = event;
+	c->sample = sample;
+	c->evsel = evsel;
+	c->al = al;
+	c->addr_al = addr_al;
+}
+
 static int flush_script_unsupported(void)
 {
 	return 0;
tools/perf/util/trace-event.h +13 -0
index 35c354a15c3a..a939318b88a6 100644
@@ -11,6 +11,7 @@ union perf_event;
 struct perf_tool;
 struct thread;
 struct tep_plugin_list;
+struct evsel;
 
 struct trace_event {
 	struct tep_handle	*pevent;
@@ -101,8 +102,20 @@ void setup_python_scripting(void);
 struct scripting_context {
 	struct tep_handle *pevent;
 	void *event_data;
+	union perf_event *event;
+	struct perf_sample *sample;
+	struct evsel *evsel;
+	struct addr_location *al;
+	struct addr_location *addr_al;
 };
 
+void scripting_context__update(struct scripting_context *scripting_context,
+			       union perf_event *event,
+			       struct perf_sample *sample,
+			       struct evsel *evsel,
+			       struct addr_location *al,
+			       struct addr_location *addr_al);
+
 int common_pc(struct scripting_context *context);
 int common_flags(struct scripting_context *context);
 int common_lock_depth(struct scripting_context *context);
Signature · 1 line
2.17.1
author reviewer
#4 lore raw

PATCH 4/13 perf scripting: Add perf_session to scripting_context

In reply to Adrian Hunter

This is preparation for allowing a script to set the itrace options for the session if they have not already been set.

---
 tools/perf/builtin-script.c                            | 2 +-
 tools/perf/util/scripting-engines/trace-event-perl.c   | 5 ++++-
 tools/perf/util/scripting-engines/trace-event-python.c | 4 +++-
 tools/perf/util/trace-event-scripting.c                | 6 ++++--
 tools/perf/util/trace-event.h                          | 4 +++-
 5 files changed, 15 insertions(+), 6 deletions(-)
tools/perf/builtin-script.c +1 -1
index 7a7a19f52db5..fd5c257d55a8 100644
@@ -4017,7 +4017,7 @@ int cmd_script(int argc, const char **argv)
 	}
 
 	if (script_name) {
-		err = scripting_ops->start_script(script_name, argc, argv);
+		err = scripting_ops->start_script(script_name, argc, argv, session);
 		if (err)
 			goto out_delete;
 		pr_debug("perf script started with script %s\n\n", script_name);
tools/perf/util/scripting-engines/trace-event-perl.c +4 -1
index 5bbf00c1179f..32a721b3e9a5 100644
@@ -473,11 +473,14 @@ static void run_start_sub(void)
 /*
  * Start trace script
  */
-static int perl_start_script(const char *script, int argc, const char **argv)
+static int perl_start_script(const char *script, int argc, const char **argv,
+			     struct perf_session *session)
 {
 	const char **command_line;
 	int i, err = 0;
 
+	scripting_context->session = session;
+
 	command_line = malloc((argc + 2) * sizeof(const char *));
 	command_line[0] = "";
 	command_line[1] = script;
tools/perf/util/scripting-engines/trace-event-python.c +3 -1
index d99f71916af7..02d134b6ba8d 100644
@@ -1746,7 +1746,8 @@ static void _free_command_line(wchar_t **command_line, int num)
 /*
  * Start trace script
  */
-static int python_start_script(const char *script, int argc, const char **argv)
+static int python_start_script(const char *script, int argc, const char **argv,
+			       struct perf_session *session)
 {
 	struct tables *tables = &tables_global;
 #if PY_MAJOR_VERSION < 3
@@ -1762,6 +1763,7 @@ static int python_start_script(const char *script, int argc, const char **argv)
 	int i, err = 0;
 	FILE *fp;
 
+	scripting_context->session = session;
 #if PY_MAJOR_VERSION < 3
 	command_line = malloc((argc + 1) * sizeof(const char *));
 	command_line[0] = script;
tools/perf/util/trace-event-scripting.c +4 -2
index a2f0c1e460a2..7172ca05265f 100644
@@ -66,7 +66,8 @@ static void print_python_unsupported_msg(void)
 
 static int python_start_script_unsupported(const char *script __maybe_unused,
 					   int argc __maybe_unused,
-					   const char **argv __maybe_unused)
+					   const char **argv __maybe_unused,
+					   struct perf_session *session __maybe_unused)
 {
 	print_python_unsupported_msg();
 
@@ -131,7 +132,8 @@ static void print_perl_unsupported_msg(void)
 
 static int perl_start_script_unsupported(const char *script __maybe_unused,
 					 int argc __maybe_unused,
-					 const char **argv __maybe_unused)
+					 const char **argv __maybe_unused,
+					 struct perf_session *session __maybe_unused)
 {
 	print_perl_unsupported_msg();
 
tools/perf/util/trace-event.h +3 -1
index a939318b88a6..73f5b29472f7 100644
@@ -73,7 +73,8 @@ struct perf_stat_config;
 struct scripting_ops {
 	const char *name;
 	const char *dirname; /* For script path .../scripts/<dirname>/... */
-	int (*start_script) (const char *script, int argc, const char **argv);
+	int (*start_script)(const char *script, int argc, const char **argv,
+			    struct perf_session *session);
 	int (*flush_script) (void);
 	int (*stop_script) (void);
 	void (*process_event) (union perf_event *event,
@@ -107,6 +108,7 @@ struct scripting_context {
 	struct evsel *evsel;
 	struct addr_location *al;
 	struct addr_location *addr_al;
+	struct perf_session *session;
 };
 
 void scripting_context__update(struct scripting_context *scripting_context,
Signature · 1 line
2.17.1
author reviewer
#5 lore raw

PATCH 5/13 perf scripting python: Assign perf_script_context

In reply to Adrian Hunter

The scripting_context pointer itself does not change and nor does it need to. Put it directly into the script as a variable at the start so it does not have to be passed on each call into the script.

---
 .../scripts/python/Perf-Trace-Util/Context.c  |  8 +++++-
 .../scripting-engines/trace-event-python.c    | 28 +++++++++++++++++++
 2 files changed, 35 insertions(+), 1 deletion(-)
tools/perf/scripts/python/Perf-Trace-Util/Context.c +7 -1
index 7cef02d75bc7..26a45ae78be4 100644
@@ -91,6 +91,12 @@ PyMODINIT_FUNC PyInit_perf_trace_context(void)
 		NULL,			/* m_clear */
 		NULL,			/* m_free */
 	};
-	return PyModule_Create(&moduledef);
+	PyObject *mod;
+
+	mod = PyModule_Create(&moduledef);
+	/* Add perf_script_context to the module so it can be imported */
+	PyObject_SetAttrString(mod, "perf_script_context", Py_None);
+
+	return mod;
 }
 #endif
tools/perf/util/scripting-engines/trace-event-python.c +28 -0
index 02d134b6ba8d..164d2f45028c 100644
@@ -1599,6 +1599,31 @@ static void python_process_stat_interval(u64 tstamp)
 	Py_DECREF(t);
 }
 
+static int perf_script_context_init(void)
+{
+	PyObject *perf_script_context;
+	PyObject *perf_trace_context;
+	PyObject *dict;
+	int ret;
+
+	perf_trace_context = PyImport_AddModule("perf_trace_context");
+	if (!perf_trace_context)
+		return -1;
+	dict = PyModule_GetDict(perf_trace_context);
+	if (!dict)
+		return -1;
+
+	perf_script_context = _PyCapsule_New(scripting_context, NULL, NULL);
+	if (!perf_script_context)
+		return -1;
+
+	ret = PyDict_SetItemString(dict, "perf_script_context", perf_script_context);
+	if (!ret)
+		ret = PyDict_SetItemString(main_dict, "perf_script_context", perf_script_context);
+	Py_DECREF(perf_script_context);
+	return ret;
+}
+
 static int run_start_sub(void)
 {
 	main_module = PyImport_AddModule("__main__");
@@ -1611,6 +1636,9 @@ static int run_start_sub(void)
 		goto error;
 	Py_INCREF(main_dict);
 
+	if (perf_script_context_init())
+		goto error;
+
 	try_call_object("trace_begin", NULL);
 
 	return 0;
Signature · 1 line
2.17.1
author reviewer
#6 lore raw

PATCH 6/13 perf script: Factor out script_fetch_insn()

In reply to Adrian Hunter

Factor out script_fetch_insn() so it can be reused.

---
 tools/perf/builtin-script.c   | 10 ++++++++--
 tools/perf/util/trace-event.h |  3 +++
 2 files changed, 11 insertions(+), 2 deletions(-)
tools/perf/builtin-script.c +8 -2
index fd5c257d55a8..57488d60b64a 100644
@@ -1417,6 +1417,13 @@ __weak void arch_fetch_insn(struct perf_sample *sample __maybe_unused,
 {
 }
 
+void script_fetch_insn(struct perf_sample *sample, struct thread *thread,
+		       struct machine *machine)
+{
+	if (sample->insn_len == 0 && native_arch)
+		arch_fetch_insn(sample, thread, machine);
+}
+
 static int perf_sample__fprintf_insn(struct perf_sample *sample,
 				     struct perf_event_attr *attr,
 				     struct thread *thread,
@@ -1424,8 +1431,7 @@ static int perf_sample__fprintf_insn(struct perf_sample *sample,
 {
 	int printed = 0;
 
-	if (sample->insn_len == 0 && native_arch)
-		arch_fetch_insn(sample, thread, machine);
+	script_fetch_insn(sample, thread, machine);
 
 	if (PRINT_FIELD(INSNLEN))
 		printed += fprintf(fp, " ilen: %d", sample->insn_len);
tools/perf/util/trace-event.h +3 -0
index 73f5b29472f7..54aadeedf28c 100644
@@ -97,6 +97,9 @@ extern unsigned int scripting_max_stack;
 
 int script_spec_register(const char *spec, struct scripting_ops *ops);
 
+void script_fetch_insn(struct perf_sample *sample, struct thread *thread,
+		       struct machine *machine);
+
 void setup_perl_scripting(void);
 void setup_python_scripting(void);
 
Signature · 1 line
2.17.1
author reviewer
#7 lore raw

PATCH 7/13 perf scripting python: Add perf_sample_insn()

In reply to Adrian Hunter

Add perf_sample_insn() to the perf_trace_context module so that a script can get the instruction bytes.

---
 .../scripts/python/Perf-Trace-Util/Context.c  | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/perf/scripts/python/Perf-Trace-Util/Context.c +27 -0
index 26a45ae78be4..d7f044259f9b 100644
@@ -7,15 +7,23 @@
 
 #include <Python.h>
 #include "../../../util/trace-event.h"
+#include "../../../util/event.h"
+#include "../../../util/symbol.h"
+#include "../../../util/thread.h"
+#include "../../../util/maps.h"
 
 #if PY_MAJOR_VERSION < 3
 #define _PyCapsule_GetPointer(arg1, arg2) \
   PyCObject_AsVoidPtr(arg1)
+#define _PyBytes_FromStringAndSize(arg1, arg2) \
+  PyString_FromStringAndSize((arg1), (arg2))
 
 PyMODINIT_FUNC initperf_trace_context(void);
 #else
 #define _PyCapsule_GetPointer(arg1, arg2) \
   PyCapsule_GetPointer((arg1), (arg2))
+#define _PyBytes_FromStringAndSize(arg1, arg2) \
+  PyBytes_FromStringAndSize((arg1), (arg2))
 
 PyMODINIT_FUNC PyInit_perf_trace_context(void);
 #endif
@@ -62,6 +70,23 @@ static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
 	return Py_BuildValue("i", common_lock_depth(c));
 }
 
+static PyObject *perf_sample_insn(PyObject *obj, PyObject *args)
+{
+	struct scripting_context *c = get_scripting_context(args);
+
+	if (!c)
+		return NULL;
+
+	if (c->sample->ip && !c->sample->insn_len &&
+	    c->al->thread->maps && c->al->thread->maps->machine)
+		script_fetch_insn(c->sample, c->al->thread, c->al->thread->maps->machine);
+
+	if (!c->sample->insn_len)
+		Py_RETURN_NONE; /* N.B. This is a return statement */
+
+	return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len);
+}
+
 static PyMethodDef ContextMethods[] = {
 	{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
 	  "Get the common preempt count event field value."},
@@ -69,6 +94,8 @@ static PyMethodDef ContextMethods[] = {
 	  "Get the common flags event field value."},
 	{ "common_lock_depth", perf_trace_context_common_lock_depth,
 	  METH_VARARGS,	"Get the common lock depth event field value."},
+	{ "perf_sample_insn", perf_sample_insn,
+	  METH_VARARGS,	"Get the machine code instruction."},
 	{ NULL, NULL, 0, NULL}
 };
 
Signature · 1 line
2.17.1
author reviewer
#8 lore raw

PATCH 8/13 perf auxtrace: Factor out itrace_do_parse_synth_opts()

In reply to Adrian Hunter

Factor out itrace_do_parse_synth_opts() so that it can be reused.

---
 tools/perf/util/auxtrace.c | 10 +++++++---
 tools/perf/util/auxtrace.h | 10 ++++++++++
 2 files changed, 17 insertions(+), 3 deletions(-)
tools/perf/util/auxtrace.c +7 -3
index 62268f8b6c4f..9350eeb3a3fc 100644
@@ -1405,10 +1405,9 @@ static int get_flags(const char **ptr, unsigned int *plus_flags, unsigned int *m
  * about the options parsed here, which is introduced after this cset,
  * when support in 'perf script' for these options is introduced.
  */
-int itrace_parse_synth_opts(const struct option *opt, const char *str,
-			    int unset)
+int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,
+			       const char *str, int unset)
 {
-	struct itrace_synth_opts *synth_opts = opt->value;
 	const char *p;
 	char *endptr;
 	bool period_type_set = false;
@@ -1596,6 +1595,11 @@ int itrace_parse_synth_opts(const struct option *opt, const char *str,
 	return -EINVAL;
 }
 
+int itrace_parse_synth_opts(const struct option *opt, const char *str, int unset)
+{
+	return itrace_do_parse_synth_opts(opt->value, str, unset);
+}
+
 static const char * const auxtrace_error_type_name[] = {
 	[PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
 };
tools/perf/util/auxtrace.h +10 -0
index 4283a4befcb6..efca761e8b84 100644
@@ -607,6 +607,8 @@ s64 perf_event__process_auxtrace(struct perf_session *session,
 				 union perf_event *event);
 int perf_event__process_auxtrace_error(struct perf_session *session,
 				       union perf_event *event);
+int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,
+			       const char *str, int unset);
 int itrace_parse_synth_opts(const struct option *opt, const char *str,
 			    int unset);
 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
@@ -738,6 +740,14 @@ void events_stats__auxtrace_error_warn(const struct events_stats *stats
 {
 }
 
+static inline
+int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts __maybe_unused,
+			       const char *str __maybe_unused, int unset __maybe_unused)
+{
+	pr_err("AUX area tracing not supported\n");
+	return -EINVAL;
+}
+
 static inline
 int itrace_parse_synth_opts(const struct option *opt __maybe_unused,
 			    const char *str __maybe_unused,
Signature · 1 line
2.17.1
author reviewer
#9 lore raw

PATCH 9/13 perf scripting python: Add perf_set_itrace_options()

In reply to Adrian Hunter

Add perf_set_itrace_options() to the perf_trace_context module so that a script can set the itrace options for a session if they have not been set already.

---
 .../scripts/python/Perf-Trace-Util/Context.c  | 44 ++++++++++++++++++-
 1 file changed, 42 insertions(+), 2 deletions(-)
tools/perf/scripts/python/Perf-Trace-Util/Context.c +42 -2
index d7f044259f9b..3c9bc12a1332 100644
@@ -11,12 +11,16 @@
 #include "../../../util/symbol.h"
 #include "../../../util/thread.h"
 #include "../../../util/maps.h"
+#include "../../../util/auxtrace.h"
+#include "../../../util/session.h"
 
 #if PY_MAJOR_VERSION < 3
 #define _PyCapsule_GetPointer(arg1, arg2) \
   PyCObject_AsVoidPtr(arg1)
 #define _PyBytes_FromStringAndSize(arg1, arg2) \
   PyString_FromStringAndSize((arg1), (arg2))
+#define _PyUnicode_AsUTF8(arg) \
+  PyString_AsString(arg)
 
 PyMODINIT_FUNC initperf_trace_context(void);
 #else
@@ -24,20 +28,28 @@ PyMODINIT_FUNC initperf_trace_context(void);
   PyCapsule_GetPointer((arg1), (arg2))
 #define _PyBytes_FromStringAndSize(arg1, arg2) \
   PyBytes_FromStringAndSize((arg1), (arg2))
+#define _PyUnicode_AsUTF8(arg) \
+  PyUnicode_AsUTF8(arg)
 
 PyMODINIT_FUNC PyInit_perf_trace_context(void);
 #endif
 
-static struct scripting_context *get_scripting_context(PyObject *args)
+static struct scripting_context *get_args(PyObject *args, const char *name, PyObject **arg2)
 {
+	int cnt = 1 + !!arg2;
 	PyObject *context;
 
-	if (!PyArg_ParseTuple(args, "O", &context))
+	if (!PyArg_UnpackTuple(args, name, 1, cnt, &context, arg2))
 		return NULL;
 
 	return _PyCapsule_GetPointer(context, NULL);
 }
 
+static struct scripting_context *get_scripting_context(PyObject *args)
+{
+	return get_args(args, "context", NULL);
+}
+
 static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
 {
 	struct scripting_context *c = get_scripting_context(args);
@@ -87,6 +99,32 @@ static PyObject *perf_sample_insn(PyObject *obj, PyObject *args)
 	return _PyBytes_FromStringAndSize(c->sample->insn, c->sample->insn_len);
 }
 
+static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args)
+{
+	struct scripting_context *c;
+	const char *itrace_options;
+	int retval = -1;
+	PyObject *str;
+
+	c = get_args(args, "itrace_options", &str);
+	if (!c)
+		return NULL;
+
+	if (!c->session || !c->session->itrace_synth_opts)
+		goto out;
+
+	if (c->session->itrace_synth_opts->set) {
+		retval = 1;
+		goto out;
+	}
+
+	itrace_options = _PyUnicode_AsUTF8(str);
+
+	retval = itrace_do_parse_synth_opts(c->session->itrace_synth_opts, itrace_options, 0);
+out:
+	return Py_BuildValue("i", retval);
+}
+
 static PyMethodDef ContextMethods[] = {
 	{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
 	  "Get the common preempt count event field value."},
@@ -96,6 +134,8 @@ static PyMethodDef ContextMethods[] = {
 	  METH_VARARGS,	"Get the common lock depth event field value."},
 	{ "perf_sample_insn", perf_sample_insn,
 	  METH_VARARGS,	"Get the machine code instruction."},
+	{ "perf_set_itrace_options", perf_set_itrace_options,
+	  METH_VARARGS,	"Set --itrace options."},
 	{ NULL, NULL, 0, NULL}
 };
 
Signature · 1 line
2.17.1
author reviewer
#10 lore raw

PATCH 10/13 perf scripting python: Add perf_sample_srcline() and perf_sample_srccode()

In reply to Adrian Hunter

Add perf_sample_srcline() and perf_sample_srccode() to the perf_trace_context module so that a script can get the srcline or srccode information.

---
 .../scripts/python/Perf-Trace-Util/Context.c  | 56 +++++++++++++++++++
 1 file changed, 56 insertions(+)
tools/perf/scripts/python/Perf-Trace-Util/Context.c +56 -0
index 3c9bc12a1332..895f5fc23965 100644
@@ -5,14 +5,23 @@
  * Copyright (C) 2010 Tom Zanussi <[email protected]>
  */
 
+/*
+ * Use Py_ssize_t for '#' formats to avoid DeprecationWarning: PY_SSIZE_T_CLEAN
+ * will be required for '#' formats.
+ */
+#define PY_SSIZE_T_CLEAN
+
 #include <Python.h>
 #include "../../../util/trace-event.h"
 #include "../../../util/event.h"
 #include "../../../util/symbol.h"
 #include "../../../util/thread.h"
+#include "../../../util/map.h"
 #include "../../../util/maps.h"
 #include "../../../util/auxtrace.h"
 #include "../../../util/session.h"
+#include "../../../util/srcline.h"
+#include "../../../util/srccode.h"
 
 #if PY_MAJOR_VERSION < 3
 #define _PyCapsule_GetPointer(arg1, arg2) \
@@ -125,6 +134,49 @@ static PyObject *perf_set_itrace_options(PyObject *obj, PyObject *args)
 	return Py_BuildValue("i", retval);
 }
 
+static PyObject *perf_sample_src(PyObject *obj, PyObject *args, bool get_srccode)
+{
+	struct scripting_context *c = get_scripting_context(args);
+	unsigned int line = 0;
+	char *srcfile = NULL;
+	char *srccode = NULL;
+	PyObject *result;
+	struct map *map;
+	int len = 0;
+	u64 addr;
+
+	if (!c)
+		return NULL;
+
+	map = c->al->map;
+	addr = c->al->addr;
+
+	if (map && map->dso)
+		srcfile = get_srcline_split(map->dso, map__rip_2objdump(map, addr), &line);
+
+	if (get_srccode) {
+		if (srcfile)
+			srccode = find_sourceline(srcfile, line, &len);
+		result = Py_BuildValue("(sIs#)", srcfile, line, srccode, (Py_ssize_t)len);
+	} else {
+		result = Py_BuildValue("(sI)", srcfile, line);
+	}
+
+	free(srcfile);
+
+	return result;
+}
+
+static PyObject *perf_sample_srcline(PyObject *obj, PyObject *args)
+{
+	return perf_sample_src(obj, args, false);
+}
+
+static PyObject *perf_sample_srccode(PyObject *obj, PyObject *args)
+{
+	return perf_sample_src(obj, args, true);
+}
+
 static PyMethodDef ContextMethods[] = {
 	{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
 	  "Get the common preempt count event field value."},
@@ -136,6 +188,10 @@ static PyMethodDef ContextMethods[] = {
 	  METH_VARARGS,	"Get the machine code instruction."},
 	{ "perf_set_itrace_options", perf_set_itrace_options,
 	  METH_VARARGS,	"Set --itrace options."},
+	{ "perf_sample_srcline", perf_sample_srcline,
+	  METH_VARARGS,	"Get source file name and line number."},
+	{ "perf_sample_srccode", perf_sample_srccode,
+	  METH_VARARGS,	"Get source file name, line number and line."},
 	{ NULL, NULL, 0, NULL}
 };
 
Signature · 1 line
2.17.1
author reviewer
#11 lore raw

PATCH 11/13 perf scripting python: Update documentation for srcline etc

In reply to Adrian Hunter

Add new fields and functions to the perf-script-python documentation.

---
 .../perf/Documentation/perf-script-python.txt | 46 +++++++++++++++++--
 1 file changed, 42 insertions(+), 4 deletions(-)
tools/perf/Documentation/perf-script-python.txt +42 -4
index 0fb9eda3cbca..5e43cfa5ea1e 100644
@@ -550,6 +550,27 @@ def trace_unhandled(event_name, context, event_fields_dict):
     pass
 ----
 
+*process_event*, if defined, is called for any non-tracepoint event
+
+----
+def process_event(param_dict):
+    pass
+----
+
+*context_switch*, if defined, is called for any context switch
+
+----
+def context_switch(ts, cpu, pid, tid, np_pid, np_tid, machine_pid, out, out_preempt, *x):
+    pass
+----
+
+*auxtrace_error*, if defined, is called for any AUX area tracing error
+
+----
+def auxtrace_error(typ, code, cpu, pid, tid, ip, ts, msg, cpumode, *x):
+    pass
+----
+
 The remaining sections provide descriptions of each of the available
 built-in perf script Python modules and their associated functions.
 
@@ -592,12 +613,18 @@ common, but need to be made accessible to user scripts nonetheless.
 perf_trace_context defines a set of functions that can be used to
 access this data in the context of the current event.  Each of these
 functions expects a context variable, which is the same as the
-context variable passed into every event handler as the second
-argument.
+context variable passed into every tracepoint event handler as the second
+argument. For non-tracepoint events, the context variable is also present
+as perf_trace_context.perf_script_context .
 
  common_pc(context) - returns common_preempt count for the current event
  common_flags(context) - returns common_flags for the current event
  common_lock_depth(context) - returns common_lock_depth for the current event
+ perf_sample_insn(context) - returns the machine code instruction
+ perf_set_itrace_options(context, itrace_options) - set --itrace options if they have not been set already
+ perf_sample_srcline(context) - returns source_file_name, line_number
+ perf_sample_srccode(context) - returns source_file_name, line_number, source_line
+
 
 Util.py Module
 ~~~~~~~~~~~~~~
@@ -616,9 +643,20 @@ SUPPORTED FIELDS
 Currently supported fields:
 
 ev_name, comm, pid, tid, cpu, ip, time, period, phys_addr, addr,
-symbol, dso, time_enabled, time_running, values, callchain,
+symbol, symoff, dso, time_enabled, time_running, values, callchain,
 brstack, brstacksym, datasrc, datasrc_decode, iregs, uregs,
-weight, transaction, raw_buf, attr.
+weight, transaction, raw_buf, attr, cpumode.
+
+Fields that may also be present:
+
+ flags - sample flags
+ flags_disp - sample flags display
+ insn_cnt - instruction count for determining instructions-per-cycle (IPC)
+ cyc_cnt - cycle count for determining IPC
+ addr_correlates_sym - addr can correlate to a symbol
+ addr_dso - addr dso
+ addr_symbol - addr symbol
+ addr_symoff - addr symbol offset
 
 Some fields have sub items:
 
Signature · 1 line
2.17.1
author reviewer
#12 lore raw

PATCH 12/13 perf scripting python: exported-sql-viewer.py: Factor out libxed.py

In reply to Adrian Hunter

Factor out libxed.py so it can be reused.

---
 .../scripts/python/exported-sql-viewer.py     |  89 +--------------
 tools/perf/scripts/python/libxed.py           | 107 ++++++++++++++++++
 2 files changed, 108 insertions(+), 88 deletions(-)
 create mode 100644 tools/perf/scripts/python/libxed.py
tools/perf/scripts/python/exported-sql-viewer.py +1 -88
index 711d4f9f5645..13f2d8a81610 100755
@@ -113,6 +113,7 @@ import os
 import random
 import copy
 import math
+from libxed import LibXED
 
 pyside_version_1 = True
 if not "--pyside-version-1" in sys.argv:
@@ -4747,94 +4748,6 @@ class MainWindow(QMainWindow):
 		dialog = AboutDialog(self.glb, self)
 		dialog.exec_()
 
-# XED Disassembler
-
-class xed_state_t(Structure):
-
-	_fields_ = [
-		("mode", c_int),
-		("width", c_int)
-	]
-
-class XEDInstruction():
-
-	def __init__(self, libxed):
-		# Current xed_decoded_inst_t structure is 192 bytes. Use 512 to allow for future expansion
-		xedd_t = c_byte * 512
-		self.xedd = xedd_t()
-		self.xedp = addressof(self.xedd)
-		libxed.xed_decoded_inst_zero(self.xedp)
-		self.state = xed_state_t()
-		self.statep = addressof(self.state)
-		# Buffer for disassembled instruction text
-		self.buffer = create_string_buffer(256)
-		self.bufferp = addressof(self.buffer)
-
-class LibXED():
-
-	def __init__(self):
-		try:
-			self.libxed = CDLL("libxed.so")
-		except:
-			self.libxed = None
-		if not self.libxed:
-			self.libxed = CDLL("/usr/local/lib/libxed.so")
-
-		self.xed_tables_init = self.libxed.xed_tables_init
-		self.xed_tables_init.restype = None
-		self.xed_tables_init.argtypes = []
-
-		self.xed_decoded_inst_zero = self.libxed.xed_decoded_inst_zero
-		self.xed_decoded_inst_zero.restype = None
-		self.xed_decoded_inst_zero.argtypes = [ c_void_p ]
-
-		self.xed_operand_values_set_mode = self.libxed.xed_operand_values_set_mode
-		self.xed_operand_values_set_mode.restype = None
-		self.xed_operand_values_set_mode.argtypes = [ c_void_p, c_void_p ]
-
-		self.xed_decoded_inst_zero_keep_mode = self.libxed.xed_decoded_inst_zero_keep_mode
-		self.xed_decoded_inst_zero_keep_mode.restype = None
-		self.xed_decoded_inst_zero_keep_mode.argtypes = [ c_void_p ]
-
-		self.xed_decode = self.libxed.xed_decode
-		self.xed_decode.restype = c_int
-		self.xed_decode.argtypes = [ c_void_p, c_void_p, c_uint ]
-
-		self.xed_format_context = self.libxed.xed_format_context
-		self.xed_format_context.restype = c_uint
-		self.xed_format_context.argtypes = [ c_int, c_void_p, c_void_p, c_int, c_ulonglong, c_void_p, c_void_p ]
-
-		self.xed_tables_init()
-
-	def Instruction(self):
-		return XEDInstruction(self)
-
-	def SetMode(self, inst, mode):
-		if mode:
-			inst.state.mode = 4 # 32-bit
-			inst.state.width = 4 # 4 bytes
-		else:
-			inst.state.mode = 1 # 64-bit
-			inst.state.width = 8 # 8 bytes
-		self.xed_operand_values_set_mode(inst.xedp, inst.statep)
-
-	def DisassembleOne(self, inst, bytes_ptr, bytes_cnt, ip):
-		self.xed_decoded_inst_zero_keep_mode(inst.xedp)
-		err = self.xed_decode(inst.xedp, bytes_ptr, bytes_cnt)
-		if err:
-			return 0, ""
-		# Use AT&T mode (2), alternative is Intel (3)
-		ok = self.xed_format_context(2, inst.xedp, inst.bufferp, sizeof(inst.buffer), ip, 0, 0)
-		if not ok:
-			return 0, ""
-		if sys.version_info[0] == 2:
-			result = inst.buffer.value
-		else:
-			result = inst.buffer.value.decode()
-		# Return instruction length and the disassembled instruction text
-		# For now, assume the length is in byte 166
-		return inst.xedd[166], result
-
 def TryOpen(file_name):
 	try:
 		return open(file_name, "rb")
tools/perf/scripts/python/libxed.py new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
2.17.1
author reviewer
#13 lore raw

PATCH 13/13 perf scripting python: intel-pt-events.py: Add --insn-trace and --src-trace

In reply to Adrian Hunter

Add an instruction trace and a source trace to the intel-pt-events.py script.

---
 tools/perf/Documentation/perf-intel-pt.txt   |   6 +-
 tools/perf/scripts/python/intel-pt-events.py | 176 +++++++++++++++++--
 2 files changed, 163 insertions(+), 19 deletions(-)
tools/perf/Documentation/perf-intel-pt.txt +5 -1
index e382dbd4ff0a..184ba62420f0 100644
@@ -174,7 +174,11 @@ Refer to script export-to-sqlite.py or export-to-postgresql.py for more details,
 and to script exported-sql-viewer.py for an example of using the database.
 
 There is also script intel-pt-events.py which provides an example of how to
-unpack the raw data for power events and PTWRITE.
+unpack the raw data for power events and PTWRITE. The script also displays
+branches, and supports 2 additional modes selected by option:
+
+ --insn-trace - instruction trace
+ --src-trace - source trace
 
 As mentioned above, it is easy to capture too much data.  One way to limit the
 data captured is to use 'snapshot' mode which is explained further below.
tools/perf/scripts/python/intel-pt-events.py +158 -18

249 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer Applied
#14 lore raw
In reply to Adrian Hunter

Em Sun, May 30, 2021 at 10:22:55PM +0300, Adrian Hunter escreveu:

Quoted 11 lines

Hi

Here are some patches to add insn, srcline and srccode to python scripting. In addition, it is made possible for a script to set itrace options.

The first 2 patches are minor tidy-ups. The next 3 are additions to scripting_context. The next 5 add new methods that python scripts can call.

Then there is a patch to the perf scripting python documentation.

And finally 2 patches add to the intel-pt-events.py script as an example.

Thanks, applied.

- Arnaldo

Quoted 35 lines

Adrian Hunter (13):
perf scripting python: Remove unnecessary 'static'
perf scripting python: Simplify perf-trace-context module functions perf scripting: Add scripting_context__update()
perf scripting: Add perf_session to scripting_context
perf scripting python: Assign perf_script_context
perf script: Factor out script_fetch_insn()
perf scripting python: Add perf_sample_insn()
perf auxtrace: Factor out itrace_do_parse_synth_opts()
perf scripting python: Add perf_set_itrace_options()
perf scripting python: Add perf_sample_srcline() and perf_sample_srccode() perf scripting python: Update documentation for srcline etc perf scripting python: exported-sql-viewer.py: Factor out libxed.py perf scripting python: intel-pt-events.py: Add --insn-trace and --src-trace

 tools/perf/Documentation/perf-intel-pt.txt         |   6 +-
 tools/perf/Documentation/perf-script-python.txt    |  46 +++++-
 tools/perf/builtin-script.c                        |  12 +-
 .../perf/scripts/python/Perf-Trace-Util/Context.c  | 168 +++++++++++++++++---
 tools/perf/scripts/python/exported-sql-viewer.py   |  89 +----------
 tools/perf/scripts/python/intel-pt-events.py       | 176 ++++++++++++++++++---
 tools/perf/scripts/python/libxed.py                | 107 +++++++++++++
 tools/perf/util/auxtrace.c                         |  10 +-
 tools/perf/util/auxtrace.h                         |  10 ++
 .../perf/util/scripting-engines/trace-event-perl.c |  11 +-
 .../util/scripting-engines/trace-event-python.c    |  37 ++++-
 tools/perf/util/trace-event-scripting.c            |  27 +++-
 tools/perf/util/trace-event.h                      |  20 ++-
 13 files changed, 568 insertions(+), 151 deletions(-)
 create mode 100644 tools/perf/scripts/python/libxed.py

Regards
Adrian

Signature · 1 line
- Arnaldo