blob: ceefa9b3d72654a9f15cc063547da58f0c891104 [file] [log] [blame]
Steven Rostedt (VMware)6ab025e2018-08-16 11:10:15 -04001// SPDX-License-Identifier: LGPL-2.1
Steven Rostedtf7d82352012-04-06 00:47:53 +02002/*
3 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 *
Steven Rostedtf7d82352012-04-06 00:47:53 +02005 *
6 * The parts for function graph printing was taken and modified from the
7 * Linux Kernel that were written by
8 * - Copyright (C) 2009 Frederic Weisbecker,
9 * Frederic Weisbecker gave his permission to relicense the code to
10 * the Lesser General Public License.
11 */
Arnaldo Carvalho de Meloca575ad2016-07-14 11:23:25 -030012#include <inttypes.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020013#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include <stdarg.h>
17#include <ctype.h>
18#include <errno.h>
Robert Richter0cf26012012-08-07 19:43:14 +020019#include <stdint.h>
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -030020#include <limits.h>
Steven Rostedtbb5a7312016-11-22 15:00:31 -030021#include <linux/time64.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020022
Arnaldo Carvalho de Meloca575ad2016-07-14 11:23:25 -030023#include <netinet/in.h>
Steven Rostedtf7d82352012-04-06 00:47:53 +020024#include "event-parse.h"
Tzvetomir Stoyanovbb3dd7e2018-10-05 12:22:25 -040025
26#include "event-parse-local.h"
Steven Rostedt668fe012012-04-06 00:47:55 +020027#include "event-utils.h"
Tzvetomir Stoyanov (VMware)266b8512018-08-28 18:50:38 -040028#include "trace-seq.h"
Steven Rostedtf7d82352012-04-06 00:47:53 +020029
30static const char *input_buf;
31static unsigned long long input_buf_ptr;
32static unsigned long long input_buf_siz;
33
Tom Zanussi5205aec2012-04-06 00:47:58 +020034static int is_flag_field;
35static int is_symbolic_field;
36
Steven Rostedtf7d82352012-04-06 00:47:53 +020037static int show_warning = 1;
38
39#define do_warning(fmt, ...) \
40 do { \
41 if (show_warning) \
42 warning(fmt, ##__VA_ARGS__); \
43 } while (0)
44
Namhyung Kim3388cc32014-03-19 10:22:53 +090045#define do_warning_event(event, fmt, ...) \
46 do { \
47 if (!show_warning) \
48 continue; \
49 \
50 if (event) \
51 warning("[%s:%s] " fmt, event->system, \
52 event->name, ##__VA_ARGS__); \
53 else \
54 warning(fmt, ##__VA_ARGS__); \
55 } while (0)
56
Steven Rostedtf7d82352012-04-06 00:47:53 +020057static void init_input_buf(const char *buf, unsigned long long size)
58{
59 input_buf = buf;
60 input_buf_siz = size;
61 input_buf_ptr = 0;
62}
63
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -040064const char *tep_get_input_buf(void)
Steven Rostedtf7d82352012-04-06 00:47:53 +020065{
66 return input_buf;
67}
68
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -040069unsigned long long tep_get_input_buf_ptr(void)
Steven Rostedtf7d82352012-04-06 00:47:53 +020070{
71 return input_buf_ptr;
72}
73
74struct event_handler {
75 struct event_handler *next;
76 int id;
77 const char *sys_name;
78 const char *event_name;
Tzvetomir Stoyanov (VMware)c32d52b2018-08-08 14:02:48 -040079 tep_event_handler_func func;
Steven Rostedtf7d82352012-04-06 00:47:53 +020080 void *context;
81};
82
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -040083struct func_params {
84 struct func_params *next;
85 enum tep_func_arg_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +020086};
87
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -040088struct tep_function_handler {
89 struct tep_function_handler *next;
90 enum tep_func_arg_type ret_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +020091 char *name;
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -040092 tep_func_handler func;
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -040093 struct func_params *params;
Steven Rostedtf7d82352012-04-06 00:47:53 +020094 int nr_args;
95};
96
97static unsigned long long
98process_defined_func(struct trace_seq *s, void *data, int size,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -050099 struct tep_event *event, struct tep_print_arg *arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200100
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -0400101static void free_func_handle(struct tep_function_handler *func);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200102
103/**
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -0400104 * tep_buffer_init - init buffer for parsing
Steven Rostedtf7d82352012-04-06 00:47:53 +0200105 * @buf: buffer to parse
106 * @size: the size of the buffer
107 *
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -0400108 * For use with tep_read_token(), this initializes the internal
109 * buffer that tep_read_token() will parse.
Steven Rostedtf7d82352012-04-06 00:47:53 +0200110 */
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -0400111void tep_buffer_init(const char *buf, unsigned long long size)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200112{
113 init_input_buf(buf, size);
114}
115
116void breakpoint(void)
117{
118 static int x;
119 x++;
120}
121
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -0400122struct tep_print_arg *alloc_arg(void)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200123{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -0400124 return calloc(1, sizeof(struct tep_print_arg));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200125}
126
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -0500127struct tep_cmdline {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200128 char *comm;
129 int pid;
130};
131
132static int cmdline_cmp(const void *a, const void *b)
133{
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -0500134 const struct tep_cmdline *ca = a;
135 const struct tep_cmdline *cb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200136
137 if (ca->pid < cb->pid)
138 return -1;
139 if (ca->pid > cb->pid)
140 return 1;
141
142 return 0;
143}
144
145struct cmdline_list {
146 struct cmdline_list *next;
147 char *comm;
148 int pid;
149};
150
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400151static int cmdline_init(struct tep_handle *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200152{
153 struct cmdline_list *cmdlist = pevent->cmdlist;
154 struct cmdline_list *item;
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -0500155 struct tep_cmdline *cmdlines;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200156 int i;
157
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300158 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
159 if (!cmdlines)
160 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200161
162 i = 0;
163 while (cmdlist) {
164 cmdlines[i].pid = cmdlist->pid;
165 cmdlines[i].comm = cmdlist->comm;
166 i++;
167 item = cmdlist;
168 cmdlist = cmdlist->next;
169 free(item);
170 }
171
172 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
173
174 pevent->cmdlines = cmdlines;
175 pevent->cmdlist = NULL;
176
177 return 0;
178}
179
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400180static const char *find_cmdline(struct tep_handle *pevent, int pid)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200181{
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -0500182 const struct tep_cmdline *comm;
183 struct tep_cmdline key;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200184
185 if (!pid)
186 return "<idle>";
187
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300188 if (!pevent->cmdlines && cmdline_init(pevent))
189 return "<not enough memory for cmdlines!>";
Steven Rostedtf7d82352012-04-06 00:47:53 +0200190
191 key.pid = pid;
192
193 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
194 sizeof(*pevent->cmdlines), cmdline_cmp);
195
196 if (comm)
197 return comm->comm;
198 return "<...>";
199}
200
201/**
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -0400202 * tep_is_pid_registered - return if a pid has a cmdline registered
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400203 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +0200204 * @pid: The pid to check if it has a cmdline registered with.
205 *
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -0400206 * Returns true if the pid has a cmdline mapped to it
207 * false otherwise.
Steven Rostedtf7d82352012-04-06 00:47:53 +0200208 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400209bool tep_is_pid_registered(struct tep_handle *tep, int pid)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200210{
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -0500211 const struct tep_cmdline *comm;
212 struct tep_cmdline key;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200213
214 if (!pid)
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -0400215 return true;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200216
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400217 if (!tep->cmdlines && cmdline_init(tep))
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -0400218 return false;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200219
220 key.pid = pid;
221
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400222 comm = bsearch(&key, tep->cmdlines, tep->cmdline_count,
223 sizeof(*tep->cmdlines), cmdline_cmp);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200224
225 if (comm)
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -0400226 return true;
227 return false;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200228}
229
230/*
231 * If the command lines have been converted to an array, then
232 * we must add this pid. This is much slower than when cmdlines
233 * are added before the array is initialized.
234 */
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500235static int add_new_comm(struct tep_handle *pevent,
236 const char *comm, int pid, bool override)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200237{
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -0500238 struct tep_cmdline *cmdlines = pevent->cmdlines;
239 struct tep_cmdline *cmdline;
240 struct tep_cmdline key;
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500241 char *new_comm;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200242
243 if (!pid)
244 return 0;
245
246 /* avoid duplicates */
247 key.pid = pid;
248
249 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
250 sizeof(*pevent->cmdlines), cmdline_cmp);
251 if (cmdline) {
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500252 if (!override) {
253 errno = EEXIST;
254 return -1;
255 }
256 new_comm = strdup(comm);
257 if (!new_comm) {
258 errno = ENOMEM;
259 return -1;
260 }
261 free(cmdline->comm);
262 cmdline->comm = new_comm;
263
264 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200265 }
266
267 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
268 if (!cmdlines) {
269 errno = ENOMEM;
270 return -1;
271 }
272
Steven Rostedtf7d82352012-04-06 00:47:53 +0200273 cmdlines[pevent->cmdline_count].comm = strdup(comm);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300274 if (!cmdlines[pevent->cmdline_count].comm) {
275 free(cmdlines);
276 errno = ENOMEM;
277 return -1;
278 }
279
280 cmdlines[pevent->cmdline_count].pid = pid;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200281
282 if (cmdlines[pevent->cmdline_count].comm)
283 pevent->cmdline_count++;
284
285 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
286 pevent->cmdlines = cmdlines;
287
288 return 0;
289}
290
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400291static int _tep_register_comm(struct tep_handle *tep,
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500292 const char *comm, int pid, bool override)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200293{
294 struct cmdline_list *item;
295
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400296 if (tep->cmdlines)
297 return add_new_comm(tep, comm, pid, override);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200298
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300299 item = malloc(sizeof(*item));
300 if (!item)
301 return -1;
302
Josef Bacikdeab6f52015-03-24 09:57:49 -0400303 if (comm)
304 item->comm = strdup(comm);
305 else
306 item->comm = strdup("<...>");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300307 if (!item->comm) {
308 free(item);
309 return -1;
310 }
Steven Rostedtf7d82352012-04-06 00:47:53 +0200311 item->pid = pid;
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400312 item->next = tep->cmdlist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200313
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400314 tep->cmdlist = item;
315 tep->cmdline_count++;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200316
317 return 0;
318}
319
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500320/**
321 * tep_register_comm - register a pid / comm mapping
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400322 * @tep: a handle to the trace event parser context
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500323 * @comm: the command line to register
324 * @pid: the pid to map the command line to
325 *
326 * This adds a mapping to search for command line names with
327 * a given pid. The comm is duplicated. If a command with the same pid
328 * already exist, -1 is returned and errno is set to EEXIST
329 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400330int tep_register_comm(struct tep_handle *tep, const char *comm, int pid)
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500331{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400332 return _tep_register_comm(tep, comm, pid, false);
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500333}
334
335/**
336 * tep_override_comm - register a pid / comm mapping
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400337 * @tep: a handle to the trace event parser context
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500338 * @comm: the command line to register
339 * @pid: the pid to map the command line to
340 *
341 * This adds a mapping to search for command line names with
342 * a given pid. The comm is duplicated. If a command with the same pid
343 * already exist, the command string is udapted with the new one
344 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400345int tep_override_comm(struct tep_handle *tep, const char *comm, int pid)
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500346{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400347 if (!tep->cmdlines && cmdline_init(tep)) {
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500348 errno = ENOMEM;
349 return -1;
350 }
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400351 return _tep_register_comm(tep, comm, pid, true);
Tzvetomir Stoyanovca3958b2018-11-30 10:44:11 -0500352}
353
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400354int tep_register_trace_clock(struct tep_handle *tep, const char *trace_clock)
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400355{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400356 tep->trace_clock = strdup(trace_clock);
357 if (!tep->trace_clock) {
Steven Rostedt (Red Hat)99ad1412015-03-24 09:57:50 -0400358 errno = ENOMEM;
359 return -1;
360 }
361 return 0;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -0400362}
363
Steven Rostedtf7d82352012-04-06 00:47:53 +0200364struct func_map {
365 unsigned long long addr;
366 char *func;
367 char *mod;
368};
369
370struct func_list {
371 struct func_list *next;
372 unsigned long long addr;
373 char *func;
374 char *mod;
375};
376
377static int func_cmp(const void *a, const void *b)
378{
379 const struct func_map *fa = a;
380 const struct func_map *fb = b;
381
382 if (fa->addr < fb->addr)
383 return -1;
384 if (fa->addr > fb->addr)
385 return 1;
386
387 return 0;
388}
389
390/*
391 * We are searching for a record in between, not an exact
392 * match.
393 */
394static int func_bcmp(const void *a, const void *b)
395{
396 const struct func_map *fa = a;
397 const struct func_map *fb = b;
398
399 if ((fa->addr == fb->addr) ||
400
401 (fa->addr > fb->addr &&
402 fa->addr < (fb+1)->addr))
403 return 0;
404
405 if (fa->addr < fb->addr)
406 return -1;
407
408 return 1;
409}
410
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400411static int func_map_init(struct tep_handle *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200412{
413 struct func_list *funclist;
414 struct func_list *item;
415 struct func_map *func_map;
416 int i;
417
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300418 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
419 if (!func_map)
420 return -1;
421
Steven Rostedtf7d82352012-04-06 00:47:53 +0200422 funclist = pevent->funclist;
423
424 i = 0;
425 while (funclist) {
426 func_map[i].func = funclist->func;
427 func_map[i].addr = funclist->addr;
428 func_map[i].mod = funclist->mod;
429 i++;
430 item = funclist;
431 funclist = funclist->next;
432 free(item);
433 }
434
435 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
436
437 /*
438 * Add a special record at the end.
439 */
440 func_map[pevent->func_count].func = NULL;
441 func_map[pevent->func_count].addr = 0;
442 func_map[pevent->func_count].mod = NULL;
443
444 pevent->func_map = func_map;
445 pevent->funclist = NULL;
446
447 return 0;
448}
449
450static struct func_map *
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400451__find_func(struct tep_handle *pevent, unsigned long long addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200452{
453 struct func_map *func;
454 struct func_map key;
455
456 if (!pevent->func_map)
457 func_map_init(pevent);
458
459 key.addr = addr;
460
461 func = bsearch(&key, pevent->func_map, pevent->func_count,
462 sizeof(*pevent->func_map), func_bcmp);
463
464 return func;
465}
466
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300467struct func_resolver {
Tzvetomir Stoyanov (VMware)4d5c58b2018-08-08 14:02:49 -0400468 tep_func_resolver_t *func;
469 void *priv;
470 struct func_map map;
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300471};
472
473/**
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -0400474 * tep_set_function_resolver - set an alternative function resolver
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400475 * @tep: a handle to the trace event parser context
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300476 * @resolver: function to be used
477 * @priv: resolver function private state.
478 *
479 * Some tools may have already a way to resolve kernel functions, allow them to
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400480 * keep using it instead of duplicating all the entries inside tep->funclist.
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300481 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400482int tep_set_function_resolver(struct tep_handle *tep,
Tzvetomir Stoyanov (VMware)ece2a4f2018-08-08 14:02:55 -0400483 tep_func_resolver_t *func, void *priv)
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300484{
485 struct func_resolver *resolver = malloc(sizeof(*resolver));
486
487 if (resolver == NULL)
488 return -1;
489
490 resolver->func = func;
491 resolver->priv = priv;
492
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400493 free(tep->func_resolver);
494 tep->func_resolver = resolver;
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300495
496 return 0;
497}
498
499/**
Tzvetomir Stoyanov (VMware)c99eeaf2018-08-08 14:03:08 -0400500 * tep_reset_function_resolver - reset alternative function resolver
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400501 * @tep: a handle to the trace event parser context
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300502 *
503 * Stop using whatever alternative resolver was set, use the default
504 * one instead.
505 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400506void tep_reset_function_resolver(struct tep_handle *tep)
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300507{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400508 free(tep->func_resolver);
509 tep->func_resolver = NULL;
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300510}
511
512static struct func_map *
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400513find_func(struct tep_handle *pevent, unsigned long long addr)
Arnaldo Carvalho de Melo33a24712015-07-22 12:36:55 -0300514{
515 struct func_map *map;
516
517 if (!pevent->func_resolver)
518 return __find_func(pevent, addr);
519
520 map = &pevent->func_resolver->map;
521 map->mod = NULL;
522 map->addr = addr;
523 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
524 &map->addr, &map->mod);
525 if (map->func == NULL)
526 return NULL;
527
528 return map;
529}
530
Steven Rostedtf7d82352012-04-06 00:47:53 +0200531/**
Tzvetomir Stoyanov (VMware)610e1e42018-08-08 14:03:05 -0400532 * tep_find_function - find a function by a given address
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400533 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +0200534 * @addr: the address to find the function with
535 *
536 * Returns a pointer to the function stored that has the given
537 * address. Note, the address does not have to be exact, it
538 * will select the function that would contain the address.
539 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400540const char *tep_find_function(struct tep_handle *tep, unsigned long long addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200541{
542 struct func_map *map;
543
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400544 map = find_func(tep, addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200545 if (!map)
546 return NULL;
547
548 return map->func;
549}
550
551/**
Tzvetomir Stoyanov (VMware)610e1e42018-08-08 14:03:05 -0400552 * tep_find_function_address - find a function address by a given address
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400553 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +0200554 * @addr: the address to find the function with
555 *
556 * Returns the address the function starts at. This can be used in
Tzvetomir Stoyanov (VMware)610e1e42018-08-08 14:03:05 -0400557 * conjunction with tep_find_function to print both the function
Steven Rostedtf7d82352012-04-06 00:47:53 +0200558 * name and the function offset.
559 */
560unsigned long long
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400561tep_find_function_address(struct tep_handle *tep, unsigned long long addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200562{
563 struct func_map *map;
564
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400565 map = find_func(tep, addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200566 if (!map)
567 return 0;
568
569 return map->addr;
570}
571
572/**
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -0400573 * tep_register_function - register a function with a given address
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400574 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +0200575 * @function: the function name to register
576 * @addr: the address the function starts at
577 * @mod: the kernel module the function may be in (NULL for none)
578 *
579 * This registers a function name with an address and module.
580 * The @func passed in is duplicated.
581 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400582int tep_register_function(struct tep_handle *tep, char *func,
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -0400583 unsigned long long addr, char *mod)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200584{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300585 struct func_list *item = malloc(sizeof(*item));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200586
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300587 if (!item)
588 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200589
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400590 item->next = tep->funclist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200591 item->func = strdup(func);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300592 if (!item->func)
593 goto out_free;
594
595 if (mod) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200596 item->mod = strdup(mod);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300597 if (!item->mod)
598 goto out_free_func;
599 } else
Steven Rostedtf7d82352012-04-06 00:47:53 +0200600 item->mod = NULL;
601 item->addr = addr;
602
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400603 tep->funclist = item;
604 tep->func_count++;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200605
606 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300607
608out_free_func:
609 free(item->func);
610 item->func = NULL;
611out_free:
612 free(item);
613 errno = ENOMEM;
614 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200615}
616
617/**
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -0400618 * tep_print_funcs - print out the stored functions
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400619 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +0200620 *
621 * This prints out the stored functions.
622 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400623void tep_print_funcs(struct tep_handle *tep)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200624{
625 int i;
626
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400627 if (!tep->func_map)
628 func_map_init(tep);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200629
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400630 for (i = 0; i < (int)tep->func_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200631 printf("%016llx %s",
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400632 tep->func_map[i].addr,
633 tep->func_map[i].func);
634 if (tep->func_map[i].mod)
635 printf(" [%s]\n", tep->func_map[i].mod);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200636 else
637 printf("\n");
638 }
639}
640
641struct printk_map {
642 unsigned long long addr;
643 char *printk;
644};
645
646struct printk_list {
647 struct printk_list *next;
648 unsigned long long addr;
649 char *printk;
650};
651
652static int printk_cmp(const void *a, const void *b)
653{
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900654 const struct printk_map *pa = a;
655 const struct printk_map *pb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200656
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900657 if (pa->addr < pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200658 return -1;
Namhyung Kim0fc45ef2012-04-09 11:54:29 +0900659 if (pa->addr > pb->addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200660 return 1;
661
662 return 0;
663}
664
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400665static int printk_map_init(struct tep_handle *pevent)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200666{
667 struct printk_list *printklist;
668 struct printk_list *item;
669 struct printk_map *printk_map;
670 int i;
671
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300672 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
673 if (!printk_map)
674 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200675
676 printklist = pevent->printklist;
677
678 i = 0;
679 while (printklist) {
680 printk_map[i].printk = printklist->printk;
681 printk_map[i].addr = printklist->addr;
682 i++;
683 item = printklist;
684 printklist = printklist->next;
685 free(item);
686 }
687
688 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
689
690 pevent->printk_map = printk_map;
691 pevent->printklist = NULL;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300692
693 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200694}
695
696static struct printk_map *
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -0400697find_printk(struct tep_handle *pevent, unsigned long long addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200698{
699 struct printk_map *printk;
700 struct printk_map key;
701
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300702 if (!pevent->printk_map && printk_map_init(pevent))
703 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200704
705 key.addr = addr;
706
707 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
708 sizeof(*pevent->printk_map), printk_cmp);
709
710 return printk;
711}
712
713/**
Tzvetomir Stoyanov (VMware)13a41892018-08-08 14:02:54 -0400714 * tep_register_print_string - register a string by its address
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400715 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +0200716 * @fmt: the string format to register
717 * @addr: the address the string was located at
718 *
719 * This registers a string by the address it was stored in the kernel.
720 * The @fmt passed in is duplicated.
721 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400722int tep_register_print_string(struct tep_handle *tep, const char *fmt,
Tzvetomir Stoyanov (VMware)13a41892018-08-08 14:02:54 -0400723 unsigned long long addr)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200724{
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300725 struct printk_list *item = malloc(sizeof(*item));
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400726 char *p;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200727
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300728 if (!item)
729 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200730
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400731 item->next = tep->printklist;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200732 item->addr = addr;
733
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400734 /* Strip off quotes and '\n' from the end */
735 if (fmt[0] == '"')
736 fmt++;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300737 item->printk = strdup(fmt);
Namhyung Kimca638582012-04-09 11:54:31 +0900738 if (!item->printk)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300739 goto out_free;
Namhyung Kimca638582012-04-09 11:54:31 +0900740
Steven Rostedt (Red Hat)18900af2013-11-01 17:53:54 -0400741 p = item->printk + strlen(item->printk) - 1;
742 if (*p == '"')
743 *p = 0;
744
745 p -= 2;
746 if (strcmp(p, "\\n") == 0)
747 *p = 0;
748
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400749 tep->printklist = item;
750 tep->printk_count++;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200751
752 return 0;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300753
754out_free:
755 free(item);
756 errno = ENOMEM;
757 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200758}
759
760/**
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -0400761 * tep_print_printk - print out the stored strings
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400762 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +0200763 *
764 * This prints the string formats that were stored.
765 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400766void tep_print_printk(struct tep_handle *tep)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200767{
768 int i;
769
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400770 if (!tep->printk_map)
771 printk_map_init(tep);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200772
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400773 for (i = 0; i < (int)tep->printk_count; i++) {
Steven Rostedtf7d82352012-04-06 00:47:53 +0200774 printf("%016llx %s\n",
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -0400775 tep->printk_map[i].addr,
776 tep->printk_map[i].printk);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200777 }
778}
779
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500780static struct tep_event *alloc_event(void)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200781{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500782 return calloc(1, sizeof(struct tep_event));
Steven Rostedtf7d82352012-04-06 00:47:53 +0200783}
784
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500785static int add_event(struct tep_handle *pevent, struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200786{
787 int i;
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -0500788 struct tep_event **events = realloc(pevent->events, sizeof(event) *
789 (pevent->nr_events + 1));
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300790 if (!events)
791 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200792
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300793 pevent->events = events;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200794
795 for (i = 0; i < pevent->nr_events; i++) {
796 if (pevent->events[i]->id > event->id)
797 break;
798 }
799 if (i < pevent->nr_events)
800 memmove(&pevent->events[i + 1],
801 &pevent->events[i],
802 sizeof(event) * (pevent->nr_events - i));
803
804 pevent->events[i] = event;
805 pevent->nr_events++;
806
807 event->pevent = pevent;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -0300808
809 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200810}
811
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400812static int event_item_type(enum tep_event_type type)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200813{
814 switch (type) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400815 case TEP_EVENT_ITEM ... TEP_EVENT_SQUOTE:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200816 return 1;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400817 case TEP_EVENT_ERROR ... TEP_EVENT_DELIM:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200818 default:
819 return 0;
820 }
821}
822
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -0400823static void free_flag_sym(struct tep_print_flag_sym *fsym)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200824{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -0400825 struct tep_print_flag_sym *next;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200826
827 while (fsym) {
828 next = fsym->next;
829 free(fsym->value);
830 free(fsym->str);
831 free(fsym);
832 fsym = next;
833 }
834}
835
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -0400836static void free_arg(struct tep_print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200837{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -0400838 struct tep_print_arg *farg;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200839
840 if (!arg)
841 return;
842
843 switch (arg->type) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400844 case TEP_PRINT_ATOM:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200845 free(arg->atom.atom);
846 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400847 case TEP_PRINT_FIELD:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200848 free(arg->field.name);
849 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400850 case TEP_PRINT_FLAGS:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200851 free_arg(arg->flags.field);
852 free(arg->flags.delim);
853 free_flag_sym(arg->flags.flags);
854 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400855 case TEP_PRINT_SYMBOL:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200856 free_arg(arg->symbol.field);
857 free_flag_sym(arg->symbol.symbols);
858 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400859 case TEP_PRINT_HEX:
860 case TEP_PRINT_HEX_STR:
Namhyung Kime080e6f2012-06-27 09:41:41 +0900861 free_arg(arg->hex.field);
862 free_arg(arg->hex.size);
863 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400864 case TEP_PRINT_INT_ARRAY:
Javi Merinob839e1e82015-03-24 11:07:19 +0000865 free_arg(arg->int_array.field);
866 free_arg(arg->int_array.count);
867 free_arg(arg->int_array.el_size);
868 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400869 case TEP_PRINT_TYPE:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200870 free(arg->typecast.type);
871 free_arg(arg->typecast.item);
872 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400873 case TEP_PRINT_STRING:
874 case TEP_PRINT_BSTRING:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200875 free(arg->string.string);
876 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400877 case TEP_PRINT_BITMASK:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -0400878 free(arg->bitmask.bitmask);
879 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400880 case TEP_PRINT_DYNAMIC_ARRAY:
881 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200882 free(arg->dynarray.index);
883 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400884 case TEP_PRINT_OP:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200885 free(arg->op.op);
886 free_arg(arg->op.left);
887 free_arg(arg->op.right);
888 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400889 case TEP_PRINT_FUNC:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200890 while (arg->func.args) {
891 farg = arg->func.args;
892 arg->func.args = farg->next;
893 free_arg(farg);
894 }
895 break;
896
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -0400897 case TEP_PRINT_NULL:
Steven Rostedtf7d82352012-04-06 00:47:53 +0200898 default:
899 break;
900 }
901
902 free(arg);
903}
904
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400905static enum tep_event_type get_type(int ch)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200906{
907 if (ch == '\n')
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400908 return TEP_EVENT_NEWLINE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200909 if (isspace(ch))
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400910 return TEP_EVENT_SPACE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200911 if (isalnum(ch) || ch == '_')
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400912 return TEP_EVENT_ITEM;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200913 if (ch == '\'')
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400914 return TEP_EVENT_SQUOTE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200915 if (ch == '"')
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400916 return TEP_EVENT_DQUOTE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200917 if (!isprint(ch))
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400918 return TEP_EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200919 if (ch == '(' || ch == ')' || ch == ',')
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400920 return TEP_EVENT_DELIM;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200921
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400922 return TEP_EVENT_OP;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200923}
924
925static int __read_char(void)
926{
927 if (input_buf_ptr >= input_buf_siz)
928 return -1;
929
930 return input_buf[input_buf_ptr++];
931}
932
933static int __peek_char(void)
934{
935 if (input_buf_ptr >= input_buf_siz)
936 return -1;
937
938 return input_buf[input_buf_ptr];
939}
940
941/**
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -0400942 * tep_peek_char - peek at the next character that will be read
Steven Rostedtf7d82352012-04-06 00:47:53 +0200943 *
944 * Returns the next character read, or -1 if end of buffer.
945 */
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -0400946int tep_peek_char(void)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200947{
948 return __peek_char();
949}
950
Namhyung Kimdeba3fb2012-04-09 11:54:30 +0900951static int extend_token(char **tok, char *buf, int size)
952{
953 char *newtok = realloc(*tok, size);
954
955 if (!newtok) {
956 free(*tok);
957 *tok = NULL;
958 return -1;
959 }
960
961 if (!*tok)
962 strcpy(newtok, buf);
963 else
964 strcat(newtok, buf);
965 *tok = newtok;
966
967 return 0;
968}
969
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400970static enum tep_event_type force_token(const char *str, char **tok);
Steven Rostedtf7d82352012-04-06 00:47:53 +0200971
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400972static enum tep_event_type __read_token(char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200973{
974 char buf[BUFSIZ];
975 int ch, last_ch, quote_ch, next_ch;
976 int i = 0;
977 int tok_size = 0;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400978 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200979
980 *tok = NULL;
981
982
983 ch = __read_char();
984 if (ch < 0)
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400985 return TEP_EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +0200986
987 type = get_type(ch);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400988 if (type == TEP_EVENT_NONE)
Steven Rostedtf7d82352012-04-06 00:47:53 +0200989 return type;
990
991 buf[i++] = ch;
992
993 switch (type) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400994 case TEP_EVENT_NEWLINE:
995 case TEP_EVENT_DELIM:
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300996 if (asprintf(tok, "%c", ch) < 0)
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -0400997 return TEP_EVENT_ERROR;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -0300998
Steven Rostedtf7d82352012-04-06 00:47:53 +0200999 return type;
1000
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001001 case TEP_EVENT_OP:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001002 switch (ch) {
1003 case '-':
1004 next_ch = __peek_char();
1005 if (next_ch == '>') {
1006 buf[i++] = __read_char();
1007 break;
1008 }
1009 /* fall through */
1010 case '+':
1011 case '|':
1012 case '&':
1013 case '>':
1014 case '<':
1015 last_ch = ch;
1016 ch = __peek_char();
1017 if (ch != last_ch)
1018 goto test_equal;
1019 buf[i++] = __read_char();
1020 switch (last_ch) {
1021 case '>':
1022 case '<':
1023 goto test_equal;
1024 default:
1025 break;
1026 }
1027 break;
1028 case '!':
1029 case '=':
1030 goto test_equal;
1031 default: /* what should we do instead? */
1032 break;
1033 }
1034 buf[i] = 0;
1035 *tok = strdup(buf);
1036 return type;
1037
1038 test_equal:
1039 ch = __peek_char();
1040 if (ch == '=')
1041 buf[i++] = __read_char();
1042 goto out;
1043
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001044 case TEP_EVENT_DQUOTE:
1045 case TEP_EVENT_SQUOTE:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001046 /* don't keep quotes */
1047 i--;
1048 quote_ch = ch;
1049 last_ch = 0;
1050 concat:
1051 do {
1052 if (i == (BUFSIZ - 1)) {
1053 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001054 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001055
1056 if (extend_token(tok, buf, tok_size) < 0)
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001057 return TEP_EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001058 i = 0;
1059 }
1060 last_ch = ch;
1061 ch = __read_char();
1062 buf[i++] = ch;
1063 /* the '\' '\' will cancel itself */
1064 if (ch == '\\' && last_ch == '\\')
1065 last_ch = 0;
1066 } while (ch != quote_ch || last_ch == '\\');
1067 /* remove the last quote */
1068 i--;
1069
1070 /*
1071 * For strings (double quotes) check the next token.
1072 * If it is another string, concatinate the two.
1073 */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001074 if (type == TEP_EVENT_DQUOTE) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001075 unsigned long long save_input_buf_ptr = input_buf_ptr;
1076
1077 do {
1078 ch = __read_char();
1079 } while (isspace(ch));
1080 if (ch == '"')
1081 goto concat;
1082 input_buf_ptr = save_input_buf_ptr;
1083 }
1084
1085 goto out;
1086
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001087 case TEP_EVENT_ERROR ... TEP_EVENT_SPACE:
1088 case TEP_EVENT_ITEM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02001089 default:
1090 break;
1091 }
1092
1093 while (get_type(__peek_char()) == type) {
1094 if (i == (BUFSIZ - 1)) {
1095 buf[i] = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001096 tok_size += BUFSIZ;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001097
1098 if (extend_token(tok, buf, tok_size) < 0)
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001099 return TEP_EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001100 i = 0;
1101 }
1102 ch = __read_char();
1103 buf[i++] = ch;
1104 }
1105
1106 out:
1107 buf[i] = 0;
Namhyung Kimdeba3fb2012-04-09 11:54:30 +09001108 if (extend_token(tok, buf, tok_size + i + 1) < 0)
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001109 return TEP_EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001110
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001111 if (type == TEP_EVENT_ITEM) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001112 /*
1113 * Older versions of the kernel has a bug that
1114 * creates invalid symbols and will break the mac80211
1115 * parsing. This is a work around to that bug.
1116 *
1117 * See Linux kernel commit:
1118 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1119 */
1120 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1121 free(*tok);
1122 *tok = NULL;
Michael Sartain952a99c2018-01-11 19:47:42 -05001123 return force_token("\"%s\" ", tok);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001124 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1125 free(*tok);
1126 *tok = NULL;
1127 return force_token("\" sta:%pM\" ", tok);
1128 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1129 free(*tok);
1130 *tok = NULL;
1131 return force_token("\" vif:%p(%d)\" ", tok);
1132 }
1133 }
1134
1135 return type;
1136}
1137
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001138static enum tep_event_type force_token(const char *str, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001139{
1140 const char *save_input_buf;
1141 unsigned long long save_input_buf_ptr;
1142 unsigned long long save_input_buf_siz;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001143 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001144
1145 /* save off the current input pointers */
1146 save_input_buf = input_buf;
1147 save_input_buf_ptr = input_buf_ptr;
1148 save_input_buf_siz = input_buf_siz;
1149
1150 init_input_buf(str, strlen(str));
1151
1152 type = __read_token(tok);
1153
1154 /* reset back to original token */
1155 input_buf = save_input_buf;
1156 input_buf_ptr = save_input_buf_ptr;
1157 input_buf_siz = save_input_buf_siz;
1158
1159 return type;
1160}
1161
1162static void free_token(char *tok)
1163{
1164 if (tok)
1165 free(tok);
1166}
1167
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001168static enum tep_event_type read_token(char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001169{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001170 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001171
1172 for (;;) {
1173 type = __read_token(tok);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001174 if (type != TEP_EVENT_SPACE)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001175 return type;
1176
1177 free_token(*tok);
1178 }
1179
1180 /* not reached */
1181 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001182 return TEP_EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001183}
1184
1185/**
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04001186 * tep_read_token - access to utilities to use the tep parser
Steven Rostedtf7d82352012-04-06 00:47:53 +02001187 * @tok: The token to return
1188 *
1189 * This will parse tokens from the string given by
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04001190 * tep_init_data().
Steven Rostedtf7d82352012-04-06 00:47:53 +02001191 *
1192 * Returns the token type.
1193 */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001194enum tep_event_type tep_read_token(char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001195{
1196 return read_token(tok);
1197}
1198
1199/**
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -04001200 * tep_free_token - free a token returned by tep_read_token
Steven Rostedtf7d82352012-04-06 00:47:53 +02001201 * @token: the token to free
1202 */
Tzvetomir Stoyanov (VMware)1634e462018-08-08 14:03:07 -04001203void tep_free_token(char *token)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001204{
1205 free_token(token);
1206}
1207
1208/* no newline */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001209static enum tep_event_type read_token_item(char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001210{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001211 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001212
1213 for (;;) {
1214 type = __read_token(tok);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001215 if (type != TEP_EVENT_SPACE && type != TEP_EVENT_NEWLINE)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001216 return type;
1217 free_token(*tok);
1218 *tok = NULL;
1219 }
1220
1221 /* not reached */
1222 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001223 return TEP_EVENT_NONE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001224}
1225
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001226static int test_type(enum tep_event_type type, enum tep_event_type expect)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001227{
1228 if (type != expect) {
1229 do_warning("Error: expected type %d but read %d",
1230 expect, type);
1231 return -1;
1232 }
1233 return 0;
1234}
1235
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001236static int test_type_token(enum tep_event_type type, const char *token,
1237 enum tep_event_type expect, const char *expect_tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001238{
1239 if (type != expect) {
1240 do_warning("Error: expected type %d but read %d",
1241 expect, type);
1242 return -1;
1243 }
1244
1245 if (strcmp(token, expect_tok) != 0) {
1246 do_warning("Error: expected '%s' but read '%s'",
1247 expect_tok, token);
1248 return -1;
1249 }
1250 return 0;
1251}
1252
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001253static int __read_expect_type(enum tep_event_type expect, char **tok, int newline_ok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001254{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001255 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001256
1257 if (newline_ok)
1258 type = read_token(tok);
1259 else
1260 type = read_token_item(tok);
1261 return test_type(type, expect);
1262}
1263
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001264static int read_expect_type(enum tep_event_type expect, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001265{
1266 return __read_expect_type(expect, tok, 1);
1267}
1268
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001269static int __read_expected(enum tep_event_type expect, const char *str,
Steven Rostedtf7d82352012-04-06 00:47:53 +02001270 int newline_ok)
1271{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001272 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001273 char *token;
1274 int ret;
1275
1276 if (newline_ok)
1277 type = read_token(&token);
1278 else
1279 type = read_token_item(&token);
1280
1281 ret = test_type_token(type, token, expect, str);
1282
1283 free_token(token);
1284
1285 return ret;
1286}
1287
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001288static int read_expected(enum tep_event_type expect, const char *str)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001289{
1290 return __read_expected(expect, str, 1);
1291}
1292
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001293static int read_expected_item(enum tep_event_type expect, const char *str)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001294{
1295 return __read_expected(expect, str, 0);
1296}
1297
1298static char *event_read_name(void)
1299{
1300 char *token;
1301
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001302 if (read_expected(TEP_EVENT_ITEM, "name") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001303 return NULL;
1304
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001305 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001306 return NULL;
1307
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001308 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001309 goto fail;
1310
1311 return token;
1312
1313 fail:
1314 free_token(token);
1315 return NULL;
1316}
1317
1318static int event_read_id(void)
1319{
1320 char *token;
1321 int id;
1322
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001323 if (read_expected_item(TEP_EVENT_ITEM, "ID") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001324 return -1;
1325
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001326 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001327 return -1;
1328
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001329 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001330 goto fail;
1331
1332 id = strtoul(token, NULL, 0);
1333 free_token(token);
1334 return id;
1335
1336 fail:
1337 free_token(token);
1338 return -1;
1339}
1340
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001341static int field_is_string(struct tep_format_field *field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001342{
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001343 if ((field->flags & TEP_FIELD_IS_ARRAY) &&
Steven Rostedtf7d82352012-04-06 00:47:53 +02001344 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1345 strstr(field->type, "s8")))
1346 return 1;
1347
1348 return 0;
1349}
1350
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001351static int field_is_dynamic(struct tep_format_field *field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001352{
1353 if (strncmp(field->type, "__data_loc", 10) == 0)
1354 return 1;
1355
1356 return 0;
1357}
1358
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001359static int field_is_long(struct tep_format_field *field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001360{
1361 /* includes long long */
1362 if (strstr(field->type, "long"))
1363 return 1;
1364
1365 return 0;
1366}
1367
Jiri Olsae23c1a52013-01-24 21:46:43 +01001368static unsigned int type_size(const char *name)
1369{
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001370 /* This covers all TEP_FIELD_IS_STRING types. */
Jiri Olsae23c1a52013-01-24 21:46:43 +01001371 static struct {
1372 const char *type;
1373 unsigned int size;
1374 } table[] = {
1375 { "u8", 1 },
1376 { "u16", 2 },
1377 { "u32", 4 },
1378 { "u64", 8 },
1379 { "s8", 1 },
1380 { "s16", 2 },
1381 { "s32", 4 },
1382 { "s64", 8 },
1383 { "char", 1 },
1384 { },
1385 };
1386 int i;
1387
1388 for (i = 0; table[i].type; i++) {
1389 if (!strcmp(table[i].type, name))
1390 return table[i].size;
1391 }
1392
1393 return 0;
1394}
1395
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001396static int event_read_fields(struct tep_event *event, struct tep_format_field **fields)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001397{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04001398 struct tep_format_field *field = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001399 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001400 char *token;
1401 char *last_token;
1402 int count = 0;
1403
1404 do {
Jiri Olsae23c1a52013-01-24 21:46:43 +01001405 unsigned int size_dynamic = 0;
1406
Steven Rostedtf7d82352012-04-06 00:47:53 +02001407 type = read_token(&token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001408 if (type == TEP_EVENT_NEWLINE) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001409 free_token(token);
1410 return count;
1411 }
1412
1413 count++;
1414
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001415 if (test_type_token(type, token, TEP_EVENT_ITEM, "field"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001416 goto fail;
1417 free_token(token);
1418
1419 type = read_token(&token);
1420 /*
1421 * The ftrace fields may still use the "special" name.
1422 * Just ignore it.
1423 */
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04001424 if (event->flags & TEP_EVENT_FL_ISFTRACE &&
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001425 type == TEP_EVENT_ITEM && strcmp(token, "special") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001426 free_token(token);
1427 type = read_token(&token);
1428 }
1429
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001430 if (test_type_token(type, token, TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001431 goto fail;
1432
1433 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001434 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001435 goto fail;
1436
1437 last_token = token;
1438
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03001439 field = calloc(1, sizeof(*field));
1440 if (!field)
1441 goto fail;
1442
Steven Rostedtf7d82352012-04-06 00:47:53 +02001443 field->event = event;
1444
1445 /* read the rest of the type */
1446 for (;;) {
1447 type = read_token(&token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001448 if (type == TEP_EVENT_ITEM ||
1449 (type == TEP_EVENT_OP && strcmp(token, "*") == 0) ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001450 /*
1451 * Some of the ftrace fields are broken and have
1452 * an illegal "." in them.
1453 */
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04001454 (event->flags & TEP_EVENT_FL_ISFTRACE &&
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001455 type == TEP_EVENT_OP && strcmp(token, ".") == 0)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001456
1457 if (strcmp(token, "*") == 0)
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001458 field->flags |= TEP_FIELD_IS_POINTER;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001459
1460 if (field->type) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001461 char *new_type;
1462 new_type = realloc(field->type,
1463 strlen(field->type) +
1464 strlen(last_token) + 2);
1465 if (!new_type) {
1466 free(last_token);
1467 goto fail;
1468 }
1469 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001470 strcat(field->type, " ");
1471 strcat(field->type, last_token);
1472 free(last_token);
1473 } else
1474 field->type = last_token;
1475 last_token = token;
1476 continue;
1477 }
1478
1479 break;
1480 }
1481
1482 if (!field->type) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001483 do_warning_event(event, "%s: no type found", __func__);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001484 goto fail;
1485 }
Jiri Olsad3542432015-04-18 17:50:18 +02001486 field->name = field->alias = last_token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001487
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001488 if (test_type(type, TEP_EVENT_OP))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001489 goto fail;
1490
1491 if (strcmp(token, "[") == 0) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001492 enum tep_event_type last_type = type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001493 char *brackets = token;
Namhyung Kimd2864472012-04-09 11:54:33 +09001494 char *new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001495 int len;
1496
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001497 field->flags |= TEP_FIELD_IS_ARRAY;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001498
1499 type = read_token(&token);
1500
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001501 if (type == TEP_EVENT_ITEM)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001502 field->arraylen = strtoul(token, NULL, 0);
1503 else
1504 field->arraylen = 0;
1505
1506 while (strcmp(token, "]") != 0) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001507 if (last_type == TEP_EVENT_ITEM &&
1508 type == TEP_EVENT_ITEM)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001509 len = 2;
1510 else
1511 len = 1;
1512 last_type = type;
1513
Namhyung Kimd2864472012-04-09 11:54:33 +09001514 new_brackets = realloc(brackets,
1515 strlen(brackets) +
1516 strlen(token) + len);
1517 if (!new_brackets) {
1518 free(brackets);
1519 goto fail;
1520 }
1521 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001522 if (len == 2)
1523 strcat(brackets, " ");
1524 strcat(brackets, token);
1525 /* We only care about the last token */
1526 field->arraylen = strtoul(token, NULL, 0);
1527 free_token(token);
1528 type = read_token(&token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001529 if (type == TEP_EVENT_NONE) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001530 do_warning_event(event, "failed to find token");
Steven Rostedtf7d82352012-04-06 00:47:53 +02001531 goto fail;
1532 }
1533 }
1534
1535 free_token(token);
1536
Namhyung Kimd2864472012-04-09 11:54:33 +09001537 new_brackets = realloc(brackets, strlen(brackets) + 2);
1538 if (!new_brackets) {
1539 free(brackets);
1540 goto fail;
1541 }
1542 brackets = new_brackets;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001543 strcat(brackets, "]");
1544
1545 /* add brackets to type */
1546
1547 type = read_token(&token);
1548 /*
1549 * If the next token is not an OP, then it is of
1550 * the format: type [] item;
1551 */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001552 if (type == TEP_EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09001553 char *new_type;
1554 new_type = realloc(field->type,
1555 strlen(field->type) +
1556 strlen(field->name) +
1557 strlen(brackets) + 2);
1558 if (!new_type) {
1559 free(brackets);
1560 goto fail;
1561 }
1562 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001563 strcat(field->type, " ");
1564 strcat(field->type, field->name);
Jiri Olsae23c1a52013-01-24 21:46:43 +01001565 size_dynamic = type_size(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001566 free_token(field->name);
1567 strcat(field->type, brackets);
Jiri Olsad3542432015-04-18 17:50:18 +02001568 field->name = field->alias = token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001569 type = read_token(&token);
1570 } else {
Namhyung Kimd2864472012-04-09 11:54:33 +09001571 char *new_type;
1572 new_type = realloc(field->type,
1573 strlen(field->type) +
1574 strlen(brackets) + 1);
1575 if (!new_type) {
1576 free(brackets);
1577 goto fail;
1578 }
1579 field->type = new_type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001580 strcat(field->type, brackets);
1581 }
1582 free(brackets);
1583 }
1584
1585 if (field_is_string(field))
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001586 field->flags |= TEP_FIELD_IS_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001587 if (field_is_dynamic(field))
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001588 field->flags |= TEP_FIELD_IS_DYNAMIC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001589 if (field_is_long(field))
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001590 field->flags |= TEP_FIELD_IS_LONG;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001591
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001592 if (test_type_token(type, token, TEP_EVENT_OP, ";"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001593 goto fail;
1594 free_token(token);
1595
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001596 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001597 goto fail_expect;
1598
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001599 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001600 goto fail_expect;
1601
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001602 if (read_expect_type(TEP_EVENT_ITEM, &token))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001603 goto fail;
1604 field->offset = strtoul(token, NULL, 0);
1605 free_token(token);
1606
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001607 if (read_expected(TEP_EVENT_OP, ";") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001608 goto fail_expect;
1609
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001610 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001611 goto fail_expect;
1612
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001613 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001614 goto fail_expect;
1615
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001616 if (read_expect_type(TEP_EVENT_ITEM, &token))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001617 goto fail;
1618 field->size = strtoul(token, NULL, 0);
1619 free_token(token);
1620
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001621 if (read_expected(TEP_EVENT_OP, ";") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001622 goto fail_expect;
1623
1624 type = read_token(&token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001625 if (type != TEP_EVENT_NEWLINE) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001626 /* newer versions of the kernel have a "signed" type */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001627 if (test_type_token(type, token, TEP_EVENT_ITEM, "signed"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001628 goto fail;
1629
1630 free_token(token);
1631
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001632 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001633 goto fail_expect;
1634
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001635 if (read_expect_type(TEP_EVENT_ITEM, &token))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001636 goto fail;
1637
Tom Zanussi10ee9fa2013-01-18 13:51:25 -06001638 if (strtoul(token, NULL, 0))
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001639 field->flags |= TEP_FIELD_IS_SIGNED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001640
1641 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001642 if (read_expected(TEP_EVENT_OP, ";") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001643 goto fail_expect;
1644
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001645 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001646 goto fail;
1647 }
1648
1649 free_token(token);
1650
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001651 if (field->flags & TEP_FIELD_IS_ARRAY) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001652 if (field->arraylen)
1653 field->elementsize = field->size / field->arraylen;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001654 else if (field->flags & TEP_FIELD_IS_DYNAMIC)
Jiri Olsae23c1a52013-01-24 21:46:43 +01001655 field->elementsize = size_dynamic;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001656 else if (field->flags & TEP_FIELD_IS_STRING)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001657 field->elementsize = 1;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04001658 else if (field->flags & TEP_FIELD_IS_LONG)
Jiri Olsae23c1a52013-01-24 21:46:43 +01001659 field->elementsize = event->pevent ?
1660 event->pevent->long_size :
1661 sizeof(long);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001662 } else
1663 field->elementsize = field->size;
1664
1665 *fields = field;
1666 fields = &field->next;
1667
1668 } while (1);
1669
1670 return 0;
1671
1672fail:
1673 free_token(token);
1674fail_expect:
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001675 if (field) {
1676 free(field->type);
1677 free(field->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001678 free(field);
Namhyung Kim57d34dc2012-05-23 11:36:47 +09001679 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001680 return -1;
1681}
1682
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001683static int event_read_format(struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001684{
1685 char *token;
1686 int ret;
1687
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001688 if (read_expected_item(TEP_EVENT_ITEM, "format") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001689 return -1;
1690
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001691 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001692 return -1;
1693
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001694 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001695 goto fail;
1696 free_token(token);
1697
1698 ret = event_read_fields(event, &event->format.common_fields);
1699 if (ret < 0)
1700 return ret;
1701 event->format.nr_common = ret;
1702
1703 ret = event_read_fields(event, &event->format.fields);
1704 if (ret < 0)
1705 return ret;
1706 event->format.nr_fields = ret;
1707
1708 return 0;
1709
1710 fail:
1711 free_token(token);
1712 return -1;
1713}
1714
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001715static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001716process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001717 char **tok, enum tep_event_type type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001718
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001719static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001720process_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001721{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001722 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001723 char *token;
1724
1725 type = read_token(&token);
1726 *tok = token;
1727
1728 return process_arg_token(event, arg, tok, type);
1729}
1730
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001731static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001732process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001733
Steven Rostedteff2c922013-11-18 14:23:14 -05001734/*
1735 * For __print_symbolic() and __print_flags, we need to completely
1736 * evaluate the first argument, which defines what to print next.
1737 */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001738static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001739process_field_arg(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Steven Rostedteff2c922013-11-18 14:23:14 -05001740{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001741 enum tep_event_type type;
Steven Rostedteff2c922013-11-18 14:23:14 -05001742
1743 type = process_arg(event, arg, tok);
1744
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001745 while (type == TEP_EVENT_OP) {
Steven Rostedteff2c922013-11-18 14:23:14 -05001746 type = process_op(event, arg, tok);
1747 }
1748
1749 return type;
1750}
1751
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001752static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001753process_cond(struct tep_event *event, struct tep_print_arg *top, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001754{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04001755 struct tep_print_arg *arg, *left, *right;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001756 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001757 char *token = NULL;
1758
1759 arg = alloc_arg();
1760 left = alloc_arg();
1761 right = alloc_arg();
1762
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001763 if (!arg || !left || !right) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001764 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001765 /* arg will be freed at out_free */
1766 free_arg(left);
1767 free_arg(right);
1768 goto out_free;
1769 }
1770
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04001771 arg->type = TEP_PRINT_OP;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001772 arg->op.left = left;
1773 arg->op.right = right;
1774
1775 *tok = NULL;
1776 type = process_arg(event, left, &token);
1777
1778 again:
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001779 if (type == TEP_EVENT_ERROR)
Dean Nelson6f56e9c2015-08-20 11:16:32 -04001780 goto out_free;
1781
Steven Rostedtf7d82352012-04-06 00:47:53 +02001782 /* Handle other operations in the arguments */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001783 if (type == TEP_EVENT_OP && strcmp(token, ":") != 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001784 type = process_op(event, left, &token);
1785 goto again;
1786 }
1787
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001788 if (test_type_token(type, token, TEP_EVENT_OP, ":"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001789 goto out_free;
1790
1791 arg->op.op = token;
1792
1793 type = process_arg(event, right, &token);
1794
1795 top->op.right = arg;
1796
1797 *tok = token;
1798 return type;
1799
1800out_free:
1801 /* Top may point to itself */
1802 top->op.right = NULL;
1803 free_token(token);
1804 free_arg(arg);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001805 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001806}
1807
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001808static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001809process_array(struct tep_event *event, struct tep_print_arg *top, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001810{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04001811 struct tep_print_arg *arg;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001812 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001813 char *token = NULL;
1814
1815 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001816 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001817 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001818 /* '*tok' is set to top->op.op. No need to free. */
1819 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001820 return TEP_EVENT_ERROR;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001821 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02001822
1823 *tok = NULL;
1824 type = process_arg(event, arg, &token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001825 if (test_type_token(type, token, TEP_EVENT_OP, "]"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02001826 goto out_free;
1827
1828 top->op.right = arg;
1829
1830 free_token(token);
1831 type = read_token_item(&token);
1832 *tok = token;
1833
1834 return type;
1835
1836out_free:
Namhyung Kim1bce6e02012-09-19 15:58:41 +09001837 free_token(token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001838 free_arg(arg);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001839 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001840}
1841
1842static int get_op_prio(char *op)
1843{
1844 if (!op[1]) {
1845 switch (op[0]) {
1846 case '~':
1847 case '!':
1848 return 4;
1849 case '*':
1850 case '/':
1851 case '%':
1852 return 6;
1853 case '+':
1854 case '-':
1855 return 7;
1856 /* '>>' and '<<' are 8 */
1857 case '<':
1858 case '>':
1859 return 9;
1860 /* '==' and '!=' are 10 */
1861 case '&':
1862 return 11;
1863 case '^':
1864 return 12;
1865 case '|':
1866 return 13;
1867 case '?':
1868 return 16;
1869 default:
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001870 do_warning("unknown op '%c'", op[0]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001871 return -1;
1872 }
1873 } else {
1874 if (strcmp(op, "++") == 0 ||
1875 strcmp(op, "--") == 0) {
1876 return 3;
1877 } else if (strcmp(op, ">>") == 0 ||
1878 strcmp(op, "<<") == 0) {
1879 return 8;
1880 } else if (strcmp(op, ">=") == 0 ||
1881 strcmp(op, "<=") == 0) {
1882 return 9;
1883 } else if (strcmp(op, "==") == 0 ||
1884 strcmp(op, "!=") == 0) {
1885 return 10;
1886 } else if (strcmp(op, "&&") == 0) {
1887 return 14;
1888 } else if (strcmp(op, "||") == 0) {
1889 return 15;
1890 } else {
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001891 do_warning("unknown op '%s'", op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001892 return -1;
1893 }
1894 }
1895}
1896
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04001897static int set_op_prio(struct tep_print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001898{
1899
1900 /* single ops are the greatest */
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04001901 if (!arg->op.left || arg->op.left->type == TEP_PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001902 arg->op.prio = 0;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001903 else
1904 arg->op.prio = get_op_prio(arg->op.op);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001905
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02001906 return arg->op.prio;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001907}
1908
1909/* Note, *tok does not get freed, but will most likely be saved */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001910static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05001911process_op(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02001912{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04001913 struct tep_print_arg *left, *right = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04001914 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001915 char *token;
1916
1917 /* the op is passed in via tok */
1918 token = *tok;
1919
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04001920 if (arg->type == TEP_PRINT_OP && !arg->op.left) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02001921 /* handle single op */
1922 if (token[1]) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09001923 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001924 goto out_free;
1925 }
1926 switch (token[0]) {
1927 case '~':
1928 case '!':
1929 case '+':
1930 case '-':
1931 break;
1932 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09001933 do_warning_event(event, "bad op token %s", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02001934 goto out_free;
1935
1936 }
1937
1938 /* make an empty left */
1939 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001940 if (!left)
1941 goto out_warn_free;
1942
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04001943 left->type = TEP_PRINT_NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001944 arg->op.left = left;
1945
1946 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001947 if (!right)
1948 goto out_warn_free;
1949
Steven Rostedtf7d82352012-04-06 00:47:53 +02001950 arg->op.right = right;
1951
1952 /* do not free the token, it belongs to an op */
1953 *tok = NULL;
1954 type = process_arg(event, right, tok);
1955
1956 } else if (strcmp(token, "?") == 0) {
1957
1958 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001959 if (!left)
1960 goto out_warn_free;
1961
Steven Rostedtf7d82352012-04-06 00:47:53 +02001962 /* copy the top arg to the left */
1963 *left = *arg;
1964
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04001965 arg->type = TEP_PRINT_OP;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001966 arg->op.op = token;
1967 arg->op.left = left;
1968 arg->op.prio = 0;
1969
Namhyung Kim41e51a22012-09-19 15:58:42 +09001970 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02001971 type = process_cond(event, arg, tok);
1972
1973 } else if (strcmp(token, ">>") == 0 ||
1974 strcmp(token, "<<") == 0 ||
1975 strcmp(token, "&") == 0 ||
1976 strcmp(token, "|") == 0 ||
1977 strcmp(token, "&&") == 0 ||
1978 strcmp(token, "||") == 0 ||
1979 strcmp(token, "-") == 0 ||
1980 strcmp(token, "+") == 0 ||
1981 strcmp(token, "*") == 0 ||
1982 strcmp(token, "^") == 0 ||
1983 strcmp(token, "/") == 0 ||
Daniel Bristot de Oliveira0e47b382016-02-22 14:08:22 -03001984 strcmp(token, "%") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001985 strcmp(token, "<") == 0 ||
1986 strcmp(token, ">") == 0 ||
Namhyung Kimff582682013-01-15 17:02:19 +09001987 strcmp(token, "<=") == 0 ||
1988 strcmp(token, ">=") == 0 ||
Steven Rostedtf7d82352012-04-06 00:47:53 +02001989 strcmp(token, "==") == 0 ||
1990 strcmp(token, "!=") == 0) {
1991
1992 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09001993 if (!left)
1994 goto out_warn_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02001995
1996 /* copy the top arg to the left */
1997 *left = *arg;
1998
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04001999 arg->type = TEP_PRINT_OP;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002000 arg->op.op = token;
2001 arg->op.left = left;
Namhyung Kim41e51a22012-09-19 15:58:42 +09002002 arg->op.right = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002003
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02002004 if (set_op_prio(arg) == -1) {
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04002005 event->flags |= TEP_EVENT_FL_FAILED;
Namhyung Kimd1de1082012-05-23 11:36:49 +09002006 /* arg->op.op (= token) will be freed at out_free */
2007 arg->op.op = NULL;
Vaibhav Nagarnaik14ffde02012-04-06 00:48:01 +02002008 goto out_free;
2009 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002010
2011 type = read_token_item(&token);
2012 *tok = token;
2013
2014 /* could just be a type pointer */
2015 if ((strcmp(arg->op.op, "*") == 0) &&
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002016 type == TEP_EVENT_DELIM && (strcmp(token, ")") == 0)) {
Namhyung Kimd2864472012-04-09 11:54:33 +09002017 char *new_atom;
2018
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002019 if (left->type != TEP_PRINT_ATOM) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002020 do_warning_event(event, "bad pointer type");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002021 goto out_free;
2022 }
Namhyung Kimd2864472012-04-09 11:54:33 +09002023 new_atom = realloc(left->atom.atom,
Steven Rostedtf7d82352012-04-06 00:47:53 +02002024 strlen(left->atom.atom) + 3);
Namhyung Kimd2864472012-04-09 11:54:33 +09002025 if (!new_atom)
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002026 goto out_warn_free;
Namhyung Kimd2864472012-04-09 11:54:33 +09002027
2028 left->atom.atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002029 strcat(left->atom.atom, " *");
2030 free(arg->op.op);
2031 *arg = *left;
2032 free(left);
2033
2034 return type;
2035 }
2036
2037 right = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002038 if (!right)
2039 goto out_warn_free;
2040
Steven Rostedtf7d82352012-04-06 00:47:53 +02002041 type = process_arg_token(event, right, tok, type);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002042 if (type == TEP_EVENT_ERROR) {
Dean Nelson6f56e9c2015-08-20 11:16:32 -04002043 free_arg(right);
2044 /* token was freed in process_arg_token() via *tok */
2045 token = NULL;
2046 goto out_free;
2047 }
Namhyung Kim3201f0d2015-04-06 14:36:16 +09002048
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002049 if (right->type == TEP_PRINT_OP &&
Namhyung Kim3201f0d2015-04-06 14:36:16 +09002050 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002051 struct tep_print_arg tmp;
Namhyung Kim3201f0d2015-04-06 14:36:16 +09002052
2053 /* rotate ops according to the priority */
2054 arg->op.right = right->op.left;
2055
2056 tmp = *arg;
2057 *arg = *right;
2058 *right = tmp;
2059
2060 arg->op.left = right;
2061 } else {
2062 arg->op.right = right;
2063 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002064
2065 } else if (strcmp(token, "[") == 0) {
2066
2067 left = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002068 if (!left)
2069 goto out_warn_free;
2070
Steven Rostedtf7d82352012-04-06 00:47:53 +02002071 *left = *arg;
2072
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002073 arg->type = TEP_PRINT_OP;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002074 arg->op.op = token;
2075 arg->op.left = left;
2076
2077 arg->op.prio = 0;
2078
Namhyung Kim41e51a22012-09-19 15:58:42 +09002079 /* it will set arg->op.right */
Steven Rostedtf7d82352012-04-06 00:47:53 +02002080 type = process_array(event, arg, tok);
2081
2082 } else {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002083 do_warning_event(event, "unknown op '%s'", token);
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04002084 event->flags |= TEP_EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002085 /* the arg is now the left side */
2086 goto out_free;
2087 }
2088
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002089 if (type == TEP_EVENT_OP && strcmp(*tok, ":") != 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02002090 int prio;
2091
2092 /* higher prios need to be closer to the root */
2093 prio = get_op_prio(*tok);
2094
2095 if (prio > arg->op.prio)
2096 return process_op(event, arg, tok);
2097
2098 return process_op(event, right, tok);
2099 }
2100
2101 return type;
2102
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002103out_warn_free:
Namhyung Kim3388cc32014-03-19 10:22:53 +09002104 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002105out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002106 free_token(token);
2107 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002108 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002109}
2110
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002111static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002112process_entry(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02002113 char **tok)
2114{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002115 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002116 char *field;
2117 char *token;
2118
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002119 if (read_expected(TEP_EVENT_OP, "->") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002120 goto out_err;
2121
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002122 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002123 goto out_free;
2124 field = token;
2125
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002126 arg->type = TEP_PRINT_FIELD;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002127 arg->field.name = field;
2128
Tom Zanussi5205aec2012-04-06 00:47:58 +02002129 if (is_flag_field) {
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04002130 arg->field.field = tep_find_any_field(event, arg->field.name);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04002131 arg->field.field->flags |= TEP_FIELD_IS_FLAG;
Tom Zanussi5205aec2012-04-06 00:47:58 +02002132 is_flag_field = 0;
2133 } else if (is_symbolic_field) {
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04002134 arg->field.field = tep_find_any_field(event, arg->field.name);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04002135 arg->field.field->flags |= TEP_FIELD_IS_SYMBOLIC;
Tom Zanussi5205aec2012-04-06 00:47:58 +02002136 is_symbolic_field = 0;
2137 }
2138
Steven Rostedtf7d82352012-04-06 00:47:53 +02002139 type = read_token(&token);
2140 *tok = token;
2141
2142 return type;
2143
2144 out_free:
2145 free_token(token);
2146 out_err:
2147 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002148 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002149}
2150
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002151static int alloc_and_process_delim(struct tep_event *event, char *next_token,
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002152 struct tep_print_arg **print_arg)
Javi Merino929a6bb2015-03-20 18:12:55 +00002153{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002154 struct tep_print_arg *field;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002155 enum tep_event_type type;
Javi Merino929a6bb2015-03-20 18:12:55 +00002156 char *token;
2157 int ret = 0;
2158
2159 field = alloc_arg();
2160 if (!field) {
2161 do_warning_event(event, "%s: not enough memory!", __func__);
2162 errno = ENOMEM;
2163 return -1;
2164 }
2165
2166 type = process_arg(event, field, &token);
2167
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002168 if (test_type_token(type, token, TEP_EVENT_DELIM, next_token)) {
Javi Merino929a6bb2015-03-20 18:12:55 +00002169 errno = EINVAL;
2170 ret = -1;
2171 free_arg(field);
2172 goto out_free_token;
2173 }
2174
2175 *print_arg = field;
2176
2177out_free_token:
2178 free_token(token);
2179
2180 return ret;
2181}
2182
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002183static char *arg_eval (struct tep_print_arg *arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002184
2185static unsigned long long
2186eval_type_str(unsigned long long val, const char *type, int pointer)
2187{
2188 int sign = 0;
2189 char *ref;
2190 int len;
2191
2192 len = strlen(type);
2193
2194 if (pointer) {
2195
2196 if (type[len-1] != '*') {
2197 do_warning("pointer expected with non pointer type");
2198 return val;
2199 }
2200
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002201 ref = malloc(len);
2202 if (!ref) {
2203 do_warning("%s: not enough memory!", __func__);
2204 return val;
2205 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002206 memcpy(ref, type, len);
2207
2208 /* chop off the " *" */
2209 ref[len - 2] = 0;
2210
2211 val = eval_type_str(val, ref, 0);
2212 free(ref);
2213 return val;
2214 }
2215
2216 /* check if this is a pointer */
2217 if (type[len - 1] == '*')
2218 return val;
2219
2220 /* Try to figure out the arg size*/
2221 if (strncmp(type, "struct", 6) == 0)
2222 /* all bets off */
2223 return val;
2224
2225 if (strcmp(type, "u8") == 0)
2226 return val & 0xff;
2227
2228 if (strcmp(type, "u16") == 0)
2229 return val & 0xffff;
2230
2231 if (strcmp(type, "u32") == 0)
2232 return val & 0xffffffff;
2233
2234 if (strcmp(type, "u64") == 0 ||
2235 strcmp(type, "s64"))
2236 return val;
2237
2238 if (strcmp(type, "s8") == 0)
2239 return (unsigned long long)(char)val & 0xff;
2240
2241 if (strcmp(type, "s16") == 0)
2242 return (unsigned long long)(short)val & 0xffff;
2243
2244 if (strcmp(type, "s32") == 0)
2245 return (unsigned long long)(int)val & 0xffffffff;
2246
2247 if (strncmp(type, "unsigned ", 9) == 0) {
2248 sign = 0;
2249 type += 9;
2250 }
2251
2252 if (strcmp(type, "char") == 0) {
2253 if (sign)
2254 return (unsigned long long)(char)val & 0xff;
2255 else
2256 return val & 0xff;
2257 }
2258
2259 if (strcmp(type, "short") == 0) {
2260 if (sign)
2261 return (unsigned long long)(short)val & 0xffff;
2262 else
2263 return val & 0xffff;
2264 }
2265
2266 if (strcmp(type, "int") == 0) {
2267 if (sign)
2268 return (unsigned long long)(int)val & 0xffffffff;
2269 else
2270 return val & 0xffffffff;
2271 }
2272
2273 return val;
2274}
2275
2276/*
2277 * Try to figure out the type.
2278 */
2279static unsigned long long
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002280eval_type(unsigned long long val, struct tep_print_arg *arg, int pointer)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002281{
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002282 if (arg->type != TEP_PRINT_TYPE) {
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002283 do_warning("expected type argument");
2284 return 0;
2285 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002286
2287 return eval_type_str(val, arg->typecast.type, pointer);
2288}
2289
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002290static int arg_num_eval(struct tep_print_arg *arg, long long *val)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002291{
2292 long long left, right;
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002293 int ret = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002294
2295 switch (arg->type) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002296 case TEP_PRINT_ATOM:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002297 *val = strtoll(arg->atom.atom, NULL, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002298 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002299 case TEP_PRINT_TYPE:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002300 ret = arg_num_eval(arg->typecast.item, val);
2301 if (!ret)
2302 break;
2303 *val = eval_type(*val, arg, 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002304 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002305 case TEP_PRINT_OP:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002306 switch (arg->op.op[0]) {
2307 case '|':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002308 ret = arg_num_eval(arg->op.left, &left);
2309 if (!ret)
2310 break;
2311 ret = arg_num_eval(arg->op.right, &right);
2312 if (!ret)
2313 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002314 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002315 *val = left || right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002316 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002317 *val = left | right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002318 break;
2319 case '&':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002320 ret = arg_num_eval(arg->op.left, &left);
2321 if (!ret)
2322 break;
2323 ret = arg_num_eval(arg->op.right, &right);
2324 if (!ret)
2325 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002326 if (arg->op.op[1])
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002327 *val = left && right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002328 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002329 *val = left & right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002330 break;
2331 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002332 ret = arg_num_eval(arg->op.left, &left);
2333 if (!ret)
2334 break;
2335 ret = arg_num_eval(arg->op.right, &right);
2336 if (!ret)
2337 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002338 switch (arg->op.op[1]) {
2339 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002340 *val = left < right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002341 break;
2342 case '<':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002343 *val = left << right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002344 break;
2345 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002346 *val = left <= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002347 break;
2348 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002349 do_warning("unknown op '%s'", arg->op.op);
2350 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002351 }
2352 break;
2353 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002354 ret = arg_num_eval(arg->op.left, &left);
2355 if (!ret)
2356 break;
2357 ret = arg_num_eval(arg->op.right, &right);
2358 if (!ret)
2359 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002360 switch (arg->op.op[1]) {
2361 case 0:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002362 *val = left > right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002363 break;
2364 case '>':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002365 *val = left >> right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002366 break;
2367 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002368 *val = left >= right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002369 break;
2370 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002371 do_warning("unknown op '%s'", arg->op.op);
2372 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002373 }
2374 break;
2375 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002376 ret = arg_num_eval(arg->op.left, &left);
2377 if (!ret)
2378 break;
2379 ret = arg_num_eval(arg->op.right, &right);
2380 if (!ret)
2381 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002382
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002383 if (arg->op.op[1] != '=') {
2384 do_warning("unknown op '%s'", arg->op.op);
2385 ret = 0;
2386 } else
2387 *val = left == right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002388 break;
2389 case '!':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002390 ret = arg_num_eval(arg->op.left, &left);
2391 if (!ret)
2392 break;
2393 ret = arg_num_eval(arg->op.right, &right);
2394 if (!ret)
2395 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002396
2397 switch (arg->op.op[1]) {
2398 case '=':
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002399 *val = left != right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002400 break;
2401 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002402 do_warning("unknown op '%s'", arg->op.op);
2403 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002404 }
2405 break;
2406 case '-':
2407 /* check for negative */
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002408 if (arg->op.left->type == TEP_PRINT_NULL)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002409 left = 0;
2410 else
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002411 ret = arg_num_eval(arg->op.left, &left);
2412 if (!ret)
2413 break;
2414 ret = arg_num_eval(arg->op.right, &right);
2415 if (!ret)
2416 break;
2417 *val = left - right;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002418 break;
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002419 case '+':
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002420 if (arg->op.left->type == TEP_PRINT_NULL)
Vaibhav Nagarnaikb4828592012-04-06 00:48:03 +02002421 left = 0;
2422 else
2423 ret = arg_num_eval(arg->op.left, &left);
2424 if (!ret)
2425 break;
2426 ret = arg_num_eval(arg->op.right, &right);
2427 if (!ret)
2428 break;
2429 *val = left + right;
2430 break;
Steven Rostedt9eb42de2016-02-26 18:13:28 -05002431 case '~':
2432 ret = arg_num_eval(arg->op.right, &right);
2433 if (!ret)
2434 break;
2435 *val = ~right;
2436 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002437 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002438 do_warning("unknown op '%s'", arg->op.op);
2439 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002440 }
2441 break;
2442
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002443 case TEP_PRINT_NULL:
2444 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2445 case TEP_PRINT_STRING:
2446 case TEP_PRINT_BSTRING:
2447 case TEP_PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002448 default:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002449 do_warning("invalid eval type %d", arg->type);
2450 ret = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002451
2452 }
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002453 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002454}
2455
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002456static char *arg_eval (struct tep_print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002457{
2458 long long val;
Tony Jones7c5b0192019-02-27 17:55:32 -08002459 static char buf[24];
Steven Rostedtf7d82352012-04-06 00:47:53 +02002460
2461 switch (arg->type) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002462 case TEP_PRINT_ATOM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002463 return arg->atom.atom;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002464 case TEP_PRINT_TYPE:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002465 return arg_eval(arg->typecast.item);
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002466 case TEP_PRINT_OP:
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002467 if (!arg_num_eval(arg, &val))
2468 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002469 sprintf(buf, "%lld", val);
2470 return buf;
2471
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002472 case TEP_PRINT_NULL:
2473 case TEP_PRINT_FIELD ... TEP_PRINT_SYMBOL:
2474 case TEP_PRINT_STRING:
2475 case TEP_PRINT_BSTRING:
2476 case TEP_PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002477 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002478 do_warning("invalid eval type %d", arg->type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002479 break;
2480 }
2481
2482 return NULL;
2483}
2484
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002485static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002486process_fields(struct tep_event *event, struct tep_print_flag_sym **list, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002487{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002488 enum tep_event_type type;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002489 struct tep_print_arg *arg = NULL;
2490 struct tep_print_flag_sym *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002491 char *token = *tok;
2492 char *value;
2493
2494 do {
2495 free_token(token);
2496 type = read_token_item(&token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002497 if (test_type_token(type, token, TEP_EVENT_OP, "{"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02002498 break;
2499
2500 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002501 if (!arg)
2502 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002503
2504 free_token(token);
2505 type = process_arg(event, arg, &token);
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002506
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002507 if (type == TEP_EVENT_OP)
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002508 type = process_op(event, arg, &token);
2509
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002510 if (type == TEP_EVENT_ERROR)
Stefan Hajnoczi00b9da72012-05-23 11:36:42 +09002511 goto out_free;
2512
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002513 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
Steven Rostedtf7d82352012-04-06 00:47:53 +02002514 goto out_free;
2515
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03002516 field = calloc(1, sizeof(*field));
2517 if (!field)
2518 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002519
2520 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002521 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002522 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002523 field->value = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002524 if (field->value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002525 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002526
2527 free_arg(arg);
2528 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002529 if (!arg)
2530 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002531
2532 free_token(token);
2533 type = process_arg(event, arg, &token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002534 if (test_type_token(type, token, TEP_EVENT_OP, "}"))
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002535 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002536
2537 value = arg_eval(arg);
Vaibhav Nagarnaikd69afed52012-04-06 00:48:00 +02002538 if (value == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002539 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002540 field->str = strdup(value);
Namhyung Kimca638582012-04-09 11:54:31 +09002541 if (field->str == NULL)
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002542 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002543 free_arg(arg);
2544 arg = NULL;
2545
2546 *list = field;
2547 list = &field->next;
2548
2549 free_token(token);
2550 type = read_token_item(&token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002551 } while (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002552
2553 *tok = token;
2554 return type;
2555
Namhyung Kimf8c49d22012-09-19 15:58:43 +09002556out_free_field:
2557 free_flag_sym(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002558out_free:
2559 free_arg(arg);
2560 free_token(token);
2561 *tok = NULL;
2562
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002563 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002564}
2565
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002566static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002567process_flags(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002568{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002569 struct tep_print_arg *field;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002570 enum tep_event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002571 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002572
2573 memset(arg, 0, sizeof(*arg));
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002574 arg->type = TEP_PRINT_FLAGS;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002575
2576 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002577 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002578 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002579 goto out_free;
2580 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002581
Steven Rostedteff2c922013-11-18 14:23:14 -05002582 type = process_field_arg(event, field, &token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002583
2584 /* Handle operations in the first argument */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002585 while (type == TEP_EVENT_OP)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002586 type = process_op(event, field, &token);
2587
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002588 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002589 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002590 free_token(token);
2591
2592 arg->flags.field = field;
2593
2594 type = read_token_item(&token);
2595 if (event_item_type(type)) {
2596 arg->flags.delim = token;
2597 type = read_token_item(&token);
2598 }
2599
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002600 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
Steven Rostedtf7d82352012-04-06 00:47:53 +02002601 goto out_free;
2602
2603 type = process_fields(event, &arg->flags.flags, &token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002604 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02002605 goto out_free;
2606
2607 free_token(token);
2608 type = read_token_item(tok);
2609 return type;
2610
Namhyung Kim70d93042012-09-19 15:58:44 +09002611out_free_field:
2612 free_arg(field);
2613out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002614 free_token(token);
2615 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002616 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002617}
2618
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002619static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002620process_symbols(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002621{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002622 struct tep_print_arg *field;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002623 enum tep_event_type type;
Rickard Strandqvist21da83f2014-06-24 13:09:10 +02002624 char *token = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002625
2626 memset(arg, 0, sizeof(*arg));
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002627 arg->type = TEP_PRINT_SYMBOL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002628
2629 field = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002630 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002631 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002632 goto out_free;
2633 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002634
Steven Rostedteff2c922013-11-18 14:23:14 -05002635 type = process_field_arg(event, field, &token);
2636
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002637 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
Namhyung Kim70d93042012-09-19 15:58:44 +09002638 goto out_free_field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002639
2640 arg->symbol.field = field;
2641
2642 type = process_fields(event, &arg->symbol.symbols, &token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002643 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02002644 goto out_free;
2645
2646 free_token(token);
2647 type = read_token_item(tok);
2648 return type;
2649
Namhyung Kim70d93042012-09-19 15:58:44 +09002650out_free_field:
2651 free_arg(field);
2652out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002653 free_token(token);
2654 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002655 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002656}
2657
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002658static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002659process_hex_common(struct tep_event *event, struct tep_print_arg *arg,
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002660 char **tok, enum tep_print_arg_type type)
Namhyung Kime080e6f2012-06-27 09:41:41 +09002661{
Namhyung Kime080e6f2012-06-27 09:41:41 +09002662 memset(arg, 0, sizeof(*arg));
Daniel Borkmann0fe05592017-01-25 02:28:17 +01002663 arg->type = type;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002664
Javi Merino929a6bb2015-03-20 18:12:55 +00002665 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2666 goto out;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002667
Javi Merino929a6bb2015-03-20 18:12:55 +00002668 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2669 goto free_field;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002670
Javi Merino929a6bb2015-03-20 18:12:55 +00002671 return read_token_item(tok);
Namhyung Kime080e6f2012-06-27 09:41:41 +09002672
Javi Merino929a6bb2015-03-20 18:12:55 +00002673free_field:
2674 free_arg(arg->hex.field);
Steven Rostedt (Red Hat)9ec72ea2016-02-09 15:40:16 -05002675 arg->hex.field = NULL;
Javi Merino929a6bb2015-03-20 18:12:55 +00002676out:
Namhyung Kime080e6f2012-06-27 09:41:41 +09002677 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002678 return TEP_EVENT_ERROR;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002679}
Namhyung Kime080e6f2012-06-27 09:41:41 +09002680
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002681static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002682process_hex(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Daniel Borkmann0fe05592017-01-25 02:28:17 +01002683{
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002684 return process_hex_common(event, arg, tok, TEP_PRINT_HEX);
Daniel Borkmann0fe05592017-01-25 02:28:17 +01002685}
2686
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002687static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002688process_hex_str(struct tep_event *event, struct tep_print_arg *arg,
Daniel Borkmann0fe05592017-01-25 02:28:17 +01002689 char **tok)
2690{
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002691 return process_hex_common(event, arg, tok, TEP_PRINT_HEX_STR);
Daniel Borkmann0fe05592017-01-25 02:28:17 +01002692}
2693
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002694static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002695process_int_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Javi Merinob839e1e82015-03-24 11:07:19 +00002696{
2697 memset(arg, 0, sizeof(*arg));
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002698 arg->type = TEP_PRINT_INT_ARRAY;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002699
Javi Merinob839e1e82015-03-24 11:07:19 +00002700 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2701 goto out;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002702
Javi Merinob839e1e82015-03-24 11:07:19 +00002703 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2704 goto free_field;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002705
Javi Merinob839e1e82015-03-24 11:07:19 +00002706 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2707 goto free_size;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002708
Javi Merinob839e1e82015-03-24 11:07:19 +00002709 return read_token_item(tok);
Namhyung Kime080e6f2012-06-27 09:41:41 +09002710
Javi Merinob839e1e82015-03-24 11:07:19 +00002711free_size:
2712 free_arg(arg->int_array.count);
Steven Rostedt (Red Hat)9ec72ea2016-02-09 15:40:16 -05002713 arg->int_array.count = NULL;
Javi Merinob839e1e82015-03-24 11:07:19 +00002714free_field:
2715 free_arg(arg->int_array.field);
Steven Rostedt (Red Hat)9ec72ea2016-02-09 15:40:16 -05002716 arg->int_array.field = NULL;
Javi Merinob839e1e82015-03-24 11:07:19 +00002717out:
Namhyung Kime080e6f2012-06-27 09:41:41 +09002718 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002719 return TEP_EVENT_ERROR;
Namhyung Kime080e6f2012-06-27 09:41:41 +09002720}
2721
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002722static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002723process_dynamic_array(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002724{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04002725 struct tep_format_field *field;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002726 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002727 char *token;
2728
2729 memset(arg, 0, sizeof(*arg));
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002730 arg->type = TEP_PRINT_DYNAMIC_ARRAY;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002731
2732 /*
2733 * The item within the parenthesis is another field that holds
2734 * the index into where the array starts.
2735 */
2736 type = read_token(&token);
2737 *tok = token;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002738 if (type != TEP_EVENT_ITEM)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002739 goto out_free;
2740
2741 /* Find the field */
2742
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04002743 field = tep_find_field(event, token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002744 if (!field)
2745 goto out_free;
2746
2747 arg->dynarray.field = field;
2748 arg->dynarray.index = 0;
2749
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002750 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002751 goto out_free;
2752
2753 free_token(token);
2754 type = read_token_item(&token);
2755 *tok = token;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002756 if (type != TEP_EVENT_OP || strcmp(token, "[") != 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002757 return type;
2758
2759 free_token(token);
2760 arg = alloc_arg();
Sasha Levinfba7a782012-12-21 15:00:58 -05002761 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002762 do_warning_event(event, "%s: not enough memory!", __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002763 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002764 return TEP_EVENT_ERROR;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002765 }
2766
Steven Rostedtf7d82352012-04-06 00:47:53 +02002767 type = process_arg(event, arg, &token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002768 if (type == TEP_EVENT_ERROR)
Namhyung Kimb3511d02012-05-23 11:36:50 +09002769 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002770
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002771 if (!test_type_token(type, token, TEP_EVENT_OP, "]"))
Namhyung Kimb3511d02012-05-23 11:36:50 +09002772 goto out_free_arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002773
2774 free_token(token);
2775 type = read_token_item(tok);
2776 return type;
2777
Namhyung Kimb3511d02012-05-23 11:36:50 +09002778 out_free_arg:
2779 free_arg(arg);
Steven Rostedtf7d82352012-04-06 00:47:53 +02002780 out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02002781 free_token(token);
2782 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002783 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002784}
2785
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002786static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002787process_dynamic_array_len(struct tep_event *event, struct tep_print_arg *arg,
He Kuang76055942015-08-29 04:22:05 +00002788 char **tok)
2789{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04002790 struct tep_format_field *field;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002791 enum tep_event_type type;
He Kuang76055942015-08-29 04:22:05 +00002792 char *token;
2793
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002794 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
He Kuang76055942015-08-29 04:22:05 +00002795 goto out_free;
2796
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002797 arg->type = TEP_PRINT_DYNAMIC_ARRAY_LEN;
He Kuang76055942015-08-29 04:22:05 +00002798
2799 /* Find the field */
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04002800 field = tep_find_field(event, token);
He Kuang76055942015-08-29 04:22:05 +00002801 if (!field)
2802 goto out_free;
2803
2804 arg->dynarray.field = field;
2805 arg->dynarray.index = 0;
2806
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002807 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
He Kuang76055942015-08-29 04:22:05 +00002808 goto out_err;
2809
2810 type = read_token(&token);
2811 *tok = token;
2812
2813 return type;
2814
2815 out_free:
2816 free_token(token);
2817 out_err:
2818 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002819 return TEP_EVENT_ERROR;
He Kuang76055942015-08-29 04:22:05 +00002820}
2821
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002822static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002823process_paren(struct tep_event *event, struct tep_print_arg *arg, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002824{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002825 struct tep_print_arg *item_arg;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002826 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002827 char *token;
2828
2829 type = process_arg(event, arg, &token);
2830
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002831 if (type == TEP_EVENT_ERROR)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002832 goto out_free;
2833
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002834 if (type == TEP_EVENT_OP)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002835 type = process_op(event, arg, &token);
2836
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002837 if (type == TEP_EVENT_ERROR)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002838 goto out_free;
2839
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002840 if (test_type_token(type, token, TEP_EVENT_DELIM, ")"))
Steven Rostedtf7d82352012-04-06 00:47:53 +02002841 goto out_free;
2842
2843 free_token(token);
2844 type = read_token_item(&token);
2845
2846 /*
2847 * If the next token is an item or another open paren, then
2848 * this was a typecast.
2849 */
2850 if (event_item_type(type) ||
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002851 (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02002852
2853 /* make this a typecast and contine */
2854
2855 /* prevous must be an atom */
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002856 if (arg->type != TEP_PRINT_ATOM) {
2857 do_warning_event(event, "previous needed to be TEP_PRINT_ATOM");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03002858 goto out_free;
2859 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002860
2861 item_arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002862 if (!item_arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002863 do_warning_event(event, "%s: not enough memory!",
2864 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002865 goto out_free;
2866 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02002867
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002868 arg->type = TEP_PRINT_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002869 arg->typecast.type = arg->atom.atom;
2870 arg->typecast.item = item_arg;
2871 type = process_arg_token(event, item_arg, &token, type);
2872
2873 }
2874
2875 *tok = token;
2876 return type;
2877
2878 out_free:
2879 free_token(token);
2880 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002881 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002882}
2883
2884
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002885static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002886process_str(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
Irina Tirdea1d037ca2012-09-11 01:15:03 +03002887 char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002888{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002889 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002890 char *token;
2891
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002892 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002893 goto out_free;
2894
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002895 arg->type = TEP_PRINT_STRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002896 arg->string.string = token;
2897 arg->string.offset = -1;
2898
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002899 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002900 goto out_err;
2901
2902 type = read_token(&token);
2903 *tok = token;
2904
2905 return type;
2906
2907 out_free:
2908 free_token(token);
2909 out_err:
2910 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002911 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002912}
2913
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002914static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002915process_bitmask(struct tep_event *event __maybe_unused, struct tep_print_arg *arg,
Tzvetomir Stoyanov (VMware)4963b0f2018-09-19 14:56:44 -04002916 char **tok)
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002917{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002918 enum tep_event_type type;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002919 char *token;
2920
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002921 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002922 goto out_free;
2923
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002924 arg->type = TEP_PRINT_BITMASK;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002925 arg->bitmask.bitmask = token;
2926 arg->bitmask.offset = -1;
2927
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002928 if (read_expected(TEP_EVENT_DELIM, ")") < 0)
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002929 goto out_err;
2930
2931 type = read_token(&token);
2932 *tok = token;
2933
2934 return type;
2935
2936 out_free:
2937 free_token(token);
2938 out_err:
2939 *tok = NULL;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002940 return TEP_EVENT_ERROR;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04002941}
2942
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04002943static struct tep_function_handler *
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04002944find_func_handler(struct tep_handle *pevent, char *func_name)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002945{
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04002946 struct tep_function_handler *func;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002947
Steven Rostedt101782e2012-10-01 20:13:51 -04002948 if (!pevent)
2949 return NULL;
2950
Steven Rostedtf7d82352012-04-06 00:47:53 +02002951 for (func = pevent->func_handlers; func; func = func->next) {
2952 if (strcmp(func->name, func_name) == 0)
2953 break;
2954 }
2955
2956 return func;
2957}
2958
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04002959static void remove_func_handler(struct tep_handle *pevent, char *func_name)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002960{
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04002961 struct tep_function_handler *func;
2962 struct tep_function_handler **next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002963
2964 next = &pevent->func_handlers;
2965 while ((func = *next)) {
2966 if (strcmp(func->name, func_name) == 0) {
2967 *next = func->next;
2968 free_func_handle(func);
2969 break;
2970 }
2971 next = &func->next;
2972 }
2973}
2974
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002975static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05002976process_func_handler(struct tep_event *event, struct tep_function_handler *func,
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002977 struct tep_print_arg *arg, char **tok)
Steven Rostedtf7d82352012-04-06 00:47:53 +02002978{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04002979 struct tep_print_arg **next_arg;
2980 struct tep_print_arg *farg;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002981 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002982 char *token;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002983 int i;
2984
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04002985 arg->type = TEP_PRINT_FUNC;
Steven Rostedtf7d82352012-04-06 00:47:53 +02002986 arg->func.func = func;
2987
2988 *tok = NULL;
2989
2990 next_arg = &(arg->func.args);
2991 for (i = 0; i < func->nr_args; i++) {
2992 farg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002993 if (!farg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09002994 do_warning_event(event, "%s: not enough memory!",
2995 __func__);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04002996 return TEP_EVENT_ERROR;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09002997 }
2998
Steven Rostedtf7d82352012-04-06 00:47:53 +02002999 type = process_arg(event, farg, &token);
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05003000 if (i < (func->nr_args - 1)) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003001 if (type != TEP_EVENT_DELIM || strcmp(token, ",") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09003002 do_warning_event(event,
3003 "Error: function '%s()' expects %d arguments but event %s only uses %d",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05003004 func->name, func->nr_args,
3005 event->name, i + 1);
3006 goto err;
3007 }
3008 } else {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003009 if (type != TEP_EVENT_DELIM || strcmp(token, ")") != 0) {
Namhyung Kim9e9e5df2014-03-19 10:22:54 +09003010 do_warning_event(event,
3011 "Error: function '%s()' only expects %d arguments but event %s has more",
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05003012 func->name, func->nr_args, event->name);
3013 goto err;
3014 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003015 }
3016
3017 *next_arg = farg;
3018 next_arg = &(farg->next);
3019 free_token(token);
3020 }
3021
3022 type = read_token(&token);
3023 *tok = token;
3024
3025 return type;
Steven Rostedt3a3ffa22013-11-18 21:38:20 -05003026
3027err:
3028 free_arg(farg);
3029 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003030 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003031}
3032
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003033static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003034process_function(struct tep_event *event, struct tep_print_arg *arg,
Steven Rostedtf7d82352012-04-06 00:47:53 +02003035 char *token, char **tok)
3036{
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04003037 struct tep_function_handler *func;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003038
3039 if (strcmp(token, "__print_flags") == 0) {
3040 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02003041 is_flag_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003042 return process_flags(event, arg, tok);
3043 }
3044 if (strcmp(token, "__print_symbolic") == 0) {
3045 free_token(token);
Tom Zanussi5205aec2012-04-06 00:47:58 +02003046 is_symbolic_field = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003047 return process_symbols(event, arg, tok);
3048 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09003049 if (strcmp(token, "__print_hex") == 0) {
3050 free_token(token);
3051 return process_hex(event, arg, tok);
3052 }
Daniel Borkmann0fe05592017-01-25 02:28:17 +01003053 if (strcmp(token, "__print_hex_str") == 0) {
3054 free_token(token);
3055 return process_hex_str(event, arg, tok);
3056 }
Javi Merinob839e1e82015-03-24 11:07:19 +00003057 if (strcmp(token, "__print_array") == 0) {
3058 free_token(token);
3059 return process_int_array(event, arg, tok);
3060 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003061 if (strcmp(token, "__get_str") == 0) {
3062 free_token(token);
3063 return process_str(event, arg, tok);
3064 }
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003065 if (strcmp(token, "__get_bitmask") == 0) {
3066 free_token(token);
3067 return process_bitmask(event, arg, tok);
3068 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003069 if (strcmp(token, "__get_dynamic_array") == 0) {
3070 free_token(token);
3071 return process_dynamic_array(event, arg, tok);
3072 }
He Kuang76055942015-08-29 04:22:05 +00003073 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3074 free_token(token);
3075 return process_dynamic_array_len(event, arg, tok);
3076 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003077
3078 func = find_func_handler(event->pevent, token);
3079 if (func) {
3080 free_token(token);
3081 return process_func_handler(event, func, arg, tok);
3082 }
3083
Namhyung Kim3388cc32014-03-19 10:22:53 +09003084 do_warning_event(event, "function %s not defined", token);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003085 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003086 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003087}
3088
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003089static enum tep_event_type
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003090process_arg_token(struct tep_event *event, struct tep_print_arg *arg,
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003091 char **tok, enum tep_event_type type)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003092{
3093 char *token;
3094 char *atom;
3095
3096 token = *tok;
3097
3098 switch (type) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003099 case TEP_EVENT_ITEM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003100 if (strcmp(token, "REC") == 0) {
3101 free_token(token);
3102 type = process_entry(event, arg, &token);
3103 break;
3104 }
3105 atom = token;
3106 /* test the next token */
3107 type = read_token_item(&token);
3108
3109 /*
3110 * If the next token is a parenthesis, then this
3111 * is a function.
3112 */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003113 if (type == TEP_EVENT_DELIM && strcmp(token, "(") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003114 free_token(token);
3115 token = NULL;
3116 /* this will free atom. */
3117 type = process_function(event, arg, atom, &token);
3118 break;
3119 }
3120 /* atoms can be more than one token long */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003121 while (type == TEP_EVENT_ITEM) {
Namhyung Kimd2864472012-04-09 11:54:33 +09003122 char *new_atom;
3123 new_atom = realloc(atom,
3124 strlen(atom) + strlen(token) + 2);
3125 if (!new_atom) {
3126 free(atom);
3127 *tok = NULL;
3128 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003129 return TEP_EVENT_ERROR;
Namhyung Kimd2864472012-04-09 11:54:33 +09003130 }
3131 atom = new_atom;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003132 strcat(atom, " ");
3133 strcat(atom, token);
3134 free_token(token);
3135 type = read_token_item(&token);
3136 }
3137
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003138 arg->type = TEP_PRINT_ATOM;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003139 arg->atom.atom = atom;
3140 break;
3141
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003142 case TEP_EVENT_DQUOTE:
3143 case TEP_EVENT_SQUOTE:
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003144 arg->type = TEP_PRINT_ATOM;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003145 arg->atom.atom = token;
3146 type = read_token_item(&token);
3147 break;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003148 case TEP_EVENT_DELIM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003149 if (strcmp(token, "(") == 0) {
3150 free_token(token);
3151 type = process_paren(event, arg, &token);
3152 break;
3153 }
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003154 case TEP_EVENT_OP:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003155 /* handle single ops */
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003156 arg->type = TEP_PRINT_OP;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003157 arg->op.op = token;
3158 arg->op.left = NULL;
3159 type = process_op(event, arg, &token);
3160
3161 /* On error, the op is freed */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003162 if (type == TEP_EVENT_ERROR)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003163 arg->op.op = NULL;
3164
3165 /* return error type if errored */
3166 break;
3167
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003168 case TEP_EVENT_ERROR ... TEP_EVENT_NEWLINE:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003169 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003170 do_warning_event(event, "unexpected type %d", type);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003171 return TEP_EVENT_ERROR;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003172 }
3173 *tok = token;
3174
3175 return type;
3176}
3177
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003178static int event_read_print_args(struct tep_event *event, struct tep_print_arg **list)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003179{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003180 enum tep_event_type type = TEP_EVENT_ERROR;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04003181 struct tep_print_arg *arg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003182 char *token;
3183 int args = 0;
3184
3185 do {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003186 if (type == TEP_EVENT_NEWLINE) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003187 type = read_token_item(&token);
3188 continue;
3189 }
3190
3191 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003192 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003193 do_warning_event(event, "%s: not enough memory!",
3194 __func__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09003195 return -1;
3196 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003197
3198 type = process_arg(event, arg, &token);
3199
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003200 if (type == TEP_EVENT_ERROR) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003201 free_token(token);
3202 free_arg(arg);
3203 return -1;
3204 }
3205
3206 *list = arg;
3207 args++;
3208
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003209 if (type == TEP_EVENT_OP) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003210 type = process_op(event, arg, &token);
3211 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003212 if (type == TEP_EVENT_ERROR) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003213 *list = NULL;
3214 free_arg(arg);
3215 return -1;
3216 }
3217 list = &arg->next;
3218 continue;
3219 }
3220
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003221 if (type == TEP_EVENT_DELIM && strcmp(token, ",") == 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003222 free_token(token);
3223 *list = arg;
3224 list = &arg->next;
3225 continue;
3226 }
3227 break;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003228 } while (type != TEP_EVENT_NONE);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003229
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003230 if (type != TEP_EVENT_NONE && type != TEP_EVENT_ERROR)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003231 free_token(token);
3232
3233 return args;
3234}
3235
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003236static int event_read_print(struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003237{
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003238 enum tep_event_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003239 char *token;
3240 int ret;
3241
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003242 if (read_expected_item(TEP_EVENT_ITEM, "print") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003243 return -1;
3244
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003245 if (read_expected(TEP_EVENT_ITEM, "fmt") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003246 return -1;
3247
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003248 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003249 return -1;
3250
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003251 if (read_expect_type(TEP_EVENT_DQUOTE, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003252 goto fail;
3253
3254 concat:
3255 event->print_fmt.format = token;
3256 event->print_fmt.args = NULL;
3257
3258 /* ok to have no arg */
3259 type = read_token_item(&token);
3260
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003261 if (type == TEP_EVENT_NONE)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003262 return 0;
3263
3264 /* Handle concatenation of print lines */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003265 if (type == TEP_EVENT_DQUOTE) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003266 char *cat;
3267
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03003268 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3269 goto fail;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003270 free_token(token);
3271 free_token(event->print_fmt.format);
3272 event->print_fmt.format = NULL;
3273 token = cat;
3274 goto concat;
3275 }
3276
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04003277 if (test_type_token(type, token, TEP_EVENT_DELIM, ","))
Steven Rostedtf7d82352012-04-06 00:47:53 +02003278 goto fail;
3279
3280 free_token(token);
3281
3282 ret = event_read_print_args(event, &event->print_fmt.args);
3283 if (ret < 0)
3284 return -1;
3285
3286 return ret;
3287
3288 fail:
3289 free_token(token);
3290 return -1;
3291}
3292
3293/**
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003294 * tep_find_common_field - return a common field by event
Steven Rostedtf7d82352012-04-06 00:47:53 +02003295 * @event: handle for the event
3296 * @name: the name of the common field to return
3297 *
3298 * Returns a common field from the event by the given @name.
Ingo Molnar3e449f72018-12-03 11:22:00 +01003299 * This only searches the common fields and not all field.
Steven Rostedtf7d82352012-04-06 00:47:53 +02003300 */
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003301struct tep_format_field *
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003302tep_find_common_field(struct tep_event *event, const char *name)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003303{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003304 struct tep_format_field *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003305
3306 for (format = event->format.common_fields;
3307 format; format = format->next) {
3308 if (strcmp(format->name, name) == 0)
3309 break;
3310 }
3311
3312 return format;
3313}
3314
3315/**
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003316 * tep_find_field - find a non-common field
Steven Rostedtf7d82352012-04-06 00:47:53 +02003317 * @event: handle for the event
3318 * @name: the name of the non-common field
3319 *
3320 * Returns a non-common field by the given @name.
3321 * This does not search common fields.
3322 */
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003323struct tep_format_field *
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003324tep_find_field(struct tep_event *event, const char *name)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003325{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003326 struct tep_format_field *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003327
3328 for (format = event->format.fields;
3329 format; format = format->next) {
3330 if (strcmp(format->name, name) == 0)
3331 break;
3332 }
3333
3334 return format;
3335}
3336
3337/**
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003338 * tep_find_any_field - find any field by name
Steven Rostedtf7d82352012-04-06 00:47:53 +02003339 * @event: handle for the event
3340 * @name: the name of the field
3341 *
3342 * Returns a field by the given @name.
Ingo Molnar3e449f72018-12-03 11:22:00 +01003343 * This searches the common field names first, then
Steven Rostedtf7d82352012-04-06 00:47:53 +02003344 * the non-common ones if a common one was not found.
3345 */
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003346struct tep_format_field *
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003347tep_find_any_field(struct tep_event *event, const char *name)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003348{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003349 struct tep_format_field *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003350
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003351 format = tep_find_common_field(event, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003352 if (format)
3353 return format;
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003354 return tep_find_field(event, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003355}
3356
3357/**
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003358 * tep_read_number - read a number from data
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003359 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02003360 * @ptr: the raw data
3361 * @size: the size of the data that holds the number
3362 *
3363 * Returns the number (converted to host) from the
3364 * raw data.
3365 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003366unsigned long long tep_read_number(struct tep_handle *tep,
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003367 const void *ptr, int size)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003368{
Tzvetomir Stoyanov6cd99d22018-11-30 10:44:10 -05003369 unsigned long long val;
3370
Steven Rostedtf7d82352012-04-06 00:47:53 +02003371 switch (size) {
3372 case 1:
3373 return *(unsigned char *)ptr;
3374 case 2:
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003375 return tep_data2host2(tep, *(unsigned short *)ptr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003376 case 4:
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003377 return tep_data2host4(tep, *(unsigned int *)ptr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003378 case 8:
Tzvetomir Stoyanov6cd99d22018-11-30 10:44:10 -05003379 memcpy(&val, (ptr), sizeof(unsigned long long));
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003380 return tep_data2host8(tep, val);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003381 default:
3382 /* BUG! */
3383 return 0;
3384 }
3385}
3386
3387/**
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003388 * tep_read_number_field - read a number from data
Steven Rostedtf7d82352012-04-06 00:47:53 +02003389 * @field: a handle to the field
3390 * @data: the raw data to read
3391 * @value: the value to place the number in
3392 *
3393 * Reads raw data according to a field offset and size,
3394 * and translates it into @value.
3395 *
3396 * Returns 0 on success, -1 otherwise.
3397 */
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003398int tep_read_number_field(struct tep_format_field *field, const void *data,
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003399 unsigned long long *value)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003400{
3401 if (!field)
3402 return -1;
3403 switch (field->size) {
3404 case 1:
3405 case 2:
3406 case 4:
3407 case 8:
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003408 *value = tep_read_number(field->event->pevent,
3409 data + field->offset, field->size);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003410 return 0;
3411 default:
3412 return -1;
3413 }
3414}
3415
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003416static int get_common_info(struct tep_handle *pevent,
Steven Rostedtf7d82352012-04-06 00:47:53 +02003417 const char *type, int *offset, int *size)
3418{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003419 struct tep_event *event;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003420 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003421
3422 /*
3423 * All events should have the same common elements.
3424 * Pick any event to find where the type is;
3425 */
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003426 if (!pevent->events) {
3427 do_warning("no event_list!");
3428 return -1;
3429 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003430
3431 event = pevent->events[0];
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003432 field = tep_find_common_field(event, type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003433 if (!field)
Steven Rostedt0866a972012-05-22 14:52:40 +09003434 return -1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003435
3436 *offset = field->offset;
3437 *size = field->size;
3438
3439 return 0;
3440}
3441
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003442static int __parse_common(struct tep_handle *pevent, void *data,
Steven Rostedtf7d82352012-04-06 00:47:53 +02003443 int *size, int *offset, const char *name)
3444{
3445 int ret;
3446
3447 if (!*size) {
3448 ret = get_common_info(pevent, name, offset, size);
3449 if (ret < 0)
3450 return ret;
3451 }
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003452 return tep_read_number(pevent, data + *offset, *size);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003453}
3454
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003455static int trace_parse_common_type(struct tep_handle *pevent, void *data)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003456{
3457 return __parse_common(pevent, data,
3458 &pevent->type_size, &pevent->type_offset,
3459 "common_type");
3460}
3461
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003462static int parse_common_pid(struct tep_handle *pevent, void *data)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003463{
3464 return __parse_common(pevent, data,
3465 &pevent->pid_size, &pevent->pid_offset,
3466 "common_pid");
3467}
3468
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003469static int parse_common_pc(struct tep_handle *pevent, void *data)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003470{
3471 return __parse_common(pevent, data,
3472 &pevent->pc_size, &pevent->pc_offset,
3473 "common_preempt_count");
3474}
3475
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003476static int parse_common_flags(struct tep_handle *pevent, void *data)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003477{
3478 return __parse_common(pevent, data,
3479 &pevent->flags_size, &pevent->flags_offset,
3480 "common_flags");
3481}
3482
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003483static int parse_common_lock_depth(struct tep_handle *pevent, void *data)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003484{
Steven Rostedt0866a972012-05-22 14:52:40 +09003485 return __parse_common(pevent, data,
3486 &pevent->ld_size, &pevent->ld_offset,
3487 "common_lock_depth");
3488}
Steven Rostedtf7d82352012-04-06 00:47:53 +02003489
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003490static int parse_common_migrate_disable(struct tep_handle *pevent, void *data)
Steven Rostedt0866a972012-05-22 14:52:40 +09003491{
3492 return __parse_common(pevent, data,
3493 &pevent->ld_size, &pevent->ld_offset,
3494 "common_migrate_disable");
Steven Rostedtf7d82352012-04-06 00:47:53 +02003495}
3496
3497static int events_id_cmp(const void *a, const void *b);
3498
3499/**
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003500 * tep_find_event - find an event by given id
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003501 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02003502 * @id: the id of the event
3503 *
3504 * Returns an event that has a given @id.
3505 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003506struct tep_event *tep_find_event(struct tep_handle *tep, int id)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003507{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003508 struct tep_event **eventptr;
3509 struct tep_event key;
3510 struct tep_event *pkey = &key;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003511
3512 /* Check cache first */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003513 if (tep->last_event && tep->last_event->id == id)
3514 return tep->last_event;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003515
3516 key.id = id;
3517
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003518 eventptr = bsearch(&pkey, tep->events, tep->nr_events,
3519 sizeof(*tep->events), events_id_cmp);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003520
3521 if (eventptr) {
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003522 tep->last_event = *eventptr;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003523 return *eventptr;
3524 }
3525
3526 return NULL;
3527}
3528
3529/**
Tzvetomir Stoyanov (VMware)610e1e42018-08-08 14:03:05 -04003530 * tep_find_event_by_name - find an event by given name
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003531 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02003532 * @sys: the system name to search for
3533 * @name: the name of the event to search for
3534 *
3535 * This returns an event with a given @name and under the system
3536 * @sys. If @sys is NULL the first event with @name is returned.
3537 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003538struct tep_event *
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003539tep_find_event_by_name(struct tep_handle *tep,
Tzvetomir Stoyanov (VMware)610e1e42018-08-08 14:03:05 -04003540 const char *sys, const char *name)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003541{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003542 struct tep_event *event = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003543 int i;
3544
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003545 if (tep->last_event &&
3546 strcmp(tep->last_event->name, name) == 0 &&
3547 (!sys || strcmp(tep->last_event->system, sys) == 0))
3548 return tep->last_event;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003549
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003550 for (i = 0; i < tep->nr_events; i++) {
3551 event = tep->events[i];
Steven Rostedtf7d82352012-04-06 00:47:53 +02003552 if (strcmp(event->name, name) == 0) {
3553 if (!sys)
3554 break;
3555 if (strcmp(event->system, sys) == 0)
3556 break;
3557 }
3558 }
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003559 if (i == tep->nr_events)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003560 event = NULL;
3561
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04003562 tep->last_event = event;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003563 return event;
3564}
3565
3566static unsigned long long
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003567eval_num_arg(void *data, int size, struct tep_event *event, struct tep_print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003568{
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003569 struct tep_handle *pevent = event->pevent;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003570 unsigned long long val = 0;
3571 unsigned long long left, right;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04003572 struct tep_print_arg *typearg = NULL;
3573 struct tep_print_arg *larg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003574 unsigned long offset;
3575 unsigned int field_size;
3576
3577 switch (arg->type) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003578 case TEP_PRINT_NULL:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003579 /* ?? */
3580 return 0;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003581 case TEP_PRINT_ATOM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003582 return strtoull(arg->atom.atom, NULL, 0);
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003583 case TEP_PRINT_FIELD:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003584 if (!arg->field.field) {
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003585 arg->field.field = tep_find_any_field(event, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003586 if (!arg->field.field)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003587 goto out_warning_field;
3588
Steven Rostedtf7d82352012-04-06 00:47:53 +02003589 }
3590 /* must be a number */
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003591 val = tep_read_number(pevent, data + arg->field.field->offset,
3592 arg->field.field->size);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003593 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003594 case TEP_PRINT_FLAGS:
3595 case TEP_PRINT_SYMBOL:
3596 case TEP_PRINT_INT_ARRAY:
3597 case TEP_PRINT_HEX:
3598 case TEP_PRINT_HEX_STR:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003599 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003600 case TEP_PRINT_TYPE:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003601 val = eval_num_arg(data, size, event, arg->typecast.item);
3602 return eval_type(val, arg, 0);
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003603 case TEP_PRINT_STRING:
3604 case TEP_PRINT_BSTRING:
3605 case TEP_PRINT_BITMASK:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003606 return 0;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003607 case TEP_PRINT_FUNC: {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003608 struct trace_seq s;
3609 trace_seq_init(&s);
3610 val = process_defined_func(&s, data, size, event, arg);
3611 trace_seq_destroy(&s);
3612 return val;
3613 }
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003614 case TEP_PRINT_OP:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003615 if (strcmp(arg->op.op, "[") == 0) {
3616 /*
3617 * Arrays are special, since we don't want
3618 * to read the arg as is.
3619 */
3620 right = eval_num_arg(data, size, event, arg->op.right);
3621
3622 /* handle typecasts */
3623 larg = arg->op.left;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003624 while (larg->type == TEP_PRINT_TYPE) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003625 if (!typearg)
3626 typearg = larg;
3627 larg = larg->typecast.item;
3628 }
3629
3630 /* Default to long size */
3631 field_size = pevent->long_size;
3632
3633 switch (larg->type) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003634 case TEP_PRINT_DYNAMIC_ARRAY:
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003635 offset = tep_read_number(pevent,
Steven Rostedtf7d82352012-04-06 00:47:53 +02003636 data + larg->dynarray.field->offset,
3637 larg->dynarray.field->size);
3638 if (larg->dynarray.field->elementsize)
3639 field_size = larg->dynarray.field->elementsize;
3640 /*
3641 * The actual length of the dynamic array is stored
3642 * in the top half of the field, and the offset
3643 * is in the bottom half of the 32 bit field.
3644 */
3645 offset &= 0xffff;
3646 offset += right;
3647 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003648 case TEP_PRINT_FIELD:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003649 if (!larg->field.field) {
3650 larg->field.field =
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003651 tep_find_any_field(event, larg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003652 if (!larg->field.field) {
3653 arg = larg;
3654 goto out_warning_field;
3655 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02003656 }
3657 field_size = larg->field.field->elementsize;
3658 offset = larg->field.field->offset +
3659 right * larg->field.field->elementsize;
3660 break;
3661 default:
3662 goto default_op; /* oops, all bets off */
3663 }
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003664 val = tep_read_number(pevent,
3665 data + offset, field_size);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003666 if (typearg)
3667 val = eval_type(val, typearg, 1);
3668 break;
3669 } else if (strcmp(arg->op.op, "?") == 0) {
3670 left = eval_num_arg(data, size, event, arg->op.left);
3671 arg = arg->op.right;
3672 if (left)
3673 val = eval_num_arg(data, size, event, arg->op.left);
3674 else
3675 val = eval_num_arg(data, size, event, arg->op.right);
3676 break;
3677 }
3678 default_op:
3679 left = eval_num_arg(data, size, event, arg->op.left);
3680 right = eval_num_arg(data, size, event, arg->op.right);
3681 switch (arg->op.op[0]) {
3682 case '!':
3683 switch (arg->op.op[1]) {
3684 case 0:
3685 val = !right;
3686 break;
3687 case '=':
3688 val = left != right;
3689 break;
3690 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003691 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003692 }
3693 break;
3694 case '~':
3695 val = ~right;
3696 break;
3697 case '|':
3698 if (arg->op.op[1])
3699 val = left || right;
3700 else
3701 val = left | right;
3702 break;
3703 case '&':
3704 if (arg->op.op[1])
3705 val = left && right;
3706 else
3707 val = left & right;
3708 break;
3709 case '<':
3710 switch (arg->op.op[1]) {
3711 case 0:
3712 val = left < right;
3713 break;
3714 case '<':
3715 val = left << right;
3716 break;
3717 case '=':
3718 val = left <= right;
3719 break;
3720 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003721 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003722 }
3723 break;
3724 case '>':
3725 switch (arg->op.op[1]) {
3726 case 0:
3727 val = left > right;
3728 break;
3729 case '>':
3730 val = left >> right;
3731 break;
3732 case '=':
3733 val = left >= right;
3734 break;
3735 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003736 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003737 }
3738 break;
3739 case '=':
3740 if (arg->op.op[1] != '=')
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003741 goto out_warning_op;
3742
Steven Rostedtf7d82352012-04-06 00:47:53 +02003743 val = left == right;
3744 break;
3745 case '-':
3746 val = left - right;
3747 break;
3748 case '+':
3749 val = left + right;
3750 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003751 case '/':
3752 val = left / right;
3753 break;
Daniel Bristot de Oliveira0e47b382016-02-22 14:08:22 -03003754 case '%':
3755 val = left % right;
3756 break;
Steven Rostedt2e7a5fc2012-04-06 00:48:04 +02003757 case '*':
3758 val = left * right;
3759 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003760 default:
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003761 goto out_warning_op;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003762 }
3763 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003764 case TEP_PRINT_DYNAMIC_ARRAY_LEN:
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003765 offset = tep_read_number(pevent,
3766 data + arg->dynarray.field->offset,
3767 arg->dynarray.field->size);
He Kuang76055942015-08-29 04:22:05 +00003768 /*
3769 * The total allocated length of the dynamic array is
3770 * stored in the top half of the field, and the offset
3771 * is in the bottom half of the 32 bit field.
3772 */
3773 val = (unsigned long long)(offset >> 16);
3774 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003775 case TEP_PRINT_DYNAMIC_ARRAY:
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003776 /* Without [], we pass the address to the dynamic data */
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04003777 offset = tep_read_number(pevent,
3778 data + arg->dynarray.field->offset,
3779 arg->dynarray.field->size);
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003780 /*
He Kuang76055942015-08-29 04:22:05 +00003781 * The total allocated length of the dynamic array is
3782 * stored in the top half of the field, and the offset
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003783 * is in the bottom half of the 32 bit field.
3784 */
3785 offset &= 0xffff;
Arnaldo Carvalho de Melo6b5fa0b2013-11-19 16:14:51 -03003786 val = (unsigned long long)((unsigned long)data + offset);
Steven Rostedt0497a9e2013-11-11 16:08:10 -05003787 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003788 default: /* not sure what to do there */
3789 return 0;
3790 }
3791 return val;
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003792
3793out_warning_op:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003794 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003795 return 0;
3796
3797out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09003798 do_warning_event(event, "%s: field %s not found",
3799 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003800 return 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003801}
3802
3803struct flag {
3804 const char *name;
3805 unsigned long long value;
3806};
3807
3808static const struct flag flags[] = {
3809 { "HI_SOFTIRQ", 0 },
3810 { "TIMER_SOFTIRQ", 1 },
3811 { "NET_TX_SOFTIRQ", 2 },
3812 { "NET_RX_SOFTIRQ", 3 },
3813 { "BLOCK_SOFTIRQ", 4 },
Christoph Hellwig511cbce2015-11-10 14:56:14 +01003814 { "IRQ_POLL_SOFTIRQ", 5 },
Steven Rostedtf7d82352012-04-06 00:47:53 +02003815 { "TASKLET_SOFTIRQ", 6 },
3816 { "SCHED_SOFTIRQ", 7 },
3817 { "HRTIMER_SOFTIRQ", 8 },
3818 { "RCU_SOFTIRQ", 9 },
3819
3820 { "HRTIMER_NORESTART", 0 },
3821 { "HRTIMER_RESTART", 1 },
3822};
3823
Steven Rostedt7c27f782015-03-24 14:58:13 -04003824static long long eval_flag(const char *flag)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003825{
3826 int i;
3827
3828 /*
3829 * Some flags in the format files do not get converted.
3830 * If the flag is not numeric, see if it is something that
3831 * we already know about.
3832 */
3833 if (isdigit(flag[0]))
3834 return strtoull(flag, NULL, 0);
3835
3836 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3837 if (strcmp(flags[i].name, flag) == 0)
3838 return flags[i].value;
3839
Steven Rostedt7c27f782015-03-24 14:58:13 -04003840 return -1LL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003841}
3842
3843static void print_str_to_seq(struct trace_seq *s, const char *format,
3844 int len_arg, const char *str)
3845{
3846 if (len_arg >= 0)
3847 trace_seq_printf(s, format, len_arg, str);
3848 else
3849 trace_seq_printf(s, format, str);
3850}
3851
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003852static void print_bitmask_to_seq(struct tep_handle *pevent,
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003853 struct trace_seq *s, const char *format,
3854 int len_arg, const void *data, int size)
3855{
3856 int nr_bits = size * 8;
3857 int str_size = (nr_bits + 3) / 4;
3858 int len = 0;
3859 char buf[3];
3860 char *str;
3861 int index;
3862 int i;
3863
3864 /*
3865 * The kernel likes to put in commas every 32 bits, we
3866 * can do the same.
3867 */
3868 str_size += (nr_bits - 1) / 32;
3869
3870 str = malloc(str_size + 1);
3871 if (!str) {
3872 do_warning("%s: not enough memory!", __func__);
3873 return;
3874 }
3875 str[str_size] = 0;
3876
3877 /* Start out with -2 for the two chars per byte */
3878 for (i = str_size - 2; i >= 0; i -= 2) {
3879 /*
3880 * data points to a bit mask of size bytes.
3881 * In the kernel, this is an array of long words, thus
Ingo Molnar3e449f72018-12-03 11:22:00 +01003882 * endianness is very important.
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04003883 */
3884 if (pevent->file_bigendian)
3885 index = size - (len + 1);
3886 else
3887 index = len;
3888
3889 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3890 memcpy(str + i, buf, 2);
3891 len++;
3892 if (!(len & 3) && i > 0) {
3893 i--;
3894 str[i] = ',';
3895 }
3896 }
3897
3898 if (len_arg >= 0)
3899 trace_seq_printf(s, format, len_arg, str);
3900 else
3901 trace_seq_printf(s, format, str);
3902
3903 free(str);
3904}
3905
Steven Rostedtf7d82352012-04-06 00:47:53 +02003906static void print_str_arg(struct trace_seq *s, void *data, int size,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05003907 struct tep_event *event, const char *format,
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04003908 int len_arg, struct tep_print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02003909{
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04003910 struct tep_handle *pevent = event->pevent;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04003911 struct tep_print_flag_sym *flag;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04003912 struct tep_format_field *field;
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003913 struct printk_map *printk;
Steven Rostedt7c27f782015-03-24 14:58:13 -04003914 long long val, fval;
Kapileshwar Singhc2e4b242015-09-22 14:22:03 +01003915 unsigned long long addr;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003916 char *str;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003917 unsigned char *hex;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003918 int print;
Namhyung Kime080e6f2012-06-27 09:41:41 +09003919 int i, len;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003920
3921 switch (arg->type) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003922 case TEP_PRINT_NULL:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003923 /* ?? */
3924 return;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003925 case TEP_PRINT_ATOM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003926 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3927 return;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003928 case TEP_PRINT_FIELD:
Namhyung Kimb7008072012-06-27 09:41:40 +09003929 field = arg->field.field;
3930 if (!field) {
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04003931 field = tep_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003932 if (!field) {
3933 str = arg->field.name;
3934 goto out_warning_field;
3935 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003936 arg->field.field = field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003937 }
3938 /* Zero sized fields, mean the rest of the data */
Namhyung Kimb7008072012-06-27 09:41:40 +09003939 len = field->size ? : size - field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02003940
3941 /*
3942 * Some events pass in pointers. If this is not an array
3943 * and the size is the same as long_size, assume that it
3944 * is a pointer.
3945 */
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04003946 if (!(field->flags & TEP_FIELD_IS_ARRAY) &&
Namhyung Kimb7008072012-06-27 09:41:40 +09003947 field->size == pevent->long_size) {
Kapileshwar Singhc2e4b242015-09-22 14:22:03 +01003948
3949 /* Handle heterogeneous recording and processing
3950 * architectures
3951 *
3952 * CASE I:
3953 * Traces recorded on 32-bit devices (32-bit
3954 * addressing) and processed on 64-bit devices:
3955 * In this case, only 32 bits should be read.
3956 *
3957 * CASE II:
3958 * Traces recorded on 64 bit devices and processed
3959 * on 32-bit devices:
3960 * In this case, 64 bits must be read.
3961 */
3962 addr = (pevent->long_size == 8) ?
3963 *(unsigned long long *)(data + field->offset) :
3964 (unsigned long long)*(unsigned int *)(data + field->offset);
3965
Steven Rostedt (Red Hat)0970b5f2013-11-01 17:53:55 -04003966 /* Check if it matches a print format */
3967 printk = find_printk(pevent, addr);
3968 if (printk)
3969 trace_seq_puts(s, printk->printk);
3970 else
Kapileshwar Singhc2e4b242015-09-22 14:22:03 +01003971 trace_seq_printf(s, "%llx", addr);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003972 break;
3973 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003974 str = malloc(len + 1);
3975 if (!str) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09003976 do_warning_event(event, "%s: not enough memory!",
3977 __func__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03003978 return;
3979 }
Namhyung Kimb7008072012-06-27 09:41:40 +09003980 memcpy(str, data + field->offset, len);
Steven Rostedtf7d82352012-04-06 00:47:53 +02003981 str[len] = 0;
3982 print_str_to_seq(s, format, len_arg, str);
3983 free(str);
3984 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04003985 case TEP_PRINT_FLAGS:
Steven Rostedtf7d82352012-04-06 00:47:53 +02003986 val = eval_num_arg(data, size, event, arg->flags.field);
3987 print = 0;
3988 for (flag = arg->flags.flags; flag; flag = flag->next) {
3989 fval = eval_flag(flag->value);
Steven Rostedt7c27f782015-03-24 14:58:13 -04003990 if (!val && fval < 0) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003991 print_str_to_seq(s, format, len_arg, flag->str);
3992 break;
3993 }
Steven Rostedt7c27f782015-03-24 14:58:13 -04003994 if (fval > 0 && (val & fval) == fval) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02003995 if (print && arg->flags.delim)
3996 trace_seq_puts(s, arg->flags.delim);
3997 print_str_to_seq(s, format, len_arg, flag->str);
3998 print = 1;
3999 val &= ~fval;
4000 }
4001 }
Steven Rostedt (VMware)3df76c92018-01-11 19:47:43 -05004002 if (val) {
4003 if (print && arg->flags.delim)
4004 trace_seq_puts(s, arg->flags.delim);
4005 trace_seq_printf(s, "0x%llx", val);
4006 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004007 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004008 case TEP_PRINT_SYMBOL:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004009 val = eval_num_arg(data, size, event, arg->symbol.field);
4010 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
4011 fval = eval_flag(flag->value);
4012 if (val == fval) {
4013 print_str_to_seq(s, format, len_arg, flag->str);
4014 break;
4015 }
4016 }
Jan Kiszkad6344472018-01-11 19:47:44 -05004017 if (!flag)
4018 trace_seq_printf(s, "0x%llx", val);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004019 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004020 case TEP_PRINT_HEX:
4021 case TEP_PRINT_HEX_STR:
4022 if (arg->hex.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
Howard Cochranb30f75e2013-11-01 17:53:56 -04004023 unsigned long offset;
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04004024 offset = tep_read_number(pevent,
Howard Cochranb30f75e2013-11-01 17:53:56 -04004025 data + arg->hex.field->dynarray.field->offset,
4026 arg->hex.field->dynarray.field->size);
4027 hex = data + (offset & 0xffff);
4028 } else {
4029 field = arg->hex.field->field.field;
4030 if (!field) {
4031 str = arg->hex.field->field.name;
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004032 field = tep_find_any_field(event, str);
Howard Cochranb30f75e2013-11-01 17:53:56 -04004033 if (!field)
4034 goto out_warning_field;
4035 arg->hex.field->field.field = field;
4036 }
4037 hex = data + field->offset;
Namhyung Kime080e6f2012-06-27 09:41:41 +09004038 }
Namhyung Kime080e6f2012-06-27 09:41:41 +09004039 len = eval_num_arg(data, size, event, arg->hex.size);
4040 for (i = 0; i < len; i++) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004041 if (i && arg->type == TEP_PRINT_HEX)
Namhyung Kime080e6f2012-06-27 09:41:41 +09004042 trace_seq_putc(s, ' ');
4043 trace_seq_printf(s, "%02x", hex[i]);
4044 }
4045 break;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004046
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004047 case TEP_PRINT_INT_ARRAY: {
Javi Merinob839e1e82015-03-24 11:07:19 +00004048 void *num;
4049 int el_size;
4050
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004051 if (arg->int_array.field->type == TEP_PRINT_DYNAMIC_ARRAY) {
Javi Merinob839e1e82015-03-24 11:07:19 +00004052 unsigned long offset;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04004053 struct tep_format_field *field =
Javi Merinob839e1e82015-03-24 11:07:19 +00004054 arg->int_array.field->dynarray.field;
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04004055 offset = tep_read_number(pevent,
4056 data + field->offset,
4057 field->size);
Javi Merinob839e1e82015-03-24 11:07:19 +00004058 num = data + (offset & 0xffff);
4059 } else {
4060 field = arg->int_array.field->field.field;
4061 if (!field) {
4062 str = arg->int_array.field->field.name;
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004063 field = tep_find_any_field(event, str);
Javi Merinob839e1e82015-03-24 11:07:19 +00004064 if (!field)
4065 goto out_warning_field;
4066 arg->int_array.field->field.field = field;
4067 }
4068 num = data + field->offset;
4069 }
4070 len = eval_num_arg(data, size, event, arg->int_array.count);
4071 el_size = eval_num_arg(data, size, event,
4072 arg->int_array.el_size);
4073 for (i = 0; i < len; i++) {
4074 if (i)
4075 trace_seq_putc(s, ' ');
4076
4077 if (el_size == 1) {
4078 trace_seq_printf(s, "%u", *(uint8_t *)num);
4079 } else if (el_size == 2) {
4080 trace_seq_printf(s, "%u", *(uint16_t *)num);
4081 } else if (el_size == 4) {
4082 trace_seq_printf(s, "%u", *(uint32_t *)num);
4083 } else if (el_size == 8) {
Namhyung Kim410ceb82015-04-24 10:45:16 +09004084 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
Javi Merinob839e1e82015-03-24 11:07:19 +00004085 } else {
4086 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4087 el_size, *(uint8_t *)num);
4088 el_size = 1;
4089 }
4090
4091 num += el_size;
4092 }
4093 break;
4094 }
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004095 case TEP_PRINT_TYPE:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004096 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004097 case TEP_PRINT_STRING: {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004098 int str_offset;
4099
4100 if (arg->string.offset == -1) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04004101 struct tep_format_field *f;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004102
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004103 f = tep_find_any_field(event, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004104 arg->string.offset = f->offset;
4105 }
Tzvetomir Stoyanov6cd99d22018-11-30 10:44:10 -05004106 str_offset = tep_data2host4(pevent, *(unsigned int *)(data + arg->string.offset));
Steven Rostedtf7d82352012-04-06 00:47:53 +02004107 str_offset &= 0xffff;
4108 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4109 break;
4110 }
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004111 case TEP_PRINT_BSTRING:
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004112 print_str_to_seq(s, format, len_arg, arg->string.string);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004113 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004114 case TEP_PRINT_BITMASK: {
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04004115 int bitmask_offset;
4116 int bitmask_size;
4117
4118 if (arg->bitmask.offset == -1) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04004119 struct tep_format_field *f;
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04004120
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004121 f = tep_find_any_field(event, arg->bitmask.bitmask);
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04004122 arg->bitmask.offset = f->offset;
4123 }
Tzvetomir Stoyanov6cd99d22018-11-30 10:44:10 -05004124 bitmask_offset = tep_data2host4(pevent, *(unsigned int *)(data + arg->bitmask.offset));
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04004125 bitmask_size = bitmask_offset >> 16;
4126 bitmask_offset &= 0xffff;
4127 print_bitmask_to_seq(pevent, s, format, len_arg,
4128 data + bitmask_offset, bitmask_size);
4129 break;
4130 }
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004131 case TEP_PRINT_OP:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004132 /*
4133 * The only op for string should be ? :
4134 */
4135 if (arg->op.op[0] != '?')
4136 return;
4137 val = eval_num_arg(data, size, event, arg->op.left);
4138 if (val)
4139 print_str_arg(s, data, size, event,
4140 format, len_arg, arg->op.right->op.left);
4141 else
4142 print_str_arg(s, data, size, event,
4143 format, len_arg, arg->op.right->op.right);
4144 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004145 case TEP_PRINT_FUNC:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004146 process_defined_func(s, data, size, event, arg);
4147 break;
4148 default:
4149 /* well... */
4150 break;
4151 }
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004152
4153 return;
4154
4155out_warning_field:
Namhyung Kim3388cc32014-03-19 10:22:53 +09004156 do_warning_event(event, "%s: field %s not found",
4157 __func__, arg->field.name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004158}
4159
4160static unsigned long long
4161process_defined_func(struct trace_seq *s, void *data, int size,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004162 struct tep_event *event, struct tep_print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004163{
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04004164 struct tep_function_handler *func_handle = arg->func.func;
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04004165 struct func_params *param;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004166 unsigned long long *args;
4167 unsigned long long ret;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004168 struct tep_print_arg *farg;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004169 struct trace_seq str;
4170 struct save_str {
4171 struct save_str *next;
4172 char *str;
4173 } *strings = NULL, *string;
4174 int i;
4175
4176 if (!func_handle->nr_args) {
4177 ret = (*func_handle->func)(s, NULL);
4178 goto out;
4179 }
4180
4181 farg = arg->func.args;
4182 param = func_handle->params;
4183
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004184 ret = ULLONG_MAX;
4185 args = malloc(sizeof(*args) * func_handle->nr_args);
4186 if (!args)
4187 goto out;
4188
Steven Rostedtf7d82352012-04-06 00:47:53 +02004189 for (i = 0; i < func_handle->nr_args; i++) {
4190 switch (param->type) {
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04004191 case TEP_FUNC_ARG_INT:
4192 case TEP_FUNC_ARG_LONG:
4193 case TEP_FUNC_ARG_PTR:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004194 args[i] = eval_num_arg(data, size, event, farg);
4195 break;
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04004196 case TEP_FUNC_ARG_STRING:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004197 trace_seq_init(&str);
4198 print_str_arg(&str, data, size, event, "%s", -1, farg);
4199 trace_seq_terminate(&str);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004200 string = malloc(sizeof(*string));
4201 if (!string) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004202 do_warning_event(event, "%s(%d): malloc str",
4203 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004204 goto out_free;
4205 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004206 string->next = strings;
4207 string->str = strdup(str.buffer);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004208 if (!string->str) {
4209 free(string);
Namhyung Kim3388cc32014-03-19 10:22:53 +09004210 do_warning_event(event, "%s(%d): malloc str",
4211 __func__, __LINE__);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004212 goto out_free;
4213 }
Robert Richter0cf26012012-08-07 19:43:14 +02004214 args[i] = (uintptr_t)string->str;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004215 strings = string;
4216 trace_seq_destroy(&str);
4217 break;
4218 default:
4219 /*
4220 * Something went totally wrong, this is not
4221 * an input error, something in this code broke.
4222 */
Namhyung Kim3388cc32014-03-19 10:22:53 +09004223 do_warning_event(event, "Unexpected end of arguments\n");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004224 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004225 }
4226 farg = farg->next;
Namhyung Kim21c69e722012-05-23 11:36:51 +09004227 param = param->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004228 }
4229
4230 ret = (*func_handle->func)(s, args);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004231out_free:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004232 free(args);
4233 while (strings) {
4234 string = strings;
4235 strings = string->next;
4236 free(string->str);
4237 free(string);
4238 }
4239
4240 out:
4241 /* TBD : handle return type here */
4242 return ret;
4243}
4244
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004245static void free_args(struct tep_print_arg *args)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004246{
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004247 struct tep_print_arg *next;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004248
4249 while (args) {
4250 next = args->next;
4251
4252 free_arg(args);
4253 args = next;
4254 }
4255}
4256
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004257static struct tep_print_arg *make_bprint_args(char *fmt, void *data, int size, struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004258{
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04004259 struct tep_handle *pevent = event->pevent;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04004260 struct tep_format_field *field, *ip_field;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004261 struct tep_print_arg *args, *arg, **next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004262 unsigned long long ip, val;
4263 char *ptr;
4264 void *bptr;
Adrian Hunter0631ca32018-11-22 13:29:37 +02004265 int vsize = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004266
4267 field = pevent->bprint_buf_field;
4268 ip_field = pevent->bprint_ip_field;
4269
4270 if (!field) {
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004271 field = tep_find_field(event, "buf");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004272 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004273 do_warning_event(event, "can't find buffer field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004274 return NULL;
4275 }
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004276 ip_field = tep_find_field(event, "ip");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004277 if (!ip_field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004278 do_warning_event(event, "can't find ip field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004279 return NULL;
4280 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004281 pevent->bprint_buf_field = field;
4282 pevent->bprint_ip_field = ip_field;
4283 }
4284
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04004285 ip = tep_read_number(pevent, data + ip_field->offset, ip_field->size);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004286
4287 /*
4288 * The first arg is the IP pointer.
4289 */
4290 args = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004291 if (!args) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004292 do_warning_event(event, "%s(%d): not enough memory!",
4293 __func__, __LINE__);
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004294 return NULL;
4295 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004296 arg = args;
4297 arg->next = NULL;
4298 next = &arg->next;
4299
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004300 arg->type = TEP_PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004301
4302 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4303 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004304
Scott Woodbbedb172015-03-11 22:13:57 -05004305 /* skip the first "%ps: " */
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004306 for (ptr = fmt + 5, bptr = data + field->offset;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004307 bptr < data + size && *ptr; ptr++) {
4308 int ls = 0;
4309
4310 if (*ptr == '%') {
4311 process_again:
4312 ptr++;
4313 switch (*ptr) {
4314 case '%':
4315 break;
4316 case 'l':
4317 ls++;
4318 goto process_again;
4319 case 'L':
4320 ls = 2;
4321 goto process_again;
4322 case '0' ... '9':
4323 goto process_again;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004324 case '.':
4325 goto process_again;
Steven Rostedt (Red Hat)55426292015-03-24 09:57:51 -04004326 case 'z':
4327 case 'Z':
4328 ls = 1;
4329 goto process_again;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004330 case 'p':
4331 ls = 1;
Steven Rostedt (VMware)37db96b2018-01-11 19:47:46 -05004332 if (isalnum(ptr[1])) {
4333 ptr++;
4334 /* Check for special pointers */
4335 switch (*ptr) {
4336 case 's':
4337 case 'S':
4338 case 'f':
4339 case 'F':
Steven Rostedt (VMware)328b82b2019-04-01 12:43:06 -04004340 case 'x':
Steven Rostedt (VMware)37db96b2018-01-11 19:47:46 -05004341 break;
4342 default:
4343 /*
4344 * Older kernels do not process
4345 * dereferenced pointers.
4346 * Only process if the pointer
4347 * value is a printable.
4348 */
4349 if (isprint(*(char *)bptr))
4350 goto process_string;
4351 }
4352 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004353 /* fall through */
4354 case 'd':
4355 case 'u':
4356 case 'x':
4357 case 'i':
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004358 switch (ls) {
4359 case 0:
4360 vsize = 4;
4361 break;
4362 case 1:
4363 vsize = pevent->long_size;
4364 break;
4365 case 2:
4366 vsize = 8;
Peter Huewec9bbabe2012-04-24 23:19:40 +02004367 break;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004368 default:
4369 vsize = ls; /* ? */
4370 break;
4371 }
4372 /* fall through */
4373 case '*':
4374 if (*ptr == '*')
4375 vsize = 4;
4376
Steven Rostedtf7d82352012-04-06 00:47:53 +02004377 /* the pointers are always 4 bytes aligned */
4378 bptr = (void *)(((unsigned long)bptr + 3) &
4379 ~3);
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04004380 val = tep_read_number(pevent, bptr, vsize);
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004381 bptr += vsize;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004382 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004383 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004384 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004385 __func__, __LINE__);
4386 goto out_free;
4387 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004388 arg->next = NULL;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004389 arg->type = TEP_PRINT_ATOM;
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004390 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4391 free(arg);
4392 goto out_free;
4393 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004394 *next = arg;
4395 next = &arg->next;
Steven Rostedtc2e6dc22011-11-15 18:47:48 -05004396 /*
4397 * The '*' case means that an arg is used as the length.
4398 * We need to continue to figure out for what.
4399 */
4400 if (*ptr == '*')
4401 goto process_again;
4402
Steven Rostedtf7d82352012-04-06 00:47:53 +02004403 break;
4404 case 's':
Steven Rostedt (VMware)37db96b2018-01-11 19:47:46 -05004405 process_string:
Steven Rostedtf7d82352012-04-06 00:47:53 +02004406 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004407 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004408 do_warning_event(event, "%s(%d): not enough memory!",
Namhyung Kimb1ac7542012-09-20 11:09:19 +09004409 __func__, __LINE__);
4410 goto out_free;
4411 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004412 arg->next = NULL;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004413 arg->type = TEP_PRINT_BSTRING;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004414 arg->string.string = strdup(bptr);
Namhyung Kimca638582012-04-09 11:54:31 +09004415 if (!arg->string.string)
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004416 goto out_free;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004417 bptr += strlen(bptr) + 1;
4418 *next = arg;
4419 next = &arg->next;
4420 default:
4421 break;
4422 }
4423 }
4424 }
4425
4426 return args;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004427
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004428out_free:
4429 free_args(args);
4430 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004431}
4432
4433static char *
Irina Tirdea1d037ca2012-09-11 01:15:03 +03004434get_bprint_format(void *data, int size __maybe_unused,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004435 struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004436{
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04004437 struct tep_handle *pevent = event->pevent;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004438 unsigned long long addr;
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04004439 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004440 struct printk_map *printk;
4441 char *format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004442
4443 field = pevent->bprint_fmt_field;
4444
4445 if (!field) {
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004446 field = tep_find_field(event, "fmt");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004447 if (!field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004448 do_warning_event(event, "can't find format field for binary printk");
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004449 return NULL;
4450 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004451 pevent->bprint_fmt_field = field;
4452 }
4453
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04004454 addr = tep_read_number(pevent, data + field->offset, field->size);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004455
4456 printk = find_printk(pevent, addr);
4457 if (!printk) {
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004458 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004459 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004460 return format;
4461 }
4462
Steven Rostedt (Red Hat)0883d9d2013-11-01 17:53:57 -04004463 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
Arnaldo Carvalho de Melo0dbca1e2012-09-12 15:39:59 -03004464 return NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004465
4466 return format;
4467}
4468
4469static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004470 struct tep_event *event, struct tep_print_arg *arg)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004471{
4472 unsigned char *buf;
Arnaldo Carvalho de Melo27f94d52012-11-09 17:40:47 -03004473 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
Steven Rostedtf7d82352012-04-06 00:47:53 +02004474
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004475 if (arg->type == TEP_PRINT_FUNC) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004476 process_defined_func(s, data, size, event, arg);
4477 return;
4478 }
4479
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004480 if (arg->type != TEP_PRINT_FIELD) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004481 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4482 arg->type);
4483 return;
4484 }
4485
4486 if (mac == 'm')
4487 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4488 if (!arg->field.field) {
4489 arg->field.field =
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004490 tep_find_any_field(event, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004491 if (!arg->field.field) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004492 do_warning_event(event, "%s: field %s not found",
4493 __func__, arg->field.name);
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03004494 return;
4495 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004496 }
4497 if (arg->field.field->size != 6) {
4498 trace_seq_printf(s, "INVALIDMAC");
4499 return;
4500 }
4501 buf = data + arg->field.field->offset;
4502 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4503}
4504
David Ahern3d199b52014-12-18 19:11:11 -07004505static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4506{
4507 const char *fmt;
4508
4509 if (i == 'i')
4510 fmt = "%03d.%03d.%03d.%03d";
4511 else
4512 fmt = "%d.%d.%d.%d";
4513
4514 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4515}
4516
4517static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4518{
4519 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4520 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4521}
4522
4523static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4524{
4525 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4526}
4527
4528static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4529{
4530 int i, j, range;
4531 unsigned char zerolength[8];
4532 int longest = 1;
4533 int colonpos = -1;
4534 uint16_t word;
4535 uint8_t hi, lo;
4536 bool needcolon = false;
4537 bool useIPv4;
4538 struct in6_addr in6;
4539
4540 memcpy(&in6, addr, sizeof(struct in6_addr));
4541
4542 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4543
4544 memset(zerolength, 0, sizeof(zerolength));
4545
4546 if (useIPv4)
4547 range = 6;
4548 else
4549 range = 8;
4550
4551 /* find position of longest 0 run */
4552 for (i = 0; i < range; i++) {
4553 for (j = i; j < range; j++) {
4554 if (in6.s6_addr16[j] != 0)
4555 break;
4556 zerolength[i]++;
4557 }
4558 }
4559 for (i = 0; i < range; i++) {
4560 if (zerolength[i] > longest) {
4561 longest = zerolength[i];
4562 colonpos = i;
4563 }
4564 }
4565 if (longest == 1) /* don't compress a single 0 */
4566 colonpos = -1;
4567
4568 /* emit address */
4569 for (i = 0; i < range; i++) {
4570 if (i == colonpos) {
4571 if (needcolon || i == 0)
4572 trace_seq_printf(s, ":");
4573 trace_seq_printf(s, ":");
4574 needcolon = false;
4575 i += longest - 1;
4576 continue;
4577 }
4578 if (needcolon) {
4579 trace_seq_printf(s, ":");
4580 needcolon = false;
4581 }
4582 /* hex u16 without leading 0s */
4583 word = ntohs(in6.s6_addr16[i]);
4584 hi = word >> 8;
4585 lo = word & 0xff;
4586 if (hi)
4587 trace_seq_printf(s, "%x%02x", hi, lo);
4588 else
4589 trace_seq_printf(s, "%x", lo);
4590
4591 needcolon = true;
4592 }
4593
4594 if (useIPv4) {
4595 if (needcolon)
4596 trace_seq_printf(s, ":");
4597 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4598 }
4599
4600 return;
4601}
4602
4603static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4604{
4605 int j;
4606
4607 for (j = 0; j < 16; j += 2) {
4608 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4609 if (i == 'I' && j < 14)
4610 trace_seq_printf(s, ":");
4611 }
4612}
4613
4614/*
4615 * %pi4 print an IPv4 address with leading zeros
4616 * %pI4 print an IPv4 address without leading zeros
4617 * %pi6 print an IPv6 address without colons
4618 * %pI6 print an IPv6 address with colons
4619 * %pI6c print an IPv6 address in compressed form with colons
4620 * %pISpc print an IP address based on sockaddr; p adds port.
4621 */
4622static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004623 void *data, int size, struct tep_event *event,
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004624 struct tep_print_arg *arg)
David Ahern3d199b52014-12-18 19:11:11 -07004625{
4626 unsigned char *buf;
4627
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004628 if (arg->type == TEP_PRINT_FUNC) {
David Ahern3d199b52014-12-18 19:11:11 -07004629 process_defined_func(s, data, size, event, arg);
4630 return 0;
4631 }
4632
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004633 if (arg->type != TEP_PRINT_FIELD) {
David Ahern3d199b52014-12-18 19:11:11 -07004634 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4635 return 0;
4636 }
4637
4638 if (!arg->field.field) {
4639 arg->field.field =
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004640 tep_find_any_field(event, arg->field.name);
David Ahern3d199b52014-12-18 19:11:11 -07004641 if (!arg->field.field) {
4642 do_warning("%s: field %s not found",
4643 __func__, arg->field.name);
4644 return 0;
4645 }
4646 }
4647
4648 buf = data + arg->field.field->offset;
4649
4650 if (arg->field.field->size != 4) {
4651 trace_seq_printf(s, "INVALIDIPv4");
4652 return 0;
4653 }
4654 print_ip4_addr(s, i, buf);
4655
4656 return 0;
4657}
4658
4659static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004660 void *data, int size, struct tep_event *event,
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004661 struct tep_print_arg *arg)
David Ahern3d199b52014-12-18 19:11:11 -07004662{
4663 char have_c = 0;
4664 unsigned char *buf;
4665 int rc = 0;
4666
4667 /* pI6c */
4668 if (i == 'I' && *ptr == 'c') {
4669 have_c = 1;
4670 ptr++;
4671 rc++;
4672 }
4673
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004674 if (arg->type == TEP_PRINT_FUNC) {
David Ahern3d199b52014-12-18 19:11:11 -07004675 process_defined_func(s, data, size, event, arg);
4676 return rc;
4677 }
4678
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004679 if (arg->type != TEP_PRINT_FIELD) {
David Ahern3d199b52014-12-18 19:11:11 -07004680 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4681 return rc;
4682 }
4683
4684 if (!arg->field.field) {
4685 arg->field.field =
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004686 tep_find_any_field(event, arg->field.name);
David Ahern3d199b52014-12-18 19:11:11 -07004687 if (!arg->field.field) {
4688 do_warning("%s: field %s not found",
4689 __func__, arg->field.name);
4690 return rc;
4691 }
4692 }
4693
4694 buf = data + arg->field.field->offset;
4695
4696 if (arg->field.field->size != 16) {
4697 trace_seq_printf(s, "INVALIDIPv6");
4698 return rc;
4699 }
4700
4701 if (have_c)
4702 print_ip6c_addr(s, buf);
4703 else
4704 print_ip6_addr(s, i, buf);
4705
4706 return rc;
4707}
4708
4709static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004710 void *data, int size, struct tep_event *event,
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004711 struct tep_print_arg *arg)
David Ahern3d199b52014-12-18 19:11:11 -07004712{
4713 char have_c = 0, have_p = 0;
4714 unsigned char *buf;
4715 struct sockaddr_storage *sa;
4716 int rc = 0;
4717
4718 /* pISpc */
4719 if (i == 'I') {
4720 if (*ptr == 'p') {
4721 have_p = 1;
4722 ptr++;
4723 rc++;
4724 }
4725 if (*ptr == 'c') {
4726 have_c = 1;
4727 ptr++;
4728 rc++;
4729 }
4730 }
4731
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004732 if (arg->type == TEP_PRINT_FUNC) {
David Ahern3d199b52014-12-18 19:11:11 -07004733 process_defined_func(s, data, size, event, arg);
4734 return rc;
4735 }
4736
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04004737 if (arg->type != TEP_PRINT_FIELD) {
David Ahern3d199b52014-12-18 19:11:11 -07004738 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4739 return rc;
4740 }
4741
4742 if (!arg->field.field) {
4743 arg->field.field =
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04004744 tep_find_any_field(event, arg->field.name);
David Ahern3d199b52014-12-18 19:11:11 -07004745 if (!arg->field.field) {
4746 do_warning("%s: field %s not found",
4747 __func__, arg->field.name);
4748 return rc;
4749 }
4750 }
4751
4752 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4753
4754 if (sa->ss_family == AF_INET) {
4755 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4756
4757 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4758 trace_seq_printf(s, "INVALIDIPv4");
4759 return rc;
4760 }
4761
4762 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4763 if (have_p)
4764 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4765
4766
4767 } else if (sa->ss_family == AF_INET6) {
4768 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4769
4770 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4771 trace_seq_printf(s, "INVALIDIPv6");
4772 return rc;
4773 }
4774
4775 if (have_p)
4776 trace_seq_printf(s, "[");
4777
4778 buf = (unsigned char *) &sa6->sin6_addr;
4779 if (have_c)
4780 print_ip6c_addr(s, buf);
4781 else
4782 print_ip6_addr(s, i, buf);
4783
4784 if (have_p)
4785 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4786 }
4787
4788 return rc;
4789}
4790
4791static int print_ip_arg(struct trace_seq *s, const char *ptr,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004792 void *data, int size, struct tep_event *event,
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004793 struct tep_print_arg *arg)
David Ahern3d199b52014-12-18 19:11:11 -07004794{
4795 char i = *ptr; /* 'i' or 'I' */
4796 char ver;
4797 int rc = 0;
4798
4799 ptr++;
4800 rc++;
4801
4802 ver = *ptr;
4803 ptr++;
4804 rc++;
4805
4806 switch (ver) {
4807 case '4':
4808 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4809 break;
4810 case '6':
4811 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4812 break;
4813 case 'S':
4814 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4815 break;
4816 default:
4817 return 0;
4818 }
4819
4820 return rc;
4821}
4822
Namhyung Kim600da3c2012-06-22 17:10:15 +09004823static int is_printable_array(char *p, unsigned int len)
4824{
4825 unsigned int i;
4826
4827 for (i = 0; i < len && p[i]; i++)
Steven Rostedt (Red Hat)5efb9fb2013-11-01 17:53:58 -04004828 if (!isprint(p[i]) && !isspace(p[i]))
Namhyung Kim600da3c2012-06-22 17:10:15 +09004829 return 0;
4830 return 1;
4831}
4832
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04004833void tep_print_field(struct trace_seq *s, void *data,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04004834 struct tep_format_field *field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004835{
Steven Rostedtf7d82352012-04-06 00:47:53 +02004836 unsigned long long val;
4837 unsigned int offset, len, i;
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04004838 struct tep_handle *pevent = field->event->pevent;
Namhyung Kimbe45d402015-12-23 22:08:41 +09004839
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004840 if (field->flags & TEP_FIELD_IS_ARRAY) {
Namhyung Kimbe45d402015-12-23 22:08:41 +09004841 offset = field->offset;
4842 len = field->size;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004843 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04004844 val = tep_read_number(pevent, data + offset, len);
Namhyung Kimbe45d402015-12-23 22:08:41 +09004845 offset = val;
4846 len = offset >> 16;
4847 offset &= 0xffff;
4848 }
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004849 if (field->flags & TEP_FIELD_IS_STRING &&
Namhyung Kimbe45d402015-12-23 22:08:41 +09004850 is_printable_array(data + offset, len)) {
4851 trace_seq_printf(s, "%s", (char *)data + offset);
4852 } else {
4853 trace_seq_puts(s, "ARRAY[");
4854 for (i = 0; i < len; i++) {
4855 if (i)
4856 trace_seq_puts(s, ", ");
4857 trace_seq_printf(s, "%02x",
4858 *((unsigned char *)data + offset + i));
4859 }
4860 trace_seq_putc(s, ']');
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004861 field->flags &= ~TEP_FIELD_IS_STRING;
Namhyung Kimbe45d402015-12-23 22:08:41 +09004862 }
4863 } else {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04004864 val = tep_read_number(pevent, data + field->offset,
4865 field->size);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004866 if (field->flags & TEP_FIELD_IS_POINTER) {
Namhyung Kimbe45d402015-12-23 22:08:41 +09004867 trace_seq_printf(s, "0x%llx", val);
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004868 } else if (field->flags & TEP_FIELD_IS_SIGNED) {
Namhyung Kimbe45d402015-12-23 22:08:41 +09004869 switch (field->size) {
4870 case 4:
4871 /*
4872 * If field is long then print it in hex.
4873 * A long usually stores pointers.
4874 */
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004875 if (field->flags & TEP_FIELD_IS_LONG)
Namhyung Kimbe45d402015-12-23 22:08:41 +09004876 trace_seq_printf(s, "0x%x", (int)val);
4877 else
4878 trace_seq_printf(s, "%d", (int)val);
4879 break;
4880 case 2:
4881 trace_seq_printf(s, "%2d", (short)val);
4882 break;
4883 case 1:
4884 trace_seq_printf(s, "%1d", (char)val);
4885 break;
4886 default:
4887 trace_seq_printf(s, "%lld", val);
4888 }
4889 } else {
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04004890 if (field->flags & TEP_FIELD_IS_LONG)
Namhyung Kimbe45d402015-12-23 22:08:41 +09004891 trace_seq_printf(s, "0x%llx", val);
4892 else
4893 trace_seq_printf(s, "%llu", val);
4894 }
4895 }
4896}
4897
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04004898void tep_print_fields(struct trace_seq *s, void *data,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004899 int size __maybe_unused, struct tep_event *event)
Namhyung Kimbe45d402015-12-23 22:08:41 +09004900{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04004901 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004902
4903 field = event->format.fields;
4904 while (field) {
4905 trace_seq_printf(s, " %s=", field->name);
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04004906 tep_print_field(s, data, field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004907 field = field->next;
4908 }
4909}
4910
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05004911static void pretty_print(struct trace_seq *s, void *data, int size, struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02004912{
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04004913 struct tep_handle *pevent = event->pevent;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04004914 struct tep_print_fmt *print_fmt = &event->print_fmt;
4915 struct tep_print_arg *arg = print_fmt->args;
4916 struct tep_print_arg *args = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004917 const char *ptr = print_fmt->format;
4918 unsigned long long val;
4919 struct func_map *func;
4920 const char *saveptr;
Steven Rostedt12e55562013-11-19 18:29:37 -05004921 struct trace_seq p;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004922 char *bprint_fmt = NULL;
4923 char format[32];
4924 int show_func;
4925 int len_as_arg;
Adrian Hunter0631ca32018-11-22 13:29:37 +02004926 int len_arg = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02004927 int len;
4928 int ls;
4929
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04004930 if (event->flags & TEP_EVENT_FL_FAILED) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004931 trace_seq_printf(s, "[FAILED TO PARSE]");
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04004932 tep_print_fields(s, data, size, event);
Steven Rostedtf7d82352012-04-06 00:47:53 +02004933 return;
4934 }
4935
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04004936 if (event->flags & TEP_EVENT_FL_ISBPRINT) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02004937 bprint_fmt = get_bprint_format(data, size, event);
4938 args = make_bprint_args(bprint_fmt, data, size, event);
4939 arg = args;
4940 ptr = bprint_fmt;
4941 }
4942
4943 for (; *ptr; ptr++) {
4944 ls = 0;
4945 if (*ptr == '\\') {
4946 ptr++;
4947 switch (*ptr) {
4948 case 'n':
4949 trace_seq_putc(s, '\n');
4950 break;
4951 case 't':
4952 trace_seq_putc(s, '\t');
4953 break;
4954 case 'r':
4955 trace_seq_putc(s, '\r');
4956 break;
4957 case '\\':
4958 trace_seq_putc(s, '\\');
4959 break;
4960 default:
4961 trace_seq_putc(s, *ptr);
4962 break;
4963 }
4964
4965 } else if (*ptr == '%') {
4966 saveptr = ptr;
4967 show_func = 0;
4968 len_as_arg = 0;
4969 cont_process:
4970 ptr++;
4971 switch (*ptr) {
4972 case '%':
4973 trace_seq_putc(s, '%');
4974 break;
4975 case '#':
4976 /* FIXME: need to handle properly */
4977 goto cont_process;
4978 case 'h':
4979 ls--;
4980 goto cont_process;
4981 case 'l':
4982 ls++;
4983 goto cont_process;
4984 case 'L':
4985 ls = 2;
4986 goto cont_process;
4987 case '*':
4988 /* The argument is the length. */
Namhyung Kim245c5a12012-09-07 11:49:45 +09004989 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09004990 do_warning_event(event, "no argument match");
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04004991 event->flags |= TEP_EVENT_FL_FAILED;
Namhyung Kim245c5a12012-09-07 11:49:45 +09004992 goto out_failed;
4993 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02004994 len_arg = eval_num_arg(data, size, event, arg);
4995 len_as_arg = 1;
4996 arg = arg->next;
4997 goto cont_process;
4998 case '.':
4999 case 'z':
5000 case 'Z':
5001 case '0' ... '9':
Steven Rostedt1d945012015-08-27 09:46:01 -04005002 case '-':
Steven Rostedtf7d82352012-04-06 00:47:53 +02005003 goto cont_process;
5004 case 'p':
5005 if (pevent->long_size == 4)
5006 ls = 1;
5007 else
5008 ls = 2;
5009
Steven Rostedt (VMware)38d70b72018-01-11 19:47:45 -05005010 if (isalnum(ptr[1]))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005011 ptr++;
Steven Rostedt (VMware)38d70b72018-01-11 19:47:45 -05005012
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005013 if (arg->type == TEP_PRINT_BSTRING) {
Steven Rostedt (VMware)37db96b2018-01-11 19:47:46 -05005014 trace_seq_puts(s, arg->string.string);
Steven Rostedt (VMware)f024cf02018-12-10 13:45:22 -05005015 arg = arg->next;
Steven Rostedt (VMware)37db96b2018-01-11 19:47:46 -05005016 break;
5017 }
5018
Steven Rostedt (VMware)38d70b72018-01-11 19:47:45 -05005019 if (*ptr == 'F' || *ptr == 'f' ||
5020 *ptr == 'S' || *ptr == 's') {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005021 show_func = *ptr;
Steven Rostedt (VMware)38d70b72018-01-11 19:47:45 -05005022 } else if (*ptr == 'M' || *ptr == 'm') {
5023 print_mac_arg(s, *ptr, data, size, event, arg);
Steven Rostedtaaf05c72012-01-09 15:58:09 -05005024 arg = arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005025 break;
Steven Rostedt (VMware)38d70b72018-01-11 19:47:45 -05005026 } else if (*ptr == 'I' || *ptr == 'i') {
David Ahern3d199b52014-12-18 19:11:11 -07005027 int n;
5028
Steven Rostedt (VMware)38d70b72018-01-11 19:47:45 -05005029 n = print_ip_arg(s, ptr, data, size, event, arg);
David Ahern3d199b52014-12-18 19:11:11 -07005030 if (n > 0) {
Steven Rostedt (VMware)38d70b72018-01-11 19:47:45 -05005031 ptr += n - 1;
David Ahern3d199b52014-12-18 19:11:11 -07005032 arg = arg->next;
5033 break;
5034 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005035 }
5036
5037 /* fall through */
5038 case 'd':
5039 case 'i':
5040 case 'x':
5041 case 'X':
5042 case 'u':
Namhyung Kim245c5a12012-09-07 11:49:45 +09005043 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09005044 do_warning_event(event, "no argument match");
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005045 event->flags |= TEP_EVENT_FL_FAILED;
Namhyung Kim245c5a12012-09-07 11:49:45 +09005046 goto out_failed;
5047 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005048
5049 len = ((unsigned long)ptr + 1) -
5050 (unsigned long)saveptr;
5051
5052 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09005053 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09005054 do_warning_event(event, "bad format!");
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005055 event->flags |= TEP_EVENT_FL_FAILED;
Namhyung Kim245c5a12012-09-07 11:49:45 +09005056 len = 31;
5057 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005058
5059 memcpy(format, saveptr, len);
5060 format[len] = 0;
5061
5062 val = eval_num_arg(data, size, event, arg);
5063 arg = arg->next;
5064
5065 if (show_func) {
5066 func = find_func(pevent, val);
5067 if (func) {
5068 trace_seq_puts(s, func->func);
5069 if (show_func == 'F')
5070 trace_seq_printf(s,
5071 "+0x%llx",
5072 val - func->addr);
5073 break;
5074 }
5075 }
Steven Rostedt (Red Hat)a66673a2016-02-09 15:40:17 -05005076 if (pevent->long_size == 8 && ls == 1 &&
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01005077 sizeof(long) != 8) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005078 char *p;
5079
Steven Rostedtf7d82352012-04-06 00:47:53 +02005080 /* make %l into %ll */
Steven Rostedt32abc2e2015-11-16 17:25:16 -05005081 if (ls == 1 && (p = strchr(format, 'l')))
Wolfgang Mauererc5b35b72012-03-22 11:18:21 +01005082 memmove(p+1, p, strlen(p)+1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005083 else if (strcmp(format, "%p") == 0)
5084 strcpy(format, "0x%llx");
Steven Rostedt32abc2e2015-11-16 17:25:16 -05005085 ls = 2;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005086 }
5087 switch (ls) {
5088 case -2:
5089 if (len_as_arg)
5090 trace_seq_printf(s, format, len_arg, (char)val);
5091 else
5092 trace_seq_printf(s, format, (char)val);
5093 break;
5094 case -1:
5095 if (len_as_arg)
5096 trace_seq_printf(s, format, len_arg, (short)val);
5097 else
5098 trace_seq_printf(s, format, (short)val);
5099 break;
5100 case 0:
5101 if (len_as_arg)
5102 trace_seq_printf(s, format, len_arg, (int)val);
5103 else
5104 trace_seq_printf(s, format, (int)val);
5105 break;
5106 case 1:
5107 if (len_as_arg)
5108 trace_seq_printf(s, format, len_arg, (long)val);
5109 else
5110 trace_seq_printf(s, format, (long)val);
5111 break;
5112 case 2:
5113 if (len_as_arg)
5114 trace_seq_printf(s, format, len_arg,
5115 (long long)val);
5116 else
5117 trace_seq_printf(s, format, (long long)val);
5118 break;
5119 default:
Namhyung Kim3388cc32014-03-19 10:22:53 +09005120 do_warning_event(event, "bad count (%d)", ls);
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005121 event->flags |= TEP_EVENT_FL_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005122 }
5123 break;
5124 case 's':
Namhyung Kim245c5a12012-09-07 11:49:45 +09005125 if (!arg) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09005126 do_warning_event(event, "no matching argument");
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005127 event->flags |= TEP_EVENT_FL_FAILED;
Namhyung Kim245c5a12012-09-07 11:49:45 +09005128 goto out_failed;
5129 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005130
5131 len = ((unsigned long)ptr + 1) -
5132 (unsigned long)saveptr;
5133
5134 /* should never happen */
Namhyung Kim245c5a12012-09-07 11:49:45 +09005135 if (len > 31) {
Namhyung Kim3388cc32014-03-19 10:22:53 +09005136 do_warning_event(event, "bad format!");
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005137 event->flags |= TEP_EVENT_FL_FAILED;
Namhyung Kim245c5a12012-09-07 11:49:45 +09005138 len = 31;
5139 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005140
5141 memcpy(format, saveptr, len);
5142 format[len] = 0;
5143 if (!len_as_arg)
5144 len_arg = -1;
Steven Rostedt12e55562013-11-19 18:29:37 -05005145 /* Use helper trace_seq */
5146 trace_seq_init(&p);
5147 print_str_arg(&p, data, size, event,
Steven Rostedtf7d82352012-04-06 00:47:53 +02005148 format, len_arg, arg);
Steven Rostedt12e55562013-11-19 18:29:37 -05005149 trace_seq_terminate(&p);
5150 trace_seq_puts(s, p.buffer);
Steven Rostedtde04f862014-04-22 19:23:30 -04005151 trace_seq_destroy(&p);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005152 arg = arg->next;
5153 break;
5154 default:
5155 trace_seq_printf(s, ">%c<", *ptr);
5156
5157 }
5158 } else
5159 trace_seq_putc(s, *ptr);
5160 }
5161
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005162 if (event->flags & TEP_EVENT_FL_FAILED) {
Namhyung Kim245c5a12012-09-07 11:49:45 +09005163out_failed:
5164 trace_seq_printf(s, "[FAILED TO PARSE]");
5165 }
5166
Steven Rostedtf7d82352012-04-06 00:47:53 +02005167 if (args) {
5168 free_args(args);
5169 free(bprint_fmt);
5170 }
5171}
5172
5173/**
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -04005174 * tep_data_latency_format - parse the data for the latency format
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005175 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02005176 * @s: the trace_seq to write to
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005177 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02005178 *
5179 * This parses out the Latency format (interrupts disabled,
5180 * need rescheduling, in hard/soft interrupt, preempt count
5181 * and lock depth) and places it into the trace_seq.
5182 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005183void tep_data_latency_format(struct tep_handle *tep,
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -04005184 struct trace_seq *s, struct tep_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005185{
5186 static int check_lock_depth = 1;
Steven Rostedt0866a972012-05-22 14:52:40 +09005187 static int check_migrate_disable = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005188 static int lock_depth_exists;
Steven Rostedt0866a972012-05-22 14:52:40 +09005189 static int migrate_disable_exists;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005190 unsigned int lat_flags;
5191 unsigned int pc;
Adrian Hunter0631ca32018-11-22 13:29:37 +02005192 int lock_depth = 0;
5193 int migrate_disable = 0;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005194 int hardirq;
5195 int softirq;
5196 void *data = record->data;
5197
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005198 lat_flags = parse_common_flags(tep, data);
5199 pc = parse_common_pc(tep, data);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005200 /* lock_depth may not always exist */
Steven Rostedtf7d82352012-04-06 00:47:53 +02005201 if (lock_depth_exists)
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005202 lock_depth = parse_common_lock_depth(tep, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09005203 else if (check_lock_depth) {
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005204 lock_depth = parse_common_lock_depth(tep, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09005205 if (lock_depth < 0)
5206 check_lock_depth = 0;
5207 else
5208 lock_depth_exists = 1;
5209 }
5210
5211 /* migrate_disable may not always exist */
5212 if (migrate_disable_exists)
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005213 migrate_disable = parse_common_migrate_disable(tep, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09005214 else if (check_migrate_disable) {
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005215 migrate_disable = parse_common_migrate_disable(tep, data);
Steven Rostedt0866a972012-05-22 14:52:40 +09005216 if (migrate_disable < 0)
5217 check_migrate_disable = 0;
5218 else
5219 migrate_disable_exists = 1;
5220 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005221
5222 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5223 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5224
5225 trace_seq_printf(s, "%c%c%c",
5226 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5227 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5228 'X' : '.',
5229 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5230 'N' : '.',
5231 (hardirq && softirq) ? 'H' :
5232 hardirq ? 'h' : softirq ? 's' : '.');
5233
5234 if (pc)
5235 trace_seq_printf(s, "%x", pc);
5236 else
5237 trace_seq_putc(s, '.');
5238
Steven Rostedt0866a972012-05-22 14:52:40 +09005239 if (migrate_disable_exists) {
5240 if (migrate_disable < 0)
5241 trace_seq_putc(s, '.');
5242 else
5243 trace_seq_printf(s, "%d", migrate_disable);
5244 }
5245
Steven Rostedtf7d82352012-04-06 00:47:53 +02005246 if (lock_depth_exists) {
5247 if (lock_depth < 0)
5248 trace_seq_putc(s, '.');
5249 else
5250 trace_seq_printf(s, "%d", lock_depth);
5251 }
5252
5253 trace_seq_terminate(s);
5254}
5255
5256/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005257 * tep_data_type - parse out the given event type
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005258 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02005259 * @rec: the record to read from
5260 *
5261 * This returns the event id from the @rec.
5262 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005263int tep_data_type(struct tep_handle *tep, struct tep_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005264{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005265 return trace_parse_common_type(tep, rec->data);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005266}
5267
5268/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005269 * tep_data_pid - parse the PID from record
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005270 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02005271 * @rec: the record to parse
5272 *
Steven Rostedtc52d9e42016-11-22 11:31:58 -05005273 * This returns the PID from a record.
Steven Rostedtf7d82352012-04-06 00:47:53 +02005274 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005275int tep_data_pid(struct tep_handle *tep, struct tep_record *rec)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005276{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005277 return parse_common_pid(tep, rec->data);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005278}
5279
5280/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005281 * tep_data_preempt_count - parse the preempt count from the record
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005282 * @tep: a handle to the trace event parser context
Steven Rostedtc52d9e42016-11-22 11:31:58 -05005283 * @rec: the record to parse
5284 *
5285 * This returns the preempt count from a record.
5286 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005287int tep_data_preempt_count(struct tep_handle *tep, struct tep_record *rec)
Steven Rostedtc52d9e42016-11-22 11:31:58 -05005288{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005289 return parse_common_pc(tep, rec->data);
Steven Rostedtc52d9e42016-11-22 11:31:58 -05005290}
5291
5292/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005293 * tep_data_flags - parse the latency flags from the record
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005294 * @tep: a handle to the trace event parser context
Steven Rostedtc52d9e42016-11-22 11:31:58 -05005295 * @rec: the record to parse
5296 *
5297 * This returns the latency flags from a record.
5298 *
5299 * Use trace_flag_type enum for the flags (see event-parse.h).
5300 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005301int tep_data_flags(struct tep_handle *tep, struct tep_record *rec)
Steven Rostedtc52d9e42016-11-22 11:31:58 -05005302{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005303 return parse_common_flags(tep, rec->data);
Steven Rostedtc52d9e42016-11-22 11:31:58 -05005304}
5305
5306/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005307 * tep_data_comm_from_pid - return the command line from PID
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005308 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02005309 * @pid: the PID of the task to search for
5310 *
5311 * This returns a pointer to the command line that has the given
5312 * @pid.
5313 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005314const char *tep_data_comm_from_pid(struct tep_handle *tep, int pid)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005315{
5316 const char *comm;
5317
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005318 comm = find_cmdline(tep, pid);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005319 return comm;
5320}
5321
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -05005322static struct tep_cmdline *
5323pid_from_cmdlist(struct tep_handle *pevent, const char *comm, struct tep_cmdline *next)
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005324{
5325 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5326
5327 if (cmdlist)
5328 cmdlist = cmdlist->next;
5329 else
5330 cmdlist = pevent->cmdlist;
5331
5332 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5333 cmdlist = cmdlist->next;
5334
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -05005335 return (struct tep_cmdline *)cmdlist;
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005336}
5337
5338/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005339 * tep_data_pid_from_comm - return the pid from a given comm
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005340 * @tep: a handle to the trace event parser context
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005341 * @comm: the cmdline to find the pid from
5342 * @next: the cmdline structure to find the next comm
5343 *
5344 * This returns the cmdline structure that holds a pid for a given
5345 * comm, or NULL if none found. As there may be more than one pid for
5346 * a given comm, the result of this call can be passed back into
Ingo Molnar3e449f72018-12-03 11:22:00 +01005347 * a recurring call in the @next parameter, and then it will find the
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005348 * next pid.
Ingo Molnar3e449f72018-12-03 11:22:00 +01005349 * Also, it does a linear search, so it may be slow.
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005350 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005351struct tep_cmdline *tep_data_pid_from_comm(struct tep_handle *tep, const char *comm,
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -05005352 struct tep_cmdline *next)
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005353{
Tzvetomir Stoyanov2e4318a2018-11-30 23:08:09 -05005354 struct tep_cmdline *cmdline;
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005355
5356 /*
5357 * If the cmdlines have not been converted yet, then use
5358 * the list.
5359 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005360 if (!tep->cmdlines)
5361 return pid_from_cmdlist(tep, comm, next);
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005362
5363 if (next) {
5364 /*
5365 * The next pointer could have been still from
5366 * a previous call before cmdlines were created
5367 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005368 if (next < tep->cmdlines ||
5369 next >= tep->cmdlines + tep->cmdline_count)
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005370 next = NULL;
5371 else
5372 cmdline = next++;
5373 }
5374
5375 if (!next)
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005376 cmdline = tep->cmdlines;
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005377
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005378 while (cmdline < tep->cmdlines + tep->cmdline_count) {
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005379 if (strcmp(cmdline->comm, comm) == 0)
5380 return cmdline;
5381 cmdline++;
5382 }
5383 return NULL;
5384}
5385
5386/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005387 * tep_cmdline_pid - return the pid associated to a given cmdline
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005388 * @tep: a handle to the trace event parser context
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005389 * @cmdline: The cmdline structure to get the pid from
5390 *
5391 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5392 * -1 is returned.
5393 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005394int tep_cmdline_pid(struct tep_handle *tep, struct tep_cmdline *cmdline)
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005395{
5396 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5397
5398 if (!cmdline)
5399 return -1;
5400
5401 /*
5402 * If cmdlines have not been created yet, or cmdline is
5403 * not part of the array, then treat it as a cmdlist instead.
5404 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005405 if (!tep->cmdlines ||
5406 cmdline < tep->cmdlines ||
5407 cmdline >= tep->cmdlines + tep->cmdline_count)
Steven Rostedt (Red Hat)27719842015-03-24 09:57:52 -04005408 return cmdlist->pid;
5409
5410 return cmdline->pid;
5411}
5412
Steven Rostedtf7d82352012-04-06 00:47:53 +02005413/**
Tzvetomir Stoyanov (VMware)dc05ebf2018-08-08 14:03:03 -04005414 * tep_event_info - parse the data into the print format
Steven Rostedtf7d82352012-04-06 00:47:53 +02005415 * @s: the trace_seq to write to
5416 * @event: the handle to the event
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09005417 * @record: the record to read from
Steven Rostedtf7d82352012-04-06 00:47:53 +02005418 *
5419 * This parses the raw @data using the given @event information and
5420 * writes the print format into the trace_seq.
5421 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005422void tep_event_info(struct trace_seq *s, struct tep_event *event,
Tzvetomir Stoyanov (VMware)4d5c58b2018-08-08 14:02:49 -04005423 struct tep_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005424{
5425 int print_pretty = 1;
5426
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005427 if (event->pevent->print_raw || (event->flags & TEP_EVENT_FL_PRINTRAW))
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005428 tep_print_fields(s, record->data, record->size, event);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005429 else {
5430
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04005431 if (event->handler && !(event->flags & TEP_EVENT_FL_NOHANDLE))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005432 print_pretty = event->handler(s, record, event,
5433 event->context);
5434
5435 if (print_pretty)
5436 pretty_print(s, record->data, record->size, event);
5437 }
5438
5439 trace_seq_terminate(s);
5440}
5441
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005442static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5443{
Tzvetomir Stoyanov44e92f82018-11-28 14:55:52 -05005444 if (!trace_clock || !use_trace_clock)
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005445 return true;
5446
5447 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
Steven Rostedt (VMware)fed33e92019-04-01 12:43:07 -04005448 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf")
5449 || !strncmp(trace_clock, "mono", 4))
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005450 return true;
5451
5452 /* trace_clock is setting in tsc or counter mode */
5453 return false;
5454}
5455
Steven Rostedta6745332016-02-29 09:01:28 -05005456/**
Tzvetomir Stoyanov (VMware)610e1e42018-08-08 14:03:05 -04005457 * tep_find_event_by_record - return the event from a given record
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005458 * @tep: a handle to the trace event parser context
Steven Rostedta6745332016-02-29 09:01:28 -05005459 * @record: The record to get the event from
5460 *
5461 * Returns the associated event for a given record, or NULL if non is
5462 * is found.
5463 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005464struct tep_event *
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005465tep_find_event_by_record(struct tep_handle *tep, struct tep_record *record)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005466{
Steven Rostedta6745332016-02-29 09:01:28 -05005467 int type;
5468
5469 if (record->size < 0) {
5470 do_warning("ug! negative record size %d", record->size);
5471 return NULL;
5472 }
5473
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005474 type = trace_parse_common_type(tep, record->data);
Steven Rostedta6745332016-02-29 09:01:28 -05005475
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005476 return tep_find_event(tep, type);
Steven Rostedta6745332016-02-29 09:01:28 -05005477}
5478
5479/**
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005480 * tep_print_event_task - Write the event task comm, pid and CPU
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005481 * @tep: a handle to the trace event parser context
Steven Rostedta6745332016-02-29 09:01:28 -05005482 * @s: the trace_seq to write to
5483 * @event: the handle to the record's event
5484 * @record: The record to get the event from
5485 *
5486 * Writes the tasks comm, pid and CPU to @s.
5487 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005488void tep_print_event_task(struct tep_handle *tep, struct trace_seq *s,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005489 struct tep_event *event,
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005490 struct tep_record *record)
Steven Rostedta6745332016-02-29 09:01:28 -05005491{
5492 void *data = record->data;
5493 const char *comm;
5494 int pid;
5495
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005496 pid = parse_common_pid(tep, data);
5497 comm = find_cmdline(tep, pid);
Steven Rostedta6745332016-02-29 09:01:28 -05005498
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005499 if (tep->latency_format)
5500 trace_seq_printf(s, "%8.8s-%-5d %3d", comm, pid, record->cpu);
5501 else
Steven Rostedta6745332016-02-29 09:01:28 -05005502 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5503}
5504
5505/**
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005506 * tep_print_event_time - Write the event timestamp
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005507 * @tep: a handle to the trace event parser context
Steven Rostedta6745332016-02-29 09:01:28 -05005508 * @s: the trace_seq to write to
5509 * @event: the handle to the record's event
5510 * @record: The record to get the event from
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005511 * @use_trace_clock: Set to parse according to the @tep->trace_clock
Steven Rostedta6745332016-02-29 09:01:28 -05005512 *
5513 * Writes the timestamp of the record into @s.
5514 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005515void tep_print_event_time(struct tep_handle *tep, struct trace_seq *s,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005516 struct tep_event *event,
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005517 struct tep_record *record,
5518 bool use_trace_clock)
Steven Rostedta6745332016-02-29 09:01:28 -05005519{
Steven Rostedtf7d82352012-04-06 00:47:53 +02005520 unsigned long secs;
5521 unsigned long usecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005522 unsigned long nsecs;
Steven Rostedt4dc10242012-04-06 00:47:57 +02005523 int p;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005524 bool use_usec_format;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005525
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005526 use_usec_format = is_timestamp_in_us(tep->trace_clock, use_trace_clock);
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005527 if (use_usec_format) {
Steven Rostedtbb5a7312016-11-22 15:00:31 -03005528 secs = record->ts / NSEC_PER_SEC;
5529 nsecs = record->ts - secs * NSEC_PER_SEC;
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005530 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005531
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005532 if (tep->latency_format) {
5533 tep_data_latency_format(tep, s, record);
Steven Rostedt4f3c88762016-03-23 10:16:28 -04005534 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02005535
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005536 if (use_usec_format) {
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005537 if (tep->flags & TEP_NSEC_OUTPUT) {
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005538 usecs = nsecs;
5539 p = 9;
5540 } else {
Steven Rostedtbb5a7312016-11-22 15:00:31 -03005541 usecs = (nsecs + 500) / NSEC_PER_USEC;
Chaos.Chen21a30102016-02-09 15:40:14 -05005542 /* To avoid usecs larger than 1 sec */
Steven Rostedtbb5a7312016-11-22 15:00:31 -03005543 if (usecs >= USEC_PER_SEC) {
5544 usecs -= USEC_PER_SEC;
Chaos.Chen21a30102016-02-09 15:40:14 -05005545 secs++;
5546 }
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005547 p = 6;
5548 }
Steven Rostedt4dc10242012-04-06 00:47:57 +02005549
Steven Rostedta6745332016-02-29 09:01:28 -05005550 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
Yoshihiro YUNOMAE1b372ca2013-11-01 17:53:53 -04005551 } else
Steven Rostedta6745332016-02-29 09:01:28 -05005552 trace_seq_printf(s, " %12llu:", record->ts);
5553}
5554
5555/**
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005556 * tep_print_event_data - Write the event data section
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005557 * @tep: a handle to the trace event parser context
Steven Rostedta6745332016-02-29 09:01:28 -05005558 * @s: the trace_seq to write to
5559 * @event: the handle to the record's event
5560 * @record: The record to get the event from
5561 *
5562 * Writes the parsing of the record's data to @s.
5563 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005564void tep_print_event_data(struct tep_handle *tep, struct trace_seq *s,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005565 struct tep_event *event,
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005566 struct tep_record *record)
Steven Rostedta6745332016-02-29 09:01:28 -05005567{
5568 static const char *spaces = " "; /* 20 spaces */
5569 int len;
5570
5571 trace_seq_printf(s, " %s: ", event->name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005572
5573 /* Space out the event names evenly. */
5574 len = strlen(event->name);
5575 if (len < 20)
5576 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5577
Tzvetomir Stoyanov (VMware)4d5c58b2018-08-08 14:02:49 -04005578 tep_event_info(s, event, record);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005579}
5580
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005581void tep_print_event(struct tep_handle *tep, struct trace_seq *s,
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04005582 struct tep_record *record, bool use_trace_clock)
Steven Rostedta6745332016-02-29 09:01:28 -05005583{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005584 struct tep_event *event;
Steven Rostedta6745332016-02-29 09:01:28 -05005585
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005586 event = tep_find_event_by_record(tep, record);
Steven Rostedta6745332016-02-29 09:01:28 -05005587 if (!event) {
Steven Rostedt (VMware)e8773722018-01-11 19:47:47 -05005588 int i;
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005589 int type = trace_parse_common_type(tep, record->data);
Steven Rostedt (VMware)e8773722018-01-11 19:47:47 -05005590
5591 do_warning("ug! no event found for type %d", type);
5592 trace_seq_printf(s, "[UNKNOWN TYPE %d]", type);
5593 for (i = 0; i < record->size; i++)
5594 trace_seq_printf(s, " %02x",
5595 ((unsigned char *)record->data)[i]);
Steven Rostedta6745332016-02-29 09:01:28 -05005596 return;
5597 }
5598
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04005599 tep_print_event_task(tep, s, event, record);
5600 tep_print_event_time(tep, s, event, record, use_trace_clock);
5601 tep_print_event_data(tep, s, event, record);
Steven Rostedta6745332016-02-29 09:01:28 -05005602}
5603
Steven Rostedtf7d82352012-04-06 00:47:53 +02005604static int events_id_cmp(const void *a, const void *b)
5605{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005606 struct tep_event * const * ea = a;
5607 struct tep_event * const * eb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005608
5609 if ((*ea)->id < (*eb)->id)
5610 return -1;
5611
5612 if ((*ea)->id > (*eb)->id)
5613 return 1;
5614
5615 return 0;
5616}
5617
5618static int events_name_cmp(const void *a, const void *b)
5619{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005620 struct tep_event * const * ea = a;
5621 struct tep_event * const * eb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005622 int res;
5623
5624 res = strcmp((*ea)->name, (*eb)->name);
5625 if (res)
5626 return res;
5627
5628 res = strcmp((*ea)->system, (*eb)->system);
5629 if (res)
5630 return res;
5631
5632 return events_id_cmp(a, b);
5633}
5634
5635static int events_system_cmp(const void *a, const void *b)
5636{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005637 struct tep_event * const * ea = a;
5638 struct tep_event * const * eb = b;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005639 int res;
5640
5641 res = strcmp((*ea)->system, (*eb)->system);
5642 if (res)
5643 return res;
5644
5645 res = strcmp((*ea)->name, (*eb)->name);
5646 if (res)
5647 return res;
5648
5649 return events_id_cmp(a, b);
5650}
5651
Tzvetomir Stoyanov6699ed72019-04-01 12:43:08 -04005652static struct tep_event **list_events_copy(struct tep_handle *tep)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005653{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005654 struct tep_event **events;
Tzvetomir Stoyanov6699ed72019-04-01 12:43:08 -04005655
5656 if (!tep)
5657 return NULL;
5658
5659 events = malloc(sizeof(*events) * (tep->nr_events + 1));
5660 if (!events)
5661 return NULL;
5662
5663 memcpy(events, tep->events, sizeof(*events) * tep->nr_events);
5664 events[tep->nr_events] = NULL;
5665 return events;
5666}
5667
5668static void list_events_sort(struct tep_event **events, int nr_events,
5669 enum tep_event_sort_type sort_type)
5670{
Steven Rostedtf7d82352012-04-06 00:47:53 +02005671 int (*sort)(const void *a, const void *b);
5672
Steven Rostedtf7d82352012-04-06 00:47:53 +02005673 switch (sort_type) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005674 case TEP_EVENT_SORT_ID:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005675 sort = events_id_cmp;
5676 break;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005677 case TEP_EVENT_SORT_NAME:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005678 sort = events_name_cmp;
5679 break;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005680 case TEP_EVENT_SORT_SYSTEM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005681 sort = events_system_cmp;
5682 break;
5683 default:
Tzvetomir Stoyanov6699ed72019-04-01 12:43:08 -04005684 sort = NULL;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005685 }
5686
Tzvetomir Stoyanov6699ed72019-04-01 12:43:08 -04005687 if (sort)
5688 qsort(events, nr_events, sizeof(*events), sort);
5689}
5690
5691/**
5692 * tep_list_events - Get events, sorted by given criteria.
5693 * @tep: a handle to the tep context
5694 * @sort_type: desired sort order of the events in the array
5695 *
5696 * Returns an array of pointers to all events, sorted by the given
5697 * @sort_type criteria. The last element of the array is NULL. The returned
5698 * memory must not be freed, it is managed by the library.
5699 * The function is not thread safe.
5700 */
5701struct tep_event **tep_list_events(struct tep_handle *tep,
5702 enum tep_event_sort_type sort_type)
5703{
5704 struct tep_event **events;
5705
5706 if (!tep)
5707 return NULL;
5708
5709 events = tep->sort_events;
5710 if (events && tep->last_type == sort_type)
5711 return events;
5712
5713 if (!events) {
5714 events = list_events_copy(tep);
5715 if (!events)
5716 return NULL;
5717
5718 tep->sort_events = events;
5719
5720 /* the internal events are sorted by id */
5721 if (sort_type == TEP_EVENT_SORT_ID) {
5722 tep->last_type = sort_type;
5723 return events;
5724 }
5725 }
5726
5727 list_events_sort(events, tep->nr_events, sort_type);
5728 tep->last_type = sort_type;
5729
5730 return events;
5731}
5732
5733
5734/**
5735 * tep_list_events_copy - Thread safe version of tep_list_events()
5736 * @tep: a handle to the tep context
5737 * @sort_type: desired sort order of the events in the array
5738 *
5739 * Returns an array of pointers to all events, sorted by the given
5740 * @sort_type criteria. The last element of the array is NULL. The returned
5741 * array is newly allocated inside the function and must be freed by the caller
5742 */
5743struct tep_event **tep_list_events_copy(struct tep_handle *tep,
5744 enum tep_event_sort_type sort_type)
5745{
5746 struct tep_event **events;
5747
5748 if (!tep)
5749 return NULL;
5750
5751 events = list_events_copy(tep);
5752 if (!events)
5753 return NULL;
5754
5755 /* the internal events are sorted by id */
5756 if (sort_type == TEP_EVENT_SORT_ID)
5757 return events;
5758
5759 list_events_sort(events, tep->nr_events, sort_type);
Steven Rostedtf7d82352012-04-06 00:47:53 +02005760
5761 return events;
5762}
5763
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04005764static struct tep_format_field **
Steven Rostedtf7d82352012-04-06 00:47:53 +02005765get_event_fields(const char *type, const char *name,
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04005766 int count, struct tep_format_field *list)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005767{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04005768 struct tep_format_field **fields;
5769 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02005770 int i = 0;
5771
Arnaldo Carvalho de Meloa6d2a612012-09-12 17:30:50 -03005772 fields = malloc(sizeof(*fields) * (count + 1));
5773 if (!fields)
5774 return NULL;
5775
Steven Rostedtf7d82352012-04-06 00:47:53 +02005776 for (field = list; field; field = field->next) {
5777 fields[i++] = field;
5778 if (i == count + 1) {
5779 do_warning("event %s has more %s fields than specified",
5780 name, type);
5781 i--;
5782 break;
5783 }
5784 }
5785
5786 if (i != count)
5787 do_warning("event %s has less %s fields than specified",
5788 name, type);
5789
5790 fields[i] = NULL;
5791
5792 return fields;
5793}
5794
5795/**
Tzvetomir Stoyanov (VMware)c99eeaf2018-08-08 14:03:08 -04005796 * tep_event_common_fields - return a list of common fields for an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005797 * @event: the event to return the common fields of.
5798 *
5799 * Returns an allocated array of fields. The last item in the array is NULL.
5800 * The array must be freed with free().
5801 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005802struct tep_format_field **tep_event_common_fields(struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005803{
5804 return get_event_fields("common", event->name,
5805 event->format.nr_common,
5806 event->format.common_fields);
5807}
5808
5809/**
Tzvetomir Stoyanov (VMware)c99eeaf2018-08-08 14:03:08 -04005810 * tep_event_fields - return a list of event specific fields for an event
Steven Rostedtf7d82352012-04-06 00:47:53 +02005811 * @event: the event to return the fields of.
5812 *
5813 * Returns an allocated array of fields. The last item in the array is NULL.
5814 * The array must be freed with free().
5815 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05005816struct tep_format_field **tep_event_fields(struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005817{
5818 return get_event_fields("event", event->name,
5819 event->format.nr_fields,
5820 event->format.fields);
5821}
5822
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04005823static void print_fields(struct trace_seq *s, struct tep_print_flag_sym *field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005824{
5825 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5826 if (field->next) {
5827 trace_seq_puts(s, ", ");
5828 print_fields(s, field->next);
5829 }
5830}
5831
5832/* for debugging */
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04005833static void print_args(struct tep_print_arg *args)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005834{
5835 int print_paren = 1;
5836 struct trace_seq s;
5837
5838 switch (args->type) {
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005839 case TEP_PRINT_NULL:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005840 printf("null");
5841 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005842 case TEP_PRINT_ATOM:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005843 printf("%s", args->atom.atom);
5844 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005845 case TEP_PRINT_FIELD:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005846 printf("REC->%s", args->field.name);
5847 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005848 case TEP_PRINT_FLAGS:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005849 printf("__print_flags(");
5850 print_args(args->flags.field);
5851 printf(", %s, ", args->flags.delim);
5852 trace_seq_init(&s);
5853 print_fields(&s, args->flags.flags);
5854 trace_seq_do_printf(&s);
5855 trace_seq_destroy(&s);
5856 printf(")");
5857 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005858 case TEP_PRINT_SYMBOL:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005859 printf("__print_symbolic(");
5860 print_args(args->symbol.field);
5861 printf(", ");
5862 trace_seq_init(&s);
5863 print_fields(&s, args->symbol.symbols);
5864 trace_seq_do_printf(&s);
5865 trace_seq_destroy(&s);
5866 printf(")");
5867 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005868 case TEP_PRINT_HEX:
Namhyung Kime080e6f2012-06-27 09:41:41 +09005869 printf("__print_hex(");
5870 print_args(args->hex.field);
5871 printf(", ");
5872 print_args(args->hex.size);
5873 printf(")");
5874 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005875 case TEP_PRINT_HEX_STR:
Daniel Borkmann0fe05592017-01-25 02:28:17 +01005876 printf("__print_hex_str(");
5877 print_args(args->hex.field);
5878 printf(", ");
5879 print_args(args->hex.size);
5880 printf(")");
5881 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005882 case TEP_PRINT_INT_ARRAY:
Javi Merinob839e1e82015-03-24 11:07:19 +00005883 printf("__print_array(");
5884 print_args(args->int_array.field);
5885 printf(", ");
5886 print_args(args->int_array.count);
5887 printf(", ");
5888 print_args(args->int_array.el_size);
5889 printf(")");
5890 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005891 case TEP_PRINT_STRING:
5892 case TEP_PRINT_BSTRING:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005893 printf("__get_str(%s)", args->string.string);
5894 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005895 case TEP_PRINT_BITMASK:
Steven Rostedt (Red Hat)473a7782014-06-02 23:20:16 -04005896 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5897 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005898 case TEP_PRINT_TYPE:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005899 printf("(%s)", args->typecast.type);
5900 print_args(args->typecast.item);
5901 break;
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04005902 case TEP_PRINT_OP:
Steven Rostedtf7d82352012-04-06 00:47:53 +02005903 if (strcmp(args->op.op, ":") == 0)
5904 print_paren = 0;
5905 if (print_paren)
5906 printf("(");
5907 print_args(args->op.left);
5908 printf(" %s ", args->op.op);
5909 print_args(args->op.right);
5910 if (print_paren)
5911 printf(")");
5912 break;
5913 default:
5914 /* we should warn... */
5915 return;
5916 }
5917 if (args->next) {
5918 printf("\n");
5919 print_args(args->next);
5920 }
5921}
5922
5923static void parse_header_field(const char *field,
5924 int *offset, int *size, int mandatory)
5925{
5926 unsigned long long save_input_buf_ptr;
5927 unsigned long long save_input_buf_siz;
5928 char *token;
5929 int type;
5930
5931 save_input_buf_ptr = input_buf_ptr;
5932 save_input_buf_siz = input_buf_siz;
5933
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005934 if (read_expected(TEP_EVENT_ITEM, "field") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005935 return;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005936 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005937 return;
5938
5939 /* type */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005940 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005941 goto fail;
5942 free_token(token);
5943
5944 /*
5945 * If this is not a mandatory field, then test it first.
5946 */
5947 if (mandatory) {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005948 if (read_expected(TEP_EVENT_ITEM, field) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005949 return;
5950 } else {
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005951 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005952 goto fail;
5953 if (strcmp(token, field) != 0)
5954 goto discard;
5955 free_token(token);
5956 }
5957
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005958 if (read_expected(TEP_EVENT_OP, ";") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005959 return;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005960 if (read_expected(TEP_EVENT_ITEM, "offset") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005961 return;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005962 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005963 return;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005964 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005965 goto fail;
5966 *offset = atoi(token);
5967 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005968 if (read_expected(TEP_EVENT_OP, ";") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005969 return;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005970 if (read_expected(TEP_EVENT_ITEM, "size") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005971 return;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005972 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005973 return;
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005974 if (read_expect_type(TEP_EVENT_ITEM, &token) < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005975 goto fail;
5976 *size = atoi(token);
5977 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005978 if (read_expected(TEP_EVENT_OP, ";") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005979 return;
5980 type = read_token(&token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005981 if (type != TEP_EVENT_NEWLINE) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02005982 /* newer versions of the kernel have a "signed" type */
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005983 if (type != TEP_EVENT_ITEM)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005984 goto fail;
5985
5986 if (strcmp(token, "signed") != 0)
5987 goto fail;
5988
5989 free_token(token);
5990
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005991 if (read_expected(TEP_EVENT_OP, ":") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005992 return;
5993
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005994 if (read_expect_type(TEP_EVENT_ITEM, &token))
Steven Rostedtf7d82352012-04-06 00:47:53 +02005995 goto fail;
5996
5997 free_token(token);
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04005998 if (read_expected(TEP_EVENT_OP, ";") < 0)
Steven Rostedtf7d82352012-04-06 00:47:53 +02005999 return;
6000
Tzvetomir Stoyanov (VMware)f25d9e02018-09-19 14:56:47 -04006001 if (read_expect_type(TEP_EVENT_NEWLINE, &token))
Steven Rostedtf7d82352012-04-06 00:47:53 +02006002 goto fail;
6003 }
6004 fail:
6005 free_token(token);
6006 return;
6007
6008 discard:
6009 input_buf_ptr = save_input_buf_ptr;
6010 input_buf_siz = save_input_buf_siz;
6011 *offset = 0;
6012 *size = 0;
6013 free_token(token);
6014}
6015
6016/**
Tzvetomir Stoyanov (VMware)c60167c2018-08-08 14:02:51 -04006017 * tep_parse_header_page - parse the data stored in the header page
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006018 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02006019 * @buf: the buffer storing the header page format string
6020 * @size: the size of @buf
6021 * @long_size: the long size to use if there is no header
6022 *
6023 * This parses the header page format for information on the
6024 * ring buffer used. The @buf should be copied from
6025 *
6026 * /sys/kernel/debug/tracing/events/header_page
6027 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006028int tep_parse_header_page(struct tep_handle *tep, char *buf, unsigned long size,
Tzvetomir Stoyanov (VMware)c60167c2018-08-08 14:02:51 -04006029 int long_size)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006030{
6031 int ignore;
6032
6033 if (!size) {
6034 /*
6035 * Old kernels did not have header page info.
6036 * Sorry but we just use what we find here in user space.
6037 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006038 tep->header_page_ts_size = sizeof(long long);
6039 tep->header_page_size_size = long_size;
6040 tep->header_page_data_offset = sizeof(long long) + long_size;
6041 tep->old_format = 1;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006042 return -1;
6043 }
6044 init_input_buf(buf, size);
6045
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006046 parse_header_field("timestamp", &tep->header_page_ts_offset,
6047 &tep->header_page_ts_size, 1);
6048 parse_header_field("commit", &tep->header_page_size_offset,
6049 &tep->header_page_size_size, 1);
6050 parse_header_field("overwrite", &tep->header_page_overwrite,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006051 &ignore, 0);
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006052 parse_header_field("data", &tep->header_page_data_offset,
6053 &tep->header_page_data_size, 1);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006054
6055 return 0;
6056}
6057
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006058static int event_matches(struct tep_event *event,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006059 int id, const char *sys_name,
6060 const char *event_name)
6061{
6062 if (id >= 0 && id != event->id)
6063 return 0;
6064
6065 if (event_name && (strcmp(event_name, event->name) != 0))
6066 return 0;
6067
6068 if (sys_name && (strcmp(sys_name, event->system) != 0))
6069 return 0;
6070
6071 return 1;
6072}
6073
6074static void free_handler(struct event_handler *handle)
6075{
6076 free((void *)handle->sys_name);
6077 free((void *)handle->event_name);
6078 free(handle);
6079}
6080
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006081static int find_event_handle(struct tep_handle *pevent, struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006082{
6083 struct event_handler *handle, **next;
6084
6085 for (next = &pevent->handlers; *next;
6086 next = &(*next)->next) {
6087 handle = *next;
6088 if (event_matches(event, handle->id,
6089 handle->sys_name,
6090 handle->event_name))
6091 break;
6092 }
6093
6094 if (!(*next))
6095 return 0;
6096
6097 pr_stat("overriding event (%d) %s:%s with new print handler",
6098 event->id, event->system, event->name);
6099
6100 event->handler = handle->func;
6101 event->context = handle->context;
6102
6103 *next = handle->next;
6104 free_handler(handle);
6105
6106 return 1;
6107}
6108
6109/**
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04006110 * __tep_parse_format - parse the event format
Steven Rostedtf7d82352012-04-06 00:47:53 +02006111 * @buf: the buffer storing the event format string
6112 * @size: the size of @buf
6113 * @sys: the system the event belongs to
6114 *
6115 * This parses the event format and creates an event structure
6116 * to quickly parse raw data for a given event.
6117 *
6118 * These files currently come from:
6119 *
6120 * /sys/kernel/debug/tracing/events/.../.../format
6121 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006122enum tep_errno __tep_parse_format(struct tep_event **eventp,
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04006123 struct tep_handle *pevent, const char *buf,
6124 unsigned long size, const char *sys)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006125{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006126 struct tep_event *event;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006127 int ret;
6128
6129 init_input_buf(buf, size);
6130
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006131 *eventp = event = alloc_event();
Steven Rostedtf7d82352012-04-06 00:47:53 +02006132 if (!event)
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006133 return TEP_ERRNO__MEM_ALLOC_FAILED;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006134
6135 event->name = event_read_name();
6136 if (!event->name) {
6137 /* Bad event? */
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006138 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimbffddff2012-08-22 16:00:29 +09006139 goto event_alloc_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006140 }
6141
6142 if (strcmp(sys, "ftrace") == 0) {
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04006143 event->flags |= TEP_EVENT_FL_ISFTRACE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006144
6145 if (strcmp(event->name, "bprint") == 0)
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04006146 event->flags |= TEP_EVENT_FL_ISBPRINT;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006147 }
6148
6149 event->id = event_read_id();
Namhyung Kimbffddff2012-08-22 16:00:29 +09006150 if (event->id < 0) {
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006151 ret = TEP_ERRNO__READ_ID_FAILED;
Namhyung Kimbffddff2012-08-22 16:00:29 +09006152 /*
6153 * This isn't an allocation error actually.
6154 * But as the ID is critical, just bail out.
6155 */
6156 goto event_alloc_failed;
6157 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006158
6159 event->system = strdup(sys);
Namhyung Kimbffddff2012-08-22 16:00:29 +09006160 if (!event->system) {
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006161 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimbffddff2012-08-22 16:00:29 +09006162 goto event_alloc_failed;
6163 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006164
Steven Rostedt101782e2012-10-01 20:13:51 -04006165 /* Add pevent to event so that it can be referenced */
6166 event->pevent = pevent;
6167
Steven Rostedtf7d82352012-04-06 00:47:53 +02006168 ret = event_read_format(event);
6169 if (ret < 0) {
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006170 ret = TEP_ERRNO__READ_FORMAT_FAILED;
Namhyung Kimbffddff2012-08-22 16:00:29 +09006171 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006172 }
6173
6174 /*
6175 * If the event has an override, don't print warnings if the event
6176 * print format fails to parse.
6177 */
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006178 if (pevent && find_event_handle(pevent, event))
Steven Rostedtf7d82352012-04-06 00:47:53 +02006179 show_warning = 0;
6180
6181 ret = event_read_print(event);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006182 show_warning = 1;
6183
Steven Rostedtf7d82352012-04-06 00:47:53 +02006184 if (ret < 0) {
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006185 ret = TEP_ERRNO__READ_PRINT_FAILED;
Namhyung Kimbffddff2012-08-22 16:00:29 +09006186 goto event_parse_failed;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006187 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006188
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04006189 if (!ret && (event->flags & TEP_EVENT_FL_ISFTRACE)) {
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006190 struct tep_format_field *field;
Tzvetomir Stoyanov (VMware)5647f942018-09-19 14:56:49 -04006191 struct tep_print_arg *arg, **list;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006192
6193 /* old ftrace had no args */
Steven Rostedtf7d82352012-04-06 00:47:53 +02006194 list = &event->print_fmt.args;
6195 for (field = event->format.fields; field; field = field->next) {
6196 arg = alloc_arg();
Namhyung Kimb1ac7542012-09-20 11:09:19 +09006197 if (!arg) {
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04006198 event->flags |= TEP_EVENT_FL_FAILED;
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006199 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimb1ac7542012-09-20 11:09:19 +09006200 }
Tzvetomir Stoyanov (VMware)1e97216f2018-09-19 14:56:50 -04006201 arg->type = TEP_PRINT_FIELD;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006202 arg->field.name = strdup(field->name);
Namhyung Kimca638582012-04-09 11:54:31 +09006203 if (!arg->field.name) {
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04006204 event->flags |= TEP_EVENT_FL_FAILED;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09006205 free_arg(arg);
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006206 return TEP_ERRNO__OLD_FTRACE_ARG_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09006207 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006208 arg->field.field = field;
Namhyung Kimfd34f0b2012-08-22 16:00:28 +09006209 *list = arg;
6210 list = &arg->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006211 }
6212 return 0;
6213 }
6214
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006215 return 0;
6216
6217 event_parse_failed:
Tzvetomir Stoyanov (VMware)c1953bc2018-09-19 14:56:48 -04006218 event->flags |= TEP_EVENT_FL_FAILED;
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006219 return ret;
6220
6221 event_alloc_failed:
6222 free(event->system);
6223 free(event->name);
6224 free(event);
6225 *eventp = NULL;
6226 return ret;
6227}
6228
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006229static enum tep_errno
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04006230__parse_event(struct tep_handle *pevent,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006231 struct tep_event **eventp,
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04006232 const char *buf, unsigned long size,
6233 const char *sys)
Jiri Olsa71ad9582013-12-03 14:09:19 +01006234{
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04006235 int ret = __tep_parse_format(eventp, pevent, buf, size, sys);
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006236 struct tep_event *event = *eventp;
Jiri Olsa71ad9582013-12-03 14:09:19 +01006237
6238 if (event == NULL)
6239 return ret;
6240
6241 if (pevent && add_event(pevent, event)) {
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006242 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
Jiri Olsa71ad9582013-12-03 14:09:19 +01006243 goto event_add_failed;
6244 }
6245
6246#define PRINT_ARGS 0
6247 if (PRINT_ARGS && event->print_fmt.args)
6248 print_args(event->print_fmt.args);
6249
6250 return 0;
6251
6252event_add_failed:
Tzvetomir Stoyanovfc398512018-11-30 10:44:08 -05006253 tep_free_event(event);
Jiri Olsa71ad9582013-12-03 14:09:19 +01006254 return ret;
6255}
6256
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006257/**
Tzvetomir Stoyanov (VMware)c60167c2018-08-08 14:02:51 -04006258 * tep_parse_format - parse the event format
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006259 * @tep: a handle to the trace event parser context
Jiri Olsa71ad9582013-12-03 14:09:19 +01006260 * @eventp: returned format
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006261 * @buf: the buffer storing the event format string
6262 * @size: the size of @buf
6263 * @sys: the system the event belongs to
6264 *
6265 * This parses the event format and creates an event structure
6266 * to quickly parse raw data for a given event.
6267 *
6268 * These files currently come from:
6269 *
6270 * /sys/kernel/debug/tracing/events/.../.../format
6271 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006272enum tep_errno tep_parse_format(struct tep_handle *tep,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006273 struct tep_event **eventp,
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006274 const char *buf,
6275 unsigned long size, const char *sys)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006276{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006277 return __parse_event(tep, eventp, buf, size, sys);
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006278}
6279
6280/**
Tzvetomir Stoyanov (VMware)c60167c2018-08-08 14:02:51 -04006281 * tep_parse_event - parse the event format
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006282 * @tep: a handle to the trace event parser context
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006283 * @buf: the buffer storing the event format string
6284 * @size: the size of @buf
6285 * @sys: the system the event belongs to
6286 *
6287 * This parses the event format and creates an event structure
6288 * to quickly parse raw data for a given event.
6289 *
6290 * These files currently come from:
6291 *
6292 * /sys/kernel/debug/tracing/events/.../.../format
6293 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006294enum tep_errno tep_parse_event(struct tep_handle *tep, const char *buf,
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006295 unsigned long size, const char *sys)
Arnaldo Carvalho de Melo2b291752012-09-18 11:13:15 -03006296{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006297 struct tep_event *event = NULL;
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006298 return __parse_event(tep, &event, buf, size, sys);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006299}
6300
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006301int get_field_val(struct trace_seq *s, struct tep_format_field *field,
Tzvetomir Stoyanov (VMware)cbc49b22018-08-08 14:02:47 -04006302 const char *name, struct tep_record *record,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006303 unsigned long long *val, int err)
6304{
6305 if (!field) {
6306 if (err)
6307 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6308 return -1;
6309 }
6310
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04006311 if (tep_read_number_field(field, record->data, val)) {
Steven Rostedtf7d82352012-04-06 00:47:53 +02006312 if (err)
6313 trace_seq_printf(s, " %s=INVALID", name);
6314 return -1;
6315 }
6316
6317 return 0;
6318}
6319
6320/**
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006321 * tep_get_field_raw - return the raw pointer into the data field
Steven Rostedtf7d82352012-04-06 00:47:53 +02006322 * @s: The seq to print to on error
6323 * @event: the event that the field is for
6324 * @name: The name of the field
6325 * @record: The record with the field name.
6326 * @len: place to store the field length.
6327 * @err: print default error if failed.
6328 *
6329 * Returns a pointer into record->data of the field and places
6330 * the length of the field in @len.
6331 *
6332 * On failure, it returns NULL.
6333 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006334void *tep_get_field_raw(struct trace_seq *s, struct tep_event *event,
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006335 const char *name, struct tep_record *record,
6336 int *len, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006337{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006338 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006339 void *data = record->data;
6340 unsigned offset;
6341 int dummy;
6342
6343 if (!event)
6344 return NULL;
6345
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04006346 field = tep_find_field(event, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006347
6348 if (!field) {
6349 if (err)
6350 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6351 return NULL;
6352 }
6353
6354 /* Allow @len to be NULL */
6355 if (!len)
6356 len = &dummy;
6357
6358 offset = field->offset;
Tzvetomir Stoyanov (VMware)bb39ccb2018-09-19 14:56:46 -04006359 if (field->flags & TEP_FIELD_IS_DYNAMIC) {
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04006360 offset = tep_read_number(event->pevent,
Steven Rostedtf7d82352012-04-06 00:47:53 +02006361 data + offset, field->size);
6362 *len = offset >> 16;
6363 offset &= 0xffff;
6364 } else
6365 *len = field->size;
6366
6367 return data + offset;
6368}
6369
6370/**
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006371 * tep_get_field_val - find a field and return its value
Steven Rostedtf7d82352012-04-06 00:47:53 +02006372 * @s: The seq to print to on error
6373 * @event: the event that the field is for
6374 * @name: The name of the field
6375 * @record: The record with the field name.
6376 * @val: place to store the value of the field.
6377 * @err: print default error if failed.
6378 *
6379 * Returns 0 on success -1 on field not found.
6380 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006381int tep_get_field_val(struct trace_seq *s, struct tep_event *event,
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006382 const char *name, struct tep_record *record,
6383 unsigned long long *val, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006384{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006385 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006386
6387 if (!event)
6388 return -1;
6389
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04006390 field = tep_find_field(event, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006391
6392 return get_field_val(s, field, name, record, val, err);
6393}
6394
6395/**
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006396 * tep_get_common_field_val - find a common field and return its value
Steven Rostedtf7d82352012-04-06 00:47:53 +02006397 * @s: The seq to print to on error
6398 * @event: the event that the field is for
6399 * @name: The name of the field
6400 * @record: The record with the field name.
6401 * @val: place to store the value of the field.
6402 * @err: print default error if failed.
6403 *
6404 * Returns 0 on success -1 on field not found.
6405 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006406int tep_get_common_field_val(struct trace_seq *s, struct tep_event *event,
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006407 const char *name, struct tep_record *record,
6408 unsigned long long *val, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006409{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006410 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006411
6412 if (!event)
6413 return -1;
6414
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04006415 field = tep_find_common_field(event, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006416
6417 return get_field_val(s, field, name, record, val, err);
6418}
6419
6420/**
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006421 * tep_get_any_field_val - find a any field and return its value
Steven Rostedtf7d82352012-04-06 00:47:53 +02006422 * @s: The seq to print to on error
6423 * @event: the event that the field is for
6424 * @name: The name of the field
6425 * @record: The record with the field name.
6426 * @val: place to store the value of the field.
6427 * @err: print default error if failed.
6428 *
6429 * Returns 0 on success -1 on field not found.
6430 */
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006431int tep_get_any_field_val(struct trace_seq *s, struct tep_event *event,
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006432 const char *name, struct tep_record *record,
6433 unsigned long long *val, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006434{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006435 struct tep_format_field *field;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006436
6437 if (!event)
6438 return -1;
6439
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04006440 field = tep_find_any_field(event, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006441
6442 return get_field_val(s, field, name, record, val, err);
6443}
6444
6445/**
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04006446 * tep_print_num_field - print a field and a format
Steven Rostedtf7d82352012-04-06 00:47:53 +02006447 * @s: The seq to print to
6448 * @fmt: The printf format to print the field with.
6449 * @event: the event that the field is for
6450 * @name: The name of the field
6451 * @record: The record with the field name.
6452 * @err: print default error if failed.
6453 *
Tzvetomir Stoyanov489b3492019-04-01 12:43:10 -04006454 * Returns positive value on success, negative in case of an error,
6455 * or 0 if buffer is full.
Steven Rostedtf7d82352012-04-06 00:47:53 +02006456 */
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04006457int tep_print_num_field(struct trace_seq *s, const char *fmt,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006458 struct tep_event *event, const char *name,
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04006459 struct tep_record *record, int err)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006460{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006461 struct tep_format_field *field = tep_find_field(event, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006462 unsigned long long val;
6463
6464 if (!field)
6465 goto failed;
6466
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04006467 if (tep_read_number_field(field, record->data, &val))
Steven Rostedtf7d82352012-04-06 00:47:53 +02006468 goto failed;
6469
6470 return trace_seq_printf(s, fmt, val);
6471
6472 failed:
6473 if (err)
6474 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6475 return -1;
6476}
6477
Steven Rostedt6d862b82013-11-01 17:54:00 -04006478/**
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04006479 * tep_print_func_field - print a field and a format for function pointers
Steven Rostedt6d862b82013-11-01 17:54:00 -04006480 * @s: The seq to print to
6481 * @fmt: The printf format to print the field with.
6482 * @event: the event that the field is for
6483 * @name: The name of the field
6484 * @record: The record with the field name.
6485 * @err: print default error if failed.
6486 *
Tzvetomir Stoyanov489b3492019-04-01 12:43:10 -04006487 * Returns positive value on success, negative in case of an error,
6488 * or 0 if buffer is full.
Steven Rostedt6d862b82013-11-01 17:54:00 -04006489 */
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04006490int tep_print_func_field(struct trace_seq *s, const char *fmt,
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006491 struct tep_event *event, const char *name,
Tzvetomir Stoyanov (VMware)6a48dc22018-08-08 14:02:52 -04006492 struct tep_record *record, int err)
Steven Rostedt6d862b82013-11-01 17:54:00 -04006493{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006494 struct tep_format_field *field = tep_find_field(event, name);
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04006495 struct tep_handle *pevent = event->pevent;
Steven Rostedt6d862b82013-11-01 17:54:00 -04006496 unsigned long long val;
6497 struct func_map *func;
6498 char tmp[128];
6499
6500 if (!field)
6501 goto failed;
6502
Tzvetomir Stoyanov (VMware)59c1bae2018-08-08 14:02:53 -04006503 if (tep_read_number_field(field, record->data, &val))
Steven Rostedt6d862b82013-11-01 17:54:00 -04006504 goto failed;
6505
6506 func = find_func(pevent, val);
6507
6508 if (func)
6509 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6510 else
6511 sprintf(tmp, "0x%08llx", val);
6512
6513 return trace_seq_printf(s, fmt, tmp);
6514
6515 failed:
6516 if (err)
6517 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6518 return -1;
6519}
6520
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006521static void free_func_handle(struct tep_function_handler *func)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006522{
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04006523 struct func_params *params;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006524
6525 free(func->name);
6526
6527 while (func->params) {
6528 params = func->params;
6529 func->params = params->next;
6530 free(params);
6531 }
6532
6533 free(func);
6534}
6535
6536/**
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006537 * tep_register_print_function - register a helper function
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006538 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02006539 * @func: the function to process the helper function
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006540 * @ret_type: the return type of the helper function
Steven Rostedtf7d82352012-04-06 00:47:53 +02006541 * @name: the name of the helper function
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006542 * @parameters: A list of enum tep_func_arg_type
Steven Rostedtf7d82352012-04-06 00:47:53 +02006543 *
6544 * Some events may have helper functions in the print format arguments.
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006545 * This allows a plugin to dynamically create a way to process one
Steven Rostedtf7d82352012-04-06 00:47:53 +02006546 * of these functions.
6547 *
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006548 * The @parameters is a variable list of tep_func_arg_type enums that
6549 * must end with TEP_FUNC_ARG_VOID.
Steven Rostedtf7d82352012-04-06 00:47:53 +02006550 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006551int tep_register_print_function(struct tep_handle *tep,
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006552 tep_func_handler func,
6553 enum tep_func_arg_type ret_type,
6554 char *name, ...)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006555{
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006556 struct tep_function_handler *func_handle;
Tzvetomir Stoyanov (VMware)0ae98632018-08-08 14:03:09 -04006557 struct func_params **next_param;
6558 struct func_params *param;
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006559 enum tep_func_arg_type type;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006560 va_list ap;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006561 int ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006562
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006563 func_handle = find_func_handler(tep, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006564 if (func_handle) {
6565 /*
6566 * This is most like caused by the users own
6567 * plugins updating the function. This overrides the
6568 * system defaults.
6569 */
6570 pr_stat("override of function helper '%s'", name);
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006571 remove_func_handler(tep, name);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006572 }
6573
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006574 func_handle = calloc(1, sizeof(*func_handle));
Namhyung Kim67ed9392012-09-07 11:49:47 +09006575 if (!func_handle) {
6576 do_warning("Failed to allocate function handler");
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006577 return TEP_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006578 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006579
6580 func_handle->ret_type = ret_type;
6581 func_handle->name = strdup(name);
6582 func_handle->func = func;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006583 if (!func_handle->name) {
6584 do_warning("Failed to allocate function name");
6585 free(func_handle);
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006586 return TEP_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006587 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006588
6589 next_param = &(func_handle->params);
6590 va_start(ap, name);
6591 for (;;) {
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006592 type = va_arg(ap, enum tep_func_arg_type);
6593 if (type == TEP_FUNC_ARG_VOID)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006594 break;
6595
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006596 if (type >= TEP_FUNC_ARG_MAX_TYPES) {
Namhyung Kim67ed9392012-09-07 11:49:47 +09006597 do_warning("Invalid argument type %d", type);
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006598 ret = TEP_ERRNO__INVALID_ARG_TYPE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006599 goto out_free;
6600 }
6601
Namhyung Kim67ed9392012-09-07 11:49:47 +09006602 param = malloc(sizeof(*param));
6603 if (!param) {
6604 do_warning("Failed to allocate function param");
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006605 ret = TEP_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kim67ed9392012-09-07 11:49:47 +09006606 goto out_free;
6607 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006608 param->type = type;
6609 param->next = NULL;
6610
6611 *next_param = param;
6612 next_param = &(param->next);
6613
6614 func_handle->nr_args++;
6615 }
6616 va_end(ap);
6617
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006618 func_handle->next = tep->func_handlers;
6619 tep->func_handlers = func_handle;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006620
6621 return 0;
6622 out_free:
6623 va_end(ap);
6624 free_func_handle(func_handle);
Namhyung Kim67ed9392012-09-07 11:49:47 +09006625 return ret;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006626}
6627
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006628/**
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006629 * tep_unregister_print_function - unregister a helper function
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006630 * @tep: a handle to the trace event parser context
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006631 * @func: the function to process the helper function
6632 * @name: the name of the helper function
6633 *
6634 * This function removes existing print handler for function @name.
6635 *
6636 * Returns 0 if the handler was removed successully, -1 otherwise.
6637 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006638int tep_unregister_print_function(struct tep_handle *tep,
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006639 tep_func_handler func, char *name)
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006640{
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006641 struct tep_function_handler *func_handle;
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006642
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006643 func_handle = find_func_handler(tep, name);
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006644 if (func_handle && func_handle->func == func) {
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006645 remove_func_handler(tep, name);
Namhyung Kim20c7e5a2014-01-16 11:31:08 +09006646 return 0;
6647 }
6648 return -1;
6649}
6650
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006651static struct tep_event *search_event(struct tep_handle *pevent, int id,
6652 const char *sys_name,
6653 const char *event_name)
Namhyung Kimad137012014-01-16 11:31:07 +09006654{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006655 struct tep_event *event;
Namhyung Kimad137012014-01-16 11:31:07 +09006656
6657 if (id >= 0) {
6658 /* search by id */
Tzvetomir Stoyanov (VMware)af85cd12018-08-08 14:02:50 -04006659 event = tep_find_event(pevent, id);
Namhyung Kimad137012014-01-16 11:31:07 +09006660 if (!event)
6661 return NULL;
6662 if (event_name && (strcmp(event_name, event->name) != 0))
6663 return NULL;
6664 if (sys_name && (strcmp(sys_name, event->system) != 0))
6665 return NULL;
6666 } else {
Tzvetomir Stoyanov (VMware)610e1e42018-08-08 14:03:05 -04006667 event = tep_find_event_by_name(pevent, sys_name, event_name);
Namhyung Kimad137012014-01-16 11:31:07 +09006668 if (!event)
6669 return NULL;
6670 }
6671 return event;
6672}
6673
Steven Rostedtf7d82352012-04-06 00:47:53 +02006674/**
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006675 * tep_register_event_handler - register a way to parse an event
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006676 * @tep: a handle to the trace event parser context
Steven Rostedtf7d82352012-04-06 00:47:53 +02006677 * @id: the id of the event to register
6678 * @sys_name: the system name the event belongs to
6679 * @event_name: the name of the event
6680 * @func: the function to call to parse the event information
Namhyung Kim16e6b8f2012-04-23 13:58:35 +09006681 * @context: the data to be passed to @func
Steven Rostedtf7d82352012-04-06 00:47:53 +02006682 *
6683 * This function allows a developer to override the parsing of
6684 * a given event. If for some reason the default print format
6685 * is not sufficient, this function will register a function
6686 * for an event to be used to parse the data instead.
6687 *
6688 * If @id is >= 0, then it is used to find the event.
6689 * else @sys_name and @event_name are used.
Tzvetomir Stoyanovf87ce7c2018-11-30 23:08:11 -05006690 *
6691 * Returns:
6692 * TEP_REGISTER_SUCCESS_OVERWRITE if an existing handler is overwritten
6693 * TEP_REGISTER_SUCCESS if a new handler is registered successfully
6694 * negative TEP_ERRNO_... in case of an error
6695 *
Steven Rostedtf7d82352012-04-06 00:47:53 +02006696 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006697int tep_register_event_handler(struct tep_handle *tep, int id,
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006698 const char *sys_name, const char *event_name,
6699 tep_event_handler_func func, void *context)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006700{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006701 struct tep_event *event;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006702 struct event_handler *handle;
6703
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006704 event = search_event(tep, id, sys_name, event_name);
Namhyung Kimad137012014-01-16 11:31:07 +09006705 if (event == NULL)
6706 goto not_found;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006707
6708 pr_stat("overriding event (%d) %s:%s with new print handler",
6709 event->id, event->system, event->name);
6710
6711 event->handler = func;
6712 event->context = context;
Tzvetomir Stoyanovf87ce7c2018-11-30 23:08:11 -05006713 return TEP_REGISTER_SUCCESS_OVERWRITE;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006714
6715 not_found:
6716 /* Save for later use. */
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006717 handle = calloc(1, sizeof(*handle));
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006718 if (!handle) {
6719 do_warning("Failed to allocate event handler");
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006720 return TEP_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006721 }
6722
Steven Rostedtf7d82352012-04-06 00:47:53 +02006723 handle->id = id;
6724 if (event_name)
6725 handle->event_name = strdup(event_name);
6726 if (sys_name)
6727 handle->sys_name = strdup(sys_name);
6728
Namhyung Kimca638582012-04-09 11:54:31 +09006729 if ((event_name && !handle->event_name) ||
6730 (sys_name && !handle->sys_name)) {
Namhyung Kim0ca8da02012-09-07 11:49:46 +09006731 do_warning("Failed to allocate event/sys name");
6732 free((void *)handle->event_name);
6733 free((void *)handle->sys_name);
6734 free(handle);
Tzvetomir Stoyanov (VMware)d97f4ef2018-08-08 14:02:58 -04006735 return TEP_ERRNO__MEM_ALLOC_FAILED;
Namhyung Kimca638582012-04-09 11:54:31 +09006736 }
6737
Steven Rostedtf7d82352012-04-06 00:47:53 +02006738 handle->func = func;
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006739 handle->next = tep->handlers;
6740 tep->handlers = handle;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006741 handle->context = context;
6742
Tzvetomir Stoyanovf87ce7c2018-11-30 23:08:11 -05006743 return TEP_REGISTER_SUCCESS;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006744}
6745
Namhyung Kimad137012014-01-16 11:31:07 +09006746static int handle_matches(struct event_handler *handler, int id,
6747 const char *sys_name, const char *event_name,
Tzvetomir Stoyanov (VMware)c32d52b2018-08-08 14:02:48 -04006748 tep_event_handler_func func, void *context)
Namhyung Kimad137012014-01-16 11:31:07 +09006749{
6750 if (id >= 0 && id != handler->id)
6751 return 0;
6752
6753 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6754 return 0;
6755
6756 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6757 return 0;
6758
6759 if (func != handler->func || context != handler->context)
6760 return 0;
6761
6762 return 1;
6763}
6764
6765/**
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006766 * tep_unregister_event_handler - unregister an existing event handler
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006767 * @tep: a handle to the trace event parser context
Namhyung Kimad137012014-01-16 11:31:07 +09006768 * @id: the id of the event to unregister
6769 * @sys_name: the system name the handler belongs to
6770 * @event_name: the name of the event handler
6771 * @func: the function to call to parse the event information
6772 * @context: the data to be passed to @func
6773 *
6774 * This function removes existing event handler (parser).
6775 *
6776 * If @id is >= 0, then it is used to find the event.
6777 * else @sys_name and @event_name are used.
6778 *
6779 * Returns 0 if handler was removed successfully, -1 if event was not found.
6780 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006781int tep_unregister_event_handler(struct tep_handle *tep, int id,
Tzvetomir Stoyanov (VMware)b843e9c2018-08-08 14:03:02 -04006782 const char *sys_name, const char *event_name,
6783 tep_event_handler_func func, void *context)
Namhyung Kimad137012014-01-16 11:31:07 +09006784{
Tzvetomir Stoyanov97fbf3f2018-11-30 10:44:07 -05006785 struct tep_event *event;
Namhyung Kimad137012014-01-16 11:31:07 +09006786 struct event_handler *handle;
6787 struct event_handler **next;
6788
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006789 event = search_event(tep, id, sys_name, event_name);
Namhyung Kimad137012014-01-16 11:31:07 +09006790 if (event == NULL)
6791 goto not_found;
6792
6793 if (event->handler == func && event->context == context) {
6794 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6795 event->id, event->system, event->name);
6796
6797 event->handler = NULL;
6798 event->context = NULL;
6799 return 0;
6800 }
6801
6802not_found:
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006803 for (next = &tep->handlers; *next; next = &(*next)->next) {
Namhyung Kimad137012014-01-16 11:31:07 +09006804 handle = *next;
6805 if (handle_matches(handle, id, sys_name, event_name,
6806 func, context))
6807 break;
6808 }
6809
6810 if (!(*next))
6811 return -1;
6812
6813 *next = handle->next;
6814 free_handler(handle);
6815
6816 return 0;
6817}
6818
Steven Rostedtf7d82352012-04-06 00:47:53 +02006819/**
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006820 * tep_alloc - create a tep handle
Steven Rostedtf7d82352012-04-06 00:47:53 +02006821 */
Tzvetomir Stoyanov (VMware)4d5c58b2018-08-08 14:02:49 -04006822struct tep_handle *tep_alloc(void)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006823{
Tzvetomir Stoyanov (VMware)096177a2018-08-08 14:02:46 -04006824 struct tep_handle *pevent = calloc(1, sizeof(*pevent));
Steven Rostedtf7d82352012-04-06 00:47:53 +02006825
Tzvetomir Stoyanoveed14f42018-11-30 23:08:08 -05006826 if (pevent) {
Arnaldo Carvalho de Melo87162d82012-09-12 15:39:59 -03006827 pevent->ref_count = 1;
Tzvetomir Stoyanov55c34ae2019-04-01 12:43:16 -04006828 pevent->host_bigendian = tep_is_bigendian();
Tzvetomir Stoyanoveed14f42018-11-30 23:08:08 -05006829 }
Steven Rostedtf7d82352012-04-06 00:47:53 +02006830
6831 return pevent;
6832}
6833
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006834void tep_ref(struct tep_handle *tep)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006835{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006836 tep->ref_count++;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006837}
6838
Tzvetomir Stoyanov477be102018-11-30 10:44:04 -05006839int tep_get_ref(struct tep_handle *tep)
6840{
6841 if (tep)
6842 return tep->ref_count;
6843 return 0;
6844}
6845
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006846void tep_free_format_field(struct tep_format_field *field)
David Ahern00ae1122015-03-19 12:36:21 -06006847{
6848 free(field->type);
Jiri Olsad3542432015-04-18 17:50:18 +02006849 if (field->alias != field->name)
6850 free(field->alias);
David Ahern00ae1122015-03-19 12:36:21 -06006851 free(field->name);
6852 free(field);
6853}
6854
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006855static void free_format_fields(struct tep_format_field *field)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006856{
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006857 struct tep_format_field *next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006858
6859 while (field) {
6860 next = field->next;
Tzvetomir Stoyanov (VMware)8b3e0872018-08-08 14:03:04 -04006861 tep_free_format_field(field);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006862 field = next;
6863 }
6864}
6865
Tzvetomir Stoyanov (VMware)2c92f982018-09-19 14:56:45 -04006866static void free_formats(struct tep_format *format)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006867{
6868 free_format_fields(format->common_fields);
6869 free_format_fields(format->fields);
6870}
6871
Tzvetomir Stoyanovfc398512018-11-30 10:44:08 -05006872void tep_free_event(struct tep_event *event)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006873{
6874 free(event->name);
6875 free(event->system);
6876
6877 free_formats(&event->format);
6878
6879 free(event->print_fmt.format);
6880 free_args(event->print_fmt.args);
6881
6882 free(event);
6883}
6884
6885/**
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006886 * tep_free - free a tep handle
6887 * @tep: the tep handle to free
Steven Rostedtf7d82352012-04-06 00:47:53 +02006888 */
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006889void tep_free(struct tep_handle *tep)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006890{
Steven Rostedta2525a02012-04-06 00:48:02 +02006891 struct cmdline_list *cmdlist, *cmdnext;
6892 struct func_list *funclist, *funcnext;
6893 struct printk_list *printklist, *printknext;
Tzvetomir Stoyanov (VMware)3cf47782018-08-08 14:02:59 -04006894 struct tep_function_handler *func_handler;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006895 struct event_handler *handle;
6896 int i;
6897
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006898 if (!tep)
Steven Rostedta2525a02012-04-06 00:48:02 +02006899 return;
6900
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006901 cmdlist = tep->cmdlist;
6902 funclist = tep->funclist;
6903 printklist = tep->printklist;
Steven Rostedta2525a02012-04-06 00:48:02 +02006904
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006905 tep->ref_count--;
6906 if (tep->ref_count)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006907 return;
6908
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006909 if (tep->cmdlines) {
6910 for (i = 0; i < tep->cmdline_count; i++)
6911 free(tep->cmdlines[i].comm);
6912 free(tep->cmdlines);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006913 }
6914
6915 while (cmdlist) {
6916 cmdnext = cmdlist->next;
6917 free(cmdlist->comm);
6918 free(cmdlist);
6919 cmdlist = cmdnext;
6920 }
6921
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006922 if (tep->func_map) {
6923 for (i = 0; i < (int)tep->func_count; i++) {
6924 free(tep->func_map[i].func);
6925 free(tep->func_map[i].mod);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006926 }
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006927 free(tep->func_map);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006928 }
6929
6930 while (funclist) {
6931 funcnext = funclist->next;
6932 free(funclist->func);
6933 free(funclist->mod);
6934 free(funclist);
6935 funclist = funcnext;
6936 }
6937
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006938 while (tep->func_handlers) {
6939 func_handler = tep->func_handlers;
6940 tep->func_handlers = func_handler->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006941 free_func_handle(func_handler);
6942 }
6943
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006944 if (tep->printk_map) {
6945 for (i = 0; i < (int)tep->printk_count; i++)
6946 free(tep->printk_map[i].printk);
6947 free(tep->printk_map);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006948 }
6949
6950 while (printklist) {
6951 printknext = printklist->next;
6952 free(printklist->printk);
6953 free(printklist);
6954 printklist = printknext;
6955 }
6956
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006957 for (i = 0; i < tep->nr_events; i++)
6958 tep_free_event(tep->events[i]);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006959
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006960 while (tep->handlers) {
6961 handle = tep->handlers;
6962 tep->handlers = handle->next;
Steven Rostedtf7d82352012-04-06 00:47:53 +02006963 free_handler(handle);
6964 }
6965
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006966 free(tep->trace_clock);
6967 free(tep->events);
6968 free(tep->sort_events);
6969 free(tep->func_resolver);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006970
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006971 free(tep);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006972}
6973
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006974void tep_unref(struct tep_handle *tep)
Steven Rostedtf7d82352012-04-06 00:47:53 +02006975{
Tzvetomir Stoyanov047ff222019-04-01 12:43:17 -04006976 tep_free(tep);
Steven Rostedtf7d82352012-04-06 00:47:53 +02006977}