+ ctx = g_new0(struct bt_context, 1);
+ ctx->refcount = 1;
+ /* Negative handle id are errors. */
+ ctx->last_trace_handle_id = 0;
+
+ /* Instanciate the trace handle container */
+ ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
+ g_direct_equal, NULL,
+ (GDestroyNotify) remove_trace_handle);
+
+ ctx->current_iterator = NULL;
+ ctx->tc = g_new0(struct trace_collection, 1);
+ bt_init_trace_collection(ctx->tc);
+
+ return ctx;
+}
+
+int bt_context_add_trace(struct bt_context *ctx, const char *path,
+ const char *format_name,
+ void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
+ int whence),
+ struct bt_mmap_stream_list *stream_list,
+ FILE *metadata)
+{
+ struct bt_trace_descriptor *td;
+ struct bt_format *fmt;
+ struct bt_trace_handle *handle;
+ int ret, closeret;
+
+ if (!ctx || !format_name || (!path && !stream_list))
+ return -EINVAL;
+
+ fmt = bt_lookup_format(g_quark_from_string(format_name));
+ if (!fmt) {
+ fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
+ format_name);
+ ret = -1;
+ goto end;
+ }
+ if (path) {
+ td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
+ if (!td) {
+ fprintf(stderr, "[warning] [Context] Cannot open_trace of format %s at path %s.\n",
+ format_name, path);
+ ret = -1;
+ goto end;
+ }
+ } else {
+ td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
+ if (!td) {
+ fprintf(stderr, "[error] [Context] Cannot open_mmap_trace of format %s.\n\n",
+ format_name);
+ ret = -1;
+ goto end;
+ }
+ }
+
+ /* Create an handle for the trace */
+ handle = bt_trace_handle_create(ctx);
+ if (!handle) {
+ fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
+ path);
+ ret = -1;