Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> |
| 3 | * |
| 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!) |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | * |
| 19 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 20 | * |
| 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. |
| 26 | */ |
| 27 | #define _GNU_SOURCE |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <ctype.h> |
| 33 | #include <errno.h> |
| 34 | |
| 35 | #include "event-parse.h" |
Steven Rostedt | 668fe01 | 2012-04-06 00:47:55 +0200 | [diff] [blame] | 36 | #include "event-utils.h" |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 37 | |
| 38 | static const char *input_buf; |
| 39 | static unsigned long long input_buf_ptr; |
| 40 | static unsigned long long input_buf_siz; |
| 41 | |
Tom Zanussi | 5205aec | 2012-04-06 00:47:58 +0200 | [diff] [blame] | 42 | static int is_flag_field; |
| 43 | static int is_symbolic_field; |
| 44 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 45 | static int show_warning = 1; |
| 46 | |
| 47 | #define do_warning(fmt, ...) \ |
| 48 | do { \ |
| 49 | if (show_warning) \ |
| 50 | warning(fmt, ##__VA_ARGS__); \ |
| 51 | } while (0) |
| 52 | |
| 53 | static void init_input_buf(const char *buf, unsigned long long size) |
| 54 | { |
| 55 | input_buf = buf; |
| 56 | input_buf_siz = size; |
| 57 | input_buf_ptr = 0; |
| 58 | } |
| 59 | |
| 60 | const char *pevent_get_input_buf(void) |
| 61 | { |
| 62 | return input_buf; |
| 63 | } |
| 64 | |
| 65 | unsigned long long pevent_get_input_buf_ptr(void) |
| 66 | { |
| 67 | return input_buf_ptr; |
| 68 | } |
| 69 | |
| 70 | struct event_handler { |
| 71 | struct event_handler *next; |
| 72 | int id; |
| 73 | const char *sys_name; |
| 74 | const char *event_name; |
| 75 | pevent_event_handler_func func; |
| 76 | void *context; |
| 77 | }; |
| 78 | |
| 79 | struct pevent_func_params { |
| 80 | struct pevent_func_params *next; |
| 81 | enum pevent_func_arg_type type; |
| 82 | }; |
| 83 | |
| 84 | struct pevent_function_handler { |
| 85 | struct pevent_function_handler *next; |
| 86 | enum pevent_func_arg_type ret_type; |
| 87 | char *name; |
| 88 | pevent_func_handler func; |
| 89 | struct pevent_func_params *params; |
| 90 | int nr_args; |
| 91 | }; |
| 92 | |
| 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); |
| 96 | |
| 97 | static void free_func_handle(struct pevent_function_handler *func); |
| 98 | |
| 99 | /** |
| 100 | * pevent_buffer_init - init buffer for parsing |
| 101 | * @buf: buffer to parse |
| 102 | * @size: the size of the buffer |
| 103 | * |
| 104 | * For use with pevent_read_token(), this initializes the internal |
| 105 | * buffer that pevent_read_token() will parse. |
| 106 | */ |
| 107 | void pevent_buffer_init(const char *buf, unsigned long long size) |
| 108 | { |
| 109 | init_input_buf(buf, size); |
| 110 | } |
| 111 | |
| 112 | void breakpoint(void) |
| 113 | { |
| 114 | static int x; |
| 115 | x++; |
| 116 | } |
| 117 | |
| 118 | struct print_arg *alloc_arg(void) |
| 119 | { |
| 120 | struct print_arg *arg; |
| 121 | |
| 122 | arg = malloc_or_die(sizeof(*arg)); |
| 123 | if (!arg) |
| 124 | return NULL; |
| 125 | memset(arg, 0, sizeof(*arg)); |
| 126 | |
| 127 | return arg; |
| 128 | } |
| 129 | |
| 130 | struct cmdline { |
| 131 | char *comm; |
| 132 | int pid; |
| 133 | }; |
| 134 | |
| 135 | static int cmdline_cmp(const void *a, const void *b) |
| 136 | { |
| 137 | const struct cmdline *ca = a; |
| 138 | const struct cmdline *cb = b; |
| 139 | |
| 140 | if (ca->pid < cb->pid) |
| 141 | return -1; |
| 142 | if (ca->pid > cb->pid) |
| 143 | return 1; |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | struct cmdline_list { |
| 149 | struct cmdline_list *next; |
| 150 | char *comm; |
| 151 | int pid; |
| 152 | }; |
| 153 | |
| 154 | static int cmdline_init(struct pevent *pevent) |
| 155 | { |
| 156 | struct cmdline_list *cmdlist = pevent->cmdlist; |
| 157 | struct cmdline_list *item; |
| 158 | struct cmdline *cmdlines; |
| 159 | int i; |
| 160 | |
| 161 | cmdlines = malloc_or_die(sizeof(*cmdlines) * pevent->cmdline_count); |
| 162 | |
| 163 | i = 0; |
| 164 | while (cmdlist) { |
| 165 | cmdlines[i].pid = cmdlist->pid; |
| 166 | cmdlines[i].comm = cmdlist->comm; |
| 167 | i++; |
| 168 | item = cmdlist; |
| 169 | cmdlist = cmdlist->next; |
| 170 | free(item); |
| 171 | } |
| 172 | |
| 173 | qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp); |
| 174 | |
| 175 | pevent->cmdlines = cmdlines; |
| 176 | pevent->cmdlist = NULL; |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | static char *find_cmdline(struct pevent *pevent, int pid) |
| 182 | { |
| 183 | const struct cmdline *comm; |
| 184 | struct cmdline key; |
| 185 | |
| 186 | if (!pid) |
| 187 | return "<idle>"; |
| 188 | |
| 189 | if (!pevent->cmdlines) |
| 190 | cmdline_init(pevent); |
| 191 | |
| 192 | key.pid = pid; |
| 193 | |
| 194 | comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, |
| 195 | sizeof(*pevent->cmdlines), cmdline_cmp); |
| 196 | |
| 197 | if (comm) |
| 198 | return comm->comm; |
| 199 | return "<...>"; |
| 200 | } |
| 201 | |
| 202 | /** |
| 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. |
| 206 | * |
| 207 | * Returns 1 if the pid has a cmdline mapped to it |
| 208 | * 0 otherwise. |
| 209 | */ |
| 210 | int pevent_pid_is_registered(struct pevent *pevent, int pid) |
| 211 | { |
| 212 | const struct cmdline *comm; |
| 213 | struct cmdline key; |
| 214 | |
| 215 | if (!pid) |
| 216 | return 1; |
| 217 | |
| 218 | if (!pevent->cmdlines) |
| 219 | cmdline_init(pevent); |
| 220 | |
| 221 | key.pid = pid; |
| 222 | |
| 223 | comm = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, |
| 224 | sizeof(*pevent->cmdlines), cmdline_cmp); |
| 225 | |
| 226 | if (comm) |
| 227 | return 1; |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* |
| 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. |
| 235 | */ |
| 236 | static int add_new_comm(struct pevent *pevent, const char *comm, int pid) |
| 237 | { |
| 238 | struct cmdline *cmdlines = pevent->cmdlines; |
| 239 | const struct cmdline *cmdline; |
| 240 | struct cmdline key; |
| 241 | |
| 242 | if (!pid) |
| 243 | return 0; |
| 244 | |
| 245 | /* avoid duplicates */ |
| 246 | key.pid = pid; |
| 247 | |
| 248 | cmdline = bsearch(&key, pevent->cmdlines, pevent->cmdline_count, |
| 249 | sizeof(*pevent->cmdlines), cmdline_cmp); |
| 250 | if (cmdline) { |
| 251 | errno = EEXIST; |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1)); |
| 256 | if (!cmdlines) { |
| 257 | errno = ENOMEM; |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | cmdlines[pevent->cmdline_count].pid = pid; |
| 262 | cmdlines[pevent->cmdline_count].comm = strdup(comm); |
| 263 | if (!cmdlines[pevent->cmdline_count].comm) |
| 264 | die("malloc comm"); |
| 265 | |
| 266 | if (cmdlines[pevent->cmdline_count].comm) |
| 267 | pevent->cmdline_count++; |
| 268 | |
| 269 | qsort(cmdlines, pevent->cmdline_count, sizeof(*cmdlines), cmdline_cmp); |
| 270 | pevent->cmdlines = cmdlines; |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /** |
| 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 |
| 280 | * |
| 281 | * This adds a mapping to search for command line names with |
| 282 | * a given pid. The comm is duplicated. |
| 283 | */ |
| 284 | int pevent_register_comm(struct pevent *pevent, const char *comm, int pid) |
| 285 | { |
| 286 | struct cmdline_list *item; |
| 287 | |
| 288 | if (pevent->cmdlines) |
| 289 | return add_new_comm(pevent, comm, pid); |
| 290 | |
| 291 | item = malloc_or_die(sizeof(*item)); |
| 292 | item->comm = strdup(comm); |
| 293 | if (!item->comm) |
| 294 | die("malloc comm"); |
| 295 | item->pid = pid; |
| 296 | item->next = pevent->cmdlist; |
| 297 | |
| 298 | pevent->cmdlist = item; |
| 299 | pevent->cmdline_count++; |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | struct func_map { |
| 305 | unsigned long long addr; |
| 306 | char *func; |
| 307 | char *mod; |
| 308 | }; |
| 309 | |
| 310 | struct func_list { |
| 311 | struct func_list *next; |
| 312 | unsigned long long addr; |
| 313 | char *func; |
| 314 | char *mod; |
| 315 | }; |
| 316 | |
| 317 | static int func_cmp(const void *a, const void *b) |
| 318 | { |
| 319 | const struct func_map *fa = a; |
| 320 | const struct func_map *fb = b; |
| 321 | |
| 322 | if (fa->addr < fb->addr) |
| 323 | return -1; |
| 324 | if (fa->addr > fb->addr) |
| 325 | return 1; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * We are searching for a record in between, not an exact |
| 332 | * match. |
| 333 | */ |
| 334 | static int func_bcmp(const void *a, const void *b) |
| 335 | { |
| 336 | const struct func_map *fa = a; |
| 337 | const struct func_map *fb = b; |
| 338 | |
| 339 | if ((fa->addr == fb->addr) || |
| 340 | |
| 341 | (fa->addr > fb->addr && |
| 342 | fa->addr < (fb+1)->addr)) |
| 343 | return 0; |
| 344 | |
| 345 | if (fa->addr < fb->addr) |
| 346 | return -1; |
| 347 | |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | static int func_map_init(struct pevent *pevent) |
| 352 | { |
| 353 | struct func_list *funclist; |
| 354 | struct func_list *item; |
| 355 | struct func_map *func_map; |
| 356 | int i; |
| 357 | |
| 358 | func_map = malloc_or_die(sizeof(*func_map) * (pevent->func_count + 1)); |
| 359 | funclist = pevent->funclist; |
| 360 | |
| 361 | i = 0; |
| 362 | while (funclist) { |
| 363 | func_map[i].func = funclist->func; |
| 364 | func_map[i].addr = funclist->addr; |
| 365 | func_map[i].mod = funclist->mod; |
| 366 | i++; |
| 367 | item = funclist; |
| 368 | funclist = funclist->next; |
| 369 | free(item); |
| 370 | } |
| 371 | |
| 372 | qsort(func_map, pevent->func_count, sizeof(*func_map), func_cmp); |
| 373 | |
| 374 | /* |
| 375 | * Add a special record at the end. |
| 376 | */ |
| 377 | func_map[pevent->func_count].func = NULL; |
| 378 | func_map[pevent->func_count].addr = 0; |
| 379 | func_map[pevent->func_count].mod = NULL; |
| 380 | |
| 381 | pevent->func_map = func_map; |
| 382 | pevent->funclist = NULL; |
| 383 | |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static struct func_map * |
| 388 | find_func(struct pevent *pevent, unsigned long long addr) |
| 389 | { |
| 390 | struct func_map *func; |
| 391 | struct func_map key; |
| 392 | |
| 393 | if (!pevent->func_map) |
| 394 | func_map_init(pevent); |
| 395 | |
| 396 | key.addr = addr; |
| 397 | |
| 398 | func = bsearch(&key, pevent->func_map, pevent->func_count, |
| 399 | sizeof(*pevent->func_map), func_bcmp); |
| 400 | |
| 401 | return func; |
| 402 | } |
| 403 | |
| 404 | /** |
| 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 |
| 408 | * |
| 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. |
| 412 | */ |
| 413 | const char *pevent_find_function(struct pevent *pevent, unsigned long long addr) |
| 414 | { |
| 415 | struct func_map *map; |
| 416 | |
| 417 | map = find_func(pevent, addr); |
| 418 | if (!map) |
| 419 | return NULL; |
| 420 | |
| 421 | return map->func; |
| 422 | } |
| 423 | |
| 424 | /** |
| 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 |
| 428 | * |
| 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. |
| 432 | */ |
| 433 | unsigned long long |
| 434 | pevent_find_function_address(struct pevent *pevent, unsigned long long addr) |
| 435 | { |
| 436 | struct func_map *map; |
| 437 | |
| 438 | map = find_func(pevent, addr); |
| 439 | if (!map) |
| 440 | return 0; |
| 441 | |
| 442 | return map->addr; |
| 443 | } |
| 444 | |
| 445 | /** |
| 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) |
| 451 | * |
| 452 | * This registers a function name with an address and module. |
| 453 | * The @func passed in is duplicated. |
| 454 | */ |
| 455 | int pevent_register_function(struct pevent *pevent, char *func, |
| 456 | unsigned long long addr, char *mod) |
| 457 | { |
| 458 | struct func_list *item; |
| 459 | |
| 460 | item = malloc_or_die(sizeof(*item)); |
| 461 | |
| 462 | item->next = pevent->funclist; |
| 463 | item->func = strdup(func); |
| 464 | if (mod) |
| 465 | item->mod = strdup(mod); |
| 466 | else |
| 467 | item->mod = NULL; |
| 468 | item->addr = addr; |
| 469 | |
| 470 | pevent->funclist = item; |
| 471 | |
| 472 | pevent->func_count++; |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * pevent_print_funcs - print out the stored functions |
| 479 | * @pevent: handle for the pevent |
| 480 | * |
| 481 | * This prints out the stored functions. |
| 482 | */ |
| 483 | void pevent_print_funcs(struct pevent *pevent) |
| 484 | { |
| 485 | int i; |
| 486 | |
| 487 | if (!pevent->func_map) |
| 488 | func_map_init(pevent); |
| 489 | |
| 490 | for (i = 0; i < (int)pevent->func_count; i++) { |
| 491 | printf("%016llx %s", |
| 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); |
| 496 | else |
| 497 | printf("\n"); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | struct printk_map { |
| 502 | unsigned long long addr; |
| 503 | char *printk; |
| 504 | }; |
| 505 | |
| 506 | struct printk_list { |
| 507 | struct printk_list *next; |
| 508 | unsigned long long addr; |
| 509 | char *printk; |
| 510 | }; |
| 511 | |
| 512 | static int printk_cmp(const void *a, const void *b) |
| 513 | { |
| 514 | const struct func_map *fa = a; |
| 515 | const struct func_map *fb = b; |
| 516 | |
| 517 | if (fa->addr < fb->addr) |
| 518 | return -1; |
| 519 | if (fa->addr > fb->addr) |
| 520 | return 1; |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static void printk_map_init(struct pevent *pevent) |
| 526 | { |
| 527 | struct printk_list *printklist; |
| 528 | struct printk_list *item; |
| 529 | struct printk_map *printk_map; |
| 530 | int i; |
| 531 | |
| 532 | printk_map = malloc_or_die(sizeof(*printk_map) * (pevent->printk_count + 1)); |
| 533 | |
| 534 | printklist = pevent->printklist; |
| 535 | |
| 536 | i = 0; |
| 537 | while (printklist) { |
| 538 | printk_map[i].printk = printklist->printk; |
| 539 | printk_map[i].addr = printklist->addr; |
| 540 | i++; |
| 541 | item = printklist; |
| 542 | printklist = printklist->next; |
| 543 | free(item); |
| 544 | } |
| 545 | |
| 546 | qsort(printk_map, pevent->printk_count, sizeof(*printk_map), printk_cmp); |
| 547 | |
| 548 | pevent->printk_map = printk_map; |
| 549 | pevent->printklist = NULL; |
| 550 | } |
| 551 | |
| 552 | static struct printk_map * |
| 553 | find_printk(struct pevent *pevent, unsigned long long addr) |
| 554 | { |
| 555 | struct printk_map *printk; |
| 556 | struct printk_map key; |
| 557 | |
| 558 | if (!pevent->printk_map) |
| 559 | printk_map_init(pevent); |
| 560 | |
| 561 | key.addr = addr; |
| 562 | |
| 563 | printk = bsearch(&key, pevent->printk_map, pevent->printk_count, |
| 564 | sizeof(*pevent->printk_map), printk_cmp); |
| 565 | |
| 566 | return printk; |
| 567 | } |
| 568 | |
| 569 | /** |
| 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 |
| 574 | * |
| 575 | * This registers a string by the address it was stored in the kernel. |
| 576 | * The @fmt passed in is duplicated. |
| 577 | */ |
| 578 | int pevent_register_print_string(struct pevent *pevent, char *fmt, |
| 579 | unsigned long long addr) |
| 580 | { |
| 581 | struct printk_list *item; |
| 582 | |
| 583 | item = malloc_or_die(sizeof(*item)); |
| 584 | |
| 585 | item->next = pevent->printklist; |
| 586 | pevent->printklist = item; |
| 587 | item->printk = strdup(fmt); |
| 588 | item->addr = addr; |
| 589 | |
| 590 | pevent->printk_count++; |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * pevent_print_printk - print out the stored strings |
| 597 | * @pevent: handle for the pevent |
| 598 | * |
| 599 | * This prints the string formats that were stored. |
| 600 | */ |
| 601 | void pevent_print_printk(struct pevent *pevent) |
| 602 | { |
| 603 | int i; |
| 604 | |
| 605 | if (!pevent->printk_map) |
| 606 | printk_map_init(pevent); |
| 607 | |
| 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); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | static struct event_format *alloc_event(void) |
| 616 | { |
| 617 | struct event_format *event; |
| 618 | |
| 619 | event = malloc_or_die(sizeof(*event)); |
| 620 | memset(event, 0, sizeof(*event)); |
| 621 | |
| 622 | return event; |
| 623 | } |
| 624 | |
| 625 | static void add_event(struct pevent *pevent, struct event_format *event) |
| 626 | { |
| 627 | int i; |
| 628 | |
| 629 | if (!pevent->events) |
| 630 | pevent->events = malloc_or_die(sizeof(event)); |
| 631 | else |
| 632 | pevent->events = |
| 633 | realloc(pevent->events, sizeof(event) * |
| 634 | (pevent->nr_events + 1)); |
| 635 | if (!pevent->events) |
| 636 | die("Can not allocate events"); |
| 637 | |
| 638 | for (i = 0; i < pevent->nr_events; i++) { |
| 639 | if (pevent->events[i]->id > event->id) |
| 640 | break; |
| 641 | } |
| 642 | if (i < pevent->nr_events) |
| 643 | memmove(&pevent->events[i + 1], |
| 644 | &pevent->events[i], |
| 645 | sizeof(event) * (pevent->nr_events - i)); |
| 646 | |
| 647 | pevent->events[i] = event; |
| 648 | pevent->nr_events++; |
| 649 | |
| 650 | event->pevent = pevent; |
| 651 | } |
| 652 | |
| 653 | static int event_item_type(enum event_type type) |
| 654 | { |
| 655 | switch (type) { |
| 656 | case EVENT_ITEM ... EVENT_SQUOTE: |
| 657 | return 1; |
| 658 | case EVENT_ERROR ... EVENT_DELIM: |
| 659 | default: |
| 660 | return 0; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | static void free_flag_sym(struct print_flag_sym *fsym) |
| 665 | { |
| 666 | struct print_flag_sym *next; |
| 667 | |
| 668 | while (fsym) { |
| 669 | next = fsym->next; |
| 670 | free(fsym->value); |
| 671 | free(fsym->str); |
| 672 | free(fsym); |
| 673 | fsym = next; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | static void free_arg(struct print_arg *arg) |
| 678 | { |
| 679 | struct print_arg *farg; |
| 680 | |
| 681 | if (!arg) |
| 682 | return; |
| 683 | |
| 684 | switch (arg->type) { |
| 685 | case PRINT_ATOM: |
| 686 | free(arg->atom.atom); |
| 687 | break; |
| 688 | case PRINT_FIELD: |
| 689 | free(arg->field.name); |
| 690 | break; |
| 691 | case PRINT_FLAGS: |
| 692 | free_arg(arg->flags.field); |
| 693 | free(arg->flags.delim); |
| 694 | free_flag_sym(arg->flags.flags); |
| 695 | break; |
| 696 | case PRINT_SYMBOL: |
| 697 | free_arg(arg->symbol.field); |
| 698 | free_flag_sym(arg->symbol.symbols); |
| 699 | break; |
| 700 | case PRINT_TYPE: |
| 701 | free(arg->typecast.type); |
| 702 | free_arg(arg->typecast.item); |
| 703 | break; |
| 704 | case PRINT_STRING: |
| 705 | case PRINT_BSTRING: |
| 706 | free(arg->string.string); |
| 707 | break; |
| 708 | case PRINT_DYNAMIC_ARRAY: |
| 709 | free(arg->dynarray.index); |
| 710 | break; |
| 711 | case PRINT_OP: |
| 712 | free(arg->op.op); |
| 713 | free_arg(arg->op.left); |
| 714 | free_arg(arg->op.right); |
| 715 | break; |
| 716 | case PRINT_FUNC: |
| 717 | while (arg->func.args) { |
| 718 | farg = arg->func.args; |
| 719 | arg->func.args = farg->next; |
| 720 | free_arg(farg); |
| 721 | } |
| 722 | break; |
| 723 | |
| 724 | case PRINT_NULL: |
| 725 | default: |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | free(arg); |
| 730 | } |
| 731 | |
| 732 | static enum event_type get_type(int ch) |
| 733 | { |
| 734 | if (ch == '\n') |
| 735 | return EVENT_NEWLINE; |
| 736 | if (isspace(ch)) |
| 737 | return EVENT_SPACE; |
| 738 | if (isalnum(ch) || ch == '_') |
| 739 | return EVENT_ITEM; |
| 740 | if (ch == '\'') |
| 741 | return EVENT_SQUOTE; |
| 742 | if (ch == '"') |
| 743 | return EVENT_DQUOTE; |
| 744 | if (!isprint(ch)) |
| 745 | return EVENT_NONE; |
| 746 | if (ch == '(' || ch == ')' || ch == ',') |
| 747 | return EVENT_DELIM; |
| 748 | |
| 749 | return EVENT_OP; |
| 750 | } |
| 751 | |
| 752 | static int __read_char(void) |
| 753 | { |
| 754 | if (input_buf_ptr >= input_buf_siz) |
| 755 | return -1; |
| 756 | |
| 757 | return input_buf[input_buf_ptr++]; |
| 758 | } |
| 759 | |
| 760 | static int __peek_char(void) |
| 761 | { |
| 762 | if (input_buf_ptr >= input_buf_siz) |
| 763 | return -1; |
| 764 | |
| 765 | return input_buf[input_buf_ptr]; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * pevent_peek_char - peek at the next character that will be read |
| 770 | * |
| 771 | * Returns the next character read, or -1 if end of buffer. |
| 772 | */ |
| 773 | int pevent_peek_char(void) |
| 774 | { |
| 775 | return __peek_char(); |
| 776 | } |
| 777 | |
| 778 | static enum event_type force_token(const char *str, char **tok); |
| 779 | |
| 780 | static enum event_type __read_token(char **tok) |
| 781 | { |
| 782 | char buf[BUFSIZ]; |
| 783 | int ch, last_ch, quote_ch, next_ch; |
| 784 | int i = 0; |
| 785 | int tok_size = 0; |
| 786 | enum event_type type; |
| 787 | |
| 788 | *tok = NULL; |
| 789 | |
| 790 | |
| 791 | ch = __read_char(); |
| 792 | if (ch < 0) |
| 793 | return EVENT_NONE; |
| 794 | |
| 795 | type = get_type(ch); |
| 796 | if (type == EVENT_NONE) |
| 797 | return type; |
| 798 | |
| 799 | buf[i++] = ch; |
| 800 | |
| 801 | switch (type) { |
| 802 | case EVENT_NEWLINE: |
| 803 | case EVENT_DELIM: |
| 804 | *tok = malloc_or_die(2); |
| 805 | (*tok)[0] = ch; |
| 806 | (*tok)[1] = 0; |
| 807 | return type; |
| 808 | |
| 809 | case EVENT_OP: |
| 810 | switch (ch) { |
| 811 | case '-': |
| 812 | next_ch = __peek_char(); |
| 813 | if (next_ch == '>') { |
| 814 | buf[i++] = __read_char(); |
| 815 | break; |
| 816 | } |
| 817 | /* fall through */ |
| 818 | case '+': |
| 819 | case '|': |
| 820 | case '&': |
| 821 | case '>': |
| 822 | case '<': |
| 823 | last_ch = ch; |
| 824 | ch = __peek_char(); |
| 825 | if (ch != last_ch) |
| 826 | goto test_equal; |
| 827 | buf[i++] = __read_char(); |
| 828 | switch (last_ch) { |
| 829 | case '>': |
| 830 | case '<': |
| 831 | goto test_equal; |
| 832 | default: |
| 833 | break; |
| 834 | } |
| 835 | break; |
| 836 | case '!': |
| 837 | case '=': |
| 838 | goto test_equal; |
| 839 | default: /* what should we do instead? */ |
| 840 | break; |
| 841 | } |
| 842 | buf[i] = 0; |
| 843 | *tok = strdup(buf); |
| 844 | return type; |
| 845 | |
| 846 | test_equal: |
| 847 | ch = __peek_char(); |
| 848 | if (ch == '=') |
| 849 | buf[i++] = __read_char(); |
| 850 | goto out; |
| 851 | |
| 852 | case EVENT_DQUOTE: |
| 853 | case EVENT_SQUOTE: |
| 854 | /* don't keep quotes */ |
| 855 | i--; |
| 856 | quote_ch = ch; |
| 857 | last_ch = 0; |
| 858 | concat: |
| 859 | do { |
| 860 | if (i == (BUFSIZ - 1)) { |
| 861 | buf[i] = 0; |
| 862 | if (*tok) { |
| 863 | *tok = realloc(*tok, tok_size + BUFSIZ); |
| 864 | if (!*tok) |
| 865 | return EVENT_NONE; |
| 866 | strcat(*tok, buf); |
| 867 | } else |
| 868 | *tok = strdup(buf); |
| 869 | |
| 870 | if (!*tok) |
| 871 | return EVENT_NONE; |
| 872 | tok_size += BUFSIZ; |
| 873 | i = 0; |
| 874 | } |
| 875 | last_ch = ch; |
| 876 | ch = __read_char(); |
| 877 | buf[i++] = ch; |
| 878 | /* the '\' '\' will cancel itself */ |
| 879 | if (ch == '\\' && last_ch == '\\') |
| 880 | last_ch = 0; |
| 881 | } while (ch != quote_ch || last_ch == '\\'); |
| 882 | /* remove the last quote */ |
| 883 | i--; |
| 884 | |
| 885 | /* |
| 886 | * For strings (double quotes) check the next token. |
| 887 | * If it is another string, concatinate the two. |
| 888 | */ |
| 889 | if (type == EVENT_DQUOTE) { |
| 890 | unsigned long long save_input_buf_ptr = input_buf_ptr; |
| 891 | |
| 892 | do { |
| 893 | ch = __read_char(); |
| 894 | } while (isspace(ch)); |
| 895 | if (ch == '"') |
| 896 | goto concat; |
| 897 | input_buf_ptr = save_input_buf_ptr; |
| 898 | } |
| 899 | |
| 900 | goto out; |
| 901 | |
| 902 | case EVENT_ERROR ... EVENT_SPACE: |
| 903 | case EVENT_ITEM: |
| 904 | default: |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | while (get_type(__peek_char()) == type) { |
| 909 | if (i == (BUFSIZ - 1)) { |
| 910 | buf[i] = 0; |
| 911 | if (*tok) { |
| 912 | *tok = realloc(*tok, tok_size + BUFSIZ); |
| 913 | if (!*tok) |
| 914 | return EVENT_NONE; |
| 915 | strcat(*tok, buf); |
| 916 | } else |
| 917 | *tok = strdup(buf); |
| 918 | |
| 919 | if (!*tok) |
| 920 | return EVENT_NONE; |
| 921 | tok_size += BUFSIZ; |
| 922 | i = 0; |
| 923 | } |
| 924 | ch = __read_char(); |
| 925 | buf[i++] = ch; |
| 926 | } |
| 927 | |
| 928 | out: |
| 929 | buf[i] = 0; |
| 930 | if (*tok) { |
| 931 | *tok = realloc(*tok, tok_size + i); |
| 932 | if (!*tok) |
| 933 | return EVENT_NONE; |
| 934 | strcat(*tok, buf); |
| 935 | } else |
| 936 | *tok = strdup(buf); |
| 937 | if (!*tok) |
| 938 | return EVENT_NONE; |
| 939 | |
| 940 | if (type == EVENT_ITEM) { |
| 941 | /* |
| 942 | * Older versions of the kernel has a bug that |
| 943 | * creates invalid symbols and will break the mac80211 |
| 944 | * parsing. This is a work around to that bug. |
| 945 | * |
| 946 | * See Linux kernel commit: |
| 947 | * 811cb50baf63461ce0bdb234927046131fc7fa8b |
| 948 | */ |
| 949 | if (strcmp(*tok, "LOCAL_PR_FMT") == 0) { |
| 950 | free(*tok); |
| 951 | *tok = NULL; |
| 952 | return force_token("\"\%s\" ", tok); |
| 953 | } else if (strcmp(*tok, "STA_PR_FMT") == 0) { |
| 954 | free(*tok); |
| 955 | *tok = NULL; |
| 956 | return force_token("\" sta:%pM\" ", tok); |
| 957 | } else if (strcmp(*tok, "VIF_PR_FMT") == 0) { |
| 958 | free(*tok); |
| 959 | *tok = NULL; |
| 960 | return force_token("\" vif:%p(%d)\" ", tok); |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | return type; |
| 965 | } |
| 966 | |
| 967 | static enum event_type force_token(const char *str, char **tok) |
| 968 | { |
| 969 | const char *save_input_buf; |
| 970 | unsigned long long save_input_buf_ptr; |
| 971 | unsigned long long save_input_buf_siz; |
| 972 | enum event_type type; |
| 973 | |
| 974 | /* save off the current input pointers */ |
| 975 | save_input_buf = input_buf; |
| 976 | save_input_buf_ptr = input_buf_ptr; |
| 977 | save_input_buf_siz = input_buf_siz; |
| 978 | |
| 979 | init_input_buf(str, strlen(str)); |
| 980 | |
| 981 | type = __read_token(tok); |
| 982 | |
| 983 | /* reset back to original token */ |
| 984 | input_buf = save_input_buf; |
| 985 | input_buf_ptr = save_input_buf_ptr; |
| 986 | input_buf_siz = save_input_buf_siz; |
| 987 | |
| 988 | return type; |
| 989 | } |
| 990 | |
| 991 | static void free_token(char *tok) |
| 992 | { |
| 993 | if (tok) |
| 994 | free(tok); |
| 995 | } |
| 996 | |
| 997 | static enum event_type read_token(char **tok) |
| 998 | { |
| 999 | enum event_type type; |
| 1000 | |
| 1001 | for (;;) { |
| 1002 | type = __read_token(tok); |
| 1003 | if (type != EVENT_SPACE) |
| 1004 | return type; |
| 1005 | |
| 1006 | free_token(*tok); |
| 1007 | } |
| 1008 | |
| 1009 | /* not reached */ |
| 1010 | *tok = NULL; |
| 1011 | return EVENT_NONE; |
| 1012 | } |
| 1013 | |
| 1014 | /** |
| 1015 | * pevent_read_token - access to utilites to use the pevent parser |
| 1016 | * @tok: The token to return |
| 1017 | * |
| 1018 | * This will parse tokens from the string given by |
| 1019 | * pevent_init_data(). |
| 1020 | * |
| 1021 | * Returns the token type. |
| 1022 | */ |
| 1023 | enum event_type pevent_read_token(char **tok) |
| 1024 | { |
| 1025 | return read_token(tok); |
| 1026 | } |
| 1027 | |
| 1028 | /** |
| 1029 | * pevent_free_token - free a token returned by pevent_read_token |
| 1030 | * @token: the token to free |
| 1031 | */ |
| 1032 | void pevent_free_token(char *token) |
| 1033 | { |
| 1034 | free_token(token); |
| 1035 | } |
| 1036 | |
| 1037 | /* no newline */ |
| 1038 | static enum event_type read_token_item(char **tok) |
| 1039 | { |
| 1040 | enum event_type type; |
| 1041 | |
| 1042 | for (;;) { |
| 1043 | type = __read_token(tok); |
| 1044 | if (type != EVENT_SPACE && type != EVENT_NEWLINE) |
| 1045 | return type; |
| 1046 | free_token(*tok); |
| 1047 | *tok = NULL; |
| 1048 | } |
| 1049 | |
| 1050 | /* not reached */ |
| 1051 | *tok = NULL; |
| 1052 | return EVENT_NONE; |
| 1053 | } |
| 1054 | |
| 1055 | static int test_type(enum event_type type, enum event_type expect) |
| 1056 | { |
| 1057 | if (type != expect) { |
| 1058 | do_warning("Error: expected type %d but read %d", |
| 1059 | expect, type); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | static int test_type_token(enum event_type type, const char *token, |
| 1066 | enum event_type expect, const char *expect_tok) |
| 1067 | { |
| 1068 | if (type != expect) { |
| 1069 | do_warning("Error: expected type %d but read %d", |
| 1070 | expect, type); |
| 1071 | return -1; |
| 1072 | } |
| 1073 | |
| 1074 | if (strcmp(token, expect_tok) != 0) { |
| 1075 | do_warning("Error: expected '%s' but read '%s'", |
| 1076 | expect_tok, token); |
| 1077 | return -1; |
| 1078 | } |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | static int __read_expect_type(enum event_type expect, char **tok, int newline_ok) |
| 1083 | { |
| 1084 | enum event_type type; |
| 1085 | |
| 1086 | if (newline_ok) |
| 1087 | type = read_token(tok); |
| 1088 | else |
| 1089 | type = read_token_item(tok); |
| 1090 | return test_type(type, expect); |
| 1091 | } |
| 1092 | |
| 1093 | static int read_expect_type(enum event_type expect, char **tok) |
| 1094 | { |
| 1095 | return __read_expect_type(expect, tok, 1); |
| 1096 | } |
| 1097 | |
| 1098 | static int __read_expected(enum event_type expect, const char *str, |
| 1099 | int newline_ok) |
| 1100 | { |
| 1101 | enum event_type type; |
| 1102 | char *token; |
| 1103 | int ret; |
| 1104 | |
| 1105 | if (newline_ok) |
| 1106 | type = read_token(&token); |
| 1107 | else |
| 1108 | type = read_token_item(&token); |
| 1109 | |
| 1110 | ret = test_type_token(type, token, expect, str); |
| 1111 | |
| 1112 | free_token(token); |
| 1113 | |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | static int read_expected(enum event_type expect, const char *str) |
| 1118 | { |
| 1119 | return __read_expected(expect, str, 1); |
| 1120 | } |
| 1121 | |
| 1122 | static int read_expected_item(enum event_type expect, const char *str) |
| 1123 | { |
| 1124 | return __read_expected(expect, str, 0); |
| 1125 | } |
| 1126 | |
| 1127 | static char *event_read_name(void) |
| 1128 | { |
| 1129 | char *token; |
| 1130 | |
| 1131 | if (read_expected(EVENT_ITEM, "name") < 0) |
| 1132 | return NULL; |
| 1133 | |
| 1134 | if (read_expected(EVENT_OP, ":") < 0) |
| 1135 | return NULL; |
| 1136 | |
| 1137 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 1138 | goto fail; |
| 1139 | |
| 1140 | return token; |
| 1141 | |
| 1142 | fail: |
| 1143 | free_token(token); |
| 1144 | return NULL; |
| 1145 | } |
| 1146 | |
| 1147 | static int event_read_id(void) |
| 1148 | { |
| 1149 | char *token; |
| 1150 | int id; |
| 1151 | |
| 1152 | if (read_expected_item(EVENT_ITEM, "ID") < 0) |
| 1153 | return -1; |
| 1154 | |
| 1155 | if (read_expected(EVENT_OP, ":") < 0) |
| 1156 | return -1; |
| 1157 | |
| 1158 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 1159 | goto fail; |
| 1160 | |
| 1161 | id = strtoul(token, NULL, 0); |
| 1162 | free_token(token); |
| 1163 | return id; |
| 1164 | |
| 1165 | fail: |
| 1166 | free_token(token); |
| 1167 | return -1; |
| 1168 | } |
| 1169 | |
| 1170 | static int field_is_string(struct format_field *field) |
| 1171 | { |
| 1172 | if ((field->flags & FIELD_IS_ARRAY) && |
| 1173 | (strstr(field->type, "char") || strstr(field->type, "u8") || |
| 1174 | strstr(field->type, "s8"))) |
| 1175 | return 1; |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | static int field_is_dynamic(struct format_field *field) |
| 1181 | { |
| 1182 | if (strncmp(field->type, "__data_loc", 10) == 0) |
| 1183 | return 1; |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static int field_is_long(struct format_field *field) |
| 1189 | { |
| 1190 | /* includes long long */ |
| 1191 | if (strstr(field->type, "long")) |
| 1192 | return 1; |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
| 1197 | static int event_read_fields(struct event_format *event, struct format_field **fields) |
| 1198 | { |
| 1199 | struct format_field *field = NULL; |
| 1200 | enum event_type type; |
| 1201 | char *token; |
| 1202 | char *last_token; |
| 1203 | int count = 0; |
| 1204 | |
| 1205 | do { |
| 1206 | type = read_token(&token); |
| 1207 | if (type == EVENT_NEWLINE) { |
| 1208 | free_token(token); |
| 1209 | return count; |
| 1210 | } |
| 1211 | |
| 1212 | count++; |
| 1213 | |
| 1214 | if (test_type_token(type, token, EVENT_ITEM, "field")) |
| 1215 | goto fail; |
| 1216 | free_token(token); |
| 1217 | |
| 1218 | type = read_token(&token); |
| 1219 | /* |
| 1220 | * The ftrace fields may still use the "special" name. |
| 1221 | * Just ignore it. |
| 1222 | */ |
| 1223 | if (event->flags & EVENT_FL_ISFTRACE && |
| 1224 | type == EVENT_ITEM && strcmp(token, "special") == 0) { |
| 1225 | free_token(token); |
| 1226 | type = read_token(&token); |
| 1227 | } |
| 1228 | |
| 1229 | if (test_type_token(type, token, EVENT_OP, ":") < 0) |
| 1230 | goto fail; |
| 1231 | |
| 1232 | free_token(token); |
| 1233 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 1234 | goto fail; |
| 1235 | |
| 1236 | last_token = token; |
| 1237 | |
| 1238 | field = malloc_or_die(sizeof(*field)); |
| 1239 | memset(field, 0, sizeof(*field)); |
| 1240 | field->event = event; |
| 1241 | |
| 1242 | /* read the rest of the type */ |
| 1243 | for (;;) { |
| 1244 | type = read_token(&token); |
| 1245 | if (type == EVENT_ITEM || |
| 1246 | (type == EVENT_OP && strcmp(token, "*") == 0) || |
| 1247 | /* |
| 1248 | * Some of the ftrace fields are broken and have |
| 1249 | * an illegal "." in them. |
| 1250 | */ |
| 1251 | (event->flags & EVENT_FL_ISFTRACE && |
| 1252 | type == EVENT_OP && strcmp(token, ".") == 0)) { |
| 1253 | |
| 1254 | if (strcmp(token, "*") == 0) |
| 1255 | field->flags |= FIELD_IS_POINTER; |
| 1256 | |
| 1257 | if (field->type) { |
| 1258 | field->type = realloc(field->type, |
| 1259 | strlen(field->type) + |
| 1260 | strlen(last_token) + 2); |
| 1261 | strcat(field->type, " "); |
| 1262 | strcat(field->type, last_token); |
| 1263 | free(last_token); |
| 1264 | } else |
| 1265 | field->type = last_token; |
| 1266 | last_token = token; |
| 1267 | continue; |
| 1268 | } |
| 1269 | |
| 1270 | break; |
| 1271 | } |
| 1272 | |
| 1273 | if (!field->type) { |
| 1274 | die("no type found"); |
| 1275 | goto fail; |
| 1276 | } |
| 1277 | field->name = last_token; |
| 1278 | |
| 1279 | if (test_type(type, EVENT_OP)) |
| 1280 | goto fail; |
| 1281 | |
| 1282 | if (strcmp(token, "[") == 0) { |
| 1283 | enum event_type last_type = type; |
| 1284 | char *brackets = token; |
| 1285 | int len; |
| 1286 | |
| 1287 | field->flags |= FIELD_IS_ARRAY; |
| 1288 | |
| 1289 | type = read_token(&token); |
| 1290 | |
| 1291 | if (type == EVENT_ITEM) |
| 1292 | field->arraylen = strtoul(token, NULL, 0); |
| 1293 | else |
| 1294 | field->arraylen = 0; |
| 1295 | |
| 1296 | while (strcmp(token, "]") != 0) { |
| 1297 | if (last_type == EVENT_ITEM && |
| 1298 | type == EVENT_ITEM) |
| 1299 | len = 2; |
| 1300 | else |
| 1301 | len = 1; |
| 1302 | last_type = type; |
| 1303 | |
| 1304 | brackets = realloc(brackets, |
| 1305 | strlen(brackets) + |
| 1306 | strlen(token) + len); |
| 1307 | if (len == 2) |
| 1308 | strcat(brackets, " "); |
| 1309 | strcat(brackets, token); |
| 1310 | /* We only care about the last token */ |
| 1311 | field->arraylen = strtoul(token, NULL, 0); |
| 1312 | free_token(token); |
| 1313 | type = read_token(&token); |
| 1314 | if (type == EVENT_NONE) { |
| 1315 | die("failed to find token"); |
| 1316 | goto fail; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | free_token(token); |
| 1321 | |
| 1322 | brackets = realloc(brackets, strlen(brackets) + 2); |
| 1323 | strcat(brackets, "]"); |
| 1324 | |
| 1325 | /* add brackets to type */ |
| 1326 | |
| 1327 | type = read_token(&token); |
| 1328 | /* |
| 1329 | * If the next token is not an OP, then it is of |
| 1330 | * the format: type [] item; |
| 1331 | */ |
| 1332 | if (type == EVENT_ITEM) { |
| 1333 | field->type = realloc(field->type, |
| 1334 | strlen(field->type) + |
| 1335 | strlen(field->name) + |
| 1336 | strlen(brackets) + 2); |
| 1337 | strcat(field->type, " "); |
| 1338 | strcat(field->type, field->name); |
| 1339 | free_token(field->name); |
| 1340 | strcat(field->type, brackets); |
| 1341 | field->name = token; |
| 1342 | type = read_token(&token); |
| 1343 | } else { |
| 1344 | field->type = realloc(field->type, |
| 1345 | strlen(field->type) + |
| 1346 | strlen(brackets) + 1); |
| 1347 | strcat(field->type, brackets); |
| 1348 | } |
| 1349 | free(brackets); |
| 1350 | } |
| 1351 | |
| 1352 | if (field_is_string(field)) |
| 1353 | field->flags |= FIELD_IS_STRING; |
| 1354 | if (field_is_dynamic(field)) |
| 1355 | field->flags |= FIELD_IS_DYNAMIC; |
| 1356 | if (field_is_long(field)) |
| 1357 | field->flags |= FIELD_IS_LONG; |
| 1358 | |
| 1359 | if (test_type_token(type, token, EVENT_OP, ";")) |
| 1360 | goto fail; |
| 1361 | free_token(token); |
| 1362 | |
| 1363 | if (read_expected(EVENT_ITEM, "offset") < 0) |
| 1364 | goto fail_expect; |
| 1365 | |
| 1366 | if (read_expected(EVENT_OP, ":") < 0) |
| 1367 | goto fail_expect; |
| 1368 | |
| 1369 | if (read_expect_type(EVENT_ITEM, &token)) |
| 1370 | goto fail; |
| 1371 | field->offset = strtoul(token, NULL, 0); |
| 1372 | free_token(token); |
| 1373 | |
| 1374 | if (read_expected(EVENT_OP, ";") < 0) |
| 1375 | goto fail_expect; |
| 1376 | |
| 1377 | if (read_expected(EVENT_ITEM, "size") < 0) |
| 1378 | goto fail_expect; |
| 1379 | |
| 1380 | if (read_expected(EVENT_OP, ":") < 0) |
| 1381 | goto fail_expect; |
| 1382 | |
| 1383 | if (read_expect_type(EVENT_ITEM, &token)) |
| 1384 | goto fail; |
| 1385 | field->size = strtoul(token, NULL, 0); |
| 1386 | free_token(token); |
| 1387 | |
| 1388 | if (read_expected(EVENT_OP, ";") < 0) |
| 1389 | goto fail_expect; |
| 1390 | |
| 1391 | type = read_token(&token); |
| 1392 | if (type != EVENT_NEWLINE) { |
| 1393 | /* newer versions of the kernel have a "signed" type */ |
| 1394 | if (test_type_token(type, token, EVENT_ITEM, "signed")) |
| 1395 | goto fail; |
| 1396 | |
| 1397 | free_token(token); |
| 1398 | |
| 1399 | if (read_expected(EVENT_OP, ":") < 0) |
| 1400 | goto fail_expect; |
| 1401 | |
| 1402 | if (read_expect_type(EVENT_ITEM, &token)) |
| 1403 | goto fail; |
| 1404 | |
| 1405 | /* add signed type */ |
| 1406 | |
| 1407 | free_token(token); |
| 1408 | if (read_expected(EVENT_OP, ";") < 0) |
| 1409 | goto fail_expect; |
| 1410 | |
| 1411 | if (read_expect_type(EVENT_NEWLINE, &token)) |
| 1412 | goto fail; |
| 1413 | } |
| 1414 | |
| 1415 | free_token(token); |
| 1416 | |
| 1417 | if (field->flags & FIELD_IS_ARRAY) { |
| 1418 | if (field->arraylen) |
| 1419 | field->elementsize = field->size / field->arraylen; |
| 1420 | else if (field->flags & FIELD_IS_STRING) |
| 1421 | field->elementsize = 1; |
| 1422 | else |
| 1423 | field->elementsize = event->pevent->long_size; |
| 1424 | } else |
| 1425 | field->elementsize = field->size; |
| 1426 | |
| 1427 | *fields = field; |
| 1428 | fields = &field->next; |
| 1429 | |
| 1430 | } while (1); |
| 1431 | |
| 1432 | return 0; |
| 1433 | |
| 1434 | fail: |
| 1435 | free_token(token); |
| 1436 | fail_expect: |
| 1437 | if (field) |
| 1438 | free(field); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | |
| 1442 | static int event_read_format(struct event_format *event) |
| 1443 | { |
| 1444 | char *token; |
| 1445 | int ret; |
| 1446 | |
| 1447 | if (read_expected_item(EVENT_ITEM, "format") < 0) |
| 1448 | return -1; |
| 1449 | |
| 1450 | if (read_expected(EVENT_OP, ":") < 0) |
| 1451 | return -1; |
| 1452 | |
| 1453 | if (read_expect_type(EVENT_NEWLINE, &token)) |
| 1454 | goto fail; |
| 1455 | free_token(token); |
| 1456 | |
| 1457 | ret = event_read_fields(event, &event->format.common_fields); |
| 1458 | if (ret < 0) |
| 1459 | return ret; |
| 1460 | event->format.nr_common = ret; |
| 1461 | |
| 1462 | ret = event_read_fields(event, &event->format.fields); |
| 1463 | if (ret < 0) |
| 1464 | return ret; |
| 1465 | event->format.nr_fields = ret; |
| 1466 | |
| 1467 | return 0; |
| 1468 | |
| 1469 | fail: |
| 1470 | free_token(token); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | |
| 1474 | static enum event_type |
| 1475 | process_arg_token(struct event_format *event, struct print_arg *arg, |
| 1476 | char **tok, enum event_type type); |
| 1477 | |
| 1478 | static enum event_type |
| 1479 | process_arg(struct event_format *event, struct print_arg *arg, char **tok) |
| 1480 | { |
| 1481 | enum event_type type; |
| 1482 | char *token; |
| 1483 | |
| 1484 | type = read_token(&token); |
| 1485 | *tok = token; |
| 1486 | |
| 1487 | return process_arg_token(event, arg, tok, type); |
| 1488 | } |
| 1489 | |
| 1490 | static enum event_type |
| 1491 | process_op(struct event_format *event, struct print_arg *arg, char **tok); |
| 1492 | |
| 1493 | static enum event_type |
| 1494 | process_cond(struct event_format *event, struct print_arg *top, char **tok) |
| 1495 | { |
| 1496 | struct print_arg *arg, *left, *right; |
| 1497 | enum event_type type; |
| 1498 | char *token = NULL; |
| 1499 | |
| 1500 | arg = alloc_arg(); |
| 1501 | left = alloc_arg(); |
| 1502 | right = alloc_arg(); |
| 1503 | |
| 1504 | arg->type = PRINT_OP; |
| 1505 | arg->op.left = left; |
| 1506 | arg->op.right = right; |
| 1507 | |
| 1508 | *tok = NULL; |
| 1509 | type = process_arg(event, left, &token); |
| 1510 | |
| 1511 | again: |
| 1512 | /* Handle other operations in the arguments */ |
| 1513 | if (type == EVENT_OP && strcmp(token, ":") != 0) { |
| 1514 | type = process_op(event, left, &token); |
| 1515 | goto again; |
| 1516 | } |
| 1517 | |
| 1518 | if (test_type_token(type, token, EVENT_OP, ":")) |
| 1519 | goto out_free; |
| 1520 | |
| 1521 | arg->op.op = token; |
| 1522 | |
| 1523 | type = process_arg(event, right, &token); |
| 1524 | |
| 1525 | top->op.right = arg; |
| 1526 | |
| 1527 | *tok = token; |
| 1528 | return type; |
| 1529 | |
| 1530 | out_free: |
| 1531 | /* Top may point to itself */ |
| 1532 | top->op.right = NULL; |
| 1533 | free_token(token); |
| 1534 | free_arg(arg); |
| 1535 | return EVENT_ERROR; |
| 1536 | } |
| 1537 | |
| 1538 | static enum event_type |
| 1539 | process_array(struct event_format *event, struct print_arg *top, char **tok) |
| 1540 | { |
| 1541 | struct print_arg *arg; |
| 1542 | enum event_type type; |
| 1543 | char *token = NULL; |
| 1544 | |
| 1545 | arg = alloc_arg(); |
| 1546 | |
| 1547 | *tok = NULL; |
| 1548 | type = process_arg(event, arg, &token); |
| 1549 | if (test_type_token(type, token, EVENT_OP, "]")) |
| 1550 | goto out_free; |
| 1551 | |
| 1552 | top->op.right = arg; |
| 1553 | |
| 1554 | free_token(token); |
| 1555 | type = read_token_item(&token); |
| 1556 | *tok = token; |
| 1557 | |
| 1558 | return type; |
| 1559 | |
| 1560 | out_free: |
| 1561 | free_token(*tok); |
| 1562 | *tok = NULL; |
| 1563 | free_arg(arg); |
| 1564 | return EVENT_ERROR; |
| 1565 | } |
| 1566 | |
| 1567 | static int get_op_prio(char *op) |
| 1568 | { |
| 1569 | if (!op[1]) { |
| 1570 | switch (op[0]) { |
| 1571 | case '~': |
| 1572 | case '!': |
| 1573 | return 4; |
| 1574 | case '*': |
| 1575 | case '/': |
| 1576 | case '%': |
| 1577 | return 6; |
| 1578 | case '+': |
| 1579 | case '-': |
| 1580 | return 7; |
| 1581 | /* '>>' and '<<' are 8 */ |
| 1582 | case '<': |
| 1583 | case '>': |
| 1584 | return 9; |
| 1585 | /* '==' and '!=' are 10 */ |
| 1586 | case '&': |
| 1587 | return 11; |
| 1588 | case '^': |
| 1589 | return 12; |
| 1590 | case '|': |
| 1591 | return 13; |
| 1592 | case '?': |
| 1593 | return 16; |
| 1594 | default: |
Vaibhav Nagarnaik | 14ffde0 | 2012-04-06 00:48:01 +0200 | [diff] [blame] | 1595 | do_warning("unknown op '%c'", op[0]); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1596 | return -1; |
| 1597 | } |
| 1598 | } else { |
| 1599 | if (strcmp(op, "++") == 0 || |
| 1600 | strcmp(op, "--") == 0) { |
| 1601 | return 3; |
| 1602 | } else if (strcmp(op, ">>") == 0 || |
| 1603 | strcmp(op, "<<") == 0) { |
| 1604 | return 8; |
| 1605 | } else if (strcmp(op, ">=") == 0 || |
| 1606 | strcmp(op, "<=") == 0) { |
| 1607 | return 9; |
| 1608 | } else if (strcmp(op, "==") == 0 || |
| 1609 | strcmp(op, "!=") == 0) { |
| 1610 | return 10; |
| 1611 | } else if (strcmp(op, "&&") == 0) { |
| 1612 | return 14; |
| 1613 | } else if (strcmp(op, "||") == 0) { |
| 1614 | return 15; |
| 1615 | } else { |
Vaibhav Nagarnaik | 14ffde0 | 2012-04-06 00:48:01 +0200 | [diff] [blame] | 1616 | do_warning("unknown op '%s'", op); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1617 | return -1; |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | |
Vaibhav Nagarnaik | 14ffde0 | 2012-04-06 00:48:01 +0200 | [diff] [blame] | 1622 | static int set_op_prio(struct print_arg *arg) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1623 | { |
| 1624 | |
| 1625 | /* single ops are the greatest */ |
Vaibhav Nagarnaik | 14ffde0 | 2012-04-06 00:48:01 +0200 | [diff] [blame] | 1626 | if (!arg->op.left || arg->op.left->type == PRINT_NULL) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1627 | arg->op.prio = 0; |
Vaibhav Nagarnaik | 14ffde0 | 2012-04-06 00:48:01 +0200 | [diff] [blame] | 1628 | else |
| 1629 | arg->op.prio = get_op_prio(arg->op.op); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1630 | |
Vaibhav Nagarnaik | 14ffde0 | 2012-04-06 00:48:01 +0200 | [diff] [blame] | 1631 | return arg->op.prio; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /* Note, *tok does not get freed, but will most likely be saved */ |
| 1635 | static enum event_type |
| 1636 | process_op(struct event_format *event, struct print_arg *arg, char **tok) |
| 1637 | { |
| 1638 | struct print_arg *left, *right = NULL; |
| 1639 | enum event_type type; |
| 1640 | char *token; |
| 1641 | |
| 1642 | /* the op is passed in via tok */ |
| 1643 | token = *tok; |
| 1644 | |
| 1645 | if (arg->type == PRINT_OP && !arg->op.left) { |
| 1646 | /* handle single op */ |
| 1647 | if (token[1]) { |
| 1648 | die("bad op token %s", token); |
| 1649 | goto out_free; |
| 1650 | } |
| 1651 | switch (token[0]) { |
| 1652 | case '~': |
| 1653 | case '!': |
| 1654 | case '+': |
| 1655 | case '-': |
| 1656 | break; |
| 1657 | default: |
| 1658 | do_warning("bad op token %s", token); |
| 1659 | goto out_free; |
| 1660 | |
| 1661 | } |
| 1662 | |
| 1663 | /* make an empty left */ |
| 1664 | left = alloc_arg(); |
| 1665 | left->type = PRINT_NULL; |
| 1666 | arg->op.left = left; |
| 1667 | |
| 1668 | right = alloc_arg(); |
| 1669 | arg->op.right = right; |
| 1670 | |
| 1671 | /* do not free the token, it belongs to an op */ |
| 1672 | *tok = NULL; |
| 1673 | type = process_arg(event, right, tok); |
| 1674 | |
| 1675 | } else if (strcmp(token, "?") == 0) { |
| 1676 | |
| 1677 | left = alloc_arg(); |
| 1678 | /* copy the top arg to the left */ |
| 1679 | *left = *arg; |
| 1680 | |
| 1681 | arg->type = PRINT_OP; |
| 1682 | arg->op.op = token; |
| 1683 | arg->op.left = left; |
| 1684 | arg->op.prio = 0; |
| 1685 | |
| 1686 | type = process_cond(event, arg, tok); |
| 1687 | |
| 1688 | } else if (strcmp(token, ">>") == 0 || |
| 1689 | strcmp(token, "<<") == 0 || |
| 1690 | strcmp(token, "&") == 0 || |
| 1691 | strcmp(token, "|") == 0 || |
| 1692 | strcmp(token, "&&") == 0 || |
| 1693 | strcmp(token, "||") == 0 || |
| 1694 | strcmp(token, "-") == 0 || |
| 1695 | strcmp(token, "+") == 0 || |
| 1696 | strcmp(token, "*") == 0 || |
| 1697 | strcmp(token, "^") == 0 || |
| 1698 | strcmp(token, "/") == 0 || |
| 1699 | strcmp(token, "<") == 0 || |
| 1700 | strcmp(token, ">") == 0 || |
| 1701 | strcmp(token, "==") == 0 || |
| 1702 | strcmp(token, "!=") == 0) { |
| 1703 | |
| 1704 | left = alloc_arg(); |
| 1705 | |
| 1706 | /* copy the top arg to the left */ |
| 1707 | *left = *arg; |
| 1708 | |
| 1709 | arg->type = PRINT_OP; |
| 1710 | arg->op.op = token; |
| 1711 | arg->op.left = left; |
| 1712 | |
Vaibhav Nagarnaik | 14ffde0 | 2012-04-06 00:48:01 +0200 | [diff] [blame] | 1713 | if (set_op_prio(arg) == -1) { |
| 1714 | event->flags |= EVENT_FL_FAILED; |
| 1715 | goto out_free; |
| 1716 | } |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1717 | |
| 1718 | type = read_token_item(&token); |
| 1719 | *tok = token; |
| 1720 | |
| 1721 | /* could just be a type pointer */ |
| 1722 | if ((strcmp(arg->op.op, "*") == 0) && |
| 1723 | type == EVENT_DELIM && (strcmp(token, ")") == 0)) { |
| 1724 | if (left->type != PRINT_ATOM) |
| 1725 | die("bad pointer type"); |
| 1726 | left->atom.atom = realloc(left->atom.atom, |
| 1727 | strlen(left->atom.atom) + 3); |
| 1728 | strcat(left->atom.atom, " *"); |
| 1729 | free(arg->op.op); |
| 1730 | *arg = *left; |
| 1731 | free(left); |
| 1732 | |
| 1733 | return type; |
| 1734 | } |
| 1735 | |
| 1736 | right = alloc_arg(); |
| 1737 | type = process_arg_token(event, right, tok, type); |
| 1738 | arg->op.right = right; |
| 1739 | |
| 1740 | } else if (strcmp(token, "[") == 0) { |
| 1741 | |
| 1742 | left = alloc_arg(); |
| 1743 | *left = *arg; |
| 1744 | |
| 1745 | arg->type = PRINT_OP; |
| 1746 | arg->op.op = token; |
| 1747 | arg->op.left = left; |
| 1748 | |
| 1749 | arg->op.prio = 0; |
| 1750 | |
| 1751 | type = process_array(event, arg, tok); |
| 1752 | |
| 1753 | } else { |
| 1754 | do_warning("unknown op '%s'", token); |
| 1755 | event->flags |= EVENT_FL_FAILED; |
| 1756 | /* the arg is now the left side */ |
| 1757 | goto out_free; |
| 1758 | } |
| 1759 | |
| 1760 | if (type == EVENT_OP && strcmp(*tok, ":") != 0) { |
| 1761 | int prio; |
| 1762 | |
| 1763 | /* higher prios need to be closer to the root */ |
| 1764 | prio = get_op_prio(*tok); |
| 1765 | |
| 1766 | if (prio > arg->op.prio) |
| 1767 | return process_op(event, arg, tok); |
| 1768 | |
| 1769 | return process_op(event, right, tok); |
| 1770 | } |
| 1771 | |
| 1772 | return type; |
| 1773 | |
| 1774 | out_free: |
| 1775 | free_token(token); |
| 1776 | *tok = NULL; |
| 1777 | return EVENT_ERROR; |
| 1778 | } |
| 1779 | |
| 1780 | static enum event_type |
| 1781 | process_entry(struct event_format *event __unused, struct print_arg *arg, |
| 1782 | char **tok) |
| 1783 | { |
| 1784 | enum event_type type; |
| 1785 | char *field; |
| 1786 | char *token; |
| 1787 | |
| 1788 | if (read_expected(EVENT_OP, "->") < 0) |
| 1789 | goto out_err; |
| 1790 | |
| 1791 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 1792 | goto out_free; |
| 1793 | field = token; |
| 1794 | |
| 1795 | arg->type = PRINT_FIELD; |
| 1796 | arg->field.name = field; |
| 1797 | |
Tom Zanussi | 5205aec | 2012-04-06 00:47:58 +0200 | [diff] [blame] | 1798 | if (is_flag_field) { |
| 1799 | arg->field.field = pevent_find_any_field(event, arg->field.name); |
| 1800 | arg->field.field->flags |= FIELD_IS_FLAG; |
| 1801 | is_flag_field = 0; |
| 1802 | } else if (is_symbolic_field) { |
| 1803 | arg->field.field = pevent_find_any_field(event, arg->field.name); |
| 1804 | arg->field.field->flags |= FIELD_IS_SYMBOLIC; |
| 1805 | is_symbolic_field = 0; |
| 1806 | } |
| 1807 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1808 | type = read_token(&token); |
| 1809 | *tok = token; |
| 1810 | |
| 1811 | return type; |
| 1812 | |
| 1813 | out_free: |
| 1814 | free_token(token); |
| 1815 | out_err: |
| 1816 | *tok = NULL; |
| 1817 | return EVENT_ERROR; |
| 1818 | } |
| 1819 | |
| 1820 | static char *arg_eval (struct print_arg *arg); |
| 1821 | |
| 1822 | static unsigned long long |
| 1823 | eval_type_str(unsigned long long val, const char *type, int pointer) |
| 1824 | { |
| 1825 | int sign = 0; |
| 1826 | char *ref; |
| 1827 | int len; |
| 1828 | |
| 1829 | len = strlen(type); |
| 1830 | |
| 1831 | if (pointer) { |
| 1832 | |
| 1833 | if (type[len-1] != '*') { |
| 1834 | do_warning("pointer expected with non pointer type"); |
| 1835 | return val; |
| 1836 | } |
| 1837 | |
| 1838 | ref = malloc_or_die(len); |
| 1839 | memcpy(ref, type, len); |
| 1840 | |
| 1841 | /* chop off the " *" */ |
| 1842 | ref[len - 2] = 0; |
| 1843 | |
| 1844 | val = eval_type_str(val, ref, 0); |
| 1845 | free(ref); |
| 1846 | return val; |
| 1847 | } |
| 1848 | |
| 1849 | /* check if this is a pointer */ |
| 1850 | if (type[len - 1] == '*') |
| 1851 | return val; |
| 1852 | |
| 1853 | /* Try to figure out the arg size*/ |
| 1854 | if (strncmp(type, "struct", 6) == 0) |
| 1855 | /* all bets off */ |
| 1856 | return val; |
| 1857 | |
| 1858 | if (strcmp(type, "u8") == 0) |
| 1859 | return val & 0xff; |
| 1860 | |
| 1861 | if (strcmp(type, "u16") == 0) |
| 1862 | return val & 0xffff; |
| 1863 | |
| 1864 | if (strcmp(type, "u32") == 0) |
| 1865 | return val & 0xffffffff; |
| 1866 | |
| 1867 | if (strcmp(type, "u64") == 0 || |
| 1868 | strcmp(type, "s64")) |
| 1869 | return val; |
| 1870 | |
| 1871 | if (strcmp(type, "s8") == 0) |
| 1872 | return (unsigned long long)(char)val & 0xff; |
| 1873 | |
| 1874 | if (strcmp(type, "s16") == 0) |
| 1875 | return (unsigned long long)(short)val & 0xffff; |
| 1876 | |
| 1877 | if (strcmp(type, "s32") == 0) |
| 1878 | return (unsigned long long)(int)val & 0xffffffff; |
| 1879 | |
| 1880 | if (strncmp(type, "unsigned ", 9) == 0) { |
| 1881 | sign = 0; |
| 1882 | type += 9; |
| 1883 | } |
| 1884 | |
| 1885 | if (strcmp(type, "char") == 0) { |
| 1886 | if (sign) |
| 1887 | return (unsigned long long)(char)val & 0xff; |
| 1888 | else |
| 1889 | return val & 0xff; |
| 1890 | } |
| 1891 | |
| 1892 | if (strcmp(type, "short") == 0) { |
| 1893 | if (sign) |
| 1894 | return (unsigned long long)(short)val & 0xffff; |
| 1895 | else |
| 1896 | return val & 0xffff; |
| 1897 | } |
| 1898 | |
| 1899 | if (strcmp(type, "int") == 0) { |
| 1900 | if (sign) |
| 1901 | return (unsigned long long)(int)val & 0xffffffff; |
| 1902 | else |
| 1903 | return val & 0xffffffff; |
| 1904 | } |
| 1905 | |
| 1906 | return val; |
| 1907 | } |
| 1908 | |
| 1909 | /* |
| 1910 | * Try to figure out the type. |
| 1911 | */ |
| 1912 | static unsigned long long |
| 1913 | eval_type(unsigned long long val, struct print_arg *arg, int pointer) |
| 1914 | { |
| 1915 | if (arg->type != PRINT_TYPE) |
| 1916 | die("expected type argument"); |
| 1917 | |
| 1918 | return eval_type_str(val, arg->typecast.type, pointer); |
| 1919 | } |
| 1920 | |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1921 | static int arg_num_eval(struct print_arg *arg, long long *val) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1922 | { |
| 1923 | long long left, right; |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1924 | int ret = 1; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1925 | |
| 1926 | switch (arg->type) { |
| 1927 | case PRINT_ATOM: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1928 | *val = strtoll(arg->atom.atom, NULL, 0); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1929 | break; |
| 1930 | case PRINT_TYPE: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1931 | ret = arg_num_eval(arg->typecast.item, val); |
| 1932 | if (!ret) |
| 1933 | break; |
| 1934 | *val = eval_type(*val, arg, 0); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1935 | break; |
| 1936 | case PRINT_OP: |
| 1937 | switch (arg->op.op[0]) { |
| 1938 | case '|': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1939 | ret = arg_num_eval(arg->op.left, &left); |
| 1940 | if (!ret) |
| 1941 | break; |
| 1942 | ret = arg_num_eval(arg->op.right, &right); |
| 1943 | if (!ret) |
| 1944 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1945 | if (arg->op.op[1]) |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1946 | *val = left || right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1947 | else |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1948 | *val = left | right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1949 | break; |
| 1950 | case '&': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1951 | ret = arg_num_eval(arg->op.left, &left); |
| 1952 | if (!ret) |
| 1953 | break; |
| 1954 | ret = arg_num_eval(arg->op.right, &right); |
| 1955 | if (!ret) |
| 1956 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1957 | if (arg->op.op[1]) |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1958 | *val = left && right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1959 | else |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1960 | *val = left & right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1961 | break; |
| 1962 | case '<': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1963 | ret = arg_num_eval(arg->op.left, &left); |
| 1964 | if (!ret) |
| 1965 | break; |
| 1966 | ret = arg_num_eval(arg->op.right, &right); |
| 1967 | if (!ret) |
| 1968 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1969 | switch (arg->op.op[1]) { |
| 1970 | case 0: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1971 | *val = left < right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1972 | break; |
| 1973 | case '<': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1974 | *val = left << right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1975 | break; |
| 1976 | case '=': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1977 | *val = left <= right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1978 | break; |
| 1979 | default: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1980 | do_warning("unknown op '%s'", arg->op.op); |
| 1981 | ret = 0; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1982 | } |
| 1983 | break; |
| 1984 | case '>': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1985 | ret = arg_num_eval(arg->op.left, &left); |
| 1986 | if (!ret) |
| 1987 | break; |
| 1988 | ret = arg_num_eval(arg->op.right, &right); |
| 1989 | if (!ret) |
| 1990 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1991 | switch (arg->op.op[1]) { |
| 1992 | case 0: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1993 | *val = left > right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1994 | break; |
| 1995 | case '>': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1996 | *val = left >> right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 1997 | break; |
| 1998 | case '=': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 1999 | *val = left >= right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2000 | break; |
| 2001 | default: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2002 | do_warning("unknown op '%s'", arg->op.op); |
| 2003 | ret = 0; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2004 | } |
| 2005 | break; |
| 2006 | case '=': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2007 | ret = arg_num_eval(arg->op.left, &left); |
| 2008 | if (!ret) |
| 2009 | break; |
| 2010 | ret = arg_num_eval(arg->op.right, &right); |
| 2011 | if (!ret) |
| 2012 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2013 | |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2014 | if (arg->op.op[1] != '=') { |
| 2015 | do_warning("unknown op '%s'", arg->op.op); |
| 2016 | ret = 0; |
| 2017 | } else |
| 2018 | *val = left == right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2019 | break; |
| 2020 | case '!': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2021 | ret = arg_num_eval(arg->op.left, &left); |
| 2022 | if (!ret) |
| 2023 | break; |
| 2024 | ret = arg_num_eval(arg->op.right, &right); |
| 2025 | if (!ret) |
| 2026 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2027 | |
| 2028 | switch (arg->op.op[1]) { |
| 2029 | case '=': |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2030 | *val = left != right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2031 | break; |
| 2032 | default: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2033 | do_warning("unknown op '%s'", arg->op.op); |
| 2034 | ret = 0; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2035 | } |
| 2036 | break; |
| 2037 | case '-': |
| 2038 | /* check for negative */ |
| 2039 | if (arg->op.left->type == PRINT_NULL) |
| 2040 | left = 0; |
| 2041 | else |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2042 | ret = arg_num_eval(arg->op.left, &left); |
| 2043 | if (!ret) |
| 2044 | break; |
| 2045 | ret = arg_num_eval(arg->op.right, &right); |
| 2046 | if (!ret) |
| 2047 | break; |
| 2048 | *val = left - right; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2049 | break; |
Vaibhav Nagarnaik | b482859 | 2012-04-06 00:48:03 +0200 | [diff] [blame] | 2050 | case '+': |
| 2051 | if (arg->op.left->type == PRINT_NULL) |
| 2052 | left = 0; |
| 2053 | else |
| 2054 | ret = arg_num_eval(arg->op.left, &left); |
| 2055 | if (!ret) |
| 2056 | break; |
| 2057 | ret = arg_num_eval(arg->op.right, &right); |
| 2058 | if (!ret) |
| 2059 | break; |
| 2060 | *val = left + right; |
| 2061 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2062 | default: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2063 | do_warning("unknown op '%s'", arg->op.op); |
| 2064 | ret = 0; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2065 | } |
| 2066 | break; |
| 2067 | |
| 2068 | case PRINT_NULL: |
| 2069 | case PRINT_FIELD ... PRINT_SYMBOL: |
| 2070 | case PRINT_STRING: |
| 2071 | case PRINT_BSTRING: |
| 2072 | default: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2073 | do_warning("invalid eval type %d", arg->type); |
| 2074 | ret = 0; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2075 | |
| 2076 | } |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2077 | return ret; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | static char *arg_eval (struct print_arg *arg) |
| 2081 | { |
| 2082 | long long val; |
| 2083 | static char buf[20]; |
| 2084 | |
| 2085 | switch (arg->type) { |
| 2086 | case PRINT_ATOM: |
| 2087 | return arg->atom.atom; |
| 2088 | case PRINT_TYPE: |
| 2089 | return arg_eval(arg->typecast.item); |
| 2090 | case PRINT_OP: |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2091 | if (!arg_num_eval(arg, &val)) |
| 2092 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2093 | sprintf(buf, "%lld", val); |
| 2094 | return buf; |
| 2095 | |
| 2096 | case PRINT_NULL: |
| 2097 | case PRINT_FIELD ... PRINT_SYMBOL: |
| 2098 | case PRINT_STRING: |
| 2099 | case PRINT_BSTRING: |
| 2100 | default: |
| 2101 | die("invalid eval type %d", arg->type); |
| 2102 | break; |
| 2103 | } |
| 2104 | |
| 2105 | return NULL; |
| 2106 | } |
| 2107 | |
| 2108 | static enum event_type |
| 2109 | process_fields(struct event_format *event, struct print_flag_sym **list, char **tok) |
| 2110 | { |
| 2111 | enum event_type type; |
| 2112 | struct print_arg *arg = NULL; |
| 2113 | struct print_flag_sym *field; |
| 2114 | char *token = *tok; |
| 2115 | char *value; |
| 2116 | |
| 2117 | do { |
| 2118 | free_token(token); |
| 2119 | type = read_token_item(&token); |
| 2120 | if (test_type_token(type, token, EVENT_OP, "{")) |
| 2121 | break; |
| 2122 | |
| 2123 | arg = alloc_arg(); |
| 2124 | |
| 2125 | free_token(token); |
| 2126 | type = process_arg(event, arg, &token); |
Stefan Hajnoczi | 00b9da7 | 2012-05-23 11:36:42 +0900 | [diff] [blame^] | 2127 | |
| 2128 | if (type == EVENT_OP) |
| 2129 | type = process_op(event, arg, &token); |
| 2130 | |
| 2131 | if (type == EVENT_ERROR) |
| 2132 | goto out_free; |
| 2133 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2134 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 2135 | goto out_free; |
| 2136 | |
| 2137 | field = malloc_or_die(sizeof(*field)); |
Julia Lawall | 54a3625 | 2012-04-06 00:47:59 +0200 | [diff] [blame] | 2138 | memset(field, 0, sizeof(*field)); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2139 | |
| 2140 | value = arg_eval(arg); |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2141 | if (value == NULL) |
| 2142 | goto out_free; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2143 | field->value = strdup(value); |
| 2144 | |
| 2145 | free_arg(arg); |
| 2146 | arg = alloc_arg(); |
| 2147 | |
| 2148 | free_token(token); |
| 2149 | type = process_arg(event, arg, &token); |
| 2150 | if (test_type_token(type, token, EVENT_OP, "}")) |
| 2151 | goto out_free; |
| 2152 | |
| 2153 | value = arg_eval(arg); |
Vaibhav Nagarnaik | d69afed5 | 2012-04-06 00:48:00 +0200 | [diff] [blame] | 2154 | if (value == NULL) |
| 2155 | goto out_free; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2156 | field->str = strdup(value); |
| 2157 | free_arg(arg); |
| 2158 | arg = NULL; |
| 2159 | |
| 2160 | *list = field; |
| 2161 | list = &field->next; |
| 2162 | |
| 2163 | free_token(token); |
| 2164 | type = read_token_item(&token); |
| 2165 | } while (type == EVENT_DELIM && strcmp(token, ",") == 0); |
| 2166 | |
| 2167 | *tok = token; |
| 2168 | return type; |
| 2169 | |
| 2170 | out_free: |
| 2171 | free_arg(arg); |
| 2172 | free_token(token); |
| 2173 | *tok = NULL; |
| 2174 | |
| 2175 | return EVENT_ERROR; |
| 2176 | } |
| 2177 | |
| 2178 | static enum event_type |
| 2179 | process_flags(struct event_format *event, struct print_arg *arg, char **tok) |
| 2180 | { |
| 2181 | struct print_arg *field; |
| 2182 | enum event_type type; |
| 2183 | char *token; |
| 2184 | |
| 2185 | memset(arg, 0, sizeof(*arg)); |
| 2186 | arg->type = PRINT_FLAGS; |
| 2187 | |
| 2188 | field = alloc_arg(); |
| 2189 | |
| 2190 | type = process_arg(event, field, &token); |
| 2191 | |
| 2192 | /* Handle operations in the first argument */ |
| 2193 | while (type == EVENT_OP) |
| 2194 | type = process_op(event, field, &token); |
| 2195 | |
| 2196 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 2197 | goto out_free; |
| 2198 | free_token(token); |
| 2199 | |
| 2200 | arg->flags.field = field; |
| 2201 | |
| 2202 | type = read_token_item(&token); |
| 2203 | if (event_item_type(type)) { |
| 2204 | arg->flags.delim = token; |
| 2205 | type = read_token_item(&token); |
| 2206 | } |
| 2207 | |
| 2208 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 2209 | goto out_free; |
| 2210 | |
| 2211 | type = process_fields(event, &arg->flags.flags, &token); |
| 2212 | if (test_type_token(type, token, EVENT_DELIM, ")")) |
| 2213 | goto out_free; |
| 2214 | |
| 2215 | free_token(token); |
| 2216 | type = read_token_item(tok); |
| 2217 | return type; |
| 2218 | |
| 2219 | out_free: |
| 2220 | free_token(token); |
| 2221 | *tok = NULL; |
| 2222 | return EVENT_ERROR; |
| 2223 | } |
| 2224 | |
| 2225 | static enum event_type |
| 2226 | process_symbols(struct event_format *event, struct print_arg *arg, char **tok) |
| 2227 | { |
| 2228 | struct print_arg *field; |
| 2229 | enum event_type type; |
| 2230 | char *token; |
| 2231 | |
| 2232 | memset(arg, 0, sizeof(*arg)); |
| 2233 | arg->type = PRINT_SYMBOL; |
| 2234 | |
| 2235 | field = alloc_arg(); |
| 2236 | |
| 2237 | type = process_arg(event, field, &token); |
| 2238 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 2239 | goto out_free; |
| 2240 | |
| 2241 | arg->symbol.field = field; |
| 2242 | |
| 2243 | type = process_fields(event, &arg->symbol.symbols, &token); |
| 2244 | if (test_type_token(type, token, EVENT_DELIM, ")")) |
| 2245 | goto out_free; |
| 2246 | |
| 2247 | free_token(token); |
| 2248 | type = read_token_item(tok); |
| 2249 | return type; |
| 2250 | |
| 2251 | out_free: |
| 2252 | free_token(token); |
| 2253 | *tok = NULL; |
| 2254 | return EVENT_ERROR; |
| 2255 | } |
| 2256 | |
| 2257 | static enum event_type |
| 2258 | process_dynamic_array(struct event_format *event, struct print_arg *arg, char **tok) |
| 2259 | { |
| 2260 | struct format_field *field; |
| 2261 | enum event_type type; |
| 2262 | char *token; |
| 2263 | |
| 2264 | memset(arg, 0, sizeof(*arg)); |
| 2265 | arg->type = PRINT_DYNAMIC_ARRAY; |
| 2266 | |
| 2267 | /* |
| 2268 | * The item within the parenthesis is another field that holds |
| 2269 | * the index into where the array starts. |
| 2270 | */ |
| 2271 | type = read_token(&token); |
| 2272 | *tok = token; |
| 2273 | if (type != EVENT_ITEM) |
| 2274 | goto out_free; |
| 2275 | |
| 2276 | /* Find the field */ |
| 2277 | |
| 2278 | field = pevent_find_field(event, token); |
| 2279 | if (!field) |
| 2280 | goto out_free; |
| 2281 | |
| 2282 | arg->dynarray.field = field; |
| 2283 | arg->dynarray.index = 0; |
| 2284 | |
| 2285 | if (read_expected(EVENT_DELIM, ")") < 0) |
| 2286 | goto out_free; |
| 2287 | |
| 2288 | free_token(token); |
| 2289 | type = read_token_item(&token); |
| 2290 | *tok = token; |
| 2291 | if (type != EVENT_OP || strcmp(token, "[") != 0) |
| 2292 | return type; |
| 2293 | |
| 2294 | free_token(token); |
| 2295 | arg = alloc_arg(); |
| 2296 | type = process_arg(event, arg, &token); |
| 2297 | if (type == EVENT_ERROR) |
| 2298 | goto out_free; |
| 2299 | |
| 2300 | if (!test_type_token(type, token, EVENT_OP, "]")) |
| 2301 | goto out_free; |
| 2302 | |
| 2303 | free_token(token); |
| 2304 | type = read_token_item(tok); |
| 2305 | return type; |
| 2306 | |
| 2307 | out_free: |
| 2308 | free(arg); |
| 2309 | free_token(token); |
| 2310 | *tok = NULL; |
| 2311 | return EVENT_ERROR; |
| 2312 | } |
| 2313 | |
| 2314 | static enum event_type |
| 2315 | process_paren(struct event_format *event, struct print_arg *arg, char **tok) |
| 2316 | { |
| 2317 | struct print_arg *item_arg; |
| 2318 | enum event_type type; |
| 2319 | char *token; |
| 2320 | |
| 2321 | type = process_arg(event, arg, &token); |
| 2322 | |
| 2323 | if (type == EVENT_ERROR) |
| 2324 | goto out_free; |
| 2325 | |
| 2326 | if (type == EVENT_OP) |
| 2327 | type = process_op(event, arg, &token); |
| 2328 | |
| 2329 | if (type == EVENT_ERROR) |
| 2330 | goto out_free; |
| 2331 | |
| 2332 | if (test_type_token(type, token, EVENT_DELIM, ")")) |
| 2333 | goto out_free; |
| 2334 | |
| 2335 | free_token(token); |
| 2336 | type = read_token_item(&token); |
| 2337 | |
| 2338 | /* |
| 2339 | * If the next token is an item or another open paren, then |
| 2340 | * this was a typecast. |
| 2341 | */ |
| 2342 | if (event_item_type(type) || |
| 2343 | (type == EVENT_DELIM && strcmp(token, "(") == 0)) { |
| 2344 | |
| 2345 | /* make this a typecast and contine */ |
| 2346 | |
| 2347 | /* prevous must be an atom */ |
| 2348 | if (arg->type != PRINT_ATOM) |
| 2349 | die("previous needed to be PRINT_ATOM"); |
| 2350 | |
| 2351 | item_arg = alloc_arg(); |
| 2352 | |
| 2353 | arg->type = PRINT_TYPE; |
| 2354 | arg->typecast.type = arg->atom.atom; |
| 2355 | arg->typecast.item = item_arg; |
| 2356 | type = process_arg_token(event, item_arg, &token, type); |
| 2357 | |
| 2358 | } |
| 2359 | |
| 2360 | *tok = token; |
| 2361 | return type; |
| 2362 | |
| 2363 | out_free: |
| 2364 | free_token(token); |
| 2365 | *tok = NULL; |
| 2366 | return EVENT_ERROR; |
| 2367 | } |
| 2368 | |
| 2369 | |
| 2370 | static enum event_type |
| 2371 | process_str(struct event_format *event __unused, struct print_arg *arg, char **tok) |
| 2372 | { |
| 2373 | enum event_type type; |
| 2374 | char *token; |
| 2375 | |
| 2376 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 2377 | goto out_free; |
| 2378 | |
| 2379 | arg->type = PRINT_STRING; |
| 2380 | arg->string.string = token; |
| 2381 | arg->string.offset = -1; |
| 2382 | |
| 2383 | if (read_expected(EVENT_DELIM, ")") < 0) |
| 2384 | goto out_err; |
| 2385 | |
| 2386 | type = read_token(&token); |
| 2387 | *tok = token; |
| 2388 | |
| 2389 | return type; |
| 2390 | |
| 2391 | out_free: |
| 2392 | free_token(token); |
| 2393 | out_err: |
| 2394 | *tok = NULL; |
| 2395 | return EVENT_ERROR; |
| 2396 | } |
| 2397 | |
| 2398 | static struct pevent_function_handler * |
| 2399 | find_func_handler(struct pevent *pevent, char *func_name) |
| 2400 | { |
| 2401 | struct pevent_function_handler *func; |
| 2402 | |
| 2403 | for (func = pevent->func_handlers; func; func = func->next) { |
| 2404 | if (strcmp(func->name, func_name) == 0) |
| 2405 | break; |
| 2406 | } |
| 2407 | |
| 2408 | return func; |
| 2409 | } |
| 2410 | |
| 2411 | static void remove_func_handler(struct pevent *pevent, char *func_name) |
| 2412 | { |
| 2413 | struct pevent_function_handler *func; |
| 2414 | struct pevent_function_handler **next; |
| 2415 | |
| 2416 | next = &pevent->func_handlers; |
| 2417 | while ((func = *next)) { |
| 2418 | if (strcmp(func->name, func_name) == 0) { |
| 2419 | *next = func->next; |
| 2420 | free_func_handle(func); |
| 2421 | break; |
| 2422 | } |
| 2423 | next = &func->next; |
| 2424 | } |
| 2425 | } |
| 2426 | |
| 2427 | static enum event_type |
| 2428 | process_func_handler(struct event_format *event, struct pevent_function_handler *func, |
| 2429 | struct print_arg *arg, char **tok) |
| 2430 | { |
| 2431 | struct print_arg **next_arg; |
| 2432 | struct print_arg *farg; |
| 2433 | enum event_type type; |
| 2434 | char *token; |
| 2435 | char *test; |
| 2436 | int i; |
| 2437 | |
| 2438 | arg->type = PRINT_FUNC; |
| 2439 | arg->func.func = func; |
| 2440 | |
| 2441 | *tok = NULL; |
| 2442 | |
| 2443 | next_arg = &(arg->func.args); |
| 2444 | for (i = 0; i < func->nr_args; i++) { |
| 2445 | farg = alloc_arg(); |
| 2446 | type = process_arg(event, farg, &token); |
| 2447 | if (i < (func->nr_args - 1)) |
| 2448 | test = ","; |
| 2449 | else |
| 2450 | test = ")"; |
| 2451 | |
| 2452 | if (test_type_token(type, token, EVENT_DELIM, test)) { |
| 2453 | free_arg(farg); |
| 2454 | free_token(token); |
| 2455 | return EVENT_ERROR; |
| 2456 | } |
| 2457 | |
| 2458 | *next_arg = farg; |
| 2459 | next_arg = &(farg->next); |
| 2460 | free_token(token); |
| 2461 | } |
| 2462 | |
| 2463 | type = read_token(&token); |
| 2464 | *tok = token; |
| 2465 | |
| 2466 | return type; |
| 2467 | } |
| 2468 | |
| 2469 | static enum event_type |
| 2470 | process_function(struct event_format *event, struct print_arg *arg, |
| 2471 | char *token, char **tok) |
| 2472 | { |
| 2473 | struct pevent_function_handler *func; |
| 2474 | |
| 2475 | if (strcmp(token, "__print_flags") == 0) { |
| 2476 | free_token(token); |
Tom Zanussi | 5205aec | 2012-04-06 00:47:58 +0200 | [diff] [blame] | 2477 | is_flag_field = 1; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2478 | return process_flags(event, arg, tok); |
| 2479 | } |
| 2480 | if (strcmp(token, "__print_symbolic") == 0) { |
| 2481 | free_token(token); |
Tom Zanussi | 5205aec | 2012-04-06 00:47:58 +0200 | [diff] [blame] | 2482 | is_symbolic_field = 1; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 2483 | return process_symbols(event, arg, tok); |
| 2484 | } |
| 2485 | if (strcmp(token, "__get_str") == 0) { |
| 2486 | free_token(token); |
| 2487 | return process_str(event, arg, tok); |
| 2488 | } |
| 2489 | if (strcmp(token, "__get_dynamic_array") == 0) { |
| 2490 | free_token(token); |
| 2491 | return process_dynamic_array(event, arg, tok); |
| 2492 | } |
| 2493 | |
| 2494 | func = find_func_handler(event->pevent, token); |
| 2495 | if (func) { |
| 2496 | free_token(token); |
| 2497 | return process_func_handler(event, func, arg, tok); |
| 2498 | } |
| 2499 | |
| 2500 | do_warning("function %s not defined", token); |
| 2501 | free_token(token); |
| 2502 | return EVENT_ERROR; |
| 2503 | } |
| 2504 | |
| 2505 | static enum event_type |
| 2506 | process_arg_token(struct event_format *event, struct print_arg *arg, |
| 2507 | char **tok, enum event_type type) |
| 2508 | { |
| 2509 | char *token; |
| 2510 | char *atom; |
| 2511 | |
| 2512 | token = *tok; |
| 2513 | |
| 2514 | switch (type) { |
| 2515 | case EVENT_ITEM: |
| 2516 | if (strcmp(token, "REC") == 0) { |
| 2517 | free_token(token); |
| 2518 | type = process_entry(event, arg, &token); |
| 2519 | break; |
| 2520 | } |
| 2521 | atom = token; |
| 2522 | /* test the next token */ |
| 2523 | type = read_token_item(&token); |
| 2524 | |
| 2525 | /* |
| 2526 | * If the next token is a parenthesis, then this |
| 2527 | * is a function. |
| 2528 | */ |
| 2529 | if (type == EVENT_DELIM && strcmp(token, "(") == 0) { |
| 2530 | free_token(token); |
| 2531 | token = NULL; |
| 2532 | /* this will free atom. */ |
| 2533 | type = process_function(event, arg, atom, &token); |
| 2534 | break; |
| 2535 | } |
| 2536 | /* atoms can be more than one token long */ |
| 2537 | while (type == EVENT_ITEM) { |
| 2538 | atom = realloc(atom, strlen(atom) + strlen(token) + 2); |
| 2539 | strcat(atom, " "); |
| 2540 | strcat(atom, token); |
| 2541 | free_token(token); |
| 2542 | type = read_token_item(&token); |
| 2543 | } |
| 2544 | |
| 2545 | arg->type = PRINT_ATOM; |
| 2546 | arg->atom.atom = atom; |
| 2547 | break; |
| 2548 | |
| 2549 | case EVENT_DQUOTE: |
| 2550 | case EVENT_SQUOTE: |
| 2551 | arg->type = PRINT_ATOM; |
| 2552 | arg->atom.atom = token; |
| 2553 | type = read_token_item(&token); |
| 2554 | break; |
| 2555 | case EVENT_DELIM: |
| 2556 | if (strcmp(token, "(") == 0) { |
| 2557 | free_token(token); |
| 2558 | type = process_paren(event, arg, &token); |
| 2559 | break; |
| 2560 | } |
| 2561 | case EVENT_OP: |
| 2562 | /* handle single ops */ |
| 2563 | arg->type = PRINT_OP; |
| 2564 | arg->op.op = token; |
| 2565 | arg->op.left = NULL; |
| 2566 | type = process_op(event, arg, &token); |
| 2567 | |
| 2568 | /* On error, the op is freed */ |
| 2569 | if (type == EVENT_ERROR) |
| 2570 | arg->op.op = NULL; |
| 2571 | |
| 2572 | /* return error type if errored */ |
| 2573 | break; |
| 2574 | |
| 2575 | case EVENT_ERROR ... EVENT_NEWLINE: |
| 2576 | default: |
| 2577 | die("unexpected type %d", type); |
| 2578 | } |
| 2579 | *tok = token; |
| 2580 | |
| 2581 | return type; |
| 2582 | } |
| 2583 | |
| 2584 | static int event_read_print_args(struct event_format *event, struct print_arg **list) |
| 2585 | { |
| 2586 | enum event_type type = EVENT_ERROR; |
| 2587 | struct print_arg *arg; |
| 2588 | char *token; |
| 2589 | int args = 0; |
| 2590 | |
| 2591 | do { |
| 2592 | if (type == EVENT_NEWLINE) { |
| 2593 | type = read_token_item(&token); |
| 2594 | continue; |
| 2595 | } |
| 2596 | |
| 2597 | arg = alloc_arg(); |
| 2598 | |
| 2599 | type = process_arg(event, arg, &token); |
| 2600 | |
| 2601 | if (type == EVENT_ERROR) { |
| 2602 | free_token(token); |
| 2603 | free_arg(arg); |
| 2604 | return -1; |
| 2605 | } |
| 2606 | |
| 2607 | *list = arg; |
| 2608 | args++; |
| 2609 | |
| 2610 | if (type == EVENT_OP) { |
| 2611 | type = process_op(event, arg, &token); |
| 2612 | free_token(token); |
| 2613 | if (type == EVENT_ERROR) { |
| 2614 | *list = NULL; |
| 2615 | free_arg(arg); |
| 2616 | return -1; |
| 2617 | } |
| 2618 | list = &arg->next; |
| 2619 | continue; |
| 2620 | } |
| 2621 | |
| 2622 | if (type == EVENT_DELIM && strcmp(token, ",") == 0) { |
| 2623 | free_token(token); |
| 2624 | *list = arg; |
| 2625 | list = &arg->next; |
| 2626 | continue; |
| 2627 | } |
| 2628 | break; |
| 2629 | } while (type != EVENT_NONE); |
| 2630 | |
| 2631 | if (type != EVENT_NONE && type != EVENT_ERROR) |
| 2632 | free_token(token); |
| 2633 | |
| 2634 | return args; |
| 2635 | } |
| 2636 | |
| 2637 | static int event_read_print(struct event_format *event) |
| 2638 | { |
| 2639 | enum event_type type; |
| 2640 | char *token; |
| 2641 | int ret; |
| 2642 | |
| 2643 | if (read_expected_item(EVENT_ITEM, "print") < 0) |
| 2644 | return -1; |
| 2645 | |
| 2646 | if (read_expected(EVENT_ITEM, "fmt") < 0) |
| 2647 | return -1; |
| 2648 | |
| 2649 | if (read_expected(EVENT_OP, ":") < 0) |
| 2650 | return -1; |
| 2651 | |
| 2652 | if (read_expect_type(EVENT_DQUOTE, &token) < 0) |
| 2653 | goto fail; |
| 2654 | |
| 2655 | concat: |
| 2656 | event->print_fmt.format = token; |
| 2657 | event->print_fmt.args = NULL; |
| 2658 | |
| 2659 | /* ok to have no arg */ |
| 2660 | type = read_token_item(&token); |
| 2661 | |
| 2662 | if (type == EVENT_NONE) |
| 2663 | return 0; |
| 2664 | |
| 2665 | /* Handle concatenation of print lines */ |
| 2666 | if (type == EVENT_DQUOTE) { |
| 2667 | char *cat; |
| 2668 | |
| 2669 | cat = malloc_or_die(strlen(event->print_fmt.format) + |
| 2670 | strlen(token) + 1); |
| 2671 | strcpy(cat, event->print_fmt.format); |
| 2672 | strcat(cat, token); |
| 2673 | free_token(token); |
| 2674 | free_token(event->print_fmt.format); |
| 2675 | event->print_fmt.format = NULL; |
| 2676 | token = cat; |
| 2677 | goto concat; |
| 2678 | } |
| 2679 | |
| 2680 | if (test_type_token(type, token, EVENT_DELIM, ",")) |
| 2681 | goto fail; |
| 2682 | |
| 2683 | free_token(token); |
| 2684 | |
| 2685 | ret = event_read_print_args(event, &event->print_fmt.args); |
| 2686 | if (ret < 0) |
| 2687 | return -1; |
| 2688 | |
| 2689 | return ret; |
| 2690 | |
| 2691 | fail: |
| 2692 | free_token(token); |
| 2693 | return -1; |
| 2694 | } |
| 2695 | |
| 2696 | /** |
| 2697 | * pevent_find_common_field - return a common field by event |
| 2698 | * @event: handle for the event |
| 2699 | * @name: the name of the common field to return |
| 2700 | * |
| 2701 | * Returns a common field from the event by the given @name. |
| 2702 | * This only searchs the common fields and not all field. |
| 2703 | */ |
| 2704 | struct format_field * |
| 2705 | pevent_find_common_field(struct event_format *event, const char *name) |
| 2706 | { |
| 2707 | struct format_field *format; |
| 2708 | |
| 2709 | for (format = event->format.common_fields; |
| 2710 | format; format = format->next) { |
| 2711 | if (strcmp(format->name, name) == 0) |
| 2712 | break; |
| 2713 | } |
| 2714 | |
| 2715 | return format; |
| 2716 | } |
| 2717 | |
| 2718 | /** |
| 2719 | * pevent_find_field - find a non-common field |
| 2720 | * @event: handle for the event |
| 2721 | * @name: the name of the non-common field |
| 2722 | * |
| 2723 | * Returns a non-common field by the given @name. |
| 2724 | * This does not search common fields. |
| 2725 | */ |
| 2726 | struct format_field * |
| 2727 | pevent_find_field(struct event_format *event, const char *name) |
| 2728 | { |
| 2729 | struct format_field *format; |
| 2730 | |
| 2731 | for (format = event->format.fields; |
| 2732 | format; format = format->next) { |
| 2733 | if (strcmp(format->name, name) == 0) |
| 2734 | break; |
| 2735 | } |
| 2736 | |
| 2737 | return format; |
| 2738 | } |
| 2739 | |
| 2740 | /** |
| 2741 | * pevent_find_any_field - find any field by name |
| 2742 | * @event: handle for the event |
| 2743 | * @name: the name of the field |
| 2744 | * |
| 2745 | * Returns a field by the given @name. |
| 2746 | * This searchs the common field names first, then |
| 2747 | * the non-common ones if a common one was not found. |
| 2748 | */ |
| 2749 | struct format_field * |
| 2750 | pevent_find_any_field(struct event_format *event, const char *name) |
| 2751 | { |
| 2752 | struct format_field *format; |
| 2753 | |
| 2754 | format = pevent_find_common_field(event, name); |
| 2755 | if (format) |
| 2756 | return format; |
| 2757 | return pevent_find_field(event, name); |
| 2758 | } |
| 2759 | |
| 2760 | /** |
| 2761 | * pevent_read_number - read a number from data |
| 2762 | * @pevent: handle for the pevent |
| 2763 | * @ptr: the raw data |
| 2764 | * @size: the size of the data that holds the number |
| 2765 | * |
| 2766 | * Returns the number (converted to host) from the |
| 2767 | * raw data. |
| 2768 | */ |
| 2769 | unsigned long long pevent_read_number(struct pevent *pevent, |
| 2770 | const void *ptr, int size) |
| 2771 | { |
| 2772 | switch (size) { |
| 2773 | case 1: |
| 2774 | return *(unsigned char *)ptr; |
| 2775 | case 2: |
| 2776 | return data2host2(pevent, ptr); |
| 2777 | case 4: |
| 2778 | return data2host4(pevent, ptr); |
| 2779 | case 8: |
| 2780 | return data2host8(pevent, ptr); |
| 2781 | default: |
| 2782 | /* BUG! */ |
| 2783 | return 0; |
| 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | /** |
| 2788 | * pevent_read_number_field - read a number from data |
| 2789 | * @field: a handle to the field |
| 2790 | * @data: the raw data to read |
| 2791 | * @value: the value to place the number in |
| 2792 | * |
| 2793 | * Reads raw data according to a field offset and size, |
| 2794 | * and translates it into @value. |
| 2795 | * |
| 2796 | * Returns 0 on success, -1 otherwise. |
| 2797 | */ |
| 2798 | int pevent_read_number_field(struct format_field *field, const void *data, |
| 2799 | unsigned long long *value) |
| 2800 | { |
| 2801 | if (!field) |
| 2802 | return -1; |
| 2803 | switch (field->size) { |
| 2804 | case 1: |
| 2805 | case 2: |
| 2806 | case 4: |
| 2807 | case 8: |
| 2808 | *value = pevent_read_number(field->event->pevent, |
| 2809 | data + field->offset, field->size); |
| 2810 | return 0; |
| 2811 | default: |
| 2812 | return -1; |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | static int get_common_info(struct pevent *pevent, |
| 2817 | const char *type, int *offset, int *size) |
| 2818 | { |
| 2819 | struct event_format *event; |
| 2820 | struct format_field *field; |
| 2821 | |
| 2822 | /* |
| 2823 | * All events should have the same common elements. |
| 2824 | * Pick any event to find where the type is; |
| 2825 | */ |
| 2826 | if (!pevent->events) |
| 2827 | die("no event_list!"); |
| 2828 | |
| 2829 | event = pevent->events[0]; |
| 2830 | field = pevent_find_common_field(event, type); |
| 2831 | if (!field) |
| 2832 | die("field '%s' not found", type); |
| 2833 | |
| 2834 | *offset = field->offset; |
| 2835 | *size = field->size; |
| 2836 | |
| 2837 | return 0; |
| 2838 | } |
| 2839 | |
| 2840 | static int __parse_common(struct pevent *pevent, void *data, |
| 2841 | int *size, int *offset, const char *name) |
| 2842 | { |
| 2843 | int ret; |
| 2844 | |
| 2845 | if (!*size) { |
| 2846 | ret = get_common_info(pevent, name, offset, size); |
| 2847 | if (ret < 0) |
| 2848 | return ret; |
| 2849 | } |
| 2850 | return pevent_read_number(pevent, data + *offset, *size); |
| 2851 | } |
| 2852 | |
| 2853 | static int trace_parse_common_type(struct pevent *pevent, void *data) |
| 2854 | { |
| 2855 | return __parse_common(pevent, data, |
| 2856 | &pevent->type_size, &pevent->type_offset, |
| 2857 | "common_type"); |
| 2858 | } |
| 2859 | |
| 2860 | static int parse_common_pid(struct pevent *pevent, void *data) |
| 2861 | { |
| 2862 | return __parse_common(pevent, data, |
| 2863 | &pevent->pid_size, &pevent->pid_offset, |
| 2864 | "common_pid"); |
| 2865 | } |
| 2866 | |
| 2867 | static int parse_common_pc(struct pevent *pevent, void *data) |
| 2868 | { |
| 2869 | return __parse_common(pevent, data, |
| 2870 | &pevent->pc_size, &pevent->pc_offset, |
| 2871 | "common_preempt_count"); |
| 2872 | } |
| 2873 | |
| 2874 | static int parse_common_flags(struct pevent *pevent, void *data) |
| 2875 | { |
| 2876 | return __parse_common(pevent, data, |
| 2877 | &pevent->flags_size, &pevent->flags_offset, |
| 2878 | "common_flags"); |
| 2879 | } |
| 2880 | |
| 2881 | static int parse_common_lock_depth(struct pevent *pevent, void *data) |
| 2882 | { |
| 2883 | int ret; |
| 2884 | |
| 2885 | ret = __parse_common(pevent, data, |
| 2886 | &pevent->ld_size, &pevent->ld_offset, |
| 2887 | "common_lock_depth"); |
| 2888 | if (ret < 0) |
| 2889 | return -1; |
| 2890 | |
| 2891 | return ret; |
| 2892 | } |
| 2893 | |
| 2894 | static int events_id_cmp(const void *a, const void *b); |
| 2895 | |
| 2896 | /** |
| 2897 | * pevent_find_event - find an event by given id |
| 2898 | * @pevent: a handle to the pevent |
| 2899 | * @id: the id of the event |
| 2900 | * |
| 2901 | * Returns an event that has a given @id. |
| 2902 | */ |
| 2903 | struct event_format *pevent_find_event(struct pevent *pevent, int id) |
| 2904 | { |
| 2905 | struct event_format **eventptr; |
| 2906 | struct event_format key; |
| 2907 | struct event_format *pkey = &key; |
| 2908 | |
| 2909 | /* Check cache first */ |
| 2910 | if (pevent->last_event && pevent->last_event->id == id) |
| 2911 | return pevent->last_event; |
| 2912 | |
| 2913 | key.id = id; |
| 2914 | |
| 2915 | eventptr = bsearch(&pkey, pevent->events, pevent->nr_events, |
| 2916 | sizeof(*pevent->events), events_id_cmp); |
| 2917 | |
| 2918 | if (eventptr) { |
| 2919 | pevent->last_event = *eventptr; |
| 2920 | return *eventptr; |
| 2921 | } |
| 2922 | |
| 2923 | return NULL; |
| 2924 | } |
| 2925 | |
| 2926 | /** |
| 2927 | * pevent_find_event_by_name - find an event by given name |
| 2928 | * @pevent: a handle to the pevent |
| 2929 | * @sys: the system name to search for |
| 2930 | * @name: the name of the event to search for |
| 2931 | * |
| 2932 | * This returns an event with a given @name and under the system |
| 2933 | * @sys. If @sys is NULL the first event with @name is returned. |
| 2934 | */ |
| 2935 | struct event_format * |
| 2936 | pevent_find_event_by_name(struct pevent *pevent, |
| 2937 | const char *sys, const char *name) |
| 2938 | { |
| 2939 | struct event_format *event; |
| 2940 | int i; |
| 2941 | |
| 2942 | if (pevent->last_event && |
| 2943 | strcmp(pevent->last_event->name, name) == 0 && |
| 2944 | (!sys || strcmp(pevent->last_event->system, sys) == 0)) |
| 2945 | return pevent->last_event; |
| 2946 | |
| 2947 | for (i = 0; i < pevent->nr_events; i++) { |
| 2948 | event = pevent->events[i]; |
| 2949 | if (strcmp(event->name, name) == 0) { |
| 2950 | if (!sys) |
| 2951 | break; |
| 2952 | if (strcmp(event->system, sys) == 0) |
| 2953 | break; |
| 2954 | } |
| 2955 | } |
| 2956 | if (i == pevent->nr_events) |
| 2957 | event = NULL; |
| 2958 | |
| 2959 | pevent->last_event = event; |
| 2960 | return event; |
| 2961 | } |
| 2962 | |
| 2963 | static unsigned long long |
| 2964 | eval_num_arg(void *data, int size, struct event_format *event, struct print_arg *arg) |
| 2965 | { |
| 2966 | struct pevent *pevent = event->pevent; |
| 2967 | unsigned long long val = 0; |
| 2968 | unsigned long long left, right; |
| 2969 | struct print_arg *typearg = NULL; |
| 2970 | struct print_arg *larg; |
| 2971 | unsigned long offset; |
| 2972 | unsigned int field_size; |
| 2973 | |
| 2974 | switch (arg->type) { |
| 2975 | case PRINT_NULL: |
| 2976 | /* ?? */ |
| 2977 | return 0; |
| 2978 | case PRINT_ATOM: |
| 2979 | return strtoull(arg->atom.atom, NULL, 0); |
| 2980 | case PRINT_FIELD: |
| 2981 | if (!arg->field.field) { |
| 2982 | arg->field.field = pevent_find_any_field(event, arg->field.name); |
| 2983 | if (!arg->field.field) |
| 2984 | die("field %s not found", arg->field.name); |
| 2985 | } |
| 2986 | /* must be a number */ |
| 2987 | val = pevent_read_number(pevent, data + arg->field.field->offset, |
| 2988 | arg->field.field->size); |
| 2989 | break; |
| 2990 | case PRINT_FLAGS: |
| 2991 | case PRINT_SYMBOL: |
| 2992 | break; |
| 2993 | case PRINT_TYPE: |
| 2994 | val = eval_num_arg(data, size, event, arg->typecast.item); |
| 2995 | return eval_type(val, arg, 0); |
| 2996 | case PRINT_STRING: |
| 2997 | case PRINT_BSTRING: |
| 2998 | return 0; |
| 2999 | case PRINT_FUNC: { |
| 3000 | struct trace_seq s; |
| 3001 | trace_seq_init(&s); |
| 3002 | val = process_defined_func(&s, data, size, event, arg); |
| 3003 | trace_seq_destroy(&s); |
| 3004 | return val; |
| 3005 | } |
| 3006 | case PRINT_OP: |
| 3007 | if (strcmp(arg->op.op, "[") == 0) { |
| 3008 | /* |
| 3009 | * Arrays are special, since we don't want |
| 3010 | * to read the arg as is. |
| 3011 | */ |
| 3012 | right = eval_num_arg(data, size, event, arg->op.right); |
| 3013 | |
| 3014 | /* handle typecasts */ |
| 3015 | larg = arg->op.left; |
| 3016 | while (larg->type == PRINT_TYPE) { |
| 3017 | if (!typearg) |
| 3018 | typearg = larg; |
| 3019 | larg = larg->typecast.item; |
| 3020 | } |
| 3021 | |
| 3022 | /* Default to long size */ |
| 3023 | field_size = pevent->long_size; |
| 3024 | |
| 3025 | switch (larg->type) { |
| 3026 | case PRINT_DYNAMIC_ARRAY: |
| 3027 | offset = pevent_read_number(pevent, |
| 3028 | data + larg->dynarray.field->offset, |
| 3029 | larg->dynarray.field->size); |
| 3030 | if (larg->dynarray.field->elementsize) |
| 3031 | field_size = larg->dynarray.field->elementsize; |
| 3032 | /* |
| 3033 | * The actual length of the dynamic array is stored |
| 3034 | * in the top half of the field, and the offset |
| 3035 | * is in the bottom half of the 32 bit field. |
| 3036 | */ |
| 3037 | offset &= 0xffff; |
| 3038 | offset += right; |
| 3039 | break; |
| 3040 | case PRINT_FIELD: |
| 3041 | if (!larg->field.field) { |
| 3042 | larg->field.field = |
| 3043 | pevent_find_any_field(event, larg->field.name); |
| 3044 | if (!larg->field.field) |
| 3045 | die("field %s not found", larg->field.name); |
| 3046 | } |
| 3047 | field_size = larg->field.field->elementsize; |
| 3048 | offset = larg->field.field->offset + |
| 3049 | right * larg->field.field->elementsize; |
| 3050 | break; |
| 3051 | default: |
| 3052 | goto default_op; /* oops, all bets off */ |
| 3053 | } |
| 3054 | val = pevent_read_number(pevent, |
| 3055 | data + offset, field_size); |
| 3056 | if (typearg) |
| 3057 | val = eval_type(val, typearg, 1); |
| 3058 | break; |
| 3059 | } else if (strcmp(arg->op.op, "?") == 0) { |
| 3060 | left = eval_num_arg(data, size, event, arg->op.left); |
| 3061 | arg = arg->op.right; |
| 3062 | if (left) |
| 3063 | val = eval_num_arg(data, size, event, arg->op.left); |
| 3064 | else |
| 3065 | val = eval_num_arg(data, size, event, arg->op.right); |
| 3066 | break; |
| 3067 | } |
| 3068 | default_op: |
| 3069 | left = eval_num_arg(data, size, event, arg->op.left); |
| 3070 | right = eval_num_arg(data, size, event, arg->op.right); |
| 3071 | switch (arg->op.op[0]) { |
| 3072 | case '!': |
| 3073 | switch (arg->op.op[1]) { |
| 3074 | case 0: |
| 3075 | val = !right; |
| 3076 | break; |
| 3077 | case '=': |
| 3078 | val = left != right; |
| 3079 | break; |
| 3080 | default: |
| 3081 | die("unknown op '%s'", arg->op.op); |
| 3082 | } |
| 3083 | break; |
| 3084 | case '~': |
| 3085 | val = ~right; |
| 3086 | break; |
| 3087 | case '|': |
| 3088 | if (arg->op.op[1]) |
| 3089 | val = left || right; |
| 3090 | else |
| 3091 | val = left | right; |
| 3092 | break; |
| 3093 | case '&': |
| 3094 | if (arg->op.op[1]) |
| 3095 | val = left && right; |
| 3096 | else |
| 3097 | val = left & right; |
| 3098 | break; |
| 3099 | case '<': |
| 3100 | switch (arg->op.op[1]) { |
| 3101 | case 0: |
| 3102 | val = left < right; |
| 3103 | break; |
| 3104 | case '<': |
| 3105 | val = left << right; |
| 3106 | break; |
| 3107 | case '=': |
| 3108 | val = left <= right; |
| 3109 | break; |
| 3110 | default: |
| 3111 | die("unknown op '%s'", arg->op.op); |
| 3112 | } |
| 3113 | break; |
| 3114 | case '>': |
| 3115 | switch (arg->op.op[1]) { |
| 3116 | case 0: |
| 3117 | val = left > right; |
| 3118 | break; |
| 3119 | case '>': |
| 3120 | val = left >> right; |
| 3121 | break; |
| 3122 | case '=': |
| 3123 | val = left >= right; |
| 3124 | break; |
| 3125 | default: |
| 3126 | die("unknown op '%s'", arg->op.op); |
| 3127 | } |
| 3128 | break; |
| 3129 | case '=': |
| 3130 | if (arg->op.op[1] != '=') |
| 3131 | die("unknown op '%s'", arg->op.op); |
| 3132 | val = left == right; |
| 3133 | break; |
| 3134 | case '-': |
| 3135 | val = left - right; |
| 3136 | break; |
| 3137 | case '+': |
| 3138 | val = left + right; |
| 3139 | break; |
Steven Rostedt | 2e7a5fc | 2012-04-06 00:48:04 +0200 | [diff] [blame] | 3140 | case '/': |
| 3141 | val = left / right; |
| 3142 | break; |
| 3143 | case '*': |
| 3144 | val = left * right; |
| 3145 | break; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 3146 | default: |
| 3147 | die("unknown op '%s'", arg->op.op); |
| 3148 | } |
| 3149 | break; |
| 3150 | default: /* not sure what to do there */ |
| 3151 | return 0; |
| 3152 | } |
| 3153 | return val; |
| 3154 | } |
| 3155 | |
| 3156 | struct flag { |
| 3157 | const char *name; |
| 3158 | unsigned long long value; |
| 3159 | }; |
| 3160 | |
| 3161 | static const struct flag flags[] = { |
| 3162 | { "HI_SOFTIRQ", 0 }, |
| 3163 | { "TIMER_SOFTIRQ", 1 }, |
| 3164 | { "NET_TX_SOFTIRQ", 2 }, |
| 3165 | { "NET_RX_SOFTIRQ", 3 }, |
| 3166 | { "BLOCK_SOFTIRQ", 4 }, |
| 3167 | { "BLOCK_IOPOLL_SOFTIRQ", 5 }, |
| 3168 | { "TASKLET_SOFTIRQ", 6 }, |
| 3169 | { "SCHED_SOFTIRQ", 7 }, |
| 3170 | { "HRTIMER_SOFTIRQ", 8 }, |
| 3171 | { "RCU_SOFTIRQ", 9 }, |
| 3172 | |
| 3173 | { "HRTIMER_NORESTART", 0 }, |
| 3174 | { "HRTIMER_RESTART", 1 }, |
| 3175 | }; |
| 3176 | |
| 3177 | static unsigned long long eval_flag(const char *flag) |
| 3178 | { |
| 3179 | int i; |
| 3180 | |
| 3181 | /* |
| 3182 | * Some flags in the format files do not get converted. |
| 3183 | * If the flag is not numeric, see if it is something that |
| 3184 | * we already know about. |
| 3185 | */ |
| 3186 | if (isdigit(flag[0])) |
| 3187 | return strtoull(flag, NULL, 0); |
| 3188 | |
| 3189 | for (i = 0; i < (int)(sizeof(flags)/sizeof(flags[0])); i++) |
| 3190 | if (strcmp(flags[i].name, flag) == 0) |
| 3191 | return flags[i].value; |
| 3192 | |
| 3193 | return 0; |
| 3194 | } |
| 3195 | |
| 3196 | static void print_str_to_seq(struct trace_seq *s, const char *format, |
| 3197 | int len_arg, const char *str) |
| 3198 | { |
| 3199 | if (len_arg >= 0) |
| 3200 | trace_seq_printf(s, format, len_arg, str); |
| 3201 | else |
| 3202 | trace_seq_printf(s, format, str); |
| 3203 | } |
| 3204 | |
| 3205 | static void print_str_arg(struct trace_seq *s, void *data, int size, |
| 3206 | struct event_format *event, const char *format, |
| 3207 | int len_arg, struct print_arg *arg) |
| 3208 | { |
| 3209 | struct pevent *pevent = event->pevent; |
| 3210 | struct print_flag_sym *flag; |
| 3211 | unsigned long long val, fval; |
| 3212 | unsigned long addr; |
| 3213 | char *str; |
| 3214 | int print; |
| 3215 | int len; |
| 3216 | |
| 3217 | switch (arg->type) { |
| 3218 | case PRINT_NULL: |
| 3219 | /* ?? */ |
| 3220 | return; |
| 3221 | case PRINT_ATOM: |
| 3222 | print_str_to_seq(s, format, len_arg, arg->atom.atom); |
| 3223 | return; |
| 3224 | case PRINT_FIELD: |
| 3225 | if (!arg->field.field) { |
| 3226 | arg->field.field = pevent_find_any_field(event, arg->field.name); |
| 3227 | if (!arg->field.field) |
| 3228 | die("field %s not found", arg->field.name); |
| 3229 | } |
| 3230 | /* Zero sized fields, mean the rest of the data */ |
| 3231 | len = arg->field.field->size ? : size - arg->field.field->offset; |
| 3232 | |
| 3233 | /* |
| 3234 | * Some events pass in pointers. If this is not an array |
| 3235 | * and the size is the same as long_size, assume that it |
| 3236 | * is a pointer. |
| 3237 | */ |
| 3238 | if (!(arg->field.field->flags & FIELD_IS_ARRAY) && |
| 3239 | arg->field.field->size == pevent->long_size) { |
| 3240 | addr = *(unsigned long *)(data + arg->field.field->offset); |
| 3241 | trace_seq_printf(s, "%lx", addr); |
| 3242 | break; |
| 3243 | } |
| 3244 | str = malloc_or_die(len + 1); |
| 3245 | memcpy(str, data + arg->field.field->offset, len); |
| 3246 | str[len] = 0; |
| 3247 | print_str_to_seq(s, format, len_arg, str); |
| 3248 | free(str); |
| 3249 | break; |
| 3250 | case PRINT_FLAGS: |
| 3251 | val = eval_num_arg(data, size, event, arg->flags.field); |
| 3252 | print = 0; |
| 3253 | for (flag = arg->flags.flags; flag; flag = flag->next) { |
| 3254 | fval = eval_flag(flag->value); |
| 3255 | if (!val && !fval) { |
| 3256 | print_str_to_seq(s, format, len_arg, flag->str); |
| 3257 | break; |
| 3258 | } |
| 3259 | if (fval && (val & fval) == fval) { |
| 3260 | if (print && arg->flags.delim) |
| 3261 | trace_seq_puts(s, arg->flags.delim); |
| 3262 | print_str_to_seq(s, format, len_arg, flag->str); |
| 3263 | print = 1; |
| 3264 | val &= ~fval; |
| 3265 | } |
| 3266 | } |
| 3267 | break; |
| 3268 | case PRINT_SYMBOL: |
| 3269 | val = eval_num_arg(data, size, event, arg->symbol.field); |
| 3270 | for (flag = arg->symbol.symbols; flag; flag = flag->next) { |
| 3271 | fval = eval_flag(flag->value); |
| 3272 | if (val == fval) { |
| 3273 | print_str_to_seq(s, format, len_arg, flag->str); |
| 3274 | break; |
| 3275 | } |
| 3276 | } |
| 3277 | break; |
| 3278 | |
| 3279 | case PRINT_TYPE: |
| 3280 | break; |
| 3281 | case PRINT_STRING: { |
| 3282 | int str_offset; |
| 3283 | |
| 3284 | if (arg->string.offset == -1) { |
| 3285 | struct format_field *f; |
| 3286 | |
| 3287 | f = pevent_find_any_field(event, arg->string.string); |
| 3288 | arg->string.offset = f->offset; |
| 3289 | } |
| 3290 | str_offset = data2host4(pevent, data + arg->string.offset); |
| 3291 | str_offset &= 0xffff; |
| 3292 | print_str_to_seq(s, format, len_arg, ((char *)data) + str_offset); |
| 3293 | break; |
| 3294 | } |
| 3295 | case PRINT_BSTRING: |
| 3296 | trace_seq_printf(s, format, arg->string.string); |
| 3297 | break; |
| 3298 | case PRINT_OP: |
| 3299 | /* |
| 3300 | * The only op for string should be ? : |
| 3301 | */ |
| 3302 | if (arg->op.op[0] != '?') |
| 3303 | return; |
| 3304 | val = eval_num_arg(data, size, event, arg->op.left); |
| 3305 | if (val) |
| 3306 | print_str_arg(s, data, size, event, |
| 3307 | format, len_arg, arg->op.right->op.left); |
| 3308 | else |
| 3309 | print_str_arg(s, data, size, event, |
| 3310 | format, len_arg, arg->op.right->op.right); |
| 3311 | break; |
| 3312 | case PRINT_FUNC: |
| 3313 | process_defined_func(s, data, size, event, arg); |
| 3314 | break; |
| 3315 | default: |
| 3316 | /* well... */ |
| 3317 | break; |
| 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | static unsigned long long |
| 3322 | process_defined_func(struct trace_seq *s, void *data, int size, |
| 3323 | struct event_format *event, struct print_arg *arg) |
| 3324 | { |
| 3325 | struct pevent_function_handler *func_handle = arg->func.func; |
| 3326 | struct pevent_func_params *param; |
| 3327 | unsigned long long *args; |
| 3328 | unsigned long long ret; |
| 3329 | struct print_arg *farg; |
| 3330 | struct trace_seq str; |
| 3331 | struct save_str { |
| 3332 | struct save_str *next; |
| 3333 | char *str; |
| 3334 | } *strings = NULL, *string; |
| 3335 | int i; |
| 3336 | |
| 3337 | if (!func_handle->nr_args) { |
| 3338 | ret = (*func_handle->func)(s, NULL); |
| 3339 | goto out; |
| 3340 | } |
| 3341 | |
| 3342 | farg = arg->func.args; |
| 3343 | param = func_handle->params; |
| 3344 | |
| 3345 | args = malloc_or_die(sizeof(*args) * func_handle->nr_args); |
| 3346 | for (i = 0; i < func_handle->nr_args; i++) { |
| 3347 | switch (param->type) { |
| 3348 | case PEVENT_FUNC_ARG_INT: |
| 3349 | case PEVENT_FUNC_ARG_LONG: |
| 3350 | case PEVENT_FUNC_ARG_PTR: |
| 3351 | args[i] = eval_num_arg(data, size, event, farg); |
| 3352 | break; |
| 3353 | case PEVENT_FUNC_ARG_STRING: |
| 3354 | trace_seq_init(&str); |
| 3355 | print_str_arg(&str, data, size, event, "%s", -1, farg); |
| 3356 | trace_seq_terminate(&str); |
| 3357 | string = malloc_or_die(sizeof(*string)); |
| 3358 | string->next = strings; |
| 3359 | string->str = strdup(str.buffer); |
| 3360 | strings = string; |
| 3361 | trace_seq_destroy(&str); |
| 3362 | break; |
| 3363 | default: |
| 3364 | /* |
| 3365 | * Something went totally wrong, this is not |
| 3366 | * an input error, something in this code broke. |
| 3367 | */ |
| 3368 | die("Unexpected end of arguments\n"); |
| 3369 | break; |
| 3370 | } |
| 3371 | farg = farg->next; |
| 3372 | } |
| 3373 | |
| 3374 | ret = (*func_handle->func)(s, args); |
| 3375 | free(args); |
| 3376 | while (strings) { |
| 3377 | string = strings; |
| 3378 | strings = string->next; |
| 3379 | free(string->str); |
| 3380 | free(string); |
| 3381 | } |
| 3382 | |
| 3383 | out: |
| 3384 | /* TBD : handle return type here */ |
| 3385 | return ret; |
| 3386 | } |
| 3387 | |
| 3388 | static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struct event_format *event) |
| 3389 | { |
| 3390 | struct pevent *pevent = event->pevent; |
| 3391 | struct format_field *field, *ip_field; |
| 3392 | struct print_arg *args, *arg, **next; |
| 3393 | unsigned long long ip, val; |
| 3394 | char *ptr; |
| 3395 | void *bptr; |
| 3396 | |
| 3397 | field = pevent->bprint_buf_field; |
| 3398 | ip_field = pevent->bprint_ip_field; |
| 3399 | |
| 3400 | if (!field) { |
| 3401 | field = pevent_find_field(event, "buf"); |
| 3402 | if (!field) |
| 3403 | die("can't find buffer field for binary printk"); |
| 3404 | ip_field = pevent_find_field(event, "ip"); |
| 3405 | if (!ip_field) |
| 3406 | die("can't find ip field for binary printk"); |
| 3407 | pevent->bprint_buf_field = field; |
| 3408 | pevent->bprint_ip_field = ip_field; |
| 3409 | } |
| 3410 | |
| 3411 | ip = pevent_read_number(pevent, data + ip_field->offset, ip_field->size); |
| 3412 | |
| 3413 | /* |
| 3414 | * The first arg is the IP pointer. |
| 3415 | */ |
| 3416 | args = alloc_arg(); |
| 3417 | arg = args; |
| 3418 | arg->next = NULL; |
| 3419 | next = &arg->next; |
| 3420 | |
| 3421 | arg->type = PRINT_ATOM; |
| 3422 | arg->atom.atom = malloc_or_die(32); |
| 3423 | sprintf(arg->atom.atom, "%lld", ip); |
| 3424 | |
| 3425 | /* skip the first "%pf : " */ |
| 3426 | for (ptr = fmt + 6, bptr = data + field->offset; |
| 3427 | bptr < data + size && *ptr; ptr++) { |
| 3428 | int ls = 0; |
| 3429 | |
| 3430 | if (*ptr == '%') { |
| 3431 | process_again: |
| 3432 | ptr++; |
| 3433 | switch (*ptr) { |
| 3434 | case '%': |
| 3435 | break; |
| 3436 | case 'l': |
| 3437 | ls++; |
| 3438 | goto process_again; |
| 3439 | case 'L': |
| 3440 | ls = 2; |
| 3441 | goto process_again; |
| 3442 | case '0' ... '9': |
| 3443 | goto process_again; |
| 3444 | case 'p': |
| 3445 | ls = 1; |
| 3446 | /* fall through */ |
| 3447 | case 'd': |
| 3448 | case 'u': |
| 3449 | case 'x': |
| 3450 | case 'i': |
| 3451 | /* the pointers are always 4 bytes aligned */ |
| 3452 | bptr = (void *)(((unsigned long)bptr + 3) & |
| 3453 | ~3); |
| 3454 | switch (ls) { |
| 3455 | case 0: |
| 3456 | ls = 4; |
| 3457 | break; |
| 3458 | case 1: |
| 3459 | ls = pevent->long_size; |
| 3460 | break; |
| 3461 | case 2: |
| 3462 | ls = 8; |
| 3463 | default: |
| 3464 | break; |
| 3465 | } |
| 3466 | val = pevent_read_number(pevent, bptr, ls); |
| 3467 | bptr += ls; |
| 3468 | arg = alloc_arg(); |
| 3469 | arg->next = NULL; |
| 3470 | arg->type = PRINT_ATOM; |
| 3471 | arg->atom.atom = malloc_or_die(32); |
| 3472 | sprintf(arg->atom.atom, "%lld", val); |
| 3473 | *next = arg; |
| 3474 | next = &arg->next; |
| 3475 | break; |
| 3476 | case 's': |
| 3477 | arg = alloc_arg(); |
| 3478 | arg->next = NULL; |
| 3479 | arg->type = PRINT_BSTRING; |
| 3480 | arg->string.string = strdup(bptr); |
| 3481 | bptr += strlen(bptr) + 1; |
| 3482 | *next = arg; |
| 3483 | next = &arg->next; |
| 3484 | default: |
| 3485 | break; |
| 3486 | } |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | return args; |
| 3491 | } |
| 3492 | |
| 3493 | static void free_args(struct print_arg *args) |
| 3494 | { |
| 3495 | struct print_arg *next; |
| 3496 | |
| 3497 | while (args) { |
| 3498 | next = args->next; |
| 3499 | |
| 3500 | free_arg(args); |
| 3501 | args = next; |
| 3502 | } |
| 3503 | } |
| 3504 | |
| 3505 | static char * |
| 3506 | get_bprint_format(void *data, int size __unused, struct event_format *event) |
| 3507 | { |
| 3508 | struct pevent *pevent = event->pevent; |
| 3509 | unsigned long long addr; |
| 3510 | struct format_field *field; |
| 3511 | struct printk_map *printk; |
| 3512 | char *format; |
| 3513 | char *p; |
| 3514 | |
| 3515 | field = pevent->bprint_fmt_field; |
| 3516 | |
| 3517 | if (!field) { |
| 3518 | field = pevent_find_field(event, "fmt"); |
| 3519 | if (!field) |
| 3520 | die("can't find format field for binary printk"); |
| 3521 | pevent->bprint_fmt_field = field; |
| 3522 | } |
| 3523 | |
| 3524 | addr = pevent_read_number(pevent, data + field->offset, field->size); |
| 3525 | |
| 3526 | printk = find_printk(pevent, addr); |
| 3527 | if (!printk) { |
| 3528 | format = malloc_or_die(45); |
| 3529 | sprintf(format, "%%pf : (NO FORMAT FOUND at %llx)\n", |
| 3530 | addr); |
| 3531 | return format; |
| 3532 | } |
| 3533 | |
| 3534 | p = printk->printk; |
| 3535 | /* Remove any quotes. */ |
| 3536 | if (*p == '"') |
| 3537 | p++; |
| 3538 | format = malloc_or_die(strlen(p) + 10); |
| 3539 | sprintf(format, "%s : %s", "%pf", p); |
| 3540 | /* remove ending quotes and new line since we will add one too */ |
| 3541 | p = format + strlen(format) - 1; |
| 3542 | if (*p == '"') |
| 3543 | *p = 0; |
| 3544 | |
| 3545 | p -= 2; |
| 3546 | if (strcmp(p, "\\n") == 0) |
| 3547 | *p = 0; |
| 3548 | |
| 3549 | return format; |
| 3550 | } |
| 3551 | |
| 3552 | static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size, |
| 3553 | struct event_format *event, struct print_arg *arg) |
| 3554 | { |
| 3555 | unsigned char *buf; |
| 3556 | char *fmt = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x"; |
| 3557 | |
| 3558 | if (arg->type == PRINT_FUNC) { |
| 3559 | process_defined_func(s, data, size, event, arg); |
| 3560 | return; |
| 3561 | } |
| 3562 | |
| 3563 | if (arg->type != PRINT_FIELD) { |
| 3564 | trace_seq_printf(s, "ARG TYPE NOT FIELD BUT %d", |
| 3565 | arg->type); |
| 3566 | return; |
| 3567 | } |
| 3568 | |
| 3569 | if (mac == 'm') |
| 3570 | fmt = "%.2x%.2x%.2x%.2x%.2x%.2x"; |
| 3571 | if (!arg->field.field) { |
| 3572 | arg->field.field = |
| 3573 | pevent_find_any_field(event, arg->field.name); |
| 3574 | if (!arg->field.field) |
| 3575 | die("field %s not found", arg->field.name); |
| 3576 | } |
| 3577 | if (arg->field.field->size != 6) { |
| 3578 | trace_seq_printf(s, "INVALIDMAC"); |
| 3579 | return; |
| 3580 | } |
| 3581 | buf = data + arg->field.field->offset; |
| 3582 | trace_seq_printf(s, fmt, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); |
| 3583 | } |
| 3584 | |
| 3585 | static void print_event_fields(struct trace_seq *s, void *data, int size, |
| 3586 | struct event_format *event) |
| 3587 | { |
| 3588 | struct format_field *field; |
| 3589 | unsigned long long val; |
| 3590 | unsigned int offset, len, i; |
| 3591 | |
| 3592 | field = event->format.fields; |
| 3593 | while (field) { |
| 3594 | trace_seq_printf(s, " %s=", field->name); |
| 3595 | if (field->flags & FIELD_IS_ARRAY) { |
| 3596 | offset = field->offset; |
| 3597 | len = field->size; |
| 3598 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 3599 | val = pevent_read_number(event->pevent, data + offset, len); |
| 3600 | offset = val; |
| 3601 | len = offset >> 16; |
| 3602 | offset &= 0xffff; |
| 3603 | } |
| 3604 | if (field->flags & FIELD_IS_STRING) { |
| 3605 | trace_seq_printf(s, "%s", (char *)data + offset); |
| 3606 | } else { |
| 3607 | trace_seq_puts(s, "ARRAY["); |
| 3608 | for (i = 0; i < len; i++) { |
| 3609 | if (i) |
| 3610 | trace_seq_puts(s, ", "); |
| 3611 | trace_seq_printf(s, "%02x", |
| 3612 | *((unsigned char *)data + offset + i)); |
| 3613 | } |
| 3614 | trace_seq_putc(s, ']'); |
| 3615 | } |
| 3616 | } else { |
| 3617 | val = pevent_read_number(event->pevent, data + field->offset, |
| 3618 | field->size); |
| 3619 | if (field->flags & FIELD_IS_POINTER) { |
| 3620 | trace_seq_printf(s, "0x%llx", val); |
| 3621 | } else if (field->flags & FIELD_IS_SIGNED) { |
| 3622 | switch (field->size) { |
| 3623 | case 4: |
| 3624 | /* |
| 3625 | * If field is long then print it in hex. |
| 3626 | * A long usually stores pointers. |
| 3627 | */ |
| 3628 | if (field->flags & FIELD_IS_LONG) |
| 3629 | trace_seq_printf(s, "0x%x", (int)val); |
| 3630 | else |
| 3631 | trace_seq_printf(s, "%d", (int)val); |
| 3632 | break; |
| 3633 | case 2: |
| 3634 | trace_seq_printf(s, "%2d", (short)val); |
| 3635 | break; |
| 3636 | case 1: |
| 3637 | trace_seq_printf(s, "%1d", (char)val); |
| 3638 | break; |
| 3639 | default: |
| 3640 | trace_seq_printf(s, "%lld", val); |
| 3641 | } |
| 3642 | } else { |
| 3643 | if (field->flags & FIELD_IS_LONG) |
| 3644 | trace_seq_printf(s, "0x%llx", val); |
| 3645 | else |
| 3646 | trace_seq_printf(s, "%llu", val); |
| 3647 | } |
| 3648 | } |
| 3649 | field = field->next; |
| 3650 | } |
| 3651 | } |
| 3652 | |
| 3653 | static void pretty_print(struct trace_seq *s, void *data, int size, struct event_format *event) |
| 3654 | { |
| 3655 | struct pevent *pevent = event->pevent; |
| 3656 | struct print_fmt *print_fmt = &event->print_fmt; |
| 3657 | struct print_arg *arg = print_fmt->args; |
| 3658 | struct print_arg *args = NULL; |
| 3659 | const char *ptr = print_fmt->format; |
| 3660 | unsigned long long val; |
| 3661 | struct func_map *func; |
| 3662 | const char *saveptr; |
| 3663 | char *bprint_fmt = NULL; |
| 3664 | char format[32]; |
| 3665 | int show_func; |
| 3666 | int len_as_arg; |
| 3667 | int len_arg; |
| 3668 | int len; |
| 3669 | int ls; |
| 3670 | |
| 3671 | if (event->flags & EVENT_FL_FAILED) { |
| 3672 | trace_seq_printf(s, "[FAILED TO PARSE]"); |
| 3673 | print_event_fields(s, data, size, event); |
| 3674 | return; |
| 3675 | } |
| 3676 | |
| 3677 | if (event->flags & EVENT_FL_ISBPRINT) { |
| 3678 | bprint_fmt = get_bprint_format(data, size, event); |
| 3679 | args = make_bprint_args(bprint_fmt, data, size, event); |
| 3680 | arg = args; |
| 3681 | ptr = bprint_fmt; |
| 3682 | } |
| 3683 | |
| 3684 | for (; *ptr; ptr++) { |
| 3685 | ls = 0; |
| 3686 | if (*ptr == '\\') { |
| 3687 | ptr++; |
| 3688 | switch (*ptr) { |
| 3689 | case 'n': |
| 3690 | trace_seq_putc(s, '\n'); |
| 3691 | break; |
| 3692 | case 't': |
| 3693 | trace_seq_putc(s, '\t'); |
| 3694 | break; |
| 3695 | case 'r': |
| 3696 | trace_seq_putc(s, '\r'); |
| 3697 | break; |
| 3698 | case '\\': |
| 3699 | trace_seq_putc(s, '\\'); |
| 3700 | break; |
| 3701 | default: |
| 3702 | trace_seq_putc(s, *ptr); |
| 3703 | break; |
| 3704 | } |
| 3705 | |
| 3706 | } else if (*ptr == '%') { |
| 3707 | saveptr = ptr; |
| 3708 | show_func = 0; |
| 3709 | len_as_arg = 0; |
| 3710 | cont_process: |
| 3711 | ptr++; |
| 3712 | switch (*ptr) { |
| 3713 | case '%': |
| 3714 | trace_seq_putc(s, '%'); |
| 3715 | break; |
| 3716 | case '#': |
| 3717 | /* FIXME: need to handle properly */ |
| 3718 | goto cont_process; |
| 3719 | case 'h': |
| 3720 | ls--; |
| 3721 | goto cont_process; |
| 3722 | case 'l': |
| 3723 | ls++; |
| 3724 | goto cont_process; |
| 3725 | case 'L': |
| 3726 | ls = 2; |
| 3727 | goto cont_process; |
| 3728 | case '*': |
| 3729 | /* The argument is the length. */ |
| 3730 | if (!arg) |
| 3731 | die("no argument match"); |
| 3732 | len_arg = eval_num_arg(data, size, event, arg); |
| 3733 | len_as_arg = 1; |
| 3734 | arg = arg->next; |
| 3735 | goto cont_process; |
| 3736 | case '.': |
| 3737 | case 'z': |
| 3738 | case 'Z': |
| 3739 | case '0' ... '9': |
| 3740 | goto cont_process; |
| 3741 | case 'p': |
| 3742 | if (pevent->long_size == 4) |
| 3743 | ls = 1; |
| 3744 | else |
| 3745 | ls = 2; |
| 3746 | |
| 3747 | if (*(ptr+1) == 'F' || |
| 3748 | *(ptr+1) == 'f') { |
| 3749 | ptr++; |
| 3750 | show_func = *ptr; |
| 3751 | } else if (*(ptr+1) == 'M' || *(ptr+1) == 'm') { |
| 3752 | print_mac_arg(s, *(ptr+1), data, size, event, arg); |
| 3753 | ptr++; |
| 3754 | break; |
| 3755 | } |
| 3756 | |
| 3757 | /* fall through */ |
| 3758 | case 'd': |
| 3759 | case 'i': |
| 3760 | case 'x': |
| 3761 | case 'X': |
| 3762 | case 'u': |
| 3763 | if (!arg) |
| 3764 | die("no argument match"); |
| 3765 | |
| 3766 | len = ((unsigned long)ptr + 1) - |
| 3767 | (unsigned long)saveptr; |
| 3768 | |
| 3769 | /* should never happen */ |
| 3770 | if (len > 31) |
| 3771 | die("bad format!"); |
| 3772 | |
| 3773 | memcpy(format, saveptr, len); |
| 3774 | format[len] = 0; |
| 3775 | |
| 3776 | val = eval_num_arg(data, size, event, arg); |
| 3777 | arg = arg->next; |
| 3778 | |
| 3779 | if (show_func) { |
| 3780 | func = find_func(pevent, val); |
| 3781 | if (func) { |
| 3782 | trace_seq_puts(s, func->func); |
| 3783 | if (show_func == 'F') |
| 3784 | trace_seq_printf(s, |
| 3785 | "+0x%llx", |
| 3786 | val - func->addr); |
| 3787 | break; |
| 3788 | } |
| 3789 | } |
| 3790 | if (pevent->long_size == 8 && ls) { |
| 3791 | char *p; |
| 3792 | |
| 3793 | ls = 2; |
| 3794 | /* make %l into %ll */ |
| 3795 | p = strchr(format, 'l'); |
| 3796 | if (p) |
| 3797 | memmove(p, p+1, strlen(p)+1); |
| 3798 | else if (strcmp(format, "%p") == 0) |
| 3799 | strcpy(format, "0x%llx"); |
| 3800 | } |
| 3801 | switch (ls) { |
| 3802 | case -2: |
| 3803 | if (len_as_arg) |
| 3804 | trace_seq_printf(s, format, len_arg, (char)val); |
| 3805 | else |
| 3806 | trace_seq_printf(s, format, (char)val); |
| 3807 | break; |
| 3808 | case -1: |
| 3809 | if (len_as_arg) |
| 3810 | trace_seq_printf(s, format, len_arg, (short)val); |
| 3811 | else |
| 3812 | trace_seq_printf(s, format, (short)val); |
| 3813 | break; |
| 3814 | case 0: |
| 3815 | if (len_as_arg) |
| 3816 | trace_seq_printf(s, format, len_arg, (int)val); |
| 3817 | else |
| 3818 | trace_seq_printf(s, format, (int)val); |
| 3819 | break; |
| 3820 | case 1: |
| 3821 | if (len_as_arg) |
| 3822 | trace_seq_printf(s, format, len_arg, (long)val); |
| 3823 | else |
| 3824 | trace_seq_printf(s, format, (long)val); |
| 3825 | break; |
| 3826 | case 2: |
| 3827 | if (len_as_arg) |
| 3828 | trace_seq_printf(s, format, len_arg, |
| 3829 | (long long)val); |
| 3830 | else |
| 3831 | trace_seq_printf(s, format, (long long)val); |
| 3832 | break; |
| 3833 | default: |
| 3834 | die("bad count (%d)", ls); |
| 3835 | } |
| 3836 | break; |
| 3837 | case 's': |
| 3838 | if (!arg) |
| 3839 | die("no matching argument"); |
| 3840 | |
| 3841 | len = ((unsigned long)ptr + 1) - |
| 3842 | (unsigned long)saveptr; |
| 3843 | |
| 3844 | /* should never happen */ |
| 3845 | if (len > 31) |
| 3846 | die("bad format!"); |
| 3847 | |
| 3848 | memcpy(format, saveptr, len); |
| 3849 | format[len] = 0; |
| 3850 | if (!len_as_arg) |
| 3851 | len_arg = -1; |
| 3852 | print_str_arg(s, data, size, event, |
| 3853 | format, len_arg, arg); |
| 3854 | arg = arg->next; |
| 3855 | break; |
| 3856 | default: |
| 3857 | trace_seq_printf(s, ">%c<", *ptr); |
| 3858 | |
| 3859 | } |
| 3860 | } else |
| 3861 | trace_seq_putc(s, *ptr); |
| 3862 | } |
| 3863 | |
| 3864 | if (args) { |
| 3865 | free_args(args); |
| 3866 | free(bprint_fmt); |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | /** |
| 3871 | * pevent_data_lat_fmt - parse the data for the latency format |
| 3872 | * @pevent: a handle to the pevent |
| 3873 | * @s: the trace_seq to write to |
| 3874 | * @data: the raw data to read from |
| 3875 | * @size: currently unused. |
| 3876 | * |
| 3877 | * This parses out the Latency format (interrupts disabled, |
| 3878 | * need rescheduling, in hard/soft interrupt, preempt count |
| 3879 | * and lock depth) and places it into the trace_seq. |
| 3880 | */ |
| 3881 | void pevent_data_lat_fmt(struct pevent *pevent, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 3882 | struct trace_seq *s, struct pevent_record *record) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 3883 | { |
| 3884 | static int check_lock_depth = 1; |
| 3885 | static int lock_depth_exists; |
| 3886 | unsigned int lat_flags; |
| 3887 | unsigned int pc; |
| 3888 | int lock_depth; |
| 3889 | int hardirq; |
| 3890 | int softirq; |
| 3891 | void *data = record->data; |
| 3892 | |
| 3893 | lat_flags = parse_common_flags(pevent, data); |
| 3894 | pc = parse_common_pc(pevent, data); |
| 3895 | /* lock_depth may not always exist */ |
| 3896 | if (check_lock_depth) { |
| 3897 | struct format_field *field; |
| 3898 | struct event_format *event; |
| 3899 | |
| 3900 | check_lock_depth = 0; |
| 3901 | event = pevent->events[0]; |
| 3902 | field = pevent_find_common_field(event, "common_lock_depth"); |
| 3903 | if (field) |
| 3904 | lock_depth_exists = 1; |
| 3905 | } |
| 3906 | if (lock_depth_exists) |
| 3907 | lock_depth = parse_common_lock_depth(pevent, data); |
| 3908 | |
| 3909 | hardirq = lat_flags & TRACE_FLAG_HARDIRQ; |
| 3910 | softirq = lat_flags & TRACE_FLAG_SOFTIRQ; |
| 3911 | |
| 3912 | trace_seq_printf(s, "%c%c%c", |
| 3913 | (lat_flags & TRACE_FLAG_IRQS_OFF) ? 'd' : |
| 3914 | (lat_flags & TRACE_FLAG_IRQS_NOSUPPORT) ? |
| 3915 | 'X' : '.', |
| 3916 | (lat_flags & TRACE_FLAG_NEED_RESCHED) ? |
| 3917 | 'N' : '.', |
| 3918 | (hardirq && softirq) ? 'H' : |
| 3919 | hardirq ? 'h' : softirq ? 's' : '.'); |
| 3920 | |
| 3921 | if (pc) |
| 3922 | trace_seq_printf(s, "%x", pc); |
| 3923 | else |
| 3924 | trace_seq_putc(s, '.'); |
| 3925 | |
| 3926 | if (lock_depth_exists) { |
| 3927 | if (lock_depth < 0) |
| 3928 | trace_seq_putc(s, '.'); |
| 3929 | else |
| 3930 | trace_seq_printf(s, "%d", lock_depth); |
| 3931 | } |
| 3932 | |
| 3933 | trace_seq_terminate(s); |
| 3934 | } |
| 3935 | |
| 3936 | /** |
| 3937 | * pevent_data_type - parse out the given event type |
| 3938 | * @pevent: a handle to the pevent |
| 3939 | * @rec: the record to read from |
| 3940 | * |
| 3941 | * This returns the event id from the @rec. |
| 3942 | */ |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 3943 | int pevent_data_type(struct pevent *pevent, struct pevent_record *rec) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 3944 | { |
| 3945 | return trace_parse_common_type(pevent, rec->data); |
| 3946 | } |
| 3947 | |
| 3948 | /** |
| 3949 | * pevent_data_event_from_type - find the event by a given type |
| 3950 | * @pevent: a handle to the pevent |
| 3951 | * @type: the type of the event. |
| 3952 | * |
| 3953 | * This returns the event form a given @type; |
| 3954 | */ |
| 3955 | struct event_format *pevent_data_event_from_type(struct pevent *pevent, int type) |
| 3956 | { |
| 3957 | return pevent_find_event(pevent, type); |
| 3958 | } |
| 3959 | |
| 3960 | /** |
| 3961 | * pevent_data_pid - parse the PID from raw data |
| 3962 | * @pevent: a handle to the pevent |
| 3963 | * @rec: the record to parse |
| 3964 | * |
| 3965 | * This returns the PID from a raw data. |
| 3966 | */ |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 3967 | int pevent_data_pid(struct pevent *pevent, struct pevent_record *rec) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 3968 | { |
| 3969 | return parse_common_pid(pevent, rec->data); |
| 3970 | } |
| 3971 | |
| 3972 | /** |
| 3973 | * pevent_data_comm_from_pid - return the command line from PID |
| 3974 | * @pevent: a handle to the pevent |
| 3975 | * @pid: the PID of the task to search for |
| 3976 | * |
| 3977 | * This returns a pointer to the command line that has the given |
| 3978 | * @pid. |
| 3979 | */ |
| 3980 | const char *pevent_data_comm_from_pid(struct pevent *pevent, int pid) |
| 3981 | { |
| 3982 | const char *comm; |
| 3983 | |
| 3984 | comm = find_cmdline(pevent, pid); |
| 3985 | return comm; |
| 3986 | } |
| 3987 | |
| 3988 | /** |
| 3989 | * pevent_data_comm_from_pid - parse the data into the print format |
| 3990 | * @s: the trace_seq to write to |
| 3991 | * @event: the handle to the event |
| 3992 | * @cpu: the cpu the event was recorded on |
| 3993 | * @data: the raw data |
| 3994 | * @size: the size of the raw data |
| 3995 | * @nsecs: the timestamp of the event |
| 3996 | * |
| 3997 | * This parses the raw @data using the given @event information and |
| 3998 | * writes the print format into the trace_seq. |
| 3999 | */ |
| 4000 | void pevent_event_info(struct trace_seq *s, struct event_format *event, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4001 | struct pevent_record *record) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4002 | { |
| 4003 | int print_pretty = 1; |
| 4004 | |
| 4005 | if (event->pevent->print_raw) |
| 4006 | print_event_fields(s, record->data, record->size, event); |
| 4007 | else { |
| 4008 | |
| 4009 | if (event->handler) |
| 4010 | print_pretty = event->handler(s, record, event, |
| 4011 | event->context); |
| 4012 | |
| 4013 | if (print_pretty) |
| 4014 | pretty_print(s, record->data, record->size, event); |
| 4015 | } |
| 4016 | |
| 4017 | trace_seq_terminate(s); |
| 4018 | } |
| 4019 | |
| 4020 | void pevent_print_event(struct pevent *pevent, struct trace_seq *s, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4021 | struct pevent_record *record) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4022 | { |
| 4023 | static char *spaces = " "; /* 20 spaces */ |
| 4024 | struct event_format *event; |
| 4025 | unsigned long secs; |
| 4026 | unsigned long usecs; |
Steven Rostedt | 4dc1024 | 2012-04-06 00:47:57 +0200 | [diff] [blame] | 4027 | unsigned long nsecs; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4028 | const char *comm; |
| 4029 | void *data = record->data; |
| 4030 | int type; |
| 4031 | int pid; |
| 4032 | int len; |
Steven Rostedt | 4dc1024 | 2012-04-06 00:47:57 +0200 | [diff] [blame] | 4033 | int p; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4034 | |
| 4035 | secs = record->ts / NSECS_PER_SEC; |
Steven Rostedt | 4dc1024 | 2012-04-06 00:47:57 +0200 | [diff] [blame] | 4036 | nsecs = record->ts - secs * NSECS_PER_SEC; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4037 | |
| 4038 | if (record->size < 0) { |
| 4039 | do_warning("ug! negative record size %d", record->size); |
| 4040 | return; |
| 4041 | } |
| 4042 | |
| 4043 | type = trace_parse_common_type(pevent, data); |
| 4044 | |
| 4045 | event = pevent_find_event(pevent, type); |
| 4046 | if (!event) { |
| 4047 | do_warning("ug! no event found for type %d", type); |
| 4048 | return; |
| 4049 | } |
| 4050 | |
| 4051 | pid = parse_common_pid(pevent, data); |
| 4052 | comm = find_cmdline(pevent, pid); |
| 4053 | |
| 4054 | if (pevent->latency_format) { |
| 4055 | trace_seq_printf(s, "%8.8s-%-5d %3d", |
| 4056 | comm, pid, record->cpu); |
| 4057 | pevent_data_lat_fmt(pevent, s, record); |
| 4058 | } else |
| 4059 | trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu); |
| 4060 | |
Steven Rostedt | 4dc1024 | 2012-04-06 00:47:57 +0200 | [diff] [blame] | 4061 | if (pevent->flags & PEVENT_NSEC_OUTPUT) { |
| 4062 | usecs = nsecs; |
| 4063 | p = 9; |
| 4064 | } else { |
| 4065 | usecs = (nsecs + 500) / NSECS_PER_USEC; |
| 4066 | p = 6; |
| 4067 | } |
| 4068 | |
| 4069 | trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4070 | |
| 4071 | /* Space out the event names evenly. */ |
| 4072 | len = strlen(event->name); |
| 4073 | if (len < 20) |
| 4074 | trace_seq_printf(s, "%.*s", 20 - len, spaces); |
| 4075 | |
| 4076 | pevent_event_info(s, event, record); |
| 4077 | } |
| 4078 | |
| 4079 | static int events_id_cmp(const void *a, const void *b) |
| 4080 | { |
| 4081 | struct event_format * const * ea = a; |
| 4082 | struct event_format * const * eb = b; |
| 4083 | |
| 4084 | if ((*ea)->id < (*eb)->id) |
| 4085 | return -1; |
| 4086 | |
| 4087 | if ((*ea)->id > (*eb)->id) |
| 4088 | return 1; |
| 4089 | |
| 4090 | return 0; |
| 4091 | } |
| 4092 | |
| 4093 | static int events_name_cmp(const void *a, const void *b) |
| 4094 | { |
| 4095 | struct event_format * const * ea = a; |
| 4096 | struct event_format * const * eb = b; |
| 4097 | int res; |
| 4098 | |
| 4099 | res = strcmp((*ea)->name, (*eb)->name); |
| 4100 | if (res) |
| 4101 | return res; |
| 4102 | |
| 4103 | res = strcmp((*ea)->system, (*eb)->system); |
| 4104 | if (res) |
| 4105 | return res; |
| 4106 | |
| 4107 | return events_id_cmp(a, b); |
| 4108 | } |
| 4109 | |
| 4110 | static int events_system_cmp(const void *a, const void *b) |
| 4111 | { |
| 4112 | struct event_format * const * ea = a; |
| 4113 | struct event_format * const * eb = b; |
| 4114 | int res; |
| 4115 | |
| 4116 | res = strcmp((*ea)->system, (*eb)->system); |
| 4117 | if (res) |
| 4118 | return res; |
| 4119 | |
| 4120 | res = strcmp((*ea)->name, (*eb)->name); |
| 4121 | if (res) |
| 4122 | return res; |
| 4123 | |
| 4124 | return events_id_cmp(a, b); |
| 4125 | } |
| 4126 | |
| 4127 | struct event_format **pevent_list_events(struct pevent *pevent, enum event_sort_type sort_type) |
| 4128 | { |
| 4129 | struct event_format **events; |
| 4130 | int (*sort)(const void *a, const void *b); |
| 4131 | |
| 4132 | events = pevent->sort_events; |
| 4133 | |
| 4134 | if (events && pevent->last_type == sort_type) |
| 4135 | return events; |
| 4136 | |
| 4137 | if (!events) { |
| 4138 | events = malloc(sizeof(*events) * (pevent->nr_events + 1)); |
| 4139 | if (!events) |
| 4140 | return NULL; |
| 4141 | |
| 4142 | memcpy(events, pevent->events, sizeof(*events) * pevent->nr_events); |
| 4143 | events[pevent->nr_events] = NULL; |
| 4144 | |
| 4145 | pevent->sort_events = events; |
| 4146 | |
| 4147 | /* the internal events are sorted by id */ |
| 4148 | if (sort_type == EVENT_SORT_ID) { |
| 4149 | pevent->last_type = sort_type; |
| 4150 | return events; |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | switch (sort_type) { |
| 4155 | case EVENT_SORT_ID: |
| 4156 | sort = events_id_cmp; |
| 4157 | break; |
| 4158 | case EVENT_SORT_NAME: |
| 4159 | sort = events_name_cmp; |
| 4160 | break; |
| 4161 | case EVENT_SORT_SYSTEM: |
| 4162 | sort = events_system_cmp; |
| 4163 | break; |
| 4164 | default: |
| 4165 | return events; |
| 4166 | } |
| 4167 | |
| 4168 | qsort(events, pevent->nr_events, sizeof(*events), sort); |
| 4169 | pevent->last_type = sort_type; |
| 4170 | |
| 4171 | return events; |
| 4172 | } |
| 4173 | |
| 4174 | static struct format_field ** |
| 4175 | get_event_fields(const char *type, const char *name, |
| 4176 | int count, struct format_field *list) |
| 4177 | { |
| 4178 | struct format_field **fields; |
| 4179 | struct format_field *field; |
| 4180 | int i = 0; |
| 4181 | |
| 4182 | fields = malloc_or_die(sizeof(*fields) * (count + 1)); |
| 4183 | for (field = list; field; field = field->next) { |
| 4184 | fields[i++] = field; |
| 4185 | if (i == count + 1) { |
| 4186 | do_warning("event %s has more %s fields than specified", |
| 4187 | name, type); |
| 4188 | i--; |
| 4189 | break; |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | if (i != count) |
| 4194 | do_warning("event %s has less %s fields than specified", |
| 4195 | name, type); |
| 4196 | |
| 4197 | fields[i] = NULL; |
| 4198 | |
| 4199 | return fields; |
| 4200 | } |
| 4201 | |
| 4202 | /** |
| 4203 | * pevent_event_common_fields - return a list of common fields for an event |
| 4204 | * @event: the event to return the common fields of. |
| 4205 | * |
| 4206 | * Returns an allocated array of fields. The last item in the array is NULL. |
| 4207 | * The array must be freed with free(). |
| 4208 | */ |
| 4209 | struct format_field **pevent_event_common_fields(struct event_format *event) |
| 4210 | { |
| 4211 | return get_event_fields("common", event->name, |
| 4212 | event->format.nr_common, |
| 4213 | event->format.common_fields); |
| 4214 | } |
| 4215 | |
| 4216 | /** |
| 4217 | * pevent_event_fields - return a list of event specific fields for an event |
| 4218 | * @event: the event to return the fields of. |
| 4219 | * |
| 4220 | * Returns an allocated array of fields. The last item in the array is NULL. |
| 4221 | * The array must be freed with free(). |
| 4222 | */ |
| 4223 | struct format_field **pevent_event_fields(struct event_format *event) |
| 4224 | { |
| 4225 | return get_event_fields("event", event->name, |
| 4226 | event->format.nr_fields, |
| 4227 | event->format.fields); |
| 4228 | } |
| 4229 | |
| 4230 | static void print_fields(struct trace_seq *s, struct print_flag_sym *field) |
| 4231 | { |
| 4232 | trace_seq_printf(s, "{ %s, %s }", field->value, field->str); |
| 4233 | if (field->next) { |
| 4234 | trace_seq_puts(s, ", "); |
| 4235 | print_fields(s, field->next); |
| 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | /* for debugging */ |
| 4240 | static void print_args(struct print_arg *args) |
| 4241 | { |
| 4242 | int print_paren = 1; |
| 4243 | struct trace_seq s; |
| 4244 | |
| 4245 | switch (args->type) { |
| 4246 | case PRINT_NULL: |
| 4247 | printf("null"); |
| 4248 | break; |
| 4249 | case PRINT_ATOM: |
| 4250 | printf("%s", args->atom.atom); |
| 4251 | break; |
| 4252 | case PRINT_FIELD: |
| 4253 | printf("REC->%s", args->field.name); |
| 4254 | break; |
| 4255 | case PRINT_FLAGS: |
| 4256 | printf("__print_flags("); |
| 4257 | print_args(args->flags.field); |
| 4258 | printf(", %s, ", args->flags.delim); |
| 4259 | trace_seq_init(&s); |
| 4260 | print_fields(&s, args->flags.flags); |
| 4261 | trace_seq_do_printf(&s); |
| 4262 | trace_seq_destroy(&s); |
| 4263 | printf(")"); |
| 4264 | break; |
| 4265 | case PRINT_SYMBOL: |
| 4266 | printf("__print_symbolic("); |
| 4267 | print_args(args->symbol.field); |
| 4268 | printf(", "); |
| 4269 | trace_seq_init(&s); |
| 4270 | print_fields(&s, args->symbol.symbols); |
| 4271 | trace_seq_do_printf(&s); |
| 4272 | trace_seq_destroy(&s); |
| 4273 | printf(")"); |
| 4274 | break; |
| 4275 | case PRINT_STRING: |
| 4276 | case PRINT_BSTRING: |
| 4277 | printf("__get_str(%s)", args->string.string); |
| 4278 | break; |
| 4279 | case PRINT_TYPE: |
| 4280 | printf("(%s)", args->typecast.type); |
| 4281 | print_args(args->typecast.item); |
| 4282 | break; |
| 4283 | case PRINT_OP: |
| 4284 | if (strcmp(args->op.op, ":") == 0) |
| 4285 | print_paren = 0; |
| 4286 | if (print_paren) |
| 4287 | printf("("); |
| 4288 | print_args(args->op.left); |
| 4289 | printf(" %s ", args->op.op); |
| 4290 | print_args(args->op.right); |
| 4291 | if (print_paren) |
| 4292 | printf(")"); |
| 4293 | break; |
| 4294 | default: |
| 4295 | /* we should warn... */ |
| 4296 | return; |
| 4297 | } |
| 4298 | if (args->next) { |
| 4299 | printf("\n"); |
| 4300 | print_args(args->next); |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | static void parse_header_field(const char *field, |
| 4305 | int *offset, int *size, int mandatory) |
| 4306 | { |
| 4307 | unsigned long long save_input_buf_ptr; |
| 4308 | unsigned long long save_input_buf_siz; |
| 4309 | char *token; |
| 4310 | int type; |
| 4311 | |
| 4312 | save_input_buf_ptr = input_buf_ptr; |
| 4313 | save_input_buf_siz = input_buf_siz; |
| 4314 | |
| 4315 | if (read_expected(EVENT_ITEM, "field") < 0) |
| 4316 | return; |
| 4317 | if (read_expected(EVENT_OP, ":") < 0) |
| 4318 | return; |
| 4319 | |
| 4320 | /* type */ |
| 4321 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 4322 | goto fail; |
| 4323 | free_token(token); |
| 4324 | |
| 4325 | /* |
| 4326 | * If this is not a mandatory field, then test it first. |
| 4327 | */ |
| 4328 | if (mandatory) { |
| 4329 | if (read_expected(EVENT_ITEM, field) < 0) |
| 4330 | return; |
| 4331 | } else { |
| 4332 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 4333 | goto fail; |
| 4334 | if (strcmp(token, field) != 0) |
| 4335 | goto discard; |
| 4336 | free_token(token); |
| 4337 | } |
| 4338 | |
| 4339 | if (read_expected(EVENT_OP, ";") < 0) |
| 4340 | return; |
| 4341 | if (read_expected(EVENT_ITEM, "offset") < 0) |
| 4342 | return; |
| 4343 | if (read_expected(EVENT_OP, ":") < 0) |
| 4344 | return; |
| 4345 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 4346 | goto fail; |
| 4347 | *offset = atoi(token); |
| 4348 | free_token(token); |
| 4349 | if (read_expected(EVENT_OP, ";") < 0) |
| 4350 | return; |
| 4351 | if (read_expected(EVENT_ITEM, "size") < 0) |
| 4352 | return; |
| 4353 | if (read_expected(EVENT_OP, ":") < 0) |
| 4354 | return; |
| 4355 | if (read_expect_type(EVENT_ITEM, &token) < 0) |
| 4356 | goto fail; |
| 4357 | *size = atoi(token); |
| 4358 | free_token(token); |
| 4359 | if (read_expected(EVENT_OP, ";") < 0) |
| 4360 | return; |
| 4361 | type = read_token(&token); |
| 4362 | if (type != EVENT_NEWLINE) { |
| 4363 | /* newer versions of the kernel have a "signed" type */ |
| 4364 | if (type != EVENT_ITEM) |
| 4365 | goto fail; |
| 4366 | |
| 4367 | if (strcmp(token, "signed") != 0) |
| 4368 | goto fail; |
| 4369 | |
| 4370 | free_token(token); |
| 4371 | |
| 4372 | if (read_expected(EVENT_OP, ":") < 0) |
| 4373 | return; |
| 4374 | |
| 4375 | if (read_expect_type(EVENT_ITEM, &token)) |
| 4376 | goto fail; |
| 4377 | |
| 4378 | free_token(token); |
| 4379 | if (read_expected(EVENT_OP, ";") < 0) |
| 4380 | return; |
| 4381 | |
| 4382 | if (read_expect_type(EVENT_NEWLINE, &token)) |
| 4383 | goto fail; |
| 4384 | } |
| 4385 | fail: |
| 4386 | free_token(token); |
| 4387 | return; |
| 4388 | |
| 4389 | discard: |
| 4390 | input_buf_ptr = save_input_buf_ptr; |
| 4391 | input_buf_siz = save_input_buf_siz; |
| 4392 | *offset = 0; |
| 4393 | *size = 0; |
| 4394 | free_token(token); |
| 4395 | } |
| 4396 | |
| 4397 | /** |
| 4398 | * pevent_parse_header_page - parse the data stored in the header page |
| 4399 | * @pevent: the handle to the pevent |
| 4400 | * @buf: the buffer storing the header page format string |
| 4401 | * @size: the size of @buf |
| 4402 | * @long_size: the long size to use if there is no header |
| 4403 | * |
| 4404 | * This parses the header page format for information on the |
| 4405 | * ring buffer used. The @buf should be copied from |
| 4406 | * |
| 4407 | * /sys/kernel/debug/tracing/events/header_page |
| 4408 | */ |
| 4409 | int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size, |
| 4410 | int long_size) |
| 4411 | { |
| 4412 | int ignore; |
| 4413 | |
| 4414 | if (!size) { |
| 4415 | /* |
| 4416 | * Old kernels did not have header page info. |
| 4417 | * Sorry but we just use what we find here in user space. |
| 4418 | */ |
| 4419 | pevent->header_page_ts_size = sizeof(long long); |
| 4420 | pevent->header_page_size_size = long_size; |
| 4421 | pevent->header_page_data_offset = sizeof(long long) + long_size; |
| 4422 | pevent->old_format = 1; |
| 4423 | return -1; |
| 4424 | } |
| 4425 | init_input_buf(buf, size); |
| 4426 | |
| 4427 | parse_header_field("timestamp", &pevent->header_page_ts_offset, |
| 4428 | &pevent->header_page_ts_size, 1); |
| 4429 | parse_header_field("commit", &pevent->header_page_size_offset, |
| 4430 | &pevent->header_page_size_size, 1); |
| 4431 | parse_header_field("overwrite", &pevent->header_page_overwrite, |
| 4432 | &ignore, 0); |
| 4433 | parse_header_field("data", &pevent->header_page_data_offset, |
| 4434 | &pevent->header_page_data_size, 1); |
| 4435 | |
| 4436 | return 0; |
| 4437 | } |
| 4438 | |
| 4439 | static int event_matches(struct event_format *event, |
| 4440 | int id, const char *sys_name, |
| 4441 | const char *event_name) |
| 4442 | { |
| 4443 | if (id >= 0 && id != event->id) |
| 4444 | return 0; |
| 4445 | |
| 4446 | if (event_name && (strcmp(event_name, event->name) != 0)) |
| 4447 | return 0; |
| 4448 | |
| 4449 | if (sys_name && (strcmp(sys_name, event->system) != 0)) |
| 4450 | return 0; |
| 4451 | |
| 4452 | return 1; |
| 4453 | } |
| 4454 | |
| 4455 | static void free_handler(struct event_handler *handle) |
| 4456 | { |
| 4457 | free((void *)handle->sys_name); |
| 4458 | free((void *)handle->event_name); |
| 4459 | free(handle); |
| 4460 | } |
| 4461 | |
| 4462 | static int find_event_handle(struct pevent *pevent, struct event_format *event) |
| 4463 | { |
| 4464 | struct event_handler *handle, **next; |
| 4465 | |
| 4466 | for (next = &pevent->handlers; *next; |
| 4467 | next = &(*next)->next) { |
| 4468 | handle = *next; |
| 4469 | if (event_matches(event, handle->id, |
| 4470 | handle->sys_name, |
| 4471 | handle->event_name)) |
| 4472 | break; |
| 4473 | } |
| 4474 | |
| 4475 | if (!(*next)) |
| 4476 | return 0; |
| 4477 | |
| 4478 | pr_stat("overriding event (%d) %s:%s with new print handler", |
| 4479 | event->id, event->system, event->name); |
| 4480 | |
| 4481 | event->handler = handle->func; |
| 4482 | event->context = handle->context; |
| 4483 | |
| 4484 | *next = handle->next; |
| 4485 | free_handler(handle); |
| 4486 | |
| 4487 | return 1; |
| 4488 | } |
| 4489 | |
| 4490 | /** |
| 4491 | * pevent_parse_event - parse the event format |
| 4492 | * @pevent: the handle to the pevent |
| 4493 | * @buf: the buffer storing the event format string |
| 4494 | * @size: the size of @buf |
| 4495 | * @sys: the system the event belongs to |
| 4496 | * |
| 4497 | * This parses the event format and creates an event structure |
| 4498 | * to quickly parse raw data for a given event. |
| 4499 | * |
| 4500 | * These files currently come from: |
| 4501 | * |
| 4502 | * /sys/kernel/debug/tracing/events/.../.../format |
| 4503 | */ |
| 4504 | int pevent_parse_event(struct pevent *pevent, |
| 4505 | const char *buf, unsigned long size, |
| 4506 | const char *sys) |
| 4507 | { |
| 4508 | struct event_format *event; |
| 4509 | int ret; |
| 4510 | |
| 4511 | init_input_buf(buf, size); |
| 4512 | |
| 4513 | event = alloc_event(); |
| 4514 | if (!event) |
| 4515 | return -ENOMEM; |
| 4516 | |
| 4517 | event->name = event_read_name(); |
| 4518 | if (!event->name) { |
| 4519 | /* Bad event? */ |
| 4520 | free(event); |
| 4521 | return -1; |
| 4522 | } |
| 4523 | |
| 4524 | if (strcmp(sys, "ftrace") == 0) { |
| 4525 | |
| 4526 | event->flags |= EVENT_FL_ISFTRACE; |
| 4527 | |
| 4528 | if (strcmp(event->name, "bprint") == 0) |
| 4529 | event->flags |= EVENT_FL_ISBPRINT; |
| 4530 | } |
| 4531 | |
| 4532 | event->id = event_read_id(); |
| 4533 | if (event->id < 0) |
| 4534 | die("failed to read event id"); |
| 4535 | |
| 4536 | event->system = strdup(sys); |
| 4537 | |
| 4538 | /* Add pevent to event so that it can be referenced */ |
| 4539 | event->pevent = pevent; |
| 4540 | |
| 4541 | ret = event_read_format(event); |
| 4542 | if (ret < 0) { |
| 4543 | do_warning("failed to read event format for %s", event->name); |
| 4544 | goto event_failed; |
| 4545 | } |
| 4546 | |
| 4547 | /* |
| 4548 | * If the event has an override, don't print warnings if the event |
| 4549 | * print format fails to parse. |
| 4550 | */ |
| 4551 | if (find_event_handle(pevent, event)) |
| 4552 | show_warning = 0; |
| 4553 | |
| 4554 | ret = event_read_print(event); |
| 4555 | if (ret < 0) { |
| 4556 | do_warning("failed to read event print fmt for %s", |
| 4557 | event->name); |
| 4558 | show_warning = 1; |
| 4559 | goto event_failed; |
| 4560 | } |
| 4561 | show_warning = 1; |
| 4562 | |
| 4563 | add_event(pevent, event); |
| 4564 | |
| 4565 | if (!ret && (event->flags & EVENT_FL_ISFTRACE)) { |
| 4566 | struct format_field *field; |
| 4567 | struct print_arg *arg, **list; |
| 4568 | |
| 4569 | /* old ftrace had no args */ |
| 4570 | |
| 4571 | list = &event->print_fmt.args; |
| 4572 | for (field = event->format.fields; field; field = field->next) { |
| 4573 | arg = alloc_arg(); |
| 4574 | *list = arg; |
| 4575 | list = &arg->next; |
| 4576 | arg->type = PRINT_FIELD; |
| 4577 | arg->field.name = strdup(field->name); |
| 4578 | arg->field.field = field; |
| 4579 | } |
| 4580 | return 0; |
| 4581 | } |
| 4582 | |
| 4583 | #define PRINT_ARGS 0 |
| 4584 | if (PRINT_ARGS && event->print_fmt.args) |
| 4585 | print_args(event->print_fmt.args); |
| 4586 | |
| 4587 | return 0; |
| 4588 | |
| 4589 | event_failed: |
| 4590 | event->flags |= EVENT_FL_FAILED; |
| 4591 | /* still add it even if it failed */ |
| 4592 | add_event(pevent, event); |
| 4593 | return -1; |
| 4594 | } |
| 4595 | |
| 4596 | int get_field_val(struct trace_seq *s, struct format_field *field, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4597 | const char *name, struct pevent_record *record, |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4598 | unsigned long long *val, int err) |
| 4599 | { |
| 4600 | if (!field) { |
| 4601 | if (err) |
| 4602 | trace_seq_printf(s, "<CANT FIND FIELD %s>", name); |
| 4603 | return -1; |
| 4604 | } |
| 4605 | |
| 4606 | if (pevent_read_number_field(field, record->data, val)) { |
| 4607 | if (err) |
| 4608 | trace_seq_printf(s, " %s=INVALID", name); |
| 4609 | return -1; |
| 4610 | } |
| 4611 | |
| 4612 | return 0; |
| 4613 | } |
| 4614 | |
| 4615 | /** |
| 4616 | * pevent_get_field_raw - return the raw pointer into the data field |
| 4617 | * @s: The seq to print to on error |
| 4618 | * @event: the event that the field is for |
| 4619 | * @name: The name of the field |
| 4620 | * @record: The record with the field name. |
| 4621 | * @len: place to store the field length. |
| 4622 | * @err: print default error if failed. |
| 4623 | * |
| 4624 | * Returns a pointer into record->data of the field and places |
| 4625 | * the length of the field in @len. |
| 4626 | * |
| 4627 | * On failure, it returns NULL. |
| 4628 | */ |
| 4629 | void *pevent_get_field_raw(struct trace_seq *s, struct event_format *event, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4630 | const char *name, struct pevent_record *record, |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4631 | int *len, int err) |
| 4632 | { |
| 4633 | struct format_field *field; |
| 4634 | void *data = record->data; |
| 4635 | unsigned offset; |
| 4636 | int dummy; |
| 4637 | |
| 4638 | if (!event) |
| 4639 | return NULL; |
| 4640 | |
| 4641 | field = pevent_find_field(event, name); |
| 4642 | |
| 4643 | if (!field) { |
| 4644 | if (err) |
| 4645 | trace_seq_printf(s, "<CANT FIND FIELD %s>", name); |
| 4646 | return NULL; |
| 4647 | } |
| 4648 | |
| 4649 | /* Allow @len to be NULL */ |
| 4650 | if (!len) |
| 4651 | len = &dummy; |
| 4652 | |
| 4653 | offset = field->offset; |
| 4654 | if (field->flags & FIELD_IS_DYNAMIC) { |
| 4655 | offset = pevent_read_number(event->pevent, |
| 4656 | data + offset, field->size); |
| 4657 | *len = offset >> 16; |
| 4658 | offset &= 0xffff; |
| 4659 | } else |
| 4660 | *len = field->size; |
| 4661 | |
| 4662 | return data + offset; |
| 4663 | } |
| 4664 | |
| 4665 | /** |
| 4666 | * pevent_get_field_val - find a field and return its value |
| 4667 | * @s: The seq to print to on error |
| 4668 | * @event: the event that the field is for |
| 4669 | * @name: The name of the field |
| 4670 | * @record: The record with the field name. |
| 4671 | * @val: place to store the value of the field. |
| 4672 | * @err: print default error if failed. |
| 4673 | * |
| 4674 | * Returns 0 on success -1 on field not found. |
| 4675 | */ |
| 4676 | int pevent_get_field_val(struct trace_seq *s, struct event_format *event, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4677 | const char *name, struct pevent_record *record, |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4678 | unsigned long long *val, int err) |
| 4679 | { |
| 4680 | struct format_field *field; |
| 4681 | |
| 4682 | if (!event) |
| 4683 | return -1; |
| 4684 | |
| 4685 | field = pevent_find_field(event, name); |
| 4686 | |
| 4687 | return get_field_val(s, field, name, record, val, err); |
| 4688 | } |
| 4689 | |
| 4690 | /** |
| 4691 | * pevent_get_common_field_val - find a common field and return its value |
| 4692 | * @s: The seq to print to on error |
| 4693 | * @event: the event that the field is for |
| 4694 | * @name: The name of the field |
| 4695 | * @record: The record with the field name. |
| 4696 | * @val: place to store the value of the field. |
| 4697 | * @err: print default error if failed. |
| 4698 | * |
| 4699 | * Returns 0 on success -1 on field not found. |
| 4700 | */ |
| 4701 | int pevent_get_common_field_val(struct trace_seq *s, struct event_format *event, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4702 | const char *name, struct pevent_record *record, |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4703 | unsigned long long *val, int err) |
| 4704 | { |
| 4705 | struct format_field *field; |
| 4706 | |
| 4707 | if (!event) |
| 4708 | return -1; |
| 4709 | |
| 4710 | field = pevent_find_common_field(event, name); |
| 4711 | |
| 4712 | return get_field_val(s, field, name, record, val, err); |
| 4713 | } |
| 4714 | |
| 4715 | /** |
| 4716 | * pevent_get_any_field_val - find a any field and return its value |
| 4717 | * @s: The seq to print to on error |
| 4718 | * @event: the event that the field is for |
| 4719 | * @name: The name of the field |
| 4720 | * @record: The record with the field name. |
| 4721 | * @val: place to store the value of the field. |
| 4722 | * @err: print default error if failed. |
| 4723 | * |
| 4724 | * Returns 0 on success -1 on field not found. |
| 4725 | */ |
| 4726 | int pevent_get_any_field_val(struct trace_seq *s, struct event_format *event, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4727 | const char *name, struct pevent_record *record, |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4728 | unsigned long long *val, int err) |
| 4729 | { |
| 4730 | struct format_field *field; |
| 4731 | |
| 4732 | if (!event) |
| 4733 | return -1; |
| 4734 | |
| 4735 | field = pevent_find_any_field(event, name); |
| 4736 | |
| 4737 | return get_field_val(s, field, name, record, val, err); |
| 4738 | } |
| 4739 | |
| 4740 | /** |
| 4741 | * pevent_print_num_field - print a field and a format |
| 4742 | * @s: The seq to print to |
| 4743 | * @fmt: The printf format to print the field with. |
| 4744 | * @event: the event that the field is for |
| 4745 | * @name: The name of the field |
| 4746 | * @record: The record with the field name. |
| 4747 | * @err: print default error if failed. |
| 4748 | * |
| 4749 | * Returns: 0 on success, -1 field not fould, or 1 if buffer is full. |
| 4750 | */ |
| 4751 | int pevent_print_num_field(struct trace_seq *s, const char *fmt, |
| 4752 | struct event_format *event, const char *name, |
Steven Rostedt | 1c69818 | 2012-04-06 00:48:06 +0200 | [diff] [blame] | 4753 | struct pevent_record *record, int err) |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4754 | { |
| 4755 | struct format_field *field = pevent_find_field(event, name); |
| 4756 | unsigned long long val; |
| 4757 | |
| 4758 | if (!field) |
| 4759 | goto failed; |
| 4760 | |
| 4761 | if (pevent_read_number_field(field, record->data, &val)) |
| 4762 | goto failed; |
| 4763 | |
| 4764 | return trace_seq_printf(s, fmt, val); |
| 4765 | |
| 4766 | failed: |
| 4767 | if (err) |
| 4768 | trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name); |
| 4769 | return -1; |
| 4770 | } |
| 4771 | |
| 4772 | static void free_func_handle(struct pevent_function_handler *func) |
| 4773 | { |
| 4774 | struct pevent_func_params *params; |
| 4775 | |
| 4776 | free(func->name); |
| 4777 | |
| 4778 | while (func->params) { |
| 4779 | params = func->params; |
| 4780 | func->params = params->next; |
| 4781 | free(params); |
| 4782 | } |
| 4783 | |
| 4784 | free(func); |
| 4785 | } |
| 4786 | |
| 4787 | /** |
| 4788 | * pevent_register_print_function - register a helper function |
| 4789 | * @pevent: the handle to the pevent |
| 4790 | * @func: the function to process the helper function |
| 4791 | * @name: the name of the helper function |
| 4792 | * @parameters: A list of enum pevent_func_arg_type |
| 4793 | * |
| 4794 | * Some events may have helper functions in the print format arguments. |
| 4795 | * This allows a plugin to dynmically create a way to process one |
| 4796 | * of these functions. |
| 4797 | * |
| 4798 | * The @parameters is a variable list of pevent_func_arg_type enums that |
| 4799 | * must end with PEVENT_FUNC_ARG_VOID. |
| 4800 | */ |
| 4801 | int pevent_register_print_function(struct pevent *pevent, |
| 4802 | pevent_func_handler func, |
| 4803 | enum pevent_func_arg_type ret_type, |
| 4804 | char *name, ...) |
| 4805 | { |
| 4806 | struct pevent_function_handler *func_handle; |
| 4807 | struct pevent_func_params **next_param; |
| 4808 | struct pevent_func_params *param; |
| 4809 | enum pevent_func_arg_type type; |
| 4810 | va_list ap; |
| 4811 | |
| 4812 | func_handle = find_func_handler(pevent, name); |
| 4813 | if (func_handle) { |
| 4814 | /* |
| 4815 | * This is most like caused by the users own |
| 4816 | * plugins updating the function. This overrides the |
| 4817 | * system defaults. |
| 4818 | */ |
| 4819 | pr_stat("override of function helper '%s'", name); |
| 4820 | remove_func_handler(pevent, name); |
| 4821 | } |
| 4822 | |
| 4823 | func_handle = malloc_or_die(sizeof(*func_handle)); |
| 4824 | memset(func_handle, 0, sizeof(*func_handle)); |
| 4825 | |
| 4826 | func_handle->ret_type = ret_type; |
| 4827 | func_handle->name = strdup(name); |
| 4828 | func_handle->func = func; |
| 4829 | if (!func_handle->name) |
| 4830 | die("Failed to allocate function name"); |
| 4831 | |
| 4832 | next_param = &(func_handle->params); |
| 4833 | va_start(ap, name); |
| 4834 | for (;;) { |
| 4835 | type = va_arg(ap, enum pevent_func_arg_type); |
| 4836 | if (type == PEVENT_FUNC_ARG_VOID) |
| 4837 | break; |
| 4838 | |
| 4839 | if (type < 0 || type >= PEVENT_FUNC_ARG_MAX_TYPES) { |
| 4840 | warning("Invalid argument type %d", type); |
| 4841 | goto out_free; |
| 4842 | } |
| 4843 | |
| 4844 | param = malloc_or_die(sizeof(*param)); |
| 4845 | param->type = type; |
| 4846 | param->next = NULL; |
| 4847 | |
| 4848 | *next_param = param; |
| 4849 | next_param = &(param->next); |
| 4850 | |
| 4851 | func_handle->nr_args++; |
| 4852 | } |
| 4853 | va_end(ap); |
| 4854 | |
| 4855 | func_handle->next = pevent->func_handlers; |
| 4856 | pevent->func_handlers = func_handle; |
| 4857 | |
| 4858 | return 0; |
| 4859 | out_free: |
| 4860 | va_end(ap); |
| 4861 | free_func_handle(func_handle); |
| 4862 | return -1; |
| 4863 | } |
| 4864 | |
| 4865 | /** |
| 4866 | * pevent_register_event_handle - register a way to parse an event |
| 4867 | * @pevent: the handle to the pevent |
| 4868 | * @id: the id of the event to register |
| 4869 | * @sys_name: the system name the event belongs to |
| 4870 | * @event_name: the name of the event |
| 4871 | * @func: the function to call to parse the event information |
| 4872 | * |
| 4873 | * This function allows a developer to override the parsing of |
| 4874 | * a given event. If for some reason the default print format |
| 4875 | * is not sufficient, this function will register a function |
| 4876 | * for an event to be used to parse the data instead. |
| 4877 | * |
| 4878 | * If @id is >= 0, then it is used to find the event. |
| 4879 | * else @sys_name and @event_name are used. |
| 4880 | */ |
| 4881 | int pevent_register_event_handler(struct pevent *pevent, |
| 4882 | int id, char *sys_name, char *event_name, |
| 4883 | pevent_event_handler_func func, |
| 4884 | void *context) |
| 4885 | { |
| 4886 | struct event_format *event; |
| 4887 | struct event_handler *handle; |
| 4888 | |
| 4889 | if (id >= 0) { |
| 4890 | /* search by id */ |
| 4891 | event = pevent_find_event(pevent, id); |
| 4892 | if (!event) |
| 4893 | goto not_found; |
| 4894 | if (event_name && (strcmp(event_name, event->name) != 0)) |
| 4895 | goto not_found; |
| 4896 | if (sys_name && (strcmp(sys_name, event->system) != 0)) |
| 4897 | goto not_found; |
| 4898 | } else { |
| 4899 | event = pevent_find_event_by_name(pevent, sys_name, event_name); |
| 4900 | if (!event) |
| 4901 | goto not_found; |
| 4902 | } |
| 4903 | |
| 4904 | pr_stat("overriding event (%d) %s:%s with new print handler", |
| 4905 | event->id, event->system, event->name); |
| 4906 | |
| 4907 | event->handler = func; |
| 4908 | event->context = context; |
| 4909 | return 0; |
| 4910 | |
| 4911 | not_found: |
| 4912 | /* Save for later use. */ |
| 4913 | handle = malloc_or_die(sizeof(*handle)); |
Steven Rostedt | 42c8013 | 2012-04-06 00:48:05 +0200 | [diff] [blame] | 4914 | memset(handle, 0, sizeof(*handle)); |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4915 | handle->id = id; |
| 4916 | if (event_name) |
| 4917 | handle->event_name = strdup(event_name); |
| 4918 | if (sys_name) |
| 4919 | handle->sys_name = strdup(sys_name); |
| 4920 | |
| 4921 | handle->func = func; |
| 4922 | handle->next = pevent->handlers; |
| 4923 | pevent->handlers = handle; |
| 4924 | handle->context = context; |
| 4925 | |
| 4926 | return -1; |
| 4927 | } |
| 4928 | |
| 4929 | /** |
| 4930 | * pevent_alloc - create a pevent handle |
| 4931 | */ |
| 4932 | struct pevent *pevent_alloc(void) |
| 4933 | { |
| 4934 | struct pevent *pevent; |
| 4935 | |
| 4936 | pevent = malloc(sizeof(*pevent)); |
| 4937 | if (!pevent) |
| 4938 | return NULL; |
| 4939 | memset(pevent, 0, sizeof(*pevent)); |
| 4940 | pevent->ref_count = 1; |
| 4941 | |
| 4942 | return pevent; |
| 4943 | } |
| 4944 | |
| 4945 | void pevent_ref(struct pevent *pevent) |
| 4946 | { |
| 4947 | pevent->ref_count++; |
| 4948 | } |
| 4949 | |
| 4950 | static void free_format_fields(struct format_field *field) |
| 4951 | { |
| 4952 | struct format_field *next; |
| 4953 | |
| 4954 | while (field) { |
| 4955 | next = field->next; |
| 4956 | free(field->type); |
| 4957 | free(field->name); |
| 4958 | free(field); |
| 4959 | field = next; |
| 4960 | } |
| 4961 | } |
| 4962 | |
| 4963 | static void free_formats(struct format *format) |
| 4964 | { |
| 4965 | free_format_fields(format->common_fields); |
| 4966 | free_format_fields(format->fields); |
| 4967 | } |
| 4968 | |
| 4969 | static void free_event(struct event_format *event) |
| 4970 | { |
| 4971 | free(event->name); |
| 4972 | free(event->system); |
| 4973 | |
| 4974 | free_formats(&event->format); |
| 4975 | |
| 4976 | free(event->print_fmt.format); |
| 4977 | free_args(event->print_fmt.args); |
| 4978 | |
| 4979 | free(event); |
| 4980 | } |
| 4981 | |
| 4982 | /** |
| 4983 | * pevent_free - free a pevent handle |
| 4984 | * @pevent: the pevent handle to free |
| 4985 | */ |
| 4986 | void pevent_free(struct pevent *pevent) |
| 4987 | { |
Steven Rostedt | a2525a0 | 2012-04-06 00:48:02 +0200 | [diff] [blame] | 4988 | struct cmdline_list *cmdlist, *cmdnext; |
| 4989 | struct func_list *funclist, *funcnext; |
| 4990 | struct printk_list *printklist, *printknext; |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 4991 | struct pevent_function_handler *func_handler; |
| 4992 | struct event_handler *handle; |
| 4993 | int i; |
| 4994 | |
Steven Rostedt | a2525a0 | 2012-04-06 00:48:02 +0200 | [diff] [blame] | 4995 | if (!pevent) |
| 4996 | return; |
| 4997 | |
| 4998 | cmdlist = pevent->cmdlist; |
| 4999 | funclist = pevent->funclist; |
| 5000 | printklist = pevent->printklist; |
| 5001 | |
Steven Rostedt | f7d8235 | 2012-04-06 00:47:53 +0200 | [diff] [blame] | 5002 | pevent->ref_count--; |
| 5003 | if (pevent->ref_count) |
| 5004 | return; |
| 5005 | |
| 5006 | if (pevent->cmdlines) { |
| 5007 | for (i = 0; i < pevent->cmdline_count; i++) |
| 5008 | free(pevent->cmdlines[i].comm); |
| 5009 | free(pevent->cmdlines); |
| 5010 | } |
| 5011 | |
| 5012 | while (cmdlist) { |
| 5013 | cmdnext = cmdlist->next; |
| 5014 | free(cmdlist->comm); |
| 5015 | free(cmdlist); |
| 5016 | cmdlist = cmdnext; |
| 5017 | } |
| 5018 | |
| 5019 | if (pevent->func_map) { |
| 5020 | for (i = 0; i < pevent->func_count; i++) { |
| 5021 | free(pevent->func_map[i].func); |
| 5022 | free(pevent->func_map[i].mod); |
| 5023 | } |
| 5024 | free(pevent->func_map); |
| 5025 | } |
| 5026 | |
| 5027 | while (funclist) { |
| 5028 | funcnext = funclist->next; |
| 5029 | free(funclist->func); |
| 5030 | free(funclist->mod); |
| 5031 | free(funclist); |
| 5032 | funclist = funcnext; |
| 5033 | } |
| 5034 | |
| 5035 | while (pevent->func_handlers) { |
| 5036 | func_handler = pevent->func_handlers; |
| 5037 | pevent->func_handlers = func_handler->next; |
| 5038 | free_func_handle(func_handler); |
| 5039 | } |
| 5040 | |
| 5041 | if (pevent->printk_map) { |
| 5042 | for (i = 0; i < pevent->printk_count; i++) |
| 5043 | free(pevent->printk_map[i].printk); |
| 5044 | free(pevent->printk_map); |
| 5045 | } |
| 5046 | |
| 5047 | while (printklist) { |
| 5048 | printknext = printklist->next; |
| 5049 | free(printklist->printk); |
| 5050 | free(printklist); |
| 5051 | printklist = printknext; |
| 5052 | } |
| 5053 | |
| 5054 | for (i = 0; i < pevent->nr_events; i++) |
| 5055 | free_event(pevent->events[i]); |
| 5056 | |
| 5057 | while (pevent->handlers) { |
| 5058 | handle = pevent->handlers; |
| 5059 | pevent->handlers = handle->next; |
| 5060 | free_handler(handle); |
| 5061 | } |
| 5062 | |
| 5063 | free(pevent->events); |
| 5064 | free(pevent->sort_events); |
| 5065 | |
| 5066 | free(pevent); |
| 5067 | } |
| 5068 | |
| 5069 | void pevent_unref(struct pevent *pevent) |
| 5070 | { |
| 5071 | pevent_free(pevent); |
| 5072 | } |