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