ElfReader: Constructor tries to get ELF from ENV FAIL_ELF_PATH

git-svn-id: https://www4.informatik.uni-erlangen.de/i4svn/danceos/trunk/devel/fail@2002 8c4709b5-6ec9-48aa-a5cd-a96041d1645a
This commit is contained in:
hoffmann
2013-01-15 12:53:49 +00:00
parent d721ce7041
commit ac7cec7684
5 changed files with 181 additions and 154 deletions

View File

@ -10,6 +10,7 @@ Required for Fail*:
- protobuf-compiler - protobuf-compiler
- cmake - cmake
- cmake-curses-gui - cmake-curses-gui
- binutils-dev
- AspectC++ (ag++, ac++): AspectC++ 1.1 or newer is known to work and can be - AspectC++ (ag++, ac++): AspectC++ 1.1 or newer is known to work and can be
obtained from http://www.aspectc.org; nightlies can be downloaded from obtained from http://www.aspectc.org; nightlies can be downloaded from
http://akut.aspectc.org http://akut.aspectc.org

View File

@ -10,157 +10,172 @@ namespace fail {
const std::string ElfReader::NOTFOUND = "[ELFReader] Function not found."; const std::string ElfReader::NOTFOUND = "[ELFReader] Function not found.";
ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){
// Try to open the ELF file
FILE * fp = fopen(path, "r");
if (!fp) {
m_log << "Error: Could not open " << path << std::endl;
return;
}
// Evaluate headers void ElfReader::setup(const char* path) {
Elf32_Ehdr ehdr; // Try to open the ELF file
Elf32_Shdr sec_hdr; FILE * fp = fopen(path, "r");
int num_hdrs,i; if (!fp) {
fseek(fp,(off_t)0,SEEK_SET); m_log << "Error: Could not open " << path << std::endl;
read_ELF_file_header(fp, &ehdr); return;
num_hdrs=ehdr.e_shnum; }
m_log << "Evaluating ELF File: " << path << std::endl;
// Parse symbol table and generate internal map // Evaluate headers
for(i=0;i<num_hdrs;i++) Elf32_Ehdr ehdr;
Elf32_Shdr sec_hdr;
int num_hdrs,i;
fseek(fp,(off_t)0,SEEK_SET);
read_ELF_file_header(fp, &ehdr);
num_hdrs=ehdr.e_shnum;
m_log << "Evaluating ELF File: " << path << std::endl;
// Parse symbol table and generate internal map
for(i=0;i<num_hdrs;i++)
{
if(read_ELF_section_header(i,&sec_hdr,fp)==-1)
{ {
if(read_ELF_section_header(i,&sec_hdr,fp)==-1) m_log << "Wrong Section to read" << std::endl;
}
else
{
if((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM))
{ {
m_log << "Wrong Section to read" << std::endl; process_symboltable(i,fp);
} }
else else
{ {
if((sec_hdr.sh_type==SHT_SYMTAB)||(sec_hdr.sh_type==SHT_DYNSYM)) continue;
{
process_symboltable(i,fp);
}
else
{
continue;
}
} }
} }
fclose(fp);
} }
int ElfReader::process_symboltable(int sect_num, FILE* fp){ fclose(fp);
}
Elf32_Shdr sect_hdr; ElfReader::ElfReader() : m_log("Fail*Elfinfo", false){
Elf32_Sym mysym; // try to open elf file from environment variable
char *name_buf=NULL; char * elfpath = getenv("FAIL_ELF_PATH");
int num_sym,link,i,idx; if(elfpath == NULL){
off_t sym_data_offset; m_log << "FAIL_ELF_PATH not set :(" << std::endl;
int sym_data_size; }else{
if(read_ELF_section_header(sect_num,&sect_hdr,fp)==-1) setup(elfpath);
{ }
return -1; }
}
//we have to check to which strtab it is linked
link=sect_hdr.sh_link;
sym_data_offset=sect_hdr.sh_offset;
sym_data_size=sect_hdr.sh_size;
num_sym=sym_data_size/sizeof(Elf32_Sym);
//read the coresponding strtab ElfReader::ElfReader(const char* path) : m_log("Fail*Elfinfo", false){
if(read_ELF_section_header(link,&sect_hdr,fp)==-1) setup(path);
{ }
return -1;
}
//get the size of strtab in file and allocate a buffer
name_buf=(char*)malloc(sect_hdr.sh_size);
if(!name_buf)
return -1;
//get the offset of strtab in file and seek to it
fseek(fp,sect_hdr.sh_offset,SEEK_SET);
//read all data from the section to the buffer.
fread(name_buf,sect_hdr.sh_size,1,fp);
//so we have the namebuf now seek to symtab data
fseek(fp,sym_data_offset,SEEK_SET);
for(i=0;i<num_sym;i++) int ElfReader::process_symboltable(int sect_num, FILE* fp){
{
fread(&mysym,sizeof(Elf32_Sym),1,fp); Elf32_Shdr sect_hdr;
idx=mysym.st_name; Elf32_Sym mysym;
char *name_buf=NULL;
int num_sym,link,i,idx;
off_t sym_data_offset;
int sym_data_size;
if(read_ELF_section_header(sect_num,&sect_hdr,fp)==-1)
{
return -1;
}
//we have to check to which strtab it is linked
link=sect_hdr.sh_link;
sym_data_offset=sect_hdr.sh_offset;
sym_data_size=sect_hdr.sh_size;
num_sym=sym_data_size/sizeof(Elf32_Sym);
int type = ELF32_ST_TYPE(mysym.st_info); //read the coresponding strtab
if((type != STT_SECTION) && (type != STT_FILE)){ if(read_ELF_section_header(link,&sect_hdr,fp)==-1)
{
return -1;
}
//get the size of strtab in file and allocate a buffer
name_buf=(char*)malloc(sect_hdr.sh_size);
if(!name_buf)
return -1;
//get the offset of strtab in file and seek to it
fseek(fp,sect_hdr.sh_offset,SEEK_SET);
//read all data from the section to the buffer.
fread(name_buf,sect_hdr.sh_size,1,fp);
//so we have the namebuf now seek to symtab data
fseek(fp,sym_data_offset,SEEK_SET);
for(i=0;i<num_sym;i++)
{
fread(&mysym,sizeof(Elf32_Sym),1,fp);
idx=mysym.st_name;
int type = ELF32_ST_TYPE(mysym.st_info);
if((type != STT_SECTION) && (type != STT_FILE)){
#ifndef __puma #ifndef __puma
m_bimap_mangled.insert( entry(name_buf+idx, mysym.st_value) ); m_bimap_mangled.insert( entry(name_buf+idx, mysym.st_value) );
m_bimap_demangled.insert( entry ( Demangler::demangle(name_buf+idx), mysym.st_value) ); m_bimap_demangled.insert( entry ( Demangler::demangle(name_buf+idx), mysym.st_value) );
#endif #endif
}
} }
free (name_buf);
return 0;
} }
free (name_buf);
return 0;
}
guest_address_t ElfReader::getAddressByName(const std::string& name) { guest_address_t ElfReader::getAddressByName(const std::string& name) {
#ifndef __puma #ifndef __puma
guest_address_t res = getAddress(m_bimap_demangled, name); guest_address_t res = getAddress(m_bimap_demangled, name);
if(res == ADDR_INV){ if(res == ADDR_INV){
res = getAddress(m_bimap_mangled, name); res = getAddress(m_bimap_mangled, name);
} }
return res; return res;
#endif #endif
} }
#ifndef __puma #ifndef __puma
guest_address_t ElfReader::getAddress(const bimap_t& map, const std::string& name){ guest_address_t ElfReader::getAddress(const bimap_t& map, const std::string& name){
typedef bimap_t::left_map::const_iterator const_iterator_t; typedef bimap_t::left_map::const_iterator const_iterator_t;
const_iterator_t iterator = map.left.find(name); const_iterator_t iterator = map.left.find(name);
if(iterator == map.left.end()){ if(iterator == map.left.end()){
return ADDR_INV; return ADDR_INV;
}else{ }else{
return iterator->second; return iterator->second;
}
} }
}
#endif #endif
#ifndef __puma #ifndef __puma
std::string ElfReader::getName(const bimap_t& map, guest_address_t address){ std::string ElfReader::getName(const bimap_t& map, guest_address_t address){
// .right switches key/value // .right switches key/value
typedef bimap_t::right_map::const_iterator const_iterator_t; typedef bimap_t::right_map::const_iterator const_iterator_t;
const_iterator_t iterator = map.right.find(address); const_iterator_t iterator = map.right.find(address);
if(iterator != map.right.end()){ if(iterator != map.right.end()){
return iterator->second; return iterator->second;
}
return NOTFOUND;
} }
return NOTFOUND;
}
std::string ElfReader::getNameByAddress(guest_address_t address) { std::string ElfReader::getNameByAddress(guest_address_t address) {
std::string res = getName(m_bimap_demangled, address); std::string res = getName(m_bimap_demangled, address);
if(res == NOTFOUND){ if(res == NOTFOUND){
return getName(m_bimap_mangled, address);
}
return res;
}
std::string ElfReader::getMangledNameByAddress(guest_address_t address) {
return getName(m_bimap_mangled, address); return getName(m_bimap_mangled, address);
} }
return res;
}
std::string ElfReader::getDemangledNameByAddress(guest_address_t address) { std::string ElfReader::getMangledNameByAddress(guest_address_t address) {
return getName(m_bimap_demangled, address); return getName(m_bimap_mangled, address);
} }
void ElfReader::printDemangled(){ std::string ElfReader::getDemangledNameByAddress(guest_address_t address) {
print_map(m_bimap_demangled.right); // print Address as first element return getName(m_bimap_demangled, address);
} }
void ElfReader::printMangled(){ void ElfReader::printDemangled(){
print_map(m_bimap_mangled.right); // print Address as first element print_map(m_bimap_demangled.right); // print Address as first element
} }
void ElfReader::printMangled(){
print_map(m_bimap_mangled.right); // print Address as first element
}
#endif #endif

View File

@ -22,13 +22,19 @@ namespace fail {
public: public:
/** /**
* Constructor. * Constructor.
* @param path Path to the ELF file. * @param path Path to the ELF file.
*/ */
ElfReader(const char* path); ElfReader(const char* path);
/** /**
* Constructor.
* @note The path is guessed from a FAIL_ELF_PATH environment variable
*/
ElfReader();
/**
* Get guest address by symbol name. * Get guest address by symbol name.
* Both mangled an demangled symbols are searched. * Both mangled an demangled symbols are searched.
* @param name The symbol name as string * @param name The symbol name as string
@ -79,11 +85,12 @@ namespace fail {
Logger m_log; Logger m_log;
void setup(const char*);
int process_symboltable(int sect_num, FILE* fp); int process_symboltable(int sect_num, FILE* fp);
#ifndef __puma #ifndef __puma
typedef boost::bimap< std::string, guest_address_t > bimap_t; typedef boost::bimap< std::string, guest_address_t > bimap_t;
typedef bimap_t::value_type entry; typedef bimap_t::value_type entry;
bimap_t m_bimap_mangled; bimap_t m_bimap_mangled;
bimap_t m_bimap_demangled; bimap_t m_bimap_demangled;

View File

@ -5,9 +5,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include "util/Logger.hpp"
#include "util/ElfReader.hpp"
#include <stdlib.h> #include <stdlib.h>
#include "experiment.hpp" #include "experiment.hpp"
#include "experimentInfo.hpp" #include "experimentInfo.hpp"
@ -28,61 +26,58 @@ using namespace fail;
#error This experiment needs: breakpoints, traps, save, and restore. Enable these in the configuration. #error This experiment needs: breakpoints, traps, save, and restore. Enable these in the configuration.
#endif #endif
#define SAVESTATE (1)
void VEZSExperiment::printEIP() {
m_log << "EIP = 0x" << hex << simulator.getCPU(0).getInstructionPointer() <<" "<< m_elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl;
}
bool VEZSExperiment::run() bool VEZSExperiment::run()
{ {
Logger log("VEZS-Example", false); m_log << "STARTING EXPERIMENT" << endl;
// Elf image path must be set in a environment variable. printEIP();
char * elfpath = getenv("CIAO_ELF_PATH");
if(elfpath == NULL){
log << " CIAO_ELF_PATH not set :(" << std::endl;
simulator.terminate();
}
ElfReader elf(elfpath);
log << "STARTING EXPERIMENT" << endl;
log << "main() address: " << elf.getAddressByName("main") << endl;
//elf.printMangled();
//elf.printDemangled();
#if(SAVESTATE)
m_log << "Booting, and saving state at ";
BPSingleListener bp; BPSingleListener bp;
#if 0
// STEP 1: run until interesting function starts, and save state // STEP 1: run until interesting function starts, and save state
bp.setWatchInstructionPointer(elf.getAddressByName("main")); bp.setWatchInstructionPointer(m_elf.getAddressByName("main"));
if(simulator.addListenerAndResume(&bp) == &bp){ if(simulator.addListenerAndResume(&bp) == &bp){
log << "test function entry reached, saving state" << endl; m_log << "test function entry reached, saving state" << endl;
} }
log << "EIP = " << hex << bp.getTriggerInstructionPointer() << endl; printEIP();
//simulator.terminate(); //simulator.terminate();
simulator.save("vezs.state"); simulator.save("vezs.state");
simulator.terminate(); simulator.terminate();
#endif #else
#if 1
//int bit_offset = 2; //int bit_offset = 2;
//for (int instr_offset = 0; instr_offset < OOSTUBS_NUMINSTR; ++instr_offset) { //for (int instr_offset = 0; instr_offset < OOSTUBS_NUMINSTR; ++instr_offset) {
// STEP 3: The actual experiment. // STEP 3: The actual experiment.
log << "restoring state" << endl; m_log << "restoring state" << endl;
simulator.restore("vezs.state"); simulator.restore("vezs.state");
log << " current EIP = " << simulator.getCPU(0).getInstructionPointer() << endl; printEIP();
BPSingleListener bpt0;
BPSingleListener bpt1;
bpt0.setWatchInstructionPointer(elf.getAddressByName("Alpha::functionTaskTask0"));
bpt1.setWatchInstructionPointer(elf.getAddressByName("_ZN4Beta17functionTaskTask1Ev")); // both mangled and demangled name a working.
simulator.addListener(&bpt1); // BPSingleListener bpt0;
simulator.addListenerAndResume(&bpt0); // BPSingleListener bpt1;
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<<elf.getMangledNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl; // m_elf.printDemangled();
// bpt0.setWatchInstructionPointer(m_elf.getAddressByName("DOM1::functionTaskmainTask"));
// bpt1.setWatchInstructionPointer(m_elf.getAddressByName("DOM1::functionTaskpersistentDetectorScopeEntryTask")); // both mangled and demangled name a working.
//
// simulator.addListener(&bpt1);
// simulator.addListenerAndResume(&bpt0);
// printEIP();
simulator.resume(); simulator.resume();
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<<elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl; //
// printEIP();
simulator.clearListeners(); // simulator.clearListeners();
bpt1.setWatchInstructionPointer(elf.getAddressByName("os::krn::SchedImpl::superDispatch_impl")); // bpt1.setWatchInstructionPointer(m_elf.getAddressByName("os::krn::SchedImpl::superDispatch_impl"));
for(int i = 0; i < 10; i++){ // for(;;){
simulator.addListenerAndResume(&bpt1); // simulator.addListenerAndResume(&bpt1);
log << "EIP = " << simulator.getCPU(0).getInstructionPointer() <<" "<< elf.getNameByAddress(simulator.getCPU(0).getInstructionPointer()) << endl; // printEIP();
} // }
#endif #endif
#if 0 #if 0
int32_t data = simulator.getCPU(0).getRegister(RID_CAX)->getData(); int32_t data = simulator.getCPU(0).getRegister(RID_CAX)->getData();

View File

@ -3,10 +3,19 @@
#include "efw/ExperimentFlow.hpp" #include "efw/ExperimentFlow.hpp"
#include "efw/JobClient.hpp" #include "efw/JobClient.hpp"
#include "util/Logger.hpp"
#include "util/ElfReader.hpp"
class VEZSExperiment : public fail::ExperimentFlow { class VEZSExperiment : public fail::ExperimentFlow {
fail::JobClient m_jc;
fail::JobClient m_jc;
fail::ElfReader m_elf;
fail::Logger m_log;
void printEIP();
public: public:
VEZSExperiment() : m_log("VEZS-example", false) {};
bool run(); bool run();
}; };