6 * Copyright 2011-2012 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * Julien Desfossez <julien.desfossez@efficios.com>
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:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
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
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>
44 #include <fcntl.h> /* For O_RDONLY */
49 void remove_trace_handle(struct bt_trace_handle
*handle
);
51 struct bt_context
*bt_context_create(void)
53 struct bt_context
*ctx
;
55 ctx
= g_new0(struct bt_context
, 1);
57 /* Negative handle id are errors. */
58 ctx
->last_trace_handle_id
= 0;
60 /* Instanciate the trace handle container */
61 ctx
->trace_handles
= g_hash_table_new_full(g_direct_hash
,
63 (GDestroyNotify
) remove_trace_handle
);
65 ctx
->current_iterator
= NULL
;
66 ctx
->tc
= g_new0(struct trace_collection
, 1);
67 bt_init_trace_collection(ctx
->tc
);
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
,
76 struct bt_mmap_stream_list
*stream_list
,
79 struct bt_trace_descriptor
*td
;
80 struct bt_format
*fmt
;
81 struct bt_trace_handle
*handle
;
84 if (!ctx
|| !format_name
|| (!path
&& !stream_list
))
87 fmt
= bt_lookup_format(g_quark_from_string(format_name
));
89 fprintf(stderr
, "[error] [Context] Format \"%s\" unknown.\n\n",
95 td
= fmt
->open_trace(path
, O_RDONLY
, packet_seek
, NULL
);
97 fprintf(stderr
, "[warning] [Context] Cannot open_trace of format %s at path %s.\n",
103 td
= fmt
->open_mmap_trace(stream_list
, packet_seek
, metadata
);
105 fprintf(stderr
, "[error] [Context] Cannot open_mmap_trace of format %s.\n\n",
112 /* Create an handle for the trace */
113 handle
= bt_trace_handle_create(ctx
);
115 fprintf(stderr
, "[error] [Context] Creating trace handle %s .\n\n",
120 handle
->format
= fmt
;
123 strncpy(handle
->path
, path
, PATH_MAX
);
124 handle
->path
[PATH_MAX
- 1] = '\0';
128 fmt
->set_handle(td
, handle
);
129 if (fmt
->set_context
)
130 fmt
->set_context(td
, ctx
);
132 /* Add new handle to container */
133 g_hash_table_insert(ctx
->trace_handles
,
134 (gpointer
) (unsigned long) handle
->id
,
136 ret
= bt_trace_collection_add(ctx
->tc
, td
);
140 ret
= fmt
->convert_index_timestamp(td
);
144 handle
->real_timestamp_begin
= fmt
->timestamp_begin(td
, handle
, BT_CLOCK_REAL
);
145 handle
->real_timestamp_end
= fmt
->timestamp_end(td
, handle
, BT_CLOCK_REAL
);
146 handle
->cycles_timestamp_begin
= fmt
->timestamp_begin(td
, handle
, BT_CLOCK_CYCLES
);
147 handle
->cycles_timestamp_end
= fmt
->timestamp_end(td
, handle
, BT_CLOCK_CYCLES
);
152 closeret
= fmt
->close_trace(td
);
154 fprintf(stderr
, "Error in close_trace callback\n");
160 int bt_context_remove_trace(struct bt_context
*ctx
, int handle_id
)
170 * Remove the handle. remove_trace_handle will be called
173 if (!g_hash_table_remove(ctx
->trace_handles
,
174 (gpointer
) (unsigned long) handle_id
)) {
183 void bt_context_destroy(struct bt_context
*ctx
)
188 * Remove all traces. The g_hash_table_destroy will call
189 * remove_trace_handle on each element.
191 g_hash_table_destroy(ctx
->trace_handles
);
193 bt_finalize_trace_collection(ctx
->tc
);
195 /* ctx->tc should always be valid */
196 assert(ctx
->tc
!= NULL
);
201 void bt_context_get(struct bt_context
*ctx
)
207 void bt_context_put(struct bt_context
*ctx
)
211 if (ctx
->refcount
== 0)
212 bt_context_destroy(ctx
);
216 void remove_trace_handle(struct bt_trace_handle
*handle
)
220 /* Remove from containers */
221 bt_trace_collection_remove(handle
->td
->ctx
->tc
, handle
->td
);
222 /* Close the trace */
223 ret
= handle
->format
->close_trace(handle
->td
);
225 fprintf(stderr
, "Error in close_trace callback\n");
228 bt_trace_handle_destroy(handle
);
This page took 0.051432 seconds and 4 git commands to generate.