-
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 all commits
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 |
|---|---|---|
|
|
@@ -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"); | ||
|
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'; | ||
| 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 | ||
| */ | ||
|
|
||
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.
If get_thread_num() fails for some reason, or the name is too long, the header is not printed at all. This is quite wrong. In case of error it should be indicated in some simple way (like we do when a file is deleted). If the name is too long, we can truncate it.