forked from llnl/LULESH
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBacktrace.cpp
More file actions
194 lines (171 loc) · 6.35 KB
/
Copy pathBacktrace.cpp
File metadata and controls
194 lines (171 loc) · 6.35 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
#include "Backtrace.h"
#include "ContextNode.h"
#include <string>
#include <iostream>
#include <fstream>
//#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <cxxabi.h>
#include <cstdarg>
#include <algorithm>
#include <cmath>
// Assigning functions to be executed before and after main()
//void __attribute__((constructor)) bootstrap();
//void __attribute__((destructor)) finalize();
//The node for currently traced function
ContextNode* topNode;
//all the traced nodes
std::vector<ContextNode*> nodes;
static void write_to_dot();
static void print_node_info_brief(ContextNode* node) {
//std::cout << "node:location:funcName:" << node << ":" << (void*)ip <<":" << funcName << std::endl;
std::cout << node << ":" << node->getReturnAddress() <<":" << node->getFunctionName();
int num_args = node->getArguments().size();
if (num_args > 0) {
for(auto & arg: node->getArguments()) {
std::cout << arg << ",";
}
}
std::cout << std::endl;
}
void bootstrap() {
//this is used before main is called, we could just call trace_func_call, but only the following
//are useful, thus we will not call trace_func_call here
unw_context_t context;
unw_cursor_t cursor;
unw_word_t ip;
unw_getcontext(&context);
unw_init_local(&cursor, &context);
unw_step(&cursor);
unw_get_reg(&cursor, UNW_REG_IP, &ip); //The address/location of the call
topNode = new ContextNode("main", (void*) ip);
topNode->addCallCount(1);
topNode->setParentNode(nullptr);
//print_node_info_brief(topNode);
nodes.push_back(topNode);
}
void finalize() {
trace_end(); //to finish main function
write_to_dot();
}
/**
* ... is a pair of formal_parameter:actual_parameter
*
*
*/
void trace_func_call(std::string funcName, int num_args, ...) {
unw_context_t context;
unw_cursor_t cursor;
unw_word_t ip;
unw_getcontext(&context);
unw_init_local(&cursor, &context);
unw_step(&cursor);
unw_get_reg(&cursor, UNW_REG_IP, &ip); //The address/location of the call
//search using ip the children nodes of the current topNode to see whether this function is called before at this location
//if found, increae only call count. If not found, this is the first time, create the node and add as child node
for (auto & node : topNode->getChildren()) {
if (node->getReturnAddress() == (void*)ip) {
node->addCallCount(1);
topNode = node;
return;
}
}
ContextNode* node = new ContextNode(funcName, (void*)ip);
//store the argument information
node->setParallelBlock(false);
char* value;
va_list argp;
va_start(argp, num_args);
int i;
for (i=0; i<num_args; i++) {
value = va_arg(argp, char*);
node->addArgument(value);
}
va_end(argp);
node->addCallCount(1);
node->setParentNode(topNode);
nodes.push_back(node);
//print_node_info_brief(node);
topNode->addChild(node);
topNode = node;
}
void trace_parallel_block(std::string funcName, int num_args, ...) {
unw_context_t context;
unw_cursor_t cursor;
unw_word_t ip;
unw_getcontext(&context);
unw_init_local(&cursor, &context);
unw_step(&cursor);
unw_get_reg(&cursor, UNW_REG_IP, &ip); //The address/location of the call
//search using ip the children nodes of the current topNode to see whether this function is called before at this location
//if found, increae only call count. If not found, this is the first time, create the node and add as child node
for (auto & node : topNode->getChildren()) {
if (node->getReturnAddress() == (void*)ip) {
node->addCallCount(1);
topNode = node;
return;
}
}
ContextNode* node = new ContextNode(funcName, (void*)ip);
//store the argument information
node->setParallelBlock(true);
char* value;
va_list argp;
va_start(argp, num_args);
int i;
for (i=0; i<num_args; i++) {
value = va_arg(argp, char*);
node->addArgument(value);
}
va_end(argp);
node->addCallCount(1);
node->setParentNode(topNode);
nodes.push_back(node);
//print_node_info_brief(node);
topNode->addChild(node);
topNode = node;
}
void trace_end() {
topNode = topNode->getParentNode();
}
//dump the CCT tree to dot files
void write_to_dot() {
std::ofstream dotFileWrite("tree.dot");
dotFileWrite << "digraph ContextTree {" << std::endl;
dotFileWrite << "rankdir=\"LR\"" << std::endl;
dotFileWrite << "node [style=\"filled\", fontname=\"Times-Roman\", fontsize=12, fillcolor=lightblue2, fontcolor=\"#000000\"];" << std::endl;
dotFileWrite << "edge [color=black, fontname=\"Times-Roman\", fontsize=10];" << std::endl;
for (auto & node : nodes) {
//print_node_info_brief(node);
if(node->getParallelBlock()) {
dotFileWrite << node->getFunctionName() << node <<
"[label=\"" << node->getFunctionName() <<
"\\n Call Location: 0x" << std::hex << node->getReturnAddress() <<
"\", fillcolor=\"#90EE90\", style=filled, fontcolor=\"#770000\"];" << std::endl;
} else {
dotFileWrite << node->getFunctionName() << node <<
"[label=\"" << node->getFunctionName() <<
"\\n Call Location: 0x" << std::hex << node->getReturnAddress() <<
"\"];" << std::endl;
}
for (auto &child : node->getChildren()) {
if (child->getArguments().size() == 0) {
dotFileWrite << node->getFunctionName() << node << " -> " <<
child->getFunctionName() << child << "[label=\" ";
dotFileWrite << std::to_string(child->getCallCount()) << "x";
dotFileWrite << "\", color=\"" << "black" << "\"];" << std::endl;
} else {
int i = 0;
for(auto & param: child->getArguments()) {
dotFileWrite << node->getFunctionName() << node << " -> " <<
child->getFunctionName() << child << "[label=\" ";
dotFileWrite << param << "," << std::to_string(child->getCallCount()) << "x";
dotFileWrite << "\", color=\"" << "black" << "\"];" << std::endl;
}
}
}
}
dotFileWrite << "}" << std::endl;
dotFileWrite.close();
}
//memory cleanup for unused nodes