Commit | Line | Data |
---|---|---|
1b368955 JD |
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 | ||
1b368955 JD |
18 | #include <assert.h> |
19 | #include <errno.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <unistd.h> | |
389fbf04 | 24 | #include <common/compat/time.h> |
1b368955 JD |
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> | |
1b368955 JD |
39 | #include <common/common.h> |
40 | ||
2f8f53af | 41 | #include <bin/lttng-relayd/lttng-viewer-abi.h> |
50adc264 | 42 | #include <common/index/ctf-index.h> |
1b368955 | 43 | |
f263b7fd JD |
44 | #include <common/compat/endian.h> |
45 | ||
1b368955 JD |
46 | #define SESSION1 "test1" |
47 | #define RELAYD_URL "net://localhost" | |
48 | #define LIVE_TIMER 2000000 | |
49 | ||
50 | /* Number of TAP tests in this file */ | |
e22ba966 | 51 | #define NUM_TESTS 11 |
1b368955 JD |
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; | |
7409dd34 | 62 | static int first_packet_stream_id = -1; |
1b368955 JD |
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 | ||
10236918 MD |
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 | ||
1b368955 JD |
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) { | |
6f04ed72 | 126 | PERROR("Socket"); |
1b368955 JD |
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) { | |
6f04ed72 | 138 | PERROR("Connect"); |
1b368955 JD |
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; | |
10236918 | 158 | ssize_t ret_len; |
1b368955 | 159 | |
c4e361a4 | 160 | cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT); |
2dc797f3 JD |
161 | cmd.data_size = htobe64(sizeof(connect)); |
162 | cmd.cmd_version = htobe32(0); | |
1b368955 | 163 | |
dcf5c25e | 164 | memset(&connect, 0, sizeof(connect)); |
1b368955 JD |
165 | connect.major = htobe32(VERSION_MAJOR); |
166 | connect.minor = htobe32(VERSION_MINOR); | |
c4e361a4 | 167 | connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND); |
1b368955 | 168 | |
10236918 MD |
169 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
170 | if (ret_len < 0) { | |
7409dd34 | 171 | diag("Error sending cmd"); |
1b368955 JD |
172 | goto error; |
173 | } | |
10236918 MD |
174 | ret_len = lttng_live_send(control_sock, &connect, sizeof(connect)); |
175 | if (ret_len < 0) { | |
7409dd34 | 176 | diag("Error sending version"); |
1b368955 JD |
177 | goto error; |
178 | } | |
179 | ||
10236918 MD |
180 | ret_len = lttng_live_recv(control_sock, &connect, sizeof(connect)); |
181 | if (ret_len == 0) { | |
7409dd34 | 182 | diag("[error] Remote side has closed connection"); |
10236918 MD |
183 | goto error; |
184 | } | |
185 | if (ret_len < 0) { | |
7409dd34 | 186 | diag("Error receiving version"); |
1b368955 JD |
187 | goto error; |
188 | } | |
10236918 | 189 | return 0; |
1b368955 JD |
190 | |
191 | error: | |
10236918 | 192 | return -1; |
1b368955 JD |
193 | } |
194 | ||
195 | /* | |
196 | * Returns the number of sessions, should be 1 during the unit test. | |
197 | */ | |
7409dd34 | 198 | int list_sessions(uint64_t *session_id) |
1b368955 JD |
199 | { |
200 | struct lttng_viewer_cmd cmd; | |
201 | struct lttng_viewer_list_sessions list; | |
202 | struct lttng_viewer_session lsession; | |
10236918 MD |
203 | int i; |
204 | ssize_t ret_len; | |
1b368955 JD |
205 | int first_session = 0; |
206 | ||
c4e361a4 | 207 | cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); |
2dc797f3 JD |
208 | cmd.data_size = htobe64(0); |
209 | cmd.cmd_version = htobe32(0); | |
1b368955 | 210 | |
10236918 MD |
211 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
212 | if (ret_len < 0) { | |
7409dd34 | 213 | diag("Error sending cmd"); |
1b368955 JD |
214 | goto error; |
215 | } | |
216 | ||
10236918 MD |
217 | ret_len = lttng_live_recv(control_sock, &list, sizeof(list)); |
218 | if (ret_len == 0) { | |
7409dd34 | 219 | diag("[error] Remote side has closed connection"); |
10236918 MD |
220 | goto error; |
221 | } | |
222 | if (ret_len < 0) { | |
7409dd34 | 223 | diag("Error receiving session list"); |
1b368955 JD |
224 | goto error; |
225 | } | |
226 | ||
227 | for (i = 0; i < be32toh(list.sessions_count); i++) { | |
10236918 MD |
228 | ret_len = lttng_live_recv(control_sock, &lsession, sizeof(lsession)); |
229 | if (ret_len < 0) { | |
7409dd34 | 230 | diag("Error receiving session"); |
1b368955 JD |
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 | ||
10236918 | 239 | return be32toh(list.sessions_count); |
1b368955 JD |
240 | |
241 | error: | |
10236918 | 242 | return -1; |
1b368955 JD |
243 | } |
244 | ||
10236918 | 245 | int create_viewer_session(void) |
25b50f8f JD |
246 | { |
247 | struct lttng_viewer_cmd cmd; | |
248 | struct lttng_viewer_create_session_response resp; | |
25b50f8f JD |
249 | ssize_t ret_len; |
250 | ||
251 | cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION); | |
2dc797f3 JD |
252 | cmd.data_size = htobe64(0); |
253 | cmd.cmd_version = htobe32(0); | |
25b50f8f | 254 | |
10236918 | 255 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
25b50f8f | 256 | if (ret_len < 0) { |
7409dd34 | 257 | diag("[error] Error sending cmd"); |
25b50f8f JD |
258 | goto error; |
259 | } | |
260 | assert(ret_len == sizeof(cmd)); | |
261 | ||
10236918 MD |
262 | ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp)); |
263 | if (ret_len == 0) { | |
7409dd34 | 264 | diag("[error] Remote side has closed connection"); |
10236918 MD |
265 | goto error; |
266 | } | |
25b50f8f | 267 | if (ret_len < 0) { |
7409dd34 | 268 | diag("[error] Error receiving create session reply"); |
25b50f8f JD |
269 | goto error; |
270 | } | |
271 | assert(ret_len == sizeof(resp)); | |
272 | ||
273 | if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) { | |
7409dd34 | 274 | diag("[error] Error creating viewer session"); |
25b50f8f JD |
275 | goto error; |
276 | } | |
10236918 | 277 | return 0; |
25b50f8f JD |
278 | |
279 | error: | |
10236918 | 280 | return -1; |
25b50f8f JD |
281 | } |
282 | ||
7409dd34 | 283 | int attach_session(uint64_t id) |
1b368955 JD |
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; | |
10236918 MD |
289 | int i; |
290 | ssize_t ret_len; | |
1b368955 JD |
291 | |
292 | session = zmalloc(sizeof(struct live_session)); | |
293 | if (!session) { | |
1b368955 JD |
294 | goto error; |
295 | } | |
296 | ||
c4e361a4 | 297 | cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION); |
2dc797f3 JD |
298 | cmd.data_size = htobe64(sizeof(rq)); |
299 | cmd.cmd_version = htobe32(0); | |
1b368955 | 300 | |
dcf5c25e | 301 | memset(&rq, 0, sizeof(rq)); |
1b368955 | 302 | rq.session_id = htobe64(id); |
c4e361a4 | 303 | rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING); |
1b368955 | 304 | |
10236918 MD |
305 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
306 | if (ret_len < 0) { | |
7409dd34 | 307 | diag("Error sending cmd LTTNG_VIEWER_ATTACH_SESSION"); |
1b368955 JD |
308 | goto error; |
309 | } | |
10236918 MD |
310 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
311 | if (ret_len < 0) { | |
7409dd34 | 312 | diag("Error sending attach request"); |
1b368955 JD |
313 | goto error; |
314 | } | |
315 | ||
10236918 MD |
316 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
317 | if (ret_len == 0) { | |
7409dd34 | 318 | diag("[error] Remote side has closed connection"); |
10236918 MD |
319 | goto error; |
320 | } | |
321 | if (ret_len < 0) { | |
7409dd34 | 322 | diag("Error receiving attach response"); |
1b368955 JD |
323 | goto error; |
324 | } | |
c4e361a4 | 325 | if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) { |
10236918 | 326 | goto error; |
1b368955 JD |
327 | } |
328 | ||
329 | session->stream_count = be32toh(rp.streams_count); | |
7409dd34 JG |
330 | if (session->stream_count == 0) { |
331 | diag("Got session stream count == 0"); | |
332 | goto error; | |
333 | } | |
1b368955 JD |
334 | session->streams = zmalloc(session->stream_count * |
335 | sizeof(struct viewer_stream)); | |
336 | if (!session->streams) { | |
1b368955 JD |
337 | goto error; |
338 | } | |
339 | ||
340 | for (i = 0; i < be32toh(rp.streams_count); i++) { | |
10236918 MD |
341 | ret_len = lttng_live_recv(control_sock, &stream, sizeof(stream)); |
342 | if (ret_len == 0) { | |
7409dd34 | 343 | diag("[error] Remote side has closed connection"); |
10236918 MD |
344 | goto error; |
345 | } | |
346 | if (ret_len < 0) { | |
7409dd34 | 347 | diag("Error receiving stream"); |
1b368955 JD |
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; | |
7409dd34 JG |
354 | session->streams[i].mmap_base = mmap(NULL, mmap_size, |
355 | PROT_READ | PROT_WRITE, | |
1b368955 JD |
356 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
357 | if (session->streams[i].mmap_base == MAP_FAILED) { | |
7409dd34 | 358 | diag("mmap error"); |
1b368955 JD |
359 | goto error; |
360 | } | |
361 | ||
362 | if (be32toh(stream.metadata_flag)) { | |
363 | session->streams[i].metadata_flag = 1; | |
364 | } | |
365 | } | |
10236918 | 366 | return session->stream_count; |
1b368955 | 367 | |
1b368955 | 368 | error: |
10236918 | 369 | return -1; |
1b368955 JD |
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; | |
10236918 | 377 | ssize_t ret_len; |
1b368955 JD |
378 | int ret; |
379 | uint64_t i; | |
380 | char *data = NULL; | |
381 | uint64_t len = 0; | |
382 | int metadata_stream_id = -1; | |
383 | ||
c4e361a4 | 384 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA); |
2dc797f3 JD |
385 | cmd.data_size = htobe64(sizeof(rq)); |
386 | cmd.cmd_version = htobe32(0); | |
1b368955 JD |
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) { | |
7409dd34 | 396 | diag("No metadata stream found"); |
1b368955 JD |
397 | goto error; |
398 | } | |
399 | ||
400 | rq.stream_id = htobe64(session->streams[metadata_stream_id].id); | |
401 | ||
a648b489 | 402 | retry: |
10236918 MD |
403 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
404 | if (ret_len < 0) { | |
7409dd34 | 405 | diag("Error sending cmd"); |
1b368955 JD |
406 | goto error; |
407 | } | |
10236918 MD |
408 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
409 | if (ret_len < 0) { | |
7409dd34 | 410 | diag("Error sending get_metadata request"); |
1b368955 JD |
411 | goto error; |
412 | } | |
10236918 MD |
413 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
414 | if (ret_len == 0) { | |
7409dd34 | 415 | diag("[error] Remote side has closed connection"); |
10236918 MD |
416 | goto error; |
417 | } | |
418 | if (ret_len < 0) { | |
7409dd34 | 419 | diag("Error receiving metadata response"); |
1b368955 JD |
420 | goto error; |
421 | } | |
422 | switch (be32toh(rp.status)) { | |
7409dd34 JG |
423 | case LTTNG_VIEWER_METADATA_OK: |
424 | break; | |
425 | case LTTNG_VIEWER_NO_NEW_METADATA: | |
426 | diag("Got LTTNG_VIEWER_NO_NEW_METADATA:"); | |
a648b489 JR |
427 | usleep(50); |
428 | goto retry; | |
7409dd34 JG |
429 | case LTTNG_VIEWER_METADATA_ERR: |
430 | diag("Got LTTNG_VIEWER_METADATA_ERR:"); | |
431 | goto error; | |
432 | default: | |
433 | diag("Got unknown status during LTTNG_VIEWER_GET_METADATA"); | |
434 | goto error; | |
1b368955 JD |
435 | } |
436 | ||
437 | len = be64toh(rp.len); | |
438 | if (len <= 0) { | |
10236918 | 439 | goto error; |
1b368955 JD |
440 | } |
441 | ||
442 | data = zmalloc(len); | |
443 | if (!data) { | |
6f04ed72 | 444 | PERROR("relay data zmalloc"); |
1b368955 JD |
445 | goto error; |
446 | } | |
10236918 MD |
447 | ret_len = lttng_live_recv(control_sock, data, len); |
448 | if (ret_len == 0) { | |
7409dd34 | 449 | diag("[error] Remote side has closed connection"); |
10236918 MD |
450 | goto error_free_data; |
451 | } | |
452 | if (ret_len < 0) { | |
7409dd34 | 453 | diag("Error receiving trace packet"); |
10236918 | 454 | goto error_free_data; |
1b368955 JD |
455 | } |
456 | free(data); | |
10236918 | 457 | ret = len; |
a648b489 | 458 | |
1b368955 | 459 | return ret; |
10236918 MD |
460 | |
461 | error_free_data: | |
462 | free(data); | |
463 | error: | |
464 | return -1; | |
1b368955 JD |
465 | } |
466 | ||
467 | int get_next_index(void) | |
468 | { | |
469 | struct lttng_viewer_cmd cmd; | |
470 | struct lttng_viewer_get_next_index rq; | |
471 | struct lttng_viewer_index rp; | |
10236918 | 472 | ssize_t ret_len; |
1b368955 JD |
473 | int id; |
474 | ||
c4e361a4 | 475 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX); |
2dc797f3 JD |
476 | cmd.data_size = htobe64(sizeof(rq)); |
477 | cmd.cmd_version = htobe32(0); | |
1b368955 JD |
478 | |
479 | for (id = 0; id < session->stream_count; id++) { | |
480 | if (session->streams[id].metadata_flag) { | |
481 | continue; | |
482 | } | |
10236918 | 483 | memset(&rq, 0, sizeof(rq)); |
1b368955 JD |
484 | rq.stream_id = htobe64(session->streams[id].id); |
485 | ||
486 | retry: | |
10236918 MD |
487 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
488 | if (ret_len < 0) { | |
7409dd34 | 489 | diag("Error sending cmd"); |
1b368955 JD |
490 | goto error; |
491 | } | |
10236918 MD |
492 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
493 | if (ret_len < 0) { | |
7409dd34 | 494 | diag("Error sending get_next_index request"); |
1b368955 JD |
495 | goto error; |
496 | } | |
10236918 MD |
497 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
498 | if (ret_len == 0) { | |
7409dd34 | 499 | diag("[error] Remote side has closed connection"); |
10236918 MD |
500 | goto error; |
501 | } | |
502 | if (ret_len < 0) { | |
7409dd34 | 503 | diag("Error receiving index response"); |
1b368955 JD |
504 | goto error; |
505 | } | |
506 | ||
507 | rp.flags = be32toh(rp.flags); | |
508 | ||
509 | switch (be32toh(rp.status)) { | |
7409dd34 JG |
510 | case LTTNG_VIEWER_INDEX_INACTIVE: |
511 | /* Skip this stream. */ | |
512 | diag("Got LTTNG_VIEWER_INDEX_INACTIVE"); | |
513 | continue; | |
514 | case LTTNG_VIEWER_INDEX_OK: | |
515 | break; | |
516 | case LTTNG_VIEWER_INDEX_RETRY: | |
517 | sleep(1); | |
518 | goto 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; | |
523 | goto error; | |
524 | case LTTNG_VIEWER_INDEX_ERR: | |
525 | diag("Got LTTNG_VIEWER_INDEX_ERR"); | |
526 | goto error; | |
527 | default: | |
528 | diag("Unknown reply status during LTTNG_VIEWER_GET_NEXT_INDEX (%d)", be32toh(rp.status)); | |
529 | goto error; | |
1b368955 | 530 | } |
7409dd34 JG |
531 | if (first_packet_stream_id < 0) { |
532 | /* | |
533 | * Initialize the first packet stream id. That is, | |
534 | * the first active stream encoutered. | |
535 | */ | |
1b368955 JD |
536 | first_packet_offset = be64toh(rp.offset); |
537 | first_packet_len = be64toh(rp.packet_size) / CHAR_BIT; | |
538 | first_packet_stream_id = id; | |
7409dd34 JG |
539 | diag("Got first packet index with offset %d and len %d", |
540 | first_packet_offset, first_packet_len); | |
1b368955 JD |
541 | } |
542 | } | |
10236918 | 543 | return 0; |
1b368955 JD |
544 | |
545 | error: | |
10236918 | 546 | return -1; |
1b368955 JD |
547 | } |
548 | ||
549 | static | |
550 | int get_data_packet(int id, uint64_t offset, | |
551 | uint64_t len) | |
552 | { | |
553 | struct lttng_viewer_cmd cmd; | |
554 | struct lttng_viewer_get_packet rq; | |
555 | struct lttng_viewer_trace_packet rp; | |
10236918 | 556 | ssize_t ret_len; |
1b368955 | 557 | |
c4e361a4 | 558 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET); |
2dc797f3 JD |
559 | cmd.data_size = htobe64(sizeof(rq)); |
560 | cmd.cmd_version = htobe32(0); | |
1b368955 | 561 | |
10236918 | 562 | memset(&rq, 0, sizeof(rq)); |
1b368955 JD |
563 | rq.stream_id = htobe64(session->streams[id].id); |
564 | /* Already in big endian. */ | |
565 | rq.offset = offset; | |
566 | rq.len = htobe32(len); | |
567 | ||
10236918 MD |
568 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
569 | if (ret_len < 0) { | |
7409dd34 | 570 | diag("Error sending cmd"); |
1b368955 JD |
571 | goto error; |
572 | } | |
10236918 MD |
573 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
574 | if (ret_len < 0) { | |
7409dd34 | 575 | diag("Error sending get_data_packet request"); |
1b368955 JD |
576 | goto error; |
577 | } | |
10236918 MD |
578 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
579 | if (ret_len == 0) { | |
7409dd34 | 580 | diag("[error] Remote side has closed connection"); |
10236918 MD |
581 | goto error; |
582 | } | |
583 | if (ret_len < 0) { | |
7409dd34 | 584 | diag("Error receiving data response"); |
1b368955 JD |
585 | goto error; |
586 | } | |
587 | rp.flags = be32toh(rp.flags); | |
588 | ||
589 | switch (be32toh(rp.status)) { | |
c4e361a4 | 590 | case LTTNG_VIEWER_GET_PACKET_OK: |
10236918 | 591 | len = be32toh(rp.len); |
7409dd34 JG |
592 | if (len == 0) { |
593 | diag("Got LTTNG_VIEWER_GET_PACKET_OK, but len == 0"); | |
594 | goto error; | |
595 | } | |
1b368955 | 596 | break; |
c4e361a4 | 597 | case LTTNG_VIEWER_GET_PACKET_RETRY: |
7409dd34 | 598 | diag("Got LTTNG_VIEWER_GET_PACKET_RETRY:"); |
10236918 | 599 | goto error; |
c4e361a4 | 600 | case LTTNG_VIEWER_GET_PACKET_ERR: |
1b368955 | 601 | if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { |
7409dd34 | 602 | diag("Got LTTNG_VIEWER_GET_PACKET_ERR with NEW_METADATA flag"); |
1b368955 JD |
603 | goto end; |
604 | } | |
7409dd34 | 605 | diag("Got LTTNG_VIEWER_GET_PACKET_ERR:"); |
10236918 | 606 | goto error; |
1b368955 | 607 | default: |
7409dd34 | 608 | diag("Got unknown status code during LTTNG_VIEWER_GET_PACKET"); |
10236918 | 609 | goto error; |
1b368955 JD |
610 | } |
611 | ||
612 | if (len > mmap_size) { | |
7409dd34 | 613 | diag("mmap_size not big enough"); |
1b368955 JD |
614 | goto error; |
615 | } | |
616 | ||
10236918 MD |
617 | ret_len = lttng_live_recv(control_sock, session->streams[id].mmap_base, len); |
618 | if (ret_len == 0) { | |
7409dd34 | 619 | diag("[error] Remote side has closed connection"); |
10236918 MD |
620 | goto error; |
621 | } | |
622 | if (ret_len < 0) { | |
7409dd34 | 623 | diag("Error receiving trace packet"); |
1b368955 JD |
624 | goto error; |
625 | } | |
1b368955 | 626 | end: |
10236918 | 627 | return 0; |
1b368955 | 628 | error: |
10236918 | 629 | return -1; |
1b368955 JD |
630 | } |
631 | ||
e22ba966 JD |
632 | int detach_viewer_session(uint64_t id) |
633 | { | |
634 | struct lttng_viewer_cmd cmd; | |
635 | struct lttng_viewer_detach_session_response resp; | |
636 | struct lttng_viewer_detach_session_request rq; | |
637 | int ret; | |
638 | ssize_t ret_len; | |
639 | ||
640 | cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION); | |
2dc797f3 JD |
641 | cmd.data_size = htobe64(sizeof(rq)); |
642 | cmd.cmd_version = htobe32(0); | |
e22ba966 JD |
643 | |
644 | memset(&rq, 0, sizeof(rq)); | |
645 | rq.session_id = htobe64(id); | |
646 | ||
647 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); | |
648 | if (ret_len < 0) { | |
649 | fprintf(stderr, "[error] Error sending cmd\n"); | |
650 | ret = ret_len; | |
651 | goto error; | |
652 | } | |
653 | ||
654 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); | |
655 | if (ret_len < 0) { | |
656 | fprintf(stderr, "Error sending attach request\n"); | |
657 | ret = ret_len; | |
658 | goto error; | |
659 | } | |
660 | ||
661 | ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp)); | |
662 | if (ret_len < 0) { | |
663 | fprintf(stderr, "[error] Error receiving detach session reply\n"); | |
664 | ret = ret_len; | |
665 | goto error; | |
666 | } | |
667 | ||
668 | if (be32toh(resp.status) != LTTNG_VIEWER_DETACH_SESSION_OK) { | |
669 | fprintf(stderr, "[error] Error detaching viewer session\n"); | |
670 | ret = -1; | |
671 | goto error; | |
672 | } | |
673 | ret = 0; | |
674 | ||
675 | error: | |
676 | return ret; | |
677 | } | |
678 | ||
1b368955 JD |
679 | int main(int argc, char **argv) |
680 | { | |
681 | int ret; | |
7409dd34 | 682 | uint64_t session_id; |
1b368955 JD |
683 | |
684 | plan_tests(NUM_TESTS); | |
685 | ||
686 | diag("Live unit tests"); | |
687 | ||
688 | ret = connect_viewer("localhost"); | |
689 | ok(ret == 0, "Connect viewer to relayd"); | |
690 | ||
691 | ret = establish_connection(); | |
692 | ok(ret == 0, "Established connection and version check with %d.%d", | |
693 | VERSION_MAJOR, VERSION_MINOR); | |
694 | ||
695 | ret = list_sessions(&session_id); | |
696 | ok(ret > 0, "List sessions : %d session(s)", ret); | |
8674c9bf JG |
697 | if (ret < 0) { |
698 | goto end; | |
699 | } | |
1b368955 | 700 | |
25b50f8f JD |
701 | ret = create_viewer_session(); |
702 | ok(ret == 0, "Create viewer session"); | |
703 | ||
1b368955 | 704 | ret = attach_session(session_id); |
7409dd34 | 705 | ok(ret > 0, "Attach to session, %d stream(s) received", ret); |
1b368955 JD |
706 | |
707 | ret = get_metadata(); | |
708 | ok(ret > 0, "Get metadata, received %d bytes", ret); | |
709 | ||
710 | ret = get_next_index(); | |
711 | ok(ret == 0, "Get one index per stream"); | |
712 | ||
713 | ret = get_data_packet(first_packet_stream_id, first_packet_offset, | |
714 | first_packet_len); | |
10236918 | 715 | ok(ret == 0, |
1b368955 JD |
716 | "Get one data packet for stream %d, offset %d, len %d", |
717 | first_packet_stream_id, first_packet_offset, | |
718 | first_packet_len); | |
719 | ||
e22ba966 JD |
720 | ret = detach_viewer_session(session_id); |
721 | ok(ret == 0, "Detach viewer session"); | |
722 | ||
723 | ret = list_sessions(&session_id); | |
724 | ok(ret > 0, "List sessions : %d session(s)", ret); | |
725 | ||
726 | ret = attach_session(session_id); | |
727 | ok(ret > 0, "Attach to session, %d streams received", ret); | |
8674c9bf | 728 | end: |
1b368955 JD |
729 | return exit_status(); |
730 | } |