Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/backtrace.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

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.

}

snap->cur_thr = i;
if (backtrace_thread(&snapshot_addr_space_accessors, snap) < 0)
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 34 additions & 0 deletions src/proc.c
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to put terminator by hand. fgets() does it. From GETS(3):

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

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
*/
Expand Down
5 changes: 5 additions & 0 deletions src/proc.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down