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