Skip to content

Commit 87b8238

Browse files
committed
Print thread name when dumping backtrace
Keeping proc interactions isoltated: added function in proc.h/.c that prints thread name to stdout. It is then used by backtrace.c directly to keep from having to store the string in memory in snapshot or any other struct
1 parent b4fcac4 commit 87b8238

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

src/backtrace.c

100644100755
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ int backtrace_snapshot(int pid, int *tids, int *index, int nr_tids)
108108
return -1;
109109

110110
for (i = 0; i < snap->num_threads; ++i) {
111-
printf("-------------------- thread %d (%d) --------------------\n",
111+
printf("-------------------- thread %d (%d) (",
112112
(index != NULL ? index[i] : i+1), snap->tids[i]);
113+
print_proc_comm(snap->tids[i]);
114+
printf(") --------------------\n");
113115

114116
snap->cur_thr = i;
115117
if (backtrace_thread(&snapshot_addr_space_accessors, snap) < 0)
@@ -145,8 +147,10 @@ int backtrace_ptrace(int pid, int *tids, int *index, int nr_tids)
145147
for (i = 0; i < count; ++i) {
146148
void *upt_info;
147149

148-
printf("-------------------- thread %d (%d) --------------------\n",
150+
printf("-------------------- thread %d (%d) (",
149151
(index != NULL ? index[i] : i+1), threads[i]);
152+
print_proc_comm(threads[i]);
153+
printf(") --------------------\n");
150154

151155
if (threads[i] != pid && attach_thread(threads[i]) < 0) {
152156
rc = -1;

src/proc.c

100644100755
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,30 @@ int print_proc_maps(int pid)
191191
return system(cmd);
192192
}
193193

194+
int print_proc_comm(int pid)
195+
{
196+
FILE *f;
197+
char buf[32] = "";
198+
int i;
199+
int rc = -1;
200+
201+
snprintf(buf, sizeof(buf), "/proc/%d/comm", pid);
202+
if ((f = fopen(buf, "r")) == NULL) {
203+
fprintf(stderr, "cannot open %s: %s\n", buf, strerror(errno));
204+
return -1;
205+
}
206+
207+
if (fgets(buf, sizeof(buf), f)) {
208+
i = strcspn(buf, "\n");
209+
buf[i] = '\0';
210+
printf("%s", buf);
211+
rc = 0;
212+
}
213+
214+
fclose(f);
215+
return rc;
216+
}
217+
194218
/*
195219
* filter for scandir(). choose only thread identifiers
196220
*/

src/proc.h

100644100755
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ struct mem_map *create_maps(int pid);
2727
*/
2828
int print_proc_maps(int pid);
2929

30+
/*
31+
* simple routine to print process comm for
32+
* debugging or advanced error reporting
33+
*/
34+
int print_proc_comm(int pid);
35+
3036
/*
3137
* get thread identifiers of the process
3238
*/

0 commit comments

Comments
 (0)