On Sat, Mar 11, 2023 at 11:28:49AM +0000, Ralph Corderoy wrote:
Hi Larry,
cmd = aprintf("gdb -batch -ex
backtrace '%s/bk' %u 1>&%d 2>&%d",
bin, getpid(), fileno(f), fileno(f));
system(cmd);
I also came up with this, probably on an SGI Iris Indigo, and got it
added to the Unix Programming FAQ. :-)
6.5 How can I generate a stack dump from within a running program?
http://www.faqs.org/faqs/unix-faq/programmer/faq/
It works surprisingly often, i.e. the process is healthy enough to run
system(3).
On Linux (or some other system using glibc) a limited facility is
built into the C library. So you can just do somthing like this:
{
void *stack_syms[32];
int frames;
frames = backtrace(stack_syms, 32);
backtrace_symbols_fd(stack_syms, frames, 2);
}
This is convenient if you want a stack trace, but the binary might be
on a rescue floppy which doesn't have space for gdb, or the user might
not have gdb installed. I use this for the fsck for ext4, and the
nice thing is that even with a stripped binary.
For example:
Signal (7) SIGBUS (sent from pid 4261) si_code=SI_USER
e2fsck(+0x36691)[0x564da1ed2691]
/lib/x86_64-linux-gnu/libc.so.6(+0x3bf90)[0x7f6e21c0bf90]
/lib/x86_64-linux-gnu/libc.so.6(read+0xd)[0x7f6e21cc80ed]
e2fsck(ask_yn+0x1de)[0x564da1ec90de]
e2fsck(fix_problem+0xfc0)[0x564da1ecc7b0]
e2fsck(+0x235b3)[0x564da1ebf5b3]
e2fsck(+0x252d3)[0x564da1ec12d3]
/lib/x86_64-linux-gnu/libext2fs.so.2(ext2fs_dblist_iterate3+0x5f)[0x7f6e21e430cf]
e2fsck(e2fsck_pass2+0x18b)[0x564da1ebdd7b]
e2fsck(e2fsck_run+0x5a)[0x564da1eb0c3a]
e2fsck(main+0x16cb)[0x564da1eacdbb]
/lib/x86_64-linux-gnu/libc.so.6(+0x2718a)[0x7f6e21bf718a]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x85)[0x7f6e21bf7245]
e2fsck(_start+0x21)[0x564da1eaefc1]
For more information see:
https://github.com/tytso/e2fsprogs/blob/master/e2fsck/sigcatcher.c#L379
- Ted