diff --git a/src/backtrace.c b/src/backtrace.c old mode 100644 new mode 100755 index f1761c4..48d0148 --- a/src/backtrace.c +++ b/src/backtrace.c @@ -103,13 +103,21 @@ int backtrace_snapshot(int pid, int *tids, int *index, int nr_tids) { int i, rc = 0; struct snapshot *snap; - + if ((snap = get_snapshot(pid, tids, index, nr_tids)) == NULL) return -1; for (i = 0; i < snap->num_threads; ++i) { - printf("-------------------- thread %d (%d) --------------------\n", - (index != NULL ? index[i] : i+1), snap->tids[i]); + int x; + char comm[16]; + char end_pad[25] = "------------------------"; + + x = get_thread_comm(snap->tids[i], comm, sizeof(comm)); + if (x > 0 && x <= sizeof(end_pad)) + { + end_pad[sizeof(end_pad) - x] = '\0'; + printf("-------------- thread %d (%d) (%s) %s\n", (index != NULL ? index[i] : i+1), snap->tids[i], comm, end_pad); + } snap->cur_thr = i; if (backtrace_thread(&snapshot_addr_space_accessors, snap) < 0) @@ -144,9 +152,17 @@ int backtrace_ptrace(int pid, int *tids, int *index, int nr_tids) for (i = 0; i < count; ++i) { void *upt_info; + int x; + char comm[16]; + char end_pad[25] = "------------------------"; - printf("-------------------- thread %d (%d) --------------------\n", - (index != NULL ? index[i] : i+1), threads[i]); + x = get_thread_comm(threads[i], comm, sizeof(comm)); + + if (x > 0 && x <= sizeof(end_pad)) + { + end_pad[sizeof(end_pad) - x] = '\0'; + printf("-------------- thread %d (%d) (%s) %s\n", (index != NULL ? index[i] : i + 1), threads[i], comm, end_pad); + } if (threads[i] != pid && attach_thread(threads[i]) < 0) { rc = -1; diff --git a/src/proc.c b/src/proc.c old mode 100644 new mode 100755 index 5674df0..266c6df --- a/src/proc.c +++ b/src/proc.c @@ -34,6 +34,9 @@ #define SLEEP_WAIT 500 +#define MAX(a,b) ((a) > (b) ? a : b) +#define MIN(a,b) ((a) < (b) ? a : b) + int attached_pid = 0; /* timeout on waiting for process to stop (us) */ @@ -191,6 +194,37 @@ int print_proc_maps(int pid) return system(cmd); } +int get_thread_comm(int pid, char * dst, size_t n) +{ + FILE *f; + char buf[32]; + int i, x; + int rc = -1; + + if (dst == NULL) + { + return -1; + } + + snprintf(buf, sizeof(buf), "/proc/%d/comm", pid); + if ((f = fopen(buf, "r")) == NULL) { + fprintf(stderr, "cannot open %s: %s\n", buf, strerror(errno)); + return -1; + } + + if (fgets(buf, sizeof(buf), f)) { + i = strcspn(buf, "\n"); + buf[i] = '\0'; + x = MAX(MIN(n, i+1), 1); + strncpy(dst, buf, x); + dst[x-1] = '\0'; + rc = x; + } + + fclose(f); + return rc; +} + /* * filter for scandir(). choose only thread identifiers */ diff --git a/src/proc.h b/src/proc.h old mode 100644 new mode 100755 index 127b71f..11c4ee9 --- a/src/proc.h +++ b/src/proc.h @@ -27,6 +27,11 @@ struct mem_map *create_maps(int pid); */ int print_proc_maps(int pid); +/* + * copy pid process comm into dst string, up to n characters + */ +int get_thread_comm(int pid, char * dst, size_t n); + /* * get thread identifiers of the process */