How to use the drgn.cast 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_object.py View on Github external
def test_cast_array(self):
        obj = Object(self.prog, 'int []', address=0xffff0000)
        self.assertEqual(cast('int *', obj),
                         Object(self.prog, 'int *', value=0xffff0000))
        self.assertEqual(cast('void *', obj),
                         Object(self.prog, 'void *', value=0xffff0000))
        self.assertEqual(cast('unsigned long', obj),
                         Object(self.prog, 'unsigned long', value=0xffff0000))
        self.assertRaisesRegex(TypeError, r"cannot convert 'int \*' to 'int \[2]'",
                               cast, 'int [2]', obj)
github osandov / drgn / tests / test_object.py View on Github external
def test_cast_primitive_value(self):
        obj = Object(self.prog, 'long', value=2**32 + 1)
        self.assertEqual(cast('int', obj),
                         Object(self.prog, 'int', value=1))
        self.assertEqual(cast('int', obj.read_()),
                         Object(self.prog, 'int', value=1))
        self.assertEqual(cast('const int', Object(self.prog, 'int', value=1)),
                         Object(self.prog, 'const int', value=1))
        self.assertRaisesRegex(TypeError,
                               "cannot convert 'int' to 'struct point'", cast,
                               point_type, Object(self.prog, 'int', value=1))
github osandov / drgn / tests / test_object.py View on Github external
def test_cast_array(self):
        obj = Object(self.prog, 'int []', address=0xffff0000)
        self.assertEqual(cast('int *', obj),
                         Object(self.prog, 'int *', value=0xffff0000))
        self.assertEqual(cast('void *', obj),
                         Object(self.prog, 'void *', value=0xffff0000))
        self.assertEqual(cast('unsigned long', obj),
                         Object(self.prog, 'unsigned long', value=0xffff0000))
        self.assertRaisesRegex(TypeError, r"cannot convert 'int \*' to 'int \[2]'",
                               cast, 'int [2]', obj)
github osandov / drgn / tests / test_object.py View on Github external
def test_cast_primitive_value(self):
        obj = Object(self.prog, 'long', value=2**32 + 1)
        self.assertEqual(cast('int', obj),
                         Object(self.prog, 'int', value=1))
        self.assertEqual(cast('int', obj.read_()),
                         Object(self.prog, 'int', value=1))
        self.assertEqual(cast('const int', Object(self.prog, 'int', value=1)),
                         Object(self.prog, 'const int', value=1))
        self.assertRaisesRegex(TypeError,
                               "cannot convert 'int' to 'struct point'", cast,
                               point_type, Object(self.prog, 'int', value=1))
github osandov / drgn / tests / test_object.py View on Github external
def test_cast_compound_value(self):
        obj = Object(self.prog, point_type, address=0xffff0000).read_()
        self.assertEqual(cast(point_type, obj), obj)
        const_point_type = point_type.qualified(Qualifiers.CONST)
        self.assertEqual(cast(const_point_type, obj),
                         Object(self.prog, const_point_type,
                                address=0xffff0000).read_())
        self.assertRaisesRegex(TypeError,
                               "cannot convert 'struct point' to 'enum color'",
                               cast, color_type, obj)
github osandov / drgn / tests / test_object.py View on Github external
def test_cast_array(self):
        obj = Object(self.prog, 'int []', address=0xffff0000)
        self.assertEqual(cast('int *', obj),
                         Object(self.prog, 'int *', value=0xffff0000))
        self.assertEqual(cast('void *', obj),
                         Object(self.prog, 'void *', value=0xffff0000))
        self.assertEqual(cast('unsigned long', obj),
                         Object(self.prog, 'unsigned long', value=0xffff0000))
        self.assertRaisesRegex(TypeError, r"cannot convert 'int \*' to 'int \[2]'",
                               cast, 'int [2]', obj)
github delphix / sdb / sdb / command.py View on Github external
def _call(self, objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
        for obj in objs:
            try:
                yield drgn.cast(self.type, obj)
            except TypeError as err:
                raise CommandError(self.name, str(err))
github delphix / sdb / sdb / commands / linux / dmesg.py View on Github external
def no_input(self) -> Iterable[drgn.Object]:
        log_idx = sdb.get_object("log_first_idx")
        log_seq = sdb.get_object("clear_seq")
        log_end = sdb.get_object("log_next_seq")
        log_buf = sdb.get_object("log_buf")

        while log_seq < log_end:
            entry = drgn.cast('struct printk_log *', log_buf + log_idx)

            yield entry

            if entry.len == 0:
                log_idx = 0
            else:
                log_idx += entry.len
            log_seq += 1
github delphix / sdb / sdb / commands / cast.py View on Github external
def call(self, objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
        for obj in objs:
            try:
                yield drgn.cast(self.type, obj)
            except TypeError as err:
                raise sdb.CommandError(self.name, str(err))
github delphix / sdb / sdb / coerce.py View on Github external
This function attemts to massage the input object into an object
        of a different type.
        """

        # same type is fine
        if obj.type_ == self.type:
            return obj

        # "void *" can be coerced to any pointer type
        if (obj.type_.kind is drgn.TypeKind.POINTER and
                obj.type_.type.primitive is drgn.PrimitiveType.C_VOID):
            return drgn.cast(self.type, obj)

        # integers can be coerced to any pointer typo
        if obj.type_.kind is drgn.TypeKind.INT:
            return drgn.cast(self.type, obj)

        # "type" can be coerced to "type *"
        if obj.type_.kind is not drgn.TypeKind.POINTER and obj.address_of_(
        ).type_ == self.type:
            return obj.address_of_()

        raise sdb.CommandError(
            self.name, "can not coerce {} to {}".format(obj.type_, self.type))