2009-05-14 20:16:46 +02:00
|
|
|
//===- DbgInfoPrinter.cpp - Print debug info in a human readable form ------==//
|
2008-12-16 10:09:19 +01:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a pass that prints instructions, and associated debug
|
|
|
|
// info:
|
2009-05-14 20:16:46 +02:00
|
|
|
//
|
2008-12-16 10:09:19 +01:00
|
|
|
// - source/line/col information
|
|
|
|
// - original variable name
|
|
|
|
// - original type name
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/IntrinsicInst.h"
|
2009-03-10 14:41:26 +01:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2008-12-16 10:09:19 +01:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
|
|
|
#include "llvm/Analysis/Passes.h"
|
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
|
|
|
#include "llvm/Support/CFG.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<bool>
|
2009-05-14 20:16:46 +02:00
|
|
|
PrintDirectory("print-fullpath",
|
|
|
|
cl::desc("Print fullpath when printing debug info"),
|
|
|
|
cl::Hidden);
|
2008-12-16 10:09:19 +01:00
|
|
|
|
|
|
|
namespace {
|
2009-05-14 20:16:46 +02:00
|
|
|
class VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass {
|
|
|
|
raw_ostream &Out;
|
|
|
|
void printStopPoint(const DbgStopPointInst *DSI);
|
|
|
|
void printFuncStart(const DbgFuncStartInst *FS);
|
|
|
|
void printVariableDeclaration(const Value *V);
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification
|
|
|
|
PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
|
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
char PrintDbgInfo::ID = 0;
|
|
|
|
static RegisterPass<PrintDbgInfo> X("print-dbginfo",
|
|
|
|
"Print debug info in human readable form");
|
2008-12-16 10:09:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
|
|
|
|
|
2009-05-14 20:16:46 +02:00
|
|
|
void PrintDbgInfo::printVariableDeclaration(const Value *V) {
|
2009-03-10 14:41:26 +01:00
|
|
|
std::string DisplayName, File, Directory, Type;
|
|
|
|
unsigned LineNo;
|
2009-05-14 20:16:46 +02:00
|
|
|
|
|
|
|
if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Out << "; ";
|
|
|
|
WriteAsOperand(Out, V, false, 0);
|
|
|
|
Out << " is variable " << DisplayName
|
2009-03-10 14:41:26 +01:00
|
|
|
<< " of type " << Type << " declared at ";
|
2009-05-14 20:16:46 +02:00
|
|
|
|
|
|
|
if (PrintDirectory)
|
|
|
|
Out << Directory << "/";
|
|
|
|
|
|
|
|
Out << File << ":" << LineNo << "\n";
|
2008-12-16 10:09:19 +01:00
|
|
|
}
|
|
|
|
|
2009-05-14 20:16:46 +02:00
|
|
|
void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) {
|
2008-12-16 10:09:19 +01:00
|
|
|
if (PrintDirectory) {
|
2009-03-13 05:39:26 +01:00
|
|
|
std::string dir;
|
2009-05-14 20:16:46 +02:00
|
|
|
GetConstantStringInfo(DSI->getDirectory(), dir);
|
2009-03-13 05:39:26 +01:00
|
|
|
Out << dir << "/";
|
2008-12-16 10:09:19 +01:00
|
|
|
}
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2009-03-13 05:39:26 +01:00
|
|
|
std::string file;
|
|
|
|
GetConstantStringInfo(DSI->getFileName(), file);
|
|
|
|
Out << file << ":" << DSI->getLine();
|
2009-05-14 20:16:46 +02:00
|
|
|
|
|
|
|
if (unsigned Col = DSI->getColumn())
|
2008-12-16 10:09:19 +01:00
|
|
|
Out << ":" << Col;
|
|
|
|
}
|
|
|
|
|
2009-05-14 20:16:46 +02:00
|
|
|
void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) {
|
2008-12-16 10:09:19 +01:00
|
|
|
DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram()));
|
2009-03-13 05:39:26 +01:00
|
|
|
std::string Res1, Res2;
|
2009-05-14 20:16:46 +02:00
|
|
|
Out << "; fully qualified function name: " << Subprogram.getDisplayName(Res1)
|
2009-06-24 00:23:13 +02:00
|
|
|
<< " return type: " << Subprogram.getReturnTypeName(Res2)
|
2009-05-14 20:16:46 +02:00
|
|
|
<< " at line " << Subprogram.getLineNumber()
|
|
|
|
<< "\n\n";
|
2008-12-16 10:09:19 +01:00
|
|
|
}
|
|
|
|
|
2009-05-14 20:16:46 +02:00
|
|
|
bool PrintDbgInfo::runOnFunction(Function &F) {
|
2008-12-16 10:09:19 +01:00
|
|
|
if (F.isDeclaration())
|
|
|
|
return false;
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
Out << "function " << F.getName() << "\n\n";
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
|
|
|
|
BasicBlock *BB = I;
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
|
|
|
|
// Skip dead blocks.
|
|
|
|
continue;
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
const DbgStopPointInst *DSI = findBBStopPoint(BB);
|
|
|
|
Out << BB->getName();
|
|
|
|
Out << ":";
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
if (DSI) {
|
|
|
|
Out << "; (";
|
|
|
|
printStopPoint(DSI);
|
|
|
|
Out << ")";
|
|
|
|
}
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
Out << "\n";
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
// A dbgstoppoint's information is valid until we encounter a new one.
|
|
|
|
const DbgStopPointInst *LastDSP = DSI;
|
2009-05-14 20:16:46 +02:00
|
|
|
bool Printed = DSI != 0;
|
|
|
|
for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
|
|
|
|
i != e; ++i) {
|
2008-12-16 10:09:19 +01:00
|
|
|
if (isa<DbgInfoIntrinsic>(i)) {
|
|
|
|
if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
|
|
|
|
if (DSI->getContext() == LastDSP->getContext() &&
|
|
|
|
DSI->getLineValue() == LastDSP->getLineValue() &&
|
2009-05-14 20:16:46 +02:00
|
|
|
DSI->getColumnValue() == LastDSP->getColumnValue())
|
2008-12-16 10:09:19 +01:00
|
|
|
// Don't print same location twice.
|
|
|
|
continue;
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
LastDSP = cast<DbgStopPointInst>(i);
|
|
|
|
|
2009-05-14 20:16:46 +02:00
|
|
|
// Don't print consecutive stoppoints, use a flag to know which one we
|
|
|
|
// printed.
|
|
|
|
Printed = false;
|
2008-12-16 10:09:19 +01:00
|
|
|
} else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
|
|
|
|
printFuncStart(FS);
|
|
|
|
}
|
|
|
|
} else {
|
2009-05-14 20:16:46 +02:00
|
|
|
if (!Printed && LastDSP) {
|
2008-12-16 10:09:19 +01:00
|
|
|
Out << "; ";
|
|
|
|
printStopPoint(LastDSP);
|
|
|
|
Out << "\n";
|
2009-05-14 20:16:46 +02:00
|
|
|
Printed = true;
|
2008-12-16 10:09:19 +01:00
|
|
|
}
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
Out << *i;
|
|
|
|
printVariableDeclaration(i);
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2009-03-10 14:41:26 +01:00
|
|
|
if (const User *U = dyn_cast<User>(i)) {
|
2009-05-14 20:16:46 +02:00
|
|
|
for(unsigned i=0;i<U->getNumOperands();i++)
|
2009-03-10 14:41:26 +01:00
|
|
|
printVariableDeclaration(U->getOperand(i));
|
|
|
|
}
|
2008-12-16 10:09:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-14 20:16:46 +02:00
|
|
|
|
2008-12-16 10:09:19 +01:00
|
|
|
return false;
|
|
|
|
}
|