plugin/tracing: merge full-tracing plugin into generic version
The full-tracing plugin was used in the DSN paper. It additionally traces the data that was accessed/written on a memory access and the contents of some CPU registers. Change-Id: I61f5230699009ce523aba341985b98148160556d
This commit is contained in:
committed by
Gerrit Code Review
parent
cffafa411c
commit
4e8098a636
@ -21,200 +21,210 @@ using namespace std;
|
||||
using namespace fail;
|
||||
|
||||
void GenericTracing::parseOptions() {
|
||||
CommandLine &cmd = CommandLine::Inst();
|
||||
CommandLine::option_handle IGNORE = cmd.addOption("", "", Arg::None, "USAGE: fail-client -Wf,[option] -Wf,[option] ... <BochsOptions...>\n\n");
|
||||
CommandLine::option_handle HELP = cmd.addOption("h", "help", Arg::None, "-h,--help\t Print usage and exit");
|
||||
CommandLine::option_handle ELF_FILE = cmd.addOption("", "elf-file", Arg::Required,
|
||||
"--elf-file\t ELF Binary File (default: $FAIL_ELF_PATH)");
|
||||
CommandLine::option_handle START_SYMBOL = cmd.addOption("s", "start-symbol", Arg::Required,
|
||||
"-s,--start-symbol\t ELF symbol to start tracing (default: main)");
|
||||
CommandLine::option_handle STOP_SYMBOL = cmd.addOption("e", "end-symbol", Arg::Required,
|
||||
"-e,--end-symbol\t ELF symbol to end tracing");
|
||||
CommandLine::option_handle SAVE_SYMBOL = cmd.addOption("S", "save-symbol", Arg::Required,
|
||||
"-S,--save-symbol\t ELF symbol to save the state of the machine (default: main)\n");
|
||||
CommandLine::option_handle STATE_FILE = cmd.addOption("f", "state-file", Arg::Required,
|
||||
"-f,--state-file\t File/dir to save the state to (default state)");
|
||||
CommandLine::option_handle TRACE_FILE = cmd.addOption("t", "trace-file", Arg::Required,
|
||||
"-t,--trace-file\t File to save the execution trace to\n");
|
||||
CommandLine &cmd = CommandLine::Inst();
|
||||
CommandLine::option_handle IGNORE = cmd.addOption("", "", Arg::None, "USAGE: fail-client -Wf,[option] -Wf,[option] ... <BochsOptions...>\n\n");
|
||||
CommandLine::option_handle HELP = cmd.addOption("h", "help", Arg::None, "-h,--help\t Print usage and exit");
|
||||
|
||||
CommandLine::option_handle MEM_SYMBOL = cmd.addOption("m", "memory-symbol", Arg::Required,
|
||||
"-m,--memory-symbol\t ELF symbol(s) to trace accesses (without specifiying all mem read/writes are traced)");
|
||||
CommandLine::option_handle MEM_REGION = cmd.addOption("M", "memory-region", Arg::Required,
|
||||
"-M,--memory-region\t restrict memory region which is traced"
|
||||
" Possible formats: 0x<address>, 0x<address>:0x<address>, 0x<address>:<length>");
|
||||
|
||||
if(!cmd.parse()) {
|
||||
cerr << "Error parsing arguments." << endl;
|
||||
exit(-1);
|
||||
}
|
||||
CommandLine::option_handle ELF_FILE = cmd.addOption("", "elf-file", Arg::Required,
|
||||
"--elf-file\t ELF Binary File (default: $FAIL_ELF_PATH)");
|
||||
CommandLine::option_handle START_SYMBOL = cmd.addOption("s", "start-symbol", Arg::Required,
|
||||
"-s,--start-symbol\t ELF symbol to start tracing (default: main)");
|
||||
CommandLine::option_handle STOP_SYMBOL = cmd.addOption("e", "end-symbol", Arg::Required,
|
||||
"-e,--end-symbol\t ELF symbol to end tracing");
|
||||
CommandLine::option_handle SAVE_SYMBOL = cmd.addOption("S", "save-symbol", Arg::Required,
|
||||
"-S,--save-symbol\t ELF symbol to save the state of the machine (default: main)\n");
|
||||
CommandLine::option_handle STATE_FILE = cmd.addOption("f", "state-file", Arg::Required,
|
||||
"-f,--state-file\t File/dir to save the state to (default state)");
|
||||
CommandLine::option_handle TRACE_FILE = cmd.addOption("t", "trace-file", Arg::Required,
|
||||
"-t,--trace-file\t File to save the execution trace to\n");
|
||||
|
||||
if (cmd[HELP]) {
|
||||
cmd.printUsage();
|
||||
exit(0);
|
||||
}
|
||||
CommandLine::option_handle FULL_TRACE = cmd.addOption("", "full-trace", Arg::None, "--full-trace\t Do a full trace (more data, default: off)");
|
||||
CommandLine::option_handle MEM_SYMBOL = cmd.addOption("m", "memory-symbol", Arg::Required,
|
||||
"-m,--memory-symbol\t ELF symbol(s) to trace accesses (without specifiying all mem read/writes are traced)");
|
||||
CommandLine::option_handle MEM_REGION = cmd.addOption("M", "memory-region", Arg::Required,
|
||||
"-M,--memory-region\t restrict memory region which is traced"
|
||||
" Possible formats: 0x<address>, 0x<address>:0x<address>, 0x<address>:<length>");
|
||||
|
||||
if (cmd[ELF_FILE].count() > 0)
|
||||
elf_file = cmd[ELF_FILE].first()->arg;
|
||||
else {
|
||||
char * elfpath = getenv("FAIL_ELF_PATH");
|
||||
if(elfpath == NULL){
|
||||
m_log << "FAIL_ELF_PATH not set :( (alternative: --elf-file) " << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
if(!cmd.parse()) {
|
||||
cerr << "Error parsing arguments." << endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
elf_file = elfpath;
|
||||
}
|
||||
m_elf = new ElfReader(elf_file.c_str());
|
||||
if (cmd[HELP]) {
|
||||
cmd.printUsage();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (cmd[START_SYMBOL].count() > 0)
|
||||
start_symbol = cmd[START_SYMBOL].first()->arg;
|
||||
else
|
||||
start_symbol = "main";
|
||||
if (cmd[ELF_FILE].count() > 0)
|
||||
elf_file = cmd[ELF_FILE].first()->arg;
|
||||
else {
|
||||
char * elfpath = getenv("FAIL_ELF_PATH");
|
||||
if(elfpath == NULL){
|
||||
m_log << "FAIL_ELF_PATH not set :( (alternative: --elf-file) " << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (cmd[STOP_SYMBOL].count() > 0)
|
||||
stop_symbol = std::string(cmd[STOP_SYMBOL].first()->arg);
|
||||
else {
|
||||
m_log << "You have to give an end symbol (-e,--end-symbol)!" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
elf_file = elfpath;
|
||||
}
|
||||
m_elf = new ElfReader(elf_file.c_str());
|
||||
|
||||
if (cmd[SAVE_SYMBOL].count() > 0)
|
||||
save_symbol = std::string(cmd[SAVE_SYMBOL].first()->arg);
|
||||
else
|
||||
save_symbol = "main";
|
||||
if (cmd[START_SYMBOL].count() > 0)
|
||||
start_symbol = cmd[START_SYMBOL].first()->arg;
|
||||
else
|
||||
start_symbol = "main";
|
||||
|
||||
if (cmd[STATE_FILE].count() > 0)
|
||||
state_file = std::string(cmd[STATE_FILE].first()->arg);
|
||||
else
|
||||
state_file = "state";
|
||||
if (cmd[STOP_SYMBOL].count() > 0)
|
||||
stop_symbol = std::string(cmd[STOP_SYMBOL].first()->arg);
|
||||
else {
|
||||
m_log << "You have to give an end symbol (-e,--end-symbol)!" << std::endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (cmd[TRACE_FILE].count() > 0)
|
||||
trace_file = std::string(cmd[TRACE_FILE].first()->arg);
|
||||
else
|
||||
trace_file = "trace.pb";
|
||||
if (cmd[SAVE_SYMBOL].count() > 0)
|
||||
save_symbol = std::string(cmd[SAVE_SYMBOL].first()->arg);
|
||||
else
|
||||
save_symbol = "main";
|
||||
|
||||
use_memory_map = false;
|
||||
if (cmd[STATE_FILE].count() > 0)
|
||||
state_file = std::string(cmd[STATE_FILE].first()->arg);
|
||||
else
|
||||
state_file = "state";
|
||||
|
||||
if (cmd[MEM_SYMBOL].count() > 0) {
|
||||
use_memory_map = true;
|
||||
option::Option *opt = cmd[MEM_SYMBOL].first();
|
||||
if (cmd[TRACE_FILE].count() > 0)
|
||||
trace_file = std::string(cmd[TRACE_FILE].first()->arg);
|
||||
else
|
||||
trace_file = "trace.pb";
|
||||
|
||||
while(opt != 0) {
|
||||
const ElfSymbol &symbol = m_elf->getSymbol(opt->arg);
|
||||
assert(symbol.isValid());
|
||||
use_memory_map = false;
|
||||
|
||||
m_log << "Adding '" << opt->arg << "' == 0x" << std::hex << symbol.getAddress()
|
||||
<< "+" << std::dec << symbol.getSize() << " to trace map" << std::endl;
|
||||
traced_memory_map.add(symbol.getAddress(), symbol.getSize());
|
||||
if (cmd[MEM_SYMBOL].count() > 0) {
|
||||
use_memory_map = true;
|
||||
option::Option *opt = cmd[MEM_SYMBOL].first();
|
||||
|
||||
opt = opt->next();
|
||||
}
|
||||
}
|
||||
while(opt != 0) {
|
||||
const ElfSymbol &symbol = m_elf->getSymbol(opt->arg);
|
||||
assert(symbol.isValid());
|
||||
|
||||
if (cmd[MEM_REGION].count() > 0) {
|
||||
use_memory_map = true;
|
||||
option::Option *opt = cmd[MEM_REGION].first();
|
||||
m_log << "Adding '" << opt->arg << "' == 0x" << std::hex << symbol.getAddress()
|
||||
<< "+" << std::dec << symbol.getSize() << " to trace map" << std::endl;
|
||||
traced_memory_map.add(symbol.getAddress(), symbol.getSize());
|
||||
|
||||
while(opt != 0) {
|
||||
char *endptr;
|
||||
guest_address_t begin = strtol(opt->arg, &endptr, 16);
|
||||
guest_address_t size;
|
||||
if (endptr == opt->arg) {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
opt = opt->next();
|
||||
}
|
||||
}
|
||||
|
||||
char delim = *endptr;
|
||||
if (delim == 0) {
|
||||
size = 1;
|
||||
} else if(delim == ':') {
|
||||
char *p = endptr +1;
|
||||
size = strtol(p, &endptr, 16) - begin;
|
||||
if (p == endptr || *endptr != 0) {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
} else if(delim == '+') {
|
||||
char *p = endptr +1;
|
||||
size = strtol(p, &endptr, 10);
|
||||
if (p == endptr || *endptr != 0) {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
} else {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
if (cmd[MEM_REGION].count() > 0) {
|
||||
use_memory_map = true;
|
||||
option::Option *opt = cmd[MEM_REGION].first();
|
||||
|
||||
traced_memory_map.add(begin, size);
|
||||
while(opt != 0) {
|
||||
char *endptr;
|
||||
guest_address_t begin = strtol(opt->arg, &endptr, 16);
|
||||
guest_address_t size;
|
||||
if (endptr == opt->arg) {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
m_log << "Adding " << opt->arg << " 0x" << std::hex << begin
|
||||
<< "+" << std::dec << size << " to trace map" << std::endl;
|
||||
char delim = *endptr;
|
||||
if (delim == 0) {
|
||||
size = 1;
|
||||
} else if(delim == ':') {
|
||||
char *p = endptr +1;
|
||||
size = strtol(p, &endptr, 16) - begin;
|
||||
if (p == endptr || *endptr != 0) {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
} else if(delim == '+') {
|
||||
char *p = endptr +1;
|
||||
size = strtol(p, &endptr, 10);
|
||||
if (p == endptr || *endptr != 0) {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
} else {
|
||||
m_log << "Couldn't parse " << opt->arg << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
opt = opt->next();
|
||||
}
|
||||
}
|
||||
traced_memory_map.add(begin, size);
|
||||
|
||||
assert(m_elf->getSymbol(start_symbol).isValid());
|
||||
assert(m_elf->getSymbol(stop_symbol).isValid());
|
||||
assert(m_elf->getSymbol(save_symbol).isValid());
|
||||
m_log << "Adding " << opt->arg << " 0x" << std::hex << begin
|
||||
<< "+" << std::dec << size << " to trace map" << std::endl;
|
||||
|
||||
m_log << "start symbol: " << start_symbol << " 0x" << std::hex << m_elf->getSymbol(start_symbol).getAddress() << std::endl;
|
||||
m_log << "save symbol: " << save_symbol << " 0x" << std::hex << m_elf->getSymbol(save_symbol).getAddress() << std::endl;
|
||||
m_log << "stop symbol: " << stop_symbol << " 0x" << std::hex << m_elf->getSymbol(stop_symbol).getAddress() << std::endl;
|
||||
opt = opt->next();
|
||||
}
|
||||
}
|
||||
|
||||
if (cmd[FULL_TRACE]) {
|
||||
this->full_trace = true;
|
||||
}
|
||||
|
||||
assert(m_elf->getSymbol(start_symbol).isValid());
|
||||
assert(m_elf->getSymbol(stop_symbol).isValid());
|
||||
assert(m_elf->getSymbol(save_symbol).isValid());
|
||||
|
||||
m_log << "start symbol: " << start_symbol << " 0x" << std::hex << m_elf->getSymbol(start_symbol).getAddress() << std::endl;
|
||||
m_log << "save symbol: " << save_symbol << " 0x" << std::hex << m_elf->getSymbol(save_symbol).getAddress() << std::endl;
|
||||
m_log << "stop symbol: " << stop_symbol << " 0x" << std::hex << m_elf->getSymbol(stop_symbol).getAddress() << std::endl;
|
||||
|
||||
m_log << "state file: " << state_file << std::endl;
|
||||
m_log << "trace file: " << trace_file << std::endl;
|
||||
m_log << "full-trace: " << this->full_trace << std::endl;
|
||||
|
||||
m_log << "state file: " << state_file << std::endl;
|
||||
m_log << "trace file: " << trace_file << std::endl;
|
||||
|
||||
}
|
||||
|
||||
bool GenericTracing::run()
|
||||
{
|
||||
parseOptions();
|
||||
parseOptions();
|
||||
|
||||
BPSingleListener l_start_symbol(m_elf->getSymbol(start_symbol).getAddress());
|
||||
BPSingleListener l_save_symbol (m_elf->getSymbol(save_symbol).getAddress());
|
||||
BPSingleListener l_stop_symbol (m_elf->getSymbol(stop_symbol).getAddress());
|
||||
BPSingleListener l_start_symbol(m_elf->getSymbol(start_symbol).getAddress());
|
||||
BPSingleListener l_save_symbol (m_elf->getSymbol(save_symbol).getAddress());
|
||||
BPSingleListener l_stop_symbol (m_elf->getSymbol(stop_symbol).getAddress());
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
// STEP 1: run until interesting function starts, start the tracing
|
||||
simulator.addListenerAndResume(&l_start_symbol);
|
||||
m_log << start_symbol << " reached, start tracing" << std::endl;
|
||||
////////////////////////////////////////////////////////////////
|
||||
// STEP 1: run until interesting function starts, start the tracing
|
||||
simulator.addListenerAndResume(&l_start_symbol);
|
||||
m_log << start_symbol << " reached, start tracing" << std::endl;
|
||||
|
||||
// restrict memory access logging to injection target
|
||||
TracingPlugin tp;
|
||||
// restrict memory access logging to injection target
|
||||
TracingPlugin tp;
|
||||
tp.setFullTrace(this->full_trace);
|
||||
|
||||
if (use_memory_map) {
|
||||
m_log << "Use restricted memory map for tracing" << std::endl;
|
||||
tp.restrictMemoryAddresses(&traced_memory_map);
|
||||
}
|
||||
if (use_memory_map) {
|
||||
m_log << "Use restricted memory map for tracing" << std::endl;
|
||||
tp.restrictMemoryAddresses(&traced_memory_map);
|
||||
}
|
||||
|
||||
ogzstream of(trace_file.c_str());
|
||||
if (of.bad()) {
|
||||
m_log << "Couldn't open trace file: " << trace_file << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
tp.setTraceFile(&of);
|
||||
ogzstream of(trace_file.c_str());
|
||||
if (of.bad()) {
|
||||
m_log << "Couldn't open trace file: " << trace_file << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
tp.setTraceFile(&of);
|
||||
|
||||
// this must be done *after* configuring the plugin:
|
||||
simulator.addFlow(&tp);
|
||||
// this must be done *after* configuring the plugin:
|
||||
simulator.addFlow(&tp);
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
// STEP 2: continue to the save point, and save state
|
||||
if (start_symbol != save_symbol) {
|
||||
simulator.addListenerAndResume(&l_save_symbol);
|
||||
}
|
||||
m_log << start_symbol << " reached, save state" << std::endl;
|
||||
simulator.save(state_file);
|
||||
////////////////////////////////////////////////////////////////
|
||||
// STEP 2: continue to the save point, and save state
|
||||
if (start_symbol != save_symbol) {
|
||||
simulator.addListenerAndResume(&l_save_symbol);
|
||||
}
|
||||
m_log << start_symbol << " reached, save state" << std::endl;
|
||||
simulator.save(state_file);
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
// Step 3: Continue to the stop point
|
||||
simulator.addListener(&l_stop_symbol);
|
||||
simulator.resume();
|
||||
////////////////////////////////////////////////////////////////
|
||||
// Step 3: Continue to the stop point
|
||||
simulator.addListener(&l_stop_symbol);
|
||||
simulator.resume();
|
||||
|
||||
////////////////////////////////////////////////////////////////
|
||||
// Step 4: tear down the tracing
|
||||
////////////////////////////////////////////////////////////////
|
||||
// Step 4: tear down the tracing
|
||||
|
||||
simulator.removeFlow(&tp);
|
||||
// serialize trace to file
|
||||
@ -222,7 +232,7 @@ bool GenericTracing::run()
|
||||
m_log << "failed to write " << trace_file << std::endl;
|
||||
return false;
|
||||
}
|
||||
of.close();
|
||||
of.close();
|
||||
|
||||
simulator.clearListeners();
|
||||
simulator.terminate();
|
||||
|
||||
@ -11,25 +11,27 @@
|
||||
|
||||
|
||||
class GenericTracing : public fail::ExperimentFlow {
|
||||
std::string start_symbol;
|
||||
std::string stop_symbol;
|
||||
std::string save_symbol;
|
||||
std::string start_symbol;
|
||||
std::string stop_symbol;
|
||||
std::string save_symbol;
|
||||
|
||||
std::string state_file;
|
||||
std::string trace_file;
|
||||
std::string elf_file;
|
||||
std::string state_file;
|
||||
std::string trace_file;
|
||||
std::string elf_file;
|
||||
|
||||
bool use_memory_map;
|
||||
fail::MemoryMap traced_memory_map;
|
||||
bool use_memory_map;
|
||||
fail::MemoryMap traced_memory_map;
|
||||
|
||||
fail::Logger m_log;
|
||||
fail::ElfReader *m_elf;
|
||||
bool full_trace;
|
||||
|
||||
fail::Logger m_log;
|
||||
fail::ElfReader *m_elf;
|
||||
|
||||
public:
|
||||
void parseOptions();
|
||||
void parseOptions();
|
||||
bool run();
|
||||
|
||||
GenericTracing() : m_log("GenericTracing", false) {};
|
||||
GenericTracing() : full_trace(false), m_log("GenericTracing", false) {};
|
||||
};
|
||||
|
||||
#endif // __TRACING_TEST_HPP__
|
||||
|
||||
Reference in New Issue
Block a user