Fix: Add missing lttng_live_should_quit checks
[babeltrace.git] / formats / lttng-live / lttng-live-comm.c
CommitLineData
4a744367
JD
1/*
2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
24#include <sys/socket.h>
25#include <sys/types.h>
26#include <netinet/in.h>
27#include <netdb.h>
28#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <errno.h>
33#include <inttypes.h>
34#include <fcntl.h>
35#include <sys/mman.h>
85e2085e 36#include <poll.h>
4a744367
JD
37
38#include <babeltrace/ctf/ctf-index.h>
39
40#include <babeltrace/babeltrace.h>
41#include <babeltrace/ctf/events.h>
42#include <babeltrace/ctf/callbacks.h>
43#include <babeltrace/ctf/iterator.h>
44
45/* for packet_index */
46#include <babeltrace/ctf/types.h>
47
48#include <babeltrace/ctf/metadata.h>
49#include <babeltrace/ctf-text/types.h>
50#include <babeltrace/ctf/events-internal.h>
51#include <formats/ctf/events-private.h>
52
af701dc4
JD
53#include <babeltrace/compat/memstream.h>
54
c98627ca 55#include "lttng-live.h"
158eea6e 56#include "lttng-viewer-abi.h"
4a744367 57
85e2085e
MD
58#define ACTIVE_POLL_DELAY 100 /* ms */
59
4a744367
JD
60/*
61 * Memory allocation zeroed
62 */
63#define zmalloc(x) calloc(1, x)
64
65#ifndef max_t
66#define max_t(type, a, b) \
67 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
68#endif
69
b5a1fa45
JD
70static void ctf_live_packet_seek(struct bt_stream_pos *stream_pos,
71 size_t index, int whence);
2acdc547
JD
72static void add_traces(gpointer key, gpointer value, gpointer user_data);
73static int del_traces(gpointer key, gpointer value, gpointer user_data);
9abca178 74static int get_new_metadata(struct lttng_live_ctx *ctx,
af701dc4
JD
75 struct lttng_live_viewer_stream *viewer_stream,
76 char **metadata_buf);
2acdc547 77
b7e8a8e1
MD
78static
79ssize_t lttng_live_recv(int fd, void *buf, size_t len)
80{
81 ssize_t ret;
82 size_t copied = 0, to_copy = len;
83
84 do {
85 ret = recv(fd, buf + copied, to_copy, 0);
86 if (ret > 0) {
87 assert(ret <= to_copy);
88 copied += ret;
89 to_copy -= ret;
90 }
91 } while ((ret > 0 && to_copy > 0)
92 || (ret < 0 && errno == EINTR));
93 if (ret > 0)
94 ret = copied;
95 /* ret = 0 means orderly shutdown, ret < 0 is error. */
96 return ret;
97}
98
99static
100ssize_t lttng_live_send(int fd, const void *buf, size_t len)
101{
102 ssize_t ret;
103
104 do {
105 ret = send(fd, buf, len, MSG_NOSIGNAL);
106 } while (ret < 0 && errno == EINTR);
107 return ret;
108}
109
b5a1fa45 110int lttng_live_connect_viewer(struct lttng_live_ctx *ctx)
4a744367
JD
111{
112 struct hostent *host;
113 struct sockaddr_in server_addr;
114 int ret;
115
8ace20bf 116 if (lttng_live_should_quit()) {
4a744367
JD
117 ret = -1;
118 goto end;
119 }
120
8ace20bf
MD
121 host = gethostbyname(ctx->relay_hostname);
122 if (!host) {
123 fprintf(stderr, "[error] Cannot lookup hostname %s\n",
124 ctx->relay_hostname);
125 goto error;
126 }
127
4a744367
JD
128 if ((ctx->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
129 perror("Socket");
8ace20bf 130 goto error;
4a744367
JD
131 }
132
133 server_addr.sin_family = AF_INET;
b5a1fa45 134 server_addr.sin_port = htons(ctx->port);
4a744367
JD
135 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
136 bzero(&(server_addr.sin_zero), 8);
137
138 if (connect(ctx->control_sock, (struct sockaddr *) &server_addr,
139 sizeof(struct sockaddr)) == -1) {
140 perror("Connect");
8ace20bf 141 goto error;
4a744367
JD
142 }
143
144 ret = 0;
145
146end:
147 return ret;
8ace20bf
MD
148
149error:
150 fprintf(stderr, "[error] Connection failed\n");
151 return -1;
4a744367
JD
152}
153
154int lttng_live_establish_connection(struct lttng_live_ctx *ctx)
155{
156 struct lttng_viewer_cmd cmd;
157 struct lttng_viewer_connect connect;
158 int ret;
159 ssize_t ret_len;
160
8ace20bf
MD
161 if (lttng_live_should_quit()) {
162 ret = -1;
163 goto end;
164 }
165
4a744367
JD
166 cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
167 cmd.data_size = sizeof(connect);
168 cmd.cmd_version = 0;
169
0c3cd7e1 170 connect.viewer_session_id = -1ULL; /* will be set on recv */
4a744367
JD
171 connect.major = htobe32(LTTNG_LIVE_MAJOR);
172 connect.minor = htobe32(LTTNG_LIVE_MINOR);
173 connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
174
b7e8a8e1 175 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 176 if (ret_len < 0) {
79620497 177 perror("[error] Error sending cmd");
4a744367
JD
178 goto error;
179 }
180 assert(ret_len == sizeof(cmd));
181
b7e8a8e1 182 ret_len = lttng_live_send(ctx->control_sock, &connect, sizeof(connect));
4a744367 183 if (ret_len < 0) {
79620497 184 perror("[error] Error sending version");
4a744367
JD
185 goto error;
186 }
187 assert(ret_len == sizeof(connect));
188
b7e8a8e1 189 ret_len = lttng_live_recv(ctx->control_sock, &connect, sizeof(connect));
f8612f31
MD
190 if (ret_len == 0) {
191 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
192 goto error;
193 }
4a744367 194 if (ret_len < 0) {
79620497 195 perror("[error] Error receiving version");
4a744367
JD
196 goto error;
197 }
198 assert(ret_len == sizeof(connect));
199
200 printf_verbose("Received viewer session ID : %" PRIu64 "\n",
201 be64toh(connect.viewer_session_id));
202 printf_verbose("Relayd version : %u.%u\n", be32toh(connect.major),
203 be32toh(connect.minor));
4a744367 204 ret = 0;
8ace20bf
MD
205end:
206 return ret;
4a744367
JD
207
208error:
8ace20bf
MD
209 fprintf(stderr, "[error] Unable to establish connection\n");
210 return -1;
4a744367
JD
211}
212
b5a1fa45
JD
213static
214void free_session_list(GPtrArray *session_list)
215{
216 int i;
217 struct lttng_live_relay_session *relay_session;
218
219 for (i = 0; i < session_list->len; i++) {
220 relay_session = g_ptr_array_index(session_list, i);
221 free(relay_session->name);
222 free(relay_session->hostname);
223 }
224 g_ptr_array_free(session_list, TRUE);
225}
226
227static
228void print_session_list(GPtrArray *session_list, const char *path)
229{
230 int i;
231 struct lttng_live_relay_session *relay_session;
232
233 for (i = 0; i < session_list->len; i++) {
234 relay_session = g_ptr_array_index(session_list, i);
235 fprintf(stdout, "%s/host/%s/%s (timer = %u, "
236 "%u stream(s), %u client(s) connected)\n",
237 path, relay_session->hostname,
238 relay_session->name, relay_session->timer,
239 relay_session->streams, relay_session->clients);
240 }
241}
242
243static
244void update_session_list(GPtrArray *session_list, char *hostname,
245 char *session_name, uint32_t streams, uint32_t clients,
246 uint32_t timer)
247{
248 int i, found = 0;
249 struct lttng_live_relay_session *relay_session;
250
251 for (i = 0; i < session_list->len; i++) {
252 relay_session = g_ptr_array_index(session_list, i);
253 if ((strncmp(relay_session->hostname, hostname, NAME_MAX) == 0) &&
254 strncmp(relay_session->name,
255 session_name, NAME_MAX) == 0) {
256 relay_session->streams += streams;
257 if (relay_session->clients < clients)
258 relay_session->clients = clients;
259 found = 1;
260 break;
261 }
262 }
263 if (found)
264 return;
265
266 relay_session = g_new0(struct lttng_live_relay_session, 1);
267 relay_session->hostname = strndup(hostname, NAME_MAX);
268 relay_session->name = strndup(session_name, NAME_MAX);
269 relay_session->clients = clients;
270 relay_session->streams = streams;
271 relay_session->timer = timer;
272 g_ptr_array_add(session_list, relay_session);
273}
274
4a744367
JD
275int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path)
276{
277 struct lttng_viewer_cmd cmd;
278 struct lttng_viewer_list_sessions list;
279 struct lttng_viewer_session lsession;
b5a1fa45 280 int i, ret, sessions_count, print_list = 0;
4a744367 281 ssize_t ret_len;
b5a1fa45
JD
282 uint64_t session_id;
283 GPtrArray *session_list = NULL;
284
8ace20bf
MD
285 if (lttng_live_should_quit()) {
286 ret = -1;
287 goto end;
288 }
289
b5a1fa45
JD
290 if (strlen(ctx->session_name) == 0) {
291 print_list = 1;
292 session_list = g_ptr_array_new();
293 }
4a744367
JD
294
295 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
296 cmd.data_size = 0;
297 cmd.cmd_version = 0;
298
b7e8a8e1 299 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 300 if (ret_len < 0) {
79620497 301 perror("[error] Error sending cmd");
4a744367
JD
302 goto error;
303 }
304 assert(ret_len == sizeof(cmd));
305
b7e8a8e1 306 ret_len = lttng_live_recv(ctx->control_sock, &list, sizeof(list));
f8612f31
MD
307 if (ret_len == 0) {
308 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
309 goto error;
310 }
4a744367 311 if (ret_len < 0) {
79620497 312 perror("[error] Error receiving session list");
4a744367
JD
313 goto error;
314 }
315 assert(ret_len == sizeof(list));
316
317 sessions_count = be32toh(list.sessions_count);
4a744367 318 for (i = 0; i < sessions_count; i++) {
b7e8a8e1 319 ret_len = lttng_live_recv(ctx->control_sock, &lsession, sizeof(lsession));
f8612f31
MD
320 if (ret_len == 0) {
321 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
322 goto error;
323 }
4a744367 324 if (ret_len < 0) {
79620497 325 perror("[error] Error receiving session");
4a744367
JD
326 goto error;
327 }
328 assert(ret_len == sizeof(lsession));
9ed31e53
MD
329 lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0';
330 lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
b5a1fa45
JD
331 session_id = be64toh(lsession.id);
332
333 if (print_list) {
334 update_session_list(session_list,
335 lsession.hostname,
336 lsession.session_name,
337 be32toh(lsession.streams),
338 be32toh(lsession.clients),
339 be32toh(lsession.live_timer));
340 } else {
341 if ((strncmp(lsession.session_name, ctx->session_name,
342 NAME_MAX) == 0) && (strncmp(lsession.hostname,
343 ctx->traced_hostname, NAME_MAX) == 0)) {
344 printf_verbose("Reading from session %" PRIu64 "\n",
345 session_id);
346 g_array_append_val(ctx->session_ids,
347 session_id);
348 }
349 }
350 }
4a744367 351
b5a1fa45
JD
352 if (print_list) {
353 print_session_list(session_list, path);
354 free_session_list(session_list);
4a744367 355 }
4a744367 356 ret = 0;
8ace20bf
MD
357end:
358 return ret;
4a744367
JD
359
360error:
8ace20bf
MD
361 fprintf(stderr, "[error] Unable to list sessions\n");
362 return -1;
4a744367
JD
363}
364
365int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream *stream,
366 uint64_t ctf_trace_id)
367{
368 struct lttng_live_ctf_trace *trace;
369 int ret = 0;
370
371 trace = g_hash_table_lookup(stream->session->ctf_traces,
372 (gpointer) ctf_trace_id);
373 if (!trace) {
374 trace = g_new0(struct lttng_live_ctf_trace, 1);
4a744367
JD
375 trace->ctf_trace_id = ctf_trace_id;
376 trace->streams = g_ptr_array_new();
377 g_hash_table_insert(stream->session->ctf_traces,
378 (gpointer) ctf_trace_id,
379 trace);
380 }
381 if (stream->metadata_flag)
382 trace->metadata_stream = stream;
383
384 stream->ctf_trace = trace;
385 g_ptr_array_add(trace->streams, stream);
386
4a744367
JD
387 return ret;
388}
389
af701dc4
JD
390static
391int open_metadata_fp_write(struct lttng_live_viewer_stream *stream,
392 char **metadata_buf, size_t *size)
393{
394 int ret = 0;
395
396 stream->metadata_fp_write =
397 babeltrace_open_memstream(metadata_buf, size);
398 if (!stream->metadata_fp_write) {
399 perror("Metadata open_memstream");
400 ret = -1;
401 }
402
403 return ret;
404}
405
4a744367
JD
406int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id)
407{
408 struct lttng_viewer_cmd cmd;
409 struct lttng_viewer_attach_session_request rq;
410 struct lttng_viewer_attach_session_response rp;
411 struct lttng_viewer_stream stream;
412 int ret, i;
413 ssize_t ret_len;
414
8ace20bf
MD
415 if (lttng_live_should_quit()) {
416 ret = -1;
417 goto end;
418 }
419
4a744367
JD
420 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
421 cmd.data_size = sizeof(rq);
422 cmd.cmd_version = 0;
423
424 memset(&rq, 0, sizeof(rq));
425 rq.session_id = htobe64(id);
426 // TODO: add cmd line parameter to select seek beginning
427 // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
428 rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST);
429
b7e8a8e1 430 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 431 if (ret_len < 0) {
79620497 432 perror("[error] Error sending cmd");
4a744367
JD
433 goto error;
434 }
435 assert(ret_len == sizeof(cmd));
436
b7e8a8e1 437 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 438 if (ret_len < 0) {
79620497 439 perror("[error] Error sending attach request");
4a744367
JD
440 goto error;
441 }
442 assert(ret_len == sizeof(rq));
443
b7e8a8e1 444 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
445 if (ret_len == 0) {
446 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
447 goto error;
448 }
4a744367 449 if (ret_len < 0) {
79620497 450 perror("[error] Error receiving attach response");
4a744367
JD
451 goto error;
452 }
453 assert(ret_len == sizeof(rp));
454
455 switch(be32toh(rp.status)) {
456 case LTTNG_VIEWER_ATTACH_OK:
457 break;
458 case LTTNG_VIEWER_ATTACH_UNK:
459 ret = -LTTNG_VIEWER_ATTACH_UNK;
460 goto end;
461 case LTTNG_VIEWER_ATTACH_ALREADY:
79620497 462 fprintf(stderr, "[error] There is already a viewer attached to this session\n");
8ace20bf 463 goto error;
4a744367
JD
464 case LTTNG_VIEWER_ATTACH_NOT_LIVE:
465 fprintf(stderr, "[error] Not a live session\n");
8ace20bf 466 goto error;
4a744367
JD
467 case LTTNG_VIEWER_ATTACH_SEEK_ERR:
468 fprintf(stderr, "[error] Wrong seek parameter\n");
8ace20bf 469 goto error;
4a744367
JD
470 default:
471 fprintf(stderr, "[error] Unknown attach return code %u\n",
472 be32toh(rp.status));
8ace20bf 473 goto error;
4a744367
JD
474 }
475 if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {
8ace20bf 476 goto error;
4a744367
JD
477 }
478
2acdc547 479 ctx->session->stream_count += be32toh(rp.streams_count);
4a744367
JD
480 /*
481 * When the session is created but not started, we do an active wait
482 * until it starts. It allows the viewer to start processing the trace
483 * as soon as the session starts.
484 */
485 if (ctx->session->stream_count == 0) {
486 ret = 0;
487 goto end;
488 }
489 printf_verbose("Waiting for %" PRIu64 " streams:\n",
490 ctx->session->stream_count);
491 ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
492 ctx->session->stream_count);
4a744367 493 for (i = 0; i < be32toh(rp.streams_count); i++) {
b7e8a8e1 494 ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
f8612f31
MD
495 if (ret_len == 0) {
496 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
497 goto error;
498 }
4a744367 499 if (ret_len < 0) {
79620497 500 perror("[error] Error receiving stream");
4a744367
JD
501 goto error;
502 }
503 assert(ret_len == sizeof(stream));
9ed31e53
MD
504 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
505 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
4a744367
JD
506
507 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
508 be64toh(stream.id), stream.path_name,
509 stream.channel_name);
510 ctx->session->streams[i].id = be64toh(stream.id);
511 ctx->session->streams[i].session = ctx->session;
512
513 ctx->session->streams[i].first_read = 1;
514 ctx->session->streams[i].mmap_size = 0;
515
516 if (be32toh(stream.metadata_flag)) {
4a744367 517 ctx->session->streams[i].metadata_flag = 1;
4a744367
JD
518 }
519 ret = lttng_live_ctf_trace_assign(&ctx->session->streams[i],
520 be64toh(stream.ctf_trace_id));
521 if (ret < 0) {
522 goto error;
523 }
524
525 }
526 ret = 0;
4a744367 527end:
4a744367 528 return ret;
8ace20bf
MD
529
530error:
531 return -1;
4a744367
JD
532}
533
2acdc547
JD
534static
535int ask_new_streams(struct lttng_live_ctx *ctx)
536{
537 int i, ret = 0;
538 uint64_t id;
539
540restart:
541 for (i = 0; i < ctx->session_ids->len; i++) {
542 id = g_array_index(ctx->session_ids, uint64_t, i);
543 ret = lttng_live_get_new_streams(ctx, id);
544 printf_verbose("Asking for new streams returns %d\n",
545 ret);
546 if (ret < 0) {
e29395a8
JG
547 if (lttng_live_should_quit()) {
548 goto end;
549 }
2acdc547
JD
550 if (ret == -LTTNG_VIEWER_NEW_STREAMS_HUP) {
551 printf_verbose("Session %" PRIu64 " closed\n",
552 id);
b5a1fa45
JD
553 /*
554 * The streams have already been closed during
555 * the reading, so we only need to get rid of
556 * the trace in our internal table of sessions.
557 */
2acdc547
JD
558 g_array_remove_index(ctx->session_ids, i);
559 /*
560 * We can't continue iterating on the g_array
561 * after a remove, we have to start again.
562 */
563 goto restart;
564 } else {
565 ret = -1;
566 goto end;
567 }
568 }
569 }
570
571end:
572 return ret;
573}
574
50d207af
JD
575static
576int append_metadata(struct lttng_live_ctx *ctx,
577 struct lttng_live_viewer_stream *viewer_stream)
578{
579 int ret;
580 struct lttng_live_viewer_stream *metadata;
581 char *metadata_buf = NULL;
582
583 printf_verbose("get_next_index: new metadata needed\n");
584 ret = get_new_metadata(ctx, viewer_stream, &metadata_buf);
585 if (ret < 0) {
586 free(metadata_buf);
587 goto error;
588 }
589
590 metadata = viewer_stream->ctf_trace->metadata_stream;
591 metadata->ctf_trace->metadata_fp =
592 babeltrace_fmemopen(metadata_buf,
593 metadata->metadata_len, "rb");
594 if (!metadata->ctf_trace->metadata_fp) {
595 perror("Metadata fmemopen");
596 free(metadata_buf);
597 ret = -1;
598 goto error;
599 }
600 ret = ctf_append_trace_metadata(
601 viewer_stream->ctf_trace->handle->td,
602 metadata->ctf_trace->metadata_fp);
251fc08c
MD
603 /* We accept empty metadata packets */
604 if (ret != 0 && ret != -ENOENT) {
50d207af
JD
605 fprintf(stderr, "[error] Appending metadata\n");
606 goto error;
607 }
251fc08c 608 ret = 0;
50d207af
JD
609
610error:
611 return ret;
612}
613
4a744367
JD
614static
615int get_data_packet(struct lttng_live_ctx *ctx,
616 struct ctf_stream_pos *pos,
617 struct lttng_live_viewer_stream *stream, uint64_t offset,
618 uint64_t len)
619{
620 struct lttng_viewer_cmd cmd;
621 struct lttng_viewer_get_packet rq;
622 struct lttng_viewer_trace_packet rp;
623 ssize_t ret_len;
624 int ret;
625
50d207af 626retry:
8ace20bf
MD
627 if (lttng_live_should_quit()) {
628 ret = -1;
629 goto end;
630 }
4a744367
JD
631 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
632 cmd.data_size = sizeof(rq);
633 cmd.cmd_version = 0;
634
635 memset(&rq, 0, sizeof(rq));
636 rq.stream_id = htobe64(stream->id);
c1e8ac81 637 rq.offset = htobe64(offset);
4a744367
JD
638 rq.len = htobe32(len);
639
b7e8a8e1 640 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 641 if (ret_len < 0) {
79620497 642 perror("[error] Error sending cmd");
4a744367
JD
643 goto error;
644 }
645 assert(ret_len == sizeof(cmd));
646
b7e8a8e1 647 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 648 if (ret_len < 0) {
79620497 649 perror("[error] Error sending get_data_packet request");
4a744367
JD
650 goto error;
651 }
652 assert(ret_len == sizeof(rq));
653
b7e8a8e1 654 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
655 if (ret_len == 0) {
656 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
657 goto error;
658 }
4a744367 659 if (ret_len < 0) {
79620497 660 perror("[error] Error receiving data response");
4a744367
JD
661 goto error;
662 }
57b9d843
JD
663 if (ret_len != sizeof(rp)) {
664 fprintf(stderr, "[error] get_data_packet: expected %" PRId64
a5e4b11e
MD
665 ", received %" PRId64 "\n", sizeof(rp),
666 ret_len);
57b9d843
JD
667 goto error;
668 }
4a744367
JD
669
670 rp.flags = be32toh(rp.flags);
671
672 switch (be32toh(rp.status)) {
673 case LTTNG_VIEWER_GET_PACKET_OK:
674 len = be32toh(rp.len);
675 printf_verbose("get_data_packet: Ok, packet size : %" PRIu64
676 "\n", len);
677 break;
678 case LTTNG_VIEWER_GET_PACKET_RETRY:
8ace20bf 679 /* Unimplemented by relay daemon */
4a744367 680 printf_verbose("get_data_packet: retry\n");
8ace20bf 681 goto error;
4a744367
JD
682 case LTTNG_VIEWER_GET_PACKET_ERR:
683 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
684 printf_verbose("get_data_packet: new metadata needed\n");
50d207af
JD
685 ret = append_metadata(ctx, stream);
686 if (ret)
687 goto error;
4a744367 688 }
2acdc547 689 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
f312b3aa 690 printf_verbose("get_data_packet: new streams needed\n");
2acdc547
JD
691 ret = ask_new_streams(ctx);
692 if (ret < 0)
693 goto error;
4d4bf6eb
MD
694 g_hash_table_foreach(ctx->session->ctf_traces,
695 add_traces, ctx->bt_ctx);
2acdc547 696 }
f312b3aa
MD
697 if (rp.flags & (LTTNG_VIEWER_FLAG_NEW_METADATA
698 | LTTNG_VIEWER_FLAG_NEW_STREAM)) {
699 goto retry;
700 }
4a744367 701 fprintf(stderr, "[error] get_data_packet: error\n");
8ace20bf 702 goto error;
4a744367
JD
703 case LTTNG_VIEWER_GET_PACKET_EOF:
704 ret = -2;
8ace20bf 705 goto end;
4a744367
JD
706 default:
707 printf_verbose("get_data_packet: unknown\n");
8ace20bf 708 goto error;
4a744367
JD
709 }
710
65ae9a82 711 if (len == 0) {
96943040 712 goto error;
4a744367
JD
713 }
714
715 if (len > stream->mmap_size) {
716 uint64_t new_size;
717
718 new_size = max_t(uint64_t, len, stream->mmap_size << 1);
719 if (pos->base_mma) {
720 /* unmap old base */
721 ret = munmap_align(pos->base_mma);
722 if (ret) {
79620497 723 perror("[error] Unable to unmap old base");
4a744367
JD
724 goto error;
725 }
726 pos->base_mma = NULL;
727 }
728 pos->base_mma = mmap_align(new_size,
729 PROT_READ | PROT_WRITE,
730 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
731 if (pos->base_mma == MAP_FAILED) {
79620497 732 perror("[error] mmap error");
4a744367 733 pos->base_mma = NULL;
4a744367
JD
734 goto error;
735 }
736
737 stream->mmap_size = new_size;
738 printf_verbose("Expanding stream mmap size to %" PRIu64 " bytes\n",
739 stream->mmap_size);
740 }
741
b7e8a8e1
MD
742 ret_len = lttng_live_recv(ctx->control_sock,
743 mmap_align_addr(pos->base_mma), len);
f8612f31
MD
744 if (ret_len == 0) {
745 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
746 goto error;
747 }
4a744367 748 if (ret_len < 0) {
79620497 749 perror("[error] Error receiving trace packet");
4a744367
JD
750 goto error;
751 }
752 assert(ret_len == len);
f312b3aa 753 ret = 0;
4a744367 754end:
4a744367 755 return ret;
8ace20bf
MD
756
757error:
758 return -1;
4a744367
JD
759}
760
4a744367 761static
9abca178
JD
762int get_one_metadata_packet(struct lttng_live_ctx *ctx,
763 struct lttng_live_viewer_stream *metadata_stream)
4a744367
JD
764{
765 uint64_t len = 0;
766 int ret;
767 struct lttng_viewer_cmd cmd;
768 struct lttng_viewer_get_metadata rq;
769 struct lttng_viewer_metadata_packet rp;
4a744367
JD
770 char *data = NULL;
771 ssize_t ret_len;
772
8ace20bf
MD
773 if (lttng_live_should_quit()) {
774 ret = -1;
775 goto end;
776 }
777
9abca178 778 rq.stream_id = htobe64(metadata_stream->id);
4a744367
JD
779 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
780 cmd.data_size = sizeof(rq);
781 cmd.cmd_version = 0;
782
b7e8a8e1 783 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 784 if (ret_len < 0) {
79620497 785 perror("[error] Error sending cmd");
4a744367
JD
786 goto error;
787 }
788 assert(ret_len == sizeof(cmd));
789
b7e8a8e1 790 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 791 if (ret_len < 0) {
79620497 792 perror("[error] Error sending get_metadata request");
4a744367
JD
793 goto error;
794 }
795 assert(ret_len == sizeof(rq));
796
b7e8a8e1 797 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
798 if (ret_len == 0) {
799 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
800 goto error;
801 }
4a744367 802 if (ret_len < 0) {
79620497 803 perror("[error] Error receiving metadata response");
4a744367
JD
804 goto error;
805 }
806 assert(ret_len == sizeof(rp));
807
808 switch (be32toh(rp.status)) {
809 case LTTNG_VIEWER_METADATA_OK:
810 printf_verbose("get_metadata : OK\n");
811 break;
812 case LTTNG_VIEWER_NO_NEW_METADATA:
813 printf_verbose("get_metadata : NO NEW\n");
9abca178 814 ret = 0;
4a744367
JD
815 goto end;
816 case LTTNG_VIEWER_METADATA_ERR:
817 printf_verbose("get_metadata : ERR\n");
8ace20bf 818 goto error;
4a744367
JD
819 default:
820 printf_verbose("get_metadata : UNKNOWN\n");
8ace20bf 821 goto error;
4a744367
JD
822 }
823
824 len = be64toh(rp.len);
825 printf_verbose("Writing %" PRIu64" bytes to metadata\n", len);
826 if (len <= 0) {
8ace20bf 827 goto error;
4a744367
JD
828 }
829
830 data = zmalloc(len);
831 if (!data) {
832 perror("relay data zmalloc");
4a744367
JD
833 goto error;
834 }
b7e8a8e1 835 ret_len = lttng_live_recv(ctx->control_sock, data, len);
f8612f31
MD
836 if (ret_len == 0) {
837 fprintf(stderr, "[error] Remote side has closed connection\n");
8ace20bf 838 goto error_free_data;
f8612f31 839 }
4a744367 840 if (ret_len < 0) {
79620497 841 perror("[error] Error receiving trace packet");
8ace20bf 842 goto error_free_data;
4a744367
JD
843 }
844 assert(ret_len == len);
845
846 do {
af701dc4
JD
847 ret_len = fwrite(data, 1, len,
848 metadata_stream->metadata_fp_write);
4a744367
JD
849 } while (ret_len < 0 && errno == EINTR);
850 if (ret_len < 0) {
af701dc4 851 fprintf(stderr, "[error] Writing in the metadata fp\n");
8ace20bf 852 goto error_free_data;
4a744367
JD
853 }
854 assert(ret_len == len);
af701dc4 855 metadata_stream->metadata_len += len;
8ace20bf 856 free(data);
9abca178 857 ret = len;
8ace20bf
MD
858end:
859 return ret;
4a744367 860
8ace20bf 861error_free_data:
4a744367 862 free(data);
4a744367 863error:
8ace20bf 864 return -1;
4a744367
JD
865}
866
9abca178
JD
867/*
868 * Return 0 on success, a negative value on error.
869 */
870static
871int get_new_metadata(struct lttng_live_ctx *ctx,
af701dc4
JD
872 struct lttng_live_viewer_stream *viewer_stream,
873 char **metadata_buf)
9abca178
JD
874{
875 int ret = 0;
876 struct lttng_live_viewer_stream *metadata_stream;
c111d5d7 877 size_t size, len_read = 0;;
9abca178
JD
878
879 metadata_stream = viewer_stream->ctf_trace->metadata_stream;
50d207af
JD
880 if (!metadata_stream) {
881 fprintf(stderr, "[error] No metadata stream\n");
882 ret = -1;
883 goto error;
884 }
af701dc4
JD
885 metadata_stream->metadata_len = 0;
886 ret = open_metadata_fp_write(metadata_stream, metadata_buf, &size);
887 if (ret < 0) {
888 goto error;
889 }
9abca178
JD
890
891 do {
892 /*
893 * get_one_metadata_packet returns the number of bytes
894 * received, 0 when we have received everything, a
895 * negative value on error.
896 */
897 ret = get_one_metadata_packet(ctx, metadata_stream);
c111d5d7
MD
898 if (ret > 0) {
899 len_read += ret;
900 }
85e2085e
MD
901 if (!len_read) {
902 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
903 }
c111d5d7 904 } while (ret > 0 || !len_read);
9abca178 905
7def295f
MD
906 if (fclose(metadata_stream->metadata_fp_write))
907 perror("fclose");
af701dc4
JD
908 metadata_stream->metadata_fp_write = NULL;
909
910error:
9abca178
JD
911 return ret;
912}
913
4a744367
JD
914/*
915 * Get one index for a stream.
916 *
917 * Returns 0 on success or a negative value on error.
918 */
919static
920int get_next_index(struct lttng_live_ctx *ctx,
921 struct lttng_live_viewer_stream *viewer_stream,
922 struct packet_index *index)
923{
924 struct lttng_viewer_cmd cmd;
925 struct lttng_viewer_get_next_index rq;
926 struct lttng_viewer_index rp;
927 int ret;
4a744367
JD
928 ssize_t ret_len;
929
930 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
931 cmd.data_size = sizeof(rq);
932 cmd.cmd_version = 0;
933
934 memset(&rq, 0, sizeof(rq));
935 rq.stream_id = htobe64(viewer_stream->id);
936
937retry:
8ace20bf
MD
938 if (lttng_live_should_quit()) {
939 ret = -1;
940 goto end;
941 }
b7e8a8e1 942 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
4a744367 943 if (ret_len < 0) {
79620497 944 perror("[error] Error sending cmd");
4a744367
JD
945 goto error;
946 }
947 assert(ret_len == sizeof(cmd));
948
b7e8a8e1 949 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
4a744367 950 if (ret_len < 0) {
79620497 951 perror("[error] Error sending get_next_index request");
4a744367
JD
952 goto error;
953 }
954 assert(ret_len == sizeof(rq));
955
b7e8a8e1 956 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
957 if (ret_len == 0) {
958 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
959 goto error;
960 }
4a744367 961 if (ret_len < 0) {
79620497 962 perror("[error] Error receiving index response");
4a744367
JD
963 goto error;
964 }
965 assert(ret_len == sizeof(rp));
966
967 rp.flags = be32toh(rp.flags);
968
969 switch (be32toh(rp.status)) {
970 case LTTNG_VIEWER_INDEX_INACTIVE:
971 printf_verbose("get_next_index: inactive\n");
972 memset(index, 0, sizeof(struct packet_index));
992e8cc0 973 index->ts_cycles.timestamp_end = be64toh(rp.timestamp_end);
4a744367
JD
974 break;
975 case LTTNG_VIEWER_INDEX_OK:
976 printf_verbose("get_next_index: Ok, need metadata update : %u\n",
977 rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA);
978 index->offset = be64toh(rp.offset);
979 index->packet_size = be64toh(rp.packet_size);
980 index->content_size = be64toh(rp.content_size);
992e8cc0
MD
981 index->ts_cycles.timestamp_begin = be64toh(rp.timestamp_begin);
982 index->ts_cycles.timestamp_end = be64toh(rp.timestamp_end);
4a744367
JD
983 index->events_discarded = be64toh(rp.events_discarded);
984
985 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
50d207af
JD
986 ret = append_metadata(ctx, viewer_stream);
987 if (ret)
4a744367 988 goto error;
4a744367 989 }
2acdc547
JD
990 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_STREAM) {
991 ret = ask_new_streams(ctx);
992 if (ret < 0)
993 goto error;
4d4bf6eb
MD
994 g_hash_table_foreach(ctx->session->ctf_traces,
995 add_traces, ctx->bt_ctx);
2acdc547 996 }
4a744367
JD
997 break;
998 case LTTNG_VIEWER_INDEX_RETRY:
999 printf_verbose("get_next_index: retry\n");
85e2085e 1000 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
4a744367
JD
1001 goto retry;
1002 case LTTNG_VIEWER_INDEX_HUP:
1003 printf_verbose("get_next_index: stream hung up\n");
1004 viewer_stream->id = -1ULL;
4a744367 1005 index->offset = EOF;
2acdc547 1006 ctx->session->stream_count--;
4a744367
JD
1007 break;
1008 case LTTNG_VIEWER_INDEX_ERR:
1009 fprintf(stderr, "[error] get_next_index: error\n");
4a744367
JD
1010 goto error;
1011 default:
1012 fprintf(stderr, "[error] get_next_index: unkwown value\n");
4a744367
JD
1013 goto error;
1014 }
4a744367 1015 ret = 0;
8ace20bf
MD
1016end:
1017 return ret;
4a744367
JD
1018
1019error:
8ace20bf 1020 return -1;
4a744367
JD
1021}
1022
b5a1fa45 1023static
4a744367
JD
1024void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index,
1025 int whence)
1026{
1027 struct ctf_stream_pos *pos;
1028 struct ctf_file_stream *file_stream;
f1f52630 1029 struct packet_index *prev_index = NULL, *cur_index;
4a744367
JD
1030 struct lttng_live_viewer_stream *viewer_stream;
1031 struct lttng_live_session *session;
1032 int ret;
1033
1034retry:
1035 pos = ctf_pos(stream_pos);
1036 file_stream = container_of(pos, struct ctf_file_stream, pos);
1037 viewer_stream = (struct lttng_live_viewer_stream *) pos->priv;
1038 session = viewer_stream->session;
1039
f1f52630
MD
1040 switch (pos->packet_index->len) {
1041 case 0:
1042 g_array_set_size(pos->packet_index, 1);
1043 cur_index = &g_array_index(pos->packet_index,
1044 struct packet_index, 0);
1045 break;
1046 case 1:
1047 g_array_set_size(pos->packet_index, 2);
1048 prev_index = &g_array_index(pos->packet_index,
1049 struct packet_index, 0);
1050 cur_index = &g_array_index(pos->packet_index,
1051 struct packet_index, 1);
1052 break;
1053 case 2:
1054 g_array_index(pos->packet_index,
1055 struct packet_index, 0) =
1056 g_array_index(pos->packet_index,
1057 struct packet_index, 1);
1058 prev_index = &g_array_index(pos->packet_index,
1059 struct packet_index, 0);
1060 cur_index = &g_array_index(pos->packet_index,
1061 struct packet_index, 1);
1062 break;
1063 default:
1064 abort();
1065 break;
1066 }
4a744367 1067 printf_verbose("get_next_index for stream %" PRIu64 "\n", viewer_stream->id);
f1f52630 1068 ret = get_next_index(session->ctx, viewer_stream, cur_index);
4a744367
JD
1069 if (ret < 0) {
1070 pos->offset = EOF;
e29395a8
JG
1071 if (!lttng_live_should_quit()) {
1072 fprintf(stderr, "[error] get_next_index failed\n");
1073 }
4a744367
JD
1074 return;
1075 }
1076
f1f52630
MD
1077 pos->packet_size = cur_index->packet_size;
1078 pos->content_size = cur_index->content_size;
4a744367 1079 pos->mmap_base_offset = 0;
f1f52630 1080 if (cur_index->offset == EOF) {
4a744367
JD
1081 pos->offset = EOF;
1082 } else {
1083 pos->offset = 0;
1084 }
1085
f1f52630
MD
1086 if (cur_index->content_size == 0) {
1087 file_stream->parent.cycles_timestamp =
1088 cur_index->ts_cycles.timestamp_end;
4a744367 1089 file_stream->parent.real_timestamp = ctf_get_real_timestamp(
f1f52630
MD
1090 &file_stream->parent,
1091 cur_index->ts_cycles.timestamp_end);
4a744367 1092 } else {
f1f52630
MD
1093 /* Convert the timestamps and append to the real_index. */
1094 cur_index->ts_real.timestamp_begin = ctf_get_real_timestamp(
1095 &file_stream->parent,
1096 cur_index->ts_cycles.timestamp_begin);
1097 cur_index->ts_real.timestamp_end = ctf_get_real_timestamp(
1098 &file_stream->parent,
1099 cur_index->ts_cycles.timestamp_end);
1100
1101 ctf_update_current_packet_index(&file_stream->parent,
1102 prev_index, cur_index);
1103
1104 file_stream->parent.cycles_timestamp =
1105 cur_index->ts_cycles.timestamp_begin;
1106 file_stream->parent.real_timestamp =
1107 cur_index->ts_real.timestamp_begin;
4a744367
JD
1108 }
1109
1110 if (pos->packet_size == 0 || pos->offset == EOF) {
1111 goto end;
1112 }
1113
1114 printf_verbose("get_data_packet for stream %" PRIu64 "\n",
1115 viewer_stream->id);
1116 ret = get_data_packet(session->ctx, pos, viewer_stream,
c1e8ac81 1117 cur_index->offset,
f1f52630 1118 cur_index->packet_size / CHAR_BIT);
4a744367
JD
1119 if (ret == -2) {
1120 goto retry;
1121 } else if (ret < 0) {
1122 pos->offset = EOF;
e29395a8
JG
1123 if (!lttng_live_should_quit()) {
1124 fprintf(stderr, "[error] get_data_packet failed\n");
1125 }
4a744367
JD
1126 return;
1127 }
1128
1129 printf_verbose("Index received : packet_size : %" PRIu64
1130 ", offset %" PRIu64 ", content_size %" PRIu64
1131 ", timestamp_end : %" PRIu64 "\n",
f1f52630
MD
1132 cur_index->packet_size, cur_index->offset,
1133 cur_index->content_size,
1134 cur_index->ts_cycles.timestamp_end);
4a744367
JD
1135
1136 /* update trace_packet_header and stream_packet_context */
1137 if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) {
1138 /* Read packet header */
1139 ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p);
1140 if (ret) {
1141 pos->offset = EOF;
1142 fprintf(stderr, "[error] trace packet header read failed\n");
1143 goto end;
1144 }
1145 }
1146 if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) {
1147 /* Read packet context */
1148 ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p);
1149 if (ret) {
1150 pos->offset = EOF;
1151 fprintf(stderr, "[error] stream packet context read failed\n");
1152 goto end;
1153 }
1154 }
1155 pos->data_offset = pos->offset;
1156
1157end:
1158 return;
1159}
1160
2acdc547
JD
1161int lttng_live_create_viewer_session(struct lttng_live_ctx *ctx)
1162{
1163 struct lttng_viewer_cmd cmd;
1164 struct lttng_viewer_create_session_response resp;
1165 int ret;
1166 ssize_t ret_len;
1167
8ace20bf
MD
1168 if (lttng_live_should_quit()) {
1169 ret = -1;
1170 goto end;
1171 }
1172
2acdc547
JD
1173 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
1174 cmd.data_size = 0;
1175 cmd.cmd_version = 0;
1176
b7e8a8e1 1177 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
2acdc547 1178 if (ret_len < 0) {
79620497 1179 perror("[error] Error sending cmd");
2acdc547
JD
1180 goto error;
1181 }
1182 assert(ret_len == sizeof(cmd));
1183
b7e8a8e1 1184 ret_len = lttng_live_recv(ctx->control_sock, &resp, sizeof(resp));
f8612f31
MD
1185 if (ret_len == 0) {
1186 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
1187 goto error;
1188 }
2acdc547 1189 if (ret_len < 0) {
79620497 1190 perror("[error] Error receiving create session reply");
2acdc547
JD
1191 goto error;
1192 }
1193 assert(ret_len == sizeof(resp));
1194
1195 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
1196 fprintf(stderr, "[error] Error creating viewer session\n");
2acdc547
JD
1197 goto error;
1198 }
1199 ret = 0;
8ace20bf
MD
1200end:
1201 return ret;
2acdc547
JD
1202
1203error:
8ace20bf 1204 return -1;
2acdc547
JD
1205}
1206
1207static
1208int del_traces(gpointer key, gpointer value, gpointer user_data)
4a744367
JD
1209{
1210 struct bt_context *bt_ctx = user_data;
1211 struct lttng_live_ctf_trace *trace = value;
1212 int ret;
1213
1214 ret = bt_context_remove_trace(bt_ctx, trace->trace_id);
1215 if (ret < 0)
1216 fprintf(stderr, "[error] removing trace from context\n");
1217
1218 /* remove the key/value pair from the HT. */
1219 return 1;
1220}
1221
2acdc547
JD
1222static
1223void add_traces(gpointer key, gpointer value, gpointer user_data)
4a744367 1224{
9abca178 1225 int i, ret;
4a744367
JD
1226 struct bt_context *bt_ctx = user_data;
1227 struct lttng_live_ctf_trace *trace = value;
1228 struct lttng_live_viewer_stream *stream;
1229 struct bt_mmap_stream *new_mmap_stream;
1230 struct bt_mmap_stream_list mmap_list;
1231 struct lttng_live_ctx *ctx = NULL;
150595c4
JD
1232 struct bt_trace_descriptor *td;
1233 struct bt_trace_handle *handle;
4a744367 1234
b5a1fa45
JD
1235 /*
1236 * We don't know how many streams we will receive for a trace, so
1237 * once we are done receiving the traces, we add all the traces
1238 * received to the bt_context.
1239 * We can receive streams during the attach command or the
1240 * get_new_streams, so we have to make sure not to add multiple
1241 * times the same traces.
1242 * If a trace is already in the context, we just skip this function.
1243 */
2acdc547
JD
1244 if (trace->in_use)
1245 return;
1246
4a744367
JD
1247 BT_INIT_LIST_HEAD(&mmap_list.head);
1248
1249 for (i = 0; i < trace->streams->len; i++) {
1250 stream = g_ptr_array_index(trace->streams, i);
1251 ctx = stream->session->ctx;
1252
1253 if (!stream->metadata_flag) {
1254 new_mmap_stream = zmalloc(sizeof(struct bt_mmap_stream));
1255 new_mmap_stream->priv = (void *) stream;
1256 new_mmap_stream->fd = -1;
1257 bt_list_add(&new_mmap_stream->list, &mmap_list.head);
1258 } else {
af701dc4
JD
1259 char *metadata_buf = NULL;
1260
4a744367 1261 /* Get all possible metadata before starting */
af701dc4
JD
1262 ret = get_new_metadata(ctx, stream, &metadata_buf);
1263 if (ret) {
1264 free(metadata_buf);
1265 goto end_free;
1266 }
1267 if (!stream->metadata_len) {
1268 fprintf(stderr, "[error] empty metadata\n");
1269 ret = -1;
1270 free(metadata_buf);
1271 goto end_free;
1272 }
1273
1274 trace->metadata_fp = babeltrace_fmemopen(metadata_buf,
1275 stream->metadata_len, "rb");
1276 if (!trace->metadata_fp) {
1277 perror("Metadata fmemopen");
1278 ret = -1;
1279 free(metadata_buf);
9abca178 1280 goto end_free;
af701dc4 1281 }
4a744367
JD
1282 }
1283 }
1284
1285 if (!trace->metadata_fp) {
1286 fprintf(stderr, "[error] No metadata stream opened\n");
1287 goto end_free;
1288 }
1289
1290 ret = bt_context_add_trace(bt_ctx, NULL, "ctf",
1291 ctf_live_packet_seek, &mmap_list, trace->metadata_fp);
1292 if (ret < 0) {
1293 fprintf(stderr, "[error] Error adding trace\n");
1294 goto end_free;
1295 }
af701dc4
JD
1296 trace->metadata_stream->metadata_len = 0;
1297
150595c4
JD
1298 handle = (struct bt_trace_handle *) g_hash_table_lookup(
1299 bt_ctx->trace_handles,
1300 (gpointer) (unsigned long) ret);
1301 td = handle->td;
1302 trace->handle = handle;
2acdc547 1303 if (bt_ctx->current_iterator) {
2acdc547
JD
1304 bt_iter_add_trace(bt_ctx->current_iterator, td);
1305 }
1306
4a744367 1307 trace->trace_id = ret;
2acdc547 1308 trace->in_use = 1;
4a744367
JD
1309
1310 goto end;
1311
1312end_free:
1313 bt_context_put(bt_ctx);
1314end:
1315 return;
1316}
1317
2acdc547 1318int lttng_live_get_new_streams(struct lttng_live_ctx *ctx, uint64_t id)
6192b988
JD
1319{
1320 struct lttng_viewer_cmd cmd;
2acdc547
JD
1321 struct lttng_viewer_new_streams_request rq;
1322 struct lttng_viewer_new_streams_response rp;
1323 struct lttng_viewer_stream stream;
1324 int ret, i;
6192b988 1325 ssize_t ret_len;
b5a1fa45 1326 uint32_t stream_count;
6192b988 1327
8ace20bf
MD
1328 if (lttng_live_should_quit()) {
1329 ret = -1;
1330 goto end;
1331 }
1332
2acdc547
JD
1333 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS);
1334 cmd.data_size = sizeof(rq);
6192b988
JD
1335 cmd.cmd_version = 0;
1336
2acdc547
JD
1337 memset(&rq, 0, sizeof(rq));
1338 rq.session_id = htobe64(id);
1339
b7e8a8e1 1340 ret_len = lttng_live_send(ctx->control_sock, &cmd, sizeof(cmd));
6192b988 1341 if (ret_len < 0) {
79620497 1342 perror("[error] Error sending cmd");
6192b988
JD
1343 goto error;
1344 }
1345 assert(ret_len == sizeof(cmd));
1346
b7e8a8e1 1347 ret_len = lttng_live_send(ctx->control_sock, &rq, sizeof(rq));
6192b988 1348 if (ret_len < 0) {
79620497 1349 perror("[error] Error sending get_new_streams request");
6192b988
JD
1350 goto error;
1351 }
2acdc547 1352 assert(ret_len == sizeof(rq));
6192b988 1353
b7e8a8e1 1354 ret_len = lttng_live_recv(ctx->control_sock, &rp, sizeof(rp));
f8612f31
MD
1355 if (ret_len == 0) {
1356 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
1357 goto error;
1358 }
2acdc547 1359 if (ret_len < 0) {
79620497 1360 perror("[error] Error receiving get_new_streams response");
6192b988
JD
1361 goto error;
1362 }
2acdc547
JD
1363 assert(ret_len == sizeof(rp));
1364
1365 switch(be32toh(rp.status)) {
1366 case LTTNG_VIEWER_NEW_STREAMS_OK:
1367 break;
1368 case LTTNG_VIEWER_NEW_STREAMS_NO_NEW:
1369 ret = 0;
1370 goto end;
1371 case LTTNG_VIEWER_NEW_STREAMS_HUP:
1372 ret = -LTTNG_VIEWER_NEW_STREAMS_HUP;
1373 goto end;
1374 case LTTNG_VIEWER_NEW_STREAMS_ERR:
1375 fprintf(stderr, "[error] get_new_streams error\n");
8ace20bf 1376 goto error;
2acdc547
JD
1377 default:
1378 fprintf(stderr, "[error] Unknown return code %u\n",
1379 be32toh(rp.status));
8ace20bf 1380 goto error;
2acdc547
JD
1381 }
1382
b5a1fa45
JD
1383 stream_count = be32toh(rp.streams_count);
1384 ctx->session->stream_count += stream_count;
2acdc547
JD
1385 /*
1386 * When the session is created but not started, we do an active wait
1387 * until it starts. It allows the viewer to start processing the trace
1388 * as soon as the session starts.
1389 */
1390 if (ctx->session->stream_count == 0) {
1391 ret = 0;
1392 goto end;
1393 }
1394 printf_verbose("Waiting for %" PRIu64 " streams:\n",
1395 ctx->session->stream_count);
1396 ctx->session->streams = g_new0(struct lttng_live_viewer_stream,
1397 ctx->session->stream_count);
b5a1fa45 1398 for (i = 0; i < stream_count; i++) {
b7e8a8e1 1399 ret_len = lttng_live_recv(ctx->control_sock, &stream, sizeof(stream));
f8612f31
MD
1400 if (ret_len == 0) {
1401 fprintf(stderr, "[error] Remote side has closed connection\n");
f8612f31
MD
1402 goto error;
1403 }
2acdc547 1404 if (ret_len < 0) {
79620497 1405 perror("[error] Error receiving stream");
2acdc547
JD
1406 goto error;
1407 }
1408 assert(ret_len == sizeof(stream));
1409 stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0';
1410 stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0';
1411
1412 printf_verbose(" stream %" PRIu64 " : %s/%s\n",
1413 be64toh(stream.id), stream.path_name,
1414 stream.channel_name);
1415 ctx->session->streams[i].id = be64toh(stream.id);
1416 ctx->session->streams[i].session = ctx->session;
1417
1418 ctx->session->streams[i].first_read = 1;
1419 ctx->session->streams[i].mmap_size = 0;
1420
1421 if (be32toh(stream.metadata_flag)) {
2acdc547 1422 ctx->session->streams[i].metadata_flag = 1;
2acdc547
JD
1423 }
1424 ret = lttng_live_ctf_trace_assign(&ctx->session->streams[i],
1425 be64toh(stream.ctf_trace_id));
1426 if (ret < 0) {
1427 goto error;
1428 }
1429
1430 }
6192b988 1431 ret = 0;
2acdc547 1432end:
6192b988 1433 return ret;
8ace20bf
MD
1434
1435error:
1436 return -1;
6192b988
JD
1437}
1438
28756729 1439int lttng_live_read(struct lttng_live_ctx *ctx)
4a744367 1440{
28756729
JG
1441 int ret = -1;
1442 int i;
4a744367
JD
1443 struct bt_ctf_iter *iter;
1444 const struct bt_ctf_event *event;
1445 struct bt_iter_pos begin_pos;
1446 struct bt_trace_descriptor *td_write;
1447 struct bt_format *fmt_write;
1448 struct ctf_text_stream_pos *sout;
2acdc547 1449 uint64_t id;
4a744367 1450
2acdc547
JD
1451 ctx->bt_ctx = bt_context_create();
1452 if (!ctx->bt_ctx) {
4a744367
JD
1453 fprintf(stderr, "[error] bt_context_create allocation\n");
1454 goto end;
1455 }
1456
1457 fmt_write = bt_lookup_format(g_quark_from_static_string("text"));
1458 if (!fmt_write) {
1459 fprintf(stderr, "[error] ctf-text error\n");
1460 goto end;
1461 }
1462
1463 td_write = fmt_write->open_trace(NULL, O_RDWR, NULL, NULL);
1464 if (!td_write) {
1465 fprintf(stderr, "[error] Error opening output trace\n");
1466 goto end_free;
1467 }
1468
1469 sout = container_of(td_write, struct ctf_text_stream_pos,
1470 trace_descriptor);
85bb4663 1471 if (!sout->parent.event_cb) {
4a744367 1472 goto end_free;
85bb4663 1473 }
4a744367 1474
6192b988
JD
1475 ret = lttng_live_create_viewer_session(ctx);
1476 if (ret < 0) {
1477 goto end_free;
1478 }
1479
2acdc547
JD
1480 for (i = 0; i < ctx->session_ids->len; i++) {
1481 id = g_array_index(ctx->session_ids, uint64_t, i);
1482 printf_verbose("Attaching to session %lu\n", id);
1483 ret = lttng_live_attach_session(ctx, id);
1484 printf_verbose("Attaching session returns %d\n", ret);
1485 if (ret < 0) {
1486 if (ret == -LTTNG_VIEWER_ATTACH_UNK) {
1487 fprintf(stderr, "[error] Unknown session ID\n");
1488 }
1489 goto end_free;
1490 }
1491 }
1492
4a744367 1493 /*
2acdc547 1494 * As long as the session is active, we try to get new streams.
4a744367 1495 */
2acdc547 1496 for (;;) {
4a744367
JD
1497 int flags;
1498
8ace20bf 1499 if (lttng_live_should_quit()) {
28756729 1500 ret = 0;
8ace20bf
MD
1501 goto end_free;
1502 }
1503
2acdc547 1504 while (!ctx->session->stream_count) {
8ace20bf
MD
1505 if (lttng_live_should_quit()
1506 || ctx->session_ids->len == 0) {
28756729 1507 ret = 0;
4a744367 1508 goto end_free;
85bb4663 1509 }
2acdc547 1510 ret = ask_new_streams(ctx);
85bb4663 1511 if (ret < 0) {
2acdc547 1512 goto end_free;
85bb4663
MD
1513 }
1514 if (!ctx->session->stream_count) {
85e2085e 1515 (void) poll(NULL, 0, ACTIVE_POLL_DELAY);
85bb4663 1516 }
2acdc547 1517 }
4a744367 1518
2acdc547
JD
1519 g_hash_table_foreach(ctx->session->ctf_traces, add_traces,
1520 ctx->bt_ctx);
4a744367
JD
1521
1522 begin_pos.type = BT_SEEK_BEGIN;
2acdc547 1523 iter = bt_ctf_iter_create(ctx->bt_ctx, &begin_pos, NULL);
4a744367 1524 if (!iter) {
e29395a8
JG
1525 if (lttng_live_should_quit()) {
1526 ret = 0;
1527 goto end;
1528 }
4a744367
JD
1529 fprintf(stderr, "[error] Iterator creation error\n");
1530 goto end;
1531 }
1532 for (;;) {
8ace20bf 1533 if (lttng_live_should_quit()) {
28756729 1534 ret = 0;
8ace20bf
MD
1535 goto end_free;
1536 }
4a744367
JD
1537 event = bt_ctf_iter_read_event_flags(iter, &flags);
1538 if (!(flags & BT_ITER_FLAG_RETRY)) {
1539 if (!event) {
1540 /* End of trace */
1541 break;
1542 }
1543 ret = sout->parent.event_cb(&sout->parent,
1544 event->parent->stream);
1545 if (ret) {
1546 fprintf(stderr, "[error] Writing "
1547 "event failed.\n");
1548 goto end_free;
1549 }
1550 }
1551 ret = bt_iter_next(bt_ctf_get_iter(iter));
1552 if (ret < 0) {
1553 goto end_free;
1554 }
1555 }
1556 bt_ctf_iter_destroy(iter);
2acdc547
JD
1557 g_hash_table_foreach_remove(ctx->session->ctf_traces,
1558 del_traces, ctx->bt_ctx);
1559 ctx->session->stream_count = 0;
1560 }
4a744367
JD
1561
1562end_free:
2acdc547 1563 bt_context_put(ctx->bt_ctx);
4a744367 1564end:
e29395a8
JG
1565 if (lttng_live_should_quit()) {
1566 ret = 0;
1567 }
28756729 1568 return ret;
4a744367 1569}
This page took 0.097596 seconds and 4 git commands to generate.