HPCToolkit
Proc.cpp
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 //************************* System Include Files ****************************
61 
62 #include <iostream>
63 using std::hex;
64 using std::dec;
65 using std::showbase;
66 using std::endl;
67 
68 #include <string>
69 using std::string;
70 
71 #include <sstream>
72 
73 //*************************** User Include Files ****************************
74 
75 #include "Proc.hpp"
76 #include "Insn.hpp"
77 
79 
80 //*************************** Forward Declarations **************************
81 
82 //***************************************************************************
83 
84 //***************************************************************************
85 // Proc
86 //***************************************************************************
87 
88 unsigned int BinUtil::Proc::nextId = 0;
89 
91  const string& name, const string& linkname,
92  BinUtil::Proc::Type t, VMA begVMA, VMA endVMA,
93  unsigned int size)
94  : m_seg(seg), m_name(name), m_linkname(linkname), m_type(t), m_begVMA(begVMA),
95  m_endVMA(endVMA), m_size(size), m_filenm(""), m_begLine(0), m_parent(NULL)
96 {
97  m_id = nextId++;
98  m_numInsns = 0;
99 }
100 
101 
103 {
104  m_seg = NULL;
105 }
106 
107 
110 {
111  Insn* insn = findInsn(m_endVMA, 0);
112  if (insn) {
113  ushort numOps = insn->numOps();
114  if (numOps != 0) {
115  insn = findInsn(m_endVMA, (ushort)(numOps - 1)); // opIndex is 0-based
116  }
117  }
118  return insn;
119 }
120 
121 
122 string
123 BinUtil::Proc::toString(int flags) const
124 {
125  std::ostringstream os;
126  dump(os, flags);
127  return os.str();
128 }
129 
130 
131 void
132 BinUtil::Proc::dump(std::ostream& os, int flags, const char* pre) const
133 {
134  string p(pre);
135  string p1 = p + " ";
136  string p2 = p + " ";
137 
138  string proc, file, b_proc, b_file, e_proc, e_file;
139  SrcFile::ln begLn, endLn, b_begLn, e_endLn2;
140  Insn* eInsn = endInsn();
141  ushort endOp = (eInsn) ? eInsn->opIndex() : (ushort)0;
142 
143  // This call performs some consistency checking
144  m_seg->findSrcCodeInfo(begVMA(), 0, endVMA(), endOp,
145  proc, file, begLn, endLn);
146 
147  // These calls perform no consistency checking
148  m_seg->findSrcCodeInfo(begVMA(), 0, b_proc, b_file, b_begLn);
149  m_seg->findSrcCodeInfo(endVMA(), endOp, e_proc, e_file, e_endLn2);
150 
151  string nm = BinUtil::canonicalizeProcName(name());
152  string ln_nm = BinUtil::canonicalizeProcName(linkName());
153 
154  os << p << "---------- Procedure Dump ----------\n";
155  os << p << " Name: `" << nm << "'\n";
156  os << p << " LinkName: `" << ln_nm << "'\n";
157  os << p << " Sym: {" << filename() << "}:" << begLine() << "\n";
158  os << p << " LnMap: {" << file << "}["
159  << BinUtil::canonicalizeProcName(proc) <<"]:" << begLn << "-" << endLn << "\n";
160  os << p << " LnMap(b): {" << b_file << "}["
161  << BinUtil::canonicalizeProcName(b_proc) << "]:" << b_begLn << "\n";
162  os << p << " LnMap(e): {" << e_file << "}["
163  << BinUtil::canonicalizeProcName(e_proc) << "]:" << e_endLn2 << "\n";
164 
165  os << p << " ID, Type: " << id() << ", `";
166  switch (type()) {
167  case Local: os << "Local'\n"; break;
168  case Weak: os << "Weak'\n"; break;
169  case Global: os << "Global'\n"; break;
170  case Quasi: os << "Quasi'\n"; break;
171  default: os << "-unknown-'\n";
172  DIAG_Die("Unknown Procedure type: " << type());
173  }
174  os << showbase
175  << p << " VMA: [" << hex << begVMA() << ", " << endVMA() << dec << "]\n";
176  os << p << " Size(b): " << size() << "\n";
177 
178  if ((flags & LM::DUMP_Flg_Insn_ty)
179  || (flags & LM::DUMP_Flg_Insn_decode)) {
180  os << p1 << "----- Instruction Dump -----\n";
181  for (ProcInsnIterator it(*this); it.isValid(); ++it) {
182  Insn* insn = it.current();
183 
184  if (flags & LM::DUMP_Flg_Insn_decode) {
185  os << p2 << hex << insn->vma() << dec << ": ";
186  insn->decode(os);
187  os << endl;
188  }
189  if (flags & LM::DUMP_Flg_Insn_ty) {
190  insn->dump(os, flags, p2.c_str());
191  }
192 
193  if (flags & LM::DUMP_Flg_Sym) {
194  VMA vma = insn->vma();
195  ushort opIdx = insn->opIndex();
196 
197  string proc1, file1;
198  SrcFile::ln line;
199  m_seg->findSrcCodeInfo(vma, opIdx, proc1, file1, line);
200  proc1 = BinUtil::canonicalizeProcName(proc1);
201 
202  os << p2 << " ";
203  if (file1 == filename()) {
204  os << "-";
205  }
206  else {
207  os << "!{" << file1 << "}";
208  }
209  os << ":" << line << ":";
210  if (proc1 == nm || proc1 == ln_nm) {
211  os << "-";
212  }
213  else {
214  os << "![" << proc1 << "]";
215  }
216  os << "\n";
217  }
218  }
219  }
220 }
221 
222 
223 void
225 {
226  dump(std::cerr);
227 }
228 
229 
230 //***************************************************************************
231 // ProcInsnIterator
232 //***************************************************************************
233 
235  : p(_p), lm(*(p.lm()))
236 {
237  reset();
238 }
239 
240 
242 {
243 }
244 
245 
246 void
248 {
249  it = lm.m_insnMap.find(p.m_begVMA);
250  endIt = lm.m_insnMap.find(p.m_endVMA);
251 
252  if (it != lm.m_insnMap.end()) {
253  // We have at least one instruction: p.endVMA should have been found
254  DIAG_Assert(endIt != lm.m_insnMap.end(), "Internal error!");
255 
256  endIt++; // 'endIt' is now one past the last valid instruction
257 
258  // We need to ensure that all VLIW instructions that match this
259  // vma are also included. Push 'endIt' back as needed; when done it
260  // should remain one past the last valid instruction
261  for (; // ((*endIt).second) returns Insn*
262  (endIt != lm.m_insnMap.end() && endIt->second->vma() == p.m_endVMA);
263  endIt++)
264  { }
265  }
266  else {
267  // 'it' == end ==> p.begVMA == p.endVMA (but not the reverse)
268  DIAG_Assert(p.m_begVMA == p.m_endVMA, "Internal error!");
269  }
270 }
const std::string & linkName() const
Definition: Proc.hpp:131
unsigned int ln
Definition: SrcFile.hpp:66
static unsigned int nextId
Definition: Proc.hpp:341
string canonicalizeProcName(const std::string &name, ProcNameMgr *procNameMgr)
Definition: BinUtils.cpp:69
Insn * findInsn(VMA vma, ushort opIndex) const
Definition: Proc.hpp:238
bfd_vma VMA
Definition: ISATypes.hpp:79
VMA begVMA() const
Definition: Proc.hpp:148
const std::string & name() const
Definition: Proc.hpp:122
bool isValid() const
Definition: Proc.hpp:397
virtual ushort numOps() const =0
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
uint size() const
Definition: Proc.hpp:161
virtual ushort opIndex() const =0
virtual void decode(std::ostream &os)
Definition: Insn.hpp:183
TextSeg * m_seg
Definition: Proc.hpp:323
VMA m_begVMA
Definition: Proc.hpp:329
unsigned short int ushort
Definition: uint.h:120
Type type() const
Definition: Proc.hpp:136
virtual void dump(std::ostream &o=std::cerr, int flags=LM::DUMP_Short, const char *pre="") const
Definition: Insn.cpp:87
VMA m_endVMA
Definition: Proc.hpp:330
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
LM::InsnMap::const_iterator endIt
Definition: Proc.hpp:422
Insn * endInsn() const
Definition: Proc.cpp:109
LM * lm() const
Definition: Proc.hpp:115
VMA vma() const
Definition: Insn.hpp:125
#define NULL
Definition: ElfHelper.cpp:85
bool findSrcCodeInfo(VMA vma, ushort opIndex, std::string &func, std::string &file, SrcFile::ln &line) const
Definition: Seg.hpp:150
LM::InsnMap::const_iterator it
Definition: Proc.hpp:421
#define DIAG_Die(...)
Definition: diagnostics.h:267
unsigned int id() const
Definition: Proc.hpp:213
const std::string & filename() const
Definition: Proc.hpp:178
InsnMap m_insnMap
Definition: LM.hpp:560
virtual ~Proc()
Definition: Proc.cpp:102