blob: 66d38f73ca853c6bd816a76012dd969063528882 [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"
Reid Spencer954da372004-07-04 12:19:56 +000026#include <iostream>
27
Chris Lattner2eacf262004-01-05 05:25:10 +000028using namespace llvm;
29
30/// getGlobalVariablesUsing - Return all of the global variables which have the
31/// specified value in their initializer somewhere.
32static 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);
39 }
40}
41
Chris Lattner2eacf262004-01-05 05:25:10 +000042/// 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.
46static 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;
53
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)) {
Jim Laskey43970fe2006-03-23 18:06:46 +000061
Chris Lattner2eacf262004-01-05 05:25:10 +000062 // 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())
Jim Laskey43970fe2006-03-23 18:06:46 +000065 if (const DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(CI)) {
66 unsigned CurLineNo = SPI->getLine();
67 unsigned CurColNo = SPI->getColumn();
Chris Lattner2eacf262004-01-05 05:25:10 +000068 const GlobalVariable *CurDesc = 0;
Jim Laskey43970fe2006-03-23 18:06:46 +000069 const Value *Op = SPI->getContext();
Misha Brukmanedf128a2005-04-21 22:36:52 +000070
Chris Lattner2eacf262004-01-05 05:25:10 +000071 if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
72 (LineNo < LastLineNo ||
73 (LineNo == LastLineNo && ColNo < LastColNo))) {
74 LastDesc = CurDesc;
75 LastLineNo = CurLineNo;
Misha Brukmanedf128a2005-04-21 22:36:52 +000076 LastColNo = CurColNo;
Chris Lattner2eacf262004-01-05 05:25:10 +000077 }
78 ShouldRecurse = false;
79 }
Chris Lattner2eacf262004-01-05 05:25:10 +000080 }
81
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;
Misha Brukmanedf128a2005-04-21 22:36:52 +000090 LastColNo = CurColNo;
Chris Lattner2eacf262004-01-05 05:25:10 +000091 }
92 }
93 }
94 }
Misha Brukmanedf128a2005-04-21 22:36:52 +000095
Chris Lattner2eacf262004-01-05 05:25:10 +000096 if (LastDesc) {
97 LineNo = LastLineNo != ~0U ? LastLineNo : 0;
98 ColNo = LastColNo != ~0U ? LastColNo : 0;
99 }
100 return LastDesc;
101}
102
103
104//===----------------------------------------------------------------------===//
105// SourceFileInfo implementation
106//
107
108SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
109 const SourceLanguage &Lang)
110 : Language(&Lang), Descriptor(Desc) {
111 Version = 0;
112 SourceText = 0;
113
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();
Misha Brukmanedf128a2005-04-21 22:36:52 +0000119
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000120 BaseName = CS->getOperand(3)->getStringValue();
121 Directory = CS->getOperand(4)->getStringValue();
Chris Lattner2eacf262004-01-05 05:25:10 +0000122 }
123}
124
125SourceFileInfo::~SourceFileInfo() {
126 delete SourceText;
127}
128
129SourceFile &SourceFileInfo::getSourceText() const {
130 // FIXME: this should take into account the source search directories!
Reid Spencer663601c2004-12-13 02:59:15 +0000131 if (SourceText == 0) { // Read the file in if we haven't already.
132 sys::Path tmpPath;
133 if (!Directory.empty())
Reid Spencerdd04df02005-07-07 23:21:43 +0000134 tmpPath.set(Directory);
135 tmpPath.appendComponent(BaseName);
Reid Spencerc7f08322005-07-07 18:21:42 +0000136 if (tmpPath.canRead())
Reid Spencer663601c2004-12-13 02:59:15 +0000137 SourceText = new SourceFile(tmpPath.toString(), Descriptor);
Chris Lattner2eacf262004-01-05 05:25:10 +0000138 else
139 SourceText = new SourceFile(BaseName, Descriptor);
Reid Spencer663601c2004-12-13 02:59:15 +0000140 }
Chris Lattner2eacf262004-01-05 05:25:10 +0000141 return *SourceText;
142}
143
144
145//===----------------------------------------------------------------------===//
146// SourceFunctionInfo implementation
147//
148SourceFunctionInfo::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.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000156 if (const GlobalVariable *GV =
Reid Spencer518310c2004-07-18 00:44:37 +0000157 dyn_cast<GlobalVariable>(CS->getOperand(1)))
158 SourceFile = &PI.getSourceFile(GV);
Chris Lattner2eacf262004-01-05 05:25:10 +0000159
160 // Entry #2 is the function name.
Jim Laskey21b6c9d2006-03-08 18:11:07 +0000161 Name = CS->getOperand(2)->getStringValue();
Chris Lattner2eacf262004-01-05 05:25:10 +0000162 }
163}
164
165/// getSourceLocation - This method returns the location of the first stopping
166/// point in the function.
167void 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;
187 }
188 }
189 }
190 RetLineNo = LineNo; RetColNo = ColNo;
191}
192
193//===----------------------------------------------------------------------===//
194// ProgramInfo implementation
195//
196
Reid Spencer9d88d1a2004-12-13 17:01:53 +0000197ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
Chris Lattner2eacf262004-01-05 05:25:10 +0000198 assert(M && "Cannot create program information with a null module!");
Reid Spencer9d88d1a2004-12-13 17:01:53 +0000199 sys::Path modulePath(M->getModuleIdentifier());
200 ProgramTimeStamp = modulePath.getTimestamp();
Chris Lattner2eacf262004-01-05 05:25:10 +0000201
202 SourceFilesIsComplete = false;
203 SourceFunctionsIsComplete = false;
204}
205
206ProgramInfo::~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;
214
215 // Delete the source language caches.
216 for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
217 delete LanguageCaches[i].second;
218}
219
220
221//===----------------------------------------------------------------------===//
222// SourceFileInfo tracking...
223//
224
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.
228///
229const SourceFileInfo &
230ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
231 SourceFileInfo *&Result = SourceFiles[Desc];
232 if (Result) return *Result;
233
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();
241
242 const SourceLanguage &Lang = SourceLanguage::get(LangID);
243 SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
244
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!
247 //
248 SourceFileIndex.insert(std::make_pair(New->getBaseName(), New));
249 return *(Result = New);
250}
251
252
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.
258const std::map<const GlobalVariable*, SourceFileInfo*> &
259ProgramInfo::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;
264
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.
267 //
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!";
273
274 std::vector<GlobalVariable*> TranslationUnits;
275 getGlobalVariablesUsing(Units, TranslationUnits);
276
277 SlowOperationInformer SOI("building source files index");
278
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]);
Chris Lattner148f4402006-07-06 22:34:06 +0000283 if (SOI.progress(i+1, e))
284 throw "While building source files index, operation cancelled.";
Chris Lattner2eacf262004-01-05 05:25:10 +0000285 }
286
287 // Ok, if we got this far, then we indexed the whole program.
288 SourceFilesIsComplete = true;
289 return SourceFiles;
290}
291
292/// getSourceFile - Look up the file with the specified name. If there is
293/// more than one match for the specified filename, prompt the user to pick
294/// one. If there is no source file that matches the specified name, throw
295/// an exception indicating that we can't find the file. Otherwise, return
296/// the file information for that file.
297const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) {
298 std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End;
299 getSourceFiles();
300 tie(Start, End) = SourceFileIndex.equal_range(Filename);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000301
Chris Lattner2eacf262004-01-05 05:25:10 +0000302 if (Start == End) throw "Could not find source file '" + Filename + "'!";
303 const SourceFileInfo &SFI = *Start->second;
304 ++Start;
305 if (Start == End) return SFI;
306
307 throw "FIXME: Multiple source files with the same name not implemented!";
308}
309
310
311//===----------------------------------------------------------------------===//
312// SourceFunctionInfo tracking...
313//
314
315
316/// getFunction - Return function information for the specified function
317/// descriptor object, adding it to the collection as needed. This method
318/// always succeeds (is unambiguous), and is always efficient.
319///
320const SourceFunctionInfo &
321ProgramInfo::getFunction(const GlobalVariable *Desc) {
322 SourceFunctionInfo *&Result = SourceFunctions[Desc];
323 if (Result) return *Result;
324
325 // Figure out what language this function comes from...
326 const GlobalVariable *SourceFileDesc = 0;
327 if (Desc && Desc->hasInitializer())
328 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
329 if (CS->getNumOperands() > 0)
Reid Spencer518310c2004-07-18 00:44:37 +0000330 if (const GlobalVariable *GV =
331 dyn_cast<GlobalVariable>(CS->getOperand(1)))
332 SourceFileDesc = GV;
Chris Lattner2eacf262004-01-05 05:25:10 +0000333
334 const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
335 return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
336}
337
338
339// getSourceFunctions - Index all of the functions in the program and return
340// them. This information is lazily computed the first time that it is
341// requested. Since this information can take a long time to compute, the user
342// is given a chance to cancel it. If this occurs, an exception is thrown.
343const std::map<const GlobalVariable*, SourceFunctionInfo*> &
344ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
345 if (SourceFunctionsIsComplete || !RequiresCompleteMap)
346 return SourceFunctions;
347
348 // Ok, all of the source function descriptors (subprogram in dwarf terms),
349 // should be on the use list of the llvm.dbg.translation_units global.
350 //
351 GlobalVariable *Units =
352 M->getGlobalVariable("llvm.dbg.globals",
353 StructType::get(std::vector<const Type*>()));
354 if (Units == 0)
355 throw "Program contains no debugging information!";
356
357 std::vector<GlobalVariable*> Functions;
358 getGlobalVariablesUsing(Units, Functions);
359
360 SlowOperationInformer SOI("building functions index");
361
362 // Loop over all of the functions found, building the SourceFunctions mapping.
363 for (unsigned i = 0, e = Functions.size(); i != e; ++i) {
364 getFunction(Functions[i]);
Chris Lattner148f4402006-07-06 22:34:06 +0000365 if (SOI.progress(i+1, e))
366 throw "While functions index, operation cancelled.";
Chris Lattner2eacf262004-01-05 05:25:10 +0000367 }
368
369 // Ok, if we got this far, then we indexed the whole program.
370 SourceFunctionsIsComplete = true;
371 return SourceFunctions;
372}