Commit | Line | Data |
---|---|---|
4a744367 JD |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * | |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
24 | #include <sys/socket.h> | |
25 | #include <sys/types.h> | |
26 | #include <netinet/in.h> | |
27 | #include <netdb.h> | |
28 | #include <stdio.h> | |
29 | #include <string.h> | |
30 | #include <stdlib.h> | |
31 | #include <unistd.h> | |
32 | #include <errno.h> | |
33 | #include <inttypes.h> | |
34 | #include <fcntl.h> | |
35 | #include <sys/mman.h> | |
36 | ||
37 | #include <babeltrace/ctf/ctf-index.h> | |
38 | ||
39 | #include <babeltrace/babeltrace.h> | |
40 | #include <babeltrace/ctf/events.h> | |
41 | #include <babeltrace/ctf/callbacks.h> | |
42 | #include <babeltrace/ctf/iterator.h> | |
43 | ||
44 | /* for packet_index */ | |
45 | #include <babeltrace/ctf/types.h> | |
46 | ||
47 | #include <babeltrace/ctf/metadata.h> | |
48 | #include <babeltrace/ctf-text/types.h> | |
49 | #include <babeltrace/ctf/events-internal.h> | |
50 | #include <formats/ctf/events-private.h> | |
51 | ||
52 | #include "lttng-live-functions.h" | |
53 | #include "lttng-viewer.h" | |
54 | ||
55 | /* | |
56 | * Memory allocation zeroed | |
57 | */ | |
58 | #define zmalloc(x) calloc(1, x) | |
59 | ||
60 | #ifndef max_t | |
61 | #define max_t(type, a, b) \ | |
62 | ((type) (a) > (type) (b) ? (type) (a) : (type) (b)) | |
63 | #endif | |
64 | ||
65 | int lttng_live_connect_viewer(struct lttng_live_ctx *ctx, char *hostname, | |
66 | int port) | |
67 | { | |
68 | struct hostent *host; | |
69 | struct sockaddr_in server_addr; | |
70 | int ret; | |
71 | ||
72 | host = gethostbyname(hostname); | |
73 | if (!host) { | |
74 | ret = -1; | |
75 | goto end; | |
76 | } | |
77 | ||
78 | if ((ctx->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { | |
79 | perror("Socket"); | |
80 | ret = -1; | |
81 | goto end; | |
82 | } | |
83 | ||
84 | server_addr.sin_family = AF_INET; | |
85 | server_addr.sin_port = htons(port); | |
86 | server_addr.sin_addr = *((struct in_addr *) host->h_addr); | |
87 | bzero(&(server_addr.sin_zero), 8); | |
88 | ||
89 | if (connect(ctx->control_sock, (struct sockaddr *) &server_addr, | |
90 | sizeof(struct sockaddr)) == -1) { | |
91 | perror("Connect"); | |
92 | ret = -1; | |
93 | goto end; | |
94 | } | |
95 | ||
96 | ret = 0; | |
97 | ||
98 | end: | |
99 | return ret; | |
100 | } | |
101 | ||
102 | int lttng_live_establish_connection(struct lttng_live_ctx *ctx) | |
103 | { | |
104 | struct lttng_viewer_cmd cmd; | |
105 | struct lttng_viewer_connect connect; | |
106 | int ret; | |
107 | ssize_t ret_len; | |
108 | ||
109 | cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT); | |
110 | cmd.data_size = sizeof(connect); | |
111 | cmd.cmd_version = 0; | |
112 | ||
0c3cd7e1 | 113 | connect.viewer_session_id = -1ULL; /* will be set on recv */ |
4a744367 JD |
114 | connect.major = htobe32(LTTNG_LIVE_MAJOR); |
115 | connect.minor = htobe32(LTTNG_LIVE_MINOR); | |
116 | connect.type = htobe32(LTTNG_VIEWER_CLIENT_COMMAND); | |
117 | ||
118 | do { | |
119 | ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0); | |
120 | } while (ret_len < 0 && errno == EINTR); | |
121 | if (ret_len < 0) { | |
122 | fprintf(stderr, "[error] Error sending cmd\n"); | |
123 | ret = ret_len; | |
124 | goto error; | |
125 | } | |
126 | assert(ret_len == sizeof(cmd)); | |
127 | ||
128 | do { | |
129 | ret_len = send(ctx->control_sock, &connect, sizeof(connect), 0); | |
130 | } while (ret_len < 0 && errno == EINTR); | |
131 | if (ret_len < 0) { | |
132 | fprintf(stderr, "[error] Error sending version\n"); | |
133 | ret = ret_len; | |
134 | goto error; | |
135 | } | |
136 | assert(ret_len == sizeof(connect)); | |
137 | ||
138 | do { | |
139 | ret_len = recv(ctx->control_sock, &connect, sizeof(connect), 0); | |
140 | } while (ret_len < 0 && errno == EINTR); | |
141 | if (ret_len < 0) { | |
142 | fprintf(stderr, "[error] Error receiving version\n"); | |
143 | ret = ret_len; | |
144 | goto error; | |
145 | } | |
146 | assert(ret_len == sizeof(connect)); | |
147 | ||
148 | printf_verbose("Received viewer session ID : %" PRIu64 "\n", | |
149 | be64toh(connect.viewer_session_id)); | |
150 | printf_verbose("Relayd version : %u.%u\n", be32toh(connect.major), | |
151 | be32toh(connect.minor)); | |
152 | ||
153 | ret = 0; | |
154 | ||
155 | error: | |
156 | return ret; | |
157 | } | |
158 | ||
159 | int lttng_live_list_sessions(struct lttng_live_ctx *ctx, const char *path) | |
160 | { | |
161 | struct lttng_viewer_cmd cmd; | |
162 | struct lttng_viewer_list_sessions list; | |
163 | struct lttng_viewer_session lsession; | |
164 | int i, ret; | |
165 | ssize_t ret_len; | |
166 | int sessions_count; | |
167 | ||
168 | cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); | |
169 | cmd.data_size = 0; | |
170 | cmd.cmd_version = 0; | |
171 | ||
172 | do { | |
173 | ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0); | |
174 | } while (ret_len < 0 && errno == EINTR); | |
175 | if (ret_len < 0) { | |
176 | fprintf(stderr, "[error] Error sending cmd\n"); | |
177 | ret = ret_len; | |
178 | goto error; | |
179 | } | |
180 | assert(ret_len == sizeof(cmd)); | |
181 | ||
182 | do { | |
183 | ret_len = recv(ctx->control_sock, &list, sizeof(list), 0); | |
184 | } while (ret_len < 0 && errno == EINTR); | |
185 | if (ret_len < 0) { | |
186 | fprintf(stderr, "[error] Error receiving session list\n"); | |
187 | ret = ret_len; | |
188 | goto error; | |
189 | } | |
190 | assert(ret_len == sizeof(list)); | |
191 | ||
192 | sessions_count = be32toh(list.sessions_count); | |
193 | fprintf(stdout, "%u active session(s)%c\n", sessions_count, | |
194 | sessions_count > 0 ? ':' : ' '); | |
195 | for (i = 0; i < sessions_count; i++) { | |
196 | do { | |
197 | ret_len = recv(ctx->control_sock, &lsession, sizeof(lsession), 0); | |
198 | } while (ret_len < 0 && errno == EINTR); | |
199 | if (ret_len < 0) { | |
200 | fprintf(stderr, "[error] Error receiving session\n"); | |
201 | ret = ret_len; | |
202 | goto error; | |
203 | } | |
204 | assert(ret_len == sizeof(lsession)); | |
9ed31e53 MD |
205 | lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0'; |
206 | lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0'; | |
4a744367 JD |
207 | |
208 | fprintf(stdout, "%s/%" PRIu64 " : %s on host %s (timer = %u, " | |
209 | "%u stream(s), %u client(s) connected)\n", | |
210 | path, be64toh(lsession.id), | |
211 | lsession.session_name, lsession.hostname, | |
212 | be32toh(lsession.live_timer), | |
213 | be32toh(lsession.streams), | |
214 | be32toh(lsession.clients)); | |
215 | } | |
216 | ||
217 | ret = 0; | |
218 | ||
219 | error: | |
220 | return ret; | |
221 | } | |
222 | ||
223 | int lttng_live_ctf_trace_assign(struct lttng_live_viewer_stream *stream, | |
224 | uint64_t ctf_trace_id) | |
225 | { | |
226 | struct lttng_live_ctf_trace *trace; | |
227 | int ret = 0; | |
228 | ||
229 | trace = g_hash_table_lookup(stream->session->ctf_traces, | |
230 | (gpointer) ctf_trace_id); | |
231 | if (!trace) { | |
232 | trace = g_new0(struct lttng_live_ctf_trace, 1); | |
233 | if (!trace) { | |
234 | ret = -1; | |
235 | fprintf(stderr, "[error] ctf_trace allocation\n"); | |
236 | goto error; | |
237 | } | |
238 | trace->ctf_trace_id = ctf_trace_id; | |
239 | trace->streams = g_ptr_array_new(); | |
240 | g_hash_table_insert(stream->session->ctf_traces, | |
241 | (gpointer) ctf_trace_id, | |
242 | trace); | |
243 | } | |
244 | if (stream->metadata_flag) | |
245 | trace->metadata_stream = stream; | |
246 | ||
247 | stream->ctf_trace = trace; | |
248 | g_ptr_array_add(trace->streams, stream); | |
249 | ||
250 | error: | |
251 | return ret; | |
252 | } | |
253 | ||
254 | int lttng_live_attach_session(struct lttng_live_ctx *ctx, uint64_t id) | |
255 | { | |
256 | struct lttng_viewer_cmd cmd; | |
257 | struct lttng_viewer_attach_session_request rq; | |
258 | struct lttng_viewer_attach_session_response rp; | |
259 | struct lttng_viewer_stream stream; | |
260 | int ret, i; | |
261 | ssize_t ret_len; | |
262 | ||
263 | cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION); | |
264 | cmd.data_size = sizeof(rq); | |
265 | cmd.cmd_version = 0; | |
266 | ||
267 | memset(&rq, 0, sizeof(rq)); | |
268 | rq.session_id = htobe64(id); | |
269 | // TODO: add cmd line parameter to select seek beginning | |
270 | // rq.seek = htobe32(LTTNG_VIEWER_SEEK_BEGINNING); | |
271 | rq.seek = htobe32(LTTNG_VIEWER_SEEK_LAST); | |
272 | ||
273 | do { | |
274 | ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0); | |
275 | } while (ret_len < 0 && errno == EINTR); | |
276 | if (ret_len < 0) { | |
277 | fprintf(stderr, "[error] Error sending cmd\n"); | |
278 | ret = ret_len; | |
279 | goto error; | |
280 | } | |
281 | assert(ret_len == sizeof(cmd)); | |
282 | ||
283 | do { | |
284 | ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0); | |
285 | } while (ret_len < 0 && errno == EINTR); | |
286 | if (ret_len < 0) { | |
287 | fprintf(stderr, "[error] Error sending attach request\n"); | |
288 | ret = ret_len; | |
289 | goto error; | |
290 | } | |
291 | assert(ret_len == sizeof(rq)); | |
292 | ||
293 | do { | |
294 | ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0); | |
295 | } while (ret_len < 0 && errno == EINTR); | |
296 | if (ret_len < 0) { | |
297 | fprintf(stderr, "[error] Error receiving attach response\n"); | |
298 | ret = ret_len; | |
299 | goto error; | |
300 | } | |
301 | assert(ret_len == sizeof(rp)); | |
302 | ||
303 | switch(be32toh(rp.status)) { | |
304 | case LTTNG_VIEWER_ATTACH_OK: | |
305 | break; | |
306 | case LTTNG_VIEWER_ATTACH_UNK: | |
307 | ret = -LTTNG_VIEWER_ATTACH_UNK; | |
308 | goto end; | |
309 | case LTTNG_VIEWER_ATTACH_ALREADY: | |
310 | fprintf(stderr, "[error] Already a viewer attached\n"); | |
311 | ret = -1; | |
312 | goto end; | |
313 | case LTTNG_VIEWER_ATTACH_NOT_LIVE: | |
314 | fprintf(stderr, "[error] Not a live session\n"); | |
315 | ret = -1; | |
316 | goto end; | |
317 | case LTTNG_VIEWER_ATTACH_SEEK_ERR: | |
318 | fprintf(stderr, "[error] Wrong seek parameter\n"); | |
319 | ret = -1; | |
320 | goto end; | |
321 | default: | |
322 | fprintf(stderr, "[error] Unknown attach return code %u\n", | |
323 | be32toh(rp.status)); | |
324 | ret = -1; | |
325 | goto end; | |
326 | } | |
327 | if (be32toh(rp.status) != LTTNG_VIEWER_ATTACH_OK) { | |
328 | ret = -1; | |
329 | goto end; | |
330 | } | |
331 | ||
332 | ctx->session->stream_count = be32toh(rp.streams_count); | |
333 | /* | |
334 | * When the session is created but not started, we do an active wait | |
335 | * until it starts. It allows the viewer to start processing the trace | |
336 | * as soon as the session starts. | |
337 | */ | |
338 | if (ctx->session->stream_count == 0) { | |
339 | ret = 0; | |
340 | goto end; | |
341 | } | |
342 | printf_verbose("Waiting for %" PRIu64 " streams:\n", | |
343 | ctx->session->stream_count); | |
344 | ctx->session->streams = g_new0(struct lttng_live_viewer_stream, | |
345 | ctx->session->stream_count); | |
346 | if (!ctx->session->streams) { | |
347 | ret = -1; | |
348 | goto error; | |
349 | } | |
350 | ||
351 | for (i = 0; i < be32toh(rp.streams_count); i++) { | |
352 | do { | |
353 | ret_len = recv(ctx->control_sock, &stream, sizeof(stream), 0); | |
354 | } while (ret_len < 0 && errno == EINTR); | |
355 | if (ret_len < 0) { | |
356 | fprintf(stderr, "[error] Error receiving stream\n"); | |
357 | ret = ret_len; | |
358 | goto error; | |
359 | } | |
360 | assert(ret_len == sizeof(stream)); | |
9ed31e53 MD |
361 | stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0'; |
362 | stream.channel_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0'; | |
4a744367 JD |
363 | |
364 | printf_verbose(" stream %" PRIu64 " : %s/%s\n", | |
365 | be64toh(stream.id), stream.path_name, | |
366 | stream.channel_name); | |
367 | ctx->session->streams[i].id = be64toh(stream.id); | |
368 | ctx->session->streams[i].session = ctx->session; | |
369 | ||
370 | ctx->session->streams[i].first_read = 1; | |
371 | ctx->session->streams[i].mmap_size = 0; | |
372 | ||
373 | if (be32toh(stream.metadata_flag)) { | |
374 | char *path; | |
375 | ||
376 | path = strdup(LTTNG_METADATA_PATH_TEMPLATE); | |
6a4d2b5a MD |
377 | if (!path) { |
378 | perror("strdup"); | |
379 | ret = -1; | |
380 | goto error; | |
381 | } | |
382 | if (!mkdtemp(path)) { | |
383 | perror("mkdtemp"); | |
384 | free(path); | |
385 | ret = -1; | |
386 | goto error; | |
387 | } | |
4a744367 | 388 | ctx->session->streams[i].metadata_flag = 1; |
4a744367 JD |
389 | snprintf(ctx->session->streams[i].path, |
390 | sizeof(ctx->session->streams[i].path), | |
391 | "%s/%s", path, | |
392 | stream.channel_name); | |
393 | ret = open(ctx->session->streams[i].path, | |
394 | O_WRONLY | O_CREAT | O_TRUNC, | |
395 | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
396 | if (ret < 0) { | |
ab9d07f0 | 397 | perror("open"); |
6a4d2b5a | 398 | free(path); |
4a744367 JD |
399 | goto error; |
400 | } | |
401 | ctx->session->streams[i].fd = ret; | |
6a4d2b5a | 402 | free(path); |
4a744367 JD |
403 | } |
404 | ret = lttng_live_ctf_trace_assign(&ctx->session->streams[i], | |
405 | be64toh(stream.ctf_trace_id)); | |
406 | if (ret < 0) { | |
407 | goto error; | |
408 | } | |
409 | ||
410 | } | |
411 | ret = 0; | |
412 | ||
413 | end: | |
414 | error: | |
415 | return ret; | |
416 | } | |
417 | ||
418 | static | |
419 | int get_data_packet(struct lttng_live_ctx *ctx, | |
420 | struct ctf_stream_pos *pos, | |
421 | struct lttng_live_viewer_stream *stream, uint64_t offset, | |
422 | uint64_t len) | |
423 | { | |
424 | struct lttng_viewer_cmd cmd; | |
425 | struct lttng_viewer_get_packet rq; | |
426 | struct lttng_viewer_trace_packet rp; | |
427 | ssize_t ret_len; | |
428 | int ret; | |
429 | ||
430 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET); | |
431 | cmd.data_size = sizeof(rq); | |
432 | cmd.cmd_version = 0; | |
433 | ||
434 | memset(&rq, 0, sizeof(rq)); | |
435 | rq.stream_id = htobe64(stream->id); | |
436 | /* Already in big endian. */ | |
437 | rq.offset = offset; | |
438 | rq.len = htobe32(len); | |
439 | ||
440 | do { | |
441 | ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0); | |
442 | } while (ret_len < 0 && errno == EINTR); | |
443 | if (ret_len < 0) { | |
444 | fprintf(stderr, "[error] Error sending cmd\n"); | |
445 | ret = ret_len; | |
446 | goto error; | |
447 | } | |
448 | assert(ret_len == sizeof(cmd)); | |
449 | ||
450 | do { | |
451 | ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0); | |
452 | } while (ret_len < 0 && errno == EINTR); | |
453 | if (ret_len < 0) { | |
454 | fprintf(stderr, "[error] Error sending get_data_packet request\n"); | |
455 | ret = ret_len; | |
456 | goto error; | |
457 | } | |
458 | assert(ret_len == sizeof(rq)); | |
459 | ||
460 | do { | |
461 | ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0); | |
462 | } while (ret_len < 0 && errno == EINTR); | |
463 | if (ret_len < 0) { | |
464 | fprintf(stderr, "[error] Error receiving data response\n"); | |
465 | ret = ret_len; | |
466 | goto error; | |
467 | } | |
468 | assert(ret_len == sizeof(rp)); | |
469 | ||
470 | rp.flags = be32toh(rp.flags); | |
471 | ||
472 | switch (be32toh(rp.status)) { | |
473 | case LTTNG_VIEWER_GET_PACKET_OK: | |
474 | len = be32toh(rp.len); | |
475 | printf_verbose("get_data_packet: Ok, packet size : %" PRIu64 | |
476 | "\n", len); | |
477 | break; | |
478 | case LTTNG_VIEWER_GET_PACKET_RETRY: | |
479 | printf_verbose("get_data_packet: retry\n"); | |
480 | ret = -1; | |
481 | goto end; | |
482 | case LTTNG_VIEWER_GET_PACKET_ERR: | |
483 | if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { | |
484 | printf_verbose("get_data_packet: new metadata needed\n"); | |
485 | ret = 0; | |
486 | goto end; | |
487 | } | |
488 | fprintf(stderr, "[error] get_data_packet: error\n"); | |
489 | ret = -1; | |
490 | goto end; | |
491 | case LTTNG_VIEWER_GET_PACKET_EOF: | |
492 | ret = -2; | |
493 | goto error; | |
494 | default: | |
495 | printf_verbose("get_data_packet: unknown\n"); | |
496 | assert(0); | |
497 | ret = -1; | |
498 | goto end; | |
499 | } | |
500 | ||
501 | if (len <= 0) { | |
502 | ret = -1; | |
503 | goto end; | |
504 | } | |
505 | ||
506 | if (len > stream->mmap_size) { | |
507 | uint64_t new_size; | |
508 | ||
509 | new_size = max_t(uint64_t, len, stream->mmap_size << 1); | |
510 | if (pos->base_mma) { | |
511 | /* unmap old base */ | |
512 | ret = munmap_align(pos->base_mma); | |
513 | if (ret) { | |
514 | fprintf(stderr, "[error] Unable to unmap old base: %s.\n", | |
515 | strerror(errno)); | |
516 | ret = -1; | |
517 | goto error; | |
518 | } | |
519 | pos->base_mma = NULL; | |
520 | } | |
521 | pos->base_mma = mmap_align(new_size, | |
522 | PROT_READ | PROT_WRITE, | |
523 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
524 | if (pos->base_mma == MAP_FAILED) { | |
525 | fprintf(stderr, "[error] mmap error %s.\n", | |
526 | strerror(errno)); | |
527 | pos->base_mma = NULL; | |
528 | ret = -1; | |
529 | goto error; | |
530 | } | |
531 | ||
532 | stream->mmap_size = new_size; | |
533 | printf_verbose("Expanding stream mmap size to %" PRIu64 " bytes\n", | |
534 | stream->mmap_size); | |
535 | } | |
536 | ||
537 | do { | |
538 | ret_len = recv(ctx->control_sock, | |
539 | mmap_align_addr(pos->base_mma), len, | |
540 | MSG_WAITALL); | |
541 | } while (ret_len < 0 && errno == EINTR); | |
542 | if (ret_len < 0) { | |
543 | fprintf(stderr, "[error] Error receiving trace packet\n"); | |
544 | ret = ret_len; | |
545 | goto error; | |
546 | } | |
547 | assert(ret_len == len); | |
548 | ||
549 | end: | |
550 | error: | |
551 | return ret; | |
552 | } | |
553 | ||
554 | /* | |
555 | * Return number of metadata bytes written or a negative value on error. | |
556 | */ | |
557 | static | |
558 | int get_new_metadata(struct lttng_live_ctx *ctx, | |
559 | struct lttng_live_viewer_stream *viewer_stream, | |
560 | uint64_t *metadata_len) | |
561 | { | |
562 | uint64_t len = 0; | |
563 | int ret; | |
564 | struct lttng_viewer_cmd cmd; | |
565 | struct lttng_viewer_get_metadata rq; | |
566 | struct lttng_viewer_metadata_packet rp; | |
567 | struct lttng_live_viewer_stream *metadata_stream; | |
568 | char *data = NULL; | |
569 | ssize_t ret_len; | |
570 | ||
571 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA); | |
572 | cmd.data_size = sizeof(rq); | |
573 | cmd.cmd_version = 0; | |
574 | ||
575 | metadata_stream = viewer_stream->ctf_trace->metadata_stream; | |
576 | rq.stream_id = htobe64(metadata_stream->id); | |
577 | ||
578 | do { | |
579 | ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0); | |
580 | } while (ret_len < 0 && errno == EINTR); | |
581 | if (ret_len < 0) { | |
582 | fprintf(stderr, "[error] Error sending cmd\n"); | |
583 | ret = ret_len; | |
584 | goto error; | |
585 | } | |
586 | assert(ret_len == sizeof(cmd)); | |
587 | ||
588 | do { | |
589 | ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0); | |
590 | } while (ret_len < 0 && errno == EINTR); | |
591 | if (ret_len < 0) { | |
592 | fprintf(stderr, "[error] Error sending get_metadata request\n"); | |
593 | ret = ret_len; | |
594 | goto error; | |
595 | } | |
596 | assert(ret_len == sizeof(rq)); | |
597 | ||
598 | do { | |
599 | ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0); | |
600 | } while (ret_len < 0 && errno == EINTR); | |
601 | if (ret_len < 0) { | |
602 | fprintf(stderr, "[error] Error receiving metadata response\n"); | |
603 | ret = ret_len; | |
604 | goto error; | |
605 | } | |
606 | assert(ret_len == sizeof(rp)); | |
607 | ||
608 | switch (be32toh(rp.status)) { | |
609 | case LTTNG_VIEWER_METADATA_OK: | |
610 | printf_verbose("get_metadata : OK\n"); | |
611 | break; | |
612 | case LTTNG_VIEWER_NO_NEW_METADATA: | |
613 | printf_verbose("get_metadata : NO NEW\n"); | |
614 | ret = -1; | |
615 | goto end; | |
616 | case LTTNG_VIEWER_METADATA_ERR: | |
617 | printf_verbose("get_metadata : ERR\n"); | |
618 | ret = -1; | |
619 | goto end; | |
620 | default: | |
621 | printf_verbose("get_metadata : UNKNOWN\n"); | |
622 | ret = -1; | |
623 | goto end; | |
624 | } | |
625 | ||
626 | len = be64toh(rp.len); | |
627 | printf_verbose("Writing %" PRIu64" bytes to metadata\n", len); | |
628 | if (len <= 0) { | |
629 | ret = -1; | |
630 | goto end; | |
631 | } | |
632 | ||
633 | data = zmalloc(len); | |
634 | if (!data) { | |
635 | perror("relay data zmalloc"); | |
636 | ret = -1; | |
637 | goto error; | |
638 | } | |
639 | do { | |
640 | ret_len = recv(ctx->control_sock, data, len, MSG_WAITALL); | |
641 | } while (ret_len < 0 && errno == EINTR); | |
642 | if (ret_len < 0) { | |
643 | fprintf(stderr, "[error] Error receiving trace packet\n"); | |
644 | ret = ret_len; | |
645 | free(data); | |
646 | goto error; | |
647 | } | |
648 | assert(ret_len == len); | |
649 | ||
650 | do { | |
651 | ret_len = write(metadata_stream->fd, data, len); | |
652 | } while (ret_len < 0 && errno == EINTR); | |
653 | if (ret_len < 0) { | |
654 | free(data); | |
655 | ret = ret_len; | |
656 | goto error; | |
657 | } | |
658 | assert(ret_len == len); | |
659 | ||
660 | free(data); | |
661 | ||
662 | *metadata_len = len; | |
663 | ret = 0; | |
664 | end: | |
665 | error: | |
666 | return ret; | |
667 | } | |
668 | ||
669 | /* | |
670 | * Get one index for a stream. | |
671 | * | |
672 | * Returns 0 on success or a negative value on error. | |
673 | */ | |
674 | static | |
675 | int get_next_index(struct lttng_live_ctx *ctx, | |
676 | struct lttng_live_viewer_stream *viewer_stream, | |
677 | struct packet_index *index) | |
678 | { | |
679 | struct lttng_viewer_cmd cmd; | |
680 | struct lttng_viewer_get_next_index rq; | |
681 | struct lttng_viewer_index rp; | |
682 | int ret; | |
683 | uint64_t metadata_len; | |
684 | ssize_t ret_len; | |
685 | ||
686 | cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX); | |
687 | cmd.data_size = sizeof(rq); | |
688 | cmd.cmd_version = 0; | |
689 | ||
690 | memset(&rq, 0, sizeof(rq)); | |
691 | rq.stream_id = htobe64(viewer_stream->id); | |
692 | ||
693 | retry: | |
694 | do { | |
695 | ret_len = send(ctx->control_sock, &cmd, sizeof(cmd), 0); | |
696 | } while (ret_len < 0 && errno == EINTR); | |
697 | if (ret_len < 0) { | |
698 | fprintf(stderr, "[error] Error sending cmd\n"); | |
699 | ret = ret_len; | |
700 | goto error; | |
701 | } | |
702 | assert(ret_len == sizeof(cmd)); | |
703 | ||
704 | do { | |
705 | ret_len = send(ctx->control_sock, &rq, sizeof(rq), 0); | |
706 | } while (ret_len < 0 && errno == EINTR); | |
707 | if (ret_len < 0) { | |
708 | fprintf(stderr, "[error] Error sending get_next_index request\n"); | |
709 | ret = ret_len; | |
710 | goto error; | |
711 | } | |
712 | assert(ret_len == sizeof(rq)); | |
713 | ||
714 | do { | |
715 | ret_len = recv(ctx->control_sock, &rp, sizeof(rp), 0); | |
716 | } while (ret_len < 0 && errno == EINTR); | |
717 | if (ret_len < 0) { | |
718 | fprintf(stderr, "[error] Error receiving index response\n"); | |
719 | ret = ret_len; | |
720 | goto error; | |
721 | } | |
722 | assert(ret_len == sizeof(rp)); | |
723 | ||
724 | rp.flags = be32toh(rp.flags); | |
725 | ||
726 | switch (be32toh(rp.status)) { | |
727 | case LTTNG_VIEWER_INDEX_INACTIVE: | |
728 | printf_verbose("get_next_index: inactive\n"); | |
729 | memset(index, 0, sizeof(struct packet_index)); | |
730 | index->timestamp_end = be64toh(rp.timestamp_end); | |
731 | break; | |
732 | case LTTNG_VIEWER_INDEX_OK: | |
733 | printf_verbose("get_next_index: Ok, need metadata update : %u\n", | |
734 | rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA); | |
735 | index->offset = be64toh(rp.offset); | |
736 | index->packet_size = be64toh(rp.packet_size); | |
737 | index->content_size = be64toh(rp.content_size); | |
738 | index->timestamp_begin = be64toh(rp.timestamp_begin); | |
739 | index->timestamp_end = be64toh(rp.timestamp_end); | |
740 | index->events_discarded = be64toh(rp.events_discarded); | |
741 | ||
742 | if (rp.flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { | |
743 | printf_verbose("get_next_index: new metadata needed\n"); | |
744 | ret = get_new_metadata(ctx, viewer_stream, | |
745 | &metadata_len); | |
746 | if (ret < 0) { | |
747 | goto error; | |
748 | } | |
749 | } | |
750 | break; | |
751 | case LTTNG_VIEWER_INDEX_RETRY: | |
752 | printf_verbose("get_next_index: retry\n"); | |
753 | sleep(1); | |
754 | goto retry; | |
755 | case LTTNG_VIEWER_INDEX_HUP: | |
756 | printf_verbose("get_next_index: stream hung up\n"); | |
757 | viewer_stream->id = -1ULL; | |
758 | viewer_stream->fd = -1; | |
759 | index->offset = EOF; | |
760 | break; | |
761 | case LTTNG_VIEWER_INDEX_ERR: | |
762 | fprintf(stderr, "[error] get_next_index: error\n"); | |
763 | ret = -1; | |
764 | goto error; | |
765 | default: | |
766 | fprintf(stderr, "[error] get_next_index: unkwown value\n"); | |
767 | ret = -1; | |
768 | goto error; | |
769 | } | |
770 | ||
771 | ret = 0; | |
772 | ||
773 | error: | |
774 | return ret; | |
775 | } | |
776 | ||
777 | void ctf_live_packet_seek(struct bt_stream_pos *stream_pos, size_t index, | |
778 | int whence) | |
779 | { | |
780 | struct ctf_stream_pos *pos; | |
781 | struct ctf_file_stream *file_stream; | |
782 | struct packet_index packet_index; | |
783 | struct lttng_live_viewer_stream *viewer_stream; | |
784 | struct lttng_live_session *session; | |
785 | int ret; | |
786 | ||
787 | retry: | |
788 | pos = ctf_pos(stream_pos); | |
789 | file_stream = container_of(pos, struct ctf_file_stream, pos); | |
790 | viewer_stream = (struct lttng_live_viewer_stream *) pos->priv; | |
791 | session = viewer_stream->session; | |
792 | ||
793 | printf_verbose("get_next_index for stream %" PRIu64 "\n", viewer_stream->id); | |
794 | ret = get_next_index(session->ctx, viewer_stream, &packet_index); | |
795 | if (ret < 0) { | |
796 | pos->offset = EOF; | |
797 | fprintf(stderr, "[error] get_next_index failed\n"); | |
798 | return; | |
799 | } | |
800 | ||
801 | pos->packet_size = packet_index.packet_size; | |
802 | pos->content_size = packet_index.content_size; | |
803 | pos->mmap_base_offset = 0; | |
804 | if (packet_index.offset == EOF) { | |
805 | pos->offset = EOF; | |
806 | } else { | |
807 | pos->offset = 0; | |
808 | } | |
809 | ||
810 | if (packet_index.content_size == 0) { | |
811 | file_stream->parent.cycles_timestamp = packet_index.timestamp_end; | |
812 | file_stream->parent.real_timestamp = ctf_get_real_timestamp( | |
813 | &file_stream->parent, packet_index.timestamp_end); | |
814 | } else { | |
815 | file_stream->parent.cycles_timestamp = packet_index.timestamp_begin; | |
816 | file_stream->parent.real_timestamp = ctf_get_real_timestamp( | |
817 | &file_stream->parent, packet_index.timestamp_begin); | |
818 | } | |
819 | ||
820 | if (pos->packet_size == 0 || pos->offset == EOF) { | |
821 | goto end; | |
822 | } | |
823 | ||
824 | printf_verbose("get_data_packet for stream %" PRIu64 "\n", | |
825 | viewer_stream->id); | |
826 | ret = get_data_packet(session->ctx, pos, viewer_stream, | |
827 | be64toh(packet_index.offset), | |
828 | packet_index.packet_size / CHAR_BIT); | |
829 | if (ret == -2) { | |
830 | goto retry; | |
831 | } else if (ret < 0) { | |
832 | pos->offset = EOF; | |
833 | fprintf(stderr, "[error] get_data_packet failed\n"); | |
834 | return; | |
835 | } | |
836 | ||
837 | printf_verbose("Index received : packet_size : %" PRIu64 | |
838 | ", offset %" PRIu64 ", content_size %" PRIu64 | |
839 | ", timestamp_end : %" PRIu64 "\n", | |
840 | packet_index.packet_size, packet_index.offset, | |
841 | packet_index.content_size, packet_index.timestamp_end); | |
842 | ||
843 | /* update trace_packet_header and stream_packet_context */ | |
844 | if (pos->prot != PROT_WRITE && file_stream->parent.trace_packet_header) { | |
845 | /* Read packet header */ | |
846 | ret = generic_rw(&pos->parent, &file_stream->parent.trace_packet_header->p); | |
847 | if (ret) { | |
848 | pos->offset = EOF; | |
849 | fprintf(stderr, "[error] trace packet header read failed\n"); | |
850 | goto end; | |
851 | } | |
852 | } | |
853 | if (pos->prot != PROT_WRITE && file_stream->parent.stream_packet_context) { | |
854 | /* Read packet context */ | |
855 | ret = generic_rw(&pos->parent, &file_stream->parent.stream_packet_context->p); | |
856 | if (ret) { | |
857 | pos->offset = EOF; | |
858 | fprintf(stderr, "[error] stream packet context read failed\n"); | |
859 | goto end; | |
860 | } | |
861 | } | |
862 | pos->data_offset = pos->offset; | |
863 | ||
864 | end: | |
865 | return; | |
866 | } | |
867 | ||
868 | static int del_traces(gpointer key, gpointer value, gpointer user_data) | |
869 | { | |
870 | struct bt_context *bt_ctx = user_data; | |
871 | struct lttng_live_ctf_trace *trace = value; | |
872 | int ret; | |
873 | ||
874 | ret = bt_context_remove_trace(bt_ctx, trace->trace_id); | |
875 | if (ret < 0) | |
876 | fprintf(stderr, "[error] removing trace from context\n"); | |
877 | ||
878 | /* remove the key/value pair from the HT. */ | |
879 | return 1; | |
880 | } | |
881 | ||
882 | static void add_traces(gpointer key, gpointer value, gpointer user_data) | |
883 | { | |
884 | int i, ret, total_metadata = 0; | |
885 | uint64_t metadata_len; | |
886 | struct bt_context *bt_ctx = user_data; | |
887 | struct lttng_live_ctf_trace *trace = value; | |
888 | struct lttng_live_viewer_stream *stream; | |
889 | struct bt_mmap_stream *new_mmap_stream; | |
890 | struct bt_mmap_stream_list mmap_list; | |
891 | struct lttng_live_ctx *ctx = NULL; | |
892 | ||
893 | BT_INIT_LIST_HEAD(&mmap_list.head); | |
894 | ||
895 | for (i = 0; i < trace->streams->len; i++) { | |
896 | stream = g_ptr_array_index(trace->streams, i); | |
897 | ctx = stream->session->ctx; | |
898 | ||
899 | if (!stream->metadata_flag) { | |
900 | new_mmap_stream = zmalloc(sizeof(struct bt_mmap_stream)); | |
901 | new_mmap_stream->priv = (void *) stream; | |
902 | new_mmap_stream->fd = -1; | |
903 | bt_list_add(&new_mmap_stream->list, &mmap_list.head); | |
904 | } else { | |
905 | /* Get all possible metadata before starting */ | |
906 | do { | |
907 | ret = get_new_metadata(ctx, stream, | |
908 | &metadata_len); | |
909 | if (ret == 0) { | |
910 | total_metadata += metadata_len; | |
911 | } | |
912 | } while (ret == 0 || total_metadata == 0); | |
913 | trace->metadata_fp = fopen(stream->path, "r"); | |
914 | } | |
915 | } | |
916 | ||
917 | if (!trace->metadata_fp) { | |
918 | fprintf(stderr, "[error] No metadata stream opened\n"); | |
919 | goto end_free; | |
920 | } | |
921 | ||
922 | ret = bt_context_add_trace(bt_ctx, NULL, "ctf", | |
923 | ctf_live_packet_seek, &mmap_list, trace->metadata_fp); | |
924 | if (ret < 0) { | |
925 | fprintf(stderr, "[error] Error adding trace\n"); | |
926 | goto end_free; | |
927 | } | |
928 | trace->trace_id = ret; | |
929 | ||
930 | goto end; | |
931 | ||
932 | end_free: | |
933 | bt_context_put(bt_ctx); | |
934 | end: | |
935 | return; | |
936 | } | |
937 | ||
938 | void lttng_live_read(struct lttng_live_ctx *ctx, uint64_t session_id) | |
939 | { | |
940 | int ret, active_session = 0; | |
941 | struct bt_context *bt_ctx; | |
942 | struct bt_ctf_iter *iter; | |
943 | const struct bt_ctf_event *event; | |
944 | struct bt_iter_pos begin_pos; | |
945 | struct bt_trace_descriptor *td_write; | |
946 | struct bt_format *fmt_write; | |
947 | struct ctf_text_stream_pos *sout; | |
948 | ||
949 | bt_ctx = bt_context_create(); | |
950 | if (!bt_ctx) { | |
951 | fprintf(stderr, "[error] bt_context_create allocation\n"); | |
952 | goto end; | |
953 | } | |
954 | ||
955 | fmt_write = bt_lookup_format(g_quark_from_static_string("text")); | |
956 | if (!fmt_write) { | |
957 | fprintf(stderr, "[error] ctf-text error\n"); | |
958 | goto end; | |
959 | } | |
960 | ||
961 | td_write = fmt_write->open_trace(NULL, O_RDWR, NULL, NULL); | |
962 | if (!td_write) { | |
963 | fprintf(stderr, "[error] Error opening output trace\n"); | |
964 | goto end_free; | |
965 | } | |
966 | ||
967 | sout = container_of(td_write, struct ctf_text_stream_pos, | |
968 | trace_descriptor); | |
969 | if (!sout->parent.event_cb) | |
970 | goto end_free; | |
971 | ||
972 | /* | |
973 | * As long as the session is active, we try to reattach to it, | |
974 | * even if all the streams get closed. | |
975 | */ | |
976 | do { | |
977 | int flags; | |
978 | ||
979 | do { | |
980 | ret = lttng_live_attach_session(ctx, session_id); | |
981 | printf_verbose("Attaching session returns %d\n", ret); | |
982 | if (ret < 0) { | |
983 | if (ret == -LTTNG_VIEWER_ATTACH_UNK) { | |
984 | if (active_session) | |
985 | goto end_free; | |
986 | fprintf(stderr, "[error] Unknown " | |
987 | "session ID\n"); | |
988 | } | |
989 | goto end_free; | |
990 | } else { | |
991 | active_session = 1; | |
992 | } | |
993 | } while (ctx->session->stream_count == 0); | |
994 | ||
995 | g_hash_table_foreach(ctx->session->ctf_traces, add_traces, bt_ctx); | |
996 | ||
997 | begin_pos.type = BT_SEEK_BEGIN; | |
998 | iter = bt_ctf_iter_create(bt_ctx, &begin_pos, NULL); | |
999 | if (!iter) { | |
1000 | fprintf(stderr, "[error] Iterator creation error\n"); | |
1001 | goto end; | |
1002 | } | |
1003 | for (;;) { | |
1004 | event = bt_ctf_iter_read_event_flags(iter, &flags); | |
1005 | if (!(flags & BT_ITER_FLAG_RETRY)) { | |
1006 | if (!event) { | |
1007 | /* End of trace */ | |
1008 | break; | |
1009 | } | |
1010 | ret = sout->parent.event_cb(&sout->parent, | |
1011 | event->parent->stream); | |
1012 | if (ret) { | |
1013 | fprintf(stderr, "[error] Writing " | |
1014 | "event failed.\n"); | |
1015 | goto end_free; | |
1016 | } | |
1017 | } | |
1018 | ret = bt_iter_next(bt_ctf_get_iter(iter)); | |
1019 | if (ret < 0) { | |
1020 | goto end_free; | |
1021 | } | |
1022 | } | |
1023 | bt_ctf_iter_destroy(iter); | |
1024 | g_hash_table_foreach_remove(ctx->session->ctf_traces, del_traces, bt_ctx); | |
1025 | } while (active_session); | |
1026 | ||
1027 | end_free: | |
1028 | bt_context_put(bt_ctx); | |
1029 | end: | |
1030 | return; | |
1031 | } |