xref: /openbmc/qemu/scripts/qemugdb/aio.py (revision dc5bd18f)
1#!/usr/bin/python
2
3# GDB debugging support: aio/iohandler debug
4#
5# Copyright (c) 2015 Red Hat, Inc.
6#
7# Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
8#
9# This work is licensed under the terms of the GNU GPL, version 2 or
10# later.  See the COPYING file in the top-level directory.
11#
12
13import gdb
14from qemugdb import coroutine
15
16def isnull(ptr):
17    return ptr == gdb.Value(0).cast(ptr.type)
18
19def dump_aiocontext(context, verbose):
20    '''Display a dump and backtrace for an aiocontext'''
21    cur = context['aio_handlers']['lh_first']
22    # Get pointers to functions we're going to process specially
23    sym_fd_coroutine_enter = gdb.parse_and_eval('fd_coroutine_enter')
24
25    while not isnull(cur):
26        entry = cur.dereference()
27        gdb.write('----\n%s\n' % entry)
28        if verbose and cur['io_read'] == sym_fd_coroutine_enter:
29            coptr = (cur['opaque'].cast(gdb.lookup_type('FDYieldUntilData').pointer()))['co']
30            coptr = coptr.cast(gdb.lookup_type('CoroutineUContext').pointer())
31            coroutine.bt_jmpbuf(coptr['env']['__jmpbuf'])
32        cur = cur['node']['le_next'];
33
34    gdb.write('----\n')
35
36class HandlersCommand(gdb.Command):
37    '''Display aio handlers'''
38    def __init__(self):
39        gdb.Command.__init__(self, 'qemu handlers', gdb.COMMAND_DATA,
40                             gdb.COMPLETE_NONE)
41
42    def invoke(self, arg, from_tty):
43        verbose = False
44        argv = gdb.string_to_argv(arg)
45
46        if len(argv) > 0 and argv[0] == '--verbose':
47            verbose = True
48            argv.pop(0)
49
50        if len(argv) > 1:
51            gdb.write('usage: qemu handlers [--verbose] [handler]\n')
52            return
53
54        if len(argv) == 1:
55            handlers_name = argv[0]
56        else:
57            handlers_name = 'qemu_aio_context'
58        dump_aiocontext(gdb.parse_and_eval(handlers_name), verbose)
59