| 1 |
# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import ctypes |
| 5 |
|
| 6 |
import _drgn |
| 7 |
|
| 8 |
_drgn_cdll = ctypes.CDLL(_drgn.__file__) |
| 9 |
|
| 10 |
|
| 11 |
class _drgn_error(ctypes.Structure): |
| 12 |
_fields_ = [ |
| 13 |
("code", ctypes.c_uint), |
| 14 |
("errnum", ctypes.c_int), |
| 15 |
("path", ctypes.c_char_p), |
| 16 |
("msg", ctypes.c_char_p), |
| 17 |
] |
| 18 |
|
| 19 |
|
| 20 |
class _drgn_type(ctypes.Structure): |
| 21 |
pass |
| 22 |
|
| 23 |
|
| 24 |
class _drgn_qualified_type(ctypes.Structure): |
| 25 |
_fields_ = [ |
| 26 |
("type", ctypes.POINTER(_drgn_type)), |
| 27 |
("qualifiers", ctypes.c_uint), |
| 28 |
] |
| 29 |
|
| 30 |
|
| 31 |
class _drgn_token(ctypes.Structure): |
| 32 |
_fields_ = [ |
| 33 |
("kind", ctypes.c_int), |
| 34 |
("value", ctypes.c_void_p), |
| 35 |
("len", ctypes.c_size_t), |
| 36 |
] |
| 37 |
|
| 38 |
|
| 39 |
_drgn_cdll.drgn_test_serialize_bits.restype = None |
| 40 |
_drgn_cdll.drgn_test_serialize_bits.argtypes = [ |
| 41 |
ctypes.c_void_p, |
| 42 |
ctypes.c_uint64, |
| 43 |
ctypes.c_uint64, |
| 44 |
ctypes.c_uint8, |
| 45 |
ctypes.c_bool, |
| 46 |
] |
| 47 |
_drgn_cdll.drgn_test_deserialize_bits.restype = ctypes.c_uint64 |
| 48 |
_drgn_cdll.drgn_test_deserialize_bits.argtypes = [ |
| 49 |
ctypes.c_void_p, |
| 50 |
ctypes.c_uint64, |
| 51 |
ctypes.c_uint8, |
| 52 |
ctypes.c_bool, |
| 53 |
] |
| 54 |
|
| 55 |
|
| 56 |
def serialize_bits(buf, bit_offset, uvalue, bit_size, little_endian): |
| 57 |
assert (bit_offset + bit_size + 7) // 8 <= len(buf) |
| 58 |
c_buf = (ctypes.c_char * len(buf)).from_buffer(buf) |
| 59 |
return _drgn_cdll.drgn_test_serialize_bits( |
| 60 |
c_buf, bit_offset, uvalue, bit_size, little_endian |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
def deserialize_bits(buf, bit_offset, bit_size, little_endian): |
| 65 |
assert (bit_offset + bit_size + 7) // 8 <= len(buf) |
| 66 |
c_buf = (ctypes.c_char * len(buf)).from_buffer_copy(buf) |
| 67 |
return _drgn_cdll.drgn_test_deserialize_bits( |
| 68 |
c_buf, bit_offset, bit_size, little_endian |
| 69 |
) |