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

Download this file

412 lines (338 with data), 12.6 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
/* $Id: oprofpp.cpp,v 1.43 2002/05/06 18:00:34 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 <algorithm>
#include <sstream>
#include <list>
#include "version.h"
#include "oprofpp.h"
#include "op_popt.h"
#include "op_libiberty.h"
#include "op_events_desc.h"
#include "op_fileio.h"
#include "file_manip.h"
#include "opf_filter.h"
using std::string;
using std::vector;
using std::cout;
using std::cerr;
static char * ctr_str;
static int showvers;
static int sort_by_counter = -1;
static char *gproffile;
static char *symbol;
static int list_symbols;
static int output_linenr_info;
static int show_shared_libs;
static char * output_format;
static int list_all_symbols_details;
static OutSymbFlag output_format_flags;
static poptOption options[] = {
{ "samples-file", 'f', POPT_ARG_STRING, &samplefile, 0, "image sample file", "file", },
{ "image-file", 'i', POPT_ARG_STRING, &imagefile, 0, "image file", "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", },
{ "demangle", 'd', POPT_ARG_NONE, &demangle, 0, "demangle GNU C++ symbol names", NULL, },
{ "counter", 'c', POPT_ARG_STRING, &ctr_str, 0, "which counter to display", "counter number[,counter nr]", },
{ "sort", 'C', POPT_ARG_INT, &sort_by_counter, 0, "which counter to use for sampels sort", "counter nr", },
{ "version", 'v', POPT_ARG_NONE, &showvers, 0, "show version", NULL, },
{ "verbose", 'V', POPT_ARG_NONE, &verbose, 0, "verbose output", NULL, },
{ "list-all-symbols-details", 'L', POPT_ARG_NONE, &list_all_symbols_details, 0, "list samples for all symbols", NULL, },
{ "output-linenr-info", 'o', POPT_ARG_NONE, &output_linenr_info, 0, "output filename:linenr info", NULL },
{ "exclude-symbol", 'e', POPT_ARG_STRING, &exclude_symbols_str, 0, "exclude these comma separated symbols", "symbol_name" },
{ "show-shared-libs", 'k', POPT_ARG_NONE, &show_shared_libs, 0,
"show details for shared libs. Only meaningfull if you have profiled with --separate-samples", NULL, },
// FIXME: clarify this
{ "output-format", 't', POPT_ARG_STRING, &output_format, 0,
"choose the output format", "output-format strings", },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0, NULL, NULL, },
};
/**
* get_options - process command line
* \param argc program arg count
* \param argv program arg array
* \param image_file where to store the image filename
* \param sample_file ditto for sample filename
* \param counter where to put the counter command line argument
*
* Process the arguments, fatally complaining on
* error. Only a part of arguments analysing is
* made here. The filename checking is deferred to
* opp_treat_options().
*
*/
static void opp_get_options(int argc, const char **argv, string & image_file,
string & sample_file, int & counter)
{
poptContext optcon;
const char *file;
optcon = op_poptGetContext(NULL, argc, argv, options, 0);
if (showvers) {
show_version(argv[0]);
}
if (!list_all_symbols_details && !list_symbols &&
!gproffile && !symbol)
quit_error(optcon, "oprofpp: no mode specified. What do you want from me ?\n");
/* check only one major mode specified */
if ((list_all_symbols_details + list_symbols + (gproffile != 0) + (symbol != 0)) > 1)
quit_error(optcon, "oprofpp: must specify only one output type.\n");
if (output_linenr_info && !list_all_symbols_details && !symbol && !list_symbols)
quit_error(optcon, "oprofpp: cannot list debug info without -L or -s option.\n");
/* non-option file, either a sample or binary image file */
file = poptGetArg(optcon);
if (show_shared_libs)
output_format_flags = static_cast<OutSymbFlag>(output_format_flags | osf_image_name);
if (output_linenr_info)
output_format_flags = static_cast<OutSymbFlag>(output_format_flags | osf_linenr_info);
if (output_format == 0) {
output_format = "hvspn";
} else {
if (!list_symbols && !list_all_symbols_details && !symbol) {
quit_error(optcon, "oprofpp: --output-format can be used only without --list-symbols or --list-all-symbols-details.\n");
}
}
if (list_symbols || list_all_symbols_details || symbol) {
OutSymbFlag fl =
OutputSymbol::ParseOutputOption(output_format);
if (fl == osf_none) {
cerr << "oprofpp: invalid --output-format flags.\n";
OutputSymbol::ShowHelp();
exit(EXIT_FAILURE);
}
output_format_flags = static_cast<OutSymbFlag>(output_format_flags | fl);
}
if (!ctr_str)
ctr_str = "";
counter = counter_mask(ctr_str);
opp_treat_options(file, optcon, image_file, sample_file,
counter, sort_by_counter);
poptFreeContext(optcon);
}
/**
* do_list_symbols - list symbol samples for an image
* \param abfd the bfd object from where come the samples
* \param samples_files the samples files where are stored samples
* \param cmask on what counters we work
* \param sort_by_ctr the counter number used for sort purpose
*
* Lists all the symbols in decreasing sample count order, to standard out.
*/
static void do_list_symbols(opp_bfd & abfd,
const opp_samples_files & samples_files,
size_t cmask, int sort_by_ctr)
{
samples_files_t samples(true, output_format_flags,
show_shared_libs, cmask);
samples.add(samples_files, abfd);
vector<const symbol_entry *> symbols;
samples.select_symbols(symbols, sort_by_ctr, 0.0, false);
OutputSymbol out(samples, cmask);
out.SetFlag(output_format_flags);
out.SetFlag(osf_show_all_counters);
out.Output(cout, symbols, true);
}
/**
* do_list_symbol - list detailed samples for a symbol
* \param abfd the bfd object from where come the samples
* \param samples_files the samples files where are stored samples
* \param cmask on what counters we work
*
* the global variable symbol is used to list all
* the samples for this symbol from the image
* specified by abfd.
*/
static void do_list_symbol(opp_bfd & abfd,
const opp_samples_files & samples_files,
size_t cmask)
{
int i = abfd.symbol_index(symbol);
if (i < 0) {
cerr << "oprofpp: symbol \"" << symbol
<< "\" not found in image file.\n";
return;
}
output_format_flags =
static_cast<OutSymbFlag>(output_format_flags | osf_details | osf_show_all_counters);
samples_files_t samples(true, output_format_flags,
false, cmask);
samples.add(samples_files, abfd);
const symbol_entry * symb = samples.find_symbol(abfd.syms[i]->value +
abfd.syms[i]->section->vma);
if (symb == 0) {
cerr << "oprofpp: symbol \"" << symbol
<< "\" not found in samples container file.\n";
return;
}
OutputSymbol out(samples, cmask);
out.SetFlag(output_format_flags);
out.Output(cout, symb);
}
#define GMON_VERSION 1
#define GMON_TAG_TIME_HIST 0
#define MULTIPLIER 2
struct gmon_hdr {
char cookie[4];
u32 version;
u32 spare[3];
};
/**
* do_dump_gprof - produce gprof sample output
* \param abfd the bfd object from where come the samples
* \param samples_files the samples files where are stored samples
* \param sort_by_ctr the counter number used for sort purpose
*
* Dump gprof-format samples for this sample file and
* counter specified ctr to the file specified by gproffile.
*
* this use the grpof format <= gcc 3.0
*/
static void do_dump_gprof(opp_bfd & abfd,
const opp_samples_files & samples_files,
int sort_by_ctr)
{
static gmon_hdr hdr = { { 'g', 'm', 'o', 'n' }, GMON_VERSION, {0,0,0,},};
FILE *fp;
u32 start, end;
uint i, j;
u32 low_pc = (u32)-1;
u32 high_pc = 0;
u16 * hist;
u32 histsize;
fp=op_open_file(gproffile, "w");
op_write_file(fp,&hdr, sizeof(gmon_hdr));
op_write_u8(fp, GMON_TAG_TIME_HIST);
// syms are sorted by vma so vma of the first symbol and vma + size
// of the last symbol give the vma range for gprof output
if (abfd.syms.size()) {
const asymbol * last_symb = abfd.syms[abfd.syms.size() - 1];
low_pc = abfd.syms[0]->value + abfd.syms[0]->section->vma;
high_pc = last_symb->value + last_symb->section->vma +
abfd.symbol_size(abfd.syms.size() - 1);
} else {
low_pc = 0;
high_pc = 0;
}
// FIXME : is this + 1 bogus ?
histsize = ((high_pc - low_pc) / MULTIPLIER) + 1;
op_write_u32_he(fp, low_pc);
op_write_u32_he(fp, high_pc);
/* size of histogram */
op_write_u32_he(fp, histsize);
/* profiling rate */
op_write_u32_he(fp, 1);
op_write_file(fp, "samples\0\0\0\0\0\0\0\0", 15);
/* abbreviation */
op_write_u8(fp, '1');
hist = (u16*)xcalloc(histsize, sizeof(u16));
for (i = 0; i < abfd.syms.size(); i++) {
abfd.get_symbol_range(i, start, end);
for (j = start; j < end; j++) {
u32 count;
u32 pos;
pos = (abfd.sym_offset(i, j) + abfd.syms[i]->value + abfd.syms[i]->section->vma - low_pc) / MULTIPLIER;
/* opp_get_options have set ctr to one value != -1 */
count = samples_files.samples_count(sort_by_ctr, j);
if (pos >= histsize) {
fprintf(stderr, "Bogus histogram bin %u, larger than %u !", pos, histsize);
continue;
}
if (hist[pos] + count > (u16)-1) {
printf("Warning: capping sample count by %u samples for "
"symbol \"%s\"\n", hist[pos] + count - ((u16)-1),
abfd.syms[i]->name);
hist[pos] = (u16)-1;
} else {
hist[pos] += (u16)count;
}
}
}
op_write_file(fp, hist, histsize * sizeof(u16));
op_close_file(fp);
free(hist);
}
/**
* do_list_symbols_details - list all samples for all symbols.
* \param abfd the bfd object from where come the samples
* \param samples_files the samples files where are stored samples
* \param cmask on what counters we work
* \param sort_by_ctr the counter number used for sort purpose
*
* Lists all the samples for all the symbols, from the image specified by
* abfd, in increasing order of vma, to standard out.
*/
static void do_list_symbols_details(opp_bfd & abfd,
const opp_samples_files & samples_files,
size_t cmask, int sort_by_ctr)
{
output_format_flags =
static_cast<OutSymbFlag>(output_format_flags | osf_details);
samples_files_t samples(false, output_format_flags,
show_shared_libs, cmask);
samples.add(samples_files, abfd);
vector<const symbol_entry *> symbols;
samples.select_symbols(symbols, sort_by_ctr, 0.0, false, true);
OutputSymbol out(samples, cmask);
out.SetFlag(output_format_flags);
// this flag are implicit for oprofpp -L
out.SetFlag(osf_show_all_counters);
out.Output(cout, symbols, false);
}
/**
* output_header() - output counter setup
*
* output to stdout the cpu type, cpu speed
* and all counter description available
*/
void opp_samples_files::output_header() const
{
const struct opd_header * header = first_header();
op_cpu cpu = static_cast<op_cpu>(header->cpu_type);
printf("Cpu type: %s\n", op_get_cpu_type_str(cpu));
printf("Cpu speed was (MHz estimation) : %f\n", header->cpu_speed);
for (uint i = 0 ; i < OP_MAX_COUNTERS; ++i) {
if (samples[i] != 0) {
op_print_event(cout, i, cpu, header->ctr_event,
header->ctr_um, header->ctr_count);
}
}
}
/**
* main
*/
int main(int argc, char const *argv[])
{
string image_file;
string sample_file;
int counter = 0;
opp_get_options(argc, argv, image_file, sample_file, counter);
opp_samples_files samples_files(sample_file, counter);
opp_bfd abfd(samples_files, image_file);
if (!gproffile)
samples_files.output_header();
if (list_symbols)
do_list_symbols(abfd, samples_files, counter, sort_by_counter);
else if (symbol)
do_list_symbol(abfd, samples_files, counter);
else if (gproffile)
do_dump_gprof(abfd, samples_files, sort_by_counter);
else if (list_all_symbols_details)
do_list_symbols_details(abfd, samples_files,
counter, sort_by_counter);
return 0;
}

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

Sign up for the SourceForge newsletter:





No, thanks