|
| 1 | +# Copyright 2019-2026 CERN and copyright holders of ALICE O2. |
| 2 | +# See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +# All rights not expressly granted are reserved. |
| 4 | +# |
| 5 | +# This software is distributed under the terms of the GNU General Public |
| 6 | +# License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +# |
| 8 | +# In applying this license CERN does not waive the privileges and immunities |
| 9 | +# granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +# or submit itself to any jurisdiction. |
| 11 | +# |
| 12 | +# lldb data formatters for o2::framework types. |
| 13 | +# |
| 14 | +# Usage: add to ~/.lldbinit or a project .lldbinit: |
| 15 | +# command script import /path/to/O2/Framework/Core/scripts/lldb_o2_formatters.py |
| 16 | + |
| 17 | +import lldb |
| 18 | + |
| 19 | +# o2::framework::VariantType enum values (must match Variant.h) |
| 20 | +_VARIANT_TYPE = { |
| 21 | + 0: 'Int', |
| 22 | + 1: 'Int64', |
| 23 | + 2: 'Float', |
| 24 | + 3: 'Double', |
| 25 | + 4: 'String', |
| 26 | + 5: 'Bool', |
| 27 | + 6: 'ArrayInt', |
| 28 | + 7: 'ArrayFloat', |
| 29 | + 8: 'ArrayDouble', |
| 30 | + 9: 'ArrayBool', |
| 31 | + 10: 'ArrayString', |
| 32 | + 11: 'Array2DInt', |
| 33 | + 12: 'Array2DFloat', |
| 34 | + 13: 'Array2DDouble', |
| 35 | + 14: 'LabeledArrayInt', |
| 36 | + 15: 'LabeledArrayFloat', |
| 37 | + 16: 'LabeledArrayDouble', |
| 38 | + 17: 'UInt8', |
| 39 | + 18: 'UInt16', |
| 40 | + 19: 'UInt32', |
| 41 | + 20: 'UInt64', |
| 42 | + 21: 'Int8', |
| 43 | + 22: 'Int16', |
| 44 | + 23: 'LabeledArrayString', |
| 45 | + 24: 'Empty', |
| 46 | + 25: 'Dict', |
| 47 | + 26: 'Unknown', |
| 48 | +} |
| 49 | + |
| 50 | +# Map VariantType value → (C type name for FindFirstType, is_pointer_to_value) |
| 51 | +# is_pointer_to_value=True means mStore holds a T* pointing to heap data (arrays) |
| 52 | +_SIMPLE_TYPES = { |
| 53 | + 0: ('int', False), |
| 54 | + 1: ('long long', False), |
| 55 | + 2: ('float', False), |
| 56 | + 3: ('double', False), |
| 57 | + 5: ('bool', False), |
| 58 | + 17: ('unsigned char', False), |
| 59 | + 18: ('unsigned short',False), |
| 60 | + 19: ('unsigned int', False), |
| 61 | + 20: ('unsigned long long', False), |
| 62 | + 21: ('signed char', False), |
| 63 | + 22: ('short', False), |
| 64 | +} |
| 65 | + |
| 66 | +_ARRAY_ELEM_TYPES = { |
| 67 | + 6: 'int', |
| 68 | + 7: 'float', |
| 69 | + 8: 'double', |
| 70 | + 9: 'bool', |
| 71 | +} |
| 72 | + |
| 73 | +MAX_ARRAY_DISPLAY = 16 |
| 74 | + |
| 75 | + |
| 76 | +import struct as _struct |
| 77 | + |
| 78 | + |
| 79 | +def _read_pointer(process, addr): |
| 80 | + err = lldb.SBError() |
| 81 | + ptr_size = process.GetAddressByteSize() |
| 82 | + data = process.ReadMemory(addr, ptr_size, err) |
| 83 | + if err.Fail() or not data: |
| 84 | + return None |
| 85 | + fmt = '<Q' if ptr_size == 8 else '<I' |
| 86 | + return _struct.unpack(fmt, data)[0] |
| 87 | + |
| 88 | + |
| 89 | +def _read_libcxx_string(process, addr): |
| 90 | + """Read a libc++ std::string directly from memory (no type lookup needed). |
| 91 | +
|
| 92 | + libc++ std::string on 64-bit little-endian (macOS x86_64 / arm64): |
| 93 | + sizeof = 24 bytes, union of: |
| 94 | + __short: byte[0] = (size<<1)|0 (short flag), bytes[1..22] = inline data |
| 95 | + __long: byte[0] low bit = 1, bytes[8..15] = size, bytes[16..23] = data ptr |
| 96 | + """ |
| 97 | + err = lldb.SBError() |
| 98 | + raw = process.ReadMemory(addr, 24, err) |
| 99 | + if err.Fail() or not raw: |
| 100 | + return '"<read error>"' |
| 101 | + b = raw if isinstance(raw[0], int) else bytes(ord(c) for c in raw) |
| 102 | + if (b[0] & 1) == 0: # short form |
| 103 | + size = b[0] >> 1 |
| 104 | + text = b[1:1 + size].decode('utf-8', errors='replace') |
| 105 | + else: # long form |
| 106 | + size = _struct.unpack_from('<Q', b, 8)[0] |
| 107 | + data_ptr = _struct.unpack_from('<Q', b, 16)[0] |
| 108 | + if data_ptr == 0: |
| 109 | + return '"<null>"' |
| 110 | + heap = process.ReadMemory(data_ptr, min(size, 512), err) |
| 111 | + if err.Fail() or not heap: |
| 112 | + return '"<read error>"' |
| 113 | + h = heap if isinstance(heap[0], int) else bytes(ord(c) for c in heap) |
| 114 | + text = h[:size].decode('utf-8', errors='replace') |
| 115 | + if size > 512: |
| 116 | + text += '...' |
| 117 | + return f'"{text}"' |
| 118 | + |
| 119 | + |
| 120 | +def variant_summary(valobj, _internal_dict): |
| 121 | + # Use GetNonSyntheticValue() so we see the real struct members even when |
| 122 | + # the synthetic provider has replaced the children with decoded values. |
| 123 | + raw = valobj.GetNonSyntheticValue() |
| 124 | + mType_val = raw.GetChildMemberWithName('mType') |
| 125 | + if not mType_val.IsValid(): |
| 126 | + return '<invalid Variant>' |
| 127 | + |
| 128 | + mType = mType_val.GetValueAsUnsigned(26) # default Unknown |
| 129 | + mSize = raw.GetChildMemberWithName('mSize').GetValueAsUnsigned(1) |
| 130 | + mStore = raw.GetChildMemberWithName('mStore') |
| 131 | + store_addr = mStore.GetLoadAddress() |
| 132 | + |
| 133 | + type_name = _VARIANT_TYPE.get(mType, f'<type={mType}>') |
| 134 | + target = valobj.GetTarget() |
| 135 | + process = valobj.GetProcess() |
| 136 | + |
| 137 | + # --- simple scalar types --- |
| 138 | + if mType in _SIMPLE_TYPES: |
| 139 | + ctype, _ = _SIMPLE_TYPES[mType] |
| 140 | + t = target.FindFirstType(ctype) |
| 141 | + if t.IsValid(): |
| 142 | + v = valobj.CreateValueFromAddress('v', store_addr, t) |
| 143 | + return f'{type_name}({v.GetValue()})' |
| 144 | + return f'{type_name}(?)' |
| 145 | + |
| 146 | + # --- String (const char* stored in mStore) --- |
| 147 | + if mType == 4: |
| 148 | + ptr = _read_pointer(process, store_addr) |
| 149 | + if ptr and ptr != 0: |
| 150 | + s = _read_cstring(process, ptr) |
| 151 | + return f'String("{s}")' |
| 152 | + return 'String(null)' |
| 153 | + |
| 154 | + # --- C-style numeric arrays (int*, float*, double*, bool*) --- |
| 155 | + if mType in _ARRAY_ELEM_TYPES: |
| 156 | + elem_type_name = _ARRAY_ELEM_TYPES[mType] |
| 157 | + ptr = _read_pointer(process, store_addr) |
| 158 | + if not ptr or ptr == 0: |
| 159 | + return f'{type_name}(null)' |
| 160 | + elem_t = target.FindFirstType(elem_type_name) |
| 161 | + if not elem_t.IsValid(): |
| 162 | + return f'{type_name}(? x {mSize})' |
| 163 | + count = min(mSize, MAX_ARRAY_DISPLAY) |
| 164 | + items = [] |
| 165 | + for i in range(count): |
| 166 | + v = valobj.CreateValueFromAddress(f'e{i}', ptr + i * elem_t.GetByteSize(), elem_t) |
| 167 | + items.append(v.GetValue() or '?') |
| 168 | + result = f'{type_name}([{", ".join(items)}]' |
| 169 | + if mSize > MAX_ARRAY_DISPLAY: |
| 170 | + result += f', ... ({mSize} total)' |
| 171 | + result += ')' |
| 172 | + return result |
| 173 | + |
| 174 | + # --- ArrayString: std::vector<std::string> stored via placement new in mStore --- |
| 175 | + if mType == 10: |
| 176 | + # libc++ std::vector layout: __begin_, __end_, __end_cap_ (all pointers) |
| 177 | + # libc++ std::string is always 24 bytes on 64-bit (SSO layout) |
| 178 | + STR_SIZE = 24 |
| 179 | + ptr_size = process.GetAddressByteSize() |
| 180 | + begin_ptr = _read_pointer(process, store_addr) |
| 181 | + end_ptr = _read_pointer(process, store_addr + ptr_size) |
| 182 | + if begin_ptr is None or end_ptr is None: |
| 183 | + return 'ArrayString(?)' |
| 184 | + |
| 185 | + count = (end_ptr - begin_ptr) // STR_SIZE if end_ptr >= begin_ptr else 0 |
| 186 | + items = [] |
| 187 | + for i in range(min(count, MAX_ARRAY_DISPLAY)): |
| 188 | + items.append(_read_libcxx_string(process, begin_ptr + i * STR_SIZE)) |
| 189 | + result = f'ArrayString([{", ".join(items)}]' |
| 190 | + if count > MAX_ARRAY_DISPLAY: |
| 191 | + result += f', ... ({count} total)' |
| 192 | + result += ')' |
| 193 | + return result |
| 194 | + |
| 195 | + return f'{type_name}(mSize={mSize})' |
| 196 | + |
| 197 | + |
| 198 | +class VariantSyntheticProvider: |
| 199 | + """Synthetic children for o2::framework::Variant — exposes decoded value as child.""" |
| 200 | + |
| 201 | + def __init__(self, valobj, _internal_dict): |
| 202 | + self.valobj = valobj |
| 203 | + self.children = [] |
| 204 | + |
| 205 | + def num_children(self): |
| 206 | + return len(self.children) |
| 207 | + |
| 208 | + def get_child_index(self, name): |
| 209 | + for i, (n, _) in enumerate(self.children): |
| 210 | + if n == name: |
| 211 | + return i |
| 212 | + return -1 |
| 213 | + |
| 214 | + def get_child_at_index(self, index): |
| 215 | + if 0 <= index < len(self.children): |
| 216 | + return self.children[index][1] |
| 217 | + return None |
| 218 | + |
| 219 | + def update(self): |
| 220 | + self.children = [] |
| 221 | + # Use GetNonSyntheticValue() to read the real struct members. |
| 222 | + raw = self.valobj.GetNonSyntheticValue() |
| 223 | + mType = raw.GetChildMemberWithName('mType').GetValueAsUnsigned(26) |
| 224 | + mSize = raw.GetChildMemberWithName('mSize').GetValueAsUnsigned(1) |
| 225 | + mStore = raw.GetChildMemberWithName('mStore') |
| 226 | + store_addr = mStore.GetLoadAddress() |
| 227 | + target = self.valobj.GetTarget() |
| 228 | + process = self.valobj.GetProcess() |
| 229 | + |
| 230 | + if mType in _SIMPLE_TYPES: |
| 231 | + ctype, _ = _SIMPLE_TYPES[mType] |
| 232 | + t = target.FindFirstType(ctype) |
| 233 | + if t.IsValid(): |
| 234 | + v = self.valobj.CreateValueFromAddress('value', store_addr, t) |
| 235 | + self.children.append(('value', v)) |
| 236 | + |
| 237 | + elif mType == 4: # String |
| 238 | + ptr = _read_pointer(process, store_addr) |
| 239 | + if ptr and ptr != 0: |
| 240 | + char_t = target.FindFirstType('char').GetPointerType() |
| 241 | + v = self.valobj.CreateValueFromAddress('value', store_addr, char_t) |
| 242 | + self.children.append(('value', v)) |
| 243 | + |
| 244 | + elif mType in _ARRAY_ELEM_TYPES: |
| 245 | + elem_type_name = _ARRAY_ELEM_TYPES[mType] |
| 246 | + ptr = _read_pointer(process, store_addr) |
| 247 | + if ptr and ptr != 0: |
| 248 | + elem_t = target.FindFirstType(elem_type_name) |
| 249 | + if elem_t.IsValid(): |
| 250 | + for i in range(min(mSize, MAX_ARRAY_DISPLAY)): |
| 251 | + v = self.valobj.CreateValueFromAddress(f'[{i}]', ptr + i * elem_t.GetByteSize(), elem_t) |
| 252 | + self.children.append((f'[{i}]', v)) |
| 253 | + |
| 254 | + elif mType == 10: # ArrayString |
| 255 | + # std::vector<std::string> via placement new; std::string = 24 bytes (libc++ 64-bit) |
| 256 | + STR_SIZE = 24 |
| 257 | + ptr_size = process.GetAddressByteSize() |
| 258 | + begin_ptr = _read_pointer(process, store_addr) |
| 259 | + end_ptr = _read_pointer(process, store_addr + ptr_size) |
| 260 | + char_t = target.FindFirstType('char') |
| 261 | + if begin_ptr is not None and end_ptr is not None and end_ptr >= begin_ptr and char_t.IsValid(): |
| 262 | + count = (end_ptr - begin_ptr) // STR_SIZE |
| 263 | + err = lldb.SBError() |
| 264 | + for i in range(min(count, MAX_ARRAY_DISPLAY)): |
| 265 | + str_addr = begin_ptr + i * STR_SIZE |
| 266 | + raw = process.ReadMemory(str_addr, STR_SIZE, err) |
| 267 | + if err.Fail() or not raw: |
| 268 | + continue |
| 269 | + b = raw if isinstance(raw[0], int) else bytes(ord(c) for c in raw) |
| 270 | + if (b[0] & 1) == 0: # short form: data inline at offset 1 |
| 271 | + data_addr = str_addr + 1 |
| 272 | + sz = max(b[0] >> 1, 1) |
| 273 | + else: # long form: data pointer at offset 16 |
| 274 | + data_addr = _struct.unpack_from('<Q', b, 16)[0] |
| 275 | + sz = max(_struct.unpack_from('<Q', b, 8)[0], 1) |
| 276 | + # Expose as char[sz] so lldb renders it as a string literal |
| 277 | + arr_t = char_t.GetArrayType(min(sz + 1, 256)) |
| 278 | + sv = self.valobj.CreateValueFromAddress(f'[{i}]', data_addr, arr_t) |
| 279 | + self.children.append((f'[{i}]', sv)) |
| 280 | + |
| 281 | + return False # no pruning needed |
| 282 | + |
| 283 | + def has_children(self): |
| 284 | + return len(self.children) > 0 |
| 285 | + |
| 286 | + |
| 287 | +def __lldb_init_module(debugger, _internal_dict): |
| 288 | + debugger.HandleCommand( |
| 289 | + 'type summary add -x "^o2::framework::Variant$" ' |
| 290 | + '--python-function lldb_o2_formatters.variant_summary' |
| 291 | + ) |
| 292 | + debugger.HandleCommand( |
| 293 | + 'type synthetic add -x "^o2::framework::Variant$" ' |
| 294 | + '--python-class lldb_o2_formatters.VariantSyntheticProvider' |
| 295 | + ) |
| 296 | + print('o2::framework::Variant formatters loaded.') |
0 commit comments