LLVM API Documentation
00001 //===-- ProgramInfo.cpp - Compute and cache info about a program ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the ProgramInfo and related classes, by sorting through 00011 // the loaded Module. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/Debugger/ProgramInfo.h" 00016 #include "llvm/Constants.h" 00017 #include "llvm/Analysis/ValueTracking.h" 00018 #include "llvm/DerivedTypes.h" 00019 #include "llvm/Intrinsics.h" 00020 #include "llvm/IntrinsicInst.h" 00021 #include "llvm/Instructions.h" 00022 #include "llvm/Module.h" 00023 #include "llvm/Debugger/SourceFile.h" 00024 #include "llvm/Debugger/SourceLanguage.h" 00025 #include "llvm/Support/SlowOperationInformer.h" 00026 #include "llvm/Support/Streams.h" 00027 #include "llvm/ADT/STLExtras.h" 00028 using namespace llvm; 00029 00030 /// getGlobalVariablesUsing - Return all of the global variables which have the 00031 /// specified value in their initializer somewhere. 00032 static void getGlobalVariablesUsing(Value *V, 00033 std::vector<GlobalVariable*> &Found) { 00034 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) { 00035 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) 00036 Found.push_back(GV); 00037 else if (Constant *C = dyn_cast<Constant>(*I)) 00038 getGlobalVariablesUsing(C, Found); 00039 } 00040 } 00041 00042 /// getNextStopPoint - Follow the def-use chains of the specified LLVM value, 00043 /// traversing the use chains until we get to a stoppoint. When we do, return 00044 /// the source location of the stoppoint. If we don't find a stoppoint, return 00045 /// null. 00046 static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo, 00047 unsigned &ColNo) { 00048 // The use-def chains can fork. As such, we pick the lowest numbered one we 00049 // find. 00050 const GlobalVariable *LastDesc = 0; 00051 unsigned LastLineNo = ~0; 00052 unsigned LastColNo = ~0; 00053 00054 for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end(); 00055 UI != E; ++UI) { 00056 bool ShouldRecurse = true; 00057 if (cast<Instruction>(*UI)->getOpcode() == Instruction::PHI) { 00058 // Infinite loops == bad, ignore PHI nodes. 00059 ShouldRecurse = false; 00060 } else if (const CallInst *CI = dyn_cast<CallInst>(*UI)) { 00061 00062 // If we found a stop point, check to see if it is earlier than what we 00063 // already have. If so, remember it. 00064 if (CI->getCalledFunction()) 00065 if (const DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(CI)) { 00066 unsigned CurLineNo = SPI->getLine(); 00067 unsigned CurColNo = SPI->getColumn(); 00068 const GlobalVariable *CurDesc = 0; 00069 const Value *Op = SPI->getContext(); 00070 00071 if ((CurDesc = dyn_cast<GlobalVariable>(Op)) && 00072 (LineNo < LastLineNo || 00073 (LineNo == LastLineNo && ColNo < LastColNo))) { 00074 LastDesc = CurDesc; 00075 LastLineNo = CurLineNo; 00076 LastColNo = CurColNo; 00077 } 00078 ShouldRecurse = false; 00079 } 00080 } 00081 00082 // If this is not a phi node or a stopping point, recursively scan the users 00083 // of this instruction to skip over region.begin's and the like. 00084 if (ShouldRecurse) { 00085 unsigned CurLineNo, CurColNo; 00086 if (const GlobalVariable *GV = getNextStopPoint(*UI, CurLineNo,CurColNo)){ 00087 if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){ 00088 LastDesc = GV; 00089 LastLineNo = CurLineNo; 00090 LastColNo = CurColNo; 00091 } 00092 } 00093 } 00094 } 00095 00096 if (LastDesc) { 00097 LineNo = LastLineNo != ~0U ? LastLineNo : 0; 00098 ColNo = LastColNo != ~0U ? LastColNo : 0; 00099 } 00100 return LastDesc; 00101 } 00102 00103 00104 //===----------------------------------------------------------------------===// 00105 // SourceFileInfo implementation 00106 // 00107 00108 SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc, 00109 const SourceLanguage &Lang) 00110 : Language(&Lang), Descriptor(Desc) { 00111 Version = 0; 00112 SourceText = 0; 00113 00114 if (Desc && Desc->hasInitializer()) 00115 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer())) 00116 if (CS->getNumOperands() > 4) { 00117 if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(1))) 00118 Version = CUI->getZExtValue(); 00119 00120 if (!GetConstantStringInfo(CS->getOperand(3), BaseName)) 00121 BaseName = ""; 00122 if (!GetConstantStringInfo(CS->getOperand(4), Directory)) 00123 Directory = ""; 00124 } 00125 } 00126 00127 SourceFileInfo::~SourceFileInfo() { 00128 delete SourceText; 00129 } 00130 00131 SourceFile &SourceFileInfo::getSourceText() const { 00132 // FIXME: this should take into account the source search directories! 00133 if (SourceText == 0) { // Read the file in if we haven't already. 00134 sys::Path tmpPath; 00135 if (!Directory.empty()) 00136 tmpPath.set(Directory); 00137 tmpPath.appendComponent(BaseName); 00138 if (tmpPath.canRead()) 00139 SourceText = new SourceFile(tmpPath.toString(), Descriptor); 00140 else 00141 SourceText = new SourceFile(BaseName, Descriptor); 00142 } 00143 return *SourceText; 00144 } 00145 00146 00147 //===----------------------------------------------------------------------===// 00148 // SourceFunctionInfo implementation 00149 // 00150 SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI, 00151 const GlobalVariable *Desc) 00152 : Descriptor(Desc) { 00153 LineNo = ColNo = 0; 00154 if (Desc && Desc->hasInitializer()) 00155 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer())) 00156 if (CS->getNumOperands() > 2) { 00157 // Entry #1 is the file descriptor. 00158 if (const GlobalVariable *GV = 00159 dyn_cast<GlobalVariable>(CS->getOperand(1))) 00160 SourceFile = &PI.getSourceFile(GV); 00161 00162 // Entry #2 is the function name. 00163 if (!GetConstantStringInfo(CS->getOperand(2), Name)) 00164 Name = ""; 00165 } 00166 } 00167 00168 /// getSourceLocation - This method returns the location of the first stopping 00169 /// point in the function. 00170 void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo, 00171 unsigned &RetColNo) const { 00172 // If we haven't computed this yet... 00173 if (!LineNo) { 00174 // Look at all of the users of the function descriptor, looking for calls to 00175 // %llvm.dbg.func.start. 00176 for (Value::use_const_iterator UI = Descriptor->use_begin(), 00177 E = Descriptor->use_end(); UI != E; ++UI) 00178 if (const CallInst *CI = dyn_cast<CallInst>(*UI)) 00179 if (const Function *F = CI->getCalledFunction()) 00180 if (F->getIntrinsicID() == Intrinsic::dbg_func_start) { 00181 // We found the start of the function. Check to see if there are 00182 // any stop points on the use-list of the function start. 00183 const GlobalVariable *SD = getNextStopPoint(CI, LineNo, ColNo); 00184 if (SD) { // We found the first stop point! 00185 // This is just a sanity check. 00186 if (getSourceFile().getDescriptor() != SD) 00187 cout << "WARNING: first line of function is not in the" 00188 << " file that the function descriptor claims it is in.\n"; 00189 break; 00190 } 00191 } 00192 } 00193 RetLineNo = LineNo; RetColNo = ColNo; 00194 } 00195 00196 //===----------------------------------------------------------------------===// 00197 // ProgramInfo implementation 00198 // 00199 00200 ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) { 00201 assert(M && "Cannot create program information with a null module!"); 00202 sys::PathWithStatus ModPath(M->getModuleIdentifier()); 00203 const sys::FileStatus *Stat = ModPath.getFileStatus(); 00204 if (Stat) 00205 ProgramTimeStamp = Stat->getTimestamp(); 00206 00207 SourceFilesIsComplete = false; 00208 SourceFunctionsIsComplete = false; 00209 } 00210 00211 ProgramInfo::~ProgramInfo() { 00212 // Delete cached information about source program objects... 00213 for (std::map<const GlobalVariable*, SourceFileInfo*>::iterator 00214 I = SourceFiles.begin(), E = SourceFiles.end(); I != E; ++I) 00215 delete I->second; 00216 for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator 00217 I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I) 00218 delete I->second; 00219 00220 // Delete the source language caches. 00221 for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i) 00222 delete LanguageCaches[i].second; 00223 } 00224 00225 00226 //===----------------------------------------------------------------------===// 00227 // SourceFileInfo tracking... 00228 // 00229 00230 /// getSourceFile - Return source file information for the specified source file 00231 /// descriptor object, adding it to the collection as needed. This method 00232 /// always succeeds (is unambiguous), and is always efficient. 00233 /// 00234 const SourceFileInfo & 00235 ProgramInfo::getSourceFile(const GlobalVariable *Desc) { 00236 SourceFileInfo *&Result = SourceFiles[Desc]; 00237 if (Result) return *Result; 00238 00239 // Figure out what language this source file comes from... 00240 unsigned LangID = 0; // Zero is unknown language 00241 if (Desc && Desc->hasInitializer()) 00242 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer())) 00243 if (CS->getNumOperands() > 2) 00244 if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(2))) 00245 LangID = CUI->getZExtValue(); 00246 00247 const SourceLanguage &Lang = SourceLanguage::get(LangID); 00248 SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this); 00249 00250 // FIXME: this should check to see if there is already a Filename/WorkingDir 00251 // pair that matches this one. If so, we shouldn't create the duplicate! 00252 // 00253 SourceFileIndex.insert(std::make_pair(New->getBaseName(), New)); 00254 return *(Result = New); 00255 } 00256 00257 00258 /// getSourceFiles - Index all of the source files in the program and return 00259 /// a mapping of it. This information is lazily computed the first time 00260 /// that it is requested. Since this information can take a long time to 00261 /// compute, the user is given a chance to cancel it. If this occurs, an 00262 /// exception is thrown. 00263 const std::map<const GlobalVariable*, SourceFileInfo*> & 00264 ProgramInfo::getSourceFiles(bool RequiresCompleteMap) { 00265 // If we have a fully populated map, or if the client doesn't need one, just 00266 // return what we have. 00267 if (SourceFilesIsComplete || !RequiresCompleteMap) 00268 return SourceFiles; 00269 00270 // Ok, all of the source file descriptors (compile_unit in dwarf terms), 00271 // should be on the use list of the llvm.dbg.translation_units global. 00272 // 00273 GlobalVariable *Units = 00274 M->getGlobalVariable("llvm.dbg.translation_units", 00275 StructType::get(std::vector<const Type*>())); 00276 if (Units == 0) 00277 throw "Program contains no debugging information!"; 00278 00279 std::vector<GlobalVariable*> TranslationUnits; 00280 getGlobalVariablesUsing(Units, TranslationUnits); 00281 00282 SlowOperationInformer SOI("building source files index"); 00283 00284 // Loop over all of the translation units found, building the SourceFiles 00285 // mapping. 00286 for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) { 00287 getSourceFile(TranslationUnits[i]); 00288 if (SOI.progress(i+1, e)) 00289 throw "While building source files index, operation cancelled."; 00290 } 00291 00292 // Ok, if we got this far, then we indexed the whole program. 00293 SourceFilesIsComplete = true; 00294 return SourceFiles; 00295 } 00296 00297 /// getSourceFile - Look up the file with the specified name. If there is 00298 /// more than one match for the specified filename, prompt the user to pick 00299 /// one. If there is no source file that matches the specified name, throw 00300 /// an exception indicating that we can't find the file. Otherwise, return 00301 /// the file information for that file. 00302 const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) { 00303 std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End; 00304 getSourceFiles(); 00305 tie(Start, End) = SourceFileIndex.equal_range(Filename); 00306 00307 if (Start == End) throw "Could not find source file '" + Filename + "'!"; 00308 const SourceFileInfo &SFI = *Start->second; 00309 ++Start; 00310 if (Start == End) return SFI; 00311 00312 throw "FIXME: Multiple source files with the same name not implemented!"; 00313 } 00314 00315 00316 //===----------------------------------------------------------------------===// 00317 // SourceFunctionInfo tracking... 00318 // 00319 00320 00321 /// getFunction - Return function information for the specified function 00322 /// descriptor object, adding it to the collection as needed. This method 00323 /// always succeeds (is unambiguous), and is always efficient. 00324 /// 00325 const SourceFunctionInfo & 00326 ProgramInfo::getFunction(const GlobalVariable *Desc) { 00327 SourceFunctionInfo *&Result = SourceFunctions[Desc]; 00328 if (Result) return *Result; 00329 00330 // Figure out what language this function comes from... 00331 const GlobalVariable *SourceFileDesc = 0; 00332 if (Desc && Desc->hasInitializer()) 00333 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer())) 00334 if (CS->getNumOperands() > 0) 00335 if (const GlobalVariable *GV = 00336 dyn_cast<GlobalVariable>(CS->getOperand(1))) 00337 SourceFileDesc = GV; 00338 00339 const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage(); 00340 return *(Result = Lang.createSourceFunctionInfo(Desc, *this)); 00341 } 00342 00343 00344 // getSourceFunctions - Index all of the functions in the program and return 00345 // them. This information is lazily computed the first time that it is 00346 // requested. Since this information can take a long time to compute, the user 00347 // is given a chance to cancel it. If this occurs, an exception is thrown. 00348 const std::map<const GlobalVariable*, SourceFunctionInfo*> & 00349 ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) { 00350 if (SourceFunctionsIsComplete || !RequiresCompleteMap) 00351 return SourceFunctions; 00352 00353 // Ok, all of the source function descriptors (subprogram in dwarf terms), 00354 // should be on the use list of the llvm.dbg.translation_units global. 00355 // 00356 GlobalVariable *Units = 00357 M->getGlobalVariable("llvm.dbg.globals", 00358 StructType::get(std::vector<const Type*>())); 00359 if (Units == 0) 00360 throw "Program contains no debugging information!"; 00361 00362 std::vector<GlobalVariable*> Functions; 00363 getGlobalVariablesUsing(Units, Functions); 00364 00365 SlowOperationInformer SOI("building functions index"); 00366 00367 // Loop over all of the functions found, building the SourceFunctions mapping. 00368 for (unsigned i = 0, e = Functions.size(); i != e; ++i) { 00369 getFunction(Functions[i]); 00370 if (SOI.progress(i+1, e)) 00371 throw "While functions index, operation cancelled."; 00372 } 00373 00374 // Ok, if we got this far, then we indexed the whole program. 00375 SourceFunctionsIsComplete = true; 00376 return SourceFunctions; 00377 }