Commit | Line | Data |
---|---|---|
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 | ||
41 | static 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 | ||
92 | end: | |
93 | return ret; | |
94 | } | |
95 | ||
96 | static 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); | |
3af4fc48 | 109 | |
4a744367 JD |
110 | /* We need a pointer to the context from the packet_seek function. */ |
111 | ctx.session->ctx = &ctx; | |
112 | ||
113 | /* HT to store the CTF traces. */ | |
114 | ctx.session->ctf_traces = g_hash_table_new(g_direct_hash, | |
115 | g_direct_equal); | |
116 | ||
117 | ret = parse_url(path, hostname, &port, &session_id); | |
118 | if (ret < 0) { | |
119 | goto end_free; | |
120 | } | |
121 | ||
122 | ret = lttng_live_connect_viewer(&ctx, hostname, port); | |
123 | if (ret < 0) { | |
124 | fprintf(stderr, "[error] Connection failed\n"); | |
125 | goto end_free; | |
126 | } | |
127 | printf_verbose("LTTng-live connected to relayd\n"); | |
128 | ||
129 | ret = lttng_live_establish_connection(&ctx); | |
130 | if (ret < 0) { | |
131 | goto end_free; | |
132 | } | |
133 | ||
134 | if (session_id == -1ULL) { | |
135 | printf_verbose("Listing sessions\n"); | |
136 | ret = lttng_live_list_sessions(&ctx, path); | |
137 | if (ret < 0) { | |
138 | fprintf(stderr, "[error] List error\n"); | |
139 | goto end_free; | |
140 | } | |
141 | } else { | |
142 | lttng_live_read(&ctx, session_id); | |
143 | } | |
144 | ||
145 | end_free: | |
146 | g_hash_table_destroy(ctx.session->ctf_traces); | |
147 | g_free(ctx.session); | |
4a744367 JD |
148 | g_free(ctx.session->streams); |
149 | return ret; | |
150 | } | |
151 | ||
152 | static | |
153 | struct bt_trace_descriptor *lttng_live_open_trace(const char *path, int flags, | |
154 | void (*packet_seek)(struct bt_stream_pos *pos, size_t index, | |
155 | int whence), FILE *metadata_fp) | |
156 | { | |
157 | struct ctf_text_stream_pos *pos; | |
158 | ||
159 | switch (flags & O_ACCMODE) { | |
160 | case O_RDONLY: | |
161 | /* OK */ | |
162 | break; | |
163 | case O_RDWR: | |
164 | fprintf(stderr, "[error] lttng live plugin cannot be used as output plugin.\n"); | |
165 | goto error; | |
166 | default: | |
167 | fprintf(stderr, "[error] Incorrect open flags.\n"); | |
168 | goto error; | |
169 | } | |
170 | ||
171 | pos = g_new0(struct ctf_text_stream_pos, 1); | |
172 | pos->parent.rw_table = NULL; | |
173 | pos->parent.event_cb = NULL; | |
174 | pos->parent.trace = &pos->trace_descriptor; | |
175 | lttng_live_open_trace_read(path); | |
176 | return &pos->trace_descriptor; | |
177 | ||
178 | error: | |
179 | return NULL; | |
180 | } | |
181 | ||
182 | static | |
183 | int lttng_live_close_trace(struct bt_trace_descriptor *td) | |
184 | { | |
185 | struct ctf_text_stream_pos *pos = | |
186 | container_of(td, struct ctf_text_stream_pos, | |
187 | trace_descriptor); | |
188 | free(pos); | |
189 | return 0; | |
190 | } | |
191 | ||
192 | static | |
193 | struct bt_format lttng_live_format = { | |
194 | .open_trace = lttng_live_open_trace, | |
195 | .close_trace = lttng_live_close_trace, | |
196 | }; | |
197 | ||
198 | static | |
199 | void __attribute__((constructor)) lttng_live_init(void) | |
200 | { | |
201 | int ret; | |
202 | ||
203 | lttng_live_format.name = g_quark_from_static_string("lttng-live"); | |
204 | ret = bt_register_format(<tng_live_format); | |
205 | assert(!ret); | |
206 | } | |
207 | ||
208 | static | |
209 | void __attribute__((destructor)) lttng_live_exit(void) | |
210 | { | |
211 | bt_unregister_format(<tng_live_format); | |
212 | } |