FileInfo Class Reference
Collaboration diagram for FileInfo:

Classes

struct  CacheItem
 
struct  CacheItemComparator
 

Public Types

typedef int Offset
 

Public Member Functions

 FileInfo (void)
 
 FileInfo (const std::string &name, bool useGdb)
 
const char * symbolByOffset (Offset offset)
 
Offset next (Offset offset)
 

Public Attributes

std::string NAME
 

Private Types

typedef std::vector< CacheItemSymbolCache
 

Private Member Functions

void createOffsetMap (void)
 

Private Attributes

SymbolCache m_symbolCache
 

Detailed Description

Definition at line 164 of file pfm_gen_analysis.cpp.

Member Typedef Documentation

typedef int FileInfo::Offset

Definition at line 167 of file pfm_gen_analysis.cpp.

Definition at line 219 of file pfm_gen_analysis.cpp.

Constructor & Destructor Documentation

FileInfo::FileInfo ( void  )
inline

Definition at line 169 of file pfm_gen_analysis.cpp.

169 : NAME("<dynamically generated>") {}
std::string NAME
FileInfo::FileInfo ( const std::string name,
bool  useGdb 
)
inline

Definition at line 170 of file pfm_gen_analysis.cpp.

170  : NAME(name)
171  {
172  if(useGdb)
173  {
174  this->createOffsetMap();
175  }
176  }
std::string NAME
void createOffsetMap(void)

Member Function Documentation

void FileInfo::createOffsetMap ( void  )
inlineprivate

Definition at line 234 of file pfm_gen_analysis.cpp.

235  {
236  std::string commandLine = "objdump -p " + NAME;
237  PipeReader objdump(commandLine.c_str());
238  std::string oldname;
239  std::string suffix;
240  int vmbase = 0;
241  bool matched = false;
242  while(objdump.output())
243  {
244  // Checks the following regexp
245  //
246  // LOAD\\s+off\\s+(0x[0-9A-Fa-f]+)\\s+vaddr\\s+(0x[0-9A-Fa-f]+)
247  //
248  // and sets vmbase to be $2 - $1 of the first matched entry.
249 
251  std::getline(objdump.output(), line);
252 
253  if(!objdump.output()) break;
254  if(line.empty()) continue;
255  const char *lineptr = line.c_str();
256  if(!skipWhitespaces(lineptr, &lineptr)) continue;
257  if(!skipString("LOAD", lineptr, &lineptr)) continue;
258  if(!skipWhitespaces(lineptr, &lineptr)) continue;
259  if(!skipString("off", lineptr, &lineptr)) continue;
260  char *endptr = 0;
261  int initialBase = strtol(lineptr, &endptr, 16);
262  if(lineptr == endptr) continue;
263  lineptr = endptr;
264  if(!skipWhitespaces(lineptr, &lineptr)) continue;
265  if(!skipString("vaddr", lineptr, &lineptr)) continue;
266  if(!skipWhitespaces(lineptr, &lineptr)) continue;
267  int finalBase = strtol(lineptr, &endptr, 16);
268  if(lineptr == endptr) continue;
269  vmbase=finalBase - initialBase;
270  matched = true;
271  break;
272  }
273  if(!matched)
274  {
275  fprintf(stderr, "Cannot determine VM base address for %s\n", NAME.c_str());
276  fprintf(stderr, "Error while running `objdump -p %s`\n", NAME.c_str());
277  exit(1);
278  }
279  std::string commandLine2 = "nm -t d -n " + NAME;
280  PipeReader nm(commandLine2.c_str());
281  while(nm.output())
282  {
284  std::getline(nm.output(), line);
285  if(!nm.output()) break;
286  if(line.empty()) continue;
287  // If line does not match "^(\\d+)[ ]\\S[ ](\S+)$", exit.
288  const char *begin = line.c_str();
289  char *endptr = 0;
290  int address = strtol(begin, &endptr, 10);
291  if(endptr == begin) continue;
292  if(*endptr++ != ' ') continue;
293  if(isspace(*endptr++)) continue;
294  if(*endptr++ != ' ') continue;
295  char *symbolName = endptr;
296  while(*endptr && !isspace(*endptr)) endptr++;
297  if(*endptr != 0) continue;
298  // If line starts with '.' forget about it.
299  if(symbolName[0] == '.') continue;
300  // Create a new symbol with the given fileoffset.
301  // The symbol is automatically saved in the FileInfo cache by offset.
302  // If a symbol with the same offset is already there, the new one
303  // replaces the old one.
304  int offset = address-vmbase;
305  if(m_symbolCache.size() && (m_symbolCache.back().OFFSET == offset)) m_symbolCache.back().NAME = symbolName;
306  else m_symbolCache.push_back(CacheItem(address-vmbase, symbolName));
307  }
308  }
T empty(T...args)
bool skipString(const char *strptr, const char *srcbuffer, const char **dstbuffer)
T getline(T...args)
std::string NAME
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:47
STL class.
T push_back(T...args)
T exit(T...args)
constexpr double nm
Definition: SystemOfUnits.h:82
T size(T...args)
SymbolCache m_symbolCache
T c_str(T...args)
T strtol(T...args)
T back(T...args)
bool skipWhitespaces(const char *srcbuffer, const char **destbuffer)
T fprintf(T...args)
T isspace(T...args)
Offset FileInfo::next ( Offset  offset)
inline

Definition at line 201 of file pfm_gen_analysis.cpp.

202  {
203  SymbolCache::iterator i = upper_bound(m_symbolCache.begin(), m_symbolCache.end(), offset, CacheItemComparator());
204  if(i == m_symbolCache.end())
205  {
206  return 0;
207  }
208  return i->OFFSET;
209  }
T upper_bound(T...args)
T end(T...args)
T begin(T...args)
SymbolCache m_symbolCache
const char* FileInfo::symbolByOffset ( Offset  offset)
inline

Definition at line 178 of file pfm_gen_analysis.cpp.

179  {
180  if(m_symbolCache.empty())
181  {
182  return 0;
183  }
184 
185  SymbolCache::iterator i = lower_bound(m_symbolCache.begin(), m_symbolCache.end(), offset, CacheItemComparator());
186  if(i->OFFSET == offset)
187  {
188  return i->NAME.c_str();
189  }
190 
191  if(i == m_symbolCache.begin())
192  {
193  return m_symbolCache.begin()->NAME.c_str();
194  }
195 
196  --i;
197 
198  return i->NAME.c_str();
199  }
T empty(T...args)
T end(T...args)
T lower_bound(T...args)
T begin(T...args)
SymbolCache m_symbolCache
T c_str(T...args)

Member Data Documentation

SymbolCache FileInfo::m_symbolCache
private

Definition at line 220 of file pfm_gen_analysis.cpp.

std::string FileInfo::NAME

Definition at line 168 of file pfm_gen_analysis.cpp.


The documentation for this class was generated from the following file: