[fb7fe6]: pp / oprofpp_util.cpp  Maximize  Restore  History

Download this file

1019 lines (864 with data), 26.4 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
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
/* $Id: oprofpp_util.cpp,v 1.52 2002/05/06 18:00:35 movement 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.
*/
// FIXME: printf -> ostream (and elsewhere)
#include <cstdarg>
#include <algorithm>
#include <sstream>
#include <iomanip>
#include <elf.h>
#include "oprofpp.h"
#include "opp_symbol.h"
#include "op_libiberty.h"
#include "op_file.h"
#include "file_manip.h"
#include "string_manip.h"
#include "op_events.h"
#include "op_events_desc.h"
using std::string;
using std::vector;
using std::ostream;
int verbose;
char const *samplefile;
const char *imagefile;
int demangle;
const char * exclude_symbols_str;
static vector<string> exclude_symbols;
void op_print_event(ostream & out, int i, op_cpu cpu_type,
u8 type, u8 um, u32 count)
{
char * typenamep;
char * typedescp;
char * umdescp;
op_get_event_desc(cpu_type, type, um,
&typenamep, &typedescp, &umdescp);
out << "Counter " << i << " counted " << typenamep << " events ("
<< typedescp << ")";
if (cpu_type != CPU_RTC) {
out << " with a unit mask of 0x"
<< hex << setw(2) << setfill('0') << unsigned(um) << " ("
<< (umdescp ? umdescp : "Not set") << ")";
}
out << " count " << dec << count << endl;
}
/**
* verbprintf
*/
void verbprintf(const char * fmt, ...)
{
if (verbose) {
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
va_end(va);
}
}
/**
* remangle - convert a filename into the related sample file name
* \param image the image filename
*/
static char *remangle(const char *image)
{
char *file;
char *c;
file = (char *)xmalloc(strlen(OP_SAMPLES_DIR) + strlen(image) + 1);
strcpy(file, OP_SAMPLES_DIR);
c = &file[strlen(file)];
strcat(file, image);
while (*c) {
if (*c == '/')
*c = OPD_MANGLE_CHAR;
c++;
}
return file;
}
/**
* demangle_filename - convert a sample filenames into the related
* image file name
* \param sample_filename the samples image filename
*
* if samples_filename does not contain any %OPD_MANGLE_CHAR
* the string samples_filename itself is returned.
*/
std::string demangle_filename(const std::string & samples_filename)
{
std::string result(samples_filename);
size_t pos = samples_filename.find_first_of(OPD_MANGLE_CHAR);
if (pos != std::string::npos) {
result.erase(0, pos);
std::replace(result.begin(), result.end(), OPD_MANGLE_CHAR, '/');
}
return result;
}
/**
* is_excluded_symbol - check if the symbol is in the exclude list
* \param symbol symbol name to check
*
* return true if symbol is in the list of excluded symbol
*/
bool is_excluded_symbol(const std::string & symbol)
{
return std::find(exclude_symbols.begin(), exclude_symbols.end(),
symbol) != exclude_symbols.end();
}
/**
* quit_error - quit with error
* \param err error to show
*
* err may be NULL
*/
void quit_error(poptContext optcon, char const *err)
{
if (err)
fprintf(stderr, err);
poptPrintHelp(optcon, stderr, 0);
exit(EXIT_FAILURE);
}
/**
* validate_counter - validate the counter nr
* \param counter_mask bit mask specifying the counter nr to use
* \param sort_by the counter nr from which we sort
*
* all error are fatal
*/
void validate_counter(int counter_mask, int & sort_by_counter)
{
if (counter_mask + 1 > 1 << OP_MAX_COUNTERS) {
cerr << "invalid counter mask " << counter_mask << "\n";
exit(EXIT_FAILURE);
}
if (sort_by_counter == -1) {
// get the first counter selected and use it as sort order
for (size_t i = 0 ; i < OP_MAX_COUNTERS ; ++i) {
if ((counter_mask & (1 << i)) != 0)
sort_by_counter = i;
}
}
if ((counter_mask & (1 << sort_by_counter)) == 0) {
cerr << "invalid sort counter nr " << sort_by_counter << "\n";
exit(EXIT_FAILURE);
}
}
/**
* opp_treat_options - process command line options
* \param file a filename passed on the command line, can be %NULL
* \param optcon poptContext to allow better message handling
* \param image_file where to store the image file name
* \param sample_file ditto for sample filename
* \param counter where to put the counter command line argument
*
* Process the arguments, fatally complaining on
* error.
*
* Most of the complexity here is to process
* filename. file is considered as a sample file
* if it contains at least one OPD_MANGLE_CHAR else
* it is an image file. If no image file is given
* on command line the sample file name is un-mangled
* -after- stripping the optionnal "#d" suffixe. This
* give some limitations on the image filename.
*
* all filename checking is made here only with a
* syntactical approch. (ie existence of filename is
* not tested)
*
* post-condition: sample_file and image_file are setup
*/
void opp_treat_options(const char* file, poptContext optcon,
string & image_file, string & sample_file,
int & counter, int & sort_by_counter)
{
char *file_ctr_str;
int temp_counter;
/* add to the exclude symbol list the symbols contained in the comma
* separated list of symbols */
if (exclude_symbols_str)
separate_token(exclude_symbols, exclude_symbols_str, ',');
/* some minor memory leak from the next calls */
if (imagefile)
imagefile = op_relative_to_absolute_path(imagefile, NULL);
if (samplefile)
samplefile = op_relative_to_absolute_path(samplefile, NULL);
if (file) {
if (imagefile && samplefile) {
quit_error(optcon, "oprofpp: too many filenames given on command line:"
"you can specify at most one sample filename"
" and one image filename.\n");
}
file = op_relative_to_absolute_path(file, NULL);
if (strchr(file, OPD_MANGLE_CHAR))
samplefile = file;
else
imagefile = file;
}
if (!samplefile) {
if (!imagefile) {
quit_error(optcon, "oprofpp: no samples file specified.\n");
} else {
/* we'll "leak" this memory */
samplefile = remangle(imagefile);
}
}
/* we can not complete filename checking of imagefile because
* it can be derived from the sample filename, we must process
* and chop optionnal suffixe "#%d" first */
/* check for a valid counter suffix in a given sample file */
temp_counter = -1;
file_ctr_str = strrchr(samplefile, '#');
if (file_ctr_str) {
sscanf(file_ctr_str + 1, "%d", &temp_counter);
}
if (temp_counter != -1 && counter != -1 && counter != 0) {
if ((counter & (1 << temp_counter)) == 0)
quit_error(optcon, "oprofpp: conflict between given counter and counter of samples file.\n");
}
if (counter == -1 || counter == 0) {
if (temp_counter != -1)
counter = 1 << temp_counter;
else
counter = 1 << 0; // use counter 0
}
/* chop suffixes */
if (file_ctr_str)
file_ctr_str[0] = '\0';
sample_file = samplefile;
if (!imagefile) {
/* we allow for user to specify a sample filename on the form
* /var/lib/oprofile/samples/}bin}nash}}}lib}libc.so so we need to
* check against this form of mangled filename */
string lib_name;
string app_name = extract_app_name(sample_file, lib_name);
if (lib_name.length())
app_name = lib_name;
image_file = demangle_filename(app_name);
}
else
image_file = imagefile;
validate_counter(counter, sort_by_counter);
}
// FIXME: only use char arrays and pointers if you MUST. Otherwise std::string
// and references everywhere please.
/**
* counter_mask - given a --counter=0,1,..., option parameter return a mask
* representing each counter. Bit i is on if counter i was specified.
* So we allow up to sizeof(uint) * CHAR_BIT different counter
*/
uint counter_mask(const std::string & str)
{
vector<string> result;
separate_token(result, str, ',');
uint mask = 0;
for (size_t i = 0 ; i < result.size(); ++i) {
istringstream stream(result[i]);
int counter;
stream >> counter;
mask |= 1 << counter;
}
return mask;
}
counter_array_t::counter_array_t()
{
for (size_t i = 0 ; i < OP_MAX_COUNTERS ; ++i)
value[i] = 0;
}
counter_array_t & counter_array_t::operator+=(const counter_array_t & rhs)
{
for (size_t i = 0 ; i < OP_MAX_COUNTERS ; ++i)
value[i] += rhs.value[i];
return *this;
}
opp_bfd::opp_bfd(opp_samples_files& samples, const std::string & filename)
:
ibfd(0),
bfd_syms(0),
sect_offset(0)
{
if (filename.length() == 0) {
fprintf(stderr,"oprofpp: oppp_bfd() empty image filename.\n");
exit(EXIT_FAILURE);
}
nr_samples = op_get_fsize(filename.c_str(), 0);
open_bfd_image(filename, samples.first_header()->is_kernel);
time_t newmtime = op_get_mtime(filename.c_str());
if (newmtime != samples.first_header()->mtime) {
fprintf(stderr, "oprofpp: WARNING: the last modified time of the binary file %s does not match\n"
"that of the sample file. Either this is the wrong binary or the binary\n"
"has been modified since the sample file was created.\n", filename.c_str());
}
samples.set_sect_offset(sect_offset);
}
opp_bfd::~opp_bfd()
{
if (bfd_syms) free(bfd_syms);
bfd_close(ibfd);
}
/**
* open_bfd_image - opp_bfd ctor helper
* \param file name of a valid image file
* \param is_kernel true if the image is the kernel or a module
*
* This function will open a bfd image and process symbols
* within this image file
*
* Failure to open the image a fatal
* gettings zero symbols from the image is not an error
*/
void opp_bfd::open_bfd_image(const std::string & filename, bool is_kernel)
{
char **matching;
ibfd = bfd_openr(filename.c_str(), NULL);
if (!ibfd) {
fprintf(stderr,"oprofpp: bfd_openr of %s failed.\n", filename.c_str());
exit(EXIT_FAILURE);
}
if (!bfd_check_format_matches(ibfd, bfd_object, &matching)) {
fprintf(stderr,"oprofpp: BFD format failure for %s.\n", filename.c_str());
exit(EXIT_FAILURE);
}
/* Kernel / kernel modules are calculated as offsets against
* the .text section, so they need special handling
*/
if (is_kernel) {
asection *sect;
sect = bfd_get_section_by_name(ibfd, ".text");
sect_offset = sect->filepos;
verbprintf("Adjusting kernel samples by 0x%x, .text filepos 0x%lx\n",
sect_offset, sect->filepos);
}
get_symbols();
}
/**
* symcomp - comparator
*
*/
static bool symcomp(const asymbol * a, const asymbol * b)
{
return a->value + a->section->vma < b->value + b->section->vma;
}
namespace {
// only add symbols that would /never/ be
// worth examining
static const char *boring_symbols[] = {
"gcc2_compiled.",
"_init"
};
static const size_t nr_boring_symbols =
sizeof(boring_symbols) / sizeof(boring_symbols[0]);
/**
* Return true if the symbol is worth looking at
*/
static bool interesting_symbol(asymbol *sym)
{
if (!(sym->section->flags & SEC_CODE))
return 0;
if (!sym->name || sym->name[0] == '\0')
return 0;
// C++ exception stuff
if (sym->name[0] == '.' && sym->name[1] == 'L')
return 0;
for (size_t i = 0; i < nr_boring_symbols; ++i) {
if (!strcmp(boring_symbols[i], sym->name))
return 0;
}
return 1;
}
} // namespace anon
/**
* get_symbols - opp_bfd ctor helper
*
* Parse and sort in ascending order all symbols
* in the file pointed to by abfd that reside in
* a %SEC_CODE section. Returns true if symbol(s)
* are found. The symbols are filtered through
* the interesting_symbol() predicate and sorted
* with the symcomp() comparator.
*/
bool opp_bfd::get_symbols()
{
uint nr_all_syms;
uint i;
size_t size;
if (!(bfd_get_file_flags(ibfd) & HAS_SYMS))
return false;
size = bfd_get_symtab_upper_bound(ibfd);
/* HAS_SYMS can be set with no symbols */
if (size < 1)
return false;
bfd_syms = (asymbol**)xmalloc(size);
nr_all_syms = bfd_canonicalize_symtab(ibfd, bfd_syms);
if (nr_all_syms < 1) {
return false;
}
for (i = 0; i < nr_all_syms; i++) {
if (interesting_symbol(bfd_syms[i])) {
syms.push_back(bfd_syms[i]);
}
}
std::stable_sort(syms.begin(), syms.end(), symcomp);
// we need to ensure than for a given vma only one symbol exist else
// we read more than one time some samples. Fix #526098
// ELF symbols size : potential bogosity here because when using
// elf symbol size we need to check than two symbols does not overlap.
for (i = 1 ; i < syms.size() ; ++i) {
if (syms[i]->value + syms[i]->section->vma ==
syms[i-1]->value + syms[i-1]->section->vma) {
// TODO: choose more carefully the symbol we drop.
// If once have FUNCTION flag and not the other keep
// it etc.
syms.erase(syms.begin() + i);
i--;
}
}
verbprintf("nr symbols %u\n", syms.size());
if (syms.empty())
return false;
return true;
}
u32 opp_bfd::sym_offset(uint sym_index, u32 num) const
{
/* take off section offset */
num -= syms[sym_index]->section->filepos;
/* and take off symbol offset from section */
num -= syms[sym_index]->value;
return num;
}
bool opp_bfd::have_debug_info() const
{
sec* section;
for (section = ibfd->sections; section; section = section->next)
if (section->flags & SEC_DEBUGGING)
break;
return section != NULL;
}
bool opp_bfd::get_linenr(uint sym_idx, uint offset,
const char*& filename, unsigned int& linenr) const
{
const char *functionname;
bfd_vma pc;
filename = 0;
linenr = 0;
asection* section = syms[sym_idx]->section;
if ((bfd_get_section_flags (ibfd, section) & SEC_ALLOC) == 0)
return false;
pc = sym_offset(sym_idx, offset) + syms[sym_idx]->value;
if (pc >= bfd_section_size(ibfd, section))
return false;
bool ret = bfd_find_nearest_line(ibfd, section, bfd_syms, pc,
&filename, &functionname, &linenr);
if (filename == NULL || ret == false) {
filename = "";
linenr = 0;
ret = false;
}
// functioname and symbol name can be different if we query linenr info
// if we accept it we can get samples for the wrong symbol (#484660)
if (ret == true && functionname &&
strcmp(functionname, syms[sym_idx]->name)) {
ret = false;
}
/* binutils 2.12 and below have a small bug where functions without a
* debug entry at the prologue start do not give a useful line number
* from bfd_find_nearest_line(). This can happen with certain gcc
* versions such as 2.95.
*
* We work around this problem by scanning forward for a vma with
* valid linenr info, if we can't get a valid line number.
* Problem uncovered by Norbert Kaufmann. The work-around decreases,
* on the tincas application, the number of failure to retrieve linenr
* info from 835 to 173. Most of the remaining are c++ inline functions
* mainly from the STL library. Fix #529622
*/
if (/*ret == false || */linenr == 0) {
// FIXME: looking at debug info for all gcc version shows
// than the same problems can -perhaps- occur for epilog code:
// find a samples files with samples in epilog and try oprofpp
// -L -o on it, check it also with op_to_source.
// first restrict the search on a sensible range of vma,
// 16 is an intuitive value based on epilog code look
size_t max_search = 16;
size_t section_size = bfd_section_size(ibfd, section);
if (pc + max_search > section_size)
max_search = section_size - pc;
for (size_t i = 1 ; i < max_search ; ++i) {
bool ret = bfd_find_nearest_line(ibfd, section,
bfd_syms, pc+i,
&filename,
&functionname,
&linenr);
if (ret == true && linenr != 0 &&
strcmp(functionname, syms[sym_idx]->name) == 0) {
return ret; // we win
}
}
// We lose it's worthwhile to try more.
// bfd_find_nearest_line clobber the memory pointed by filename
// from a previous call when the filename change across
// multiple calls. The more easy way to recover is to reissue
// the first call, we don't need to recheck return value, we
// know that the call will succeed.
bfd_find_nearest_line(ibfd, section, bfd_syms, pc,
&filename, &functionname, &linenr);
}
return ret;
}
// #define USE_ELF_INTERNAL
#ifdef USE_ELF_INTERNAL
struct elf_internal_sym {
bfd_vma st_value; /* Value of the symbol */
bfd_vma st_size; /* Associated symbol size */
unsigned long st_name; /* Symbol name, index in string tbl */
unsigned char st_info; /* Type and binding attributes */
unsigned char st_other; /* No defined meaning, 0 */
unsigned short st_shndx; /* Associated section index */
};
typedef struct elf_internal_sym Elf_Internal_Sym;
typedef struct
{
/* The BFD symbol. */
asymbol symbol;
/* ELF symbol information. */
Elf_Internal_Sym internal_elf_sym;
} elf_symbol_type;
#endif /* USE_ELF_INTERNAL */
size_t opp_bfd::symbol_size(uint sym_idx) const
{
asymbol * next, *sym;
sym = syms[sym_idx];
next = (sym_idx == syms.size() - 1) ? NULL : syms[sym_idx + 1];
u32 start = sym->section->filepos + sym->value;
size_t length;
#ifndef USE_ELF_INTERNAL
u32 end;
if (next) {
end = next->value;
/* offset of section */
end += next->section->filepos;
} else
end = nr_samples;
length = end - start;
#else /* !USE_ELF_INTERNAL */
size_t length =
((elf_symbol_type *)sym)->internal_elf_sym.st_size;
// some asm symbol can have a zero length such system_call
// entry point in vmlinux. Calculate the length from the next
// symbol vma
if (length == 0) {
u32 next_offset = start;
if (next) {
next_offset = next->value + next->section->filepos;
} else {
next_offset = nr_samples;
}
length = next_offset - start;
}
#endif /* USE_ELF_INTERNAL */
return length;
}
void opp_bfd::get_symbol_range(uint sym_idx, u32 & start, u32 & end) const
{
asymbol *sym, *next;
sym = syms[sym_idx];
next = (sym_idx == syms.size() - 1) ? NULL : syms[sym_idx + 1];
verbprintf("Symbol %s, value 0x%lx\n", sym->name, sym->value);
start = sym->value;
/* offset of section */
start += sym->section->filepos;
verbprintf("in section %s, filepos 0x%lx\n", sym->section->name, sym->section->filepos);
if (is_excluded_symbol(sym->name)) {
end = start;
} else {
end = start + symbol_size(sym_idx);
}
verbprintf("start 0x%x, end 0x%x\n", start, end);
if (start >= nr_samples + sect_offset) {
fprintf(stderr,"oprofpp: start 0x%x out of range (max 0x%x)\n", start, nr_samples);
exit(EXIT_FAILURE);
}
if (end > nr_samples + sect_offset) {
fprintf(stderr,"oprofpp: end 0x%x out of range (max 0x%x)\n", end, nr_samples);
exit(EXIT_FAILURE);
}
if (start > end) {
fprintf(stderr,"oprofpp: start 0x%x overflow or end 0x%x underflow\n", start, end);
exit(EXIT_FAILURE);
}
}
/**
* symbol_index - find a symbol
* \param name the symbol name
*
* find and return the index of a symbol.
* if the name is not found -1 is returned
*/
int opp_bfd::symbol_index(const char* symbol) const
{
for (size_t i = 0; i < syms.size(); i++) {
if (!strcmp(syms[i]->name, symbol))
return i;
}
return -1;
}
/**
* check_headers - check coherence between two headers.
* \param f1 first header
* \param f2 second header
*
* verify that header f1 and f2 are coherent.
* all error are fatal
*/
void check_headers(opd_header const * f1, opd_header const * f2)
{
if (f1->mtime != f2->mtime) {
fprintf(stderr, "oprofpp: header timestamps are different (%ld, %ld)\n", f1->mtime, f2->mtime);
exit(EXIT_FAILURE);
}
if (f1->is_kernel != f2->is_kernel) {
fprintf(stderr, "oprofpp: header is_kernel flags are different\n");
exit(EXIT_FAILURE);
}
if (f1->cpu_speed != f2->cpu_speed) {
fprintf(stderr, "oprofpp: header cpu speeds are different (%f, %f)",
f2->cpu_speed, f2->cpu_speed);
exit(EXIT_FAILURE);
}
if (f1->separate_samples != f2->separate_samples) {
fprintf(stderr, "oprofpp: header separate_samples are different (%d, %d)",
f2->separate_samples, f2->separate_samples);
exit(EXIT_FAILURE);
}
}
void check_event(const struct opd_header * header)
{
char * ctr_name;
char * ctr_desc;
char * ctr_um_desc;
op_cpu cpu = static_cast<op_cpu>(header->cpu_type);
op_get_event_desc(cpu, header->ctr_event, header->ctr_um,
&ctr_name, &ctr_desc, &ctr_um_desc);
}
/**
* opp_samples_files - construct an opp_samples_files object
* \param sample_file the base name of sample file
* \param counter which samples files to open, -1 means try to open
* all samples files.
*
* at least one sample file (based on sample_file name)
* must be opened. If more than one sample file is open
* their header must be coherent. Each header is also
* sanitized.
*
* all error are fatal
*/
opp_samples_files::opp_samples_files(const std::string & sample_file,
int counter_)
:
nr_counters(2),
sample_filename(sample_file),
counter_mask(counter_),
first_file(-1)
{
uint i, j;
time_t mtime = 0;
/* no samplefiles open initially */
for (i = 0; i < OP_MAX_COUNTERS; ++i) {
samples[i] = 0;
}
for (i = 0; i < OP_MAX_COUNTERS ; ++i) {
if ((counter_mask & (1 << i)) != 0) {
/* if only the i th bit is set in counter spec we do
* not allow opening failure to get a more precise
* error message */
open_samples_file(i, (counter_mask & ~(1 << i)) != 0);
}
}
/* find first open file */
for (first_file = 0; first_file < OP_MAX_COUNTERS ; ++first_file) {
if (samples[first_file] != 0)
break;
}
if (first_file == OP_MAX_COUNTERS) {
fprintf(stderr, "Can not open any samples files for %s last error %s\n", sample_filename.c_str(), strerror(errno));
exit(EXIT_FAILURE);
}
const struct opd_header * header = samples[first_file]->header();
mtime = header->mtime;
/* determine how many counters are possible via the sample file */
op_cpu cpu = static_cast<op_cpu>(header->cpu_type);
nr_counters = op_get_cpu_nr_counters(cpu);
/* check sample files match */
for (j = first_file + 1; j < OP_MAX_COUNTERS; ++j) {
if (samples[j] == 0)
continue;
samples[first_file]->check_headers(*samples[j]);
}
/* sanity check on ctr_um, ctr_event and cpu_type */
for (i = 0 ; i < OP_MAX_COUNTERS; ++i) {
if (samples[i] != 0)
check_event(samples[i]->header());
}
}
/**
* ~opp_samples_files - destroy an object opp_samples
*
* close and free all related resource to the samples file(s)
*/
opp_samples_files::~opp_samples_files()
{
uint i;
for (i = 0 ; i < OP_MAX_COUNTERS; ++i) {
delete samples[i];
}
}
/**
* open_samples_file - ctor helper
* \param counter the counter number
* \param can_fail allow to fail gracefully
*
* open and mmap the given samples files,
* the member var samples[counter], header[counter]
* etc. are updated in case of success.
* The header is checked but coherence between
* header can not be sanitized at this point.
*
* if can_fail == false all error are fatal.
*/
void opp_samples_files::open_samples_file(u32 counter, bool can_fail)
{
std::ostringstream filename;
filename << sample_filename << "#" << counter;
std::string temp = filename.str();
if (access(temp.c_str(), R_OK) == 0) {
samples[counter] = new samples_file_t(temp);
}
else {
if (can_fail == false) {
/* FIXME: nicer message if e.g. wrong counter */
fprintf(stderr, "oprofpp: Opening %s failed. %s\n", temp.c_str(), strerror(errno));
exit(EXIT_FAILURE);
}
}
}
/**
* accumulate_samples - lookup samples from a vma address
* \param counter where to accumulate the samples
* \param index index of the samples.
*
* return false if no samples has been found
*/
bool opp_samples_files::accumulate_samples(counter_array_t& counter, uint index) const
{
bool found_samples = false;
for (uint k = 0; k < nr_counters; ++k) {
if (samples_count(k, index)) {
found_samples = true;
counter[k] += samples_count(k, index);
}
}
return found_samples;
}
/**
* accumulate_samples - lookup samples from a range of vma address
* \param counter where to accumulate the samples
* \param start start index of the samples.
* \param end end index of the samples.
*
* return false if no samples has been found
*/
bool opp_samples_files::accumulate_samples(counter_array_t& counter,
uint start, uint end) const
{
bool found_samples = false;
for (uint k = 0; k < nr_counters; ++k) {
if (is_open(k)) {
counter[k] += samples[k]->count(start, end);
if (counter[k])
found_samples = true;
}
}
return found_samples;
}
void opp_samples_files::set_sect_offset(u32 sect_offset)
{
for (uint k = 0; k < nr_counters; ++k) {
if (is_open(k)) {
samples[k]->sect_offset = sect_offset;
}
}
}
/**
* samples_file_t - construct a samples_file_t object
* \param filename the full path of sample file
*
* open and mmap the samples file specified by filename
* samples file header coherence are checked
*
* all error are fatal
*
*/
samples_file_t::samples_file_t(const std::string & filename)
:
sect_offset(0)
{
db_open(&db_tree, filename.c_str(), DB_RDONLY, sizeof(struct opd_header));
}
/**
* ~samples_file_t - destroy a samples_file_t object
*
* close and unmap the samples file
*
*/
samples_file_t::~samples_file_t()
{
if (db_tree.base_memory)
db_close(&db_tree);
}
/**
* check_headers - check than the lhs and rhs headers are
* coherent (same size, same mtime etc.)
* \param rhs the other samples_file_t
*
* all error are fatal
*
*/
bool samples_file_t::check_headers(const samples_file_t & rhs) const
{
::check_headers(header(), rhs.header());
return true;
}
void db_tree_callback(db_key_t, db_value_t value, void * data)
{
u32 * count = (u32 *)data;
*count += value;
}
/**
* count - return the number of samples in given range
* \param start start samples nr of range
* \param end end samples br of range
*
* return the number of samples in the the range [start, end]
* no range checking is performed.
*
* This actually code duplicate partially accumulate member of
* opp_samples_files which in future must use this as it internal
* implementation
*/
u32 samples_file_t::count(uint start, uint end) const
{
u32 count = 0;
db_travel(&db_tree, start - sect_offset, end - sect_offset,
db_tree_callback, &count);
return count;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks