public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* How to backtrace an separate stack?
@ 2022-03-03 11:22 Stefan Hajnoczi
  2022-03-07 10:49 ` Pedro Alves
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Stefan Hajnoczi @ 2022-03-03 11:22 UTC (permalink / raw)
  To: gdb; +Cc: qemu-devel, Dr. David Alan Gilbert, tom, pedro

[-- Attachment #1: Type: text/plain, Size: 2086 bytes --]

Hi,
The QEMU emulator uses coroutines with separate stacks. It can be
challenging to debug coroutines that have yielded because GDB is not
aware of them (no thread is currently executing them).

QEMU has a GDB Python script that helps. It "creates" a stack frame for
a given coroutine by temporarily setting register values and then using
the "bt" command. This works on a live process under ptrace control but
not for coredumps where registers can't be set.

Here is the script (or see the bottom of this email for an inline copy
of the relevant code):
https://gitlab.com/qemu-project/qemu/-/blob/master/scripts/qemugdb/coroutine.py

I hoped that "select-frame address ADDRESS" could be used instead so
this would work on coredumps too. Unfortunately "select-frame" only
searches stack frames that GDB is already aware of, so it cannot be used
to backtrace coroutine stacks.

Is there a way to backtrace a stack at an arbitrary address in GDB?

Thanks,
Stefan
---
def get_jmpbuf_regs(jmpbuf):
    JB_RBX  = 0
    JB_RBP  = 1
    JB_R12  = 2
    JB_R13  = 3
    JB_R14  = 4
    JB_R15  = 5
    JB_RSP  = 6
    JB_PC   = 7

    pointer_guard = get_glibc_pointer_guard()
    return {'rbx': jmpbuf[JB_RBX],
        'rbp': glibc_ptr_demangle(jmpbuf[JB_RBP], pointer_guard),
        'rsp': glibc_ptr_demangle(jmpbuf[JB_RSP], pointer_guard),
        'r12': jmpbuf[JB_R12],
        'r13': jmpbuf[JB_R13],
        'r14': jmpbuf[JB_R14],
        'r15': jmpbuf[JB_R15],
        'rip': glibc_ptr_demangle(jmpbuf[JB_PC], pointer_guard) }

def bt_jmpbuf(jmpbuf):
    '''Backtrace a jmpbuf'''
    regs = get_jmpbuf_regs(jmpbuf)
    old = dict()

    # remember current stack frame and select the topmost
    # so that register modifications don't wreck it
    selected_frame = gdb.selected_frame()
    gdb.newest_frame().select()

    for i in regs:
        old[i] = gdb.parse_and_eval('(uint64_t)$%s' % i)

    for i in regs:
        gdb.execute('set $%s = %s' % (i, regs[i]))

    gdb.execute('bt')

    for i in regs:
        gdb.execute('set $%s = %s' % (i, old[i]))

    selected_frame.select()

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-03-18 21:13 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-03 11:22 How to backtrace an separate stack? Stefan Hajnoczi
2022-03-07 10:49 ` Pedro Alves
2022-03-08  9:47   ` Stefan Hajnoczi
2022-03-07 14:49 ` Florian Weimer
2022-03-07 17:30   ` Tom Tromey
2022-03-09 10:06     ` Florian Weimer
2022-03-09 19:50       ` Tom Tromey
2022-03-07 16:58 ` Tom Tromey
2022-03-07 17:18   ` Pedro Alves
2022-03-08  8:43     ` Stefan Hajnoczi
2022-03-14 20:30   ` Tom Tromey
2022-03-15 14:17     ` Stefan Hajnoczi
2022-03-18 21:13       ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).