lttngbabeltrace

Get number format from babeltrace2 Python API


I use babeltrace2 python bindings to parse some traces that were written by LTTNG. I have a simple code, like this:

import bt2

path = argv[1]
for msg in bt2.TraceCollectionMessageIterator(path):
    if type(msg) is not bt2._EventMessageConst:
        continue

    event = msg.event
    payload = event.payload_field

    for arg_name in payload:
        print("{} = {}".format(arg_name, payload[arg_name]))

I have a trace line that when I print with babeltrace cli I get:

[16:31:48.244238766] prov1:event1: { cpu_id = 31 }, { ptr_value = 0x444 }

But the result of my code is:

ptr_value = 1092

I understand that this of course is happening because the value is an integer and is not presented as hex. But the format I set in LTTNG is hex and I want to present it as such in my script as well.

Is there a way to understand the correct format the value was defined with in the LTTNG trace? I assume this info must be available, because the babeltrace cli tool is able to understand that, but I don't see it anywhere in the python binding API.


Solution

  • In babeltrace stable-2.0, the preferred display base for integer field classes is available via the bindings through the preferred_display_base property. See the source code.

    Here is your example adjusted to check the preferred display base for integer field classes.

    import bt2
    import sys
    
    path = sys.argv[1]
    for msg in bt2.TraceCollectionMessageIterator(path):
        if type(msg) is not bt2._EventMessageConst:
            continue
    
        event = msg.event
        payload = event.payload_field
    
        for arg_name in payload:
            if issubclass(type(payload[arg_name].cls), bt2._IntegerFieldClassConst):
                base = payload[arg_name].cls.preferred_display_base
                if base == 2:
                    value = bin(payload[arg_name])
                elif base == 8:
                    value = oct(payload[arg_name])
                elif base == 10:
                    value = payload[arg_name]
                elif base == 16:
                    value = hex(payload[arg_name])
                print("{}  = {}".format(arg_name, value))
            else:
                print("{} value = {}".format(arg_name, payload[arg_name]))