2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <netinet/in.h>
36 #include <babeltrace/ctf/ctf-index.h>
38 #include <babeltrace/babeltrace.h>
39 #include <babeltrace/endian.h>
40 #include <babeltrace/ctf/events.h>
41 #include <babeltrace/ctf/callbacks.h>
42 #include <babeltrace/ctf/iterator.h>
44 /* for packet_index */
45 #include <babeltrace/ctf/types.h>
47 #include <babeltrace/ctf/metadata.h>
48 #include <babeltrace/ctf-text/types.h>
49 #include <babeltrace/ctf/events-internal.h>
50 #include <formats/ctf/events-private.h>
52 #include <babeltrace/compat/memstream.h>
53 #include <babeltrace/compat/send.h>
54 #include <babeltrace/compat/string.h>
55 #include <babeltrace/compat/mman.h>
57 #include "lttng-live.h"
58 #include "lttng-viewer-abi.h"
60 #define ACTIVE_POLL_DELAY 100 /* ms */
63 * Memory allocation zeroed
65 #define zmalloc(x) calloc(1, x)
68 #define max_t(type, a, b) \
69 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
72 static void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
,
73 size_t index
, int whence
);
74 static int add_traces(struct lttng_live_ctx
*ctx
);
75 static int del_traces(gpointer key
, gpointer value
, gpointer user_data
);
76 static int get_new_metadata(struct lttng_live_ctx
*ctx
,
77 struct lttng_live_viewer_stream
*viewer_stream
,
81 ssize_t
lttng_live_recv(int fd
, void *buf
, size_t len
)
84 size_t copied
= 0, to_copy
= len
;
87 ret
= recv(fd
, buf
+ copied
, to_copy
, 0);
89 assert(ret
<= to_copy
);
93 } while ((ret
> 0 && to_copy
> 0)
94 || (ret
< 0 && errno
== EINTR
));
97 /* ret = 0 means orderly shutdown, ret < 0 is error. */
102 ssize_t
lttng_live_send(int fd
, const void *buf
, size_t len
)
107 ret
= bt_send_nosigpipe(fd
, buf
, len
);
108 } while (ret
< 0 && errno
== EINTR
);
112 int lttng_live_connect_viewer(struct lttng_live_ctx
*ctx
)
114 struct hostent
*host
;
115 struct sockaddr_in server_addr
;
118 if (lttng_live_should_quit()) {
123 host
= gethostbyname(ctx
->relay_hostname
);
125 fprintf(stderr
, "[error] Cannot lookup hostname %s\n",
126 ctx
->relay_hostname
);
130 if ((ctx
->control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
135 server_addr
.sin_family
= AF_INET
;
136 server_addr
.sin_port
= htons(ctx
->port
);
137 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
138 memset(&(server_addr
.sin_zero
), 0, 8);
140 if (connect(ctx
->control_sock
, (struct sockaddr
*) &server_addr
,
141 sizeof(struct sockaddr
)) == -1) {
152 fprintf(stderr
, "[error] Connection failed\n");
156 int lttng_live_establish_connection(struct lttng_live_ctx
*ctx
)
158 struct lttng_viewer_cmd cmd
;
159 struct lttng_viewer_connect connect
;
160 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(connect
);
161 char cmd_buf
[cmd_buf_len
];
165 if (lttng_live_should_quit()) {
170 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
171 cmd
.data_size
= htobe64((uint64_t) sizeof(connect
));
172 cmd
.cmd_version
= htobe32(0);
174 connect
.viewer_session_id
= -1ULL; /* will be set on recv */
175 connect
.major
= htobe32(LTTNG_LIVE_MAJOR
);
176 connect
.minor
= htobe32(LTTNG_LIVE_MINOR
);
177 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
180 * Merge the cmd and connection request to prevent a write-write
181 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
182 * second write to be performed quickly in presence of Nagle's algorithm.
184 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
185 memcpy(cmd_buf
+ sizeof(cmd
), &connect
, sizeof(connect
));
187 ret_len
= lttng_live_send(ctx
->control_sock
, cmd_buf
, cmd_buf_len
);
189 perror("[error] Error sending cmd for establishing session");
192 assert(ret_len
== cmd_buf_len
);
194 ret_len
= lttng_live_recv(ctx
->control_sock
, &connect
, sizeof(connect
));
196 fprintf(stderr
, "[error] Remote side has closed connection\n");
200 perror("[error] Error receiving version");
203 assert(ret_len
== sizeof(connect
));
205 printf_verbose("Received viewer session ID : %" PRIu64
"\n",
206 be64toh(connect
.viewer_session_id
));
207 printf_verbose("Relayd version : %u.%u\n", be32toh(connect
.major
),
208 be32toh(connect
.minor
));
210 if (LTTNG_LIVE_MAJOR
!= be32toh(connect
.major
)) {
211 fprintf(stderr
, "[error] Incompatible lttng-relayd protocol\n");
214 /* Use the smallest protocol version implemented. */
215 if (LTTNG_LIVE_MINOR
> be32toh(connect
.minor
)) {
216 ctx
->minor
= be32toh(connect
.minor
);
218 ctx
->minor
= LTTNG_LIVE_MINOR
;
220 ctx
->major
= LTTNG_LIVE_MAJOR
;
226 fprintf(stderr
, "[error] Unable to establish connection\n");
231 void free_session_list(GPtrArray
*session_list
)
234 struct lttng_live_relay_session
*relay_session
;
236 for (i
= 0; i
< session_list
->len
; i
++) {
237 relay_session
= g_ptr_array_index(session_list
, i
);
238 free(relay_session
->name
);
239 free(relay_session
->hostname
);
241 g_ptr_array_free(session_list
, TRUE
);
245 void print_session_list(GPtrArray
*session_list
, const char *path
)
248 struct lttng_live_relay_session
*relay_session
;
250 for (i
= 0; i
< session_list
->len
; i
++) {
251 relay_session
= g_ptr_array_index(session_list
, i
);
252 fprintf(LTTNG_LIVE_OUTPUT_FP
, "%s/host/%s/%s (timer = %u, "
253 "%u stream(s), %u client(s) connected)\n",
254 path
, relay_session
->hostname
,
255 relay_session
->name
, relay_session
->timer
,
256 relay_session
->streams
, relay_session
->clients
);
261 void update_session_list(GPtrArray
*session_list
, char *hostname
,
262 char *session_name
, uint32_t streams
, uint32_t clients
,
266 struct lttng_live_relay_session
*relay_session
;
268 for (i
= 0; i
< session_list
->len
; i
++) {
269 relay_session
= g_ptr_array_index(session_list
, i
);
270 if ((strncmp(relay_session
->hostname
, hostname
, MAXNAMLEN
) == 0) &&
271 strncmp(relay_session
->name
,
272 session_name
, MAXNAMLEN
) == 0) {
273 relay_session
->streams
+= streams
;
274 if (relay_session
->clients
< clients
)
275 relay_session
->clients
= clients
;
283 relay_session
= g_new0(struct lttng_live_relay_session
, 1);
284 relay_session
->hostname
= bt_strndup(hostname
, MAXNAMLEN
);
285 relay_session
->name
= bt_strndup(session_name
, MAXNAMLEN
);
286 relay_session
->clients
= clients
;
287 relay_session
->streams
= streams
;
288 relay_session
->timer
= timer
;
289 g_ptr_array_add(session_list
, relay_session
);
292 int lttng_live_list_sessions(struct lttng_live_ctx
*ctx
, const char *path
)
294 struct lttng_viewer_cmd cmd
;
295 struct lttng_viewer_list_sessions list
;
296 struct lttng_viewer_session lsession
;
297 int i
, ret
, sessions_count
, print_list
= 0;
300 GPtrArray
*session_list
= NULL
;
302 if (lttng_live_should_quit()) {
307 if (strlen(ctx
->session_name
) == 0) {
309 session_list
= g_ptr_array_new();
312 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
313 cmd
.data_size
= htobe64((uint64_t) 0);
314 cmd
.cmd_version
= htobe32(0);
316 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
318 perror("[error] Error sending cmd");
321 assert(ret_len
== sizeof(cmd
));
323 ret_len
= lttng_live_recv(ctx
->control_sock
, &list
, sizeof(list
));
325 fprintf(stderr
, "[error] Remote side has closed connection\n");
329 perror("[error] Error receiving session list");
332 assert(ret_len
== sizeof(list
));
334 sessions_count
= be32toh(list
.sessions_count
);
335 for (i
= 0; i
< sessions_count
; i
++) {
336 ret_len
= lttng_live_recv(ctx
->control_sock
, &lsession
, sizeof(lsession
));
338 fprintf(stderr
, "[error] Remote side has closed connection\n");
342 perror("[error] Error receiving session");
345 assert(ret_len
== sizeof(lsession
));
346 lsession
.hostname
[LTTNG_VIEWER_HOST_NAME_MAX
- 1] = '\0';
347 lsession
.session_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
348 session_id
= be64toh(lsession
.id
);
351 update_session_list(session_list
,
353 lsession
.session_name
,
354 be32toh(lsession
.streams
),
355 be32toh(lsession
.clients
),
356 be32toh(lsession
.live_timer
));
358 if ((strncmp(lsession
.session_name
, ctx
->session_name
,
359 MAXNAMLEN
) == 0) && (strncmp(lsession
.hostname
,
360 ctx
->traced_hostname
, MAXNAMLEN
) == 0)) {
361 printf_verbose("Reading from session %" PRIu64
"\n",
363 g_array_append_val(ctx
->session_ids
,
370 print_session_list(session_list
, path
);
371 free_session_list(session_list
);
378 fprintf(stderr
, "[error] Unable to list sessions\n");
382 int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream
*stream
,
383 uint64_t ctf_trace_id
)
385 struct lttng_live_ctf_trace
*trace
;
388 trace
= g_hash_table_lookup(stream
->session
->ctf_traces
,
391 trace
= g_new0(struct lttng_live_ctf_trace
, 1);
392 trace
->ctf_trace_id
= ctf_trace_id
;
393 printf_verbose("Create trace ctf_trace_id %" PRIu64
"\n", ctf_trace_id
);
394 BT_INIT_LIST_HEAD(&trace
->stream_list
);
395 g_hash_table_insert(stream
->session
->ctf_traces
,
396 &trace
->ctf_trace_id
,
399 if (stream
->metadata_flag
)
400 trace
->metadata_stream
= stream
;
402 assert(!stream
->in_trace
);
403 stream
->in_trace
= 1;
404 stream
->ctf_trace
= trace
;
405 bt_list_add(&stream
->trace_stream_node
, &trace
->stream_list
);
411 int open_metadata_fp_write(struct lttng_live_viewer_stream
*stream
,
412 char **metadata_buf
, size_t *size
)
416 stream
->metadata_fp_write
=
417 babeltrace_open_memstream(metadata_buf
, size
);
418 if (!stream
->metadata_fp_write
) {
419 perror("Metadata open_memstream");
426 int lttng_live_attach_session(struct lttng_live_ctx
*ctx
, uint64_t id
)
428 struct lttng_viewer_cmd cmd
;
429 struct lttng_viewer_attach_session_request rq
;
430 struct lttng_viewer_attach_session_response rp
;
431 struct lttng_viewer_stream stream
;
432 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
433 char cmd_buf
[cmd_buf_len
];
437 if (lttng_live_should_quit()) {
442 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
443 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
444 cmd
.cmd_version
= htobe32(0);
446 memset(&rq
, 0, sizeof(rq
));
447 rq
.session_id
= htobe64(id
);
448 // TODO: add cmd line parameter to select seek beginning
449 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
450 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_LAST
);
453 * Merge the cmd and connection request to prevent a write-write
454 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
455 * second write to be performed quickly in presence of Nagle's algorithm.
457 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
458 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
460 ret_len
= lttng_live_send(ctx
->control_sock
, cmd_buf
, cmd_buf_len
);
462 perror("[error] Error sending attach command and request");
465 assert(ret_len
== cmd_buf_len
);
467 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
469 fprintf(stderr
, "[error] Remote side has closed connection\n");
473 perror("[error] Error receiving attach response");
476 assert(ret_len
== sizeof(rp
));
478 switch(be32toh(rp
.status
)) {
479 case LTTNG_VIEWER_ATTACH_OK
:
481 case LTTNG_VIEWER_ATTACH_UNK
:
482 ret
= -LTTNG_VIEWER_ATTACH_UNK
;
484 case LTTNG_VIEWER_ATTACH_ALREADY
:
485 fprintf(stderr
, "[error] There is already a viewer attached to this session\n");
487 case LTTNG_VIEWER_ATTACH_NOT_LIVE
:
488 fprintf(stderr
, "[error] Not a live session\n");
490 case LTTNG_VIEWER_ATTACH_SEEK_ERR
:
491 fprintf(stderr
, "[error] Wrong seek parameter\n");
494 fprintf(stderr
, "[error] Unknown attach return code %u\n",
498 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
502 ctx
->session
->stream_count
+= be32toh(rp
.streams_count
);
504 * When the session is created but not started, we do an active wait
505 * until it starts. It allows the viewer to start processing the trace
506 * as soon as the session starts.
508 if (ctx
->session
->stream_count
== 0) {
512 printf_verbose("Waiting for %d streams:\n",
513 be32toh(rp
.streams_count
));
514 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
515 struct lttng_live_viewer_stream
*lvstream
;
517 lvstream
= g_new0(struct lttng_live_viewer_stream
, 1);
518 ret_len
= lttng_live_recv(ctx
->control_sock
, &stream
,
521 fprintf(stderr
, "[error] Remote side has closed connection\n");
526 perror("[error] Error receiving stream");
530 assert(ret_len
== sizeof(stream
));
531 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
532 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
534 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
535 be64toh(stream
.id
), stream
.path_name
,
536 stream
.channel_name
);
537 lvstream
->id
= be64toh(stream
.id
);
538 lvstream
->session
= ctx
->session
;
540 lvstream
->mmap_size
= 0;
541 lvstream
->ctf_stream_id
= -1ULL;
543 if (be32toh(stream
.metadata_flag
)) {
544 lvstream
->metadata_flag
= 1;
546 ret
= lttng_live_ctf_trace_assign(lvstream
,
547 be64toh(stream
.ctf_trace_id
));
552 bt_list_add(&lvstream
->session_stream_node
,
553 &ctx
->session
->stream_list
);
564 * Ask the relay for new streams.
566 * Returns the number of new streams received or a negative value on error.
569 int ask_new_streams(struct lttng_live_ctx
*ctx
)
571 int i
, ret
= 0, nb_streams
= 0;
575 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
576 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
577 ret
= lttng_live_get_new_streams(ctx
, id
);
578 printf_verbose("Asking for new streams returns %d\n", ret
);
579 if (lttng_live_should_quit()) {
584 if (ret
== -LTTNG_VIEWER_NEW_STREAMS_HUP
) {
585 printf_verbose("Session %" PRIu64
" closed\n",
588 * The streams have already been closed during
589 * the reading, so we only need to get rid of
590 * the trace in our internal table of sessions.
592 g_array_remove_index(ctx
->session_ids
, i
);
594 * We can't continue iterating on the g_array
595 * after a remove, we have to start again.
606 if (ctx
->session_ids
->len
== 0) {
607 /* All sessions are closed. */
618 int append_metadata(struct lttng_live_ctx
*ctx
,
619 struct lttng_live_viewer_stream
*viewer_stream
)
622 struct lttng_live_viewer_stream
*metadata
;
623 char *metadata_buf
= NULL
;
625 if (!viewer_stream
->ctf_trace
->handle
) {
626 printf_verbose("append_metadata: trace handle not ready yet.\n");
630 printf_verbose("get_next_index: new metadata needed\n");
631 ret
= get_new_metadata(ctx
, viewer_stream
, &metadata_buf
);
637 metadata
= viewer_stream
->ctf_trace
->metadata_stream
;
638 metadata
->ctf_trace
->metadata_fp
=
639 babeltrace_fmemopen(metadata_buf
,
640 metadata
->metadata_len
, "rb");
641 if (!metadata
->ctf_trace
->metadata_fp
) {
642 perror("Metadata fmemopen");
647 ret
= ctf_append_trace_metadata(
648 viewer_stream
->ctf_trace
->handle
->td
,
649 metadata
->ctf_trace
->metadata_fp
);
650 /* We accept empty metadata packets */
651 if (ret
!= 0 && ret
!= -ENOENT
) {
652 fprintf(stderr
, "[error] Appending metadata\n");
662 int get_data_packet(struct lttng_live_ctx
*ctx
,
663 struct ctf_stream_pos
*pos
,
664 struct lttng_live_viewer_stream
*stream
, uint64_t offset
,
667 struct lttng_viewer_cmd cmd
;
668 struct lttng_viewer_get_packet rq
;
669 struct lttng_viewer_trace_packet rp
;
670 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
671 char cmd_buf
[cmd_buf_len
];
676 if (lttng_live_should_quit()) {
681 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
682 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
683 cmd
.cmd_version
= htobe32(0);
685 memset(&rq
, 0, sizeof(rq
));
686 rq
.stream_id
= htobe64(stream
->id
);
687 rq
.offset
= htobe64(offset
);
688 rq
.len
= htobe32(len
);
691 * Merge the cmd and connection request to prevent a write-write
692 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
693 * second write to be performed quickly in presence of Nagle's algorithm.
695 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
696 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
698 ret_len
= lttng_live_send(ctx
->control_sock
, cmd_buf
, cmd_buf_len
);
700 perror("[error] Error sending get_data_packet cmd and request");
703 assert(ret_len
== cmd_buf_len
);
705 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
707 fprintf(stderr
, "[error] Remote side has closed connection\n");
711 perror("[error] Error receiving data response");
714 if (ret_len
!= sizeof(rp
)) {
715 fprintf(stderr
, "[error] get_data_packet: expected %zu"
716 ", received %zd\n", sizeof(rp
),
721 rp
.flags
= be32toh(rp
.flags
);
723 switch (be32toh(rp
.status
)) {
724 case LTTNG_VIEWER_GET_PACKET_OK
:
725 len
= be32toh(rp
.len
);
726 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
729 case LTTNG_VIEWER_GET_PACKET_RETRY
:
730 /* Unimplemented by relay daemon */
731 printf_verbose("get_data_packet: retry\n");
733 case LTTNG_VIEWER_GET_PACKET_ERR
:
734 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
735 printf_verbose("get_data_packet: new metadata needed\n");
736 ret
= append_metadata(ctx
, stream
);
740 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
741 printf_verbose("get_data_packet: new streams needed\n");
742 ret
= ask_new_streams(ctx
);
745 } else if (ret
> 0) {
746 ret
= add_traces(ctx
);
752 if (rp
.flags
& (LTTNG_VIEWER_FLAG_NEW_METADATA
753 | LTTNG_VIEWER_FLAG_NEW_STREAM
)) {
756 fprintf(stderr
, "[error] get_data_packet: error\n");
758 case LTTNG_VIEWER_GET_PACKET_EOF
:
762 printf_verbose("get_data_packet: unknown\n");
770 if (len
> stream
->mmap_size
) {
773 new_size
= max_t(uint64_t, len
, stream
->mmap_size
<< 1);
776 ret
= munmap_align(pos
->base_mma
);
778 perror("[error] Unable to unmap old base");
781 pos
->base_mma
= NULL
;
783 pos
->base_mma
= mmap_align(new_size
,
784 PROT_READ
| PROT_WRITE
,
785 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
786 if (pos
->base_mma
== MAP_FAILED
) {
787 perror("[error] mmap error");
788 pos
->base_mma
= NULL
;
792 stream
->mmap_size
= new_size
;
793 printf_verbose("Expanding stream mmap size to %" PRIu64
" bytes\n",
797 ret_len
= lttng_live_recv(ctx
->control_sock
,
798 mmap_align_addr(pos
->base_mma
), len
);
800 fprintf(stderr
, "[error] Remote side has closed connection\n");
804 perror("[error] Error receiving trace packet");
807 assert(ret_len
== len
);
817 int get_one_metadata_packet(struct lttng_live_ctx
*ctx
,
818 struct lttng_live_viewer_stream
*metadata_stream
)
822 struct lttng_viewer_cmd cmd
;
823 struct lttng_viewer_get_metadata rq
;
824 struct lttng_viewer_metadata_packet rp
;
827 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
828 char cmd_buf
[cmd_buf_len
];
830 if (lttng_live_should_quit()) {
835 rq
.stream_id
= htobe64(metadata_stream
->id
);
836 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
837 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
838 cmd
.cmd_version
= htobe32(0);
841 * Merge the cmd and connection request to prevent a write-write
842 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
843 * second write to be performed quickly in presence of Nagle's algorithm.
845 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
846 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
848 printf_verbose("get_metadata for trace_id: %d, ctf_trace_id: %" PRIu64
"\n",
849 metadata_stream
->ctf_trace
->trace_id
,
850 metadata_stream
->ctf_trace
->ctf_trace_id
);
851 ret_len
= lttng_live_send(ctx
->control_sock
, cmd_buf
, cmd_buf_len
);
853 perror("[error] Error sending get_metadata cmd and request");
856 assert(ret_len
== cmd_buf_len
);
858 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
860 fprintf(stderr
, "[error] Remote side has closed connection\n");
864 perror("[error] Error receiving metadata response");
867 assert(ret_len
== sizeof(rp
));
869 switch (be32toh(rp
.status
)) {
870 case LTTNG_VIEWER_METADATA_OK
:
871 printf_verbose("get_metadata : OK\n");
873 case LTTNG_VIEWER_NO_NEW_METADATA
:
874 printf_verbose("get_metadata : NO NEW\n");
877 case LTTNG_VIEWER_METADATA_ERR
:
878 printf_verbose("get_metadata : ERR\n");
881 printf_verbose("get_metadata : UNKNOWN\n");
885 len
= be64toh(rp
.len
);
886 printf_verbose("Writing %" PRIu64
" bytes to metadata\n", len
);
893 perror("relay data zmalloc");
896 ret_len
= lttng_live_recv(ctx
->control_sock
, data
, len
);
898 fprintf(stderr
, "[error] Remote side has closed connection\n");
899 goto error_free_data
;
902 perror("[error] Error receiving trace packet");
903 goto error_free_data
;
905 assert(ret_len
== len
);
908 ret_len
= fwrite(data
, 1, len
,
909 metadata_stream
->metadata_fp_write
);
910 } while (ret_len
< 0 && errno
== EINTR
);
912 fprintf(stderr
, "[error] Writing in the metadata fp\n");
913 goto error_free_data
;
915 assert(ret_len
== len
);
916 metadata_stream
->metadata_len
+= len
;
929 * Return 0 on success, a negative value on error.
932 int get_new_metadata(struct lttng_live_ctx
*ctx
,
933 struct lttng_live_viewer_stream
*viewer_stream
,
937 struct lttng_live_viewer_stream
*metadata_stream
;
938 size_t size
, len_read
= 0;
940 metadata_stream
= viewer_stream
->ctf_trace
->metadata_stream
;
941 if (!metadata_stream
) {
942 fprintf(stderr
, "[error] No metadata stream\n");
946 metadata_stream
->metadata_len
= 0;
947 ret
= open_metadata_fp_write(metadata_stream
, metadata_buf
, &size
);
953 if (lttng_live_should_quit()) {
958 * get_one_metadata_packet returns the number of bytes
959 * received, 0 when we have received everything, a
960 * negative value on error.
962 ret
= get_one_metadata_packet(ctx
, metadata_stream
);
967 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
970 break; /* Stop on error. */
972 } while (ret
> 0 || !len_read
);
974 if (babeltrace_close_memstream(metadata_buf
, &size
,
975 metadata_stream
->metadata_fp_write
)) {
976 perror("babeltrace_close_memstream");
978 metadata_stream
->metadata_fp_write
= NULL
;
985 * Assign the fields from a lttng_viewer_index to a packet_index.
988 void lttng_index_to_packet_index(struct lttng_viewer_index
*lindex
,
989 struct packet_index
*pindex
)
994 pindex
->offset
= be64toh(lindex
->offset
);
995 pindex
->packet_size
= be64toh(lindex
->packet_size
);
996 pindex
->content_size
= be64toh(lindex
->content_size
);
997 pindex
->ts_cycles
.timestamp_begin
= be64toh(lindex
->timestamp_begin
);
998 pindex
->ts_cycles
.timestamp_end
= be64toh(lindex
->timestamp_end
);
999 pindex
->events_discarded
= be64toh(lindex
->events_discarded
);
1003 * Get one index for a stream.
1005 * Returns 0 on success or a negative value on error.
1008 int get_next_index(struct lttng_live_ctx
*ctx
,
1009 struct lttng_live_viewer_stream
*viewer_stream
,
1010 struct packet_index
*index
, uint64_t *stream_id
)
1012 struct lttng_viewer_cmd cmd
;
1013 struct lttng_viewer_get_next_index rq
;
1016 struct lttng_viewer_index
*rp
= &viewer_stream
->current_index
;
1017 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1018 char cmd_buf
[cmd_buf_len
];
1020 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
1021 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1022 cmd
.cmd_version
= htobe32(0);
1024 memset(&rq
, 0, sizeof(rq
));
1025 rq
.stream_id
= htobe64(viewer_stream
->id
);
1028 * Merge the cmd and connection request to prevent a write-write
1029 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1030 * second write to be performed quickly in presence of Nagle's algorithm.
1032 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1033 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1035 if (lttng_live_should_quit()) {
1039 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd_buf
, cmd_buf_len
);
1041 perror("[error] Error sending get_next_index cmd and request");
1044 assert(ret_len
== cmd_buf_len
);
1046 ret_len
= lttng_live_recv(ctx
->control_sock
, rp
, sizeof(*rp
));
1048 fprintf(stderr
, "[error] Remote side has closed connection\n");
1052 perror("[error] Error receiving index response");
1055 assert(ret_len
== sizeof(*rp
));
1057 rp
->flags
= be32toh(rp
->flags
);
1059 switch (be32toh(rp
->status
)) {
1060 case LTTNG_VIEWER_INDEX_INACTIVE
:
1061 printf_verbose("get_next_index: inactive\n");
1063 if (index
->ts_cycles
.timestamp_end
==
1064 be64toh(rp
->timestamp_end
)) {
1065 /* Already seen this timestamp. */
1066 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1069 memset(index
, 0, sizeof(struct packet_index
));
1070 index
->ts_cycles
.timestamp_end
= be64toh(rp
->timestamp_end
);
1071 *stream_id
= be64toh(rp
->stream_id
);
1073 case LTTNG_VIEWER_INDEX_OK
:
1074 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
1075 rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
);
1076 lttng_index_to_packet_index(rp
, index
);
1077 *stream_id
= be64toh(rp
->stream_id
);
1078 viewer_stream
->data_pending
= 1;
1080 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
1081 ret
= append_metadata(ctx
, viewer_stream
);
1085 if (rp
->flags
& LTTNG_VIEWER_FLAG_NEW_STREAM
) {
1086 printf_verbose("get_next_index: need new streams\n");
1087 ret
= ask_new_streams(ctx
);
1090 } else if (ret
> 0) {
1091 ret
= add_traces(ctx
);
1098 case LTTNG_VIEWER_INDEX_RETRY
:
1099 printf_verbose("get_next_index: retry\n");
1100 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1102 case LTTNG_VIEWER_INDEX_HUP
:
1103 printf_verbose("get_next_index: stream hung up\n");
1104 viewer_stream
->id
= -1ULL;
1105 index
->offset
= EOF
;
1106 ctx
->session
->stream_count
--;
1107 viewer_stream
->in_trace
= 0;
1108 bt_list_del(&viewer_stream
->trace_stream_node
);
1109 bt_list_del(&viewer_stream
->session_stream_node
);
1110 g_free(viewer_stream
);
1111 *stream_id
= be64toh(rp
->stream_id
);
1113 case LTTNG_VIEWER_INDEX_ERR
:
1114 fprintf(stderr
, "[error] get_next_index: error\n");
1117 fprintf(stderr
, "[error] get_next_index: unkwown value\n");
1129 void read_packet_header(struct ctf_stream_pos
*pos
,
1130 struct ctf_file_stream
*file_stream
)
1134 /* update trace_packet_header and stream_packet_context */
1135 if (!(pos
->prot
& PROT_WRITE
) &&
1136 file_stream
->parent
.trace_packet_header
) {
1137 /* Read packet header */
1138 ret
= generic_rw(&pos
->parent
,
1139 &file_stream
->parent
.trace_packet_header
->p
);
1142 fprintf(stderr
, "[error] trace packet "
1143 "header read failed\n");
1147 if (!(pos
->prot
& PROT_WRITE
) &&
1148 file_stream
->parent
.stream_packet_context
) {
1149 /* Read packet context */
1150 ret
= generic_rw(&pos
->parent
,
1151 &file_stream
->parent
.stream_packet_context
->p
);
1154 fprintf(stderr
, "[error] stream packet "
1155 "context read failed\n");
1159 pos
->data_offset
= pos
->offset
;
1166 * Handle the seek parameters.
1167 * Returns 0 if the packet_seek can continue, a positive value to
1168 * cleanly exit the packet_seek, a negative value on error.
1171 int handle_seek_position(size_t index
, int whence
,
1172 struct lttng_live_viewer_stream
*viewer_stream
,
1173 struct ctf_stream_pos
*pos
,
1174 struct ctf_file_stream
*file_stream
)
1184 * We only allow to seek to 0.
1187 fprintf(stderr
, "[error] Arbitrary seek in lttng-live "
1188 "trace not supported\n");
1198 fprintf(stderr
, "[error] Invalid seek parameter\n");
1207 void ctf_live_packet_seek(struct bt_stream_pos
*stream_pos
, size_t index
,
1210 struct ctf_stream_pos
*pos
;
1211 struct ctf_file_stream
*file_stream
;
1212 struct packet_index
*prev_index
= NULL
, *cur_index
;
1213 struct lttng_live_viewer_stream
*viewer_stream
;
1214 struct lttng_live_session
*session
;
1215 uint64_t stream_id
= -1ULL;
1218 pos
= ctf_pos(stream_pos
);
1219 file_stream
= container_of(pos
, struct ctf_file_stream
, pos
);
1220 viewer_stream
= (struct lttng_live_viewer_stream
*) pos
->priv
;
1221 session
= viewer_stream
->session
;
1223 ret
= handle_seek_position(index
, whence
, viewer_stream
, pos
,
1226 ret
= -BT_PACKET_SEEK_ERROR
;
1231 switch (pos
->packet_index
->len
) {
1233 g_array_set_size(pos
->packet_index
, 1);
1234 cur_index
= &g_array_index(pos
->packet_index
,
1235 struct packet_index
, 0);
1238 g_array_set_size(pos
->packet_index
, 2);
1239 prev_index
= &g_array_index(pos
->packet_index
,
1240 struct packet_index
, 0);
1241 cur_index
= &g_array_index(pos
->packet_index
,
1242 struct packet_index
, 1);
1245 g_array_index(pos
->packet_index
,
1246 struct packet_index
, 0) =
1247 g_array_index(pos
->packet_index
,
1248 struct packet_index
, 1);
1249 prev_index
= &g_array_index(pos
->packet_index
,
1250 struct packet_index
, 0);
1251 cur_index
= &g_array_index(pos
->packet_index
,
1252 struct packet_index
, 1);
1259 if (viewer_stream
->data_pending
) {
1260 lttng_index_to_packet_index(&viewer_stream
->current_index
, cur_index
);
1262 printf_verbose("get_next_index for stream %" PRIu64
"\n", viewer_stream
->id
);
1263 ret
= get_next_index(session
->ctx
, viewer_stream
, cur_index
, &stream_id
);
1266 if (!lttng_live_should_quit()) {
1267 fprintf(stderr
, "[error] get_next_index failed\n");
1269 ret
= -BT_PACKET_SEEK_ERROR
;
1272 printf_verbose("Index received : packet_size : %" PRIu64
1273 ", offset %" PRIu64
", content_size %" PRIu64
1274 ", timestamp_end : %" PRIu64
"\n",
1275 cur_index
->packet_size
, cur_index
->offset
,
1276 cur_index
->content_size
,
1277 cur_index
->ts_cycles
.timestamp_end
);
1282 * On the first time we receive an index, the stream_id needs to
1283 * be set for the stream in order to use it, we don't want any
1284 * data at this stage.
1286 if (file_stream
->parent
.stream_id
== -1ULL) {
1288 * Warning: with lttng-tools < 2.4.2, the beacon does not
1289 * contain the real stream ID, it is memset to 0, so this
1290 * might create a problem when a session has multiple
1291 * channels. We can't detect it at this stage, lttng-tools
1292 * has to be upgraded to fix this problem.
1294 printf_verbose("Assigning stream_id %" PRIu64
"\n",
1296 file_stream
->parent
.stream_id
= stream_id
;
1297 viewer_stream
->ctf_stream_id
= stream_id
;
1303 pos
->packet_size
= cur_index
->packet_size
;
1304 pos
->content_size
= cur_index
->content_size
;
1305 pos
->mmap_base_offset
= 0;
1306 if (cur_index
->offset
== EOF
) {
1312 if (cur_index
->content_size
== 0) {
1313 /* Beacon packet index */
1314 if (file_stream
->parent
.stream_class
) {
1315 file_stream
->parent
.cycles_timestamp
=
1316 cur_index
->ts_cycles
.timestamp_end
;
1317 file_stream
->parent
.real_timestamp
= ctf_get_real_timestamp(
1318 &file_stream
->parent
,
1319 cur_index
->ts_cycles
.timestamp_end
);
1322 * Duplicate the data from the previous index, because
1323 * the one we just received is only a beacon with no
1324 * relevant information except the timestamp_end. We
1325 * don't need to keep this timestamp_end because we already
1326 * updated the file_stream timestamps, so we only need
1327 * to keep the last real index data as prev_index. That
1328 * way, we keep the original prev timestamps and
1329 * discarded events counter. This is the same behaviour
1330 * as if we were reading a local trace, we would not
1331 * have fake indexes between real indexes.
1333 memcpy(cur_index
, prev_index
, sizeof(struct packet_index
));
1336 /* Real packet index */
1337 if (file_stream
->parent
.stream_class
) {
1338 /* Convert the timestamps and append to the real_index. */
1339 cur_index
->ts_real
.timestamp_begin
= ctf_get_real_timestamp(
1340 &file_stream
->parent
,
1341 cur_index
->ts_cycles
.timestamp_begin
);
1342 cur_index
->ts_real
.timestamp_end
= ctf_get_real_timestamp(
1343 &file_stream
->parent
,
1344 cur_index
->ts_cycles
.timestamp_end
);
1347 ctf_update_current_packet_index(&file_stream
->parent
,
1348 prev_index
, cur_index
);
1351 * We need to check if we are in trace read or called
1352 * from packet indexing. In this last case, the
1353 * collection is not there, so we cannot print the
1356 if ((&file_stream
->parent
)->stream_class
->trace
->parent
.collection
) {
1357 ctf_print_discarded_lost(stderr
, &file_stream
->parent
);
1360 file_stream
->parent
.cycles_timestamp
=
1361 cur_index
->ts_cycles
.timestamp_begin
;
1362 file_stream
->parent
.real_timestamp
=
1363 cur_index
->ts_real
.timestamp_begin
;
1367 * Flush the output between attempts to grab a packet, thus
1368 * ensuring we flush at least at the periodical timer period.
1369 * This ensures the output remains reactive for interactive users and
1370 * that the output is flushed when redirected to a file by the shell.
1372 if (fflush(LTTNG_LIVE_OUTPUT_FP
) < 0) {
1377 if (pos
->packet_size
== 0 || pos
->offset
== EOF
) {
1381 printf_verbose("get_data_packet for stream %" PRIu64
"\n",
1383 ret
= get_data_packet(session
->ctx
, pos
, viewer_stream
,
1385 cur_index
->packet_size
/ CHAR_BIT
);
1388 } else if (ret
< 0) {
1390 if (!lttng_live_should_quit()) {
1391 fprintf(stderr
, "[error] get_data_packet failed\n");
1392 ret
= -BT_PACKET_SEEK_ERROR
;
1398 viewer_stream
->data_pending
= 0;
1400 read_packet_header(pos
, file_stream
);
1403 bt_packet_seek_set_error(ret
);
1406 int lttng_live_create_viewer_session(struct lttng_live_ctx
*ctx
)
1408 struct lttng_viewer_cmd cmd
;
1409 struct lttng_viewer_create_session_response resp
;
1413 if (lttng_live_should_quit()) {
1418 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
1419 cmd
.data_size
= htobe64((uint64_t) 0);
1420 cmd
.cmd_version
= htobe32(0);
1422 ret_len
= lttng_live_send(ctx
->control_sock
, &cmd
, sizeof(cmd
));
1424 perror("[error] Error sending cmd");
1427 assert(ret_len
== sizeof(cmd
));
1429 ret_len
= lttng_live_recv(ctx
->control_sock
, &resp
, sizeof(resp
));
1431 fprintf(stderr
, "[error] Remote side has closed connection\n");
1435 perror("[error] Error receiving create session reply");
1438 assert(ret_len
== sizeof(resp
));
1440 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
1441 fprintf(stderr
, "[error] Error creating viewer session\n");
1453 int del_traces(gpointer key
, gpointer value
, gpointer user_data
)
1455 struct bt_context
*bt_ctx
= user_data
;
1456 struct lttng_live_ctf_trace
*trace
= value
;
1458 struct lttng_live_viewer_stream
*lvstream
, *tmp
;
1461 * We don't have ownership of the live viewer stream, just
1462 * remove them from our list.
1464 bt_list_for_each_entry_safe(lvstream
, tmp
, &trace
->stream_list
,
1465 trace_stream_node
) {
1466 lvstream
->in_trace
= 0;
1467 bt_list_del(&lvstream
->trace_stream_node
);
1469 if (trace
->in_use
) {
1470 ret
= bt_context_remove_trace(bt_ctx
, trace
->trace_id
);
1472 fprintf(stderr
, "[error] removing trace from context\n");
1475 /* remove the key/value pair from the HT. */
1480 int add_one_trace(struct lttng_live_ctx
*ctx
,
1481 struct lttng_live_ctf_trace
*trace
)
1484 struct bt_context
*bt_ctx
= ctx
->bt_ctx
;
1485 struct lttng_live_viewer_stream
*stream
;
1486 struct bt_mmap_stream
*new_mmap_stream
;
1487 struct bt_mmap_stream_list mmap_list
;
1488 struct bt_trace_descriptor
*td
;
1489 struct bt_trace_handle
*handle
;
1491 printf_verbose("Add one trace ctf_trace_id: %" PRIu64
1492 " (metadata_stream: %p)\n",
1493 trace
->ctf_trace_id
, trace
->metadata_stream
);
1495 * We don't know how many streams we will receive for a trace, so
1496 * once we are done receiving the traces, we add all the traces
1497 * received to the bt_context.
1498 * We can receive streams during the attach command or the
1499 * get_new_streams, so we have to make sure not to add multiple
1500 * times the same traces.
1501 * If a trace is already in the context, we just skip this function.
1503 if (trace
->in_use
) {
1504 printf_verbose("Trace already in use\n");
1509 * add_one_trace can be called recursively if during the
1510 * bt_context_add_trace call we need to fetch new streams, so we need to
1511 * prevent a recursive call to process our current trace.
1515 BT_INIT_LIST_HEAD(&mmap_list
.head
);
1517 bt_list_for_each_entry(stream
, &trace
->stream_list
, trace_stream_node
) {
1518 if (!stream
->metadata_flag
) {
1519 new_mmap_stream
= zmalloc(sizeof(struct bt_mmap_stream
));
1520 new_mmap_stream
->priv
= (void *) stream
;
1521 new_mmap_stream
->fd
= -1;
1522 bt_list_add(&new_mmap_stream
->list
, &mmap_list
.head
);
1524 char *metadata_buf
= NULL
;
1526 /* Get all possible metadata before starting */
1527 ret
= get_new_metadata(ctx
, stream
, &metadata_buf
);
1532 if (!stream
->metadata_len
) {
1533 fprintf(stderr
, "[error] empty metadata\n");
1539 printf_verbose("Metadata stream found\n");
1540 trace
->metadata_fp
= babeltrace_fmemopen(metadata_buf
,
1541 stream
->metadata_len
, "rb");
1542 if (!trace
->metadata_fp
) {
1543 perror("Metadata fmemopen");
1551 if (!trace
->metadata_fp
) {
1552 fprintf(stderr
, "[error] No metadata stream opened\n");
1557 ret
= bt_context_add_trace(bt_ctx
, NULL
, "ctf",
1558 ctf_live_packet_seek
, &mmap_list
, trace
->metadata_fp
);
1560 fprintf(stderr
, "[error] Error adding trace\n");
1564 trace
->metadata_stream
->metadata_len
= 0;
1566 handle
= (struct bt_trace_handle
*) g_hash_table_lookup(
1567 bt_ctx
->trace_handles
,
1568 (gpointer
) (unsigned long) ret
);
1570 trace
->handle
= handle
;
1571 if (bt_ctx
->current_iterator
) {
1572 bt_iter_add_trace(bt_ctx
->current_iterator
, td
);
1575 trace
->trace_id
= ret
;
1576 printf_verbose("Trace now in use, id = %d\n", trace
->trace_id
);
1581 bt_context_put(bt_ctx
);
1587 * Make sure all the traces we know have a metadata stream or loop on
1588 * ask_new_streams until it is done. This must be called before we call
1591 * Return 0 when all known traces have a metadata stream, a negative value
1595 int check_traces_metadata(struct lttng_live_ctx
*ctx
)
1598 struct lttng_live_ctf_trace
*trace
;
1604 g_hash_table_iter_init(&it
, ctx
->session
->ctf_traces
);
1605 while (g_hash_table_iter_next(&it
, &key
, &value
)) {
1606 trace
= (struct lttng_live_ctf_trace
*) value
;
1607 printf_verbose("Check trace %" PRIu64
" metadata\n", trace
->ctf_trace_id
);
1608 while (!trace
->metadata_stream
) {
1609 printf_verbose("Waiting for metadata stream\n");
1610 if (lttng_live_should_quit()) {
1614 ret
= ask_new_streams(ctx
);
1617 } else if (ret
== 0) {
1618 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1621 * If ask_new_stream got streams from a trace we did not know
1622 * about until now, we have to reinitialize the iterator.
1632 printf_verbose("End check traces metadata\n");
1637 int add_traces(struct lttng_live_ctx
*ctx
)
1640 struct lttng_live_ctf_trace
*trace
;
1644 unsigned int nr_traces
;
1646 printf_verbose("Begin add traces\n");
1649 nr_traces
= g_hash_table_size(ctx
->session
->ctf_traces
);
1651 ret
= check_traces_metadata(ctx
);
1656 g_hash_table_iter_init(&it
, ctx
->session
->ctf_traces
);
1657 while (g_hash_table_iter_next(&it
, &key
, &value
)) {
1658 trace
= (struct lttng_live_ctf_trace
*) value
;
1659 ret
= add_one_trace(ctx
, trace
);
1664 * If a new trace got added while we were adding the trace, the
1665 * iterator is invalid and we have to restart.
1667 if (g_hash_table_size(ctx
->session
->ctf_traces
) != nr_traces
) {
1668 printf_verbose("New trace(s) added during add_one_trace()\n");
1669 printf_verbose("JORAJ: GREP HERE\n");
1677 printf_verbose("End add traces\n");
1682 * Request new streams for a session.
1683 * Returns the number of streams received or a negative value on error.
1685 int lttng_live_get_new_streams(struct lttng_live_ctx
*ctx
, uint64_t id
)
1687 struct lttng_viewer_cmd cmd
;
1688 struct lttng_viewer_new_streams_request rq
;
1689 struct lttng_viewer_new_streams_response rp
;
1690 struct lttng_viewer_stream stream
;
1691 int ret
, i
, nb_streams
= 0;
1693 uint32_t stream_count
;
1694 const size_t cmd_buf_len
= sizeof(cmd
) + sizeof(rq
);
1695 char cmd_buf
[cmd_buf_len
];
1697 if (lttng_live_should_quit()) {
1702 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEW_STREAMS
);
1703 cmd
.data_size
= htobe64((uint64_t) sizeof(rq
));
1704 cmd
.cmd_version
= htobe32(0);
1706 memset(&rq
, 0, sizeof(rq
));
1707 rq
.session_id
= htobe64(id
);
1710 * Merge the cmd and connection request to prevent a write-write
1711 * sequence on the TCP socket. Otherwise, a delayed ACK will prevent the
1712 * second write to be performed quickly in presence of Nagle's algorithm.
1714 memcpy(cmd_buf
, &cmd
, sizeof(cmd
));
1715 memcpy(cmd_buf
+ sizeof(cmd
), &rq
, sizeof(rq
));
1717 ret_len
= lttng_live_send(ctx
->control_sock
, cmd_buf
, cmd_buf_len
);
1719 perror("[error] Error sending get_new_streams cmd and request");
1722 assert(ret_len
== cmd_buf_len
);
1724 ret_len
= lttng_live_recv(ctx
->control_sock
, &rp
, sizeof(rp
));
1726 fprintf(stderr
, "[error] Remote side has closed connection\n");
1730 perror("[error] Error receiving get_new_streams response");
1733 assert(ret_len
== sizeof(rp
));
1735 switch(be32toh(rp
.status
)) {
1736 case LTTNG_VIEWER_NEW_STREAMS_OK
:
1738 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW
:
1741 case LTTNG_VIEWER_NEW_STREAMS_HUP
:
1742 ret
= -LTTNG_VIEWER_NEW_STREAMS_HUP
;
1744 case LTTNG_VIEWER_NEW_STREAMS_ERR
:
1745 fprintf(stderr
, "[error] get_new_streams error\n");
1748 fprintf(stderr
, "[error] Unknown return code %u\n",
1749 be32toh(rp
.status
));
1753 stream_count
= be32toh(rp
.streams_count
);
1754 ctx
->session
->stream_count
+= stream_count
;
1756 * When the session is created but not started, we do an active wait
1757 * until it starts. It allows the viewer to start processing the trace
1758 * as soon as the session starts.
1760 if (ctx
->session
->stream_count
== 0) {
1764 printf_verbose("Waiting for %d streams:\n", stream_count
);
1766 for (i
= 0; i
< stream_count
; i
++) {
1767 struct lttng_live_viewer_stream
*lvstream
;
1769 lvstream
= g_new0(struct lttng_live_viewer_stream
, 1);
1770 ret_len
= lttng_live_recv(ctx
->control_sock
, &stream
,
1773 fprintf(stderr
, "[error] Remote side has closed connection\n");
1778 perror("[error] Error receiving stream");
1782 assert(ret_len
== sizeof(stream
));
1783 stream
.path_name
[LTTNG_VIEWER_PATH_MAX
- 1] = '\0';
1784 stream
.channel_name
[LTTNG_VIEWER_NAME_MAX
- 1] = '\0';
1786 printf_verbose(" stream %" PRIu64
" : %s/%s\n",
1787 be64toh(stream
.id
), stream
.path_name
,
1788 stream
.channel_name
);
1789 lvstream
->id
= be64toh(stream
.id
);
1790 lvstream
->session
= ctx
->session
;
1792 lvstream
->mmap_size
= 0;
1793 lvstream
->ctf_stream_id
= -1ULL;
1795 if (be32toh(stream
.metadata_flag
)) {
1796 lvstream
->metadata_flag
= 1;
1798 ret
= lttng_live_ctf_trace_assign(lvstream
,
1799 be64toh(stream
.ctf_trace_id
));
1805 bt_list_add(&lvstream
->session_stream_node
,
1806 &ctx
->session
->stream_list
);
1816 int lttng_live_read(struct lttng_live_ctx
*ctx
)
1820 struct bt_ctf_iter
*iter
;
1821 const struct bt_ctf_event
*event
;
1822 struct bt_iter_pos begin_pos
;
1823 struct bt_trace_descriptor
*td_write
;
1824 struct bt_format
*fmt_write
;
1825 struct ctf_text_stream_pos
*sout
;
1828 ctx
->bt_ctx
= bt_context_create();
1830 fprintf(stderr
, "[error] bt_context_create allocation\n");
1834 fmt_write
= bt_lookup_format(g_quark_from_static_string("text"));
1836 fprintf(stderr
, "[error] ctf-text error\n");
1840 td_write
= fmt_write
->open_trace(NULL
, O_RDWR
, NULL
, NULL
);
1842 fprintf(stderr
, "[error] Error opening output trace\n");
1846 sout
= container_of(td_write
, struct ctf_text_stream_pos
,
1848 if (!sout
->parent
.event_cb
) {
1852 ret
= lttng_live_create_viewer_session(ctx
);
1857 for (i
= 0; i
< ctx
->session_ids
->len
; i
++) {
1858 id
= g_array_index(ctx
->session_ids
, uint64_t, i
);
1859 printf_verbose("Attaching to session %" PRIu64
"\n", id
);
1860 ret
= lttng_live_attach_session(ctx
, id
);
1861 printf_verbose("Attaching session returns %d\n", ret
);
1863 if (ret
== -LTTNG_VIEWER_ATTACH_UNK
) {
1864 fprintf(stderr
, "[error] Unknown session ID\n");
1871 * As long as the session is active, we try to get new streams.
1876 if (lttng_live_should_quit()) {
1881 while (!ctx
->session
->stream_count
) {
1882 if (lttng_live_should_quit()
1883 || ctx
->session_ids
->len
== 0) {
1887 ret
= ask_new_streams(ctx
);
1892 if (!ctx
->session
->stream_count
) {
1893 (void) poll(NULL
, 0, ACTIVE_POLL_DELAY
);
1897 ret
= add_traces(ctx
);
1902 begin_pos
.type
= BT_SEEK_BEGIN
;
1903 iter
= bt_ctf_iter_create(ctx
->bt_ctx
, &begin_pos
, NULL
);
1905 if (lttng_live_should_quit()) {
1909 fprintf(stderr
, "[error] Iterator creation error\n");
1913 if (lttng_live_should_quit()) {
1917 event
= bt_ctf_iter_read_event_flags(iter
, &flags
);
1918 if (!(flags
& BT_ITER_FLAG_RETRY
)) {
1923 ret
= sout
->parent
.event_cb(&sout
->parent
,
1924 event
->parent
->stream
);
1926 fprintf(stderr
, "[error] Writing "
1931 ret
= bt_iter_next(bt_ctf_get_iter(iter
));
1936 bt_ctf_iter_destroy(iter
);
1937 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
,
1938 del_traces
, ctx
->bt_ctx
);
1939 ctx
->session
->stream_count
= 0;
1943 g_hash_table_foreach_remove(ctx
->session
->ctf_traces
,
1944 del_traces
, ctx
->bt_ctx
);
1945 bt_context_put(ctx
->bt_ctx
);
1947 if (lttng_live_should_quit()) {