7 #include <sys/ptrace.h>
10 #include <libunwind-ptrace.h>
12 static void usage(FILE *out)
14 fprintf(out, "usage: pstack [-hna] PID\n");
17 static int dumpstack(pid_t pid, int resolve)
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: %s\n", unw_strerror(s));
35 if(resolve && (!(s = unw_get_proc_name(&uw, pnm, sizeof(pnm), &pcoff)) || (s == -UNW_ENOMEM))) {
36 pnm[sizeof(pnm) - 1] = 0;
38 unw_get_reg(&uw, UNW_REG_IP, ®);
39 printf("%s(%jx)+%jx\n", pnm, (intmax_t)(reg - pcoff), (intmax_t)pcoff);
41 printf("%s+%jx\n", pnm, (intmax_t)pcoff);
44 unw_get_reg(&uw, UNW_REG_IP, ®);
45 printf("%jx\n", (intmax_t)reg);
47 } while((s = unw_step(&uw)) > 0);
49 fprintf(stderr, "pstack: step: %s\n", unw_strerror(s));
53 unw_destroy_addr_space(rps);
57 int main(int argc, char **argv)
63 while((c = getopt(argc, argv, "hna")) != -1) {
79 if(argc - optind < 1) {
83 pid = atoi(argv[optind]);
84 if(ptrace(PTRACE_ATTACH, pid, NULL, NULL)) {
85 fprintf(stderr, "pstack: attach %i: %s\n", pid, strerror(errno));
89 if(waitpid(pid, &s, 0) < 0) {
90 fprintf(stderr, "pstack: wait: %s\n", strerror(errno));
97 if(ptrace(PTRACE_DETACH, pid, NULL, NULL)) {
98 fprintf(stderr, "pstack: detach: %s\n", strerror(errno));
105 * compile-command: "gcc -Wall -g -o pstack pstack.c -lunwind-ptrace -lunwind-x86_64"