trace-event-python.c 38 KB

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