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

Download this file

opp_symbol.cpp    470 lines (389 with data), 12.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
/* COPYRIGHT (C) 2002 Philippe Elie
* 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.
*
* first write by P.Elie.
*/
#include <sstream>
#include <iomanip>
#include "file_manip.h"
#include "opp_symbol.h"
#include "opf_filter.h"
using std::string;
using std::cout;
using std::cerr;
using std::vector;
using std::ostream;
using std::ostringstream;
using std::endl;
struct output_option {
char option;
OutSymbFlag flag;
const char * help_string;
};
static const output_option output_options[] = {
{ 'v', osf_vma, "vma offset" },
{ 's', osf_nr_samples, "nr samples" },
{ 'S', osf_nr_samples_cumulated, "nr cumulated samples" },
{ 'p', osf_percent, "nr percent samples" },
{ 'P', osf_percent_cumulated, "nr cumulated percent samples" },
// { 't', osf_time, "time" },
// { 'T', ofs_time_cumulated, "cumulated time" },
{ 'n', osf_symb_name, "symbol name" },
// { 'd', osf_demangle, "demangle symbol" },
{ 'l', osf_linenr_info, "source file name and line nr" },
{ 'L', osf_short_linenr_info, "base name of source file and line nr" },
{ 'i', osf_image_name, "image name" },
{ 'I', osf_short_image_name, "base name of image name" },
{ 'h', osf_header, "header" },
{ 'd', osf_details, "detailed samples for each selected symbol" }
};
const size_t nr_output_option = sizeof(output_options) / sizeof(output_options[0]);
static const output_option * find_option(char ch)
{
for (size_t i = 0 ; i < nr_output_option ; ++i) {
if (ch == output_options[i].option) {
return &output_options[i];
}
}
return 0;
}
OutSymbFlag OutputSymbol::ParseOutputOption(const string & option)
{
size_t flag = 0;
for (size_t i = 0 ; i < option.length() ; ++i) {
const output_option * opt = find_option(option[i]);
if (!opt)
return osf_none;
flag |= opt->flag;
}
return static_cast<OutSymbFlag>(flag);
}
void OutputSymbol::ShowHelp()
{
for (size_t i = 0 ; i < nr_output_option ; ++i) {
cerr << output_options[i].option << "\t"
<< output_options[i].help_string << endl;
}
}
typedef string (OutputSymbol::*fct_format)(const string & symb_name,
const sample_entry & symb,
size_t ctr);
/// decribe one field of the colummned output.
struct field_description {
OutSymbFlag flag;
size_t width;
const char * header_name;
fct_format formater;
};
/// describe each possible field of colummned output.
// FIXME: some field have header_name too long (> field_description::width)
// TODO: use % of the screen width here. sum of % equal to 100, then calculate
// ratio between 100 and the selected % to grow non fixed field use also
// lib[n?]curses to get the console width (look info source) (so on add a fixed
// field flags)
static const field_description field_descr[] = {
{ osf_vma, 9,
"vma", &OutputSymbol::format_vma },
{ osf_nr_samples, 9,
"samples", &OutputSymbol::format_nr_samples },
{ osf_nr_samples_cumulated, 9,
"cum. samples", &OutputSymbol::format_nr_cumulated_samples },
{ osf_percent, 12,
"%-age", &OutputSymbol::format_percent },
{ osf_percent_cumulated, 10,
"cum %-age", &OutputSymbol::format_cumulated_percent },
// future { osf_time, 12, "time", 0 },
// future { osf_time_cumulated, 12, "cumul time", 0 },
{ osf_symb_name, 24,
"symbol name", &OutputSymbol::format_symb_name },
{ osf_linenr_info, 28, "linenr info",
&OutputSymbol::format_linenr_info },
{ osf_short_linenr_info, 20, "linenr info",
&OutputSymbol::format_short_linenr_info },
{ osf_image_name, 24, "image name",
&OutputSymbol::format_image_name },
{ osf_short_image_name, 16, "image name",
&OutputSymbol::format_short_image_name },
};
const size_t nr_field_descr = sizeof(field_descr) / sizeof(field_descr[0]);
OutputSymbol::OutputSymbol(const samples_files_t & samples_files_,
int counter_)
:
flags(osf_none),
samples_files(samples_files_),
counter(counter_),
first_output(true)
{
for (size_t i = 0 ; i < samples_files.get_nr_counters() ; ++i) {
total_count[i] = samples_files.samples_count(i);
cumulated_samples[i] = 0;
cumulated_percent[i] = 0;
}
}
const field_description * OutputSymbol::GetFieldDescr(OutSymbFlag flag)
{
for (size_t i = 0 ; i < nr_field_descr ; ++i) {
if (flag == field_descr[i].flag)
return &field_descr[i];
}
return 0;
}
void OutputSymbol::SetFlag(OutSymbFlag flag)
{
flags = static_cast<OutSymbFlag>(flags | flag);
}
size_t OutputSymbol::OutputField(std::ostream & out, const std::string & name,
const sample_entry & sample,
OutSymbFlag fl, size_t ctr)
{
size_t sz = 0;
const field_description * field = GetFieldDescr(fl);
if (field) {
string str = (this->*field->formater)(name, sample, ctr);
out << str;
sz = 1; // at least one separator char
if (str.length() < field->width)
sz = field->width - str.length();
}
return sz;
}
size_t OutputSymbol::OutputHeaderField(std::ostream & out, OutSymbFlag fl)
{
size_t sz = 0;
const field_description * field = GetFieldDescr(fl);
if (field) {
out << field->header_name;
sz = 1; // at least one separator char
if (strlen(field->header_name) < field->width)
sz = field->width - strlen(field->header_name);
}
return sz;
}
void OutputSymbol::Output(std::ostream & out, const symbol_entry * symb)
{
DoOutput(out, symb->name, symb->sample, flags);
if (flags & osf_details) {
OutputDetails(out, symb);
}
}
void OutputSymbol::OutputDetails(std::ostream & out, const symbol_entry * symb)
{
// We need to save the accumulated count and to restore it on
// exit so global cumulation and detailed cumulation are separate
u32 temp_total_count[OP_MAX_COUNTERS];
u32 temp_cumulated_samples[OP_MAX_COUNTERS];
u32 temp_cumulated_percent[OP_MAX_COUNTERS];
for (size_t i = 0 ; i < samples_files.get_nr_counters() ; ++i) {
temp_total_count[i] = total_count[i];
temp_cumulated_samples[i] = cumulated_samples[i];
temp_cumulated_percent[i] = cumulated_percent[i];
total_count[i] = symb->sample.counter[i];
cumulated_samples[i] = 0;
cumulated_percent[i] = 0;
}
for (size_t cur = symb->first ; cur != symb->last ; ++cur) {
out << ' ';
DoOutput(out, symb->name, samples_files.get_samples(cur),
static_cast<OutSymbFlag>(flags & osf_details_mask));
}
for (size_t i = 0 ; i < samples_files.get_nr_counters() ; ++i) {
total_count[i] = temp_total_count[i];
cumulated_samples[i] = temp_cumulated_samples[i];
cumulated_percent[i] = temp_cumulated_percent[i];
}
}
void OutputSymbol::DoOutput(std::ostream & out, const std::string & name,
const sample_entry & sample, OutSymbFlag flag)
{
OutputHeader(out);
size_t last_field_pad = 0;
// first output the vma field
if (flag & osf_vma) {
last_field_pad = OutputField(out, name, sample, osf_vma, 0);
}
// now the repeated field.
for (size_t ctr = 0 ; ctr < samples_files.get_nr_counters(); ++ctr) {
if ((counter & (1 << ctr)) != 0) {
size_t repeated_flag = (flag & osf_repeat_mask);
for (size_t i = 0 ; repeated_flag != 0 ; ++i) {
if ((repeated_flag & (1 << i)) != 0) {
out << string(last_field_pad, ' ');
OutSymbFlag fl =
static_cast<OutSymbFlag>(1 << i);
last_field_pad = OutputField(out, name,
sample,
fl, ctr);
repeated_flag &= ~(1 << i);
}
}
}
}
// now the remaining field
size_t temp_flag = flag & ~(osf_vma|osf_repeat_mask|osf_details|osf_show_all_counters|osf_header);
for (size_t i = 0 ; temp_flag != 0 ; ++i) {
if ((temp_flag & (1 << i)) != 0) {
out << string(last_field_pad, ' ');
OutSymbFlag fl = static_cast<OutSymbFlag>(1 << i);
last_field_pad = OutputField(out, name, sample, fl, 0);
temp_flag &= ~(1 << i);
}
}
out << "\n";
}
void OutputSymbol::OutputHeader(std::ostream & out)
{
if (first_output == false) {
return;
}
first_output = false;
if ((flags & osf_header) == 0) {
return;
}
size_t last_field_pad = 0;
// first output the vma field
if (flags & osf_vma) {
last_field_pad = OutputHeaderField(out, osf_vma);
}
// now the repeated field.
for (size_t ctr = 0 ; ctr < samples_files.get_nr_counters(); ++ctr) {
if ((counter & (1 << ctr)) != 0) {
size_t repeated_flag = (flags & osf_repeat_mask);
for (size_t i = 0 ; repeated_flag != 0 ; ++i) {
if ((repeated_flag & (1 << i)) != 0) {
out << string(last_field_pad, ' ');
OutSymbFlag fl =
static_cast<OutSymbFlag>(1 << i);
last_field_pad =
OutputHeaderField(out, fl);
repeated_flag &= ~(1 << i);
}
}
}
}
// now the remaining field
size_t temp_flag = flags & ~(osf_vma|osf_repeat_mask|osf_details|osf_show_all_counters|osf_header);
for (size_t i = 0 ; temp_flag != 0 ; ++i) {
if ((temp_flag & (1 << i)) != 0) {
out << string(last_field_pad, ' ');
OutSymbFlag fl = static_cast<OutSymbFlag>(1 << i);
last_field_pad = OutputHeaderField(out, fl);
temp_flag &= ~(1 << i);
}
}
out << "\n";
}
void OutputSymbol::Output(std::ostream & out,
const std::vector<const symbol_entry *> & symbols,
bool reverse)
{
if (reverse) {
vector<const symbol_entry*>::const_reverse_iterator it;
for (it = symbols.rbegin(); it != symbols.rend(); ++it) {
Output(out, *it);
}
} else {
vector<const symbol_entry*>::const_iterator it;
for (it = symbols.begin(); it != symbols.end(); ++it) {
Output(out, *it);
}
}
}
string OutputSymbol::format_vma(const std::string &,
const sample_entry & sample, size_t)
{
ostringstream out;
out << std::hex << std::setw(8) << std::setfill('0') << sample.vma;
return out.str();
}
string OutputSymbol::format_symb_name(const std::string & name,
const sample_entry &, size_t)
{
int const is_anon = name[0] == '?';
return is_anon ? string("(no symbol)") : name;
}
string OutputSymbol::format_image_name(const std::string &,
const sample_entry & sample, size_t)
{
return sample.file_loc.image_name;
}
string OutputSymbol::format_short_image_name(const std::string &,
const sample_entry & sample,
size_t)
{
return basename(sample.file_loc.image_name);
}
string OutputSymbol::format_linenr_info(const std::string &,
const sample_entry & sample, size_t)
{
ostringstream out;
if (sample.file_loc.filename.length()) {
out << sample.file_loc.filename << ":"
<< sample.file_loc.linenr;
} else {
out << "(no location information)";
}
return out.str();
}
string OutputSymbol::format_short_linenr_info(const std::string &,
const sample_entry & sample,
size_t)
{
ostringstream out;
if (sample.file_loc.filename.length()) {
out << basename(sample.file_loc.filename)
<< ":" << sample.file_loc.linenr;
} else {
out << "(no location information)";
}
return out.str();
}
string OutputSymbol::format_nr_samples(const std::string &,
const sample_entry & sample, size_t ctr)
{
ostringstream out;
out << sample.counter[ctr];
return out.str();
}
string OutputSymbol::format_nr_cumulated_samples(const std::string &,
const sample_entry & sample,
size_t ctr)
{
ostringstream out;
cumulated_samples[ctr] += sample.counter[ctr];
out << cumulated_samples[ctr];
return out.str();
}
string OutputSymbol::format_percent(const std::string &,
const sample_entry & sample, size_t ctr)
{
ostringstream out;
double ratio = total_count[ctr]
? double(sample.counter[ctr]) / total_count[ctr]
: 0.0;
out << ratio * 100.0;
return out.str();
}
string OutputSymbol::format_cumulated_percent(const std::string &,
const sample_entry & sample,
size_t ctr)
{
ostringstream out;
cumulated_percent[ctr] += sample.counter[ctr];
double ratio = total_count[ctr]
? double(cumulated_percent[ctr]) / total_count[ctr]
: 0.0;
out << ratio * 100.0;
return out.str();
}