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