Correcting declaration of variable for the value of DW_AT_comp_dir of Modules.
[dyninst.git] / symtabAPI / h / Module.h
1 /*
2  * See the dyninst/COPYRIGHT file for copyright information.
3  * 
4  * We provide the Paradyn Tools (below described as "Paradyn")
5  * on an AS IS basis, and do not warrant its validity or performance.
6  * We reserve the right to update, modify, or discontinue this
7  * software at any time.  We shall have no obligation to supply such
8  * updates or modifications or any other form of support to you.
9  * 
10  * By your use of Paradyn, you understand and agree that we (or any
11  * other person or entity with proprietary rights in Paradyn) are
12  * under no obligation to provide either maintenance services,
13  * update services, notices of latent defects, or correction of
14  * defects for Paradyn.
15  * 
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  * 
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  * 
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29  */
30
31 #ifndef __MODULE__H__
32 #define __MODULE__H__
33
34 #include "symutil.h"
35 #include "Symbol.h"
36
37 #include "Annotatable.h"
38 #include "Serialization.h"
39 #include "IBSTree.h"
40 #include "IBSTree-fast.h"
41 #if defined(cap_dwarf)
42 #include "elfutils/libdw.h"
43 #endif
44 #include <boost/shared_ptr.hpp>
45 #include "RangeLookup.h"
46
47 #include "StringTable.h"
48
49 namespace Dyninst{
50         namespace SymtabAPI{
51
52                 class typeCollection;
53                 class LineInformation;
54                 class localVar;
55                 class Symtab;
56
57
58                 class SYMTAB_EXPORT Statement : public AddressRange
59                 {
60                         friend class Module;
61                         friend class LineInformation;
62                         Statement(int file_index, unsigned int line, unsigned int col = 0,
63                                           Offset start_addr = (Offset) -1L, Offset end_addr = (Offset) -1L) :
64                                         AddressRange(start_addr, end_addr),
65                                         file_index_(file_index),
66                                         line_(line),
67                                         column_(col)
68                         {
69                         }
70
71                         unsigned int file_index_; // Maybe this should be module?
72                         unsigned int line_;
73                         unsigned int column_;
74                         StringTablePtr strings_;
75                 public:
76                         StringTablePtr getStrings_() const;
77
78                         void setStrings_(StringTablePtr strings_);
79
80                 public:
81
82                         Statement() : AddressRange(0,0), file_index_(0), line_(0), column_(0)  {}
83                         struct StatementLess {
84                                 bool operator () ( const Statement &lhs, const Statement &rhs ) const;
85                         };
86
87                         typedef StatementLess LineNoTupleLess;
88                         bool operator==(const Statement &cmp) const;
89 //    bool operator==(const char* file) const {return strcmp(file, first) == 0; }
90                         bool operator==(Offset addr) const {
91                                 return AddressRange::contains(addr);
92                         }
93                         bool operator<(Offset addr) const {
94                                 return startAddr() <= addr;
95                         }
96                         bool operator>(Offset addr) const {
97                                 return !((*this) < addr || (*this == addr));
98                         }
99                         ~Statement() {}
100
101                         Offset startAddr() const { return first;}
102                         Offset endAddr() const {return second;}
103                         std::string getFile() const;
104                         unsigned int getFileIndex() const { return file_index_; }
105                         unsigned int getLine()const {return line_;}
106                         unsigned int getColumn() const { return column_; }
107                         struct addr_range {};
108                         struct line_info {};
109                         struct upper_bound {};
110
111                         typedef Statement* Ptr;
112                         typedef const Statement* ConstPtr;
113 //    typedef boost::shared_ptr<Statement> Ptr;
114 //    typedef boost::shared_ptr<const Statement> ConstPtr;
115
116                 };
117                 template <typename OS>
118                 OS& operator<<(OS& os, const Statement& s)
119                 {
120                         os << "<statement>: [" << std::hex << s.startAddr() << ", " << s.endAddr() << std::dec << ") @ " << s.getFile()
121                            << " (" << s.getFileIndex() << "): " << s.getLine();
122                         return os;
123                 }
124                 template <typename OS>
125                 OS& operator<<(OS& os, Statement* s)
126                 {
127                         os << "<statement>: [" << std::hex << s->startAddr() << ", " << s->endAddr() << std::dec << ") @ " << s->getFile()
128                            << " (" << s->getFileIndex() << "): " << s->getLine();
129                         return os;
130                 }
131
132
133                 typedef Statement LineNoTuple;
134 #define MODULE_ANNOTATABLE_CLASS AnnotatableSparse
135
136                 class SYMTAB_EXPORT Module : public LookupInterface
137                 {
138                         friend class Symtab;
139
140                 public:
141 #if defined(cap_dwarf)
142                         typedef Dwarf_Die DebugInfoT;
143 #else
144                         typedef void* DebugInfoT;
145 #endif
146
147                         Module();
148                         Module(supportedLanguages lang, Offset adr, std::string fullNm,
149                                    Symtab *img);
150                         Module(const Module &mod);
151                         bool operator==(Module &mod);
152
153                         const std::string &fileName() const;
154                         const std::string &fullName() const;
155                         bool setName(std::string newName);
156
157                         supportedLanguages language() const;
158                         void setLanguage(supportedLanguages lang);
159
160                         Offset addr() const;
161                         Symtab *exec() const;
162
163                         bool isShared() const;
164                         ~Module();
165
166             std::string getCompDir();
167
168                         // Symbol output methods
169                         virtual bool findSymbol(std::vector<Symbol *> &ret,
170                                                                         const std::string& name,
171                                                                         Symbol::SymbolType sType = Symbol::ST_UNKNOWN,
172                                                                         NameType nameType = anyName,
173                                                                         bool isRegex = false,
174                                                                         bool checkCase = false,
175                                                                         bool includeUndefined = false);
176                         virtual bool getAllSymbolsByType(std::vector<Symbol *> &ret,
177                                                                                          Symbol::SymbolType sType);
178                         virtual bool getAllSymbols(std::vector<Symbol *> &ret);
179
180
181                         // Function based methods
182                         bool getAllFunctions(std::vector<Function *>&ret);
183                         bool findFunctionByEntryOffset(Function *&ret, const Offset offset);
184                         bool findFunctionsByName(std::vector<Function *> &ret, const std::string& name,
185                                                                          NameType nameType = anyName,
186                                                                          bool isRegex = false,
187                                                                          bool checkCase = true);
188
189                         // Variable based methods
190                         bool findVariableByOffset(Variable *&ret, const Offset offset);
191                         bool findVariablesByName(std::vector<Variable *> &ret, const std::string& name,
192                                                                          NameType nameType = anyName,
193                                                                          bool isRegex = false,
194                                                                          bool checkCase = true);
195                         bool getAllVariables(std::vector<Variable *> &ret);
196
197
198                         // Type output methods
199                         virtual bool findType(Type *&type, std::string name);
200                         virtual bool findVariableType(Type *&type, std::string name);
201
202                         std::vector<Type *> *getAllTypes();
203                         std::vector<std::pair<std::string, Type *> > *getAllGlobalVars();
204
205                         typeCollection *getModuleTypes();
206
207                         /***** Local Variable Information *****/
208                         bool findLocalVariable(std::vector<localVar *>&vars, std::string name);
209
210                         /***** Line Number Information *****/
211                         bool getAddressRanges(std::vector<AddressRange >&ranges,
212                                                                   std::string lineSource, unsigned int LineNo);
213                         bool getSourceLines(std::vector<Statement::Ptr> &lines,
214                                                                 Offset addressInRange);
215                         bool getSourceLines(std::vector<LineNoTuple> &lines,
216                                                                 Offset addressInRange);
217                         bool getStatements(std::vector<Statement::Ptr> &statements);
218                         LineInformation *getLineInformation();
219                         LineInformation* parseLineInformation();
220
221                         bool setDefaultNamespacePrefix(std::string str);
222
223
224                         //  Super secret private methods that aren't really private
225                         typeCollection *getModuleTypesPrivate();
226                         void setModuleTypes(typeCollection* tc)
227                         {
228                                 typeInfo_ = tc;
229                         }
230
231                         bool setLineInfo(Dyninst::SymtabAPI::LineInformation *lineInfo);
232                         void addRange(Dyninst::Address low, Dyninst::Address high);
233                         bool hasRanges() const { return !ranges.empty() || ranges_finalized; }
234                         void addDebugInfo(Module::DebugInfoT info);
235
236                         void finalizeRanges();
237
238                 private:
239                         Dyninst::SymtabAPI::LineInformation* lineInfo_;
240                         typeCollection* typeInfo_;
241                         std::vector<Module::DebugInfoT> info_;
242
243
244                         std::string fileName_;                   // short file
245                         std::string fullName_;                   // full path to file
246                         std::string compDir_;
247                         supportedLanguages language_;
248                         Offset addr_;                      // starting address of module
249                         Symtab *exec_;
250                         std::set<AddressRange > ranges;
251
252                         StringTablePtr strings_;
253                 public:
254                         StringTablePtr & getStrings() ;
255
256                 private:
257                         bool ranges_finalized;
258
259                         void finalizeOneRange(Address ext_s, Address ext_e) const;
260                 };
261                 template <typename OS>
262                 OS& operator<<(OS& os, const Module& m)
263                 {
264                         os << m.fileName() << ": " << m.addr();
265                         return os;
266                 }
267                 template <typename OS>
268                 OS& operator<<(OS& os, Module* m)
269                 {
270                         os << m->fileName() << ": " << m->addr();
271                         return os;
272                 }
273
274                 typedef Dyninst::SimpleInterval<Offset, Module*> ModRange;
275
276                 inline bool operator==(Offset off, const ModRange& r) {
277                         return (r.low() <= off) && (off < r.high());
278                 }
279                 inline bool operator==(const ModRange& r, Offset off) {
280                         return off == r;
281                 }
282                 template<typename OS>
283                 OS& operator<<(OS& os, const ModRange& m)
284                 {
285                         os << m.id() << ": [" << m.low() << ", " << m.high() << ")";
286                         return os;
287                 }
288
289
290         }//namespace SymtabAPI
291
292 }//namespace Dyninst
293
294
295 #endif