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 | |
---|
10 | #include <rld.h> |
---|
11 | |
---|
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, |
---|
22 | const char* const theLibraryName, |
---|
23 | bool verbose |
---|
24 | ) : executable(theExecutableName), |
---|
25 | loadAddress(0) |
---|
26 | { |
---|
27 | if (theLibraryName != nullptr) |
---|
28 | libraryName = theLibraryName; |
---|
29 | |
---|
30 | if (verbose) { |
---|
31 | std::cerr << "Loading executable " << theExecutableName; |
---|
32 | if (theLibraryName != nullptr) |
---|
33 | std::cerr << " (" << theLibraryName << ')'; |
---|
34 | std::cerr << std::endl; |
---|
35 | } |
---|
36 | |
---|
37 | executable.open(); |
---|
38 | executable.begin(); |
---|
39 | executable.load_symbols(symbols); |
---|
40 | debug.begin(executable.elf()); |
---|
41 | debug.load_debug(); |
---|
42 | debug.load_functions(); |
---|
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 | } |
---|
52 | } |
---|
53 | |
---|
54 | ExecutableInfo::~ExecutableInfo() |
---|
55 | { |
---|
56 | debug.end(); |
---|
57 | executable.end(); |
---|
58 | executable.close(); |
---|
59 | } |
---|
60 | |
---|
61 | void ExecutableInfo::dumpCoverageMaps( void ) { |
---|
62 | ExecutableInfo::CoverageMaps::iterator itr; |
---|
63 | |
---|
64 | for (auto& cm : coverageMaps) { |
---|
65 | std::cerr << "Coverage Map for " << cm.first << std::endl; |
---|
66 | cm.second->dump(); |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | void ExecutableInfo::dumpExecutableInfo( void ){ |
---|
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(); |
---|
77 | } |
---|
78 | |
---|
79 | CoverageMapBase* ExecutableInfo::getCoverageMap ( uint32_t address ) |
---|
80 | { |
---|
81 | CoverageMapBase* aCoverageMap = NULL; |
---|
82 | CoverageMaps::iterator it; |
---|
83 | std::string itsSymbol; |
---|
84 | |
---|
85 | // Obtain the coverage map containing the specified address. |
---|
86 | itsSymbol = theSymbolTable.getSymbol( address ); |
---|
87 | if (itsSymbol != "") { |
---|
88 | it = coverageMaps.find( itsSymbol ); |
---|
89 | aCoverageMap = (*it).second; |
---|
90 | } |
---|
91 | |
---|
92 | return aCoverageMap; |
---|
93 | } |
---|
94 | |
---|
95 | const std::string ExecutableInfo::getFileName ( void ) const |
---|
96 | { |
---|
97 | return executable.name().full(); |
---|
98 | } |
---|
99 | |
---|
100 | const std::string ExecutableInfo::getLibraryName( void ) const |
---|
101 | { |
---|
102 | return libraryName; |
---|
103 | } |
---|
104 | |
---|
105 | uint32_t ExecutableInfo::getLoadAddress( void ) const |
---|
106 | { |
---|
107 | return loadAddress; |
---|
108 | } |
---|
109 | |
---|
110 | SymbolTable* ExecutableInfo::getSymbolTable ( void ) |
---|
111 | { |
---|
112 | return &theSymbolTable; |
---|
113 | } |
---|
114 | |
---|
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 ( |
---|
126 | const std::string& fileName, |
---|
127 | const std::string& symbolName, |
---|
128 | uint32_t lowAddress, |
---|
129 | uint32_t highAddress |
---|
130 | ) |
---|
131 | { |
---|
132 | CoverageMapBase *theMap; |
---|
133 | CoverageMaps::iterator itr; |
---|
134 | |
---|
135 | itr = coverageMaps.find( symbolName ); |
---|
136 | if ( itr == coverageMaps.end() ) { |
---|
137 | theMap = new CoverageMap( fileName, lowAddress, highAddress ); |
---|
138 | coverageMaps[ symbolName ] = theMap; |
---|
139 | } else { |
---|
140 | theMap = itr->second; |
---|
141 | theMap->Add( lowAddress, highAddress ); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
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 | |
---|
158 | bool ExecutableInfo::hasDynamicLibrary( void ) |
---|
159 | { |
---|
160 | return !libraryName.empty(); |
---|
161 | } |
---|
162 | |
---|
163 | void ExecutableInfo::mergeCoverage( void ) { |
---|
164 | for (auto& cm : coverageMaps) { |
---|
165 | if (SymbolsToAnalyze->isDesired( cm.first )) |
---|
166 | SymbolsToAnalyze->mergeCoverageMap( cm.first, cm.second ); |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | void ExecutableInfo::setLoadAddress( uint32_t address ) |
---|
171 | { |
---|
172 | loadAddress = address; |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|