Commit | Line | Data |
---|---|---|
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> | |
b469d2dd JD |
29 | #include <stdlib.h> |
30 | ||
6cba487f MD |
31 | #include <fts.h> |
32 | #include <fcntl.h> /* For O_RDONLY */ | |
33 | ||
34 | /* TODO ybrosseau: should be hidden in the CTF format */ | |
06789ffd | 35 | #include <babeltrace/ctf/types.h> /* for ctf_packet_seek */ |
6cba487f MD |
36 | |
37 | #include <glib.h> | |
38 | ||
39 | struct 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 | ||
53 | ctx->tc = g_new0(struct trace_collection, 1); | |
54 | init_trace_collection(ctx->tc); | |
55 | ||
b469d2dd | 56 | return ctx; |
6cba487f | 57 | } |
b469d2dd | 58 | |
6cba487f | 59 | int bt_context_add_trace(struct bt_context *ctx, const char *path, |
282e1952 | 60 | const char *format_name) |
6cba487f MD |
61 | { |
62 | struct trace_descriptor *td; | |
63 | struct format *fmt; | |
64 | struct bt_trace_handle *handle; | |
1059a2bf | 65 | int ret; |
6cba487f | 66 | |
282e1952 MD |
67 | fmt = bt_lookup_format(g_quark_from_string(format_name)); |
68 | if (!fmt) { | |
69 | fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n", | |
70 | format_name); | |
71 | ret = -1; | |
72 | goto end; | |
73 | } | |
6cba487f | 74 | td = fmt->open_trace(path, O_RDONLY, |
06789ffd | 75 | ctf_packet_seek, NULL); |
6cba487f | 76 | if (!td) { |
02dc4610 | 77 | fprintf(stderr, "[error] [Context] Cannot open_trace of the format %s .\n\n", |
6cba487f | 78 | path); |
1059a2bf JD |
79 | ret = -1; |
80 | goto end; | |
6cba487f MD |
81 | } |
82 | ||
83 | /* Create an handle for the trace */ | |
84 | handle = bt_trace_handle_create(ctx); | |
85 | if (handle < 0) { | |
02dc4610 | 86 | fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n", |
6cba487f | 87 | path); |
1059a2bf JD |
88 | ret = -1; |
89 | goto end; | |
6cba487f MD |
90 | } |
91 | handle->format = fmt; | |
92 | handle->td = td; | |
93 | strncpy(handle->path, path, PATH_MAX); | |
94 | handle->path[PATH_MAX - 1] = '\0'; | |
95 | ||
96 | /* Add new handle to container */ | |
97 | g_hash_table_insert(ctx->trace_handles, | |
98 | (gpointer) (unsigned long) handle->id, | |
99 | handle); | |
1059a2bf JD |
100 | ret = trace_collection_add(ctx->tc, td); |
101 | if (ret == 0) | |
102 | return handle->id; | |
103 | end: | |
104 | return ret; | |
b469d2dd JD |
105 | } |
106 | ||
6cba487f | 107 | void bt_context_remove_trace(struct bt_context *ctx, int handle_id) |
b469d2dd | 108 | { |
6cba487f MD |
109 | struct bt_trace_handle *handle; |
110 | ||
111 | handle = g_hash_table_lookup(ctx->trace_handles, | |
112 | (gpointer) (unsigned long) handle_id); | |
113 | assert(handle != NULL); | |
114 | ||
115 | /* Remove from containers */ | |
116 | trace_collection_remove(ctx->tc, handle->td); | |
117 | g_hash_table_remove(ctx->trace_handles, | |
118 | (gpointer) (unsigned long) handle_id); | |
119 | ||
120 | /* Close the trace */ | |
121 | handle->format->close_trace(handle->td); | |
122 | ||
123 | /* Destory the handle */ | |
124 | bt_trace_handle_destroy(handle); | |
125 | } | |
126 | ||
127 | static | |
128 | void bt_context_destroy(struct bt_context *ctx) | |
129 | { | |
130 | finalize_trace_collection(ctx->tc); | |
131 | ||
132 | /* Remote all traces. The g_hash_table_destroy will call | |
133 | * bt_trace_handle_destroy on each elements. | |
134 | */ | |
135 | g_hash_table_destroy(ctx->trace_handles); | |
136 | ||
137 | /* ctx->tc should always be valid */ | |
138 | assert(ctx->tc != NULL); | |
139 | g_free(ctx->tc); | |
140 | g_free(ctx); | |
b469d2dd JD |
141 | } |
142 | ||
6cba487f | 143 | void bt_context_get(struct bt_context *ctx) |
b469d2dd | 144 | { |
6cba487f MD |
145 | ctx->refcount++; |
146 | } | |
b469d2dd | 147 | |
6cba487f MD |
148 | void bt_context_put(struct bt_context *ctx) |
149 | { | |
b469d2dd JD |
150 | ctx->refcount--; |
151 | if (ctx->refcount == 0) | |
6cba487f | 152 | bt_context_destroy(ctx); |
b469d2dd | 153 | } |