-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
571 lines (471 loc) · 12.8 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#include <sys/ptrace.h>
#include <asm/ptrace.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <limits.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <bfd.h>
#include <libgen.h>
#include "etpan-symbols.h"
#include "chash.h"
#include "carray.h"
static int attach(pid_t pid)
{
int status;
long r;
r = ptrace(PTRACE_ATTACH, pid, 0, 0);
if (r < 0) {
fprintf(stderr, "could not attach\n");
return r;
}
r = waitpid(pid, &status, WUNTRACED);
if (r < 0) {
fprintf(stderr, "could not be stopped\n");
ptrace(PTRACE_DETACH, pid, 0, 0);
return r;
}
return 0;
}
static int attach_thread(pid_t pid)
{
int status;
long r;
r = ptrace(PTRACE_ATTACH, pid, 0, 0);
if (r < 0) {
fprintf(stderr, "could not attach\n");
return r;
}
r = waitpid(pid, &status, __WCLONE);
if (r < 0) {
fprintf(stderr, "could not be stopped\n");
ptrace(PTRACE_DETACH, pid, 0, 0);
return r;
}
return 0;
}
static void detach(pid_t pid)
{
ptrace(PTRACE_DETACH, pid, 0, 0);
}
static int get_thread_list(pid_t pid,
pid_t ** p_thread_list, unsigned int * p_thread_count)
{
char dirname[PATH_MAX];
DIR * dir;
struct dirent * ent;
unsigned int count;
pid_t * thread_list;
unsigned int thread_count;
snprintf(dirname, sizeof(dirname), "/proc/%i/task", pid);
dir = opendir(dirname);
if (dir == NULL)
return -1;
count = 0;
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] != '.')
count ++;
}
thread_count = count;
thread_list = malloc(thread_count * sizeof(* thread_list));
rewinddir(dir);
count = 0;
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] != '.') {
thread_list[count] = strtoul(ent->d_name, NULL, 10);
count ++;
}
}
closedir(dir);
* p_thread_list = thread_list;
* p_thread_count = thread_count;
return 0;
}
#define MAX_FRAME 512
static int get_stack(pid_t pid,
unsigned long ** p_stackframe, unsigned int * p_stackframe_count)
{
unsigned long pc;
unsigned long fp;
unsigned long stackframe[MAX_FRAME];
unsigned int stackframe_count;
unsigned long * result;
#ifdef __x86_64
pc = ptrace(PTRACE_PEEKUSER, pid, RIP, 0);
#else
pc = ptrace(PTRACE_PEEKUSER, pid, EIP * 4, 0);
#endif
if (errno != 0) {
fprintf(stderr, "error peek eip\n");
return -1;
}
#ifdef __x86_64
fp = ptrace(PTRACE_PEEKUSER, pid, RBP, 0);
#else
fp = ptrace(PTRACE_PEEKUSER, pid, EBP * 4, 0);
#endif
if (errno != 0) {
fprintf(stderr, "error peek ebp\n");
return -1;
}
stackframe_count = 0;
while (1) {
unsigned long nextfp;
stackframe[stackframe_count] = pc;
stackframe_count ++;
nextfp = ptrace(PTRACE_PEEKDATA, pid, fp, 0);
if (errno != 0)
break;
#ifdef __x86_64
pc = ptrace(PTRACE_PEEKDATA, pid, fp + 8, 0);
#else
pc = ptrace(PTRACE_PEEKDATA, pid, fp + 4, 0);
#endif
fp = nextfp;
if (fp == 0)
break;
}
result = malloc(stackframe_count * sizeof(* result));
memcpy(result, stackframe, stackframe_count * sizeof(* result));
* p_stackframe = result;
* p_stackframe_count = stackframe_count;
return 0;
}
struct stackframe_elt {
unsigned long * stackframe;
unsigned int stackframe_count;
unsigned int sample_count;
};
static void sample(pid_t pid, chash * thread_hash)
{
pid_t * tab;
unsigned int count;
unsigned int i;
int r;
r = attach(pid);
if (r < 0)
exit(EXIT_FAILURE);
r = get_thread_list(pid, &tab, &count);
if (r < 0)
exit(EXIT_FAILURE);
for(i = 0 ; i < count ; i ++) {
if (tab[i] != pid)
attach_thread(tab[i]);
}
for(i = 0 ; i < count ; i ++) {
unsigned long * stackframe;
unsigned int stackframe_count;
chashdatum key;
chashdatum value;
struct stackframe_elt * elt;
chash * stack_hash;
unsigned int k;
r = get_stack(tab[i], &stackframe, &stackframe_count);
if (r < 0)
exit(EXIT_FAILURE);
key.data = &tab[i];
key.len = sizeof(tab[i]);
r = chash_get(thread_hash, &key, &value);
if (r < 0) {
stack_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
value.data = stack_hash;
value.len = 0;
chash_set(thread_hash, &key, &value, NULL);
}
else {
stack_hash = value.data;
}
for(k = 1 ; k <= stackframe_count ; k ++) {
key.data = stackframe + (stackframe_count - k);
key.len = k * sizeof(* stackframe);
r = chash_get(stack_hash, &key, &value);
if (r < 0) {
elt = malloc(sizeof(* elt));
elt->stackframe = malloc(sizeof(* elt->stackframe) * k);
memcpy(elt->stackframe, stackframe + (stackframe_count - k),
k * sizeof(* stackframe));
elt->stackframe_count = k;
elt->sample_count = 1;
value.data = elt;
value.len = 0;
chash_set(stack_hash, &key, &value, NULL);
}
else {
elt = value.data;
elt->sample_count ++;
}
}
}
for(i = 0 ; i < count ; i ++) {
if (tab[i] != pid)
detach(tab[i]);
}
detach(pid);
free(tab);
}
static int compare_stack(const void * a, const void * b)
{
struct stackframe_elt * const * p_elt_a;
struct stackframe_elt * const * p_elt_b;
struct stackframe_elt * elt_a;
struct stackframe_elt * elt_b;
unsigned int min_stack;
unsigned int frame_index;
unsigned int offset_a;
unsigned int offset_b;
p_elt_a = a;
p_elt_b = b;
elt_a = * p_elt_a;
elt_b = * p_elt_b;
min_stack = elt_a->stackframe_count;
if (elt_b->stackframe_count < min_stack)
min_stack = elt_b->stackframe_count;
offset_a = elt_a->stackframe_count - min_stack;
offset_b = elt_b->stackframe_count - min_stack;
frame_index = min_stack - 1;
while (1) {
if (elt_a->stackframe[offset_a + frame_index] ==
elt_b->stackframe[offset_b + frame_index]) {
/* do nothing */
}
else if (elt_a->stackframe[offset_a + frame_index] <
elt_b->stackframe[offset_b + frame_index]) {
return -1;
}
else {
return 1;
}
if (frame_index == 0)
break;
frame_index --;
}
if (elt_a->stackframe_count < elt_b->stackframe_count)
return -1;
else if (elt_a->stackframe_count > elt_b->stackframe_count)
return 1;
return 0;
}
static unsigned int common_count(struct stackframe_elt * elt_a,
struct stackframe_elt * elt_b)
{
unsigned int min_stack;
unsigned int frame_index;
unsigned int offset_a;
unsigned int offset_b;
min_stack = elt_a->stackframe_count;
if (elt_b->stackframe_count < min_stack)
min_stack = elt_b->stackframe_count;
offset_a = elt_a->stackframe_count - min_stack;
offset_b = elt_b->stackframe_count - min_stack;
frame_index = min_stack - 1;
while (1) {
if (elt_a->stackframe[offset_a + frame_index] ==
elt_b->stackframe[offset_b + frame_index]) {
/* do nothing */
}
else {
return min_stack - 1 - frame_index;
}
if (frame_index == 0)
break;
frame_index --;
}
return min_stack;
}
static void print_stackframe(struct stackframe_elt * elt)
{
unsigned int frame_index;
printf("%u: ", elt->sample_count);
frame_index = elt->stackframe_count - 1;
while (1) {
printf("%p ", (void *) elt->stackframe[frame_index]);
if (frame_index == 0)
break;
frame_index --;
}
printf("\n");
}
struct stackframe_node {
struct stackframe_elt * elt;
carray * children;
};
static int compare_sample(const void * a, const void * b)
{
struct stackframe_node * const * p_elt_a;
struct stackframe_node * const * p_elt_b;
struct stackframe_node * elt_a;
struct stackframe_node * elt_b;
p_elt_a = a;
p_elt_b = b;
elt_a = * p_elt_a;
elt_b = * p_elt_b;
return elt_b->elt->sample_count - elt_a->elt->sample_count;
}
static void sort_tree(struct stackframe_node * node)
{
void ** data;
unsigned int i;
data = carray_data(node->children);
qsort(data, carray_count(node->children), sizeof(* data), compare_sample);
for(i = 0 ; i < carray_count(node->children) ; i ++) {
struct stackframe_node * child;
child = carray_get(node->children, i);
sort_tree(child);
}
}
static const char * my_basename(const char * basename)
{
const char * result;
const char * p;
result = basename;
p = result;
while ((p = strchr(result, '/')) != NULL) {
result = p + 1;
}
return result;
}
static void print_tree(struct etpan_symbol_table * symtable,
struct stackframe_node * node, unsigned int level)
{
unsigned int i;
int r;
if (level > 0) {
struct etpan_debug_symbol symbol;
for(i = 0 ; i < level ; i ++)
printf(" ");
r = etpan_get_symbol(symtable,
(void *) node->elt->stackframe[0], &symbol);
if (r) {
const char *name;
char address_str[32];
name = symbol.functionname;
if (name == NULL || *name == '\0') {
snprintf(address_str, sizeof(address_str), "%p",
(void *) node->elt->stackframe[0]);
name = address_str;
}
if (symbol.filename != NULL) {
printf("%u %s (in %s) %s:%u\n", node->elt->sample_count,
name, my_basename(symbol.libname),
my_basename(symbol.filename), symbol.line);
}
else {
printf("%u %s (in %s)\n", node->elt->sample_count,
name, my_basename(symbol.libname));
}
}
else {
printf("%u %p\n", node->elt->sample_count,
(void *) node->elt->stackframe[0]);
}
}
for(i = 0 ; i < carray_count(node->children) ; i ++) {
struct stackframe_node * child;
child = carray_get(node->children, i);
print_tree(symtable, child, level + 1);
}
}
static void show_tree(struct etpan_symbol_table * symtable,
struct stackframe_elt ** stack_table, unsigned int count)
{
unsigned int i;
struct stackframe_elt * elt;
unsigned int previous_count;
struct stackframe_elt * previous;
unsigned int indent;
carray * current_node;
struct stackframe_node * root;
qsort(stack_table, count, sizeof(* stack_table), compare_stack);
current_node = carray_new(16);
root = malloc(sizeof(* root));
root->elt = NULL;
root->children = carray_new(4);
carray_add(current_node, root, NULL);
indent = 0;
previous = NULL;
previous_count = 0;
for(i = 0 ; i < count ; i ++) {
unsigned int common;
struct stackframe_node * node;
struct stackframe_node * parent;
elt = stack_table[i];
common = 0;
if (previous_count > 0) {
unsigned int remove_count;
common = common_count(previous, elt);
remove_count = (previous->stackframe_count - common);
indent -= remove_count;
carray_set_size(current_node, carray_count(current_node) - remove_count);
}
parent = carray_get(current_node, carray_count(current_node) - 1);
node = malloc(sizeof(* root));
node->elt = elt;
node->children = carray_new(4);
carray_add(parent->children, node, NULL);
carray_add(current_node, node, NULL);
indent ++;
previous = elt;
previous_count = elt->stackframe_count;
}
sort_tree(root);
print_tree(symtable, root, 0);
}
int main(int argc, char ** argv)
{
pid_t pid;
unsigned int k;
chash * thread_hash;
unsigned int sample_count;
unsigned int sample_delay;
chashiter * iter;
struct etpan_symbol_table * symtable;
if (argc < 3) {
fprintf(stderr, "syntax: sample <pid> <delay>\n");
exit(EXIT_FAILURE);
}
pid = strtoul(argv[1], NULL, 10);
thread_hash = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY);
sample_delay = 10 * 1000;
sample_count = strtoul(argv[2], NULL, 10) * (1000000 / sample_delay);
printf("sampling %u %u\n", sample_delay, sample_count);
for(k = 0 ; k < sample_count ; k ++) {
sample(pid, thread_hash);
usleep(sample_delay);
}
symtable = etpan_get_symtable(pid);
for(iter = chash_begin(thread_hash) ; iter != NULL ;
iter = chash_next(thread_hash, iter)) {
chashdatum key;
chashdatum value;
chashiter * stack_iter;
chash * stack_hash;
pid_t pid;
struct stackframe_elt ** stack_table;
unsigned int count;
chash_key(iter, &key);
chash_value(iter, &value);
memcpy(&pid, key.data, sizeof(pid));
printf("thread %u:\n", pid);
stack_hash = value.data;
count = chash_count(stack_hash);
stack_table = malloc(count * sizeof(* stack_table));
count = 0;
for(stack_iter = chash_begin(stack_hash) ; stack_iter != NULL ;
stack_iter = chash_next(stack_hash, stack_iter)) {
struct stackframe_elt * elt;
chash_value(stack_iter, &value);
elt = value.data;
stack_table[count] = elt;
count ++;
}
show_tree(symtable, stack_table, count);
free(stack_table);
}
etpan_symbol_table_free(symtable);
chash_free(thread_hash);
exit(EXIT_SUCCESS);
}