How to use the drgn.Object 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
self.assertEqual(container_of(obj, point_type, 'y'),
                         Object(self.prog, pointer_type(8, point_type),
                                value=0xffff0008))

        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'),
github osandov / drgn / tests / test_object.py View on Github external
def test_member(self):
        reference = Object(self.prog, point_type, address=0xffff0000)
        unnamed_reference = Object(self.prog, struct_type('point', 8, (
            (struct_type(None, 8, point_type.members), None),
        )), address=0xffff0000)
        ptr = Object(self.prog, pointer_type(8, point_type), value=0xffff0000)
        for obj in [reference, unnamed_reference, ptr]:
            self.assertEqual(obj.member_('x'),
                             Object(self.prog, 'int', address=0xffff0000))
            self.assertEqual(obj.member_('x'), obj.x)
            self.assertEqual(obj.member_('y'),
                             Object(self.prog, 'int', address=0xffff0004))
            self.assertEqual(obj.member_('y'), obj.y)

            self.assertRaisesRegex(LookupError, "'struct point' has no member 'z'",
                                   obj.member_, 'z')
            self.assertRaisesRegex(AttributeError, "'struct point' has no member 'z'",
                                   getattr, obj, 'z')
github osandov / drgn / tests / test_object.py View on Github external
def test_bit_field_member(self):
        segment = b'\x07\x10\x5e\x5f\x1f\0\0\0'
        prog = mock_program(segments=[
            MockMemorySegment(segment, virt_addr=0xffff0000),
        ])

        type_ = struct_type('bits', 8, (
            (int_type('int', 4, True), 'x', 0, 4),
            (int_type('int', 4, True, Qualifiers.CONST), 'y', 4, 28),
            (int_type('int', 4, True), 'z', 32, 5),
        ))

        obj = Object(prog, type_, address=0xffff0000)
        self.assertEqual(obj.x,
                         Object(prog, int_type('int', 4, True),
                                address=0xffff0000, bit_field_size=4))
        self.assertEqual(
            obj.y,
            Object(prog, int_type('int', 4, True, Qualifiers.CONST),
                   address=0xffff0000, bit_field_size=28, bit_offset=4))
        self.assertEqual(obj.z,
                         Object(prog, int_type('int', 4, True),
                                address=0xffff0004, bit_field_size=5))
github osandov / drgn / tests / test_object.py View on Github external
def test_add(self):
        self._test_arithmetic(operator.add, 1, 2, 3, floating_point=True)

        ptr = Object(self.prog, 'int *', value=0xffff0000)
        arr = Object(self.prog, 'int [2]', address=0xffff0000)
        ptr1 = Object(self.prog, 'int *', value=0xffff0004)
        self.assertEqual(ptr + self.int(1), ptr1)
        self.assertEqual(self.unsigned_int(1) + ptr, ptr1)
        self.assertEqual(arr + self.int(1), ptr1)
        self.assertEqual(ptr1 + self.int(-1), ptr)
        self.assertEqual(self.int(-1) + ptr1, ptr)

        self.assertEqual(ptr + 1, ptr1)
        self.assertEqual(1 + ptr, ptr1)
        self.assertRaises(TypeError, operator.add, ptr, ptr)
        self.assertRaises(TypeError, operator.add, ptr, 2.0)
        self.assertRaises(TypeError, operator.add, 2.0, ptr)

        void_ptr = Object(self.prog, 'void *', value=0xffff0000)
        void_ptr1 = Object(self.prog, 'void *', value=0xffff0001)
        self.assertEqual(void_ptr + self.int(1), void_ptr1)
        self.assertEqual(self.unsigned_int(1) + void_ptr, void_ptr1)
github osandov / drgn / tests / test_object.py View on Github external
])
        strings = [
            (Object(prog, 'char *', address=0xfffefff8), b'hello'),
            (Object(prog, 'char [2]', address=0xffff0000), b'he'),
            (Object(prog, 'char [8]', address=0xffff0000), b'hello'),
        ]
        for obj, expected in strings:
            with self.subTest(obj=obj):
                self.assertEqual(obj.string_(), expected)
                self.assertEqual(obj.read_().string_(), expected)

        strings = [
            Object(prog, 'char []', address=0xffff0000),
            Object(prog, 'int []', address=0xffff0000),
            Object(prog, 'int [2]', address=0xffff0000),
            Object(prog, 'int *', value=0xffff0000),
        ]
        for obj in strings:
            self.assertEqual(obj.string_(), b'hello')

        self.assertRaisesRegex(TypeError, 'must be an array or pointer',
                               Object(prog, 'int', value=1).string_)
github osandov / drgn / tests / test_object.py View on Github external
def test_extended_integer(self):
        self.assertEqual(+Object(self.prog, int_type('byte', 1, True), value=1),
                         Object(self.prog, 'int', value=1))
        self.assertEqual(+Object(self.prog, int_type('ubyte', 1, False), value=-1),
                         Object(self.prog, 'int', value=0xff))
        self.assertEqual(+Object(self.prog, int_type('qword', 8, True), value=1),
                         Object(self.prog, int_type('qword', 8, True), value=1))
        self.assertEqual(+Object(self.prog, int_type('qword', 8, False), value=1),
                         Object(self.prog, int_type('qword', 8, False), value=1))
github delphix / sdb / tests / unit / commands / test_echo.py View on Github external
def test_multiple_piped():
    line = 'echo'
    objs = [
        drgn.Object(MOCK_PROGRAM, 'void *', value=0),
        drgn.Object(MOCK_PROGRAM, 'int', value=1),
    ]

    ret = invoke(MOCK_PROGRAM, objs, line)

    assert len(ret) == 2
    assert ret[0].value_() == 0
    assert ret[0].type_ == MOCK_PROGRAM.type('void *')
    assert ret[1].value_() == 1
    assert ret[1].type_ == MOCK_PROGRAM.type('int')
github osandov / drgn / tests / test_dwarf.py View on Github external
DwarfAttrib(DW_AT.low_pc, DW_FORM.addr, 0x7fc3eb9b1c30),
                ],
                [
                    DwarfDie(
                        DW_TAG.formal_parameter,
                        [DwarfAttrib(DW_AT.type, DW_FORM.ref4, 0)],
                    ),
                ]
            ),
        ]
        type_ = function_type(int_type('int', 4, True),
                              ((int_type('int', 1, True),),), False)

        prog = dwarf_program(dies)
        self.assertEqual(prog['abs'],
                         Object(prog, type_, address=0x7fc3eb9b1c30))
        self.assertEqual(prog.object('abs', FindObjectFlags.FUNCTION),
                         prog['abs'])
        self.assertRaisesRegex(LookupError, 'could not find variable',
                               prog.object, 'abs', FindObjectFlags.VARIABLE)

        del dies[1].attribs[2]
        prog = dwarf_program(dies)
        self.assertRaisesRegex(LookupError, 'could not find address',
                               prog.object, 'abs')
github delphix / sdb / sdb / commands / linux / internal / slub_helpers.py View on Github external
is set in the target
    """
    assert cache.type_.type_name() == 'struct kmem_cache *'
    assert p.type_.type_name() == 'void *'
    hardened_ptr = p + cache.offset.value_()

    #
    # We basically do what `freelist_dereference()` and
    # `freelist_ptr()` do in the kernel source:
    #
    # ptr <- (void *)*(unsigned long *)(hardened_ptr)
    #
    intermediate_ulong = drgn.Object(sdb.get_prog(),
                                     type='unsigned long',
                                     address=hardened_ptr.value_())
    ptr = drgn.Object(sdb.get_prog(),
                      type='void *',
                      value=intermediate_ulong.value_())

    #
    # ptr_addr <- (unsigned long)hardened_ptr
    #
    ptr_addr = drgn.Object(sdb.get_prog(),
                           type='unsigned long',
                           value=hardened_ptr.value_())

    #
    # return (void *)((unsigned long)ptr ^ cache->random ^ ptr_addr)
    #
    ptr_as_ulong = drgn.Object(sdb.get_prog(),
                               type='unsigned long',
                               value=ptr.value_())
github delphix / sdb / sdb / commands / threads.py View on Github external
EXAMPLE
        sdb> threads | filter 'obj.comm == "java"' | threads
        task               state         pid  prio comm
        ------------------ ------------- ---- ---- ----
        0xffff95d48b0e8000 INTERRUPTIBLE 4386 120  java
        0xffff95d48b0e96c0 INTERRUPTIBLE 4388 120  java
        0xffff95d48b0ead80 INTERRUPTIBLE 4387 120  java
        0xffff95d48b0edb00 INTERRUPTIBLE 4304 120  java
        0xffff95d4af20ad80 INTERRUPTIBLE 4395 120  java
    """

    names = ["threads", "thread"]
    input_type = "struct task_struct *"
    output_type = "struct task_struct *"

    FIELDS: Dict[str, Callable[[drgn.Object], Union[str, int]]] = {
        "task": lambda obj: hex(obj.value_()),
        "state": lambda obj: str(Stacks.task_struct_get_state(obj)),
        "pid": lambda obj: int(obj.pid),
        "prio": lambda obj: int(obj.prio),
        "comm": lambda obj: str(obj.comm.string_().decode("utf-8")),
    }

    def pretty_print(self, objs: Iterable[drgn.Object]) -> None:
        fields = list(Threads.FIELDS.keys())
        table = Table(fields, None, {"task": str})
        for obj in objs:
            row_dict = {field: Threads.FIELDS[field](obj) for field in fields}
            table.add_row(row_dict["task"], row_dict)
        table.print_()

    def no_input(self) -> Iterable[drgn.Object]: