ctf.fs: bt_ctf_notif_iter_create(): assert() that all medops exist
[babeltrace.git] / lib / context.c
index 5820fb3e5533ba97693044ce0e419ca0382545d8..56ac320e393e037dc0720826dd06dee519b7d070 100644 (file)
@@ -115,7 +115,7 @@ int bt_context_add_trace(struct bt_context *ctx, const char *path,
                fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
                                path);
                ret = -1;
-               goto error;
+               goto error_close;
        }
        handle->format = fmt;
        handle->td = td;
@@ -124,31 +124,67 @@ int bt_context_add_trace(struct bt_context *ctx, const char *path,
                handle->path[PATH_MAX - 1] = '\0';
        }
 
+       ret = bt_trace_collection_add(ctx->tc, td);
+       if (ret != 0)
+               goto error_destroy_handle;
+
        if (fmt->set_handle)
                fmt->set_handle(td, handle);
        if (fmt->set_context)
                fmt->set_context(td, ctx);
 
+       if (fmt->convert_index_timestamp) {
+               ret = fmt->convert_index_timestamp(td);
+               if (ret < 0)
+                       goto error_collection_del;
+       }
+
+       if (fmt->timestamp_begin) {
+               ret = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL,
+                               &handle->real_timestamp_begin);
+               if (ret < 0 && ret != -ENOENT) {
+                       ret = -1;
+                       goto error_collection_del;
+               }
+       }
+       if (fmt->timestamp_end) {
+               ret = fmt->timestamp_end(td, handle, BT_CLOCK_REAL,
+                               &handle->real_timestamp_end);
+               if (ret < 0 && ret != -ENOENT) {
+                       ret = -1;
+                       goto error_collection_del;
+               }
+       }
+       if (fmt->timestamp_begin) {
+               ret = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES,
+                               &handle->cycles_timestamp_begin);
+               if (ret < 0 && ret != -ENOENT) {
+                       ret = -1;
+                       goto error_collection_del;
+               }
+       }
+       if (fmt->timestamp_end) {
+               ret = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES,
+                               &handle->cycles_timestamp_end);
+               if (ret < 0 && ret != -ENOENT) {
+                       ret = -1;
+                       goto error_collection_del;
+               }
+       }
+
        /* Add new handle to container */
        g_hash_table_insert(ctx->trace_handles,
-               (gpointer) (unsigned long) handle->id,
+               GUINT_TO_POINTER(handle->id),
                handle);
-       ret = bt_trace_collection_add(ctx->tc, td);
-       if (ret != 0)
-               goto error;
-
-       ret = fmt->convert_index_timestamp(td);
-       if (ret < 0)
-               goto error;
-
-       handle->real_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL);
-       handle->real_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_REAL);
-       handle->cycles_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES);
-       handle->cycles_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES);
 
        return handle->id;
 
-error:
+error_collection_del:
+       /* Remove from containers */
+       bt_trace_collection_remove(handle->td->ctx->tc, handle->td);
+error_destroy_handle:
+       bt_trace_handle_destroy(handle);
+error_close:
        closeret = fmt->close_trace(td);
        if (closeret) {
                fprintf(stderr, "Error in close_trace callback\n");
@@ -171,7 +207,7 @@ int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
         * automatically.
         */
        if (!g_hash_table_remove(ctx->trace_handles,
-               (gpointer) (unsigned long) handle_id)) {
+               GUINT_TO_POINTER(handle_id))) {
                ret = -ENOENT;
                goto end;
        }
@@ -217,6 +253,8 @@ void remove_trace_handle(struct bt_trace_handle *handle)
 {
        int ret;
 
+       if (!handle->td->ctx)
+               return;
        /* Remove from containers */
        bt_trace_collection_remove(handle->td->ctx->tc, handle->td);
        /* Close the trace */
This page took 0.025996 seconds and 4 git commands to generate.