2 * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
4 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License (not later!)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses>
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 * The parts for function graph printing was taken and modified from the
21 * Linux Kernel that were written by
22 * - Copyright (C) 2009 Frederic Weisbecker,
23 * Frederic Weisbecker gave his permission to relicense the code to
24 * the Lesser General Public License.
35 #include <linux/string.h>
37 #include <netinet/in.h>
38 #include "event-parse.h"
39 #include "event-utils.h"
41 static const char *input_buf;
42 static unsigned long long input_buf_ptr;
43 static unsigned long long input_buf_siz;
45 static int is_flag_field;
46 static int is_symbolic_field;
48 static int show_warning = 1;
50 #define do_warning(fmt, ...) \
53 warning(fmt, ##__VA_ARGS__); \
56 #define do_warning_event(event, fmt, ...) \
62 warning("[%s:%s] " fmt, event->system, \
63 event->name, ##__VA_ARGS__); \
65 warning(fmt, ##__VA_ARGS__); \
68 static void init_input_buf(const char *buf, unsigned long long size)
75 const char *pevent_get_input_buf(void)
80 unsigned long long pevent_get_input_buf_ptr(void)
85 struct event_handler {
86 struct event_handler *next;
89 const char *event_name;
90 pevent_event_handler_func func;
94 struct pevent_func_params {
95 struct pevent_func_params *next;
96 enum pevent_func_arg_type type;
99 struct pevent_function_handler {
100 struct pevent_function_handler *next;
101 enum pevent_func_arg_type ret_type;
103 pevent_func_handler func;
104 struct pevent_func_params *params;
108 static unsigned long long
109 process_defined_func(struct trace_seq *s, void *data, int size,
110 struct event_format *event, struct print_arg *arg);
112 static void free_func_handle(struct pevent_function_handler *func);
115 * pevent_buffer_init - init buffer for parsing
116 * @buf: buffer to parse
117 * @size: the size of the buffer
119 * For use with pevent_read_token(), this initializes the internal
120 * buffer that pevent_read_token() will parse.
122 void pevent_buffer_init(const char *buf, unsigned long long size)
124 init_input_buf(buf, size);
127 void breakpoint(void)
133 struct print_arg *alloc_arg(void)
135 return calloc(1, sizeof(struct print_arg));
143 static int cmdline_cmp(const void *a, const void *b)
145 const struct cmdline *ca = a;
146 const struct cmdline *cb = b;
148 if (ca->pid < cb->pid)
150 if (ca->pid > cb->pid)
156 struct cmdline_list {
157 struct cmdline_list *next;
162 static int cmdline_init(struct pevent *pevent)
164 struct cmdline_list *cmdlist = pevent->cmdlist;
165 struct cmdline_list *item;
166 struct cmdline *cmdlines;
169 cmdlines = malloc(sizeof(*cmdlines) * pevent->cmdline_count);
175 cmdlines[i].pid = cmdlist->pid;
176 cmdlines[i].comm = cmdlist->comm;
179 cmdlist = cmdlist->next;
183 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
185 pevent->cmdlines = cmdlines;
186 pevent->cmdlist = NULL;
191 static const char *find_cmdline(struct pevent *pevent, int pid)
193 const struct cmdline *comm;
199 if (!pevent->cmdlines && cmdline_init(pevent))
200 return "<not enough memory for cmdlines!>";
204 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
205 sizeof(*pevent->cmdlines), cmdline_cmp);
213 * pevent_pid_is_registered - return if a pid has a cmdline registered
214 * @pevent: handle for the pevent
215 * @pid: The pid to check if it has a cmdline registered with.
217 * Returns 1 if the pid has a cmdline mapped to it
220 int pevent_pid_is_registered(struct pevent *pevent, int pid)
222 const struct cmdline *comm;
228 if (!pevent->cmdlines && cmdline_init(pevent))
233 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
234 sizeof(*pevent->cmdlines), cmdline_cmp);
242 * If the command lines have been converted to an array, then
243 * we must add this pid. This is much slower than when cmdlines
244 * are added before the array is initialized.
246 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
248 struct cmdline *cmdlines = pevent->cmdlines;
249 const struct cmdline *cmdline;
255 /* avoid duplicates */
258 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
259 sizeof(*pevent->cmdlines), cmdline_cmp);
265 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
271 cmdlines[pevent->cmdline_count].comm = strdup(comm);
272 if (!cmdlines[pevent->cmdline_count].comm) {
278 cmdlines[pevent->cmdline_count].pid = pid;
280 if (cmdlines[pevent->cmdline_count].comm)
281 pevent->cmdline_count++;
283 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
284 pevent->cmdlines = cmdlines;
290 * pevent_register_comm - register a pid / comm mapping
291 * @pevent: handle for the pevent
292 * @comm: the command line to register
293 * @pid: the pid to map the command line to
295 * This adds a mapping to search for command line names with
296 * a given pid. The comm is duplicated.
298 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
300 struct cmdline_list *item;
302 if (pevent->cmdlines)
303 return add_new_comm(pevent, comm, pid);
305 item = malloc(sizeof(*item));
310 item->comm = strdup(comm);
312 item->comm = strdup("<...>");
318 item->next = pevent->cmdlist;
320 pevent->cmdlist = item;
321 pevent->cmdline_count++;
326 int pevent_register_trace_clock(struct pevent *pevent, const char *trace_clock)
328 pevent->trace_clock = strdup(trace_clock);
329 if (!pevent->trace_clock) {
337 unsigned long long addr;
343 struct func_list *next;
344 unsigned long long addr;
349 static int func_cmp(const void *a, const void *b)
351 const struct func_map *fa = a;
352 const struct func_map *fb = b;
354 if (fa->addr < fb->addr)
356 if (fa->addr > fb->addr)
363 * We are searching for a record in between, not an exact
366 static int func_bcmp(const void *a, const void *b)
368 const struct func_map *fa = a;
369 const struct func_map *fb = b;
371 if ((fa->addr == fb->addr) ||
373 (fa->addr > fb->addr &&
374 fa->addr < (fb+1)->addr))
377 if (fa->addr < fb->addr)
383 static int func_map_init(struct pevent *pevent)
385 struct func_list *funclist;
386 struct func_list *item;
387 struct func_map *func_map;
390 func_map = malloc(sizeof(*func_map) * (pevent->func_count + 1));
394 funclist = pevent->funclist;
398 func_map[i].func = funclist->func;
399 func_map[i].addr = funclist->addr;
400 func_map[i].mod = funclist->mod;
403 funclist = funclist->next;
407 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
410 * Add a special record at the end.
412 func_map[pevent->func_count].func = NULL;
413 func_map[pevent->func_count].addr = 0;
414 func_map[pevent->func_count].mod = NULL;
416 pevent->func_map = func_map;
417 pevent->funclist = NULL;
422 static struct func_map *
423 __find_func(struct pevent *pevent, unsigned long long addr)
425 struct func_map *func;
428 if (!pevent->func_map)
429 func_map_init(pevent);
433 func = bsearch(&key, pevent->func_map, pevent->func_count,
434 sizeof(*pevent->func_map), func_bcmp);
439 struct func_resolver {
440 pevent_func_resolver_t *func;
446 * pevent_set_function_resolver - set an alternative function resolver
447 * @pevent: handle for the pevent
448 * @resolver: function to be used
449 * @priv: resolver function private state.
451 * Some tools may have already a way to resolve kernel functions, allow them to
452 * keep using it instead of duplicating all the entries inside
455 int pevent_set_function_resolver(struct pevent *pevent,
456 pevent_func_resolver_t *func, void *priv)
458 struct func_resolver *resolver = malloc(sizeof(*resolver));
460 if (resolver == NULL)
463 resolver->func = func;
464 resolver->priv = priv;
466 free(pevent->func_resolver);
467 pevent->func_resolver = resolver;
473 * pevent_reset_function_resolver - reset alternative function resolver
474 * @pevent: handle for the pevent
476 * Stop using whatever alternative resolver was set, use the default
479 void pevent_reset_function_resolver(struct pevent *pevent)
481 free(pevent->func_resolver);
482 pevent->func_resolver = NULL;
485 static struct func_map *
486 find_func(struct pevent *pevent, unsigned long long addr)
488 struct func_map *map;
490 if (!pevent->func_resolver)
491 return __find_func(pevent, addr);
493 map = &pevent->func_resolver->map;
496 map->func = pevent->func_resolver->func(pevent->func_resolver->priv,
497 &map->addr, &map->mod);
498 if (map->func == NULL)
505 * pevent_find_function - find a function by a given address
506 * @pevent: handle for the pevent
507 * @addr: the address to find the function with
509 * Returns a pointer to the function stored that has the given
510 * address. Note, the address does not have to be exact, it
511 * will select the function that would contain the address.
513 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
515 struct func_map *map;
517 map = find_func(pevent, addr);
525 * pevent_find_function_address - find a function address by a given address
526 * @pevent: handle for the pevent
527 * @addr: the address to find the function with
529 * Returns the address the function starts at. This can be used in
530 * conjunction with pevent_find_function to print both the function
531 * name and the function offset.
534 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
536 struct func_map *map;
538 map = find_func(pevent, addr);
546 * pevent_register_function - register a function with a given address
547 * @pevent: handle for the pevent
548 * @function: the function name to register
549 * @addr: the address the function starts at
550 * @mod: the kernel module the function may be in (NULL for none)
552 * This registers a function name with an address and module.
553 * The @func passed in is duplicated.
555 int pevent_register_function(struct pevent *pevent, char *func,
556 unsigned long long addr, char *mod)
558 struct func_list *item = malloc(sizeof(*item));
563 item->next = pevent->funclist;
564 item->func = strdup(func);
569 item->mod = strdup(mod);
576 pevent->funclist = item;
577 pevent->func_count++;
591 * pevent_print_funcs - print out the stored functions
592 * @pevent: handle for the pevent
594 * This prints out the stored functions.
596 void pevent_print_funcs(struct pevent *pevent)
600 if (!pevent->func_map)
601 func_map_init(pevent);
603 for (i = 0; i < (int)pevent->func_count; i++) {
605 pevent->func_map[i].addr,
606 pevent->func_map[i].func);
607 if (pevent->func_map[i].mod)
608 printf(" [%s]\n", pevent->func_map[i].mod);
615 unsigned long long addr;
620 struct printk_list *next;
621 unsigned long long addr;
625 static int printk_cmp(const void *a, const void *b)
627 const struct printk_map *pa = a;
628 const struct printk_map *pb = b;
630 if (pa->addr < pb->addr)
632 if (pa->addr > pb->addr)
638 static int printk_map_init(struct pevent *pevent)
640 struct printk_list *printklist;
641 struct printk_list *item;
642 struct printk_map *printk_map;
645 printk_map = malloc(sizeof(*printk_map) * (pevent->printk_count + 1));
649 printklist = pevent->printklist;
653 printk_map[i].printk = printklist->printk;
654 printk_map[i].addr = printklist->addr;
657 printklist = printklist->next;
661 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
663 pevent->printk_map = printk_map;
664 pevent->printklist = NULL;
669 static struct printk_map *
670 find_printk(struct pevent *pevent, unsigned long long addr)
672 struct printk_map *printk;
673 struct printk_map key;
675 if (!pevent->printk_map && printk_map_init(pevent))
680 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
681 sizeof(*pevent->printk_map), printk_cmp);
687 * pevent_register_print_string - register a string by its address
688 * @pevent: handle for the pevent
689 * @fmt: the string format to register
690 * @addr: the address the string was located at
692 * This registers a string by the address it was stored in the kernel.
693 * The @fmt passed in is duplicated.
695 int pevent_register_print_string(struct pevent *pevent, const char *fmt,
696 unsigned long long addr)
698 struct printk_list *item = malloc(sizeof(*item));
704 item->next = pevent->printklist;
707 /* Strip off quotes and '\n' from the end */
710 item->printk = strdup(fmt);
714 p = item->printk + strlen(item->printk) - 1;
719 if (strcmp(p, "\\n") == 0)
722 pevent->printklist = item;
723 pevent->printk_count++;
734 * pevent_print_printk - print out the stored strings
735 * @pevent: handle for the pevent
737 * This prints the string formats that were stored.
739 void pevent_print_printk(struct pevent *pevent)
743 if (!pevent->printk_map)
744 printk_map_init(pevent);
746 for (i = 0; i < (int)pevent->printk_count; i++) {
747 printf("%016llx %s\n",
748 pevent->printk_map[i].addr,
749 pevent->printk_map[i].printk);
753 static struct event_format *alloc_event(void)
755 return calloc(1, sizeof(struct event_format));
758 static int add_event(struct pevent *pevent, struct event_format *event)
761 struct event_format **events = realloc(pevent->events, sizeof(event) *
762 (pevent->nr_events + 1));
766 pevent->events = events;
768 for (i = 0; i < pevent->nr_events; i++) {
769 if (pevent->events[i]->id > event->id)
772 if (i < pevent->nr_events)
773 memmove(&pevent->events[i + 1],
775 sizeof(event) * (pevent->nr_events - i));
777 pevent->events[i] = event;
780 event->pevent = pevent;
785 static int event_item_type(enum event_type type)
788 case EVENT_ITEM ... EVENT_SQUOTE:
790 case EVENT_ERROR ... EVENT_DELIM:
796 static void free_flag_sym(struct print_flag_sym *fsym)
798 struct print_flag_sym *next;
809 static void free_arg(struct print_arg *arg)
811 struct print_arg *farg;
818 free(arg->atom.atom);
821 free(arg->field.name);
824 free_arg(arg->flags.field);
825 free(arg->flags.delim);
826 free_flag_sym(arg->flags.flags);
829 free_arg(arg->symbol.field);
830 free_flag_sym(arg->symbol.symbols);
833 free_arg(arg->hex.field);
834 free_arg(arg->hex.size);
836 case PRINT_INT_ARRAY:
837 free_arg(arg->int_array.field);
838 free_arg(arg->int_array.count);
839 free_arg(arg->int_array.el_size);
842 free(arg->typecast.type);
843 free_arg(arg->typecast.item);
847 free(arg->string.string);
850 free(arg->bitmask.bitmask);
852 case PRINT_DYNAMIC_ARRAY:
853 case PRINT_DYNAMIC_ARRAY_LEN:
854 free(arg->dynarray.index);
858 free_arg(arg->op.left);
859 free_arg(arg->op.right);
862 while (arg->func.args) {
863 farg = arg->func.args;
864 arg->func.args = farg->next;
877 static enum event_type get_type(int ch)
880 return EVENT_NEWLINE;
883 if (isalnum(ch) || ch == '_')
891 if (ch == '(' || ch == ')' || ch == ',')
897 static int __read_char(void)
899 if (input_buf_ptr >= input_buf_siz)
902 return input_buf[input_buf_ptr++];
905 static int __peek_char(void)
907 if (input_buf_ptr >= input_buf_siz)
910 return input_buf[input_buf_ptr];
914 * pevent_peek_char - peek at the next character that will be read
916 * Returns the next character read, or -1 if end of buffer.
918 int pevent_peek_char(void)
920 return __peek_char();
923 static int extend_token(char **tok, char *buf, int size)
925 char *newtok = realloc(*tok, size);
942 static enum event_type force_token(const char *str, char **tok);
944 static enum event_type __read_token(char **tok)
947 int ch, last_ch, quote_ch, next_ch;
950 enum event_type type;
960 if (type == EVENT_NONE)
968 if (asprintf(tok, "%c", ch) < 0)
976 next_ch = __peek_char();
977 if (next_ch == '>') {
978 buf[i++] = __read_char();
991 buf[i++] = __read_char();
1003 default: /* what should we do instead? */
1013 buf[i++] = __read_char();
1018 /* don't keep quotes */
1024 if (i == (BUFSIZ - 1)) {
1028 if (extend_token(tok, buf, tok_size) < 0)
1035 /* the '\' '\' will cancel itself */
1036 if (ch == '\\' && last_ch == '\\')
1038 } while (ch != quote_ch || last_ch == '\\');
1039 /* remove the last quote */
1043 * For strings (double quotes) check the next token.
1044 * If it is another string, concatinate the two.
1046 if (type == EVENT_DQUOTE) {
1047 unsigned long long save_input_buf_ptr = input_buf_ptr;
1051 } while (isspace(ch));
1054 input_buf_ptr = save_input_buf_ptr;
1059 case EVENT_ERROR ... EVENT_SPACE:
1065 while (get_type(__peek_char()) == type) {
1066 if (i == (BUFSIZ - 1)) {
1070 if (extend_token(tok, buf, tok_size) < 0)
1080 if (extend_token(tok, buf, tok_size + i + 1) < 0)
1083 if (type == EVENT_ITEM) {
1085 * Older versions of the kernel has a bug that
1086 * creates invalid symbols and will break the mac80211
1087 * parsing. This is a work around to that bug.
1089 * See Linux kernel commit:
1090 * 811cb50baf63461ce0bdb234927046131fc7fa8b
1092 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
1095 return force_token("\"\%s\" ", tok);
1096 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
1099 return force_token("\" sta:%pM\" ", tok);
1100 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
1103 return force_token("\" vif:%p(%d)\" ", tok);
1110 static enum event_type force_token(const char *str, char **tok)
1112 const char *save_input_buf;
1113 unsigned long long save_input_buf_ptr;
1114 unsigned long long save_input_buf_siz;
1115 enum event_type type;
1117 /* save off the current input pointers */
1118 save_input_buf = input_buf;
1119 save_input_buf_ptr = input_buf_ptr;
1120 save_input_buf_siz = input_buf_siz;
1122 init_input_buf(str, strlen(str));
1124 type = __read_token(tok);
1126 /* reset back to original token */
1127 input_buf = save_input_buf;
1128 input_buf_ptr = save_input_buf_ptr;
1129 input_buf_siz = save_input_buf_siz;
1134 static void free_token(char *tok)
1140 static enum event_type read_token(char **tok)
1142 enum event_type type;
1145 type = __read_token(tok);
1146 if (type != EVENT_SPACE)
1158 * pevent_read_token - access to utilites to use the pevent parser
1159 * @tok: The token to return
1161 * This will parse tokens from the string given by
1162 * pevent_init_data().
1164 * Returns the token type.
1166 enum event_type pevent_read_token(char **tok)
1168 return read_token(tok);
1172 * pevent_free_token - free a token returned by pevent_read_token
1173 * @token: the token to free
1175 void pevent_free_token(char *token)
1181 static enum event_type read_token_item(char **tok)
1183 enum event_type type;
1186 type = __read_token(tok);
1187 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1198 static int test_type(enum event_type type, enum event_type expect)
1200 if (type != expect) {
1201 do_warning("Error: expected type %d but read %d",
1208 static int test_type_token(enum event_type type, const char *token,
1209 enum event_type expect, const char *expect_tok)
1211 if (type != expect) {
1212 do_warning("Error: expected type %d but read %d",
1217 if (strcmp(token, expect_tok) != 0) {
1218 do_warning("Error: expected '%s' but read '%s'",
1225 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1227 enum event_type type;
1230 type = read_token(tok);
1232 type = read_token_item(tok);
1233 return test_type(type, expect);
1236 static int read_expect_type(enum event_type expect, char **tok)
1238 return __read_expect_type(expect, tok, 1);
1241 static int __read_expected(enum event_type expect, const char *str,
1244 enum event_type type;
1249 type = read_token(&token);
1251 type = read_token_item(&token);
1253 ret = test_type_token(type, token, expect, str);
1260 static int read_expected(enum event_type expect, const char *str)
1262 return __read_expected(expect, str, 1);
1265 static int read_expected_item(enum event_type expect, const char *str)
1267 return __read_expected(expect, str, 0);
1270 static char *event_read_name(void)
1274 if (read_expected(EVENT_ITEM, "name") < 0)
1277 if (read_expected(EVENT_OP, ":") < 0)
1280 if (read_expect_type(EVENT_ITEM, &token) < 0)
1290 static int event_read_id(void)
1295 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1298 if (read_expected(EVENT_OP, ":") < 0)
1301 if (read_expect_type(EVENT_ITEM, &token) < 0)
1304 id = strtoul(token, NULL, 0);
1313 static int field_is_string(struct format_field *field)
1315 if ((field->flags & FIELD_IS_ARRAY) &&
1316 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1317 strstr(field->type, "s8")))
1323 static int field_is_dynamic(struct format_field *field)
1325 if (strncmp(field->type, "__data_loc", 10) == 0)
1331 static int field_is_long(struct format_field *field)
1333 /* includes long long */
1334 if (strstr(field->type, "long"))
1340 static unsigned int type_size(const char *name)
1342 /* This covers all FIELD_IS_STRING types. */
1360 for (i = 0; table[i].type; i++) {
1361 if (!strcmp(table[i].type, name))
1362 return table[i].size;
1368 static int event_read_fields(struct event_format *event, struct format_field **fields)
1370 struct format_field *field = NULL;
1371 enum event_type type;
1377 unsigned int size_dynamic = 0;
1379 type = read_token(&token);
1380 if (type == EVENT_NEWLINE) {
1387 if (test_type_token(type, token, EVENT_ITEM, "field"))
1391 type = read_token(&token);
1393 * The ftrace fields may still use the "special" name.
1396 if (event->flags & EVENT_FL_ISFTRACE &&
1397 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1399 type = read_token(&token);
1402 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1406 if (read_expect_type(EVENT_ITEM, &token) < 0)
1411 field = calloc(1, sizeof(*field));
1415 field->event = event;
1417 /* read the rest of the type */
1419 type = read_token(&token);
1420 if (type == EVENT_ITEM ||
1421 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1423 * Some of the ftrace fields are broken and have
1424 * an illegal "." in them.
1426 (event->flags & EVENT_FL_ISFTRACE &&
1427 type == EVENT_OP && strcmp(token, ".") == 0)) {
1429 if (strcmp(token, "*") == 0)
1430 field->flags |= FIELD_IS_POINTER;
1434 new_type = realloc(field->type,
1435 strlen(field->type) +
1436 strlen(last_token) + 2);
1441 field->type = new_type;
1442 strcat(field->type, " ");
1443 strcat(field->type, last_token);
1446 field->type = last_token;
1455 do_warning_event(event, "%s: no type found", __func__);
1458 field->name = field->alias = last_token;
1460 if (test_type(type, EVENT_OP))
1463 if (strcmp(token, "[") == 0) {
1464 enum event_type last_type = type;
1465 char *brackets = token;
1469 field->flags |= FIELD_IS_ARRAY;
1471 type = read_token(&token);
1473 if (type == EVENT_ITEM)
1474 field->arraylen = strtoul(token, NULL, 0);
1476 field->arraylen = 0;
1478 while (strcmp(token, "]") != 0) {
1479 if (last_type == EVENT_ITEM &&
1486 new_brackets = realloc(brackets,
1488 strlen(token) + len);
1489 if (!new_brackets) {
1493 brackets = new_brackets;
1495 strcat(brackets, " ");
1496 strcat(brackets, token);
1497 /* We only care about the last token */
1498 field->arraylen = strtoul(token, NULL, 0);
1500 type = read_token(&token);
1501 if (type == EVENT_NONE) {
1502 do_warning_event(event, "failed to find token");
1509 new_brackets = realloc(brackets, strlen(brackets) + 2);
1510 if (!new_brackets) {
1514 brackets = new_brackets;
1515 strcat(brackets, "]");
1517 /* add brackets to type */
1519 type = read_token(&token);
1521 * If the next token is not an OP, then it is of
1522 * the format: type [] item;
1524 if (type == EVENT_ITEM) {
1526 new_type = realloc(field->type,
1527 strlen(field->type) +
1528 strlen(field->name) +
1529 strlen(brackets) + 2);
1534 field->type = new_type;
1535 strcat(field->type, " ");
1536 strcat(field->type, field->name);
1537 size_dynamic = type_size(field->name);
1538 free_token(field->name);
1539 strcat(field->type, brackets);
1540 field->name = field->alias = token;
1541 type = read_token(&token);
1544 new_type = realloc(field->type,
1545 strlen(field->type) +
1546 strlen(brackets) + 1);
1551 field->type = new_type;
1552 strcat(field->type, brackets);
1557 if (field_is_string(field))
1558 field->flags |= FIELD_IS_STRING;
1559 if (field_is_dynamic(field))
1560 field->flags |= FIELD_IS_DYNAMIC;
1561 if (field_is_long(field))
1562 field->flags |= FIELD_IS_LONG;
1564 if (test_type_token(type, token, EVENT_OP, ";"))
1568 if (read_expected(EVENT_ITEM, "offset") < 0)
1571 if (read_expected(EVENT_OP, ":") < 0)
1574 if (read_expect_type(EVENT_ITEM, &token))
1576 field->offset = strtoul(token, NULL, 0);
1579 if (read_expected(EVENT_OP, ";") < 0)
1582 if (read_expected(EVENT_ITEM, "size") < 0)
1585 if (read_expected(EVENT_OP, ":") < 0)
1588 if (read_expect_type(EVENT_ITEM, &token))
1590 field->size = strtoul(token, NULL, 0);
1593 if (read_expected(EVENT_OP, ";") < 0)
1596 type = read_token(&token);
1597 if (type != EVENT_NEWLINE) {
1598 /* newer versions of the kernel have a "signed" type */
1599 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1604 if (read_expected(EVENT_OP, ":") < 0)
1607 if (read_expect_type(EVENT_ITEM, &token))
1610 if (strtoul(token, NULL, 0))
1611 field->flags |= FIELD_IS_SIGNED;
1614 if (read_expected(EVENT_OP, ";") < 0)
1617 if (read_expect_type(EVENT_NEWLINE, &token))
1623 if (field->flags & FIELD_IS_ARRAY) {
1624 if (field->arraylen)
1625 field->elementsize = field->size / field->arraylen;
1626 else if (field->flags & FIELD_IS_DYNAMIC)
1627 field->elementsize = size_dynamic;
1628 else if (field->flags & FIELD_IS_STRING)
1629 field->elementsize = 1;
1630 else if (field->flags & FIELD_IS_LONG)
1631 field->elementsize = event->pevent ?
1632 event->pevent->long_size :
1635 field->elementsize = field->size;
1638 fields = &field->next;
1655 static int event_read_format(struct event_format *event)
1660 if (read_expected_item(EVENT_ITEM, "format") < 0)
1663 if (read_expected(EVENT_OP, ":") < 0)
1666 if (read_expect_type(EVENT_NEWLINE, &token))
1670 ret = event_read_fields(event, &event->format.common_fields);
1673 event->format.nr_common = ret;
1675 ret = event_read_fields(event, &event->format.fields);
1678 event->format.nr_fields = ret;
1687 static enum event_type
1688 process_arg_token(struct event_format *event, struct print_arg *arg,
1689 char **tok, enum event_type type);
1691 static enum event_type
1692 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1694 enum event_type type;
1697 type = read_token(&token);
1700 return process_arg_token(event, arg, tok, type);
1703 static enum event_type
1704 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1707 * For __print_symbolic() and __print_flags, we need to completely
1708 * evaluate the first argument, which defines what to print next.
1710 static enum event_type
1711 process_field_arg(struct event_format *event, struct print_arg *arg, char **tok)
1713 enum event_type type;
1715 type = process_arg(event, arg, tok);
1717 while (type == EVENT_OP) {
1718 type = process_op(event, arg, tok);
1724 static enum event_type
1725 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1727 struct print_arg *arg, *left, *right;
1728 enum event_type type;
1733 right = alloc_arg();
1735 if (!arg || !left || !right) {
1736 do_warning_event(event, "%s: not enough memory!", __func__);
1737 /* arg will be freed at out_free */
1743 arg->type = PRINT_OP;
1744 arg->op.left = left;
1745 arg->op.right = right;
1748 type = process_arg(event, left, &token);
1751 if (type == EVENT_ERROR)
1754 /* Handle other operations in the arguments */
1755 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1756 type = process_op(event, left, &token);
1760 if (test_type_token(type, token, EVENT_OP, ":"))
1765 type = process_arg(event, right, &token);
1767 top->op.right = arg;
1773 /* Top may point to itself */
1774 top->op.right = NULL;
1780 static enum event_type
1781 process_array(struct event_format *event, struct print_arg *top, char **tok)
1783 struct print_arg *arg;
1784 enum event_type type;
1789 do_warning_event(event, "%s: not enough memory!", __func__);
1790 /* '*tok' is set to top->op.op. No need to free. */
1796 type = process_arg(event, arg, &token);
1797 if (test_type_token(type, token, EVENT_OP, "]"))
1800 top->op.right = arg;
1803 type = read_token_item(&token);
1814 static int get_op_prio(char *op)
1828 /* '>>' and '<<' are 8 */
1832 /* '==' and '!=' are 10 */
1842 do_warning("unknown op '%c'", op[0]);
1846 if (strcmp(op, "++") == 0 ||
1847 strcmp(op, "--") == 0) {
1849 } else if (strcmp(op, ">>") == 0 ||
1850 strcmp(op, "<<") == 0) {
1852 } else if (strcmp(op, ">=") == 0 ||
1853 strcmp(op, "<=") == 0) {
1855 } else if (strcmp(op, "==") == 0 ||
1856 strcmp(op, "!=") == 0) {
1858 } else if (strcmp(op, "&&") == 0) {
1860 } else if (strcmp(op, "||") == 0) {
1863 do_warning("unknown op '%s'", op);
1869 static int set_op_prio(struct print_arg *arg)
1872 /* single ops are the greatest */
1873 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1876 arg->op.prio = get_op_prio(arg->op.op);
1878 return arg->op.prio;
1881 /* Note, *tok does not get freed, but will most likely be saved */
1882 static enum event_type
1883 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1885 struct print_arg *left, *right = NULL;
1886 enum event_type type;
1889 /* the op is passed in via tok */
1892 if (arg->type == PRINT_OP && !arg->op.left) {
1893 /* handle single op */
1895 do_warning_event(event, "bad op token %s", token);
1905 do_warning_event(event, "bad op token %s", token);
1910 /* make an empty left */
1915 left->type = PRINT_NULL;
1916 arg->op.left = left;
1918 right = alloc_arg();
1922 arg->op.right = right;
1924 /* do not free the token, it belongs to an op */
1926 type = process_arg(event, right, tok);
1928 } else if (strcmp(token, "?") == 0) {
1934 /* copy the top arg to the left */
1937 arg->type = PRINT_OP;
1939 arg->op.left = left;
1942 /* it will set arg->op.right */
1943 type = process_cond(event, arg, tok);
1945 } else if (strcmp(token, ">>") == 0 ||
1946 strcmp(token, "<<") == 0 ||
1947 strcmp(token, "&") == 0 ||
1948 strcmp(token, "|") == 0 ||
1949 strcmp(token, "&&") == 0 ||
1950 strcmp(token, "||") == 0 ||
1951 strcmp(token, "-") == 0 ||
1952 strcmp(token, "+") == 0 ||
1953 strcmp(token, "*") == 0 ||
1954 strcmp(token, "^") == 0 ||
1955 strcmp(token, "/") == 0 ||
1956 strcmp(token, "%") == 0 ||
1957 strcmp(token, "<") == 0 ||
1958 strcmp(token, ">") == 0 ||
1959 strcmp(token, "<=") == 0 ||
1960 strcmp(token, ">=") == 0 ||
1961 strcmp(token, "==") == 0 ||
1962 strcmp(token, "!=") == 0) {
1968 /* copy the top arg to the left */
1971 arg->type = PRINT_OP;
1973 arg->op.left = left;
1974 arg->op.right = NULL;
1976 if (set_op_prio(arg) == -1) {
1977 event->flags |= EVENT_FL_FAILED;
1978 /* arg->op.op (= token) will be freed at out_free */
1983 type = read_token_item(&token);
1986 /* could just be a type pointer */
1987 if ((strcmp(arg->op.op, "*") == 0) &&
1988 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1991 if (left->type != PRINT_ATOM) {
1992 do_warning_event(event, "bad pointer type");
1995 new_atom = realloc(left->atom.atom,
1996 strlen(left->atom.atom) + 3);
2000 left->atom.atom = new_atom;
2001 strcat(left->atom.atom, " *");
2009 right = alloc_arg();
2013 type = process_arg_token(event, right, tok, type);
2014 if (type == EVENT_ERROR) {
2016 /* token was freed in process_arg_token() via *tok */
2021 if (right->type == PRINT_OP &&
2022 get_op_prio(arg->op.op) < get_op_prio(right->op.op)) {
2023 struct print_arg tmp;
2025 /* rotate ops according to the priority */
2026 arg->op.right = right->op.left;
2032 arg->op.left = right;
2034 arg->op.right = right;
2037 } else if (strcmp(token, "[") == 0) {
2045 arg->type = PRINT_OP;
2047 arg->op.left = left;
2051 /* it will set arg->op.right */
2052 type = process_array(event, arg, tok);
2055 do_warning_event(event, "unknown op '%s'", token);
2056 event->flags |= EVENT_FL_FAILED;
2057 /* the arg is now the left side */
2061 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
2064 /* higher prios need to be closer to the root */
2065 prio = get_op_prio(*tok);
2067 if (prio > arg->op.prio)
2068 return process_op(event, arg, tok);
2070 return process_op(event, right, tok);
2076 do_warning_event(event, "%s: not enough memory!", __func__);
2083 static enum event_type
2084 process_entry(struct event_format *event __maybe_unused, struct print_arg *arg,
2087 enum event_type type;
2091 if (read_expected(EVENT_OP, "->") < 0)
2094 if (read_expect_type(EVENT_ITEM, &token) < 0)
2098 arg->type = PRINT_FIELD;
2099 arg->field.name = field;
2101 if (is_flag_field) {
2102 arg->field.field = pevent_find_any_field(event, arg->field.name);
2103 arg->field.field->flags |= FIELD_IS_FLAG;
2105 } else if (is_symbolic_field) {
2106 arg->field.field = pevent_find_any_field(event, arg->field.name);
2107 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
2108 is_symbolic_field = 0;
2111 type = read_token(&token);
2123 static int alloc_and_process_delim(struct event_format *event, char *next_token,
2124 struct print_arg **print_arg)
2126 struct print_arg *field;
2127 enum event_type type;
2131 field = alloc_arg();
2133 do_warning_event(event, "%s: not enough memory!", __func__);
2138 type = process_arg(event, field, &token);
2140 if (test_type_token(type, token, EVENT_DELIM, next_token)) {
2144 goto out_free_token;
2155 static char *arg_eval (struct print_arg *arg);
2157 static unsigned long long
2158 eval_type_str(unsigned long long val, const char *type, int pointer)
2168 if (type[len-1] != '*') {
2169 do_warning("pointer expected with non pointer type");
2175 do_warning("%s: not enough memory!", __func__);
2178 memcpy(ref, type, len);
2180 /* chop off the " *" */
2183 val = eval_type_str(val, ref, 0);
2188 /* check if this is a pointer */
2189 if (type[len - 1] == '*')
2192 /* Try to figure out the arg size*/
2193 if (strncmp(type, "struct", 6) == 0)
2197 if (strcmp(type, "u8") == 0)
2200 if (strcmp(type, "u16") == 0)
2201 return val & 0xffff;
2203 if (strcmp(type, "u32") == 0)
2204 return val & 0xffffffff;
2206 if (strcmp(type, "u64") == 0 ||
2207 strcmp(type, "s64"))
2210 if (strcmp(type, "s8") == 0)
2211 return (unsigned long long)(char)val & 0xff;
2213 if (strcmp(type, "s16") == 0)
2214 return (unsigned long long)(short)val & 0xffff;
2216 if (strcmp(type, "s32") == 0)
2217 return (unsigned long long)(int)val & 0xffffffff;
2219 if (strncmp(type, "unsigned ", 9) == 0) {
2224 if (strcmp(type, "char") == 0) {
2226 return (unsigned long long)(char)val & 0xff;
2231 if (strcmp(type, "short") == 0) {
2233 return (unsigned long long)(short)val & 0xffff;
2235 return val & 0xffff;
2238 if (strcmp(type, "int") == 0) {
2240 return (unsigned long long)(int)val & 0xffffffff;
2242 return val & 0xffffffff;
2249 * Try to figure out the type.
2251 static unsigned long long
2252 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
2254 if (arg->type != PRINT_TYPE) {
2255 do_warning("expected type argument");
2259 return eval_type_str(val, arg->typecast.type, pointer);
2262 static int arg_num_eval(struct print_arg *arg, long long *val)
2264 long long left, right;
2267 switch (arg->type) {
2269 *val = strtoll(arg->atom.atom, NULL, 0);
2272 ret = arg_num_eval(arg->typecast.item, val);
2275 *val = eval_type(*val, arg, 0);
2278 switch (arg->op.op[0]) {
2280 ret = arg_num_eval(arg->op.left, &left);
2283 ret = arg_num_eval(arg->op.right, &right);
2287 *val = left || right;
2289 *val = left | right;
2292 ret = arg_num_eval(arg->op.left, &left);
2295 ret = arg_num_eval(arg->op.right, &right);
2299 *val = left && right;
2301 *val = left & right;
2304 ret = arg_num_eval(arg->op.left, &left);
2307 ret = arg_num_eval(arg->op.right, &right);
2310 switch (arg->op.op[1]) {
2312 *val = left < right;
2315 *val = left << right;
2318 *val = left <= right;
2321 do_warning("unknown op '%s'", arg->op.op);
2326 ret = arg_num_eval(arg->op.left, &left);
2329 ret = arg_num_eval(arg->op.right, &right);
2332 switch (arg->op.op[1]) {
2334 *val = left > right;
2337 *val = left >> right;
2340 *val = left >= right;
2343 do_warning("unknown op '%s'", arg->op.op);
2348 ret = arg_num_eval(arg->op.left, &left);
2351 ret = arg_num_eval(arg->op.right, &right);
2355 if (arg->op.op[1] != '=') {
2356 do_warning("unknown op '%s'", arg->op.op);
2359 *val = left == right;
2362 ret = arg_num_eval(arg->op.left, &left);
2365 ret = arg_num_eval(arg->op.right, &right);
2369 switch (arg->op.op[1]) {
2371 *val = left != right;
2374 do_warning("unknown op '%s'", arg->op.op);
2379 /* check for negative */
2380 if (arg->op.left->type == PRINT_NULL)
2383 ret = arg_num_eval(arg->op.left, &left);
2386 ret = arg_num_eval(arg->op.right, &right);
2389 *val = left - right;
2392 if (arg->op.left->type == PRINT_NULL)
2395 ret = arg_num_eval(arg->op.left, &left);
2398 ret = arg_num_eval(arg->op.right, &right);
2401 *val = left + right;
2404 ret = arg_num_eval(arg->op.right, &right);
2410 do_warning("unknown op '%s'", arg->op.op);
2416 case PRINT_FIELD ... PRINT_SYMBOL:
2421 do_warning("invalid eval type %d", arg->type);
2428 static char *arg_eval (struct print_arg *arg)
2431 static char buf[20];
2433 switch (arg->type) {
2435 return arg->atom.atom;
2437 return arg_eval(arg->typecast.item);
2439 if (!arg_num_eval(arg, &val))
2441 sprintf(buf, "%lld", val);
2445 case PRINT_FIELD ... PRINT_SYMBOL:
2450 do_warning("invalid eval type %d", arg->type);
2457 static enum event_type
2458 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2460 enum event_type type;
2461 struct print_arg *arg = NULL;
2462 struct print_flag_sym *field;
2468 type = read_token_item(&token);
2469 if (test_type_token(type, token, EVENT_OP, "{"))
2477 type = process_arg(event, arg, &token);
2479 if (type == EVENT_OP)
2480 type = process_op(event, arg, &token);
2482 if (type == EVENT_ERROR)
2485 if (test_type_token(type, token, EVENT_DELIM, ","))
2488 field = calloc(1, sizeof(*field));
2492 value = arg_eval(arg);
2494 goto out_free_field;
2495 field->value = strdup(value);
2496 if (field->value == NULL)
2497 goto out_free_field;
2505 type = process_arg(event, arg, &token);
2506 if (test_type_token(type, token, EVENT_OP, "}"))
2507 goto out_free_field;
2509 value = arg_eval(arg);
2511 goto out_free_field;
2512 field->str = strdup(value);
2513 if (field->str == NULL)
2514 goto out_free_field;
2519 list = &field->next;
2522 type = read_token_item(&token);
2523 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2529 free_flag_sym(field);
2538 static enum event_type
2539 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2541 struct print_arg *field;
2542 enum event_type type;
2545 memset(arg, 0, sizeof(*arg));
2546 arg->type = PRINT_FLAGS;
2548 field = alloc_arg();
2550 do_warning_event(event, "%s: not enough memory!", __func__);
2554 type = process_field_arg(event, field, &token);
2556 /* Handle operations in the first argument */
2557 while (type == EVENT_OP)
2558 type = process_op(event, field, &token);
2560 if (test_type_token(type, token, EVENT_DELIM, ","))
2561 goto out_free_field;
2564 arg->flags.field = field;
2566 type = read_token_item(&token);
2567 if (event_item_type(type)) {
2568 arg->flags.delim = token;
2569 type = read_token_item(&token);
2572 if (test_type_token(type, token, EVENT_DELIM, ","))
2575 type = process_fields(event, &arg->flags.flags, &token);
2576 if (test_type_token(type, token, EVENT_DELIM, ")"))
2580 type = read_token_item(tok);
2591 static enum event_type
2592 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2594 struct print_arg *field;
2595 enum event_type type;
2598 memset(arg, 0, sizeof(*arg));
2599 arg->type = PRINT_SYMBOL;
2601 field = alloc_arg();
2603 do_warning_event(event, "%s: not enough memory!", __func__);
2607 type = process_field_arg(event, field, &token);
2609 if (test_type_token(type, token, EVENT_DELIM, ","))
2610 goto out_free_field;
2612 arg->symbol.field = field;
2614 type = process_fields(event, &arg->symbol.symbols, &token);
2615 if (test_type_token(type, token, EVENT_DELIM, ")"))
2619 type = read_token_item(tok);
2630 static enum event_type
2631 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2633 memset(arg, 0, sizeof(*arg));
2634 arg->type = PRINT_HEX;
2636 if (alloc_and_process_delim(event, ",", &arg->hex.field))
2639 if (alloc_and_process_delim(event, ")", &arg->hex.size))
2642 return read_token_item(tok);
2645 free_arg(arg->hex.field);
2646 arg->hex.field = NULL;
2652 static enum event_type
2653 process_int_array(struct event_format *event, struct print_arg *arg, char **tok)
2655 memset(arg, 0, sizeof(*arg));
2656 arg->type = PRINT_INT_ARRAY;
2658 if (alloc_and_process_delim(event, ",", &arg->int_array.field))
2661 if (alloc_and_process_delim(event, ",", &arg->int_array.count))
2664 if (alloc_and_process_delim(event, ")", &arg->int_array.el_size))
2667 return read_token_item(tok);
2670 free_arg(arg->int_array.count);
2671 arg->int_array.count = NULL;
2673 free_arg(arg->int_array.field);
2674 arg->int_array.field = NULL;
2680 static enum event_type
2681 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2683 struct format_field *field;
2684 enum event_type type;
2687 memset(arg, 0, sizeof(*arg));
2688 arg->type = PRINT_DYNAMIC_ARRAY;
2691 * The item within the parenthesis is another field that holds
2692 * the index into where the array starts.
2694 type = read_token(&token);
2696 if (type != EVENT_ITEM)
2699 /* Find the field */
2701 field = pevent_find_field(event, token);
2705 arg->dynarray.field = field;
2706 arg->dynarray.index = 0;
2708 if (read_expected(EVENT_DELIM, ")") < 0)
2712 type = read_token_item(&token);
2714 if (type != EVENT_OP || strcmp(token, "[") != 0)
2720 do_warning_event(event, "%s: not enough memory!", __func__);
2725 type = process_arg(event, arg, &token);
2726 if (type == EVENT_ERROR)
2729 if (!test_type_token(type, token, EVENT_OP, "]"))
2733 type = read_token_item(tok);
2744 static enum event_type
2745 process_dynamic_array_len(struct event_format *event, struct print_arg *arg,
2748 struct format_field *field;
2749 enum event_type type;
2752 if (read_expect_type(EVENT_ITEM, &token) < 0)
2755 arg->type = PRINT_DYNAMIC_ARRAY_LEN;
2757 /* Find the field */
2758 field = pevent_find_field(event, token);
2762 arg->dynarray.field = field;
2763 arg->dynarray.index = 0;
2765 if (read_expected(EVENT_DELIM, ")") < 0)
2768 type = read_token(&token);
2780 static enum event_type
2781 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2783 struct print_arg *item_arg;
2784 enum event_type type;
2787 type = process_arg(event, arg, &token);
2789 if (type == EVENT_ERROR)
2792 if (type == EVENT_OP)
2793 type = process_op(event, arg, &token);
2795 if (type == EVENT_ERROR)
2798 if (test_type_token(type, token, EVENT_DELIM, ")"))
2802 type = read_token_item(&token);
2805 * If the next token is an item or another open paren, then
2806 * this was a typecast.
2808 if (event_item_type(type) ||
2809 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2811 /* make this a typecast and contine */
2813 /* prevous must be an atom */
2814 if (arg->type != PRINT_ATOM) {
2815 do_warning_event(event, "previous needed to be PRINT_ATOM");
2819 item_arg = alloc_arg();
2821 do_warning_event(event, "%s: not enough memory!",
2826 arg->type = PRINT_TYPE;
2827 arg->typecast.type = arg->atom.atom;
2828 arg->typecast.item = item_arg;
2829 type = process_arg_token(event, item_arg, &token, type);
2843 static enum event_type
2844 process_str(struct event_format *event __maybe_unused, struct print_arg *arg,
2847 enum event_type type;
2850 if (read_expect_type(EVENT_ITEM, &token) < 0)
2853 arg->type = PRINT_STRING;
2854 arg->string.string = token;
2855 arg->string.offset = -1;
2857 if (read_expected(EVENT_DELIM, ")") < 0)
2860 type = read_token(&token);
2872 static enum event_type
2873 process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg,
2876 enum event_type type;
2879 if (read_expect_type(EVENT_ITEM, &token) < 0)
2882 arg->type = PRINT_BITMASK;
2883 arg->bitmask.bitmask = token;
2884 arg->bitmask.offset = -1;
2886 if (read_expected(EVENT_DELIM, ")") < 0)
2889 type = read_token(&token);
2901 static struct pevent_function_handler *
2902 find_func_handler(struct pevent *pevent, char *func_name)
2904 struct pevent_function_handler *func;
2909 for (func = pevent->func_handlers; func; func = func->next) {
2910 if (strcmp(func->name, func_name) == 0)
2917 static void remove_func_handler(struct pevent *pevent, char *func_name)
2919 struct pevent_function_handler *func;
2920 struct pevent_function_handler **next;
2922 next = &pevent->func_handlers;
2923 while ((func = *next)) {
2924 if (strcmp(func->name, func_name) == 0) {
2926 free_func_handle(func);
2933 static enum event_type
2934 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2935 struct print_arg *arg, char **tok)
2937 struct print_arg **next_arg;
2938 struct print_arg *farg;
2939 enum event_type type;
2943 arg->type = PRINT_FUNC;
2944 arg->func.func = func;
2948 next_arg = &(arg->func.args);
2949 for (i = 0; i < func->nr_args; i++) {
2952 do_warning_event(event, "%s: not enough memory!",
2957 type = process_arg(event, farg, &token);
2958 if (i < (func->nr_args - 1)) {
2959 if (type != EVENT_DELIM || strcmp(token, ",") != 0) {
2960 do_warning_event(event,
2961 "Error: function '%s()' expects %d arguments but event %s only uses %d",
2962 func->name, func->nr_args,
2963 event->name, i + 1);
2967 if (type != EVENT_DELIM || strcmp(token, ")") != 0) {
2968 do_warning_event(event,
2969 "Error: function '%s()' only expects %d arguments but event %s has more",
2970 func->name, func->nr_args, event->name);
2976 next_arg = &(farg->next);
2980 type = read_token(&token);
2991 static enum event_type
2992 process_function(struct event_format *event, struct print_arg *arg,
2993 char *token, char **tok)
2995 struct pevent_function_handler *func;
2997 if (strcmp(token, "__print_flags") == 0) {
3000 return process_flags(event, arg, tok);
3002 if (strcmp(token, "__print_symbolic") == 0) {
3004 is_symbolic_field = 1;
3005 return process_symbols(event, arg, tok);
3007 if (strcmp(token, "__print_hex") == 0) {
3009 return process_hex(event, arg, tok);
3011 if (strcmp(token, "__print_array") == 0) {
3013 return process_int_array(event, arg, tok);
3015 if (strcmp(token, "__get_str") == 0) {
3017 return process_str(event, arg, tok);
3019 if (strcmp(token, "__get_bitmask") == 0) {
3021 return process_bitmask(event, arg, tok);
3023 if (strcmp(token, "__get_dynamic_array") == 0) {
3025 return process_dynamic_array(event, arg, tok);
3027 if (strcmp(token, "__get_dynamic_array_len") == 0) {
3029 return process_dynamic_array_len(event, arg, tok);
3032 func = find_func_handler(event->pevent, token);
3035 return process_func_handler(event, func, arg, tok);
3038 do_warning_event(event, "function %s not defined", token);
3043 static enum event_type
3044 process_arg_token(struct event_format *event, struct print_arg *arg,
3045 char **tok, enum event_type type)
3054 if (strcmp(token, "REC") == 0) {
3056 type = process_entry(event, arg, &token);
3060 /* test the next token */
3061 type = read_token_item(&token);
3064 * If the next token is a parenthesis, then this
3067 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
3070 /* this will free atom. */
3071 type = process_function(event, arg, atom, &token);
3074 /* atoms can be more than one token long */
3075 while (type == EVENT_ITEM) {
3077 new_atom = realloc(atom,
3078 strlen(atom) + strlen(token) + 2);
3087 strcat(atom, token);
3089 type = read_token_item(&token);
3092 arg->type = PRINT_ATOM;
3093 arg->atom.atom = atom;
3098 arg->type = PRINT_ATOM;
3099 arg->atom.atom = token;
3100 type = read_token_item(&token);
3103 if (strcmp(token, "(") == 0) {
3105 type = process_paren(event, arg, &token);
3109 /* handle single ops */
3110 arg->type = PRINT_OP;
3112 arg->op.left = NULL;
3113 type = process_op(event, arg, &token);
3115 /* On error, the op is freed */
3116 if (type == EVENT_ERROR)
3119 /* return error type if errored */
3122 case EVENT_ERROR ... EVENT_NEWLINE:
3124 do_warning_event(event, "unexpected type %d", type);
3132 static int event_read_print_args(struct event_format *event, struct print_arg **list)
3134 enum event_type type = EVENT_ERROR;
3135 struct print_arg *arg;
3140 if (type == EVENT_NEWLINE) {
3141 type = read_token_item(&token);
3147 do_warning_event(event, "%s: not enough memory!",
3152 type = process_arg(event, arg, &token);
3154 if (type == EVENT_ERROR) {
3163 if (type == EVENT_OP) {
3164 type = process_op(event, arg, &token);
3166 if (type == EVENT_ERROR) {
3175 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
3182 } while (type != EVENT_NONE);
3184 if (type != EVENT_NONE && type != EVENT_ERROR)
3190 static int event_read_print(struct event_format *event)
3192 enum event_type type;
3196 if (read_expected_item(EVENT_ITEM, "print") < 0)
3199 if (read_expected(EVENT_ITEM, "fmt") < 0)
3202 if (read_expected(EVENT_OP, ":") < 0)
3205 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
3209 event->print_fmt.format = token;
3210 event->print_fmt.args = NULL;
3212 /* ok to have no arg */
3213 type = read_token_item(&token);
3215 if (type == EVENT_NONE)
3218 /* Handle concatenation of print lines */
3219 if (type == EVENT_DQUOTE) {
3222 if (asprintf(&cat, "%s%s", event->print_fmt.format, token) < 0)
3225 free_token(event->print_fmt.format);
3226 event->print_fmt.format = NULL;
3231 if (test_type_token(type, token, EVENT_DELIM, ","))
3236 ret = event_read_print_args(event, &event->print_fmt.args);
3248 * pevent_find_common_field - return a common field by event
3249 * @event: handle for the event
3250 * @name: the name of the common field to return
3252 * Returns a common field from the event by the given @name.
3253 * This only searchs the common fields and not all field.
3255 struct format_field *
3256 pevent_find_common_field(struct event_format *event, const char *name)
3258 struct format_field *format;
3260 for (format = event->format.common_fields;
3261 format; format = format->next) {
3262 if (strcmp(format->name, name) == 0)
3270 * pevent_find_field - find a non-common field
3271 * @event: handle for the event
3272 * @name: the name of the non-common field
3274 * Returns a non-common field by the given @name.
3275 * This does not search common fields.
3277 struct format_field *
3278 pevent_find_field(struct event_format *event, const char *name)
3280 struct format_field *format;
3282 for (format = event->format.fields;
3283 format; format = format->next) {
3284 if (strcmp(format->name, name) == 0)
3292 * pevent_find_any_field - find any field by name
3293 * @event: handle for the event
3294 * @name: the name of the field
3296 * Returns a field by the given @name.
3297 * This searchs the common field names first, then
3298 * the non-common ones if a common one was not found.
3300 struct format_field *
3301 pevent_find_any_field(struct event_format *event, const char *name)
3303 struct format_field *format;
3305 format = pevent_find_common_field(event, name);
3308 return pevent_find_field(event, name);
3312 * pevent_read_number - read a number from data
3313 * @pevent: handle for the pevent
3314 * @ptr: the raw data
3315 * @size: the size of the data that holds the number
3317 * Returns the number (converted to host) from the
3320 unsigned long long pevent_read_number(struct pevent *pevent,
3321 const void *ptr, int size)
3325 return *(unsigned char *)ptr;
3327 return data2host2(pevent, ptr);
3329 return data2host4(pevent, ptr);
3331 return data2host8(pevent, ptr);
3339 * pevent_read_number_field - read a number from data
3340 * @field: a handle to the field
3341 * @data: the raw data to read
3342 * @value: the value to place the number in
3344 * Reads raw data according to a field offset and size,
3345 * and translates it into @value.
3347 * Returns 0 on success, -1 otherwise.
3349 int pevent_read_number_field(struct format_field *field, const void *data,
3350 unsigned long long *value)
3354 switch (field->size) {
3359 *value = pevent_read_number(field->event->pevent,
3360 data + field->offset, field->size);
3367 static int get_common_info(struct pevent *pevent,
3368 const char *type, int *offset, int *size)
3370 struct event_format *event;
3371 struct format_field *field;
3374 * All events should have the same common elements.
3375 * Pick any event to find where the type is;
3377 if (!pevent->events) {
3378 do_warning("no event_list!");
3382 event = pevent->events[0];
3383 field = pevent_find_common_field(event, type);
3387 *offset = field->offset;
3388 *size = field->size;
3393 static int __parse_common(struct pevent *pevent, void *data,
3394 int *size, int *offset, const char *name)
3399 ret = get_common_info(pevent, name, offset, size);
3403 return pevent_read_number(pevent, data + *offset, *size);
3406 static int trace_parse_common_type(struct pevent *pevent, void *data)
3408 return __parse_common(pevent, data,
3409 &pevent->type_size, &pevent->type_offset,
3413 static int parse_common_pid(struct pevent *pevent, void *data)
3415 return __parse_common(pevent, data,
3416 &pevent->pid_size, &pevent->pid_offset,
3420 static int parse_common_pc(struct pevent *pevent, void *data)
3422 return __parse_common(pevent, data,
3423 &pevent->pc_size, &pevent->pc_offset,
3424 "common_preempt_count");
3427 static int parse_common_flags(struct pevent *pevent, void *data)
3429 return __parse_common(pevent, data,
3430 &pevent->flags_size, &pevent->flags_offset,
3434 static int parse_common_lock_depth(struct pevent *pevent, void *data)
3436 return __parse_common(pevent, data,
3437 &pevent->ld_size, &pevent->ld_offset,
3438 "common_lock_depth");
3441 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
3443 return __parse_common(pevent, data,
3444 &pevent->ld_size, &pevent->ld_offset,
3445 "common_migrate_disable");
3448 static int events_id_cmp(const void *a, const void *b);
3451 * pevent_find_event - find an event by given id
3452 * @pevent: a handle to the pevent
3453 * @id: the id of the event
3455 * Returns an event that has a given @id.
3457 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3459 struct event_format **eventptr;
3460 struct event_format key;
3461 struct event_format *pkey = &key;
3463 /* Check cache first */
3464 if (pevent->last_event && pevent->last_event->id == id)
3465 return pevent->last_event;
3469 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3470 sizeof(*pevent->events), events_id_cmp);
3473 pevent->last_event = *eventptr;
3481 * pevent_find_event_by_name - find an event by given name
3482 * @pevent: a handle to the pevent
3483 * @sys: the system name to search for
3484 * @name: the name of the event to search for
3486 * This returns an event with a given @name and under the system
3487 * @sys. If @sys is NULL the first event with @name is returned.
3489 struct event_format *
3490 pevent_find_event_by_name(struct pevent *pevent,
3491 const char *sys, const char *name)
3493 struct event_format *event;
3496 if (pevent->last_event &&
3497 strcmp(pevent->last_event->name, name) == 0 &&
3498 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3499 return pevent->last_event;
3501 for (i = 0; i < pevent->nr_events; i++) {
3502 event = pevent->events[i];
3503 if (strcmp(event->name, name) == 0) {
3506 if (strcmp(event->system, sys) == 0)
3510 if (i == pevent->nr_events)
3513 pevent->last_event = event;
3517 static unsigned long long
3518 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3520 struct pevent *pevent = event->pevent;
3521 unsigned long long val = 0;
3522 unsigned long long left, right;
3523 struct print_arg *typearg = NULL;
3524 struct print_arg *larg;
3525 unsigned long offset;
3526 unsigned int field_size;
3528 switch (arg->type) {
3533 return strtoull(arg->atom.atom, NULL, 0);
3535 if (!arg->field.field) {
3536 arg->field.field = pevent_find_any_field(event, arg->field.name);
3537 if (!arg->field.field)
3538 goto out_warning_field;
3541 /* must be a number */
3542 val = pevent_read_number(pevent, data + arg->field.field->offset,
3543 arg->field.field->size);
3547 case PRINT_INT_ARRAY:
3551 val = eval_num_arg(data, size, event, arg->typecast.item);
3552 return eval_type(val, arg, 0);
3560 val = process_defined_func(&s, data, size, event, arg);
3561 trace_seq_destroy(&s);
3565 if (strcmp(arg->op.op, "[") == 0) {
3567 * Arrays are special, since we don't want
3568 * to read the arg as is.
3570 right = eval_num_arg(data, size, event, arg->op.right);
3572 /* handle typecasts */
3573 larg = arg->op.left;
3574 while (larg->type == PRINT_TYPE) {
3577 larg = larg->typecast.item;
3580 /* Default to long size */
3581 field_size = pevent->long_size;
3583 switch (larg->type) {
3584 case PRINT_DYNAMIC_ARRAY:
3585 offset = pevent_read_number(pevent,
3586 data + larg->dynarray.field->offset,
3587 larg->dynarray.field->size);
3588 if (larg->dynarray.field->elementsize)
3589 field_size = larg->dynarray.field->elementsize;
3591 * The actual length of the dynamic array is stored
3592 * in the top half of the field, and the offset
3593 * is in the bottom half of the 32 bit field.
3599 if (!larg->field.field) {
3601 pevent_find_any_field(event, larg->field.name);
3602 if (!larg->field.field) {
3604 goto out_warning_field;
3607 field_size = larg->field.field->elementsize;
3608 offset = larg->field.field->offset +
3609 right * larg->field.field->elementsize;
3612 goto default_op; /* oops, all bets off */
3614 val = pevent_read_number(pevent,
3615 data + offset, field_size);
3617 val = eval_type(val, typearg, 1);
3619 } else if (strcmp(arg->op.op, "?") == 0) {
3620 left = eval_num_arg(data, size, event, arg->op.left);
3621 arg = arg->op.right;
3623 val = eval_num_arg(data, size, event, arg->op.left);
3625 val = eval_num_arg(data, size, event, arg->op.right);
3629 left = eval_num_arg(data, size, event, arg->op.left);
3630 right = eval_num_arg(data, size, event, arg->op.right);
3631 switch (arg->op.op[0]) {
3633 switch (arg->op.op[1]) {
3638 val = left != right;
3641 goto out_warning_op;
3649 val = left || right;
3655 val = left && right;
3660 switch (arg->op.op[1]) {
3665 val = left << right;
3668 val = left <= right;
3671 goto out_warning_op;
3675 switch (arg->op.op[1]) {
3680 val = left >> right;
3683 val = left >= right;
3686 goto out_warning_op;
3690 if (arg->op.op[1] != '=')
3691 goto out_warning_op;
3693 val = left == right;
3711 goto out_warning_op;
3714 case PRINT_DYNAMIC_ARRAY_LEN:
3715 offset = pevent_read_number(pevent,
3716 data + arg->dynarray.field->offset,
3717 arg->dynarray.field->size);
3719 * The total allocated length of the dynamic array is
3720 * stored in the top half of the field, and the offset
3721 * is in the bottom half of the 32 bit field.
3723 val = (unsigned long long)(offset >> 16);
3725 case PRINT_DYNAMIC_ARRAY:
3726 /* Without [], we pass the address to the dynamic data */
3727 offset = pevent_read_number(pevent,
3728 data + arg->dynarray.field->offset,
3729 arg->dynarray.field->size);
3731 * The total allocated length of the dynamic array is
3732 * stored in the top half of the field, and the offset
3733 * is in the bottom half of the 32 bit field.
3736 val = (unsigned long long)((unsigned long)data + offset);
3738 default: /* not sure what to do there */
3744 do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);
3748 do_warning_event(event, "%s: field %s not found",
3749 __func__, arg->field.name);
3755 unsigned long long value;
3758 static const struct flag flags[] = {
3759 { "HI_SOFTIRQ", 0 },
3760 { "TIMER_SOFTIRQ", 1 },
3761 { "NET_TX_SOFTIRQ", 2 },
3762 { "NET_RX_SOFTIRQ", 3 },
3763 { "BLOCK_SOFTIRQ", 4 },
3764 { "IRQ_POLL_SOFTIRQ", 5 },
3765 { "TASKLET_SOFTIRQ", 6 },
3766 { "SCHED_SOFTIRQ", 7 },
3767 { "HRTIMER_SOFTIRQ", 8 },
3768 { "RCU_SOFTIRQ", 9 },
3770 { "HRTIMER_NORESTART", 0 },
3771 { "HRTIMER_RESTART", 1 },
3774 static long long eval_flag(const char *flag)
3779 * Some flags in the format files do not get converted.
3780 * If the flag is not numeric, see if it is something that
3781 * we already know about.
3783 if (isdigit(flag[0]))
3784 return strtoull(flag, NULL, 0);
3786 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3787 if (strcmp(flags[i].name, flag) == 0)
3788 return flags[i].value;
3793 static void print_str_to_seq(struct trace_seq *s, const char *format,
3794 int len_arg, const char *str)
3797 trace_seq_printf(s, format, len_arg, str);
3799 trace_seq_printf(s, format, str);
3802 static void print_bitmask_to_seq(struct pevent *pevent,
3803 struct trace_seq *s, const char *format,
3804 int len_arg, const void *data, int size)
3806 int nr_bits = size * 8;
3807 int str_size = (nr_bits + 3) / 4;
3815 * The kernel likes to put in commas every 32 bits, we
3818 str_size += (nr_bits - 1) / 32;
3820 str = malloc(str_size + 1);
3822 do_warning("%s: not enough memory!", __func__);
3827 /* Start out with -2 for the two chars per byte */
3828 for (i = str_size - 2; i >= 0; i -= 2) {
3830 * data points to a bit mask of size bytes.
3831 * In the kernel, this is an array of long words, thus
3832 * endianess is very important.
3834 if (pevent->file_bigendian)
3835 index = size - (len + 1);
3839 snprintf(buf, 3, "%02x", *((unsigned char *)data + index));
3840 memcpy(str + i, buf, 2);
3842 if (!(len & 3) && i > 0) {
3849 trace_seq_printf(s, format, len_arg, str);
3851 trace_seq_printf(s, format, str);
3856 static void print_str_arg(struct trace_seq *s, void *data, int size,
3857 struct event_format *event, const char *format,
3858 int len_arg, struct print_arg *arg)
3860 struct pevent *pevent = event->pevent;
3861 struct print_flag_sym *flag;
3862 struct format_field *field;
3863 struct printk_map *printk;
3864 long long val, fval;
3865 unsigned long long addr;
3871 switch (arg->type) {
3876 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3879 field = arg->field.field;
3881 field = pevent_find_any_field(event, arg->field.name);
3883 str = arg->field.name;
3884 goto out_warning_field;
3886 arg->field.field = field;
3888 /* Zero sized fields, mean the rest of the data */
3889 len = field->size ? : size - field->offset;
3892 * Some events pass in pointers. If this is not an array
3893 * and the size is the same as long_size, assume that it
3896 if (!(field->flags & FIELD_IS_ARRAY) &&
3897 field->size == pevent->long_size) {
3899 /* Handle heterogeneous recording and processing
3903 * Traces recorded on 32-bit devices (32-bit
3904 * addressing) and processed on 64-bit devices:
3905 * In this case, only 32 bits should be read.
3908 * Traces recorded on 64 bit devices and processed
3909 * on 32-bit devices:
3910 * In this case, 64 bits must be read.
3912 addr = (pevent->long_size == 8) ?
3913 *(unsigned long long *)(data + field->offset) :
3914 (unsigned long long)*(unsigned int *)(data + field->offset);
3916 /* Check if it matches a print format */
3917 printk = find_printk(pevent, addr);
3919 trace_seq_puts(s, printk->printk);
3921 trace_seq_printf(s, "%llx", addr);
3924 str = malloc(len + 1);
3926 do_warning_event(event, "%s: not enough memory!",
3930 memcpy(str, data + field->offset, len);
3932 print_str_to_seq(s, format, len_arg, str);
3936 val = eval_num_arg(data, size, event, arg->flags.field);
3938 for (flag = arg->flags.flags; flag; flag = flag->next) {
3939 fval = eval_flag(flag->value);
3940 if (!val && fval < 0) {
3941 print_str_to_seq(s, format, len_arg, flag->str);
3944 if (fval > 0 && (val & fval) == fval) {
3945 if (print && arg->flags.delim)
3946 trace_seq_puts(s, arg->flags.delim);
3947 print_str_to_seq(s, format, len_arg, flag->str);
3954 val = eval_num_arg(data, size, event, arg->symbol.field);
3955 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3956 fval = eval_flag(flag->value);
3958 print_str_to_seq(s, format, len_arg, flag->str);
3964 if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) {
3965 unsigned long offset;
3966 offset = pevent_read_number(pevent,
3967 data + arg->hex.field->dynarray.field->offset,
3968 arg->hex.field->dynarray.field->size);
3969 hex = data + (offset & 0xffff);
3971 field = arg->hex.field->field.field;
3973 str = arg->hex.field->field.name;
3974 field = pevent_find_any_field(event, str);
3976 goto out_warning_field;
3977 arg->hex.field->field.field = field;
3979 hex = data + field->offset;
3981 len = eval_num_arg(data, size, event, arg->hex.size);
3982 for (i = 0; i < len; i++) {
3984 trace_seq_putc(s, ' ');
3985 trace_seq_printf(s, "%02x", hex[i]);
3989 case PRINT_INT_ARRAY: {
3993 if (arg->int_array.field->type == PRINT_DYNAMIC_ARRAY) {
3994 unsigned long offset;
3995 struct format_field *field =
3996 arg->int_array.field->dynarray.field;
3997 offset = pevent_read_number(pevent,
3998 data + field->offset,
4000 num = data + (offset & 0xffff);
4002 field = arg->int_array.field->field.field;
4004 str = arg->int_array.field->field.name;
4005 field = pevent_find_any_field(event, str);
4007 goto out_warning_field;
4008 arg->int_array.field->field.field = field;
4010 num = data + field->offset;
4012 len = eval_num_arg(data, size, event, arg->int_array.count);
4013 el_size = eval_num_arg(data, size, event,
4014 arg->int_array.el_size);
4015 for (i = 0; i < len; i++) {
4017 trace_seq_putc(s, ' ');
4020 trace_seq_printf(s, "%u", *(uint8_t *)num);
4021 } else if (el_size == 2) {
4022 trace_seq_printf(s, "%u", *(uint16_t *)num);
4023 } else if (el_size == 4) {
4024 trace_seq_printf(s, "%u", *(uint32_t *)num);
4025 } else if (el_size == 8) {
4026 trace_seq_printf(s, "%"PRIu64, *(uint64_t *)num);
4028 trace_seq_printf(s, "BAD SIZE:%d 0x%x",
4029 el_size, *(uint8_t *)num);
4039 case PRINT_STRING: {
4042 if (arg->string.offset == -1) {
4043 struct format_field *f;
4045 f = pevent_find_any_field(event, arg->string.string);
4046 arg->string.offset = f->offset;
4048 str_offset = data2host4(pevent, data + arg->string.offset);
4049 str_offset &= 0xffff;
4050 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
4054 print_str_to_seq(s, format, len_arg, arg->string.string);
4056 case PRINT_BITMASK: {
4060 if (arg->bitmask.offset == -1) {
4061 struct format_field *f;
4063 f = pevent_find_any_field(event, arg->bitmask.bitmask);
4064 arg->bitmask.offset = f->offset;
4066 bitmask_offset = data2host4(pevent, data + arg->bitmask.offset);
4067 bitmask_size = bitmask_offset >> 16;
4068 bitmask_offset &= 0xffff;
4069 print_bitmask_to_seq(pevent, s, format, len_arg,
4070 data + bitmask_offset, bitmask_size);
4075 * The only op for string should be ? :
4077 if (arg->op.op[0] != '?')
4079 val = eval_num_arg(data, size, event, arg->op.left);
4081 print_str_arg(s, data, size, event,
4082 format, len_arg, arg->op.right->op.left);
4084 print_str_arg(s, data, size, event,
4085 format, len_arg, arg->op.right->op.right);
4088 process_defined_func(s, data, size, event, arg);
4098 do_warning_event(event, "%s: field %s not found",
4099 __func__, arg->field.name);
4102 static unsigned long long
4103 process_defined_func(struct trace_seq *s, void *data, int size,
4104 struct event_format *event, struct print_arg *arg)
4106 struct pevent_function_handler *func_handle = arg->func.func;
4107 struct pevent_func_params *param;
4108 unsigned long long *args;
4109 unsigned long long ret;
4110 struct print_arg *farg;
4111 struct trace_seq str;
4113 struct save_str *next;
4115 } *strings = NULL, *string;
4118 if (!func_handle->nr_args) {
4119 ret = (*func_handle->func)(s, NULL);
4123 farg = arg->func.args;
4124 param = func_handle->params;
4127 args = malloc(sizeof(*args) * func_handle->nr_args);
4131 for (i = 0; i < func_handle->nr_args; i++) {
4132 switch (param->type) {
4133 case PEVENT_FUNC_ARG_INT:
4134 case PEVENT_FUNC_ARG_LONG:
4135 case PEVENT_FUNC_ARG_PTR:
4136 args[i] = eval_num_arg(data, size, event, farg);
4138 case PEVENT_FUNC_ARG_STRING:
4139 trace_seq_init(&str);
4140 print_str_arg(&str, data, size, event, "%s", -1, farg);
4141 trace_seq_terminate(&str);
4142 string = malloc(sizeof(*string));
4144 do_warning_event(event, "%s(%d): malloc str",
4145 __func__, __LINE__);
4148 string->next = strings;
4149 string->str = strdup(str.buffer);
4152 do_warning_event(event, "%s(%d): malloc str",
4153 __func__, __LINE__);
4156 args[i] = (uintptr_t)string->str;
4158 trace_seq_destroy(&str);
4162 * Something went totally wrong, this is not
4163 * an input error, something in this code broke.
4165 do_warning_event(event, "Unexpected end of arguments\n");
4169 param = param->next;
4172 ret = (*func_handle->func)(s, args);
4177 strings = string->next;
4183 /* TBD : handle return type here */
4187 static void free_args(struct print_arg *args)
4189 struct print_arg *next;
4199 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
4201 struct pevent *pevent = event->pevent;
4202 struct format_field *field, *ip_field;
4203 struct print_arg *args, *arg, **next;
4204 unsigned long long ip, val;
4209 field = pevent->bprint_buf_field;
4210 ip_field = pevent->bprint_ip_field;
4213 field = pevent_find_field(event, "buf");
4215 do_warning_event(event, "can't find buffer field for binary printk");
4218 ip_field = pevent_find_field(event, "ip");
4220 do_warning_event(event, "can't find ip field for binary printk");
4223 pevent->bprint_buf_field = field;
4224 pevent->bprint_ip_field = ip_field;
4227 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
4230 * The first arg is the IP pointer.
4234 do_warning_event(event, "%s(%d): not enough memory!",
4235 __func__, __LINE__);
4242 arg->type = PRINT_ATOM;
4244 if (asprintf(&arg->atom.atom, "%lld", ip) < 0)
4247 /* skip the first "%ps: " */
4248 for (ptr = fmt + 5, bptr = data + field->offset;
4249 bptr < data + size && *ptr; ptr++) {
4284 vsize = pevent->long_size;
4298 /* the pointers are always 4 bytes aligned */
4299 bptr = (void *)(((unsigned long)bptr + 3) &
4301 val = pevent_read_number(pevent, bptr, vsize);
4305 do_warning_event(event, "%s(%d): not enough memory!",
4306 __func__, __LINE__);
4310 arg->type = PRINT_ATOM;
4311 if (asprintf(&arg->atom.atom, "%lld", val) < 0) {
4318 * The '*' case means that an arg is used as the length.
4319 * We need to continue to figure out for what.
4328 do_warning_event(event, "%s(%d): not enough memory!",
4329 __func__, __LINE__);
4333 arg->type = PRINT_BSTRING;
4334 arg->string.string = strdup(bptr);
4335 if (!arg->string.string)
4337 bptr += strlen(bptr) + 1;
4354 get_bprint_format(void *data, int size __maybe_unused,
4355 struct event_format *event)
4357 struct pevent *pevent = event->pevent;
4358 unsigned long long addr;
4359 struct format_field *field;
4360 struct printk_map *printk;
4363 field = pevent->bprint_fmt_field;
4366 field = pevent_find_field(event, "fmt");
4368 do_warning_event(event, "can't find format field for binary printk");
4371 pevent->bprint_fmt_field = field;
4374 addr = pevent_read_number(pevent, data + field->offset, field->size);
4376 printk = find_printk(pevent, addr);
4378 if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)
4383 if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)
4389 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
4390 struct event_format *event, struct print_arg *arg)
4393 const char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
4395 if (arg->type == PRINT_FUNC) {
4396 process_defined_func(s, data, size, event, arg);
4400 if (arg->type != PRINT_FIELD) {
4401 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
4407 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
4408 if (!arg->field.field) {
4410 pevent_find_any_field(event, arg->field.name);
4411 if (!arg->field.field) {
4412 do_warning_event(event, "%s: field %s not found",
4413 __func__, arg->field.name);
4417 if (arg->field.field->size != 6) {
4418 trace_seq_printf(s, "INVALIDMAC");
4421 buf = data + arg->field.field->offset;
4422 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
4425 static void print_ip4_addr(struct trace_seq *s, char i, unsigned char *buf)
4430 fmt = "%03d.%03d.%03d.%03d";
4432 fmt = "%d.%d.%d.%d";
4434 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3]);
4437 static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
4439 return ((unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
4440 (unsigned long)(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
4443 static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
4445 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
4448 static void print_ip6c_addr(struct trace_seq *s, unsigned char *addr)
4451 unsigned char zerolength[8];
4456 bool needcolon = false;
4458 struct in6_addr in6;
4460 memcpy(&in6, addr, sizeof(struct in6_addr));
4462 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
4464 memset(zerolength, 0, sizeof(zerolength));
4471 /* find position of longest 0 run */
4472 for (i = 0; i < range; i++) {
4473 for (j = i; j < range; j++) {
4474 if (in6.s6_addr16[j] != 0)
4479 for (i = 0; i < range; i++) {
4480 if (zerolength[i] > longest) {
4481 longest = zerolength[i];
4485 if (longest == 1) /* don't compress a single 0 */
4489 for (i = 0; i < range; i++) {
4490 if (i == colonpos) {
4491 if (needcolon || i == 0)
4492 trace_seq_printf(s, ":");
4493 trace_seq_printf(s, ":");
4499 trace_seq_printf(s, ":");
4502 /* hex u16 without leading 0s */
4503 word = ntohs(in6.s6_addr16[i]);
4507 trace_seq_printf(s, "%x%02x", hi, lo);
4509 trace_seq_printf(s, "%x", lo);
4516 trace_seq_printf(s, ":");
4517 print_ip4_addr(s, 'I', &in6.s6_addr[12]);
4523 static void print_ip6_addr(struct trace_seq *s, char i, unsigned char *buf)
4527 for (j = 0; j < 16; j += 2) {
4528 trace_seq_printf(s, "%02x%02x", buf[j], buf[j+1]);
4529 if (i == 'I' && j < 14)
4530 trace_seq_printf(s, ":");
4535 * %pi4 print an IPv4 address with leading zeros
4536 * %pI4 print an IPv4 address without leading zeros
4537 * %pi6 print an IPv6 address without colons
4538 * %pI6 print an IPv6 address with colons
4539 * %pI6c print an IPv6 address in compressed form with colons
4540 * %pISpc print an IP address based on sockaddr; p adds port.
4542 static int print_ipv4_arg(struct trace_seq *s, const char *ptr, char i,
4543 void *data, int size, struct event_format *event,
4544 struct print_arg *arg)
4548 if (arg->type == PRINT_FUNC) {
4549 process_defined_func(s, data, size, event, arg);
4553 if (arg->type != PRINT_FIELD) {
4554 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4558 if (!arg->field.field) {
4560 pevent_find_any_field(event, arg->field.name);
4561 if (!arg->field.field) {
4562 do_warning("%s: field %s not found",
4563 __func__, arg->field.name);
4568 buf = data + arg->field.field->offset;
4570 if (arg->field.field->size != 4) {
4571 trace_seq_printf(s, "INVALIDIPv4");
4574 print_ip4_addr(s, i, buf);
4579 static int print_ipv6_arg(struct trace_seq *s, const char *ptr, char i,
4580 void *data, int size, struct event_format *event,
4581 struct print_arg *arg)
4588 if (i == 'I' && *ptr == 'c') {
4594 if (arg->type == PRINT_FUNC) {
4595 process_defined_func(s, data, size, event, arg);
4599 if (arg->type != PRINT_FIELD) {
4600 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4604 if (!arg->field.field) {
4606 pevent_find_any_field(event, arg->field.name);
4607 if (!arg->field.field) {
4608 do_warning("%s: field %s not found",
4609 __func__, arg->field.name);
4614 buf = data + arg->field.field->offset;
4616 if (arg->field.field->size != 16) {
4617 trace_seq_printf(s, "INVALIDIPv6");
4622 print_ip6c_addr(s, buf);
4624 print_ip6_addr(s, i, buf);
4629 static int print_ipsa_arg(struct trace_seq *s, const char *ptr, char i,
4630 void *data, int size, struct event_format *event,
4631 struct print_arg *arg)
4633 char have_c = 0, have_p = 0;
4635 struct sockaddr_storage *sa;
4652 if (arg->type == PRINT_FUNC) {
4653 process_defined_func(s, data, size, event, arg);
4657 if (arg->type != PRINT_FIELD) {
4658 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", arg->type);
4662 if (!arg->field.field) {
4664 pevent_find_any_field(event, arg->field.name);
4665 if (!arg->field.field) {
4666 do_warning("%s: field %s not found",
4667 __func__, arg->field.name);
4672 sa = (struct sockaddr_storage *) (data + arg->field.field->offset);
4674 if (sa->ss_family == AF_INET) {
4675 struct sockaddr_in *sa4 = (struct sockaddr_in *) sa;
4677 if (arg->field.field->size < sizeof(struct sockaddr_in)) {
4678 trace_seq_printf(s, "INVALIDIPv4");
4682 print_ip4_addr(s, i, (unsigned char *) &sa4->sin_addr);
4684 trace_seq_printf(s, ":%d", ntohs(sa4->sin_port));
4687 } else if (sa->ss_family == AF_INET6) {
4688 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *) sa;
4690 if (arg->field.field->size < sizeof(struct sockaddr_in6)) {
4691 trace_seq_printf(s, "INVALIDIPv6");
4696 trace_seq_printf(s, "[");
4698 buf = (unsigned char *) &sa6->sin6_addr;
4700 print_ip6c_addr(s, buf);
4702 print_ip6_addr(s, i, buf);
4705 trace_seq_printf(s, "]:%d", ntohs(sa6->sin6_port));
4711 static int print_ip_arg(struct trace_seq *s, const char *ptr,
4712 void *data, int size, struct event_format *event,
4713 struct print_arg *arg)
4715 char i = *ptr; /* 'i' or 'I' */
4728 rc += print_ipv4_arg(s, ptr, i, data, size, event, arg);
4731 rc += print_ipv6_arg(s, ptr, i, data, size, event, arg);
4734 rc += print_ipsa_arg(s, ptr, i, data, size, event, arg);
4743 static int is_printable_array(char *p, unsigned int len)
4747 for (i = 0; i < len && p[i]; i++)
4748 if (!isprint(p[i]) && !isspace(p[i]))
4753 void pevent_print_field(struct trace_seq *s, void *data,
4754 struct format_field *field)
4756 unsigned long long val;
4757 unsigned int offset, len, i;
4758 struct pevent *pevent = field->event->pevent;
4760 if (field->flags & FIELD_IS_ARRAY) {
4761 offset = field->offset;
4763 if (field->flags & FIELD_IS_DYNAMIC) {
4764 val = pevent_read_number(pevent, data + offset, len);
4769 if (field->flags & FIELD_IS_STRING &&
4770 is_printable_array(data + offset, len)) {
4771 trace_seq_printf(s, "%s", (char *)data + offset);
4773 trace_seq_puts(s, "ARRAY[");
4774 for (i = 0; i < len; i++) {
4776 trace_seq_puts(s, ", ");
4777 trace_seq_printf(s, "%02x",
4778 *((unsigned char *)data + offset + i));
4780 trace_seq_putc(s, ']');
4781 field->flags &= ~FIELD_IS_STRING;
4784 val = pevent_read_number(pevent, data + field->offset,
4786 if (field->flags & FIELD_IS_POINTER) {
4787 trace_seq_printf(s, "0x%llx", val);
4788 } else if (field->flags & FIELD_IS_SIGNED) {
4789 switch (field->size) {
4792 * If field is long then print it in hex.
4793 * A long usually stores pointers.
4795 if (field->flags & FIELD_IS_LONG)
4796 trace_seq_printf(s, "0x%x", (int)val);
4798 trace_seq_printf(s, "%d", (int)val);
4801 trace_seq_printf(s, "%2d", (short)val);
4804 trace_seq_printf(s, "%1d", (char)val);
4807 trace_seq_printf(s, "%lld", val);
4810 if (field->flags & FIELD_IS_LONG)
4811 trace_seq_printf(s, "0x%llx", val);
4813 trace_seq_printf(s, "%llu", val);
4818 void pevent_print_fields(struct trace_seq *s, void *data,
4819 int size __maybe_unused, struct event_format *event)
4821 struct format_field *field;
4823 field = event->format.fields;
4825 trace_seq_printf(s, " %s=", field->name);
4826 pevent_print_field(s, data, field);
4827 field = field->next;
4831 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
4833 struct pevent *pevent = event->pevent;
4834 struct print_fmt *print_fmt = &event->print_fmt;
4835 struct print_arg *arg = print_fmt->args;
4836 struct print_arg *args = NULL;
4837 const char *ptr = print_fmt->format;
4838 unsigned long long val;
4839 struct func_map *func;
4840 const char *saveptr;
4842 char *bprint_fmt = NULL;
4850 if (event->flags & EVENT_FL_FAILED) {
4851 trace_seq_printf(s, "[FAILED TO PARSE]");
4852 pevent_print_fields(s, data, size, event);
4856 if (event->flags & EVENT_FL_ISBPRINT) {
4857 bprint_fmt = get_bprint_format(data, size, event);
4858 args = make_bprint_args(bprint_fmt, data, size, event);
4863 for (; *ptr; ptr++) {
4869 trace_seq_putc(s, '\n');
4872 trace_seq_putc(s, '\t');
4875 trace_seq_putc(s, '\r');
4878 trace_seq_putc(s, '\\');
4881 trace_seq_putc(s, *ptr);
4885 } else if (*ptr == '%') {
4893 trace_seq_putc(s, '%');
4896 /* FIXME: need to handle properly */
4908 /* The argument is the length. */
4910 do_warning_event(event, "no argument match");
4911 event->flags |= EVENT_FL_FAILED;
4914 len_arg = eval_num_arg(data, size, event, arg);
4925 if (pevent->long_size == 4)
4930 if (isalnum(ptr[1]))
4933 if (*ptr == 'F' || *ptr == 'f' ||
4934 *ptr == 'S' || *ptr == 's') {
4936 } else if (*ptr == 'M' || *ptr == 'm') {
4937 print_mac_arg(s, *ptr, data, size, event, arg);
4940 } else if (*ptr == 'I' || *ptr == 'i') {
4943 n = print_ip_arg(s, ptr, data, size, event, arg);
4958 do_warning_event(event, "no argument match");
4959 event->flags |= EVENT_FL_FAILED;
4963 len = ((unsigned long)ptr + 1) -
4964 (unsigned long)saveptr;
4966 /* should never happen */
4968 do_warning_event(event, "bad format!");
4969 event->flags |= EVENT_FL_FAILED;
4973 memcpy(format, saveptr, len);
4976 val = eval_num_arg(data, size, event, arg);
4980 func = find_func(pevent, val);
4982 trace_seq_puts(s, func->func);
4983 if (show_func == 'F')
4990 if (pevent->long_size == 8 && ls == 1 &&
4991 sizeof(long) != 8) {
4994 /* make %l into %ll */
4995 if (ls == 1 && (p = strchr(format, 'l')))
4996 memmove(p+1, p, strlen(p)+1);
4997 else if (strcmp(format, "%p") == 0)
4998 strcpy(format, "0x%llx");
5004 trace_seq_printf(s, format, len_arg, (char)val);
5006 trace_seq_printf(s, format, (char)val);
5010 trace_seq_printf(s, format, len_arg, (short)val);
5012 trace_seq_printf(s, format, (short)val);
5016 trace_seq_printf(s, format, len_arg, (int)val);
5018 trace_seq_printf(s, format, (int)val);
5022 trace_seq_printf(s, format, len_arg, (long)val);
5024 trace_seq_printf(s, format, (long)val);
5028 trace_seq_printf(s, format, len_arg,
5031 trace_seq_printf(s, format, (long long)val);
5034 do_warning_event(event, "bad count (%d)", ls);
5035 event->flags |= EVENT_FL_FAILED;
5040 do_warning_event(event, "no matching argument");
5041 event->flags |= EVENT_FL_FAILED;
5045 len = ((unsigned long)ptr + 1) -
5046 (unsigned long)saveptr;
5048 /* should never happen */
5050 do_warning_event(event, "bad format!");
5051 event->flags |= EVENT_FL_FAILED;
5055 memcpy(format, saveptr, len);
5059 /* Use helper trace_seq */
5061 print_str_arg(&p, data, size, event,
5062 format, len_arg, arg);
5063 trace_seq_terminate(&p);
5064 trace_seq_puts(s, p.buffer);
5065 trace_seq_destroy(&p);
5069 trace_seq_printf(s, ">%c<", *ptr);
5073 trace_seq_putc(s, *ptr);
5076 if (event->flags & EVENT_FL_FAILED) {
5078 trace_seq_printf(s, "[FAILED TO PARSE]");
5088 * pevent_data_lat_fmt - parse the data for the latency format
5089 * @pevent: a handle to the pevent
5090 * @s: the trace_seq to write to
5091 * @record: the record to read from
5093 * This parses out the Latency format (interrupts disabled,
5094 * need rescheduling, in hard/soft interrupt, preempt count
5095 * and lock depth) and places it into the trace_seq.
5097 void pevent_data_lat_fmt(struct pevent *pevent,
5098 struct trace_seq *s, struct pevent_record *record)
5100 static int check_lock_depth = 1;
5101 static int check_migrate_disable = 1;
5102 static int lock_depth_exists;
5103 static int migrate_disable_exists;
5104 unsigned int lat_flags;
5107 int migrate_disable;
5110 void *data = record->data;
5112 lat_flags = parse_common_flags(pevent, data);
5113 pc = parse_common_pc(pevent, data);
5114 /* lock_depth may not always exist */
5115 if (lock_depth_exists)
5116 lock_depth = parse_common_lock_depth(pevent, data);
5117 else if (check_lock_depth) {
5118 lock_depth = parse_common_lock_depth(pevent, data);
5120 check_lock_depth = 0;
5122 lock_depth_exists = 1;
5125 /* migrate_disable may not always exist */
5126 if (migrate_disable_exists)
5127 migrate_disable = parse_common_migrate_disable(pevent, data);
5128 else if (check_migrate_disable) {
5129 migrate_disable = parse_common_migrate_disable(pevent, data);
5130 if (migrate_disable < 0)
5131 check_migrate_disable = 0;
5133 migrate_disable_exists = 1;
5136 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5137 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5139 trace_seq_printf(s, "%c%c%c",
5140 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5141 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5143 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5145 (hardirq && softirq) ? 'H' :
5146 hardirq ? 'h' : softirq ? 's' : '.');
5149 trace_seq_printf(s, "%x", pc);
5151 trace_seq_putc(s, '.');
5153 if (migrate_disable_exists) {
5154 if (migrate_disable < 0)
5155 trace_seq_putc(s, '.');
5157 trace_seq_printf(s, "%d", migrate_disable);
5160 if (lock_depth_exists) {
5162 trace_seq_putc(s, '.');
5164 trace_seq_printf(s, "%d", lock_depth);
5167 trace_seq_terminate(s);
5171 * pevent_data_type - parse out the given event type
5172 * @pevent: a handle to the pevent
5173 * @rec: the record to read from
5175 * This returns the event id from the @rec.
5177 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
5179 return trace_parse_common_type(pevent, rec->data);
5183 * pevent_data_event_from_type - find the event by a given type
5184 * @pevent: a handle to the pevent
5185 * @type: the type of the event.
5187 * This returns the event form a given @type;
5189 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5191 return pevent_find_event(pevent, type);
5195 * pevent_data_pid - parse the PID from raw data
5196 * @pevent: a handle to the pevent
5197 * @rec: the record to parse
5199 * This returns the PID from a raw data.
5201 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5203 return parse_common_pid(pevent, rec->data);
5207 * pevent_data_comm_from_pid - return the command line from PID
5208 * @pevent: a handle to the pevent
5209 * @pid: the PID of the task to search for
5211 * This returns a pointer to the command line that has the given
5214 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5218 comm = find_cmdline(pevent, pid);
5222 static struct cmdline *
5223 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5225 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5228 cmdlist = cmdlist->next;
5230 cmdlist = pevent->cmdlist;
5232 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5233 cmdlist = cmdlist->next;
5235 return (struct cmdline *)cmdlist;
5239 * pevent_data_pid_from_comm - return the pid from a given comm
5240 * @pevent: a handle to the pevent
5241 * @comm: the cmdline to find the pid from
5242 * @next: the cmdline structure to find the next comm
5244 * This returns the cmdline structure that holds a pid for a given
5245 * comm, or NULL if none found. As there may be more than one pid for
5246 * a given comm, the result of this call can be passed back into
5247 * a recurring call in the @next paramater, and then it will find the
5249 * Also, it does a linear seach, so it may be slow.
5251 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5252 struct cmdline *next)
5254 struct cmdline *cmdline;
5257 * If the cmdlines have not been converted yet, then use
5260 if (!pevent->cmdlines)
5261 return pid_from_cmdlist(pevent, comm, next);
5265 * The next pointer could have been still from
5266 * a previous call before cmdlines were created
5268 if (next < pevent->cmdlines ||
5269 next >= pevent->cmdlines + pevent->cmdline_count)
5276 cmdline = pevent->cmdlines;
5278 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5279 if (strcmp(cmdline->comm, comm) == 0)
5287 * pevent_cmdline_pid - return the pid associated to a given cmdline
5288 * @cmdline: The cmdline structure to get the pid from
5290 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5293 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5295 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5301 * If cmdlines have not been created yet, or cmdline is
5302 * not part of the array, then treat it as a cmdlist instead.
5304 if (!pevent->cmdlines ||
5305 cmdline < pevent->cmdlines ||
5306 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5307 return cmdlist->pid;
5309 return cmdline->pid;
5313 * pevent_data_comm_from_pid - parse the data into the print format
5314 * @s: the trace_seq to write to
5315 * @event: the handle to the event
5316 * @record: the record to read from
5318 * This parses the raw @data using the given @event information and
5319 * writes the print format into the trace_seq.
5321 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5322 struct pevent_record *record)
5324 int print_pretty = 1;
5326 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5327 pevent_print_fields(s, record->data, record->size, event);
5330 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5331 print_pretty = event->handler(s, record, event,
5335 pretty_print(s, record->data, record->size, event);
5338 trace_seq_terminate(s);
5341 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5343 if (!use_trace_clock)
5346 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5347 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5350 /* trace_clock is setting in tsc or counter mode */
5355 * pevent_find_event_by_record - return the event from a given record
5356 * @pevent: a handle to the pevent
5357 * @record: The record to get the event from
5359 * Returns the associated event for a given record, or NULL if non is
5362 struct event_format *
5363 pevent_find_event_by_record(struct pevent *pevent, struct pevent_record *record)
5367 if (record->size < 0) {
5368 do_warning("ug! negative record size %d", record->size);
5372 type = trace_parse_common_type(pevent, record->data);
5374 return pevent_find_event(pevent, type);
5378 * pevent_print_event_task - Write the event task comm, pid and CPU
5379 * @pevent: a handle to the pevent
5380 * @s: the trace_seq to write to
5381 * @event: the handle to the record's event
5382 * @record: The record to get the event from
5384 * Writes the tasks comm, pid and CPU to @s.
5386 void pevent_print_event_task(struct pevent *pevent, struct trace_seq *s,
5387 struct event_format *event,
5388 struct pevent_record *record)
5390 void *data = record->data;
5394 pid = parse_common_pid(pevent, data);
5395 comm = find_cmdline(pevent, pid);
5397 if (pevent->latency_format) {
5398 trace_seq_printf(s, "%8.8s-%-5d %3d",
5399 comm, pid, record->cpu);
5401 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5405 * pevent_print_event_time - Write the event timestamp
5406 * @pevent: a handle to the pevent
5407 * @s: the trace_seq to write to
5408 * @event: the handle to the record's event
5409 * @record: The record to get the event from
5410 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5412 * Writes the timestamp of the record into @s.
5414 void pevent_print_event_time(struct pevent *pevent, struct trace_seq *s,
5415 struct event_format *event,
5416 struct pevent_record *record,
5417 bool use_trace_clock)
5420 unsigned long usecs;
5421 unsigned long nsecs;
5423 bool use_usec_format;
5425 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5427 if (use_usec_format) {
5428 secs = record->ts / NSECS_PER_SEC;
5429 nsecs = record->ts - secs * NSECS_PER_SEC;
5432 if (pevent->latency_format) {
5433 pevent_data_lat_fmt(pevent, s, record);
5436 if (use_usec_format) {
5437 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5441 usecs = (nsecs + 500) / NSECS_PER_USEC;
5442 /* To avoid usecs larger than 1 sec */
5443 if (usecs >= 1000000) {
5450 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5452 trace_seq_printf(s, " %12llu:", record->ts);
5456 * pevent_print_event_data - Write the event data section
5457 * @pevent: a handle to the pevent
5458 * @s: the trace_seq to write to
5459 * @event: the handle to the record's event
5460 * @record: The record to get the event from
5462 * Writes the parsing of the record's data to @s.
5464 void pevent_print_event_data(struct pevent *pevent, struct trace_seq *s,
5465 struct event_format *event,
5466 struct pevent_record *record)
5468 static const char *spaces = " "; /* 20 spaces */
5471 trace_seq_printf(s, " %s: ", event->name);
5473 /* Space out the event names evenly. */
5474 len = strlen(event->name);
5476 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5478 pevent_event_info(s, event, record);
5481 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5482 struct pevent_record *record, bool use_trace_clock)
5484 struct event_format *event;
5486 event = pevent_find_event_by_record(pevent, record);
5488 do_warning("ug! no event found for type %d",
5489 trace_parse_common_type(pevent, record->data));
5493 pevent_print_event_task(pevent, s, event, record);
5494 pevent_print_event_time(pevent, s, event, record, use_trace_clock);
5495 pevent_print_event_data(pevent, s, event, record);
5498 static int events_id_cmp(const void *a, const void *b)
5500 struct event_format * const * ea = a;
5501 struct event_format * const * eb = b;
5503 if ((*ea)->id < (*eb)->id)
5506 if ((*ea)->id > (*eb)->id)
5512 static int events_name_cmp(const void *a, const void *b)
5514 struct event_format * const * ea = a;
5515 struct event_format * const * eb = b;
5518 res = strcmp((*ea)->name, (*eb)->name);
5522 res = strcmp((*ea)->system, (*eb)->system);
5526 return events_id_cmp(a, b);
5529 static int events_system_cmp(const void *a, const void *b)
5531 struct event_format * const * ea = a;
5532 struct event_format * const * eb = b;
5535 res = strcmp((*ea)->system, (*eb)->system);
5539 res = strcmp((*ea)->name, (*eb)->name);
5543 return events_id_cmp(a, b);
5546 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5548 struct event_format **events;
5549 int (*sort)(const void *a, const void *b);
5551 events = pevent->sort_events;
5553 if (events && pevent->last_type == sort_type)
5557 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5561 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5562 events[pevent->nr_events] = NULL;
5564 pevent->sort_events = events;
5566 /* the internal events are sorted by id */
5567 if (sort_type == EVENT_SORT_ID) {
5568 pevent->last_type = sort_type;
5573 switch (sort_type) {
5575 sort = events_id_cmp;
5577 case EVENT_SORT_NAME:
5578 sort = events_name_cmp;
5580 case EVENT_SORT_SYSTEM:
5581 sort = events_system_cmp;
5587 qsort(events, pevent->nr_events, sizeof(*events), sort);
5588 pevent->last_type = sort_type;
5593 static struct format_field **
5594 get_event_fields(const char *type, const char *name,
5595 int count, struct format_field *list)
5597 struct format_field **fields;
5598 struct format_field *field;
5601 fields = malloc(sizeof(*fields) * (count + 1));
5605 for (field = list; field; field = field->next) {
5606 fields[i++] = field;
5607 if (i == count + 1) {
5608 do_warning("event %s has more %s fields than specified",
5616 do_warning("event %s has less %s fields than specified",
5625 * pevent_event_common_fields - return a list of common fields for an event
5626 * @event: the event to return the common fields of.
5628 * Returns an allocated array of fields. The last item in the array is NULL.
5629 * The array must be freed with free().
5631 struct format_field **pevent_event_common_fields(struct event_format *event)
5633 return get_event_fields("common", event->name,
5634 event->format.nr_common,
5635 event->format.common_fields);
5639 * pevent_event_fields - return a list of event specific fields for an event
5640 * @event: the event to return the fields of.
5642 * Returns an allocated array of fields. The last item in the array is NULL.
5643 * The array must be freed with free().
5645 struct format_field **pevent_event_fields(struct event_format *event)
5647 return get_event_fields("event", event->name,
5648 event->format.nr_fields,
5649 event->format.fields);
5652 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5654 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5656 trace_seq_puts(s, ", ");
5657 print_fields(s, field->next);
5662 static void print_args(struct print_arg *args)
5664 int print_paren = 1;
5667 switch (args->type) {
5672 printf("%s", args->atom.atom);
5675 printf("REC->%s", args->field.name);
5678 printf("__print_flags(");
5679 print_args(args->flags.field);
5680 printf(", %s, ", args->flags.delim);
5682 print_fields(&s, args->flags.flags);
5683 trace_seq_do_printf(&s);
5684 trace_seq_destroy(&s);
5688 printf("__print_symbolic(");
5689 print_args(args->symbol.field);
5692 print_fields(&s, args->symbol.symbols);
5693 trace_seq_do_printf(&s);
5694 trace_seq_destroy(&s);
5698 printf("__print_hex(");
5699 print_args(args->hex.field);
5701 print_args(args->hex.size);
5704 case PRINT_INT_ARRAY:
5705 printf("__print_array(");
5706 print_args(args->int_array.field);
5708 print_args(args->int_array.count);
5710 print_args(args->int_array.el_size);
5715 printf("__get_str(%s)", args->string.string);
5718 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5721 printf("(%s)", args->typecast.type);
5722 print_args(args->typecast.item);
5725 if (strcmp(args->op.op, ":") == 0)
5729 print_args(args->op.left);
5730 printf(" %s ", args->op.op);
5731 print_args(args->op.right);
5736 /* we should warn... */
5741 print_args(args->next);
5745 static void parse_header_field(const char *field,
5746 int *offset, int *size, int mandatory)
5748 unsigned long long save_input_buf_ptr;
5749 unsigned long long save_input_buf_siz;
5753 save_input_buf_ptr = input_buf_ptr;
5754 save_input_buf_siz = input_buf_siz;
5756 if (read_expected(EVENT_ITEM, "field") < 0)
5758 if (read_expected(EVENT_OP, ":") < 0)
5762 if (read_expect_type(EVENT_ITEM, &token) < 0)
5767 * If this is not a mandatory field, then test it first.
5770 if (read_expected(EVENT_ITEM, field) < 0)
5773 if (read_expect_type(EVENT_ITEM, &token) < 0)
5775 if (strcmp(token, field) != 0)
5780 if (read_expected(EVENT_OP, ";") < 0)
5782 if (read_expected(EVENT_ITEM, "offset") < 0)
5784 if (read_expected(EVENT_OP, ":") < 0)
5786 if (read_expect_type(EVENT_ITEM, &token) < 0)
5788 *offset = atoi(token);
5790 if (read_expected(EVENT_OP, ";") < 0)
5792 if (read_expected(EVENT_ITEM, "size") < 0)
5794 if (read_expected(EVENT_OP, ":") < 0)
5796 if (read_expect_type(EVENT_ITEM, &token) < 0)
5798 *size = atoi(token);
5800 if (read_expected(EVENT_OP, ";") < 0)
5802 type = read_token(&token);
5803 if (type != EVENT_NEWLINE) {
5804 /* newer versions of the kernel have a "signed" type */
5805 if (type != EVENT_ITEM)
5808 if (strcmp(token, "signed") != 0)
5813 if (read_expected(EVENT_OP, ":") < 0)
5816 if (read_expect_type(EVENT_ITEM, &token))
5820 if (read_expected(EVENT_OP, ";") < 0)
5823 if (read_expect_type(EVENT_NEWLINE, &token))
5831 input_buf_ptr = save_input_buf_ptr;
5832 input_buf_siz = save_input_buf_siz;
5839 * pevent_parse_header_page - parse the data stored in the header page
5840 * @pevent: the handle to the pevent
5841 * @buf: the buffer storing the header page format string
5842 * @size: the size of @buf
5843 * @long_size: the long size to use if there is no header
5845 * This parses the header page format for information on the
5846 * ring buffer used. The @buf should be copied from
5848 * /sys/kernel/debug/tracing/events/header_page
5850 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5857 * Old kernels did not have header page info.
5858 * Sorry but we just use what we find here in user space.
5860 pevent->header_page_ts_size = sizeof(long long);
5861 pevent->header_page_size_size = long_size;
5862 pevent->header_page_data_offset = sizeof(long long) + long_size;
5863 pevent->old_format = 1;
5866 init_input_buf(buf, size);
5868 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5869 &pevent->header_page_ts_size, 1);
5870 parse_header_field("commit", &pevent->header_page_size_offset,
5871 &pevent->header_page_size_size, 1);
5872 parse_header_field("overwrite", &pevent->header_page_overwrite,
5874 parse_header_field("data", &pevent->header_page_data_offset,
5875 &pevent->header_page_data_size, 1);
5880 static int event_matches(struct event_format *event,
5881 int id, const char *sys_name,
5882 const char *event_name)
5884 if (id >= 0 && id != event->id)
5887 if (event_name && (strcmp(event_name, event->name) != 0))
5890 if (sys_name && (strcmp(sys_name, event->system) != 0))
5896 static void free_handler(struct event_handler *handle)
5898 free((void *)handle->sys_name);
5899 free((void *)handle->event_name);
5903 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5905 struct event_handler *handle, **next;
5907 for (next = &pevent->handlers; *next;
5908 next = &(*next)->next) {
5910 if (event_matches(event, handle->id,
5912 handle->event_name))
5919 pr_stat("overriding event (%d) %s:%s with new print handler",
5920 event->id, event->system, event->name);
5922 event->handler = handle->func;
5923 event->context = handle->context;
5925 *next = handle->next;
5926 free_handler(handle);
5932 * __pevent_parse_format - parse the event format
5933 * @buf: the buffer storing the event format string
5934 * @size: the size of @buf
5935 * @sys: the system the event belongs to
5937 * This parses the event format and creates an event structure
5938 * to quickly parse raw data for a given event.
5940 * These files currently come from:
5942 * /sys/kernel/debug/tracing/events/.../.../format
5944 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5945 struct pevent *pevent, const char *buf,
5946 unsigned long size, const char *sys)
5948 struct event_format *event;
5951 init_input_buf(buf, size);
5953 *eventp = event = alloc_event();
5955 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5957 event->name = event_read_name();
5960 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5961 goto event_alloc_failed;
5964 if (strcmp(sys, "ftrace") == 0) {
5965 event->flags |= EVENT_FL_ISFTRACE;
5967 if (strcmp(event->name, "bprint") == 0)
5968 event->flags |= EVENT_FL_ISBPRINT;
5971 event->id = event_read_id();
5972 if (event->id < 0) {
5973 ret = PEVENT_ERRNO__READ_ID_FAILED;
5975 * This isn't an allocation error actually.
5976 * But as the ID is critical, just bail out.
5978 goto event_alloc_failed;
5981 event->system = strdup(sys);
5982 if (!event->system) {
5983 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5984 goto event_alloc_failed;
5987 /* Add pevent to event so that it can be referenced */
5988 event->pevent = pevent;
5990 ret = event_read_format(event);
5992 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5993 goto event_parse_failed;
5997 * If the event has an override, don't print warnings if the event
5998 * print format fails to parse.
6000 if (pevent && find_event_handle(pevent, event))
6003 ret = event_read_print(event);
6007 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
6008 goto event_parse_failed;
6011 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6012 struct format_field *field;
6013 struct print_arg *arg, **list;
6015 /* old ftrace had no args */
6016 list = &event->print_fmt.args;
6017 for (field = event->format.fields; field; field = field->next) {
6020 event->flags |= EVENT_FL_FAILED;
6021 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6023 arg->type = PRINT_FIELD;
6024 arg->field.name = strdup(field->name);
6025 if (!arg->field.name) {
6026 event->flags |= EVENT_FL_FAILED;
6028 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6030 arg->field.field = field;
6040 event->flags |= EVENT_FL_FAILED;
6044 free(event->system);
6051 static enum pevent_errno
6052 __pevent_parse_event(struct pevent *pevent,
6053 struct event_format **eventp,
6054 const char *buf, unsigned long size,
6057 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
6058 struct event_format *event = *eventp;
6063 if (pevent && add_event(pevent, event)) {
6064 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6065 goto event_add_failed;
6068 #define PRINT_ARGS 0
6069 if (PRINT_ARGS && event->print_fmt.args)
6070 print_args(event->print_fmt.args);
6075 pevent_free_format(event);
6080 * pevent_parse_format - parse the event format
6081 * @pevent: the handle to the pevent
6082 * @eventp: returned format
6083 * @buf: the buffer storing the event format string
6084 * @size: the size of @buf
6085 * @sys: the system the event belongs to
6087 * This parses the event format and creates an event structure
6088 * to quickly parse raw data for a given event.
6090 * These files currently come from:
6092 * /sys/kernel/debug/tracing/events/.../.../format
6094 enum pevent_errno pevent_parse_format(struct pevent *pevent,
6095 struct event_format **eventp,
6097 unsigned long size, const char *sys)
6099 return __pevent_parse_event(pevent, eventp, buf, size, sys);
6103 * pevent_parse_event - parse the event format
6104 * @pevent: the handle to the pevent
6105 * @buf: the buffer storing the event format string
6106 * @size: the size of @buf
6107 * @sys: the system the event belongs to
6109 * This parses the event format and creates an event structure
6110 * to quickly parse raw data for a given event.
6112 * These files currently come from:
6114 * /sys/kernel/debug/tracing/events/.../.../format
6116 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
6117 unsigned long size, const char *sys)
6119 struct event_format *event = NULL;
6120 return __pevent_parse_event(pevent, &event, buf, size, sys);
6124 #define _PE(code, str) str
6125 static const char * const pevent_error_str[] = {
6130 int pevent_strerror(struct pevent *pevent __maybe_unused,
6131 enum pevent_errno errnum, char *buf, size_t buflen)
6137 str_error_r(errnum, buf, buflen);
6141 if (errnum <= __PEVENT_ERRNO__START ||
6142 errnum >= __PEVENT_ERRNO__END)
6145 idx = errnum - __PEVENT_ERRNO__START - 1;
6146 msg = pevent_error_str[idx];
6147 snprintf(buf, buflen, "%s", msg);
6152 int get_field_val(struct trace_seq *s, struct format_field *field,
6153 const char *name, struct pevent_record *record,
6154 unsigned long long *val, int err)
6158 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6162 if (pevent_read_number_field(field, record->data, val)) {
6164 trace_seq_printf(s, " %s=INVALID", name);
6172 * pevent_get_field_raw - return the raw pointer into the data field
6173 * @s: The seq to print to on error
6174 * @event: the event that the field is for
6175 * @name: The name of the field
6176 * @record: The record with the field name.
6177 * @len: place to store the field length.
6178 * @err: print default error if failed.
6180 * Returns a pointer into record->data of the field and places
6181 * the length of the field in @len.
6183 * On failure, it returns NULL.
6185 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
6186 const char *name, struct pevent_record *record,
6189 struct format_field *field;
6190 void *data = record->data;
6197 field = pevent_find_field(event, name);
6201 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6205 /* Allow @len to be NULL */
6209 offset = field->offset;
6210 if (field->flags & FIELD_IS_DYNAMIC) {
6211 offset = pevent_read_number(event->pevent,
6212 data + offset, field->size);
6213 *len = offset >> 16;
6218 return data + offset;
6222 * pevent_get_field_val - find a field and return its value
6223 * @s: The seq to print to on error
6224 * @event: the event that the field is for
6225 * @name: The name of the field
6226 * @record: The record with the field name.
6227 * @val: place to store the value of the field.
6228 * @err: print default error if failed.
6230 * Returns 0 on success -1 on field not found.
6232 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
6233 const char *name, struct pevent_record *record,
6234 unsigned long long *val, int err)
6236 struct format_field *field;
6241 field = pevent_find_field(event, name);
6243 return get_field_val(s, field, name, record, val, err);
6247 * pevent_get_common_field_val - find a common field and return its value
6248 * @s: The seq to print to on error
6249 * @event: the event that the field is for
6250 * @name: The name of the field
6251 * @record: The record with the field name.
6252 * @val: place to store the value of the field.
6253 * @err: print default error if failed.
6255 * Returns 0 on success -1 on field not found.
6257 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6258 const char *name, struct pevent_record *record,
6259 unsigned long long *val, int err)
6261 struct format_field *field;
6266 field = pevent_find_common_field(event, name);
6268 return get_field_val(s, field, name, record, val, err);
6272 * pevent_get_any_field_val - find a any field and return its value
6273 * @s: The seq to print to on error
6274 * @event: the event that the field is for
6275 * @name: The name of the field
6276 * @record: The record with the field name.
6277 * @val: place to store the value of the field.
6278 * @err: print default error if failed.
6280 * Returns 0 on success -1 on field not found.
6282 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6283 const char *name, struct pevent_record *record,
6284 unsigned long long *val, int err)
6286 struct format_field *field;
6291 field = pevent_find_any_field(event, name);
6293 return get_field_val(s, field, name, record, val, err);
6297 * pevent_print_num_field - print a field and a format
6298 * @s: The seq to print to
6299 * @fmt: The printf format to print the field with.
6300 * @event: the event that the field is for
6301 * @name: The name of the field
6302 * @record: The record with the field name.
6303 * @err: print default error if failed.
6305 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6307 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6308 struct event_format *event, const char *name,
6309 struct pevent_record *record, int err)
6311 struct format_field *field = pevent_find_field(event, name);
6312 unsigned long long val;
6317 if (pevent_read_number_field(field, record->data, &val))
6320 return trace_seq_printf(s, fmt, val);
6324 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6329 * pevent_print_func_field - print a field and a format for function pointers
6330 * @s: The seq to print to
6331 * @fmt: The printf format to print the field with.
6332 * @event: the event that the field is for
6333 * @name: The name of the field
6334 * @record: The record with the field name.
6335 * @err: print default error if failed.
6337 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6339 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6340 struct event_format *event, const char *name,
6341 struct pevent_record *record, int err)
6343 struct format_field *field = pevent_find_field(event, name);
6344 struct pevent *pevent = event->pevent;
6345 unsigned long long val;
6346 struct func_map *func;
6352 if (pevent_read_number_field(field, record->data, &val))
6355 func = find_func(pevent, val);
6358 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6360 sprintf(tmp, "0x%08llx", val);
6362 return trace_seq_printf(s, fmt, tmp);
6366 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6370 static void free_func_handle(struct pevent_function_handler *func)
6372 struct pevent_func_params *params;
6376 while (func->params) {
6377 params = func->params;
6378 func->params = params->next;
6386 * pevent_register_print_function - register a helper function
6387 * @pevent: the handle to the pevent
6388 * @func: the function to process the helper function
6389 * @ret_type: the return type of the helper function
6390 * @name: the name of the helper function
6391 * @parameters: A list of enum pevent_func_arg_type
6393 * Some events may have helper functions in the print format arguments.
6394 * This allows a plugin to dynamically create a way to process one
6395 * of these functions.
6397 * The @parameters is a variable list of pevent_func_arg_type enums that
6398 * must end with PEVENT_FUNC_ARG_VOID.
6400 int pevent_register_print_function(struct pevent *pevent,
6401 pevent_func_handler func,
6402 enum pevent_func_arg_type ret_type,
6405 struct pevent_function_handler *func_handle;
6406 struct pevent_func_params **next_param;
6407 struct pevent_func_params *param;
6408 enum pevent_func_arg_type type;
6412 func_handle = find_func_handler(pevent, name);
6415 * This is most like caused by the users own
6416 * plugins updating the function. This overrides the
6419 pr_stat("override of function helper '%s'", name);
6420 remove_func_handler(pevent, name);
6423 func_handle = calloc(1, sizeof(*func_handle));
6425 do_warning("Failed to allocate function handler");
6426 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6429 func_handle->ret_type = ret_type;
6430 func_handle->name = strdup(name);
6431 func_handle->func = func;
6432 if (!func_handle->name) {
6433 do_warning("Failed to allocate function name");
6435 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6438 next_param = &(func_handle->params);
6441 type = va_arg(ap, enum pevent_func_arg_type);
6442 if (type == PEVENT_FUNC_ARG_VOID)
6445 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6446 do_warning("Invalid argument type %d", type);
6447 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6451 param = malloc(sizeof(*param));
6453 do_warning("Failed to allocate function param");
6454 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6460 *next_param = param;
6461 next_param = &(param->next);
6463 func_handle->nr_args++;
6467 func_handle->next = pevent->func_handlers;
6468 pevent->func_handlers = func_handle;
6473 free_func_handle(func_handle);
6478 * pevent_unregister_print_function - unregister a helper function
6479 * @pevent: the handle to the pevent
6480 * @func: the function to process the helper function
6481 * @name: the name of the helper function
6483 * This function removes existing print handler for function @name.
6485 * Returns 0 if the handler was removed successully, -1 otherwise.
6487 int pevent_unregister_print_function(struct pevent *pevent,
6488 pevent_func_handler func, char *name)
6490 struct pevent_function_handler *func_handle;
6492 func_handle = find_func_handler(pevent, name);
6493 if (func_handle && func_handle->func == func) {
6494 remove_func_handler(pevent, name);
6500 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6501 const char *sys_name,
6502 const char *event_name)
6504 struct event_format *event;
6508 event = pevent_find_event(pevent, id);
6511 if (event_name && (strcmp(event_name, event->name) != 0))
6513 if (sys_name && (strcmp(sys_name, event->system) != 0))
6516 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6524 * pevent_register_event_handler - register a way to parse an event
6525 * @pevent: the handle to the pevent
6526 * @id: the id of the event to register
6527 * @sys_name: the system name the event belongs to
6528 * @event_name: the name of the event
6529 * @func: the function to call to parse the event information
6530 * @context: the data to be passed to @func
6532 * This function allows a developer to override the parsing of
6533 * a given event. If for some reason the default print format
6534 * is not sufficient, this function will register a function
6535 * for an event to be used to parse the data instead.
6537 * If @id is >= 0, then it is used to find the event.
6538 * else @sys_name and @event_name are used.
6540 int pevent_register_event_handler(struct pevent *pevent, int id,
6541 const char *sys_name, const char *event_name,
6542 pevent_event_handler_func func, void *context)
6544 struct event_format *event;
6545 struct event_handler *handle;
6547 event = pevent_search_event(pevent, id, sys_name, event_name);
6551 pr_stat("overriding event (%d) %s:%s with new print handler",
6552 event->id, event->system, event->name);
6554 event->handler = func;
6555 event->context = context;
6559 /* Save for later use. */
6560 handle = calloc(1, sizeof(*handle));
6562 do_warning("Failed to allocate event handler");
6563 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6568 handle->event_name = strdup(event_name);
6570 handle->sys_name = strdup(sys_name);
6572 if ((event_name && !handle->event_name) ||
6573 (sys_name && !handle->sys_name)) {
6574 do_warning("Failed to allocate event/sys name");
6575 free((void *)handle->event_name);
6576 free((void *)handle->sys_name);
6578 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6581 handle->func = func;
6582 handle->next = pevent->handlers;
6583 pevent->handlers = handle;
6584 handle->context = context;
6589 static int handle_matches(struct event_handler *handler, int id,
6590 const char *sys_name, const char *event_name,
6591 pevent_event_handler_func func, void *context)
6593 if (id >= 0 && id != handler->id)
6596 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6599 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6602 if (func != handler->func || context != handler->context)
6609 * pevent_unregister_event_handler - unregister an existing event handler
6610 * @pevent: the handle to the pevent
6611 * @id: the id of the event to unregister
6612 * @sys_name: the system name the handler belongs to
6613 * @event_name: the name of the event handler
6614 * @func: the function to call to parse the event information
6615 * @context: the data to be passed to @func
6617 * This function removes existing event handler (parser).
6619 * If @id is >= 0, then it is used to find the event.
6620 * else @sys_name and @event_name are used.
6622 * Returns 0 if handler was removed successfully, -1 if event was not found.
6624 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6625 const char *sys_name, const char *event_name,
6626 pevent_event_handler_func func, void *context)
6628 struct event_format *event;
6629 struct event_handler *handle;
6630 struct event_handler **next;
6632 event = pevent_search_event(pevent, id, sys_name, event_name);
6636 if (event->handler == func && event->context == context) {
6637 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6638 event->id, event->system, event->name);
6640 event->handler = NULL;
6641 event->context = NULL;
6646 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6648 if (handle_matches(handle, id, sys_name, event_name,
6656 *next = handle->next;
6657 free_handler(handle);
6663 * pevent_alloc - create a pevent handle
6665 struct pevent *pevent_alloc(void)
6667 struct pevent *pevent = calloc(1, sizeof(*pevent));
6670 pevent->ref_count = 1;
6675 void pevent_ref(struct pevent *pevent)
6677 pevent->ref_count++;
6680 void pevent_free_format_field(struct format_field *field)
6683 if (field->alias != field->name)
6689 static void free_format_fields(struct format_field *field)
6691 struct format_field *next;
6695 pevent_free_format_field(field);
6700 static void free_formats(struct format *format)
6702 free_format_fields(format->common_fields);
6703 free_format_fields(format->fields);
6706 void pevent_free_format(struct event_format *event)
6709 free(event->system);
6711 free_formats(&event->format);
6713 free(event->print_fmt.format);
6714 free_args(event->print_fmt.args);
6720 * pevent_free - free a pevent handle
6721 * @pevent: the pevent handle to free
6723 void pevent_free(struct pevent *pevent)
6725 struct cmdline_list *cmdlist, *cmdnext;
6726 struct func_list *funclist, *funcnext;
6727 struct printk_list *printklist, *printknext;
6728 struct pevent_function_handler *func_handler;
6729 struct event_handler *handle;
6735 cmdlist = pevent->cmdlist;
6736 funclist = pevent->funclist;
6737 printklist = pevent->printklist;
6739 pevent->ref_count--;
6740 if (pevent->ref_count)
6743 if (pevent->cmdlines) {
6744 for (i = 0; i < pevent->cmdline_count; i++)
6745 free(pevent->cmdlines[i].comm);
6746 free(pevent->cmdlines);
6750 cmdnext = cmdlist->next;
6751 free(cmdlist->comm);
6756 if (pevent->func_map) {
6757 for (i = 0; i < (int)pevent->func_count; i++) {
6758 free(pevent->func_map[i].func);
6759 free(pevent->func_map[i].mod);
6761 free(pevent->func_map);
6765 funcnext = funclist->next;
6766 free(funclist->func);
6767 free(funclist->mod);
6769 funclist = funcnext;
6772 while (pevent->func_handlers) {
6773 func_handler = pevent->func_handlers;
6774 pevent->func_handlers = func_handler->next;
6775 free_func_handle(func_handler);
6778 if (pevent->printk_map) {
6779 for (i = 0; i < (int)pevent->printk_count; i++)
6780 free(pevent->printk_map[i].printk);
6781 free(pevent->printk_map);
6784 while (printklist) {
6785 printknext = printklist->next;
6786 free(printklist->printk);
6788 printklist = printknext;
6791 for (i = 0; i < pevent->nr_events; i++)
6792 pevent_free_format(pevent->events[i]);
6794 while (pevent->handlers) {
6795 handle = pevent->handlers;
6796 pevent->handlers = handle->next;
6797 free_handler(handle);
6800 free(pevent->trace_clock);
6801 free(pevent->events);
6802 free(pevent->sort_events);
6803 free(pevent->func_resolver);
6808 void pevent_unref(struct pevent *pevent)
6810 pevent_free(pevent);