How to use the drgn.struct_type 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_dwarf.py View on Github external
),
                    DwarfDie(
                        DW_TAG.member,
                        [
                            DwarfAttrib(DW_AT.name, DW_FORM.string, 'z'),
                            DwarfAttrib(DW_AT.bit_size, DW_FORM.data1, 20),
                            DwarfAttrib(DW_AT.data_bit_offset, DW_FORM.data1, 44),
                            DwarfAttrib(DW_AT.type, DW_FORM.ref4, 1),
                        ],
                    ),
                ],
            ),
            int_die,
        ]

        t = struct_type('point', 8, [
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32, 12),
            (int_type('int', 4, True), 'z', 44, 20),
        ])

        # With DW_AT_data_bit_offset.
        self.assertFromDwarf(dies, t, little_endian=True)
        self.assertFromDwarf(dies, t, little_endian=False)

        # With DW_AT_bit_offset on big-endian.
        dies[0].children[1].attribs[2] = DwarfAttrib(DW_AT.bit_offset,
                                                     DW_FORM.data1, 32)
        dies[0].children[2].attribs[2] = DwarfAttrib(DW_AT.bit_offset,
                                                     DW_FORM.data1, 44)
        self.assertFromDwarf(dies, t, little_endian=False)
github delphix / sdb / tests / __init__.py View on Github external
};
    ```
    """
    assert len(member_names) == len(member_types)
    struct_size, bit_offset = 0, 0
    member_list = []
    for member_name, type_ in zip(member_names, member_types):
        member_tuple = (type_, member_name, bit_offset, 0)
        member_list.append(member_tuple)
        if type_.kind == drgn.TypeKind.ARRAY:
            bit_offset += 8 * type_.length * type_.type.size
            struct_size += type_.length * type_.type.size
        else:
            bit_offset += 8 * type_.size
            struct_size += type_.size
    return drgn.struct_type(name, struct_size, member_list)
github osandov / drgn / tests / test_object.py View on Github external
self.assertEqual(container_of(obj, line_segment_type, 'a.x'),
                         Object(self.prog, pointer_type(8, line_segment_type),
                                value=0xffff000c))
        self.assertEqual(container_of(obj, line_segment_type, 'b.x'),
                         Object(self.prog, pointer_type(8, line_segment_type),
                                value=0xffff0004))

        polygon_type = struct_type('polygon', 0, (
            (array_type(None, point_type), 'points'),
        ))
        self.assertEqual(container_of(obj, polygon_type, 'points[3].x'),
                         Object(self.prog, pointer_type(8, polygon_type),
                                value=0xfffefff4))

        small_point_type = struct_type('small_point', 1, (
            (int_type('int', 4, True), 'x', 0, 4),
            (int_type('int', 4, True), 'y', 4, 4),
        ))
        self.assertRaisesRegex(ValueError,
                               r'container_of\(\) member is not byte-aligned',
                               container_of, obj, small_point_type, 'y')

        self.assertRaisesRegex(TypeError,
                               r'container_of\(\) argument must be a pointer',
                               container_of, obj[0], point_type, 'x')

        self.assertRaisesRegex(TypeError, 'not a structure, union, or class',
                               container_of, obj, obj.type_, 'x'),

        type_ = struct_type('foo', 16, (
            (array_type(8, int_type('int', 4, True)), 'arr'),
github osandov / drgn / tests / test_type.py View on Github external
        f = lambda: struct_type('foo', 0, ((f, 'next'),))
        self.assertEqual(repr(f()),
github delphix / sdb / tests / unit / __init__.py View on Github external
};
    ```
    """
    assert len(member_names) == len(member_types)
    struct_size, bit_offset = 0, 0
    member_list = []
    for member_name, type_ in zip(member_names, member_types):
        member_type = drgn.TypeMember(type_, member_name, bit_offset, 0)
        member_list.append(member_type)
        if type_.kind == drgn.TypeKind.ARRAY:
            bit_offset += 8 * type_.length * type_.type.size
            struct_size += type_.length * type_.type.size
        else:
            bit_offset += 8 * type_.size
            struct_size += type_.size
    return drgn.struct_type(name, struct_size, member_list)
github osandov / drgn / tests / test_type.py View on Github external
def test_cycle(self):
        t1 = struct_type('foo', 8, ((lambda: pointer_type(8, t1), 'next'),))
        t2 = struct_type('foo', 8, ((lambda: pointer_type(8, t2), 'next'),))
        t3, t4 = (struct_type('foo', 8, ((lambda: pointer_type(8, t4), 'next'),)),
                  struct_type('foo', 8, ((lambda: pointer_type(8, t3), 'next'),)))
        self.assertEqual(t1, t2)
        self.assertEqual(t2, t3)
        self.assertEqual(t3, t4)

        self.assertEqual(repr(t1), "struct_type(tag='foo', size=8, members=((pointer_type(size=8, type=struct_type(tag='foo', ...)), 'next', 0, 0),))")
github osandov / drgn / tests / test_object.py View on Github external
.y = -1,
}""")
        self.assertEqual(obj.format_(members_same_line=True),
                         "(struct point){ .x = (int)99, .y = (int)-1 }")
        self.assertEqual(obj.format_(member_names=False), """\
(struct point){
	(int)99,
	(int)-1,
}""")
        self.assertEqual(
            obj.format_(members_same_line=True, member_names=False),
            "(struct point){ (int)99, (int)-1 }")

        type_ = struct_type('foo', 16, (
            (point_type, 'point'),
            (struct_type(None, 8, (
                (int_type('int', 4, True), 'bar'),
                (int_type('int', 4, True), 'baz', 32),
            )), None, 64),
        ))
        obj = Object(prog, type_, address=0xffff0000)
        expected = """\
(struct foo){
	.point = (struct point){
		.x = (int)99,
		.y = (int)-1,
	},
	.bar = (int)12345,
	.baz = (int)0,
}"""
        self.assertEqual(str(obj), expected)
        self.assertEqual(str(obj.read_()), expected)
github osandov / drgn / tests / test_type.py View on Github external
self.assertEqual(t.kind, TypeKind.STRUCT)
        self.assertIsNone(t.primitive)
        self.assertEqual(t.tag, 'point')
        self.assertEqual(t.size, 8)
        self.assertEqual(t.members, (
            (int_type('int', 4, True), 'x', 0, 0),
            (int_type('int', 4, True), 'y', 32, 0),
        ))
        self.assertTrue(t.is_complete())

        self.assertEqual(t, struct_type('point', 8, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        )))
        # Different tag.
        self.assertNotEqual(t, struct_type('pt', 8, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        )))
        # Different size.
        self.assertNotEqual(t, struct_type('point', 16, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        )))
        # One is anonymous.
        self.assertNotEqual(t, struct_type(None, 8, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        )))
        # Different members.
        self.assertNotEqual(t, struct_type('point', 8, (
            (int_type('long', 8, True), 'x', 0),
github osandov / drgn / tests / test_type.py View on Github external
def test_struct(self):
        t = struct_type('point', 8, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        ))
        self.assertEqual(t.kind, TypeKind.STRUCT)
        self.assertIsNone(t.primitive)
        self.assertEqual(t.tag, 'point')
        self.assertEqual(t.size, 8)
        self.assertEqual(t.members, (
            (int_type('int', 4, True), 'x', 0, 0),
            (int_type('int', 4, True), 'y', 32, 0),
        ))
        self.assertTrue(t.is_complete())

        self.assertEqual(t, struct_type('point', 8, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        )))
        # Different tag.
        self.assertNotEqual(t, struct_type('pt', 8, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        )))
        # Different size.
        self.assertNotEqual(t, struct_type('point', 16, (
            (int_type('int', 4, True), 'x', 0),
            (int_type('int', 4, True), 'y', 32),
        )))
        # One is anonymous.
        self.assertNotEqual(t, struct_type(None, 8, (
            (int_type('int', 4, True), 'x', 0),