#include "llvm/Debugger/ProgramInfo.h"
#include "llvm/Constants.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Intrinsics.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/Debugger/SourceFile.h"
#include "llvm/Debugger/SourceLanguage.h"
#include "llvm/Support/SlowOperationInformer.h"
#include "llvm/Support/Streams.h"
#include "llvm/ADT/STLExtras.h"
using namespace llvm;
static void getGlobalVariablesUsing(Value *V,
std::vector<GlobalVariable*> &Found) {
for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I))
Found.push_back(GV);
else if (Constant *C = dyn_cast<Constant>(*I))
getGlobalVariablesUsing(C, Found);
}
}
static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
unsigned &ColNo) {
const GlobalVariable *LastDesc = 0;
unsigned LastLineNo = ~0;
unsigned LastColNo = ~0;
for (Value::use_const_iterator UI = V->use_begin(), E = V->use_end();
UI != E; ++UI) {
bool ShouldRecurse = true;
if (cast<Instruction>(*UI)->getOpcode() == Instruction::PHI) {
ShouldRecurse = false;
} else if (const CallInst *CI = dyn_cast<CallInst>(*UI)) {
if (CI->getCalledFunction())
if (const DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(CI)) {
unsigned CurLineNo = SPI->getLine();
unsigned CurColNo = SPI->getColumn();
const GlobalVariable *CurDesc = 0;
const Value *Op = SPI->getContext();
if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
(LineNo < LastLineNo ||
(LineNo == LastLineNo && ColNo < LastColNo))) {
LastDesc = CurDesc;
LastLineNo = CurLineNo;
LastColNo = CurColNo;
}
ShouldRecurse = false;
}
}
if (ShouldRecurse) {
unsigned CurLineNo, CurColNo;
if (const GlobalVariable *GV = getNextStopPoint(*UI, CurLineNo,CurColNo)){
if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){
LastDesc = GV;
LastLineNo = CurLineNo;
LastColNo = CurColNo;
}
}
}
}
if (LastDesc) {
LineNo = LastLineNo != ~0U ? LastLineNo : 0;
ColNo = LastColNo != ~0U ? LastColNo : 0;
}
return LastDesc;
}
SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
const SourceLanguage &Lang)
: Language(&Lang), Descriptor(Desc) {
Version = 0;
SourceText = 0;
if (Desc && Desc->hasInitializer())
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
if (CS->getNumOperands() > 4) {
if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(1)))
Version = CUI->getZExtValue();
if (!GetConstantStringInfo(CS->getOperand(3), BaseName))
BaseName = "";
if (!GetConstantStringInfo(CS->getOperand(4), Directory))
Directory = "";
}
}
SourceFileInfo::~SourceFileInfo() {
delete SourceText;
}
SourceFile &SourceFileInfo::getSourceText() const {
if (SourceText == 0) { sys::Path tmpPath;
if (!Directory.empty())
tmpPath.set(Directory);
tmpPath.appendComponent(BaseName);
if (tmpPath.canRead())
SourceText = new SourceFile(tmpPath.toString(), Descriptor);
else
SourceText = new SourceFile(BaseName, Descriptor);
}
return *SourceText;
}
SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI,
const GlobalVariable *Desc)
: Descriptor(Desc) {
LineNo = ColNo = 0;
if (Desc && Desc->hasInitializer())
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
if (CS->getNumOperands() > 2) {
if (const GlobalVariable *GV =
dyn_cast<GlobalVariable>(CS->getOperand(1)))
SourceFile = &PI.getSourceFile(GV);
if (!GetConstantStringInfo(CS->getOperand(2), Name))
Name = "";
}
}
void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
unsigned &RetColNo) const {
if (!LineNo) {
for (Value::use_const_iterator UI = Descriptor->use_begin(),
E = Descriptor->use_end(); UI != E; ++UI)
if (const CallInst *CI = dyn_cast<CallInst>(*UI))
if (const Function *F = CI->getCalledFunction())
if (F->getIntrinsicID() == Intrinsic::dbg_func_start) {
const GlobalVariable *SD = getNextStopPoint(CI, LineNo, ColNo);
if (SD) { if (getSourceFile().getDescriptor() != SD)
cout << "WARNING: first line of function is not in the"
<< " file that the function descriptor claims it is in.\n";
break;
}
}
}
RetLineNo = LineNo; RetColNo = ColNo;
}
ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
assert(M && "Cannot create program information with a null module!");
sys::PathWithStatus ModPath(M->getModuleIdentifier());
const sys::FileStatus *Stat = ModPath.getFileStatus();
if (Stat)
ProgramTimeStamp = Stat->getTimestamp();
SourceFilesIsComplete = false;
SourceFunctionsIsComplete = false;
}
ProgramInfo::~ProgramInfo() {
for (std::map<const GlobalVariable*, SourceFileInfo*>::iterator
I = SourceFiles.begin(), E = SourceFiles.end(); I != E; ++I)
delete I->second;
for (std::map<const GlobalVariable*, SourceFunctionInfo*>::iterator
I = SourceFunctions.begin(), E = SourceFunctions.end(); I != E; ++I)
delete I->second;
for (unsigned i = 0, e = LanguageCaches.size(); i != e; ++i)
delete LanguageCaches[i].second;
}
const SourceFileInfo &
ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
SourceFileInfo *&Result = SourceFiles[Desc];
if (Result) return *Result;
unsigned LangID = 0; if (Desc && Desc->hasInitializer())
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
if (CS->getNumOperands() > 2)
if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(2)))
LangID = CUI->getZExtValue();
const SourceLanguage &Lang = SourceLanguage::get(LangID);
SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
SourceFileIndex.insert(std::make_pair(New->getBaseName(), New));
return *(Result = New);
}
const std::map<const GlobalVariable*, SourceFileInfo*> &
ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
if (SourceFilesIsComplete || !RequiresCompleteMap)
return SourceFiles;
GlobalVariable *Units =
M->getGlobalVariable("llvm.dbg.translation_units",
StructType::get(std::vector<const Type*>()));
if (Units == 0)
throw "Program contains no debugging information!";
std::vector<GlobalVariable*> TranslationUnits;
getGlobalVariablesUsing(Units, TranslationUnits);
SlowOperationInformer SOI("building source files index");
for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
getSourceFile(TranslationUnits[i]);
if (SOI.progress(i+1, e))
throw "While building source files index, operation cancelled.";
}
SourceFilesIsComplete = true;
return SourceFiles;
}
const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) {
std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End;
getSourceFiles();
tie(Start, End) = SourceFileIndex.equal_range(Filename);
if (Start == End) throw "Could not find source file '" + Filename + "'!";
const SourceFileInfo &SFI = *Start->second;
++Start;
if (Start == End) return SFI;
throw "FIXME: Multiple source files with the same name not implemented!";
}
const SourceFunctionInfo &
ProgramInfo::getFunction(const GlobalVariable *Desc) {
SourceFunctionInfo *&Result = SourceFunctions[Desc];
if (Result) return *Result;
const GlobalVariable *SourceFileDesc = 0;
if (Desc && Desc->hasInitializer())
if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
if (CS->getNumOperands() > 0)
if (const GlobalVariable *GV =
dyn_cast<GlobalVariable>(CS->getOperand(1)))
SourceFileDesc = GV;
const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
}
const std::map<const GlobalVariable*, SourceFunctionInfo*> &
ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
if (SourceFunctionsIsComplete || !RequiresCompleteMap)
return SourceFunctions;
GlobalVariable *Units =
M->getGlobalVariable("llvm.dbg.globals",
StructType::get(std::vector<const Type*>()));
if (Units == 0)
throw "Program contains no debugging information!";
std::vector<GlobalVariable*> Functions;
getGlobalVariablesUsing(Units, Functions);
SlowOperationInformer SOI("building functions index");
for (unsigned i = 0, e = Functions.size(); i != e; ++i) {
getFunction(Functions[i]);
if (SOI.progress(i+1, e))
throw "While functions index, operation cancelled.";
}
SourceFunctionsIsComplete = true;
return SourceFunctions;
}