trace-event-python.c 37 KB

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