-
Notifications
You must be signed in to change notification settings - Fork 17
Print thread name when dumping backtrace #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,8 +108,10 @@ int backtrace_snapshot(int pid, int *tids, int *index, int nr_tids) | |
| return -1; | ||
|
|
||
| for (i = 0; i < snap->num_threads; ++i) { | ||
| printf("-------------------- thread %d (%d) --------------------\n", | ||
| printf("-------------------- thread %d (%d) (", | ||
| (index != NULL ? index[i] : i+1), snap->tids[i]); | ||
| print_proc_comm(snap->tids[i]); | ||
| printf(") --------------------\n"); | ||
|
|
||
| snap->cur_thr = i; | ||
| 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) | |
| for (i = 0; i < count; ++i) { | ||
| void *upt_info; | ||
|
|
||
| printf("-------------------- thread %d (%d) --------------------\n", | ||
| printf("-------------------- thread %d (%d) (", | ||
| (index != NULL ? index[i] : i+1), threads[i]); | ||
| print_proc_comm(threads[i]); | ||
|
||
| printf(") --------------------\n"); | ||
|
|
||
| if (threads[i] != pid && attach_thread(threads[i]) < 0) { | ||
| rc = -1; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -191,6 +191,30 @@ int print_proc_maps(int pid) | |
| return system(cmd); | ||
| } | ||
|
|
||
| int print_proc_comm(int pid) | ||
| { | ||
| FILE *f; | ||
| char buf[32] = ""; | ||
|
||
| int i; | ||
| int rc = -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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to put terminator by hand.
|
||
| buf[i] = '\0'; | ||
| printf("%s", buf); | ||
| rc = 0; | ||
| } | ||
|
|
||
| fclose(f); | ||
| return rc; | ||
| } | ||
|
|
||
| /* | ||
| * filter for scandir(). choose only thread identifiers | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,12 @@ struct mem_map *create_maps(int pid); | |
| */ | ||
| int print_proc_maps(int pid); | ||
|
|
||
| /* | ||
| * simple routine to print process comm for | ||
| * debugging or advanced error reporting | ||
| */ | ||
| int print_proc_comm(int pid); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we will decide to polish output, probably this should return number of bytes written or pointer to static data. Latter makes it convenient to pass value to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By the way, I think it's better to name it |
||
|
|
||
| /* | ||
| * get thread identifiers of the process | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's consider implementing better formatting. It was simple solution from the beginning but I think when changing this code it is the best time to polish output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I was not happy with how this looked, but I wanted to keep it a function call into proc and not have to store it in snapshot.
Do you have any suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, no need to store it because we use it single time at this point.
We can have 80+1 character buffer filled up with '-', then use
snprintf()to print thread number, pid, and name. We could makeprint_proc_comm()returning pointer to static data so only singlesnprintf()will be needed. Not sure it is best/simplest solution, it's first coming to my mind. Or better we can prepare the title in another buffer, calculate its length and choose a position in "----" to make it center aligned.