All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 163 of file pfm_gen_analysis.cpp.

Member Typedef Documentation

typedef int FileInfo::Offset

Definition at line 166 of file pfm_gen_analysis.cpp.

typedef std::vector<CacheItem> FileInfo::SymbolCache
private

Definition at line 218 of file pfm_gen_analysis.cpp.

Constructor & Destructor Documentation

FileInfo::FileInfo ( void  )
inline

Definition at line 168 of file pfm_gen_analysis.cpp.

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

Definition at line 169 of file pfm_gen_analysis.cpp.

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

Member Function Documentation

void FileInfo::createOffsetMap ( void  )
inlineprivate

Definition at line 233 of file pfm_gen_analysis.cpp.

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

Definition at line 200 of file pfm_gen_analysis.cpp.

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

Definition at line 177 of file pfm_gen_analysis.cpp.

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

Member Data Documentation

SymbolCache FileInfo::m_symbolCache
private

Definition at line 219 of file pfm_gen_analysis.cpp.

std::string FileInfo::NAME

Definition at line 167 of file pfm_gen_analysis.cpp.


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