Skip to content

Commit 90283ec

Browse files
committed
Cleaner string handling and printing of process comm
1 parent 87b8238 commit 90283ec

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/backtrace.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ int backtrace_snapshot(int pid, int *tids, int *index, int nr_tids)
103103
{
104104
int i, rc = 0;
105105
struct snapshot *snap;
106-
106+
107107
if ((snap = get_snapshot(pid, tids, index, nr_tids)) == NULL)
108108
return -1;
109109

110110
for (i = 0; i < snap->num_threads; ++i) {
111-
printf("-------------------- thread %d (%d) (",
112-
(index != NULL ? index[i] : i+1), snap->tids[i]);
113-
print_proc_comm(snap->tids[i]);
114-
printf(") --------------------\n");
111+
char comm[16];
112+
113+
get_thread_comm(snap->tids[i], comm, sizeof(comm));
114+
printf("-------------------- thread %d (%d) (%s) --------------------\n", (index != NULL ? index[i] : i+1), snap->tids[i], comm);
115115

116116
snap->cur_thr = i;
117117
if (backtrace_thread(&snapshot_addr_space_accessors, snap) < 0)
@@ -146,11 +146,11 @@ int backtrace_ptrace(int pid, int *tids, int *index, int nr_tids)
146146

147147
for (i = 0; i < count; ++i) {
148148
void *upt_info;
149+
char comm[16];
150+
151+
get_thread_comm(threads[i], comm, sizeof(comm));
149152

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

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

src/proc.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,18 @@ int print_proc_maps(int pid)
191191
return system(cmd);
192192
}
193193

194-
int print_proc_comm(int pid)
194+
int get_thread_comm(int pid, char * dst, size_t n)
195195
{
196196
FILE *f;
197-
char buf[32] = "";
198-
int i;
197+
char buf[32];
198+
int i, x;
199199
int rc = -1;
200200

201+
if (dst == NULL)
202+
{
203+
return -1;
204+
}
205+
201206
snprintf(buf, sizeof(buf), "/proc/%d/comm", pid);
202207
if ((f = fopen(buf, "r")) == NULL) {
203208
fprintf(stderr, "cannot open %s: %s\n", buf, strerror(errno));
@@ -207,8 +212,10 @@ int print_proc_comm(int pid)
207212
if (fgets(buf, sizeof(buf), f)) {
208213
i = strcspn(buf, "\n");
209214
buf[i] = '\0';
210-
printf("%s", buf);
211-
rc = 0;
215+
x = max(min(n-1, i+1), 0);
216+
strncpy(dst, buf, x);
217+
dst[x] = '\0';
218+
rc = x;
212219
}
213220

214221
fclose(f);

src/proc.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ struct mem_map *create_maps(int pid);
2828
int print_proc_maps(int pid);
2929

3030
/*
31-
* simple routine to print process comm for
32-
* debugging or advanced error reporting
31+
* copy pid process comm into dst string, up to n characters
3332
*/
34-
int print_proc_comm(int pid);
33+
int get_thread_comm(int pid, char * dst, size_t n);
3534

3635
/*
3736
* get thread identifiers of the process

0 commit comments

Comments
 (0)