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