trace-event-python.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. /*
  2. * trace-event-python. Feed trace events to an embedded Python interpreter.
  3. *
  4. * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <Python.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdbool.h>
  26. #include <errno.h>
  27. #include <linux/bitmap.h>
  28. #include "../../perf.h"
  29. #include "../debug.h"
  30. #include "../callchain.h"
  31. #include "../evsel.h"
  32. #include "../util.h"
  33. #include "../event.h"
  34. #include "../thread.h"
  35. #include "../comm.h"
  36. #include "../machine.h"
  37. #include "../db-export.h"
  38. #include "../thread-stack.h"
  39. #include "../trace-event.h"
  40. #include "../machine.h"
  41. #include "../call-path.h"
  42. #include "thread_map.h"
  43. #include "cpumap.h"
  44. #include "stat.h"
  45. PyMODINIT_FUNC initperf_trace_context(void);
  46. #define TRACE_EVENT_TYPE_MAX \
  47. ((1 << (sizeof(unsigned short) * 8)) - 1)
  48. static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
  49. #define MAX_FIELDS 64
  50. #define N_COMMON_FIELDS 7
  51. extern struct scripting_context *scripting_context;
  52. static char *cur_field_name;
  53. static int zero_flag_atom;
  54. static PyObject *main_module, *main_dict;
  55. struct tables {
  56. struct db_export dbe;
  57. PyObject *evsel_handler;
  58. PyObject *machine_handler;
  59. PyObject *thread_handler;
  60. PyObject *comm_handler;
  61. PyObject *comm_thread_handler;
  62. PyObject *dso_handler;
  63. PyObject *symbol_handler;
  64. PyObject *branch_type_handler;
  65. PyObject *sample_handler;
  66. PyObject *call_path_handler;
  67. PyObject *call_return_handler;
  68. bool db_export_mode;
  69. };
  70. static struct tables tables_global;
  71. static void handler_call_die(const char *handler_name) NORETURN;
  72. static void handler_call_die(const char *handler_name)
  73. {
  74. PyErr_Print();
  75. Py_FatalError("problem in Python trace event handler");
  76. // Py_FatalError does not return
  77. // but we have to make the compiler happy
  78. abort();
  79. }
  80. /*
  81. * Insert val into into the dictionary and decrement the reference counter.
  82. * This is necessary for dictionaries since PyDict_SetItemString() does not
  83. * steal a reference, as opposed to PyTuple_SetItem().
  84. */
  85. static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObject *val)
  86. {
  87. PyDict_SetItemString(dict, key, val);
  88. Py_DECREF(val);
  89. }
  90. static PyObject *get_handler(const char *handler_name)
  91. {
  92. PyObject *handler;
  93. handler = PyDict_GetItemString(main_dict, handler_name);
  94. if (handler && !PyCallable_Check(handler))
  95. return NULL;
  96. return handler;
  97. }
  98. static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
  99. {
  100. PyObject *retval;
  101. retval = PyObject_CallObject(handler, args);
  102. if (retval == NULL)
  103. handler_call_die(die_msg);
  104. Py_DECREF(retval);
  105. }
  106. static void try_call_object(const char *handler_name, PyObject *args)
  107. {
  108. PyObject *handler;
  109. handler = get_handler(handler_name);
  110. if (handler)
  111. call_object(handler, args, handler_name);
  112. }
  113. static void define_value(enum print_arg_type field_type,
  114. const char *ev_name,
  115. const char *field_name,
  116. const char *field_value,
  117. const char *field_str)
  118. {
  119. const char *handler_name = "define_flag_value";
  120. PyObject *t;
  121. unsigned long long value;
  122. unsigned n = 0;
  123. if (field_type == PRINT_SYMBOL)
  124. handler_name = "define_symbolic_value";
  125. t = PyTuple_New(4);
  126. if (!t)
  127. Py_FatalError("couldn't create Python tuple");
  128. value = eval_flag(field_value);
  129. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  130. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  131. PyTuple_SetItem(t, n++, PyInt_FromLong(value));
  132. PyTuple_SetItem(t, n++, PyString_FromString(field_str));
  133. try_call_object(handler_name, t);
  134. Py_DECREF(t);
  135. }
  136. static void define_values(enum print_arg_type field_type,
  137. struct print_flag_sym *field,
  138. const char *ev_name,
  139. const char *field_name)
  140. {
  141. define_value(field_type, ev_name, field_name, field->value,
  142. field->str);
  143. if (field->next)
  144. define_values(field_type, field->next, ev_name, field_name);
  145. }
  146. static void define_field(enum print_arg_type field_type,
  147. const char *ev_name,
  148. const char *field_name,
  149. const char *delim)
  150. {
  151. const char *handler_name = "define_flag_field";
  152. PyObject *t;
  153. unsigned n = 0;
  154. if (field_type == PRINT_SYMBOL)
  155. handler_name = "define_symbolic_field";
  156. if (field_type == PRINT_FLAGS)
  157. t = PyTuple_New(3);
  158. else
  159. t = PyTuple_New(2);
  160. if (!t)
  161. Py_FatalError("couldn't create Python tuple");
  162. PyTuple_SetItem(t, n++, PyString_FromString(ev_name));
  163. PyTuple_SetItem(t, n++, PyString_FromString(field_name));
  164. if (field_type == PRINT_FLAGS)
  165. PyTuple_SetItem(t, n++, PyString_FromString(delim));
  166. try_call_object(handler_name, t);
  167. Py_DECREF(t);
  168. }
  169. static void define_event_symbols(struct event_format *event,
  170. const char *ev_name,
  171. struct print_arg *args)
  172. {
  173. if (args == NULL)
  174. return;
  175. switch (args->type) {
  176. case PRINT_NULL:
  177. break;
  178. case PRINT_ATOM:
  179. define_value(PRINT_FLAGS, ev_name, cur_field_name, "0",
  180. args->atom.atom);
  181. zero_flag_atom = 0;
  182. break;
  183. case PRINT_FIELD:
  184. free(cur_field_name);
  185. cur_field_name = strdup(args->field.name);
  186. break;
  187. case PRINT_FLAGS:
  188. define_event_symbols(event, ev_name, args->flags.field);
  189. define_field(PRINT_FLAGS, ev_name, cur_field_name,
  190. args->flags.delim);
  191. define_values(PRINT_FLAGS, args->flags.flags, ev_name,
  192. cur_field_name);
  193. break;
  194. case PRINT_SYMBOL:
  195. define_event_symbols(event, ev_name, args->symbol.field);
  196. define_field(PRINT_SYMBOL, ev_name, cur_field_name, NULL);
  197. define_values(PRINT_SYMBOL, args->symbol.symbols, ev_name,
  198. cur_field_name);
  199. break;
  200. case PRINT_HEX:
  201. define_event_symbols(event, ev_name, args->hex.field);
  202. define_event_symbols(event, ev_name, args->hex.size);
  203. break;
  204. case PRINT_INT_ARRAY:
  205. define_event_symbols(event, ev_name, args->int_array.field);
  206. define_event_symbols(event, ev_name, args->int_array.count);
  207. define_event_symbols(event, ev_name, args->int_array.el_size);
  208. break;
  209. case PRINT_STRING:
  210. break;
  211. case PRINT_TYPE:
  212. define_event_symbols(event, ev_name, args->typecast.item);
  213. break;
  214. case PRINT_OP:
  215. if (strcmp(args->op.op, ":") == 0)
  216. zero_flag_atom = 1;
  217. define_event_symbols(event, ev_name, args->op.left);
  218. define_event_symbols(event, ev_name, args->op.right);
  219. break;
  220. default:
  221. /* gcc warns for these? */
  222. case PRINT_BSTRING:
  223. case PRINT_DYNAMIC_ARRAY:
  224. case PRINT_DYNAMIC_ARRAY_LEN:
  225. case PRINT_FUNC:
  226. case PRINT_BITMASK:
  227. /* we should warn... */
  228. return;
  229. }
  230. if (args->next)
  231. define_event_symbols(event, ev_name, args->next);
  232. }
  233. static PyObject *get_field_numeric_entry(struct event_format *event,
  234. struct format_field *field, void *data)
  235. {
  236. bool is_array = field->flags & FIELD_IS_ARRAY;
  237. PyObject *obj, *list = NULL;
  238. unsigned long long val;
  239. unsigned int item_size, n_items, i;
  240. if (is_array) {
  241. list = PyList_New(field->arraylen);
  242. item_size = field->size / field->arraylen;
  243. n_items = field->arraylen;
  244. } else {
  245. item_size = field->size;
  246. n_items = 1;
  247. }
  248. for (i = 0; i < n_items; i++) {
  249. val = read_size(event, data + field->offset + i * item_size,
  250. item_size);
  251. if (field->flags & FIELD_IS_SIGNED) {
  252. if ((long long)val >= LONG_MIN &&
  253. (long long)val <= LONG_MAX)
  254. obj = PyInt_FromLong(val);
  255. else
  256. obj = PyLong_FromLongLong(val);
  257. } else {
  258. if (val <= LONG_MAX)
  259. obj = PyInt_FromLong(val);
  260. else
  261. obj = PyLong_FromUnsignedLongLong(val);
  262. }
  263. if (is_array)
  264. PyList_SET_ITEM(list, i, obj);
  265. }
  266. if (is_array)
  267. obj = list;
  268. return obj;
  269. }
  270. static PyObject *python_process_callchain(struct perf_sample *sample,
  271. struct perf_evsel *evsel,
  272. struct addr_location *al)
  273. {
  274. PyObject *pylist;
  275. pylist = PyList_New(0);
  276. if (!pylist)
  277. Py_FatalError("couldn't create Python list");
  278. if (!symbol_conf.use_callchain || !sample->callchain)
  279. goto exit;
  280. if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
  281. sample, NULL, NULL,
  282. scripting_max_stack) != 0) {
  283. pr_err("Failed to resolve callchain. Skipping\n");
  284. goto exit;
  285. }
  286. callchain_cursor_commit(&callchain_cursor);
  287. while (1) {
  288. PyObject *pyelem;
  289. struct callchain_cursor_node *node;
  290. node = callchain_cursor_current(&callchain_cursor);
  291. if (!node)
  292. break;
  293. pyelem = PyDict_New();
  294. if (!pyelem)
  295. Py_FatalError("couldn't create Python dictionary");
  296. pydict_set_item_string_decref(pyelem, "ip",
  297. PyLong_FromUnsignedLongLong(node->ip));
  298. if (node->sym) {
  299. PyObject *pysym = PyDict_New();
  300. if (!pysym)
  301. Py_FatalError("couldn't create Python dictionary");
  302. pydict_set_item_string_decref(pysym, "start",
  303. PyLong_FromUnsignedLongLong(node->sym->start));
  304. pydict_set_item_string_decref(pysym, "end",
  305. PyLong_FromUnsignedLongLong(node->sym->end));
  306. pydict_set_item_string_decref(pysym, "binding",
  307. PyInt_FromLong(node->sym->binding));
  308. pydict_set_item_string_decref(pysym, "name",
  309. PyString_FromStringAndSize(node->sym->name,
  310. node->sym->namelen));
  311. pydict_set_item_string_decref(pyelem, "sym", pysym);
  312. }
  313. if (node->map) {
  314. struct map *map = node->map;
  315. const char *dsoname = "[unknown]";
  316. if (map && map->dso && (map->dso->name || map->dso->long_name)) {
  317. if (symbol_conf.show_kernel_path && map->dso->long_name)
  318. dsoname = map->dso->long_name;
  319. else if (map->dso->name)
  320. dsoname = map->dso->name;
  321. }
  322. pydict_set_item_string_decref(pyelem, "dso",
  323. PyString_FromString(dsoname));
  324. }
  325. callchain_cursor_advance(&callchain_cursor);
  326. PyList_Append(pylist, pyelem);
  327. Py_DECREF(pyelem);
  328. }
  329. exit:
  330. return pylist;
  331. }
  332. static void python_process_tracepoint(struct perf_sample *sample,
  333. struct perf_evsel *evsel,
  334. struct addr_location *al)
  335. {
  336. struct event_format *event = evsel->tp_format;
  337. PyObject *handler, *context, *t, *obj, *callchain;
  338. PyObject *dict = NULL;
  339. static char handler_name[256];
  340. struct format_field *field;
  341. unsigned long s, ns;
  342. unsigned n = 0;
  343. int pid;
  344. int cpu = sample->cpu;
  345. void *data = sample->raw_data;
  346. unsigned long long nsecs = sample->time;
  347. const char *comm = thread__comm_str(al->thread);
  348. t = PyTuple_New(MAX_FIELDS);
  349. if (!t)
  350. Py_FatalError("couldn't create Python tuple");
  351. if (!event)
  352. die("ug! no event found for type %d", (int)evsel->attr.config);
  353. pid = raw_field_value(event, "common_pid", data);
  354. sprintf(handler_name, "%s__%s", event->system, event->name);
  355. if (!test_and_set_bit(event->id, events_defined))
  356. define_event_symbols(event, handler_name, event->print_fmt.args);
  357. handler = get_handler(handler_name);
  358. if (!handler) {
  359. dict = PyDict_New();
  360. if (!dict)
  361. Py_FatalError("couldn't create Python dict");
  362. }
  363. s = nsecs / NSECS_PER_SEC;
  364. ns = nsecs - s * NSECS_PER_SEC;
  365. scripting_context->event_data = data;
  366. scripting_context->pevent = evsel->tp_format->pevent;
  367. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  368. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  369. PyTuple_SetItem(t, n++, context);
  370. /* ip unwinding */
  371. callchain = python_process_callchain(sample, evsel, al);
  372. if (handler) {
  373. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  374. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  375. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  376. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  377. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  378. PyTuple_SetItem(t, n++, callchain);
  379. } else {
  380. pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
  381. pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
  382. pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
  383. pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
  384. pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
  385. pydict_set_item_string_decref(dict, "common_callchain", callchain);
  386. }
  387. for (field = event->format.fields; field; field = field->next) {
  388. if (field->flags & FIELD_IS_STRING) {
  389. int offset;
  390. if (field->flags & FIELD_IS_DYNAMIC) {
  391. offset = *(int *)(data + field->offset);
  392. offset &= 0xffff;
  393. } else
  394. offset = field->offset;
  395. obj = PyString_FromString((char *)data + offset);
  396. } else { /* FIELD_IS_NUMERIC */
  397. obj = get_field_numeric_entry(event, field, data);
  398. }
  399. if (handler)
  400. PyTuple_SetItem(t, n++, obj);
  401. else
  402. pydict_set_item_string_decref(dict, field->name, obj);
  403. }
  404. if (!handler)
  405. PyTuple_SetItem(t, n++, dict);
  406. if (_PyTuple_Resize(&t, n) == -1)
  407. Py_FatalError("error resizing Python tuple");
  408. if (handler) {
  409. call_object(handler, t, handler_name);
  410. } else {
  411. try_call_object("trace_unhandled", t);
  412. Py_DECREF(dict);
  413. }
  414. Py_DECREF(t);
  415. }
  416. static PyObject *tuple_new(unsigned int sz)
  417. {
  418. PyObject *t;
  419. t = PyTuple_New(sz);
  420. if (!t)
  421. Py_FatalError("couldn't create Python tuple");
  422. return t;
  423. }
  424. static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
  425. {
  426. #if BITS_PER_LONG == 64
  427. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  428. #endif
  429. #if BITS_PER_LONG == 32
  430. return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
  431. #endif
  432. }
  433. static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
  434. {
  435. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  436. }
  437. static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
  438. {
  439. return PyTuple_SetItem(t, pos, PyString_FromString(s));
  440. }
  441. static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
  442. {
  443. struct tables *tables = container_of(dbe, struct tables, dbe);
  444. PyObject *t;
  445. t = tuple_new(2);
  446. tuple_set_u64(t, 0, evsel->db_id);
  447. tuple_set_string(t, 1, perf_evsel__name(evsel));
  448. call_object(tables->evsel_handler, t, "evsel_table");
  449. Py_DECREF(t);
  450. return 0;
  451. }
  452. static int python_export_machine(struct db_export *dbe,
  453. struct machine *machine)
  454. {
  455. struct tables *tables = container_of(dbe, struct tables, dbe);
  456. PyObject *t;
  457. t = tuple_new(3);
  458. tuple_set_u64(t, 0, machine->db_id);
  459. tuple_set_s32(t, 1, machine->pid);
  460. tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
  461. call_object(tables->machine_handler, t, "machine_table");
  462. Py_DECREF(t);
  463. return 0;
  464. }
  465. static int python_export_thread(struct db_export *dbe, struct thread *thread,
  466. u64 main_thread_db_id, struct machine *machine)
  467. {
  468. struct tables *tables = container_of(dbe, struct tables, dbe);
  469. PyObject *t;
  470. t = tuple_new(5);
  471. tuple_set_u64(t, 0, thread->db_id);
  472. tuple_set_u64(t, 1, machine->db_id);
  473. tuple_set_u64(t, 2, main_thread_db_id);
  474. tuple_set_s32(t, 3, thread->pid_);
  475. tuple_set_s32(t, 4, thread->tid);
  476. call_object(tables->thread_handler, t, "thread_table");
  477. Py_DECREF(t);
  478. return 0;
  479. }
  480. static int python_export_comm(struct db_export *dbe, struct comm *comm)
  481. {
  482. struct tables *tables = container_of(dbe, struct tables, dbe);
  483. PyObject *t;
  484. t = tuple_new(2);
  485. tuple_set_u64(t, 0, comm->db_id);
  486. tuple_set_string(t, 1, comm__str(comm));
  487. call_object(tables->comm_handler, t, "comm_table");
  488. Py_DECREF(t);
  489. return 0;
  490. }
  491. static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
  492. struct comm *comm, struct thread *thread)
  493. {
  494. struct tables *tables = container_of(dbe, struct tables, dbe);
  495. PyObject *t;
  496. t = tuple_new(3);
  497. tuple_set_u64(t, 0, db_id);
  498. tuple_set_u64(t, 1, comm->db_id);
  499. tuple_set_u64(t, 2, thread->db_id);
  500. call_object(tables->comm_thread_handler, t, "comm_thread_table");
  501. Py_DECREF(t);
  502. return 0;
  503. }
  504. static int python_export_dso(struct db_export *dbe, struct dso *dso,
  505. struct machine *machine)
  506. {
  507. struct tables *tables = container_of(dbe, struct tables, dbe);
  508. char sbuild_id[BUILD_ID_SIZE * 2 + 1];
  509. PyObject *t;
  510. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  511. t = tuple_new(5);
  512. tuple_set_u64(t, 0, dso->db_id);
  513. tuple_set_u64(t, 1, machine->db_id);
  514. tuple_set_string(t, 2, dso->short_name);
  515. tuple_set_string(t, 3, dso->long_name);
  516. tuple_set_string(t, 4, sbuild_id);
  517. call_object(tables->dso_handler, t, "dso_table");
  518. Py_DECREF(t);
  519. return 0;
  520. }
  521. static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
  522. struct dso *dso)
  523. {
  524. struct tables *tables = container_of(dbe, struct tables, dbe);
  525. u64 *sym_db_id = symbol__priv(sym);
  526. PyObject *t;
  527. t = tuple_new(6);
  528. tuple_set_u64(t, 0, *sym_db_id);
  529. tuple_set_u64(t, 1, dso->db_id);
  530. tuple_set_u64(t, 2, sym->start);
  531. tuple_set_u64(t, 3, sym->end);
  532. tuple_set_s32(t, 4, sym->binding);
  533. tuple_set_string(t, 5, sym->name);
  534. call_object(tables->symbol_handler, t, "symbol_table");
  535. Py_DECREF(t);
  536. return 0;
  537. }
  538. static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
  539. const char *name)
  540. {
  541. struct tables *tables = container_of(dbe, struct tables, dbe);
  542. PyObject *t;
  543. t = tuple_new(2);
  544. tuple_set_s32(t, 0, branch_type);
  545. tuple_set_string(t, 1, name);
  546. call_object(tables->branch_type_handler, t, "branch_type_table");
  547. Py_DECREF(t);
  548. return 0;
  549. }
  550. static int python_export_sample(struct db_export *dbe,
  551. struct export_sample *es)
  552. {
  553. struct tables *tables = container_of(dbe, struct tables, dbe);
  554. PyObject *t;
  555. t = tuple_new(21);
  556. tuple_set_u64(t, 0, es->db_id);
  557. tuple_set_u64(t, 1, es->evsel->db_id);
  558. tuple_set_u64(t, 2, es->al->machine->db_id);
  559. tuple_set_u64(t, 3, es->al->thread->db_id);
  560. tuple_set_u64(t, 4, es->comm_db_id);
  561. tuple_set_u64(t, 5, es->dso_db_id);
  562. tuple_set_u64(t, 6, es->sym_db_id);
  563. tuple_set_u64(t, 7, es->offset);
  564. tuple_set_u64(t, 8, es->sample->ip);
  565. tuple_set_u64(t, 9, es->sample->time);
  566. tuple_set_s32(t, 10, es->sample->cpu);
  567. tuple_set_u64(t, 11, es->addr_dso_db_id);
  568. tuple_set_u64(t, 12, es->addr_sym_db_id);
  569. tuple_set_u64(t, 13, es->addr_offset);
  570. tuple_set_u64(t, 14, es->sample->addr);
  571. tuple_set_u64(t, 15, es->sample->period);
  572. tuple_set_u64(t, 16, es->sample->weight);
  573. tuple_set_u64(t, 17, es->sample->transaction);
  574. tuple_set_u64(t, 18, es->sample->data_src);
  575. tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
  576. tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
  577. call_object(tables->sample_handler, t, "sample_table");
  578. Py_DECREF(t);
  579. return 0;
  580. }
  581. static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
  582. {
  583. struct tables *tables = container_of(dbe, struct tables, dbe);
  584. PyObject *t;
  585. u64 parent_db_id, sym_db_id;
  586. parent_db_id = cp->parent ? cp->parent->db_id : 0;
  587. sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
  588. t = tuple_new(4);
  589. tuple_set_u64(t, 0, cp->db_id);
  590. tuple_set_u64(t, 1, parent_db_id);
  591. tuple_set_u64(t, 2, sym_db_id);
  592. tuple_set_u64(t, 3, cp->ip);
  593. call_object(tables->call_path_handler, t, "call_path_table");
  594. Py_DECREF(t);
  595. return 0;
  596. }
  597. static int python_export_call_return(struct db_export *dbe,
  598. struct call_return *cr)
  599. {
  600. struct tables *tables = container_of(dbe, struct tables, dbe);
  601. u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
  602. PyObject *t;
  603. t = tuple_new(11);
  604. tuple_set_u64(t, 0, cr->db_id);
  605. tuple_set_u64(t, 1, cr->thread->db_id);
  606. tuple_set_u64(t, 2, comm_db_id);
  607. tuple_set_u64(t, 3, cr->cp->db_id);
  608. tuple_set_u64(t, 4, cr->call_time);
  609. tuple_set_u64(t, 5, cr->return_time);
  610. tuple_set_u64(t, 6, cr->branch_count);
  611. tuple_set_u64(t, 7, cr->call_ref);
  612. tuple_set_u64(t, 8, cr->return_ref);
  613. tuple_set_u64(t, 9, cr->cp->parent->db_id);
  614. tuple_set_s32(t, 10, cr->flags);
  615. call_object(tables->call_return_handler, t, "call_return_table");
  616. Py_DECREF(t);
  617. return 0;
  618. }
  619. static int python_process_call_return(struct call_return *cr, void *data)
  620. {
  621. struct db_export *dbe = data;
  622. return db_export__call_return(dbe, cr);
  623. }
  624. static void python_process_general_event(struct perf_sample *sample,
  625. struct perf_evsel *evsel,
  626. struct addr_location *al)
  627. {
  628. PyObject *handler, *t, *dict, *callchain, *dict_sample;
  629. static char handler_name[64];
  630. unsigned n = 0;
  631. /*
  632. * Use the MAX_FIELDS to make the function expandable, though
  633. * currently there is only one item for the tuple.
  634. */
  635. t = PyTuple_New(MAX_FIELDS);
  636. if (!t)
  637. Py_FatalError("couldn't create Python tuple");
  638. dict = PyDict_New();
  639. if (!dict)
  640. Py_FatalError("couldn't create Python dictionary");
  641. dict_sample = PyDict_New();
  642. if (!dict_sample)
  643. Py_FatalError("couldn't create Python dictionary");
  644. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  645. handler = get_handler(handler_name);
  646. if (!handler)
  647. goto exit;
  648. pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
  649. pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
  650. (const char *)&evsel->attr, sizeof(evsel->attr)));
  651. pydict_set_item_string_decref(dict_sample, "pid",
  652. PyInt_FromLong(sample->pid));
  653. pydict_set_item_string_decref(dict_sample, "tid",
  654. PyInt_FromLong(sample->tid));
  655. pydict_set_item_string_decref(dict_sample, "cpu",
  656. PyInt_FromLong(sample->cpu));
  657. pydict_set_item_string_decref(dict_sample, "ip",
  658. PyLong_FromUnsignedLongLong(sample->ip));
  659. pydict_set_item_string_decref(dict_sample, "time",
  660. PyLong_FromUnsignedLongLong(sample->time));
  661. pydict_set_item_string_decref(dict_sample, "period",
  662. PyLong_FromUnsignedLongLong(sample->period));
  663. pydict_set_item_string_decref(dict, "sample", dict_sample);
  664. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  665. (const char *)sample->raw_data, sample->raw_size));
  666. pydict_set_item_string_decref(dict, "comm",
  667. PyString_FromString(thread__comm_str(al->thread)));
  668. if (al->map) {
  669. pydict_set_item_string_decref(dict, "dso",
  670. PyString_FromString(al->map->dso->name));
  671. }
  672. if (al->sym) {
  673. pydict_set_item_string_decref(dict, "symbol",
  674. PyString_FromString(al->sym->name));
  675. }
  676. /* ip unwinding */
  677. callchain = python_process_callchain(sample, evsel, al);
  678. pydict_set_item_string_decref(dict, "callchain", callchain);
  679. PyTuple_SetItem(t, n++, dict);
  680. if (_PyTuple_Resize(&t, n) == -1)
  681. Py_FatalError("error resizing Python tuple");
  682. call_object(handler, t, handler_name);
  683. exit:
  684. Py_DECREF(dict);
  685. Py_DECREF(t);
  686. }
  687. static void python_process_event(union perf_event *event,
  688. struct perf_sample *sample,
  689. struct perf_evsel *evsel,
  690. struct addr_location *al)
  691. {
  692. struct tables *tables = &tables_global;
  693. switch (evsel->attr.type) {
  694. case PERF_TYPE_TRACEPOINT:
  695. python_process_tracepoint(sample, evsel, al);
  696. break;
  697. /* Reserve for future process_hw/sw/raw APIs */
  698. default:
  699. if (tables->db_export_mode)
  700. db_export__sample(&tables->dbe, event, sample, evsel, al);
  701. else
  702. python_process_general_event(sample, evsel, al);
  703. }
  704. }
  705. static void get_handler_name(char *str, size_t size,
  706. struct perf_evsel *evsel)
  707. {
  708. char *p = str;
  709. scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
  710. while ((p = strchr(p, ':'))) {
  711. *p = '_';
  712. p++;
  713. }
  714. }
  715. static void
  716. process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
  717. struct perf_counts_values *count)
  718. {
  719. PyObject *handler, *t;
  720. static char handler_name[256];
  721. int n = 0;
  722. t = PyTuple_New(MAX_FIELDS);
  723. if (!t)
  724. Py_FatalError("couldn't create Python tuple");
  725. get_handler_name(handler_name, sizeof(handler_name),
  726. counter);
  727. handler = get_handler(handler_name);
  728. if (!handler) {
  729. pr_debug("can't find python handler %s\n", handler_name);
  730. return;
  731. }
  732. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  733. PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
  734. tuple_set_u64(t, n++, tstamp);
  735. tuple_set_u64(t, n++, count->val);
  736. tuple_set_u64(t, n++, count->ena);
  737. tuple_set_u64(t, n++, count->run);
  738. if (_PyTuple_Resize(&t, n) == -1)
  739. Py_FatalError("error resizing Python tuple");
  740. call_object(handler, t, handler_name);
  741. Py_DECREF(t);
  742. }
  743. static void python_process_stat(struct perf_stat_config *config,
  744. struct perf_evsel *counter, u64 tstamp)
  745. {
  746. struct thread_map *threads = counter->threads;
  747. struct cpu_map *cpus = counter->cpus;
  748. int cpu, thread;
  749. if (config->aggr_mode == AGGR_GLOBAL) {
  750. process_stat(counter, -1, -1, tstamp,
  751. &counter->counts->aggr);
  752. return;
  753. }
  754. for (thread = 0; thread < threads->nr; thread++) {
  755. for (cpu = 0; cpu < cpus->nr; cpu++) {
  756. process_stat(counter, cpus->map[cpu],
  757. thread_map__pid(threads, thread), tstamp,
  758. perf_counts(counter->counts, cpu, thread));
  759. }
  760. }
  761. }
  762. static void python_process_stat_interval(u64 tstamp)
  763. {
  764. PyObject *handler, *t;
  765. static const char handler_name[] = "stat__interval";
  766. int n = 0;
  767. t = PyTuple_New(MAX_FIELDS);
  768. if (!t)
  769. Py_FatalError("couldn't create Python tuple");
  770. handler = get_handler(handler_name);
  771. if (!handler) {
  772. pr_debug("can't find python handler %s\n", handler_name);
  773. return;
  774. }
  775. tuple_set_u64(t, n++, tstamp);
  776. if (_PyTuple_Resize(&t, n) == -1)
  777. Py_FatalError("error resizing Python tuple");
  778. call_object(handler, t, handler_name);
  779. Py_DECREF(t);
  780. }
  781. static int run_start_sub(void)
  782. {
  783. main_module = PyImport_AddModule("__main__");
  784. if (main_module == NULL)
  785. return -1;
  786. Py_INCREF(main_module);
  787. main_dict = PyModule_GetDict(main_module);
  788. if (main_dict == NULL)
  789. goto error;
  790. Py_INCREF(main_dict);
  791. try_call_object("trace_begin", NULL);
  792. return 0;
  793. error:
  794. Py_XDECREF(main_dict);
  795. Py_XDECREF(main_module);
  796. return -1;
  797. }
  798. #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
  799. tables->handler_name = get_handler(#table_name); \
  800. if (tables->handler_name) \
  801. tables->dbe.export_ ## name = python_export_ ## name; \
  802. } while (0)
  803. #define SET_TABLE_HANDLER(name) \
  804. SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
  805. static void set_table_handlers(struct tables *tables)
  806. {
  807. const char *perf_db_export_mode = "perf_db_export_mode";
  808. const char *perf_db_export_calls = "perf_db_export_calls";
  809. PyObject *db_export_mode, *db_export_calls;
  810. bool export_calls = false;
  811. int ret;
  812. memset(tables, 0, sizeof(struct tables));
  813. if (db_export__init(&tables->dbe))
  814. Py_FatalError("failed to initialize export");
  815. db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
  816. if (!db_export_mode)
  817. return;
  818. ret = PyObject_IsTrue(db_export_mode);
  819. if (ret == -1)
  820. handler_call_die(perf_db_export_mode);
  821. if (!ret)
  822. return;
  823. tables->dbe.crp = NULL;
  824. db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
  825. if (db_export_calls) {
  826. ret = PyObject_IsTrue(db_export_calls);
  827. if (ret == -1)
  828. handler_call_die(perf_db_export_calls);
  829. export_calls = !!ret;
  830. }
  831. if (export_calls) {
  832. tables->dbe.crp =
  833. call_return_processor__new(python_process_call_return,
  834. &tables->dbe);
  835. if (!tables->dbe.crp)
  836. Py_FatalError("failed to create calls processor");
  837. }
  838. tables->db_export_mode = true;
  839. /*
  840. * Reserve per symbol space for symbol->db_id via symbol__priv()
  841. */
  842. symbol_conf.priv_size = sizeof(u64);
  843. SET_TABLE_HANDLER(evsel);
  844. SET_TABLE_HANDLER(machine);
  845. SET_TABLE_HANDLER(thread);
  846. SET_TABLE_HANDLER(comm);
  847. SET_TABLE_HANDLER(comm_thread);
  848. SET_TABLE_HANDLER(dso);
  849. SET_TABLE_HANDLER(symbol);
  850. SET_TABLE_HANDLER(branch_type);
  851. SET_TABLE_HANDLER(sample);
  852. SET_TABLE_HANDLER(call_path);
  853. SET_TABLE_HANDLER(call_return);
  854. }
  855. /*
  856. * Start trace script
  857. */
  858. static int python_start_script(const char *script, int argc, const char **argv)
  859. {
  860. struct tables *tables = &tables_global;
  861. const char **command_line;
  862. char buf[PATH_MAX];
  863. int i, err = 0;
  864. FILE *fp;
  865. command_line = malloc((argc + 1) * sizeof(const char *));
  866. command_line[0] = script;
  867. for (i = 1; i < argc + 1; i++)
  868. command_line[i] = argv[i - 1];
  869. Py_Initialize();
  870. initperf_trace_context();
  871. PySys_SetArgv(argc + 1, (char **)command_line);
  872. fp = fopen(script, "r");
  873. if (!fp) {
  874. sprintf(buf, "Can't open python script \"%s\"", script);
  875. perror(buf);
  876. err = -1;
  877. goto error;
  878. }
  879. err = PyRun_SimpleFile(fp, script);
  880. if (err) {
  881. fprintf(stderr, "Error running python script %s\n", script);
  882. goto error;
  883. }
  884. err = run_start_sub();
  885. if (err) {
  886. fprintf(stderr, "Error starting python script %s\n", script);
  887. goto error;
  888. }
  889. set_table_handlers(tables);
  890. if (tables->db_export_mode) {
  891. err = db_export__branch_types(&tables->dbe);
  892. if (err)
  893. goto error;
  894. }
  895. free(command_line);
  896. return err;
  897. error:
  898. Py_Finalize();
  899. free(command_line);
  900. return err;
  901. }
  902. static int python_flush_script(void)
  903. {
  904. struct tables *tables = &tables_global;
  905. return db_export__flush(&tables->dbe);
  906. }
  907. /*
  908. * Stop trace script
  909. */
  910. static int python_stop_script(void)
  911. {
  912. struct tables *tables = &tables_global;
  913. try_call_object("trace_end", NULL);
  914. db_export__exit(&tables->dbe);
  915. Py_XDECREF(main_dict);
  916. Py_XDECREF(main_module);
  917. Py_Finalize();
  918. return 0;
  919. }
  920. static int python_generate_script(struct pevent *pevent, const char *outfile)
  921. {
  922. struct event_format *event = NULL;
  923. struct format_field *f;
  924. char fname[PATH_MAX];
  925. int not_first, count;
  926. FILE *ofp;
  927. sprintf(fname, "%s.py", outfile);
  928. ofp = fopen(fname, "w");
  929. if (ofp == NULL) {
  930. fprintf(stderr, "couldn't open %s\n", fname);
  931. return -1;
  932. }
  933. fprintf(ofp, "# perf script event handlers, "
  934. "generated by perf script -g python\n");
  935. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  936. " License version 2\n\n");
  937. fprintf(ofp, "# The common_* event handler fields are the most useful "
  938. "fields common to\n");
  939. fprintf(ofp, "# all events. They don't necessarily correspond to "
  940. "the 'common_*' fields\n");
  941. fprintf(ofp, "# in the format files. Those fields not available as "
  942. "handler params can\n");
  943. fprintf(ofp, "# be retrieved using Python functions of the form "
  944. "common_*(context).\n");
  945. fprintf(ofp, "# See the perf-trace-python Documentation for the list "
  946. "of available functions.\n\n");
  947. fprintf(ofp, "import os\n");
  948. fprintf(ofp, "import sys\n\n");
  949. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  950. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  951. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  952. fprintf(ofp, "from Core import *\n\n\n");
  953. fprintf(ofp, "def trace_begin():\n");
  954. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  955. fprintf(ofp, "def trace_end():\n");
  956. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  957. while ((event = trace_find_next_event(pevent, event))) {
  958. fprintf(ofp, "def %s__%s(", event->system, event->name);
  959. fprintf(ofp, "event_name, ");
  960. fprintf(ofp, "context, ");
  961. fprintf(ofp, "common_cpu,\n");
  962. fprintf(ofp, "\tcommon_secs, ");
  963. fprintf(ofp, "common_nsecs, ");
  964. fprintf(ofp, "common_pid, ");
  965. fprintf(ofp, "common_comm,\n\t");
  966. fprintf(ofp, "common_callchain, ");
  967. not_first = 0;
  968. count = 0;
  969. for (f = event->format.fields; f; f = f->next) {
  970. if (not_first++)
  971. fprintf(ofp, ", ");
  972. if (++count % 5 == 0)
  973. fprintf(ofp, "\n\t");
  974. fprintf(ofp, "%s", f->name);
  975. }
  976. fprintf(ofp, "):\n");
  977. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  978. "common_secs, common_nsecs,\n\t\t\t"
  979. "common_pid, common_comm)\n\n");
  980. fprintf(ofp, "\t\tprint \"");
  981. not_first = 0;
  982. count = 0;
  983. for (f = event->format.fields; f; f = f->next) {
  984. if (not_first++)
  985. fprintf(ofp, ", ");
  986. if (count && count % 3 == 0) {
  987. fprintf(ofp, "\" \\\n\t\t\"");
  988. }
  989. count++;
  990. fprintf(ofp, "%s=", f->name);
  991. if (f->flags & FIELD_IS_STRING ||
  992. f->flags & FIELD_IS_FLAG ||
  993. f->flags & FIELD_IS_ARRAY ||
  994. f->flags & FIELD_IS_SYMBOLIC)
  995. fprintf(ofp, "%%s");
  996. else if (f->flags & FIELD_IS_SIGNED)
  997. fprintf(ofp, "%%d");
  998. else
  999. fprintf(ofp, "%%u");
  1000. }
  1001. fprintf(ofp, "\" %% \\\n\t\t(");
  1002. not_first = 0;
  1003. count = 0;
  1004. for (f = event->format.fields; f; f = f->next) {
  1005. if (not_first++)
  1006. fprintf(ofp, ", ");
  1007. if (++count % 5 == 0)
  1008. fprintf(ofp, "\n\t\t");
  1009. if (f->flags & FIELD_IS_FLAG) {
  1010. if ((count - 1) % 5 != 0) {
  1011. fprintf(ofp, "\n\t\t");
  1012. count = 4;
  1013. }
  1014. fprintf(ofp, "flag_str(\"");
  1015. fprintf(ofp, "%s__%s\", ", event->system,
  1016. event->name);
  1017. fprintf(ofp, "\"%s\", %s)", f->name,
  1018. f->name);
  1019. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  1020. if ((count - 1) % 5 != 0) {
  1021. fprintf(ofp, "\n\t\t");
  1022. count = 4;
  1023. }
  1024. fprintf(ofp, "symbol_str(\"");
  1025. fprintf(ofp, "%s__%s\", ", event->system,
  1026. event->name);
  1027. fprintf(ofp, "\"%s\", %s)", f->name,
  1028. f->name);
  1029. } else
  1030. fprintf(ofp, "%s", f->name);
  1031. }
  1032. fprintf(ofp, ")\n\n");
  1033. fprintf(ofp, "\t\tfor node in common_callchain:");
  1034. fprintf(ofp, "\n\t\t\tif 'sym' in node:");
  1035. fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
  1036. fprintf(ofp, "\n\t\t\telse:");
  1037. fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
  1038. fprintf(ofp, "\t\tprint \"\\n\"\n\n");
  1039. }
  1040. fprintf(ofp, "def trace_unhandled(event_name, context, "
  1041. "event_fields_dict):\n");
  1042. fprintf(ofp, "\t\tprint ' '.join(['%%s=%%s'%%(k,str(v))"
  1043. "for k,v in sorted(event_fields_dict.items())])\n\n");
  1044. fprintf(ofp, "def print_header("
  1045. "event_name, cpu, secs, nsecs, pid, comm):\n"
  1046. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  1047. "(event_name, cpu, secs, nsecs, pid, comm),\n");
  1048. fclose(ofp);
  1049. fprintf(stderr, "generated Python script: %s\n", fname);
  1050. return 0;
  1051. }
  1052. struct scripting_ops python_scripting_ops = {
  1053. .name = "Python",
  1054. .start_script = python_start_script,
  1055. .flush_script = python_flush_script,
  1056. .stop_script = python_stop_script,
  1057. .process_event = python_process_event,
  1058. .process_stat = python_process_stat,
  1059. .process_stat_interval = python_process_stat_interval,
  1060. .generate_script = python_generate_script,
  1061. };