Initial checkin of the LLVM source-level debugger. This is still not finished,
[oota-llvm.git] / lib / Debugger / ProgramInfo.cpp
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Debugger/ProgramInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/iOther.h"
20 #include "llvm/Module.h"
21 #include "llvm/Debugger/SourceFile.h"
22 #include "llvm/Debugger/SourceLanguage.h"
23 #include "Support/FileUtilities.h"
24 #include "Support/SlowOperationInformer.h"
25 #include "Support/STLExtras.h"
26 using namespace llvm;
27
28 /// getGlobalVariablesUsing - Return all of the global variables which have the
29 /// specified value in their initializer somewhere.
30 static void getGlobalVariablesUsing(Value *V,
31                                     std::vector<GlobalVariable*> &Found) {
32   for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
33     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
34       Found.push_back(GV);
35     else if (Constant *C = dyn_cast<Constant>(*I))
36       getGlobalVariablesUsing(C, Found);
37   }
38 }
39
40 /// getStringValue - Turn an LLVM constant pointer that eventually points to a
41 /// global into a string value.  Return an empty string if we can't do it.
42 ///
43 static std::string getStringValue(Value *V, unsigned Offset = 0) {
44   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
45     if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
46       ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
47       if (Init->getType()->getElementType() == Type::SByteTy ||
48           Init->getType()->getElementType() == Type::UByteTy) {
49         std::string Result = Init->getAsString();
50         if (Offset < Result.size()) {
51           // If we are pointing INTO The string, erase the beginning...
52           Result.erase(Result.begin(), Result.begin()+Offset);
53
54           // Take off the null terminator, and any string fragments after it.
55           std::string::size_type NullPos = Result.find_first_of((char)0);
56           if (NullPos != std::string::npos)
57             Result.erase(Result.begin()+NullPos, Result.end());
58           return Result;
59         }
60       }
61     }
62   } else if (Constant *C = dyn_cast<Constant>(V)) {
63     if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C))
64       return getStringValue(CPR->getValue(), Offset);
65     else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
66       if (CE->getOpcode() == Instruction::GetElementPtr) {
67         // Turn a gep into the specified offset.
68         if (CE->getNumOperands() == 3 &&
69             cast<Constant>(CE->getOperand(1))->isNullValue() &&
70             isa<ConstantInt>(CE->getOperand(2))) {
71           return getStringValue(CE->getOperand(0),
72                    Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
73         }
74       }
75     }
76   }
77   return "";
78 }
79
80 /// getNextStopPoint - Follow the def-use chains of the specified LLVM value,
81 /// traversing the use chains until we get to a stoppoint.  When we do, return
82 /// the source location of the stoppoint.  If we don't find a stoppoint, return
83 /// null.
84 static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
85                                               unsigned &ColNo) {
86   // The use-def chains can fork.  As such, we pick the lowest numbered one we
87   // find.
88   const GlobalVariable *LastDesc = 0;
89   unsigned LastLineNo = ~0;
90   unsigned LastColNo = ~0;
91
92   for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
93        UI != E; ++UI) {
94     bool ShouldRecurse = true;
95     if (cast<Instruction>(*UI)->getOpcode() == Instruction::PHI) {
96       // Infinite loops == bad, ignore PHI nodes.
97       ShouldRecurse = false;
98     } else if (const CallInst *CI = dyn_cast<CallInst>(*UI)) {
99       // If we found a stop point, check to see if it is earlier than what we
100       // already have.  If so, remember it.
101       if (const Function *F = CI->getCalledFunction())
102         if (F->getIntrinsicID() == Intrinsic::dbg_stoppoint) {
103           unsigned CurLineNo = ~0, CurColNo = ~0;
104           const GlobalVariable *CurDesc = 0;
105           if (const ConstantInt *C = dyn_cast<ConstantInt>(CI->getOperand(2)))
106             CurLineNo = C->getRawValue();
107           if (const ConstantInt *C = dyn_cast<ConstantInt>(CI->getOperand(3)))
108             CurColNo = C->getRawValue();
109           const Value *Op = CI->getOperand(4);
110           if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Op))
111             Op = CPR->getValue();
112           
113           if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
114               (LineNo < LastLineNo ||
115                (LineNo == LastLineNo && ColNo < LastColNo))) {
116             LastDesc = CurDesc;
117             LastLineNo = CurLineNo;
118             LastColNo = CurColNo;            
119           }
120           ShouldRecurse = false;
121         }
122
123     }
124
125     // If this is not a phi node or a stopping point, recursively scan the users
126     // of this instruction to skip over region.begin's and the like.
127     if (ShouldRecurse) {
128       unsigned CurLineNo, CurColNo;
129       if (const GlobalVariable *GV = getNextStopPoint(*UI, CurLineNo,CurColNo)){
130         if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){
131           LastDesc = GV;
132           LastLineNo = CurLineNo;
133           LastColNo = CurColNo;            
134         }
135       }
136     }
137   }
138   
139   if (LastDesc) {
140     LineNo = LastLineNo != ~0U ? LastLineNo : 0;
141     ColNo  = LastColNo  != ~0U ? LastColNo : 0;
142   }
143   return LastDesc;
144 }
145
146
147 //===----------------------------------------------------------------------===//
148 // SourceFileInfo implementation
149 //
150
151 SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
152                                const SourceLanguage &Lang)
153   : Language(&Lang), Descriptor(Desc) {
154   Version = 0;
155   SourceText = 0;
156
157   if (Desc && Desc->hasInitializer())
158     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
159       if (CS->getNumOperands() > 4) {
160         if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(1)))
161           Version = CUI->getValue();
162         
163         BaseName  = getStringValue(CS->getOperand(3));
164         Directory = getStringValue(CS->getOperand(4));
165       }
166 }
167
168 SourceFileInfo::~SourceFileInfo() {
169   delete SourceText;
170 }
171
172 SourceFile &SourceFileInfo::getSourceText() const {
173   // FIXME: this should take into account the source search directories!
174   if (SourceText == 0)  // Read the file in if we haven't already.
175     if (!Directory.empty() && FileOpenable(Directory+"/"+BaseName))
176       SourceText = new SourceFile(Directory+"/"+BaseName, Descriptor);
177     else
178       SourceText = new SourceFile(BaseName, Descriptor);
179   return *SourceText;
180 }
181
182
183 //===----------------------------------------------------------------------===//
184 // SourceFunctionInfo implementation
185 //
186 SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI,
187                                        const GlobalVariable *Desc)
188   : Descriptor(Desc) {
189   LineNo = ColNo = 0;
190   if (Desc && Desc->hasInitializer())
191     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
192       if (CS->getNumOperands() > 2) {
193         // Entry #1 is the file descriptor.
194         if (const ConstantPointerRef *CPR =
195             dyn_cast<ConstantPointerRef>(CS->getOperand(1)))
196           if (const GlobalVariable *GV =
197               dyn_cast<GlobalVariable>(CPR->getValue()))
198             SourceFile = &PI.getSourceFile(GV);
199
200         // Entry #2 is the function name.
201         Name = getStringValue(CS->getOperand(2));
202       }
203 }
204
205 /// getSourceLocation - This method returns the location of the first stopping
206 /// point in the function.
207 void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
208                                            unsigned &RetColNo) const {
209   // If we haven't computed this yet...
210   if (!LineNo) {
211     // Look at all of the users of the function descriptor, looking for calls to
212     // %llvm.dbg.func.start.
213     for (Value::use_const_iterator UI = Descriptor->use_begin(),
214            E = Descriptor->use_end(); UI != E; ++UI)
215       if (const CallInst *CI = dyn_cast<CallInst>(*UI))
216         if (const Function *F = CI->getCalledFunction())
217           if (F->getIntrinsicID() == Intrinsic::dbg_func_start) {
218             // We found the start of the function.  Check to see if there are
219             // any stop points on the use-list of the function start.
220             const GlobalVariable *SD = getNextStopPoint(CI, LineNo, ColNo);
221             if (SD) {             // We found the first stop point!
222               // This is just a sanity check.
223               if (getSourceFile().getDescriptor() != SD)
224                 std::cout << "WARNING: first line of function is not in the"
225                   " file that the function descriptor claims it is in.\n";
226               break;
227             }
228           }
229   }
230   RetLineNo = LineNo; RetColNo = ColNo;
231 }
232
233 //===----------------------------------------------------------------------===//
234 // ProgramInfo implementation
235 //
236
237 ProgramInfo::ProgramInfo(Module *m) : M(m) {
238   assert(M && "Cannot create program information with a null module!");
239   ProgramTimeStamp = getFileTimestamp(M->getModuleIdentifier());
240
241   SourceFilesIsComplete = false;
242   SourceFunctionsIsComplete = false;
243 }
244
245 ProgramInfo::~ProgramInfo() {
246   // Delete cached information about source program objects...
247   for (std::map<const GlobalVariable*, SourceFileInfo*>::iterator
248          I = SourceFiles.begin(), E = SourceFiles.end(); I != E; ++I)
249     delete I->second;
250   for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator
251          I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I)
252     delete I->second;
253
254   // Delete the source language caches.
255   for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
256     delete LanguageCaches[i].second;
257 }
258
259
260 //===----------------------------------------------------------------------===//
261 // SourceFileInfo tracking...
262 //
263
264 /// getSourceFile - Return source file information for the specified source file
265 /// descriptor object, adding it to the collection as needed.  This method
266 /// always succeeds (is unambiguous), and is always efficient.
267 ///
268 const SourceFileInfo &
269 ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
270   SourceFileInfo *&Result = SourceFiles[Desc];
271   if (Result) return *Result;
272
273   // Figure out what language this source file comes from...
274   unsigned LangID = 0;   // Zero is unknown language
275   if (Desc && Desc->hasInitializer())
276     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
277       if (CS->getNumOperands() > 2)
278         if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(2)))
279           LangID = CUI->getValue();
280
281   const SourceLanguage &Lang = SourceLanguage::get(LangID);
282   SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
283
284   // FIXME: this should check to see if there is already a Filename/WorkingDir
285   // pair that matches this one.  If so, we shouldn't create the duplicate!
286   //
287   SourceFileIndex.insert(std::make_pair(New->getBaseName(), New));
288   return *(Result = New);
289 }
290
291
292 /// getSourceFiles - Index all of the source files in the program and return
293 /// a mapping of it.  This information is lazily computed the first time
294 /// that it is requested.  Since this information can take a long time to
295 /// compute, the user is given a chance to cancel it.  If this occurs, an
296 /// exception is thrown.
297 const std::map<const GlobalVariable*, SourceFileInfo*> &
298 ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
299   // If we have a fully populated map, or if the client doesn't need one, just
300   // return what we have.
301   if (SourceFilesIsComplete || !RequiresCompleteMap)
302     return SourceFiles;
303
304   // Ok, all of the source file descriptors (compile_unit in dwarf terms),
305   // should be on the use list of the llvm.dbg.translation_units global.
306   //
307   GlobalVariable *Units =
308     M->getGlobalVariable("llvm.dbg.translation_units",
309                          StructType::get(std::vector<const Type*>()));
310   if (Units == 0)
311     throw "Program contains no debugging information!";
312
313   std::vector<GlobalVariable*> TranslationUnits;
314   getGlobalVariablesUsing(Units, TranslationUnits);
315
316   SlowOperationInformer SOI("building source files index");
317
318   // Loop over all of the translation units found, building the SourceFiles
319   // mapping.
320   for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
321     getSourceFile(TranslationUnits[i]);
322     SOI.progress(i+1, e);
323   }
324
325   // Ok, if we got this far, then we indexed the whole program.
326   SourceFilesIsComplete = true;
327   return SourceFiles;
328 }
329
330 /// getSourceFile - Look up the file with the specified name.  If there is
331 /// more than one match for the specified filename, prompt the user to pick
332 /// one.  If there is no source file that matches the specified name, throw
333 /// an exception indicating that we can't find the file.  Otherwise, return
334 /// the file information for that file.
335 const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) {
336   std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End;
337   getSourceFiles();
338   tie(Start, End) = SourceFileIndex.equal_range(Filename);
339   
340   if (Start == End) throw "Could not find source file '" + Filename + "'!";
341   const SourceFileInfo &SFI = *Start->second;
342   ++Start;
343   if (Start == End) return SFI;
344
345   throw "FIXME: Multiple source files with the same name not implemented!";
346 }
347
348
349 //===----------------------------------------------------------------------===//
350 // SourceFunctionInfo tracking...
351 //
352
353
354 /// getFunction - Return function information for the specified function
355 /// descriptor object, adding it to the collection as needed.  This method
356 /// always succeeds (is unambiguous), and is always efficient.
357 ///
358 const SourceFunctionInfo &
359 ProgramInfo::getFunction(const GlobalVariable *Desc) {
360   SourceFunctionInfo *&Result = SourceFunctions[Desc];
361   if (Result) return *Result;
362
363   // Figure out what language this function comes from...
364   const GlobalVariable *SourceFileDesc = 0;
365   if (Desc && Desc->hasInitializer())
366     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
367       if (CS->getNumOperands() > 0)
368         if (const ConstantPointerRef *CPR =
369             dyn_cast<ConstantPointerRef>(CS->getOperand(1)))
370           SourceFileDesc = dyn_cast<GlobalVariable>(CPR->getValue());
371
372   const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
373   return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
374 }
375
376
377 // getSourceFunctions - Index all of the functions in the program and return
378 // them.  This information is lazily computed the first time that it is
379 // requested.  Since this information can take a long time to compute, the user
380 // is given a chance to cancel it.  If this occurs, an exception is thrown.
381 const std::map<const GlobalVariable*, SourceFunctionInfo*> &
382 ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
383   if (SourceFunctionsIsComplete || !RequiresCompleteMap)
384     return SourceFunctions;
385
386   // Ok, all of the source function descriptors (subprogram in dwarf terms),
387   // should be on the use list of the llvm.dbg.translation_units global.
388   //
389   GlobalVariable *Units =
390     M->getGlobalVariable("llvm.dbg.globals",
391                          StructType::get(std::vector<const Type*>()));
392   if (Units == 0)
393     throw "Program contains no debugging information!";
394
395   std::vector<GlobalVariable*> Functions;
396   getGlobalVariablesUsing(Units, Functions);
397
398   SlowOperationInformer SOI("building functions index");
399
400   // Loop over all of the functions found, building the SourceFunctions mapping.
401   for (unsigned i = 0, e = Functions.size(); i != e; ++i) {
402     getFunction(Functions[i]);
403     SOI.progress(i+1, e);
404   }
405
406   // Ok, if we got this far, then we indexed the whole program.
407   SourceFunctionsIsComplete = true;
408   return SourceFunctions;
409 }