2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <common/compat/time.h>
13 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/in.h>
24 #include <lttng/lttng.h>
26 #include <urcu/list.h>
27 #include <common/common.h>
29 #include <bin/lttng-relayd/lttng-viewer-abi.h>
30 #include <common/index/ctf-index.h>
32 #include <common/compat/errno.h>
33 #include <common/compat/endian.h>
35 #define SESSION1 "test1"
36 #define RELAYD_URL "net://localhost"
37 #define LIVE_TIMER 2000000
39 /* Number of TAP tests in this file */
41 #define mmap_size 524288
43 #ifdef HAVE_LIBLTTNG_UST_CTL
44 #include <lttng/lttng-export.h>
45 #include <lttng/ust-sigbus.h>
46 LTTNG_EXPORT
DEFINE_LTTNG_UST_SIGBUS_STATE();
49 static int control_sock
;
50 struct live_session
*session
;
52 static int first_packet_offset
;
53 static int first_packet_len
;
54 static int first_packet_stream_id
= -1;
56 struct viewer_stream
{
58 uint64_t ctf_trace_id
;
67 struct viewer_stream
*streams
;
68 uint64_t live_timer_interval
;
69 uint64_t stream_count
;
73 ssize_t
lttng_live_recv(int fd
, void *buf
, size_t len
)
76 size_t copied
= 0, to_copy
= len
;
79 ret
= recv(fd
, buf
+ copied
, to_copy
, 0);
81 LTTNG_ASSERT(ret
<= to_copy
);
85 } while ((ret
> 0 && to_copy
> 0)
86 || (ret
< 0 && errno
== EINTR
));
89 /* ret = 0 means orderly shutdown, ret < 0 is error. */
94 ssize_t
lttng_live_send(int fd
, const void *buf
, size_t len
)
99 ret
= send(fd
, buf
, len
, MSG_NOSIGNAL
);
100 } while (ret
< 0 && errno
== EINTR
);
105 int connect_viewer(const char *hostname
)
107 struct hostent
*host
;
108 struct sockaddr_in server_addr
;
111 host
= gethostbyname(hostname
);
117 if ((control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
123 server_addr
.sin_family
= AF_INET
;
124 server_addr
.sin_port
= htons(5344);
125 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
126 bzero(&(server_addr
.sin_zero
), 8);
128 if (connect(control_sock
, (struct sockaddr
*) &server_addr
,
129 sizeof(struct sockaddr
)) == -1) {
135 server_addr
.sin_family
= AF_INET
;
136 server_addr
.sin_port
= htons(5345);
137 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
138 bzero(&(server_addr
.sin_zero
), 8);
147 int establish_connection(void)
149 struct lttng_viewer_cmd cmd
;
150 struct lttng_viewer_connect connect
;
153 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
154 cmd
.data_size
= htobe64(sizeof(connect
));
155 cmd
.cmd_version
= htobe32(0);
157 memset(&connect
, 0, sizeof(connect
));
158 connect
.major
= htobe32(VERSION_MAJOR
);
159 connect
.minor
= htobe32(VERSION_MINOR
);
160 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
162 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
164 diag("Error sending cmd");
167 ret_len
= lttng_live_send(control_sock
, &connect
, sizeof(connect
));
169 diag("Error sending version");
173 ret_len
= lttng_live_recv(control_sock
, &connect
, sizeof(connect
));
175 diag("[error] Remote side has closed connection");
179 diag("Error receiving version");
189 * Returns the number of sessions, should be 1 during the unit test.
192 int list_sessions(uint64_t *session_id
)
194 struct lttng_viewer_cmd cmd
;
195 struct lttng_viewer_list_sessions list
;
196 struct lttng_viewer_session lsession
;
199 int first_session
= 0;
201 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
202 cmd
.data_size
= htobe64(0);
203 cmd
.cmd_version
= htobe32(0);
205 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
207 diag("Error sending cmd");
211 ret_len
= lttng_live_recv(control_sock
, &list
, sizeof(list
));
213 diag("[error] Remote side has closed connection");
217 diag("Error receiving session list");
221 for (i
= 0; i
< be32toh(list
.sessions_count
); i
++) {
222 ret_len
= lttng_live_recv(control_sock
, &lsession
, sizeof(lsession
));
224 diag("Error receiving session");
227 if (lsession
.streams
> 0 && first_session
<= 0) {
228 first_session
= be64toh(lsession
.id
);
229 *session_id
= first_session
;
233 return be32toh(list
.sessions_count
);
240 int create_viewer_session(void)
242 struct lttng_viewer_cmd cmd
;
243 struct lttng_viewer_create_session_response resp
;
246 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
247 cmd
.data_size
= htobe64(0);
248 cmd
.cmd_version
= htobe32(0);
250 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
252 diag("[error] Error sending cmd");
255 LTTNG_ASSERT(ret_len
== sizeof(cmd
));
257 ret_len
= lttng_live_recv(control_sock
, &resp
, sizeof(resp
));
259 diag("[error] Remote side has closed connection");
263 diag("[error] Error receiving create session reply");
266 LTTNG_ASSERT(ret_len
== sizeof(resp
));
268 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
269 diag("[error] Error creating viewer session");
279 int attach_session(uint64_t id
)
281 struct lttng_viewer_cmd cmd
;
282 struct lttng_viewer_attach_session_request rq
;
283 struct lttng_viewer_attach_session_response rp
;
284 struct lttng_viewer_stream stream
;
288 session
= zmalloc(sizeof(struct live_session
));
293 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
294 cmd
.data_size
= htobe64(sizeof(rq
));
295 cmd
.cmd_version
= htobe32(0);
297 memset(&rq
, 0, sizeof(rq
));
298 rq
.session_id
= htobe64(id
);
299 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_BEGINNING
);
301 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
303 diag("Error sending cmd LTTNG_VIEWER_ATTACH_SESSION");
306 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
308 diag("Error sending attach request");
312 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
314 diag("[error] Remote side has closed connection");
318 diag("Error receiving attach response");
321 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
325 session
->stream_count
= be32toh(rp
.streams_count
);
326 if (session
->stream_count
== 0) {
327 diag("Got session stream count == 0");
330 session
->streams
= zmalloc(session
->stream_count
*
331 sizeof(struct viewer_stream
));
332 if (!session
->streams
) {
336 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
337 ret_len
= lttng_live_recv(control_sock
, &stream
, sizeof(stream
));
339 diag("[error] Remote side has closed connection");
343 diag("Error receiving stream");
346 session
->streams
[i
].id
= be64toh(stream
.id
);
348 session
->streams
[i
].ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
349 session
->streams
[i
].first_read
= 1;
350 session
->streams
[i
].mmap_base
= mmap(NULL
, mmap_size
,
351 PROT_READ
| PROT_WRITE
,
352 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
353 if (session
->streams
[i
].mmap_base
== MAP_FAILED
) {
358 if (be32toh(stream
.metadata_flag
)) {
359 session
->streams
[i
].metadata_flag
= 1;
362 return session
->stream_count
;
369 int get_metadata(void)
371 struct lttng_viewer_cmd cmd
;
372 struct lttng_viewer_get_metadata rq
;
373 struct lttng_viewer_metadata_packet rp
;
379 int metadata_stream_id
= -1;
381 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
382 cmd
.data_size
= htobe64(sizeof(rq
));
383 cmd
.cmd_version
= htobe32(0);
385 for (i
= 0; i
< session
->stream_count
; i
++) {
386 if (session
->streams
[i
].metadata_flag
) {
387 metadata_stream_id
= i
;
392 if (metadata_stream_id
< 0) {
393 diag("No metadata stream found");
397 rq
.stream_id
= htobe64(session
->streams
[metadata_stream_id
].id
);
400 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
402 diag("Error sending cmd");
405 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
407 diag("Error sending get_metadata request");
410 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
412 diag("[error] Remote side has closed connection");
416 diag("Error receiving metadata response");
419 switch (be32toh(rp
.status
)) {
420 case LTTNG_VIEWER_METADATA_OK
:
422 case LTTNG_VIEWER_NO_NEW_METADATA
:
423 diag("Got LTTNG_VIEWER_NO_NEW_METADATA:");
426 case LTTNG_VIEWER_METADATA_ERR
:
427 diag("Got LTTNG_VIEWER_METADATA_ERR:");
430 diag("Got unknown status during LTTNG_VIEWER_GET_METADATA");
434 len
= be64toh(rp
.len
);
441 PERROR("relay data zmalloc");
444 ret_len
= lttng_live_recv(control_sock
, data
, len
);
446 diag("[error] Remote side has closed connection");
447 goto error_free_data
;
450 diag("Error receiving trace packet");
451 goto error_free_data
;
465 int get_next_index(void)
467 struct lttng_viewer_cmd cmd
;
468 struct lttng_viewer_get_next_index rq
;
469 struct lttng_viewer_index rp
;
473 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
474 cmd
.data_size
= htobe64(sizeof(rq
));
475 cmd
.cmd_version
= htobe32(0);
477 for (id
= 0; id
< session
->stream_count
; id
++) {
478 if (session
->streams
[id
].metadata_flag
) {
481 memset(&rq
, 0, sizeof(rq
));
482 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
485 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
487 diag("Error sending cmd");
490 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
492 diag("Error sending get_next_index request");
495 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
497 diag("[error] Remote side has closed connection");
501 diag("Error receiving index response");
505 rp
.flags
= be32toh(rp
.flags
);
507 switch (be32toh(rp
.status
)) {
508 case LTTNG_VIEWER_INDEX_INACTIVE
:
509 /* Skip this stream. */
510 diag("Got LTTNG_VIEWER_INDEX_INACTIVE");
512 case LTTNG_VIEWER_INDEX_OK
:
514 case LTTNG_VIEWER_INDEX_RETRY
:
517 case LTTNG_VIEWER_INDEX_HUP
:
518 diag("Got LTTNG_VIEWER_INDEX_HUP");
519 session
->streams
[id
].id
= -1ULL;
520 session
->streams
[id
].fd
= -1;
522 case LTTNG_VIEWER_INDEX_ERR
:
523 diag("Got LTTNG_VIEWER_INDEX_ERR");
526 diag("Unknown reply status during LTTNG_VIEWER_GET_NEXT_INDEX (%d)", be32toh(rp
.status
));
529 if (first_packet_stream_id
< 0) {
531 * Initialize the first packet stream id. That is,
532 * the first active stream encoutered.
534 first_packet_offset
= be64toh(rp
.offset
);
535 first_packet_len
= be64toh(rp
.packet_size
) / CHAR_BIT
;
536 first_packet_stream_id
= id
;
537 diag("Got first packet index with offset %d and len %d",
538 first_packet_offset
, first_packet_len
);
548 int get_data_packet(int id
, uint64_t offset
,
551 struct lttng_viewer_cmd cmd
;
552 struct lttng_viewer_get_packet rq
;
553 struct lttng_viewer_trace_packet rp
;
556 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
557 cmd
.data_size
= htobe64(sizeof(rq
));
558 cmd
.cmd_version
= htobe32(0);
560 memset(&rq
, 0, sizeof(rq
));
561 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
562 /* Already in big endian. */
564 rq
.len
= htobe32(len
);
566 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
568 diag("Error sending cmd");
571 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
573 diag("Error sending get_data_packet request");
576 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
578 diag("[error] Remote side has closed connection");
582 diag("Error receiving data response");
585 rp
.flags
= be32toh(rp
.flags
);
587 switch (be32toh(rp
.status
)) {
588 case LTTNG_VIEWER_GET_PACKET_OK
:
589 len
= be32toh(rp
.len
);
591 diag("Got LTTNG_VIEWER_GET_PACKET_OK, but len == 0");
595 case LTTNG_VIEWER_GET_PACKET_RETRY
:
596 diag("Got LTTNG_VIEWER_GET_PACKET_RETRY:");
598 case LTTNG_VIEWER_GET_PACKET_ERR
:
599 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
600 diag("Got LTTNG_VIEWER_GET_PACKET_ERR with NEW_METADATA flag");
603 diag("Got LTTNG_VIEWER_GET_PACKET_ERR:");
606 diag("Got unknown status code during LTTNG_VIEWER_GET_PACKET");
610 if (len
> mmap_size
) {
611 diag("mmap_size not big enough");
615 ret_len
= lttng_live_recv(control_sock
, session
->streams
[id
].mmap_base
, len
);
617 diag("[error] Remote side has closed connection");
621 diag("Error receiving trace packet");
631 int detach_viewer_session(uint64_t id
)
633 struct lttng_viewer_cmd cmd
;
634 struct lttng_viewer_detach_session_response resp
;
635 struct lttng_viewer_detach_session_request rq
;
639 cmd
.cmd
= htobe32(LTTNG_VIEWER_DETACH_SESSION
);
640 cmd
.data_size
= htobe64(sizeof(rq
));
641 cmd
.cmd_version
= htobe32(0);
643 memset(&rq
, 0, sizeof(rq
));
644 rq
.session_id
= htobe64(id
);
646 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
648 fprintf(stderr
, "[error] Error sending cmd\n");
653 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
655 fprintf(stderr
, "Error sending attach request\n");
660 ret_len
= lttng_live_recv(control_sock
, &resp
, sizeof(resp
));
662 fprintf(stderr
, "[error] Error receiving detach session reply\n");
667 if (be32toh(resp
.status
) != LTTNG_VIEWER_DETACH_SESSION_OK
) {
668 fprintf(stderr
, "[error] Error detaching viewer session\n");
678 int main(int argc
, char **argv
)
683 plan_tests(NUM_TESTS
);
685 diag("Live unit tests");
687 ret
= connect_viewer("localhost");
688 ok(ret
== 0, "Connect viewer to relayd");
690 ret
= establish_connection();
691 ok(ret
== 0, "Established connection and version check with %d.%d",
692 VERSION_MAJOR
, VERSION_MINOR
);
694 ret
= list_sessions(&session_id
);
695 ok(ret
> 0, "List sessions : %d session(s)", ret
);
700 ret
= create_viewer_session();
701 ok(ret
== 0, "Create viewer session");
703 ret
= attach_session(session_id
);
704 ok(ret
> 0, "Attach to session, %d stream(s) received", ret
);
706 ret
= get_metadata();
707 ok(ret
> 0, "Get metadata, received %d bytes", ret
);
709 ret
= get_next_index();
710 ok(ret
== 0, "Get one index per stream");
712 ret
= get_data_packet(first_packet_stream_id
, first_packet_offset
,
715 "Get one data packet for stream %d, offset %d, len %d",
716 first_packet_stream_id
, first_packet_offset
,
719 ret
= detach_viewer_session(session_id
);
720 ok(ret
== 0, "Detach viewer session");
722 ret
= list_sessions(&session_id
);
723 ok(ret
> 0, "List sessions : %d session(s)", ret
);
725 ret
= attach_session(session_id
);
726 ok(ret
> 0, "Attach to session, %d streams received", ret
);
728 return exit_status();