blob: a315a2d5ba2c48f60e0fd9b16b1287d3285f62e0 [file] [log] [blame]
Chris Lattner2eacf262004-01-05 05:25:10 +00001//===-- ProgramInfo.cpp - Compute and cache info about a program ----------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner2eacf262004-01-05 05:25:10 +00003// 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.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner2eacf262004-01-05 05:25:10 +00008//===----------------------------------------------------------------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00009//
Chris Lattner2eacf262004-01-05 05:25:10 +000010// 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"
Jim Laskey43970fe2006-03-23 18:06:46 +000019#include "llvm/IntrinsicInst.h"
Chris Lattner4ab78e02004-07-29 17:15:38 +000020#include "llvm/Instructions.h"
Chris Lattner2eacf262004-01-05 05:25:10 +000021#include "llvm/Module.h"
22#include "llvm/Debugger/SourceFile.h"
23#include "llvm/Debugger/SourceLanguage.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/Support/SlowOperationInformer.h"
25#include "llvm/ADT/STLExtras.h"
Chris Lattner2eacf262004-01-05 05:25:10 +000026using namespace llvm;
27
28/// getGlobalVariablesUsing - Return all of the global variables which have the
29/// specified value in their initializer somewhere.
30static 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
Chris Lattner2eacf262004-01-05 05:25:10 +000040/// getNextStopPoint - Follow the def-use chains of the specified LLVM value,
41/// traversing the use chains until we get to a stoppoint. When we do, return
42/// the source location of the stoppoint. If we don't find a stoppoint, return
43/// null.
44static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
45 unsigned &ColNo) {
46 // The use-def chains can fork. As such, we pick the lowest numbered one we
47 // find.
48 const GlobalVariable *LastDesc = 0;
49 unsigned LastLineNo = ~0;
50 unsigned LastColNo = ~0;
51
52 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
53 UI != E; ++UI) {
54 bool ShouldRecurse = true;
55 if (cast<Instruction>(*UI)->getOpcode() == Instruction::PHI) {
56 // Infinite loops == bad, ignore PHI nodes.
57 ShouldRecurse = false;
58 } else if (const CallInst *CI = dyn_cast<CallInst>(*UI)) {
Jim Laskey43970fe2006-03-23 18:06:46 +000059
Chris Lattner2eacf262004-01-05 05:25:10 +000060 // If we found a stop point, check to see if it is earlier than what we
61 // already have. If so, remember it.
Reid Spencer3ed469c2006-11-02 20:25:50 +000062 if (CI->getCalledFunction())
Jim Laskey43970fe2006-03-23 18:06:46 +000063 if (const DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(CI)) {
64 unsigned CurLineNo = SPI->getLine();
65 unsigned CurColNo = SPI->getColumn();
Chris Lattner2eacf262004-01-05 05:25:10 +000066 const GlobalVariable *CurDesc = 0;
Jim Laskey43970fe2006-03-23 18:06:46 +000067 const Value *Op = SPI->getContext();
Misha Brukmanedf128a2005-04-21 22:36:52 +000068
Chris Lattner2eacf262004-01-05 05:25:10 +000069 if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
70 (LineNo < LastLineNo ||
71 (LineNo == LastLineNo && ColNo < LastColNo))) {
72 LastDesc = CurDesc;
73 LastLineNo = CurLineNo;
Misha Brukmanedf128a2005-04-21 22:36:52 +000074 LastColNo = CurColNo;
Chris Lattner2eacf262004-01-05 05:25:10 +000075 }
76 ShouldRecurse = false;
77 }
Chris Lattner2eacf262004-01-05 05:25:10 +000078 }
79
80 // If this is not a phi node or a stopping point, recursively scan the users
81 // of this instruction to skip over region.begin's and the like.
82 if (ShouldRecurse) {
83 unsigned CurLineNo, CurColNo;
84 if (const GlobalVariable *GV = getNextStopPoint(*UI, CurLineNo,CurColNo)){
85 if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){
86 LastDesc = GV;
87 LastLineNo = CurLineNo;
Misha Brukmanedf128a2005-04-21 22:36:52 +000088 LastColNo = CurColNo;
Chris Lattner2eacf262004-01-05 05:25:10 +000089 }
90 }
91 }
92 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000093
Chris Lattner2eacf262004-01-05 05:25:10 +000094 if (LastDesc) {
95 LineNo = LastLineNo != ~0U ? LastLineNo : 0;
96 ColNo = LastColNo != ~0U ? LastColNo : 0;
97 }
98 return LastDesc;
99}
100
101
102//===----------------------------------------------------------------------===//
103// SourceFileInfo implementation
104//
105
106SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
107 const SourceLanguage &Lang)
108 : Language(&Lang), Descriptor(Desc) {
109 Version = 0;
110 SourceText = 0;
111
112 if (Desc && Desc->hasInitializer())
113 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
114 if (CS->getNumOperands() > 4) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000115 if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(1)))
116 Version = CUI->getZExtValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000117
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000118 BaseName = CS->getOperand(3)->getStringValue();
119 Directory = CS->getOperand(4)->getStringValue();
Chris Lattner2eacf262004-01-05 05:25:10 +0000120 }
121}
122
123SourceFileInfo::~SourceFileInfo() {
124 delete SourceText;
125}
126
127SourceFile &SourceFileInfo::getSourceText() const {
128 // FIXME: this should take into account the source search directories!
Reid Spencer663601c2004-12-13 02:59:15 +0000129 if (SourceText == 0) { // Read the file in if we haven't already.
130 sys::Path tmpPath;
131 if (!Directory.empty())
Reid Spencerdd04df02005-07-07 23:21:43 +0000132 tmpPath.set(Directory);
133 tmpPath.appendComponent(BaseName);
Reid Spencerc7f08322005-07-07 18:21:42 +0000134 if (tmpPath.canRead())
Reid Spencer663601c2004-12-13 02:59:15 +0000135 SourceText = new SourceFile(tmpPath.toString(), Descriptor);
Chris Lattner2eacf262004-01-05 05:25:10 +0000136 else
137 SourceText = new SourceFile(BaseName, Descriptor);
Reid Spencer663601c2004-12-13 02:59:15 +0000138 }
Chris Lattner2eacf262004-01-05 05:25:10 +0000139 return *SourceText;
140}
141
142
143//===----------------------------------------------------------------------===//
144// SourceFunctionInfo implementation
145//
146SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI,
147 const GlobalVariable *Desc)
148 : Descriptor(Desc) {
149 LineNo = ColNo = 0;
150 if (Desc && Desc->hasInitializer())
151 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
152 if (CS->getNumOperands() > 2) {
153 // Entry #1 is the file descriptor.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000154 if (const GlobalVariable *GV =
Reid Spencer518310c2004-07-18 00:44:37 +0000155 dyn_cast<GlobalVariable>(CS->getOperand(1)))
156 SourceFile = &PI.getSourceFile(GV);
Chris Lattner2eacf262004-01-05 05:25:10 +0000157
158 // Entry #2 is the function name.
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000159 Name = CS->getOperand(2)->getStringValue();
Chris Lattner2eacf262004-01-05 05:25:10 +0000160 }
161}
162
163/// getSourceLocation - This method returns the location of the first stopping
164/// point in the function.
165void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
166 unsigned &RetColNo) const {
167 // If we haven't computed this yet...
168 if (!LineNo) {
169 // Look at all of the users of the function descriptor, looking for calls to
170 // %llvm.dbg.func.start.
171 for (Value::use_const_iterator UI = Descriptor->use_begin(),
172 E = Descriptor->use_end(); UI != E; ++UI)
173 if (const CallInst *CI = dyn_cast<CallInst>(*UI))
174 if (const Function *F = CI->getCalledFunction())
175 if (F->getIntrinsicID() == Intrinsic::dbg_func_start) {
176 // We found the start of the function. Check to see if there are
177 // any stop points on the use-list of the function start.
178 const GlobalVariable *SD = getNextStopPoint(CI, LineNo, ColNo);
179 if (SD) { // We found the first stop point!
180 // This is just a sanity check.
181 if (getSourceFile().getDescriptor() != SD)
Bill Wendlingbcd24982006-12-07 20:28:15 +0000182 cout << "WARNING: first line of function is not in the"
183 << " file that the function descriptor claims it is in.\n";
Chris Lattner2eacf262004-01-05 05:25:10 +0000184 break;
185 }
186 }
187 }
188 RetLineNo = LineNo; RetColNo = ColNo;
189}
190
191//===----------------------------------------------------------------------===//
192// ProgramInfo implementation
193//
194
Reid Spencer9d88d1a2004-12-13 17:01:53 +0000195ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
Chris Lattner2eacf262004-01-05 05:25:10 +0000196 assert(M && "Cannot create program information with a null module!");
Chris Lattner252ad032006-07-28 22:03:44 +0000197 sys::FileStatus Stat;
198 if (!sys::Path(M->getModuleIdentifier()).getFileStatus(Stat))
199 ProgramTimeStamp = Stat.getTimestamp();
Chris Lattner2eacf262004-01-05 05:25:10 +0000200
201 SourceFilesIsComplete = false;
202 SourceFunctionsIsComplete = false;
203}
204
205ProgramInfo::~ProgramInfo() {
206 // Delete cached information about source program objects...
207 for (std::map<const GlobalVariable*, SourceFileInfo*>::iterator
208 I = SourceFiles.begin(), E = SourceFiles.end(); I != E; ++I)
209 delete I->second;
210 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator
211 I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I)
212 delete I->second;
213
214 // Delete the source language caches.
215 for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
216 delete LanguageCaches[i].second;
217}
218
219
220//===----------------------------------------------------------------------===//
221// SourceFileInfo tracking...
222//
223
224/// getSourceFile - Return source file information for the specified source file
225/// descriptor object, adding it to the collection as needed. This method
226/// always succeeds (is unambiguous), and is always efficient.
227///
228const SourceFileInfo &
229ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
230 SourceFileInfo *&Result = SourceFiles[Desc];
231 if (Result) return *Result;
232
233 // Figure out what language this source file comes from...
234 unsigned LangID = 0; // Zero is unknown language
235 if (Desc && Desc->hasInitializer())
236 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
237 if (CS->getNumOperands() > 2)
Reid Spencerb83eb642006-10-20 07:07:24 +0000238 if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(2)))
239 LangID = CUI->getZExtValue();
Chris Lattner2eacf262004-01-05 05:25:10 +0000240
241 const SourceLanguage &Lang = SourceLanguage::get(LangID);
242 SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
243
244 // FIXME: this should check to see if there is already a Filename/WorkingDir
245 // pair that matches this one. If so, we shouldn't create the duplicate!
246 //
247 SourceFileIndex.insert(std::make_pair(New->getBaseName(), New));
248 return *(Result = New);
249}
250
251
252/// getSourceFiles - Index all of the source files in the program and return
253/// a mapping of it. This information is lazily computed the first time
254/// that it is requested. Since this information can take a long time to
255/// compute, the user is given a chance to cancel it. If this occurs, an
256/// exception is thrown.
257const std::map<const GlobalVariable*, SourceFileInfo*> &
258ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
259 // If we have a fully populated map, or if the client doesn't need one, just
260 // return what we have.
261 if (SourceFilesIsComplete || !RequiresCompleteMap)
262 return SourceFiles;
263
264 // Ok, all of the source file descriptors (compile_unit in dwarf terms),
265 // should be on the use list of the llvm.dbg.translation_units global.
266 //
267 GlobalVariable *Units =
268 M->getGlobalVariable("llvm.dbg.translation_units",
269 StructType::get(std::vector<const Type*>()));
270 if (Units == 0)
271 throw "Program contains no debugging information!";
272
273 std::vector<GlobalVariable*> TranslationUnits;
274 getGlobalVariablesUsing(Units, TranslationUnits);
275
276 SlowOperationInformer SOI("building source files index");
277
278 // Loop over all of the translation units found, building the SourceFiles
279 // mapping.
280 for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
281 getSourceFile(TranslationUnits[i]);
Chris Lattner148f4402006-07-06 22:34:06 +0000282 if (SOI.progress(i+1, e))
283 throw "While building source files index, operation cancelled.";
Chris Lattner2eacf262004-01-05 05:25:10 +0000284 }
285
286 // Ok, if we got this far, then we indexed the whole program.
287 SourceFilesIsComplete = true;
288 return SourceFiles;
289}
290
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.
296const 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);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000300
Chris Lattner2eacf262004-01-05 05:25:10 +0000301 if (Start == End) throw "Could not find source file '" + Filename + "'!";
302 const SourceFileInfo &SFI = *Start->second;
303 ++Start;
304 if (Start == End) return SFI;
305
306 throw "FIXME: Multiple source files with the same name not implemented!";
307}
308
309
310//===----------------------------------------------------------------------===//
311// SourceFunctionInfo tracking...
312//
313
314
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.
318///
319const SourceFunctionInfo &
320ProgramInfo::getFunction(const GlobalVariable *Desc) {
321 SourceFunctionInfo *&Result = SourceFunctions[Desc];
322 if (Result) return *Result;
323
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)
Reid Spencer518310c2004-07-18 00:44:37 +0000329 if (const GlobalVariable *GV =
330 dyn_cast<GlobalVariable>(CS->getOperand(1)))
331 SourceFileDesc = GV;
Chris Lattner2eacf262004-01-05 05:25:10 +0000332
333 const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
334 return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
335}
336
337
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.
342const std::map<const GlobalVariable*, SourceFunctionInfo*> &
343ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
344 if (SourceFunctionsIsComplete || !RequiresCompleteMap)
345 return SourceFunctions;
346
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.
349 //
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!";
355
356 std::vector<GlobalVariable*> Functions;
357 getGlobalVariablesUsing(Units, Functions);
358
359 SlowOperationInformer SOI("building functions index");
360
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]);
Chris Lattner148f4402006-07-06 22:34:06 +0000364 if (SOI.progress(i+1, e))
365 throw "While functions index, operation cancelled.";
Chris Lattner2eacf262004-01-05 05:25:10 +0000366 }
367
368 // Ok, if we got this far, then we indexed the whole program.
369 SourceFunctionsIsComplete = true;
370 return SourceFunctions;
371}