-
Notifications
You must be signed in to change notification settings - Fork 5
/
glider.c
248 lines (200 loc) · 5.66 KB
/
glider.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
/*
* Copyright (c) 2007-2012 Message Systems, Inc. All rights reserved
* For licensing information, see:
* https://bitbucket.org/wez/gimli/src/tip/LICENSE
*/
#include "impl.h"
struct glider_args {
int nthread;
int nframe;
void **pcaddrs;
gimli_stack_frame_t *frames;
gimli_proc_t proc;
gimli_thread_t thread;
gimli_stack_trace_t trace;
int suppress;
};
static gimli_iter_status_t collect_frame(
gimli_proc_t proc,
gimli_thread_t thread,
gimli_stack_frame_t frame,
void *arg)
{
struct glider_args *args = arg;
int no = gimli_stack_frame_number(frame);
args->pcaddrs[no] = (void*)(intptr_t)gimli_stack_frame_pcaddr(frame);
args->frames[no] = frame;
return GIMLI_ITER_CONT;
}
static gimli_iter_status_t should_suppress_thread(
struct module_item *mod, void *arg)
{
struct glider_args *args = arg;
if (mod->api_version == 2 && mod->ptr.v2->on_begin_thread_trace) {
if (mod->ptr.v2->on_begin_thread_trace(&ana_api,
mod->exename, args->nthread,
gimli_stack_trace_num_frames(args->trace),
args->pcaddrs, (void**)args->frames) == GIMLI_ANA_SUPPRESS) {
args->suppress = 1;
return GIMLI_ITER_STOP;
}
}
return GIMLI_ITER_CONT;
}
static gimli_iter_status_t should_suppress_frame(
struct module_item *mod, void *arg)
{
struct glider_args *args = arg;
if (mod->api_version == 2 && mod->ptr.v2->before_print_frame) {
if (mod->ptr.v2->before_print_frame(&ana_api,
mod->exename, args->nthread, args->nframe,
args->pcaddrs[args->nframe], args->frames[args->nframe])
== GIMLI_ANA_SUPPRESS) {
args->suppress = 1;
return GIMLI_ITER_STOP;
}
}
return GIMLI_ITER_CONT;
}
static gimli_iter_status_t after_print_frame(
struct module_item *mod, void *arg)
{
struct glider_args *args = arg;
if (mod->api_version == 2 && mod->ptr.v2->after_print_frame) {
mod->ptr.v2->after_print_frame(&ana_api,
mod->exename, args->nthread, args->nframe,
args->pcaddrs[args->nframe], args->frames[args->nframe]);
}
return GIMLI_ITER_CONT;
}
static gimli_iter_status_t after_print_thread(
struct module_item *mod, void *arg)
{
struct glider_args *args = arg;
if (mod->api_version == 2 && mod->ptr.v2->on_end_thread_trace) {
mod->ptr.v2->on_end_thread_trace(&ana_api,
mod->exename, args->nthread,
gimli_stack_trace_num_frames(args->trace),
args->pcaddrs, (void**)args->frames);
}
return GIMLI_ITER_CONT;
}
static void render_thread(gimli_proc_t proc,
gimli_thread_t thread,
struct glider_args *args)
{
int num_frames = gimli_stack_trace_num_frames(args->trace);
args->suppress = 0;
gimli_visit_modules(should_suppress_thread, args);
if (args->suppress) return;
printf("Thread %d (LWP %d)\n", args->nthread, thread->lwpid);
for (args->nframe = 0; args->nframe < num_frames; args->nframe++) {
args->suppress = 0;
gimli_visit_modules(should_suppress_frame, args);
if (args->suppress) continue;
gimli_render_frame(args->nthread, args->nframe, args->frames[args->nframe]);
gimli_visit_modules(after_print_frame, args);
gimli_visit_modules(after_print_thread, args);
}
printf("\n");
}
static gimli_iter_status_t trace_thread(
gimli_proc_t proc,
gimli_thread_t thread,
void *arg)
{
struct glider_args *args = arg;
args->trace = gimli_thread_stack_trace(thread, max_frames);
if (args->trace) {
struct glider_args *args = arg;
args->thread = thread;
gimli_stack_trace_visit(args->trace, collect_frame, args);
render_thread(proc, thread, args);
args->nthread++;
gimli_stack_trace_delete(args->trace);
args->trace = NULL;
}
return GIMLI_ITER_CONT;
}
static gimli_iter_status_t print_siginfo(gimli_proc_t proc,
gimli_stack_frame_t frame,
const char *varname, gimli_type_t t, gimli_addr_t addr,
int depth, void *arg)
{
siginfo_t si;
char buf[1024];
int i;
if (gimli_read_mem(proc, addr, &si, sizeof(si)) != sizeof(si)) {
return GIMLI_ITER_CONT;
}
gimli_render_siginfo(proc, &si, buf, sizeof(buf));
for (i = 0; i < (depth + 1) * 4; i++) putchar(' ');
printf("%s\n", buf);
return GIMLI_ITER_STOP;
}
static const char *siginfo_names[] = {
"siginfo_t",
"struct siginfo",
};
static void trace_process(int pid)
{
int i;
struct glider_args args;
if (!tracer_attach(pid)) {
return;
}
gimli_module_register_var_printer_for_types(siginfo_names,
sizeof(siginfo_names)/sizeof(siginfo_names[0]),
print_siginfo, NULL);
args.proc = the_proc;
args.frames = calloc(max_frames, sizeof(*args.frames));
args.nthread = 0;
if (!args.frames) {
fprintf(stderr, "Not enough memory to trace %d frames\n", max_frames);
return;
}
args.pcaddrs = calloc(max_frames, sizeof(*args.pcaddrs));
if (!args.pcaddrs) {
fprintf(stderr, "Not enough memory to trace %d frames\n", max_frames);
return;
}
gimli_load_modules(the_proc);
gimli_show_memory_map(the_proc);
gimli_proc_visit_threads(the_proc, trace_thread, &args);
printf("\n");
gimli_module_call_tracers(the_proc);
free(args.frames);
free(args.pcaddrs);
}
int main(int argc, char *argv[])
{
int pid;
int c;
while (1) {
c = getopt(argc, argv, "d");
if (c == -1) {
break;
}
switch (c) {
/* -d option enables copious dwarf debugging */
case 'd':
debug = 1;
break;
default:
fprintf(stderr, "invalid option %c\n", c);
return 1;
}
}
if (getenv("GIMLI_DWARF_DEBUG")) {
debug = 1;
}
if (optind < argc) {
pid = atoi(argv[optind]);
trace_process(pid);
return 0;
}
fprintf(stderr, "usage: %s <pid>\n", argv[0]);
return 1;
}
/* vim:ts=2:sw=2:et:
*/