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