1 //===-- ProgramInfo.cpp - Compute and cache info about a program ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ProgramInfo and related classes, by sorting through
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"
28 /// getGlobalVariablesUsing - Return all of the global variables which have the
29 /// specified value in their initializer somewhere.
30 static 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))
35 else if (Constant *C = dyn_cast<Constant>(*I))
36 getGlobalVariablesUsing(C, Found);
40 /// 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
44 static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
46 // The use-def chains can fork. As such, we pick the lowest numbered one we
48 const GlobalVariable *LastDesc = 0;
49 unsigned LastLineNo = ~0;
50 unsigned LastColNo = ~0;
52 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
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)) {
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 (CI->getCalledFunction())
63 if (const DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(CI)) {
64 unsigned CurLineNo = SPI->getLine();
65 unsigned CurColNo = SPI->getColumn();
66 const GlobalVariable *CurDesc = 0;
67 const Value *Op = SPI->getContext();
69 if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
70 (LineNo < LastLineNo ||
71 (LineNo == LastLineNo && ColNo < LastColNo))) {
73 LastLineNo = CurLineNo;
76 ShouldRecurse = false;
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.
83 unsigned CurLineNo, CurColNo;
84 if (const GlobalVariable *GV = getNextStopPoint(*UI, CurLineNo,CurColNo)){
85 if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){
87 LastLineNo = CurLineNo;
95 LineNo = LastLineNo != ~0U ? LastLineNo : 0;
96 ColNo = LastColNo != ~0U ? LastColNo : 0;
102 //===----------------------------------------------------------------------===//
103 // SourceFileInfo implementation
106 SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
107 const SourceLanguage &Lang)
108 : Language(&Lang), Descriptor(Desc) {
112 if (Desc && Desc->hasInitializer())
113 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
114 if (CS->getNumOperands() > 4) {
115 if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(1)))
116 Version = CUI->getZExtValue();
118 BaseName = CS->getOperand(3)->getStringValue();
119 Directory = CS->getOperand(4)->getStringValue();
123 SourceFileInfo::~SourceFileInfo() {
127 SourceFile &SourceFileInfo::getSourceText() const {
128 // FIXME: this should take into account the source search directories!
129 if (SourceText == 0) { // Read the file in if we haven't already.
131 if (!Directory.empty())
132 tmpPath.set(Directory);
133 tmpPath.appendComponent(BaseName);
134 if (tmpPath.canRead())
135 SourceText = new SourceFile(tmpPath.toString(), Descriptor);
137 SourceText = new SourceFile(BaseName, Descriptor);
143 //===----------------------------------------------------------------------===//
144 // SourceFunctionInfo implementation
146 SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI,
147 const GlobalVariable *Desc)
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.
154 if (const GlobalVariable *GV =
155 dyn_cast<GlobalVariable>(CS->getOperand(1)))
156 SourceFile = &PI.getSourceFile(GV);
158 // Entry #2 is the function name.
159 Name = CS->getOperand(2)->getStringValue();
163 /// getSourceLocation - This method returns the location of the first stopping
164 /// point in the function.
165 void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
166 unsigned &RetColNo) const {
167 // If we haven't computed this yet...
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)
182 cout << "WARNING: first line of function is not in the"
183 << " file that the function descriptor claims it is in.\n";
188 RetLineNo = LineNo; RetColNo = ColNo;
191 //===----------------------------------------------------------------------===//
192 // ProgramInfo implementation
195 ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
196 assert(M && "Cannot create program information with a null module!");
197 sys::PathWithStatus ModPath(M->getModuleIdentifier());
198 const sys::FileStatus *Stat = ModPath.getFileStatus();
200 ProgramTimeStamp = Stat->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)
211 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator
212 I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I)
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 (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(2)))
240 LangID = CUI->getZExtValue();
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)
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*>()));
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
281 for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
282 getSourceFile(TranslationUnits[i]);
283 if (SOI.progress(i+1, e))
284 throw "While building source files index, operation cancelled.";
287 // Ok, if we got this far, then we indexed the whole program.
288 SourceFilesIsComplete = true;
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.
297 const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) {
298 std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End;
300 tie(Start, End) = SourceFileIndex.equal_range(Filename);
302 if (Start == End) throw "Could not find source file '" + Filename + "'!";
303 const SourceFileInfo &SFI = *Start->second;
305 if (Start == End) return SFI;
307 throw "FIXME: Multiple source files with the same name not implemented!";
311 //===----------------------------------------------------------------------===//
312 // SourceFunctionInfo tracking...
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.
320 const SourceFunctionInfo &
321 ProgramInfo::getFunction(const GlobalVariable *Desc) {
322 SourceFunctionInfo *&Result = SourceFunctions[Desc];
323 if (Result) return *Result;
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)
330 if (const GlobalVariable *GV =
331 dyn_cast<GlobalVariable>(CS->getOperand(1)))
334 const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
335 return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
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.
343 const std::map<const GlobalVariable*, SourceFunctionInfo*> &
344 ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
345 if (SourceFunctionsIsComplete || !RequiresCompleteMap)
346 return SourceFunctions;
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.
351 GlobalVariable *Units =
352 M->getGlobalVariable("llvm.dbg.globals",
353 StructType::get(std::vector<const Type*>()));
355 throw "Program contains no debugging information!";
357 std::vector<GlobalVariable*> Functions;
358 getGlobalVariablesUsing(Units, Functions);
360 SlowOperationInformer SOI("building functions index");
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]);
365 if (SOI.progress(i+1, e))
366 throw "While functions index, operation cancelled.";
369 // Ok, if we got this far, then we indexed the whole program.
370 SourceFunctionsIsComplete = true;
371 return SourceFunctions;