2 * Copyright (c) - 2013 Julien Desfossez <jdesfossez@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by as
6 * published by the Free Software Foundation; only version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/compat/time.h>
25 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
36 #include <lttng/lttng.h>
38 #include <urcu/list.h>
39 #include <bin/lttng-sessiond/session.h>
40 #include <common/common.h>
42 #include <bin/lttng-relayd/lttng-viewer-abi.h>
43 #include <common/index/ctf-index.h>
45 #include <common/compat/endian.h>
47 #define SESSION1 "test1"
48 #define RELAYD_URL "net://localhost"
49 #define LIVE_TIMER 2000000
51 /* Number of TAP tests in this file */
53 #define mmap_size 524288
55 int ust_consumerd32_fd
;
56 int ust_consumerd64_fd
;
58 static int control_sock
;
59 struct live_session
*session
;
61 static int first_packet_offset
;
62 static int first_packet_len
;
63 static int first_packet_stream_id
= -1;
65 struct viewer_stream
{
67 uint64_t ctf_trace_id
;
76 struct viewer_stream
*streams
;
77 uint64_t live_timer_interval
;
78 uint64_t stream_count
;
82 ssize_t
lttng_live_recv(int fd
, void *buf
, size_t len
)
85 size_t copied
= 0, to_copy
= len
;
88 ret
= recv(fd
, buf
+ copied
, to_copy
, 0);
90 assert(ret
<= to_copy
);
94 } while ((ret
> 0 && to_copy
> 0)
95 || (ret
< 0 && errno
== EINTR
));
98 /* ret = 0 means orderly shutdown, ret < 0 is error. */
103 ssize_t
lttng_live_send(int fd
, const void *buf
, size_t len
)
108 ret
= send(fd
, buf
, len
, MSG_NOSIGNAL
);
109 } while (ret
< 0 && errno
== EINTR
);
114 int connect_viewer(char *hostname
)
116 struct hostent
*host
;
117 struct sockaddr_in server_addr
;
120 host
= gethostbyname(hostname
);
126 if ((control_sock
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
132 server_addr
.sin_family
= AF_INET
;
133 server_addr
.sin_port
= htons(5344);
134 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
135 bzero(&(server_addr
.sin_zero
), 8);
137 if (connect(control_sock
, (struct sockaddr
*) &server_addr
,
138 sizeof(struct sockaddr
)) == -1) {
144 server_addr
.sin_family
= AF_INET
;
145 server_addr
.sin_port
= htons(5345);
146 server_addr
.sin_addr
= *((struct in_addr
*) host
->h_addr
);
147 bzero(&(server_addr
.sin_zero
), 8);
155 int establish_connection(void)
157 struct lttng_viewer_cmd cmd
;
158 struct lttng_viewer_connect connect
;
161 cmd
.cmd
= htobe32(LTTNG_VIEWER_CONNECT
);
162 cmd
.data_size
= htobe64(sizeof(connect
));
163 cmd
.cmd_version
= htobe32(0);
165 memset(&connect
, 0, sizeof(connect
));
166 connect
.major
= htobe32(VERSION_MAJOR
);
167 connect
.minor
= htobe32(VERSION_MINOR
);
168 connect
.type
= htobe32(LTTNG_VIEWER_CLIENT_COMMAND
);
170 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
172 diag("Error sending cmd");
175 ret_len
= lttng_live_send(control_sock
, &connect
, sizeof(connect
));
177 diag("Error sending version");
181 ret_len
= lttng_live_recv(control_sock
, &connect
, sizeof(connect
));
183 diag("[error] Remote side has closed connection");
187 diag("Error receiving version");
197 * Returns the number of sessions, should be 1 during the unit test.
199 int list_sessions(uint64_t *session_id
)
201 struct lttng_viewer_cmd cmd
;
202 struct lttng_viewer_list_sessions list
;
203 struct lttng_viewer_session lsession
;
206 int first_session
= 0;
208 cmd
.cmd
= htobe32(LTTNG_VIEWER_LIST_SESSIONS
);
209 cmd
.data_size
= htobe64(0);
210 cmd
.cmd_version
= htobe32(0);
212 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
214 diag("Error sending cmd");
218 ret_len
= lttng_live_recv(control_sock
, &list
, sizeof(list
));
220 diag("[error] Remote side has closed connection");
224 diag("Error receiving session list");
228 for (i
= 0; i
< be32toh(list
.sessions_count
); i
++) {
229 ret_len
= lttng_live_recv(control_sock
, &lsession
, sizeof(lsession
));
231 diag("Error receiving session");
234 if (lsession
.streams
> 0 && first_session
<= 0) {
235 first_session
= be64toh(lsession
.id
);
236 *session_id
= first_session
;
240 return be32toh(list
.sessions_count
);
246 int create_viewer_session(void)
248 struct lttng_viewer_cmd cmd
;
249 struct lttng_viewer_create_session_response resp
;
252 cmd
.cmd
= htobe32(LTTNG_VIEWER_CREATE_SESSION
);
253 cmd
.data_size
= htobe64(0);
254 cmd
.cmd_version
= htobe32(0);
256 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
258 diag("[error] Error sending cmd");
261 assert(ret_len
== sizeof(cmd
));
263 ret_len
= lttng_live_recv(control_sock
, &resp
, sizeof(resp
));
265 diag("[error] Remote side has closed connection");
269 diag("[error] Error receiving create session reply");
272 assert(ret_len
== sizeof(resp
));
274 if (be32toh(resp
.status
) != LTTNG_VIEWER_CREATE_SESSION_OK
) {
275 diag("[error] Error creating viewer session");
284 int attach_session(uint64_t id
)
286 struct lttng_viewer_cmd cmd
;
287 struct lttng_viewer_attach_session_request rq
;
288 struct lttng_viewer_attach_session_response rp
;
289 struct lttng_viewer_stream stream
;
293 session
= zmalloc(sizeof(struct live_session
));
298 cmd
.cmd
= htobe32(LTTNG_VIEWER_ATTACH_SESSION
);
299 cmd
.data_size
= htobe64(sizeof(rq
));
300 cmd
.cmd_version
= htobe32(0);
302 memset(&rq
, 0, sizeof(rq
));
303 rq
.session_id
= htobe64(id
);
304 rq
.seek
= htobe32(LTTNG_VIEWER_SEEK_BEGINNING
);
306 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
308 diag("Error sending cmd LTTNG_VIEWER_ATTACH_SESSION");
311 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
313 diag("Error sending attach request");
317 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
319 diag("[error] Remote side has closed connection");
323 diag("Error receiving attach response");
326 if (be32toh(rp
.status
) != LTTNG_VIEWER_ATTACH_OK
) {
330 session
->stream_count
= be32toh(rp
.streams_count
);
331 if (session
->stream_count
== 0) {
332 diag("Got session stream count == 0");
335 session
->streams
= zmalloc(session
->stream_count
*
336 sizeof(struct viewer_stream
));
337 if (!session
->streams
) {
341 for (i
= 0; i
< be32toh(rp
.streams_count
); i
++) {
342 ret_len
= lttng_live_recv(control_sock
, &stream
, sizeof(stream
));
344 diag("[error] Remote side has closed connection");
348 diag("Error receiving stream");
351 session
->streams
[i
].id
= be64toh(stream
.id
);
353 session
->streams
[i
].ctf_trace_id
= be64toh(stream
.ctf_trace_id
);
354 session
->streams
[i
].first_read
= 1;
355 session
->streams
[i
].mmap_base
= mmap(NULL
, mmap_size
,
356 PROT_READ
| PROT_WRITE
,
357 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
358 if (session
->streams
[i
].mmap_base
== MAP_FAILED
) {
363 if (be32toh(stream
.metadata_flag
)) {
364 session
->streams
[i
].metadata_flag
= 1;
367 return session
->stream_count
;
373 int get_metadata(void)
375 struct lttng_viewer_cmd cmd
;
376 struct lttng_viewer_get_metadata rq
;
377 struct lttng_viewer_metadata_packet rp
;
383 int metadata_stream_id
= -1;
385 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_METADATA
);
386 cmd
.data_size
= htobe64(sizeof(rq
));
387 cmd
.cmd_version
= htobe32(0);
389 for (i
= 0; i
< session
->stream_count
; i
++) {
390 if (session
->streams
[i
].metadata_flag
) {
391 metadata_stream_id
= i
;
396 if (metadata_stream_id
< 0) {
397 diag("No metadata stream found");
401 rq
.stream_id
= htobe64(session
->streams
[metadata_stream_id
].id
);
403 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
405 diag("Error sending cmd");
408 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
410 diag("Error sending get_metadata request");
413 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
415 diag("[error] Remote side has closed connection");
419 diag("Error receiving metadata response");
422 switch (be32toh(rp
.status
)) {
423 case LTTNG_VIEWER_METADATA_OK
:
425 case LTTNG_VIEWER_NO_NEW_METADATA
:
426 diag("Got LTTNG_VIEWER_NO_NEW_METADATA:");
429 case LTTNG_VIEWER_METADATA_ERR
:
430 diag("Got LTTNG_VIEWER_METADATA_ERR:");
433 diag("Got unknown status during LTTNG_VIEWER_GET_METADATA");
437 len
= be64toh(rp
.len
);
444 PERROR("relay data zmalloc");
447 ret_len
= lttng_live_recv(control_sock
, data
, len
);
449 diag("[error] Remote side has closed connection");
450 goto error_free_data
;
453 diag("Error receiving trace packet");
454 goto error_free_data
;
467 int get_next_index(void)
469 struct lttng_viewer_cmd cmd
;
470 struct lttng_viewer_get_next_index rq
;
471 struct lttng_viewer_index rp
;
475 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_NEXT_INDEX
);
476 cmd
.data_size
= htobe64(sizeof(rq
));
477 cmd
.cmd_version
= htobe32(0);
479 for (id
= 0; id
< session
->stream_count
; id
++) {
480 if (session
->streams
[id
].metadata_flag
) {
483 memset(&rq
, 0, sizeof(rq
));
484 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
487 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
489 diag("Error sending cmd");
492 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
494 diag("Error sending get_next_index request");
497 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
499 diag("[error] Remote side has closed connection");
503 diag("Error receiving index response");
507 rp
.flags
= be32toh(rp
.flags
);
509 switch (be32toh(rp
.status
)) {
510 case LTTNG_VIEWER_INDEX_INACTIVE
:
511 /* Skip this stream. */
512 diag("Got LTTNG_VIEWER_INDEX_INACTIVE");
514 case LTTNG_VIEWER_INDEX_OK
:
516 case LTTNG_VIEWER_INDEX_RETRY
:
519 case LTTNG_VIEWER_INDEX_HUP
:
520 diag("Got LTTNG_VIEWER_INDEX_HUP");
521 session
->streams
[id
].id
= -1ULL;
522 session
->streams
[id
].fd
= -1;
524 case LTTNG_VIEWER_INDEX_ERR
:
525 diag("Got LTTNG_VIEWER_INDEX_ERR");
528 diag("Unknown reply status during LTTNG_VIEWER_GET_NEXT_INDEX (%d)", be32toh(rp
.status
));
531 if (first_packet_stream_id
< 0) {
533 * Initialize the first packet stream id. That is,
534 * the first active stream encoutered.
536 first_packet_offset
= be64toh(rp
.offset
);
537 first_packet_len
= be64toh(rp
.packet_size
) / CHAR_BIT
;
538 first_packet_stream_id
= id
;
539 diag("Got first packet index with offset %d and len %d",
540 first_packet_offset
, first_packet_len
);
550 int get_data_packet(int id
, uint64_t offset
,
553 struct lttng_viewer_cmd cmd
;
554 struct lttng_viewer_get_packet rq
;
555 struct lttng_viewer_trace_packet rp
;
558 cmd
.cmd
= htobe32(LTTNG_VIEWER_GET_PACKET
);
559 cmd
.data_size
= htobe64(sizeof(rq
));
560 cmd
.cmd_version
= htobe32(0);
562 memset(&rq
, 0, sizeof(rq
));
563 rq
.stream_id
= htobe64(session
->streams
[id
].id
);
564 /* Already in big endian. */
566 rq
.len
= htobe32(len
);
568 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
570 diag("Error sending cmd");
573 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
575 diag("Error sending get_data_packet request");
578 ret_len
= lttng_live_recv(control_sock
, &rp
, sizeof(rp
));
580 diag("[error] Remote side has closed connection");
584 diag("Error receiving data response");
587 rp
.flags
= be32toh(rp
.flags
);
589 switch (be32toh(rp
.status
)) {
590 case LTTNG_VIEWER_GET_PACKET_OK
:
591 len
= be32toh(rp
.len
);
593 diag("Got LTTNG_VIEWER_GET_PACKET_OK, but len == 0");
597 case LTTNG_VIEWER_GET_PACKET_RETRY
:
598 diag("Got LTTNG_VIEWER_GET_PACKET_RETRY:");
600 case LTTNG_VIEWER_GET_PACKET_ERR
:
601 if (rp
.flags
& LTTNG_VIEWER_FLAG_NEW_METADATA
) {
602 diag("Got LTTNG_VIEWER_GET_PACKET_ERR with NEW_METADATA flag");
605 diag("Got LTTNG_VIEWER_GET_PACKET_ERR:");
608 diag("Got unknown status code during LTTNG_VIEWER_GET_PACKET");
612 if (len
> mmap_size
) {
613 diag("mmap_size not big enough");
617 ret_len
= lttng_live_recv(control_sock
, session
->streams
[id
].mmap_base
, len
);
619 diag("[error] Remote side has closed connection");
623 diag("Error receiving trace packet");
632 int detach_viewer_session(uint64_t id
)
634 struct lttng_viewer_cmd cmd
;
635 struct lttng_viewer_detach_session_response resp
;
636 struct lttng_viewer_detach_session_request rq
;
640 cmd
.cmd
= htobe32(LTTNG_VIEWER_DETACH_SESSION
);
641 cmd
.data_size
= htobe64(sizeof(rq
));
642 cmd
.cmd_version
= htobe32(0);
644 memset(&rq
, 0, sizeof(rq
));
645 rq
.session_id
= htobe64(id
);
647 ret_len
= lttng_live_send(control_sock
, &cmd
, sizeof(cmd
));
649 fprintf(stderr
, "[error] Error sending cmd\n");
654 ret_len
= lttng_live_send(control_sock
, &rq
, sizeof(rq
));
656 fprintf(stderr
, "Error sending attach request\n");
661 ret_len
= lttng_live_recv(control_sock
, &resp
, sizeof(resp
));
663 fprintf(stderr
, "[error] Error receiving detach session reply\n");
668 if (be32toh(resp
.status
) != LTTNG_VIEWER_DETACH_SESSION_OK
) {
669 fprintf(stderr
, "[error] Error detaching viewer session\n");
679 int main(int argc
, char **argv
)
684 plan_tests(NUM_TESTS
);
686 diag("Live unit tests");
688 ret
= connect_viewer("localhost");
689 ok(ret
== 0, "Connect viewer to relayd");
691 ret
= establish_connection();
692 ok(ret
== 0, "Established connection and version check with %d.%d",
693 VERSION_MAJOR
, VERSION_MINOR
);
695 ret
= list_sessions(&session_id
);
696 ok(ret
> 0, "List sessions : %d session(s)", ret
);
698 ret
= create_viewer_session();
699 ok(ret
== 0, "Create viewer session");
701 ret
= attach_session(session_id
);
702 ok(ret
> 0, "Attach to session, %d stream(s) received", ret
);
704 ret
= get_metadata();
705 ok(ret
> 0, "Get metadata, received %d bytes", ret
);
707 ret
= get_next_index();
708 ok(ret
== 0, "Get one index per stream");
710 ret
= get_data_packet(first_packet_stream_id
, first_packet_offset
,
713 "Get one data packet for stream %d, offset %d, len %d",
714 first_packet_stream_id
, first_packet_offset
,
717 ret
= detach_viewer_session(session_id
);
718 ok(ret
== 0, "Detach viewer session");
720 ret
= list_sessions(&session_id
);
721 ok(ret
> 0, "List sessions : %d session(s)", ret
);
723 ret
= attach_session(session_id
);
724 ok(ret
> 0, "Attach to session, %d streams received", ret
);
726 return exit_status();