Fix SEEK_BEGIN for streams that do not contain any event
[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.
20 */
21
22#include <babeltrace/babeltrace.h>
23#include <babeltrace/context.h>
08c22d05 24#include <babeltrace/context-internal.h>
6cba487f
MD
25#include <babeltrace/trace-handle.h>
26#include <babeltrace/trace-handle-internal.h>
27#include <babeltrace/trace-collection.h>
28#include <babeltrace/format.h>
e1d01c39 29#include <babeltrace/babeltrace-internal.h>
b469d2dd 30#include <stdlib.h>
e1d01c39
MD
31#include <string.h>
32#include <assert.h>
7f89ddce 33#include <errno.h>
b469d2dd 34
6cba487f
MD
35#include <fcntl.h> /* For O_RDONLY */
36
6cba487f
MD
37#include <glib.h>
38
39struct bt_context *bt_context_create(void)
b469d2dd
JD
40{
41 struct bt_context *ctx;
42
6cba487f 43 ctx = g_new0(struct bt_context, 1);
b469d2dd 44 ctx->refcount = 1;
6cba487f 45 /* Negative handle id are errors. */
842c2b97 46 ctx->last_trace_handle_id = 0;
b469d2dd 47
6cba487f
MD
48 /* Instanciate the trace handle container */
49 ctx->trace_handles = g_hash_table_new_full(g_direct_hash,
50 g_direct_equal, NULL,
51 (GDestroyNotify) bt_trace_handle_destroy);
52
e003e871 53 ctx->current_iterator = NULL;
6cba487f
MD
54 ctx->tc = g_new0(struct trace_collection, 1);
55 init_trace_collection(ctx->tc);
56
b469d2dd 57 return ctx;
6cba487f 58}
b469d2dd 59
6cba487f 60int bt_context_add_trace(struct bt_context *ctx, const char *path,
613f532b 61 const char *format_name,
20d0dcf9 62 void (*packet_seek)(struct stream_pos *pos, size_t index,
0d4c669f
MD
63 int whence),
64 struct mmap_stream_list *stream_list,
65 FILE *metadata)
6cba487f
MD
66{
67 struct trace_descriptor *td;
68 struct format *fmt;
69 struct bt_trace_handle *handle;
f824ae04 70 int ret, closeret;
6cba487f 71
7f89ddce
MD
72 if (!ctx || !format_name || (!path && !stream_list))
73 return -EINVAL;
74
282e1952
MD
75 fmt = bt_lookup_format(g_quark_from_string(format_name));
76 if (!fmt) {
77 fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
78 format_name);
79 ret = -1;
80 goto end;
81 }
0d4c669f
MD
82 if (path) {
83 td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
84 if (!td) {
ca718275 85 fprintf(stderr, "[warning] [Context] Cannot open_trace of the format %s .\n\n",
0d4c669f
MD
86 path);
87 ret = -1;
88 goto end;
89 }
90 } else {
91 td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
92 if (!td) {
93 fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n",
94 path);
95 ret = -1;
96 goto end;
97 }
6cba487f
MD
98 }
99
100 /* Create an handle for the trace */
101 handle = bt_trace_handle_create(ctx);
102 if (handle < 0) {
02dc4610 103 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
6cba487f 104 path);
1059a2bf 105 ret = -1;
e6d85e38 106 goto error;
6cba487f
MD
107 }
108 handle->format = fmt;
109 handle->td = td;
afe9cd4a
JD
110 if (path) {
111 strncpy(handle->path, path, PATH_MAX);
112 handle->path[PATH_MAX - 1] = '\0';
113 }
6cba487f 114
98a04903
JD
115 if (fmt->set_handle)
116 fmt->set_handle(td, handle);
117 if (fmt->set_context)
118 fmt->set_context(td, ctx);
119
6cba487f
MD
120 /* Add new handle to container */
121 g_hash_table_insert(ctx->trace_handles,
122 (gpointer) (unsigned long) handle->id,
123 handle);
1059a2bf 124 ret = trace_collection_add(ctx->tc, td);
03798a93 125 if (ret != 0)
e6d85e38 126 goto error;
03798a93
JD
127
128 ret = fmt->convert_index_timestamp(td);
129 if (ret < 0)
e6d85e38 130 goto error;
03798a93
JD
131
132 handle->real_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL);
133 handle->real_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_REAL);
134 handle->cycles_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES);
135 handle->cycles_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES);
136
137 return handle->id;
e6d85e38
JD
138
139error:
f824ae04
MD
140 closeret = fmt->close_trace(td);
141 if (closeret) {
142 fprintf(stderr, "Error in close_trace callback\n");
143 }
1059a2bf
JD
144end:
145 return ret;
b469d2dd
JD
146}
147
7f89ddce 148int bt_context_remove_trace(struct bt_context *ctx, int handle_id)
b469d2dd 149{
6cba487f 150 struct bt_trace_handle *handle;
f824ae04 151 int ret;
6cba487f 152
7f89ddce
MD
153 if (!ctx)
154 return -EINVAL;
155
6cba487f
MD
156 handle = g_hash_table_lookup(ctx->trace_handles,
157 (gpointer) (unsigned long) handle_id);
7f89ddce
MD
158 if (!handle)
159 return -ENOENT;
6cba487f
MD
160
161 /* Remove from containers */
162 trace_collection_remove(ctx->tc, handle->td);
6cba487f 163 /* Close the trace */
f824ae04
MD
164 ret = handle->format->close_trace(handle->td);
165 if (ret) {
166 fprintf(stderr, "Error in close_trace callback\n");
167 return ret;
168 }
188e72bf
JD
169 /* Remove and free the handle */
170 g_hash_table_remove(ctx->trace_handles,
171 (gpointer) (unsigned long) handle_id);
7f89ddce 172 return 0;
6cba487f
MD
173}
174
175static
176void bt_context_destroy(struct bt_context *ctx)
177{
7f89ddce 178 assert(ctx);
6cba487f
MD
179 finalize_trace_collection(ctx->tc);
180
e6d85e38
JD
181 /*
182 * Remove all traces. The g_hash_table_destroy will call
6cba487f
MD
183 * bt_trace_handle_destroy on each elements.
184 */
185 g_hash_table_destroy(ctx->trace_handles);
186
187 /* ctx->tc should always be valid */
188 assert(ctx->tc != NULL);
189 g_free(ctx->tc);
190 g_free(ctx);
b469d2dd
JD
191}
192
6cba487f 193void bt_context_get(struct bt_context *ctx)
b469d2dd 194{
7f89ddce 195 assert(ctx);
6cba487f
MD
196 ctx->refcount++;
197}
b469d2dd 198
6cba487f
MD
199void bt_context_put(struct bt_context *ctx)
200{
7f89ddce 201 assert(ctx);
b469d2dd
JD
202 ctx->refcount--;
203 if (ctx->refcount == 0)
6cba487f 204 bt_context_destroy(ctx);
b469d2dd 205}
This page took 0.034309 seconds and 4 git commands to generate.