API : trace_handle
[babeltrace.git] / lib / callbacks.c
1 /*
2 * callbacks.c
3 *
4 * Babeltrace Library
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/babeltrace.h>
22 #include <babeltrace/babeltrace-internal.h>
23 #include <babeltrace/callbacks-internal.h>
24 #include <babeltrace/context.h>
25 #include <babeltrace/ctf-ir/metadata.h>
26 #include <babeltrace/iterator-internal.h>
27 #include <inttypes.h>
28
29 static
30 struct bt_dependencies *_babeltrace_dependencies_create(const char *first,
31 va_list ap)
32 {
33 const char *iter;
34 struct bt_dependencies *dep;
35
36 dep = g_new0(struct bt_dependencies, 1);
37 dep->refcount = 1;
38 dep->deps = g_array_new(FALSE, TRUE, sizeof(GQuark));
39 iter = first;
40 while (iter) {
41 GQuark q = g_quark_from_string(iter);
42 g_array_append_val(dep->deps, q);
43 iter = va_arg(ap, const char *);
44 }
45 return dep;
46 }
47
48 struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...)
49 {
50 va_list ap;
51 struct bt_dependencies *deps;
52
53 va_start(ap, first);
54 deps = _babeltrace_dependencies_create(first, ap);
55 va_end(ap);
56 return deps;
57 }
58
59 /*
60 * babeltrace_iter_add_callback: Add a callback to iterator.
61 */
62 int babeltrace_iter_add_callback(struct babeltrace_iter *iter,
63 bt_event_name event, void *private_data, int flags,
64 enum bt_cb_ret (*callback)(struct bt_ctf_data *ctf_data,
65 void *private_data),
66 struct bt_dependencies *depends,
67 struct bt_dependencies *weak_depends,
68 struct bt_dependencies *provides)
69 {
70 int i, stream_id;
71 gpointer *event_id_ptr;
72 unsigned long event_id;
73 struct trace_collection *tc = iter->ctx->tc;
74
75 for (i = 0; i < tc->array->len; i++) {
76 struct ctf_trace *tin;
77 struct trace_descriptor *td_read;
78
79 td_read = g_ptr_array_index(tc->array, i);
80 tin = container_of(td_read, struct ctf_trace, parent);
81
82 for (stream_id = 0; stream_id < tin->streams->len; stream_id++) {
83 struct ctf_stream_class *stream;
84 struct bt_stream_callbacks *bt_stream_cb = NULL;
85 struct bt_callback_chain *bt_chain = NULL;
86 struct bt_callback new_callback;
87
88 stream = g_ptr_array_index(tin->streams, stream_id);
89
90 if (stream_id >= iter->callbacks->len) {
91 g_array_set_size(iter->callbacks, stream->stream_id + 1);
92 }
93 bt_stream_cb = &g_array_index(iter->callbacks,
94 struct bt_stream_callbacks, stream->stream_id);
95 if (!bt_stream_cb->per_id_callbacks) {
96 bt_stream_cb->per_id_callbacks = g_array_new(FALSE, TRUE,
97 sizeof(struct bt_callback_chain));
98 }
99
100 if (event) {
101 /* find the event id */
102 event_id_ptr = g_hash_table_lookup(stream->event_quark_to_id,
103 (gconstpointer) (unsigned long) event);
104 /* event not found in this stream class */
105 if (!event_id_ptr) {
106 fprintf(stderr, "event not found\n");
107 continue;
108 }
109 event_id = (uint64_t)(unsigned long) *event_id_ptr;
110
111 /* find or create the bt_callback_chain for this event */
112 if (event_id >= bt_stream_cb->per_id_callbacks->len) {
113 g_array_set_size(bt_stream_cb->per_id_callbacks, event_id + 1);
114 }
115 bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
116 struct bt_callback_chain, event_id);
117 if (!bt_chain->callback) {
118 bt_chain->callback = g_array_new(FALSE, TRUE,
119 sizeof(struct bt_callback));
120 }
121 } else {
122 /* callback for all events */
123 if (!iter->main_callbacks.callback) {
124 iter->main_callbacks.callback = g_array_new(FALSE, TRUE,
125 sizeof(struct bt_callback));
126 }
127 bt_chain = &iter->main_callbacks;
128 }
129
130 new_callback.private_data = private_data;
131 new_callback.flags = flags;
132 new_callback.callback = callback;
133 new_callback.depends = depends;
134 new_callback.weak_depends = weak_depends;
135 new_callback.provides = provides;
136
137 /* TODO : take care of priority, for now just FIFO */
138 g_array_append_val(bt_chain->callback, new_callback);
139 }
140 }
141
142 return 0;
143 }
144
145 static
146 struct ctf_stream_event *extract_ctf_stream_event(struct ctf_stream *stream)
147 {
148 struct ctf_stream_class *stream_class = stream->stream_class;
149 struct ctf_event *event_class;
150 struct ctf_stream_event *event;
151 uint64_t id = stream->event_id;
152
153 if (id >= stream_class->events_by_id->len) {
154 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
155 return NULL;
156 }
157 event = g_ptr_array_index(stream->events_by_id, id);
158 if (!event) {
159 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
160 return NULL;
161 }
162 event_class = g_ptr_array_index(stream_class->events_by_id, id);
163 if (!event_class) {
164 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
165 return NULL;
166 }
167
168 return event;
169 }
170
171 void process_callbacks(struct babeltrace_iter *iter,
172 struct ctf_stream *stream)
173 {
174 struct bt_stream_callbacks *bt_stream_cb;
175 struct bt_callback_chain *bt_chain;
176 struct bt_callback *cb;
177 int i;
178 enum bt_cb_ret ret;
179 struct bt_ctf_data ctf_data;
180
181 ctf_data.event = extract_ctf_stream_event(stream);
182 ctf_data.stream = stream;
183
184 /* process all events callback first */
185 if (iter->main_callbacks.callback) {
186 for (i = 0; i < iter->main_callbacks.callback->len; i++) {
187 cb = &g_array_index(iter->main_callbacks.callback, struct bt_callback, i);
188 if (!cb)
189 goto end;
190 ret = cb->callback(&ctf_data, cb->private_data);
191 switch (ret) {
192 case BT_CB_OK_STOP:
193 case BT_CB_ERROR_STOP:
194 goto end;
195 default:
196 break;
197 }
198 }
199 }
200
201 /* process per event callbacks */
202 bt_stream_cb = &g_array_index(iter->callbacks,
203 struct bt_stream_callbacks, stream->stream_id);
204 if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks)
205 goto end;
206
207 if (stream->event_id > bt_stream_cb->per_id_callbacks->len)
208 goto end;
209 bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
210 struct bt_callback_chain, stream->event_id);
211 if (!bt_chain || !bt_chain->callback)
212 goto end;
213
214 for (i = 0; i < bt_chain->callback->len; i++) {
215 cb = &g_array_index(bt_chain->callback, struct bt_callback, i);
216 if (!cb)
217 goto end;
218 ret = cb->callback(&ctf_data, cb->private_data);
219 switch (ret) {
220 case BT_CB_OK_STOP:
221 case BT_CB_ERROR_STOP:
222 goto end;
223 default:
224 break;
225 }
226 }
227
228 end:
229 return;
230 }
This page took 0.037109 seconds and 4 git commands to generate.