-
Notifications
You must be signed in to change notification settings - Fork 0
/
callgraph.cc
212 lines (169 loc) · 5.41 KB
/
callgraph.cc
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
/*
* Copyright (c) 2015-2016 SUSE Linux GmbH
*
* Licensed under the GNU General Public License Version 2
* as published by the Free Software Foundation.
*
* See http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* for details.
*
* Author: Joerg Roedel <[email protected]>
*/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
#include "callgraph.h"
#include "assembly.h"
#include "helper.h"
using result_type = std::map<std::string, std::set<std::string> >;
static void cg_from_one_function(assembly::asm_file &file,
std::string fn_name,
result_type &result,
std::set<std::string> &symbols,
const struct cg_options &opts)
{
auto fn = file.get_function(fn_name, assembly::func_flags::STRIP_DEBUG);
std::string rs_name = base_fn_name(fn_name);
if (fn == nullptr)
return;
fn->for_each_statement([&result, &rs_name, &symbols, &opts]
(assembly::asm_statement &stmt) {
if (stmt.type() != assembly::stmt_type::INSTRUCTION)
return;
auto instr = stmt.instr();
if (instr.size() < 4 || instr.substr(0, 4) != "call")
return;
// Now we have a call instruction - find the target
stmt.param(0, [&result, &rs_name, &symbols, &opts]
(const assembly::asm_param ¶m) {
if (!param.tokens()) {
std::cerr << "Error: Empty param in call instruction" << std::endl;
return;
}
param.token(0, [&result, &rs_name, &symbols, &opts]
(enum assembly::token_type type, std::string token) {
if (type != assembly::token_type::IDENTIFIER)
return;
std::string insert_token = base_fn_name(token);
if ((symbols.find(insert_token) == symbols.end()) &&
!opts.include_external)
return;
result[rs_name].insert(insert_token);
});
});
});
}
static void generate_callgraph_from_functions(std::vector<assembly::asm_file> &files,
std::vector<std::string> &functions,
std::set<std::string> &symbols,
std::map<std::string, size_t> &sym_file_map,
result_type &results,
const struct cg_options &opts)
{
std::vector<std::string> new_functions = functions;
unsigned iterations = 0;
while (new_functions.size() > 0) {
std::vector<std::string> tmp_functions;
result_type new_results;
for (size_t idx = 0, size = files.size(); idx != size; ++idx) {
files[idx].for_each_symbol([&files, &results, &new_results, &symbols,
&opts, &idx, &new_functions, &tmp_functions]
(std::string sym, assembly::asm_symbol info) {
if (info.m_type != assembly::symbol_type::FUNCTION)
return;
auto base_name = base_fn_name(sym);
auto pos = std::find(new_functions.begin(), new_functions.end(), base_name);
if (pos == new_functions.end())
return;
cg_from_one_function(files[idx], sym, new_results, symbols, opts);
});
for (auto &nr : new_results) {
results.emplace(nr.first, nr.second);
for (auto &s : nr.second) {
if (results.find(s) != results.end())
continue;
tmp_functions.emplace_back(s);
}
}
}
new_functions = std::move(tmp_functions);
if (++iterations >= opts.maxdepth)
break;
}
}
void generate_callgraph(const struct cg_options &opts)
{
const char *output_file = opts.output_file.c_str();
std::map<std::string, size_t> sym_file_map;
std::vector<assembly::asm_file> files;
std::vector<std::string> functions;
std::set<std::string> symbols;
result_type results;
std::ofstream of;
for (auto fn : opts.input_files)
files.emplace_back(fn);
for (auto &file : files)
file.load();
of.open(output_file);
for (size_t idx = 0, size = files.size(); idx != size; ++idx) {
// First fill the results with known symbols
files[idx].for_each_symbol([&results, &opts, &idx, &sym_file_map, &symbols, &functions]
(std::string sym, assembly::asm_symbol info) {
// First check if this symbol is a function
if (info.m_type != assembly::symbol_type::FUNCTION)
return;
auto base_name = base_fn_name(sym);
auto pos = std::find(opts.functions.begin(), opts.functions.end(), base_name);
if (pos != opts.functions.end())
functions.emplace_back(base_name);
symbols.emplace(base_name);
sym_file_map[base_name] = idx;
});
}
if (functions.size() > 0) {
generate_callgraph_from_functions(files, functions, symbols, sym_file_map, results, opts);
} else {
for (size_t idx = 0, size = files.size(); idx != size; ++idx) {
files[idx].for_each_symbol([&files, &results, &symbols, &opts, &idx]
(std::string sym, assembly::asm_symbol info) {
cg_from_one_function(files[idx], sym, results, symbols, opts);
});
}
}
of << "digraph {" << std::endl;
// rankdir=Lr seems to produce better results
of << "\trankdir=LR;" << std::endl;
// Print the results
std::string indent = "";
bool subgraphs = false;
if (files.size() > 1) {
indent = "\t";
subgraphs = true;
}
for (size_t idx = 0, size = files.size(); idx != size; ++idx) {
if (subgraphs) {
of << "\tsubgraph cluster_" << idx << " {" << std::endl;
of << "\t\tlabel=\"" << base_name(opts.input_files[idx]) << "\";" << std::endl;
}
for (auto &r : results) {
int num = 0;
if (sym_file_map[r.first] != idx)
continue;
of << indent << '\t' << r.first << " -> {";
for (auto &s : r.second) {
if (num++)
of << ", ";
of << s;
}
of << '}' << std::endl;
}
if (subgraphs) {
of << "\t}" << std::endl;
}
}
of << "}" << std::endl;
}