| 1 | /* |
| 2 | * context.c |
| 3 | * |
| 4 | * Babeltrace Library |
| 5 | * |
| 6 | * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * Julien Desfossez <julien.desfossez@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <babeltrace/babeltrace.h> |
| 31 | #include <babeltrace/context.h> |
| 32 | #include <babeltrace/context-internal.h> |
| 33 | #include <babeltrace/trace-handle.h> |
| 34 | #include <babeltrace/trace-handle-internal.h> |
| 35 | #include <babeltrace/trace-collection.h> |
| 36 | #include <babeltrace/format.h> |
| 37 | #include <babeltrace/babeltrace-internal.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include <assert.h> |
| 41 | #include <errno.h> |
| 42 | |
| 43 | #include <fcntl.h> /* For O_RDONLY */ |
| 44 | |
| 45 | #include <glib.h> |
| 46 | |
| 47 | struct bt_context *bt_context_create(void) |
| 48 | { |
| 49 | struct bt_context *ctx; |
| 50 | |
| 51 | ctx = g_new0(struct bt_context, 1); |
| 52 | ctx->refcount = 1; |
| 53 | /* Negative handle id are errors. */ |
| 54 | ctx->last_trace_handle_id = 0; |
| 55 | |
| 56 | /* Instanciate the trace handle container */ |
| 57 | ctx->trace_handles = g_hash_table_new_full(g_direct_hash, |
| 58 | g_direct_equal, NULL, |
| 59 | (GDestroyNotify) bt_trace_handle_destroy); |
| 60 | |
| 61 | ctx->current_iterator = NULL; |
| 62 | ctx->tc = g_new0(struct trace_collection, 1); |
| 63 | bt_init_trace_collection(ctx->tc); |
| 64 | |
| 65 | return ctx; |
| 66 | } |
| 67 | |
| 68 | int bt_context_add_trace(struct bt_context *ctx, const char *path, |
| 69 | const char *format_name, |
| 70 | void (*packet_seek)(struct stream_pos *pos, size_t index, |
| 71 | int whence), |
| 72 | struct mmap_stream_list *stream_list, |
| 73 | FILE *metadata) |
| 74 | { |
| 75 | struct trace_descriptor *td; |
| 76 | struct format *fmt; |
| 77 | struct bt_trace_handle *handle; |
| 78 | int ret, closeret; |
| 79 | |
| 80 | if (!ctx || !format_name || (!path && !stream_list)) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | fmt = bt_lookup_format(g_quark_from_string(format_name)); |
| 84 | if (!fmt) { |
| 85 | fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n", |
| 86 | format_name); |
| 87 | ret = -1; |
| 88 | goto end; |
| 89 | } |
| 90 | if (path) { |
| 91 | td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL); |
| 92 | if (!td) { |
| 93 | fprintf(stderr, "[warning] [Context] Cannot open_trace of format %s at path %s.\n", |
| 94 | format_name, path); |
| 95 | ret = -1; |
| 96 | goto end; |
| 97 | } |
| 98 | } else { |
| 99 | td = fmt->open_mmap_trace(stream_list, packet_seek, metadata); |
| 100 | if (!td) { |
| 101 | fprintf(stderr, "[error] [Context] Cannot open_mmap_trace of format %s.\n\n", |
| 102 | format_name); |
| 103 | ret = -1; |
| 104 | goto end; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* Create an handle for the trace */ |
| 109 | handle = bt_trace_handle_create(ctx); |
| 110 | if (handle < 0) { |
| 111 | fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n", |
| 112 | path); |
| 113 | ret = -1; |
| 114 | goto error; |
| 115 | } |
| 116 | handle->format = fmt; |
| 117 | handle->td = td; |
| 118 | if (path) { |
| 119 | strncpy(handle->path, path, PATH_MAX); |
| 120 | handle->path[PATH_MAX - 1] = '\0'; |
| 121 | } |
| 122 | |
| 123 | if (fmt->set_handle) |
| 124 | fmt->set_handle(td, handle); |
| 125 | if (fmt->set_context) |
| 126 | fmt->set_context(td, ctx); |
| 127 | |
| 128 | /* Add new handle to container */ |
| 129 | g_hash_table_insert(ctx->trace_handles, |
| 130 | (gpointer) (unsigned long) handle->id, |
| 131 | handle); |
| 132 | ret = bt_trace_collection_add(ctx->tc, td); |
| 133 | if (ret != 0) |
| 134 | goto error; |
| 135 | |
| 136 | ret = fmt->convert_index_timestamp(td); |
| 137 | if (ret < 0) |
| 138 | goto error; |
| 139 | |
| 140 | handle->real_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL); |
| 141 | handle->real_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_REAL); |
| 142 | handle->cycles_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES); |
| 143 | handle->cycles_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES); |
| 144 | |
| 145 | return handle->id; |
| 146 | |
| 147 | error: |
| 148 | closeret = fmt->close_trace(td); |
| 149 | if (closeret) { |
| 150 | fprintf(stderr, "Error in close_trace callback\n"); |
| 151 | } |
| 152 | end: |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | int bt_context_remove_trace(struct bt_context *ctx, int handle_id) |
| 157 | { |
| 158 | struct bt_trace_handle *handle; |
| 159 | int ret; |
| 160 | |
| 161 | if (!ctx) |
| 162 | return -EINVAL; |
| 163 | |
| 164 | handle = g_hash_table_lookup(ctx->trace_handles, |
| 165 | (gpointer) (unsigned long) handle_id); |
| 166 | if (!handle) |
| 167 | return -ENOENT; |
| 168 | |
| 169 | /* Remove from containers */ |
| 170 | bt_trace_collection_remove(ctx->tc, handle->td); |
| 171 | /* Close the trace */ |
| 172 | ret = handle->format->close_trace(handle->td); |
| 173 | if (ret) { |
| 174 | fprintf(stderr, "Error in close_trace callback\n"); |
| 175 | return ret; |
| 176 | } |
| 177 | /* Remove and free the handle */ |
| 178 | g_hash_table_remove(ctx->trace_handles, |
| 179 | (gpointer) (unsigned long) handle_id); |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static |
| 184 | void bt_context_destroy(struct bt_context *ctx) |
| 185 | { |
| 186 | assert(ctx); |
| 187 | bt_finalize_trace_collection(ctx->tc); |
| 188 | |
| 189 | /* |
| 190 | * Remove all traces. The g_hash_table_destroy will call |
| 191 | * bt_trace_handle_destroy on each elements. |
| 192 | */ |
| 193 | g_hash_table_destroy(ctx->trace_handles); |
| 194 | |
| 195 | /* ctx->tc should always be valid */ |
| 196 | assert(ctx->tc != NULL); |
| 197 | g_free(ctx->tc); |
| 198 | g_free(ctx); |
| 199 | } |
| 200 | |
| 201 | void bt_context_get(struct bt_context *ctx) |
| 202 | { |
| 203 | assert(ctx); |
| 204 | ctx->refcount++; |
| 205 | } |
| 206 | |
| 207 | void bt_context_put(struct bt_context *ctx) |
| 208 | { |
| 209 | assert(ctx); |
| 210 | ctx->refcount--; |
| 211 | if (ctx->refcount == 0) |
| 212 | bt_context_destroy(ctx); |
| 213 | } |