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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * The parts for function graph printing was taken and modified from the
22 * Linux Kernel that were written by
23 * - Copyright (C) 2009 Frederic Weisbecker,
24 * Frederic Weisbecker gave his permission to relicense the code to
25 * the Lesser General Public License.
35 #include "event-parse.h"
36 #include "event-utils.h"
38 static const char *input_buf;
39 static unsigned long long input_buf_ptr;
40 static unsigned long long input_buf_siz;
42 static int is_flag_field;
43 static int is_symbolic_field;
45 static int show_warning = 1;
47 #define do_warning(fmt, ...) \
50 warning(fmt, ##__VA_ARGS__); \
53 static void init_input_buf(const char *buf, unsigned long long size)
60 const char *pevent_get_input_buf(void)
65 unsigned long long pevent_get_input_buf_ptr(void)
70 struct event_handler {
71 struct event_handler *next;
74 const char *event_name;
75 pevent_event_handler_func func;
79 struct pevent_func_params {
80 struct pevent_func_params *next;
81 enum pevent_func_arg_type type;
84 struct pevent_function_handler {
85 struct pevent_function_handler *next;
86 enum pevent_func_arg_type ret_type;
88 pevent_func_handler func;
89 struct pevent_func_params *params;
93 static unsigned long long
94 process_defined_func(struct trace_seq *s, void *data, int size,
95 struct event_format *event, struct print_arg *arg);
97 static void free_func_handle(struct pevent_function_handler *func);
100 * pevent_buffer_init - init buffer for parsing
101 * @buf: buffer to parse
102 * @size: the size of the buffer
104 * For use with pevent_read_token(), this initializes the internal
105 * buffer that pevent_read_token() will parse.
107 void pevent_buffer_init(const char *buf, unsigned long long size)
109 init_input_buf(buf, size);
112 void breakpoint(void)
118 struct print_arg *alloc_arg(void)
120 struct print_arg *arg;
122 arg = malloc_or_die(sizeof(*arg));
125 memset(arg, 0, sizeof(*arg));
135 static int cmdline_cmp(const void *a, const void *b)
137 const struct cmdline *ca = a;
138 const struct cmdline *cb = b;
140 if (ca->pid < cb->pid)
142 if (ca->pid > cb->pid)
148 struct cmdline_list {
149 struct cmdline_list *next;
154 static int cmdline_init(struct pevent *pevent)
156 struct cmdline_list *cmdlist = pevent->cmdlist;
157 struct cmdline_list *item;
158 struct cmdline *cmdlines;
161 cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count);
165 cmdlines[i].pid = cmdlist->pid;
166 cmdlines[i].comm = cmdlist->comm;
169 cmdlist = cmdlist->next;
173 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
175 pevent->cmdlines = cmdlines;
176 pevent->cmdlist = NULL;
181 static char *find_cmdline(struct pevent *pevent, int pid)
183 const struct cmdline *comm;
189 if (!pevent->cmdlines)
190 cmdline_init(pevent);
194 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
195 sizeof(*pevent->cmdlines), cmdline_cmp);
203 * pevent_pid_is_registered - return if a pid has a cmdline registered
204 * @pevent: handle for the pevent
205 * @pid: The pid to check if it has a cmdline registered with.
207 * Returns 1 if the pid has a cmdline mapped to it
210 int pevent_pid_is_registered(struct pevent *pevent, int pid)
212 const struct cmdline *comm;
218 if (!pevent->cmdlines)
219 cmdline_init(pevent);
223 comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
224 sizeof(*pevent->cmdlines), cmdline_cmp);
232 * If the command lines have been converted to an array, then
233 * we must add this pid. This is much slower than when cmdlines
234 * are added before the array is initialized.
236 static int add_new_comm(struct pevent *pevent, const char *comm, int pid)
238 struct cmdline *cmdlines = pevent->cmdlines;
239 const struct cmdline *cmdline;
245 /* avoid duplicates */
248 cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count,
249 sizeof(*pevent->cmdlines), cmdline_cmp);
255 cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1));
261 cmdlines[pevent->cmdline_count].pid = pid;
262 cmdlines[pevent->cmdline_count].comm = strdup(comm);
263 if (!cmdlines[pevent->cmdline_count].comm)
266 if (cmdlines[pevent->cmdline_count].comm)
267 pevent->cmdline_count++;
269 qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp);
270 pevent->cmdlines = cmdlines;
276 * pevent_register_comm - register a pid / comm mapping
277 * @pevent: handle for the pevent
278 * @comm: the command line to register
279 * @pid: the pid to map the command line to
281 * This adds a mapping to search for command line names with
282 * a given pid. The comm is duplicated.
284 int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)
286 struct cmdline_list *item;
288 if (pevent->cmdlines)
289 return add_new_comm(pevent, comm, pid);
291 item = malloc_or_die(sizeof(*item));
292 item->comm = strdup(comm);
296 item->next = pevent->cmdlist;
298 pevent->cmdlist = item;
299 pevent->cmdline_count++;
305 unsigned long long addr;
311 struct func_list *next;
312 unsigned long long addr;
317 static int func_cmp(const void *a, const void *b)
319 const struct func_map *fa = a;
320 const struct func_map *fb = b;
322 if (fa->addr < fb->addr)
324 if (fa->addr > fb->addr)
331 * We are searching for a record in between, not an exact
334 static int func_bcmp(const void *a, const void *b)
336 const struct func_map *fa = a;
337 const struct func_map *fb = b;
339 if ((fa->addr == fb->addr) ||
341 (fa->addr > fb->addr &&
342 fa->addr < (fb+1)->addr))
345 if (fa->addr < fb->addr)
351 static int func_map_init(struct pevent *pevent)
353 struct func_list *funclist;
354 struct func_list *item;
355 struct func_map *func_map;
358 func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1));
359 funclist = pevent->funclist;
363 func_map[i].func = funclist->func;
364 func_map[i].addr = funclist->addr;
365 func_map[i].mod = funclist->mod;
368 funclist = funclist->next;
372 qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp);
375 * Add a special record at the end.
377 func_map[pevent->func_count].func = NULL;
378 func_map[pevent->func_count].addr = 0;
379 func_map[pevent->func_count].mod = NULL;
381 pevent->func_map = func_map;
382 pevent->funclist = NULL;
387 static struct func_map *
388 find_func(struct pevent *pevent, unsigned long long addr)
390 struct func_map *func;
393 if (!pevent->func_map)
394 func_map_init(pevent);
398 func = bsearch(&key, pevent->func_map, pevent->func_count,
399 sizeof(*pevent->func_map), func_bcmp);
405 * pevent_find_function - find a function by a given address
406 * @pevent: handle for the pevent
407 * @addr: the address to find the function with
409 * Returns a pointer to the function stored that has the given
410 * address. Note, the address does not have to be exact, it
411 * will select the function that would contain the address.
413 const char *pevent_find_function(struct pevent *pevent, unsigned long long addr)
415 struct func_map *map;
417 map = find_func(pevent, addr);
425 * pevent_find_function_address - find a function address by a given address
426 * @pevent: handle for the pevent
427 * @addr: the address to find the function with
429 * Returns the address the function starts at. This can be used in
430 * conjunction with pevent_find_function to print both the function
431 * name and the function offset.
434 pevent_find_function_address(struct pevent *pevent, unsigned long long addr)
436 struct func_map *map;
438 map = find_func(pevent, addr);
446 * pevent_register_function - register a function with a given address
447 * @pevent: handle for the pevent
448 * @function: the function name to register
449 * @addr: the address the function starts at
450 * @mod: the kernel module the function may be in (NULL for none)
452 * This registers a function name with an address and module.
453 * The @func passed in is duplicated.
455 int pevent_register_function(struct pevent *pevent, char *func,
456 unsigned long long addr, char *mod)
458 struct func_list *item;
460 item = malloc_or_die(sizeof(*item));
462 item->next = pevent->funclist;
463 item->func = strdup(func);
465 item->mod = strdup(mod);
470 pevent->funclist = item;
472 pevent->func_count++;
478 * pevent_print_funcs - print out the stored functions
479 * @pevent: handle for the pevent
481 * This prints out the stored functions.
483 void pevent_print_funcs(struct pevent *pevent)
487 if (!pevent->func_map)
488 func_map_init(pevent);
490 for (i = 0; i < (int)pevent->func_count; i++) {
492 pevent->func_map[i].addr,
493 pevent->func_map[i].func);
494 if (pevent->func_map[i].mod)
495 printf(" [%s]\n", pevent->func_map[i].mod);
502 unsigned long long addr;
507 struct printk_list *next;
508 unsigned long long addr;
512 static int printk_cmp(const void *a, const void *b)
514 const struct func_map *fa = a;
515 const struct func_map *fb = b;
517 if (fa->addr < fb->addr)
519 if (fa->addr > fb->addr)
525 static void printk_map_init(struct pevent *pevent)
527 struct printk_list *printklist;
528 struct printk_list *item;
529 struct printk_map *printk_map;
532 printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
534 printklist = pevent->printklist;
538 printk_map[i].printk = printklist->printk;
539 printk_map[i].addr = printklist->addr;
542 printklist = printklist->next;
546 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
548 pevent->printk_map = printk_map;
549 pevent->printklist = NULL;
552 static struct printk_map *
553 find_printk(struct pevent *pevent, unsigned long long addr)
555 struct printk_map *printk;
556 struct printk_map key;
558 if (!pevent->printk_map)
559 printk_map_init(pevent);
563 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
564 sizeof(*pevent->printk_map), printk_cmp);
570 * pevent_register_print_string - register a string by its address
571 * @pevent: handle for the pevent
572 * @fmt: the string format to register
573 * @addr: the address the string was located at
575 * This registers a string by the address it was stored in the kernel.
576 * The @fmt passed in is duplicated.
578 int pevent_register_print_string(struct pevent *pevent, char *fmt,
579 unsigned long long addr)
581 struct printk_list *item;
583 item = malloc_or_die(sizeof(*item));
585 item->next = pevent->printklist;
586 pevent->printklist = item;
587 item->printk = strdup(fmt);
590 pevent->printk_count++;
596 * pevent_print_printk - print out the stored strings
597 * @pevent: handle for the pevent
599 * This prints the string formats that were stored.
601 void pevent_print_printk(struct pevent *pevent)
605 if (!pevent->printk_map)
606 printk_map_init(pevent);
608 for (i = 0; i < (int)pevent->printk_count; i++) {
609 printf("%016llx %s\n",
610 pevent->printk_map[i].addr,
611 pevent->printk_map[i].printk);
615 static struct event_format *alloc_event(void)
617 struct event_format *event;
619 event = malloc(sizeof(*event));
622 memset(event, 0, sizeof(*event));
627 static void add_event(struct pevent *pevent, struct event_format *event)
632 pevent->events = malloc_or_die(sizeof(event));
635 realloc(pevent->events, sizeof(event) *
636 (pevent->nr_events + 1));
638 die("Can not allocate events");
640 for (i = 0; i < pevent->nr_events; i++) {
641 if (pevent->events[i]->id > event->id)
644 if (i < pevent->nr_events)
645 memmove(&pevent->events[i + 1],
647 sizeof(event) * (pevent->nr_events - i));
649 pevent->events[i] = event;
652 event->pevent = pevent;
655 static int event_item_type(enum event_type type)
658 case EVENT_ITEM ... EVENT_SQUOTE:
660 case EVENT_ERROR ... EVENT_DELIM:
666 static void free_flag_sym(struct print_flag_sym *fsym)
668 struct print_flag_sym *next;
679 static void free_arg(struct print_arg *arg)
681 struct print_arg *farg;
688 free(arg->atom.atom);
691 free(arg->field.name);
694 free_arg(arg->flags.field);
695 free(arg->flags.delim);
696 free_flag_sym(arg->flags.flags);
699 free_arg(arg->symbol.field);
700 free_flag_sym(arg->symbol.symbols);
703 free_arg(arg->hex.field);
704 free_arg(arg->hex.size);
707 free(arg->typecast.type);
708 free_arg(arg->typecast.item);
712 free(arg->string.string);
714 case PRINT_DYNAMIC_ARRAY:
715 free(arg->dynarray.index);
719 free_arg(arg->op.left);
720 free_arg(arg->op.right);
723 while (arg->func.args) {
724 farg = arg->func.args;
725 arg->func.args = farg->next;
738 static enum event_type get_type(int ch)
741 return EVENT_NEWLINE;
744 if (isalnum(ch) || ch == '_')
752 if (ch == '(' || ch == ')' || ch == ',')
758 static int __read_char(void)
760 if (input_buf_ptr >= input_buf_siz)
763 return input_buf[input_buf_ptr++];
766 static int __peek_char(void)
768 if (input_buf_ptr >= input_buf_siz)
771 return input_buf[input_buf_ptr];
775 * pevent_peek_char - peek at the next character that will be read
777 * Returns the next character read, or -1 if end of buffer.
779 int pevent_peek_char(void)
781 return __peek_char();
784 static enum event_type force_token(const char *str, char **tok);
786 static enum event_type __read_token(char **tok)
789 int ch, last_ch, quote_ch, next_ch;
792 enum event_type type;
802 if (type == EVENT_NONE)
810 *tok = malloc_or_die(2);
818 next_ch = __peek_char();
819 if (next_ch == '>') {
820 buf[i++] = __read_char();
833 buf[i++] = __read_char();
845 default: /* what should we do instead? */
855 buf[i++] = __read_char();
860 /* don't keep quotes */
866 if (i == (BUFSIZ - 1)) {
869 *tok = realloc(*tok, tok_size + BUFSIZ);
884 /* the '\' '\' will cancel itself */
885 if (ch == '\\' && last_ch == '\\')
887 } while (ch != quote_ch || last_ch == '\\');
888 /* remove the last quote */
892 * For strings (double quotes) check the next token.
893 * If it is another string, concatinate the two.
895 if (type == EVENT_DQUOTE) {
896 unsigned long long save_input_buf_ptr = input_buf_ptr;
900 } while (isspace(ch));
903 input_buf_ptr = save_input_buf_ptr;
908 case EVENT_ERROR ... EVENT_SPACE:
914 while (get_type(__peek_char()) == type) {
915 if (i == (BUFSIZ - 1)) {
918 *tok = realloc(*tok, tok_size + BUFSIZ);
937 *tok = realloc(*tok, tok_size + i);
946 if (type == EVENT_ITEM) {
948 * Older versions of the kernel has a bug that
949 * creates invalid symbols and will break the mac80211
950 * parsing. This is a work around to that bug.
952 * See Linux kernel commit:
953 * 811cb50baf63461ce0bdb234927046131fc7fa8b
955 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
958 return force_token("\"\%s\" ", tok);
959 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
962 return force_token("\" sta:%pM\" ", tok);
963 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
966 return force_token("\" vif:%p(%d)\" ", tok);
973 static enum event_type force_token(const char *str, char **tok)
975 const char *save_input_buf;
976 unsigned long long save_input_buf_ptr;
977 unsigned long long save_input_buf_siz;
978 enum event_type type;
980 /* save off the current input pointers */
981 save_input_buf = input_buf;
982 save_input_buf_ptr = input_buf_ptr;
983 save_input_buf_siz = input_buf_siz;
985 init_input_buf(str, strlen(str));
987 type = __read_token(tok);
989 /* reset back to original token */
990 input_buf = save_input_buf;
991 input_buf_ptr = save_input_buf_ptr;
992 input_buf_siz = save_input_buf_siz;
997 static void free_token(char *tok)
1003 static enum event_type read_token(char **tok)
1005 enum event_type type;
1008 type = __read_token(tok);
1009 if (type != EVENT_SPACE)
1021 * pevent_read_token - access to utilites to use the pevent parser
1022 * @tok: The token to return
1024 * This will parse tokens from the string given by
1025 * pevent_init_data().
1027 * Returns the token type.
1029 enum event_type pevent_read_token(char **tok)
1031 return read_token(tok);
1035 * pevent_free_token - free a token returned by pevent_read_token
1036 * @token: the token to free
1038 void pevent_free_token(char *token)
1044 static enum event_type read_token_item(char **tok)
1046 enum event_type type;
1049 type = __read_token(tok);
1050 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1061 static int test_type(enum event_type type, enum event_type expect)
1063 if (type != expect) {
1064 do_warning("Error: expected type %d but read %d",
1071 static int test_type_token(enum event_type type, const char *token,
1072 enum event_type expect, const char *expect_tok)
1074 if (type != expect) {
1075 do_warning("Error: expected type %d but read %d",
1080 if (strcmp(token, expect_tok) != 0) {
1081 do_warning("Error: expected '%s' but read '%s'",
1088 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1090 enum event_type type;
1093 type = read_token(tok);
1095 type = read_token_item(tok);
1096 return test_type(type, expect);
1099 static int read_expect_type(enum event_type expect, char **tok)
1101 return __read_expect_type(expect, tok, 1);
1104 static int __read_expected(enum event_type expect, const char *str,
1107 enum event_type type;
1112 type = read_token(&token);
1114 type = read_token_item(&token);
1116 ret = test_type_token(type, token, expect, str);
1123 static int read_expected(enum event_type expect, const char *str)
1125 return __read_expected(expect, str, 1);
1128 static int read_expected_item(enum event_type expect, const char *str)
1130 return __read_expected(expect, str, 0);
1133 static char *event_read_name(void)
1137 if (read_expected(EVENT_ITEM, "name") < 0)
1140 if (read_expected(EVENT_OP, ":") < 0)
1143 if (read_expect_type(EVENT_ITEM, &token) < 0)
1153 static int event_read_id(void)
1158 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1161 if (read_expected(EVENT_OP, ":") < 0)
1164 if (read_expect_type(EVENT_ITEM, &token) < 0)
1167 id = strtoul(token, NULL, 0);
1176 static int field_is_string(struct format_field *field)
1178 if ((field->flags & FIELD_IS_ARRAY) &&
1179 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1180 strstr(field->type, "s8")))
1186 static int field_is_dynamic(struct format_field *field)
1188 if (strncmp(field->type, "__data_loc", 10) == 0)
1194 static int field_is_long(struct format_field *field)
1196 /* includes long long */
1197 if (strstr(field->type, "long"))
1203 static int event_read_fields(struct event_format *event, struct format_field **fields)
1205 struct format_field *field = NULL;
1206 enum event_type type;
1212 type = read_token(&token);
1213 if (type == EVENT_NEWLINE) {
1220 if (test_type_token(type, token, EVENT_ITEM, "field"))
1224 type = read_token(&token);
1226 * The ftrace fields may still use the "special" name.
1229 if (event->flags & EVENT_FL_ISFTRACE &&
1230 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1232 type = read_token(&token);
1235 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1239 if (read_expect_type(EVENT_ITEM, &token) < 0)
1244 field = malloc_or_die(sizeof(*field));
1245 memset(field, 0, sizeof(*field));
1246 field->event = event;
1248 /* read the rest of the type */
1250 type = read_token(&token);
1251 if (type == EVENT_ITEM ||
1252 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1254 * Some of the ftrace fields are broken and have
1255 * an illegal "." in them.
1257 (event->flags & EVENT_FL_ISFTRACE &&
1258 type == EVENT_OP && strcmp(token, ".") == 0)) {
1260 if (strcmp(token, "*") == 0)
1261 field->flags |= FIELD_IS_POINTER;
1264 field->type = realloc(field->type,
1265 strlen(field->type) +
1266 strlen(last_token) + 2);
1267 strcat(field->type, " ");
1268 strcat(field->type, last_token);
1271 field->type = last_token;
1280 die("no type found");
1283 field->name = last_token;
1285 if (test_type(type, EVENT_OP))
1288 if (strcmp(token, "[") == 0) {
1289 enum event_type last_type = type;
1290 char *brackets = token;
1293 field->flags |= FIELD_IS_ARRAY;
1295 type = read_token(&token);
1297 if (type == EVENT_ITEM)
1298 field->arraylen = strtoul(token, NULL, 0);
1300 field->arraylen = 0;
1302 while (strcmp(token, "]") != 0) {
1303 if (last_type == EVENT_ITEM &&
1310 brackets = realloc(brackets,
1312 strlen(token) + len);
1314 strcat(brackets, " ");
1315 strcat(brackets, token);
1316 /* We only care about the last token */
1317 field->arraylen = strtoul(token, NULL, 0);
1319 type = read_token(&token);
1320 if (type == EVENT_NONE) {
1321 die("failed to find token");
1328 brackets = realloc(brackets, strlen(brackets) + 2);
1329 strcat(brackets, "]");
1331 /* add brackets to type */
1333 type = read_token(&token);
1335 * If the next token is not an OP, then it is of
1336 * the format: type [] item;
1338 if (type == EVENT_ITEM) {
1339 field->type = realloc(field->type,
1340 strlen(field->type) +
1341 strlen(field->name) +
1342 strlen(brackets) + 2);
1343 strcat(field->type, " ");
1344 strcat(field->type, field->name);
1345 free_token(field->name);
1346 strcat(field->type, brackets);
1347 field->name = token;
1348 type = read_token(&token);
1350 field->type = realloc(field->type,
1351 strlen(field->type) +
1352 strlen(brackets) + 1);
1353 strcat(field->type, brackets);
1358 if (field_is_string(field))
1359 field->flags |= FIELD_IS_STRING;
1360 if (field_is_dynamic(field))
1361 field->flags |= FIELD_IS_DYNAMIC;
1362 if (field_is_long(field))
1363 field->flags |= FIELD_IS_LONG;
1365 if (test_type_token(type, token, EVENT_OP, ";"))
1369 if (read_expected(EVENT_ITEM, "offset") < 0)
1372 if (read_expected(EVENT_OP, ":") < 0)
1375 if (read_expect_type(EVENT_ITEM, &token))
1377 field->offset = strtoul(token, NULL, 0);
1380 if (read_expected(EVENT_OP, ";") < 0)
1383 if (read_expected(EVENT_ITEM, "size") < 0)
1386 if (read_expected(EVENT_OP, ":") < 0)
1389 if (read_expect_type(EVENT_ITEM, &token))
1391 field->size = strtoul(token, NULL, 0);
1394 if (read_expected(EVENT_OP, ";") < 0)
1397 type = read_token(&token);
1398 if (type != EVENT_NEWLINE) {
1399 /* newer versions of the kernel have a "signed" type */
1400 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1405 if (read_expected(EVENT_OP, ":") < 0)
1408 if (read_expect_type(EVENT_ITEM, &token))
1411 /* add signed type */
1414 if (read_expected(EVENT_OP, ";") < 0)
1417 if (read_expect_type(EVENT_NEWLINE, &token))
1423 if (field->flags & FIELD_IS_ARRAY) {
1424 if (field->arraylen)
1425 field->elementsize = field->size / field->arraylen;
1426 else if (field->flags & FIELD_IS_STRING)
1427 field->elementsize = 1;
1429 field->elementsize = event->pevent->long_size;
1431 field->elementsize = field->size;
1434 fields = &field->next;
1451 static int event_read_format(struct event_format *event)
1456 if (read_expected_item(EVENT_ITEM, "format") < 0)
1459 if (read_expected(EVENT_OP, ":") < 0)
1462 if (read_expect_type(EVENT_NEWLINE, &token))
1466 ret = event_read_fields(event, &event->format.common_fields);
1469 event->format.nr_common = ret;
1471 ret = event_read_fields(event, &event->format.fields);
1474 event->format.nr_fields = ret;
1483 static enum event_type
1484 process_arg_token(struct event_format *event, struct print_arg *arg,
1485 char **tok, enum event_type type);
1487 static enum event_type
1488 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1490 enum event_type type;
1493 type = read_token(&token);
1496 return process_arg_token(event, arg, tok, type);
1499 static enum event_type
1500 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1502 static enum event_type
1503 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1505 struct print_arg *arg, *left, *right;
1506 enum event_type type;
1511 right = alloc_arg();
1513 arg->type = PRINT_OP;
1514 arg->op.left = left;
1515 arg->op.right = right;
1518 type = process_arg(event, left, &token);
1521 /* Handle other operations in the arguments */
1522 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1523 type = process_op(event, left, &token);
1527 if (test_type_token(type, token, EVENT_OP, ":"))
1532 type = process_arg(event, right, &token);
1534 top->op.right = arg;
1540 /* Top may point to itself */
1541 top->op.right = NULL;
1547 static enum event_type
1548 process_array(struct event_format *event, struct print_arg *top, char **tok)
1550 struct print_arg *arg;
1551 enum event_type type;
1557 type = process_arg(event, arg, &token);
1558 if (test_type_token(type, token, EVENT_OP, "]"))
1561 top->op.right = arg;
1564 type = read_token_item(&token);
1576 static int get_op_prio(char *op)
1590 /* '>>' and '<<' are 8 */
1594 /* '==' and '!=' are 10 */
1604 do_warning("unknown op '%c'", op[0]);
1608 if (strcmp(op, "++") == 0 ||
1609 strcmp(op, "--") == 0) {
1611 } else if (strcmp(op, ">>") == 0 ||
1612 strcmp(op, "<<") == 0) {
1614 } else if (strcmp(op, ">=") == 0 ||
1615 strcmp(op, "<=") == 0) {
1617 } else if (strcmp(op, "==") == 0 ||
1618 strcmp(op, "!=") == 0) {
1620 } else if (strcmp(op, "&&") == 0) {
1622 } else if (strcmp(op, "||") == 0) {
1625 do_warning("unknown op '%s'", op);
1631 static int set_op_prio(struct print_arg *arg)
1634 /* single ops are the greatest */
1635 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1638 arg->op.prio = get_op_prio(arg->op.op);
1640 return arg->op.prio;
1643 /* Note, *tok does not get freed, but will most likely be saved */
1644 static enum event_type
1645 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1647 struct print_arg *left, *right = NULL;
1648 enum event_type type;
1651 /* the op is passed in via tok */
1654 if (arg->type == PRINT_OP && !arg->op.left) {
1655 /* handle single op */
1657 die("bad op token %s", token);
1667 do_warning("bad op token %s", token);
1672 /* make an empty left */
1674 left->type = PRINT_NULL;
1675 arg->op.left = left;
1677 right = alloc_arg();
1678 arg->op.right = right;
1680 /* do not free the token, it belongs to an op */
1682 type = process_arg(event, right, tok);
1684 } else if (strcmp(token, "?") == 0) {
1687 /* copy the top arg to the left */
1690 arg->type = PRINT_OP;
1692 arg->op.left = left;
1695 type = process_cond(event, arg, tok);
1697 } else if (strcmp(token, ">>") == 0 ||
1698 strcmp(token, "<<") == 0 ||
1699 strcmp(token, "&") == 0 ||
1700 strcmp(token, "|") == 0 ||
1701 strcmp(token, "&&") == 0 ||
1702 strcmp(token, "||") == 0 ||
1703 strcmp(token, "-") == 0 ||
1704 strcmp(token, "+") == 0 ||
1705 strcmp(token, "*") == 0 ||
1706 strcmp(token, "^") == 0 ||
1707 strcmp(token, "/") == 0 ||
1708 strcmp(token, "<") == 0 ||
1709 strcmp(token, ">") == 0 ||
1710 strcmp(token, "==") == 0 ||
1711 strcmp(token, "!=") == 0) {
1715 /* copy the top arg to the left */
1718 arg->type = PRINT_OP;
1720 arg->op.left = left;
1722 if (set_op_prio(arg) == -1) {
1723 event->flags |= EVENT_FL_FAILED;
1724 /* arg->op.op (= token) will be freed at out_free */
1729 type = read_token_item(&token);
1732 /* could just be a type pointer */
1733 if ((strcmp(arg->op.op, "*") == 0) &&
1734 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1735 if (left->type != PRINT_ATOM)
1736 die("bad pointer type");
1737 left->atom.atom = realloc(left->atom.atom,
1738 strlen(left->atom.atom) + 3);
1739 strcat(left->atom.atom, " *");
1747 right = alloc_arg();
1748 type = process_arg_token(event, right, tok, type);
1749 arg->op.right = right;
1751 } else if (strcmp(token, "[") == 0) {
1756 arg->type = PRINT_OP;
1758 arg->op.left = left;
1762 type = process_array(event, arg, tok);
1765 do_warning("unknown op '%s'", token);
1766 event->flags |= EVENT_FL_FAILED;
1767 /* the arg is now the left side */
1771 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1774 /* higher prios need to be closer to the root */
1775 prio = get_op_prio(*tok);
1777 if (prio > arg->op.prio)
1778 return process_op(event, arg, tok);
1780 return process_op(event, right, tok);
1791 static enum event_type
1792 process_entry(struct event_format *event __unused, struct print_arg *arg,
1795 enum event_type type;
1799 if (read_expected(EVENT_OP, "->") < 0)
1802 if (read_expect_type(EVENT_ITEM, &token) < 0)
1806 arg->type = PRINT_FIELD;
1807 arg->field.name = field;
1809 if (is_flag_field) {
1810 arg->field.field = pevent_find_any_field(event, arg->field.name);
1811 arg->field.field->flags |= FIELD_IS_FLAG;
1813 } else if (is_symbolic_field) {
1814 arg->field.field = pevent_find_any_field(event, arg->field.name);
1815 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1816 is_symbolic_field = 0;
1819 type = read_token(&token);
1831 static char *arg_eval (struct print_arg *arg);
1833 static unsigned long long
1834 eval_type_str(unsigned long long val, const char *type, int pointer)
1844 if (type[len-1] != '*') {
1845 do_warning("pointer expected with non pointer type");
1849 ref = malloc_or_die(len);
1850 memcpy(ref, type, len);
1852 /* chop off the " *" */
1855 val = eval_type_str(val, ref, 0);
1860 /* check if this is a pointer */
1861 if (type[len - 1] == '*')
1864 /* Try to figure out the arg size*/
1865 if (strncmp(type, "struct", 6) == 0)
1869 if (strcmp(type, "u8") == 0)
1872 if (strcmp(type, "u16") == 0)
1873 return val & 0xffff;
1875 if (strcmp(type, "u32") == 0)
1876 return val & 0xffffffff;
1878 if (strcmp(type, "u64") == 0 ||
1879 strcmp(type, "s64"))
1882 if (strcmp(type, "s8") == 0)
1883 return (unsigned long long)(char)val & 0xff;
1885 if (strcmp(type, "s16") == 0)
1886 return (unsigned long long)(short)val & 0xffff;
1888 if (strcmp(type, "s32") == 0)
1889 return (unsigned long long)(int)val & 0xffffffff;
1891 if (strncmp(type, "unsigned ", 9) == 0) {
1896 if (strcmp(type, "char") == 0) {
1898 return (unsigned long long)(char)val & 0xff;
1903 if (strcmp(type, "short") == 0) {
1905 return (unsigned long long)(short)val & 0xffff;
1907 return val & 0xffff;
1910 if (strcmp(type, "int") == 0) {
1912 return (unsigned long long)(int)val & 0xffffffff;
1914 return val & 0xffffffff;
1921 * Try to figure out the type.
1923 static unsigned long long
1924 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1926 if (arg->type != PRINT_TYPE)
1927 die("expected type argument");
1929 return eval_type_str(val, arg->typecast.type, pointer);
1932 static int arg_num_eval(struct print_arg *arg, long long *val)
1934 long long left, right;
1937 switch (arg->type) {
1939 *val = strtoll(arg->atom.atom, NULL, 0);
1942 ret = arg_num_eval(arg->typecast.item, val);
1945 *val = eval_type(*val, arg, 0);
1948 switch (arg->op.op[0]) {
1950 ret = arg_num_eval(arg->op.left, &left);
1953 ret = arg_num_eval(arg->op.right, &right);
1957 *val = left || right;
1959 *val = left | right;
1962 ret = arg_num_eval(arg->op.left, &left);
1965 ret = arg_num_eval(arg->op.right, &right);
1969 *val = left && right;
1971 *val = left & right;
1974 ret = arg_num_eval(arg->op.left, &left);
1977 ret = arg_num_eval(arg->op.right, &right);
1980 switch (arg->op.op[1]) {
1982 *val = left < right;
1985 *val = left << right;
1988 *val = left <= right;
1991 do_warning("unknown op '%s'", arg->op.op);
1996 ret = arg_num_eval(arg->op.left, &left);
1999 ret = arg_num_eval(arg->op.right, &right);
2002 switch (arg->op.op[1]) {
2004 *val = left > right;
2007 *val = left >> right;
2010 *val = left >= right;
2013 do_warning("unknown op '%s'", arg->op.op);
2018 ret = arg_num_eval(arg->op.left, &left);
2021 ret = arg_num_eval(arg->op.right, &right);
2025 if (arg->op.op[1] != '=') {
2026 do_warning("unknown op '%s'", arg->op.op);
2029 *val = left == right;
2032 ret = arg_num_eval(arg->op.left, &left);
2035 ret = arg_num_eval(arg->op.right, &right);
2039 switch (arg->op.op[1]) {
2041 *val = left != right;
2044 do_warning("unknown op '%s'", arg->op.op);
2049 /* check for negative */
2050 if (arg->op.left->type == PRINT_NULL)
2053 ret = arg_num_eval(arg->op.left, &left);
2056 ret = arg_num_eval(arg->op.right, &right);
2059 *val = left - right;
2062 if (arg->op.left->type == PRINT_NULL)
2065 ret = arg_num_eval(arg->op.left, &left);
2068 ret = arg_num_eval(arg->op.right, &right);
2071 *val = left + right;
2074 do_warning("unknown op '%s'", arg->op.op);
2080 case PRINT_FIELD ... PRINT_SYMBOL:
2084 do_warning("invalid eval type %d", arg->type);
2091 static char *arg_eval (struct print_arg *arg)
2094 static char buf[20];
2096 switch (arg->type) {
2098 return arg->atom.atom;
2100 return arg_eval(arg->typecast.item);
2102 if (!arg_num_eval(arg, &val))
2104 sprintf(buf, "%lld", val);
2108 case PRINT_FIELD ... PRINT_SYMBOL:
2112 die("invalid eval type %d", arg->type);
2119 static enum event_type
2120 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2122 enum event_type type;
2123 struct print_arg *arg = NULL;
2124 struct print_flag_sym *field;
2130 type = read_token_item(&token);
2131 if (test_type_token(type, token, EVENT_OP, "{"))
2137 type = process_arg(event, arg, &token);
2139 if (type == EVENT_OP)
2140 type = process_op(event, arg, &token);
2142 if (type == EVENT_ERROR)
2145 if (test_type_token(type, token, EVENT_DELIM, ","))
2148 field = malloc_or_die(sizeof(*field));
2149 memset(field, 0, sizeof(*field));
2151 value = arg_eval(arg);
2154 field->value = strdup(value);
2160 type = process_arg(event, arg, &token);
2161 if (test_type_token(type, token, EVENT_OP, "}"))
2164 value = arg_eval(arg);
2167 field->str = strdup(value);
2172 list = &field->next;
2175 type = read_token_item(&token);
2176 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2189 static enum event_type
2190 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2192 struct print_arg *field;
2193 enum event_type type;
2196 memset(arg, 0, sizeof(*arg));
2197 arg->type = PRINT_FLAGS;
2199 field = alloc_arg();
2201 type = process_arg(event, field, &token);
2203 /* Handle operations in the first argument */
2204 while (type == EVENT_OP)
2205 type = process_op(event, field, &token);
2207 if (test_type_token(type, token, EVENT_DELIM, ","))
2211 arg->flags.field = field;
2213 type = read_token_item(&token);
2214 if (event_item_type(type)) {
2215 arg->flags.delim = token;
2216 type = read_token_item(&token);
2219 if (test_type_token(type, token, EVENT_DELIM, ","))
2222 type = process_fields(event, &arg->flags.flags, &token);
2223 if (test_type_token(type, token, EVENT_DELIM, ")"))
2227 type = read_token_item(tok);
2236 static enum event_type
2237 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2239 struct print_arg *field;
2240 enum event_type type;
2243 memset(arg, 0, sizeof(*arg));
2244 arg->type = PRINT_SYMBOL;
2246 field = alloc_arg();
2248 type = process_arg(event, field, &token);
2249 if (test_type_token(type, token, EVENT_DELIM, ","))
2252 arg->symbol.field = field;
2254 type = process_fields(event, &arg->symbol.symbols, &token);
2255 if (test_type_token(type, token, EVENT_DELIM, ")"))
2259 type = read_token_item(tok);
2268 static enum event_type
2269 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2271 struct print_arg *field;
2272 enum event_type type;
2275 memset(arg, 0, sizeof(*arg));
2276 arg->type = PRINT_HEX;
2278 field = alloc_arg();
2279 type = process_arg(event, field, &token);
2281 if (test_type_token(type, token, EVENT_DELIM, ","))
2284 arg->hex.field = field;
2288 field = alloc_arg();
2289 type = process_arg(event, field, &token);
2291 if (test_type_token(type, token, EVENT_DELIM, ")"))
2294 arg->hex.size = field;
2297 type = read_token_item(tok);
2307 static enum event_type
2308 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2310 struct format_field *field;
2311 enum event_type type;
2314 memset(arg, 0, sizeof(*arg));
2315 arg->type = PRINT_DYNAMIC_ARRAY;
2318 * The item within the parenthesis is another field that holds
2319 * the index into where the array starts.
2321 type = read_token(&token);
2323 if (type != EVENT_ITEM)
2326 /* Find the field */
2328 field = pevent_find_field(event, token);
2332 arg->dynarray.field = field;
2333 arg->dynarray.index = 0;
2335 if (read_expected(EVENT_DELIM, ")") < 0)
2339 type = read_token_item(&token);
2341 if (type != EVENT_OP || strcmp(token, "[") != 0)
2346 type = process_arg(event, arg, &token);
2347 if (type == EVENT_ERROR)
2350 if (!test_type_token(type, token, EVENT_OP, "]"))
2354 type = read_token_item(tok);
2365 static enum event_type
2366 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2368 struct print_arg *item_arg;
2369 enum event_type type;
2372 type = process_arg(event, arg, &token);
2374 if (type == EVENT_ERROR)
2377 if (type == EVENT_OP)
2378 type = process_op(event, arg, &token);
2380 if (type == EVENT_ERROR)
2383 if (test_type_token(type, token, EVENT_DELIM, ")"))
2387 type = read_token_item(&token);
2390 * If the next token is an item or another open paren, then
2391 * this was a typecast.
2393 if (event_item_type(type) ||
2394 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2396 /* make this a typecast and contine */
2398 /* prevous must be an atom */
2399 if (arg->type != PRINT_ATOM)
2400 die("previous needed to be PRINT_ATOM");
2402 item_arg = alloc_arg();
2404 arg->type = PRINT_TYPE;
2405 arg->typecast.type = arg->atom.atom;
2406 arg->typecast.item = item_arg;
2407 type = process_arg_token(event, item_arg, &token, type);
2421 static enum event_type
2422 process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
2424 enum event_type type;
2427 if (read_expect_type(EVENT_ITEM, &token) < 0)
2430 arg->type = PRINT_STRING;
2431 arg->string.string = token;
2432 arg->string.offset = -1;
2434 if (read_expected(EVENT_DELIM, ")") < 0)
2437 type = read_token(&token);
2449 static struct pevent_function_handler *
2450 find_func_handler(struct pevent *pevent, char *func_name)
2452 struct pevent_function_handler *func;
2454 for (func = pevent->func_handlers; func; func = func->next) {
2455 if (strcmp(func->name, func_name) == 0)
2462 static void remove_func_handler(struct pevent *pevent, char *func_name)
2464 struct pevent_function_handler *func;
2465 struct pevent_function_handler **next;
2467 next = &pevent->func_handlers;
2468 while ((func = *next)) {
2469 if (strcmp(func->name, func_name) == 0) {
2471 free_func_handle(func);
2478 static enum event_type
2479 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2480 struct print_arg *arg, char **tok)
2482 struct print_arg **next_arg;
2483 struct print_arg *farg;
2484 enum event_type type;
2489 arg->type = PRINT_FUNC;
2490 arg->func.func = func;
2494 next_arg = &(arg->func.args);
2495 for (i = 0; i < func->nr_args; i++) {
2497 type = process_arg(event, farg, &token);
2498 if (i < (func->nr_args - 1))
2503 if (test_type_token(type, token, EVENT_DELIM, test)) {
2510 next_arg = &(farg->next);
2514 type = read_token(&token);
2520 static enum event_type
2521 process_function(struct event_format *event, struct print_arg *arg,
2522 char *token, char **tok)
2524 struct pevent_function_handler *func;
2526 if (strcmp(token, "__print_flags") == 0) {
2529 return process_flags(event, arg, tok);
2531 if (strcmp(token, "__print_symbolic") == 0) {
2533 is_symbolic_field = 1;
2534 return process_symbols(event, arg, tok);
2536 if (strcmp(token, "__print_hex") == 0) {
2538 return process_hex(event, arg, tok);
2540 if (strcmp(token, "__get_str") == 0) {
2542 return process_str(event, arg, tok);
2544 if (strcmp(token, "__get_dynamic_array") == 0) {
2546 return process_dynamic_array(event, arg, tok);
2549 func = find_func_handler(event->pevent, token);
2552 return process_func_handler(event, func, arg, tok);
2555 do_warning("function %s not defined", token);
2560 static enum event_type
2561 process_arg_token(struct event_format *event, struct print_arg *arg,
2562 char **tok, enum event_type type)
2571 if (strcmp(token, "REC") == 0) {
2573 type = process_entry(event, arg, &token);
2577 /* test the next token */
2578 type = read_token_item(&token);
2581 * If the next token is a parenthesis, then this
2584 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2587 /* this will free atom. */
2588 type = process_function(event, arg, atom, &token);
2591 /* atoms can be more than one token long */
2592 while (type == EVENT_ITEM) {
2593 atom = realloc(atom, strlen(atom) + strlen(token) + 2);
2595 strcat(atom, token);
2597 type = read_token_item(&token);
2600 arg->type = PRINT_ATOM;
2601 arg->atom.atom = atom;
2606 arg->type = PRINT_ATOM;
2607 arg->atom.atom = token;
2608 type = read_token_item(&token);
2611 if (strcmp(token, "(") == 0) {
2613 type = process_paren(event, arg, &token);
2617 /* handle single ops */
2618 arg->type = PRINT_OP;
2620 arg->op.left = NULL;
2621 type = process_op(event, arg, &token);
2623 /* On error, the op is freed */
2624 if (type == EVENT_ERROR)
2627 /* return error type if errored */
2630 case EVENT_ERROR ... EVENT_NEWLINE:
2632 die("unexpected type %d", type);
2639 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2641 enum event_type type = EVENT_ERROR;
2642 struct print_arg *arg;
2647 if (type == EVENT_NEWLINE) {
2648 type = read_token_item(&token);
2654 type = process_arg(event, arg, &token);
2656 if (type == EVENT_ERROR) {
2665 if (type == EVENT_OP) {
2666 type = process_op(event, arg, &token);
2668 if (type == EVENT_ERROR) {
2677 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2684 } while (type != EVENT_NONE);
2686 if (type != EVENT_NONE && type != EVENT_ERROR)
2692 static int event_read_print(struct event_format *event)
2694 enum event_type type;
2698 if (read_expected_item(EVENT_ITEM, "print") < 0)
2701 if (read_expected(EVENT_ITEM, "fmt") < 0)
2704 if (read_expected(EVENT_OP, ":") < 0)
2707 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2711 event->print_fmt.format = token;
2712 event->print_fmt.args = NULL;
2714 /* ok to have no arg */
2715 type = read_token_item(&token);
2717 if (type == EVENT_NONE)
2720 /* Handle concatenation of print lines */
2721 if (type == EVENT_DQUOTE) {
2724 cat = malloc_or_die(strlen(event->print_fmt.format) +
2726 strcpy(cat, event->print_fmt.format);
2729 free_token(event->print_fmt.format);
2730 event->print_fmt.format = NULL;
2735 if (test_type_token(type, token, EVENT_DELIM, ","))
2740 ret = event_read_print_args(event, &event->print_fmt.args);
2752 * pevent_find_common_field - return a common field by event
2753 * @event: handle for the event
2754 * @name: the name of the common field to return
2756 * Returns a common field from the event by the given @name.
2757 * This only searchs the common fields and not all field.
2759 struct format_field *
2760 pevent_find_common_field(struct event_format *event, const char *name)
2762 struct format_field *format;
2764 for (format = event->format.common_fields;
2765 format; format = format->next) {
2766 if (strcmp(format->name, name) == 0)
2774 * pevent_find_field - find a non-common field
2775 * @event: handle for the event
2776 * @name: the name of the non-common field
2778 * Returns a non-common field by the given @name.
2779 * This does not search common fields.
2781 struct format_field *
2782 pevent_find_field(struct event_format *event, const char *name)
2784 struct format_field *format;
2786 for (format = event->format.fields;
2787 format; format = format->next) {
2788 if (strcmp(format->name, name) == 0)
2796 * pevent_find_any_field - find any field by name
2797 * @event: handle for the event
2798 * @name: the name of the field
2800 * Returns a field by the given @name.
2801 * This searchs the common field names first, then
2802 * the non-common ones if a common one was not found.
2804 struct format_field *
2805 pevent_find_any_field(struct event_format *event, const char *name)
2807 struct format_field *format;
2809 format = pevent_find_common_field(event, name);
2812 return pevent_find_field(event, name);
2816 * pevent_read_number - read a number from data
2817 * @pevent: handle for the pevent
2818 * @ptr: the raw data
2819 * @size: the size of the data that holds the number
2821 * Returns the number (converted to host) from the
2824 unsigned long long pevent_read_number(struct pevent *pevent,
2825 const void *ptr, int size)
2829 return *(unsigned char *)ptr;
2831 return data2host2(pevent, ptr);
2833 return data2host4(pevent, ptr);
2835 return data2host8(pevent, ptr);
2843 * pevent_read_number_field - read a number from data
2844 * @field: a handle to the field
2845 * @data: the raw data to read
2846 * @value: the value to place the number in
2848 * Reads raw data according to a field offset and size,
2849 * and translates it into @value.
2851 * Returns 0 on success, -1 otherwise.
2853 int pevent_read_number_field(struct format_field *field, const void *data,
2854 unsigned long long *value)
2858 switch (field->size) {
2863 *value = pevent_read_number(field->event->pevent,
2864 data + field->offset, field->size);
2871 static int get_common_info(struct pevent *pevent,
2872 const char *type, int *offset, int *size)
2874 struct event_format *event;
2875 struct format_field *field;
2878 * All events should have the same common elements.
2879 * Pick any event to find where the type is;
2881 if (!pevent->events)
2882 die("no event_list!");
2884 event = pevent->events[0];
2885 field = pevent_find_common_field(event, type);
2889 *offset = field->offset;
2890 *size = field->size;
2895 static int __parse_common(struct pevent *pevent, void *data,
2896 int *size, int *offset, const char *name)
2901 ret = get_common_info(pevent, name, offset, size);
2905 return pevent_read_number(pevent, data + *offset, *size);
2908 static int trace_parse_common_type(struct pevent *pevent, void *data)
2910 return __parse_common(pevent, data,
2911 &pevent->type_size, &pevent->type_offset,
2915 static int parse_common_pid(struct pevent *pevent, void *data)
2917 return __parse_common(pevent, data,
2918 &pevent->pid_size, &pevent->pid_offset,
2922 static int parse_common_pc(struct pevent *pevent, void *data)
2924 return __parse_common(pevent, data,
2925 &pevent->pc_size, &pevent->pc_offset,
2926 "common_preempt_count");
2929 static int parse_common_flags(struct pevent *pevent, void *data)
2931 return __parse_common(pevent, data,
2932 &pevent->flags_size, &pevent->flags_offset,
2936 static int parse_common_lock_depth(struct pevent *pevent, void *data)
2938 return __parse_common(pevent, data,
2939 &pevent->ld_size, &pevent->ld_offset,
2940 "common_lock_depth");
2943 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2945 return __parse_common(pevent, data,
2946 &pevent->ld_size, &pevent->ld_offset,
2947 "common_migrate_disable");
2950 static int events_id_cmp(const void *a, const void *b);
2953 * pevent_find_event - find an event by given id
2954 * @pevent: a handle to the pevent
2955 * @id: the id of the event
2957 * Returns an event that has a given @id.
2959 struct event_format *pevent_find_event(struct pevent *pevent, int id)
2961 struct event_format **eventptr;
2962 struct event_format key;
2963 struct event_format *pkey = &key;
2965 /* Check cache first */
2966 if (pevent->last_event && pevent->last_event->id == id)
2967 return pevent->last_event;
2971 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
2972 sizeof(*pevent->events), events_id_cmp);
2975 pevent->last_event = *eventptr;
2983 * pevent_find_event_by_name - find an event by given name
2984 * @pevent: a handle to the pevent
2985 * @sys: the system name to search for
2986 * @name: the name of the event to search for
2988 * This returns an event with a given @name and under the system
2989 * @sys. If @sys is NULL the first event with @name is returned.
2991 struct event_format *
2992 pevent_find_event_by_name(struct pevent *pevent,
2993 const char *sys, const char *name)
2995 struct event_format *event;
2998 if (pevent->last_event &&
2999 strcmp(pevent->last_event->name, name) == 0 &&
3000 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3001 return pevent->last_event;
3003 for (i = 0; i < pevent->nr_events; i++) {
3004 event = pevent->events[i];
3005 if (strcmp(event->name, name) == 0) {
3008 if (strcmp(event->system, sys) == 0)
3012 if (i == pevent->nr_events)
3015 pevent->last_event = event;
3019 static unsigned long long
3020 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3022 struct pevent *pevent = event->pevent;
3023 unsigned long long val = 0;
3024 unsigned long long left, right;
3025 struct print_arg *typearg = NULL;
3026 struct print_arg *larg;
3027 unsigned long offset;
3028 unsigned int field_size;
3030 switch (arg->type) {
3035 return strtoull(arg->atom.atom, NULL, 0);
3037 if (!arg->field.field) {
3038 arg->field.field = pevent_find_any_field(event, arg->field.name);
3039 if (!arg->field.field)
3040 die("field %s not found", arg->field.name);
3042 /* must be a number */
3043 val = pevent_read_number(pevent, data + arg->field.field->offset,
3044 arg->field.field->size);
3051 val = eval_num_arg(data, size, event, arg->typecast.item);
3052 return eval_type(val, arg, 0);
3059 val = process_defined_func(&s, data, size, event, arg);
3060 trace_seq_destroy(&s);
3064 if (strcmp(arg->op.op, "[") == 0) {
3066 * Arrays are special, since we don't want
3067 * to read the arg as is.
3069 right = eval_num_arg(data, size, event, arg->op.right);
3071 /* handle typecasts */
3072 larg = arg->op.left;
3073 while (larg->type == PRINT_TYPE) {
3076 larg = larg->typecast.item;
3079 /* Default to long size */
3080 field_size = pevent->long_size;
3082 switch (larg->type) {
3083 case PRINT_DYNAMIC_ARRAY:
3084 offset = pevent_read_number(pevent,
3085 data + larg->dynarray.field->offset,
3086 larg->dynarray.field->size);
3087 if (larg->dynarray.field->elementsize)
3088 field_size = larg->dynarray.field->elementsize;
3090 * The actual length of the dynamic array is stored
3091 * in the top half of the field, and the offset
3092 * is in the bottom half of the 32 bit field.
3098 if (!larg->field.field) {
3100 pevent_find_any_field(event, larg->field.name);
3101 if (!larg->field.field)
3102 die("field %s not found", larg->field.name);
3104 field_size = larg->field.field->elementsize;
3105 offset = larg->field.field->offset +
3106 right * larg->field.field->elementsize;
3109 goto default_op; /* oops, all bets off */
3111 val = pevent_read_number(pevent,
3112 data + offset, field_size);
3114 val = eval_type(val, typearg, 1);
3116 } else if (strcmp(arg->op.op, "?") == 0) {
3117 left = eval_num_arg(data, size, event, arg->op.left);
3118 arg = arg->op.right;
3120 val = eval_num_arg(data, size, event, arg->op.left);
3122 val = eval_num_arg(data, size, event, arg->op.right);
3126 left = eval_num_arg(data, size, event, arg->op.left);
3127 right = eval_num_arg(data, size, event, arg->op.right);
3128 switch (arg->op.op[0]) {
3130 switch (arg->op.op[1]) {
3135 val = left != right;
3138 die("unknown op '%s'", arg->op.op);
3146 val = left || right;
3152 val = left && right;
3157 switch (arg->op.op[1]) {
3162 val = left << right;
3165 val = left <= right;
3168 die("unknown op '%s'", arg->op.op);
3172 switch (arg->op.op[1]) {
3177 val = left >> right;
3180 val = left >= right;
3183 die("unknown op '%s'", arg->op.op);
3187 if (arg->op.op[1] != '=')
3188 die("unknown op '%s'", arg->op.op);
3189 val = left == right;
3204 die("unknown op '%s'", arg->op.op);
3207 default: /* not sure what to do there */
3215 unsigned long long value;
3218 static const struct flag flags[] = {
3219 { "HI_SOFTIRQ", 0 },
3220 { "TIMER_SOFTIRQ", 1 },
3221 { "NET_TX_SOFTIRQ", 2 },
3222 { "NET_RX_SOFTIRQ", 3 },
3223 { "BLOCK_SOFTIRQ", 4 },
3224 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3225 { "TASKLET_SOFTIRQ", 6 },
3226 { "SCHED_SOFTIRQ", 7 },
3227 { "HRTIMER_SOFTIRQ", 8 },
3228 { "RCU_SOFTIRQ", 9 },
3230 { "HRTIMER_NORESTART", 0 },
3231 { "HRTIMER_RESTART", 1 },
3234 static unsigned long long eval_flag(const char *flag)
3239 * Some flags in the format files do not get converted.
3240 * If the flag is not numeric, see if it is something that
3241 * we already know about.
3243 if (isdigit(flag[0]))
3244 return strtoull(flag, NULL, 0);
3246 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3247 if (strcmp(flags[i].name, flag) == 0)
3248 return flags[i].value;
3253 static void print_str_to_seq(struct trace_seq *s, const char *format,
3254 int len_arg, const char *str)
3257 trace_seq_printf(s, format, len_arg, str);
3259 trace_seq_printf(s, format, str);
3262 static void print_str_arg(struct trace_seq *s, void *data, int size,
3263 struct event_format *event, const char *format,
3264 int len_arg, struct print_arg *arg)
3266 struct pevent *pevent = event->pevent;
3267 struct print_flag_sym *flag;
3268 struct format_field *field;
3269 unsigned long long val, fval;
3276 switch (arg->type) {
3281 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3284 field = arg->field.field;
3286 field = pevent_find_any_field(event, arg->field.name);
3288 die("field %s not found", arg->field.name);
3289 arg->field.field = field;
3291 /* Zero sized fields, mean the rest of the data */
3292 len = field->size ? : size - field->offset;
3295 * Some events pass in pointers. If this is not an array
3296 * and the size is the same as long_size, assume that it
3299 if (!(field->flags & FIELD_IS_ARRAY) &&
3300 field->size == pevent->long_size) {
3301 addr = *(unsigned long *)(data + field->offset);
3302 trace_seq_printf(s, "%lx", addr);
3305 str = malloc_or_die(len + 1);
3306 memcpy(str, data + field->offset, len);
3308 print_str_to_seq(s, format, len_arg, str);
3312 val = eval_num_arg(data, size, event, arg->flags.field);
3314 for (flag = arg->flags.flags; flag; flag = flag->next) {
3315 fval = eval_flag(flag->value);
3316 if (!val && !fval) {
3317 print_str_to_seq(s, format, len_arg, flag->str);
3320 if (fval && (val & fval) == fval) {
3321 if (print && arg->flags.delim)
3322 trace_seq_puts(s, arg->flags.delim);
3323 print_str_to_seq(s, format, len_arg, flag->str);
3330 val = eval_num_arg(data, size, event, arg->symbol.field);
3331 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3332 fval = eval_flag(flag->value);
3334 print_str_to_seq(s, format, len_arg, flag->str);
3340 field = arg->hex.field->field.field;
3342 str = arg->hex.field->field.name;
3343 field = pevent_find_any_field(event, str);
3345 die("field %s not found", str);
3346 arg->hex.field->field.field = field;
3348 hex = data + field->offset;
3349 len = eval_num_arg(data, size, event, arg->hex.size);
3350 for (i = 0; i < len; i++) {
3352 trace_seq_putc(s, ' ');
3353 trace_seq_printf(s, "%02x", hex[i]);
3359 case PRINT_STRING: {
3362 if (arg->string.offset == -1) {
3363 struct format_field *f;
3365 f = pevent_find_any_field(event, arg->string.string);
3366 arg->string.offset = f->offset;
3368 str_offset = data2host4(pevent, data + arg->string.offset);
3369 str_offset &= 0xffff;
3370 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3374 print_str_to_seq(s, format, len_arg, arg->string.string);
3378 * The only op for string should be ? :
3380 if (arg->op.op[0] != '?')
3382 val = eval_num_arg(data, size, event, arg->op.left);
3384 print_str_arg(s, data, size, event,
3385 format, len_arg, arg->op.right->op.left);
3387 print_str_arg(s, data, size, event,
3388 format, len_arg, arg->op.right->op.right);
3391 process_defined_func(s, data, size, event, arg);
3399 static unsigned long long
3400 process_defined_func(struct trace_seq *s, void *data, int size,
3401 struct event_format *event, struct print_arg *arg)
3403 struct pevent_function_handler *func_handle = arg->func.func;
3404 struct pevent_func_params *param;
3405 unsigned long long *args;
3406 unsigned long long ret;
3407 struct print_arg *farg;
3408 struct trace_seq str;
3410 struct save_str *next;
3412 } *strings = NULL, *string;
3415 if (!func_handle->nr_args) {
3416 ret = (*func_handle->func)(s, NULL);
3420 farg = arg->func.args;
3421 param = func_handle->params;
3423 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3424 for (i = 0; i < func_handle->nr_args; i++) {
3425 switch (param->type) {
3426 case PEVENT_FUNC_ARG_INT:
3427 case PEVENT_FUNC_ARG_LONG:
3428 case PEVENT_FUNC_ARG_PTR:
3429 args[i] = eval_num_arg(data, size, event, farg);
3431 case PEVENT_FUNC_ARG_STRING:
3432 trace_seq_init(&str);
3433 print_str_arg(&str, data, size, event, "%s", -1, farg);
3434 trace_seq_terminate(&str);
3435 string = malloc_or_die(sizeof(*string));
3436 string->next = strings;
3437 string->str = strdup(str.buffer);
3439 trace_seq_destroy(&str);
3443 * Something went totally wrong, this is not
3444 * an input error, something in this code broke.
3446 die("Unexpected end of arguments\n");
3450 param = param->next;
3453 ret = (*func_handle->func)(s, args);
3457 strings = string->next;
3463 /* TBD : handle return type here */
3467 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3469 struct pevent *pevent = event->pevent;
3470 struct format_field *field, *ip_field;
3471 struct print_arg *args, *arg, **next;
3472 unsigned long long ip, val;
3477 field = pevent->bprint_buf_field;
3478 ip_field = pevent->bprint_ip_field;
3481 field = pevent_find_field(event, "buf");
3483 die("can't find buffer field for binary printk");
3484 ip_field = pevent_find_field(event, "ip");
3486 die("can't find ip field for binary printk");
3487 pevent->bprint_buf_field = field;
3488 pevent->bprint_ip_field = ip_field;
3491 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3494 * The first arg is the IP pointer.
3501 arg->type = PRINT_ATOM;
3502 arg->atom.atom = malloc_or_die(32);
3503 sprintf(arg->atom.atom, "%lld", ip);
3505 /* skip the first "%pf : " */
3506 for (ptr = fmt + 6, bptr = data + field->offset;
3507 bptr < data + size && *ptr; ptr++) {
3538 vsize = pevent->long_size;
3551 /* the pointers are always 4 bytes aligned */
3552 bptr = (void *)(((unsigned long)bptr + 3) &
3554 val = pevent_read_number(pevent, bptr, vsize);
3558 arg->type = PRINT_ATOM;
3559 arg->atom.atom = malloc_or_die(32);
3560 sprintf(arg->atom.atom, "%lld", val);
3564 * The '*' case means that an arg is used as the length.
3565 * We need to continue to figure out for what.
3574 arg->type = PRINT_BSTRING;
3575 arg->string.string = strdup(bptr);
3576 bptr += strlen(bptr) + 1;
3588 static void free_args(struct print_arg *args)
3590 struct print_arg *next;
3601 get_bprint_format(void *data, int size __unused, struct event_format *event)
3603 struct pevent *pevent = event->pevent;
3604 unsigned long long addr;
3605 struct format_field *field;
3606 struct printk_map *printk;
3610 field = pevent->bprint_fmt_field;
3613 field = pevent_find_field(event, "fmt");
3615 die("can't find format field for binary printk");
3616 pevent->bprint_fmt_field = field;
3619 addr = pevent_read_number(pevent, data + field->offset, field->size);
3621 printk = find_printk(pevent, addr);
3623 format = malloc_or_die(45);
3624 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3630 /* Remove any quotes. */
3633 format = malloc_or_die(strlen(p) + 10);
3634 sprintf(format, "%s : %s", "%pf", p);
3635 /* remove ending quotes and new line since we will add one too */
3636 p = format + strlen(format) - 1;
3641 if (strcmp(p, "\\n") == 0)
3647 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3648 struct event_format *event, struct print_arg *arg)
3651 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3653 if (arg->type == PRINT_FUNC) {
3654 process_defined_func(s, data, size, event, arg);
3658 if (arg->type != PRINT_FIELD) {
3659 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3665 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3666 if (!arg->field.field) {
3668 pevent_find_any_field(event, arg->field.name);
3669 if (!arg->field.field)
3670 die("field %s not found", arg->field.name);
3672 if (arg->field.field->size != 6) {
3673 trace_seq_printf(s, "INVALIDMAC");
3676 buf = data + arg->field.field->offset;
3677 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3680 static int is_printable_array(char *p, unsigned int len)
3684 for (i = 0; i < len && p[i]; i++)
3690 static void print_event_fields(struct trace_seq *s, void *data, int size,
3691 struct event_format *event)
3693 struct format_field *field;
3694 unsigned long long val;
3695 unsigned int offset, len, i;
3697 field = event->format.fields;
3699 trace_seq_printf(s, " %s=", field->name);
3700 if (field->flags & FIELD_IS_ARRAY) {
3701 offset = field->offset;
3703 if (field->flags & FIELD_IS_DYNAMIC) {
3704 val = pevent_read_number(event->pevent, data + offset, len);
3709 if (field->flags & FIELD_IS_STRING &&
3710 is_printable_array(data + offset, len)) {
3711 trace_seq_printf(s, "%s", (char *)data + offset);
3713 trace_seq_puts(s, "ARRAY[");
3714 for (i = 0; i < len; i++) {
3716 trace_seq_puts(s, ", ");
3717 trace_seq_printf(s, "%02x",
3718 *((unsigned char *)data + offset + i));
3720 trace_seq_putc(s, ']');
3721 field->flags &= ~FIELD_IS_STRING;
3724 val = pevent_read_number(event->pevent, data + field->offset,
3726 if (field->flags & FIELD_IS_POINTER) {
3727 trace_seq_printf(s, "0x%llx", val);
3728 } else if (field->flags & FIELD_IS_SIGNED) {
3729 switch (field->size) {
3732 * If field is long then print it in hex.
3733 * A long usually stores pointers.
3735 if (field->flags & FIELD_IS_LONG)
3736 trace_seq_printf(s, "0x%x", (int)val);
3738 trace_seq_printf(s, "%d", (int)val);
3741 trace_seq_printf(s, "%2d", (short)val);
3744 trace_seq_printf(s, "%1d", (char)val);
3747 trace_seq_printf(s, "%lld", val);
3750 if (field->flags & FIELD_IS_LONG)
3751 trace_seq_printf(s, "0x%llx", val);
3753 trace_seq_printf(s, "%llu", val);
3756 field = field->next;
3760 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3762 struct pevent *pevent = event->pevent;
3763 struct print_fmt *print_fmt = &event->print_fmt;
3764 struct print_arg *arg = print_fmt->args;
3765 struct print_arg *args = NULL;
3766 const char *ptr = print_fmt->format;
3767 unsigned long long val;
3768 struct func_map *func;
3769 const char *saveptr;
3770 char *bprint_fmt = NULL;
3778 if (event->flags & EVENT_FL_FAILED) {
3779 trace_seq_printf(s, "[FAILED TO PARSE]");
3780 print_event_fields(s, data, size, event);
3784 if (event->flags & EVENT_FL_ISBPRINT) {
3785 bprint_fmt = get_bprint_format(data, size, event);
3786 args = make_bprint_args(bprint_fmt, data, size, event);
3791 for (; *ptr; ptr++) {
3797 trace_seq_putc(s, '\n');
3800 trace_seq_putc(s, '\t');
3803 trace_seq_putc(s, '\r');
3806 trace_seq_putc(s, '\\');
3809 trace_seq_putc(s, *ptr);
3813 } else if (*ptr == '%') {
3821 trace_seq_putc(s, '%');
3824 /* FIXME: need to handle properly */
3836 /* The argument is the length. */
3838 die("no argument match");
3839 len_arg = eval_num_arg(data, size, event, arg);
3849 if (pevent->long_size == 4)
3854 if (*(ptr+1) == 'F' ||
3858 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3859 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3872 die("no argument match");
3874 len = ((unsigned long)ptr + 1) -
3875 (unsigned long)saveptr;
3877 /* should never happen */
3881 memcpy(format, saveptr, len);
3884 val = eval_num_arg(data, size, event, arg);
3888 func = find_func(pevent, val);
3890 trace_seq_puts(s, func->func);
3891 if (show_func == 'F')
3898 if (pevent->long_size == 8 && ls) {
3902 /* make %l into %ll */
3903 p = strchr(format, 'l');
3905 memmove(p, p+1, strlen(p)+1);
3906 else if (strcmp(format, "%p") == 0)
3907 strcpy(format, "0x%llx");
3912 trace_seq_printf(s, format, len_arg, (char)val);
3914 trace_seq_printf(s, format, (char)val);
3918 trace_seq_printf(s, format, len_arg, (short)val);
3920 trace_seq_printf(s, format, (short)val);
3924 trace_seq_printf(s, format, len_arg, (int)val);
3926 trace_seq_printf(s, format, (int)val);
3930 trace_seq_printf(s, format, len_arg, (long)val);
3932 trace_seq_printf(s, format, (long)val);
3936 trace_seq_printf(s, format, len_arg,
3939 trace_seq_printf(s, format, (long long)val);
3942 die("bad count (%d)", ls);
3947 die("no matching argument");
3949 len = ((unsigned long)ptr + 1) -
3950 (unsigned long)saveptr;
3952 /* should never happen */
3956 memcpy(format, saveptr, len);
3960 print_str_arg(s, data, size, event,
3961 format, len_arg, arg);
3965 trace_seq_printf(s, ">%c<", *ptr);
3969 trace_seq_putc(s, *ptr);
3979 * pevent_data_lat_fmt - parse the data for the latency format
3980 * @pevent: a handle to the pevent
3981 * @s: the trace_seq to write to
3982 * @data: the raw data to read from
3983 * @size: currently unused.
3985 * This parses out the Latency format (interrupts disabled,
3986 * need rescheduling, in hard/soft interrupt, preempt count
3987 * and lock depth) and places it into the trace_seq.
3989 void pevent_data_lat_fmt(struct pevent *pevent,
3990 struct trace_seq *s, struct pevent_record *record)
3992 static int check_lock_depth = 1;
3993 static int check_migrate_disable = 1;
3994 static int lock_depth_exists;
3995 static int migrate_disable_exists;
3996 unsigned int lat_flags;
3999 int migrate_disable;
4002 void *data = record->data;
4004 lat_flags = parse_common_flags(pevent, data);
4005 pc = parse_common_pc(pevent, data);
4006 /* lock_depth may not always exist */
4007 if (lock_depth_exists)
4008 lock_depth = parse_common_lock_depth(pevent, data);
4009 else if (check_lock_depth) {
4010 lock_depth = parse_common_lock_depth(pevent, data);
4012 check_lock_depth = 0;
4014 lock_depth_exists = 1;
4017 /* migrate_disable may not always exist */
4018 if (migrate_disable_exists)
4019 migrate_disable = parse_common_migrate_disable(pevent, data);
4020 else if (check_migrate_disable) {
4021 migrate_disable = parse_common_migrate_disable(pevent, data);
4022 if (migrate_disable < 0)
4023 check_migrate_disable = 0;
4025 migrate_disable_exists = 1;
4028 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4029 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4031 trace_seq_printf(s, "%c%c%c",
4032 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4033 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4035 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4037 (hardirq && softirq) ? 'H' :
4038 hardirq ? 'h' : softirq ? 's' : '.');
4041 trace_seq_printf(s, "%x", pc);
4043 trace_seq_putc(s, '.');
4045 if (migrate_disable_exists) {
4046 if (migrate_disable < 0)
4047 trace_seq_putc(s, '.');
4049 trace_seq_printf(s, "%d", migrate_disable);
4052 if (lock_depth_exists) {
4054 trace_seq_putc(s, '.');
4056 trace_seq_printf(s, "%d", lock_depth);
4059 trace_seq_terminate(s);
4063 * pevent_data_type - parse out the given event type
4064 * @pevent: a handle to the pevent
4065 * @rec: the record to read from
4067 * This returns the event id from the @rec.
4069 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4071 return trace_parse_common_type(pevent, rec->data);
4075 * pevent_data_event_from_type - find the event by a given type
4076 * @pevent: a handle to the pevent
4077 * @type: the type of the event.
4079 * This returns the event form a given @type;
4081 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4083 return pevent_find_event(pevent, type);
4087 * pevent_data_pid - parse the PID from raw data
4088 * @pevent: a handle to the pevent
4089 * @rec: the record to parse
4091 * This returns the PID from a raw data.
4093 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4095 return parse_common_pid(pevent, rec->data);
4099 * pevent_data_comm_from_pid - return the command line from PID
4100 * @pevent: a handle to the pevent
4101 * @pid: the PID of the task to search for
4103 * This returns a pointer to the command line that has the given
4106 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4110 comm = find_cmdline(pevent, pid);
4115 * pevent_data_comm_from_pid - parse the data into the print format
4116 * @s: the trace_seq to write to
4117 * @event: the handle to the event
4118 * @cpu: the cpu the event was recorded on
4119 * @data: the raw data
4120 * @size: the size of the raw data
4121 * @nsecs: the timestamp of the event
4123 * This parses the raw @data using the given @event information and
4124 * writes the print format into the trace_seq.
4126 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4127 struct pevent_record *record)
4129 int print_pretty = 1;
4131 if (event->pevent->print_raw)
4132 print_event_fields(s, record->data, record->size, event);
4136 print_pretty = event->handler(s, record, event,
4140 pretty_print(s, record->data, record->size, event);
4143 trace_seq_terminate(s);
4146 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4147 struct pevent_record *record)
4149 static char *spaces = " "; /* 20 spaces */
4150 struct event_format *event;
4152 unsigned long usecs;
4153 unsigned long nsecs;
4155 void *data = record->data;
4161 secs = record->ts / NSECS_PER_SEC;
4162 nsecs = record->ts - secs * NSECS_PER_SEC;
4164 if (record->size < 0) {
4165 do_warning("ug! negative record size %d", record->size);
4169 type = trace_parse_common_type(pevent, data);
4171 event = pevent_find_event(pevent, type);
4173 do_warning("ug! no event found for type %d", type);
4177 pid = parse_common_pid(pevent, data);
4178 comm = find_cmdline(pevent, pid);
4180 if (pevent->latency_format) {
4181 trace_seq_printf(s, "%8.8s-%-5d %3d",
4182 comm, pid, record->cpu);
4183 pevent_data_lat_fmt(pevent, s, record);
4185 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4187 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4191 usecs = (nsecs + 500) / NSECS_PER_USEC;
4195 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4197 /* Space out the event names evenly. */
4198 len = strlen(event->name);
4200 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4202 pevent_event_info(s, event, record);
4205 static int events_id_cmp(const void *a, const void *b)
4207 struct event_format * const * ea = a;
4208 struct event_format * const * eb = b;
4210 if ((*ea)->id < (*eb)->id)
4213 if ((*ea)->id > (*eb)->id)
4219 static int events_name_cmp(const void *a, const void *b)
4221 struct event_format * const * ea = a;
4222 struct event_format * const * eb = b;
4225 res = strcmp((*ea)->name, (*eb)->name);
4229 res = strcmp((*ea)->system, (*eb)->system);
4233 return events_id_cmp(a, b);
4236 static int events_system_cmp(const void *a, const void *b)
4238 struct event_format * const * ea = a;
4239 struct event_format * const * eb = b;
4242 res = strcmp((*ea)->system, (*eb)->system);
4246 res = strcmp((*ea)->name, (*eb)->name);
4250 return events_id_cmp(a, b);
4253 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4255 struct event_format **events;
4256 int (*sort)(const void *a, const void *b);
4258 events = pevent->sort_events;
4260 if (events && pevent->last_type == sort_type)
4264 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4268 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4269 events[pevent->nr_events] = NULL;
4271 pevent->sort_events = events;
4273 /* the internal events are sorted by id */
4274 if (sort_type == EVENT_SORT_ID) {
4275 pevent->last_type = sort_type;
4280 switch (sort_type) {
4282 sort = events_id_cmp;
4284 case EVENT_SORT_NAME:
4285 sort = events_name_cmp;
4287 case EVENT_SORT_SYSTEM:
4288 sort = events_system_cmp;
4294 qsort(events, pevent->nr_events, sizeof(*events), sort);
4295 pevent->last_type = sort_type;
4300 static struct format_field **
4301 get_event_fields(const char *type, const char *name,
4302 int count, struct format_field *list)
4304 struct format_field **fields;
4305 struct format_field *field;
4308 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4309 for (field = list; field; field = field->next) {
4310 fields[i++] = field;
4311 if (i == count + 1) {
4312 do_warning("event %s has more %s fields than specified",
4320 do_warning("event %s has less %s fields than specified",
4329 * pevent_event_common_fields - return a list of common fields for an event
4330 * @event: the event to return the common fields of.
4332 * Returns an allocated array of fields. The last item in the array is NULL.
4333 * The array must be freed with free().
4335 struct format_field **pevent_event_common_fields(struct event_format *event)
4337 return get_event_fields("common", event->name,
4338 event->format.nr_common,
4339 event->format.common_fields);
4343 * pevent_event_fields - return a list of event specific fields for an event
4344 * @event: the event to return the fields of.
4346 * Returns an allocated array of fields. The last item in the array is NULL.
4347 * The array must be freed with free().
4349 struct format_field **pevent_event_fields(struct event_format *event)
4351 return get_event_fields("event", event->name,
4352 event->format.nr_fields,
4353 event->format.fields);
4356 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4358 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4360 trace_seq_puts(s, ", ");
4361 print_fields(s, field->next);
4366 static void print_args(struct print_arg *args)
4368 int print_paren = 1;
4371 switch (args->type) {
4376 printf("%s", args->atom.atom);
4379 printf("REC->%s", args->field.name);
4382 printf("__print_flags(");
4383 print_args(args->flags.field);
4384 printf(", %s, ", args->flags.delim);
4386 print_fields(&s, args->flags.flags);
4387 trace_seq_do_printf(&s);
4388 trace_seq_destroy(&s);
4392 printf("__print_symbolic(");
4393 print_args(args->symbol.field);
4396 print_fields(&s, args->symbol.symbols);
4397 trace_seq_do_printf(&s);
4398 trace_seq_destroy(&s);
4402 printf("__print_hex(");
4403 print_args(args->hex.field);
4405 print_args(args->hex.size);
4410 printf("__get_str(%s)", args->string.string);
4413 printf("(%s)", args->typecast.type);
4414 print_args(args->typecast.item);
4417 if (strcmp(args->op.op, ":") == 0)
4421 print_args(args->op.left);
4422 printf(" %s ", args->op.op);
4423 print_args(args->op.right);
4428 /* we should warn... */
4433 print_args(args->next);
4437 static void parse_header_field(const char *field,
4438 int *offset, int *size, int mandatory)
4440 unsigned long long save_input_buf_ptr;
4441 unsigned long long save_input_buf_siz;
4445 save_input_buf_ptr = input_buf_ptr;
4446 save_input_buf_siz = input_buf_siz;
4448 if (read_expected(EVENT_ITEM, "field") < 0)
4450 if (read_expected(EVENT_OP, ":") < 0)
4454 if (read_expect_type(EVENT_ITEM, &token) < 0)
4459 * If this is not a mandatory field, then test it first.
4462 if (read_expected(EVENT_ITEM, field) < 0)
4465 if (read_expect_type(EVENT_ITEM, &token) < 0)
4467 if (strcmp(token, field) != 0)
4472 if (read_expected(EVENT_OP, ";") < 0)
4474 if (read_expected(EVENT_ITEM, "offset") < 0)
4476 if (read_expected(EVENT_OP, ":") < 0)
4478 if (read_expect_type(EVENT_ITEM, &token) < 0)
4480 *offset = atoi(token);
4482 if (read_expected(EVENT_OP, ";") < 0)
4484 if (read_expected(EVENT_ITEM, "size") < 0)
4486 if (read_expected(EVENT_OP, ":") < 0)
4488 if (read_expect_type(EVENT_ITEM, &token) < 0)
4490 *size = atoi(token);
4492 if (read_expected(EVENT_OP, ";") < 0)
4494 type = read_token(&token);
4495 if (type != EVENT_NEWLINE) {
4496 /* newer versions of the kernel have a "signed" type */
4497 if (type != EVENT_ITEM)
4500 if (strcmp(token, "signed") != 0)
4505 if (read_expected(EVENT_OP, ":") < 0)
4508 if (read_expect_type(EVENT_ITEM, &token))
4512 if (read_expected(EVENT_OP, ";") < 0)
4515 if (read_expect_type(EVENT_NEWLINE, &token))
4523 input_buf_ptr = save_input_buf_ptr;
4524 input_buf_siz = save_input_buf_siz;
4531 * pevent_parse_header_page - parse the data stored in the header page
4532 * @pevent: the handle to the pevent
4533 * @buf: the buffer storing the header page format string
4534 * @size: the size of @buf
4535 * @long_size: the long size to use if there is no header
4537 * This parses the header page format for information on the
4538 * ring buffer used. The @buf should be copied from
4540 * /sys/kernel/debug/tracing/events/header_page
4542 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4549 * Old kernels did not have header page info.
4550 * Sorry but we just use what we find here in user space.
4552 pevent->header_page_ts_size = sizeof(long long);
4553 pevent->header_page_size_size = long_size;
4554 pevent->header_page_data_offset = sizeof(long long) + long_size;
4555 pevent->old_format = 1;
4558 init_input_buf(buf, size);
4560 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4561 &pevent->header_page_ts_size, 1);
4562 parse_header_field("commit", &pevent->header_page_size_offset,
4563 &pevent->header_page_size_size, 1);
4564 parse_header_field("overwrite", &pevent->header_page_overwrite,
4566 parse_header_field("data", &pevent->header_page_data_offset,
4567 &pevent->header_page_data_size, 1);
4572 static int event_matches(struct event_format *event,
4573 int id, const char *sys_name,
4574 const char *event_name)
4576 if (id >= 0 && id != event->id)
4579 if (event_name && (strcmp(event_name, event->name) != 0))
4582 if (sys_name && (strcmp(sys_name, event->system) != 0))
4588 static void free_handler(struct event_handler *handle)
4590 free((void *)handle->sys_name);
4591 free((void *)handle->event_name);
4595 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4597 struct event_handler *handle, **next;
4599 for (next = &pevent->handlers; *next;
4600 next = &(*next)->next) {
4602 if (event_matches(event, handle->id,
4604 handle->event_name))
4611 pr_stat("overriding event (%d) %s:%s with new print handler",
4612 event->id, event->system, event->name);
4614 event->handler = handle->func;
4615 event->context = handle->context;
4617 *next = handle->next;
4618 free_handler(handle);
4624 * pevent_parse_event - parse the event format
4625 * @pevent: the handle to the pevent
4626 * @buf: the buffer storing the event format string
4627 * @size: the size of @buf
4628 * @sys: the system the event belongs to
4630 * This parses the event format and creates an event structure
4631 * to quickly parse raw data for a given event.
4633 * These files currently come from:
4635 * /sys/kernel/debug/tracing/events/.../.../format
4637 int pevent_parse_event(struct pevent *pevent,
4638 const char *buf, unsigned long size,
4641 struct event_format *event;
4644 init_input_buf(buf, size);
4646 event = alloc_event();
4650 event->name = event_read_name();
4657 if (strcmp(sys, "ftrace") == 0) {
4659 event->flags |= EVENT_FL_ISFTRACE;
4661 if (strcmp(event->name, "bprint") == 0)
4662 event->flags |= EVENT_FL_ISBPRINT;
4665 event->id = event_read_id();
4667 die("failed to read event id");
4669 event->system = strdup(sys);
4671 /* Add pevent to event so that it can be referenced */
4672 event->pevent = pevent;
4674 ret = event_read_format(event);
4676 do_warning("failed to read event format for %s", event->name);
4681 * If the event has an override, don't print warnings if the event
4682 * print format fails to parse.
4684 if (find_event_handle(pevent, event))
4687 ret = event_read_print(event);
4689 do_warning("failed to read event print fmt for %s",
4696 add_event(pevent, event);
4698 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4699 struct format_field *field;
4700 struct print_arg *arg, **list;
4702 /* old ftrace had no args */
4704 list = &event->print_fmt.args;
4705 for (field = event->format.fields; field; field = field->next) {
4709 arg->type = PRINT_FIELD;
4710 arg->field.name = strdup(field->name);
4711 arg->field.field = field;
4716 #define PRINT_ARGS 0
4717 if (PRINT_ARGS && event->print_fmt.args)
4718 print_args(event->print_fmt.args);
4723 event->flags |= EVENT_FL_FAILED;
4724 /* still add it even if it failed */
4725 add_event(pevent, event);
4729 int get_field_val(struct trace_seq *s, struct format_field *field,
4730 const char *name, struct pevent_record *record,
4731 unsigned long long *val, int err)
4735 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4739 if (pevent_read_number_field(field, record->data, val)) {
4741 trace_seq_printf(s, " %s=INVALID", name);
4749 * pevent_get_field_raw - return the raw pointer into the data field
4750 * @s: The seq to print to on error
4751 * @event: the event that the field is for
4752 * @name: The name of the field
4753 * @record: The record with the field name.
4754 * @len: place to store the field length.
4755 * @err: print default error if failed.
4757 * Returns a pointer into record->data of the field and places
4758 * the length of the field in @len.
4760 * On failure, it returns NULL.
4762 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
4763 const char *name, struct pevent_record *record,
4766 struct format_field *field;
4767 void *data = record->data;
4774 field = pevent_find_field(event, name);
4778 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4782 /* Allow @len to be NULL */
4786 offset = field->offset;
4787 if (field->flags & FIELD_IS_DYNAMIC) {
4788 offset = pevent_read_number(event->pevent,
4789 data + offset, field->size);
4790 *len = offset >> 16;
4795 return data + offset;
4799 * pevent_get_field_val - find a field and return its value
4800 * @s: The seq to print to on error
4801 * @event: the event that the field is for
4802 * @name: The name of the field
4803 * @record: The record with the field name.
4804 * @val: place to store the value of the field.
4805 * @err: print default error if failed.
4807 * Returns 0 on success -1 on field not found.
4809 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
4810 const char *name, struct pevent_record *record,
4811 unsigned long long *val, int err)
4813 struct format_field *field;
4818 field = pevent_find_field(event, name);
4820 return get_field_val(s, field, name, record, val, err);
4824 * pevent_get_common_field_val - find a common field and return its value
4825 * @s: The seq to print to on error
4826 * @event: the event that the field is for
4827 * @name: The name of the field
4828 * @record: The record with the field name.
4829 * @val: place to store the value of the field.
4830 * @err: print default error if failed.
4832 * Returns 0 on success -1 on field not found.
4834 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
4835 const char *name, struct pevent_record *record,
4836 unsigned long long *val, int err)
4838 struct format_field *field;
4843 field = pevent_find_common_field(event, name);
4845 return get_field_val(s, field, name, record, val, err);
4849 * pevent_get_any_field_val - find a any field and return its value
4850 * @s: The seq to print to on error
4851 * @event: the event that the field is for
4852 * @name: The name of the field
4853 * @record: The record with the field name.
4854 * @val: place to store the value of the field.
4855 * @err: print default error if failed.
4857 * Returns 0 on success -1 on field not found.
4859 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
4860 const char *name, struct pevent_record *record,
4861 unsigned long long *val, int err)
4863 struct format_field *field;
4868 field = pevent_find_any_field(event, name);
4870 return get_field_val(s, field, name, record, val, err);
4874 * pevent_print_num_field - print a field and a format
4875 * @s: The seq to print to
4876 * @fmt: The printf format to print the field with.
4877 * @event: the event that the field is for
4878 * @name: The name of the field
4879 * @record: The record with the field name.
4880 * @err: print default error if failed.
4882 * Returns: 0 on success, -1 field not fould, or 1 if buffer is full.
4884 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
4885 struct event_format *event, const char *name,
4886 struct pevent_record *record, int err)
4888 struct format_field *field = pevent_find_field(event, name);
4889 unsigned long long val;
4894 if (pevent_read_number_field(field, record->data, &val))
4897 return trace_seq_printf(s, fmt, val);
4901 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
4905 static void free_func_handle(struct pevent_function_handler *func)
4907 struct pevent_func_params *params;
4911 while (func->params) {
4912 params = func->params;
4913 func->params = params->next;
4921 * pevent_register_print_function - register a helper function
4922 * @pevent: the handle to the pevent
4923 * @func: the function to process the helper function
4924 * @name: the name of the helper function
4925 * @parameters: A list of enum pevent_func_arg_type
4927 * Some events may have helper functions in the print format arguments.
4928 * This allows a plugin to dynmically create a way to process one
4929 * of these functions.
4931 * The @parameters is a variable list of pevent_func_arg_type enums that
4932 * must end with PEVENT_FUNC_ARG_VOID.
4934 int pevent_register_print_function(struct pevent *pevent,
4935 pevent_func_handler func,
4936 enum pevent_func_arg_type ret_type,
4939 struct pevent_function_handler *func_handle;
4940 struct pevent_func_params **next_param;
4941 struct pevent_func_params *param;
4942 enum pevent_func_arg_type type;
4945 func_handle = find_func_handler(pevent, name);
4948 * This is most like caused by the users own
4949 * plugins updating the function. This overrides the
4952 pr_stat("override of function helper '%s'", name);
4953 remove_func_handler(pevent, name);
4956 func_handle = malloc_or_die(sizeof(*func_handle));
4957 memset(func_handle, 0, sizeof(*func_handle));
4959 func_handle->ret_type = ret_type;
4960 func_handle->name = strdup(name);
4961 func_handle->func = func;
4962 if (!func_handle->name)
4963 die("Failed to allocate function name");
4965 next_param = &(func_handle->params);
4968 type = va_arg(ap, enum pevent_func_arg_type);
4969 if (type == PEVENT_FUNC_ARG_VOID)
4972 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
4973 warning("Invalid argument type %d", type);
4977 param = malloc_or_die(sizeof(*param));
4981 *next_param = param;
4982 next_param = &(param->next);
4984 func_handle->nr_args++;
4988 func_handle->next = pevent->func_handlers;
4989 pevent->func_handlers = func_handle;
4994 free_func_handle(func_handle);
4999 * pevent_register_event_handle - register a way to parse an event
5000 * @pevent: the handle to the pevent
5001 * @id: the id of the event to register
5002 * @sys_name: the system name the event belongs to
5003 * @event_name: the name of the event
5004 * @func: the function to call to parse the event information
5006 * This function allows a developer to override the parsing of
5007 * a given event. If for some reason the default print format
5008 * is not sufficient, this function will register a function
5009 * for an event to be used to parse the data instead.
5011 * If @id is >= 0, then it is used to find the event.
5012 * else @sys_name and @event_name are used.
5014 int pevent_register_event_handler(struct pevent *pevent,
5015 int id, char *sys_name, char *event_name,
5016 pevent_event_handler_func func,
5019 struct event_format *event;
5020 struct event_handler *handle;
5024 event = pevent_find_event(pevent, id);
5027 if (event_name && (strcmp(event_name, event->name) != 0))
5029 if (sys_name && (strcmp(sys_name, event->system) != 0))
5032 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5037 pr_stat("overriding event (%d) %s:%s with new print handler",
5038 event->id, event->system, event->name);
5040 event->handler = func;
5041 event->context = context;
5045 /* Save for later use. */
5046 handle = malloc_or_die(sizeof(*handle));
5047 memset(handle, 0, sizeof(*handle));
5050 handle->event_name = strdup(event_name);
5052 handle->sys_name = strdup(sys_name);
5054 handle->func = func;
5055 handle->next = pevent->handlers;
5056 pevent->handlers = handle;
5057 handle->context = context;
5063 * pevent_alloc - create a pevent handle
5065 struct pevent *pevent_alloc(void)
5067 struct pevent *pevent;
5069 pevent = malloc(sizeof(*pevent));
5072 memset(pevent, 0, sizeof(*pevent));
5073 pevent->ref_count = 1;
5078 void pevent_ref(struct pevent *pevent)
5080 pevent->ref_count++;
5083 static void free_format_fields(struct format_field *field)
5085 struct format_field *next;
5096 static void free_formats(struct format *format)
5098 free_format_fields(format->common_fields);
5099 free_format_fields(format->fields);
5102 static void free_event(struct event_format *event)
5105 free(event->system);
5107 free_formats(&event->format);
5109 free(event->print_fmt.format);
5110 free_args(event->print_fmt.args);
5116 * pevent_free - free a pevent handle
5117 * @pevent: the pevent handle to free
5119 void pevent_free(struct pevent *pevent)
5121 struct cmdline_list *cmdlist, *cmdnext;
5122 struct func_list *funclist, *funcnext;
5123 struct printk_list *printklist, *printknext;
5124 struct pevent_function_handler *func_handler;
5125 struct event_handler *handle;
5131 cmdlist = pevent->cmdlist;
5132 funclist = pevent->funclist;
5133 printklist = pevent->printklist;
5135 pevent->ref_count--;
5136 if (pevent->ref_count)
5139 if (pevent->cmdlines) {
5140 for (i = 0; i < pevent->cmdline_count; i++)
5141 free(pevent->cmdlines[i].comm);
5142 free(pevent->cmdlines);
5146 cmdnext = cmdlist->next;
5147 free(cmdlist->comm);
5152 if (pevent->func_map) {
5153 for (i = 0; i < pevent->func_count; i++) {
5154 free(pevent->func_map[i].func);
5155 free(pevent->func_map[i].mod);
5157 free(pevent->func_map);
5161 funcnext = funclist->next;
5162 free(funclist->func);
5163 free(funclist->mod);
5165 funclist = funcnext;
5168 while (pevent->func_handlers) {
5169 func_handler = pevent->func_handlers;
5170 pevent->func_handlers = func_handler->next;
5171 free_func_handle(func_handler);
5174 if (pevent->printk_map) {
5175 for (i = 0; i < pevent->printk_count; i++)
5176 free(pevent->printk_map[i].printk);
5177 free(pevent->printk_map);
5180 while (printklist) {
5181 printknext = printklist->next;
5182 free(printklist->printk);
5184 printklist = printknext;
5187 for (i = 0; i < pevent->nr_events; i++)
5188 free_event(pevent->events[i]);
5190 while (pevent->handlers) {
5191 handle = pevent->handlers;
5192 pevent->handlers = handle->next;
5193 free_handler(handle);
5196 free(pevent->events);
5197 free(pevent->sort_events);
5202 void pevent_unref(struct pevent *pevent)
5204 pevent_free(pevent);