summaryrefslogtreecommitdiff
path: root/tools/perf/util/scripting-engines/trace-event-python.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf/util/scripting-engines/trace-event-python.c')
-rw-r--r--tools/perf/util/scripting-engines/trace-event-python.c133
1 files changed, 83 insertions, 50 deletions
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index b1b5e94537e4..8f832ae316ca 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -50,6 +50,7 @@
#include "../thread-stack.h"
#include "../trace-event.h"
#include "../call-path.h"
+#include "dwarf-regs.h"
#include "map.h"
#include "symbol.h"
#include "thread_map.h"
@@ -389,7 +390,6 @@ static unsigned long get_offset(struct symbol *sym, struct addr_location *al)
}
static PyObject *python_process_callchain(struct perf_sample *sample,
- struct evsel *evsel,
struct addr_location *al)
{
PyObject *pylist;
@@ -403,7 +403,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,
goto exit;
cursor = get_tls_callchain_cursor();
- if (thread__resolve_callchain(al->thread, cursor, evsel,
+ if (thread__resolve_callchain(al->thread, cursor,
sample, NULL, NULL,
scripting_max_stack) != 0) {
pr_err("Failed to resolve callchain. Skipping\n");
@@ -436,7 +436,7 @@ static PyObject *python_process_callchain(struct perf_sample *sample,
pydict_set_item_string_decref(pysym, "end",
PyLong_FromUnsignedLongLong(node->ms.sym->end));
pydict_set_item_string_decref(pysym, "binding",
- _PyLong_FromLong(node->ms.sym->binding));
+ _PyLong_FromLong(symbol__binding(node->ms.sym)));
pydict_set_item_string_decref(pysym, "name",
_PyUnicode_FromStringAndSize(node->ms.sym->name,
node->ms.sym->namelen));
@@ -650,11 +650,9 @@ static PyObject *get_sample_value_as_tuple(struct sample_read_value *value,
return t;
}
-static void set_sample_read_in_dict(PyObject *dict_sample,
- struct perf_sample *sample,
- struct evsel *evsel)
+static void set_sample_read_in_dict(PyObject *dict_sample, struct perf_sample *sample)
{
- u64 read_format = evsel->core.attr.read_format;
+ u64 read_format = sample->evsel->core.attr.read_format;
PyObject *values;
unsigned int i;
@@ -713,7 +711,8 @@ static void set_sample_datasrc_in_dict(PyObject *dict,
_PyUnicode_FromString(decode));
}
-static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, char *bf, int size)
+static void regs_map(struct regs_dump *regs, uint64_t mask, uint16_t e_machine, uint32_t e_flags,
+ char *bf, int size)
{
unsigned int i = 0, r;
int printed = 0;
@@ -731,7 +730,7 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, ch
printed += scnprintf(bf + printed, size - printed,
"%5s:0x%" PRIx64 " ",
- perf_reg_name(r, arch), val);
+ perf_reg_name(r, e_machine, e_flags), val);
}
}
@@ -739,25 +738,36 @@ static void regs_map(struct regs_dump *regs, uint64_t mask, const char *arch, ch
static int set_regs_in_dict(PyObject *dict,
struct perf_sample *sample,
- struct evsel *evsel)
+ uint16_t e_machine,
+ uint32_t e_flags)
{
- struct perf_event_attr *attr = &evsel->core.attr;
- const char *arch = perf_env__arch(evsel__env(evsel));
+ struct perf_event_attr *attr = &sample->evsel->core.attr;
int size = (__sw_hweight64(attr->sample_regs_intr) * MAX_REG_SIZE) + 1;
- char *bf = malloc(size);
- if (!bf)
- return -1;
+ char *bf = NULL;
- regs_map(&sample->intr_regs, attr->sample_regs_intr, arch, bf, size);
+ if (sample->intr_regs) {
+ bf = malloc(size);
+ if (!bf)
+ return -1;
- pydict_set_item_string_decref(dict, "iregs",
- _PyUnicode_FromString(bf));
+ regs_map(sample->intr_regs, attr->sample_regs_intr, e_machine, e_flags, bf, size);
- regs_map(&sample->user_regs, attr->sample_regs_user, arch, bf, size);
+ pydict_set_item_string_decref(dict, "iregs",
+ _PyUnicode_FromString(bf));
+ }
- pydict_set_item_string_decref(dict, "uregs",
- _PyUnicode_FromString(bf));
+ if (sample->user_regs) {
+ if (!bf) {
+ bf = malloc(size);
+ if (!bf)
+ return -1;
+ }
+ regs_map(sample->user_regs, attr->sample_regs_user, e_machine, e_flags, bf, size);
+
+ pydict_set_item_string_decref(dict, "uregs",
+ _PyUnicode_FromString(bf));
+ }
free(bf);
return 0;
@@ -769,14 +779,13 @@ static void set_sym_in_dict(PyObject *dict, struct addr_location *al,
const char *sym_field, const char *symoff_field,
const char *map_pgoff)
{
- char sbuild_id[SBUILD_ID_SIZE];
-
if (al->map) {
+ char sbuild_id[SBUILD_ID_SIZE];
struct dso *dso = map__dso(al->map);
pydict_set_item_string_decref(dict, dso_field,
_PyUnicode_FromString(dso__name(dso)));
- build_id__sprintf(dso__bid(dso), sbuild_id);
+ build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id));
pydict_set_item_string_decref(dict, dso_bid_field,
_PyUnicode_FromString(sbuild_id));
pydict_set_item_string_decref(dict, dso_map_start,
@@ -818,12 +827,14 @@ static void python_process_sample_flags(struct perf_sample *sample, PyObject *di
}
static PyObject *get_perf_sample_dict(struct perf_sample *sample,
- struct evsel *evsel,
struct addr_location *al,
struct addr_location *addr_al,
PyObject *callchain)
{
PyObject *dict, *dict_sample, *brstack, *brstacksym;
+ uint16_t e_machine = EM_HOST;
+ uint32_t e_flags = EF_HOST;
+ struct evsel *evsel = sample->evsel;
dict = PyDict_New();
if (!dict)
@@ -856,7 +867,7 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
PyLong_FromUnsignedLongLong(sample->phys_addr));
pydict_set_item_string_decref(dict_sample, "addr",
PyLong_FromUnsignedLongLong(sample->addr));
- set_sample_read_in_dict(dict_sample, sample, evsel);
+ set_sample_read_in_dict(dict_sample, sample);
pydict_set_item_string_decref(dict_sample, "weight",
PyLong_FromUnsignedLongLong(sample->weight));
pydict_set_item_string_decref(dict_sample, "ins_lat",
@@ -910,7 +921,10 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
PyLong_FromUnsignedLongLong(sample->cyc_cnt));
}
- if (set_regs_in_dict(dict, sample, evsel))
+ if (al->thread)
+ e_machine = thread__e_machine(al->thread, /*machine=*/NULL, &e_flags);
+
+ if (set_regs_in_dict(dict, sample, e_machine, e_flags))
Py_FatalError("Failed to setting regs in dict");
return dict;
@@ -918,7 +932,6 @@ static PyObject *get_perf_sample_dict(struct perf_sample *sample,
#ifdef HAVE_LIBTRACEEVENT
static void python_process_tracepoint(struct perf_sample *sample,
- struct evsel *evsel,
struct addr_location *al,
struct addr_location *addr_al)
{
@@ -936,6 +949,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
const char *comm = thread__comm_str(al->thread);
const char *default_handler_name = "trace_unhandled";
DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
+ struct evsel *evsel = sample->evsel;
bitmap_zero(events_defined, TRACE_EVENT_TYPE_MAX);
@@ -977,7 +991,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
PyTuple_SetItem(t, n++, context);
/* ip unwinding */
- callchain = python_process_callchain(sample, evsel, al);
+ callchain = python_process_callchain(sample, al);
/* Need an additional reference for the perf_sample dict */
Py_INCREF(callchain);
@@ -1033,7 +1047,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
PyTuple_SetItem(t, n++, dict);
if (get_argument_count(handler) == (int) n + 1) {
- all_entries_dict = get_perf_sample_dict(sample, evsel, al, addr_al,
+ all_entries_dict = get_perf_sample_dict(sample, al, addr_al,
callchain);
PyTuple_SetItem(t, n++, all_entries_dict);
} else {
@@ -1052,7 +1066,6 @@ static void python_process_tracepoint(struct perf_sample *sample,
}
#else
static void python_process_tracepoint(struct perf_sample *sample __maybe_unused,
- struct evsel *evsel __maybe_unused,
struct addr_location *al __maybe_unused,
struct addr_location *addr_al __maybe_unused)
{
@@ -1227,7 +1240,7 @@ static int python_export_dso(struct db_export *dbe, struct dso *dso,
char sbuild_id[SBUILD_ID_SIZE];
PyObject *t;
- build_id__sprintf(dso__bid(dso), sbuild_id);
+ build_id__snprintf(dso__bid(dso), sbuild_id, sizeof(sbuild_id));
t = tuple_new(5);
@@ -1257,7 +1270,7 @@ static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
tuple_set_d64(t, 1, dso__db_id(dso));
tuple_set_d64(t, 2, sym->start);
tuple_set_d64(t, 3, sym->end);
- tuple_set_s32(t, 4, sym->binding);
+ tuple_set_s32(t, 4, symbol__binding(sym));
tuple_set_string(t, 5, sym->name);
call_object(tables->symbol_handler, t, "symbol_table");
@@ -1294,8 +1307,8 @@ static void python_export_sample_table(struct db_export *dbe,
t = tuple_new(28);
tuple_set_d64(t, 0, es->db_id);
- tuple_set_d64(t, 1, es->evsel->db_id);
- tuple_set_d64(t, 2, maps__machine(es->al->maps)->db_id);
+ tuple_set_d64(t, 1, es->sample->evsel->db_id);
+ tuple_set_d64(t, 2, maps__machine(thread__maps(es->al->thread))->db_id);
tuple_set_d64(t, 3, thread__db_id(es->al->thread));
tuple_set_d64(t, 4, es->comm_db_id);
tuple_set_d64(t, 5, es->dso_db_id);
@@ -1335,7 +1348,7 @@ static void python_export_synth(struct db_export *dbe, struct export_sample *es)
t = tuple_new(3);
tuple_set_d64(t, 0, es->db_id);
- tuple_set_d64(t, 1, es->evsel->core.attr.config);
+ tuple_set_d64(t, 1, es->sample->evsel->core.attr.config);
tuple_set_bytes(t, 2, es->sample->raw_data, es->sample->raw_size);
call_object(tables->synth_handler, t, "synth_data");
@@ -1350,7 +1363,7 @@ static int python_export_sample(struct db_export *dbe,
python_export_sample_table(dbe, es);
- if (es->evsel->core.attr.type == PERF_TYPE_SYNTH && tables->synth_handler)
+ if (es->sample->evsel->core.attr.type == PERF_TYPE_SYNTH && tables->synth_handler)
python_export_synth(dbe, es);
return 0;
@@ -1447,7 +1460,6 @@ static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
}
static void python_process_general_event(struct perf_sample *sample,
- struct evsel *evsel,
struct addr_location *al,
struct addr_location *addr_al)
{
@@ -1470,8 +1482,8 @@ static void python_process_general_event(struct perf_sample *sample,
Py_FatalError("couldn't create Python tuple");
/* ip unwinding */
- callchain = python_process_callchain(sample, evsel, al);
- dict = get_perf_sample_dict(sample, evsel, al, addr_al, callchain);
+ callchain = python_process_callchain(sample, al);
+ dict = get_perf_sample_dict(sample, al, addr_al, callchain);
PyTuple_SetItem(t, n++, dict);
if (_PyTuple_Resize(&t, n) == -1)
@@ -1484,24 +1496,23 @@ static void python_process_general_event(struct perf_sample *sample,
static void python_process_event(union perf_event *event,
struct perf_sample *sample,
- struct evsel *evsel,
struct addr_location *al,
struct addr_location *addr_al)
{
struct tables *tables = &tables_global;
- scripting_context__update(scripting_context, event, sample, evsel, al, addr_al);
+ scripting_context__update(scripting_context, event, sample, al, addr_al);
- switch (evsel->core.attr.type) {
+ switch (sample->evsel->core.attr.type) {
case PERF_TYPE_TRACEPOINT:
- python_process_tracepoint(sample, evsel, al, addr_al);
+ python_process_tracepoint(sample, al, addr_al);
break;
/* Reserve for future process_hw/sw/raw APIs */
default:
if (tables->db_export_mode)
- db_export__sample(&tables->dbe, event, sample, evsel, al, addr_al);
+ db_export__sample(&tables->dbe, event, sample, al, addr_al);
else
- python_process_general_event(sample, evsel, al, addr_al);
+ python_process_general_event(sample, al, addr_al);
}
}
@@ -1596,6 +1607,9 @@ static void python_process_auxtrace_error(struct perf_session *session __maybe_u
const char *handler_name = "auxtrace_error";
unsigned long long tm = e->time;
const char *msg = e->msg;
+ s32 machine_pid = 0, vcpu = 0;
+ char msg_buf[MAX_AUXTRACE_ERROR_MSG + 1];
+ int msg_max;
PyObject *handler, *t;
handler = get_handler(handler_name);
@@ -1607,6 +1621,25 @@ static void python_process_auxtrace_error(struct perf_session *session __maybe_u
msg = (const char *)&e->time;
}
+ /* Bound msg to the bytes within the event, ensure NUL-termination */
+ msg_max = (int)((void *)event + event->header.size - (void *)msg);
+ if (msg_max <= 0) {
+ msg_buf[0] = '\0';
+ } else {
+ if (msg_max > (int)sizeof(msg_buf) - 1)
+ msg_max = sizeof(msg_buf) - 1;
+ memcpy(msg_buf, msg, msg_max);
+ msg_buf[msg_max] = '\0';
+ }
+
+ /* Only access fmt >= 2 fields if the event is large enough */
+ if (e->fmt >= 2 &&
+ event->header.size >= offsetof(typeof(event->auxtrace_error), vcpu) +
+ sizeof(event->auxtrace_error.vcpu)) {
+ machine_pid = e->machine_pid;
+ vcpu = e->vcpu;
+ }
+
t = tuple_new(11);
tuple_set_u32(t, 0, e->type);
@@ -1616,10 +1649,10 @@ static void python_process_auxtrace_error(struct perf_session *session __maybe_u
tuple_set_s32(t, 4, e->tid);
tuple_set_u64(t, 5, e->ip);
tuple_set_u64(t, 6, tm);
- tuple_set_string(t, 7, msg);
+ tuple_set_string(t, 7, msg_buf);
tuple_set_u32(t, 8, cpumode);
- tuple_set_s32(t, 9, e->machine_pid);
- tuple_set_s32(t, 10, e->vcpu);
+ tuple_set_s32(t, 9, machine_pid);
+ tuple_set_s32(t, 10, vcpu);
call_object(handler, t, handler_name);
@@ -1683,7 +1716,7 @@ static void python_process_stat(struct perf_stat_config *config,
struct perf_cpu_map *cpus = counter->core.cpus;
for (int thread = 0; thread < perf_thread_map__nr(threads); thread++) {
- int idx;
+ unsigned int idx;
struct perf_cpu cpu;
perf_cpu_map__for_each_cpu(cpu, idx, cpus) {