aboutsummaryrefslogtreecommitdiffstats
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.c69
1 files changed, 35 insertions, 34 deletions
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index cc75a3cef388..cd9774df3750 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -56,6 +56,17 @@ static void handler_call_die(const char *handler_name)
56 Py_FatalError("problem in Python trace event handler"); 56 Py_FatalError("problem in Python trace event handler");
57} 57}
58 58
59/*
60 * Insert val into into the dictionary and decrement the reference counter.
61 * This is necessary for dictionaries since PyDict_SetItemString() does not
62 * steal a reference, as opposed to PyTuple_SetItem().
63 */
64static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
65{
66 PyDict_SetItemString(dict, key, val);
67 Py_DECREF(val);
68}
69
59static void define_value(enum print_arg_type field_type, 70static void define_value(enum print_arg_type field_type,
60 const char *ev_name, 71 const char *ev_name,
61 const char *field_name, 72 const char *field_name,
@@ -150,8 +161,7 @@ static void define_event_symbols(struct event_format *event,
150 zero_flag_atom = 0; 161 zero_flag_atom = 0;
151 break; 162 break;
152 case PRINT_FIELD: 163 case PRINT_FIELD:
153 if (cur_field_name) 164 free(cur_field_name);
154 free(cur_field_name);
155 cur_field_name = strdup(args->field.name); 165 cur_field_name = strdup(args->field.name);
156 break; 166 break;
157 case PRINT_FLAGS: 167 case PRINT_FLAGS:
@@ -220,13 +230,10 @@ static inline struct event_format *find_cache_event(struct perf_evsel *evsel)
220 return event; 230 return event;
221} 231}
222 232
223static void python_process_tracepoint(union perf_event *perf_event 233static void python_process_tracepoint(struct perf_sample *sample,
224 __maybe_unused, 234 struct perf_evsel *evsel,
225 struct perf_sample *sample, 235 struct thread *thread,
226 struct perf_evsel *evsel, 236 struct addr_location *al)
227 struct machine *machine __maybe_unused,
228 struct thread *thread,
229 struct addr_location *al)
230{ 237{
231 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL; 238 PyObject *handler, *retval, *context, *t, *obj, *dict = NULL;
232 static char handler_name[256]; 239 static char handler_name[256];
@@ -239,7 +246,7 @@ static void python_process_tracepoint(union perf_event *perf_event
239 int cpu = sample->cpu; 246 int cpu = sample->cpu;
240 void *data = sample->raw_data; 247 void *data = sample->raw_data;
241 unsigned long long nsecs = sample->time; 248 unsigned long long nsecs = sample->time;
242 char *comm = thread->comm; 249 const char *comm = thread__comm_str(thread);
243 250
244 t = PyTuple_New(MAX_FIELDS); 251 t = PyTuple_New(MAX_FIELDS);
245 if (!t) 252 if (!t)
@@ -279,11 +286,11 @@ static void python_process_tracepoint(union perf_event *perf_event
279 PyTuple_SetItem(t, n++, PyInt_FromLong(pid)); 286 PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
280 PyTuple_SetItem(t, n++, PyString_FromString(comm)); 287 PyTuple_SetItem(t, n++, PyString_FromString(comm));
281 } else { 288 } else {
282 PyDict_SetItemString(dict, "common_cpu", PyInt_FromLong(cpu)); 289 pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
283 PyDict_SetItemString(dict, "common_s", PyInt_FromLong(s)); 290 pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
284 PyDict_SetItemString(dict, "common_ns", PyInt_FromLong(ns)); 291 pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
285 PyDict_SetItemString(dict, "common_pid", PyInt_FromLong(pid)); 292 pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
286 PyDict_SetItemString(dict, "common_comm", PyString_FromString(comm)); 293 pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
287 } 294 }
288 for (field = event->format.fields; field; field = field->next) { 295 for (field = event->format.fields; field; field = field->next) {
289 if (field->flags & FIELD_IS_STRING) { 296 if (field->flags & FIELD_IS_STRING) {
@@ -313,7 +320,7 @@ static void python_process_tracepoint(union perf_event *perf_event
313 if (handler) 320 if (handler)
314 PyTuple_SetItem(t, n++, obj); 321 PyTuple_SetItem(t, n++, obj);
315 else 322 else
316 PyDict_SetItemString(dict, field->name, obj); 323 pydict_set_item_string_decref(dict, field->name, obj);
317 324
318 } 325 }
319 if (!handler) 326 if (!handler)
@@ -340,11 +347,8 @@ static void python_process_tracepoint(union perf_event *perf_event
340 Py_DECREF(t); 347 Py_DECREF(t);
341} 348}
342 349
343static void python_process_general_event(union perf_event *perf_event 350static void python_process_general_event(struct perf_sample *sample,
344 __maybe_unused,
345 struct perf_sample *sample,
346 struct perf_evsel *evsel, 351 struct perf_evsel *evsel,
347 struct machine *machine __maybe_unused,
348 struct thread *thread, 352 struct thread *thread,
349 struct addr_location *al) 353 struct addr_location *al)
350{ 354{
@@ -370,21 +374,21 @@ static void python_process_general_event(union perf_event *perf_event
370 if (!handler || !PyCallable_Check(handler)) 374 if (!handler || !PyCallable_Check(handler))
371 goto exit; 375 goto exit;
372 376
373 PyDict_SetItemString(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); 377 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
374 PyDict_SetItemString(dict, "attr", PyString_FromStringAndSize( 378 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
375 (const char *)&evsel->attr, sizeof(evsel->attr))); 379 (const char *)&evsel->attr, sizeof(evsel->attr)));
376 PyDict_SetItemString(dict, "sample", PyString_FromStringAndSize( 380 pydict_set_item_string_decref(dict, "sample", PyString_FromStringAndSize(
377 (const char *)sample, sizeof(*sample))); 381 (const char *)sample, sizeof(*sample)));
378 PyDict_SetItemString(dict, "raw_buf", PyString_FromStringAndSize( 382 pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
379 (const char *)sample->raw_data, sample->raw_size)); 383 (const char *)sample->raw_data, sample->raw_size));
380 PyDict_SetItemString(dict, "comm", 384 pydict_set_item_string_decref(dict, "comm",
381 PyString_FromString(thread->comm)); 385 PyString_FromString(thread__comm_str(thread)));
382 if (al->map) { 386 if (al->map) {
383 PyDict_SetItemString(dict, "dso", 387 pydict_set_item_string_decref(dict, "dso",
384 PyString_FromString(al->map->dso->name)); 388 PyString_FromString(al->map->dso->name));
385 } 389 }
386 if (al->sym) { 390 if (al->sym) {
387 PyDict_SetItemString(dict, "symbol", 391 pydict_set_item_string_decref(dict, "symbol",
388 PyString_FromString(al->sym->name)); 392 PyString_FromString(al->sym->name));
389 } 393 }
390 394
@@ -400,22 +404,19 @@ exit:
400 Py_DECREF(t); 404 Py_DECREF(t);
401} 405}
402 406
403static void python_process_event(union perf_event *perf_event, 407static void python_process_event(union perf_event *event __maybe_unused,
404 struct perf_sample *sample, 408 struct perf_sample *sample,
405 struct perf_evsel *evsel, 409 struct perf_evsel *evsel,
406 struct machine *machine,
407 struct thread *thread, 410 struct thread *thread,
408 struct addr_location *al) 411 struct addr_location *al)
409{ 412{
410 switch (evsel->attr.type) { 413 switch (evsel->attr.type) {
411 case PERF_TYPE_TRACEPOINT: 414 case PERF_TYPE_TRACEPOINT:
412 python_process_tracepoint(perf_event, sample, evsel, 415 python_process_tracepoint(sample, evsel, thread, al);
413 machine, thread, al);
414 break; 416 break;
415 /* Reserve for future process_hw/sw/raw APIs */ 417 /* Reserve for future process_hw/sw/raw APIs */
416 default: 418 default:
417 python_process_general_event(perf_event, sample, evsel, 419 python_process_general_event(sample, evsel, thread, al);
418 machine, thread, al);
419 } 420 }
420} 421}
421 422