blob: 1d160855cda9224affef9c2d3f385125bef2fcf5 [file] [log] [blame]
Tom Zanussi16c632d2009-11-25 01:15:48 -06001/*
Ingo Molnar133dc4c2010-11-16 18:45:39 +01002 * trace-event-perl. Feed perf script events to an embedded Perl interpreter.
Tom Zanussi16c632d2009-11-25 01:15:48 -06003 *
4 * Copyright (C) 2009 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
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <ctype.h>
26#include <errno.h>
Jiri Olsacdae2d12014-10-26 23:44:04 +010027#include <linux/bitmap.h>
Tom Zanussi16c632d2009-11-25 01:15:48 -060028
Tom Zanussi82d156c2010-01-27 02:27:55 -060029#include "../util.h"
David Ahern12046092012-08-29 09:55:32 -060030#include <EXTERN.h>
31#include <perl.h>
32
33#include "../../perf.h"
Dima Koganf7380c12016-03-29 12:47:53 -030034#include "../callchain.h"
35#include "../machine.h"
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -020036#include "../thread.h"
37#include "../event.h"
Tom Zanussi82d156c2010-01-27 02:27:55 -060038#include "../trace-event.h"
Robert Richter37a058e2011-12-15 18:23:43 +010039#include "../evsel.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +020040#include "../debug.h"
Tom Zanussi82d156c2010-01-27 02:27:55 -060041
Tom Zanussi82d156c2010-01-27 02:27:55 -060042void boot_Perf__Trace__Context(pTHX_ CV *cv);
43void boot_DynaLoader(pTHX_ CV *cv);
44typedef PerlInterpreter * INTERP;
Tom Zanussi16c632d2009-11-25 01:15:48 -060045
Tom Zanussid1b93772009-11-25 01:15:50 -060046void xs_init(pTHX);
47
Tom Zanussid1b93772009-11-25 01:15:50 -060048void xs_init(pTHX)
49{
50 const char *file = __FILE__;
51 dXSUB_SYS;
52
53 newXS("Perf::Trace::Context::bootstrap", boot_Perf__Trace__Context,
54 file);
55 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
56}
57
Tom Zanussi16c632d2009-11-25 01:15:48 -060058INTERP my_perl;
59
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040060#define TRACE_EVENT_TYPE_MAX \
Tom Zanussi16c632d2009-11-25 01:15:48 -060061 ((1 << (sizeof(unsigned short) * 8)) - 1)
62
Steven Rostedt (Red Hat)609a7402015-05-13 13:44:36 -040063static DECLARE_BITMAP(events_defined, TRACE_EVENT_TYPE_MAX);
Tom Zanussi16c632d2009-11-25 01:15:48 -060064
Tom Zanussi82d156c2010-01-27 02:27:55 -060065extern struct scripting_context *scripting_context;
Tom Zanussi16c632d2009-11-25 01:15:48 -060066
67static char *cur_field_name;
68static int zero_flag_atom;
69
70static void define_symbolic_value(const char *ev_name,
71 const char *field_name,
72 const char *field_value,
73 const char *field_str)
74{
75 unsigned long long value;
76 dSP;
77
78 value = eval_flag(field_value);
79
80 ENTER;
81 SAVETMPS;
82 PUSHMARK(SP);
83
84 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
85 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
86 XPUSHs(sv_2mortal(newSVuv(value)));
87 XPUSHs(sv_2mortal(newSVpv(field_str, 0)));
88
89 PUTBACK;
90 if (get_cv("main::define_symbolic_value", 0))
91 call_pv("main::define_symbolic_value", G_SCALAR);
92 SPAGAIN;
93 PUTBACK;
94 FREETMPS;
95 LEAVE;
96}
97
98static void define_symbolic_values(struct print_flag_sym *field,
99 const char *ev_name,
100 const char *field_name)
101{
102 define_symbolic_value(ev_name, field_name, field->value, field->str);
103 if (field->next)
104 define_symbolic_values(field->next, ev_name, field_name);
105}
106
107static void define_symbolic_field(const char *ev_name,
108 const char *field_name)
109{
110 dSP;
111
112 ENTER;
113 SAVETMPS;
114 PUSHMARK(SP);
115
116 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
117 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
118
119 PUTBACK;
120 if (get_cv("main::define_symbolic_field", 0))
121 call_pv("main::define_symbolic_field", G_SCALAR);
122 SPAGAIN;
123 PUTBACK;
124 FREETMPS;
125 LEAVE;
126}
127
128static void define_flag_value(const char *ev_name,
129 const char *field_name,
130 const char *field_value,
131 const char *field_str)
132{
133 unsigned long long value;
134 dSP;
135
136 value = eval_flag(field_value);
137
138 ENTER;
139 SAVETMPS;
140 PUSHMARK(SP);
141
142 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
143 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
144 XPUSHs(sv_2mortal(newSVuv(value)));
145 XPUSHs(sv_2mortal(newSVpv(field_str, 0)));
146
147 PUTBACK;
148 if (get_cv("main::define_flag_value", 0))
149 call_pv("main::define_flag_value", G_SCALAR);
150 SPAGAIN;
151 PUTBACK;
152 FREETMPS;
153 LEAVE;
154}
155
156static void define_flag_values(struct print_flag_sym *field,
157 const char *ev_name,
158 const char *field_name)
159{
160 define_flag_value(ev_name, field_name, field->value, field->str);
161 if (field->next)
162 define_flag_values(field->next, ev_name, field_name);
163}
164
165static void define_flag_field(const char *ev_name,
166 const char *field_name,
167 const char *delim)
168{
169 dSP;
170
171 ENTER;
172 SAVETMPS;
173 PUSHMARK(SP);
174
175 XPUSHs(sv_2mortal(newSVpv(ev_name, 0)));
176 XPUSHs(sv_2mortal(newSVpv(field_name, 0)));
177 XPUSHs(sv_2mortal(newSVpv(delim, 0)));
178
179 PUTBACK;
180 if (get_cv("main::define_flag_field", 0))
181 call_pv("main::define_flag_field", G_SCALAR);
182 SPAGAIN;
183 PUTBACK;
184 FREETMPS;
185 LEAVE;
186}
187
Frederic Weisbecker8784eb72012-05-22 16:30:49 +0200188static void define_event_symbols(struct event_format *event,
Tom Zanussi16c632d2009-11-25 01:15:48 -0600189 const char *ev_name,
190 struct print_arg *args)
191{
Taeung Song8579aca2016-02-26 00:12:59 +0900192 if (args == NULL)
193 return;
194
Tom Zanussi16c632d2009-11-25 01:15:48 -0600195 switch (args->type) {
196 case PRINT_NULL:
197 break;
198 case PRINT_ATOM:
199 define_flag_value(ev_name, cur_field_name, "0",
200 args->atom.atom);
201 zero_flag_atom = 0;
202 break;
203 case PRINT_FIELD:
Arnaldo Carvalho de Melof5385652013-12-26 15:54:57 -0300204 free(cur_field_name);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600205 cur_field_name = strdup(args->field.name);
206 break;
207 case PRINT_FLAGS:
208 define_event_symbols(event, ev_name, args->flags.field);
209 define_flag_field(ev_name, cur_field_name, args->flags.delim);
210 define_flag_values(args->flags.flags, ev_name, cur_field_name);
211 break;
212 case PRINT_SYMBOL:
213 define_event_symbols(event, ev_name, args->symbol.field);
214 define_symbolic_field(ev_name, cur_field_name);
215 define_symbolic_values(args->symbol.symbols, ev_name,
216 cur_field_name);
217 break;
Namhyung Kime080e6f2012-06-27 09:41:41 +0900218 case PRINT_HEX:
219 define_event_symbols(event, ev_name, args->hex.field);
220 define_event_symbols(event, ev_name, args->hex.size);
221 break;
Javi Merinob839e1e82015-03-24 11:07:19 +0000222 case PRINT_INT_ARRAY:
223 define_event_symbols(event, ev_name, args->int_array.field);
224 define_event_symbols(event, ev_name, args->int_array.count);
225 define_event_symbols(event, ev_name, args->int_array.el_size);
226 break;
Frederic Weisbeckere326e752012-05-22 16:30:48 +0200227 case PRINT_BSTRING:
228 case PRINT_DYNAMIC_ARRAY:
He Kuang76055942015-08-29 04:22:05 +0000229 case PRINT_DYNAMIC_ARRAY_LEN:
Tom Zanussi16c632d2009-11-25 01:15:48 -0600230 case PRINT_STRING:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400231 case PRINT_BITMASK:
Tom Zanussi16c632d2009-11-25 01:15:48 -0600232 break;
233 case PRINT_TYPE:
234 define_event_symbols(event, ev_name, args->typecast.item);
235 break;
236 case PRINT_OP:
237 if (strcmp(args->op.op, ":") == 0)
238 zero_flag_atom = 1;
239 define_event_symbols(event, ev_name, args->op.left);
240 define_event_symbols(event, ev_name, args->op.right);
241 break;
Frederic Weisbeckere326e752012-05-22 16:30:48 +0200242 case PRINT_FUNC:
Tom Zanussi16c632d2009-11-25 01:15:48 -0600243 default:
Frederic Weisbeckere326e752012-05-22 16:30:48 +0200244 pr_err("Unsupported print arg type\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600245 /* we should warn... */
246 return;
247 }
248
249 if (args->next)
250 define_event_symbols(event, ev_name, args->next);
251}
252
Dima Koganf7380c12016-03-29 12:47:53 -0300253static SV *perl_process_callchain(struct perf_sample *sample,
254 struct perf_evsel *evsel,
255 struct addr_location *al)
256{
257 AV *list;
258
259 list = newAV();
260 if (!list)
261 goto exit;
262
263 if (!symbol_conf.use_callchain || !sample->callchain)
264 goto exit;
265
266 if (thread__resolve_callchain(al->thread, evsel,
267 sample, NULL, NULL,
268 PERF_MAX_STACK_DEPTH) != 0) {
269 pr_err("Failed to resolve callchain. Skipping\n");
270 goto exit;
271 }
272 callchain_cursor_commit(&callchain_cursor);
273
274
275 while (1) {
276 HV *elem;
277 struct callchain_cursor_node *node;
278 node = callchain_cursor_current(&callchain_cursor);
279 if (!node)
280 break;
281
282 elem = newHV();
283 if (!elem)
284 goto exit;
285
286 hv_stores(elem, "ip", newSVuv(node->ip));
287
288 if (node->sym) {
289 HV *sym = newHV();
290 if (!sym)
291 goto exit;
292 hv_stores(sym, "start", newSVuv(node->sym->start));
293 hv_stores(sym, "end", newSVuv(node->sym->end));
294 hv_stores(sym, "binding", newSVuv(node->sym->binding));
295 hv_stores(sym, "name", newSVpvn(node->sym->name,
296 node->sym->namelen));
297 hv_stores(elem, "sym", newRV_noinc((SV*)sym));
298 }
299
300 if (node->map) {
301 struct map *map = node->map;
302 const char *dsoname = "[unknown]";
303 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
304 if (symbol_conf.show_kernel_path && map->dso->long_name)
305 dsoname = map->dso->long_name;
306 else if (map->dso->name)
307 dsoname = map->dso->name;
308 }
309 hv_stores(elem, "dso", newSVpv(dsoname,0));
310 }
311
312 callchain_cursor_advance(&callchain_cursor);
313 av_push(list, newRV_noinc((SV*)elem));
314 }
315
316exit:
317 return newRV_noinc((SV*)list);
318}
319
Arnaldo Carvalho de Melo8853a1b2013-12-19 16:39:31 -0300320static void perl_process_tracepoint(struct perf_sample *sample,
Robert Richter37a058e2011-12-15 18:23:43 +0100321 struct perf_evsel *evsel,
Dima Koganf7380c12016-03-29 12:47:53 -0300322 struct addr_location *al)
Tom Zanussi16c632d2009-11-25 01:15:48 -0600323{
Dima Koganf7380c12016-03-29 12:47:53 -0300324 struct thread *thread = al->thread;
Jiri Olsacdae2d12014-10-26 23:44:04 +0100325 struct event_format *event = evsel->tp_format;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600326 struct format_field *field;
327 static char handler[256];
328 unsigned long long val;
329 unsigned long s, ns;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600330 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700331 int cpu = sample->cpu;
332 void *data = sample->raw_data;
333 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200334 const char *comm = thread__comm_str(thread);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600335
336 dSP;
337
Robert Richter37a058e2011-12-15 18:23:43 +0100338 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
339 return;
340
Tom Zanussi16c632d2009-11-25 01:15:48 -0600341 if (!event)
Arnaldo Carvalho de Melo6650b182013-10-14 18:25:12 -0300342 die("ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600343
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300344 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600345
346 sprintf(handler, "%s::%s", event->system, event->name);
347
Jiri Olsacdae2d12014-10-26 23:44:04 +0100348 if (!test_and_set_bit(event->id, events_defined))
349 define_event_symbols(event, handler, event->print_fmt.args);
350
Tom Zanussi16c632d2009-11-25 01:15:48 -0600351 s = nsecs / NSECS_PER_SEC;
352 ns = nsecs - s * NSECS_PER_SEC;
353
354 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600355 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600356
357 ENTER;
358 SAVETMPS;
359 PUSHMARK(SP);
360
361 XPUSHs(sv_2mortal(newSVpv(handler, 0)));
362 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
363 XPUSHs(sv_2mortal(newSVuv(cpu)));
364 XPUSHs(sv_2mortal(newSVuv(s)));
365 XPUSHs(sv_2mortal(newSVuv(ns)));
366 XPUSHs(sv_2mortal(newSViv(pid)));
367 XPUSHs(sv_2mortal(newSVpv(comm, 0)));
Dima Koganf7380c12016-03-29 12:47:53 -0300368 XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
Tom Zanussi16c632d2009-11-25 01:15:48 -0600369
370 /* common fields other than pid can be accessed via xsub fns */
371
372 for (field = event->format.fields; field; field = field->next) {
373 if (field->flags & FIELD_IS_STRING) {
374 int offset;
375 if (field->flags & FIELD_IS_DYNAMIC) {
376 offset = *(int *)(data + field->offset);
377 offset &= 0xffff;
378 } else
379 offset = field->offset;
380 XPUSHs(sv_2mortal(newSVpv((char *)data + offset, 0)));
381 } else { /* FIELD_IS_NUMERIC */
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300382 val = read_size(event, data + field->offset,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300383 field->size);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600384 if (field->flags & FIELD_IS_SIGNED) {
385 XPUSHs(sv_2mortal(newSViv(val)));
386 } else {
387 XPUSHs(sv_2mortal(newSVuv(val)));
388 }
389 }
390 }
391
392 PUTBACK;
Tom Zanussid1b93772009-11-25 01:15:50 -0600393
Tom Zanussi16c632d2009-11-25 01:15:48 -0600394 if (get_cv(handler, 0))
395 call_pv(handler, G_SCALAR);
396 else if (get_cv("main::trace_unhandled", 0)) {
397 XPUSHs(sv_2mortal(newSVpv(handler, 0)));
398 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
399 XPUSHs(sv_2mortal(newSVuv(cpu)));
400 XPUSHs(sv_2mortal(newSVuv(nsecs)));
401 XPUSHs(sv_2mortal(newSViv(pid)));
402 XPUSHs(sv_2mortal(newSVpv(comm, 0)));
Dima Koganf7380c12016-03-29 12:47:53 -0300403 XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
Tom Zanussi16c632d2009-11-25 01:15:48 -0600404 call_pv("main::trace_unhandled", G_SCALAR);
405 }
406 SPAGAIN;
407 PUTBACK;
408 FREETMPS;
409 LEAVE;
410}
411
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300412static void perl_process_event_generic(union perf_event *event,
Robert Richter37a058e2011-12-15 18:23:43 +0100413 struct perf_sample *sample,
Arnaldo Carvalho de Melo8853a1b2013-12-19 16:39:31 -0300414 struct perf_evsel *evsel)
Robert Richter37a058e2011-12-15 18:23:43 +0100415{
416 dSP;
417
418 if (!get_cv("process_event", 0))
419 return;
420
421 ENTER;
422 SAVETMPS;
423 PUSHMARK(SP);
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300424 XPUSHs(sv_2mortal(newSVpvn((const char *)event, event->header.size)));
Robert Richter37a058e2011-12-15 18:23:43 +0100425 XPUSHs(sv_2mortal(newSVpvn((const char *)&evsel->attr, sizeof(evsel->attr))));
426 XPUSHs(sv_2mortal(newSVpvn((const char *)sample, sizeof(*sample))));
427 XPUSHs(sv_2mortal(newSVpvn((const char *)sample->raw_data, sample->raw_size)));
428 PUTBACK;
429 call_pv("process_event", G_SCALAR);
430 SPAGAIN;
431 PUTBACK;
432 FREETMPS;
433 LEAVE;
434}
435
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300436static void perl_process_event(union perf_event *event,
Robert Richter37a058e2011-12-15 18:23:43 +0100437 struct perf_sample *sample,
438 struct perf_evsel *evsel,
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300439 struct addr_location *al)
Robert Richter37a058e2011-12-15 18:23:43 +0100440{
Dima Koganf7380c12016-03-29 12:47:53 -0300441 perl_process_tracepoint(sample, evsel, al);
Arnaldo Carvalho de Melo8853a1b2013-12-19 16:39:31 -0300442 perl_process_event_generic(event, sample, evsel);
Robert Richter37a058e2011-12-15 18:23:43 +0100443}
444
Tom Zanussi16c632d2009-11-25 01:15:48 -0600445static void run_start_sub(void)
446{
447 dSP; /* access to Perl stack */
448 PUSHMARK(SP);
449
450 if (get_cv("main::trace_begin", 0))
451 call_pv("main::trace_begin", G_DISCARD | G_NOARGS);
452}
453
454/*
455 * Start trace script
456 */
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600457static int perl_start_script(const char *script, int argc, const char **argv)
Tom Zanussi16c632d2009-11-25 01:15:48 -0600458{
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600459 const char **command_line;
460 int i, err = 0;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600461
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600462 command_line = malloc((argc + 2) * sizeof(const char *));
463 command_line[0] = "";
Tom Zanussi16c632d2009-11-25 01:15:48 -0600464 command_line[1] = script;
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600465 for (i = 2; i < argc + 2; i++)
466 command_line[i] = argv[i - 2];
Tom Zanussi16c632d2009-11-25 01:15:48 -0600467
468 my_perl = perl_alloc();
469 perl_construct(my_perl);
470
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600471 if (perl_parse(my_perl, xs_init, argc + 2, (char **)command_line,
472 (char **)NULL)) {
473 err = -1;
474 goto error;
475 }
Tom Zanussi16c632d2009-11-25 01:15:48 -0600476
Tom Zanussi8f11d852009-12-15 02:53:37 -0600477 if (perl_run(my_perl)) {
478 err = -1;
479 goto error;
480 }
481
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600482 if (SvTRUE(ERRSV)) {
483 err = -1;
484 goto error;
485 }
Tom Zanussi16c632d2009-11-25 01:15:48 -0600486
487 run_start_sub();
488
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600489 free(command_line);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600490 return 0;
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600491error:
492 perl_free(my_perl);
493 free(command_line);
494
495 return err;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600496}
497
Adrian Hunterd445dd22014-08-15 22:08:37 +0300498static int perl_flush_script(void)
499{
500 return 0;
501}
502
Tom Zanussi16c632d2009-11-25 01:15:48 -0600503/*
504 * Stop trace script
505 */
506static int perl_stop_script(void)
507{
508 dSP; /* access to Perl stack */
509 PUSHMARK(SP);
510
511 if (get_cv("main::trace_end", 0))
512 call_pv("main::trace_end", G_DISCARD | G_NOARGS);
513
514 perl_destruct(my_perl);
515 perl_free(my_perl);
516
Tom Zanussi16c632d2009-11-25 01:15:48 -0600517 return 0;
518}
519
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300520static int perl_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi16c632d2009-11-25 01:15:48 -0600521{
Frederic Weisbecker8784eb72012-05-22 16:30:49 +0200522 struct event_format *event = NULL;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600523 struct format_field *f;
524 char fname[PATH_MAX];
525 int not_first, count;
526 FILE *ofp;
527
528 sprintf(fname, "%s.pl", outfile);
529 ofp = fopen(fname, "w");
530 if (ofp == NULL) {
531 fprintf(stderr, "couldn't open %s\n", fname);
532 return -1;
533 }
534
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100535 fprintf(ofp, "# perf script event handlers, "
536 "generated by perf script -g perl\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600537
538 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
539 " License version 2\n\n");
540
541 fprintf(ofp, "# The common_* event handler fields are the most useful "
542 "fields common to\n");
543
544 fprintf(ofp, "# all events. They don't necessarily correspond to "
545 "the 'common_*' fields\n");
546
547 fprintf(ofp, "# in the format files. Those fields not available as "
548 "handler params can\n");
549
550 fprintf(ofp, "# be retrieved using Perl functions of the form "
551 "common_*($context).\n");
552
553 fprintf(ofp, "# See Context.pm for the list of available "
554 "functions.\n\n");
555
556 fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
557 "Perf-Trace-Util/lib\";\n");
558
559 fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n");
560 fprintf(ofp, "use Perf::Trace::Core;\n");
561 fprintf(ofp, "use Perf::Trace::Context;\n");
562 fprintf(ofp, "use Perf::Trace::Util;\n\n");
563
564 fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n");
Dima Koganf7380c12016-03-29 12:47:53 -0300565 fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n");
566
567
568 fprintf(ofp, "\n\
569sub print_backtrace\n\
570{\n\
571 my $callchain = shift;\n\
572 for my $node (@$callchain)\n\
573 {\n\
574 if(exists $node->{sym})\n\
575 {\n\
576 printf( \"\\t[\\%%x] \\%%s\\n\", $node->{ip}, $node->{sym}{name});\n\
577 }\n\
578 else\n\
579 {\n\
580 printf( \"\\t[\\%%x]\\n\", $node{ip});\n\
581 }\n\
582 }\n\
583}\n\n\
584");
585
Tom Zanussi16c632d2009-11-25 01:15:48 -0600586
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300587 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi16c632d2009-11-25 01:15:48 -0600588 fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
589 fprintf(ofp, "\tmy (");
590
591 fprintf(ofp, "$event_name, ");
592 fprintf(ofp, "$context, ");
593 fprintf(ofp, "$common_cpu, ");
594 fprintf(ofp, "$common_secs, ");
595 fprintf(ofp, "$common_nsecs,\n");
596 fprintf(ofp, "\t $common_pid, ");
Dima Koganf7380c12016-03-29 12:47:53 -0300597 fprintf(ofp, "$common_comm, ");
598 fprintf(ofp, "$common_callchain,\n\t ");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600599
600 not_first = 0;
601 count = 0;
602
603 for (f = event->format.fields; f; f = f->next) {
604 if (not_first++)
605 fprintf(ofp, ", ");
606 if (++count % 5 == 0)
607 fprintf(ofp, "\n\t ");
608
609 fprintf(ofp, "$%s", f->name);
610 }
611 fprintf(ofp, ") = @_;\n\n");
612
613 fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
614 "$common_secs, $common_nsecs,\n\t "
Dima Koganf7380c12016-03-29 12:47:53 -0300615 "$common_pid, $common_comm, $common_callchain);\n\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600616
617 fprintf(ofp, "\tprintf(\"");
618
619 not_first = 0;
620 count = 0;
621
622 for (f = event->format.fields; f; f = f->next) {
623 if (not_first++)
624 fprintf(ofp, ", ");
625 if (count && count % 4 == 0) {
626 fprintf(ofp, "\".\n\t \"");
627 }
628 count++;
629
630 fprintf(ofp, "%s=", f->name);
631 if (f->flags & FIELD_IS_STRING ||
632 f->flags & FIELD_IS_FLAG ||
633 f->flags & FIELD_IS_SYMBOLIC)
634 fprintf(ofp, "%%s");
635 else if (f->flags & FIELD_IS_SIGNED)
636 fprintf(ofp, "%%d");
637 else
638 fprintf(ofp, "%%u");
639 }
640
641 fprintf(ofp, "\\n\",\n\t ");
642
643 not_first = 0;
644 count = 0;
645
646 for (f = event->format.fields; f; f = f->next) {
647 if (not_first++)
648 fprintf(ofp, ", ");
649
650 if (++count % 5 == 0)
651 fprintf(ofp, "\n\t ");
652
653 if (f->flags & FIELD_IS_FLAG) {
654 if ((count - 1) % 5 != 0) {
655 fprintf(ofp, "\n\t ");
656 count = 4;
657 }
658 fprintf(ofp, "flag_str(\"");
659 fprintf(ofp, "%s::%s\", ", event->system,
660 event->name);
661 fprintf(ofp, "\"%s\", $%s)", f->name,
662 f->name);
663 } else if (f->flags & FIELD_IS_SYMBOLIC) {
664 if ((count - 1) % 5 != 0) {
665 fprintf(ofp, "\n\t ");
666 count = 4;
667 }
668 fprintf(ofp, "symbol_str(\"");
669 fprintf(ofp, "%s::%s\", ", event->system,
670 event->name);
671 fprintf(ofp, "\"%s\", $%s)", f->name,
672 f->name);
673 } else
674 fprintf(ofp, "$%s", f->name);
675 }
676
Dima Koganf7380c12016-03-29 12:47:53 -0300677 fprintf(ofp, ");\n\n");
678
679 fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
680
Tom Zanussi16c632d2009-11-25 01:15:48 -0600681 fprintf(ofp, "}\n\n");
682 }
683
684 fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
685 "$common_cpu, $common_secs, $common_nsecs,\n\t "
Dima Koganf7380c12016-03-29 12:47:53 -0300686 "$common_pid, $common_comm, $common_callchain) = @_;\n\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600687
688 fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
689 "$common_secs, $common_nsecs,\n\t $common_pid, "
Dima Koganf7380c12016-03-29 12:47:53 -0300690 "$common_comm, $common_callchain);\n");
691 fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
692 fprintf(ofp, "}\n\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600693
694 fprintf(ofp, "sub print_header\n{\n"
695 "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
696 "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
Robert Richter37a058e2011-12-15 18:23:43 +0100697 "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}\n");
698
699 fprintf(ofp,
700 "\n# Packed byte string args of process_event():\n"
701 "#\n"
702 "# $event:\tunion perf_event\tutil/event.h\n"
703 "# $attr:\tstruct perf_event_attr\tlinux/perf_event.h\n"
704 "# $sample:\tstruct perf_sample\tutil/event.h\n"
705 "# $raw_data:\tperf_sample->raw_data\tutil/event.h\n"
706 "\n"
707 "sub process_event\n"
708 "{\n"
709 "\tmy ($event, $attr, $sample, $raw_data) = @_;\n"
710 "\n"
711 "\tmy @event\t= unpack(\"LSS\", $event);\n"
712 "\tmy @attr\t= unpack(\"LLQQQQQLLQQ\", $attr);\n"
713 "\tmy @sample\t= unpack(\"QLLQQQQQLL\", $sample);\n"
714 "\tmy @raw_data\t= unpack(\"C*\", $raw_data);\n"
715 "\n"
716 "\tuse Data::Dumper;\n"
717 "\tprint Dumper \\@event, \\@attr, \\@sample, \\@raw_data;\n"
718 "}\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600719
720 fclose(ofp);
721
722 fprintf(stderr, "generated Perl script: %s\n", fname);
723
724 return 0;
725}
726
727struct scripting_ops perl_scripting_ops = {
728 .name = "Perl",
729 .start_script = perl_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300730 .flush_script = perl_flush_script,
Tom Zanussi16c632d2009-11-25 01:15:48 -0600731 .stop_script = perl_stop_script,
732 .process_event = perl_process_event,
733 .generate_script = perl_generate_script,
734};