1 //===- ProgramInfo.h - Information about the loaded program -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines various pieces of information about the currently loaded
11 // program. One instance of this object is created every time a program is
12 // loaded, and destroyed every time it is unloaded.
14 // The various pieces of information gathered about the source program are all
15 // designed to be extended by various SourceLanguage implementations. This
16 // allows source languages to keep any extended information that they support in
17 // the derived class portions of the class.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_DEBUGGER_PROGRAMINFO_H
22 #define LLVM_DEBUGGER_PROGRAMINFO_H
24 #include "llvm/System/TimeValue.h"
36 /// SourceLanguageCache - SourceLanguage implementations are allowed to cache
37 /// stuff in the ProgramInfo object. The only requirement we have on these
38 /// instances is that they are destroyable.
39 struct SourceLanguageCache {
40 virtual ~SourceLanguageCache() {}
43 /// SourceFileInfo - One instance of this structure is created for each
44 /// source file in the program.
46 class SourceFileInfo {
47 /// BaseName - The filename of the source file.
50 /// Directory - The working directory of this source file when it was
52 std::string Directory;
54 /// Version - The version of the LLVM debug information that this file was
58 /// Language - The source language that the file was compiled with. This
59 /// pointer is never null.
61 const SourceLanguage *Language;
63 /// Descriptor - The LLVM Global Variable which describes the source file.
65 const GlobalVariable *Descriptor;
67 /// SourceText - The body of this source file, or null if it has not yet
69 mutable SourceFile *SourceText;
71 SourceFileInfo(const GlobalVariable *Desc, const SourceLanguage &Lang);
74 const std::string &getBaseName() const { return BaseName; }
75 const std::string &getDirectory() const { return Directory; }
76 unsigned getDebugVersion() const { return Version; }
77 const GlobalVariable *getDescriptor() const { return Descriptor; }
78 SourceFile &getSourceText() const;
80 const SourceLanguage &getLanguage() const { return *Language; }
84 /// SourceFunctionInfo - An instance of this class is used to represent each
85 /// source function in the program.
87 class SourceFunctionInfo {
88 /// Name - This contains an abstract name that is potentially useful to the
89 /// end-user. If there is no explicit support for the current language,
90 /// then this string is used to identify the function.
93 /// Descriptor - The descriptor for this function.
95 const GlobalVariable *Descriptor;
97 /// SourceFile - The file that this function is defined in.
99 const SourceFileInfo *SourceFile;
101 /// LineNo, ColNo - The location of the first stop-point in the function.
102 /// These are computed on demand.
103 mutable unsigned LineNo, ColNo;
106 SourceFunctionInfo(ProgramInfo &PI, const GlobalVariable *Desc);
107 virtual ~SourceFunctionInfo() {}
109 /// getSymbolicName - Return a human-readable symbolic name to identify the
110 /// function (for example, in stack traces).
111 virtual std::string getSymbolicName() const { return Name; }
113 /// getDescriptor - This returns the descriptor for the function.
115 const GlobalVariable *getDescriptor() const { return Descriptor; }
117 /// getSourceFile - This returns the source file that defines the function.
119 const SourceFileInfo &getSourceFile() const { return *SourceFile; }
121 /// getSourceLocation - This method returns the location of the first
122 /// stopping point in the function. If the body of the function cannot be
123 /// found, this returns zeros for both values.
124 void getSourceLocation(unsigned &LineNo, unsigned &ColNo) const;
128 /// ProgramInfo - This object contains information about the loaded program.
129 /// When a new program is loaded, an instance of this class is created. When
130 /// the program is unloaded, the instance is destroyed. This object basically
131 /// manages the lazy computation of information useful for the debugger.
135 /// ProgramTimeStamp - This is the timestamp of the executable file that we
136 /// currently have loaded into the debugger.
137 sys::TimeValue ProgramTimeStamp;
139 /// SourceFiles - This map is used to transform source file descriptors into
140 /// their corresponding SourceFileInfo objects. This mapping owns the
141 /// memory for the SourceFileInfo objects.
143 bool SourceFilesIsComplete;
144 std::map<const GlobalVariable*, SourceFileInfo*> SourceFiles;
146 /// SourceFileIndex - Mapping from source file basenames to the information
147 /// about the file. Note that there can be filename collisions, so this is
148 /// a multimap. This map is populated incrementally as the user interacts
149 /// with the program, through the getSourceFileFromDesc method. If ALL of
150 /// the source files are needed, the getSourceFiles() method scans the
151 /// entire program looking for them.
153 std::multimap<std::string, SourceFileInfo*> SourceFileIndex;
155 /// SourceFunctions - This map contains entries functions in the source
156 /// program. If SourceFunctionsIsComplete is true, then this is ALL of the
157 /// functions in the program are in this map.
158 bool SourceFunctionsIsComplete;
159 std::map<const GlobalVariable*, SourceFunctionInfo*> SourceFunctions;
161 /// LanguageCaches - Each source language is permitted to keep a per-program
162 /// cache of information specific to whatever it needs. This vector is
163 /// effectively a small map from the languages that are active in the
164 /// program to their caches. This can be accessed by the language by the
165 /// "getLanguageCache" method.
166 std::vector<std::pair<const SourceLanguage*,
167 SourceLanguageCache*> > LanguageCaches;
169 ProgramInfo(Module *m);
172 /// getProgramTimeStamp - Return the time-stamp of the program when it was
174 sys::TimeValue getProgramTimeStamp() const { return ProgramTimeStamp; }
176 //===------------------------------------------------------------------===//
177 // Interfaces to the source code files that make up the program.
180 /// getSourceFile - Return source file information for the specified source
181 /// file descriptor object, adding it to the collection as needed. This
182 /// method always succeeds (is unambiguous), and is always efficient.
184 const SourceFileInfo &getSourceFile(const GlobalVariable *Desc);
186 /// getSourceFile - Look up the file with the specified name. If there is
187 /// more than one match for the specified filename, prompt the user to pick
188 /// one. If there is no source file that matches the specified name, throw
189 /// an exception indicating that we can't find the file. Otherwise, return
190 /// the file information for that file.
192 /// If the source file hasn't been discovered yet in the program, this
193 /// method might have to index the whole program by calling the
194 /// getSourceFiles() method.
196 const SourceFileInfo &getSourceFile(const std::string &Filename);
198 /// getSourceFiles - Index all of the source files in the program and return
199 /// them. This information is lazily computed the first time that it is
200 /// requested. Since this information can take a long time to compute, the
201 /// user is given a chance to cancel it. If this occurs, an exception is
203 const std::map<const GlobalVariable*, SourceFileInfo*> &
204 getSourceFiles(bool RequiresCompleteMap = true);
206 //===------------------------------------------------------------------===//
207 // Interfaces to the functions that make up the program.
210 /// getFunction - Return source function information for the specified
211 /// function descriptor object, adding it to the collection as needed. This
212 /// method always succeeds (is unambiguous), and is always efficient.
214 const SourceFunctionInfo &getFunction(const GlobalVariable *Desc);
216 /// getSourceFunctions - Index all of the functions in the program and
217 /// return them. This information is lazily computed the first time that it
218 /// is requested. Since this information can take a long time to compute,
219 /// the user is given a chance to cancel it. If this occurs, an exception
221 const std::map<const GlobalVariable*, SourceFunctionInfo*> &
222 getSourceFunctions(bool RequiresCompleteMap = true);
224 /// addSourceFunctionsRead - Return true if the source functions map is
225 /// complete: that is, all functions in the program have been read in.
226 bool allSourceFunctionsRead() const { return SourceFunctionsIsComplete; }
228 /// getLanguageCache - This method is used to build per-program caches of
229 /// information, such as the functions or types visible to the program.
230 /// This can be used by SourceLanguage implementations because it requires
231 /// an accessible [sl]::CacheType typedef, where [sl] is the C++ type of the
232 /// source-language subclass.
233 template<typename SL>
234 typename SL::CacheType &getLanguageCache(const SL *L) {
235 for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
236 if (LanguageCaches[i].first == L)
237 return *(typename SL::CacheType*)LanguageCaches[i].second;
238 typename SL::CacheType *NewCache = L->createSourceLanguageCache(*this);
239 LanguageCaches.push_back(std::make_pair(L, NewCache));
244 } // end namespace llvm