blob: 5d1eb1ccd96c3e0cf73449c97aaee9246d1f9619 [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
Arnaldo Carvalho de Melo91d7b2d2016-04-14 14:48:07 -0300266 if (thread__resolve_callchain(al->thread, &callchain_cursor, evsel,
Arnaldo Carvalho de Melofe176082016-05-19 11:34:06 -0300267 sample, NULL, NULL, scripting_max_stack) != 0) {
Dima Koganf7380c12016-03-29 12:47:53 -0300268 pr_err("Failed to resolve callchain. Skipping\n");
269 goto exit;
270 }
271 callchain_cursor_commit(&callchain_cursor);
272
273
274 while (1) {
275 HV *elem;
276 struct callchain_cursor_node *node;
277 node = callchain_cursor_current(&callchain_cursor);
278 if (!node)
279 break;
280
281 elem = newHV();
282 if (!elem)
283 goto exit;
284
Arnaldo Carvalho de Melo76e20522016-04-05 12:21:44 -0300285 if (!hv_stores(elem, "ip", newSVuv(node->ip))) {
286 hv_undef(elem);
287 goto exit;
288 }
Dima Koganf7380c12016-03-29 12:47:53 -0300289
290 if (node->sym) {
291 HV *sym = newHV();
Arnaldo Carvalho de Melo76e20522016-04-05 12:21:44 -0300292 if (!sym) {
293 hv_undef(elem);
Dima Koganf7380c12016-03-29 12:47:53 -0300294 goto exit;
Arnaldo Carvalho de Melo76e20522016-04-05 12:21:44 -0300295 }
296 if (!hv_stores(sym, "start", newSVuv(node->sym->start)) ||
297 !hv_stores(sym, "end", newSVuv(node->sym->end)) ||
298 !hv_stores(sym, "binding", newSVuv(node->sym->binding)) ||
299 !hv_stores(sym, "name", newSVpvn(node->sym->name,
300 node->sym->namelen)) ||
301 !hv_stores(elem, "sym", newRV_noinc((SV*)sym))) {
302 hv_undef(sym);
303 hv_undef(elem);
304 goto exit;
305 }
Dima Koganf7380c12016-03-29 12:47:53 -0300306 }
307
308 if (node->map) {
309 struct map *map = node->map;
310 const char *dsoname = "[unknown]";
311 if (map && map->dso && (map->dso->name || map->dso->long_name)) {
312 if (symbol_conf.show_kernel_path && map->dso->long_name)
313 dsoname = map->dso->long_name;
314 else if (map->dso->name)
315 dsoname = map->dso->name;
316 }
Arnaldo Carvalho de Melo76e20522016-04-05 12:21:44 -0300317 if (!hv_stores(elem, "dso", newSVpv(dsoname,0))) {
318 hv_undef(elem);
319 goto exit;
320 }
Dima Koganf7380c12016-03-29 12:47:53 -0300321 }
322
323 callchain_cursor_advance(&callchain_cursor);
324 av_push(list, newRV_noinc((SV*)elem));
325 }
326
327exit:
328 return newRV_noinc((SV*)list);
329}
330
Arnaldo Carvalho de Melo8853a1b2013-12-19 16:39:31 -0300331static void perl_process_tracepoint(struct perf_sample *sample,
Robert Richter37a058e2011-12-15 18:23:43 +0100332 struct perf_evsel *evsel,
Dima Koganf7380c12016-03-29 12:47:53 -0300333 struct addr_location *al)
Tom Zanussi16c632d2009-11-25 01:15:48 -0600334{
Dima Koganf7380c12016-03-29 12:47:53 -0300335 struct thread *thread = al->thread;
Jiri Olsacdae2d12014-10-26 23:44:04 +0100336 struct event_format *event = evsel->tp_format;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600337 struct format_field *field;
338 static char handler[256];
339 unsigned long long val;
340 unsigned long s, ns;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600341 int pid;
David Ahernbe6d8422011-03-09 22:23:23 -0700342 int cpu = sample->cpu;
343 void *data = sample->raw_data;
344 unsigned long long nsecs = sample->time;
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200345 const char *comm = thread__comm_str(thread);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600346
347 dSP;
348
Robert Richter37a058e2011-12-15 18:23:43 +0100349 if (evsel->attr.type != PERF_TYPE_TRACEPOINT)
350 return;
351
Tom Zanussi16c632d2009-11-25 01:15:48 -0600352 if (!event)
Arnaldo Carvalho de Melo6650b182013-10-14 18:25:12 -0300353 die("ug! no event found for type %" PRIu64, (u64)evsel->attr.config);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600354
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300355 pid = raw_field_value(event, "common_pid", data);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600356
357 sprintf(handler, "%s::%s", event->system, event->name);
358
Jiri Olsacdae2d12014-10-26 23:44:04 +0100359 if (!test_and_set_bit(event->id, events_defined))
360 define_event_symbols(event, handler, event->print_fmt.args);
361
Tom Zanussi16c632d2009-11-25 01:15:48 -0600362 s = nsecs / NSECS_PER_SEC;
363 ns = nsecs - s * NSECS_PER_SEC;
364
365 scripting_context->event_data = data;
Tom Zanussi2de95332013-01-18 13:51:27 -0600366 scripting_context->pevent = evsel->tp_format->pevent;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600367
368 ENTER;
369 SAVETMPS;
370 PUSHMARK(SP);
371
372 XPUSHs(sv_2mortal(newSVpv(handler, 0)));
373 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
374 XPUSHs(sv_2mortal(newSVuv(cpu)));
375 XPUSHs(sv_2mortal(newSVuv(s)));
376 XPUSHs(sv_2mortal(newSVuv(ns)));
377 XPUSHs(sv_2mortal(newSViv(pid)));
378 XPUSHs(sv_2mortal(newSVpv(comm, 0)));
Dima Koganf7380c12016-03-29 12:47:53 -0300379 XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
Tom Zanussi16c632d2009-11-25 01:15:48 -0600380
381 /* common fields other than pid can be accessed via xsub fns */
382
383 for (field = event->format.fields; field; field = field->next) {
384 if (field->flags & FIELD_IS_STRING) {
385 int offset;
386 if (field->flags & FIELD_IS_DYNAMIC) {
387 offset = *(int *)(data + field->offset);
388 offset &= 0xffff;
389 } else
390 offset = field->offset;
391 XPUSHs(sv_2mortal(newSVpv((char *)data + offset, 0)));
392 } else { /* FIELD_IS_NUMERIC */
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300393 val = read_size(event, data + field->offset,
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300394 field->size);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600395 if (field->flags & FIELD_IS_SIGNED) {
396 XPUSHs(sv_2mortal(newSViv(val)));
397 } else {
398 XPUSHs(sv_2mortal(newSVuv(val)));
399 }
400 }
401 }
402
403 PUTBACK;
Tom Zanussid1b93772009-11-25 01:15:50 -0600404
Tom Zanussi16c632d2009-11-25 01:15:48 -0600405 if (get_cv(handler, 0))
406 call_pv(handler, G_SCALAR);
407 else if (get_cv("main::trace_unhandled", 0)) {
408 XPUSHs(sv_2mortal(newSVpv(handler, 0)));
409 XPUSHs(sv_2mortal(newSViv(PTR2IV(scripting_context))));
410 XPUSHs(sv_2mortal(newSVuv(cpu)));
411 XPUSHs(sv_2mortal(newSVuv(nsecs)));
412 XPUSHs(sv_2mortal(newSViv(pid)));
413 XPUSHs(sv_2mortal(newSVpv(comm, 0)));
Dima Koganf7380c12016-03-29 12:47:53 -0300414 XPUSHs(sv_2mortal(perl_process_callchain(sample, evsel, al)));
Tom Zanussi16c632d2009-11-25 01:15:48 -0600415 call_pv("main::trace_unhandled", G_SCALAR);
416 }
417 SPAGAIN;
418 PUTBACK;
419 FREETMPS;
420 LEAVE;
421}
422
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300423static void perl_process_event_generic(union perf_event *event,
Robert Richter37a058e2011-12-15 18:23:43 +0100424 struct perf_sample *sample,
Arnaldo Carvalho de Melo8853a1b2013-12-19 16:39:31 -0300425 struct perf_evsel *evsel)
Robert Richter37a058e2011-12-15 18:23:43 +0100426{
427 dSP;
428
429 if (!get_cv("process_event", 0))
430 return;
431
432 ENTER;
433 SAVETMPS;
434 PUSHMARK(SP);
Arnaldo Carvalho de Melo97822432012-08-07 23:50:21 -0300435 XPUSHs(sv_2mortal(newSVpvn((const char *)event, event->header.size)));
Robert Richter37a058e2011-12-15 18:23:43 +0100436 XPUSHs(sv_2mortal(newSVpvn((const char *)&evsel->attr, sizeof(evsel->attr))));
437 XPUSHs(sv_2mortal(newSVpvn((const char *)sample, sizeof(*sample))));
438 XPUSHs(sv_2mortal(newSVpvn((const char *)sample->raw_data, sample->raw_size)));
439 PUTBACK;
440 call_pv("process_event", G_SCALAR);
441 SPAGAIN;
442 PUTBACK;
443 FREETMPS;
444 LEAVE;
445}
446
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300447static void perl_process_event(union perf_event *event,
Robert Richter37a058e2011-12-15 18:23:43 +0100448 struct perf_sample *sample,
449 struct perf_evsel *evsel,
Arnaldo Carvalho de Melof9d5d542015-04-01 13:29:25 -0300450 struct addr_location *al)
Robert Richter37a058e2011-12-15 18:23:43 +0100451{
Dima Koganf7380c12016-03-29 12:47:53 -0300452 perl_process_tracepoint(sample, evsel, al);
Arnaldo Carvalho de Melo8853a1b2013-12-19 16:39:31 -0300453 perl_process_event_generic(event, sample, evsel);
Robert Richter37a058e2011-12-15 18:23:43 +0100454}
455
Tom Zanussi16c632d2009-11-25 01:15:48 -0600456static void run_start_sub(void)
457{
458 dSP; /* access to Perl stack */
459 PUSHMARK(SP);
460
461 if (get_cv("main::trace_begin", 0))
462 call_pv("main::trace_begin", G_DISCARD | G_NOARGS);
463}
464
465/*
466 * Start trace script
467 */
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600468static int perl_start_script(const char *script, int argc, const char **argv)
Tom Zanussi16c632d2009-11-25 01:15:48 -0600469{
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600470 const char **command_line;
471 int i, err = 0;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600472
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600473 command_line = malloc((argc + 2) * sizeof(const char *));
474 command_line[0] = "";
Tom Zanussi16c632d2009-11-25 01:15:48 -0600475 command_line[1] = script;
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600476 for (i = 2; i < argc + 2; i++)
477 command_line[i] = argv[i - 2];
Tom Zanussi16c632d2009-11-25 01:15:48 -0600478
479 my_perl = perl_alloc();
480 perl_construct(my_perl);
481
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600482 if (perl_parse(my_perl, xs_init, argc + 2, (char **)command_line,
483 (char **)NULL)) {
484 err = -1;
485 goto error;
486 }
Tom Zanussi16c632d2009-11-25 01:15:48 -0600487
Tom Zanussi8f11d852009-12-15 02:53:37 -0600488 if (perl_run(my_perl)) {
489 err = -1;
490 goto error;
491 }
492
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600493 if (SvTRUE(ERRSV)) {
494 err = -1;
495 goto error;
496 }
Tom Zanussi16c632d2009-11-25 01:15:48 -0600497
498 run_start_sub();
499
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600500 free(command_line);
Tom Zanussi16c632d2009-11-25 01:15:48 -0600501 return 0;
Tom Zanussi586bc5c2009-12-15 02:53:35 -0600502error:
503 perl_free(my_perl);
504 free(command_line);
505
506 return err;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600507}
508
Adrian Hunterd445dd22014-08-15 22:08:37 +0300509static int perl_flush_script(void)
510{
511 return 0;
512}
513
Tom Zanussi16c632d2009-11-25 01:15:48 -0600514/*
515 * Stop trace script
516 */
517static int perl_stop_script(void)
518{
519 dSP; /* access to Perl stack */
520 PUSHMARK(SP);
521
522 if (get_cv("main::trace_end", 0))
523 call_pv("main::trace_end", G_DISCARD | G_NOARGS);
524
525 perl_destruct(my_perl);
526 perl_free(my_perl);
527
Tom Zanussi16c632d2009-11-25 01:15:48 -0600528 return 0;
529}
530
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300531static int perl_generate_script(struct pevent *pevent, const char *outfile)
Tom Zanussi16c632d2009-11-25 01:15:48 -0600532{
Frederic Weisbecker8784eb72012-05-22 16:30:49 +0200533 struct event_format *event = NULL;
Tom Zanussi16c632d2009-11-25 01:15:48 -0600534 struct format_field *f;
535 char fname[PATH_MAX];
536 int not_first, count;
537 FILE *ofp;
538
539 sprintf(fname, "%s.pl", outfile);
540 ofp = fopen(fname, "w");
541 if (ofp == NULL) {
542 fprintf(stderr, "couldn't open %s\n", fname);
543 return -1;
544 }
545
Ingo Molnar133dc4c2010-11-16 18:45:39 +0100546 fprintf(ofp, "# perf script event handlers, "
547 "generated by perf script -g perl\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600548
549 fprintf(ofp, "# Licensed under the terms of the GNU GPL"
550 " License version 2\n\n");
551
552 fprintf(ofp, "# The common_* event handler fields are the most useful "
553 "fields common to\n");
554
555 fprintf(ofp, "# all events. They don't necessarily correspond to "
556 "the 'common_*' fields\n");
557
558 fprintf(ofp, "# in the format files. Those fields not available as "
559 "handler params can\n");
560
561 fprintf(ofp, "# be retrieved using Perl functions of the form "
562 "common_*($context).\n");
563
564 fprintf(ofp, "# See Context.pm for the list of available "
565 "functions.\n\n");
566
567 fprintf(ofp, "use lib \"$ENV{'PERF_EXEC_PATH'}/scripts/perl/"
568 "Perf-Trace-Util/lib\";\n");
569
570 fprintf(ofp, "use lib \"./Perf-Trace-Util/lib\";\n");
571 fprintf(ofp, "use Perf::Trace::Core;\n");
572 fprintf(ofp, "use Perf::Trace::Context;\n");
573 fprintf(ofp, "use Perf::Trace::Util;\n\n");
574
575 fprintf(ofp, "sub trace_begin\n{\n\t# optional\n}\n\n");
Dima Koganf7380c12016-03-29 12:47:53 -0300576 fprintf(ofp, "sub trace_end\n{\n\t# optional\n}\n");
577
578
579 fprintf(ofp, "\n\
580sub print_backtrace\n\
581{\n\
582 my $callchain = shift;\n\
583 for my $node (@$callchain)\n\
584 {\n\
585 if(exists $node->{sym})\n\
586 {\n\
587 printf( \"\\t[\\%%x] \\%%s\\n\", $node->{ip}, $node->{sym}{name});\n\
588 }\n\
589 else\n\
590 {\n\
591 printf( \"\\t[\\%%x]\\n\", $node{ip});\n\
592 }\n\
593 }\n\
594}\n\n\
595");
596
Tom Zanussi16c632d2009-11-25 01:15:48 -0600597
Arnaldo Carvalho de Meloda378962012-06-27 13:08:42 -0300598 while ((event = trace_find_next_event(pevent, event))) {
Tom Zanussi16c632d2009-11-25 01:15:48 -0600599 fprintf(ofp, "sub %s::%s\n{\n", event->system, event->name);
600 fprintf(ofp, "\tmy (");
601
602 fprintf(ofp, "$event_name, ");
603 fprintf(ofp, "$context, ");
604 fprintf(ofp, "$common_cpu, ");
605 fprintf(ofp, "$common_secs, ");
606 fprintf(ofp, "$common_nsecs,\n");
607 fprintf(ofp, "\t $common_pid, ");
Dima Koganf7380c12016-03-29 12:47:53 -0300608 fprintf(ofp, "$common_comm, ");
609 fprintf(ofp, "$common_callchain,\n\t ");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600610
611 not_first = 0;
612 count = 0;
613
614 for (f = event->format.fields; f; f = f->next) {
615 if (not_first++)
616 fprintf(ofp, ", ");
617 if (++count % 5 == 0)
618 fprintf(ofp, "\n\t ");
619
620 fprintf(ofp, "$%s", f->name);
621 }
622 fprintf(ofp, ") = @_;\n\n");
623
624 fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
625 "$common_secs, $common_nsecs,\n\t "
Dima Koganf7380c12016-03-29 12:47:53 -0300626 "$common_pid, $common_comm, $common_callchain);\n\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600627
628 fprintf(ofp, "\tprintf(\"");
629
630 not_first = 0;
631 count = 0;
632
633 for (f = event->format.fields; f; f = f->next) {
634 if (not_first++)
635 fprintf(ofp, ", ");
636 if (count && count % 4 == 0) {
637 fprintf(ofp, "\".\n\t \"");
638 }
639 count++;
640
641 fprintf(ofp, "%s=", f->name);
642 if (f->flags & FIELD_IS_STRING ||
643 f->flags & FIELD_IS_FLAG ||
644 f->flags & FIELD_IS_SYMBOLIC)
645 fprintf(ofp, "%%s");
646 else if (f->flags & FIELD_IS_SIGNED)
647 fprintf(ofp, "%%d");
648 else
649 fprintf(ofp, "%%u");
650 }
651
652 fprintf(ofp, "\\n\",\n\t ");
653
654 not_first = 0;
655 count = 0;
656
657 for (f = event->format.fields; f; f = f->next) {
658 if (not_first++)
659 fprintf(ofp, ", ");
660
661 if (++count % 5 == 0)
662 fprintf(ofp, "\n\t ");
663
664 if (f->flags & FIELD_IS_FLAG) {
665 if ((count - 1) % 5 != 0) {
666 fprintf(ofp, "\n\t ");
667 count = 4;
668 }
669 fprintf(ofp, "flag_str(\"");
670 fprintf(ofp, "%s::%s\", ", event->system,
671 event->name);
672 fprintf(ofp, "\"%s\", $%s)", f->name,
673 f->name);
674 } else if (f->flags & FIELD_IS_SYMBOLIC) {
675 if ((count - 1) % 5 != 0) {
676 fprintf(ofp, "\n\t ");
677 count = 4;
678 }
679 fprintf(ofp, "symbol_str(\"");
680 fprintf(ofp, "%s::%s\", ", event->system,
681 event->name);
682 fprintf(ofp, "\"%s\", $%s)", f->name,
683 f->name);
684 } else
685 fprintf(ofp, "$%s", f->name);
686 }
687
Dima Koganf7380c12016-03-29 12:47:53 -0300688 fprintf(ofp, ");\n\n");
689
690 fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
691
Tom Zanussi16c632d2009-11-25 01:15:48 -0600692 fprintf(ofp, "}\n\n");
693 }
694
695 fprintf(ofp, "sub trace_unhandled\n{\n\tmy ($event_name, $context, "
696 "$common_cpu, $common_secs, $common_nsecs,\n\t "
Dima Koganf7380c12016-03-29 12:47:53 -0300697 "$common_pid, $common_comm, $common_callchain) = @_;\n\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600698
699 fprintf(ofp, "\tprint_header($event_name, $common_cpu, "
700 "$common_secs, $common_nsecs,\n\t $common_pid, "
Dima Koganf7380c12016-03-29 12:47:53 -0300701 "$common_comm, $common_callchain);\n");
702 fprintf(ofp, "\tprint_backtrace($common_callchain);\n");
703 fprintf(ofp, "}\n\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600704
705 fprintf(ofp, "sub print_header\n{\n"
706 "\tmy ($event_name, $cpu, $secs, $nsecs, $pid, $comm) = @_;\n\n"
707 "\tprintf(\"%%-20s %%5u %%05u.%%09u %%8u %%-20s \",\n\t "
Robert Richter37a058e2011-12-15 18:23:43 +0100708 "$event_name, $cpu, $secs, $nsecs, $pid, $comm);\n}\n");
709
710 fprintf(ofp,
711 "\n# Packed byte string args of process_event():\n"
712 "#\n"
713 "# $event:\tunion perf_event\tutil/event.h\n"
714 "# $attr:\tstruct perf_event_attr\tlinux/perf_event.h\n"
715 "# $sample:\tstruct perf_sample\tutil/event.h\n"
716 "# $raw_data:\tperf_sample->raw_data\tutil/event.h\n"
717 "\n"
718 "sub process_event\n"
719 "{\n"
720 "\tmy ($event, $attr, $sample, $raw_data) = @_;\n"
721 "\n"
722 "\tmy @event\t= unpack(\"LSS\", $event);\n"
723 "\tmy @attr\t= unpack(\"LLQQQQQLLQQ\", $attr);\n"
724 "\tmy @sample\t= unpack(\"QLLQQQQQLL\", $sample);\n"
725 "\tmy @raw_data\t= unpack(\"C*\", $raw_data);\n"
726 "\n"
727 "\tuse Data::Dumper;\n"
728 "\tprint Dumper \\@event, \\@attr, \\@sample, \\@raw_data;\n"
729 "}\n");
Tom Zanussi16c632d2009-11-25 01:15:48 -0600730
731 fclose(ofp);
732
733 fprintf(stderr, "generated Perl script: %s\n", fname);
734
735 return 0;
736}
737
738struct scripting_ops perl_scripting_ops = {
739 .name = "Perl",
740 .start_script = perl_start_script,
Adrian Hunterd445dd22014-08-15 22:08:37 +0300741 .flush_script = perl_flush_script,
Tom Zanussi16c632d2009-11-25 01:15:48 -0600742 .stop_script = perl_stop_script,
743 .process_event = perl_process_event,
744 .generate_script = perl_generate_script,
745};