-
Notifications
You must be signed in to change notification settings - Fork 2
/
dwarfprofile.cxx
645 lines (559 loc) · 15.4 KB
/
dwarfprofile.cxx
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
* dwarfprofile.c - produce a tree of size information from a set of
* dwarf data for a binary.
*
* Copyright (C) 2013, Mark J. Wielaard <mark@klomp.org>
*
* This file is free software. You can redistribute it and/or modify
* it under the terms of the GNU General Public License (GPL); either
* version 3, or (at your option) any later version.
*/
#include <argp.h>
#include <error.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <logging.hxx>
// Older versions of elfutils/libdw dwarf.h don't define this one.
#ifndef DW_TAG_GNU_call_site
#define DW_TAG_GNU_call_site 0x4109
#endif
// Are we generating a Flat Calltree Profile Format
static bool generate_fcpf = false;
// Are we generating a Calltree Profile Format
static bool generate_cpf = false;
// Are we generating a XML
static bool generate_xml = false;
// Ignore DIEs without a name (e.g. lexical blocks)
static bool ignore_no_name = false;
// Size used for single address DIEs
static int single_address_size = 1;
// For debugging in flat output show DIE offsets.
static bool show_die_offset = false;
// File strings cache for the current CU.
static Dwarf_Files *files;
static struct argp argp;
static char *
escape_name(const char *fname)
{
int i;
char *escaped;
if (!fname)
return NULL;
// We have a pseudo-main that contains all the data
if (!strcmp(fname, "main"))
return strdup ("__main__");
escaped = (char *)malloc(strlen(fname) + 1);
for (i = 0; fname[i]; i++)
{
if (fname[i] != '<' && fname[i] != '>' && fname[i] != '&')
escaped[i] = fname[i];
else
escaped[i] = '_';
}
escaped[i] = '\0';
return escaped;
}
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
switch (key)
{
case ARGP_KEY_INIT:
/* dwfl_standard_argp needs a Dwfl pointer to fill in. */
state->child_inputs[0] = state->input;
break;
case 'f':
generate_fcpf = true;
ignore_no_name = true;
single_address_size = 0;
break;
case 'c':
generate_cpf = true;
ignore_no_name = true;
single_address_size = 0;
break;
case 'x':
generate_xml = true;
break;
case 'i':
ignore_no_name = true;
break;
case 's':
single_address_size = atoi (arg);
break;
case 'd':
show_die_offset = true;
break;
case ARGP_KEY_FINI:
if (generate_cpf + generate_xml + generate_fcpf > 1)
{
argp_failure (state, EXIT_FAILURE, 0,
"Can only generate one format"
" (XML, CTF or FCTF) at a time.\n");
return EINVAL;
}
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Returns size of code described by this DIE. Returns zero if this
DIE doesn't cover any code. 1 is returned for DIEs that do describe
code by have unknown size. */
static Dwarf_Word
DIE_code_size (Dwarf_Die *die)
{
Dwarf_Addr base;
Dwarf_Addr begin;
Dwarf_Addr end;
ptrdiff_t off = 0;
Dwarf_Word size = 0;
do
{
// Also handles lowpc plus highpc as special one range case.
off = dwarf_ranges (die, off, &base, &begin, &end);
if (off > 0)
{
size += (end - begin);
}
}
while (off > 0);
if (size == 0 && (dwarf_hasattr (die, DW_AT_entry_pc)
|| dwarf_hasattr (die, DW_AT_low_pc)))
size = single_address_size;
return size;
}
/* Returns the tag of the DIE declaring the given DIE following
DW_AT_abstract_origin and DW_AT_specification. */
static int
DIE_decl_tag (Dwarf_Die *die, Dwarf_Die **decl)
{
Dwarf_Die die_mem;
Dwarf_Attribute attr_mem;
Dwarf_Attribute *attr;
*decl = die;
do
{
attr = dwarf_attr (die, DW_AT_abstract_origin, &attr_mem);
if (attr == NULL)
attr = dwarf_attr (die, DW_AT_specification, &attr_mem);
if (attr == NULL)
break;
die = *decl;
*decl = dwarf_formref_die (attr, &die_mem);
}
while (*decl != NULL); /* Wouldn't that actually be an error? */
return dwarf_tag (die);
}
#if 0
/* Returns a static constant string representation of the DIE tag.
Returns NULL when unknown. Would be nice if libdw had this. */
static const char *
TAG_name (int tag)
{
/* Just recognize code/function DIEs. Add more if necessary. */
switch (tag)
{
case DW_TAG_compile_unit:
return "compile_unit";
case DW_TAG_subprogram:
return "subprogram";
case DW_TAG_catch_block:
return "catch_block";
case DW_TAG_inlined_subroutine:
return "inlined_subroutine";
case DW_TAG_lexical_block:
return "lexical_block";
case DW_TAG_module:
return "module";
case DW_TAG_partial_unit:
return "partial_unit";
case DW_TAG_try_block:
return "try_block";
case DW_TAG_with_stmt:
return "with_stmt";
case DW_TAG_GNU_call_site:
return "call_site";
case DW_TAG_label:
return "label";
default:
return NULL;
}
}
#endif
/* Returns the code size of the DIE and fills in the what and where
info if the size is greater than zero. */
Dwarf_Word
DIE_what_where_size (Dwarf_Die *die,
struct what_info *what, struct where_info *where)
{
Dwarf_Word size = DIE_code_size (die);
if (size > 0)
{
Dwarf_Die *decl;
const char *what_file, *where_file;
what->tag = DIE_decl_tag (die, &decl);
what->die_off = dwarf_dieoffset (decl);
what->name = dwarf_diename (die);
what_file = dwarf_decl_file (die);
what->line = 0;
what->col = 0;
dwarf_decl_line (die, &what->line);
dwarf_decl_column (die, &what->col);
if (decl == die)
{
where->tag = what->tag;
where->die_off = what->die_off;
where_file = what_file;
where->line = what->line;
where->col = what->col;
}
else
{
where->tag = dwarf_tag (die);
where->die_off = dwarf_dieoffset (die);
Dwarf_Word value;
Dwarf_Attribute attr_mem;
where_file = what_file;
if (dwarf_formudata (dwarf_attr (die, DW_AT_call_file, &attr_mem),
&value) == 0)
where_file = dwarf_filesrc (files, value, NULL, NULL);
where->line = what->line;
if (dwarf_formudata (dwarf_attr (die, DW_AT_call_line, &attr_mem),
&value) == 0)
where->line = value;
where->col = what->col;
if (dwarf_formudata (dwarf_attr (die, DW_AT_call_column, &attr_mem),
&value) == 0)
where->col = value;
/* XXX is this really right or just cosmetics? If all
information of what and where are the same just pretend
what == where anyway. Note we force the what die_off
because all information can apparently be derived from
the where. */
if (where->tag == what->tag
&& where_file == what_file
&& where->line == what->line
&& where->col == what->col)
what->die_off = where->die_off;
}
where->size = size;
what->file = escape_name (what_file);
where->file = escape_name (where_file);
// Register these addresses cf. die-code-size etc.
{
Dwarf_Addr base;
Dwarf_Addr begin;
Dwarf_Addr end;
ptrdiff_t off = 0;
do
{
// Also handles lowpc plus highpc as special one range case.
off = dwarf_ranges (die, off, &base, &begin, &end);
if (off > 0)
{
register_address_span (what, begin, end);
}
}
while (off > 0);
if (size == 0 && (dwarf_hasattr (die, DW_AT_entry_pc)
|| dwarf_hasattr (die, DW_AT_low_pc)) &&
single_address_size > 0)
{
fprintf (stderr, "test me - size zero die: 0x%ld", (long) base);
register_address_span (what, base, base + 1);
}
}
}
else
{
what->file = NULL;
where->file = NULL;
}
return size;
}
#if 0
/* Returns a hopefully unique identifier for what code is being used
based on the definition tag, name, file, line and col if
known. String has to be freed by caller. */
static char *
what_identifier_string (const struct what_info *what)
{
char *res;
int tag = what->tag;
const char *orig_name = what->name;
const char *file = what->file;
int line = what->line;
int col = what->col;
Dwarf_Word die_off = what->die_off;
if (orig_name != NULL)
{
char *name = escape_name (orig_name);
if (file != NULL)
{
if (line != 0)
{
if (col != 0)
{
if (asprintf (&res, "%s:%s:%s:%d:%d", TAG_name (tag),
name, file, line, col) < 0)
res = NULL;
}
else
{
if (asprintf (&res, "%s:%s:%s:%d", TAG_name (tag),
name, file, line) < 0)
res = NULL;
}
}
else
{
if (asprintf (&res, "%s:%s:%s", TAG_name (tag), name, file) < 0)
res = NULL;
}
}
else
{
if (asprintf (&res, "%s:%s", TAG_name (tag), name) < 0)
res = NULL;
}
free (name);
}
else
{
// No name, use DIE offset to generate something (possibly non-unique).
if (asprintf (&res, "%s_%#lx", TAG_name (tag), (long)die_off) < 0)
res = NULL;
}
return res;
}
/* Returns a string describing the location where a DIE was used.
String has to be freed by caller. */
static char *
where_string (const struct where_info *where)
{
char *res;
int tag = where->tag;
const char *file = where->file;
int line = where->line;
int col = where->col;
if (file != NULL)
{
if (line != 0)
{
if (col != 0)
{
if (asprintf (&res, "%s:%s:%d:%d", TAG_name (tag),
file, line, col) < 0)
res = NULL;
}
else
{
if (asprintf (&res, "%s:%s:%d", TAG_name (tag),
file, line) < 0)
res = NULL;
}
}
else
{
if (asprintf (&res, "%s:%s", TAG_name (tag), file) < 0)
res = NULL;
}
}
else
{
if (asprintf (&res, "%s", TAG_name (tag)) < 0)
res = NULL;
}
return res;
}
#endif
/* We treat nested subprograms as "inlines", keep track of how deep we nest. */
// static int in_top_level_subprogram = 0;
static void
output_die_begin (struct what_info *what, struct where_info *where, int indent)
{
}
static void
output_die_end (struct what_info *pwhat, struct where_info *pwhere,
struct what_info *what, struct where_info *where,
Dwarf_Word children_size, int indent)
{
}
/* Walks all (code) children of the given DIE and returns the total
code size. */
static Dwarf_Word
walk_children (Dwarf_Die *die, int indent)
{
Dwarf_Word total = 0;
if (! dwarf_haschildren (die))
return total;
struct what_info pwhat;
struct where_info pwhere;
DIE_what_where_size (die, &pwhat, &pwhere);
Dwarf_Die child;
if (dwarf_child (die, &child) == 0)
{
do
{
struct what_info what;
struct where_info where;
/* Only DIEs with a code size have children with code and
the code size of a DIE >= the sum of the code size of the
children. */
Dwarf_Word size = DIE_what_where_size (&child, &what, &where);
if (size > 0)
{
/* Even if we don't use this DIE because it doesn't have
a name, we still want to walk the children. */
bool use_die = ((what.name != NULL) || (! ignore_no_name));
if (use_die)
{
/* Note we add the whole DIE size, which include the
size of all children. So only add children_size
below if we don't report this DIE. */
total += size;
output_die_begin (&what, &where, indent);
}
Dwarf_Word children_size = walk_children (&child, indent + 1);
if (use_die)
output_die_end (&pwhat, &pwhere, &what, &where, children_size, indent);
else
total += children_size;
}
if (what.file)
free (what.file);
if (where.file)
free (where.file);
}
while (dwarf_siblingof (&child, &child) == 0);
}
return total;
}
static void
output_cu_begin (struct what_info *what, struct where_info *where)
{
output_die_begin (what, where, 2); // indent 2 (dwarfprofile + module).
}
static void
output_cu_end (struct what_info *what, struct where_info *where,
Dwarf_Word children_size)
{
output_die_end (NULL, NULL, what, where, children_size, 2);
}
static void
handle_cu (Dwarf_Die *cu)
{
/* Skip CUs without any code. */
Dwarf_Word size = DIE_code_size (cu);
const char *name = dwarf_diename (cu);
// XXX ehe, name == NULL, when does that happen?
if (size == 0 || name == NULL)
return;
/* Construct a (short) name and file to refer to this CU. */
const char *short_name = rindex (name, '/');
short_name = (short_name != NULL) ? short_name + 1 : name;
Dwarf_Attribute attr;
const char *dir = dwarf_formstring (dwarf_attr (cu, DW_AT_comp_dir, &attr));
char *full_name;
const char *file = name;
if (dir != NULL && name[0] != '/')
{
if (asprintf (&full_name, "%s/%s", dir, name) != -1)
file = full_name;
}
/* Compile Unit DIEs only really have where info, but construct a
what for consistency. XXX Need to handle imported_unit/partial_units? */
struct where_info where;
struct what_info what;
where.tag = what.tag = dwarf_tag (cu);
where.die_off = what.die_off = dwarf_dieoffset (cu);
what.name = short_name;
where.file = what.file = escape_name (file);
where.line = what.line = 0;
where.col = what.col = 0;
where.size = size;
/* cache the file list for this CU. */
if (dwarf_getsrcfiles (cu, &files, NULL) != 0)
files = NULL; // There better not be any DW_AT_desc_files...
output_cu_begin (&what, &where);
Dwarf_Word children_size = walk_children (cu, 3); // indent 3 (dp/mod/cu)
output_cu_end (&what, &where, children_size);
if (file != name)
free (full_name);
}
static void
output_module_begin (const char *name)
{
fprintf (stderr, "process '%s' ", name);
}
static void
output_module_end (const char *name)
{
fprintf (stderr, "... done\n");
}
static int
handle_module (Dwfl_Module *mod, void **userdata, const char *name,
Dwarf_Addr base, void *arg)
{
Dwarf_Die *cu = NULL;
Dwarf_Addr bias;
output_module_begin (name);
while ((cu = dwfl_module_nextcu (mod, cu, &bias)) != NULL)
handle_cu (cu);
output_module_end (name);
return DWARF_CB_OK;
}
void
output_paths ()
{
}
int
main (int argc, char **argv)
{
const struct argp_option options[] =
{
{ NULL, 0, NULL, 0, "Output selection options:", 2 },
{ "flatcalltree", 'f', NULL, 0,
"Output Flat Calltree Profile Format (implies -i -s0)", 0 },
{ "calltree", 'c', NULL, 0,
"Output Calltree Profile Format (implies -i -s0)", 0 },
{ "xml", 'x', NULL, 0, "XML output", 0 },
{ NULL, 0, NULL, 0, "Code DIE selection options:", 3 },
{ "ignore-no-name", 'i', NULL, 0,
"Ignore code DIEs without a name (e.g. lexical_blocks)", 0 },
{ "single-address", 's', "size", 0,
"Size to use for single-address DIEs (e.g. labels or call_sites,"
" which only have a DW_AT_low_pc, but not DW_AT_high_pc)."
" Defaults to 1. When 0, single-address DIEs are ignored", 0 },
{ NULL, 0, NULL, 0, ("Miscellaneous:"), 0 },
// Anything else (help, usage, etc.)
{ "die-offsets", 'd', NULL, 0, "Show DIE offsets (debug only)", 0 },
{ NULL, 0, NULL, 0, NULL, 0 }
};
const struct argp_child argp_children[] =
{
{ .argp = dwfl_standard_argp () },
{ .argp = NULL },
};
argp.children = argp_children;
argp.options = options;
argp.parser = parse_opt;
int cnt;
Dwfl *dwfl = NULL;
error_t e = argp_parse (&argp, argc, argv, 0, &cnt, &dwfl);
if (e != 0 || dwfl == NULL)
exit (-1);
ptrdiff_t res = dwfl_getmodules (dwfl, handle_module, NULL, 0);
if (res != 0) // We should handle all modules, anything else is an error
{
fprintf (stderr, "dwfl_getmodules failed: %s\n", dwfl_errmsg (-1));
exit (-1);
}
output_paths ();
dwfl_end (dwfl);
dump_results();
return 0;
}