pstack: Add some name resolution options. master
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 10 Mar 2025 16:20:16 +0000 (17:20 +0100)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 10 Mar 2025 16:20:16 +0000 (17:20 +0100)
pstack.c

index 2c5be1f..9a04a66 100644 (file)
--- a/pstack.c
+++ b/pstack.c
 
 static void usage(FILE *out)
 {
-    fprintf(out, "usage: pstack [-h] PID\n");
+    fprintf(out, "usage: pstack [-hna] PID\n");
 }
 
-static int dumpstack(pid_t pid)
+static int dumpstack(pid_t pid, int resolve)
 {
     int rv, s;
     unw_addr_space_t rps;
@@ -27,21 +27,26 @@ static int dumpstack(pid_t pid)
     ptd = _UPT_create(pid);
     rv = 0;
     if((s = unw_init_remote(&uw, rps, ptd)) != 0) {
-       fprintf(stderr, "pstack: init_remote: %i\n", s);
+       fprintf(stderr, "pstack: init_remote: %s\n", unw_strerror(s));
        rv = 1;
        goto out;
     }
     do {
-       if(!(s = unw_get_proc_name(&uw, pnm, sizeof(pnm), &pcoff)) || (s == -UNW_ENOMEM)) {
+       if(resolve && (!(s = unw_get_proc_name(&uw, pnm, sizeof(pnm), &pcoff)) || (s == -UNW_ENOMEM))) {
            pnm[sizeof(pnm) - 1] = 0;
-           printf("%s+%jx\n", pnm, (intmax_t)pcoff);
+           if(resolve == 2) {
+               unw_get_reg(&uw, UNW_REG_IP, &reg);
+               printf("%s(%jx)+%jx\n", pnm, (intmax_t)(reg - pcoff), (intmax_t)pcoff);
+           } else {
+               printf("%s+%jx\n", pnm, (intmax_t)pcoff);
+           }
        } else {
            unw_get_reg(&uw, UNW_REG_IP, &reg);
            printf("%jx\n", (intmax_t)reg);
        }
     } while((s = unw_step(&uw)) > 0);
     if(s < 0) {
-       fprintf(stderr, "pstack: step: %i\n", s);
+       fprintf(stderr, "pstack: step: %s\n", unw_strerror(s));
     }
 out:
     _UPT_destroy(ptd);
@@ -51,14 +56,21 @@ out:
 
 int main(int argc, char **argv)
 {
-    int c, s;
+    int c, s, res;
     pid_t pid;
     
-    while((c = getopt(argc, argv, "h")) != -1) {
+    res = 1;
+    while((c = getopt(argc, argv, "hna")) != -1) {
        switch(c) {
        case 'h':
            usage(stdout);
            exit(0);
+       case 'n':
+           res = 0;
+           break;
+       case 'a':
+           res = 2;
+           break;
        default:
            usage(stderr);
            exit(1);
@@ -81,7 +93,7 @@ int main(int argc, char **argv)
        if(WIFSTOPPED(s))
            break;
     }
-    dumpstack(pid);
+    dumpstack(pid, res);
     if(ptrace(PTRACE_DETACH, pid, NULL, NULL)) {
        fprintf(stderr, "pstack: detach: %s\n", strerror(errno));
     }