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 (*(ptr+1) == 'F' || *(ptr+1) == 'f' ||
4931 *(ptr+1) == 'S' || *(ptr+1) == 's') {
4934 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
4935 print_mac_arg(s, *(ptr+1), data, size, event, arg);
4939 } else if (*(ptr+1) == 'I' || *(ptr+1) == 'i') {
4942 n = print_ip_arg(s, ptr+1, data, size, event, arg);
4957 do_warning_event(event, "no argument match");
4958 event->flags |= EVENT_FL_FAILED;
4962 len = ((unsigned long)ptr + 1) -
4963 (unsigned long)saveptr;
4965 /* should never happen */
4967 do_warning_event(event, "bad format!");
4968 event->flags |= EVENT_FL_FAILED;
4972 memcpy(format, saveptr, len);
4975 val = eval_num_arg(data, size, event, arg);
4979 func = find_func(pevent, val);
4981 trace_seq_puts(s, func->func);
4982 if (show_func == 'F')
4989 if (pevent->long_size == 8 && ls == 1 &&
4990 sizeof(long) != 8) {
4993 /* make %l into %ll */
4994 if (ls == 1 && (p = strchr(format, 'l')))
4995 memmove(p+1, p, strlen(p)+1);
4996 else if (strcmp(format, "%p") == 0)
4997 strcpy(format, "0x%llx");
5003 trace_seq_printf(s, format, len_arg, (char)val);
5005 trace_seq_printf(s, format, (char)val);
5009 trace_seq_printf(s, format, len_arg, (short)val);
5011 trace_seq_printf(s, format, (short)val);
5015 trace_seq_printf(s, format, len_arg, (int)val);
5017 trace_seq_printf(s, format, (int)val);
5021 trace_seq_printf(s, format, len_arg, (long)val);
5023 trace_seq_printf(s, format, (long)val);
5027 trace_seq_printf(s, format, len_arg,
5030 trace_seq_printf(s, format, (long long)val);
5033 do_warning_event(event, "bad count (%d)", ls);
5034 event->flags |= EVENT_FL_FAILED;
5039 do_warning_event(event, "no matching argument");
5040 event->flags |= EVENT_FL_FAILED;
5044 len = ((unsigned long)ptr + 1) -
5045 (unsigned long)saveptr;
5047 /* should never happen */
5049 do_warning_event(event, "bad format!");
5050 event->flags |= EVENT_FL_FAILED;
5054 memcpy(format, saveptr, len);
5058 /* Use helper trace_seq */
5060 print_str_arg(&p, data, size, event,
5061 format, len_arg, arg);
5062 trace_seq_terminate(&p);
5063 trace_seq_puts(s, p.buffer);
5064 trace_seq_destroy(&p);
5068 trace_seq_printf(s, ">%c<", *ptr);
5072 trace_seq_putc(s, *ptr);
5075 if (event->flags & EVENT_FL_FAILED) {
5077 trace_seq_printf(s, "[FAILED TO PARSE]");
5087 * pevent_data_lat_fmt - parse the data for the latency format
5088 * @pevent: a handle to the pevent
5089 * @s: the trace_seq to write to
5090 * @record: the record to read from
5092 * This parses out the Latency format (interrupts disabled,
5093 * need rescheduling, in hard/soft interrupt, preempt count
5094 * and lock depth) and places it into the trace_seq.
5096 void pevent_data_lat_fmt(struct pevent *pevent,
5097 struct trace_seq *s, struct pevent_record *record)
5099 static int check_lock_depth = 1;
5100 static int check_migrate_disable = 1;
5101 static int lock_depth_exists;
5102 static int migrate_disable_exists;
5103 unsigned int lat_flags;
5106 int migrate_disable;
5109 void *data = record->data;
5111 lat_flags = parse_common_flags(pevent, data);
5112 pc = parse_common_pc(pevent, data);
5113 /* lock_depth may not always exist */
5114 if (lock_depth_exists)
5115 lock_depth = parse_common_lock_depth(pevent, data);
5116 else if (check_lock_depth) {
5117 lock_depth = parse_common_lock_depth(pevent, data);
5119 check_lock_depth = 0;
5121 lock_depth_exists = 1;
5124 /* migrate_disable may not always exist */
5125 if (migrate_disable_exists)
5126 migrate_disable = parse_common_migrate_disable(pevent, data);
5127 else if (check_migrate_disable) {
5128 migrate_disable = parse_common_migrate_disable(pevent, data);
5129 if (migrate_disable < 0)
5130 check_migrate_disable = 0;
5132 migrate_disable_exists = 1;
5135 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
5136 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
5138 trace_seq_printf(s, "%c%c%c",
5139 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
5140 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
5142 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
5144 (hardirq && softirq) ? 'H' :
5145 hardirq ? 'h' : softirq ? 's' : '.');
5148 trace_seq_printf(s, "%x", pc);
5150 trace_seq_putc(s, '.');
5152 if (migrate_disable_exists) {
5153 if (migrate_disable < 0)
5154 trace_seq_putc(s, '.');
5156 trace_seq_printf(s, "%d", migrate_disable);
5159 if (lock_depth_exists) {
5161 trace_seq_putc(s, '.');
5163 trace_seq_printf(s, "%d", lock_depth);
5166 trace_seq_terminate(s);
5170 * pevent_data_type - parse out the given event type
5171 * @pevent: a handle to the pevent
5172 * @rec: the record to read from
5174 * This returns the event id from the @rec.
5176 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
5178 return trace_parse_common_type(pevent, rec->data);
5182 * pevent_data_event_from_type - find the event by a given type
5183 * @pevent: a handle to the pevent
5184 * @type: the type of the event.
5186 * This returns the event form a given @type;
5188 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
5190 return pevent_find_event(pevent, type);
5194 * pevent_data_pid - parse the PID from raw data
5195 * @pevent: a handle to the pevent
5196 * @rec: the record to parse
5198 * This returns the PID from a raw data.
5200 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
5202 return parse_common_pid(pevent, rec->data);
5206 * pevent_data_comm_from_pid - return the command line from PID
5207 * @pevent: a handle to the pevent
5208 * @pid: the PID of the task to search for
5210 * This returns a pointer to the command line that has the given
5213 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
5217 comm = find_cmdline(pevent, pid);
5221 static struct cmdline *
5222 pid_from_cmdlist(struct pevent *pevent, const char *comm, struct cmdline *next)
5224 struct cmdline_list *cmdlist = (struct cmdline_list *)next;
5227 cmdlist = cmdlist->next;
5229 cmdlist = pevent->cmdlist;
5231 while (cmdlist && strcmp(cmdlist->comm, comm) != 0)
5232 cmdlist = cmdlist->next;
5234 return (struct cmdline *)cmdlist;
5238 * pevent_data_pid_from_comm - return the pid from a given comm
5239 * @pevent: a handle to the pevent
5240 * @comm: the cmdline to find the pid from
5241 * @next: the cmdline structure to find the next comm
5243 * This returns the cmdline structure that holds a pid for a given
5244 * comm, or NULL if none found. As there may be more than one pid for
5245 * a given comm, the result of this call can be passed back into
5246 * a recurring call in the @next paramater, and then it will find the
5248 * Also, it does a linear seach, so it may be slow.
5250 struct cmdline *pevent_data_pid_from_comm(struct pevent *pevent, const char *comm,
5251 struct cmdline *next)
5253 struct cmdline *cmdline;
5256 * If the cmdlines have not been converted yet, then use
5259 if (!pevent->cmdlines)
5260 return pid_from_cmdlist(pevent, comm, next);
5264 * The next pointer could have been still from
5265 * a previous call before cmdlines were created
5267 if (next < pevent->cmdlines ||
5268 next >= pevent->cmdlines + pevent->cmdline_count)
5275 cmdline = pevent->cmdlines;
5277 while (cmdline < pevent->cmdlines + pevent->cmdline_count) {
5278 if (strcmp(cmdline->comm, comm) == 0)
5286 * pevent_cmdline_pid - return the pid associated to a given cmdline
5287 * @cmdline: The cmdline structure to get the pid from
5289 * Returns the pid for a give cmdline. If @cmdline is NULL, then
5292 int pevent_cmdline_pid(struct pevent *pevent, struct cmdline *cmdline)
5294 struct cmdline_list *cmdlist = (struct cmdline_list *)cmdline;
5300 * If cmdlines have not been created yet, or cmdline is
5301 * not part of the array, then treat it as a cmdlist instead.
5303 if (!pevent->cmdlines ||
5304 cmdline < pevent->cmdlines ||
5305 cmdline >= pevent->cmdlines + pevent->cmdline_count)
5306 return cmdlist->pid;
5308 return cmdline->pid;
5312 * pevent_data_comm_from_pid - parse the data into the print format
5313 * @s: the trace_seq to write to
5314 * @event: the handle to the event
5315 * @record: the record to read from
5317 * This parses the raw @data using the given @event information and
5318 * writes the print format into the trace_seq.
5320 void pevent_event_info(struct trace_seq *s, struct event_format *event,
5321 struct pevent_record *record)
5323 int print_pretty = 1;
5325 if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))
5326 pevent_print_fields(s, record->data, record->size, event);
5329 if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))
5330 print_pretty = event->handler(s, record, event,
5334 pretty_print(s, record->data, record->size, event);
5337 trace_seq_terminate(s);
5340 static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock)
5342 if (!use_trace_clock)
5345 if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global")
5346 || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf"))
5349 /* trace_clock is setting in tsc or counter mode */
5354 * pevent_find_event_by_record - return the event from a given record
5355 * @pevent: a handle to the pevent
5356 * @record: The record to get the event from
5358 * Returns the associated event for a given record, or NULL if non is
5361 struct event_format *
5362 pevent_find_event_by_record(struct pevent *pevent, struct pevent_record *record)
5366 if (record->size < 0) {
5367 do_warning("ug! negative record size %d", record->size);
5371 type = trace_parse_common_type(pevent, record->data);
5373 return pevent_find_event(pevent, type);
5377 * pevent_print_event_task - Write the event task comm, pid and CPU
5378 * @pevent: a handle to the pevent
5379 * @s: the trace_seq to write to
5380 * @event: the handle to the record's event
5381 * @record: The record to get the event from
5383 * Writes the tasks comm, pid and CPU to @s.
5385 void pevent_print_event_task(struct pevent *pevent, struct trace_seq *s,
5386 struct event_format *event,
5387 struct pevent_record *record)
5389 void *data = record->data;
5393 pid = parse_common_pid(pevent, data);
5394 comm = find_cmdline(pevent, pid);
5396 if (pevent->latency_format) {
5397 trace_seq_printf(s, "%8.8s-%-5d %3d",
5398 comm, pid, record->cpu);
5400 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
5404 * pevent_print_event_time - Write the event timestamp
5405 * @pevent: a handle to the pevent
5406 * @s: the trace_seq to write to
5407 * @event: the handle to the record's event
5408 * @record: The record to get the event from
5409 * @use_trace_clock: Set to parse according to the @pevent->trace_clock
5411 * Writes the timestamp of the record into @s.
5413 void pevent_print_event_time(struct pevent *pevent, struct trace_seq *s,
5414 struct event_format *event,
5415 struct pevent_record *record,
5416 bool use_trace_clock)
5419 unsigned long usecs;
5420 unsigned long nsecs;
5422 bool use_usec_format;
5424 use_usec_format = is_timestamp_in_us(pevent->trace_clock,
5426 if (use_usec_format) {
5427 secs = record->ts / NSECS_PER_SEC;
5428 nsecs = record->ts - secs * NSECS_PER_SEC;
5431 if (pevent->latency_format) {
5432 pevent_data_lat_fmt(pevent, s, record);
5435 if (use_usec_format) {
5436 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
5440 usecs = (nsecs + 500) / NSECS_PER_USEC;
5441 /* To avoid usecs larger than 1 sec */
5442 if (usecs >= 1000000) {
5449 trace_seq_printf(s, " %5lu.%0*lu:", secs, p, usecs);
5451 trace_seq_printf(s, " %12llu:", record->ts);
5455 * pevent_print_event_data - Write the event data section
5456 * @pevent: a handle to the pevent
5457 * @s: the trace_seq to write to
5458 * @event: the handle to the record's event
5459 * @record: The record to get the event from
5461 * Writes the parsing of the record's data to @s.
5463 void pevent_print_event_data(struct pevent *pevent, struct trace_seq *s,
5464 struct event_format *event,
5465 struct pevent_record *record)
5467 static const char *spaces = " "; /* 20 spaces */
5470 trace_seq_printf(s, " %s: ", event->name);
5472 /* Space out the event names evenly. */
5473 len = strlen(event->name);
5475 trace_seq_printf(s, "%.*s", 20 - len, spaces);
5477 pevent_event_info(s, event, record);
5480 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
5481 struct pevent_record *record, bool use_trace_clock)
5483 struct event_format *event;
5485 event = pevent_find_event_by_record(pevent, record);
5487 do_warning("ug! no event found for type %d",
5488 trace_parse_common_type(pevent, record->data));
5492 pevent_print_event_task(pevent, s, event, record);
5493 pevent_print_event_time(pevent, s, event, record, use_trace_clock);
5494 pevent_print_event_data(pevent, s, event, record);
5497 static int events_id_cmp(const void *a, const void *b)
5499 struct event_format * const * ea = a;
5500 struct event_format * const * eb = b;
5502 if ((*ea)->id < (*eb)->id)
5505 if ((*ea)->id > (*eb)->id)
5511 static int events_name_cmp(const void *a, const void *b)
5513 struct event_format * const * ea = a;
5514 struct event_format * const * eb = b;
5517 res = strcmp((*ea)->name, (*eb)->name);
5521 res = strcmp((*ea)->system, (*eb)->system);
5525 return events_id_cmp(a, b);
5528 static int events_system_cmp(const void *a, const void *b)
5530 struct event_format * const * ea = a;
5531 struct event_format * const * eb = b;
5534 res = strcmp((*ea)->system, (*eb)->system);
5538 res = strcmp((*ea)->name, (*eb)->name);
5542 return events_id_cmp(a, b);
5545 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
5547 struct event_format **events;
5548 int (*sort)(const void *a, const void *b);
5550 events = pevent->sort_events;
5552 if (events && pevent->last_type == sort_type)
5556 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
5560 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
5561 events[pevent->nr_events] = NULL;
5563 pevent->sort_events = events;
5565 /* the internal events are sorted by id */
5566 if (sort_type == EVENT_SORT_ID) {
5567 pevent->last_type = sort_type;
5572 switch (sort_type) {
5574 sort = events_id_cmp;
5576 case EVENT_SORT_NAME:
5577 sort = events_name_cmp;
5579 case EVENT_SORT_SYSTEM:
5580 sort = events_system_cmp;
5586 qsort(events, pevent->nr_events, sizeof(*events), sort);
5587 pevent->last_type = sort_type;
5592 static struct format_field **
5593 get_event_fields(const char *type, const char *name,
5594 int count, struct format_field *list)
5596 struct format_field **fields;
5597 struct format_field *field;
5600 fields = malloc(sizeof(*fields) * (count + 1));
5604 for (field = list; field; field = field->next) {
5605 fields[i++] = field;
5606 if (i == count + 1) {
5607 do_warning("event %s has more %s fields than specified",
5615 do_warning("event %s has less %s fields than specified",
5624 * pevent_event_common_fields - return a list of common fields for an event
5625 * @event: the event to return the common fields of.
5627 * Returns an allocated array of fields. The last item in the array is NULL.
5628 * The array must be freed with free().
5630 struct format_field **pevent_event_common_fields(struct event_format *event)
5632 return get_event_fields("common", event->name,
5633 event->format.nr_common,
5634 event->format.common_fields);
5638 * pevent_event_fields - return a list of event specific fields for an event
5639 * @event: the event to return the fields of.
5641 * Returns an allocated array of fields. The last item in the array is NULL.
5642 * The array must be freed with free().
5644 struct format_field **pevent_event_fields(struct event_format *event)
5646 return get_event_fields("event", event->name,
5647 event->format.nr_fields,
5648 event->format.fields);
5651 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
5653 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
5655 trace_seq_puts(s, ", ");
5656 print_fields(s, field->next);
5661 static void print_args(struct print_arg *args)
5663 int print_paren = 1;
5666 switch (args->type) {
5671 printf("%s", args->atom.atom);
5674 printf("REC->%s", args->field.name);
5677 printf("__print_flags(");
5678 print_args(args->flags.field);
5679 printf(", %s, ", args->flags.delim);
5681 print_fields(&s, args->flags.flags);
5682 trace_seq_do_printf(&s);
5683 trace_seq_destroy(&s);
5687 printf("__print_symbolic(");
5688 print_args(args->symbol.field);
5691 print_fields(&s, args->symbol.symbols);
5692 trace_seq_do_printf(&s);
5693 trace_seq_destroy(&s);
5697 printf("__print_hex(");
5698 print_args(args->hex.field);
5700 print_args(args->hex.size);
5703 case PRINT_INT_ARRAY:
5704 printf("__print_array(");
5705 print_args(args->int_array.field);
5707 print_args(args->int_array.count);
5709 print_args(args->int_array.el_size);
5714 printf("__get_str(%s)", args->string.string);
5717 printf("__get_bitmask(%s)", args->bitmask.bitmask);
5720 printf("(%s)", args->typecast.type);
5721 print_args(args->typecast.item);
5724 if (strcmp(args->op.op, ":") == 0)
5728 print_args(args->op.left);
5729 printf(" %s ", args->op.op);
5730 print_args(args->op.right);
5735 /* we should warn... */
5740 print_args(args->next);
5744 static void parse_header_field(const char *field,
5745 int *offset, int *size, int mandatory)
5747 unsigned long long save_input_buf_ptr;
5748 unsigned long long save_input_buf_siz;
5752 save_input_buf_ptr = input_buf_ptr;
5753 save_input_buf_siz = input_buf_siz;
5755 if (read_expected(EVENT_ITEM, "field") < 0)
5757 if (read_expected(EVENT_OP, ":") < 0)
5761 if (read_expect_type(EVENT_ITEM, &token) < 0)
5766 * If this is not a mandatory field, then test it first.
5769 if (read_expected(EVENT_ITEM, field) < 0)
5772 if (read_expect_type(EVENT_ITEM, &token) < 0)
5774 if (strcmp(token, field) != 0)
5779 if (read_expected(EVENT_OP, ";") < 0)
5781 if (read_expected(EVENT_ITEM, "offset") < 0)
5783 if (read_expected(EVENT_OP, ":") < 0)
5785 if (read_expect_type(EVENT_ITEM, &token) < 0)
5787 *offset = atoi(token);
5789 if (read_expected(EVENT_OP, ";") < 0)
5791 if (read_expected(EVENT_ITEM, "size") < 0)
5793 if (read_expected(EVENT_OP, ":") < 0)
5795 if (read_expect_type(EVENT_ITEM, &token) < 0)
5797 *size = atoi(token);
5799 if (read_expected(EVENT_OP, ";") < 0)
5801 type = read_token(&token);
5802 if (type != EVENT_NEWLINE) {
5803 /* newer versions of the kernel have a "signed" type */
5804 if (type != EVENT_ITEM)
5807 if (strcmp(token, "signed") != 0)
5812 if (read_expected(EVENT_OP, ":") < 0)
5815 if (read_expect_type(EVENT_ITEM, &token))
5819 if (read_expected(EVENT_OP, ";") < 0)
5822 if (read_expect_type(EVENT_NEWLINE, &token))
5830 input_buf_ptr = save_input_buf_ptr;
5831 input_buf_siz = save_input_buf_siz;
5838 * pevent_parse_header_page - parse the data stored in the header page
5839 * @pevent: the handle to the pevent
5840 * @buf: the buffer storing the header page format string
5841 * @size: the size of @buf
5842 * @long_size: the long size to use if there is no header
5844 * This parses the header page format for information on the
5845 * ring buffer used. The @buf should be copied from
5847 * /sys/kernel/debug/tracing/events/header_page
5849 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
5856 * Old kernels did not have header page info.
5857 * Sorry but we just use what we find here in user space.
5859 pevent->header_page_ts_size = sizeof(long long);
5860 pevent->header_page_size_size = long_size;
5861 pevent->header_page_data_offset = sizeof(long long) + long_size;
5862 pevent->old_format = 1;
5865 init_input_buf(buf, size);
5867 parse_header_field("timestamp", &pevent->header_page_ts_offset,
5868 &pevent->header_page_ts_size, 1);
5869 parse_header_field("commit", &pevent->header_page_size_offset,
5870 &pevent->header_page_size_size, 1);
5871 parse_header_field("overwrite", &pevent->header_page_overwrite,
5873 parse_header_field("data", &pevent->header_page_data_offset,
5874 &pevent->header_page_data_size, 1);
5879 static int event_matches(struct event_format *event,
5880 int id, const char *sys_name,
5881 const char *event_name)
5883 if (id >= 0 && id != event->id)
5886 if (event_name && (strcmp(event_name, event->name) != 0))
5889 if (sys_name && (strcmp(sys_name, event->system) != 0))
5895 static void free_handler(struct event_handler *handle)
5897 free((void *)handle->sys_name);
5898 free((void *)handle->event_name);
5902 static int find_event_handle(struct pevent *pevent, struct event_format *event)
5904 struct event_handler *handle, **next;
5906 for (next = &pevent->handlers; *next;
5907 next = &(*next)->next) {
5909 if (event_matches(event, handle->id,
5911 handle->event_name))
5918 pr_stat("overriding event (%d) %s:%s with new print handler",
5919 event->id, event->system, event->name);
5921 event->handler = handle->func;
5922 event->context = handle->context;
5924 *next = handle->next;
5925 free_handler(handle);
5931 * __pevent_parse_format - parse the event format
5932 * @buf: the buffer storing the event format string
5933 * @size: the size of @buf
5934 * @sys: the system the event belongs to
5936 * This parses the event format and creates an event structure
5937 * to quickly parse raw data for a given event.
5939 * These files currently come from:
5941 * /sys/kernel/debug/tracing/events/.../.../format
5943 enum pevent_errno __pevent_parse_format(struct event_format **eventp,
5944 struct pevent *pevent, const char *buf,
5945 unsigned long size, const char *sys)
5947 struct event_format *event;
5950 init_input_buf(buf, size);
5952 *eventp = event = alloc_event();
5954 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
5956 event->name = event_read_name();
5959 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5960 goto event_alloc_failed;
5963 if (strcmp(sys, "ftrace") == 0) {
5964 event->flags |= EVENT_FL_ISFTRACE;
5966 if (strcmp(event->name, "bprint") == 0)
5967 event->flags |= EVENT_FL_ISBPRINT;
5970 event->id = event_read_id();
5971 if (event->id < 0) {
5972 ret = PEVENT_ERRNO__READ_ID_FAILED;
5974 * This isn't an allocation error actually.
5975 * But as the ID is critical, just bail out.
5977 goto event_alloc_failed;
5980 event->system = strdup(sys);
5981 if (!event->system) {
5982 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
5983 goto event_alloc_failed;
5986 /* Add pevent to event so that it can be referenced */
5987 event->pevent = pevent;
5989 ret = event_read_format(event);
5991 ret = PEVENT_ERRNO__READ_FORMAT_FAILED;
5992 goto event_parse_failed;
5996 * If the event has an override, don't print warnings if the event
5997 * print format fails to parse.
5999 if (pevent && find_event_handle(pevent, event))
6002 ret = event_read_print(event);
6006 ret = PEVENT_ERRNO__READ_PRINT_FAILED;
6007 goto event_parse_failed;
6010 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
6011 struct format_field *field;
6012 struct print_arg *arg, **list;
6014 /* old ftrace had no args */
6015 list = &event->print_fmt.args;
6016 for (field = event->format.fields; field; field = field->next) {
6019 event->flags |= EVENT_FL_FAILED;
6020 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6022 arg->type = PRINT_FIELD;
6023 arg->field.name = strdup(field->name);
6024 if (!arg->field.name) {
6025 event->flags |= EVENT_FL_FAILED;
6027 return PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED;
6029 arg->field.field = field;
6039 event->flags |= EVENT_FL_FAILED;
6043 free(event->system);
6050 static enum pevent_errno
6051 __pevent_parse_event(struct pevent *pevent,
6052 struct event_format **eventp,
6053 const char *buf, unsigned long size,
6056 int ret = __pevent_parse_format(eventp, pevent, buf, size, sys);
6057 struct event_format *event = *eventp;
6062 if (pevent && add_event(pevent, event)) {
6063 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6064 goto event_add_failed;
6067 #define PRINT_ARGS 0
6068 if (PRINT_ARGS && event->print_fmt.args)
6069 print_args(event->print_fmt.args);
6074 pevent_free_format(event);
6079 * pevent_parse_format - parse the event format
6080 * @pevent: the handle to the pevent
6081 * @eventp: returned format
6082 * @buf: the buffer storing the event format string
6083 * @size: the size of @buf
6084 * @sys: the system the event belongs to
6086 * This parses the event format and creates an event structure
6087 * to quickly parse raw data for a given event.
6089 * These files currently come from:
6091 * /sys/kernel/debug/tracing/events/.../.../format
6093 enum pevent_errno pevent_parse_format(struct pevent *pevent,
6094 struct event_format **eventp,
6096 unsigned long size, const char *sys)
6098 return __pevent_parse_event(pevent, eventp, buf, size, sys);
6102 * pevent_parse_event - parse the event format
6103 * @pevent: the handle to the pevent
6104 * @buf: the buffer storing the event format string
6105 * @size: the size of @buf
6106 * @sys: the system the event belongs to
6108 * This parses the event format and creates an event structure
6109 * to quickly parse raw data for a given event.
6111 * These files currently come from:
6113 * /sys/kernel/debug/tracing/events/.../.../format
6115 enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,
6116 unsigned long size, const char *sys)
6118 struct event_format *event = NULL;
6119 return __pevent_parse_event(pevent, &event, buf, size, sys);
6123 #define _PE(code, str) str
6124 static const char * const pevent_error_str[] = {
6129 int pevent_strerror(struct pevent *pevent __maybe_unused,
6130 enum pevent_errno errnum, char *buf, size_t buflen)
6136 str_error_r(errnum, buf, buflen);
6140 if (errnum <= __PEVENT_ERRNO__START ||
6141 errnum >= __PEVENT_ERRNO__END)
6144 idx = errnum - __PEVENT_ERRNO__START - 1;
6145 msg = pevent_error_str[idx];
6146 snprintf(buf, buflen, "%s", msg);
6151 int get_field_val(struct trace_seq *s, struct format_field *field,
6152 const char *name, struct pevent_record *record,
6153 unsigned long long *val, int err)
6157 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6161 if (pevent_read_number_field(field, record->data, val)) {
6163 trace_seq_printf(s, " %s=INVALID", name);
6171 * pevent_get_field_raw - return the raw pointer into the data field
6172 * @s: The seq to print to on error
6173 * @event: the event that the field is for
6174 * @name: The name of the field
6175 * @record: The record with the field name.
6176 * @len: place to store the field length.
6177 * @err: print default error if failed.
6179 * Returns a pointer into record->data of the field and places
6180 * the length of the field in @len.
6182 * On failure, it returns NULL.
6184 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
6185 const char *name, struct pevent_record *record,
6188 struct format_field *field;
6189 void *data = record->data;
6196 field = pevent_find_field(event, name);
6200 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
6204 /* Allow @len to be NULL */
6208 offset = field->offset;
6209 if (field->flags & FIELD_IS_DYNAMIC) {
6210 offset = pevent_read_number(event->pevent,
6211 data + offset, field->size);
6212 *len = offset >> 16;
6217 return data + offset;
6221 * pevent_get_field_val - find a field and return its value
6222 * @s: The seq to print to on error
6223 * @event: the event that the field is for
6224 * @name: The name of the field
6225 * @record: The record with the field name.
6226 * @val: place to store the value of the field.
6227 * @err: print default error if failed.
6229 * Returns 0 on success -1 on field not found.
6231 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
6232 const char *name, struct pevent_record *record,
6233 unsigned long long *val, int err)
6235 struct format_field *field;
6240 field = pevent_find_field(event, name);
6242 return get_field_val(s, field, name, record, val, err);
6246 * pevent_get_common_field_val - find a common field and return its value
6247 * @s: The seq to print to on error
6248 * @event: the event that the field is for
6249 * @name: The name of the field
6250 * @record: The record with the field name.
6251 * @val: place to store the value of the field.
6252 * @err: print default error if failed.
6254 * Returns 0 on success -1 on field not found.
6256 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
6257 const char *name, struct pevent_record *record,
6258 unsigned long long *val, int err)
6260 struct format_field *field;
6265 field = pevent_find_common_field(event, name);
6267 return get_field_val(s, field, name, record, val, err);
6271 * pevent_get_any_field_val - find a any field and return its value
6272 * @s: The seq to print to on error
6273 * @event: the event that the field is for
6274 * @name: The name of the field
6275 * @record: The record with the field name.
6276 * @val: place to store the value of the field.
6277 * @err: print default error if failed.
6279 * Returns 0 on success -1 on field not found.
6281 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
6282 const char *name, struct pevent_record *record,
6283 unsigned long long *val, int err)
6285 struct format_field *field;
6290 field = pevent_find_any_field(event, name);
6292 return get_field_val(s, field, name, record, val, err);
6296 * pevent_print_num_field - print a field and a format
6297 * @s: The seq to print to
6298 * @fmt: The printf format to print the field with.
6299 * @event: the event that the field is for
6300 * @name: The name of the field
6301 * @record: The record with the field name.
6302 * @err: print default error if failed.
6304 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6306 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
6307 struct event_format *event, const char *name,
6308 struct pevent_record *record, int err)
6310 struct format_field *field = pevent_find_field(event, name);
6311 unsigned long long val;
6316 if (pevent_read_number_field(field, record->data, &val))
6319 return trace_seq_printf(s, fmt, val);
6323 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6328 * pevent_print_func_field - print a field and a format for function pointers
6329 * @s: The seq to print to
6330 * @fmt: The printf format to print the field with.
6331 * @event: the event that the field is for
6332 * @name: The name of the field
6333 * @record: The record with the field name.
6334 * @err: print default error if failed.
6336 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
6338 int pevent_print_func_field(struct trace_seq *s, const char *fmt,
6339 struct event_format *event, const char *name,
6340 struct pevent_record *record, int err)
6342 struct format_field *field = pevent_find_field(event, name);
6343 struct pevent *pevent = event->pevent;
6344 unsigned long long val;
6345 struct func_map *func;
6351 if (pevent_read_number_field(field, record->data, &val))
6354 func = find_func(pevent, val);
6357 snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val);
6359 sprintf(tmp, "0x%08llx", val);
6361 return trace_seq_printf(s, fmt, tmp);
6365 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
6369 static void free_func_handle(struct pevent_function_handler *func)
6371 struct pevent_func_params *params;
6375 while (func->params) {
6376 params = func->params;
6377 func->params = params->next;
6385 * pevent_register_print_function - register a helper function
6386 * @pevent: the handle to the pevent
6387 * @func: the function to process the helper function
6388 * @ret_type: the return type of the helper function
6389 * @name: the name of the helper function
6390 * @parameters: A list of enum pevent_func_arg_type
6392 * Some events may have helper functions in the print format arguments.
6393 * This allows a plugin to dynamically create a way to process one
6394 * of these functions.
6396 * The @parameters is a variable list of pevent_func_arg_type enums that
6397 * must end with PEVENT_FUNC_ARG_VOID.
6399 int pevent_register_print_function(struct pevent *pevent,
6400 pevent_func_handler func,
6401 enum pevent_func_arg_type ret_type,
6404 struct pevent_function_handler *func_handle;
6405 struct pevent_func_params **next_param;
6406 struct pevent_func_params *param;
6407 enum pevent_func_arg_type type;
6411 func_handle = find_func_handler(pevent, name);
6414 * This is most like caused by the users own
6415 * plugins updating the function. This overrides the
6418 pr_stat("override of function helper '%s'", name);
6419 remove_func_handler(pevent, name);
6422 func_handle = calloc(1, sizeof(*func_handle));
6424 do_warning("Failed to allocate function handler");
6425 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6428 func_handle->ret_type = ret_type;
6429 func_handle->name = strdup(name);
6430 func_handle->func = func;
6431 if (!func_handle->name) {
6432 do_warning("Failed to allocate function name");
6434 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6437 next_param = &(func_handle->params);
6440 type = va_arg(ap, enum pevent_func_arg_type);
6441 if (type == PEVENT_FUNC_ARG_VOID)
6444 if (type >= PEVENT_FUNC_ARG_MAX_TYPES) {
6445 do_warning("Invalid argument type %d", type);
6446 ret = PEVENT_ERRNO__INVALID_ARG_TYPE;
6450 param = malloc(sizeof(*param));
6452 do_warning("Failed to allocate function param");
6453 ret = PEVENT_ERRNO__MEM_ALLOC_FAILED;
6459 *next_param = param;
6460 next_param = &(param->next);
6462 func_handle->nr_args++;
6466 func_handle->next = pevent->func_handlers;
6467 pevent->func_handlers = func_handle;
6472 free_func_handle(func_handle);
6477 * pevent_unregister_print_function - unregister a helper function
6478 * @pevent: the handle to the pevent
6479 * @func: the function to process the helper function
6480 * @name: the name of the helper function
6482 * This function removes existing print handler for function @name.
6484 * Returns 0 if the handler was removed successully, -1 otherwise.
6486 int pevent_unregister_print_function(struct pevent *pevent,
6487 pevent_func_handler func, char *name)
6489 struct pevent_function_handler *func_handle;
6491 func_handle = find_func_handler(pevent, name);
6492 if (func_handle && func_handle->func == func) {
6493 remove_func_handler(pevent, name);
6499 static struct event_format *pevent_search_event(struct pevent *pevent, int id,
6500 const char *sys_name,
6501 const char *event_name)
6503 struct event_format *event;
6507 event = pevent_find_event(pevent, id);
6510 if (event_name && (strcmp(event_name, event->name) != 0))
6512 if (sys_name && (strcmp(sys_name, event->system) != 0))
6515 event = pevent_find_event_by_name(pevent, sys_name, event_name);
6523 * pevent_register_event_handler - register a way to parse an event
6524 * @pevent: the handle to the pevent
6525 * @id: the id of the event to register
6526 * @sys_name: the system name the event belongs to
6527 * @event_name: the name of the event
6528 * @func: the function to call to parse the event information
6529 * @context: the data to be passed to @func
6531 * This function allows a developer to override the parsing of
6532 * a given event. If for some reason the default print format
6533 * is not sufficient, this function will register a function
6534 * for an event to be used to parse the data instead.
6536 * If @id is >= 0, then it is used to find the event.
6537 * else @sys_name and @event_name are used.
6539 int pevent_register_event_handler(struct pevent *pevent, int id,
6540 const char *sys_name, const char *event_name,
6541 pevent_event_handler_func func, void *context)
6543 struct event_format *event;
6544 struct event_handler *handle;
6546 event = pevent_search_event(pevent, id, sys_name, event_name);
6550 pr_stat("overriding event (%d) %s:%s with new print handler",
6551 event->id, event->system, event->name);
6553 event->handler = func;
6554 event->context = context;
6558 /* Save for later use. */
6559 handle = calloc(1, sizeof(*handle));
6561 do_warning("Failed to allocate event handler");
6562 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6567 handle->event_name = strdup(event_name);
6569 handle->sys_name = strdup(sys_name);
6571 if ((event_name && !handle->event_name) ||
6572 (sys_name && !handle->sys_name)) {
6573 do_warning("Failed to allocate event/sys name");
6574 free((void *)handle->event_name);
6575 free((void *)handle->sys_name);
6577 return PEVENT_ERRNO__MEM_ALLOC_FAILED;
6580 handle->func = func;
6581 handle->next = pevent->handlers;
6582 pevent->handlers = handle;
6583 handle->context = context;
6588 static int handle_matches(struct event_handler *handler, int id,
6589 const char *sys_name, const char *event_name,
6590 pevent_event_handler_func func, void *context)
6592 if (id >= 0 && id != handler->id)
6595 if (event_name && (strcmp(event_name, handler->event_name) != 0))
6598 if (sys_name && (strcmp(sys_name, handler->sys_name) != 0))
6601 if (func != handler->func || context != handler->context)
6608 * pevent_unregister_event_handler - unregister an existing event handler
6609 * @pevent: the handle to the pevent
6610 * @id: the id of the event to unregister
6611 * @sys_name: the system name the handler belongs to
6612 * @event_name: the name of the event handler
6613 * @func: the function to call to parse the event information
6614 * @context: the data to be passed to @func
6616 * This function removes existing event handler (parser).
6618 * If @id is >= 0, then it is used to find the event.
6619 * else @sys_name and @event_name are used.
6621 * Returns 0 if handler was removed successfully, -1 if event was not found.
6623 int pevent_unregister_event_handler(struct pevent *pevent, int id,
6624 const char *sys_name, const char *event_name,
6625 pevent_event_handler_func func, void *context)
6627 struct event_format *event;
6628 struct event_handler *handle;
6629 struct event_handler **next;
6631 event = pevent_search_event(pevent, id, sys_name, event_name);
6635 if (event->handler == func && event->context == context) {
6636 pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.",
6637 event->id, event->system, event->name);
6639 event->handler = NULL;
6640 event->context = NULL;
6645 for (next = &pevent->handlers; *next; next = &(*next)->next) {
6647 if (handle_matches(handle, id, sys_name, event_name,
6655 *next = handle->next;
6656 free_handler(handle);
6662 * pevent_alloc - create a pevent handle
6664 struct pevent *pevent_alloc(void)
6666 struct pevent *pevent = calloc(1, sizeof(*pevent));
6669 pevent->ref_count = 1;
6674 void pevent_ref(struct pevent *pevent)
6676 pevent->ref_count++;
6679 void pevent_free_format_field(struct format_field *field)
6682 if (field->alias != field->name)
6688 static void free_format_fields(struct format_field *field)
6690 struct format_field *next;
6694 pevent_free_format_field(field);
6699 static void free_formats(struct format *format)
6701 free_format_fields(format->common_fields);
6702 free_format_fields(format->fields);
6705 void pevent_free_format(struct event_format *event)
6708 free(event->system);
6710 free_formats(&event->format);
6712 free(event->print_fmt.format);
6713 free_args(event->print_fmt.args);
6719 * pevent_free - free a pevent handle
6720 * @pevent: the pevent handle to free
6722 void pevent_free(struct pevent *pevent)
6724 struct cmdline_list *cmdlist, *cmdnext;
6725 struct func_list *funclist, *funcnext;
6726 struct printk_list *printklist, *printknext;
6727 struct pevent_function_handler *func_handler;
6728 struct event_handler *handle;
6734 cmdlist = pevent->cmdlist;
6735 funclist = pevent->funclist;
6736 printklist = pevent->printklist;
6738 pevent->ref_count--;
6739 if (pevent->ref_count)
6742 if (pevent->cmdlines) {
6743 for (i = 0; i < pevent->cmdline_count; i++)
6744 free(pevent->cmdlines[i].comm);
6745 free(pevent->cmdlines);
6749 cmdnext = cmdlist->next;
6750 free(cmdlist->comm);
6755 if (pevent->func_map) {
6756 for (i = 0; i < (int)pevent->func_count; i++) {
6757 free(pevent->func_map[i].func);
6758 free(pevent->func_map[i].mod);
6760 free(pevent->func_map);
6764 funcnext = funclist->next;
6765 free(funclist->func);
6766 free(funclist->mod);
6768 funclist = funcnext;
6771 while (pevent->func_handlers) {
6772 func_handler = pevent->func_handlers;
6773 pevent->func_handlers = func_handler->next;
6774 free_func_handle(func_handler);
6777 if (pevent->printk_map) {
6778 for (i = 0; i < (int)pevent->printk_count; i++)
6779 free(pevent->printk_map[i].printk);
6780 free(pevent->printk_map);
6783 while (printklist) {
6784 printknext = printklist->next;
6785 free(printklist->printk);
6787 printklist = printknext;
6790 for (i = 0; i < pevent->nr_events; i++)
6791 pevent_free_format(pevent->events[i]);
6793 while (pevent->handlers) {
6794 handle = pevent->handlers;
6795 pevent->handlers = handle->next;
6796 free_handler(handle);
6799 free(pevent->trace_clock);
6800 free(pevent->events);
6801 free(pevent->sort_events);
6802 free(pevent->func_resolver);
6807 void pevent_unref(struct pevent *pevent)
6809 pevent_free(pevent);