Update version to 1.0.0-pre2
[babeltrace.git] / lib / context.c
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>
24 #include <babeltrace/context-internal.h>
25 #include <babeltrace/trace-handle.h>
26 #include <babeltrace/trace-handle-internal.h>
27 #include <babeltrace/trace-collection.h>
28 #include <babeltrace/format.h>
29 #include <babeltrace/babeltrace-internal.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include <fts.h>
35 #include <fcntl.h> /* For O_RDONLY */
36
37 #include <glib.h>
38
39 struct bt_context *bt_context_create(void)
40 {
41 struct bt_context *ctx;
42
43 ctx = g_new0(struct bt_context, 1);
44 ctx->refcount = 1;
45 /* Negative handle id are errors. */
46 ctx->last_trace_handle_id = 0;
47
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
53 ctx->tc = g_new0(struct trace_collection, 1);
54 init_trace_collection(ctx->tc);
55
56 return ctx;
57 }
58
59 int bt_context_add_trace(struct bt_context *ctx, const char *path,
60 const char *format_name,
61 void (*packet_seek)(struct stream_pos *pos, size_t index,
62 int whence),
63 struct mmap_stream_list *stream_list,
64 FILE *metadata)
65 {
66 struct trace_descriptor *td;
67 struct format *fmt;
68 struct bt_trace_handle *handle;
69 int ret;
70
71 fmt = bt_lookup_format(g_quark_from_string(format_name));
72 if (!fmt) {
73 fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
74 format_name);
75 ret = -1;
76 goto end;
77 }
78 if (path) {
79 td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
80 if (!td) {
81 fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n",
82 path);
83 ret = -1;
84 goto end;
85 }
86 } else {
87 td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
88 if (!td) {
89 fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n",
90 path);
91 ret = -1;
92 goto end;
93 }
94 }
95
96 /* Create an handle for the trace */
97 handle = bt_trace_handle_create(ctx);
98 if (handle < 0) {
99 fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
100 path);
101 ret = -1;
102 goto end;
103 }
104 handle->format = fmt;
105 handle->td = td;
106 strncpy(handle->path, path, PATH_MAX);
107 handle->path[PATH_MAX - 1] = '\0';
108
109 /* Add new handle to container */
110 g_hash_table_insert(ctx->trace_handles,
111 (gpointer) (unsigned long) handle->id,
112 handle);
113 ret = trace_collection_add(ctx->tc, td);
114 if (ret == 0)
115 return handle->id;
116 end:
117 return ret;
118 }
119
120 void bt_context_remove_trace(struct bt_context *ctx, int handle_id)
121 {
122 struct bt_trace_handle *handle;
123
124 handle = g_hash_table_lookup(ctx->trace_handles,
125 (gpointer) (unsigned long) handle_id);
126 assert(handle != NULL);
127
128 /* Remove from containers */
129 trace_collection_remove(ctx->tc, handle->td);
130 g_hash_table_remove(ctx->trace_handles,
131 (gpointer) (unsigned long) handle_id);
132
133 /* Close the trace */
134 handle->format->close_trace(handle->td);
135
136 /* Destory the handle */
137 bt_trace_handle_destroy(handle);
138 }
139
140 static
141 void bt_context_destroy(struct bt_context *ctx)
142 {
143 finalize_trace_collection(ctx->tc);
144
145 /* Remote all traces. The g_hash_table_destroy will call
146 * bt_trace_handle_destroy on each elements.
147 */
148 g_hash_table_destroy(ctx->trace_handles);
149
150 /* ctx->tc should always be valid */
151 assert(ctx->tc != NULL);
152 g_free(ctx->tc);
153 g_free(ctx);
154 }
155
156 void bt_context_get(struct bt_context *ctx)
157 {
158 ctx->refcount++;
159 }
160
161 void bt_context_put(struct bt_context *ctx)
162 {
163 ctx->refcount--;
164 if (ctx->refcount == 0)
165 bt_context_destroy(ctx);
166 }
This page took 0.054792 seconds and 4 git commands to generate.