[100f517] | 1 | /*! @file ExecutableInfo.cc |
---|
| 2 | * @brief ExecutableInfo Implementation |
---|
| 3 | * |
---|
| 4 | * This file contains the implementation of the functionality |
---|
| 5 | * of the ExecutableInfo class. |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include <stdio.h> |
---|
| 9 | |
---|
[fb987e8] | 10 | #include <rld.h> |
---|
| 11 | |
---|
[100f517] | 12 | #include "ExecutableInfo.h" |
---|
| 13 | #include "app_common.h" |
---|
| 14 | #include "CoverageMap.h" |
---|
| 15 | #include "DesiredSymbols.h" |
---|
| 16 | #include "SymbolTable.h" |
---|
| 17 | |
---|
| 18 | namespace Coverage { |
---|
| 19 | |
---|
| 20 | ExecutableInfo::ExecutableInfo( |
---|
| 21 | const char* const theExecutableName, |
---|
[99c90b3] | 22 | const char* const theLibraryName, |
---|
| 23 | bool verbose |
---|
[fb987e8] | 24 | ) : executable(theExecutableName), |
---|
| 25 | loadAddress(0) |
---|
[100f517] | 26 | { |
---|
[99c90b3] | 27 | if (theLibraryName != nullptr) |
---|
[100f517] | 28 | libraryName = theLibraryName; |
---|
[881824f] | 29 | |
---|
[99c90b3] | 30 | if (verbose) { |
---|
| 31 | std::cerr << "Loading executable " << theExecutableName; |
---|
| 32 | if (theLibraryName != nullptr) |
---|
| 33 | std::cerr << " (" << theLibraryName << ')'; |
---|
| 34 | std::cerr << std::endl; |
---|
| 35 | } |
---|
| 36 | |
---|
[881824f] | 37 | executable.open(); |
---|
| 38 | executable.begin(); |
---|
| 39 | executable.load_symbols(symbols); |
---|
| 40 | debug.begin(executable.elf()); |
---|
| 41 | debug.load_debug(); |
---|
[bf8e59f] | 42 | debug.load_functions(); |
---|
[99c90b3] | 43 | |
---|
| 44 | for (auto& cu : debug.get_cus()) { |
---|
| 45 | for (auto& func : cu.get_functions()) { |
---|
| 46 | if (func.has_machine_code() && (!func.is_inlined() || func.is_external())) { |
---|
| 47 | createCoverageMap (cu.name(), func.name(), |
---|
| 48 | func.pc_low(), func.pc_high()); |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | } |
---|
[100f517] | 52 | } |
---|
| 53 | |
---|
| 54 | ExecutableInfo::~ExecutableInfo() |
---|
| 55 | { |
---|
[fb987e8] | 56 | debug.end(); |
---|
| 57 | executable.end(); |
---|
| 58 | executable.close(); |
---|
[100f517] | 59 | } |
---|
| 60 | |
---|
| 61 | void ExecutableInfo::dumpCoverageMaps( void ) { |
---|
[fb987e8] | 62 | ExecutableInfo::CoverageMaps::iterator itr; |
---|
[100f517] | 63 | |
---|
[d6ae3ae] | 64 | for (auto& cm : coverageMaps) { |
---|
| 65 | std::cerr << "Coverage Map for " << cm.first << std::endl; |
---|
| 66 | cm.second->dump(); |
---|
[100f517] | 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | void ExecutableInfo::dumpExecutableInfo( void ){ |
---|
[fb987e8] | 71 | std::cout << std::endl |
---|
| 72 | << "== Executable info ==" << std::endl |
---|
| 73 | << "executable = " << getFileName () << std::endl |
---|
| 74 | << "library = " << libraryName << std::endl |
---|
| 75 | << "loadAddress = " << loadAddress << std::endl; |
---|
| 76 | theSymbolTable.dumpSymbolTable(); |
---|
[100f517] | 77 | } |
---|
| 78 | |
---|
| 79 | CoverageMapBase* ExecutableInfo::getCoverageMap ( uint32_t address ) |
---|
| 80 | { |
---|
[fb987e8] | 81 | CoverageMapBase* aCoverageMap = NULL; |
---|
| 82 | CoverageMaps::iterator it; |
---|
| 83 | std::string itsSymbol; |
---|
[100f517] | 84 | |
---|
| 85 | // Obtain the coverage map containing the specified address. |
---|
[fb987e8] | 86 | itsSymbol = theSymbolTable.getSymbol( address ); |
---|
[100f517] | 87 | if (itsSymbol != "") { |
---|
| 88 | it = coverageMaps.find( itsSymbol ); |
---|
| 89 | aCoverageMap = (*it).second; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | return aCoverageMap; |
---|
| 93 | } |
---|
| 94 | |
---|
[fb987e8] | 95 | const std::string ExecutableInfo::getFileName ( void ) const |
---|
[100f517] | 96 | { |
---|
[fb987e8] | 97 | return executable.name().full(); |
---|
[100f517] | 98 | } |
---|
| 99 | |
---|
[fb987e8] | 100 | const std::string ExecutableInfo::getLibraryName( void ) const |
---|
[100f517] | 101 | { |
---|
| 102 | return libraryName; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | uint32_t ExecutableInfo::getLoadAddress( void ) const |
---|
| 106 | { |
---|
| 107 | return loadAddress; |
---|
| 108 | } |
---|
| 109 | |
---|
[fb987e8] | 110 | SymbolTable* ExecutableInfo::getSymbolTable ( void ) |
---|
[100f517] | 111 | { |
---|
[fb987e8] | 112 | return &theSymbolTable; |
---|
[100f517] | 113 | } |
---|
| 114 | |
---|
[99c90b3] | 115 | CoverageMapBase& ExecutableInfo::findCoverageMap( |
---|
| 116 | const std::string& symbolName |
---|
| 117 | ) |
---|
| 118 | { |
---|
| 119 | CoverageMaps::iterator cmi = coverageMaps.find( symbolName ); |
---|
| 120 | if ( cmi == coverageMaps.end() ) |
---|
| 121 | throw rld::error (symbolName, "ExecutableInfo::findCoverageMap"); |
---|
| 122 | return *(cmi->second); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | void ExecutableInfo::createCoverageMap ( |
---|
[cb018bc] | 126 | const std::string& fileName, |
---|
[100f517] | 127 | const std::string& symbolName, |
---|
| 128 | uint32_t lowAddress, |
---|
| 129 | uint32_t highAddress |
---|
| 130 | ) |
---|
| 131 | { |
---|
[bf8e59f] | 132 | CoverageMapBase *theMap; |
---|
| 133 | CoverageMaps::iterator itr; |
---|
[100f517] | 134 | |
---|
| 135 | itr = coverageMaps.find( symbolName ); |
---|
| 136 | if ( itr == coverageMaps.end() ) { |
---|
[cb018bc] | 137 | theMap = new CoverageMap( fileName, lowAddress, highAddress ); |
---|
[100f517] | 138 | coverageMaps[ symbolName ] = theMap; |
---|
| 139 | } else { |
---|
| 140 | theMap = itr->second; |
---|
| 141 | theMap->Add( lowAddress, highAddress ); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
[fb987e8] | 145 | void ExecutableInfo::getSourceAndLine( |
---|
| 146 | const unsigned int address, |
---|
| 147 | std::string& line |
---|
| 148 | ) |
---|
| 149 | { |
---|
| 150 | std::string file; |
---|
| 151 | int lno; |
---|
| 152 | debug.get_source (address, file, lno); |
---|
| 153 | std::ostringstream ss; |
---|
| 154 | ss << file << ':' << lno; |
---|
| 155 | line = ss.str (); |
---|
| 156 | } |
---|
| 157 | |
---|
[100f517] | 158 | bool ExecutableInfo::hasDynamicLibrary( void ) |
---|
| 159 | { |
---|
[fb987e8] | 160 | return !libraryName.empty(); |
---|
[100f517] | 161 | } |
---|
| 162 | |
---|
| 163 | void ExecutableInfo::mergeCoverage( void ) { |
---|
[99c90b3] | 164 | for (auto& cm : coverageMaps) { |
---|
| 165 | if (SymbolsToAnalyze->isDesired( cm.first )) |
---|
| 166 | SymbolsToAnalyze->mergeCoverageMap( cm.first, cm.second ); |
---|
[100f517] | 167 | } |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | void ExecutableInfo::setLoadAddress( uint32_t address ) |
---|
| 171 | { |
---|
| 172 | loadAddress = address; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | } |
---|