|
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/. */ |
|
4 |
|
5 /* |
|
6 |
|
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. |
|
11 |
|
12 */ |
|
13 |
|
14 #include <fstream> |
|
15 #include <hash_map> |
|
16 #include <limits.h> |
|
17 #include <unistd.h> |
|
18 #include <stdio.h> |
|
19 #include <fcntl.h> |
|
20 |
|
21 #include "elf_symbol_table.h" |
|
22 |
|
23 #define _GNU_SOURCE |
|
24 #include <getopt.h> |
|
25 |
|
26 const char *opt_exe; |
|
27 int opt_tick = 0; |
|
28 |
|
29 elf_symbol_table table; |
|
30 |
|
31 typedef hash_map<unsigned int, unsigned int> histogram_t; |
|
32 histogram_t histogram; |
|
33 |
|
34 static struct option long_options[] = { |
|
35 { "exe", required_argument, 0, 'e' }, |
|
36 { "tick", optional_argument, 0, 't' }, |
|
37 { 0, 0, 0, 0 } |
|
38 }; |
|
39 |
|
40 static void |
|
41 usage(const char *name) |
|
42 { |
|
43 cerr << "usage: " << name << " --exe=<image> [--tick[=count]]" << endl; |
|
44 } |
|
45 |
|
46 static void |
|
47 map_addrs(int fd) |
|
48 { |
|
49 // Read the binary addresses from stdin. |
|
50 unsigned int buf[128]; |
|
51 ssize_t cb; |
|
52 |
|
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"); |
|
57 |
|
58 unsigned int *addr = buf; |
|
59 unsigned int *limit = buf + (cb / 4); |
|
60 |
|
61 for (; addr < limit; ++addr) { |
|
62 const Elf32_Sym *sym = table.lookup(*addr); |
|
63 if (sym) |
|
64 ++histogram[reinterpret_cast<unsigned int>(sym)]; |
|
65 |
|
66 if (opt_tick && (++count % opt_tick == 0)) { |
|
67 cerr << "."; |
|
68 flush(cerr); |
|
69 } |
|
70 } |
|
71 } |
|
72 |
|
73 if (opt_tick) |
|
74 cerr << endl; |
|
75 } |
|
76 |
|
77 int |
|
78 main(int argc, char *argv[]) |
|
79 { |
|
80 int c; |
|
81 while (1) { |
|
82 int option_index = 0; |
|
83 c = getopt_long(argc, argv, "e:t", long_options, &option_index); |
|
84 |
|
85 if (c < 0) |
|
86 break; |
|
87 |
|
88 switch (c) { |
|
89 case 'e': |
|
90 opt_exe = optarg; |
|
91 break; |
|
92 |
|
93 case 't': |
|
94 opt_tick = optarg ? atoi(optarg) : 1000000; |
|
95 break; |
|
96 |
|
97 default: |
|
98 usage(argv[0]); |
|
99 return 1; |
|
100 } |
|
101 } |
|
102 |
|
103 if (! opt_exe) { |
|
104 usage(argv[0]); |
|
105 return 1; |
|
106 } |
|
107 |
|
108 table.init(opt_exe); |
|
109 |
|
110 // Process addresses. |
|
111 if (optind >= argc) { |
|
112 map_addrs(STDIN_FILENO); |
|
113 } |
|
114 else { |
|
115 do { |
|
116 int fd = open(argv[optind], O_RDONLY); |
|
117 if (fd < 0) { |
|
118 perror(argv[optind]); |
|
119 return 1; |
|
120 } |
|
121 |
|
122 map_addrs(fd); |
|
123 close(fd); |
|
124 } while (++optind < argc); |
|
125 } |
|
126 |
|
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 ", |
|
133 sym->st_value, |
|
134 sym->st_size, |
|
135 sym->st_shndx, |
|
136 i->second); |
|
137 |
|
138 cout << table.get_symbol_name(sym) << endl; |
|
139 } |
|
140 |
|
141 return 0; |
|
142 } |