blob: ebca794320f4f5b30edb5a8bc2d4eb946c011b3b [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"
Chris Lattner4ab78e02004-07-29 17:15:38 +000019#include "llvm/Instructions.h"
Chris Lattner2eacf262004-01-05 05:25:10 +000020#include "llvm/Module.h"
21#include "llvm/Debugger/SourceFile.h"
22#include "llvm/Debugger/SourceLanguage.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/SlowOperationInformer.h"
24#include "llvm/ADT/STLExtras.h"
Reid Spencer954da372004-07-04 12:19:56 +000025#include <iostream>
26
Chris Lattner2eacf262004-01-05 05:25:10 +000027using namespace llvm;
28
29/// getGlobalVariablesUsing - Return all of the global variables which have the
30/// specified value in their initializer somewhere.
31static void getGlobalVariablesUsing(Value *V,
32 std::vector<GlobalVariable*> &Found) {
33 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
34 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
35 Found.push_back(GV);
36 else if (Constant *C = dyn_cast<Constant>(*I))
37 getGlobalVariablesUsing(C, Found);
38 }
39}
40
Chris Lattner2eacf262004-01-05 05:25:10 +000041/// getNextStopPoint - Follow the def-use chains of the specified LLVM value,
42/// traversing the use chains until we get to a stoppoint. When we do, return
43/// the source location of the stoppoint. If we don't find a stoppoint, return
44/// null.
45static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
46 unsigned &ColNo) {
47 // The use-def chains can fork. As such, we pick the lowest numbered one we
48 // find.
49 const GlobalVariable *LastDesc = 0;
50 unsigned LastLineNo = ~0;
51 unsigned LastColNo = ~0;
52
53 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
54 UI != E; ++UI) {
55 bool ShouldRecurse = true;
56 if (cast<Instruction>(*UI)->getOpcode() == Instruction::PHI) {
57 // Infinite loops == bad, ignore PHI nodes.
58 ShouldRecurse = false;
59 } else if (const CallInst *CI = dyn_cast<CallInst>(*UI)) {
60 // If we found a stop point, check to see if it is earlier than what we
61 // already have. If so, remember it.
62 if (const Function *F = CI->getCalledFunction())
63 if (F->getIntrinsicID() == Intrinsic::dbg_stoppoint) {
64 unsigned CurLineNo = ~0, CurColNo = ~0;
65 const GlobalVariable *CurDesc = 0;
66 if (const ConstantInt *C = dyn_cast<ConstantInt>(CI->getOperand(2)))
67 CurLineNo = C->getRawValue();
68 if (const ConstantInt *C = dyn_cast<ConstantInt>(CI->getOperand(3)))
69 CurColNo = C->getRawValue();
70 const Value *Op = CI->getOperand(4);
Misha Brukmanedf128a2005-04-21 22:36:52 +000071
Chris Lattner2eacf262004-01-05 05:25:10 +000072 if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
73 (LineNo < LastLineNo ||
74 (LineNo == LastLineNo && ColNo < LastColNo))) {
75 LastDesc = CurDesc;
76 LastLineNo = CurLineNo;
Misha Brukmanedf128a2005-04-21 22:36:52 +000077 LastColNo = CurColNo;
Chris Lattner2eacf262004-01-05 05:25:10 +000078 }
79 ShouldRecurse = false;
80 }
81
82 }
83
84 // If this is not a phi node or a stopping point, recursively scan the users
85 // of this instruction to skip over region.begin's and the like.
86 if (ShouldRecurse) {
87 unsigned CurLineNo, CurColNo;
88 if (const GlobalVariable *GV = getNextStopPoint(*UI, CurLineNo,CurColNo)){
89 if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){
90 LastDesc = GV;
91 LastLineNo = CurLineNo;
Misha Brukmanedf128a2005-04-21 22:36:52 +000092 LastColNo = CurColNo;
Chris Lattner2eacf262004-01-05 05:25:10 +000093 }
94 }
95 }
96 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000097
Chris Lattner2eacf262004-01-05 05:25:10 +000098 if (LastDesc) {
99 LineNo = LastLineNo != ~0U ? LastLineNo : 0;
100 ColNo = LastColNo != ~0U ? LastColNo : 0;
101 }
102 return LastDesc;
103}
104
105
106//===----------------------------------------------------------------------===//
107// SourceFileInfo implementation
108//
109
110SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
111 const SourceLanguage &Lang)
112 : Language(&Lang), Descriptor(Desc) {
113 Version = 0;
114 SourceText = 0;
115
116 if (Desc && Desc->hasInitializer())
117 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
118 if (CS->getNumOperands() > 4) {
119 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(1)))
120 Version = CUI->getValue();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000121
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000122 BaseName = CS->getOperand(3)->getStringValue();
123 Directory = CS->getOperand(4)->getStringValue();
Chris Lattner2eacf262004-01-05 05:25:10 +0000124 }
125}
126
127SourceFileInfo::~SourceFileInfo() {
128 delete SourceText;
129}
130
131SourceFile &SourceFileInfo::getSourceText() const {
132 // FIXME: this should take into account the source search directories!
Reid Spencer663601c2004-12-13 02:59:15 +0000133 if (SourceText == 0) { // Read the file in if we haven't already.
134 sys::Path tmpPath;
135 if (!Directory.empty())
Reid Spencerdd04df02005-07-07 23:21:43 +0000136 tmpPath.set(Directory);
137 tmpPath.appendComponent(BaseName);
Reid Spencerc7f08322005-07-07 18:21:42 +0000138 if (tmpPath.canRead())
Reid Spencer663601c2004-12-13 02:59:15 +0000139 SourceText = new SourceFile(tmpPath.toString(), Descriptor);
Chris Lattner2eacf262004-01-05 05:25:10 +0000140 else
141 SourceText = new SourceFile(BaseName, Descriptor);
Reid Spencer663601c2004-12-13 02:59:15 +0000142 }
Chris Lattner2eacf262004-01-05 05:25:10 +0000143 return *SourceText;
144}
145
146
147//===----------------------------------------------------------------------===//
148// SourceFunctionInfo implementation
149//
150SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI,
151 const GlobalVariable *Desc)
152 : Descriptor(Desc) {
153 LineNo = ColNo = 0;
154 if (Desc && Desc->hasInitializer())
155 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
156 if (CS->getNumOperands() > 2) {
157 // Entry #1 is the file descriptor.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000158 if (const GlobalVariable *GV =
Reid Spencer518310c2004-07-18 00:44:37 +0000159 dyn_cast<GlobalVariable>(CS->getOperand(1)))
160 SourceFile = &PI.getSourceFile(GV);
Chris Lattner2eacf262004-01-05 05:25:10 +0000161
162 // Entry #2 is the function name.
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000163 Name = CS->getOperand(2)->getStringValue();
Chris Lattner2eacf262004-01-05 05:25:10 +0000164 }
165}
166
167/// getSourceLocation - This method returns the location of the first stopping
168/// point in the function.
169void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
170 unsigned &RetColNo) const {
171 // If we haven't computed this yet...
172 if (!LineNo) {
173 // Look at all of the users of the function descriptor, looking for calls to
174 // %llvm.dbg.func.start.
175 for (Value::use_const_iterator UI = Descriptor->use_begin(),
176 E = Descriptor->use_end(); UI != E; ++UI)
177 if (const CallInst *CI = dyn_cast<CallInst>(*UI))
178 if (const Function *F = CI->getCalledFunction())
179 if (F->getIntrinsicID() == Intrinsic::dbg_func_start) {
180 // We found the start of the function. Check to see if there are
181 // any stop points on the use-list of the function start.
182 const GlobalVariable *SD = getNextStopPoint(CI, LineNo, ColNo);
183 if (SD) { // We found the first stop point!
184 // This is just a sanity check.
185 if (getSourceFile().getDescriptor() != SD)
186 std::cout << "WARNING: first line of function is not in the"
187 " file that the function descriptor claims it is in.\n";
188 break;
189 }
190 }
191 }
192 RetLineNo = LineNo; RetColNo = ColNo;
193}
194
195//===----------------------------------------------------------------------===//
196// ProgramInfo implementation
197//
198
Reid Spencer9d88d1a2004-12-13 17:01:53 +0000199ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
Chris Lattner2eacf262004-01-05 05:25:10 +0000200 assert(M && "Cannot create program information with a null module!");
Reid Spencer9d88d1a2004-12-13 17:01:53 +0000201 sys::Path modulePath(M->getModuleIdentifier());
202 ProgramTimeStamp = modulePath.getTimestamp();
Chris Lattner2eacf262004-01-05 05:25:10 +0000203
204 SourceFilesIsComplete = false;
205 SourceFunctionsIsComplete = false;
206}
207
208ProgramInfo::~ProgramInfo() {
209 // Delete cached information about source program objects...
210 for (std::map<const GlobalVariable*, SourceFileInfo*>::iterator
211 I = SourceFiles.begin(), E = SourceFiles.end(); I != E; ++I)
212 delete I->second;
213 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator
214 I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I)
215 delete I->second;
216
217 // Delete the source language caches.
218 for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
219 delete LanguageCaches[i].second;
220}
221
222
223//===----------------------------------------------------------------------===//
224// SourceFileInfo tracking...
225//
226
227/// getSourceFile - Return source file information for the specified source file
228/// descriptor object, adding it to the collection as needed. This method
229/// always succeeds (is unambiguous), and is always efficient.
230///
231const SourceFileInfo &
232ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
233 SourceFileInfo *&Result = SourceFiles[Desc];
234 if (Result) return *Result;
235
236 // Figure out what language this source file comes from...
237 unsigned LangID = 0; // Zero is unknown language
238 if (Desc && Desc->hasInitializer())
239 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
240 if (CS->getNumOperands() > 2)
241 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(2)))
242 LangID = CUI->getValue();
243
244 const SourceLanguage &Lang = SourceLanguage::get(LangID);
245 SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
246
247 // FIXME: this should check to see if there is already a Filename/WorkingDir
248 // pair that matches this one. If so, we shouldn't create the duplicate!
249 //
250 SourceFileIndex.insert(std::make_pair(New->getBaseName(), New));
251 return *(Result = New);
252}
253
254
255/// getSourceFiles - Index all of the source files in the program and return
256/// a mapping of it. This information is lazily computed the first time
257/// that it is requested. Since this information can take a long time to
258/// compute, the user is given a chance to cancel it. If this occurs, an
259/// exception is thrown.
260const std::map<const GlobalVariable*, SourceFileInfo*> &
261ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
262 // If we have a fully populated map, or if the client doesn't need one, just
263 // return what we have.
264 if (SourceFilesIsComplete || !RequiresCompleteMap)
265 return SourceFiles;
266
267 // Ok, all of the source file descriptors (compile_unit in dwarf terms),
268 // should be on the use list of the llvm.dbg.translation_units global.
269 //
270 GlobalVariable *Units =
271 M->getGlobalVariable("llvm.dbg.translation_units",
272 StructType::get(std::vector<const Type*>()));
273 if (Units == 0)
274 throw "Program contains no debugging information!";
275
276 std::vector<GlobalVariable*> TranslationUnits;
277 getGlobalVariablesUsing(Units, TranslationUnits);
278
279 SlowOperationInformer SOI("building source files index");
280
281 // Loop over all of the translation units found, building the SourceFiles
282 // mapping.
283 for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
284 getSourceFile(TranslationUnits[i]);
285 SOI.progress(i+1, e);
286 }
287
288 // Ok, if we got this far, then we indexed the whole program.
289 SourceFilesIsComplete = true;
290 return SourceFiles;
291}
292
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.
298const 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);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000302
Chris Lattner2eacf262004-01-05 05:25:10 +0000303 if (Start == End) throw "Could not find source file '" + Filename + "'!";
304 const SourceFileInfo &SFI = *Start->second;
305 ++Start;
306 if (Start == End) return SFI;
307
308 throw "FIXME: Multiple source files with the same name not implemented!";
309}
310
311
312//===----------------------------------------------------------------------===//
313// SourceFunctionInfo tracking...
314//
315
316
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.
320///
321const SourceFunctionInfo &
322ProgramInfo::getFunction(const GlobalVariable *Desc) {
323 SourceFunctionInfo *&Result = SourceFunctions[Desc];
324 if (Result) return *Result;
325
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)
Reid Spencer518310c2004-07-18 00:44:37 +0000331 if (const GlobalVariable *GV =
332 dyn_cast<GlobalVariable>(CS->getOperand(1)))
333 SourceFileDesc = GV;
Chris Lattner2eacf262004-01-05 05:25:10 +0000334
335 const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
336 return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
337}
338
339
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.
344const std::map<const GlobalVariable*, SourceFunctionInfo*> &
345ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
346 if (SourceFunctionsIsComplete || !RequiresCompleteMap)
347 return SourceFunctions;
348
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.
351 //
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!";
357
358 std::vector<GlobalVariable*> Functions;
359 getGlobalVariablesUsing(Units, Functions);
360
361 SlowOperationInformer SOI("building functions index");
362
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 SOI.progress(i+1, e);
367 }
368
369 // Ok, if we got this far, then we indexed the whole program.
370 SourceFunctionsIsComplete = true;
371 return SourceFunctions;
372}