How to use the drgn.type.IntType function in drgn

To help you get started, we’ve selected a few drgn examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github osandov / drgn / tests / test_typeindex.py View on Github external
from drgn.typename import BasicTypeName, TypedefTypeName
from tests.test_type import TypeTestCase


TYPES = {
    'void': VoidType(),
    '_Bool': BoolType('_Bool', 1),
    'char': IntType('char', 1, True),
    'signed char': IntType('signed char', 1, True),
    'unsigned char': IntType('unsigned char', 1, False),
    'short': IntType('short', 2, True),
    'unsigned short': IntType('unsigned short', 2, False),
    'int': IntType('int', 4, True),
    'unsigned int': IntType('unsigned int', 4, False),
    'long': IntType('long', 8, True),
    'unsigned long': IntType('unsigned long', 8, False),
    'long long': IntType('long long', 8, True),
    'unsigned long long': IntType('unsigned long long', 8, False),
    'float': FloatType('float', 4),
    'double': FloatType('double', 8),
    'long double': FloatType('long double', 16),
}
TYPES['ptrdiff_t'] = TypedefType('ptrdiff_t', TYPES['long'])


class MockTypeIndex(TypeIndex):
    def __init__(self):
        super().__init__(8)

    def _find_type(self, type_name, filename=None):
        if isinstance(type_name, (BasicTypeName, TypedefTypeName)):
            try:
github osandov / drgn / tests / test_dwarftypeindex.py View on Github external
]),
            # signed int
            Die(self.cu, DW_TAG.base_type, [
                DieAttrib(DW_AT.byte_size, DW_FORM.data1, b'\x04'),
                DieAttrib(DW_AT.encoding, DW_FORM.data1, b'\x05'),
                DieAttrib(DW_AT.name, DW_FORM.string, b'signed int'),
            ]),
            # unsigned int
            Die(self.cu, DW_TAG.base_type, [
                DieAttrib(DW_AT.byte_size, DW_FORM.data1, b'\x04'),
                DieAttrib(DW_AT.encoding, DW_FORM.data1, b'\x07'),
                DieAttrib(DW_AT.name, DW_FORM.string, b'unsigned int'),
            ]),
        ]

        self.assertFromDwarfType(0, IntType('int', 4, True))
        self.assertFromDwarfType(1, IntType('int', 4, True))
        self.assertFromDwarfType(2, IntType('unsigned int', 4, False))
github osandov / drgn / tests / test_dwarftypeindex.py View on Github external
Die(self.cu, DW_TAG.base_type, [
                DieAttrib(DW_AT.byte_size, DW_FORM.data1, b'\x08'),
                DieAttrib(DW_AT.encoding, DW_FORM.data1, b'\x05'),
                DieAttrib(DW_AT.name, DW_FORM.string, b'signed long'),
            ]),
            # unsigned long
            Die(self.cu, DW_TAG.base_type, [
                DieAttrib(DW_AT.byte_size, DW_FORM.data1, b'\x08'),
                DieAttrib(DW_AT.encoding, DW_FORM.data1, b'\x07'),
                DieAttrib(DW_AT.name, DW_FORM.string, b'unsigned long'),
            ]),
        ]

        self.assertFromDwarfType(0, IntType('long', 8, True))
        self.assertFromDwarfType(1, IntType('long', 8, True))
        self.assertFromDwarfType(2, IntType('unsigned long', 8, False))
github osandov / drgn / tests / test_dwarftypeindex.py View on Github external
DieAttrib(DW_AT.type, DW_FORM.ref4, 0),
            ]),
            # const void
            Die(self.cu, DW_TAG.const_type, []),
        ]

        self.assertFromDwarfType(1, IntType('int', 4, True, {'const'}))
        self.assertFromDwarfType(2, IntType('int', 4, True, {'volatile'}))
        self.assertFromDwarfType(3, IntType('int', 4, True, {'const', 'volatile'}))
        self.assertFromDwarfType(4, IntType('int', 4, True, {'_Atomic', 'const', 'volatile'}))
        self.assertFromDwarfType(5, IntType('int', 4, True, {'restrict'}))
        self.assertFromDwarfType(6, VoidType({'const'}))

        self.assertEqual(self.type_index._from_dwarf_type(self.cu.die(1),
                                                          frozenset({'volatile'})),
                         IntType('int', 4, True, {'const', 'volatile'}))
        self.assertEqual(self.type_index._from_dwarf_type(self.cu.die(6),
                                                          frozenset({'volatile'})),
                         VoidType({'const', 'volatile'}))
github osandov / drgn / tests / test_typeindex.py View on Github external
IntType,
    TypedefType,
    VoidType,
)
from drgn.typeindex import TypeIndex
from drgn.typename import BasicTypeName, TypedefTypeName
from tests.test_type import TypeTestCase


TYPES = {
    'void': VoidType(),
    '_Bool': BoolType('_Bool', 1),
    'char': IntType('char', 1, True),
    'signed char': IntType('signed char', 1, True),
    'unsigned char': IntType('unsigned char', 1, False),
    'short': IntType('short', 2, True),
    'unsigned short': IntType('unsigned short', 2, False),
    'int': IntType('int', 4, True),
    'unsigned int': IntType('unsigned int', 4, False),
    'long': IntType('long', 8, True),
    'unsigned long': IntType('unsigned long', 8, False),
    'long long': IntType('long long', 8, True),
    'unsigned long long': IntType('unsigned long long', 8, False),
    'float': FloatType('float', 4),
    'double': FloatType('double', 8),
    'long double': FloatType('long double', 16),
}
TYPES['ptrdiff_t'] = TypedefType('ptrdiff_t', TYPES['long'])


class MockTypeIndex(TypeIndex):
    def __init__(self):
github osandov / drgn / drgn / type.py View on Github external
if self.qualifiers:
            return IntType(self.name, self.size, self.signed)
        return self

    def is_integer(self) -> bool:
        return True

    def _convert(self, value: Any) -> int:
        try:
            value = value.__int__()
        except AttributeError:
            raise TypeError(f'cannot convert to {self.name!r}') from None
        return _int_convert(value, 8 * self.size, self.signed)


class BoolType(IntType):
    """
    A BoolType represents a boolean type. It has a name, size, and qualifiers.
    See help(IntType), help(ArithmeticType), and help(Type) for more
    information.
    """

    def __init__(self, name: str, size: int,
                 qualifiers: Iterable[str] = frozenset()) -> None:
        super().__init__(name, size, False, qualifiers)
        setattr(self, '_read', _BOOL_READ[size])

    def __repr__(self) -> str:
        return ArithmeticType.__repr__(self)

    def unqualified(self) -> 'BoolType':
        if self.qualifiers:
github osandov / drgn / drgn / typeindex.py View on Github external
def _corresponding_unsigned_type(type_: Union[IntType, BitFieldType]) -> Union[IntType, BitFieldType]:
    if isinstance(type_, BitFieldType):
        if type_._int_type.signed:
            underlying_type = _corresponding_unsigned_type(type_._int_type)
            return BitFieldType(underlying_type, None, type_.bit_size)
        else:
            return type_
    elif isinstance(type_, BoolType):
        return type_
    else:
        if type_.signed:
            return IntType('unsigned ' + type_.name, type_.size, False)
        else:
            return type_
github osandov / drgn / drgn / internal / dwarftypeindex.py View on Github external
int_type: Type
                try:
                    type_die = dwarf_type.type()
                except DwarfAttribNotFoundError:
                    # GCC before 5.1 did not include DW_AT_type for
                    # DW_TAG_enumeration_type DIEs, so we have to fabricate the
                    # compatible type.
                    size = dwarf_type.size()
                    # GCC before 7.1 didn't include DW_AT_encoding for
                    # DW_TAG_enumeration_type DIEs, either, so we also have to
                    # guess at the sign.
                    signed = any(enumerator[1] < 0 for enumerator in enumerators)
                    int_type = IntType('', dwarf_type.size(), signed)
                else:
                    int_type = self._from_dwarf_type(type_die)
                    if not isinstance(int_type, IntType):
                        raise DwarfFormatError('enum compatible type is not an integer type')
                return EnumType(name, int_type, enumerators, qualifiers)
        elif dwarf_type.tag == DW_TAG.typedef:
            type_: Type = self._from_dwarf_type(
                dwarf_type.type(), may_be_flexible_array=may_be_flexible_array)
            return TypedefType(dwarf_type.name(), type_, qualifiers)
        elif dwarf_type.tag == DW_TAG.pointer_type:
            size = dwarf_type.size()
            try:
                deref_type = dwarf_type.type()
            except DwarfAttribNotFoundError:
                type_ = VoidType()
            else:
                type_ = self._from_dwarf_type(deref_type)
            return PointerType(size, type_, qualifiers)
        elif dwarf_type.tag == DW_TAG.array_type:
github osandov / drgn / drgn / internal / dwarftypeindex.py View on Github external
except (DwarfAttribNotFoundError, ValueError):
                pass

        name: Optional[str]
        size: Optional[int]
        if dwarf_type.tag == DW_TAG.base_type:
            encoding = dwarf_type.find_constant(DW_AT.encoding)
            name = str(parse_type_name(dwarf_type.name()))
            size = dwarf_type.size()
            if encoding == DW_ATE.boolean:
                return BoolType(name, size, qualifiers)
            elif encoding == DW_ATE.float:
                return FloatType(name, size, qualifiers)
            elif (encoding == DW_ATE.signed or
                  encoding == DW_ATE.signed_char):
                return IntType(name, size, True, qualifiers)
            elif (encoding == DW_ATE.unsigned or
                  encoding == DW_ATE.unsigned_char):
                return IntType(name, size, False, qualifiers)
            else:
                raise NotImplementedError(DW_ATE.str(encoding))
        elif (dwarf_type.tag == DW_TAG.structure_type or
              dwarf_type.tag == DW_TAG.union_type):
            if dwarf_type.find_flag(DW_AT.declaration):
                size = None
                members = None
            else:
                size = dwarf_type.size()
                member_dies = [child for child in dwarf_type.children()
                               if child.tag == DW_TAG.member]
                members = []
                for i, child in enumerate(member_dies):