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