| 1 | /* |
| 2 | * Copyright (C) 2011 Julien Desfossez |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License Version 2 as |
| 6 | * published by the Free Software Foundation; |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <config.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdint.h> |
| 22 | #include <babeltrace/babeltrace.h> |
| 23 | #include <babeltrace/ctf/events.h> |
| 24 | #include <babeltrace/ctf/callbacks.h> |
| 25 | #include <babeltrace/ctf/iterator.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <pthread.h> |
| 28 | #include <popt.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <ftw.h> |
| 31 | #include <dirent.h> |
| 32 | #include <ctype.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <unistd.h> |
| 35 | #include <string.h> |
| 36 | #include <errno.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <fts.h> |
| 39 | #include <assert.h> |
| 40 | |
| 41 | #include "lttngtoptypes.h" |
| 42 | #include "cputop.h" |
| 43 | #include "iostreamtop.h" |
| 44 | #include "cursesdisplay.h" |
| 45 | #include "common.h" |
| 46 | |
| 47 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
| 48 | |
| 49 | const char *opt_input_path; |
| 50 | |
| 51 | struct lttngtop *copy; |
| 52 | pthread_t display_thread; |
| 53 | pthread_t timer_thread; |
| 54 | |
| 55 | unsigned long refresh_display = 1 * NSEC_PER_SEC; |
| 56 | unsigned long last_display_update = 0; |
| 57 | int quit = 0; |
| 58 | |
| 59 | enum { |
| 60 | OPT_NONE = 0, |
| 61 | OPT_HELP, |
| 62 | OPT_LIST, |
| 63 | OPT_VERBOSE, |
| 64 | OPT_DEBUG, |
| 65 | OPT_NAMES, |
| 66 | }; |
| 67 | |
| 68 | static struct poptOption long_options[] = { |
| 69 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 70 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
| 71 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
| 72 | }; |
| 73 | |
| 74 | void *refresh_thread(void *p) |
| 75 | { |
| 76 | while (1) { |
| 77 | if (quit) |
| 78 | return NULL; |
| 79 | sem_wait(&pause_sem); |
| 80 | sem_post(&pause_sem); |
| 81 | sem_post(&timer); |
| 82 | sleep(refresh_display/NSEC_PER_SEC); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void *ncurses_display(void *p) |
| 87 | { |
| 88 | unsigned int current_display_index = 0; |
| 89 | |
| 90 | sem_wait(&bootstrap); |
| 91 | init_ncurses(); |
| 92 | |
| 93 | while (1) { |
| 94 | sem_wait(&timer); |
| 95 | sem_wait(&goodtodisplay); |
| 96 | sem_wait(&pause_sem); |
| 97 | |
| 98 | copy = g_ptr_array_index(copies, current_display_index); |
| 99 | assert(copy); |
| 100 | display(current_display_index++); |
| 101 | |
| 102 | sem_post(&goodtoupdate); |
| 103 | sem_post(&pause_sem); |
| 104 | |
| 105 | if (quit) { |
| 106 | reset_ncurses(); |
| 107 | pthread_exit(0); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * hook on each event to check the timestamp and refresh the display if |
| 114 | * necessary |
| 115 | */ |
| 116 | enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data) |
| 117 | { |
| 118 | unsigned long timestamp; |
| 119 | |
| 120 | timestamp = bt_ctf_get_timestamp(call_data); |
| 121 | if (timestamp == -1ULL) |
| 122 | goto error; |
| 123 | |
| 124 | if (last_display_update == 0) |
| 125 | last_display_update = timestamp; |
| 126 | |
| 127 | if (timestamp - last_display_update >= refresh_display) { |
| 128 | sem_wait(&goodtoupdate); |
| 129 | g_ptr_array_add(copies, get_copy_lttngtop(last_display_update, |
| 130 | timestamp)); |
| 131 | sem_post(&goodtodisplay); |
| 132 | sem_post(&bootstrap); |
| 133 | last_display_update = timestamp; |
| 134 | } |
| 135 | return BT_CB_OK; |
| 136 | |
| 137 | error: |
| 138 | fprintf(stderr, "check_timestamp callback error\n"); |
| 139 | return BT_CB_ERROR_STOP; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * get_perf_counter : get or create and return a perf_counter struct for |
| 144 | * either a process or a cpu (only one of the 2 parameters mandatory) |
| 145 | */ |
| 146 | struct perfcounter *get_perf_counter(const char *name, struct processtop *proc, |
| 147 | struct cputime *cpu) |
| 148 | { |
| 149 | struct perfcounter *ret, *global; |
| 150 | GHashTable *table; |
| 151 | |
| 152 | if (proc) |
| 153 | table = proc->perf; |
| 154 | else if (cpu) |
| 155 | table = cpu->perf; |
| 156 | else |
| 157 | goto error; |
| 158 | |
| 159 | ret = g_hash_table_lookup(table, (gpointer) name); |
| 160 | if (ret) |
| 161 | goto end; |
| 162 | |
| 163 | ret = g_new0(struct perfcounter, 1); |
| 164 | /* by default, make it visible in the UI */ |
| 165 | ret->visible = 1; |
| 166 | g_hash_table_insert(table, (gpointer) strdup(name), ret); |
| 167 | |
| 168 | global = g_hash_table_lookup(global_perf_liszt, (gpointer) name); |
| 169 | if (!global) { |
| 170 | global = g_new0(struct perfcounter, 1); |
| 171 | memcpy(global, ret, sizeof(struct perfcounter)); |
| 172 | /* by default, sort on the first perf context */ |
| 173 | if (g_hash_table_size(global_perf_liszt) == 0) |
| 174 | global->sort = 1; |
| 175 | g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global); |
| 176 | } |
| 177 | |
| 178 | end: |
| 179 | return ret; |
| 180 | |
| 181 | error: |
| 182 | return NULL; |
| 183 | } |
| 184 | |
| 185 | void update_perf_value(struct processtop *proc, struct cputime *cpu, |
| 186 | const char *name, int value) |
| 187 | { |
| 188 | struct perfcounter *cpu_perf, *process_perf; |
| 189 | |
| 190 | cpu_perf = get_perf_counter(name, NULL, cpu); |
| 191 | if (cpu_perf->count < value) { |
| 192 | process_perf = get_perf_counter(name, proc, NULL); |
| 193 | process_perf->count += value - cpu_perf->count; |
| 194 | cpu_perf->count = value; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void extract_perf_counter_scope(const struct bt_ctf_event *event, |
| 199 | const struct definition *scope, |
| 200 | struct processtop *proc, |
| 201 | struct cputime *cpu) |
| 202 | { |
| 203 | struct definition const * const *list = NULL; |
| 204 | unsigned int count; |
| 205 | int i, ret; |
| 206 | |
| 207 | if (!scope) |
| 208 | goto end; |
| 209 | |
| 210 | ret = bt_ctf_get_field_list(event, scope, &list, &count); |
| 211 | if (ret < 0) |
| 212 | goto end; |
| 213 | |
| 214 | for (i = 0; i < count; i++) { |
| 215 | const char *name = bt_ctf_field_name(list[i]); |
| 216 | if (strncmp(name, "perf_", 5) == 0) { |
| 217 | int value = bt_ctf_get_uint64(list[i]); |
| 218 | if (bt_ctf_field_get_error()) |
| 219 | continue; |
| 220 | update_perf_value(proc, cpu, name, value); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | end: |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event) |
| 229 | { |
| 230 | struct cputime *cpu; |
| 231 | const struct definition *scope; |
| 232 | |
| 233 | cpu = get_cpu(get_cpu_id(event)); |
| 234 | |
| 235 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); |
| 236 | extract_perf_counter_scope(event, scope, proc, cpu); |
| 237 | |
| 238 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); |
| 239 | extract_perf_counter_scope(event, scope, proc, cpu); |
| 240 | |
| 241 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT); |
| 242 | extract_perf_counter_scope(event, scope, proc, cpu); |
| 243 | } |
| 244 | |
| 245 | enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data, |
| 246 | void *private_data) |
| 247 | { |
| 248 | int pid, tid, ppid; |
| 249 | char *comm; |
| 250 | struct processtop *parent, *child; |
| 251 | unsigned long timestamp; |
| 252 | |
| 253 | /* FIXME : display nice error when missing context pid, tid, ppid and comm */ |
| 254 | |
| 255 | timestamp = bt_ctf_get_timestamp(call_data); |
| 256 | if (timestamp == -1ULL) |
| 257 | goto error; |
| 258 | |
| 259 | pid = get_context_pid(call_data); |
| 260 | if (pid == -1ULL) { |
| 261 | // fprintf(stderr, "Missing pid context info\n"); |
| 262 | goto error; |
| 263 | } |
| 264 | tid = get_context_tid(call_data); |
| 265 | if (tid == -1ULL) { |
| 266 | // fprintf(stderr, "Missing tid context info\n"); |
| 267 | goto error; |
| 268 | } |
| 269 | ppid = get_context_ppid(call_data); |
| 270 | if (ppid == -1ULL) { |
| 271 | // fprintf(stderr, "Missing ppid context info\n"); |
| 272 | goto error; |
| 273 | } |
| 274 | comm = get_context_comm(call_data); |
| 275 | if (!comm) { |
| 276 | // fprintf(stderr, "Missing procname context info\n"); |
| 277 | goto error; |
| 278 | } |
| 279 | |
| 280 | /* find or create the current process */ |
| 281 | child = find_process_tid(<tngtop, tid, comm); |
| 282 | if (!child) |
| 283 | child = add_proc(<tngtop, tid, comm, timestamp); |
| 284 | update_proc(child, pid, tid, ppid, comm); |
| 285 | |
| 286 | if (pid != tid) { |
| 287 | /* find or create the parent */ |
| 288 | parent = find_process_tid(<tngtop, pid, comm); |
| 289 | if (!parent) { |
| 290 | parent = add_proc(<tngtop, pid, comm, timestamp); |
| 291 | parent->pid = pid; |
| 292 | } |
| 293 | |
| 294 | /* attach the parent to the current process */ |
| 295 | child->threadparent = parent; |
| 296 | add_thread(parent, child); |
| 297 | } |
| 298 | |
| 299 | update_perf_counter(child, call_data); |
| 300 | |
| 301 | return BT_CB_OK; |
| 302 | |
| 303 | error: |
| 304 | return BT_CB_ERROR_STOP; |
| 305 | } |
| 306 | |
| 307 | void init_lttngtop() |
| 308 | { |
| 309 | copies = g_ptr_array_new(); |
| 310 | global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal); |
| 311 | |
| 312 | sem_init(&goodtodisplay, 0, 0); |
| 313 | sem_init(&goodtoupdate, 0, 1); |
| 314 | sem_init(&timer, 0, 1); |
| 315 | sem_init(&bootstrap, 0, 0); |
| 316 | sem_init(&pause_sem, 0, 1); |
| 317 | sem_init(&end_trace_sem, 0, 0); |
| 318 | |
| 319 | reset_global_counters(); |
| 320 | lttngtop.nbproc = 0; |
| 321 | lttngtop.nbthreads = 0; |
| 322 | lttngtop.nbfiles = 0; |
| 323 | |
| 324 | lttngtop.process_table = g_ptr_array_new(); |
| 325 | lttngtop.files_table = g_ptr_array_new(); |
| 326 | lttngtop.cpu_table = g_ptr_array_new(); |
| 327 | } |
| 328 | |
| 329 | void usage(FILE *fp) |
| 330 | { |
| 331 | fprintf(fp, "LTTngTop %s\n\n", VERSION); |
| 332 | fprintf(fp, "Usage : lttngtop /path/to/trace\n"); |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Return 0 if caller should continue, < 0 if caller should return |
| 337 | * error, > 0 if caller should exit without reporting error. |
| 338 | */ |
| 339 | static int parse_options(int argc, char **argv) |
| 340 | { |
| 341 | poptContext pc; |
| 342 | int opt, ret = 0; |
| 343 | |
| 344 | if (argc == 1) { |
| 345 | usage(stdout); |
| 346 | return 1; /* exit cleanly */ |
| 347 | } |
| 348 | |
| 349 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
| 350 | poptReadDefaultConfig(pc, 0); |
| 351 | |
| 352 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 353 | switch (opt) { |
| 354 | case OPT_HELP: |
| 355 | usage(stdout); |
| 356 | ret = 1; /* exit cleanly */ |
| 357 | goto end; |
| 358 | case OPT_LIST: |
| 359 | // list_formats(stdout); |
| 360 | ret = 1; |
| 361 | goto end; |
| 362 | case OPT_VERBOSE: |
| 363 | // babeltrace_verbose = 1; |
| 364 | break; |
| 365 | case OPT_DEBUG: |
| 366 | // babeltrace_debug = 1; |
| 367 | break; |
| 368 | case OPT_NAMES: |
| 369 | // opt_field_names = 1; |
| 370 | break; |
| 371 | default: |
| 372 | ret = -EINVAL; |
| 373 | goto end; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | opt_input_path = poptGetArg(pc); |
| 378 | if (!opt_input_path) { |
| 379 | ret = -EINVAL; |
| 380 | goto end; |
| 381 | } |
| 382 | end: |
| 383 | if (pc) { |
| 384 | poptFreeContext(pc); |
| 385 | } |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | void iter_trace(struct bt_context *bt_ctx) |
| 390 | { |
| 391 | struct bt_ctf_iter *iter; |
| 392 | struct bt_iter_pos begin_pos; |
| 393 | const struct bt_ctf_event *event; |
| 394 | int ret = 0; |
| 395 | |
| 396 | begin_pos.type = BT_SEEK_BEGIN; |
| 397 | iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL); |
| 398 | |
| 399 | /* at each event check if we need to refresh */ |
| 400 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, |
| 401 | check_timestamp, |
| 402 | NULL, NULL, NULL); |
| 403 | /* at each event, verify the status of the process table */ |
| 404 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, |
| 405 | fix_process_table, |
| 406 | NULL, NULL, NULL); |
| 407 | /* to handle the scheduling events */ |
| 408 | bt_ctf_iter_add_callback(iter, |
| 409 | g_quark_from_static_string("sched_switch"), |
| 410 | NULL, 0, handle_sched_switch, NULL, NULL, NULL); |
| 411 | /* to clean up the process table */ |
| 412 | bt_ctf_iter_add_callback(iter, |
| 413 | g_quark_from_static_string("sched_process_free"), |
| 414 | NULL, 0, handle_sched_process_free, NULL, NULL, NULL); |
| 415 | /* to get all the process from the statedumps */ |
| 416 | bt_ctf_iter_add_callback(iter, |
| 417 | g_quark_from_static_string( |
| 418 | "lttng_statedump_process_state"), |
| 419 | NULL, 0, handle_statedump_process_state, |
| 420 | NULL, NULL, NULL); |
| 421 | |
| 422 | /* for IO top */ |
| 423 | bt_ctf_iter_add_callback(iter, |
| 424 | g_quark_from_static_string("exit_syscall"), |
| 425 | NULL, 0, handle_exit_syscall, NULL, NULL, NULL); |
| 426 | bt_ctf_iter_add_callback(iter, |
| 427 | g_quark_from_static_string("sys_write"), |
| 428 | NULL, 0, handle_sys_write, NULL, NULL, NULL); |
| 429 | bt_ctf_iter_add_callback(iter, |
| 430 | g_quark_from_static_string("sys_read"), |
| 431 | NULL, 0, handle_sys_read, NULL, NULL, NULL); |
| 432 | bt_ctf_iter_add_callback(iter, |
| 433 | g_quark_from_static_string("sys_open"), |
| 434 | NULL, 0, handle_sys_open, NULL, NULL, NULL); |
| 435 | bt_ctf_iter_add_callback(iter, |
| 436 | g_quark_from_static_string("sys_close"), |
| 437 | NULL, 0, handle_sys_close, NULL, NULL, NULL); |
| 438 | bt_ctf_iter_add_callback(iter, |
| 439 | g_quark_from_static_string( |
| 440 | "lttng_statedump_file_descriptor"), |
| 441 | NULL, 0, handle_statedump_file_descriptor, |
| 442 | NULL, NULL, NULL); |
| 443 | |
| 444 | while ((event = bt_ctf_iter_read_event(iter)) != NULL) { |
| 445 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
| 446 | if (ret < 0) |
| 447 | goto end_iter; |
| 448 | } |
| 449 | |
| 450 | /* block until quit, we reached the end of the trace */ |
| 451 | sem_wait(&end_trace_sem); |
| 452 | |
| 453 | end_iter: |
| 454 | bt_ctf_iter_destroy(iter); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * bt_context_add_traces_recursive: Open a trace recursively |
| 459 | * (copied from BSD code in converter/babeltrace.c) |
| 460 | * |
| 461 | * Find each trace present in the subdirectory starting from the given |
| 462 | * path, and add them to the context. The packet_seek parameter can be |
| 463 | * NULL: this specify to use the default format packet_seek. |
| 464 | * |
| 465 | * Return: 0 on success, nonzero on failure. |
| 466 | * Unable to open toplevel: failure. |
| 467 | * Unable to open some subdirectory or file: warn and continue; |
| 468 | */ |
| 469 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, |
| 470 | const char *format_str, |
| 471 | void (*packet_seek)(struct stream_pos *pos, |
| 472 | size_t offset, int whence)) |
| 473 | { |
| 474 | FTS *tree; |
| 475 | FTSENT *node; |
| 476 | GArray *trace_ids; |
| 477 | char lpath[PATH_MAX]; |
| 478 | char * const paths[2] = { lpath, NULL }; |
| 479 | int ret = -1; |
| 480 | |
| 481 | /* |
| 482 | * Need to copy path, because fts_open can change it. |
| 483 | * It is the pointer array, not the strings, that are constant. |
| 484 | */ |
| 485 | strncpy(lpath, path, PATH_MAX); |
| 486 | lpath[PATH_MAX - 1] = '\0'; |
| 487 | |
| 488 | tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0); |
| 489 | if (tree == NULL) { |
| 490 | fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n", |
| 491 | path); |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | trace_ids = g_array_new(FALSE, TRUE, sizeof(int)); |
| 496 | |
| 497 | while ((node = fts_read(tree))) { |
| 498 | int dirfd, metafd; |
| 499 | |
| 500 | if (!(node->fts_info & FTS_D)) |
| 501 | continue; |
| 502 | |
| 503 | dirfd = open(node->fts_accpath, 0); |
| 504 | if (dirfd < 0) { |
| 505 | fprintf(stderr, "[error] [Context] Unable to open trace " |
| 506 | "directory file descriptor.\n"); |
| 507 | ret = dirfd; |
| 508 | goto error; |
| 509 | } |
| 510 | metafd = openat(dirfd, "metadata", O_RDONLY); |
| 511 | if (metafd < 0) { |
| 512 | ret = close(dirfd); |
| 513 | if (ret < 0) { |
| 514 | perror("close"); |
| 515 | goto error; |
| 516 | } |
| 517 | } else { |
| 518 | int trace_id; |
| 519 | |
| 520 | ret = close(metafd); |
| 521 | if (ret < 0) { |
| 522 | perror("close"); |
| 523 | goto error; |
| 524 | } |
| 525 | ret = close(dirfd); |
| 526 | if (ret < 0) { |
| 527 | perror("close"); |
| 528 | goto error; |
| 529 | } |
| 530 | |
| 531 | trace_id = bt_context_add_trace(ctx, |
| 532 | node->fts_accpath, format_str, |
| 533 | packet_seek, NULL, NULL); |
| 534 | if (trace_id < 0) { |
| 535 | fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s " |
| 536 | "for reading.\n", node->fts_accpath, path); |
| 537 | /* Allow to skip erroneous traces. */ |
| 538 | continue; |
| 539 | } |
| 540 | g_array_append_val(trace_ids, trace_id); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | g_array_free(trace_ids, TRUE); |
| 545 | return ret; |
| 546 | |
| 547 | error: |
| 548 | return ret; |
| 549 | } |
| 550 | |
| 551 | static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list, |
| 552 | int field_cnt, int *tid_check, int *pid_check, |
| 553 | int *procname_check, int *ppid_check) |
| 554 | { |
| 555 | int j; |
| 556 | |
| 557 | for (j = 0; j < field_cnt; j++) { |
| 558 | if (*tid_check == 0) { |
| 559 | if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "tid", 3) == 0) { |
| 560 | (*tid_check)++; |
| 561 | } |
| 562 | } |
| 563 | if (*pid_check == 0) { |
| 564 | if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "pid", 3) == 0) |
| 565 | (*pid_check)++; |
| 566 | } |
| 567 | if (*ppid_check == 0) { |
| 568 | if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "ppid", 4) == 0) |
| 569 | (*ppid_check)++; |
| 570 | } |
| 571 | if (*procname_check == 0) { |
| 572 | if (strncmp(bt_ctf_get_decl_field_name(field_list[j]), "procname", 8) == 0) |
| 573 | (*procname_check)++; |
| 574 | } |
| 575 | } |
| 576 | /* if all checks are OK, no need to continue the checks */ |
| 577 | if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 && |
| 578 | *procname_check == 1) |
| 579 | return 0; |
| 580 | |
| 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * check_requirements: check if the required context informations are available |
| 586 | * |
| 587 | * If each mandatory context information is available for at least in one |
| 588 | * event, return 0 otherwise return -1. |
| 589 | */ |
| 590 | int check_requirements(struct bt_context *ctx) |
| 591 | { |
| 592 | unsigned int i, evt_cnt, field_cnt; |
| 593 | struct bt_ctf_event_decl *const * evt_list; |
| 594 | const struct bt_ctf_field_decl *const * field_list; |
| 595 | int tid_check = 0; |
| 596 | int pid_check = 0; |
| 597 | int procname_check = 0; |
| 598 | int ppid_check = 0; |
| 599 | int ret = 0; |
| 600 | |
| 601 | bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt); |
| 602 | for (i = 0; i < evt_cnt; i++) { |
| 603 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT, |
| 604 | &field_list, &field_cnt); |
| 605 | ret = check_field_requirements(field_list, field_cnt, |
| 606 | &tid_check, &pid_check, &procname_check, |
| 607 | &ppid_check); |
| 608 | if (ret == 0) |
| 609 | goto end; |
| 610 | |
| 611 | bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT, |
| 612 | &field_list, &field_cnt); |
| 613 | ret = check_field_requirements(field_list, field_cnt, |
| 614 | &tid_check, &pid_check, &procname_check, |
| 615 | &ppid_check); |
| 616 | if (ret == 0) |
| 617 | goto end; |
| 618 | |
| 619 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT, |
| 620 | &field_list, &field_cnt); |
| 621 | ret = check_field_requirements(field_list, field_cnt, |
| 622 | &tid_check, &pid_check, &procname_check, |
| 623 | &ppid_check); |
| 624 | if (ret == 0) |
| 625 | goto end; |
| 626 | } |
| 627 | |
| 628 | if (tid_check == 0) { |
| 629 | ret = -1; |
| 630 | fprintf(stderr, "[error] missing tid context information\n"); |
| 631 | } |
| 632 | if (pid_check == 0) { |
| 633 | ret = -1; |
| 634 | fprintf(stderr, "[error] missing pid context information\n"); |
| 635 | } |
| 636 | if (ppid_check == 0) { |
| 637 | ret = -1; |
| 638 | fprintf(stderr, "[error] missing ppid context information\n"); |
| 639 | } |
| 640 | if (procname_check == 0) { |
| 641 | ret = -1; |
| 642 | fprintf(stderr, "[error] missing procname context information\n"); |
| 643 | } |
| 644 | |
| 645 | end: |
| 646 | return ret; |
| 647 | } |
| 648 | |
| 649 | int main(int argc, char **argv) |
| 650 | { |
| 651 | int ret; |
| 652 | struct bt_context *bt_ctx = NULL; |
| 653 | |
| 654 | ret = parse_options(argc, argv); |
| 655 | if (ret < 0) { |
| 656 | fprintf(stdout, "Error parsing options.\n\n"); |
| 657 | usage(stdout); |
| 658 | exit(EXIT_FAILURE); |
| 659 | } else if (ret > 0) { |
| 660 | exit(EXIT_SUCCESS); |
| 661 | } |
| 662 | |
| 663 | init_lttngtop(); |
| 664 | |
| 665 | bt_ctx = bt_context_create(); |
| 666 | ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL); |
| 667 | if (ret < 0) { |
| 668 | fprintf(stderr, "[error] Opening the trace\n"); |
| 669 | goto end; |
| 670 | } |
| 671 | |
| 672 | ret = check_requirements(bt_ctx); |
| 673 | if (ret < 0) { |
| 674 | fprintf(stderr, "[error] some mandatory contexts were missing, exiting.\n"); |
| 675 | goto end; |
| 676 | } |
| 677 | |
| 678 | pthread_create(&display_thread, NULL, ncurses_display, (void *) NULL); |
| 679 | pthread_create(&timer_thread, NULL, refresh_thread, (void *) NULL); |
| 680 | |
| 681 | iter_trace(bt_ctx); |
| 682 | |
| 683 | quit = 1; |
| 684 | pthread_join(display_thread, NULL); |
| 685 | pthread_join(timer_thread, NULL); |
| 686 | |
| 687 | end: |
| 688 | bt_context_put(bt_ctx); |
| 689 | return 0; |
| 690 | } |