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