Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / Debugger / ProgramInfo.cpp
blobb60f5fc4bcfc7d0ee4264305d407e266b4fdfa73
1 //===-- ProgramInfo.cpp - Compute and cache info about a program ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ProgramInfo and related classes, by sorting through
11 // the loaded Module.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Debugger/ProgramInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Debugger/SourceFile.h"
23 #include "llvm/Debugger/SourceLanguage.h"
24 #include "llvm/Support/SlowOperationInformer.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include <iostream>
28 using namespace llvm;
30 /// getGlobalVariablesUsing - Return all of the global variables which have the
31 /// specified value in their initializer somewhere.
32 static void getGlobalVariablesUsing(Value *V,
33 std::vector<GlobalVariable*> &Found) {
34 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
35 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
36 Found.push_back(GV);
37 else if (Constant *C = dyn_cast<Constant>(*I))
38 getGlobalVariablesUsing(C, Found);
42 /// getNextStopPoint - Follow the def-use chains of the specified LLVM value,
43 /// traversing the use chains until we get to a stoppoint. When we do, return
44 /// the source location of the stoppoint. If we don't find a stoppoint, return
45 /// null.
46 static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
47 unsigned &ColNo) {
48 // The use-def chains can fork. As such, we pick the lowest numbered one we
49 // find.
50 const GlobalVariable *LastDesc = 0;
51 unsigned LastLineNo = ~0;
52 unsigned LastColNo = ~0;
54 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
55 UI != E; ++UI) {
56 bool ShouldRecurse = true;
57 if (cast<Instruction>(*UI)->getOpcode() == Instruction::PHI) {
58 // Infinite loops == bad, ignore PHI nodes.
59 ShouldRecurse = false;
60 } else if (const CallInst *CI = dyn_cast<CallInst>(*UI)) {
62 // If we found a stop point, check to see if it is earlier than what we
63 // already have. If so, remember it.
64 if (const Function *F = CI->getCalledFunction())
65 if (const DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(CI)) {
66 unsigned CurLineNo = SPI->getLine();
67 unsigned CurColNo = SPI->getColumn();
68 const GlobalVariable *CurDesc = 0;
69 const Value *Op = SPI->getContext();
71 if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
72 (LineNo < LastLineNo ||
73 (LineNo == LastLineNo && ColNo < LastColNo))) {
74 LastDesc = CurDesc;
75 LastLineNo = CurLineNo;
76 LastColNo = CurColNo;
78 ShouldRecurse = false;
82 // If this is not a phi node or a stopping point, recursively scan the users
83 // of this instruction to skip over region.begin's and the like.
84 if (ShouldRecurse) {
85 unsigned CurLineNo, CurColNo;
86 if (const GlobalVariable *GV = getNextStopPoint(*UI, CurLineNo,CurColNo)){
87 if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){
88 LastDesc = GV;
89 LastLineNo = CurLineNo;
90 LastColNo = CurColNo;
96 if (LastDesc) {
97 LineNo = LastLineNo != ~0U ? LastLineNo : 0;
98 ColNo = LastColNo != ~0U ? LastColNo : 0;
100 return LastDesc;
104 //===----------------------------------------------------------------------===//
105 // SourceFileInfo implementation
108 SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
109 const SourceLanguage &Lang)
110 : Language(&Lang), Descriptor(Desc) {
111 Version = 0;
112 SourceText = 0;
114 if (Desc && Desc->hasInitializer())
115 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
116 if (CS->getNumOperands() > 4) {
117 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(1)))
118 Version = CUI->getValue();
120 BaseName = CS->getOperand(3)->getStringValue();
121 Directory = CS->getOperand(4)->getStringValue();
125 SourceFileInfo::~SourceFileInfo() {
126 delete SourceText;
129 SourceFile &SourceFileInfo::getSourceText() const {
130 // FIXME: this should take into account the source search directories!
131 if (SourceText == 0) { // Read the file in if we haven't already.
132 sys::Path tmpPath;
133 if (!Directory.empty())
134 tmpPath.set(Directory);
135 tmpPath.appendComponent(BaseName);
136 if (tmpPath.canRead())
137 SourceText = new SourceFile(tmpPath.toString(), Descriptor);
138 else
139 SourceText = new SourceFile(BaseName, Descriptor);
141 return *SourceText;
145 //===----------------------------------------------------------------------===//
146 // SourceFunctionInfo implementation
148 SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI,
149 const GlobalVariable *Desc)
150 : Descriptor(Desc) {
151 LineNo = ColNo = 0;
152 if (Desc && Desc->hasInitializer())
153 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
154 if (CS->getNumOperands() > 2) {
155 // Entry #1 is the file descriptor.
156 if (const GlobalVariable *GV =
157 dyn_cast<GlobalVariable>(CS->getOperand(1)))
158 SourceFile = &PI.getSourceFile(GV);
160 // Entry #2 is the function name.
161 Name = CS->getOperand(2)->getStringValue();
165 /// getSourceLocation - This method returns the location of the first stopping
166 /// point in the function.
167 void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
168 unsigned &RetColNo) const {
169 // If we haven't computed this yet...
170 if (!LineNo) {
171 // Look at all of the users of the function descriptor, looking for calls to
172 // %llvm.dbg.func.start.
173 for (Value::use_const_iterator UI = Descriptor->use_begin(),
174 E = Descriptor->use_end(); UI != E; ++UI)
175 if (const CallInst *CI = dyn_cast<CallInst>(*UI))
176 if (const Function *F = CI->getCalledFunction())
177 if (F->getIntrinsicID() == Intrinsic::dbg_func_start) {
178 // We found the start of the function. Check to see if there are
179 // any stop points on the use-list of the function start.
180 const GlobalVariable *SD = getNextStopPoint(CI, LineNo, ColNo);
181 if (SD) { // We found the first stop point!
182 // This is just a sanity check.
183 if (getSourceFile().getDescriptor() != SD)
184 std::cout << "WARNING: first line of function is not in the"
185 " file that the function descriptor claims it is in.\n";
186 break;
190 RetLineNo = LineNo; RetColNo = ColNo;
193 //===----------------------------------------------------------------------===//
194 // ProgramInfo implementation
197 ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
198 assert(M && "Cannot create program information with a null module!");
199 sys::FileStatus Stat;
200 if (!sys::Path(M->getModuleIdentifier()).getFileStatus(Stat))
201 ProgramTimeStamp = Stat.getTimestamp();
203 SourceFilesIsComplete = false;
204 SourceFunctionsIsComplete = false;
207 ProgramInfo::~ProgramInfo() {
208 // Delete cached information about source program objects...
209 for (std::map<const GlobalVariable*, SourceFileInfo*>::iterator
210 I = SourceFiles.begin(), E = SourceFiles.end(); I != E; ++I)
211 delete I->second;
212 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator
213 I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I)
214 delete I->second;
216 // Delete the source language caches.
217 for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
218 delete LanguageCaches[i].second;
222 //===----------------------------------------------------------------------===//
223 // SourceFileInfo tracking...
226 /// getSourceFile - Return source file information for the specified source file
227 /// descriptor object, adding it to the collection as needed. This method
228 /// always succeeds (is unambiguous), and is always efficient.
230 const SourceFileInfo &
231 ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
232 SourceFileInfo *&Result = SourceFiles[Desc];
233 if (Result) return *Result;
235 // Figure out what language this source file comes from...
236 unsigned LangID = 0; // Zero is unknown language
237 if (Desc && Desc->hasInitializer())
238 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
239 if (CS->getNumOperands() > 2)
240 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(2)))
241 LangID = CUI->getValue();
243 const SourceLanguage &Lang = SourceLanguage::get(LangID);
244 SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
246 // FIXME: this should check to see if there is already a Filename/WorkingDir
247 // pair that matches this one. If so, we shouldn't create the duplicate!
249 SourceFileIndex.insert(std::make_pair(New->getBaseName(), New));
250 return *(Result = New);
254 /// getSourceFiles - Index all of the source files in the program and return
255 /// a mapping of it. This information is lazily computed the first time
256 /// that it is requested. Since this information can take a long time to
257 /// compute, the user is given a chance to cancel it. If this occurs, an
258 /// exception is thrown.
259 const std::map<const GlobalVariable*, SourceFileInfo*> &
260 ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
261 // If we have a fully populated map, or if the client doesn't need one, just
262 // return what we have.
263 if (SourceFilesIsComplete || !RequiresCompleteMap)
264 return SourceFiles;
266 // Ok, all of the source file descriptors (compile_unit in dwarf terms),
267 // should be on the use list of the llvm.dbg.translation_units global.
269 GlobalVariable *Units =
270 M->getGlobalVariable("llvm.dbg.translation_units",
271 StructType::get(std::vector<const Type*>()));
272 if (Units == 0)
273 throw "Program contains no debugging information!";
275 std::vector<GlobalVariable*> TranslationUnits;
276 getGlobalVariablesUsing(Units, TranslationUnits);
278 SlowOperationInformer SOI("building source files index");
280 // Loop over all of the translation units found, building the SourceFiles
281 // mapping.
282 for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
283 getSourceFile(TranslationUnits[i]);
284 if (SOI.progress(i+1, e))
285 throw "While building source files index, operation cancelled.";
288 // Ok, if we got this far, then we indexed the whole program.
289 SourceFilesIsComplete = true;
290 return SourceFiles;
293 /// getSourceFile - Look up the file with the specified name. If there is
294 /// more than one match for the specified filename, prompt the user to pick
295 /// one. If there is no source file that matches the specified name, throw
296 /// an exception indicating that we can't find the file. Otherwise, return
297 /// the file information for that file.
298 const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) {
299 std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End;
300 getSourceFiles();
301 tie(Start, End) = SourceFileIndex.equal_range(Filename);
303 if (Start == End) throw "Could not find source file '" + Filename + "'!";
304 const SourceFileInfo &SFI = *Start->second;
305 ++Start;
306 if (Start == End) return SFI;
308 throw "FIXME: Multiple source files with the same name not implemented!";
312 //===----------------------------------------------------------------------===//
313 // SourceFunctionInfo tracking...
317 /// getFunction - Return function information for the specified function
318 /// descriptor object, adding it to the collection as needed. This method
319 /// always succeeds (is unambiguous), and is always efficient.
321 const SourceFunctionInfo &
322 ProgramInfo::getFunction(const GlobalVariable *Desc) {
323 SourceFunctionInfo *&Result = SourceFunctions[Desc];
324 if (Result) return *Result;
326 // Figure out what language this function comes from...
327 const GlobalVariable *SourceFileDesc = 0;
328 if (Desc && Desc->hasInitializer())
329 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
330 if (CS->getNumOperands() > 0)
331 if (const GlobalVariable *GV =
332 dyn_cast<GlobalVariable>(CS->getOperand(1)))
333 SourceFileDesc = GV;
335 const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
336 return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
340 // getSourceFunctions - Index all of the functions in the program and return
341 // them. This information is lazily computed the first time that it is
342 // requested. Since this information can take a long time to compute, the user
343 // is given a chance to cancel it. If this occurs, an exception is thrown.
344 const std::map<const GlobalVariable*, SourceFunctionInfo*> &
345 ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
346 if (SourceFunctionsIsComplete || !RequiresCompleteMap)
347 return SourceFunctions;
349 // Ok, all of the source function descriptors (subprogram in dwarf terms),
350 // should be on the use list of the llvm.dbg.translation_units global.
352 GlobalVariable *Units =
353 M->getGlobalVariable("llvm.dbg.globals",
354 StructType::get(std::vector<const Type*>()));
355 if (Units == 0)
356 throw "Program contains no debugging information!";
358 std::vector<GlobalVariable*> Functions;
359 getGlobalVariablesUsing(Units, Functions);
361 SlowOperationInformer SOI("building functions index");
363 // Loop over all of the functions found, building the SourceFunctions mapping.
364 for (unsigned i = 0, e = Functions.size(); i != e; ++i) {
365 getFunction(Functions[i]);
366 if (SOI.progress(i+1, e))
367 throw "While functions index, operation cancelled.";
370 // Ok, if we got this far, then we indexed the whole program.
371 SourceFunctionsIsComplete = true;
372 return SourceFunctions;