+struct bt_dependencies *_babeltrace_dependencies_create(const char *first,
+ va_list ap)
+{
+ const char *iter;
+ struct bt_dependencies *dep;
+
+ dep = g_new0(struct bt_dependencies, 1);
+ dep->refcount = 1;
+ dep->deps = g_array_new(FALSE, TRUE, sizeof(GQuark));
+ iter = first;
+ while (iter) {
+ GQuark q = g_quark_from_string(iter);
+ g_array_append_val(dep->deps, q);
+ iter = va_arg(ap, const char *);
+ }
+ return dep;
+}
+
+struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...)
+{
+ va_list ap;
+ struct bt_dependencies *deps;
+
+ va_start(ap, first);
+ deps = _babeltrace_dependencies_create(first, ap);
+ va_end(ap);
+ return deps;
+}
+
+/*
+ * babeltrace_iter_add_callback: Add a callback to iterator.
+ */
+int babeltrace_iter_add_callback(struct babeltrace_iter *iter,
+ bt_event_name event, void *private_data, int flags,
+ enum bt_cb_ret (*callback)(struct bt_ctf_data *ctf_data,
+ void *private_data),
+ struct bt_dependencies *depends,
+ struct bt_dependencies *weak_depends,
+ struct bt_dependencies *provides)
+{
+ int i, stream_id;
+ gpointer *event_id_ptr;
+ unsigned long event_id;
+ struct trace_collection *tc = iter->tc;
+
+ for (i = 0; i < tc->array->len; i++) {
+ struct ctf_trace *tin;
+ struct trace_descriptor *td_read;
+
+ td_read = g_ptr_array_index(tc->array, i);
+ tin = container_of(td_read, struct ctf_trace, parent);
+
+ for (stream_id = 0; stream_id < tin->streams->len; stream_id++) {
+ struct ctf_stream_class *stream;
+ struct bt_stream_callbacks *bt_stream_cb = NULL;
+ struct bt_callback_chain *bt_chain = NULL;
+ struct bt_callback new_callback;
+
+ stream = g_ptr_array_index(tin->streams, stream_id);
+
+ /* find or create the bt_stream_callbacks for this stream */
+ if (iter->callbacks->len >= stream_id) {
+ bt_stream_cb = &g_array_index(iter->callbacks,
+ struct bt_stream_callbacks, stream->stream_id);
+ } else {
+ g_array_set_size(iter->callbacks, stream->stream_id);
+ }
+ if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks) {
+ struct bt_stream_callbacks new_stream_cb;
+ new_stream_cb.per_id_callbacks = g_array_new(1, 1,
+ sizeof(struct bt_callback_chain));
+ g_array_insert_val(iter->callbacks, stream->stream_id, new_stream_cb);
+ bt_stream_cb = &g_array_index(iter->callbacks,
+ struct bt_stream_callbacks, stream->stream_id);
+ }
+
+ if (event) {
+ /* find the event id */
+ event_id_ptr = g_hash_table_lookup(stream->event_quark_to_id,
+ (gconstpointer) (unsigned long) event);
+ /* event not found in this stream class */
+ if (!event_id_ptr) {
+ printf("event not found\n");
+ continue;
+ }
+ event_id = (uint64_t)(unsigned long)*event_id_ptr;
+
+ /* find or create the bt_callback_chain for this event */
+ if (bt_stream_cb->per_id_callbacks->len >= event_id) {
+ bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
+ struct bt_callback_chain, event_id);
+ } else {
+ g_array_set_size(bt_stream_cb->per_id_callbacks, event_id);
+ }
+ if (!bt_chain || !bt_chain->callback) {
+ struct bt_callback_chain new_chain;
+ new_chain.callback = g_array_new(1, 1, sizeof(struct bt_callback));
+ g_array_insert_val(bt_stream_cb->per_id_callbacks, event_id,
+ new_chain);
+ bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
+ struct bt_callback_chain, event_id);
+ }
+ } else {
+ /* callback for all events */
+ if (!iter->main_callbacks.callback) {
+ iter->main_callbacks.callback = g_array_new(1, 1,
+ sizeof(struct bt_callback));
+ }
+ bt_chain = &iter->main_callbacks;
+ }
+
+ new_callback.private_data = private_data;
+ new_callback.flags = flags;
+ new_callback.callback = callback;
+ new_callback.depends = depends;
+ new_callback.weak_depends = weak_depends;
+ new_callback.provides = provides;
+
+ /* TODO : take care of priority, for now just FIFO */
+ g_array_append_val(bt_chain->callback, new_callback);
+ }
+ }
+
+ return 0;
+}
+
+static int stream_read_event(struct ctf_file_stream *sin)