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