4 This is a brief howto for using the Babeltrace Python module.
9 By default, the Python bindings are not generated.
10 If you wish to generate and install the Python bindings, you can use the
11 --enable-python-bindings configure option.
13 $ ./configure --enable-python-bindings
15 The Python module is automatically generated using SWIG, therefore the
16 swig2.0 package on Debian/Ubuntu is requied.
21 Once installed, the Python module can be used by importing it in Python.
22 In the Python interpreter:
26 Then the starting point is to create a context and add a trace to it.
28 >>> ctx = babeltrace.Context()
29 >>> ctx.add_trace("path/to/trace", <format>)
31 Where <format> is a string containing the format name in which the trace
32 was produced. To print a list of available formats to the standard
33 output, it is possible to use the print_format_list function.
35 >>> out = babeltrace.File(None) # This returns stdout
36 >>> babeltrace.print_format_list(out)
38 When a trace is added to a context, it is opened and ready to read using
39 an iterator. While creating an iterator, optional starting and ending
40 position may be specified. So far, only ctf iterator are supported.
42 >>> begin_pos = babeltrace.IterPos(babeltrace.SEEK_BEGIN)
43 >>> iterator = babeltrace.ctf.Iterator(ctx, begin_pos)
45 From there, it is possible to read the events.
47 >>> event = iterator.read_event()
49 It is simple to obtain the timestamp of that event.
51 >>> timestamp = event.get_timestamp()
53 Let's say that we want to extract the prev_comm context info for a
54 sched_switch event. To do so, it is needed to set an event scope
55 with which we can obtain the field wanted.
57 >>> if event.get_name == "sched_switch":
58 ... #prev_comm only for sched_switch events
59 ... scope = event.get_top_level_scope(babeltrace.ctf.scope.EVENT_FIELDS)
60 ... field = event.get_field(scope, "_prev_comm")
61 ... prev_comm = field.get_char_array()
63 It is also possible to move on to the next event.
65 >>> ret = iterator.next() # Move the iterator
66 >>> if ret == 0: # No error occured
67 ... event = iterator.read_event() # Read the next event
69 For many usage script examples of the Babeltrace Python module, see the
70 bindings/python/examples directory.