Skip to content

Commit 256ab86

Browse files
authored
disassemble: include all blocks per function (#64)
Dyninst is a recursive disassembler. It supports non-continues functions. To get all instructions of the functions we need to iterate over its basic blocks and disassemble each of them.
1 parent 5beaafe commit 256ab86

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

disassemble/disassemble.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,46 +58,46 @@ int main(int argc, char** argv) {
5858
InstructionDecoder decoder((const void *)nullptr, 1, sts->getArch());
5959
for(auto fit = all.begin(); fit != all.end(); ++fit) {
6060
Function* f = *fit;
61-
// get address of entry point for current function
62-
Address crtAddr = f->addr();
6361
int instr_count = 0;
64-
auto fbl = f->blocks().end();
65-
fbl--;
66-
Block* b = *fbl;
67-
Address lastAddr = b->end();
68-
// if current function has zero instructions, d o n t output it
69-
if(crtAddr == lastAddr)
62+
// if current function has zero basic blocks, d o n t output it
63+
if(f->blocks().empty())
7064
continue;
7165
cout << "\n\n" << hex << setfill('0') << setw(2 * sts->getAddressWidth()) << f->addr() << " <" << f->name() << ">:\n";
72-
while(crtAddr < lastAddr) {
73-
// decode current instruction
74-
const unsigned char *instr_ptr = (const unsigned char *)f->isrc()->getPtrToInstruction(crtAddr);
75-
instr = decoder.decode(instr_ptr);
66+
auto fbl = f->blocks().end();
67+
fbl--;
68+
for (Block *b : f->blocks()) {
69+
Address crtAddr = b->start();
70+
Address lastAddr = b->end();
71+
while(crtAddr < lastAddr) {
72+
// decode current instruction
73+
const unsigned char *instr_ptr = (const unsigned char *)f->isrc()->getPtrToInstruction(crtAddr);
74+
instr = decoder.decode(instr_ptr);
7675

77-
// failed to decode the instruction
78-
if (instr.size() == 0)
79-
break;
76+
// failed to decode the instruction
77+
if (instr.size() == 0)
78+
break;
8079

81-
// pretty print it
82-
cout << hex << setfill(' ') << setw(8) << crtAddr << ": ";
83-
for (size_t i = 0; i < instr.size() && i < l1_width; i++) {
84-
cout << hex << setfill('0') << setw(2) << (unsigned)instr_ptr[i] << " ";
85-
}
86-
for (size_t i = min(instr.size(), (size_t)l1_width); i < 8; i++) {
87-
cout << " ";
88-
}
89-
cout << instr.format() << "\n";
90-
if (instr.size() > l1_width) {
91-
cout << hex << setfill(' ') << setw(8) << crtAddr + l1_width << ": ";
92-
for (size_t i = l1_width; i < instr.size(); i++) {
80+
// pretty print it
81+
cout << hex << setfill(' ') << setw(8) << crtAddr << ": ";
82+
for (size_t i = 0; i < instr.size() && i < l1_width; i++) {
9383
cout << hex << setfill('0') << setw(2) << (unsigned)instr_ptr[i] << " ";
9484
}
95-
cout << "\n";
96-
}
85+
for (size_t i = min(instr.size(), (size_t)l1_width); i < 8; i++) {
86+
cout << " ";
87+
}
88+
cout << instr.format() << "\n";
89+
if (instr.size() > l1_width) {
90+
cout << hex << setfill(' ') << setw(8) << crtAddr + l1_width << ": ";
91+
for (size_t i = l1_width; i < instr.size(); i++) {
92+
cout << hex << setfill('0') << setw(2) << (unsigned)instr_ptr[i] << " ";
93+
}
94+
cout << "\n";
95+
}
9796

98-
// go to the address of the next instruction
99-
crtAddr += instr.size();
100-
instr_count++;
97+
// go to the address of the next instruction
98+
crtAddr += instr.size();
99+
instr_count++;
100+
}
101101
}
102102
}
103103
return 0;

0 commit comments

Comments
 (0)