HPCToolkit
Proc.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef BinUtil_Proc_hpp
61 #define BinUtil_Proc_hpp
62 
63 //************************* System Include Files ****************************
64 
65 #include <iostream>
66 #include <string>
67 
68 //*************************** User Include Files ****************************
69 
70 #include <include/gcc-attr.h>
71 #include <include/uint.h>
72 
73 #include "LM.hpp"
74 #include "Seg.hpp"
75 
76 #include <lib/isa/ISATypes.hpp>
77 
78 //*************************** Forward Declarations **************************
79 
80 class TextSeg;
81 
82 //***************************************************************************
83 // Proc
84 //***************************************************************************
85 
86 namespace BinUtil {
87 
88 // --------------------------------------------------------------------------
89 // 'Proc' represents a procedure in the 'TextSeg' of a 'LM'
90 // --------------------------------------------------------------------------
91 
92 class Proc {
93 public:
95 
96 public:
97  // -------------------------------------------------------
98  // Constructor/Destructor
99  // -------------------------------------------------------
100  Proc(TextSeg* seg, const std::string& name, const std::string& linkname,
101  Type t, VMA begVMA, VMA endVMA, unsigned int size);
102 
103  virtual ~Proc();
104 
105 
106  // -------------------------------------------------------
107  // Basic data
108  // -------------------------------------------------------
109 
110  TextSeg*
111  seg() const
112  { return m_seg; }
113 
114  LM*
115  lm() const
116  { return m_seg->lm(); }
117 
118  // Returns the name as determined by debugging information; if this
119  // is unavailable returns the name found in the symbol table. (Note
120  // that no demangling is performed.)
121  const std::string&
122  name() const
123  { return m_name; }
124 
125  void
126  name(const std::string& name)
127  { m_name = name; }
128 
129  // Returns the name as found in the symbol table
130  const std::string&
131  linkName() const
132  { return m_linkname; }
133 
134  // Return type of procedure
135  Type
136  type() const
137  { return m_type; }
138 
139  void
141  { m_type = type; }
142 
143  // Return the begin and end virtual memory address of a procedure:
144  // [beg, end]. Note that the end address points to the beginning of
145  // the last instruction which is different than the convention used
146  // for 'Seg'.
147  VMA
148  begVMA() const
149  { return m_begVMA; }
150 
151  VMA
152  endVMA() const
153  { return m_endVMA; }
154 
155  void
156  endVMA(VMA endVMA)
157  { m_endVMA = endVMA; }
158 
159  // Return size, which is (endVMA - startVMA) + sizeof(last instruction)
160  uint
161  size() const
162  { return m_size; }
163 
164  void
165  size(unsigned int size)
166  { m_size = size; }
167 
168 
169  // -------------------------------------------------------
170  // Symbolic information: availability depends upon debugging information
171  // -------------------------------------------------------
172 
173  bool
175  { return SrcFile::isValid(m_begLine); }
176 
177  const std::string&
178  filename() const
179  { return m_filenm; }
180 
181  void
182  filename(std::string& x)
183  { m_filenm = x; }
184 
186  begLine() const
187  { return m_begLine; }
188 
189  void
191  { m_begLine = x; }
192 
193  Proc*
194  parent() const
195  { return m_parent; }
196 
197  void
199  { m_parent = x; }
200 
201  // -------------------------------------------------------
202  //
203  // -------------------------------------------------------
204 
205  // Return true if virtual memory address 'vma' is within the procedure
206  // WARNING: vma must be unrelocated
207  bool
208  isIn(VMA vma) const
209  { return (m_begVMA <= vma && vma <= m_endVMA); }
210 
211  // Return the unique number assigned to this procedure
212  unsigned int
213  id() const
214  { return m_id; }
215 
216  // Return the number of instructions in the procedure (FIXME: never computed)
217  unsigned int
218  numInsns() const
219  { return m_numInsns; }
220 
221  // Return the first and last instruction in the procedure
222  Insn*
223  begInsn() const
224  { return findInsn(m_begVMA, 0); }
225 
226  Insn*
227  endInsn() const;
228 
229  // -------------------------------------------------------
230  // Convenient wrappers for the 'LM' versions of the same.
231  // -------------------------------------------------------
232 
233  MachInsn*
234  findMachInsn(VMA vma, ushort &sz) const
235  { return m_seg->lm()->findMachInsn(vma, sz); }
236 
237  Insn*
238  findInsn(VMA vma, ushort opIndex) const
239  { return m_seg->lm()->findInsn(vma, opIndex); }
240 
241  bool
242  findSrcCodeInfo(VMA vma, ushort opIndex,
243  std::string& func, std::string& file,
244  SrcFile::ln& line) const
245  { return m_seg->lm()->findSrcCodeInfo(vma, opIndex, func, file, line); }
246 
247  bool
248  findSrcCodeInfo(VMA begVMA, ushort bOpIndex,
249  VMA endVMA, ushort eOpIndex,
250  std::string& func, std::string& file,
251  SrcFile::ln& begLine, SrcFile::ln& endLine,
252  unsigned flags = 1) const
253  {
254  return m_seg->lm()->findSrcCodeInfo(begVMA, bOpIndex, endVMA, eOpIndex,
255  func, file, begLine, endLine, flags);
256  }
257 
258  // -------------------------------------------------------
259  // debugging
260  // -------------------------------------------------------
261  // Current meaning of 'flags'
262  // 0 : short dump (without instructions)
263  // 1 : full dump
264 
265  std::string
266  toString(int flags = LM::DUMP_Short) const;
267 
268  virtual void
269  dump(std::ostream& o = std::cerr,
270  int flags = LM::DUMP_Short, const char* pre = "") const;
271 
272  void
273  ddump() const;
274 
275  friend class ProcInsnIterator;
276 
277 
278  // -------------------------------------------------------
279  //
280  // -------------------------------------------------------
281 
282  static bool
283  isProcBFDSym(asymbol* sym)
284  {
285  flagword flg = sym->flags;
286 
287  return ( ((flg & BSF_FUNCTION) && !bfd_is_und_section(sym->section))
288  ||
289  ((flg & BSF_GLOBAL) && !(flg & BSF_OBJECT)
290  && !(flg & BSF_THREAD_LOCAL)
291  && !bfd_is_abs_section(sym->section)
292  && !bfd_is_und_section(sym->section)) );
293  }
294 
295  static bool
296  isDummyProcBFDSym(asymbol* sym)
297  {
298  flagword flg = sym->flags;
299 
300  return (flg & BSF_OBJECT);
301  }
302 
303  bool
304  isDummyProc() const
305  {
306  return (m_type == Data);
307  }
308 
309 private:
310  // Should not be used
312  { }
313 
315  { }
316 
317  Proc&
319  { return *this; }
320 
321 protected:
322 private:
323  TextSeg* m_seg; // we do not own
324 
325  std::string m_name;
326  std::string m_linkname;
328 
329  VMA m_begVMA; // points to the beginning of the first instruction
330  VMA m_endVMA; // points to the beginning of the last instruction
331  unsigned int m_size;
332 
333  // symbolic information: may or may not be known
334  std::string m_filenm; // filename and
335  SrcFile::ln m_begLine; // begin line of definition, if known
336  Proc* m_parent; // parent routine, if lexically nested
337 
338  unsigned int m_id; // a unique identifier
339  unsigned int m_numInsns;
340 
341  static unsigned int nextId;
342 };
343 
344 } // namespace BinUtil
345 
346 
347 //***************************************************************************
348 // ProcInsnIterator
349 //***************************************************************************
350 
351 namespace BinUtil {
352 
353 // --------------------------------------------------------------------------
354 // 'ProcInsnIterator' enumerates a procedure's
355 // instructions maintaining relative order.
356 // --------------------------------------------------------------------------
357 
359 public:
360  ProcInsnIterator(const Proc& _p);
361  ~ProcInsnIterator();
362 
363  // Returns the current object or NULL
364  Insn*
365  current() const
366  {
367  if (it != endIt) {
368  return (*it).second;
369  }
370  else {
371  return NULL;
372  }
373  }
374 
375  // Note: This is the 'operation VMA' and may not actually be the true
376  // VMA! Use the 'Insn' for the true VMA.
377  VMA
378  currentVMA() const
379  {
380  if (it != endIt) {
381  return (*it).first;
382  }
383  else {
384  return 0;
385  }
386  }
387 
388  void
389  operator++() // prefix increment
390  { ++it; }
391 
392  void
393  operator++(int) // postfix increment
394  { it++; }
395 
396  bool
397  isValid() const
398  { return (it != endIt); }
399 
400  bool
401  isEmpty() const
402  { return (it == endIt); }
403 
404  // Reset and prepare for iteration again
405  void reset();
406 
407 private:
408  // Should not be used
410 
412 
415  { return *this; }
416 
417 protected:
418 private:
419  const Proc& p;
420  const LM& lm;
421  LM::InsnMap::const_iterator it;
422  LM::InsnMap::const_iterator endIt;
423 };
424 
425 } // namespace BinUtil
426 
427 
428 //***************************************************************************
429 
430 #endif // BinUtil_Proc_hpp
const std::string & linkName() const
Definition: Proc.hpp:131
unsigned int ln
Definition: SrcFile.hpp:66
static unsigned int nextId
Definition: Proc.hpp:341
Insn * findInsn(VMA vma, ushort opIndex) const
Definition: Proc.hpp:238
bfd_vma VMA
Definition: ISATypes.hpp:79
void name(const std::string &name)
Definition: Proc.hpp:126
VMA begVMA() const
Definition: Proc.hpp:148
Proc(const Proc &GCC_ATTR_UNUSED p)
Definition: Proc.hpp:314
LM * lm() const
Definition: Seg.hpp:105
const std::string & name() const
Definition: Proc.hpp:122
bool isValid() const
Definition: Proc.hpp:397
void endVMA(VMA endVMA)
Definition: Proc.hpp:156
std::string m_linkname
Definition: Proc.hpp:326
virtual void dump(std::ostream &o=std::cerr, int flags=LM::DUMP_Short, const char *pre="") const
Definition: Proc.cpp:132
SrcFile::ln begLine() const
Definition: Proc.hpp:186
bool isValid(SrcFile::ln line)
Definition: SrcFile.hpp:70
uint size() const
Definition: Proc.hpp:161
SrcFile::ln m_begLine
Definition: Proc.hpp:335
bool isEmpty() const
Definition: Proc.hpp:401
std::string m_name
Definition: Proc.hpp:325
bool isIn(VMA vma) const
Definition: Proc.hpp:208
Proc & operator=(const Proc &GCC_ATTR_UNUSED p)
Definition: Proc.hpp:318
bool findSrcCodeInfo(VMA vma, ushort opIndex, std::string &func, std::string &file, SrcFile::ln &line) const
Definition: Proc.hpp:242
MachInsn * findMachInsn(VMA vma, ushort &size) const
Definition: LM.cpp:503
friend class ProcInsnIterator
Definition: Proc.hpp:275
void size(unsigned int size)
Definition: Proc.hpp:165
void type(Type type)
Definition: Proc.hpp:140
bool isDummyProc() const
Definition: Proc.hpp:304
Insn * begInsn() const
Definition: Proc.hpp:223
Insn * current() const
Definition: Proc.hpp:365
TextSeg * m_seg
Definition: Proc.hpp:323
static bool isDummyProcBFDSym(asymbol *sym)
Definition: Proc.hpp:296
VMA m_begVMA
Definition: Proc.hpp:329
MachInsn * findMachInsn(VMA vma, ushort &sz) const
Definition: Proc.hpp:234
unsigned int m_numInsns
Definition: Proc.hpp:339
unsigned short int ushort
Definition: uint.h:120
unsigned int uint
Definition: uint.h:124
Type type() const
Definition: Proc.hpp:136
VMA currentVMA() const
Definition: Proc.hpp:378
unsigned int numInsns() const
Definition: Proc.hpp:218
unsigned int m_size
Definition: Proc.hpp:331
void filename(std::string &x)
Definition: Proc.hpp:182
Type m_type
Definition: Proc.hpp:327
VMA m_endVMA
Definition: Proc.hpp:330
bool findSrcCodeInfo(VMA begVMA, ushort bOpIndex, VMA endVMA, ushort eOpIndex, std::string &func, std::string &file, SrcFile::ln &begLine, SrcFile::ln &endLine, unsigned flags=1) const
Definition: Proc.hpp:248
ProcInsnIterator & operator=(const ProcInsnIterator &GCC_ATTR_UNUSED i)
Definition: Proc.hpp:414
std::string toString(int flags=LM::DUMP_Short) const
Definition: Proc.cpp:123
VMA endVMA() const
Definition: Proc.hpp:152
void ddump() const
Definition: Proc.cpp:224
void parent(Proc *x)
Definition: Proc.hpp:198
LM::InsnMap::const_iterator endIt
Definition: Proc.hpp:422
Insn * endInsn() const
Definition: Proc.cpp:109
unsigned int m_id
Definition: Proc.hpp:338
Proc * m_parent
Definition: Proc.hpp:336
LM * lm() const
Definition: Proc.hpp:115
void MachInsn
Definition: ISATypes.hpp:87
TextSeg * seg() const
Definition: Proc.hpp:111
#define NULL
Definition: ElfHelper.cpp:85
Proc * parent() const
Definition: Proc.hpp:194
static bool isProcBFDSym(asymbol *sym)
Definition: Proc.hpp:283
Insn * findInsn(VMA vma, ushort opIndex) const
Definition: LM.hpp:335
LM::InsnMap::const_iterator it
Definition: Proc.hpp:421
bool findSrcCodeInfo(VMA vma, ushort opIndex, std::string &func, std::string &file, SrcFile::ln &line)
bool hasSymbolic()
Definition: Proc.hpp:174
#define GCC_ATTR_UNUSED
Definition: gcc-attr.h:80
std::string m_filenm
Definition: Proc.hpp:334
unsigned int id() const
Definition: Proc.hpp:213
const std::string & filename() const
Definition: Proc.hpp:178
void begLine(SrcFile::ln x)
Definition: Proc.hpp:190
virtual ~Proc()
Definition: Proc.cpp:102