-
Notifications
You must be signed in to change notification settings - Fork 53
/
pglobal.c
341 lines (284 loc) · 6.54 KB
/
pglobal.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
/*
* SPDX-License-Identifier: GPL-2.0-only
*
* Copyright (C) 2007 Davi E. M. Arnaut <[email protected]>
*/
#include <argp.h>
#include <malloc.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "dwarves.h"
#include "dutil.h"
static int verbose;
static struct conf_fprintf conf = {
.emit_stats = 1,
};
static struct conf_load conf_load = {
.conf_fprintf = &conf,
};
struct extvar {
struct extvar *next;
const char *name;
const struct variable *var;
const struct cu *cu;
};
struct extfun {
struct extfun *next;
const char *name;
const struct function *fun;
const struct cu *cu;
};
static void *tree;
static void oom(const char *msg)
{
fprintf(stderr, "pglobal: out of memory (%s)\n", msg);
exit(EXIT_FAILURE);
}
static struct extvar *extvar__new(const struct variable *var,
const struct cu *cu)
{
struct extvar *gvar = malloc(sizeof(*gvar));
if (gvar != NULL) {
gvar->next = NULL;
gvar->var = var;
gvar->cu = cu;
gvar->name = variable__name(var);
}
return gvar;
}
static struct extfun *extfun__new(struct function *fun,
const struct cu *cu)
{
struct extfun *gfun = malloc(sizeof(*gfun));
if (gfun != NULL) {
gfun->next = NULL;
gfun->fun = fun;
gfun->cu = cu;
gfun->name = function__name(fun);
}
return gfun;
}
static int extvar__compare(const void *a, const void *b)
{
const struct extvar *ga = a, *gb = b;
return strcmp(ga->name, gb->name);
}
static int extfun__compare(const void *a, const void *b)
{
const struct extfun *ga = a, *gb = b;
return strcmp(ga->name, gb->name);
}
static void extvar__add(const struct variable *var, const struct cu *cu)
{
struct extvar **nodep, *gvar = extvar__new(var, cu);
if (gvar != NULL) {
nodep = tsearch(gvar, &tree, extvar__compare);
if (nodep == NULL)
oom("tsearch");
else if (*nodep != gvar) {
if (gvar->var->declaration) {
gvar->next = (*nodep)->next;
(*nodep)->next = gvar;
} else {
gvar->next = *nodep;
*nodep = gvar;
}
}
}
}
static void extfun__add(struct function *fun, const struct cu *cu)
{
struct extfun **nodep, *gfun = extfun__new(fun, cu);
if (gfun != NULL) {
nodep = tsearch(gfun, &tree, extfun__compare);
if (nodep == NULL)
oom("tsearch");
else if (*nodep != gfun) {
gfun->next = (*nodep)->next;
(*nodep)->next = gfun;
}
}
}
static int cu_extvar_iterator(struct cu *cu, void *cookie __maybe_unused)
{
struct tag *pos;
uint32_t id;
cu__for_each_variable(cu, id, pos) {
struct variable *var = tag__variable(pos);
if (var->external)
extvar__add(var, cu);
}
return 0;
}
static int cu_extfun_iterator(struct cu *cu, void *cookie __maybe_unused)
{
struct function *pos;
uint32_t id;
cu__for_each_function(cu, id, pos)
if (pos->external)
extfun__add(pos, cu);
return 0;
}
static inline const struct extvar *node__variable(const void *nodep)
{
return *((const struct extvar **)nodep);
}
static inline const struct extfun *node__function(const void *nodep)
{
return *((const struct extfun **)nodep);
}
static inline struct tag *extvar__tag(const struct extvar *gvar)
{
return (struct tag *)gvar->var;
}
static inline struct tag *extfun__tag(const struct extfun *gfun)
{
return (struct tag *)gfun->fun;
}
static void declaration_action__walk(const void *nodep, const VISIT which,
const int depth __maybe_unused)
{
uint32_t count = 0;
struct tag *tag;
const struct extvar *pos, *gvar = NULL;
switch(which) {
case preorder:
break;
case postorder:
gvar = node__variable(nodep);
break;
case endorder:
break;
case leaf:
gvar = node__variable(nodep);
break;
}
if (gvar == NULL)
return;
tag = extvar__tag(gvar);
tag__fprintf(tag, gvar->cu, NULL, stdout);
for (pos = gvar->next; pos; pos = pos->next)
count++;
printf("; /* %u */\n\n", count);
}
static void function_action__walk(const void *nodep, const VISIT which,
const int depth __maybe_unused)
{
struct tag *tag;
const struct extfun *gfun = NULL;
switch(which) {
case preorder:
break;
case postorder:
gfun = node__function(nodep);
break;
case endorder:
break;
case leaf:
gfun = node__function(nodep);
break;
}
if (gfun == NULL)
return;
tag = extfun__tag(gfun);
tag__fprintf(tag, gfun->cu, NULL, stdout);
fputs("\n\n", stdout);
}
static void free_node(void *nodep)
{
void **node = nodep;
free(*node);
}
/* Name and version of program. */
ARGP_PROGRAM_VERSION_HOOK_DEF = dwarves_print_version;
static const struct argp_option pglobal__options[] = {
{
.key = 'v',
.name = "variables",
.doc = "show global variables",
},
{
.key = 'f',
.name = "functions",
.doc = "show global functions",
},
{
.name = "format_path",
.key = 'F',
.arg = "FORMAT_LIST",
.doc = "List of debugging formats to try"
},
{
.key = 'V',
.name = "verbose",
.doc = "be verbose",
},
{
.name = NULL,
}
};
static int walk_var, walk_fun;
static error_t pglobal__options_parser(int key, char *arg __maybe_unused,
struct argp_state *state)
{
switch (key) {
case ARGP_KEY_INIT:
if (state->child_inputs != NULL)
state->child_inputs[0] = state->input;
break;
case 'v': walk_var = 1; break;
case 'f': walk_fun = 1; break;
case 'V': verbose = 1; break;
case 'F': conf_load.format_path = arg; break;
default: return ARGP_ERR_UNKNOWN;
}
return 0;
}
static const char pglobal__args_doc[] = "FILE";
static struct argp pglobal__argp = {
.options = pglobal__options,
.parser = pglobal__options_parser,
.args_doc = pglobal__args_doc,
};
int main(int argc, char *argv[])
{
int err, remaining, rc = EXIT_FAILURE;
if (argp_parse(&pglobal__argp, argc, argv, 0, &remaining, NULL) ||
remaining == argc) {
argp_help(&pglobal__argp, stderr, ARGP_HELP_SEE, argv[0]);
goto out;
}
if (dwarves__init()) {
fputs("pglobal: insufficient memory\n", stderr);
goto out;
}
dwarves__resolve_cacheline_size(&conf_load, 0);
struct cus *cus = cus__new();
if (cus == NULL) {
fputs("pglobal: insufficient memory\n", stderr);
goto out_dwarves_exit;
}
err = cus__load_files(cus, &conf_load, argv + remaining);
if (err != 0) {
cus__fprintf_load_files_err(cus, "pglobal", argv + remaining, err, stderr);
goto out_cus_delete;
}
if (walk_var) {
cus__for_each_cu(cus, cu_extvar_iterator, NULL, NULL);
twalk(tree, declaration_action__walk);
} else if (walk_fun) {
cus__for_each_cu(cus, cu_extfun_iterator, NULL, NULL);
twalk(tree, function_action__walk);
}
tdestroy(tree, free_node);
rc = EXIT_SUCCESS;
out_cus_delete:
cus__delete(cus);
out_dwarves_exit:
dwarves__exit();
out:
return rc;
}