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 if (!item->func || (mod && !item->mod))
473 pevent->funclist = item;
474 pevent->func_count++;
480 * pevent_print_funcs - print out the stored functions
481 * @pevent: handle for the pevent
483 * This prints out the stored functions.
485 void pevent_print_funcs(struct pevent *pevent)
489 if (!pevent->func_map)
490 func_map_init(pevent);
492 for (i = 0; i < (int)pevent->func_count; i++) {
494 pevent->func_map[i].addr,
495 pevent->func_map[i].func);
496 if (pevent->func_map[i].mod)
497 printf(" [%s]\n", pevent->func_map[i].mod);
504 unsigned long long addr;
509 struct printk_list *next;
510 unsigned long long addr;
514 static int printk_cmp(const void *a, const void *b)
516 const struct printk_map *pa = a;
517 const struct printk_map *pb = b;
519 if (pa->addr < pb->addr)
521 if (pa->addr > pb->addr)
527 static void printk_map_init(struct pevent *pevent)
529 struct printk_list *printklist;
530 struct printk_list *item;
531 struct printk_map *printk_map;
534 printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1));
536 printklist = pevent->printklist;
540 printk_map[i].printk = printklist->printk;
541 printk_map[i].addr = printklist->addr;
544 printklist = printklist->next;
548 qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp);
550 pevent->printk_map = printk_map;
551 pevent->printklist = NULL;
554 static struct printk_map *
555 find_printk(struct pevent *pevent, unsigned long long addr)
557 struct printk_map *printk;
558 struct printk_map key;
560 if (!pevent->printk_map)
561 printk_map_init(pevent);
565 printk = bsearch(&key, pevent->printk_map, pevent->printk_count,
566 sizeof(*pevent->printk_map), printk_cmp);
572 * pevent_register_print_string - register a string by its address
573 * @pevent: handle for the pevent
574 * @fmt: the string format to register
575 * @addr: the address the string was located at
577 * This registers a string by the address it was stored in the kernel.
578 * The @fmt passed in is duplicated.
580 int pevent_register_print_string(struct pevent *pevent, char *fmt,
581 unsigned long long addr)
583 struct printk_list *item;
585 item = malloc_or_die(sizeof(*item));
587 item->next = pevent->printklist;
588 item->printk = strdup(fmt);
594 pevent->printklist = item;
595 pevent->printk_count++;
601 * pevent_print_printk - print out the stored strings
602 * @pevent: handle for the pevent
604 * This prints the string formats that were stored.
606 void pevent_print_printk(struct pevent *pevent)
610 if (!pevent->printk_map)
611 printk_map_init(pevent);
613 for (i = 0; i < (int)pevent->printk_count; i++) {
614 printf("%016llx %s\n",
615 pevent->printk_map[i].addr,
616 pevent->printk_map[i].printk);
620 static struct event_format *alloc_event(void)
622 struct event_format *event;
624 event = malloc(sizeof(*event));
627 memset(event, 0, sizeof(*event));
632 static void add_event(struct pevent *pevent, struct event_format *event)
636 pevent->events = realloc(pevent->events, sizeof(event) *
637 (pevent->nr_events + 1));
639 die("Can not allocate events");
641 for (i = 0; i < pevent->nr_events; i++) {
642 if (pevent->events[i]->id > event->id)
645 if (i < pevent->nr_events)
646 memmove(&pevent->events[i + 1],
648 sizeof(event) * (pevent->nr_events - i));
650 pevent->events[i] = event;
653 event->pevent = pevent;
656 static int event_item_type(enum event_type type)
659 case EVENT_ITEM ... EVENT_SQUOTE:
661 case EVENT_ERROR ... EVENT_DELIM:
667 static void free_flag_sym(struct print_flag_sym *fsym)
669 struct print_flag_sym *next;
680 static void free_arg(struct print_arg *arg)
682 struct print_arg *farg;
689 free(arg->atom.atom);
692 free(arg->field.name);
695 free_arg(arg->flags.field);
696 free(arg->flags.delim);
697 free_flag_sym(arg->flags.flags);
700 free_arg(arg->symbol.field);
701 free_flag_sym(arg->symbol.symbols);
704 free_arg(arg->hex.field);
705 free_arg(arg->hex.size);
708 free(arg->typecast.type);
709 free_arg(arg->typecast.item);
713 free(arg->string.string);
715 case PRINT_DYNAMIC_ARRAY:
716 free(arg->dynarray.index);
720 free_arg(arg->op.left);
721 free_arg(arg->op.right);
724 while (arg->func.args) {
725 farg = arg->func.args;
726 arg->func.args = farg->next;
739 static enum event_type get_type(int ch)
742 return EVENT_NEWLINE;
745 if (isalnum(ch) || ch == '_')
753 if (ch == '(' || ch == ')' || ch == ',')
759 static int __read_char(void)
761 if (input_buf_ptr >= input_buf_siz)
764 return input_buf[input_buf_ptr++];
767 static int __peek_char(void)
769 if (input_buf_ptr >= input_buf_siz)
772 return input_buf[input_buf_ptr];
776 * pevent_peek_char - peek at the next character that will be read
778 * Returns the next character read, or -1 if end of buffer.
780 int pevent_peek_char(void)
782 return __peek_char();
785 static int extend_token(char **tok, char *buf, int size)
787 char *newtok = realloc(*tok, size);
804 static enum event_type force_token(const char *str, char **tok);
806 static enum event_type __read_token(char **tok)
809 int ch, last_ch, quote_ch, next_ch;
812 enum event_type type;
822 if (type == EVENT_NONE)
830 *tok = malloc_or_die(2);
838 next_ch = __peek_char();
839 if (next_ch == '>') {
840 buf[i++] = __read_char();
853 buf[i++] = __read_char();
865 default: /* what should we do instead? */
875 buf[i++] = __read_char();
880 /* don't keep quotes */
886 if (i == (BUFSIZ - 1)) {
890 if (extend_token(tok, buf, tok_size) < 0)
897 /* the '\' '\' will cancel itself */
898 if (ch == '\\' && last_ch == '\\')
900 } while (ch != quote_ch || last_ch == '\\');
901 /* remove the last quote */
905 * For strings (double quotes) check the next token.
906 * If it is another string, concatinate the two.
908 if (type == EVENT_DQUOTE) {
909 unsigned long long save_input_buf_ptr = input_buf_ptr;
913 } while (isspace(ch));
916 input_buf_ptr = save_input_buf_ptr;
921 case EVENT_ERROR ... EVENT_SPACE:
927 while (get_type(__peek_char()) == type) {
928 if (i == (BUFSIZ - 1)) {
932 if (extend_token(tok, buf, tok_size) < 0)
942 if (extend_token(tok, buf, tok_size + i + 1) < 0)
945 if (type == EVENT_ITEM) {
947 * Older versions of the kernel has a bug that
948 * creates invalid symbols and will break the mac80211
949 * parsing. This is a work around to that bug.
951 * See Linux kernel commit:
952 * 811cb50baf63461ce0bdb234927046131fc7fa8b
954 if (strcmp(*tok, "LOCAL_PR_FMT") == 0) {
957 return force_token("\"\%s\" ", tok);
958 } else if (strcmp(*tok, "STA_PR_FMT") == 0) {
961 return force_token("\" sta:%pM\" ", tok);
962 } else if (strcmp(*tok, "VIF_PR_FMT") == 0) {
965 return force_token("\" vif:%p(%d)\" ", tok);
972 static enum event_type force_token(const char *str, char **tok)
974 const char *save_input_buf;
975 unsigned long long save_input_buf_ptr;
976 unsigned long long save_input_buf_siz;
977 enum event_type type;
979 /* save off the current input pointers */
980 save_input_buf = input_buf;
981 save_input_buf_ptr = input_buf_ptr;
982 save_input_buf_siz = input_buf_siz;
984 init_input_buf(str, strlen(str));
986 type = __read_token(tok);
988 /* reset back to original token */
989 input_buf = save_input_buf;
990 input_buf_ptr = save_input_buf_ptr;
991 input_buf_siz = save_input_buf_siz;
996 static void free_token(char *tok)
1002 static enum event_type read_token(char **tok)
1004 enum event_type type;
1007 type = __read_token(tok);
1008 if (type != EVENT_SPACE)
1020 * pevent_read_token - access to utilites to use the pevent parser
1021 * @tok: The token to return
1023 * This will parse tokens from the string given by
1024 * pevent_init_data().
1026 * Returns the token type.
1028 enum event_type pevent_read_token(char **tok)
1030 return read_token(tok);
1034 * pevent_free_token - free a token returned by pevent_read_token
1035 * @token: the token to free
1037 void pevent_free_token(char *token)
1043 static enum event_type read_token_item(char **tok)
1045 enum event_type type;
1048 type = __read_token(tok);
1049 if (type != EVENT_SPACE && type != EVENT_NEWLINE)
1060 static int test_type(enum event_type type, enum event_type expect)
1062 if (type != expect) {
1063 do_warning("Error: expected type %d but read %d",
1070 static int test_type_token(enum event_type type, const char *token,
1071 enum event_type expect, const char *expect_tok)
1073 if (type != expect) {
1074 do_warning("Error: expected type %d but read %d",
1079 if (strcmp(token, expect_tok) != 0) {
1080 do_warning("Error: expected '%s' but read '%s'",
1087 static int __read_expect_type(enum event_type expect, char **tok, int newline_ok)
1089 enum event_type type;
1092 type = read_token(tok);
1094 type = read_token_item(tok);
1095 return test_type(type, expect);
1098 static int read_expect_type(enum event_type expect, char **tok)
1100 return __read_expect_type(expect, tok, 1);
1103 static int __read_expected(enum event_type expect, const char *str,
1106 enum event_type type;
1111 type = read_token(&token);
1113 type = read_token_item(&token);
1115 ret = test_type_token(type, token, expect, str);
1122 static int read_expected(enum event_type expect, const char *str)
1124 return __read_expected(expect, str, 1);
1127 static int read_expected_item(enum event_type expect, const char *str)
1129 return __read_expected(expect, str, 0);
1132 static char *event_read_name(void)
1136 if (read_expected(EVENT_ITEM, "name") < 0)
1139 if (read_expected(EVENT_OP, ":") < 0)
1142 if (read_expect_type(EVENT_ITEM, &token) < 0)
1152 static int event_read_id(void)
1157 if (read_expected_item(EVENT_ITEM, "ID") < 0)
1160 if (read_expected(EVENT_OP, ":") < 0)
1163 if (read_expect_type(EVENT_ITEM, &token) < 0)
1166 id = strtoul(token, NULL, 0);
1175 static int field_is_string(struct format_field *field)
1177 if ((field->flags & FIELD_IS_ARRAY) &&
1178 (strstr(field->type, "char") || strstr(field->type, "u8") ||
1179 strstr(field->type, "s8")))
1185 static int field_is_dynamic(struct format_field *field)
1187 if (strncmp(field->type, "__data_loc", 10) == 0)
1193 static int field_is_long(struct format_field *field)
1195 /* includes long long */
1196 if (strstr(field->type, "long"))
1202 static int event_read_fields(struct event_format *event, struct format_field **fields)
1204 struct format_field *field = NULL;
1205 enum event_type type;
1211 type = read_token(&token);
1212 if (type == EVENT_NEWLINE) {
1219 if (test_type_token(type, token, EVENT_ITEM, "field"))
1223 type = read_token(&token);
1225 * The ftrace fields may still use the "special" name.
1228 if (event->flags & EVENT_FL_ISFTRACE &&
1229 type == EVENT_ITEM && strcmp(token, "special") == 0) {
1231 type = read_token(&token);
1234 if (test_type_token(type, token, EVENT_OP, ":") < 0)
1238 if (read_expect_type(EVENT_ITEM, &token) < 0)
1243 field = malloc_or_die(sizeof(*field));
1244 memset(field, 0, sizeof(*field));
1245 field->event = event;
1247 /* read the rest of the type */
1249 type = read_token(&token);
1250 if (type == EVENT_ITEM ||
1251 (type == EVENT_OP && strcmp(token, "*") == 0) ||
1253 * Some of the ftrace fields are broken and have
1254 * an illegal "." in them.
1256 (event->flags & EVENT_FL_ISFTRACE &&
1257 type == EVENT_OP && strcmp(token, ".") == 0)) {
1259 if (strcmp(token, "*") == 0)
1260 field->flags |= FIELD_IS_POINTER;
1264 new_type = realloc(field->type,
1265 strlen(field->type) +
1266 strlen(last_token) + 2);
1271 field->type = new_type;
1272 strcat(field->type, " ");
1273 strcat(field->type, last_token);
1276 field->type = last_token;
1285 die("no type found");
1288 field->name = last_token;
1290 if (test_type(type, EVENT_OP))
1293 if (strcmp(token, "[") == 0) {
1294 enum event_type last_type = type;
1295 char *brackets = token;
1299 field->flags |= FIELD_IS_ARRAY;
1301 type = read_token(&token);
1303 if (type == EVENT_ITEM)
1304 field->arraylen = strtoul(token, NULL, 0);
1306 field->arraylen = 0;
1308 while (strcmp(token, "]") != 0) {
1309 if (last_type == EVENT_ITEM &&
1316 new_brackets = realloc(brackets,
1318 strlen(token) + len);
1319 if (!new_brackets) {
1323 brackets = new_brackets;
1325 strcat(brackets, " ");
1326 strcat(brackets, token);
1327 /* We only care about the last token */
1328 field->arraylen = strtoul(token, NULL, 0);
1330 type = read_token(&token);
1331 if (type == EVENT_NONE) {
1332 die("failed to find token");
1339 new_brackets = realloc(brackets, strlen(brackets) + 2);
1340 if (!new_brackets) {
1344 brackets = new_brackets;
1345 strcat(brackets, "]");
1347 /* add brackets to type */
1349 type = read_token(&token);
1351 * If the next token is not an OP, then it is of
1352 * the format: type [] item;
1354 if (type == EVENT_ITEM) {
1356 new_type = realloc(field->type,
1357 strlen(field->type) +
1358 strlen(field->name) +
1359 strlen(brackets) + 2);
1364 field->type = new_type;
1365 strcat(field->type, " ");
1366 strcat(field->type, field->name);
1367 free_token(field->name);
1368 strcat(field->type, brackets);
1369 field->name = token;
1370 type = read_token(&token);
1373 new_type = realloc(field->type,
1374 strlen(field->type) +
1375 strlen(brackets) + 1);
1380 field->type = new_type;
1381 strcat(field->type, brackets);
1386 if (field_is_string(field))
1387 field->flags |= FIELD_IS_STRING;
1388 if (field_is_dynamic(field))
1389 field->flags |= FIELD_IS_DYNAMIC;
1390 if (field_is_long(field))
1391 field->flags |= FIELD_IS_LONG;
1393 if (test_type_token(type, token, EVENT_OP, ";"))
1397 if (read_expected(EVENT_ITEM, "offset") < 0)
1400 if (read_expected(EVENT_OP, ":") < 0)
1403 if (read_expect_type(EVENT_ITEM, &token))
1405 field->offset = strtoul(token, NULL, 0);
1408 if (read_expected(EVENT_OP, ";") < 0)
1411 if (read_expected(EVENT_ITEM, "size") < 0)
1414 if (read_expected(EVENT_OP, ":") < 0)
1417 if (read_expect_type(EVENT_ITEM, &token))
1419 field->size = strtoul(token, NULL, 0);
1422 if (read_expected(EVENT_OP, ";") < 0)
1425 type = read_token(&token);
1426 if (type != EVENT_NEWLINE) {
1427 /* newer versions of the kernel have a "signed" type */
1428 if (test_type_token(type, token, EVENT_ITEM, "signed"))
1433 if (read_expected(EVENT_OP, ":") < 0)
1436 if (read_expect_type(EVENT_ITEM, &token))
1439 /* add signed type */
1442 if (read_expected(EVENT_OP, ";") < 0)
1445 if (read_expect_type(EVENT_NEWLINE, &token))
1451 if (field->flags & FIELD_IS_ARRAY) {
1452 if (field->arraylen)
1453 field->elementsize = field->size / field->arraylen;
1454 else if (field->flags & FIELD_IS_STRING)
1455 field->elementsize = 1;
1457 field->elementsize = event->pevent->long_size;
1459 field->elementsize = field->size;
1462 fields = &field->next;
1479 static int event_read_format(struct event_format *event)
1484 if (read_expected_item(EVENT_ITEM, "format") < 0)
1487 if (read_expected(EVENT_OP, ":") < 0)
1490 if (read_expect_type(EVENT_NEWLINE, &token))
1494 ret = event_read_fields(event, &event->format.common_fields);
1497 event->format.nr_common = ret;
1499 ret = event_read_fields(event, &event->format.fields);
1502 event->format.nr_fields = ret;
1511 static enum event_type
1512 process_arg_token(struct event_format *event, struct print_arg *arg,
1513 char **tok, enum event_type type);
1515 static enum event_type
1516 process_arg(struct event_format *event, struct print_arg *arg, char **tok)
1518 enum event_type type;
1521 type = read_token(&token);
1524 return process_arg_token(event, arg, tok, type);
1527 static enum event_type
1528 process_op(struct event_format *event, struct print_arg *arg, char **tok);
1530 static enum event_type
1531 process_cond(struct event_format *event, struct print_arg *top, char **tok)
1533 struct print_arg *arg, *left, *right;
1534 enum event_type type;
1539 right = alloc_arg();
1541 arg->type = PRINT_OP;
1542 arg->op.left = left;
1543 arg->op.right = right;
1546 type = process_arg(event, left, &token);
1549 /* Handle other operations in the arguments */
1550 if (type == EVENT_OP && strcmp(token, ":") != 0) {
1551 type = process_op(event, left, &token);
1555 if (test_type_token(type, token, EVENT_OP, ":"))
1560 type = process_arg(event, right, &token);
1562 top->op.right = arg;
1568 /* Top may point to itself */
1569 top->op.right = NULL;
1575 static enum event_type
1576 process_array(struct event_format *event, struct print_arg *top, char **tok)
1578 struct print_arg *arg;
1579 enum event_type type;
1585 type = process_arg(event, arg, &token);
1586 if (test_type_token(type, token, EVENT_OP, "]"))
1589 top->op.right = arg;
1592 type = read_token_item(&token);
1604 static int get_op_prio(char *op)
1618 /* '>>' and '<<' are 8 */
1622 /* '==' and '!=' are 10 */
1632 do_warning("unknown op '%c'", op[0]);
1636 if (strcmp(op, "++") == 0 ||
1637 strcmp(op, "--") == 0) {
1639 } else if (strcmp(op, ">>") == 0 ||
1640 strcmp(op, "<<") == 0) {
1642 } else if (strcmp(op, ">=") == 0 ||
1643 strcmp(op, "<=") == 0) {
1645 } else if (strcmp(op, "==") == 0 ||
1646 strcmp(op, "!=") == 0) {
1648 } else if (strcmp(op, "&&") == 0) {
1650 } else if (strcmp(op, "||") == 0) {
1653 do_warning("unknown op '%s'", op);
1659 static int set_op_prio(struct print_arg *arg)
1662 /* single ops are the greatest */
1663 if (!arg->op.left || arg->op.left->type == PRINT_NULL)
1666 arg->op.prio = get_op_prio(arg->op.op);
1668 return arg->op.prio;
1671 /* Note, *tok does not get freed, but will most likely be saved */
1672 static enum event_type
1673 process_op(struct event_format *event, struct print_arg *arg, char **tok)
1675 struct print_arg *left, *right = NULL;
1676 enum event_type type;
1679 /* the op is passed in via tok */
1682 if (arg->type == PRINT_OP && !arg->op.left) {
1683 /* handle single op */
1685 die("bad op token %s", token);
1695 do_warning("bad op token %s", token);
1700 /* make an empty left */
1702 left->type = PRINT_NULL;
1703 arg->op.left = left;
1705 right = alloc_arg();
1706 arg->op.right = right;
1708 /* do not free the token, it belongs to an op */
1710 type = process_arg(event, right, tok);
1712 } else if (strcmp(token, "?") == 0) {
1715 /* copy the top arg to the left */
1718 arg->type = PRINT_OP;
1720 arg->op.left = left;
1723 type = process_cond(event, arg, tok);
1725 } else if (strcmp(token, ">>") == 0 ||
1726 strcmp(token, "<<") == 0 ||
1727 strcmp(token, "&") == 0 ||
1728 strcmp(token, "|") == 0 ||
1729 strcmp(token, "&&") == 0 ||
1730 strcmp(token, "||") == 0 ||
1731 strcmp(token, "-") == 0 ||
1732 strcmp(token, "+") == 0 ||
1733 strcmp(token, "*") == 0 ||
1734 strcmp(token, "^") == 0 ||
1735 strcmp(token, "/") == 0 ||
1736 strcmp(token, "<") == 0 ||
1737 strcmp(token, ">") == 0 ||
1738 strcmp(token, "==") == 0 ||
1739 strcmp(token, "!=") == 0) {
1743 /* copy the top arg to the left */
1746 arg->type = PRINT_OP;
1748 arg->op.left = left;
1750 if (set_op_prio(arg) == -1) {
1751 event->flags |= EVENT_FL_FAILED;
1752 /* arg->op.op (= token) will be freed at out_free */
1757 type = read_token_item(&token);
1760 /* could just be a type pointer */
1761 if ((strcmp(arg->op.op, "*") == 0) &&
1762 type == EVENT_DELIM && (strcmp(token, ")") == 0)) {
1765 if (left->type != PRINT_ATOM)
1766 die("bad pointer type");
1767 new_atom = realloc(left->atom.atom,
1768 strlen(left->atom.atom) + 3);
1772 left->atom.atom = new_atom;
1773 strcat(left->atom.atom, " *");
1781 right = alloc_arg();
1782 type = process_arg_token(event, right, tok, type);
1783 arg->op.right = right;
1785 } else if (strcmp(token, "[") == 0) {
1790 arg->type = PRINT_OP;
1792 arg->op.left = left;
1796 type = process_array(event, arg, tok);
1799 do_warning("unknown op '%s'", token);
1800 event->flags |= EVENT_FL_FAILED;
1801 /* the arg is now the left side */
1805 if (type == EVENT_OP && strcmp(*tok, ":") != 0) {
1808 /* higher prios need to be closer to the root */
1809 prio = get_op_prio(*tok);
1811 if (prio > arg->op.prio)
1812 return process_op(event, arg, tok);
1814 return process_op(event, right, tok);
1825 static enum event_type
1826 process_entry(struct event_format *event __unused, struct print_arg *arg,
1829 enum event_type type;
1833 if (read_expected(EVENT_OP, "->") < 0)
1836 if (read_expect_type(EVENT_ITEM, &token) < 0)
1840 arg->type = PRINT_FIELD;
1841 arg->field.name = field;
1843 if (is_flag_field) {
1844 arg->field.field = pevent_find_any_field(event, arg->field.name);
1845 arg->field.field->flags |= FIELD_IS_FLAG;
1847 } else if (is_symbolic_field) {
1848 arg->field.field = pevent_find_any_field(event, arg->field.name);
1849 arg->field.field->flags |= FIELD_IS_SYMBOLIC;
1850 is_symbolic_field = 0;
1853 type = read_token(&token);
1865 static char *arg_eval (struct print_arg *arg);
1867 static unsigned long long
1868 eval_type_str(unsigned long long val, const char *type, int pointer)
1878 if (type[len-1] != '*') {
1879 do_warning("pointer expected with non pointer type");
1883 ref = malloc_or_die(len);
1884 memcpy(ref, type, len);
1886 /* chop off the " *" */
1889 val = eval_type_str(val, ref, 0);
1894 /* check if this is a pointer */
1895 if (type[len - 1] == '*')
1898 /* Try to figure out the arg size*/
1899 if (strncmp(type, "struct", 6) == 0)
1903 if (strcmp(type, "u8") == 0)
1906 if (strcmp(type, "u16") == 0)
1907 return val & 0xffff;
1909 if (strcmp(type, "u32") == 0)
1910 return val & 0xffffffff;
1912 if (strcmp(type, "u64") == 0 ||
1913 strcmp(type, "s64"))
1916 if (strcmp(type, "s8") == 0)
1917 return (unsigned long long)(char)val & 0xff;
1919 if (strcmp(type, "s16") == 0)
1920 return (unsigned long long)(short)val & 0xffff;
1922 if (strcmp(type, "s32") == 0)
1923 return (unsigned long long)(int)val & 0xffffffff;
1925 if (strncmp(type, "unsigned ", 9) == 0) {
1930 if (strcmp(type, "char") == 0) {
1932 return (unsigned long long)(char)val & 0xff;
1937 if (strcmp(type, "short") == 0) {
1939 return (unsigned long long)(short)val & 0xffff;
1941 return val & 0xffff;
1944 if (strcmp(type, "int") == 0) {
1946 return (unsigned long long)(int)val & 0xffffffff;
1948 return val & 0xffffffff;
1955 * Try to figure out the type.
1957 static unsigned long long
1958 eval_type(unsigned long long val, struct print_arg *arg, int pointer)
1960 if (arg->type != PRINT_TYPE)
1961 die("expected type argument");
1963 return eval_type_str(val, arg->typecast.type, pointer);
1966 static int arg_num_eval(struct print_arg *arg, long long *val)
1968 long long left, right;
1971 switch (arg->type) {
1973 *val = strtoll(arg->atom.atom, NULL, 0);
1976 ret = arg_num_eval(arg->typecast.item, val);
1979 *val = eval_type(*val, arg, 0);
1982 switch (arg->op.op[0]) {
1984 ret = arg_num_eval(arg->op.left, &left);
1987 ret = arg_num_eval(arg->op.right, &right);
1991 *val = left || right;
1993 *val = left | right;
1996 ret = arg_num_eval(arg->op.left, &left);
1999 ret = arg_num_eval(arg->op.right, &right);
2003 *val = left && right;
2005 *val = left & right;
2008 ret = arg_num_eval(arg->op.left, &left);
2011 ret = arg_num_eval(arg->op.right, &right);
2014 switch (arg->op.op[1]) {
2016 *val = left < right;
2019 *val = left << right;
2022 *val = left <= right;
2025 do_warning("unknown op '%s'", arg->op.op);
2030 ret = arg_num_eval(arg->op.left, &left);
2033 ret = arg_num_eval(arg->op.right, &right);
2036 switch (arg->op.op[1]) {
2038 *val = left > right;
2041 *val = left >> right;
2044 *val = left >= right;
2047 do_warning("unknown op '%s'", arg->op.op);
2052 ret = arg_num_eval(arg->op.left, &left);
2055 ret = arg_num_eval(arg->op.right, &right);
2059 if (arg->op.op[1] != '=') {
2060 do_warning("unknown op '%s'", arg->op.op);
2063 *val = left == right;
2066 ret = arg_num_eval(arg->op.left, &left);
2069 ret = arg_num_eval(arg->op.right, &right);
2073 switch (arg->op.op[1]) {
2075 *val = left != right;
2078 do_warning("unknown op '%s'", arg->op.op);
2083 /* check for negative */
2084 if (arg->op.left->type == PRINT_NULL)
2087 ret = arg_num_eval(arg->op.left, &left);
2090 ret = arg_num_eval(arg->op.right, &right);
2093 *val = left - right;
2096 if (arg->op.left->type == PRINT_NULL)
2099 ret = arg_num_eval(arg->op.left, &left);
2102 ret = arg_num_eval(arg->op.right, &right);
2105 *val = left + right;
2108 do_warning("unknown op '%s'", arg->op.op);
2114 case PRINT_FIELD ... PRINT_SYMBOL:
2118 do_warning("invalid eval type %d", arg->type);
2125 static char *arg_eval (struct print_arg *arg)
2128 static char buf[20];
2130 switch (arg->type) {
2132 return arg->atom.atom;
2134 return arg_eval(arg->typecast.item);
2136 if (!arg_num_eval(arg, &val))
2138 sprintf(buf, "%lld", val);
2142 case PRINT_FIELD ... PRINT_SYMBOL:
2146 die("invalid eval type %d", arg->type);
2153 static enum event_type
2154 process_fields(struct event_format *event, struct print_flag_sym **list, char **tok)
2156 enum event_type type;
2157 struct print_arg *arg = NULL;
2158 struct print_flag_sym *field;
2164 type = read_token_item(&token);
2165 if (test_type_token(type, token, EVENT_OP, "{"))
2171 type = process_arg(event, arg, &token);
2173 if (type == EVENT_OP)
2174 type = process_op(event, arg, &token);
2176 if (type == EVENT_ERROR)
2179 if (test_type_token(type, token, EVENT_DELIM, ","))
2182 field = malloc_or_die(sizeof(*field));
2183 memset(field, 0, sizeof(*field));
2185 value = arg_eval(arg);
2188 field->value = strdup(value);
2189 if (field->value == NULL)
2196 type = process_arg(event, arg, &token);
2197 if (test_type_token(type, token, EVENT_OP, "}"))
2200 value = arg_eval(arg);
2203 field->str = strdup(value);
2204 if (field->str == NULL)
2210 list = &field->next;
2213 type = read_token_item(&token);
2214 } while (type == EVENT_DELIM && strcmp(token, ",") == 0);
2227 static enum event_type
2228 process_flags(struct event_format *event, struct print_arg *arg, char **tok)
2230 struct print_arg *field;
2231 enum event_type type;
2234 memset(arg, 0, sizeof(*arg));
2235 arg->type = PRINT_FLAGS;
2237 field = alloc_arg();
2239 type = process_arg(event, field, &token);
2241 /* Handle operations in the first argument */
2242 while (type == EVENT_OP)
2243 type = process_op(event, field, &token);
2245 if (test_type_token(type, token, EVENT_DELIM, ","))
2249 arg->flags.field = field;
2251 type = read_token_item(&token);
2252 if (event_item_type(type)) {
2253 arg->flags.delim = token;
2254 type = read_token_item(&token);
2257 if (test_type_token(type, token, EVENT_DELIM, ","))
2260 type = process_fields(event, &arg->flags.flags, &token);
2261 if (test_type_token(type, token, EVENT_DELIM, ")"))
2265 type = read_token_item(tok);
2274 static enum event_type
2275 process_symbols(struct event_format *event, struct print_arg *arg, char **tok)
2277 struct print_arg *field;
2278 enum event_type type;
2281 memset(arg, 0, sizeof(*arg));
2282 arg->type = PRINT_SYMBOL;
2284 field = alloc_arg();
2286 type = process_arg(event, field, &token);
2287 if (test_type_token(type, token, EVENT_DELIM, ","))
2290 arg->symbol.field = field;
2292 type = process_fields(event, &arg->symbol.symbols, &token);
2293 if (test_type_token(type, token, EVENT_DELIM, ")"))
2297 type = read_token_item(tok);
2306 static enum event_type
2307 process_hex(struct event_format *event, struct print_arg *arg, char **tok)
2309 struct print_arg *field;
2310 enum event_type type;
2313 memset(arg, 0, sizeof(*arg));
2314 arg->type = PRINT_HEX;
2316 field = alloc_arg();
2317 type = process_arg(event, field, &token);
2319 if (test_type_token(type, token, EVENT_DELIM, ","))
2322 arg->hex.field = field;
2326 field = alloc_arg();
2327 type = process_arg(event, field, &token);
2329 if (test_type_token(type, token, EVENT_DELIM, ")"))
2332 arg->hex.size = field;
2335 type = read_token_item(tok);
2345 static enum event_type
2346 process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok)
2348 struct format_field *field;
2349 enum event_type type;
2352 memset(arg, 0, sizeof(*arg));
2353 arg->type = PRINT_DYNAMIC_ARRAY;
2356 * The item within the parenthesis is another field that holds
2357 * the index into where the array starts.
2359 type = read_token(&token);
2361 if (type != EVENT_ITEM)
2364 /* Find the field */
2366 field = pevent_find_field(event, token);
2370 arg->dynarray.field = field;
2371 arg->dynarray.index = 0;
2373 if (read_expected(EVENT_DELIM, ")") < 0)
2377 type = read_token_item(&token);
2379 if (type != EVENT_OP || strcmp(token, "[") != 0)
2384 type = process_arg(event, arg, &token);
2385 if (type == EVENT_ERROR)
2388 if (!test_type_token(type, token, EVENT_OP, "]"))
2392 type = read_token_item(tok);
2403 static enum event_type
2404 process_paren(struct event_format *event, struct print_arg *arg, char **tok)
2406 struct print_arg *item_arg;
2407 enum event_type type;
2410 type = process_arg(event, arg, &token);
2412 if (type == EVENT_ERROR)
2415 if (type == EVENT_OP)
2416 type = process_op(event, arg, &token);
2418 if (type == EVENT_ERROR)
2421 if (test_type_token(type, token, EVENT_DELIM, ")"))
2425 type = read_token_item(&token);
2428 * If the next token is an item or another open paren, then
2429 * this was a typecast.
2431 if (event_item_type(type) ||
2432 (type == EVENT_DELIM && strcmp(token, "(") == 0)) {
2434 /* make this a typecast and contine */
2436 /* prevous must be an atom */
2437 if (arg->type != PRINT_ATOM)
2438 die("previous needed to be PRINT_ATOM");
2440 item_arg = alloc_arg();
2442 arg->type = PRINT_TYPE;
2443 arg->typecast.type = arg->atom.atom;
2444 arg->typecast.item = item_arg;
2445 type = process_arg_token(event, item_arg, &token, type);
2459 static enum event_type
2460 process_str(struct event_format *event __unused, struct print_arg *arg, char **tok)
2462 enum event_type type;
2465 if (read_expect_type(EVENT_ITEM, &token) < 0)
2468 arg->type = PRINT_STRING;
2469 arg->string.string = token;
2470 arg->string.offset = -1;
2472 if (read_expected(EVENT_DELIM, ")") < 0)
2475 type = read_token(&token);
2487 static struct pevent_function_handler *
2488 find_func_handler(struct pevent *pevent, char *func_name)
2490 struct pevent_function_handler *func;
2492 for (func = pevent->func_handlers; func; func = func->next) {
2493 if (strcmp(func->name, func_name) == 0)
2500 static void remove_func_handler(struct pevent *pevent, char *func_name)
2502 struct pevent_function_handler *func;
2503 struct pevent_function_handler **next;
2505 next = &pevent->func_handlers;
2506 while ((func = *next)) {
2507 if (strcmp(func->name, func_name) == 0) {
2509 free_func_handle(func);
2516 static enum event_type
2517 process_func_handler(struct event_format *event, struct pevent_function_handler *func,
2518 struct print_arg *arg, char **tok)
2520 struct print_arg **next_arg;
2521 struct print_arg *farg;
2522 enum event_type type;
2527 arg->type = PRINT_FUNC;
2528 arg->func.func = func;
2532 next_arg = &(arg->func.args);
2533 for (i = 0; i < func->nr_args; i++) {
2535 type = process_arg(event, farg, &token);
2536 if (i < (func->nr_args - 1))
2541 if (test_type_token(type, token, EVENT_DELIM, test)) {
2548 next_arg = &(farg->next);
2552 type = read_token(&token);
2558 static enum event_type
2559 process_function(struct event_format *event, struct print_arg *arg,
2560 char *token, char **tok)
2562 struct pevent_function_handler *func;
2564 if (strcmp(token, "__print_flags") == 0) {
2567 return process_flags(event, arg, tok);
2569 if (strcmp(token, "__print_symbolic") == 0) {
2571 is_symbolic_field = 1;
2572 return process_symbols(event, arg, tok);
2574 if (strcmp(token, "__print_hex") == 0) {
2576 return process_hex(event, arg, tok);
2578 if (strcmp(token, "__get_str") == 0) {
2580 return process_str(event, arg, tok);
2582 if (strcmp(token, "__get_dynamic_array") == 0) {
2584 return process_dynamic_array(event, arg, tok);
2587 func = find_func_handler(event->pevent, token);
2590 return process_func_handler(event, func, arg, tok);
2593 do_warning("function %s not defined", token);
2598 static enum event_type
2599 process_arg_token(struct event_format *event, struct print_arg *arg,
2600 char **tok, enum event_type type)
2609 if (strcmp(token, "REC") == 0) {
2611 type = process_entry(event, arg, &token);
2615 /* test the next token */
2616 type = read_token_item(&token);
2619 * If the next token is a parenthesis, then this
2622 if (type == EVENT_DELIM && strcmp(token, "(") == 0) {
2625 /* this will free atom. */
2626 type = process_function(event, arg, atom, &token);
2629 /* atoms can be more than one token long */
2630 while (type == EVENT_ITEM) {
2632 new_atom = realloc(atom,
2633 strlen(atom) + strlen(token) + 2);
2642 strcat(atom, token);
2644 type = read_token_item(&token);
2647 arg->type = PRINT_ATOM;
2648 arg->atom.atom = atom;
2653 arg->type = PRINT_ATOM;
2654 arg->atom.atom = token;
2655 type = read_token_item(&token);
2658 if (strcmp(token, "(") == 0) {
2660 type = process_paren(event, arg, &token);
2664 /* handle single ops */
2665 arg->type = PRINT_OP;
2667 arg->op.left = NULL;
2668 type = process_op(event, arg, &token);
2670 /* On error, the op is freed */
2671 if (type == EVENT_ERROR)
2674 /* return error type if errored */
2677 case EVENT_ERROR ... EVENT_NEWLINE:
2679 die("unexpected type %d", type);
2686 static int event_read_print_args(struct event_format *event, struct print_arg **list)
2688 enum event_type type = EVENT_ERROR;
2689 struct print_arg *arg;
2694 if (type == EVENT_NEWLINE) {
2695 type = read_token_item(&token);
2701 type = process_arg(event, arg, &token);
2703 if (type == EVENT_ERROR) {
2712 if (type == EVENT_OP) {
2713 type = process_op(event, arg, &token);
2715 if (type == EVENT_ERROR) {
2724 if (type == EVENT_DELIM && strcmp(token, ",") == 0) {
2731 } while (type != EVENT_NONE);
2733 if (type != EVENT_NONE && type != EVENT_ERROR)
2739 static int event_read_print(struct event_format *event)
2741 enum event_type type;
2745 if (read_expected_item(EVENT_ITEM, "print") < 0)
2748 if (read_expected(EVENT_ITEM, "fmt") < 0)
2751 if (read_expected(EVENT_OP, ":") < 0)
2754 if (read_expect_type(EVENT_DQUOTE, &token) < 0)
2758 event->print_fmt.format = token;
2759 event->print_fmt.args = NULL;
2761 /* ok to have no arg */
2762 type = read_token_item(&token);
2764 if (type == EVENT_NONE)
2767 /* Handle concatenation of print lines */
2768 if (type == EVENT_DQUOTE) {
2771 cat = malloc_or_die(strlen(event->print_fmt.format) +
2773 strcpy(cat, event->print_fmt.format);
2776 free_token(event->print_fmt.format);
2777 event->print_fmt.format = NULL;
2782 if (test_type_token(type, token, EVENT_DELIM, ","))
2787 ret = event_read_print_args(event, &event->print_fmt.args);
2799 * pevent_find_common_field - return a common field by event
2800 * @event: handle for the event
2801 * @name: the name of the common field to return
2803 * Returns a common field from the event by the given @name.
2804 * This only searchs the common fields and not all field.
2806 struct format_field *
2807 pevent_find_common_field(struct event_format *event, const char *name)
2809 struct format_field *format;
2811 for (format = event->format.common_fields;
2812 format; format = format->next) {
2813 if (strcmp(format->name, name) == 0)
2821 * pevent_find_field - find a non-common field
2822 * @event: handle for the event
2823 * @name: the name of the non-common field
2825 * Returns a non-common field by the given @name.
2826 * This does not search common fields.
2828 struct format_field *
2829 pevent_find_field(struct event_format *event, const char *name)
2831 struct format_field *format;
2833 for (format = event->format.fields;
2834 format; format = format->next) {
2835 if (strcmp(format->name, name) == 0)
2843 * pevent_find_any_field - find any field by name
2844 * @event: handle for the event
2845 * @name: the name of the field
2847 * Returns a field by the given @name.
2848 * This searchs the common field names first, then
2849 * the non-common ones if a common one was not found.
2851 struct format_field *
2852 pevent_find_any_field(struct event_format *event, const char *name)
2854 struct format_field *format;
2856 format = pevent_find_common_field(event, name);
2859 return pevent_find_field(event, name);
2863 * pevent_read_number - read a number from data
2864 * @pevent: handle for the pevent
2865 * @ptr: the raw data
2866 * @size: the size of the data that holds the number
2868 * Returns the number (converted to host) from the
2871 unsigned long long pevent_read_number(struct pevent *pevent,
2872 const void *ptr, int size)
2876 return *(unsigned char *)ptr;
2878 return data2host2(pevent, ptr);
2880 return data2host4(pevent, ptr);
2882 return data2host8(pevent, ptr);
2890 * pevent_read_number_field - read a number from data
2891 * @field: a handle to the field
2892 * @data: the raw data to read
2893 * @value: the value to place the number in
2895 * Reads raw data according to a field offset and size,
2896 * and translates it into @value.
2898 * Returns 0 on success, -1 otherwise.
2900 int pevent_read_number_field(struct format_field *field, const void *data,
2901 unsigned long long *value)
2905 switch (field->size) {
2910 *value = pevent_read_number(field->event->pevent,
2911 data + field->offset, field->size);
2918 static int get_common_info(struct pevent *pevent,
2919 const char *type, int *offset, int *size)
2921 struct event_format *event;
2922 struct format_field *field;
2925 * All events should have the same common elements.
2926 * Pick any event to find where the type is;
2928 if (!pevent->events)
2929 die("no event_list!");
2931 event = pevent->events[0];
2932 field = pevent_find_common_field(event, type);
2936 *offset = field->offset;
2937 *size = field->size;
2942 static int __parse_common(struct pevent *pevent, void *data,
2943 int *size, int *offset, const char *name)
2948 ret = get_common_info(pevent, name, offset, size);
2952 return pevent_read_number(pevent, data + *offset, *size);
2955 static int trace_parse_common_type(struct pevent *pevent, void *data)
2957 return __parse_common(pevent, data,
2958 &pevent->type_size, &pevent->type_offset,
2962 static int parse_common_pid(struct pevent *pevent, void *data)
2964 return __parse_common(pevent, data,
2965 &pevent->pid_size, &pevent->pid_offset,
2969 static int parse_common_pc(struct pevent *pevent, void *data)
2971 return __parse_common(pevent, data,
2972 &pevent->pc_size, &pevent->pc_offset,
2973 "common_preempt_count");
2976 static int parse_common_flags(struct pevent *pevent, void *data)
2978 return __parse_common(pevent, data,
2979 &pevent->flags_size, &pevent->flags_offset,
2983 static int parse_common_lock_depth(struct pevent *pevent, void *data)
2985 return __parse_common(pevent, data,
2986 &pevent->ld_size, &pevent->ld_offset,
2987 "common_lock_depth");
2990 static int parse_common_migrate_disable(struct pevent *pevent, void *data)
2992 return __parse_common(pevent, data,
2993 &pevent->ld_size, &pevent->ld_offset,
2994 "common_migrate_disable");
2997 static int events_id_cmp(const void *a, const void *b);
3000 * pevent_find_event - find an event by given id
3001 * @pevent: a handle to the pevent
3002 * @id: the id of the event
3004 * Returns an event that has a given @id.
3006 struct event_format *pevent_find_event(struct pevent *pevent, int id)
3008 struct event_format **eventptr;
3009 struct event_format key;
3010 struct event_format *pkey = &key;
3012 /* Check cache first */
3013 if (pevent->last_event && pevent->last_event->id == id)
3014 return pevent->last_event;
3018 eventptr = bsearch(&pkey, pevent->events, pevent->nr_events,
3019 sizeof(*pevent->events), events_id_cmp);
3022 pevent->last_event = *eventptr;
3030 * pevent_find_event_by_name - find an event by given name
3031 * @pevent: a handle to the pevent
3032 * @sys: the system name to search for
3033 * @name: the name of the event to search for
3035 * This returns an event with a given @name and under the system
3036 * @sys. If @sys is NULL the first event with @name is returned.
3038 struct event_format *
3039 pevent_find_event_by_name(struct pevent *pevent,
3040 const char *sys, const char *name)
3042 struct event_format *event;
3045 if (pevent->last_event &&
3046 strcmp(pevent->last_event->name, name) == 0 &&
3047 (!sys || strcmp(pevent->last_event->system, sys) == 0))
3048 return pevent->last_event;
3050 for (i = 0; i < pevent->nr_events; i++) {
3051 event = pevent->events[i];
3052 if (strcmp(event->name, name) == 0) {
3055 if (strcmp(event->system, sys) == 0)
3059 if (i == pevent->nr_events)
3062 pevent->last_event = event;
3066 static unsigned long long
3067 eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg)
3069 struct pevent *pevent = event->pevent;
3070 unsigned long long val = 0;
3071 unsigned long long left, right;
3072 struct print_arg *typearg = NULL;
3073 struct print_arg *larg;
3074 unsigned long offset;
3075 unsigned int field_size;
3077 switch (arg->type) {
3082 return strtoull(arg->atom.atom, NULL, 0);
3084 if (!arg->field.field) {
3085 arg->field.field = pevent_find_any_field(event, arg->field.name);
3086 if (!arg->field.field)
3087 die("field %s not found", arg->field.name);
3089 /* must be a number */
3090 val = pevent_read_number(pevent, data + arg->field.field->offset,
3091 arg->field.field->size);
3098 val = eval_num_arg(data, size, event, arg->typecast.item);
3099 return eval_type(val, arg, 0);
3106 val = process_defined_func(&s, data, size, event, arg);
3107 trace_seq_destroy(&s);
3111 if (strcmp(arg->op.op, "[") == 0) {
3113 * Arrays are special, since we don't want
3114 * to read the arg as is.
3116 right = eval_num_arg(data, size, event, arg->op.right);
3118 /* handle typecasts */
3119 larg = arg->op.left;
3120 while (larg->type == PRINT_TYPE) {
3123 larg = larg->typecast.item;
3126 /* Default to long size */
3127 field_size = pevent->long_size;
3129 switch (larg->type) {
3130 case PRINT_DYNAMIC_ARRAY:
3131 offset = pevent_read_number(pevent,
3132 data + larg->dynarray.field->offset,
3133 larg->dynarray.field->size);
3134 if (larg->dynarray.field->elementsize)
3135 field_size = larg->dynarray.field->elementsize;
3137 * The actual length of the dynamic array is stored
3138 * in the top half of the field, and the offset
3139 * is in the bottom half of the 32 bit field.
3145 if (!larg->field.field) {
3147 pevent_find_any_field(event, larg->field.name);
3148 if (!larg->field.field)
3149 die("field %s not found", larg->field.name);
3151 field_size = larg->field.field->elementsize;
3152 offset = larg->field.field->offset +
3153 right * larg->field.field->elementsize;
3156 goto default_op; /* oops, all bets off */
3158 val = pevent_read_number(pevent,
3159 data + offset, field_size);
3161 val = eval_type(val, typearg, 1);
3163 } else if (strcmp(arg->op.op, "?") == 0) {
3164 left = eval_num_arg(data, size, event, arg->op.left);
3165 arg = arg->op.right;
3167 val = eval_num_arg(data, size, event, arg->op.left);
3169 val = eval_num_arg(data, size, event, arg->op.right);
3173 left = eval_num_arg(data, size, event, arg->op.left);
3174 right = eval_num_arg(data, size, event, arg->op.right);
3175 switch (arg->op.op[0]) {
3177 switch (arg->op.op[1]) {
3182 val = left != right;
3185 die("unknown op '%s'", arg->op.op);
3193 val = left || right;
3199 val = left && right;
3204 switch (arg->op.op[1]) {
3209 val = left << right;
3212 val = left <= right;
3215 die("unknown op '%s'", arg->op.op);
3219 switch (arg->op.op[1]) {
3224 val = left >> right;
3227 val = left >= right;
3230 die("unknown op '%s'", arg->op.op);
3234 if (arg->op.op[1] != '=')
3235 die("unknown op '%s'", arg->op.op);
3236 val = left == right;
3251 die("unknown op '%s'", arg->op.op);
3254 default: /* not sure what to do there */
3262 unsigned long long value;
3265 static const struct flag flags[] = {
3266 { "HI_SOFTIRQ", 0 },
3267 { "TIMER_SOFTIRQ", 1 },
3268 { "NET_TX_SOFTIRQ", 2 },
3269 { "NET_RX_SOFTIRQ", 3 },
3270 { "BLOCK_SOFTIRQ", 4 },
3271 { "BLOCK_IOPOLL_SOFTIRQ", 5 },
3272 { "TASKLET_SOFTIRQ", 6 },
3273 { "SCHED_SOFTIRQ", 7 },
3274 { "HRTIMER_SOFTIRQ", 8 },
3275 { "RCU_SOFTIRQ", 9 },
3277 { "HRTIMER_NORESTART", 0 },
3278 { "HRTIMER_RESTART", 1 },
3281 static unsigned long long eval_flag(const char *flag)
3286 * Some flags in the format files do not get converted.
3287 * If the flag is not numeric, see if it is something that
3288 * we already know about.
3290 if (isdigit(flag[0]))
3291 return strtoull(flag, NULL, 0);
3293 for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++)
3294 if (strcmp(flags[i].name, flag) == 0)
3295 return flags[i].value;
3300 static void print_str_to_seq(struct trace_seq *s, const char *format,
3301 int len_arg, const char *str)
3304 trace_seq_printf(s, format, len_arg, str);
3306 trace_seq_printf(s, format, str);
3309 static void print_str_arg(struct trace_seq *s, void *data, int size,
3310 struct event_format *event, const char *format,
3311 int len_arg, struct print_arg *arg)
3313 struct pevent *pevent = event->pevent;
3314 struct print_flag_sym *flag;
3315 struct format_field *field;
3316 unsigned long long val, fval;
3323 switch (arg->type) {
3328 print_str_to_seq(s, format, len_arg, arg->atom.atom);
3331 field = arg->field.field;
3333 field = pevent_find_any_field(event, arg->field.name);
3335 die("field %s not found", arg->field.name);
3336 arg->field.field = field;
3338 /* Zero sized fields, mean the rest of the data */
3339 len = field->size ? : size - field->offset;
3342 * Some events pass in pointers. If this is not an array
3343 * and the size is the same as long_size, assume that it
3346 if (!(field->flags & FIELD_IS_ARRAY) &&
3347 field->size == pevent->long_size) {
3348 addr = *(unsigned long *)(data + field->offset);
3349 trace_seq_printf(s, "%lx", addr);
3352 str = malloc_or_die(len + 1);
3353 memcpy(str, data + field->offset, len);
3355 print_str_to_seq(s, format, len_arg, str);
3359 val = eval_num_arg(data, size, event, arg->flags.field);
3361 for (flag = arg->flags.flags; flag; flag = flag->next) {
3362 fval = eval_flag(flag->value);
3363 if (!val && !fval) {
3364 print_str_to_seq(s, format, len_arg, flag->str);
3367 if (fval && (val & fval) == fval) {
3368 if (print && arg->flags.delim)
3369 trace_seq_puts(s, arg->flags.delim);
3370 print_str_to_seq(s, format, len_arg, flag->str);
3377 val = eval_num_arg(data, size, event, arg->symbol.field);
3378 for (flag = arg->symbol.symbols; flag; flag = flag->next) {
3379 fval = eval_flag(flag->value);
3381 print_str_to_seq(s, format, len_arg, flag->str);
3387 field = arg->hex.field->field.field;
3389 str = arg->hex.field->field.name;
3390 field = pevent_find_any_field(event, str);
3392 die("field %s not found", str);
3393 arg->hex.field->field.field = field;
3395 hex = data + field->offset;
3396 len = eval_num_arg(data, size, event, arg->hex.size);
3397 for (i = 0; i < len; i++) {
3399 trace_seq_putc(s, ' ');
3400 trace_seq_printf(s, "%02x", hex[i]);
3406 case PRINT_STRING: {
3409 if (arg->string.offset == -1) {
3410 struct format_field *f;
3412 f = pevent_find_any_field(event, arg->string.string);
3413 arg->string.offset = f->offset;
3415 str_offset = data2host4(pevent, data + arg->string.offset);
3416 str_offset &= 0xffff;
3417 print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset);
3421 print_str_to_seq(s, format, len_arg, arg->string.string);
3425 * The only op for string should be ? :
3427 if (arg->op.op[0] != '?')
3429 val = eval_num_arg(data, size, event, arg->op.left);
3431 print_str_arg(s, data, size, event,
3432 format, len_arg, arg->op.right->op.left);
3434 print_str_arg(s, data, size, event,
3435 format, len_arg, arg->op.right->op.right);
3438 process_defined_func(s, data, size, event, arg);
3446 static unsigned long long
3447 process_defined_func(struct trace_seq *s, void *data, int size,
3448 struct event_format *event, struct print_arg *arg)
3450 struct pevent_function_handler *func_handle = arg->func.func;
3451 struct pevent_func_params *param;
3452 unsigned long long *args;
3453 unsigned long long ret;
3454 struct print_arg *farg;
3455 struct trace_seq str;
3457 struct save_str *next;
3459 } *strings = NULL, *string;
3462 if (!func_handle->nr_args) {
3463 ret = (*func_handle->func)(s, NULL);
3467 farg = arg->func.args;
3468 param = func_handle->params;
3470 args = malloc_or_die(sizeof(*args) * func_handle->nr_args);
3471 for (i = 0; i < func_handle->nr_args; i++) {
3472 switch (param->type) {
3473 case PEVENT_FUNC_ARG_INT:
3474 case PEVENT_FUNC_ARG_LONG:
3475 case PEVENT_FUNC_ARG_PTR:
3476 args[i] = eval_num_arg(data, size, event, farg);
3478 case PEVENT_FUNC_ARG_STRING:
3479 trace_seq_init(&str);
3480 print_str_arg(&str, data, size, event, "%s", -1, farg);
3481 trace_seq_terminate(&str);
3482 string = malloc_or_die(sizeof(*string));
3483 string->next = strings;
3484 string->str = strdup(str.buffer);
3488 args[i] = (unsigned long long)string->str;
3490 trace_seq_destroy(&str);
3494 * Something went totally wrong, this is not
3495 * an input error, something in this code broke.
3497 die("Unexpected end of arguments\n");
3501 param = param->next;
3504 ret = (*func_handle->func)(s, args);
3508 strings = string->next;
3514 /* TBD : handle return type here */
3518 static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event)
3520 struct pevent *pevent = event->pevent;
3521 struct format_field *field, *ip_field;
3522 struct print_arg *args, *arg, **next;
3523 unsigned long long ip, val;
3528 field = pevent->bprint_buf_field;
3529 ip_field = pevent->bprint_ip_field;
3532 field = pevent_find_field(event, "buf");
3534 die("can't find buffer field for binary printk");
3535 ip_field = pevent_find_field(event, "ip");
3537 die("can't find ip field for binary printk");
3538 pevent->bprint_buf_field = field;
3539 pevent->bprint_ip_field = ip_field;
3542 ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size);
3545 * The first arg is the IP pointer.
3552 arg->type = PRINT_ATOM;
3553 arg->atom.atom = malloc_or_die(32);
3554 sprintf(arg->atom.atom, "%lld", ip);
3556 /* skip the first "%pf : " */
3557 for (ptr = fmt + 6, bptr = data + field->offset;
3558 bptr < data + size && *ptr; ptr++) {
3589 vsize = pevent->long_size;
3603 /* the pointers are always 4 bytes aligned */
3604 bptr = (void *)(((unsigned long)bptr + 3) &
3606 val = pevent_read_number(pevent, bptr, vsize);
3610 arg->type = PRINT_ATOM;
3611 arg->atom.atom = malloc_or_die(32);
3612 sprintf(arg->atom.atom, "%lld", val);
3616 * The '*' case means that an arg is used as the length.
3617 * We need to continue to figure out for what.
3626 arg->type = PRINT_BSTRING;
3627 arg->string.string = strdup(bptr);
3628 if (!arg->string.string)
3630 bptr += strlen(bptr) + 1;
3642 static void free_args(struct print_arg *args)
3644 struct print_arg *next;
3655 get_bprint_format(void *data, int size __unused, struct event_format *event)
3657 struct pevent *pevent = event->pevent;
3658 unsigned long long addr;
3659 struct format_field *field;
3660 struct printk_map *printk;
3664 field = pevent->bprint_fmt_field;
3667 field = pevent_find_field(event, "fmt");
3669 die("can't find format field for binary printk");
3670 pevent->bprint_fmt_field = field;
3673 addr = pevent_read_number(pevent, data + field->offset, field->size);
3675 printk = find_printk(pevent, addr);
3677 format = malloc_or_die(45);
3678 sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n",
3684 /* Remove any quotes. */
3687 format = malloc_or_die(strlen(p) + 10);
3688 sprintf(format, "%s : %s", "%pf", p);
3689 /* remove ending quotes and new line since we will add one too */
3690 p = format + strlen(format) - 1;
3695 if (strcmp(p, "\\n") == 0)
3701 static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,
3702 struct event_format *event, struct print_arg *arg)
3705 char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x";
3707 if (arg->type == PRINT_FUNC) {
3708 process_defined_func(s, data, size, event, arg);
3712 if (arg->type != PRINT_FIELD) {
3713 trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d",
3719 fmt = "%.2x%.2x%.2x%.2x%.2x%.2x";
3720 if (!arg->field.field) {
3722 pevent_find_any_field(event, arg->field.name);
3723 if (!arg->field.field)
3724 die("field %s not found", arg->field.name);
3726 if (arg->field.field->size != 6) {
3727 trace_seq_printf(s, "INVALIDMAC");
3730 buf = data + arg->field.field->offset;
3731 trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
3734 static int is_printable_array(char *p, unsigned int len)
3738 for (i = 0; i < len && p[i]; i++)
3744 static void print_event_fields(struct trace_seq *s, void *data, int size,
3745 struct event_format *event)
3747 struct format_field *field;
3748 unsigned long long val;
3749 unsigned int offset, len, i;
3751 field = event->format.fields;
3753 trace_seq_printf(s, " %s=", field->name);
3754 if (field->flags & FIELD_IS_ARRAY) {
3755 offset = field->offset;
3757 if (field->flags & FIELD_IS_DYNAMIC) {
3758 val = pevent_read_number(event->pevent, data + offset, len);
3763 if (field->flags & FIELD_IS_STRING &&
3764 is_printable_array(data + offset, len)) {
3765 trace_seq_printf(s, "%s", (char *)data + offset);
3767 trace_seq_puts(s, "ARRAY[");
3768 for (i = 0; i < len; i++) {
3770 trace_seq_puts(s, ", ");
3771 trace_seq_printf(s, "%02x",
3772 *((unsigned char *)data + offset + i));
3774 trace_seq_putc(s, ']');
3775 field->flags &= ~FIELD_IS_STRING;
3778 val = pevent_read_number(event->pevent, data + field->offset,
3780 if (field->flags & FIELD_IS_POINTER) {
3781 trace_seq_printf(s, "0x%llx", val);
3782 } else if (field->flags & FIELD_IS_SIGNED) {
3783 switch (field->size) {
3786 * If field is long then print it in hex.
3787 * A long usually stores pointers.
3789 if (field->flags & FIELD_IS_LONG)
3790 trace_seq_printf(s, "0x%x", (int)val);
3792 trace_seq_printf(s, "%d", (int)val);
3795 trace_seq_printf(s, "%2d", (short)val);
3798 trace_seq_printf(s, "%1d", (char)val);
3801 trace_seq_printf(s, "%lld", val);
3804 if (field->flags & FIELD_IS_LONG)
3805 trace_seq_printf(s, "0x%llx", val);
3807 trace_seq_printf(s, "%llu", val);
3810 field = field->next;
3814 static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event)
3816 struct pevent *pevent = event->pevent;
3817 struct print_fmt *print_fmt = &event->print_fmt;
3818 struct print_arg *arg = print_fmt->args;
3819 struct print_arg *args = NULL;
3820 const char *ptr = print_fmt->format;
3821 unsigned long long val;
3822 struct func_map *func;
3823 const char *saveptr;
3824 char *bprint_fmt = NULL;
3832 if (event->flags & EVENT_FL_FAILED) {
3833 trace_seq_printf(s, "[FAILED TO PARSE]");
3834 print_event_fields(s, data, size, event);
3838 if (event->flags & EVENT_FL_ISBPRINT) {
3839 bprint_fmt = get_bprint_format(data, size, event);
3840 args = make_bprint_args(bprint_fmt, data, size, event);
3845 for (; *ptr; ptr++) {
3851 trace_seq_putc(s, '\n');
3854 trace_seq_putc(s, '\t');
3857 trace_seq_putc(s, '\r');
3860 trace_seq_putc(s, '\\');
3863 trace_seq_putc(s, *ptr);
3867 } else if (*ptr == '%') {
3875 trace_seq_putc(s, '%');
3878 /* FIXME: need to handle properly */
3890 /* The argument is the length. */
3892 die("no argument match");
3893 len_arg = eval_num_arg(data, size, event, arg);
3903 if (pevent->long_size == 4)
3908 if (*(ptr+1) == 'F' ||
3912 } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') {
3913 print_mac_arg(s, *(ptr+1), data, size, event, arg);
3926 die("no argument match");
3928 len = ((unsigned long)ptr + 1) -
3929 (unsigned long)saveptr;
3931 /* should never happen */
3935 memcpy(format, saveptr, len);
3938 val = eval_num_arg(data, size, event, arg);
3942 func = find_func(pevent, val);
3944 trace_seq_puts(s, func->func);
3945 if (show_func == 'F')
3952 if (pevent->long_size == 8 && ls &&
3953 sizeof(long) != 8) {
3957 /* make %l into %ll */
3958 p = strchr(format, 'l');
3960 memmove(p+1, p, strlen(p)+1);
3961 else if (strcmp(format, "%p") == 0)
3962 strcpy(format, "0x%llx");
3967 trace_seq_printf(s, format, len_arg, (char)val);
3969 trace_seq_printf(s, format, (char)val);
3973 trace_seq_printf(s, format, len_arg, (short)val);
3975 trace_seq_printf(s, format, (short)val);
3979 trace_seq_printf(s, format, len_arg, (int)val);
3981 trace_seq_printf(s, format, (int)val);
3985 trace_seq_printf(s, format, len_arg, (long)val);
3987 trace_seq_printf(s, format, (long)val);
3991 trace_seq_printf(s, format, len_arg,
3994 trace_seq_printf(s, format, (long long)val);
3997 die("bad count (%d)", ls);
4002 die("no matching argument");
4004 len = ((unsigned long)ptr + 1) -
4005 (unsigned long)saveptr;
4007 /* should never happen */
4011 memcpy(format, saveptr, len);
4015 print_str_arg(s, data, size, event,
4016 format, len_arg, arg);
4020 trace_seq_printf(s, ">%c<", *ptr);
4024 trace_seq_putc(s, *ptr);
4034 * pevent_data_lat_fmt - parse the data for the latency format
4035 * @pevent: a handle to the pevent
4036 * @s: the trace_seq to write to
4037 * @record: the record to read from
4039 * This parses out the Latency format (interrupts disabled,
4040 * need rescheduling, in hard/soft interrupt, preempt count
4041 * and lock depth) and places it into the trace_seq.
4043 void pevent_data_lat_fmt(struct pevent *pevent,
4044 struct trace_seq *s, struct pevent_record *record)
4046 static int check_lock_depth = 1;
4047 static int check_migrate_disable = 1;
4048 static int lock_depth_exists;
4049 static int migrate_disable_exists;
4050 unsigned int lat_flags;
4053 int migrate_disable;
4056 void *data = record->data;
4058 lat_flags = parse_common_flags(pevent, data);
4059 pc = parse_common_pc(pevent, data);
4060 /* lock_depth may not always exist */
4061 if (lock_depth_exists)
4062 lock_depth = parse_common_lock_depth(pevent, data);
4063 else if (check_lock_depth) {
4064 lock_depth = parse_common_lock_depth(pevent, data);
4066 check_lock_depth = 0;
4068 lock_depth_exists = 1;
4071 /* migrate_disable may not always exist */
4072 if (migrate_disable_exists)
4073 migrate_disable = parse_common_migrate_disable(pevent, data);
4074 else if (check_migrate_disable) {
4075 migrate_disable = parse_common_migrate_disable(pevent, data);
4076 if (migrate_disable < 0)
4077 check_migrate_disable = 0;
4079 migrate_disable_exists = 1;
4082 hardirq = lat_flags & TRACE_FLAG_HARDIRQ;
4083 softirq = lat_flags & TRACE_FLAG_SOFTIRQ;
4085 trace_seq_printf(s, "%c%c%c",
4086 (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
4087 (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
4089 (lat_flags & TRACE_FLAG_NEED_RESCHED) ?
4091 (hardirq && softirq) ? 'H' :
4092 hardirq ? 'h' : softirq ? 's' : '.');
4095 trace_seq_printf(s, "%x", pc);
4097 trace_seq_putc(s, '.');
4099 if (migrate_disable_exists) {
4100 if (migrate_disable < 0)
4101 trace_seq_putc(s, '.');
4103 trace_seq_printf(s, "%d", migrate_disable);
4106 if (lock_depth_exists) {
4108 trace_seq_putc(s, '.');
4110 trace_seq_printf(s, "%d", lock_depth);
4113 trace_seq_terminate(s);
4117 * pevent_data_type - parse out the given event type
4118 * @pevent: a handle to the pevent
4119 * @rec: the record to read from
4121 * This returns the event id from the @rec.
4123 int pevent_data_type(struct pevent *pevent, struct pevent_record *rec)
4125 return trace_parse_common_type(pevent, rec->data);
4129 * pevent_data_event_from_type - find the event by a given type
4130 * @pevent: a handle to the pevent
4131 * @type: the type of the event.
4133 * This returns the event form a given @type;
4135 struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type)
4137 return pevent_find_event(pevent, type);
4141 * pevent_data_pid - parse the PID from raw data
4142 * @pevent: a handle to the pevent
4143 * @rec: the record to parse
4145 * This returns the PID from a raw data.
4147 int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec)
4149 return parse_common_pid(pevent, rec->data);
4153 * pevent_data_comm_from_pid - return the command line from PID
4154 * @pevent: a handle to the pevent
4155 * @pid: the PID of the task to search for
4157 * This returns a pointer to the command line that has the given
4160 const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid)
4164 comm = find_cmdline(pevent, pid);
4169 * pevent_data_comm_from_pid - parse the data into the print format
4170 * @s: the trace_seq to write to
4171 * @event: the handle to the event
4172 * @record: the record to read from
4174 * This parses the raw @data using the given @event information and
4175 * writes the print format into the trace_seq.
4177 void pevent_event_info(struct trace_seq *s, struct event_format *event,
4178 struct pevent_record *record)
4180 int print_pretty = 1;
4182 if (event->pevent->print_raw)
4183 print_event_fields(s, record->data, record->size, event);
4187 print_pretty = event->handler(s, record, event,
4191 pretty_print(s, record->data, record->size, event);
4194 trace_seq_terminate(s);
4197 void pevent_print_event(struct pevent *pevent, struct trace_seq *s,
4198 struct pevent_record *record)
4200 static char *spaces = " "; /* 20 spaces */
4201 struct event_format *event;
4203 unsigned long usecs;
4204 unsigned long nsecs;
4206 void *data = record->data;
4212 secs = record->ts / NSECS_PER_SEC;
4213 nsecs = record->ts - secs * NSECS_PER_SEC;
4215 if (record->size < 0) {
4216 do_warning("ug! negative record size %d", record->size);
4220 type = trace_parse_common_type(pevent, data);
4222 event = pevent_find_event(pevent, type);
4224 do_warning("ug! no event found for type %d", type);
4228 pid = parse_common_pid(pevent, data);
4229 comm = find_cmdline(pevent, pid);
4231 if (pevent->latency_format) {
4232 trace_seq_printf(s, "%8.8s-%-5d %3d",
4233 comm, pid, record->cpu);
4234 pevent_data_lat_fmt(pevent, s, record);
4236 trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu);
4238 if (pevent->flags & PEVENT_NSEC_OUTPUT) {
4242 usecs = (nsecs + 500) / NSECS_PER_USEC;
4246 trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name);
4248 /* Space out the event names evenly. */
4249 len = strlen(event->name);
4251 trace_seq_printf(s, "%.*s", 20 - len, spaces);
4253 pevent_event_info(s, event, record);
4256 static int events_id_cmp(const void *a, const void *b)
4258 struct event_format * const * ea = a;
4259 struct event_format * const * eb = b;
4261 if ((*ea)->id < (*eb)->id)
4264 if ((*ea)->id > (*eb)->id)
4270 static int events_name_cmp(const void *a, const void *b)
4272 struct event_format * const * ea = a;
4273 struct event_format * const * eb = b;
4276 res = strcmp((*ea)->name, (*eb)->name);
4280 res = strcmp((*ea)->system, (*eb)->system);
4284 return events_id_cmp(a, b);
4287 static int events_system_cmp(const void *a, const void *b)
4289 struct event_format * const * ea = a;
4290 struct event_format * const * eb = b;
4293 res = strcmp((*ea)->system, (*eb)->system);
4297 res = strcmp((*ea)->name, (*eb)->name);
4301 return events_id_cmp(a, b);
4304 struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type)
4306 struct event_format **events;
4307 int (*sort)(const void *a, const void *b);
4309 events = pevent->sort_events;
4311 if (events && pevent->last_type == sort_type)
4315 events = malloc(sizeof(*events) * (pevent->nr_events + 1));
4319 memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events);
4320 events[pevent->nr_events] = NULL;
4322 pevent->sort_events = events;
4324 /* the internal events are sorted by id */
4325 if (sort_type == EVENT_SORT_ID) {
4326 pevent->last_type = sort_type;
4331 switch (sort_type) {
4333 sort = events_id_cmp;
4335 case EVENT_SORT_NAME:
4336 sort = events_name_cmp;
4338 case EVENT_SORT_SYSTEM:
4339 sort = events_system_cmp;
4345 qsort(events, pevent->nr_events, sizeof(*events), sort);
4346 pevent->last_type = sort_type;
4351 static struct format_field **
4352 get_event_fields(const char *type, const char *name,
4353 int count, struct format_field *list)
4355 struct format_field **fields;
4356 struct format_field *field;
4359 fields = malloc_or_die(sizeof(*fields) * (count + 1));
4360 for (field = list; field; field = field->next) {
4361 fields[i++] = field;
4362 if (i == count + 1) {
4363 do_warning("event %s has more %s fields than specified",
4371 do_warning("event %s has less %s fields than specified",
4380 * pevent_event_common_fields - return a list of common fields for an event
4381 * @event: the event to return the common fields of.
4383 * Returns an allocated array of fields. The last item in the array is NULL.
4384 * The array must be freed with free().
4386 struct format_field **pevent_event_common_fields(struct event_format *event)
4388 return get_event_fields("common", event->name,
4389 event->format.nr_common,
4390 event->format.common_fields);
4394 * pevent_event_fields - return a list of event specific fields for an event
4395 * @event: the event to return the fields of.
4397 * Returns an allocated array of fields. The last item in the array is NULL.
4398 * The array must be freed with free().
4400 struct format_field **pevent_event_fields(struct event_format *event)
4402 return get_event_fields("event", event->name,
4403 event->format.nr_fields,
4404 event->format.fields);
4407 static void print_fields(struct trace_seq *s, struct print_flag_sym *field)
4409 trace_seq_printf(s, "{ %s, %s }", field->value, field->str);
4411 trace_seq_puts(s, ", ");
4412 print_fields(s, field->next);
4417 static void print_args(struct print_arg *args)
4419 int print_paren = 1;
4422 switch (args->type) {
4427 printf("%s", args->atom.atom);
4430 printf("REC->%s", args->field.name);
4433 printf("__print_flags(");
4434 print_args(args->flags.field);
4435 printf(", %s, ", args->flags.delim);
4437 print_fields(&s, args->flags.flags);
4438 trace_seq_do_printf(&s);
4439 trace_seq_destroy(&s);
4443 printf("__print_symbolic(");
4444 print_args(args->symbol.field);
4447 print_fields(&s, args->symbol.symbols);
4448 trace_seq_do_printf(&s);
4449 trace_seq_destroy(&s);
4453 printf("__print_hex(");
4454 print_args(args->hex.field);
4456 print_args(args->hex.size);
4461 printf("__get_str(%s)", args->string.string);
4464 printf("(%s)", args->typecast.type);
4465 print_args(args->typecast.item);
4468 if (strcmp(args->op.op, ":") == 0)
4472 print_args(args->op.left);
4473 printf(" %s ", args->op.op);
4474 print_args(args->op.right);
4479 /* we should warn... */
4484 print_args(args->next);
4488 static void parse_header_field(const char *field,
4489 int *offset, int *size, int mandatory)
4491 unsigned long long save_input_buf_ptr;
4492 unsigned long long save_input_buf_siz;
4496 save_input_buf_ptr = input_buf_ptr;
4497 save_input_buf_siz = input_buf_siz;
4499 if (read_expected(EVENT_ITEM, "field") < 0)
4501 if (read_expected(EVENT_OP, ":") < 0)
4505 if (read_expect_type(EVENT_ITEM, &token) < 0)
4510 * If this is not a mandatory field, then test it first.
4513 if (read_expected(EVENT_ITEM, field) < 0)
4516 if (read_expect_type(EVENT_ITEM, &token) < 0)
4518 if (strcmp(token, field) != 0)
4523 if (read_expected(EVENT_OP, ";") < 0)
4525 if (read_expected(EVENT_ITEM, "offset") < 0)
4527 if (read_expected(EVENT_OP, ":") < 0)
4529 if (read_expect_type(EVENT_ITEM, &token) < 0)
4531 *offset = atoi(token);
4533 if (read_expected(EVENT_OP, ";") < 0)
4535 if (read_expected(EVENT_ITEM, "size") < 0)
4537 if (read_expected(EVENT_OP, ":") < 0)
4539 if (read_expect_type(EVENT_ITEM, &token) < 0)
4541 *size = atoi(token);
4543 if (read_expected(EVENT_OP, ";") < 0)
4545 type = read_token(&token);
4546 if (type != EVENT_NEWLINE) {
4547 /* newer versions of the kernel have a "signed" type */
4548 if (type != EVENT_ITEM)
4551 if (strcmp(token, "signed") != 0)
4556 if (read_expected(EVENT_OP, ":") < 0)
4559 if (read_expect_type(EVENT_ITEM, &token))
4563 if (read_expected(EVENT_OP, ";") < 0)
4566 if (read_expect_type(EVENT_NEWLINE, &token))
4574 input_buf_ptr = save_input_buf_ptr;
4575 input_buf_siz = save_input_buf_siz;
4582 * pevent_parse_header_page - parse the data stored in the header page
4583 * @pevent: the handle to the pevent
4584 * @buf: the buffer storing the header page format string
4585 * @size: the size of @buf
4586 * @long_size: the long size to use if there is no header
4588 * This parses the header page format for information on the
4589 * ring buffer used. The @buf should be copied from
4591 * /sys/kernel/debug/tracing/events/header_page
4593 int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,
4600 * Old kernels did not have header page info.
4601 * Sorry but we just use what we find here in user space.
4603 pevent->header_page_ts_size = sizeof(long long);
4604 pevent->header_page_size_size = long_size;
4605 pevent->header_page_data_offset = sizeof(long long) + long_size;
4606 pevent->old_format = 1;
4609 init_input_buf(buf, size);
4611 parse_header_field("timestamp", &pevent->header_page_ts_offset,
4612 &pevent->header_page_ts_size, 1);
4613 parse_header_field("commit", &pevent->header_page_size_offset,
4614 &pevent->header_page_size_size, 1);
4615 parse_header_field("overwrite", &pevent->header_page_overwrite,
4617 parse_header_field("data", &pevent->header_page_data_offset,
4618 &pevent->header_page_data_size, 1);
4623 static int event_matches(struct event_format *event,
4624 int id, const char *sys_name,
4625 const char *event_name)
4627 if (id >= 0 && id != event->id)
4630 if (event_name && (strcmp(event_name, event->name) != 0))
4633 if (sys_name && (strcmp(sys_name, event->system) != 0))
4639 static void free_handler(struct event_handler *handle)
4641 free((void *)handle->sys_name);
4642 free((void *)handle->event_name);
4646 static int find_event_handle(struct pevent *pevent, struct event_format *event)
4648 struct event_handler *handle, **next;
4650 for (next = &pevent->handlers; *next;
4651 next = &(*next)->next) {
4653 if (event_matches(event, handle->id,
4655 handle->event_name))
4662 pr_stat("overriding event (%d) %s:%s with new print handler",
4663 event->id, event->system, event->name);
4665 event->handler = handle->func;
4666 event->context = handle->context;
4668 *next = handle->next;
4669 free_handler(handle);
4675 * pevent_parse_event - parse the event format
4676 * @pevent: the handle to the pevent
4677 * @buf: the buffer storing the event format string
4678 * @size: the size of @buf
4679 * @sys: the system the event belongs to
4681 * This parses the event format and creates an event structure
4682 * to quickly parse raw data for a given event.
4684 * These files currently come from:
4686 * /sys/kernel/debug/tracing/events/.../.../format
4688 int pevent_parse_event(struct pevent *pevent,
4689 const char *buf, unsigned long size,
4692 struct event_format *event;
4695 init_input_buf(buf, size);
4697 event = alloc_event();
4701 event->name = event_read_name();
4708 if (strcmp(sys, "ftrace") == 0) {
4710 event->flags |= EVENT_FL_ISFTRACE;
4712 if (strcmp(event->name, "bprint") == 0)
4713 event->flags |= EVENT_FL_ISBPRINT;
4716 event->id = event_read_id();
4718 die("failed to read event id");
4720 event->system = strdup(sys);
4722 die("failed to allocate system");
4724 /* Add pevent to event so that it can be referenced */
4725 event->pevent = pevent;
4727 ret = event_read_format(event);
4729 do_warning("failed to read event format for %s", event->name);
4734 * If the event has an override, don't print warnings if the event
4735 * print format fails to parse.
4737 if (find_event_handle(pevent, event))
4740 ret = event_read_print(event);
4742 do_warning("failed to read event print fmt for %s",
4749 add_event(pevent, event);
4751 if (!ret && (event->flags & EVENT_FL_ISFTRACE)) {
4752 struct format_field *field;
4753 struct print_arg *arg, **list;
4755 /* old ftrace had no args */
4757 list = &event->print_fmt.args;
4758 for (field = event->format.fields; field; field = field->next) {
4762 arg->type = PRINT_FIELD;
4763 arg->field.name = strdup(field->name);
4764 if (!arg->field.name) {
4765 do_warning("failed to allocate field name");
4766 event->flags |= EVENT_FL_FAILED;
4769 arg->field.field = field;
4774 #define PRINT_ARGS 0
4775 if (PRINT_ARGS && event->print_fmt.args)
4776 print_args(event->print_fmt.args);
4781 event->flags |= EVENT_FL_FAILED;
4782 /* still add it even if it failed */
4783 add_event(pevent, event);
4787 int get_field_val(struct trace_seq *s, struct format_field *field,
4788 const char *name, struct pevent_record *record,
4789 unsigned long long *val, int err)
4793 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4797 if (pevent_read_number_field(field, record->data, val)) {
4799 trace_seq_printf(s, " %s=INVALID", name);
4807 * pevent_get_field_raw - return the raw pointer into the data field
4808 * @s: The seq to print to on error
4809 * @event: the event that the field is for
4810 * @name: The name of the field
4811 * @record: The record with the field name.
4812 * @len: place to store the field length.
4813 * @err: print default error if failed.
4815 * Returns a pointer into record->data of the field and places
4816 * the length of the field in @len.
4818 * On failure, it returns NULL.
4820 void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event,
4821 const char *name, struct pevent_record *record,
4824 struct format_field *field;
4825 void *data = record->data;
4832 field = pevent_find_field(event, name);
4836 trace_seq_printf(s, "<CANT FIND FIELD %s>", name);
4840 /* Allow @len to be NULL */
4844 offset = field->offset;
4845 if (field->flags & FIELD_IS_DYNAMIC) {
4846 offset = pevent_read_number(event->pevent,
4847 data + offset, field->size);
4848 *len = offset >> 16;
4853 return data + offset;
4857 * pevent_get_field_val - find a field and return its value
4858 * @s: The seq to print to on error
4859 * @event: the event that the field is for
4860 * @name: The name of the field
4861 * @record: The record with the field name.
4862 * @val: place to store the value of the field.
4863 * @err: print default error if failed.
4865 * Returns 0 on success -1 on field not found.
4867 int pevent_get_field_val(struct trace_seq *s, struct event_format *event,
4868 const char *name, struct pevent_record *record,
4869 unsigned long long *val, int err)
4871 struct format_field *field;
4876 field = pevent_find_field(event, name);
4878 return get_field_val(s, field, name, record, val, err);
4882 * pevent_get_common_field_val - find a common field and return its value
4883 * @s: The seq to print to on error
4884 * @event: the event that the field is for
4885 * @name: The name of the field
4886 * @record: The record with the field name.
4887 * @val: place to store the value of the field.
4888 * @err: print default error if failed.
4890 * Returns 0 on success -1 on field not found.
4892 int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event,
4893 const char *name, struct pevent_record *record,
4894 unsigned long long *val, int err)
4896 struct format_field *field;
4901 field = pevent_find_common_field(event, name);
4903 return get_field_val(s, field, name, record, val, err);
4907 * pevent_get_any_field_val - find a any field and return its value
4908 * @s: The seq to print to on error
4909 * @event: the event that the field is for
4910 * @name: The name of the field
4911 * @record: The record with the field name.
4912 * @val: place to store the value of the field.
4913 * @err: print default error if failed.
4915 * Returns 0 on success -1 on field not found.
4917 int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event,
4918 const char *name, struct pevent_record *record,
4919 unsigned long long *val, int err)
4921 struct format_field *field;
4926 field = pevent_find_any_field(event, name);
4928 return get_field_val(s, field, name, record, val, err);
4932 * pevent_print_num_field - print a field and a format
4933 * @s: The seq to print to
4934 * @fmt: The printf format to print the field with.
4935 * @event: the event that the field is for
4936 * @name: The name of the field
4937 * @record: The record with the field name.
4938 * @err: print default error if failed.
4940 * Returns: 0 on success, -1 field not found, or 1 if buffer is full.
4942 int pevent_print_num_field(struct trace_seq *s, const char *fmt,
4943 struct event_format *event, const char *name,
4944 struct pevent_record *record, int err)
4946 struct format_field *field = pevent_find_field(event, name);
4947 unsigned long long val;
4952 if (pevent_read_number_field(field, record->data, &val))
4955 return trace_seq_printf(s, fmt, val);
4959 trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name);
4963 static void free_func_handle(struct pevent_function_handler *func)
4965 struct pevent_func_params *params;
4969 while (func->params) {
4970 params = func->params;
4971 func->params = params->next;
4979 * pevent_register_print_function - register a helper function
4980 * @pevent: the handle to the pevent
4981 * @func: the function to process the helper function
4982 * @ret_type: the return type of the helper function
4983 * @name: the name of the helper function
4984 * @parameters: A list of enum pevent_func_arg_type
4986 * Some events may have helper functions in the print format arguments.
4987 * This allows a plugin to dynamically create a way to process one
4988 * of these functions.
4990 * The @parameters is a variable list of pevent_func_arg_type enums that
4991 * must end with PEVENT_FUNC_ARG_VOID.
4993 int pevent_register_print_function(struct pevent *pevent,
4994 pevent_func_handler func,
4995 enum pevent_func_arg_type ret_type,
4998 struct pevent_function_handler *func_handle;
4999 struct pevent_func_params **next_param;
5000 struct pevent_func_params *param;
5001 enum pevent_func_arg_type type;
5004 func_handle = find_func_handler(pevent, name);
5007 * This is most like caused by the users own
5008 * plugins updating the function. This overrides the
5011 pr_stat("override of function helper '%s'", name);
5012 remove_func_handler(pevent, name);
5015 func_handle = malloc_or_die(sizeof(*func_handle));
5016 memset(func_handle, 0, sizeof(*func_handle));
5018 func_handle->ret_type = ret_type;
5019 func_handle->name = strdup(name);
5020 func_handle->func = func;
5021 if (!func_handle->name)
5022 die("Failed to allocate function name");
5024 next_param = &(func_handle->params);
5027 type = va_arg(ap, enum pevent_func_arg_type);
5028 if (type == PEVENT_FUNC_ARG_VOID)
5031 if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) {
5032 warning("Invalid argument type %d", type);
5036 param = malloc_or_die(sizeof(*param));
5040 *next_param = param;
5041 next_param = &(param->next);
5043 func_handle->nr_args++;
5047 func_handle->next = pevent->func_handlers;
5048 pevent->func_handlers = func_handle;
5053 free_func_handle(func_handle);
5058 * pevent_register_event_handler - register a way to parse an event
5059 * @pevent: the handle to the pevent
5060 * @id: the id of the event to register
5061 * @sys_name: the system name the event belongs to
5062 * @event_name: the name of the event
5063 * @func: the function to call to parse the event information
5064 * @context: the data to be passed to @func
5066 * This function allows a developer to override the parsing of
5067 * a given event. If for some reason the default print format
5068 * is not sufficient, this function will register a function
5069 * for an event to be used to parse the data instead.
5071 * If @id is >= 0, then it is used to find the event.
5072 * else @sys_name and @event_name are used.
5074 int pevent_register_event_handler(struct pevent *pevent,
5075 int id, char *sys_name, char *event_name,
5076 pevent_event_handler_func func,
5079 struct event_format *event;
5080 struct event_handler *handle;
5084 event = pevent_find_event(pevent, id);
5087 if (event_name && (strcmp(event_name, event->name) != 0))
5089 if (sys_name && (strcmp(sys_name, event->system) != 0))
5092 event = pevent_find_event_by_name(pevent, sys_name, event_name);
5097 pr_stat("overriding event (%d) %s:%s with new print handler",
5098 event->id, event->system, event->name);
5100 event->handler = func;
5101 event->context = context;
5105 /* Save for later use. */
5106 handle = malloc_or_die(sizeof(*handle));
5107 memset(handle, 0, sizeof(*handle));
5110 handle->event_name = strdup(event_name);
5112 handle->sys_name = strdup(sys_name);
5114 if ((event_name && !handle->event_name) ||
5115 (sys_name && !handle->sys_name)) {
5116 die("Failed to allocate event/sys name");
5119 handle->func = func;
5120 handle->next = pevent->handlers;
5121 pevent->handlers = handle;
5122 handle->context = context;
5128 * pevent_alloc - create a pevent handle
5130 struct pevent *pevent_alloc(void)
5132 struct pevent *pevent;
5134 pevent = malloc(sizeof(*pevent));
5137 memset(pevent, 0, sizeof(*pevent));
5138 pevent->ref_count = 1;
5143 void pevent_ref(struct pevent *pevent)
5145 pevent->ref_count++;
5148 static void free_format_fields(struct format_field *field)
5150 struct format_field *next;
5161 static void free_formats(struct format *format)
5163 free_format_fields(format->common_fields);
5164 free_format_fields(format->fields);
5167 static void free_event(struct event_format *event)
5170 free(event->system);
5172 free_formats(&event->format);
5174 free(event->print_fmt.format);
5175 free_args(event->print_fmt.args);
5181 * pevent_free - free a pevent handle
5182 * @pevent: the pevent handle to free
5184 void pevent_free(struct pevent *pevent)
5186 struct cmdline_list *cmdlist, *cmdnext;
5187 struct func_list *funclist, *funcnext;
5188 struct printk_list *printklist, *printknext;
5189 struct pevent_function_handler *func_handler;
5190 struct event_handler *handle;
5196 cmdlist = pevent->cmdlist;
5197 funclist = pevent->funclist;
5198 printklist = pevent->printklist;
5200 pevent->ref_count--;
5201 if (pevent->ref_count)
5204 if (pevent->cmdlines) {
5205 for (i = 0; i < pevent->cmdline_count; i++)
5206 free(pevent->cmdlines[i].comm);
5207 free(pevent->cmdlines);
5211 cmdnext = cmdlist->next;
5212 free(cmdlist->comm);
5217 if (pevent->func_map) {
5218 for (i = 0; i < pevent->func_count; i++) {
5219 free(pevent->func_map[i].func);
5220 free(pevent->func_map[i].mod);
5222 free(pevent->func_map);
5226 funcnext = funclist->next;
5227 free(funclist->func);
5228 free(funclist->mod);
5230 funclist = funcnext;
5233 while (pevent->func_handlers) {
5234 func_handler = pevent->func_handlers;
5235 pevent->func_handlers = func_handler->next;
5236 free_func_handle(func_handler);
5239 if (pevent->printk_map) {
5240 for (i = 0; i < pevent->printk_count; i++)
5241 free(pevent->printk_map[i].printk);
5242 free(pevent->printk_map);
5245 while (printklist) {
5246 printknext = printklist->next;
5247 free(printklist->printk);
5249 printklist = printknext;
5252 for (i = 0; i < pevent->nr_events; i++)
5253 free_event(pevent->events[i]);
5255 while (pevent->handlers) {
5256 handle = pevent->handlers;
5257 pevent->handlers = handle->next;
5258 free_handler(handle);
5261 free(pevent->events);
5262 free(pevent->sort_events);
5267 void pevent_unref(struct pevent *pevent)
5269 pevent_free(pevent);