Cleanup: iterator unused pointer value
[babeltrace.git] / formats / lttng-live / lttng-live.c
CommitLineData
4a744367
JD
1/*
2 * BabelTrace - LTTng live Output
3 *
4 * Copyright 2013 Julien Desfossez <jdesfossez@efficios.com>
5 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#include <babeltrace/ctf-text/types.h>
27#include <babeltrace/format.h>
28#include <babeltrace/babeltrace-internal.h>
29#include <inttypes.h>
30#include <sys/mman.h>
31#include <errno.h>
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <fcntl.h>
35#include <dirent.h>
36#include <glib.h>
37#include <unistd.h>
38#include <stdlib.h>
39#include "lttng-live-functions.h"
40
41static int parse_url(const char *path, char *hostname, int *port,
42 uint64_t *session_id)
43{
44 char remain[NAME_MAX];
45 int ret, proto, proto_offset = 0;
46
47 ret = sscanf(path, "net%d%s", &proto, remain);
48 if (ret < 2) {
49 proto = 4;
50 /* net:// */
51 proto_offset = strlen("net://");
52 } else {
53 /* net4:// or net6:// */
54 proto_offset = strlen("netX://");
55 }
56 /* TODO : parse for IPv6 as well */
57 /* Parse the hostname or IP */
58 ret = sscanf(path + proto_offset, "%[a-zA-Z.1-9%-]%s",
59 hostname, remain);
60 if (ret == 2) {
61 /* Optional port number */
62 if (remain[0] == ':') {
63 ret = sscanf(remain, ":%d%s", port, remain);
64 /* Optional session ID with port number */
65 if (ret == 2) {
66 ret = sscanf(remain, "/%" PRIu64, session_id);
67 }
68 /* Optional session ID */
69 } else if (remain[0] == '/') {
70 ret = sscanf(remain, "/%" PRIu64, session_id);
71 } else {
72 fprintf(stderr, "[error] wrong delimitor : %c\n",
73 remain[0]);
74 ret = -1;
75 goto end;
76 }
77 }
78
79 if (*port < 0)
80 *port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT;
81
82 if (*session_id == -1ULL)
83 printf_verbose("Connecting to hostname : %s, port : %d, "
84 "proto : IPv%d\n",
85 hostname, *port, proto);
86 else
87 printf_verbose("Connecting to hostname : %s, port : %d, "
88 "session id : %" PRIu64 ", proto : IPv%d\n",
89 hostname, *port, *session_id, proto);
90 ret = 0;
91
92end:
93 return ret;
94}
95
96static int lttng_live_open_trace_read(const char *path)
97{
98 char hostname[NAME_MAX];
99 int port = -1;
100 uint64_t session_id = -1ULL;
101 int ret = 0;
102 struct lttng_live_ctx ctx;
103
104 /* set default */
105 opt_context_field_names = 1;
106 opt_payload_field_names = 1;
107
108 ctx.session = g_new0(struct lttng_live_session, 1);
109 if (!ctx.session) {
110 ret = -1;
111 goto end;
112 }
113 /* We need a pointer to the context from the packet_seek function. */
114 ctx.session->ctx = &ctx;
115
116 /* HT to store the CTF traces. */
117 ctx.session->ctf_traces = g_hash_table_new(g_direct_hash,
118 g_direct_equal);
119
120 ret = parse_url(path, hostname, &port, &session_id);
121 if (ret < 0) {
122 goto end_free;
123 }
124
125 ret = lttng_live_connect_viewer(&ctx, hostname, port);
126 if (ret < 0) {
127 fprintf(stderr, "[error] Connection failed\n");
128 goto end_free;
129 }
130 printf_verbose("LTTng-live connected to relayd\n");
131
132 ret = lttng_live_establish_connection(&ctx);
133 if (ret < 0) {
134 goto end_free;
135 }
136
137 if (session_id == -1ULL) {
138 printf_verbose("Listing sessions\n");
139 ret = lttng_live_list_sessions(&ctx, path);
140 if (ret < 0) {
141 fprintf(stderr, "[error] List error\n");
142 goto end_free;
143 }
144 } else {
145 lttng_live_read(&ctx, session_id);
146 }
147
148end_free:
149 g_hash_table_destroy(ctx.session->ctf_traces);
150 g_free(ctx.session);
151end:
152 g_free(ctx.session->streams);
153 return ret;
154}
155
156static
157struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags,
158 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
159 int whence), FILE *metadata_fp)
160{
161 struct ctf_text_stream_pos *pos;
162
163 switch (flags & O_ACCMODE) {
164 case O_RDONLY:
165 /* OK */
166 break;
167 case O_RDWR:
168 fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n");
169 goto error;
170 default:
171 fprintf(stderr, "[error] Incorrect open flags.\n");
172 goto error;
173 }
174
175 pos = g_new0(struct ctf_text_stream_pos, 1);
176 pos->parent.rw_table = NULL;
177 pos->parent.event_cb = NULL;
178 pos->parent.trace = &pos->trace_descriptor;
179 lttng_live_open_trace_read(path);
180 return &pos->trace_descriptor;
181
182error:
183 return NULL;
184}
185
186static
187int lttng_live_close_trace(struct bt_trace_descriptor *td)
188{
189 struct ctf_text_stream_pos *pos =
190 container_of(td, struct ctf_text_stream_pos,
191 trace_descriptor);
192 free(pos);
193 return 0;
194}
195
196static
197struct bt_format lttng_live_format = {
198 .open_trace = lttng_live_open_trace,
199 .close_trace = lttng_live_close_trace,
200};
201
202static
203void __attribute__((constructor)) lttng_live_init(void)
204{
205 int ret;
206
207 lttng_live_format.name = g_quark_from_static_string("lttng-live");
208 ret = bt_register_format(&lttng_live_format);
209 assert(!ret);
210}
211
212static
213void __attribute__((destructor)) lttng_live_exit(void)
214{
215 bt_unregister_format(&lttng_live_format);
216}
This page took 0.030592 seconds and 4 git commands to generate.