Handle negative time and offset from Epoch
[babeltrace.git] / lib / context.c
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/format-internal.h>
38 #include <babeltrace/babeltrace-internal.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <assert.h>
42 #include <errno.h>
43
44 #include <fcntl.h> /* For O_RDONLY */
45
46 #include <glib.h>
47
48 static
49 void remove_trace_handle(struct bt_trace_handle *handle);
50
51 struct bt_context *bt_context_create(void)
52 {
53 struct bt_context *ctx;
54
55 ctx = g_new0(struct bt_context, 1);
56 ctx->refcount = 1;
57 /* Negative handle id are errors. */
58 ctx->last_trace_handle_id = 0;
59
60 /* Instanciate the trace handle container */
61 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
62 g_direct_equal, NULL,
63 (GDestroyNotify) remove_trace_handle);
64
65 ctx->current_iterator = NULL;
66 ctx->tc = g_new0(struct trace_collection, 1);
67 bt_init_trace_collection(ctx->tc);
68
69 return ctx;
70 }
71
72 int bt_context_add_trace(struct bt_context *ctx, const char *path,
73 const char *format_name,
74 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
75 int whence),
76 struct bt_mmap_stream_list *stream_list,
77 FILE *metadata)
78 {
79 struct bt_trace_descriptor *td;
80 struct bt_format *fmt;
81 struct bt_trace_handle *handle;
82 int ret, closeret;
83
84 if (!ctx || !format_name || (!path && !stream_list))
85 return -EINVAL;
86
87 fmt = bt_lookup_format(g_quark_from_string(format_name));
88 if (!fmt) {
89 fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
90 format_name);
91 ret = -1;
92 goto end;
93 }
94 if (path) {
95 td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
96 if (!td) {
97 fprintf(stderr, "[warning] [Context] Cannot open_trace of format %s at path %s.\n",
98 format_name, path);
99 ret = -1;
100 goto end;
101 }
102 } else {
103 td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
104 if (!td) {
105 fprintf(stderr, "[error] [Context] Cannot open_mmap_trace of format %s.\n\n",
106 format_name);
107 ret = -1;
108 goto end;
109 }
110 }
111
112 /* Create an handle for the trace */
113 handle = bt_trace_handle_create(ctx);
114 if (!handle) {
115 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
116 path);
117 ret = -1;
118 goto error_close;
119 }
120 handle->format = fmt;
121 handle->td = td;
122 if (path) {
123 strncpy(handle->path, path, PATH_MAX);
124 handle->path[PATH_MAX - 1] = '\0';
125 }
126
127 ret = bt_trace_collection_add(ctx->tc, td);
128 if (ret != 0)
129 goto error_destroy_handle;
130
131 if (fmt->set_handle)
132 fmt->set_handle(td, handle);
133 if (fmt->set_context)
134 fmt->set_context(td, ctx);
135
136 if (fmt->convert_index_timestamp) {
137 ret = fmt->convert_index_timestamp(td);
138 if (ret < 0)
139 goto error_collection_del;
140 }
141
142 if (fmt->timestamp_begin) {
143 ret = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL,
144 &handle->real_timestamp_begin);
145 if (ret < 0 && ret != -ENOENT) {
146 ret = -1;
147 goto error_collection_del;
148 }
149 }
150 if (fmt->timestamp_end) {
151 ret = fmt->timestamp_end(td, handle, BT_CLOCK_REAL,
152 &handle->real_timestamp_end);
153 if (ret < 0 && ret != -ENOENT) {
154 ret = -1;
155 goto error_collection_del;
156 }
157 }
158 if (fmt->timestamp_begin) {
159 ret = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES,
160 &handle->cycles_timestamp_begin);
161 if (ret < 0 && ret != -ENOENT) {
162 ret = -1;
163 goto error_collection_del;
164 }
165 }
166 if (fmt->timestamp_end) {
167 ret = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES,
168 &handle->cycles_timestamp_end);
169 if (ret < 0 && ret != -ENOENT) {
170 ret = -1;
171 goto error_collection_del;
172 }
173 }
174
175 /* Add new handle to container */
176 g_hash_table_insert(ctx->trace_handles,
177 (gpointer) (unsigned long) handle->id,
178 handle);
179
180 return handle->id;
181
182 error_collection_del:
183 /* Remove from containers */
184 bt_trace_collection_remove(handle->td->ctx->tc, handle->td);
185 error_destroy_handle:
186 bt_trace_handle_destroy(handle);
187 error_close:
188 closeret = fmt->close_trace(td);
189 if (closeret) {
190 fprintf(stderr, "Error in close_trace callback\n");
191 }
192 end:
193 return ret;
194 }
195
196 int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
197 {
198 int ret = 0;
199
200 if (!ctx) {
201 ret = -EINVAL;
202 goto end;
203 }
204
205 /*
206 * Remove the handle. remove_trace_handle will be called
207 * automatically.
208 */
209 if (!g_hash_table_remove(ctx->trace_handles,
210 (gpointer) (unsigned long) handle_id)) {
211 ret = -ENOENT;
212 goto end;
213 }
214 end:
215 return ret;
216 }
217
218 static
219 void bt_context_destroy(struct bt_context *ctx)
220 {
221 assert(ctx);
222
223 /*
224 * Remove all traces. The g_hash_table_destroy will call
225 * remove_trace_handle on each element.
226 */
227 g_hash_table_destroy(ctx->trace_handles);
228
229 bt_finalize_trace_collection(ctx->tc);
230
231 /* ctx->tc should always be valid */
232 assert(ctx->tc != NULL);
233 g_free(ctx->tc);
234 g_free(ctx);
235 }
236
237 void bt_context_get(struct bt_context *ctx)
238 {
239 assert(ctx);
240 ctx->refcount++;
241 }
242
243 void bt_context_put(struct bt_context *ctx)
244 {
245 assert(ctx);
246 ctx->refcount--;
247 if (ctx->refcount == 0)
248 bt_context_destroy(ctx);
249 }
250
251 static
252 void remove_trace_handle(struct bt_trace_handle *handle)
253 {
254 int ret;
255
256 if (!handle->td->ctx)
257 return;
258 /* Remove from containers */
259 bt_trace_collection_remove(handle->td->ctx->tc, handle->td);
260 /* Close the trace */
261 ret = handle->format->close_trace(handle->td);
262 if (ret) {
263 fprintf(stderr, "Error in close_trace callback\n");
264 }
265
266 bt_trace_handle_destroy(handle);
267 }
This page took 0.034916 seconds and 4 git commands to generate.