1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 A program that computes a call histogram from runtime call
8 information. It reads a list of address references (e.g., as
9 computed by libcygprof.so), and uses an ELF image to map the
10 addresses to functions.
21 #include "elf_symbol_table.h"
29 elf_symbol_table table
;
31 typedef hash_map
<unsigned int, unsigned int> histogram_t
;
32 histogram_t histogram
;
34 static struct option long_options
[] = {
35 { "exe", required_argument
, 0, 'e' },
36 { "tick", optional_argument
, 0, 't' },
41 usage(const char *name
)
43 cerr
<< "usage: " << name
<< " --exe=<image> [--tick[=count]]" << endl
;
49 // Read the binary addresses from stdin.
50 unsigned int buf
[128];
53 unsigned int count
= 0;
54 while ((cb
= read(fd
, buf
, sizeof buf
)) > 0) {
55 if (cb
% sizeof buf
[0])
56 fprintf(stderr
, "unaligned read\n");
58 unsigned int *addr
= buf
;
59 unsigned int *limit
= buf
+ (cb
/ 4);
61 for (; addr
< limit
; ++addr
) {
62 const Elf32_Sym
*sym
= table
.lookup(*addr
);
64 ++histogram
[reinterpret_cast<unsigned int>(sym
)];
66 if (opt_tick
&& (++count
% opt_tick
== 0)) {
78 main(int argc
, char *argv
[])
83 c
= getopt_long(argc
, argv
, "e:t", long_options
, &option_index
);
94 opt_tick
= optarg
? atoi(optarg
) : 1000000;
110 // Process addresses.
111 if (optind
>= argc
) {
112 map_addrs(STDIN_FILENO
);
116 int fd
= open(argv
[optind
], O_RDONLY
);
118 perror(argv
[optind
]);
124 } while (++optind
< argc
);
127 // Emit the histogram.
128 histogram_t::const_iterator limit
= histogram
.end();
129 histogram_t::const_iterator i
;
130 for (i
= histogram
.begin(); i
!= limit
; ++i
) {
131 const Elf32_Sym
*sym
= reinterpret_cast<const Elf32_Sym
*>(i
->first
);
132 cout
.form("%08x %6d %2d %10d ",
138 cout
<< table
.get_symbol_name(sym
) << endl
;