Comments (9)
There's not a single API for that, but you could generate a CSV file of the instructions in each binary and then compare the outputs.
#include <iostream>
#include <iomanip>
#include "CodeObject.h"
#include "InstructionDecoder.h"
namespace dp = Dyninst::ParseAPI;
namespace di = Dyninst::InstructionAPI;
int main(int argc, char** argv) {
if(argc != 2) {
std::cerr << "Usage: " << argv[0] << " <file>\n";
return -1;
}
auto* sts = new dp::SymtabCodeSource(argv[1]);
auto* co = new dp::CodeObject(sts);
for(auto* f : co->funcs()) {
di::InstructionDecoder decoder(
f->isrc()->getPtrToInstruction(f->addr()),
di::InstructionDecoder::maxInstructionLength,
f->region()->getArch()
);
auto insn = decoder.decode();
auto prev_size = 0UL;
while(insn.isValid()) {
auto const loc = f->addr() + prev_size;
std::cout << std::hex << "0x" << loc << "," << insn.format() << '\n';
prev_size = insn.size();
insn = decoder.decode();
}
}
}
from dyninst.
Is there any way that I could insert an instruction right behind any address instead of jumping to .dyninst section? If so, it would be much easier for me to generate such a map file.
from dyninst.
That's not possible when doing dynamic instrumentation. It might be doable for static instrumentation. Which toolkit you are using to insert the ptwrite (BPatch, ParseAPI, etc.)?
from dyninst.
BPatch bpatch;
struct instruction_t {
unsigned long address;
std::vector<unsigned char> bytes;
};
std::vector<instruction_t> tracked_instructions;
class InstructionSnippet : public Dyninst::PatchAPI::Snippet {
public:
InstructionSnippet() {}
InstructionSnippet(std::vector<unsigned char> bytes) {instruction_bytes = bytes;}
bool generate(Dyninst::PatchAPI::Point* pt, Dyninst::Buffer& buf) override {
buf.copy(instruction_bytes.data(), instruction_bytes.size());
return true;
}
private:
std::vector<unsigned char> instruction_bytes;
};
BPatch_point* FindPoint(BPatch_image* image, Dyninst::Address address) {
std::vector<BPatch_point*> points;
image->findPoints(address, points);
if (points.size() == 0) {
std::cerr << "No address found: 1.address is inside of a instruction 2.address out of bound" << std::endl;
exit(1);
}
else if (points.size() > 1) {
std::cerr << "More than one address found" << std::endl;
exit(1);
}
return points[0];
}
int main(int argc, char** argv) {
// Open the input binary
BPatch_binaryEdit* app = bpatch.openBinary(argv[1]);
if (!app) {
std::cerr << "Failed to open binary" << std::endl;
return -1;
}
BPatch_image* image = app->getImage();
for (auto instruction : tracked_instructions) {
BPatch_point* point = FindPoint(image, instruction.address);
Snippet::Ptr codeBufferSnippet = InstructionSnippet::create(new InstructionSnippet(instruction.bytes));
Point* patch_point = Dyninst::PatchAPI::convert(point, BPatch_callAfter);
patch_point->pushBack(codeBufferSnippet);
}
// Write the instrumented binary to disk
app->writeFile(argv[2]);
return 0;
}
Currently I use the code above to staticly rewrite a file, the tracked_instructions
stores the instruction address I want to insert after it and the bytes of an instruction needed to be inserted. However, I do not want to introduce the .dyninstInst
section since it makes harder for me to generate a map file. Is there any way for me to use any toolkit to achieve my goal?
from dyninst.
However, I do not want to introduce the .dyninstInst section since it makes harder for me to generate a map file.
There is no way to avoid this. If you instrument an already-instrumented binary, the Dyninst runtime library will fail. Adding a section is how a binary is "tagged" that it's been instrumented by Dyninst.
Is there any way for me to use any toolkit to achieve my goal?
I'm not sure. Let me take a look, and I will get back to you.
from dyninst.
Plus, I found that this demo can insert a ptwrite
right behind the function entry. However, when I changed the Point::FuncEntry
to an arbitrary address, things got failed. By the way, I wonder if I insert 200 ptwrite
into the binary, is there necessary 200 more ptwrite
in the rewrite file, or Dyninst may 'use' some ptwrite
twice? At last, can I just simply assume that If I insert ptwrite
in address order, in the rewritten file, ptwrite
will also appear in address order. This will be very helpful for me in generating a map file.
from dyninst.
I'm not too sure what you mean by things god failed.
If you want to insert at a specific instruction, try PreInsn or PostInsn.
Here is an example trying to insert nop before a specific instruction.
https://github.com/dyninst/examples/blob/amdgpu_insert_snippet/insertSnippet/noop_snippet.cpp
Focus on the line 66-67 for the patchMgr->findPoints call.
from dyninst.
Hello,
I apologize if my previous explanations were not clear. My primary concern is the ability to insert a specific instruction at a designated address and to generate a map file that indicates the locations of the instructions before and after insertion.
Currently, I am able to insert an instruction(the code is attached in the previous comment), but I am struggling to find an effective way to generate this map file. To elaborate, I have an input file containing addresses of instructions and the bytecode of instructions that need to be inserted after these addresses. My goal is to complete the insertion and generate a map file that looks something like 0x40116e -> 0x521163: f3 0f ae e3
. This file would serve to indicate and track the status of my insertions.
I would appreciate any guidance or suggestions on how to achieve this functionality.
Thank you for your assistance.
from dyninst.
You can run your program with DYNINST_DEBUG_RELOC=1,
which should generate some sort of trace of how blocks are being reallocated.
Then either you can parse the output file, or you add a new argument to the API to pass in a map that records/updates these information.
from dyninst.
Related Issues (20)
- The `Parser::parse_gap_heuristic(CodeRegion * cr)` function causes the Assertion `!findFuncByEntry(cr,curAddr)` failed.
- Race in Parser::set_edge_parsing_status HOT 2
- Inconsistencies in data structures produced by Dyninst HOT 2
- BPatch_funcCallExpr does not conform to ARMv8 ABI HOT 14
- aarch64 stackwalking needs work. HOT 1
- Segmentation fault on libc-2.27.so:i386
- Make Parser::getSyscallNumber architecture-agnostic and handle multiple syscall arguments
- Issues with Static Instruction Insertion using Dyninst HOT 4
- Difficulty Inserting Instructions Using Dyninst Toolkit: Seeking Unified Solution HOT 1
- GitHub Release for 13.0.0 HOT 3
- release cmake configury: unable to install under /usr/lib64/... HOT 7
- AMD GPU Instruction Decoder Failed to expand operand HOT 1
- Upgrading SelectiveTaint: Replacing dyninst9.3 with dyninst13.0 in Ubuntu 14.04 Environment HOT 13
- (Unintentional) dependency in C++ static constructor order; causes SIGFPE when source files are reordered HOT 1
- How to instrument a non-x86_64 binary with dyninst library built upon x86_64 ? HOT 2
- Substantial increase in build times with gcc-14. HOT 1
- Build failures with gcc-14 HOT 1
- strange runtime error: odd - couldn't find kept register 2 HOT 1
- Instruction Operation missing x86 FLAG read/write properties
- Just upgrde gcc version to 8.5.0, also do those soft link trick,but found issue"Dyninst requires gcc >= 6.0" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dyninst.