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> | |
39 | #include <bin/lttng-sessiond/session.h> | |
40 | #include <common/common.h> | |
41 | ||
2f8f53af | 42 | #include <bin/lttng-relayd/lttng-viewer-abi.h> |
50adc264 | 43 | #include <common/index/ctf-index.h> |
1b368955 | 44 | |
f263b7fd JD |
45 | #include <common/compat/endian.h> |
46 | ||
1b368955 JD |
47 | #define SESSION1 "test1" |
48 | #define RELAYD_URL "net://localhost" | |
49 | #define LIVE_TIMER 2000000 | |
50 | ||
51 | /* Number of TAP tests in this file */ | |
e22ba966 | 52 | #define NUM_TESTS 11 |
1b368955 JD |
53 | #define mmap_size 524288 |
54 | ||
55 | int ust_consumerd32_fd; | |
56 | int ust_consumerd64_fd; | |
57 | ||
58 | static int control_sock; | |
59 | struct live_session *session; | |
60 | ||
61 | static int first_packet_offset; | |
62 | static int first_packet_len; | |
7409dd34 | 63 | static int first_packet_stream_id = -1; |
1b368955 JD |
64 | |
65 | struct viewer_stream { | |
66 | uint64_t id; | |
67 | uint64_t ctf_trace_id; | |
68 | void *mmap_base; | |
69 | int fd; | |
70 | int metadata_flag; | |
71 | int first_read; | |
72 | char path[PATH_MAX]; | |
73 | }; | |
74 | ||
75 | struct live_session { | |
76 | struct viewer_stream *streams; | |
77 | uint64_t live_timer_interval; | |
78 | uint64_t stream_count; | |
79 | }; | |
80 | ||
10236918 MD |
81 | static |
82 | ssize_t lttng_live_recv(int fd, void *buf, size_t len) | |
83 | { | |
84 | ssize_t ret; | |
85 | size_t copied = 0, to_copy = len; | |
86 | ||
87 | do { | |
88 | ret = recv(fd, buf + copied, to_copy, 0); | |
89 | if (ret > 0) { | |
90 | assert(ret <= to_copy); | |
91 | copied += ret; | |
92 | to_copy -= ret; | |
93 | } | |
94 | } while ((ret > 0 && to_copy > 0) | |
95 | || (ret < 0 && errno == EINTR)); | |
96 | if (ret > 0) | |
97 | ret = copied; | |
98 | /* ret = 0 means orderly shutdown, ret < 0 is error. */ | |
99 | return ret; | |
100 | } | |
101 | ||
102 | static | |
103 | ssize_t lttng_live_send(int fd, const void *buf, size_t len) | |
104 | { | |
105 | ssize_t ret; | |
106 | ||
107 | do { | |
108 | ret = send(fd, buf, len, MSG_NOSIGNAL); | |
109 | } while (ret < 0 && errno == EINTR); | |
110 | return ret; | |
111 | } | |
112 | ||
1b368955 JD |
113 | static |
114 | int connect_viewer(char *hostname) | |
115 | { | |
116 | struct hostent *host; | |
117 | struct sockaddr_in server_addr; | |
118 | int ret; | |
119 | ||
120 | host = gethostbyname(hostname); | |
121 | if (!host) { | |
122 | ret = -1; | |
123 | goto end; | |
124 | } | |
125 | ||
126 | if ((control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
6f04ed72 | 127 | PERROR("Socket"); |
1b368955 JD |
128 | ret = -1; |
129 | goto end; | |
130 | } | |
131 | ||
132 | server_addr.sin_family = AF_INET; | |
133 | server_addr.sin_port = htons(5344); | |
134 | server_addr.sin_addr = *((struct in_addr *) host->h_addr); | |
135 | bzero(&(server_addr.sin_zero), 8); | |
136 | ||
137 | if (connect(control_sock, (struct sockaddr *) &server_addr, | |
138 | sizeof(struct sockaddr)) == -1) { | |
6f04ed72 | 139 | PERROR("Connect"); |
1b368955 JD |
140 | ret = -1; |
141 | goto end; | |
142 | } | |
143 | ||
144 | server_addr.sin_family = AF_INET; | |
145 | server_addr.sin_port = htons(5345); | |
146 | server_addr.sin_addr = *((struct in_addr *) host->h_addr); | |
147 | bzero(&(server_addr.sin_zero), 8); | |
148 | ||
149 | ret = 0; | |
150 | ||
151 | end: | |
152 | return ret; | |
153 | } | |
154 | ||
155 | int establish_connection(void) | |
156 | { | |
157 | struct lttng_viewer_cmd cmd; | |
158 | struct lttng_viewer_connect connect; | |
10236918 | 159 | ssize_t ret_len; |
1b368955 | 160 | |
c4e361a4 | 161 | cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT); |
2dc797f3 JD |
162 | cmd.data_size = htobe64(sizeof(connect)); |
163 | cmd.cmd_version = htobe32(0); | |
1b368955 | 164 | |
dcf5c25e | 165 | memset(&connect, 0, sizeof(connect)); |
1b368955 JD |
166 | connect.major = htobe32(VERSION_MAJOR); |
167 | connect.minor = htobe32(VERSION_MINOR); | |
c4e361a4 | 168 | connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND); |
1b368955 | 169 | |
10236918 MD |
170 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
171 | if (ret_len < 0) { | |
7409dd34 | 172 | diag("Error sending cmd"); |
1b368955 JD |
173 | goto error; |
174 | } | |
10236918 MD |
175 | ret_len = lttng_live_send(control_sock, &connect, sizeof(connect)); |
176 | if (ret_len < 0) { | |
7409dd34 | 177 | diag("Error sending version"); |
1b368955 JD |
178 | goto error; |
179 | } | |
180 | ||
10236918 MD |
181 | ret_len = lttng_live_recv(control_sock, &connect, sizeof(connect)); |
182 | if (ret_len == 0) { | |
7409dd34 | 183 | diag("[error] Remote side has closed connection"); |
10236918 MD |
184 | goto error; |
185 | } | |
186 | if (ret_len < 0) { | |
7409dd34 | 187 | diag("Error receiving version"); |
1b368955 JD |
188 | goto error; |
189 | } | |
10236918 | 190 | return 0; |
1b368955 JD |
191 | |
192 | error: | |
10236918 | 193 | return -1; |
1b368955 JD |
194 | } |
195 | ||
196 | /* | |
197 | * Returns the number of sessions, should be 1 during the unit test. | |
198 | */ | |
7409dd34 | 199 | int list_sessions(uint64_t *session_id) |
1b368955 JD |
200 | { |
201 | struct lttng_viewer_cmd cmd; | |
202 | struct lttng_viewer_list_sessions list; | |
203 | struct lttng_viewer_session lsession; | |
10236918 MD |
204 | int i; |
205 | ssize_t ret_len; | |
1b368955 JD |
206 | int first_session = 0; |
207 | ||
c4e361a4 | 208 | cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); |
2dc797f3 JD |
209 | cmd.data_size = htobe64(0); |
210 | cmd.cmd_version = htobe32(0); | |
1b368955 | 211 | |
10236918 MD |
212 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
213 | if (ret_len < 0) { | |
7409dd34 | 214 | diag("Error sending cmd"); |
1b368955 JD |
215 | goto error; |
216 | } | |
217 | ||
10236918 MD |
218 | ret_len = lttng_live_recv(control_sock, &list, sizeof(list)); |
219 | if (ret_len == 0) { | |
7409dd34 | 220 | diag("[error] Remote side has closed connection"); |
10236918 MD |
221 | goto error; |
222 | } | |
223 | if (ret_len < 0) { | |
7409dd34 | 224 | diag("Error receiving session list"); |
1b368955 JD |
225 | goto error; |
226 | } | |
227 | ||
228 | for (i = 0; i < be32toh(list.sessions_count); i++) { | |
10236918 MD |
229 | ret_len = lttng_live_recv(control_sock, &lsession, sizeof(lsession)); |
230 | if (ret_len < 0) { | |
7409dd34 | 231 | diag("Error receiving session"); |
1b368955 JD |
232 | goto error; |
233 | } | |
234 | if (lsession.streams > 0 && first_session <= 0) { | |
235 | first_session = be64toh(lsession.id); | |
236 | *session_id = first_session; | |
237 | } | |
238 | } | |
239 | ||
10236918 | 240 | return be32toh(list.sessions_count); |
1b368955 JD |
241 | |
242 | error: | |
10236918 | 243 | return -1; |
1b368955 JD |
244 | } |
245 | ||
10236918 | 246 | int create_viewer_session(void) |
25b50f8f JD |
247 | { |
248 | struct lttng_viewer_cmd cmd; | |
249 | struct lttng_viewer_create_session_response resp; | |
25b50f8f JD |
250 | ssize_t ret_len; |
251 | ||
252 | cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION); | |
2dc797f3 JD |
253 | cmd.data_size = htobe64(0); |
254 | cmd.cmd_version = htobe32(0); | |
25b50f8f | 255 | |
10236918 | 256 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
25b50f8f | 257 | if (ret_len < 0) { |
7409dd34 | 258 | diag("[error] Error sending cmd"); |
25b50f8f JD |
259 | goto error; |
260 | } | |
261 | assert(ret_len == sizeof(cmd)); | |
262 | ||
10236918 MD |
263 | ret_len = lttng_live_recv(control_sock, &resp, sizeof(resp)); |
264 | if (ret_len == 0) { | |
7409dd34 | 265 | diag("[error] Remote side has closed connection"); |
10236918 MD |
266 | goto error; |
267 | } | |
25b50f8f | 268 | if (ret_len < 0) { |
7409dd34 | 269 | diag("[error] Error receiving create session reply"); |
25b50f8f JD |
270 | goto error; |
271 | } | |
272 | assert(ret_len == sizeof(resp)); | |
273 | ||
274 | if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) { | |
7409dd34 | 275 | diag("[error] Error creating viewer session"); |
25b50f8f JD |
276 | goto error; |
277 | } | |
10236918 | 278 | return 0; |
25b50f8f JD |
279 | |
280 | error: | |
10236918 | 281 | return -1; |
25b50f8f JD |
282 | } |
283 | ||
7409dd34 | 284 | int attach_session(uint64_t id) |
1b368955 JD |
285 | { |
286 | struct lttng_viewer_cmd cmd; | |
287 | struct lttng_viewer_attach_session_request rq; | |
288 | struct lttng_viewer_attach_session_response rp; | |
289 | struct lttng_viewer_stream stream; | |
10236918 MD |
290 | int i; |
291 | ssize_t ret_len; | |
1b368955 JD |
292 | |
293 | session = zmalloc(sizeof(struct live_session)); | |
294 | if (!session) { | |
1b368955 JD |
295 | goto error; |
296 | } | |
297 | ||
c4e361a4 | 298 | cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION); |
2dc797f3 JD |
299 | cmd.data_size = htobe64(sizeof(rq)); |
300 | cmd.cmd_version = htobe32(0); | |
1b368955 | 301 | |
dcf5c25e | 302 | memset(&rq, 0, sizeof(rq)); |
1b368955 | 303 | rq.session_id = htobe64(id); |
c4e361a4 | 304 | rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING); |
1b368955 | 305 | |
10236918 MD |
306 | ret_len = lttng_live_send(control_sock, &cmd, sizeof(cmd)); |
307 | if (ret_len < 0) { | |
7409dd34 | 308 | diag("Error sending cmd LTTNG_VIEWER_ATTACH_SESSION"); |
1b368955 JD |
309 | goto error; |
310 | } | |
10236918 MD |
311 | ret_len = lttng_live_send(control_sock, &rq, sizeof(rq)); |
312 | if (ret_len < 0) { | |
7409dd34 | 313 | diag("Error sending attach request"); |
1b368955 JD |
314 | goto error; |
315 | } | |
316 | ||
10236918 MD |
317 | ret_len = lttng_live_recv(control_sock, &rp, sizeof(rp)); |
318 | if (ret_len == 0) { | |
7409dd34 | 319 | diag("[error] Remote side has closed connection"); |
10236918 MD |
320 | goto error; |
321 | } | |
322 | if (ret_len < 0) { | |
7409dd34 | 323 | diag("Error receiving attach response"); |
1b368955 JD |
324 | goto error; |
325 | } | |
c4e361a4 | 326 | if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) { |
10236918 | 327 | goto error; |
1b368955 JD |
328 | } |
329 | ||
330 | session->stream_count = be32toh(rp.streams_count); | |
7409dd34 JG |
331 | if (session->stream_count == 0) { |
332 | diag("Got session stream count == 0"); | |
333 | goto error; | |
334 | } | |
1b368955 JD |
335 | session->streams = zmalloc(session->stream_count * |
336 | sizeof(struct viewer_stream)); | |
337 | if (!session->streams) { | |
1b368955 JD |
338 | goto error; |
339 | } | |
340 | ||
341 | for (i = 0; i < be32toh(rp.streams_count); i++) { | |
10236918 MD |
342 | ret_len = lttng_live_recv(control_sock, &stream, sizeof(stream)); |
343 | if (ret_len == 0) { | |
7409dd34 | 344 | diag("[error] Remote side has closed connection"); |
10236918 MD |
345 | goto error; |
346 | } | |
347 | if (ret_len < 0) { | |
7409dd34 | 348 | diag("Error receiving stream"); |
1b368955 JD |
349 | goto error; |
350 | } | |
351 | session->streams[i].id = be64toh(stream.id); | |
352 | ||
353 | session->streams[i].ctf_trace_id = be64toh(stream.ctf_trace_id); | |
354 | session->streams[i].first_read = 1; | |
7409dd34 JG |
355 | session->streams[i].mmap_base = mmap(NULL, mmap_size, |
356 | PROT_READ | PROT_WRITE, | |
1b368955 JD |
357 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
358 | if (session->streams[i].mmap_base == MAP_FAILED) { | |
7409dd34 | 359 | diag("mmap error"); |
1b368955 JD |
360 | goto error; |
361 | } | |
362 | ||
363 | if (be32toh(stream.metadata_flag)) { | |
364 | session->streams[i].metadata_flag = 1; | |
365 | } | |
366 | } | |
10236918 | 367 | return session->stream_count; |
1b368955 | 368 | |
1b368955 | 369 | error: |
10236918 | 370 | return -1; |
1b368955 JD |
371 | } |
372 | ||
373 | int get_metadata(void) | |
374 | { | |
375 | struct lttng_viewer_cmd cmd; | |
376 | struct lttng_viewer_get_metadata rq; | |
377 | struct lttng_viewer_metadata_packet rp; | |
10236918 | 378 | ssize_t ret_len; |
1b368955 JD |
379 | int ret; |
380 | uint64_t i; | |
381 | char *data = NULL; | |
382 | uint64_t len = 0; | |
383 | int metadata_stream_id = -1; | |
384 | ||
c4e361a4 | 385 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA); |
2dc797f3 JD |
386 | cmd.data_size = htobe64(sizeof(rq)); |
387 | cmd.cmd_version = htobe32(0); | |
1b368955 JD |
388 | |
389 | for (i = 0; i < session->stream_count; i++) { | |
390 | if (session->streams[i].metadata_flag) { | |
391 | metadata_stream_id = i; | |
392 | break; | |
393 | } | |
394 | } | |
395 | ||
396 | if (metadata_stream_id < 0) { | |
7409dd34 | 397 | diag("No metadata stream found"); |
1b368955 JD |
398 | goto error; |
399 | } | |
400 | ||
401 | rq.stream_id = htobe64(session->streams[metadata_stream_id].id); | |
402 | ||
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:"); | |
427 | ret = 0; | |
428 | goto end; | |
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; |
1b368955 | 458 | end: |
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); | |
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); | |
725 | ||
1b368955 JD |
726 | return exit_status(); |
727 | } |