Clean-up: move global sessiond symbols out of main.o
[lttng-tools.git] / tests / regression / tools / live / live_test.c
1 /*
2 * Copyright (c) - 2013 Julien Desfossez <jdesfossez@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #include <assert.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <common/compat/time.h>
25 #include <sys/types.h>
26 #include <inttypes.h>
27 #include <stdlib.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <netdb.h>
31 #include <fcntl.h>
32 #include <sys/mman.h>
33 #include <sys/stat.h>
34
35 #include <tap/tap.h>
36 #include <lttng/lttng.h>
37
38 #include <urcu/list.h>
39 #include <common/common.h>
40
41 #include <bin/lttng-relayd/lttng-viewer-abi.h>
42 #include <common/index/ctf-index.h>
43
44 #include <common/compat/endian.h>
45
46 #define SESSION1 "test1"
47 #define RELAYD_URL "net://localhost"
48 #define LIVE_TIMER 2000000
49
50 /* Number of TAP tests in this file */
51 #define NUM_TESTS 11
52 #define mmap_size 524288
53
54 int ust_consumerd32_fd;
55 int ust_consumerd64_fd;
56
57 static int control_sock;
58 struct live_session *session;
59
60 static int first_packet_offset;
61 static int first_packet_len;
62 static int first_packet_stream_id = -1;
63
64 struct viewer_stream {
65 uint64_t id;
66 uint64_t ctf_trace_id;
67 void *mmap_base;
68 int fd;
69 int metadata_flag;
70 int first_read;
71 char path[PATH_MAX];
72 };
73
74 struct live_session {
75 struct viewer_stream *streams;
76 uint64_t live_timer_interval;
77 uint64_t stream_count;
78 };
79
80 static
81 ssize_t lttng_live_recv(int fd, void *buf, size_t len)
82 {
83 ssize_t ret;
84 size_t copied = 0, to_copy = len;
85
86 do {
87 ret = recv(fd, buf + copied, to_copy, 0);
88 if (ret > 0) {
89 assert(ret <= to_copy);
90 copied += ret;
91 to_copy -= ret;
92 }
93 } while ((ret > 0 && to_copy > 0)
94 || (ret < 0 && errno == EINTR));
95 if (ret > 0)
96 ret = copied;
97 /* ret = 0 means orderly shutdown, ret < 0 is error. */
98 return ret;
99 }
100
101 static
102 ssize_t lttng_live_send(int fd, const void *buf, size_t len)
103 {
104 ssize_t ret;
105
106 do {
107 ret = send(fd, buf, len, MSG_NOSIGNAL);
108 } while (ret < 0 && errno == EINTR);
109 return ret;
110 }
111
112 static
113 int connect_viewer(char *hostname)
114 {
115 struct hostent *host;
116 struct sockaddr_in server_addr;
117 int ret;
118
119 host = gethostbyname(hostname);
120 if (!host) {
121 ret = -1;
122 goto end;
123 }
124
125 if ((control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
126 PERROR("Socket");
127 ret = -1;
128 goto end;
129 }
130
131 server_addr.sin_family = AF_INET;
132 server_addr.sin_port = htons(5344);
133 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
134 bzero(&(server_addr.sin_zero), 8);
135
136 if (connect(control_sock, (struct sockaddr *) &server_addr,
137 sizeof(struct sockaddr)) == -1) {
138 PERROR("Connect");
139 ret = -1;
140 goto end;
141 }
142
143 server_addr.sin_family = AF_INET;
144 server_addr.sin_port = htons(5345);
145 server_addr.sin_addr = *((struct in_addr *) host->h_addr);
146 bzero(&(server_addr.sin_zero), 8);
147
148 ret = 0;
149
150 end:
151 return ret;
152 }
153
154 int establish_connection(void)
155 {
156 struct lttng_viewer_cmd cmd;
157 struct lttng_viewer_connect connect;
158 ssize_t ret_len;
159
160 cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT);
161 cmd.data_size = htobe64(sizeof(connect));
162 cmd.cmd_version = htobe32(0);
163
164 memset(&connect, 0, sizeof(connect));
165 connect.major = htobe32(VERSION_MAJOR);
166 connect.minor = htobe32(VERSION_MINOR);
167 connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND);
168
169 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
170 if (ret_len < 0) {
171 diag("Error sending cmd");
172 goto error;
173 }
174 ret_len = lttng_live_send(control_sock, &connect, sizeof(connect));
175 if (ret_len < 0) {
176 diag("Error sending version");
177 goto error;
178 }
179
180 ret_len = lttng_live_recv(control_sock, &connect, sizeof(connect));
181 if (ret_len == 0) {
182 diag("[error] Remote side has closed connection");
183 goto error;
184 }
185 if (ret_len < 0) {
186 diag("Error receiving version");
187 goto error;
188 }
189 return 0;
190
191 error:
192 return -1;
193 }
194
195 /*
196 * Returns the number of sessions, should be 1 during the unit test.
197 */
198 int list_sessions(uint64_t *session_id)
199 {
200 struct lttng_viewer_cmd cmd;
201 struct lttng_viewer_list_sessions list;
202 struct lttng_viewer_session lsession;
203 int i;
204 ssize_t ret_len;
205 int first_session = 0;
206
207 cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS);
208 cmd.data_size = htobe64(0);
209 cmd.cmd_version = htobe32(0);
210
211 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
212 if (ret_len < 0) {
213 diag("Error sending cmd");
214 goto error;
215 }
216
217 ret_len = lttng_live_recv(control_sock, &list, sizeof(list));
218 if (ret_len == 0) {
219 diag("[error] Remote side has closed connection");
220 goto error;
221 }
222 if (ret_len < 0) {
223 diag("Error receiving session list");
224 goto error;
225 }
226
227 for (i = 0; i < be32toh(list.sessions_count); i++) {
228 ret_len = lttng_live_recv(control_sock, &lsession, sizeof(lsession));
229 if (ret_len < 0) {
230 diag("Error receiving session");
231 goto error;
232 }
233 if (lsession.streams > 0 && first_session <= 0) {
234 first_session = be64toh(lsession.id);
235 *session_id = first_session;
236 }
237 }
238
239 return be32toh(list.sessions_count);
240
241 error:
242 return -1;
243 }
244
245 int create_viewer_session(void)
246 {
247 struct lttng_viewer_cmd cmd;
248 struct lttng_viewer_create_session_response resp;
249 ssize_t ret_len;
250
251 cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION);
252 cmd.data_size = htobe64(0);
253 cmd.cmd_version = htobe32(0);
254
255 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
256 if (ret_len < 0) {
257 diag("[error] Error sending cmd");
258 goto error;
259 }
260 assert(ret_len == sizeof(cmd));
261
262 ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp));
263 if (ret_len == 0) {
264 diag("[error] Remote side has closed connection");
265 goto error;
266 }
267 if (ret_len < 0) {
268 diag("[error] Error receiving create session reply");
269 goto error;
270 }
271 assert(ret_len == sizeof(resp));
272
273 if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) {
274 diag("[error] Error creating viewer session");
275 goto error;
276 }
277 return 0;
278
279 error:
280 return -1;
281 }
282
283 int attach_session(uint64_t id)
284 {
285 struct lttng_viewer_cmd cmd;
286 struct lttng_viewer_attach_session_request rq;
287 struct lttng_viewer_attach_session_response rp;
288 struct lttng_viewer_stream stream;
289 int i;
290 ssize_t ret_len;
291
292 session = zmalloc(sizeof(struct live_session));
293 if (!session) {
294 goto error;
295 }
296
297 cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION);
298 cmd.data_size = htobe64(sizeof(rq));
299 cmd.cmd_version = htobe32(0);
300
301 memset(&rq, 0, sizeof(rq));
302 rq.session_id = htobe64(id);
303 rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING);
304
305 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
306 if (ret_len < 0) {
307 diag("Error sending cmd LTTNG_VIEWER_ATTACH_SESSION");
308 goto error;
309 }
310 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
311 if (ret_len < 0) {
312 diag("Error sending attach request");
313 goto error;
314 }
315
316 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
317 if (ret_len == 0) {
318 diag("[error] Remote side has closed connection");
319 goto error;
320 }
321 if (ret_len < 0) {
322 diag("Error receiving attach response");
323 goto error;
324 }
325 if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) {
326 goto error;
327 }
328
329 session->stream_count = be32toh(rp.streams_count);
330 if (session->stream_count == 0) {
331 diag("Got session stream count == 0");
332 goto error;
333 }
334 session->streams = zmalloc(session->stream_count *
335 sizeof(struct viewer_stream));
336 if (!session->streams) {
337 goto error;
338 }
339
340 for (i = 0; i < be32toh(rp.streams_count); i++) {
341 ret_len = lttng_live_recv(control_sock, &stream, sizeof(stream));
342 if (ret_len == 0) {
343 diag("[error] Remote side has closed connection");
344 goto error;
345 }
346 if (ret_len < 0) {
347 diag("Error receiving stream");
348 goto error;
349 }
350 session->streams[i].id = be64toh(stream.id);
351
352 session->streams[i].ctf_trace_id = be64toh(stream.ctf_trace_id);
353 session->streams[i].first_read = 1;
354 session->streams[i].mmap_base = mmap(NULL, mmap_size,
355 PROT_READ | PROT_WRITE,
356 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
357 if (session->streams[i].mmap_base == MAP_FAILED) {
358 diag("mmap error");
359 goto error;
360 }
361
362 if (be32toh(stream.metadata_flag)) {
363 session->streams[i].metadata_flag = 1;
364 }
365 }
366 return session->stream_count;
367
368 error:
369 return -1;
370 }
371
372 int get_metadata(void)
373 {
374 struct lttng_viewer_cmd cmd;
375 struct lttng_viewer_get_metadata rq;
376 struct lttng_viewer_metadata_packet rp;
377 ssize_t ret_len;
378 int ret;
379 uint64_t i;
380 char *data = NULL;
381 uint64_t len = 0;
382 int metadata_stream_id = -1;
383
384 cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA);
385 cmd.data_size = htobe64(sizeof(rq));
386 cmd.cmd_version = htobe32(0);
387
388 for (i = 0; i < session->stream_count; i++) {
389 if (session->streams[i].metadata_flag) {
390 metadata_stream_id = i;
391 break;
392 }
393 }
394
395 if (metadata_stream_id < 0) {
396 diag("No metadata stream found");
397 goto error;
398 }
399
400 rq.stream_id = htobe64(session->streams[metadata_stream_id].id);
401
402 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
403 if (ret_len < 0) {
404 diag("Error sending cmd");
405 goto error;
406 }
407 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
408 if (ret_len < 0) {
409 diag("Error sending get_metadata request");
410 goto error;
411 }
412 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
413 if (ret_len == 0) {
414 diag("[error] Remote side has closed connection");
415 goto error;
416 }
417 if (ret_len < 0) {
418 diag("Error receiving metadata response");
419 goto error;
420 }
421 switch (be32toh(rp.status)) {
422 case LTTNG_VIEWER_METADATA_OK:
423 break;
424 case LTTNG_VIEWER_NO_NEW_METADATA:
425 diag("Got LTTNG_VIEWER_NO_NEW_METADATA:");
426 ret = 0;
427 goto end;
428 case LTTNG_VIEWER_METADATA_ERR:
429 diag("Got LTTNG_VIEWER_METADATA_ERR:");
430 goto error;
431 default:
432 diag("Got unknown status during LTTNG_VIEWER_GET_METADATA");
433 goto error;
434 }
435
436 len = be64toh(rp.len);
437 if (len <= 0) {
438 goto error;
439 }
440
441 data = zmalloc(len);
442 if (!data) {
443 PERROR("relay data zmalloc");
444 goto error;
445 }
446 ret_len = lttng_live_recv(control_sock, data, len);
447 if (ret_len == 0) {
448 diag("[error] Remote side has closed connection");
449 goto error_free_data;
450 }
451 if (ret_len < 0) {
452 diag("Error receiving trace packet");
453 goto error_free_data;
454 }
455 free(data);
456 ret = len;
457 end:
458 return ret;
459
460 error_free_data:
461 free(data);
462 error:
463 return -1;
464 }
465
466 int get_next_index(void)
467 {
468 struct lttng_viewer_cmd cmd;
469 struct lttng_viewer_get_next_index rq;
470 struct lttng_viewer_index rp;
471 ssize_t ret_len;
472 int id;
473
474 cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX);
475 cmd.data_size = htobe64(sizeof(rq));
476 cmd.cmd_version = htobe32(0);
477
478 for (id = 0; id < session->stream_count; id++) {
479 if (session->streams[id].metadata_flag) {
480 continue;
481 }
482 memset(&rq, 0, sizeof(rq));
483 rq.stream_id = htobe64(session->streams[id].id);
484
485 retry:
486 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
487 if (ret_len < 0) {
488 diag("Error sending cmd");
489 goto error;
490 }
491 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
492 if (ret_len < 0) {
493 diag("Error sending get_next_index request");
494 goto error;
495 }
496 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
497 if (ret_len == 0) {
498 diag("[error] Remote side has closed connection");
499 goto error;
500 }
501 if (ret_len < 0) {
502 diag("Error receiving index response");
503 goto error;
504 }
505
506 rp.flags = be32toh(rp.flags);
507
508 switch (be32toh(rp.status)) {
509 case LTTNG_VIEWER_INDEX_INACTIVE:
510 /* Skip this stream. */
511 diag("Got LTTNG_VIEWER_INDEX_INACTIVE");
512 continue;
513 case LTTNG_VIEWER_INDEX_OK:
514 break;
515 case LTTNG_VIEWER_INDEX_RETRY:
516 sleep(1);
517 goto retry;
518 case LTTNG_VIEWER_INDEX_HUP:
519 diag("Got LTTNG_VIEWER_INDEX_HUP");
520 session->streams[id].id = -1ULL;
521 session->streams[id].fd = -1;
522 goto error;
523 case LTTNG_VIEWER_INDEX_ERR:
524 diag("Got LTTNG_VIEWER_INDEX_ERR");
525 goto error;
526 default:
527 diag("Unknown reply status during LTTNG_VIEWER_GET_NEXT_INDEX (%d)", be32toh(rp.status));
528 goto error;
529 }
530 if (first_packet_stream_id < 0) {
531 /*
532 * Initialize the first packet stream id. That is,
533 * the first active stream encoutered.
534 */
535 first_packet_offset = be64toh(rp.offset);
536 first_packet_len = be64toh(rp.packet_size) / CHAR_BIT;
537 first_packet_stream_id = id;
538 diag("Got first packet index with offset %d and len %d",
539 first_packet_offset, first_packet_len);
540 }
541 }
542 return 0;
543
544 error:
545 return -1;
546 }
547
548 static
549 int get_data_packet(int id, uint64_t offset,
550 uint64_t len)
551 {
552 struct lttng_viewer_cmd cmd;
553 struct lttng_viewer_get_packet rq;
554 struct lttng_viewer_trace_packet rp;
555 ssize_t ret_len;
556
557 cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET);
558 cmd.data_size = htobe64(sizeof(rq));
559 cmd.cmd_version = htobe32(0);
560
561 memset(&rq, 0, sizeof(rq));
562 rq.stream_id = htobe64(session->streams[id].id);
563 /* Already in big endian. */
564 rq.offset = offset;
565 rq.len = htobe32(len);
566
567 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
568 if (ret_len < 0) {
569 diag("Error sending cmd");
570 goto error;
571 }
572 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
573 if (ret_len < 0) {
574 diag("Error sending get_data_packet request");
575 goto error;
576 }
577 ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp));
578 if (ret_len == 0) {
579 diag("[error] Remote side has closed connection");
580 goto error;
581 }
582 if (ret_len < 0) {
583 diag("Error receiving data response");
584 goto error;
585 }
586 rp.flags = be32toh(rp.flags);
587
588 switch (be32toh(rp.status)) {
589 case LTTNG_VIEWER_GET_PACKET_OK:
590 len = be32toh(rp.len);
591 if (len == 0) {
592 diag("Got LTTNG_VIEWER_GET_PACKET_OK, but len == 0");
593 goto error;
594 }
595 break;
596 case LTTNG_VIEWER_GET_PACKET_RETRY:
597 diag("Got LTTNG_VIEWER_GET_PACKET_RETRY:");
598 goto error;
599 case LTTNG_VIEWER_GET_PACKET_ERR:
600 if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) {
601 diag("Got LTTNG_VIEWER_GET_PACKET_ERR with NEW_METADATA flag");
602 goto end;
603 }
604 diag("Got LTTNG_VIEWER_GET_PACKET_ERR:");
605 goto error;
606 default:
607 diag("Got unknown status code during LTTNG_VIEWER_GET_PACKET");
608 goto error;
609 }
610
611 if (len > mmap_size) {
612 diag("mmap_size not big enough");
613 goto error;
614 }
615
616 ret_len = lttng_live_recv(control_sock, session->streams[id].mmap_base, len);
617 if (ret_len == 0) {
618 diag("[error] Remote side has closed connection");
619 goto error;
620 }
621 if (ret_len < 0) {
622 diag("Error receiving trace packet");
623 goto error;
624 }
625 end:
626 return 0;
627 error:
628 return -1;
629 }
630
631 int detach_viewer_session(uint64_t id)
632 {
633 struct lttng_viewer_cmd cmd;
634 struct lttng_viewer_detach_session_response resp;
635 struct lttng_viewer_detach_session_request rq;
636 int ret;
637 ssize_t ret_len;
638
639 cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION);
640 cmd.data_size = htobe64(sizeof(rq));
641 cmd.cmd_version = htobe32(0);
642
643 memset(&rq, 0, sizeof(rq));
644 rq.session_id = htobe64(id);
645
646 ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd));
647 if (ret_len < 0) {
648 fprintf(stderr, "[error] Error sending cmd\n");
649 ret = ret_len;
650 goto error;
651 }
652
653 ret_len = lttng_live_send(control_sock, &rq, sizeof(rq));
654 if (ret_len < 0) {
655 fprintf(stderr, "Error sending attach request\n");
656 ret = ret_len;
657 goto error;
658 }
659
660 ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp));
661 if (ret_len < 0) {
662 fprintf(stderr, "[error] Error receiving detach session reply\n");
663 ret = ret_len;
664 goto error;
665 }
666
667 if (be32toh(resp.status) != LTTNG_VIEWER_DETACH_SESSION_OK) {
668 fprintf(stderr, "[error] Error detaching viewer session\n");
669 ret = -1;
670 goto error;
671 }
672 ret = 0;
673
674 error:
675 return ret;
676 }
677
678 int main(int argc, char **argv)
679 {
680 int ret;
681 uint64_t session_id;
682
683 plan_tests(NUM_TESTS);
684
685 diag("Live unit tests");
686
687 ret = connect_viewer("localhost");
688 ok(ret == 0, "Connect viewer to relayd");
689
690 ret = establish_connection();
691 ok(ret == 0, "Established connection and version check with %d.%d",
692 VERSION_MAJOR, VERSION_MINOR);
693
694 ret = list_sessions(&session_id);
695 ok(ret > 0, "List sessions : %d session(s)", ret);
696
697 ret = create_viewer_session();
698 ok(ret == 0, "Create viewer session");
699
700 ret = attach_session(session_id);
701 ok(ret > 0, "Attach to session, %d stream(s) received", ret);
702
703 ret = get_metadata();
704 ok(ret > 0, "Get metadata, received %d bytes", ret);
705
706 ret = get_next_index();
707 ok(ret == 0, "Get one index per stream");
708
709 ret = get_data_packet(first_packet_stream_id, first_packet_offset,
710 first_packet_len);
711 ok(ret == 0,
712 "Get one data packet for stream %d, offset %d, len %d",
713 first_packet_stream_id, first_packet_offset,
714 first_packet_len);
715
716 ret = detach_viewer_session(session_id);
717 ok(ret == 0, "Detach viewer session");
718
719 ret = list_sessions(&session_id);
720 ok(ret > 0, "List sessions : %d session(s)", ret);
721
722 ret = attach_session(session_id);
723 ok(ret > 0, "Attach to session, %d streams received", ret);
724
725 return exit_status();
726 }
This page took 0.069894 seconds and 4 git commands to generate.