Made warning red.
[llvm-complete.git] / lib / Debugger / ProgramInfo.cpp
blob3bbb0ec9362275656e20239020ddac32a2797b4e
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::Path modulePath(M->getModuleIdentifier());
200 ProgramTimeStamp = modulePath.getTimestamp();
202 SourceFilesIsComplete = false;
203 SourceFunctionsIsComplete = false;
206 ProgramInfo::~ProgramInfo() {
207 // Delete cached information about source program objects...
208 for (std::map<const GlobalVariable*, SourceFileInfo*>::iterator
209 I = SourceFiles.begin(), E = SourceFiles.end(); I != E; ++I)
210 delete I->second;
211 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator
212 I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I)
213 delete I->second;
215 // Delete the source language caches.
216 for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
217 delete LanguageCaches[i].second;
221 //===----------------------------------------------------------------------===//
222 // SourceFileInfo tracking...
225 /// getSourceFile - Return source file information for the specified source file
226 /// descriptor object, adding it to the collection as needed. This method
227 /// always succeeds (is unambiguous), and is always efficient.
229 const SourceFileInfo &
230 ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
231 SourceFileInfo *&Result = SourceFiles[Desc];
232 if (Result) return *Result;
234 // Figure out what language this source file comes from...
235 unsigned LangID = 0; // Zero is unknown language
236 if (Desc && Desc->hasInitializer())
237 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
238 if (CS->getNumOperands() > 2)
239 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(2)))
240 LangID = CUI->getValue();
242 const SourceLanguage &Lang = SourceLanguage::get(LangID);
243 SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
245 // FIXME: this should check to see if there is already a Filename/WorkingDir
246 // pair that matches this one. If so, we shouldn't create the duplicate!
248 SourceFileIndex.insert(std::make_pair(New->getBaseName(), New));
249 return *(Result = New);
253 /// getSourceFiles - Index all of the source files in the program and return
254 /// a mapping of it. This information is lazily computed the first time
255 /// that it is requested. Since this information can take a long time to
256 /// compute, the user is given a chance to cancel it. If this occurs, an
257 /// exception is thrown.
258 const std::map<const GlobalVariable*, SourceFileInfo*> &
259 ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
260 // If we have a fully populated map, or if the client doesn't need one, just
261 // return what we have.
262 if (SourceFilesIsComplete || !RequiresCompleteMap)
263 return SourceFiles;
265 // Ok, all of the source file descriptors (compile_unit in dwarf terms),
266 // should be on the use list of the llvm.dbg.translation_units global.
268 GlobalVariable *Units =
269 M->getGlobalVariable("llvm.dbg.translation_units",
270 StructType::get(std::vector<const Type*>()));
271 if (Units == 0)
272 throw "Program contains no debugging information!";
274 std::vector<GlobalVariable*> TranslationUnits;
275 getGlobalVariablesUsing(Units, TranslationUnits);
277 SlowOperationInformer SOI("building source files index");
279 // Loop over all of the translation units found, building the SourceFiles
280 // mapping.
281 for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
282 getSourceFile(TranslationUnits[i]);
283 SOI.progress(i+1, e);
286 // Ok, if we got this far, then we indexed the whole program.
287 SourceFilesIsComplete = true;
288 return SourceFiles;
291 /// getSourceFile - Look up the file with the specified name. If there is
292 /// more than one match for the specified filename, prompt the user to pick
293 /// one. If there is no source file that matches the specified name, throw
294 /// an exception indicating that we can't find the file. Otherwise, return
295 /// the file information for that file.
296 const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) {
297 std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End;
298 getSourceFiles();
299 tie(Start, End) = SourceFileIndex.equal_range(Filename);
301 if (Start == End) throw "Could not find source file '" + Filename + "'!";
302 const SourceFileInfo &SFI = *Start->second;
303 ++Start;
304 if (Start == End) return SFI;
306 throw "FIXME: Multiple source files with the same name not implemented!";
310 //===----------------------------------------------------------------------===//
311 // SourceFunctionInfo tracking...
315 /// getFunction - Return function information for the specified function
316 /// descriptor object, adding it to the collection as needed. This method
317 /// always succeeds (is unambiguous), and is always efficient.
319 const SourceFunctionInfo &
320 ProgramInfo::getFunction(const GlobalVariable *Desc) {
321 SourceFunctionInfo *&Result = SourceFunctions[Desc];
322 if (Result) return *Result;
324 // Figure out what language this function comes from...
325 const GlobalVariable *SourceFileDesc = 0;
326 if (Desc && Desc->hasInitializer())
327 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
328 if (CS->getNumOperands() > 0)
329 if (const GlobalVariable *GV =
330 dyn_cast<GlobalVariable>(CS->getOperand(1)))
331 SourceFileDesc = GV;
333 const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
334 return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
338 // getSourceFunctions - Index all of the functions in the program and return
339 // them. This information is lazily computed the first time that it is
340 // requested. Since this information can take a long time to compute, the user
341 // is given a chance to cancel it. If this occurs, an exception is thrown.
342 const std::map<const GlobalVariable*, SourceFunctionInfo*> &
343 ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
344 if (SourceFunctionsIsComplete || !RequiresCompleteMap)
345 return SourceFunctions;
347 // Ok, all of the source function descriptors (subprogram in dwarf terms),
348 // should be on the use list of the llvm.dbg.translation_units global.
350 GlobalVariable *Units =
351 M->getGlobalVariable("llvm.dbg.globals",
352 StructType::get(std::vector<const Type*>()));
353 if (Units == 0)
354 throw "Program contains no debugging information!";
356 std::vector<GlobalVariable*> Functions;
357 getGlobalVariablesUsing(Units, Functions);
359 SlowOperationInformer SOI("building functions index");
361 // Loop over all of the functions found, building the SourceFunctions mapping.
362 for (unsigned i = 0, e = Functions.size(); i != e; ++i) {
363 getFunction(Functions[i]);
364 SOI.progress(i+1, e);
367 // Ok, if we got this far, then we indexed the whole program.
368 SourceFunctionsIsComplete = true;
369 return SourceFunctions;