Babeltrace 2 C API
2.0.0
Open-source trace manipulation framework
|
This example shows a basic sink component class packaged as a shared object plugin.
The name of the plugin is epitome
and the name of the sink component class is output
. Therefore the component class is identified in the babeltrace2
command-line tool as sink.epitome.output
.
A sink.epitome.output
component prints one text line to the standard output for each event message it consumes, for example:
#1: kmem_kmalloc (5 payload members)
#2: kmem_kfree (2 payload members)
#3: sched_waking (4 payload members)
#4: sched_migrate_task (5 payload members)
#5: sched_stat_runtime (4 payload members)
#6: sched_wakeup (4 payload members)
#7: rcu_utilization (1 payload member)
#8: rcu_utilization (1 payload member)
#9: sched_switch (7 payload members)
#10: syscall_entry_write (3 payload members)
...
For each line, there is:
A sink.epitome.output
component does not need any initialization parameter: it just prints to the standard output.
A sink.epitome.output
component creates a single input port named in
.
To simplify this example, a sink.epitome.output
component doesn't check the return status codes of API functions, but you must check them in production code.
The sink component class implementation and the shared object plugin macros are in the same file, epitome.c
:
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <babeltrace2/babeltrace.h>
struct epitome_out {
uint64_t index;
};
static
const bt_value *params,
void *initialize_method_data)
{
struct epitome_out *epitome_out = malloc(sizeof(*epitome_out));
epitome_out->index = 1;
epitome_out);
"in", NULL, NULL);
}
static
{
free(epitome_out);
}
static
{
self_component_sink, 0);
in_port, &epitome_out->message_iterator);
}
static
void print_message(
struct epitome_out *epitome_out,
const bt_message *message)
{
goto end;
}
printf("#%" PRIu64 ": %s (%" PRIu64 " payload member%s)\n",
member_count, member_count == 1 ? "" : "s");
epitome_out->index++;
end:
return;
}
{
uint64_t message_count;
&message_count);
switch (next_status) {
goto end;
goto end;
goto end;
goto end;
default:
break;
}
for (uint64_t i = 0; i < message_count; i++) {
print_message(epitome_out, message);
}
end:
return status;
}
epitome_out_initialize);
epitome_out_graph_is_configured);
As per the Compile and link a Babeltrace 2 shared object plugin guide, you can build the shared object plugin as such:
$ cc epitome.c -fPIC -c $(pkg-config --cflags babeltrace2)
$ ld epitome.o -o epitome.so -shared $(pkg-config --libs babeltrace2)
With the babeltrace2
tool, you can use a sink.epitome.output
component, reading a CTF trace (see babeltrace2-source.ctf.fs(7)
) for example:
$ babeltrace2 --plugin-path=. /path/to/ctf/trace --component=sink.epitome.output
struct bt_self_component_sink_configuration bt_self_component_sink_configuration
Self sink component configuration.
Definition: types.h:97
bt_message_iterator_create_from_sink_component_status bt_message_iterator_create_from_sink_component(bt_self_component_sink *self_component_sink, bt_self_component_port_input *port, bt_message_iterator **message_iterator)
Creates a message iterator on the input port port from the sink component self_component_sink,...
struct bt_value bt_value
Value.
Definition: types.h:107
#define BT_PLUGIN_SINK_COMPONENT_CLASS_INITIALIZE_METHOD(_name, _method)
Alias of BT_PLUGIN_SINK_COMPONENT_CLASS_INITIALIZE_METHOD_WITH_ID() with the _plugin_id parameter set...
Definition: plugin-dev.h:2332
bt_self_component_port_input * bt_self_component_sink_borrow_input_port_by_index(bt_self_component_sink *self_component, uint64_t index)
Borrows the self component input port at index index from the sink component self_component.
#define BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD(_name, _method)
Alias of BT_PLUGIN_SINK_COMPONENT_CLASS_FINALIZE_METHOD_WITH_ID() with the _plugin_id parameter set t...
Definition: plugin-dev.h:2194
void bt_self_component_set_data(bt_self_component *self_component, void *user_data)
Sets the user data of the component self_component to data.
Try again.
Definition: message-iterator.h:400
void * bt_self_component_get_data(const bt_self_component *self_component)
Returns the user data of the component self_component.
bt_component_class_initialize_method_status
Status codes for bt_component_class_source_initialize_method, bt_component_class_filter_initialize_me...
Definition: component-class-dev.h:881
#define BT_PLUGIN_SINK_COMPONENT_CLASS(_name, _consume_method)
Alias of BT_PLUGIN_SINK_COMPONENT_CLASS_WITH_ID() with the _plugin_id parameter set to auto,...
Definition: plugin-dev.h:904
struct bt_message_iterator bt_message_iterator
Message iterator.
Definition: types.h:72
#define BT_PLUGIN_MODULE()
Defines a plugin module.
Definition: plugin-dev.h:338
Success.
Definition: component-class-dev.h:833
const bt_field * bt_event_borrow_payload_field_const(const bt_event *event)
Borrows the payload field of the event event (const version).
Event message.
Definition: message.h:997
const bt_message ** bt_message_array_const
Array of constant messages.
Definition: types.h:196
Out of memory.
Definition: message-iterator.h:406
bt_component_class_sink_graph_is_configured_method_status
Status codes for bt_component_class_sink_graph_is_configured_method.
Definition: component-class-dev.h:828
bt_message_iterator_next_status bt_message_iterator_next(bt_message_iterator *message_iterator, bt_message_array_const *messages, uint64_t *count)
Returns the next messages of the message iterator message_iterator into the *messages array of size *...
Try again.
Definition: component-class-dev.h:520
void bt_message_iterator_put_ref(const bt_message_iterator *message_iterator)
Decrements the reference count of the message iterator message_iterator.
const char * bt_event_class_get_name(const bt_event_class *event_class)
Returns the name of the event class event_class.
const bt_event_class * bt_event_borrow_class_const(const bt_event *event)
Borrows the class of the event event (const version).
struct bt_self_component_sink bt_self_component_sink
Self sink component.
Definition: types.h:96
struct bt_event bt_event
Event.
Definition: types.h:50
const bt_event * bt_message_event_borrow_event_const(const bt_message *message)
Borrows the event of the event message message (const version).
bt_self_component_add_port_status bt_self_component_sink_add_input_port(bt_self_component_sink *self_component, const char *name, void *user_data, bt_self_component_port_input **self_component_port)
Adds an input port named name and having the user data user_data to the sink component self_component...
bt_component_class_sink_consume_method_status
Status codes for bt_component_class_sink_consume_method.
Definition: component-class-dev.h:503
struct bt_field bt_field
Field.
Definition: types.h:53
Success.
Definition: component-class-dev.h:886
struct bt_event_class bt_event_class
Event class.
Definition: types.h:51
bt_message_iterator_next_status
Status code for bt_message_iterator_next().
Definition: message-iterator.h:383
Other error.
Definition: message-iterator.h:412
uint64_t bt_field_class_structure_get_member_count(const bt_field_class *field_class)
Returns the number of members contained in the structure field class field_class.
Sink component is finished processing.
Definition: component-class-dev.h:514
void bt_message_put_ref(const bt_message *message)
Decrements the reference count of the message message.
End of iteration.
Definition: message-iterator.h:394
Success.
Definition: component-class-dev.h:508
#define BT_PLUGIN(_name)
Alias of BT_PLUGIN_WITH_ID() with the _id parameter set to auto.
Definition: plugin-dev.h:426
const bt_field_class * bt_field_borrow_class_const(const bt_field *field)
Borrows the class of the field field (const version).
bt_message_type bt_message_get_type(const bt_message *message)
Returns the type enumerator of the message message.
Out of memory.
Definition: component-class-dev.h:526
struct bt_self_component_port_input bt_self_component_port_input
Self component input port.
Definition: types.h:93
struct bt_message bt_message
Message.
Definition: types.h:71
User error.
Definition: component-class-dev.h:532
#define BT_PLUGIN_SINK_COMPONENT_CLASS_GRAPH_IS_CONFIGURED_METHOD(_name, _method)
Alias of BT_PLUGIN_SINK_COMPONENT_CLASS_GRAPH_IS_CONFIGURED_METHOD_WITH_ID() with the _plugin_id para...
Definition: plugin-dev.h:2287
static bt_self_component * bt_self_component_sink_as_self_component(bt_self_component_sink *self_component)
Upcasts the self sink component self_component to the common bt_self_component type.
Definition: self-component.h:869