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 | ||
1b368955 JD |
54 | static int control_sock; |
55 | struct live_session *session; | |
56 | ||
57 | static int first_packet_offset; | |
58 | static int first_packet_len; | |
7409dd34 | 59 | static int first_packet_stream_id = -1; |
1b368955 JD |
60 | |
61 | struct viewer_stream { | |
62 | uint64_t id; | |
63 | uint64_t ctf_trace_id; | |
64 | void *mmap_base; | |
65 | int fd; | |
66 | int metadata_flag; | |
67 | int first_read; | |
68 | char path[PATH_MAX]; | |
69 | }; | |
70 | ||
71 | struct live_session { | |
72 | struct viewer_stream *streams; | |
73 | uint64_t live_timer_interval; | |
74 | uint64_t stream_count; | |
75 | }; | |
76 | ||
10236918 MD |
77 | static |
78 | ssize_t lttng_live_recv(int fd, void *buf, size_t len) | |
79 | { | |
80 | ssize_t ret; | |
81 | size_t copied = 0, to_copy = len; | |
82 | ||
83 | do { | |
84 | ret = recv(fd, buf + copied, to_copy, 0); | |
85 | if (ret > 0) { | |
86 | assert(ret <= to_copy); | |
87 | copied += ret; | |
88 | to_copy -= ret; | |
89 | } | |
90 | } while ((ret > 0 && to_copy > 0) | |
91 | || (ret < 0 && errno == EINTR)); | |
92 | if (ret > 0) | |
93 | ret = copied; | |
94 | /* ret = 0 means orderly shutdown, ret < 0 is error. */ | |
95 | return ret; | |
96 | } | |
97 | ||
98 | static | |
99 | ssize_t lttng_live_send(int fd, const void *buf, size_t len) | |
100 | { | |
101 | ssize_t ret; | |
102 | ||
103 | do { | |
104 | ret = send(fd, buf, len, MSG_NOSIGNAL); | |
105 | } while (ret < 0 && errno == EINTR); | |
106 | return ret; | |
107 | } | |
108 | ||
1b368955 JD |
109 | static |
110 | int connect_viewer(char *hostname) | |
111 | { | |
112 | struct hostent *host; | |
113 | struct sockaddr_in server_addr; | |
114 | int ret; | |
115 | ||
116 | host = gethostbyname(hostname); | |
117 | if (!host) { | |
118 | ret = -1; | |
119 | goto end; | |
120 | } | |
121 | ||
122 | if ((control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
6f04ed72 | 123 | PERROR("Socket"); |
1b368955 JD |
124 | ret = -1; |
125 | goto end; | |
126 | } | |
127 | ||
128 | server_addr.sin_family = AF_INET; | |
129 | server_addr.sin_port = htons(5344); | |
130 | server_addr.sin_addr = *((struct in_addr *) host->h_addr); | |
131 | bzero(&(server_addr.sin_zero), 8); | |
132 | ||
133 | if (connect(control_sock, (struct sockaddr *) &server_addr, | |
134 | sizeof(struct sockaddr)) == -1) { | |
6f04ed72 | 135 | PERROR("Connect"); |
1b368955 JD |
136 | ret = -1; |
137 | goto end; | |
138 | } | |
139 | ||
140 | server_addr.sin_family = AF_INET; | |
141 | server_addr.sin_port = htons(5345); | |
142 | server_addr.sin_addr = *((struct in_addr *) host->h_addr); | |
143 | bzero(&(server_addr.sin_zero), 8); | |
144 | ||
145 | ret = 0; | |
146 | ||
147 | end: | |
148 | return ret; | |
149 | } | |
150 | ||
151 | int establish_connection(void) | |
152 | { | |
153 | struct lttng_viewer_cmd cmd; | |
154 | struct lttng_viewer_connect connect; | |
10236918 | 155 | ssize_t ret_len; |
1b368955 | 156 | |
c4e361a4 | 157 | cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT); |
2dc797f3 JD |
158 | cmd.data_size = htobe64(sizeof(connect)); |
159 | cmd.cmd_version = htobe32(0); | |
1b368955 | 160 | |
dcf5c25e | 161 | memset(&connect, 0, sizeof(connect)); |
1b368955 JD |
162 | connect.major = htobe32(VERSION_MAJOR); |
163 | connect.minor = htobe32(VERSION_MINOR); | |
c4e361a4 | 164 | connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND); |
1b368955 | 165 | |
10236918 MD |
166 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
167 | if (ret_len < 0) { | |
7409dd34 | 168 | diag("Error sending cmd"); |
1b368955 JD |
169 | goto error; |
170 | } | |
10236918 MD |
171 | ret_len = lttng_live_send(control_sock, &connect, sizeof(connect)); |
172 | if (ret_len < 0) { | |
7409dd34 | 173 | diag("Error sending version"); |
1b368955 JD |
174 | goto error; |
175 | } | |
176 | ||
10236918 MD |
177 | ret_len = lttng_live_recv(control_sock, &connect, sizeof(connect)); |
178 | if (ret_len == 0) { | |
7409dd34 | 179 | diag("[error] Remote side has closed connection"); |
10236918 MD |
180 | goto error; |
181 | } | |
182 | if (ret_len < 0) { | |
7409dd34 | 183 | diag("Error receiving version"); |
1b368955 JD |
184 | goto error; |
185 | } | |
10236918 | 186 | return 0; |
1b368955 JD |
187 | |
188 | error: | |
10236918 | 189 | return -1; |
1b368955 JD |
190 | } |
191 | ||
192 | /* | |
193 | * Returns the number of sessions, should be 1 during the unit test. | |
194 | */ | |
7409dd34 | 195 | int list_sessions(uint64_t *session_id) |
1b368955 JD |
196 | { |
197 | struct lttng_viewer_cmd cmd; | |
198 | struct lttng_viewer_list_sessions list; | |
199 | struct lttng_viewer_session lsession; | |
10236918 MD |
200 | int i; |
201 | ssize_t ret_len; | |
1b368955 JD |
202 | int first_session = 0; |
203 | ||
c4e361a4 | 204 | cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); |
2dc797f3 JD |
205 | cmd.data_size = htobe64(0); |
206 | cmd.cmd_version = htobe32(0); | |
1b368955 | 207 | |
10236918 MD |
208 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
209 | if (ret_len < 0) { | |
7409dd34 | 210 | diag("Error sending cmd"); |
1b368955 JD |
211 | goto error; |
212 | } | |
213 | ||
10236918 MD |
214 | ret_len = lttng_live_recv(control_sock, &list, sizeof(list)); |
215 | if (ret_len == 0) { | |
7409dd34 | 216 | diag("[error] Remote side has closed connection"); |
10236918 MD |
217 | goto error; |
218 | } | |
219 | if (ret_len < 0) { | |
7409dd34 | 220 | diag("Error receiving session list"); |
1b368955 JD |
221 | goto error; |
222 | } | |
223 | ||
224 | for (i = 0; i < be32toh(list.sessions_count); i++) { | |
10236918 MD |
225 | ret_len = lttng_live_recv(control_sock, &lsession, sizeof(lsession)); |
226 | if (ret_len < 0) { | |
7409dd34 | 227 | diag("Error receiving session"); |
1b368955 JD |
228 | goto error; |
229 | } | |
230 | if (lsession.streams > 0 && first_session <= 0) { | |
231 | first_session = be64toh(lsession.id); | |
232 | *session_id = first_session; | |
233 | } | |
234 | } | |
235 | ||
10236918 | 236 | return be32toh(list.sessions_count); |
1b368955 JD |
237 | |
238 | error: | |
10236918 | 239 | return -1; |
1b368955 JD |
240 | } |
241 | ||
10236918 | 242 | int create_viewer_session(void) |
25b50f8f JD |
243 | { |
244 | struct lttng_viewer_cmd cmd; | |
245 | struct lttng_viewer_create_session_response resp; | |
25b50f8f JD |
246 | ssize_t ret_len; |
247 | ||
248 | cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION); | |
2dc797f3 JD |
249 | cmd.data_size = htobe64(0); |
250 | cmd.cmd_version = htobe32(0); | |
25b50f8f | 251 | |
10236918 | 252 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
25b50f8f | 253 | if (ret_len < 0) { |
7409dd34 | 254 | diag("[error] Error sending cmd"); |
25b50f8f JD |
255 | goto error; |
256 | } | |
257 | assert(ret_len == sizeof(cmd)); | |
258 | ||
10236918 MD |
259 | ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp)); |
260 | if (ret_len == 0) { | |
7409dd34 | 261 | diag("[error] Remote side has closed connection"); |
10236918 MD |
262 | goto error; |
263 | } | |
25b50f8f | 264 | if (ret_len < 0) { |
7409dd34 | 265 | diag("[error] Error receiving create session reply"); |
25b50f8f JD |
266 | goto error; |
267 | } | |
268 | assert(ret_len == sizeof(resp)); | |
269 | ||
270 | if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) { | |
7409dd34 | 271 | diag("[error] Error creating viewer session"); |
25b50f8f JD |
272 | goto error; |
273 | } | |
10236918 | 274 | return 0; |
25b50f8f JD |
275 | |
276 | error: | |
10236918 | 277 | return -1; |
25b50f8f JD |
278 | } |
279 | ||
7409dd34 | 280 | int attach_session(uint64_t id) |
1b368955 JD |
281 | { |
282 | struct lttng_viewer_cmd cmd; | |
283 | struct lttng_viewer_attach_session_request rq; | |
284 | struct lttng_viewer_attach_session_response rp; | |
285 | struct lttng_viewer_stream stream; | |
10236918 MD |
286 | int i; |
287 | ssize_t ret_len; | |
1b368955 JD |
288 | |
289 | session = zmalloc(sizeof(struct live_session)); | |
290 | if (!session) { | |
1b368955 JD |
291 | goto error; |
292 | } | |
293 | ||
c4e361a4 | 294 | cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION); |
2dc797f3 JD |
295 | cmd.data_size = htobe64(sizeof(rq)); |
296 | cmd.cmd_version = htobe32(0); | |
1b368955 | 297 | |
dcf5c25e | 298 | memset(&rq, 0, sizeof(rq)); |
1b368955 | 299 | rq.session_id = htobe64(id); |
c4e361a4 | 300 | rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING); |
1b368955 | 301 | |
10236918 MD |
302 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
303 | if (ret_len < 0) { | |
7409dd34 | 304 | diag("Error sending cmd LTTNG_VIEWER_ATTACH_SESSION"); |
1b368955 JD |
305 | goto error; |
306 | } | |
10236918 MD |
307 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
308 | if (ret_len < 0) { | |
7409dd34 | 309 | diag("Error sending attach request"); |
1b368955 JD |
310 | goto error; |
311 | } | |
312 | ||
10236918 MD |
313 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
314 | if (ret_len == 0) { | |
7409dd34 | 315 | diag("[error] Remote side has closed connection"); |
10236918 MD |
316 | goto error; |
317 | } | |
318 | if (ret_len < 0) { | |
7409dd34 | 319 | diag("Error receiving attach response"); |
1b368955 JD |
320 | goto error; |
321 | } | |
c4e361a4 | 322 | if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) { |
10236918 | 323 | goto error; |
1b368955 JD |
324 | } |
325 | ||
326 | session->stream_count = be32toh(rp.streams_count); | |
7409dd34 JG |
327 | if (session->stream_count == 0) { |
328 | diag("Got session stream count == 0"); | |
329 | goto error; | |
330 | } | |
1b368955 JD |
331 | session->streams = zmalloc(session->stream_count * |
332 | sizeof(struct viewer_stream)); | |
333 | if (!session->streams) { | |
1b368955 JD |
334 | goto error; |
335 | } | |
336 | ||
337 | for (i = 0; i < be32toh(rp.streams_count); i++) { | |
10236918 MD |
338 | ret_len = lttng_live_recv(control_sock, &stream, sizeof(stream)); |
339 | if (ret_len == 0) { | |
7409dd34 | 340 | diag("[error] Remote side has closed connection"); |
10236918 MD |
341 | goto error; |
342 | } | |
343 | if (ret_len < 0) { | |
7409dd34 | 344 | diag("Error receiving stream"); |
1b368955 JD |
345 | goto error; |
346 | } | |
347 | session->streams[i].id = be64toh(stream.id); | |
348 | ||
349 | session->streams[i].ctf_trace_id = be64toh(stream.ctf_trace_id); | |
350 | session->streams[i].first_read = 1; | |
7409dd34 JG |
351 | session->streams[i].mmap_base = mmap(NULL, mmap_size, |
352 | PROT_READ | PROT_WRITE, | |
1b368955 JD |
353 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
354 | if (session->streams[i].mmap_base == MAP_FAILED) { | |
7409dd34 | 355 | diag("mmap error"); |
1b368955 JD |
356 | goto error; |
357 | } | |
358 | ||
359 | if (be32toh(stream.metadata_flag)) { | |
360 | session->streams[i].metadata_flag = 1; | |
361 | } | |
362 | } | |
10236918 | 363 | return session->stream_count; |
1b368955 | 364 | |
1b368955 | 365 | error: |
10236918 | 366 | return -1; |
1b368955 JD |
367 | } |
368 | ||
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 | ||
439 | data = zmalloc(len); | |
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 | ||
464 | int get_next_index(void) | |
465 | { | |
466 | struct lttng_viewer_cmd cmd; | |
467 | struct lttng_viewer_get_next_index rq; | |
468 | struct lttng_viewer_index rp; | |
10236918 | 469 | ssize_t ret_len; |
1b368955 JD |
470 | int id; |
471 | ||
c4e361a4 | 472 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX); |
2dc797f3 JD |
473 | cmd.data_size = htobe64(sizeof(rq)); |
474 | cmd.cmd_version = htobe32(0); | |
1b368955 JD |
475 | |
476 | for (id = 0; id < session->stream_count; id++) { | |
477 | if (session->streams[id].metadata_flag) { | |
478 | continue; | |
479 | } | |
10236918 | 480 | memset(&rq, 0, sizeof(rq)); |
1b368955 JD |
481 | rq.stream_id = htobe64(session->streams[id].id); |
482 | ||
483 | retry: | |
10236918 MD |
484 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
485 | if (ret_len < 0) { | |
7409dd34 | 486 | diag("Error sending cmd"); |
1b368955 JD |
487 | goto error; |
488 | } | |
10236918 MD |
489 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
490 | if (ret_len < 0) { | |
7409dd34 | 491 | diag("Error sending get_next_index request"); |
1b368955 JD |
492 | goto error; |
493 | } | |
10236918 MD |
494 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
495 | if (ret_len == 0) { | |
7409dd34 | 496 | diag("[error] Remote side has closed connection"); |
10236918 MD |
497 | goto error; |
498 | } | |
499 | if (ret_len < 0) { | |
7409dd34 | 500 | diag("Error receiving index response"); |
1b368955 JD |
501 | goto error; |
502 | } | |
503 | ||
504 | rp.flags = be32toh(rp.flags); | |
505 | ||
506 | switch (be32toh(rp.status)) { | |
7409dd34 JG |
507 | case LTTNG_VIEWER_INDEX_INACTIVE: |
508 | /* Skip this stream. */ | |
509 | diag("Got LTTNG_VIEWER_INDEX_INACTIVE"); | |
510 | continue; | |
511 | case LTTNG_VIEWER_INDEX_OK: | |
512 | break; | |
513 | case LTTNG_VIEWER_INDEX_RETRY: | |
514 | sleep(1); | |
515 | goto retry; | |
516 | case LTTNG_VIEWER_INDEX_HUP: | |
517 | diag("Got LTTNG_VIEWER_INDEX_HUP"); | |
518 | session->streams[id].id = -1ULL; | |
519 | session->streams[id].fd = -1; | |
520 | goto error; | |
521 | case LTTNG_VIEWER_INDEX_ERR: | |
522 | diag("Got LTTNG_VIEWER_INDEX_ERR"); | |
523 | goto error; | |
524 | default: | |
525 | diag("Unknown reply status during LTTNG_VIEWER_GET_NEXT_INDEX (%d)", be32toh(rp.status)); | |
526 | goto error; | |
1b368955 | 527 | } |
7409dd34 JG |
528 | if (first_packet_stream_id < 0) { |
529 | /* | |
530 | * Initialize the first packet stream id. That is, | |
531 | * the first active stream encoutered. | |
532 | */ | |
1b368955 JD |
533 | first_packet_offset = be64toh(rp.offset); |
534 | first_packet_len = be64toh(rp.packet_size) / CHAR_BIT; | |
535 | first_packet_stream_id = id; | |
7409dd34 JG |
536 | diag("Got first packet index with offset %d and len %d", |
537 | first_packet_offset, first_packet_len); | |
1b368955 JD |
538 | } |
539 | } | |
10236918 | 540 | return 0; |
1b368955 JD |
541 | |
542 | error: | |
10236918 | 543 | return -1; |
1b368955 JD |
544 | } |
545 | ||
546 | static | |
547 | int get_data_packet(int id, uint64_t offset, | |
548 | uint64_t len) | |
549 | { | |
550 | struct lttng_viewer_cmd cmd; | |
551 | struct lttng_viewer_get_packet rq; | |
552 | struct lttng_viewer_trace_packet rp; | |
10236918 | 553 | ssize_t ret_len; |
1b368955 | 554 | |
c4e361a4 | 555 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET); |
2dc797f3 JD |
556 | cmd.data_size = htobe64(sizeof(rq)); |
557 | cmd.cmd_version = htobe32(0); | |
1b368955 | 558 | |
10236918 | 559 | memset(&rq, 0, sizeof(rq)); |
1b368955 JD |
560 | rq.stream_id = htobe64(session->streams[id].id); |
561 | /* Already in big endian. */ | |
562 | rq.offset = offset; | |
563 | rq.len = htobe32(len); | |
564 | ||
10236918 MD |
565 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
566 | if (ret_len < 0) { | |
7409dd34 | 567 | diag("Error sending cmd"); |
1b368955 JD |
568 | goto error; |
569 | } | |
10236918 MD |
570 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
571 | if (ret_len < 0) { | |
7409dd34 | 572 | diag("Error sending get_data_packet request"); |
1b368955 JD |
573 | goto error; |
574 | } | |
10236918 MD |
575 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
576 | if (ret_len == 0) { | |
7409dd34 | 577 | diag("[error] Remote side has closed connection"); |
10236918 MD |
578 | goto error; |
579 | } | |
580 | if (ret_len < 0) { | |
7409dd34 | 581 | diag("Error receiving data response"); |
1b368955 JD |
582 | goto error; |
583 | } | |
584 | rp.flags = be32toh(rp.flags); | |
585 | ||
586 | switch (be32toh(rp.status)) { | |
c4e361a4 | 587 | case LTTNG_VIEWER_GET_PACKET_OK: |
10236918 | 588 | len = be32toh(rp.len); |
7409dd34 JG |
589 | if (len == 0) { |
590 | diag("Got LTTNG_VIEWER_GET_PACKET_OK, but len == 0"); | |
591 | goto error; | |
592 | } | |
1b368955 | 593 | break; |
c4e361a4 | 594 | case LTTNG_VIEWER_GET_PACKET_RETRY: |
7409dd34 | 595 | diag("Got LTTNG_VIEWER_GET_PACKET_RETRY:"); |
10236918 | 596 | goto error; |
c4e361a4 | 597 | case LTTNG_VIEWER_GET_PACKET_ERR: |
1b368955 | 598 | if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { |
7409dd34 | 599 | diag("Got LTTNG_VIEWER_GET_PACKET_ERR with NEW_METADATA flag"); |
1b368955 JD |
600 | goto end; |
601 | } | |
7409dd34 | 602 | diag("Got LTTNG_VIEWER_GET_PACKET_ERR:"); |
10236918 | 603 | goto error; |
1b368955 | 604 | default: |
7409dd34 | 605 | diag("Got unknown status code during LTTNG_VIEWER_GET_PACKET"); |
10236918 | 606 | goto error; |
1b368955 JD |
607 | } |
608 | ||
609 | if (len > mmap_size) { | |
7409dd34 | 610 | diag("mmap_size not big enough"); |
1b368955 JD |
611 | goto error; |
612 | } | |
613 | ||
10236918 MD |
614 | ret_len = lttng_live_recv(control_sock, session->streams[id].mmap_base, len); |
615 | if (ret_len == 0) { | |
7409dd34 | 616 | diag("[error] Remote side has closed connection"); |
10236918 MD |
617 | goto error; |
618 | } | |
619 | if (ret_len < 0) { | |
7409dd34 | 620 | diag("Error receiving trace packet"); |
1b368955 JD |
621 | goto error; |
622 | } | |
1b368955 | 623 | end: |
10236918 | 624 | return 0; |
1b368955 | 625 | error: |
10236918 | 626 | return -1; |
1b368955 JD |
627 | } |
628 | ||
e22ba966 JD |
629 | int detach_viewer_session(uint64_t id) |
630 | { | |
631 | struct lttng_viewer_cmd cmd; | |
632 | struct lttng_viewer_detach_session_response resp; | |
633 | struct lttng_viewer_detach_session_request rq; | |
634 | int ret; | |
635 | ssize_t ret_len; | |
636 | ||
637 | cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION); | |
2dc797f3 JD |
638 | cmd.data_size = htobe64(sizeof(rq)); |
639 | cmd.cmd_version = htobe32(0); | |
e22ba966 JD |
640 | |
641 | memset(&rq, 0, sizeof(rq)); | |
642 | rq.session_id = htobe64(id); | |
643 | ||
644 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); | |
645 | if (ret_len < 0) { | |
646 | fprintf(stderr, "[error] Error sending cmd\n"); | |
647 | ret = ret_len; | |
648 | goto error; | |
649 | } | |
650 | ||
651 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); | |
652 | if (ret_len < 0) { | |
653 | fprintf(stderr, "Error sending attach request\n"); | |
654 | ret = ret_len; | |
655 | goto error; | |
656 | } | |
657 | ||
658 | ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp)); | |
659 | if (ret_len < 0) { | |
660 | fprintf(stderr, "[error] Error receiving detach session reply\n"); | |
661 | ret = ret_len; | |
662 | goto error; | |
663 | } | |
664 | ||
665 | if (be32toh(resp.status) != LTTNG_VIEWER_DETACH_SESSION_OK) { | |
666 | fprintf(stderr, "[error] Error detaching viewer session\n"); | |
667 | ret = -1; | |
668 | goto error; | |
669 | } | |
670 | ret = 0; | |
671 | ||
672 | error: | |
673 | return ret; | |
674 | } | |
675 | ||
1b368955 JD |
676 | int main(int argc, char **argv) |
677 | { | |
678 | int ret; | |
7409dd34 | 679 | uint64_t session_id; |
1b368955 JD |
680 | |
681 | plan_tests(NUM_TESTS); | |
682 | ||
683 | diag("Live unit tests"); | |
684 | ||
685 | ret = connect_viewer("localhost"); | |
686 | ok(ret == 0, "Connect viewer to relayd"); | |
687 | ||
688 | ret = establish_connection(); | |
689 | ok(ret == 0, "Established connection and version check with %d.%d", | |
690 | VERSION_MAJOR, VERSION_MINOR); | |
691 | ||
692 | ret = list_sessions(&session_id); | |
693 | ok(ret > 0, "List sessions : %d session(s)", ret); | |
8674c9bf JG |
694 | if (ret < 0) { |
695 | goto end; | |
696 | } | |
1b368955 | 697 | |
25b50f8f JD |
698 | ret = create_viewer_session(); |
699 | ok(ret == 0, "Create viewer session"); | |
700 | ||
1b368955 | 701 | ret = attach_session(session_id); |
7409dd34 | 702 | ok(ret > 0, "Attach to session, %d stream(s) received", ret); |
1b368955 JD |
703 | |
704 | ret = get_metadata(); | |
705 | ok(ret > 0, "Get metadata, received %d bytes", ret); | |
706 | ||
707 | ret = get_next_index(); | |
708 | ok(ret == 0, "Get one index per stream"); | |
709 | ||
710 | ret = get_data_packet(first_packet_stream_id, first_packet_offset, | |
711 | first_packet_len); | |
10236918 | 712 | ok(ret == 0, |
1b368955 JD |
713 | "Get one data packet for stream %d, offset %d, len %d", |
714 | first_packet_stream_id, first_packet_offset, | |
715 | first_packet_len); | |
716 | ||
e22ba966 JD |
717 | ret = detach_viewer_session(session_id); |
718 | ok(ret == 0, "Detach viewer session"); | |
719 | ||
720 | ret = list_sessions(&session_id); | |
721 | ok(ret > 0, "List sessions : %d session(s)", ret); | |
722 | ||
723 | ret = attach_session(session_id); | |
724 | ok(ret > 0, "Attach to session, %d streams received", ret); | |
8674c9bf | 725 | end: |
1b368955 JD |
726 | return exit_status(); |
727 | } |