-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphlet.cpp
119 lines (100 loc) · 2.78 KB
/
graphlet.cpp
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
/*
* Generates a set of 3-graphlets describing the user-generated portion of
* the given program binary.
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <string>
#include <vector>
#include "InstructionDecoder.h"
#include "Instruction.h"
#include "CodeSource.h"
#include "CodeObject.h"
#include "Function.h"
#include "dyntypes.h"
#include "graphlet.h"
#include "colors.h"
using namespace std;
using namespace Dyninst;
using namespace Dyninst::ParseAPI;
using namespace Dyninst::InstructionAPI;
using namespace graphlets;
bool COLOR = true;
unsigned short node_insns_color(Block * A)
{
unsigned short ret = 0;
Block::Insns insns;
A->getInsns(insns);
for (auto iit = insns.begin(); iit != insns.end(); ++iit) {
InsnColor::insn_color c = InsnColor::lookup(iit->second);
if(c != InsnColor::NOCOLOR) {
assert(c <= 16);
ret |= (1 << c);
}
}
return ret;
}
unsigned short node_branch_color(Block * A)
{
using namespace Dyninst::InstructionAPI;
unsigned short ret = BranchColor::NOBRANCH;
Block::Insns insns;
A->getInsns(insns);
if (insns.empty()) return ret;
BranchColor::branch_color c = BranchColor::lookup(insns.rbegin()->second);
ret = c;
return ret;
}
bool node::operator<(node const& o) const {
if(this == &o)
return false;
if (color_ != NULL && o.color_ != NULL) {
if (color_->compact() != o.color_->compact()) {
return color_->compact() < o.color_->compact();
}
} else if (color_ != NULL && o.color_ == NULL) {
return false;
} else if (color_ == NULL && o.color_ != NULL) {
return true;
}
// Continue to compare other fields
// when 1. both nodes don't have a color;
// or 2. both nodes have the same color.
if(ins_ < o.ins_)
return true;
else if(o.ins_ < ins_)
return false;
else if(outs_ < o.outs_)
return true;
else if(o.outs_ < outs_)
return false;
else if(self_ < o.self_)
return true;
else
return false;
}
void node::print() const {
printf("{"); ins_.print(); printf(" }");
printf(" {"); outs_.print(); printf(" }");
printf(" {"); self_.print(); printf(" }");
}
std::string node::toString() const {
std::string ret;
ret += "[ {" + ins_.toString() + " }";
ret += " {" + outs_.toString() + " }";
ret += " {" + self_.toString() + " } ]";
return ret;
}
std::string node::compact(bool colors) const {
std::string ret;
ret += ins_.compact() + "/" ;
ret += outs_.compact() + "/";
ret += self_.compact();
if(colors && color_ != NULL)
ret += "/" + color_->compact();
return ret;
}