7 #include <sys/ptrace.h>
10 #include <libunwind-ptrace.h>
12 static void usage(FILE *out)
14 fprintf(out, "usage: pstack [-h] PID\n");
17 static int dumpstack(pid_t pid)
22 unw_word_t reg, pcoff;
26 rps = unw_create_addr_space(&_UPT_accessors, 0);
27 ptd = _UPT_create(pid);
29 if((s = unw_init_remote(&uw, rps, ptd)) != 0) {
30 fprintf(stderr, "pstack: init_remote: %i\n", s);
35 if(!(s = unw_get_proc_name(&uw, pnm, sizeof(pnm), &pcoff)) || (s == -UNW_ENOMEM)) {
36 pnm[sizeof(pnm) - 1] = 0;
37 printf("%s+%jx\n", pnm, (intmax_t)pcoff);
39 unw_get_reg(&uw, UNW_REG_IP, ®);
40 printf("%jx\n", (intmax_t)reg);
42 } while((s = unw_step(&uw)) > 0);
44 fprintf(stderr, "pstack: step: %i\n", s);
48 unw_destroy_addr_space(rps);
52 int main(int argc, char **argv)
57 while((c = getopt(argc, argv, "h")) != -1) {
67 if(argc - optind < 1) {
71 pid = atoi(argv[optind]);
72 if(ptrace(PTRACE_ATTACH, pid, NULL, NULL)) {
73 fprintf(stderr, "pstack: attach %i: %s\n", pid, strerror(errno));
77 if(waitpid(pid, &s, 0) < 0) {
78 fprintf(stderr, "pstack: wait: %s\n", strerror(errno));
85 if(ptrace(PTRACE_DETACH, pid, NULL, NULL)) {
86 fprintf(stderr, "pstack: detach: %s\n", strerror(errno));
93 * compile-command: "gcc -Wall -g -o pstack pstack.c -lunwind-ptrace -lunwind-x86_64"