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