Fixes in babeltrace core to support non-CTF traces
[babeltrace.git] / lib / context.c
CommitLineData
b469d2dd
JD
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.
c462e188
MD
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.
b469d2dd
JD
28 */
29
30#include <babeltrace/babeltrace.h>
31#include <babeltrace/context.h>
08c22d05 32#include <babeltrace/context-internal.h>
6cba487f
MD
33#include <babeltrace/trace-handle.h>
34#include <babeltrace/trace-handle-internal.h>
35#include <babeltrace/trace-collection.h>
36#include <babeltrace/format.h>
9a7312d3 37#include <babeltrace/format-internal.h>
e1d01c39 38#include <babeltrace/babeltrace-internal.h>
b469d2dd 39#include <stdlib.h>
e1d01c39
MD
40#include <string.h>
41#include <assert.h>
7f89ddce 42#include <errno.h>
b469d2dd 43
6cba487f
MD
44#include <fcntl.h> /* For O_RDONLY */
45
6cba487f
MD
46#include <glib.h>
47
9a7312d3
JG
48static
49void remove_trace_handle(struct bt_trace_handle *handle);
50
6cba487f 51struct bt_context *bt_context_create(void)
b469d2dd
JD
52{
53 struct bt_context *ctx;
54
6cba487f 55 ctx = g_new0(struct bt_context, 1);
b469d2dd 56 ctx->refcount = 1;
6cba487f 57 /* Negative handle id are errors. */
842c2b97 58 ctx->last_trace_handle_id = 0;
b469d2dd 59
6cba487f
MD
60 /* Instanciate the trace handle container */
61 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
62 g_direct_equal, NULL,
9a7312d3 63 (GDestroyNotify) remove_trace_handle);
6cba487f 64
e003e871 65 ctx->current_iterator = NULL;
6cba487f 66 ctx->tc = g_new0(struct trace_collection, 1);
2552c374 67 bt_init_trace_collection(ctx->tc);
6cba487f 68
b469d2dd 69 return ctx;
6cba487f 70}
b469d2dd 71
6cba487f 72int bt_context_add_trace(struct bt_context *ctx, const char *path,
613f532b 73 const char *format_name,
1cf393f6 74 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
0d4c669f 75 int whence),
c150f912 76 struct bt_mmap_stream_list *stream_list,
0d4c669f 77 FILE *metadata)
6cba487f 78{
1b8455b7 79 struct bt_trace_descriptor *td;
37b99bdb 80 struct bt_format *fmt;
6cba487f 81 struct bt_trace_handle *handle;
f824ae04 82 int ret, closeret;
6cba487f 83
7f89ddce
MD
84 if (!ctx || !format_name || (!path && !stream_list))
85 return -EINVAL;
86
282e1952
MD
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 }
0d4c669f
MD
94 if (path) {
95 td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
96 if (!td) {
abc40d24 97 fprintf(stderr, "[warning] [Context] Cannot open_trace of format %s at path %s.\n",
0d575aea 98 format_name, path);
0d4c669f
MD
99 ret = -1;
100 goto end;
101 }
102 } else {
103 td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
104 if (!td) {
0d575aea
SM
105 fprintf(stderr, "[error] [Context] Cannot open_mmap_trace of format %s.\n\n",
106 format_name);
0d4c669f
MD
107 ret = -1;
108 goto end;
109 }
6cba487f
MD
110 }
111
112 /* Create an handle for the trace */
113 handle = bt_trace_handle_create(ctx);
642871ed 114 if (!handle) {
02dc4610 115 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
6cba487f 116 path);
1059a2bf 117 ret = -1;
e6d85e38 118 goto error;
6cba487f
MD
119 }
120 handle->format = fmt;
121 handle->td = td;
afe9cd4a
JD
122 if (path) {
123 strncpy(handle->path, path, PATH_MAX);
124 handle->path[PATH_MAX - 1] = '\0';
125 }
6cba487f 126
98a04903
JD
127 if (fmt->set_handle)
128 fmt->set_handle(td, handle);
129 if (fmt->set_context)
130 fmt->set_context(td, ctx);
131
6cba487f
MD
132 /* Add new handle to container */
133 g_hash_table_insert(ctx->trace_handles,
134 (gpointer) (unsigned long) handle->id,
135 handle);
2552c374 136 ret = bt_trace_collection_add(ctx->tc, td);
03798a93 137 if (ret != 0)
e6d85e38 138 goto error;
03798a93 139
34d99418
JD
140 if (fmt->convert_index_timestamp) {
141 ret = fmt->convert_index_timestamp(td);
142 if (ret < 0)
143 goto error;
144 }
03798a93 145
34d99418
JD
146 if (fmt->timestamp_begin)
147 handle->real_timestamp_begin = fmt->timestamp_begin(td,
148 handle, BT_CLOCK_REAL);
149 if (fmt->timestamp_end)
150 handle->real_timestamp_end = fmt->timestamp_end(td, handle,
151 BT_CLOCK_REAL);
152 if (fmt->timestamp_begin)
153 handle->cycles_timestamp_begin = fmt->timestamp_begin(td,
154 handle, BT_CLOCK_CYCLES);
155 if (fmt->timestamp_end)
156 handle->cycles_timestamp_end = fmt->timestamp_end(td, handle,
157 BT_CLOCK_CYCLES);
03798a93
JD
158
159 return handle->id;
e6d85e38
JD
160
161error:
f824ae04
MD
162 closeret = fmt->close_trace(td);
163 if (closeret) {
164 fprintf(stderr, "Error in close_trace callback\n");
165 }
1059a2bf
JD
166end:
167 return ret;
b469d2dd
JD
168}
169
7f89ddce 170int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
b469d2dd 171{
9a7312d3 172 int ret = 0;
6cba487f 173
9a7312d3
JG
174 if (!ctx) {
175 ret = -EINVAL;
176 goto end;
177 }
6cba487f 178
9a7312d3
JG
179 /*
180 * Remove the handle. remove_trace_handle will be called
181 * automatically.
182 */
183 if (!g_hash_table_remove(ctx->trace_handles,
184 (gpointer) (unsigned long) handle_id)) {
185 ret = -ENOENT;
186 goto end;
f824ae04 187 }
9a7312d3
JG
188end:
189 return ret;
6cba487f
MD
190}
191
192static
193void bt_context_destroy(struct bt_context *ctx)
194{
7f89ddce 195 assert(ctx);
6cba487f 196
e6d85e38
JD
197 /*
198 * Remove all traces. The g_hash_table_destroy will call
9a7312d3 199 * remove_trace_handle on each element.
6cba487f
MD
200 */
201 g_hash_table_destroy(ctx->trace_handles);
202
9a7312d3
JG
203 bt_finalize_trace_collection(ctx->tc);
204
6cba487f
MD
205 /* ctx->tc should always be valid */
206 assert(ctx->tc != NULL);
207 g_free(ctx->tc);
208 g_free(ctx);
b469d2dd
JD
209}
210
6cba487f 211void bt_context_get(struct bt_context *ctx)
b469d2dd 212{
7f89ddce 213 assert(ctx);
6cba487f
MD
214 ctx->refcount++;
215}
b469d2dd 216
6cba487f
MD
217void bt_context_put(struct bt_context *ctx)
218{
7f89ddce 219 assert(ctx);
b469d2dd
JD
220 ctx->refcount--;
221 if (ctx->refcount == 0)
6cba487f 222 bt_context_destroy(ctx);
b469d2dd 223}
9a7312d3
JG
224
225static
226void remove_trace_handle(struct bt_trace_handle *handle)
227{
228 int ret;
229
34d99418
JD
230 if (!handle->td->ctx)
231 return;
9a7312d3
JG
232 /* Remove from containers */
233 bt_trace_collection_remove(handle->td->ctx->tc, handle->td);
234 /* Close the trace */
235 ret = handle->format->close_trace(handle->td);
236 if (ret) {
237 fprintf(stderr, "Error in close_trace callback\n");
238 }
239
240 bt_trace_handle_destroy(handle);
241}
This page took 0.037752 seconds and 4 git commands to generate.