| 1 | /* |
| 2 | * Copyright (C) 2013 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 | #include <sys/mman.h> |
| 41 | #include <sys/wait.h> |
| 42 | #include <lttng/lttng.h> |
| 43 | #ifdef LTTNGTOP_MMAP_LIVE |
| 44 | #include <lttng/lttngtop-helper.h> |
| 45 | #include <babeltrace/lttngtopmmappacketseek.h> |
| 46 | #include "mmap-live.h" |
| 47 | #endif /* LTTNGTOP_MMAP_LIVE */ |
| 48 | |
| 49 | #include "lttngtoptypes.h" |
| 50 | #include "cputop.h" |
| 51 | #include "iostreamtop.h" |
| 52 | #include "cursesdisplay.h" |
| 53 | #include "common.h" |
| 54 | #include "network-live.h" |
| 55 | |
| 56 | #define NET_URL_PREFIX "net://" |
| 57 | #define NET4_URL_PREFIX "net4://" |
| 58 | #define NET6_URL_PREFIX "net6://" |
| 59 | |
| 60 | #define DEFAULT_FILE_ARRAY_SIZE 1 |
| 61 | |
| 62 | const char *opt_input_path; |
| 63 | int opt_textdump; |
| 64 | int opt_child; |
| 65 | int opt_begin; |
| 66 | int opt_all; |
| 67 | |
| 68 | int quit = 0; |
| 69 | /* We need at least one valid trace to start processing. */ |
| 70 | int valid_trace = 0; |
| 71 | |
| 72 | struct lttngtop *copy; |
| 73 | pthread_t display_thread; |
| 74 | pthread_t timer_thread; |
| 75 | |
| 76 | unsigned long refresh_display = 1 * NSEC_PER_SEC; |
| 77 | unsigned long last_display_update = 0; |
| 78 | unsigned long last_event_ts = 0; |
| 79 | struct syscall *last_syscall; |
| 80 | |
| 81 | /* list of FDs available for being read with snapshots */ |
| 82 | struct bt_mmap_stream_list mmap_list; |
| 83 | GPtrArray *lttng_consumer_stream_array; |
| 84 | int sessiond_metadata, consumerd_metadata; |
| 85 | struct lttng_consumer_local_data *ctx = NULL; |
| 86 | /* list of snapshots currently not consumed */ |
| 87 | GPtrArray *available_snapshots; |
| 88 | sem_t metadata_available; |
| 89 | int reload_trace = 0; |
| 90 | |
| 91 | uint64_t prev_ts = 0; |
| 92 | |
| 93 | enum { |
| 94 | OPT_NONE = 0, |
| 95 | OPT_HELP, |
| 96 | OPT_TEXTDUMP, |
| 97 | OPT_PID, |
| 98 | OPT_CHILD, |
| 99 | OPT_HOSTNAME, |
| 100 | OPT_RELAY_HOSTNAME, |
| 101 | OPT_KPROBES, |
| 102 | OPT_BEGIN, |
| 103 | OPT_ALL, |
| 104 | }; |
| 105 | |
| 106 | static struct poptOption long_options[] = { |
| 107 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 108 | { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL }, |
| 109 | { "textdump", 't', POPT_ARG_NONE, NULL, OPT_TEXTDUMP, NULL, NULL }, |
| 110 | { "child", 'f', POPT_ARG_NONE, NULL, OPT_CHILD, NULL, NULL }, |
| 111 | { "begin", 'b', POPT_ARG_NONE, NULL, OPT_BEGIN, NULL, NULL }, |
| 112 | { "pid", 'p', POPT_ARG_STRING, &opt_tid, OPT_PID, NULL, NULL }, |
| 113 | { "hostname", 'n', POPT_ARG_STRING, &opt_hostname, OPT_HOSTNAME, NULL, NULL }, |
| 114 | { "relay-hostname", 'r', POPT_ARG_STRING, &opt_relay_hostname, |
| 115 | OPT_RELAY_HOSTNAME, NULL, NULL }, |
| 116 | { "kprobes", 'k', POPT_ARG_STRING, &opt_kprobes, OPT_KPROBES, NULL, NULL }, |
| 117 | { "all", 'a', POPT_ARG_NONE, NULL, OPT_ALL, NULL, NULL }, |
| 118 | { NULL, 0, 0, NULL, 0, NULL, NULL }, |
| 119 | }; |
| 120 | |
| 121 | #ifdef LTTNGTOP_MMAP_LIVE |
| 122 | static void handle_textdump_sigterm(int signal) |
| 123 | { |
| 124 | quit = 1; |
| 125 | lttng_destroy_session("test"); |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | void *refresh_thread(void *p) |
| 130 | { |
| 131 | while (1) { |
| 132 | if (quit) { |
| 133 | sem_post(&pause_sem); |
| 134 | sem_post(&timer); |
| 135 | sem_post(&end_trace_sem); |
| 136 | sem_post(&goodtodisplay); |
| 137 | sem_post(&goodtoupdate); |
| 138 | pthread_exit(0); |
| 139 | } |
| 140 | if (!opt_input_path) { |
| 141 | #ifdef LTTNGTOP_MMAP_LIVE |
| 142 | mmap_live_flush(mmap_list); |
| 143 | #endif |
| 144 | } |
| 145 | sem_wait(&pause_sem); |
| 146 | sem_post(&pause_sem); |
| 147 | sem_post(&timer); |
| 148 | sleep(refresh_display/NSEC_PER_SEC); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | void *ncurses_display(void *p) |
| 153 | { |
| 154 | unsigned int current_display_index = 0; |
| 155 | |
| 156 | sem_wait(&bootstrap); |
| 157 | /* |
| 158 | * Prevent the 1 second delay when we hit ESC |
| 159 | */ |
| 160 | ESCDELAY = 0; |
| 161 | init_ncurses(); |
| 162 | |
| 163 | while (1) { |
| 164 | sem_wait(&timer); |
| 165 | sem_wait(&goodtodisplay); |
| 166 | sem_wait(&pause_sem); |
| 167 | |
| 168 | if (quit) { |
| 169 | sem_post(&pause_sem); |
| 170 | sem_post(&timer); |
| 171 | reset_ncurses(); |
| 172 | pthread_exit(0); |
| 173 | } |
| 174 | |
| 175 | copy = g_ptr_array_index(copies, current_display_index); |
| 176 | assert(copy); |
| 177 | display(current_display_index++); |
| 178 | |
| 179 | sem_post(&goodtoupdate); |
| 180 | sem_post(&pause_sem); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void print_fields(struct bt_ctf_event *event, const char *procname, |
| 185 | int pid) |
| 186 | { |
| 187 | unsigned int cnt, i; |
| 188 | const struct bt_definition *const * list; |
| 189 | const struct bt_declaration *l; |
| 190 | const struct bt_definition *scope; |
| 191 | enum ctf_type_id type; |
| 192 | const char *str; |
| 193 | struct processtop *current_proc; |
| 194 | struct files *current_file; |
| 195 | int fd, fd_value = -1; |
| 196 | |
| 197 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_FIELDS); |
| 198 | |
| 199 | bt_ctf_get_field_list(event, scope, &list, &cnt); |
| 200 | for (i = 0; i < cnt; i++) { |
| 201 | if (i != 0) |
| 202 | printf(", "); |
| 203 | printf("%s = ", bt_ctf_field_name(list[i])); |
| 204 | l = bt_ctf_get_decl_from_def(list[i]); |
| 205 | if (strncmp(bt_ctf_field_name(list[i]), "fd", 2) == 0) |
| 206 | fd = 1; |
| 207 | else |
| 208 | fd = 0; |
| 209 | type = bt_ctf_field_type(l); |
| 210 | if (type == CTF_TYPE_INTEGER) { |
| 211 | if (bt_ctf_get_int_signedness(l) == 0) { |
| 212 | fd_value = bt_ctf_get_uint64(list[i]); |
| 213 | printf("%" PRIu64, bt_ctf_get_uint64(list[i])); |
| 214 | } else { |
| 215 | fd_value = bt_ctf_get_int64(list[i]); |
| 216 | printf("%" PRId64, bt_ctf_get_int64(list[i])); |
| 217 | } |
| 218 | } else if (type == CTF_TYPE_STRING) { |
| 219 | printf("%s", bt_ctf_get_string(list[i])); |
| 220 | } else if (type == CTF_TYPE_ARRAY) { |
| 221 | str = bt_ctf_get_char_array(list[i]); |
| 222 | if (!bt_ctf_field_get_error() && str) |
| 223 | printf("%s", str); |
| 224 | } |
| 225 | if (fd) { |
| 226 | current_proc = find_process_tid(<tngtop, pid, procname); |
| 227 | if (!current_proc) |
| 228 | continue; |
| 229 | current_file = get_file(current_proc, fd_value); |
| 230 | if (!current_file || !current_file->name) |
| 231 | continue; |
| 232 | printf("<%s>", current_file->name); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * hook on each event to check the timestamp and refresh the display if |
| 239 | * necessary |
| 240 | */ |
| 241 | enum bt_cb_ret textdump(struct bt_ctf_event *call_data, void *private_data) |
| 242 | { |
| 243 | unsigned long timestamp; |
| 244 | uint64_t delta; |
| 245 | struct tm start; |
| 246 | uint64_t ts_nsec_start; |
| 247 | int pid, cpu_id, tid, ret, lookup, current_syscall = 0; |
| 248 | const struct bt_definition *scope; |
| 249 | const char *hostname, *procname; |
| 250 | struct cputime *cpu; |
| 251 | char *from_syscall = NULL; |
| 252 | |
| 253 | timestamp = bt_ctf_get_timestamp(call_data); |
| 254 | |
| 255 | /* can happen in network live when tracing is idle */ |
| 256 | if (timestamp < last_event_ts) |
| 257 | goto end_stop; |
| 258 | |
| 259 | last_event_ts = timestamp; |
| 260 | |
| 261 | start = format_timestamp(timestamp); |
| 262 | ts_nsec_start = timestamp % NSEC_PER_SEC; |
| 263 | |
| 264 | pid = get_context_pid(call_data); |
| 265 | if (pid == -1ULL && opt_tid) { |
| 266 | goto error; |
| 267 | } |
| 268 | |
| 269 | tid = get_context_tid(call_data); |
| 270 | |
| 271 | hostname = get_context_hostname(call_data); |
| 272 | if (opt_child) |
| 273 | lookup = pid; |
| 274 | else |
| 275 | lookup = tid; |
| 276 | if (opt_tid || opt_hostname || opt_exec_name) { |
| 277 | if (!lookup_filter_tid_list(lookup)) { |
| 278 | /* To display when a process of ours in getting scheduled in */ |
| 279 | if (strcmp(bt_ctf_event_name(call_data), "sched_switch") == 0) { |
| 280 | int next_tid; |
| 281 | |
| 282 | scope = bt_ctf_get_top_level_scope(call_data, |
| 283 | BT_EVENT_FIELDS); |
| 284 | next_tid = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 285 | scope, "_next_tid")); |
| 286 | if (bt_ctf_field_get_error()) { |
| 287 | fprintf(stderr, "Missing next_tid field\n"); |
| 288 | goto error; |
| 289 | } |
| 290 | if (!lookup_filter_tid_list(next_tid)) { |
| 291 | if (!opt_all) |
| 292 | goto end; |
| 293 | } else { |
| 294 | if (opt_all) |
| 295 | printf("%c[1m", 27); |
| 296 | } |
| 297 | } else if (!opt_all) { |
| 298 | goto end; |
| 299 | } |
| 300 | } else { |
| 301 | if (opt_all) |
| 302 | printf("%c[1m", 27); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (last_syscall && (strncmp(bt_ctf_event_name(call_data), |
| 307 | "exit_syscall", 12)) != 0) { |
| 308 | last_syscall = NULL; |
| 309 | printf(" ...interrupted...\n"); |
| 310 | } |
| 311 | |
| 312 | cpu_id = get_cpu_id(call_data); |
| 313 | procname = get_context_comm(call_data); |
| 314 | if (strncmp(bt_ctf_event_name(call_data), "sys_", 4) == 0) { |
| 315 | cpu = get_cpu(cpu_id); |
| 316 | cpu->current_syscall = g_new0(struct syscall, 1); |
| 317 | cpu->current_syscall->name = strdup(bt_ctf_event_name(call_data)); |
| 318 | cpu->current_syscall->ts_start = timestamp; |
| 319 | cpu->current_syscall->cpu_id = cpu_id; |
| 320 | last_syscall = cpu->current_syscall; |
| 321 | current_syscall = 1; |
| 322 | } else if ((strncmp(bt_ctf_event_name(call_data), "exit_syscall", 12)) == 0) { |
| 323 | struct tm start_ts; |
| 324 | |
| 325 | /* Return code of a syscall if it was the last displayed event. */ |
| 326 | if (last_syscall && last_syscall->ts_start == prev_ts) { |
| 327 | if (last_syscall->cpu_id == cpu_id) { |
| 328 | int64_t syscall_ret; |
| 329 | |
| 330 | delta = timestamp - last_syscall->ts_start; |
| 331 | scope = bt_ctf_get_top_level_scope(call_data, |
| 332 | BT_EVENT_FIELDS); |
| 333 | syscall_ret = bt_ctf_get_int64(bt_ctf_get_field(call_data, |
| 334 | scope, "_ret")); |
| 335 | |
| 336 | printf("= %" PRId64 " (%" PRIu64 ".%09" PRIu64 "s)\n", |
| 337 | syscall_ret, delta / NSEC_PER_SEC, |
| 338 | delta % NSEC_PER_SEC); |
| 339 | last_syscall = NULL; |
| 340 | goto end; |
| 341 | } else { |
| 342 | last_syscall = NULL; |
| 343 | printf(" ...interrupted...\n"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | cpu = get_cpu(cpu_id); |
| 348 | if (cpu->current_syscall) { |
| 349 | delta = timestamp - cpu->current_syscall->ts_start; |
| 350 | start_ts = format_timestamp(cpu->current_syscall->ts_start); |
| 351 | ret = asprintf(&from_syscall, " [from %02d:%02d:%02d.%09" PRIu64 |
| 352 | " (+%" PRIu64 ".%09" PRIu64 ") (cpu %d) %s]", |
| 353 | start_ts.tm_hour, start_ts.tm_min, start_ts.tm_sec, |
| 354 | cpu->current_syscall->ts_start % NSEC_PER_SEC, |
| 355 | delta / NSEC_PER_SEC, delta % NSEC_PER_SEC, |
| 356 | cpu_id, cpu->current_syscall->name); |
| 357 | if (ret < 0) { |
| 358 | goto error; |
| 359 | } |
| 360 | free(cpu->current_syscall->name); |
| 361 | g_free(cpu->current_syscall); |
| 362 | cpu->current_syscall = NULL; |
| 363 | last_syscall = NULL; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (prev_ts == 0) |
| 368 | prev_ts = timestamp; |
| 369 | delta = timestamp - prev_ts; |
| 370 | prev_ts = timestamp; |
| 371 | |
| 372 | printf("%02d:%02d:%02d.%09" PRIu64 " (+%" PRIu64 ".%09" PRIu64 ") %s%s" |
| 373 | "(cpu %d) [%s (%d/%d)] %s (", |
| 374 | start.tm_hour, start.tm_min, start.tm_sec, |
| 375 | ts_nsec_start, delta / NSEC_PER_SEC, |
| 376 | delta % NSEC_PER_SEC, (hostname) ? hostname : "", |
| 377 | (hostname) ? " ": "", cpu_id, procname, pid, tid, |
| 378 | bt_ctf_event_name(call_data)); |
| 379 | print_fields(call_data, procname, pid); |
| 380 | printf(")%s%c", (from_syscall) ? from_syscall : "", |
| 381 | (!current_syscall) ? '\n' : ' '); |
| 382 | |
| 383 | free(from_syscall); |
| 384 | if (opt_all && (opt_tid || opt_hostname || opt_exec_name)) |
| 385 | printf("%c[0m", 27); |
| 386 | |
| 387 | end: |
| 388 | return BT_CB_OK; |
| 389 | error: |
| 390 | return BT_CB_ERROR_STOP; |
| 391 | end_stop: |
| 392 | return BT_CB_OK_STOP; |
| 393 | } |
| 394 | |
| 395 | enum bt_cb_ret handle_kprobes(struct bt_ctf_event *call_data, void *private_data) |
| 396 | { |
| 397 | int i; |
| 398 | struct kprobes *kprobe; |
| 399 | |
| 400 | /* for kprobes */ |
| 401 | for (i = 0; i < lttngtop.kprobes_table->len; i++) { |
| 402 | kprobe = g_ptr_array_index(lttngtop.kprobes_table, i); |
| 403 | if (strcmp(bt_ctf_event_name(call_data), kprobe->probe_name) == 0) { |
| 404 | kprobe->count++; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return BT_CB_OK; |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * hook on each event to check the timestamp and refresh the display if |
| 413 | * necessary |
| 414 | */ |
| 415 | enum bt_cb_ret check_timestamp(struct bt_ctf_event *call_data, void *private_data) |
| 416 | { |
| 417 | unsigned long timestamp; |
| 418 | |
| 419 | timestamp = bt_ctf_get_timestamp(call_data); |
| 420 | if (timestamp == -1ULL) |
| 421 | goto error; |
| 422 | |
| 423 | /* can happen in network live when tracing is idle */ |
| 424 | if (timestamp < last_event_ts) |
| 425 | goto end_stop; |
| 426 | |
| 427 | last_event_ts = timestamp; |
| 428 | |
| 429 | if (last_display_update == 0) |
| 430 | last_display_update = timestamp; |
| 431 | |
| 432 | if (timestamp - last_display_update >= refresh_display) { |
| 433 | sem_wait(&goodtoupdate); |
| 434 | g_ptr_array_add(copies, get_copy_lttngtop(last_display_update, |
| 435 | timestamp)); |
| 436 | sem_post(&goodtodisplay); |
| 437 | sem_post(&bootstrap); |
| 438 | last_display_update = timestamp; |
| 439 | } |
| 440 | return BT_CB_OK; |
| 441 | |
| 442 | error: |
| 443 | fprintf(stderr, "check_timestamp callback error\n"); |
| 444 | return BT_CB_ERROR_STOP; |
| 445 | |
| 446 | end_stop: |
| 447 | return BT_CB_OK_STOP; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * get_perf_counter : get or create and return a perf_counter struct for |
| 452 | * either a process or a cpu (only one of the 2 parameters mandatory) |
| 453 | */ |
| 454 | struct perfcounter *get_perf_counter(const char *name, struct processtop *proc, |
| 455 | struct cputime *cpu) |
| 456 | { |
| 457 | struct perfcounter *ret; |
| 458 | GHashTable *table; |
| 459 | |
| 460 | if (proc) |
| 461 | table = proc->perf; |
| 462 | else if (cpu) |
| 463 | table = cpu->perf; |
| 464 | else |
| 465 | goto error; |
| 466 | |
| 467 | ret = g_hash_table_lookup(table, (gpointer) name); |
| 468 | if (ret) |
| 469 | goto end; |
| 470 | |
| 471 | ret = g_new0(struct perfcounter, 1); |
| 472 | /* by default, make it visible in the UI */ |
| 473 | ret->visible = 1; |
| 474 | g_hash_table_insert(table, (gpointer) strdup(name), ret); |
| 475 | |
| 476 | end: |
| 477 | return ret; |
| 478 | |
| 479 | error: |
| 480 | return NULL; |
| 481 | } |
| 482 | |
| 483 | void update_perf_value(struct processtop *proc, struct cputime *cpu, |
| 484 | const char *name, int value) |
| 485 | { |
| 486 | struct perfcounter *cpu_perf, *process_perf; |
| 487 | |
| 488 | cpu_perf = get_perf_counter(name, NULL, cpu); |
| 489 | if (cpu_perf->count < value) { |
| 490 | process_perf = get_perf_counter(name, proc, NULL); |
| 491 | process_perf->count += value - cpu_perf->count; |
| 492 | cpu_perf->count = value; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void extract_perf_counter_scope(const struct bt_ctf_event *event, |
| 497 | const struct bt_definition *scope, |
| 498 | struct processtop *proc, |
| 499 | struct cputime *cpu) |
| 500 | { |
| 501 | struct bt_definition const * const *list = NULL; |
| 502 | const struct bt_definition *field; |
| 503 | unsigned int count; |
| 504 | struct perfcounter *perfcounter; |
| 505 | GHashTableIter iter; |
| 506 | gpointer key; |
| 507 | int ret; |
| 508 | |
| 509 | if (!scope) |
| 510 | goto end; |
| 511 | |
| 512 | ret = bt_ctf_get_field_list(event, scope, &list, &count); |
| 513 | if (ret < 0) |
| 514 | goto end; |
| 515 | |
| 516 | if (count == 0) |
| 517 | goto end; |
| 518 | |
| 519 | g_hash_table_iter_init(&iter, global_perf_liszt); |
| 520 | while (g_hash_table_iter_next (&iter, &key, (gpointer) &perfcounter)) { |
| 521 | field = bt_ctf_get_field(event, scope, (char *) key); |
| 522 | if (field) { |
| 523 | int value = bt_ctf_get_uint64(field); |
| 524 | if (bt_ctf_field_get_error()) |
| 525 | continue; |
| 526 | update_perf_value(proc, cpu, (char *) key, value); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | end: |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | void update_perf_counter(struct processtop *proc, const struct bt_ctf_event *event) |
| 535 | { |
| 536 | struct cputime *cpu; |
| 537 | const struct bt_definition *scope; |
| 538 | |
| 539 | cpu = get_cpu(get_cpu_id(event)); |
| 540 | |
| 541 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_EVENT_CONTEXT); |
| 542 | extract_perf_counter_scope(event, scope, proc, cpu); |
| 543 | |
| 544 | scope = bt_ctf_get_top_level_scope(event, BT_STREAM_PACKET_CONTEXT); |
| 545 | extract_perf_counter_scope(event, scope, proc, cpu); |
| 546 | |
| 547 | scope = bt_ctf_get_top_level_scope(event, BT_EVENT_CONTEXT); |
| 548 | extract_perf_counter_scope(event, scope, proc, cpu); |
| 549 | } |
| 550 | |
| 551 | enum bt_cb_ret fix_process_table(struct bt_ctf_event *call_data, |
| 552 | void *private_data) |
| 553 | { |
| 554 | int pid, tid, ppid, vpid, vtid, vppid; |
| 555 | char *comm, *hostname; |
| 556 | struct processtop *parent, *child; |
| 557 | unsigned long timestamp; |
| 558 | |
| 559 | timestamp = bt_ctf_get_timestamp(call_data); |
| 560 | if (timestamp == -1ULL) |
| 561 | goto error; |
| 562 | |
| 563 | pid = get_context_pid(call_data); |
| 564 | if (pid == -1ULL) { |
| 565 | goto error; |
| 566 | } |
| 567 | |
| 568 | tid = get_context_tid(call_data); |
| 569 | if (tid == -1ULL) { |
| 570 | goto error; |
| 571 | } |
| 572 | ppid = get_context_ppid(call_data); |
| 573 | if (ppid == -1ULL) { |
| 574 | goto end; |
| 575 | } |
| 576 | vpid = get_context_vpid(call_data); |
| 577 | if (pid == -1ULL) { |
| 578 | vpid = -1; |
| 579 | } |
| 580 | vtid = get_context_vtid(call_data); |
| 581 | if (tid == -1ULL) { |
| 582 | vtid = -1; |
| 583 | } |
| 584 | vppid = get_context_vppid(call_data); |
| 585 | if (ppid == -1ULL) { |
| 586 | vppid = -1; |
| 587 | } |
| 588 | comm = get_context_comm(call_data); |
| 589 | if (!comm) { |
| 590 | goto error; |
| 591 | } |
| 592 | /* optional */ |
| 593 | hostname = get_context_hostname(call_data); |
| 594 | |
| 595 | /* find or create the current process */ |
| 596 | child = find_process_tid(<tngtop, tid, comm); |
| 597 | if (!child) |
| 598 | child = add_proc(<tngtop, tid, comm, timestamp, hostname); |
| 599 | if (!child) |
| 600 | goto end; |
| 601 | update_proc(child, pid, tid, ppid, vpid, vtid, vppid, comm, hostname); |
| 602 | |
| 603 | if (pid != tid) { |
| 604 | /* find or create the parent */ |
| 605 | parent = find_process_tid(<tngtop, pid, comm); |
| 606 | if (!parent) { |
| 607 | parent = add_proc(<tngtop, pid, comm, timestamp, hostname); |
| 608 | if (parent) |
| 609 | parent->pid = pid; |
| 610 | } |
| 611 | |
| 612 | /* attach the parent to the current process */ |
| 613 | child->threadparent = parent; |
| 614 | add_thread(parent, child); |
| 615 | } |
| 616 | |
| 617 | update_perf_counter(child, call_data); |
| 618 | |
| 619 | end: |
| 620 | return BT_CB_OK; |
| 621 | |
| 622 | error: |
| 623 | return BT_CB_ERROR_STOP; |
| 624 | } |
| 625 | |
| 626 | void init_lttngtop() |
| 627 | { |
| 628 | copies = g_ptr_array_new(); |
| 629 | global_perf_liszt = g_hash_table_new(g_str_hash, g_str_equal); |
| 630 | global_filter_list = g_hash_table_new(g_str_hash, g_str_equal); |
| 631 | global_host_list = g_hash_table_new(g_str_hash, g_str_equal); |
| 632 | tid_filter_list = g_hash_table_new(g_str_hash, |
| 633 | g_str_equal); |
| 634 | |
| 635 | sem_init(&goodtodisplay, 0, 0); |
| 636 | sem_init(&goodtoupdate, 0, 1); |
| 637 | sem_init(&timer, 0, 1); |
| 638 | sem_init(&bootstrap, 0, 0); |
| 639 | sem_init(&pause_sem, 0, 1); |
| 640 | sem_init(&end_trace_sem, 0, 0); |
| 641 | |
| 642 | reset_global_counters(); |
| 643 | lttngtop.nbproc = 0; |
| 644 | lttngtop.nbthreads = 0; |
| 645 | lttngtop.nbfiles = 0; |
| 646 | |
| 647 | lttngtop.process_hash_table = g_hash_table_new(g_direct_hash, |
| 648 | g_direct_equal); |
| 649 | lttngtop.process_table = g_ptr_array_new(); |
| 650 | lttngtop.files_table = g_ptr_array_new(); |
| 651 | lttngtop.cpu_table = g_ptr_array_new(); |
| 652 | |
| 653 | toggle_filter = -1; |
| 654 | } |
| 655 | |
| 656 | void usage(FILE *fp) |
| 657 | { |
| 658 | fprintf(fp, "LTTngTop %s\n\n", VERSION); |
| 659 | fprintf(fp, "Usage : lttngtop [OPTIONS] TRACE\n"); |
| 660 | fprintf(fp, " TRACE Path to the trace to analyse (-r for network live tracing, nothing for mmap live streaming)\n"); |
| 661 | fprintf(fp, " -h, --help This help message\n"); |
| 662 | fprintf(fp, " -t, --textdump Display live events in text-only\n"); |
| 663 | fprintf(fp, " -p, --pid Comma-separated list of PIDs to display\n"); |
| 664 | fprintf(fp, " -f, --child Follow threads associated with selected PIDs\n"); |
| 665 | fprintf(fp, " -n, --hostname Comma-separated list of hostnames to display (require hostname context in trace)\n"); |
| 666 | fprintf(fp, " -a, --all In textdump mode, display all events but write in bold the processes we are interested in (-f, -p and -n)\n"); |
| 667 | fprintf(fp, " -k, --kprobes Comma-separated list of kprobes to insert (same format as lttng enable-event)\n"); |
| 668 | fprintf(fp, " -r, --relay-hostname Network live streaming : hostname of the lttng-relayd (default port)\n"); |
| 669 | fprintf(fp, " -b, --begin Network live streaming : read the trace for the beginning of the recording\n"); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Parse probe options. |
| 674 | * Shamelessly stolen from lttng-tools : |
| 675 | * src/bin/lttng/commands/enable_events.c |
| 676 | */ |
| 677 | static struct kprobes *parse_probe_opts(char *opt) |
| 678 | { |
| 679 | char s_hex[19]; |
| 680 | char name[LTTNG_SYMBOL_NAME_LEN]; |
| 681 | struct kprobes *kprobe; |
| 682 | int ret; |
| 683 | |
| 684 | /* |
| 685 | kprobe->probe_addr = 0; |
| 686 | kprobe->probe_offset = 0; |
| 687 | asprintf(&kprobe->probe_name, "probe_sys_open"); |
| 688 | asprintf(&kprobe->symbol_name, "sys_open"); |
| 689 | */ |
| 690 | |
| 691 | if (opt == NULL) { |
| 692 | kprobe = NULL; |
| 693 | goto end; |
| 694 | } |
| 695 | |
| 696 | kprobe = g_new0(struct kprobes, 1); |
| 697 | |
| 698 | /* Check for symbol+offset */ |
| 699 | ret = sscanf(opt, "%[^'+']+%s", name, s_hex); |
| 700 | if (ret == 2) { |
| 701 | ret = asprintf(&kprobe->probe_name, "probe_%s", name); |
| 702 | ret = asprintf(&kprobe->symbol_name, "%s", name); |
| 703 | |
| 704 | if (strlen(s_hex) == 0) { |
| 705 | fprintf(stderr, "Invalid probe offset %s", s_hex); |
| 706 | ret = -1; |
| 707 | goto end; |
| 708 | } |
| 709 | kprobe->probe_offset = strtoul(s_hex, NULL, 0); |
| 710 | kprobe->probe_addr = 0; |
| 711 | goto end; |
| 712 | } |
| 713 | |
| 714 | /* Check for symbol */ |
| 715 | if (isalpha(name[0])) { |
| 716 | ret = sscanf(opt, "%s", name); |
| 717 | if (ret == 1) { |
| 718 | ret = asprintf(&kprobe->probe_name, "probe_%s", name); |
| 719 | ret = asprintf(&kprobe->symbol_name, "%s", name); |
| 720 | kprobe->probe_offset = 0; |
| 721 | kprobe->probe_addr = 0; |
| 722 | goto end; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* Check for address */ |
| 727 | ret = sscanf(opt, "%s", s_hex); |
| 728 | if (ret > 0) { |
| 729 | if (strlen(s_hex) == 0) { |
| 730 | fprintf(stderr, "Invalid probe address %s", s_hex); |
| 731 | ret = -1; |
| 732 | goto end; |
| 733 | } |
| 734 | ret = asprintf(&kprobe->probe_name, "probe_%s", s_hex); |
| 735 | kprobe->probe_offset = 0; |
| 736 | kprobe->probe_addr = strtoul(s_hex, NULL, 0); |
| 737 | goto end; |
| 738 | } |
| 739 | |
| 740 | /* No match */ |
| 741 | kprobe = NULL; |
| 742 | |
| 743 | end: |
| 744 | return kprobe; |
| 745 | } |
| 746 | |
| 747 | /* |
| 748 | * Return 0 if caller should continue, < 0 if caller should return |
| 749 | * error, > 0 if caller should exit without reporting error. |
| 750 | */ |
| 751 | static int parse_options(int argc, char **argv) |
| 752 | { |
| 753 | poptContext pc; |
| 754 | int opt, ret = 0; |
| 755 | char *tmp_str; |
| 756 | int *tid; |
| 757 | int i; |
| 758 | |
| 759 | remote_live = 0; |
| 760 | |
| 761 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
| 762 | poptReadDefaultConfig(pc, 0); |
| 763 | |
| 764 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 765 | switch (opt) { |
| 766 | case OPT_HELP: |
| 767 | usage(stdout); |
| 768 | ret = 1; /* exit cleanly */ |
| 769 | goto end; |
| 770 | case OPT_TEXTDUMP: |
| 771 | opt_textdump = 1; |
| 772 | break; |
| 773 | case OPT_ALL: |
| 774 | opt_all = 1; |
| 775 | break; |
| 776 | case OPT_CHILD: |
| 777 | opt_child = 1; |
| 778 | break; |
| 779 | case OPT_PID: |
| 780 | toggle_filter = 1; |
| 781 | tmp_str = strtok(opt_tid, ","); |
| 782 | while (tmp_str) { |
| 783 | tid = malloc(sizeof(int)); |
| 784 | *tid = atoi(tmp_str); |
| 785 | g_hash_table_insert(tid_filter_list, |
| 786 | (gpointer) tid, tid); |
| 787 | tmp_str = strtok(NULL, ","); |
| 788 | } |
| 789 | break; |
| 790 | case OPT_BEGIN: |
| 791 | /* start reading the live trace from the beginning */ |
| 792 | opt_begin = 1; |
| 793 | break; |
| 794 | case OPT_HOSTNAME: |
| 795 | toggle_filter = 1; |
| 796 | tmp_str = strtok(opt_hostname, ","); |
| 797 | while (tmp_str) { |
| 798 | add_hostname_list(tmp_str, 1); |
| 799 | tmp_str = strtok(NULL, ","); |
| 800 | } |
| 801 | break; |
| 802 | case OPT_RELAY_HOSTNAME: |
| 803 | remote_live = 1; |
| 804 | break; |
| 805 | case OPT_KPROBES: |
| 806 | lttngtop.kprobes_table = g_ptr_array_new(); |
| 807 | tmp_str = strtok(opt_kprobes, ","); |
| 808 | while (tmp_str) { |
| 809 | struct kprobes *kprobe; |
| 810 | |
| 811 | kprobe = parse_probe_opts(tmp_str); |
| 812 | if (kprobe) { |
| 813 | g_ptr_array_add( |
| 814 | lttngtop.kprobes_table, |
| 815 | kprobe); |
| 816 | } else { |
| 817 | ret = -EINVAL; |
| 818 | goto end; |
| 819 | } |
| 820 | tmp_str = strtok(NULL, ","); |
| 821 | } |
| 822 | break; |
| 823 | default: |
| 824 | ret = -EINVAL; |
| 825 | goto end; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | opt_exec_name = NULL; |
| 830 | opt_exec_argv = NULL; |
| 831 | for (i = 0; i < argc; i++) { |
| 832 | if (argv[i][0] == '-' && argv[i][1] == '-') { |
| 833 | opt_exec_name = argv[i + 1]; |
| 834 | opt_exec_argv = &argv[i + 1]; |
| 835 | break; |
| 836 | } |
| 837 | } |
| 838 | if (!opt_exec_name) { |
| 839 | opt_input_path = poptGetArg(pc); |
| 840 | } |
| 841 | |
| 842 | end: |
| 843 | if (pc) { |
| 844 | poptFreeContext(pc); |
| 845 | } |
| 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | void iter_trace(struct bt_context *bt_ctx) |
| 850 | { |
| 851 | struct bt_ctf_iter *iter; |
| 852 | struct bt_iter_pos begin_pos; |
| 853 | struct kprobes *kprobe; |
| 854 | const struct bt_ctf_event *event; |
| 855 | int i; |
| 856 | int ret = 0; |
| 857 | |
| 858 | begin_pos.type = BT_SEEK_BEGIN; |
| 859 | iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL); |
| 860 | |
| 861 | /* at each event, verify the status of the process table */ |
| 862 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, |
| 863 | fix_process_table, |
| 864 | NULL, NULL, NULL); |
| 865 | /* to handle the follow child option */ |
| 866 | bt_ctf_iter_add_callback(iter, |
| 867 | g_quark_from_static_string("sched_process_fork"), |
| 868 | NULL, 0, handle_sched_process_fork, NULL, NULL, NULL); |
| 869 | /* to clean up the process table */ |
| 870 | bt_ctf_iter_add_callback(iter, |
| 871 | g_quark_from_static_string("sched_process_free"), |
| 872 | NULL, 0, handle_sched_process_free, NULL, NULL, NULL); |
| 873 | /* to get all the process from the statedumps */ |
| 874 | bt_ctf_iter_add_callback(iter, |
| 875 | g_quark_from_static_string( |
| 876 | "lttng_statedump_process_state"), |
| 877 | NULL, 0, handle_statedump_process_state, |
| 878 | NULL, NULL, NULL); |
| 879 | bt_ctf_iter_add_callback(iter, |
| 880 | g_quark_from_static_string( |
| 881 | "lttng_statedump_file_descriptor"), |
| 882 | NULL, 0, handle_statedump_file_descriptor, |
| 883 | NULL, NULL, NULL); |
| 884 | bt_ctf_iter_add_callback(iter, |
| 885 | g_quark_from_static_string("sys_open"), |
| 886 | NULL, 0, handle_sys_open, NULL, NULL, NULL); |
| 887 | bt_ctf_iter_add_callback(iter, |
| 888 | g_quark_from_static_string("sys_close"), |
| 889 | NULL, 0, handle_sys_close, NULL, NULL, NULL); |
| 890 | bt_ctf_iter_add_callback(iter, |
| 891 | g_quark_from_static_string("exit_syscall"), |
| 892 | NULL, 0, handle_exit_syscall, NULL, NULL, NULL); |
| 893 | if (opt_textdump) { |
| 894 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, |
| 895 | textdump, |
| 896 | NULL, NULL, NULL); |
| 897 | } else { |
| 898 | /* at each event check if we need to refresh */ |
| 899 | bt_ctf_iter_add_callback(iter, 0, NULL, 0, |
| 900 | check_timestamp, |
| 901 | NULL, NULL, NULL); |
| 902 | /* to handle the scheduling events */ |
| 903 | bt_ctf_iter_add_callback(iter, |
| 904 | g_quark_from_static_string("sched_switch"), |
| 905 | NULL, 0, handle_sched_switch, NULL, NULL, NULL); |
| 906 | /* for IO top */ |
| 907 | bt_ctf_iter_add_callback(iter, |
| 908 | g_quark_from_static_string("sys_write"), |
| 909 | NULL, 0, handle_sys_write, NULL, NULL, NULL); |
| 910 | bt_ctf_iter_add_callback(iter, |
| 911 | g_quark_from_static_string("sys_read"), |
| 912 | NULL, 0, handle_sys_read, NULL, NULL, NULL); |
| 913 | |
| 914 | /* for kprobes */ |
| 915 | if (lttngtop.kprobes_table) { |
| 916 | for (i = 0; i < lttngtop.kprobes_table->len; i++) { |
| 917 | kprobe = g_ptr_array_index(lttngtop.kprobes_table, i); |
| 918 | bt_ctf_iter_add_callback(iter, |
| 919 | g_quark_from_static_string( |
| 920 | kprobe->probe_name), |
| 921 | NULL, 0, handle_kprobes, |
| 922 | NULL, NULL, NULL); |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | if (opt_exec_name) { |
| 928 | pid_t pid; |
| 929 | |
| 930 | pid = fork(); |
| 931 | if (pid == 0) { |
| 932 | execvpe(opt_exec_name, opt_exec_argv, opt_exec_env); |
| 933 | exit(EXIT_SUCCESS); |
| 934 | } else if (pid > 0) { |
| 935 | opt_exec_pid = pid; |
| 936 | g_hash_table_insert(tid_filter_list, |
| 937 | (gpointer) &pid, |
| 938 | &pid); |
| 939 | } else { |
| 940 | perror("fork"); |
| 941 | exit(EXIT_FAILURE); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | while ((event = bt_ctf_iter_read_event(iter)) != NULL) { |
| 946 | if (quit || reload_trace) |
| 947 | goto end_iter; |
| 948 | ret = bt_iter_next(bt_ctf_get_iter(iter)); |
| 949 | if (ret < 0) |
| 950 | goto end_iter; |
| 951 | } |
| 952 | |
| 953 | /* block until quit, we reached the end of the trace */ |
| 954 | sem_wait(&end_trace_sem); |
| 955 | |
| 956 | end_iter: |
| 957 | bt_ctf_iter_destroy(iter); |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * bt_context_add_traces_recursive: Open a trace recursively |
| 962 | * (copied from BSD code in converter/babeltrace.c) |
| 963 | * |
| 964 | * Find each trace present in the subdirectory starting from the given |
| 965 | * path, and add them to the context. The packet_seek parameter can be |
| 966 | * NULL: this specify to use the default format packet_seek. |
| 967 | * |
| 968 | * Return: 0 on success, nonzero on failure. |
| 969 | * Unable to open toplevel: failure. |
| 970 | * Unable to open some subdirectory or file: warn and continue; |
| 971 | */ |
| 972 | int bt_context_add_traces_recursive(struct bt_context *ctx, const char *path, |
| 973 | const char *format_str, |
| 974 | void (*packet_seek)(struct bt_stream_pos *pos, |
| 975 | size_t offset, int whence)) |
| 976 | { |
| 977 | FTS *tree; |
| 978 | FTSENT *node; |
| 979 | GArray *trace_ids; |
| 980 | char lpath[PATH_MAX]; |
| 981 | char * const paths[2] = { lpath, NULL }; |
| 982 | int ret = -1; |
| 983 | |
| 984 | if ((strncmp(path, NET4_URL_PREFIX, sizeof(NET4_URL_PREFIX) - 1)) == 0 || |
| 985 | (strncmp(path, NET6_URL_PREFIX, sizeof(NET6_URL_PREFIX) - 1)) == 0 || |
| 986 | (strncmp(path, NET_URL_PREFIX, sizeof(NET_URL_PREFIX) - 1)) == 0) { |
| 987 | ret = bt_context_add_trace(ctx, |
| 988 | path, format_str, packet_seek, NULL, NULL); |
| 989 | if (ret < 0) { |
| 990 | fprintf(stderr, "[warning] [Context] cannot open trace \"%s\" " |
| 991 | "for reading.\n", path); |
| 992 | /* Allow to skip erroneous traces. */ |
| 993 | ret = 1; /* partial error */ |
| 994 | } |
| 995 | return ret; |
| 996 | } |
| 997 | /* |
| 998 | * Need to copy path, because fts_open can change it. |
| 999 | * It is the pointer array, not the strings, that are constant. |
| 1000 | */ |
| 1001 | strncpy(lpath, path, PATH_MAX); |
| 1002 | lpath[PATH_MAX - 1] = '\0'; |
| 1003 | |
| 1004 | tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0); |
| 1005 | if (tree == NULL) { |
| 1006 | fprintf(stderr, "[error] [Context] Cannot traverse \"%s\" for reading.\n", |
| 1007 | path); |
| 1008 | return -EINVAL; |
| 1009 | } |
| 1010 | |
| 1011 | trace_ids = g_array_new(FALSE, TRUE, sizeof(int)); |
| 1012 | |
| 1013 | while ((node = fts_read(tree))) { |
| 1014 | int dirfd, metafd; |
| 1015 | |
| 1016 | if (!(node->fts_info & FTS_D)) |
| 1017 | continue; |
| 1018 | |
| 1019 | dirfd = open(node->fts_accpath, 0); |
| 1020 | if (dirfd < 0) { |
| 1021 | fprintf(stderr, "[error] [Context] Unable to open trace " |
| 1022 | "directory file descriptor.\n"); |
| 1023 | ret = dirfd; |
| 1024 | goto error; |
| 1025 | } |
| 1026 | metafd = openat(dirfd, "metadata", O_RDONLY); |
| 1027 | if (metafd < 0) { |
| 1028 | close(dirfd); |
| 1029 | continue; |
| 1030 | } else { |
| 1031 | int trace_id; |
| 1032 | |
| 1033 | ret = close(metafd); |
| 1034 | if (ret < 0) { |
| 1035 | perror("close"); |
| 1036 | goto error; |
| 1037 | } |
| 1038 | ret = close(dirfd); |
| 1039 | if (ret < 0) { |
| 1040 | perror("close"); |
| 1041 | goto error; |
| 1042 | } |
| 1043 | |
| 1044 | trace_id = bt_context_add_trace(ctx, |
| 1045 | node->fts_accpath, format_str, |
| 1046 | packet_seek, NULL, NULL); |
| 1047 | if (trace_id < 0) { |
| 1048 | fprintf(stderr, "[warning] [Context] opening trace \"%s\" from %s " |
| 1049 | "for reading.\n", node->fts_accpath, path); |
| 1050 | /* Allow to skip erroneous traces. */ |
| 1051 | continue; |
| 1052 | } |
| 1053 | g_array_append_val(trace_ids, trace_id); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | g_array_free(trace_ids, TRUE); |
| 1058 | return ret; |
| 1059 | |
| 1060 | error: |
| 1061 | return ret; |
| 1062 | } |
| 1063 | |
| 1064 | static int check_field_requirements(const struct bt_ctf_field_decl *const * field_list, |
| 1065 | int field_cnt, int *tid_check, int *pid_check, |
| 1066 | int *procname_check, int *ppid_check) |
| 1067 | { |
| 1068 | int j; |
| 1069 | struct perfcounter *global; |
| 1070 | const char *name; |
| 1071 | |
| 1072 | for (j = 0; j < field_cnt; j++) { |
| 1073 | name = bt_ctf_get_decl_field_name(field_list[j]); |
| 1074 | if (*tid_check == 0) { |
| 1075 | if (strncmp(name, "tid", 3) == 0) |
| 1076 | (*tid_check)++; |
| 1077 | } |
| 1078 | if (*pid_check == 0) { |
| 1079 | if (strncmp(name, "pid", 3) == 0) |
| 1080 | (*pid_check)++; |
| 1081 | } |
| 1082 | if (*ppid_check == 0) { |
| 1083 | if (strncmp(name, "ppid", 4) == 0) |
| 1084 | (*ppid_check)++; |
| 1085 | } |
| 1086 | if (*procname_check == 0) { |
| 1087 | if (strncmp(name, "procname", 8) == 0) |
| 1088 | (*procname_check)++; |
| 1089 | } |
| 1090 | if (strncmp(name, "perf_", 5) == 0) { |
| 1091 | global = g_hash_table_lookup(global_perf_liszt, (gpointer) name); |
| 1092 | if (!global) { |
| 1093 | global = g_new0(struct perfcounter, 1); |
| 1094 | /* by default, sort on the first perf context */ |
| 1095 | if (g_hash_table_size(global_perf_liszt) == 0) |
| 1096 | global->sort = 1; |
| 1097 | global->visible = 1; |
| 1098 | g_hash_table_insert(global_perf_liszt, (gpointer) strdup(name), global); |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | if (*tid_check == 1 && *pid_check == 1 && *ppid_check == 1 && |
| 1104 | *procname_check == 1) |
| 1105 | return 0; |
| 1106 | |
| 1107 | return -1; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * check_requirements: check if the required context informations are available |
| 1112 | * |
| 1113 | * If each mandatory context information is available for at least in one |
| 1114 | * event, return 0 otherwise return -1. |
| 1115 | */ |
| 1116 | int check_requirements(struct bt_context *ctx) |
| 1117 | { |
| 1118 | unsigned int i, evt_cnt, field_cnt; |
| 1119 | struct bt_ctf_event_decl *const * evt_list; |
| 1120 | const struct bt_ctf_field_decl *const * field_list; |
| 1121 | int tid_check = 0; |
| 1122 | int pid_check = 0; |
| 1123 | int procname_check = 0; |
| 1124 | int ppid_check = 0; |
| 1125 | int ret = 0; |
| 1126 | |
| 1127 | ret = bt_ctf_get_event_decl_list(0, ctx, &evt_list, &evt_cnt); |
| 1128 | if (ret < 0) { |
| 1129 | goto end; |
| 1130 | } |
| 1131 | |
| 1132 | for (i = 0; i < evt_cnt; i++) { |
| 1133 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_EVENT_CONTEXT, |
| 1134 | &field_list, &field_cnt); |
| 1135 | ret = check_field_requirements(field_list, field_cnt, |
| 1136 | &tid_check, &pid_check, &procname_check, |
| 1137 | &ppid_check); |
| 1138 | |
| 1139 | bt_ctf_get_decl_fields(evt_list[i], BT_EVENT_CONTEXT, |
| 1140 | &field_list, &field_cnt); |
| 1141 | ret = check_field_requirements(field_list, field_cnt, |
| 1142 | &tid_check, &pid_check, &procname_check, |
| 1143 | &ppid_check); |
| 1144 | |
| 1145 | bt_ctf_get_decl_fields(evt_list[i], BT_STREAM_PACKET_CONTEXT, |
| 1146 | &field_list, &field_cnt); |
| 1147 | ret = check_field_requirements(field_list, field_cnt, |
| 1148 | &tid_check, &pid_check, &procname_check, |
| 1149 | &ppid_check); |
| 1150 | } |
| 1151 | |
| 1152 | if (tid_check == 0) { |
| 1153 | ret = -1; |
| 1154 | fprintf(stderr, "[error] missing tid context information\n"); |
| 1155 | } |
| 1156 | if (pid_check == 0) { |
| 1157 | ret = -1; |
| 1158 | fprintf(stderr, "[error] missing pid context information\n"); |
| 1159 | } |
| 1160 | if (ppid_check == 0) { |
| 1161 | ret = -1; |
| 1162 | fprintf(stderr, "[error] missing ppid context information\n"); |
| 1163 | } |
| 1164 | if (procname_check == 0) { |
| 1165 | ret = -1; |
| 1166 | fprintf(stderr, "[error] missing procname context information\n"); |
| 1167 | } |
| 1168 | if (ret == 0) { |
| 1169 | valid_trace = 1; |
| 1170 | } |
| 1171 | |
| 1172 | end: |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
| 1176 | static void handle_sigchild(int signal) |
| 1177 | { |
| 1178 | int status; |
| 1179 | |
| 1180 | waitpid(opt_exec_pid, &status, 0); |
| 1181 | } |
| 1182 | |
| 1183 | int main(int argc, char **argv, char **envp) |
| 1184 | { |
| 1185 | int ret; |
| 1186 | struct bt_context *bt_ctx = NULL; |
| 1187 | |
| 1188 | //babeltrace_verbose = 1; |
| 1189 | init_lttngtop(); |
| 1190 | ret = parse_options(argc, argv); |
| 1191 | if (ret < 0) { |
| 1192 | fprintf(stdout, "Error parsing options.\n\n"); |
| 1193 | usage(stdout); |
| 1194 | exit(EXIT_FAILURE); |
| 1195 | } else if (ret > 0) { |
| 1196 | exit(EXIT_SUCCESS); |
| 1197 | } |
| 1198 | |
| 1199 | if (opt_exec_name) { |
| 1200 | opt_exec_env = envp; |
| 1201 | signal(SIGCHLD, handle_sigchild); |
| 1202 | } |
| 1203 | |
| 1204 | if (!opt_input_path && !remote_live && !opt_exec_name) { |
| 1205 | /* mmap live */ |
| 1206 | #ifdef LTTNGTOP_MMAP_LIVE |
| 1207 | if (opt_textdump) { |
| 1208 | signal(SIGTERM, handle_textdump_sigterm); |
| 1209 | signal(SIGINT, handle_textdump_sigterm); |
| 1210 | } |
| 1211 | mmap_live_loop(bt_ctx); |
| 1212 | pthread_join(timer_thread, NULL); |
| 1213 | quit = 1; |
| 1214 | pthread_join(display_thread, NULL); |
| 1215 | |
| 1216 | lttng_stop_tracing("test"); |
| 1217 | lttng_destroy_session("test"); |
| 1218 | |
| 1219 | goto end; |
| 1220 | #else |
| 1221 | fprintf(stderr, "[ERROR] Mmap live support not compiled, specify a " |
| 1222 | "trace directory or -r <relayd hostname/IP>\n"); |
| 1223 | usage(stdout); |
| 1224 | ret = -1; |
| 1225 | goto end; |
| 1226 | #endif /* LTTNGTOP_MMAP_LIVE */ |
| 1227 | } else if (!opt_input_path && remote_live) { |
| 1228 | /* network live */ |
| 1229 | bt_ctx = bt_context_create(); |
| 1230 | ret = bt_context_add_traces_recursive(bt_ctx, opt_relay_hostname, |
| 1231 | "lttng-live", NULL); |
| 1232 | if (ret < 0) { |
| 1233 | fprintf(stderr, "[error] Opening the trace\n"); |
| 1234 | goto end; |
| 1235 | } |
| 1236 | } else { |
| 1237 | //init_lttngtop(); |
| 1238 | |
| 1239 | bt_ctx = bt_context_create(); |
| 1240 | ret = bt_context_add_traces_recursive(bt_ctx, opt_input_path, "ctf", NULL); |
| 1241 | if (ret < 0) { |
| 1242 | fprintf(stderr, "[error] Opening the trace\n"); |
| 1243 | goto end; |
| 1244 | } |
| 1245 | |
| 1246 | ret = check_requirements(bt_ctx); |
| 1247 | if (ret < 0 && !valid_trace) { |
| 1248 | fprintf(stderr, "[error] some mandatory contexts " |
| 1249 | "were missing, exiting.\n"); |
| 1250 | //goto end; |
| 1251 | } |
| 1252 | |
| 1253 | if (!opt_textdump) { |
| 1254 | pthread_create(&display_thread, NULL, ncurses_display, |
| 1255 | (void *) NULL); |
| 1256 | pthread_create(&timer_thread, NULL, refresh_thread, |
| 1257 | (void *) NULL); |
| 1258 | } |
| 1259 | |
| 1260 | iter_trace(bt_ctx); |
| 1261 | } |
| 1262 | |
| 1263 | |
| 1264 | pthread_join(display_thread, NULL); |
| 1265 | quit = 1; |
| 1266 | pthread_join(timer_thread, NULL); |
| 1267 | |
| 1268 | ret = 0; |
| 1269 | |
| 1270 | end: |
| 1271 | if (bt_ctx) |
| 1272 | bt_context_put(bt_ctx); |
| 1273 | |
| 1274 | return ret; |
| 1275 | } |