[5d4628]: pp / oprofpp.c Maximize Restore History

Download this file

oprofpp.c    628 lines (528 with data), 15.8 kB

  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
/* $Id: oprofpp.c,v 1.19 2000/12/06 20:39:58 moz Exp $ */
/* COPYRIGHT (C) 2000 THE VICTORIA UNIVERSITY OF MANCHESTER and John Levon
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "oprofpp.h"
static int showvers;
static char *samplefile;
static char *imagefile;
static char *gproffile;
static char *symbol;
static int list_symbols;
static int demangle;
static struct opd_fentry *samples;
static uint nr_samples;
static struct opd_footer footer;
static char *ctr0_name;
static char *ctr0_desc;
static char *ctr0_um_desc;
static char *ctr1_name;
static char *ctr1_desc;
static char *ctr1_um_desc;
static int ctr;
static u32 sect_offset;
static struct poptOption options[] = {
{ "samples-file", 'f', POPT_ARG_STRING, &samplefile, 0, "image sample file", "file", },
{ "image-file", 'i', POPT_ARG_STRING, &imagefile, 0, "image file other than default", "file", },
{ "list-symbols", 'l', POPT_ARG_NONE, &list_symbols, 0, "list samples by symbol", NULL, },
{ "dump-gprof-file", 'g', POPT_ARG_STRING, &gproffile, 0, "dump gprof format file", "file", },
{ "list-symbol", 's', POPT_ARG_STRING, &symbol, 0, "give detailed samples for a symbol", "symbol", },
{ "gcc-demangle", 'd', POPT_ARG_NONE, &demangle, 0, "demangle GNU C++ symbol names", NULL, },
{ "counter", 'c', POPT_ARG_INT, &ctr, 0, "which counter to use", "0|1", },
{ "version", 'v', POPT_ARG_NONE, &showvers, 0, "show version", NULL, },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0, NULL, NULL, },
};
/**
* get_options - process command line
* @argc: program arg count
* @argv: program arg array
*
* Process the arguments, fatally complaining on
* error. samplefile is guaranteed to have some
* non-NULL value after this function.
*/
static void get_options(int argc, char *argv[])
{
poptContext optcon;
char c;
optcon = poptGetContext(NULL, argc, argv, options, 0);
c=poptGetNextOpt(optcon);
if (c<-1) {
fprintf(stderr, "oprofpp: %s: %s\n",
poptBadOption(optcon, POPT_BADOPTION_NOALIAS),
poptStrerror(c));
poptPrintHelp(optcon, stderr, 0);
exit(1);
}
if (showvers) {
printf(VERSION_STRING " compiled on " __DATE__ " " __TIME__ "\n");
exit(0);
}
if (!list_symbols && !gproffile && !symbol) {
fprintf(stderr, "oprofpp: no mode specified. What do you want from me ?\n");
poptPrintHelp(optcon, stderr, 0);
exit(1);
}
if (!samplefile) {
samplefile = poptGetArg(optcon);
if (!samplefile) {
fprintf(stderr, "oprofpp: no samples file specified.\n");
poptPrintHelp(optcon, stderr, 0);
exit(1);
}
}
}
/**
* printf_symbol - output a symbol name
* @name: verbatim symbol name
*
* Print the symbol name to stdout, demangling
* the symbol if the global variable demangle is %TRUE.
*
* The demangled name lists the parameters and type
* qualifiers such as "const".
*/
void printf_symbol(const char *name)
{
if (demangle) {
char *cp=(char *)name;
char *unmangled=cp;
while (*cp && *cp=='_')
cp++;
if (*cp) {
unmangled = cplus_demangle(cp,DMGL_PARAMS|DMGL_ANSI);
if (unmangled) {
/* FIXME: print leading underscores ? */
printf("%s",unmangled);
free(unmangled);
return;
}
}
}
printf("%s",name);
}
/**
* open_image_file - open associated binary image
* @mangled: name of samples file
*
* This function will open and process the binary
* image associated with the samples file @mangled,
* unless imagefile is non-%NULL, in which case
* that will be opened instead.
*
* @mangled may be a relative or absolute pathname.
*
* Failure to open the image is fatal. This function
* returns a pointer to the bfd descriptor for the
* image.
*
* The global struct footer must be valid. sect_offset
* will be set as appropriate.
*/
bfd *open_image_file(char *mangled)
{
char *c = &mangled[strlen(mangled)];
char *file;
char **matching;
bfd *ibfd;
if (!imagefile) {
/* strip leading dirs */
while (c!=mangled && *c!='/')
c--;
c++;
file = strdup(c);
if (!file) {
fprintf(stderr, "oprofpp: strdup() failed.\n");
exit(1);
}
c=file;
do {
if (*c==OPD_MANGLE_CHAR)
*c='/';
} while (*c++);
} else
file = imagefile;
ibfd = bfd_openr(file, NULL);
if (!ibfd) {
fprintf(stderr,"oprofpp: bfd_openr of %s failed.\n",file);
exit(1);
}
if (!bfd_check_format_matches(ibfd, bfd_object, &matching)) {
free(matching);
fprintf(stderr,"oprofpp: BFD format failure for %s.\n",file);
exit(1);
}
if (footer.is_kernel) {
asection *sect;
sect = bfd_get_section_by_name(ibfd, ".text");
sect_offset = OPD_KERNEL_OFFSET - sect->filepos;
printf("Adjusting kernel samples by 0x%x, .text filepos 0x%lx\n",sect_offset,sect->filepos);
}
return ibfd;
}
/**
* sym_offset - return offset from a symbol's start
* @sym: symbol
* @num: number of fentry
*
* Returns the offset of a sample at position @num
* in the samples file from the start of symbol @sym.
*/
u32 sym_offset(asymbol *sym, u32 num)
{
if (num - sect_offset > num) {
fprintf(stderr,"oprofpp: less than zero offset ? \n");
exit(1);
}
/* adjust for kernel images */
num -= sect_offset;
/* take off section offset */
num -= sym->section->filepos;
/* and take off symbol offset from section */
num -= sym->value;
return num;
}
int symcomp(const void *a, const void *b)
{
u32 va = (*((asymbol **)a))->value + (*((asymbol **)a))->section->filepos;
u32 vb = (*((asymbol **)b))->value + (*((asymbol **)b))->section->filepos;
if (va<vb)
return -1;
return (va>vb);
}
/* need a better filter, but only this gets rid of _start
* and other crud easily.
*/
static int interesting_symbol(asymbol *sym)
{
if (!(sym->section->flags & SEC_CODE))
return 0;
if (streq("",sym->name))
return 0;
if (streq("_init",sym->name))
return 0;
if (!(sym->flags & BSF_FUNCTION))
return 0;
return 1;
}
/**
* get_symbols - get symbols from bfd
* @ibfd: bfd to read from
* @symsp: pointer to array of symbol pointers
*
* Parse and sort in ascending order all symbols
* in the file pointed to by @ibfd that reside in
* a %SEC_CODE section. Returns the number
* of symbols found. @symsp will be set to point
* to the symbol pointer array.
*/
uint get_symbols(bfd *ibfd, asymbol ***symsp)
{
uint nr_all_syms;
uint nr_syms=0;
uint i;
size_t size;
asymbol **filt_syms;
asymbol **syms;
if (!(bfd_get_file_flags(ibfd) & HAS_SYMS))
return 0;
size = bfd_get_symtab_upper_bound(ibfd);
/* HAS_SYMS can be set with no symbols */
if (size<1)
return 0;
syms = opd_malloc(size);
nr_all_syms = bfd_canonicalize_symtab(ibfd, syms);
if (nr_all_syms < 1) {
opd_free(syms);
return 0;
}
for (i=0; i < nr_all_syms; i++) {
if (interesting_symbol(syms[i]))
nr_syms++;
}
filt_syms = opd_malloc(sizeof(asymbol *)*nr_syms);
for (nr_syms=0,i=0; i < nr_all_syms; i++) {
if (interesting_symbol(syms[i])) {
filt_syms[nr_syms] = syms[i];
nr_syms++;
}
}
qsort(filt_syms, nr_syms, sizeof(asymbol *), symcomp);
*symsp = filt_syms;
return nr_syms;
}
/**
* get_symbol_range - get range of symbol
* @sym: symbol
* @next: next symbol
* @start: pointer to start var
* @end: pointer to end var
*
* Calculates the range of sample file entries covered
* by @sym. @start and @end will be filled in appropriately.
* If @next is %NULL, all entries up to the end of the sample
* file will be used.
*/
void get_symbol_range(asymbol *sym, asymbol *next, u32 *start, u32 *end)
{
printf("Symbol %s, value 0x%lx\n",sym->name, sym->value);
*start = sym->value;
/* offset of section */
*start += sym->section->filepos;
printf("in section %s, filepos 0x%lx\n",sym->section->name, sym->section->filepos);
/* adjust for kernel image */
*start += sect_offset;
if (next) {
*end = next->value;
/* offset of section */
*end += next->section->filepos;
/* adjust for kernel image */
*end += sect_offset;
} else
*end = nr_samples;
printf("start 0x%x, end 0x%x\n",*start,*end);
}
struct opp_count {
asymbol *sym;
u32 count0;
u32 count1;
};
int countcomp(const void *a, const void *b)
{
struct opp_count *ca= (struct opp_count *)a;
struct opp_count *cb= (struct opp_count *)b;
if (ctr) {
if (ca->count1 < cb->count1)
return -1;
return (ca->count1 > cb->count1);
} else {
if (ca->count0 < cb->count0)
return -1;
return (ca->count0 > cb->count0);
}
}
/**
* do_list_symbols - list symbol samples for an image
*
* Lists all the symbols from the image specified by samplefile,
* in decreasing sample count order, to standard out.
*/
void do_list_symbols(void)
{
bfd *ibfd;
asymbol **syms;
struct opp_count *scounts;
u32 start, end;
uint num,tot0=0,tot1=0,i,j;
ibfd = open_image_file(samplefile);
num = get_symbols(ibfd,&syms);
if (!num) {
fprintf(stderr, "oprofpp: couldn't get any symbols from image file.\n");
exit(1);
}
scounts = opd_calloc0(num,sizeof(struct opp_count));
for (i=0; i < num; i++) {
scounts[i].sym = syms[i];
get_symbol_range(syms[i], (i==num-1) ? NULL : syms[i+1], &start, &end);
if (start >= nr_samples) {
fprintf(stderr,"oprofpp: start 0x%x out of range (max 0x%x)\n",start,nr_samples);
exit(1);
}
if (end > nr_samples) {
fprintf(stderr,"oprofpp: end 0x%x out of range (max 0x%x)\n",end,nr_samples);
exit(1);
}
for (j=start; j < end; j++) {
if (samples[j].count0)
printf("Adding %u samples for symbol $%s$ at pos j 0x%x\n",samples[j].count0,syms[i]->name,j);
scounts[i].count0 += samples[j].count0;
scounts[i].count1 += samples[j].count1;
tot0 += samples[j].count0;
tot1 += samples[j].count1;
}
}
qsort(scounts, num, sizeof(struct opp_count), countcomp);
for (i=0; i < num; i++) {
printf_symbol(scounts[i].sym->name);
if (ctr && scounts[i].count1) {
printf("[0x%.8lx]: %2.4f%% (%u samples)\n",scounts[i].sym->value+scounts[i].sym->section->vma,
(((double)scounts[i].count1)/tot1)*100.0, scounts[i].count1);
} else if (scounts[i].count0) {
printf("[0x%.8lx]: %2.4f%% (%u samples)\n",scounts[i].sym->value+scounts[i].sym->section->vma,
(((double)scounts[i].count0)/tot0)*100.0, scounts[i].count0);
} else if (!streq("",scounts[i].sym->name))
printf(" (0 samples)\n");
}
opd_free(scounts);
opd_free(syms);
bfd_close(ibfd);
}
/**
* do_list_symbol - list detailed samples for a symbol
*
* Lists all the samples for the symbol specified by symbol, from the image
* specified by @samplefile, in decreasing sample count order, to standard out.
*/
void do_list_symbol(void)
{
bfd *ibfd;
asymbol **syms;
u32 start, end;
u32 num,i,j;
ibfd = open_image_file(samplefile);
num = get_symbols(ibfd,&syms);
if (!num) {
fprintf(stderr, "oprofpp: couldn't get any symbols from image file.\n");
exit(1);
}
for (i=0; i < num; i++) {
if (streq(syms[i]->name,symbol))
goto found;
}
fprintf(stderr, "oprofpp: symbol \"%s\" not found in image file.\n", symbol);
return;
found:
printf("Samples for symbol \"%s\" in image %s\n", symbol, ibfd->filename);
get_symbol_range(syms[i], (i==num-1) ? NULL : syms[i+1], &start, &end);
for (j=start; j < end; j++) {
if (samples[j].count0 || samples[j].count1) {
printf("%s+%x/%x: %s:%u %s:%u\n", symbol, sym_offset(syms[i], j), end-start,
ctr0_name, samples[j].count0, ctr1_name, samples[j].count1);
}
}
opd_free(syms);
bfd_close(ibfd);
}
#define GMON_VERSION 1
#define GMON_TAG_BB_COUNT 2
struct gmon_hdr {
char cookie[4];
u32 version;
u32 spare[3];
};
/**
* do_dump_gprof - produce gprof sample output
*
* Dump gprof-format samples for the image specified by samplefile to
* the file specified by gproffile.
*
* FIXME: this is totally broken (and never worked or was tested)
*/
void do_dump_gprof(void)
{
static struct gmon_hdr hdr = { "gmon", GMON_VERSION, {0,0,0,},};
FILE *fp;
bfd *ibfd;
asymbol **syms;
u32 start, end;
uint nblocks=0;
uint num,i,j;
ibfd = open_image_file(samplefile);
num = get_symbols(ibfd,&syms);
if (!num) {
fprintf(stderr, "oprofpp: couldn't get any symbols from image file.\n");
exit(1);
}
fp=opd_open_file(gproffile,"w");
opd_write_file(fp,&hdr, sizeof(struct gmon_hdr));
opd_write_u8(fp, GMON_TAG_BB_COUNT);
for (i=0; i < num; i++) {
get_symbol_range(syms[i], (i==num-1) ? NULL : syms[i+1], &start, &end);
for (j=start; j < end; j++) {
if (ctr && samples[j].count1)
nblocks++;
else if (samples[j].count0)
nblocks++;
}
}
opd_write_u32_he(fp,nblocks);
for (i=0; i < num; i++) {
get_symbol_range(syms[i], (i==num-1) ? NULL : syms[i+1], &start, &end);
for (j=start; j < end; j++) {
if (ctr && samples[j].count1) {
opd_write_u32_he(fp, sym_offset(syms[i],j) + syms[i]->value);
opd_write_u32_he(fp, samples[j].count1);
} else if (samples[j].count0) {
opd_write_u32_he(fp, sym_offset(syms[i],j) + syms[i]->value);
opd_write_u32_he(fp, samples[j].count0);
}
}
}
opd_close_file(fp);
opd_free(syms);
bfd_close(ibfd);
}
int main(int argc, char *argv[])
{
fd_t fd;
FILE *fp;
size_t size;
get_options(argc, argv);
fp = fopen(samplefile,"r");
if (!fp) {
fprintf(stderr, "oprofpp: fopen of %s failed. %s", samplefile, strerror(errno));
exit(1);
}
if (fseek(fp, -sizeof(struct opd_footer), SEEK_END)==-1) {
fprintf(stderr, "oprofpp: fseek of %s failed. %s", samplefile, strerror(errno));
exit(1);
}
if (fread(&footer, sizeof(struct opd_footer), 1, fp)!=1) {
fprintf(stderr, "oprofpp: fread of %s failed. %s", samplefile, strerror(errno));
exit(1);
}
fclose(fp);
if (footer.magic!=OPD_MAGIC) {
fprintf(stderr, "oprofpp: wrong magic 0x%x, expected 0x%x.\n", footer.magic, OPD_MAGIC);
exit(1);
}
if (footer.version!=OPD_VERSION) {
fprintf(stderr, "oprofpp: wrong version 0x%x, expected 0x%x.\n", footer.version, OPD_VERSION);
exit(1);
}
if (footer.ctr0_type_val) {
op_get_event_desc(footer.ctr0_type_val, footer.ctr0_um, &ctr0_name, &ctr0_desc, &ctr0_um_desc);
printf("Counter 0 counted %s events (%s) with a unit mask of 0x%.2x (%s)\n",ctr0_name, ctr0_desc,
footer.ctr0_um, ctr0_um_desc ? ctr0_um_desc : "Not set");
}
if (footer.ctr1_type_val) {
op_get_event_desc(footer.ctr1_type_val, footer.ctr1_um, &ctr1_name, &ctr1_desc, &ctr1_um_desc);
printf("Counter 1 counted %s events (%s) with a unit mask of 0x%.2x (%s)\n",ctr1_name, ctr1_desc,
footer.ctr1_um, ctr1_um_desc ? ctr1_um_desc : "Not set");
}
fd = open(samplefile, O_RDONLY);
if (fd==-1) {
fprintf(stderr, "oprofpp: Opening %s failed. %s",samplefile, strerror(errno));
exit(1);
}
size = opd_get_fsize(samplefile) - sizeof(struct opd_footer);
samples = (struct opd_fentry *)mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (samples==(void *)-1) {
fprintf(stderr, "oprofpp: mmap of %s failed. %s", samplefile, strerror(errno));
exit(1);
}
nr_samples = size/sizeof(struct opd_fentry);
if (list_symbols) {
do_list_symbols();
} else if (symbol) {
do_list_symbol();
} else if (gproffile) {
do_dump_gprof();
}
munmap(samples, size);
close(fd);
return 0;
}