diff options
| author | zy <[email protected]> | 2023-11-24 01:22:52 -0500 |
|---|---|---|
| committer | zy <[email protected]> | 2023-11-24 01:22:52 -0500 |
| commit | c326969ad65bd0b0b5aa2f17bfe7feaea14fd554 (patch) | |
| tree | de68ef012261f7284de8a8407ef2aa73501b88b9 /tools | |
| parent | 59993f7e3740f0aaa06483b9eb50d1d25349c0fa (diff) | |
stack-pid move to tools
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/stack-pid.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tools/stack-pid.c b/tools/stack-pid.c new file mode 100644 index 0000000..8a03d4d --- /dev/null +++ b/tools/stack-pid.c @@ -0,0 +1,54 @@ +#include <sys/ptrace.h> +#include <stdio.h> +#include <stdlib.h> + +#include <stdarg.h> + +#include <libunwind-ptrace.h> + +void die(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + exit(1); +} + + +int main(int argc, char **argv) +{ + if (argc != 2) + die("USAGE: unwind-pid <pid>\n"); + + unw_addr_space_t as = unw_create_addr_space(&_UPT_accessors, 0); + + pid_t pid = atoi(argv[1]); + if (ptrace(PTRACE_ATTACH, pid, 0, 0) != 0) + die("ERROR: cannot attach to %d\n", pid); + + void *context = _UPT_create(pid); + unw_cursor_t cursor; + if (unw_init_remote(&cursor, as, context) != 0) + die("ERROR: cannot initialize cursor for remote unwinding\n"); + + do { + unw_word_t offset, pc; + char sym[4096]; + if (unw_get_reg(&cursor, UNW_REG_IP, &pc)) + die("ERROR: cannot read program counter\n"); + + printf("0x%lx: ", pc); + + if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) + printf("(%s+0x%lx)\n", sym, offset); + else + printf("-- no symbol name found\n"); + } while (unw_step(&cursor) > 0); + + _UPT_destroy(context); + (void) ptrace(PTRACE_DETACH, pid, 0, 0); + + return 0; +} |
