How to use the drgn.container_of 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_container_of(self):
        obj = Object(self.prog, 'int *', value=0xffff000c)
        container_of(obj, point_type, 'x')
        self.assertEqual(container_of(obj, point_type, 'x'),
                         Object(self.prog, pointer_type(8, point_type),
                                value=0xffff000c))
        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'),
        ))
github osandov / drgn / drgn / helpers / linux / list.py View on Github external
def list_next_entry(pos, member):
    """
    .. c:function:: type *list_next_entry(type *pos, member)

    Return the next entry in a list.
    """
    return container_of(getattr(pos, member).next, pos.type_.type, member)
github osandov / drgn / drgn / helpers / linux / fs.py View on Github external
def inode_path(inode):
    """
    .. c:function:: char *inode_path(struct inode *inode)

    Return any path of an inode from the root of its filesystem.
    """
    if hlist_empty(inode.i_dentry):
        return None
    return dentry_path(container_of(inode.i_dentry.first, 'struct dentry',
                                    'd_u.d_alias'))
github osandov / drgn / drgn / helpers / linux / list.py View on Github external
def hlist_for_each_entry(type, head, member):
    """
    .. c:function:: hlist_for_each_entry(type, struct hlist_head *head, member)

    Iterate over all of the entries in a has list, given the type of the entry
    and the ``struct hlist_node`` member in that type.

    :return: Iterator of ``type *`` objects.
    """
    for pos in hlist_for_each(head):
        yield container_of(pos, type, member)
github osandov / drgn / drgn / helpers / linux / pid.py View on Github external
prog = prog_or_ns
        ns = prog_or_ns['init_pid_ns'].address_of_()
    else:
        prog = prog_or_ns.prog_
        ns = prog_or_ns
    if hasattr(ns, 'idr'):
        for nr, entry in idr_for_each(ns.idr):
            yield cast('struct pid *', entry)
    else:
        pid_hash = prog['pid_hash']
        for i in range(1 << prog['pidhash_shift'].value_()):
            for upid in hlist_for_each_entry('struct upid',
                                             pid_hash[i].address_of_(),
                                             'pid_chain'):
                if upid.ns == ns:
                    yield container_of(upid, 'struct pid',
                                       f'numbers[{int(ns.level)}]')
github delphix / sdb / sdb / commands / container_of.py View on Github external
def _call(self, objs: Iterable[drgn.Object]) -> Iterable[drgn.Object]:
        sname = get_valid_struct_name(self, self.args.struct_name)
        for obj in objs:
            try:
                container_obj = drgn.container_of(obj, sname, self.args.member)
            except (TypeError, LookupError) as err:
                raise sdb.CommandError(self.name, str(err))
            yield container_obj
github osandov / drgn / drgn / helpers / linux / block.py View on Github external
def for_each_partition(prog):
    """
    Iterate over all partitions in the system.

    :return: Iterator of ``struct hd_struct *`` objects.
    """
    for device in _for_each_block_device(prog):
        yield container_of(device, 'struct hd_struct', '__dev')
github osandov / drgn / drgn / helpers / linux / list.py View on Github external
def list_prev_entry(pos, member):
    """
    .. c:function:: type *list_prev_entry(type *pos, member)

    Return the previous entry in a list.
    """
    return container_of(getattr(pos, member).prev, pos.type_.type, member)
github osandov / drgn / drgn / helpers / linux / list.py View on Github external
def list_first_entry(head, type, member):
    """
    .. c:function:: type *list_first_entry(struct list_head *head, type, member)

    Return the first entry in a list.

    The list is assumed to be non-empty.

    See also :func:`list_first_entry_or_null()`.
    """
    return container_of(head.next, type, member)
github osandov / drgn / drgn / helpers / linux / rbtree.py View on Github external
def rb_find(type, root, member, key, cmp):
    """
    .. c:function:: type *rb_find(type, struct rb_root *root, member, key_type key, int (*cmp)(key_type, type *))

    Find an entry in a red-black tree, given a key and a comparator function
    which takes the key and an entry. The comparator should return < 0 if the
    key is less than the entry, > 0 if it is greater than the entry, or 0 if it
    matches the entry. This returns a ``NULL`` object if no entry matches the
    key.

    Note that this function does not have an analogue in the Linux kernel
    source code, as tree searches are all open-coded.
    """
    node = root.rb_node.read_()
    while node:
        entry = container_of(node, type, member)
        ret = cmp(key, entry)
        if ret < 0:
            node = node.rb_left.read_()
        elif ret > 0:
            node = node.rb_right.read_()
        else:
            return entry
    return node