trace-event-python.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  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. pydict_set_item_string_decref(dict_sample, "phys_addr",
  432. PyLong_FromUnsignedLongLong(sample->phys_addr));
  433. set_sample_read_in_dict(dict_sample, sample, evsel);
  434. pydict_set_item_string_decref(dict, "sample", dict_sample);
  435. pydict_set_item_string_decref(dict, "raw_buf", PyString_FromStringAndSize(
  436. (const char *)sample->raw_data, sample->raw_size));
  437. pydict_set_item_string_decref(dict, "comm",
  438. PyString_FromString(thread__comm_str(al->thread)));
  439. if (al->map) {
  440. pydict_set_item_string_decref(dict, "dso",
  441. PyString_FromString(al->map->dso->name));
  442. }
  443. if (al->sym) {
  444. pydict_set_item_string_decref(dict, "symbol",
  445. PyString_FromString(al->sym->name));
  446. }
  447. pydict_set_item_string_decref(dict, "callchain", callchain);
  448. return dict;
  449. }
  450. static void python_process_tracepoint(struct perf_sample *sample,
  451. struct perf_evsel *evsel,
  452. struct addr_location *al)
  453. {
  454. struct event_format *event = evsel->tp_format;
  455. PyObject *handler, *context, *t, *obj = NULL, *callchain;
  456. PyObject *dict = NULL, *all_entries_dict = NULL;
  457. static char handler_name[256];
  458. struct format_field *field;
  459. unsigned long s, ns;
  460. unsigned n = 0;
  461. int pid;
  462. int cpu = sample->cpu;
  463. void *data = sample->raw_data;
  464. unsigned long long nsecs = sample->time;
  465. const char *comm = thread__comm_str(al->thread);
  466. const char *default_handler_name = "trace_unhandled";
  467. if (!event) {
  468. snprintf(handler_name, sizeof(handler_name),
  469. "ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
  470. Py_FatalError(handler_name);
  471. }
  472. pid = raw_field_value(event, "common_pid", data);
  473. sprintf(handler_name, "%s__%s", event->system, event->name);
  474. if (!test_and_set_bit(event->id, events_defined))
  475. define_event_symbols(event, handler_name, event->print_fmt.args);
  476. handler = get_handler(handler_name);
  477. if (!handler) {
  478. handler = get_handler(default_handler_name);
  479. if (!handler)
  480. return;
  481. dict = PyDict_New();
  482. if (!dict)
  483. Py_FatalError("couldn't create Python dict");
  484. }
  485. t = PyTuple_New(MAX_FIELDS);
  486. if (!t)
  487. Py_FatalError("couldn't create Python tuple");
  488. s = nsecs / NSEC_PER_SEC;
  489. ns = nsecs - s * NSEC_PER_SEC;
  490. scripting_context->event_data = data;
  491. scripting_context->pevent = evsel->tp_format->pevent;
  492. context = PyCObject_FromVoidPtr(scripting_context, NULL);
  493. PyTuple_SetItem(t, n++, PyString_FromString(handler_name));
  494. PyTuple_SetItem(t, n++, context);
  495. /* ip unwinding */
  496. callchain = python_process_callchain(sample, evsel, al);
  497. /* Need an additional reference for the perf_sample dict */
  498. Py_INCREF(callchain);
  499. if (!dict) {
  500. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  501. PyTuple_SetItem(t, n++, PyInt_FromLong(s));
  502. PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
  503. PyTuple_SetItem(t, n++, PyInt_FromLong(pid));
  504. PyTuple_SetItem(t, n++, PyString_FromString(comm));
  505. PyTuple_SetItem(t, n++, callchain);
  506. } else {
  507. pydict_set_item_string_decref(dict, "common_cpu", PyInt_FromLong(cpu));
  508. pydict_set_item_string_decref(dict, "common_s", PyInt_FromLong(s));
  509. pydict_set_item_string_decref(dict, "common_ns", PyInt_FromLong(ns));
  510. pydict_set_item_string_decref(dict, "common_pid", PyInt_FromLong(pid));
  511. pydict_set_item_string_decref(dict, "common_comm", PyString_FromString(comm));
  512. pydict_set_item_string_decref(dict, "common_callchain", callchain);
  513. }
  514. for (field = event->format.fields; field; field = field->next) {
  515. unsigned int offset, len;
  516. unsigned long long val;
  517. if (field->flags & FIELD_IS_ARRAY) {
  518. offset = field->offset;
  519. len = field->size;
  520. if (field->flags & FIELD_IS_DYNAMIC) {
  521. val = pevent_read_number(scripting_context->pevent,
  522. data + offset, len);
  523. offset = val;
  524. len = offset >> 16;
  525. offset &= 0xffff;
  526. }
  527. if (field->flags & FIELD_IS_STRING &&
  528. is_printable_array(data + offset, len)) {
  529. obj = PyString_FromString((char *) data + offset);
  530. } else {
  531. obj = PyByteArray_FromStringAndSize((const char *) data + offset, len);
  532. field->flags &= ~FIELD_IS_STRING;
  533. }
  534. } else { /* FIELD_IS_NUMERIC */
  535. obj = get_field_numeric_entry(event, field, data);
  536. }
  537. if (!dict)
  538. PyTuple_SetItem(t, n++, obj);
  539. else
  540. pydict_set_item_string_decref(dict, field->name, obj);
  541. }
  542. if (dict)
  543. PyTuple_SetItem(t, n++, dict);
  544. if (get_argument_count(handler) == (int) n + 1) {
  545. all_entries_dict = get_perf_sample_dict(sample, evsel, al,
  546. callchain);
  547. PyTuple_SetItem(t, n++, all_entries_dict);
  548. } else {
  549. Py_DECREF(callchain);
  550. }
  551. if (_PyTuple_Resize(&t, n) == -1)
  552. Py_FatalError("error resizing Python tuple");
  553. if (!dict) {
  554. call_object(handler, t, handler_name);
  555. } else {
  556. call_object(handler, t, default_handler_name);
  557. Py_DECREF(dict);
  558. }
  559. Py_XDECREF(all_entries_dict);
  560. Py_DECREF(t);
  561. }
  562. static PyObject *tuple_new(unsigned int sz)
  563. {
  564. PyObject *t;
  565. t = PyTuple_New(sz);
  566. if (!t)
  567. Py_FatalError("couldn't create Python tuple");
  568. return t;
  569. }
  570. static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
  571. {
  572. #if BITS_PER_LONG == 64
  573. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  574. #endif
  575. #if BITS_PER_LONG == 32
  576. return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
  577. #endif
  578. }
  579. static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
  580. {
  581. return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
  582. }
  583. static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
  584. {
  585. return PyTuple_SetItem(t, pos, PyString_FromString(s));
  586. }
  587. static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
  588. {
  589. struct tables *tables = container_of(dbe, struct tables, dbe);
  590. PyObject *t;
  591. t = tuple_new(2);
  592. tuple_set_u64(t, 0, evsel->db_id);
  593. tuple_set_string(t, 1, perf_evsel__name(evsel));
  594. call_object(tables->evsel_handler, t, "evsel_table");
  595. Py_DECREF(t);
  596. return 0;
  597. }
  598. static int python_export_machine(struct db_export *dbe,
  599. struct machine *machine)
  600. {
  601. struct tables *tables = container_of(dbe, struct tables, dbe);
  602. PyObject *t;
  603. t = tuple_new(3);
  604. tuple_set_u64(t, 0, machine->db_id);
  605. tuple_set_s32(t, 1, machine->pid);
  606. tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
  607. call_object(tables->machine_handler, t, "machine_table");
  608. Py_DECREF(t);
  609. return 0;
  610. }
  611. static int python_export_thread(struct db_export *dbe, struct thread *thread,
  612. u64 main_thread_db_id, struct machine *machine)
  613. {
  614. struct tables *tables = container_of(dbe, struct tables, dbe);
  615. PyObject *t;
  616. t = tuple_new(5);
  617. tuple_set_u64(t, 0, thread->db_id);
  618. tuple_set_u64(t, 1, machine->db_id);
  619. tuple_set_u64(t, 2, main_thread_db_id);
  620. tuple_set_s32(t, 3, thread->pid_);
  621. tuple_set_s32(t, 4, thread->tid);
  622. call_object(tables->thread_handler, t, "thread_table");
  623. Py_DECREF(t);
  624. return 0;
  625. }
  626. static int python_export_comm(struct db_export *dbe, struct comm *comm)
  627. {
  628. struct tables *tables = container_of(dbe, struct tables, dbe);
  629. PyObject *t;
  630. t = tuple_new(2);
  631. tuple_set_u64(t, 0, comm->db_id);
  632. tuple_set_string(t, 1, comm__str(comm));
  633. call_object(tables->comm_handler, t, "comm_table");
  634. Py_DECREF(t);
  635. return 0;
  636. }
  637. static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
  638. struct comm *comm, struct thread *thread)
  639. {
  640. struct tables *tables = container_of(dbe, struct tables, dbe);
  641. PyObject *t;
  642. t = tuple_new(3);
  643. tuple_set_u64(t, 0, db_id);
  644. tuple_set_u64(t, 1, comm->db_id);
  645. tuple_set_u64(t, 2, thread->db_id);
  646. call_object(tables->comm_thread_handler, t, "comm_thread_table");
  647. Py_DECREF(t);
  648. return 0;
  649. }
  650. static int python_export_dso(struct db_export *dbe, struct dso *dso,
  651. struct machine *machine)
  652. {
  653. struct tables *tables = container_of(dbe, struct tables, dbe);
  654. char sbuild_id[SBUILD_ID_SIZE];
  655. PyObject *t;
  656. build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
  657. t = tuple_new(5);
  658. tuple_set_u64(t, 0, dso->db_id);
  659. tuple_set_u64(t, 1, machine->db_id);
  660. tuple_set_string(t, 2, dso->short_name);
  661. tuple_set_string(t, 3, dso->long_name);
  662. tuple_set_string(t, 4, sbuild_id);
  663. call_object(tables->dso_handler, t, "dso_table");
  664. Py_DECREF(t);
  665. return 0;
  666. }
  667. static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
  668. struct dso *dso)
  669. {
  670. struct tables *tables = container_of(dbe, struct tables, dbe);
  671. u64 *sym_db_id = symbol__priv(sym);
  672. PyObject *t;
  673. t = tuple_new(6);
  674. tuple_set_u64(t, 0, *sym_db_id);
  675. tuple_set_u64(t, 1, dso->db_id);
  676. tuple_set_u64(t, 2, sym->start);
  677. tuple_set_u64(t, 3, sym->end);
  678. tuple_set_s32(t, 4, sym->binding);
  679. tuple_set_string(t, 5, sym->name);
  680. call_object(tables->symbol_handler, t, "symbol_table");
  681. Py_DECREF(t);
  682. return 0;
  683. }
  684. static int python_export_branch_type(struct db_export *dbe, u32 branch_type,
  685. const char *name)
  686. {
  687. struct tables *tables = container_of(dbe, struct tables, dbe);
  688. PyObject *t;
  689. t = tuple_new(2);
  690. tuple_set_s32(t, 0, branch_type);
  691. tuple_set_string(t, 1, name);
  692. call_object(tables->branch_type_handler, t, "branch_type_table");
  693. Py_DECREF(t);
  694. return 0;
  695. }
  696. static int python_export_sample(struct db_export *dbe,
  697. struct export_sample *es)
  698. {
  699. struct tables *tables = container_of(dbe, struct tables, dbe);
  700. PyObject *t;
  701. t = tuple_new(22);
  702. tuple_set_u64(t, 0, es->db_id);
  703. tuple_set_u64(t, 1, es->evsel->db_id);
  704. tuple_set_u64(t, 2, es->al->machine->db_id);
  705. tuple_set_u64(t, 3, es->al->thread->db_id);
  706. tuple_set_u64(t, 4, es->comm_db_id);
  707. tuple_set_u64(t, 5, es->dso_db_id);
  708. tuple_set_u64(t, 6, es->sym_db_id);
  709. tuple_set_u64(t, 7, es->offset);
  710. tuple_set_u64(t, 8, es->sample->ip);
  711. tuple_set_u64(t, 9, es->sample->time);
  712. tuple_set_s32(t, 10, es->sample->cpu);
  713. tuple_set_u64(t, 11, es->addr_dso_db_id);
  714. tuple_set_u64(t, 12, es->addr_sym_db_id);
  715. tuple_set_u64(t, 13, es->addr_offset);
  716. tuple_set_u64(t, 14, es->sample->addr);
  717. tuple_set_u64(t, 15, es->sample->period);
  718. tuple_set_u64(t, 16, es->sample->weight);
  719. tuple_set_u64(t, 17, es->sample->transaction);
  720. tuple_set_u64(t, 18, es->sample->data_src);
  721. tuple_set_s32(t, 19, es->sample->flags & PERF_BRANCH_MASK);
  722. tuple_set_s32(t, 20, !!(es->sample->flags & PERF_IP_FLAG_IN_TX));
  723. tuple_set_u64(t, 21, es->call_path_id);
  724. call_object(tables->sample_handler, t, "sample_table");
  725. Py_DECREF(t);
  726. return 0;
  727. }
  728. static int python_export_call_path(struct db_export *dbe, struct call_path *cp)
  729. {
  730. struct tables *tables = container_of(dbe, struct tables, dbe);
  731. PyObject *t;
  732. u64 parent_db_id, sym_db_id;
  733. parent_db_id = cp->parent ? cp->parent->db_id : 0;
  734. sym_db_id = cp->sym ? *(u64 *)symbol__priv(cp->sym) : 0;
  735. t = tuple_new(4);
  736. tuple_set_u64(t, 0, cp->db_id);
  737. tuple_set_u64(t, 1, parent_db_id);
  738. tuple_set_u64(t, 2, sym_db_id);
  739. tuple_set_u64(t, 3, cp->ip);
  740. call_object(tables->call_path_handler, t, "call_path_table");
  741. Py_DECREF(t);
  742. return 0;
  743. }
  744. static int python_export_call_return(struct db_export *dbe,
  745. struct call_return *cr)
  746. {
  747. struct tables *tables = container_of(dbe, struct tables, dbe);
  748. u64 comm_db_id = cr->comm ? cr->comm->db_id : 0;
  749. PyObject *t;
  750. t = tuple_new(11);
  751. tuple_set_u64(t, 0, cr->db_id);
  752. tuple_set_u64(t, 1, cr->thread->db_id);
  753. tuple_set_u64(t, 2, comm_db_id);
  754. tuple_set_u64(t, 3, cr->cp->db_id);
  755. tuple_set_u64(t, 4, cr->call_time);
  756. tuple_set_u64(t, 5, cr->return_time);
  757. tuple_set_u64(t, 6, cr->branch_count);
  758. tuple_set_u64(t, 7, cr->call_ref);
  759. tuple_set_u64(t, 8, cr->return_ref);
  760. tuple_set_u64(t, 9, cr->cp->parent->db_id);
  761. tuple_set_s32(t, 10, cr->flags);
  762. call_object(tables->call_return_handler, t, "call_return_table");
  763. Py_DECREF(t);
  764. return 0;
  765. }
  766. static int python_process_call_return(struct call_return *cr, void *data)
  767. {
  768. struct db_export *dbe = data;
  769. return db_export__call_return(dbe, cr);
  770. }
  771. static void python_process_general_event(struct perf_sample *sample,
  772. struct perf_evsel *evsel,
  773. struct addr_location *al)
  774. {
  775. PyObject *handler, *t, *dict, *callchain;
  776. static char handler_name[64];
  777. unsigned n = 0;
  778. snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
  779. handler = get_handler(handler_name);
  780. if (!handler)
  781. return;
  782. /*
  783. * Use the MAX_FIELDS to make the function expandable, though
  784. * currently there is only one item for the tuple.
  785. */
  786. t = PyTuple_New(MAX_FIELDS);
  787. if (!t)
  788. Py_FatalError("couldn't create Python tuple");
  789. /* ip unwinding */
  790. callchain = python_process_callchain(sample, evsel, al);
  791. dict = get_perf_sample_dict(sample, evsel, al, callchain);
  792. PyTuple_SetItem(t, n++, dict);
  793. if (_PyTuple_Resize(&t, n) == -1)
  794. Py_FatalError("error resizing Python tuple");
  795. call_object(handler, t, handler_name);
  796. Py_DECREF(dict);
  797. Py_DECREF(t);
  798. }
  799. static void python_process_event(union perf_event *event,
  800. struct perf_sample *sample,
  801. struct perf_evsel *evsel,
  802. struct addr_location *al)
  803. {
  804. struct tables *tables = &tables_global;
  805. switch (evsel->attr.type) {
  806. case PERF_TYPE_TRACEPOINT:
  807. python_process_tracepoint(sample, evsel, al);
  808. break;
  809. /* Reserve for future process_hw/sw/raw APIs */
  810. default:
  811. if (tables->db_export_mode)
  812. db_export__sample(&tables->dbe, event, sample, evsel, al);
  813. else
  814. python_process_general_event(sample, evsel, al);
  815. }
  816. }
  817. static void get_handler_name(char *str, size_t size,
  818. struct perf_evsel *evsel)
  819. {
  820. char *p = str;
  821. scnprintf(str, size, "stat__%s", perf_evsel__name(evsel));
  822. while ((p = strchr(p, ':'))) {
  823. *p = '_';
  824. p++;
  825. }
  826. }
  827. static void
  828. process_stat(struct perf_evsel *counter, int cpu, int thread, u64 tstamp,
  829. struct perf_counts_values *count)
  830. {
  831. PyObject *handler, *t;
  832. static char handler_name[256];
  833. int n = 0;
  834. t = PyTuple_New(MAX_FIELDS);
  835. if (!t)
  836. Py_FatalError("couldn't create Python tuple");
  837. get_handler_name(handler_name, sizeof(handler_name),
  838. counter);
  839. handler = get_handler(handler_name);
  840. if (!handler) {
  841. pr_debug("can't find python handler %s\n", handler_name);
  842. return;
  843. }
  844. PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
  845. PyTuple_SetItem(t, n++, PyInt_FromLong(thread));
  846. tuple_set_u64(t, n++, tstamp);
  847. tuple_set_u64(t, n++, count->val);
  848. tuple_set_u64(t, n++, count->ena);
  849. tuple_set_u64(t, n++, count->run);
  850. if (_PyTuple_Resize(&t, n) == -1)
  851. Py_FatalError("error resizing Python tuple");
  852. call_object(handler, t, handler_name);
  853. Py_DECREF(t);
  854. }
  855. static void python_process_stat(struct perf_stat_config *config,
  856. struct perf_evsel *counter, u64 tstamp)
  857. {
  858. struct thread_map *threads = counter->threads;
  859. struct cpu_map *cpus = counter->cpus;
  860. int cpu, thread;
  861. if (config->aggr_mode == AGGR_GLOBAL) {
  862. process_stat(counter, -1, -1, tstamp,
  863. &counter->counts->aggr);
  864. return;
  865. }
  866. for (thread = 0; thread < threads->nr; thread++) {
  867. for (cpu = 0; cpu < cpus->nr; cpu++) {
  868. process_stat(counter, cpus->map[cpu],
  869. thread_map__pid(threads, thread), tstamp,
  870. perf_counts(counter->counts, cpu, thread));
  871. }
  872. }
  873. }
  874. static void python_process_stat_interval(u64 tstamp)
  875. {
  876. PyObject *handler, *t;
  877. static const char handler_name[] = "stat__interval";
  878. int n = 0;
  879. t = PyTuple_New(MAX_FIELDS);
  880. if (!t)
  881. Py_FatalError("couldn't create Python tuple");
  882. handler = get_handler(handler_name);
  883. if (!handler) {
  884. pr_debug("can't find python handler %s\n", handler_name);
  885. return;
  886. }
  887. tuple_set_u64(t, n++, tstamp);
  888. if (_PyTuple_Resize(&t, n) == -1)
  889. Py_FatalError("error resizing Python tuple");
  890. call_object(handler, t, handler_name);
  891. Py_DECREF(t);
  892. }
  893. static int run_start_sub(void)
  894. {
  895. main_module = PyImport_AddModule("__main__");
  896. if (main_module == NULL)
  897. return -1;
  898. Py_INCREF(main_module);
  899. main_dict = PyModule_GetDict(main_module);
  900. if (main_dict == NULL)
  901. goto error;
  902. Py_INCREF(main_dict);
  903. try_call_object("trace_begin", NULL);
  904. return 0;
  905. error:
  906. Py_XDECREF(main_dict);
  907. Py_XDECREF(main_module);
  908. return -1;
  909. }
  910. #define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
  911. tables->handler_name = get_handler(#table_name); \
  912. if (tables->handler_name) \
  913. tables->dbe.export_ ## name = python_export_ ## name; \
  914. } while (0)
  915. #define SET_TABLE_HANDLER(name) \
  916. SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
  917. static void set_table_handlers(struct tables *tables)
  918. {
  919. const char *perf_db_export_mode = "perf_db_export_mode";
  920. const char *perf_db_export_calls = "perf_db_export_calls";
  921. const char *perf_db_export_callchains = "perf_db_export_callchains";
  922. PyObject *db_export_mode, *db_export_calls, *db_export_callchains;
  923. bool export_calls = false;
  924. bool export_callchains = false;
  925. int ret;
  926. memset(tables, 0, sizeof(struct tables));
  927. if (db_export__init(&tables->dbe))
  928. Py_FatalError("failed to initialize export");
  929. db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
  930. if (!db_export_mode)
  931. return;
  932. ret = PyObject_IsTrue(db_export_mode);
  933. if (ret == -1)
  934. handler_call_die(perf_db_export_mode);
  935. if (!ret)
  936. return;
  937. /* handle export calls */
  938. tables->dbe.crp = NULL;
  939. db_export_calls = PyDict_GetItemString(main_dict, perf_db_export_calls);
  940. if (db_export_calls) {
  941. ret = PyObject_IsTrue(db_export_calls);
  942. if (ret == -1)
  943. handler_call_die(perf_db_export_calls);
  944. export_calls = !!ret;
  945. }
  946. if (export_calls) {
  947. tables->dbe.crp =
  948. call_return_processor__new(python_process_call_return,
  949. &tables->dbe);
  950. if (!tables->dbe.crp)
  951. Py_FatalError("failed to create calls processor");
  952. }
  953. /* handle export callchains */
  954. tables->dbe.cpr = NULL;
  955. db_export_callchains = PyDict_GetItemString(main_dict,
  956. perf_db_export_callchains);
  957. if (db_export_callchains) {
  958. ret = PyObject_IsTrue(db_export_callchains);
  959. if (ret == -1)
  960. handler_call_die(perf_db_export_callchains);
  961. export_callchains = !!ret;
  962. }
  963. if (export_callchains) {
  964. /*
  965. * Attempt to use the call path root from the call return
  966. * processor, if the call return processor is in use. Otherwise,
  967. * we allocate a new call path root. This prevents exporting
  968. * duplicate call path ids when both are in use simultaniously.
  969. */
  970. if (tables->dbe.crp)
  971. tables->dbe.cpr = tables->dbe.crp->cpr;
  972. else
  973. tables->dbe.cpr = call_path_root__new();
  974. if (!tables->dbe.cpr)
  975. Py_FatalError("failed to create call path root");
  976. }
  977. tables->db_export_mode = true;
  978. /*
  979. * Reserve per symbol space for symbol->db_id via symbol__priv()
  980. */
  981. symbol_conf.priv_size = sizeof(u64);
  982. SET_TABLE_HANDLER(evsel);
  983. SET_TABLE_HANDLER(machine);
  984. SET_TABLE_HANDLER(thread);
  985. SET_TABLE_HANDLER(comm);
  986. SET_TABLE_HANDLER(comm_thread);
  987. SET_TABLE_HANDLER(dso);
  988. SET_TABLE_HANDLER(symbol);
  989. SET_TABLE_HANDLER(branch_type);
  990. SET_TABLE_HANDLER(sample);
  991. SET_TABLE_HANDLER(call_path);
  992. SET_TABLE_HANDLER(call_return);
  993. }
  994. /*
  995. * Start trace script
  996. */
  997. static int python_start_script(const char *script, int argc, const char **argv)
  998. {
  999. struct tables *tables = &tables_global;
  1000. const char **command_line;
  1001. char buf[PATH_MAX];
  1002. int i, err = 0;
  1003. FILE *fp;
  1004. command_line = malloc((argc + 1) * sizeof(const char *));
  1005. command_line[0] = script;
  1006. for (i = 1; i < argc + 1; i++)
  1007. command_line[i] = argv[i - 1];
  1008. Py_Initialize();
  1009. initperf_trace_context();
  1010. PySys_SetArgv(argc + 1, (char **)command_line);
  1011. fp = fopen(script, "r");
  1012. if (!fp) {
  1013. sprintf(buf, "Can't open python script \"%s\"", script);
  1014. perror(buf);
  1015. err = -1;
  1016. goto error;
  1017. }
  1018. err = PyRun_SimpleFile(fp, script);
  1019. if (err) {
  1020. fprintf(stderr, "Error running python script %s\n", script);
  1021. goto error;
  1022. }
  1023. err = run_start_sub();
  1024. if (err) {
  1025. fprintf(stderr, "Error starting python script %s\n", script);
  1026. goto error;
  1027. }
  1028. set_table_handlers(tables);
  1029. if (tables->db_export_mode) {
  1030. err = db_export__branch_types(&tables->dbe);
  1031. if (err)
  1032. goto error;
  1033. }
  1034. free(command_line);
  1035. return err;
  1036. error:
  1037. Py_Finalize();
  1038. free(command_line);
  1039. return err;
  1040. }
  1041. static int python_flush_script(void)
  1042. {
  1043. struct tables *tables = &tables_global;
  1044. return db_export__flush(&tables->dbe);
  1045. }
  1046. /*
  1047. * Stop trace script
  1048. */
  1049. static int python_stop_script(void)
  1050. {
  1051. struct tables *tables = &tables_global;
  1052. try_call_object("trace_end", NULL);
  1053. db_export__exit(&tables->dbe);
  1054. Py_XDECREF(main_dict);
  1055. Py_XDECREF(main_module);
  1056. Py_Finalize();
  1057. return 0;
  1058. }
  1059. static int python_generate_script(struct pevent *pevent, const char *outfile)
  1060. {
  1061. struct event_format *event = NULL;
  1062. struct format_field *f;
  1063. char fname[PATH_MAX];
  1064. int not_first, count;
  1065. FILE *ofp;
  1066. sprintf(fname, "%s.py", outfile);
  1067. ofp = fopen(fname, "w");
  1068. if (ofp == NULL) {
  1069. fprintf(stderr, "couldn't open %s\n", fname);
  1070. return -1;
  1071. }
  1072. fprintf(ofp, "# perf script event handlers, "
  1073. "generated by perf script -g python\n");
  1074. fprintf(ofp, "# Licensed under the terms of the GNU GPL"
  1075. " License version 2\n\n");
  1076. fprintf(ofp, "# The common_* event handler fields are the most useful "
  1077. "fields common to\n");
  1078. fprintf(ofp, "# all events. They don't necessarily correspond to "
  1079. "the 'common_*' fields\n");
  1080. fprintf(ofp, "# in the format files. Those fields not available as "
  1081. "handler params can\n");
  1082. fprintf(ofp, "# be retrieved using Python functions of the form "
  1083. "common_*(context).\n");
  1084. fprintf(ofp, "# See the perf-script-python Documentation for the list "
  1085. "of available functions.\n\n");
  1086. fprintf(ofp, "import os\n");
  1087. fprintf(ofp, "import sys\n\n");
  1088. fprintf(ofp, "sys.path.append(os.environ['PERF_EXEC_PATH'] + \\\n");
  1089. fprintf(ofp, "\t'/scripts/python/Perf-Trace-Util/lib/Perf/Trace')\n");
  1090. fprintf(ofp, "\nfrom perf_trace_context import *\n");
  1091. fprintf(ofp, "from Core import *\n\n\n");
  1092. fprintf(ofp, "def trace_begin():\n");
  1093. fprintf(ofp, "\tprint \"in trace_begin\"\n\n");
  1094. fprintf(ofp, "def trace_end():\n");
  1095. fprintf(ofp, "\tprint \"in trace_end\"\n\n");
  1096. while ((event = trace_find_next_event(pevent, event))) {
  1097. fprintf(ofp, "def %s__%s(", event->system, event->name);
  1098. fprintf(ofp, "event_name, ");
  1099. fprintf(ofp, "context, ");
  1100. fprintf(ofp, "common_cpu,\n");
  1101. fprintf(ofp, "\tcommon_secs, ");
  1102. fprintf(ofp, "common_nsecs, ");
  1103. fprintf(ofp, "common_pid, ");
  1104. fprintf(ofp, "common_comm,\n\t");
  1105. fprintf(ofp, "common_callchain, ");
  1106. not_first = 0;
  1107. count = 0;
  1108. for (f = event->format.fields; f; f = f->next) {
  1109. if (not_first++)
  1110. fprintf(ofp, ", ");
  1111. if (++count % 5 == 0)
  1112. fprintf(ofp, "\n\t");
  1113. fprintf(ofp, "%s", f->name);
  1114. }
  1115. if (not_first++)
  1116. fprintf(ofp, ", ");
  1117. if (++count % 5 == 0)
  1118. fprintf(ofp, "\n\t\t");
  1119. fprintf(ofp, "perf_sample_dict");
  1120. fprintf(ofp, "):\n");
  1121. fprintf(ofp, "\t\tprint_header(event_name, common_cpu, "
  1122. "common_secs, common_nsecs,\n\t\t\t"
  1123. "common_pid, common_comm)\n\n");
  1124. fprintf(ofp, "\t\tprint \"");
  1125. not_first = 0;
  1126. count = 0;
  1127. for (f = event->format.fields; f; f = f->next) {
  1128. if (not_first++)
  1129. fprintf(ofp, ", ");
  1130. if (count && count % 3 == 0) {
  1131. fprintf(ofp, "\" \\\n\t\t\"");
  1132. }
  1133. count++;
  1134. fprintf(ofp, "%s=", f->name);
  1135. if (f->flags & FIELD_IS_STRING ||
  1136. f->flags & FIELD_IS_FLAG ||
  1137. f->flags & FIELD_IS_ARRAY ||
  1138. f->flags & FIELD_IS_SYMBOLIC)
  1139. fprintf(ofp, "%%s");
  1140. else if (f->flags & FIELD_IS_SIGNED)
  1141. fprintf(ofp, "%%d");
  1142. else
  1143. fprintf(ofp, "%%u");
  1144. }
  1145. fprintf(ofp, "\" %% \\\n\t\t(");
  1146. not_first = 0;
  1147. count = 0;
  1148. for (f = event->format.fields; f; f = f->next) {
  1149. if (not_first++)
  1150. fprintf(ofp, ", ");
  1151. if (++count % 5 == 0)
  1152. fprintf(ofp, "\n\t\t");
  1153. if (f->flags & FIELD_IS_FLAG) {
  1154. if ((count - 1) % 5 != 0) {
  1155. fprintf(ofp, "\n\t\t");
  1156. count = 4;
  1157. }
  1158. fprintf(ofp, "flag_str(\"");
  1159. fprintf(ofp, "%s__%s\", ", event->system,
  1160. event->name);
  1161. fprintf(ofp, "\"%s\", %s)", f->name,
  1162. f->name);
  1163. } else if (f->flags & FIELD_IS_SYMBOLIC) {
  1164. if ((count - 1) % 5 != 0) {
  1165. fprintf(ofp, "\n\t\t");
  1166. count = 4;
  1167. }
  1168. fprintf(ofp, "symbol_str(\"");
  1169. fprintf(ofp, "%s__%s\", ", event->system,
  1170. event->name);
  1171. fprintf(ofp, "\"%s\", %s)", f->name,
  1172. f->name);
  1173. } else
  1174. fprintf(ofp, "%s", f->name);
  1175. }
  1176. fprintf(ofp, ")\n\n");
  1177. fprintf(ofp, "\t\tprint 'Sample: {'+"
  1178. "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
  1179. fprintf(ofp, "\t\tfor node in common_callchain:");
  1180. fprintf(ofp, "\n\t\t\tif 'sym' in node:");
  1181. fprintf(ofp, "\n\t\t\t\tprint \"\\t[%%x] %%s\" %% (node['ip'], node['sym']['name'])");
  1182. fprintf(ofp, "\n\t\t\telse:");
  1183. fprintf(ofp, "\n\t\t\t\tprint \"\t[%%x]\" %% (node['ip'])\n\n");
  1184. fprintf(ofp, "\t\tprint \"\\n\"\n\n");
  1185. }
  1186. fprintf(ofp, "def trace_unhandled(event_name, context, "
  1187. "event_fields_dict, perf_sample_dict):\n");
  1188. fprintf(ofp, "\t\tprint get_dict_as_string(event_fields_dict)\n");
  1189. fprintf(ofp, "\t\tprint 'Sample: {'+"
  1190. "get_dict_as_string(perf_sample_dict['sample'], ', ')+'}'\n\n");
  1191. fprintf(ofp, "def print_header("
  1192. "event_name, cpu, secs, nsecs, pid, comm):\n"
  1193. "\tprint \"%%-20s %%5u %%05u.%%09u %%8u %%-20s \" %% \\\n\t"
  1194. "(event_name, cpu, secs, nsecs, pid, comm),\n\n");
  1195. fprintf(ofp, "def get_dict_as_string(a_dict, delimiter=' '):\n"
  1196. "\treturn delimiter.join"
  1197. "(['%%s=%%s'%%(k,str(v))for k,v in sorted(a_dict.items())])\n");
  1198. fclose(ofp);
  1199. fprintf(stderr, "generated Python script: %s\n", fname);
  1200. return 0;
  1201. }
  1202. struct scripting_ops python_scripting_ops = {
  1203. .name = "Python",
  1204. .start_script = python_start_script,
  1205. .flush_script = python_flush_script,
  1206. .stop_script = python_stop_script,
  1207. .process_event = python_process_event,
  1208. .process_stat = python_process_stat,
  1209. .process_stat_interval = python_process_stat_interval,
  1210. .generate_script = python_generate_script,
  1211. };