-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhichvars.C
154 lines (142 loc) · 4.11 KB
/
whichvars.C
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
#include <string>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <ios>
#include <dyninst/Symtab.h>
#include <dyninst/Function.h>
#include <dyninst/Variable.h>
#include <dyninst/Type.h>
#include <boost/icl/interval_map.hpp>
#include <unistd.h>
using namespace Dyninst;
using namespace SymtabAPI;
using namespace std;
using namespace boost;
using namespace boost::icl;
enum exit_codes {
EXIT_OK=0,
EXIT_ARGS=1,
EXIT_MODULE=2,
EXIT_NOFUNCS=3,
EXIT_LFUNC=4,
EXIT_LF_NOTUNIQ=5,
EXIT_GLOBALS=6
};
int main(int argc, char **argv){
//Name the object file to be parsed:
std::string file;
int opt;
bool verbose=false;
while ((opt = getopt(argc, argv, "v")) != -1) {
switch (opt) {
case 'v':
verbose=true;
break;
default:
std::cerr << "Usage:" << argv[0] << " [-g][-f][-l] name" << std::endl;
exit(EXIT_ARGS);
}
}
if(optind >= argc)
file= "./raja-perf.exe";
else
file=argv[optind];
Symtab *obj = NULL;
// Parse the object file
bool err = Symtab::openFile(obj, file);
if( err == false)
exit(EXIT_MODULE);
/*--------*/
typedef set< pair<localVar*,Function*> > LVarSet;
interval_map<Address, LVarSet> llmap;
//iterate through all the functions
vector <Function *> funcs;
if (!obj->getAllFunctions(funcs))
exit(EXIT_NOFUNCS);
for( auto i: funcs) {
if(verbose)
cout << endl << "Func: " << i->getName() << endl;
//iterate through all the local variables and parameters
vector <localVar *> lvars;
i->getParams(lvars);
i->getLocalVariables(lvars);
for(auto j: lvars) {
if(verbose) {
if( j->getName()=="this")
cout << "\tthis <" << j->getType()->getName() << ">\n";
else
cout << '\t' << j->getName() << " Defined: " << j->getFileName()
<< ':' << j->getLineNum() << endl;
}
vector<VariableLocation> &lvlocs=j->getLocationLists();
for(auto k: lvlocs) {
// this unfortunately seems to be happening. It may either be a
// problem with the DWARF or a problem with dyninst.
if( k.lowPC == 0 || k.hiPC ==0xFFFFFFFFFFFFFFFF){
cerr << "Location List for " << j->getName() << " from "
<< i->getName() << " seems insane [" << hex << k.lowPC << ','
<< k.hiPC << "]: skipping\n";
continue;
}
discrete_interval<Address> addr_inter
= construct<discrete_interval<Address> >
(k.lowPC,k.hiPC,interval_bounds::closed());
if(verbose){
cout << "\t\t[" << hex << k.lowPC << dec;
vector<Statement *> lines;
if( i->getModule()->getSourceLines(lines, k.lowPC))
for(auto l:lines)
cout << ' ' << l->getFile() << ':' << l->getLine() << 'c'
<< l->getColumn();
lines.clear();
cout << ',' << hex << k.hiPC << dec;
if( i->getModule()->getSourceLines( lines, k.hiPC))
for(auto l:lines)
cout << ' ' << l->getFile() << ':' << l->getLine() << 'c'
<< l->getColumn();
cout << ']' << endl;
}
LVarSet newone;
pair<localVar*,Function*> newpair;
newpair.first=j;
newpair.second=i;
newone.insert(newpair);
llmap.add(make_pair(addr_inter,newone));
}
}
}
for(auto i: llmap) {
cout << '[' << hex << i.first.lower() << dec << ' ';
vector<Statement *> lines;
auto funcp=i.second.begin()->second;
if( funcp->getModule()->getSourceLines(lines, i.first.lower()))
for(auto l:lines)
cout << ' ' << l->getFile() << ':' << l->getLine() << 'c'
<< l->getColumn();
if( i.first.lower() != i.first.upper()){
lines.clear();
cout << ',' << hex << i.first.upper() << dec <<
' ';
if( funcp->getModule()->getSourceLines(lines, i.first.upper()))
for(auto l:lines)
cout << ' ' << l->getFile() << ':' << l->getLine() << 'c'
<< l->getColumn();
}
cout << ']' << ": " << endl;
for( auto j: i.second) {
if( j.first->getName()=="this")
cout << "\tthis <" << "Func:" << funcp->getName() << ' '
<< j.first->getType()->getName() << ">\n";
else {
cout << '\t' << j.first->getName();
// if( !j.first->getFileName().empty())
cout << " [" << j.first->getFileName() << ':'
<< j.first->getLineNum() << ']';
cout << endl;
}
}
cout << endl;
}
}