2013-05-08 20:44:14 +00:00
|
|
|
//===--- DebugIR.cpp - Transform debug metadata to allow debugging IR -----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// A Module transform pass that emits a succinct version of the IR and replaces
|
|
|
|
// the source file metadata to allow debuggers to step through the IR.
|
|
|
|
//
|
2013-06-28 19:05:23 +00:00
|
|
|
// FIXME: instead of replacing debug metadata, this pass should allow for
|
|
|
|
// additional metadata to be used to point capable debuggers to the IR file
|
|
|
|
// without destroying the mapping to the original source file.
|
2013-05-08 20:44:14 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-03-04 11:26:31 +00:00
|
|
|
#include "llvm/IR/ValueMap.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "DebugIR.h"
|
2014-01-07 12:34:26 +00:00
|
|
|
#include "llvm/IR/AssemblyAnnotationWriter.h"
|
2014-03-06 00:22:06 +00:00
|
|
|
#include "llvm/IR/DIBuilder.h"
|
2013-06-28 19:05:23 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-03-06 00:46:21 +00:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2014-03-06 03:23:41 +00:00
|
|
|
#include "llvm/IR/InstVisitor.h"
|
2013-05-08 20:44:14 +00:00
|
|
|
#include "llvm/IR/Instruction.h"
|
Revert patches to add case-range support for PR1255.
The work on this project was left in an unfinished and inconsistent state.
Hopefully someone will eventually get a chance to implement this feature, but
in the meantime, it is better to put things back the way the were. I have
left support in the bitcode reader to handle the case-range bitcode format,
so that we do not lose bitcode compatibility with the llvm 3.3 release.
This reverts the following commits: 155464, 156374, 156377, 156613, 156704,
156757, 156804 156808, 156985, 157046, 157112, 157183, 157315, 157384, 157575,
157576, 157586, 157612, 157810, 157814, 157815, 157880, 157881, 157882, 157884,
157887, 157901, 158979, 157987, 157989, 158986, 158997, 159076, 159101, 159100,
159200, 159201, 159207, 159527, 159532, 159540, 159583, 159618, 159658, 159659,
159660, 159661, 159703, 159704, 160076, 167356, 172025, 186736
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190328 91177308-0d34-0410-b5e6-96231b3b80d8
2013-09-09 19:14:35 +00:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-05-08 20:44:14 +00:00
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2013-06-28 19:05:23 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Support/FormattedStream.h"
|
2013-06-28 19:05:23 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2013-06-28 19:05:23 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#define STR_HELPER(x) #x
|
|
|
|
#define STR(x) STR_HELPER(x)
|
|
|
|
|
2013-05-08 20:44:14 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "debug-ir"
|
|
|
|
|
2013-05-08 20:44:14 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
/// Builds a map of Value* to line numbers on which the Value appears in a
|
|
|
|
/// textual representation of the IR by plugging into the AssemblyWriter by
|
|
|
|
/// masquerading as an AssemblyAnnotationWriter.
|
|
|
|
class ValueToLineMap : public AssemblyAnnotationWriter {
|
|
|
|
ValueMap<const Value *, unsigned int> Lines;
|
|
|
|
typedef ValueMap<const Value *, unsigned int>::const_iterator LineIter;
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
void addEntry(const Value *V, formatted_raw_ostream &Out) {
|
|
|
|
Out.flush();
|
|
|
|
Lines.insert(std::make_pair(V, Out.getLine() + 1));
|
|
|
|
}
|
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
public:
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
/// Prints Module to a null buffer in order to build the map of Value pointers
|
|
|
|
/// to line numbers.
|
2013-06-28 19:05:23 +00:00
|
|
|
ValueToLineMap(const Module *M) {
|
2013-05-23 22:34:33 +00:00
|
|
|
raw_null_ostream ThrowAway;
|
|
|
|
M->print(ThrowAway, this);
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
// This function is called after an Instruction, GlobalValue, or GlobalAlias
|
|
|
|
// is printed.
|
2014-03-05 09:10:37 +00:00
|
|
|
void printInfoComment(const Value &V, formatted_raw_ostream &Out) override {
|
2013-06-28 19:05:23 +00:00
|
|
|
addEntry(&V, Out);
|
|
|
|
}
|
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
void emitFunctionAnnot(const Function *F,
|
|
|
|
formatted_raw_ostream &Out) override {
|
2013-06-28 19:05:23 +00:00
|
|
|
addEntry(F, Out);
|
2013-05-23 22:34:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If V appears on a line in the textual IR representation, sets Line to the
|
|
|
|
/// line number and returns true, otherwise returns false.
|
|
|
|
bool getLine(const Value *V, unsigned int &Line) const {
|
|
|
|
LineIter i = Lines.find(V);
|
|
|
|
if (i != Lines.end()) {
|
|
|
|
Line = i->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
/// Removes debug intrisncs like llvm.dbg.declare and llvm.dbg.value.
|
|
|
|
class DebugIntrinsicsRemover : public InstVisitor<DebugIntrinsicsRemover> {
|
|
|
|
void remove(Instruction &I) { I.eraseFromParent(); }
|
2013-05-08 20:44:14 +00:00
|
|
|
|
|
|
|
public:
|
2013-06-28 19:05:23 +00:00
|
|
|
static void process(Module &M) {
|
|
|
|
DebugIntrinsicsRemover Remover;
|
|
|
|
Remover.visit(&M);
|
|
|
|
}
|
2013-05-23 22:34:33 +00:00
|
|
|
void visitDbgDeclareInst(DbgDeclareInst &I) { remove(I); }
|
|
|
|
void visitDbgValueInst(DbgValueInst &I) { remove(I); }
|
|
|
|
void visitDbgInfoIntrinsic(DbgInfoIntrinsic &I) { remove(I); }
|
|
|
|
};
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
/// Removes debug metadata (!dbg) nodes from all instructions, and optionally
|
|
|
|
/// metadata named "llvm.dbg.cu" if RemoveNamedInfo is true.
|
2013-05-23 22:34:33 +00:00
|
|
|
class DebugMetadataRemover : public InstVisitor<DebugMetadataRemover> {
|
2013-06-28 19:05:23 +00:00
|
|
|
bool RemoveNamedInfo;
|
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
public:
|
2013-06-28 19:05:23 +00:00
|
|
|
static void process(Module &M, bool RemoveNamedInfo = true) {
|
|
|
|
DebugMetadataRemover Remover(RemoveNamedInfo);
|
|
|
|
Remover.run(&M);
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugMetadataRemover(bool RemoveNamedInfo)
|
|
|
|
: RemoveNamedInfo(RemoveNamedInfo) {}
|
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
void visitInstruction(Instruction &I) {
|
|
|
|
if (I.getMetadata(LLVMContext::MD_dbg))
|
2014-04-25 05:29:35 +00:00
|
|
|
I.setMetadata(LLVMContext::MD_dbg, nullptr);
|
2013-05-23 22:34:33 +00:00
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
void run(Module *M) {
|
|
|
|
// Remove debug metadata attached to instructions
|
|
|
|
visit(M);
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
if (RemoveNamedInfo) {
|
|
|
|
// Remove CU named metadata (and all children nodes)
|
|
|
|
NamedMDNode *Node = M->getNamedMetadata("llvm.dbg.cu");
|
|
|
|
if (Node)
|
|
|
|
M->eraseNamedMetadata(Node);
|
|
|
|
}
|
2013-05-23 22:34:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
/// Updates debug metadata in a Module:
|
|
|
|
/// - changes Filename/Directory to values provided on construction
|
|
|
|
/// - adds/updates line number (DebugLoc) entries associated with each
|
|
|
|
/// instruction to reflect the instruction's location in an LLVM IR file
|
|
|
|
class DIUpdater : public InstVisitor<DIUpdater> {
|
|
|
|
/// Builder of debug information
|
|
|
|
DIBuilder Builder;
|
|
|
|
|
|
|
|
/// Helper for type attributes/sizes/etc
|
|
|
|
DataLayout Layout;
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
/// Map of Value* to line numbers
|
|
|
|
const ValueToLineMap LineTable;
|
|
|
|
|
|
|
|
/// Map of Value* (in original Module) to Value* (in optional cloned Module)
|
|
|
|
const ValueToValueMapTy *VMap;
|
2013-05-23 22:34:33 +00:00
|
|
|
|
|
|
|
/// Directory of debug metadata
|
2013-06-28 19:05:23 +00:00
|
|
|
DebugInfoFinder Finder;
|
|
|
|
|
|
|
|
/// Source filename and directory
|
|
|
|
StringRef Filename;
|
|
|
|
StringRef Directory;
|
|
|
|
|
|
|
|
// CU nodes needed when creating DI subprograms
|
|
|
|
MDNode *FileNode;
|
|
|
|
MDNode *LexicalBlockFileNode;
|
|
|
|
const MDNode *CUNode;
|
|
|
|
|
|
|
|
ValueMap<const Function *, MDNode *> SubprogramDescriptors;
|
|
|
|
DenseMap<const Type *, MDNode *> TypeDescriptors;
|
2013-05-23 22:34:33 +00:00
|
|
|
|
|
|
|
public:
|
2013-06-28 19:05:23 +00:00
|
|
|
DIUpdater(Module &M, StringRef Filename = StringRef(),
|
2014-04-25 05:29:35 +00:00
|
|
|
StringRef Directory = StringRef(), const Module *DisplayM = nullptr,
|
|
|
|
const ValueToValueMapTy *VMap = nullptr)
|
2013-06-28 19:05:23 +00:00
|
|
|
: Builder(M), Layout(&M), LineTable(DisplayM ? DisplayM : &M), VMap(VMap),
|
2014-04-25 05:29:35 +00:00
|
|
|
Finder(), Filename(Filename), Directory(Directory), FileNode(nullptr),
|
|
|
|
LexicalBlockFileNode(nullptr), CUNode(nullptr) {
|
2013-06-28 19:05:23 +00:00
|
|
|
Finder.processModule(M);
|
|
|
|
visit(&M);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DIUpdater() { Builder.finalize(); }
|
|
|
|
|
|
|
|
void visitModule(Module &M) {
|
|
|
|
if (Finder.compile_unit_count() > 1)
|
|
|
|
report_fatal_error("DebugIR pass supports only a signle compile unit per "
|
|
|
|
"Module.");
|
2014-03-18 09:41:07 +00:00
|
|
|
createCompileUnit(Finder.compile_unit_count() == 1 ?
|
2014-04-25 05:29:35 +00:00
|
|
|
(MDNode*)*Finder.compile_units().begin() : nullptr);
|
2013-06-28 19:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void visitFunction(Function &F) {
|
|
|
|
if (F.isDeclaration() || findDISubprogram(&F))
|
|
|
|
return;
|
|
|
|
|
|
|
|
StringRef MangledName = F.getName();
|
|
|
|
DICompositeType Sig = createFunctionSignature(&F);
|
|
|
|
|
|
|
|
// find line of function declaration
|
|
|
|
unsigned Line = 0;
|
|
|
|
if (!findLine(&F, Line)) {
|
|
|
|
DEBUG(dbgs() << "WARNING: No line for Function " << F.getName().str()
|
|
|
|
<< "\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Instruction *FirstInst = F.begin()->begin();
|
|
|
|
unsigned ScopeLine = 0;
|
|
|
|
if (!findLine(FirstInst, ScopeLine)) {
|
|
|
|
DEBUG(dbgs() << "WARNING: No line for 1st Instruction in Function "
|
|
|
|
<< F.getName().str() << "\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Local = F.hasInternalLinkage();
|
|
|
|
bool IsDefinition = !F.isDeclaration();
|
|
|
|
bool IsOptimized = false;
|
|
|
|
|
|
|
|
int FuncFlags = llvm::DIDescriptor::FlagPrototyped;
|
|
|
|
assert(CUNode && FileNode);
|
2013-07-02 18:37:35 +00:00
|
|
|
DISubprogram Sub = Builder.createFunction(
|
2013-06-28 19:05:23 +00:00
|
|
|
DICompileUnit(CUNode), F.getName(), MangledName, DIFile(FileNode), Line,
|
|
|
|
Sig, Local, IsDefinition, ScopeLine, FuncFlags, IsOptimized, &F);
|
2013-07-02 18:37:35 +00:00
|
|
|
assert(Sub.isSubprogram());
|
2013-07-17 23:25:22 +00:00
|
|
|
DEBUG(dbgs() << "create subprogram mdnode " << *Sub << ": "
|
2013-06-28 19:05:23 +00:00
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
SubprogramDescriptors.insert(std::make_pair(&F, Sub));
|
|
|
|
}
|
2013-05-23 22:34:33 +00:00
|
|
|
|
|
|
|
void visitInstruction(Instruction &I) {
|
|
|
|
DebugLoc Loc(I.getDebugLoc());
|
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
/// If a ValueToValueMap is provided, use it to get the real instruction as
|
|
|
|
/// the line table was generated on a clone of the module on which we are
|
|
|
|
/// operating.
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *RealInst = nullptr;
|
2013-06-28 19:05:23 +00:00
|
|
|
if (VMap)
|
|
|
|
RealInst = VMap->lookup(&I);
|
|
|
|
|
|
|
|
if (!RealInst)
|
|
|
|
RealInst = &I;
|
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
unsigned Col = 0; // FIXME: support columns
|
|
|
|
unsigned Line;
|
2013-06-28 19:05:23 +00:00
|
|
|
if (!LineTable.getLine(RealInst, Line)) {
|
2013-05-23 22:34:33 +00:00
|
|
|
// Instruction has no line, it may have been removed (in the module that
|
|
|
|
// will be passed to the debugger) so there is nothing to do here.
|
2013-06-28 19:05:23 +00:00
|
|
|
DEBUG(dbgs() << "WARNING: no LineTable entry for instruction " << RealInst
|
|
|
|
<< "\n");
|
|
|
|
DEBUG(RealInst->dump());
|
2013-05-23 22:34:33 +00:00
|
|
|
return;
|
2013-06-28 19:05:23 +00:00
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
|
|
|
DebugLoc NewLoc;
|
|
|
|
if (!Loc.isUnknown())
|
|
|
|
// I had a previous debug location: re-use the DebugLoc
|
2013-06-28 19:05:23 +00:00
|
|
|
NewLoc = DebugLoc::get(Line, Col, Loc.getScope(RealInst->getContext()),
|
|
|
|
Loc.getInlinedAt(RealInst->getContext()));
|
|
|
|
else if (MDNode *scope = findScope(&I))
|
2014-04-25 05:29:35 +00:00
|
|
|
NewLoc = DebugLoc::get(Line, Col, scope, nullptr);
|
2013-06-28 19:05:23 +00:00
|
|
|
else {
|
|
|
|
DEBUG(dbgs() << "WARNING: no valid scope for instruction " << &I
|
|
|
|
<< ". no DebugLoc will be present."
|
|
|
|
<< "\n");
|
2013-05-08 20:44:14 +00:00
|
|
|
return;
|
2013-06-28 19:05:23 +00:00
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
addDebugLocation(I, NewLoc);
|
2013-05-08 20:44:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-23 22:34:33 +00:00
|
|
|
private:
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
void createCompileUnit(MDNode *CUToReplace) {
|
|
|
|
std::string Flags;
|
|
|
|
bool IsOptimized = false;
|
|
|
|
StringRef Producer;
|
|
|
|
unsigned RuntimeVersion(0);
|
|
|
|
StringRef SplitName;
|
|
|
|
|
|
|
|
if (CUToReplace) {
|
|
|
|
// save fields from existing CU to re-use in the new CU
|
|
|
|
DICompileUnit ExistingCU(CUToReplace);
|
|
|
|
Producer = ExistingCU.getProducer();
|
|
|
|
IsOptimized = ExistingCU.isOptimized();
|
|
|
|
Flags = ExistingCU.getFlags();
|
|
|
|
RuntimeVersion = ExistingCU.getRunTimeVersion();
|
|
|
|
SplitName = ExistingCU.getSplitDebugFilename();
|
|
|
|
} else {
|
|
|
|
Producer =
|
|
|
|
"LLVM Version " STR(LLVM_VERSION_MAJOR) "." STR(LLVM_VERSION_MINOR);
|
|
|
|
}
|
|
|
|
|
2013-07-19 00:51:47 +00:00
|
|
|
CUNode =
|
|
|
|
Builder.createCompileUnit(dwarf::DW_LANG_C99, Filename, Directory,
|
|
|
|
Producer, IsOptimized, Flags, RuntimeVersion);
|
2013-06-28 19:05:23 +00:00
|
|
|
|
|
|
|
if (CUToReplace)
|
|
|
|
CUToReplace->replaceAllUsesWith(const_cast<MDNode *>(CUNode));
|
|
|
|
|
|
|
|
DICompileUnit CU(CUNode);
|
|
|
|
FileNode = Builder.createFile(Filename, Directory);
|
|
|
|
LexicalBlockFileNode = Builder.createLexicalBlockFile(CU, DIFile(FileNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the MDNode* that represents the DI scope to associate with I
|
|
|
|
MDNode *findScope(const Instruction *I) {
|
|
|
|
const Function *F = I->getParent()->getParent();
|
|
|
|
if (MDNode *ret = findDISubprogram(F))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "WARNING: Using fallback lexical block file scope "
|
|
|
|
<< LexicalBlockFileNode << " as scope for instruction " << I
|
|
|
|
<< "\n");
|
|
|
|
return LexicalBlockFileNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the MDNode* that is the descriptor for F
|
|
|
|
MDNode *findDISubprogram(const Function *F) {
|
|
|
|
typedef ValueMap<const Function *, MDNode *>::const_iterator FuncNodeIter;
|
|
|
|
FuncNodeIter i = SubprogramDescriptors.find(F);
|
|
|
|
if (i != SubprogramDescriptors.end())
|
|
|
|
return i->second;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "searching for DI scope node for Function " << F
|
|
|
|
<< " in a list of " << Finder.subprogram_count()
|
|
|
|
<< " subprogram nodes"
|
|
|
|
<< "\n");
|
|
|
|
|
2014-03-18 09:41:07 +00:00
|
|
|
for (DISubprogram S : Finder.subprograms()) {
|
2013-06-28 19:05:23 +00:00
|
|
|
if (S.getFunction() == F) {
|
2014-03-18 09:41:07 +00:00
|
|
|
DEBUG(dbgs() << "Found DISubprogram " << S << " for function "
|
2013-06-28 19:05:23 +00:00
|
|
|
<< S.getFunction() << "\n");
|
2014-03-18 09:41:07 +00:00
|
|
|
return S;
|
2013-06-28 19:05:23 +00:00
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
}
|
2013-06-28 19:05:23 +00:00
|
|
|
DEBUG(dbgs() << "unable to find DISubprogram node for function "
|
|
|
|
<< F->getName().str() << "\n");
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2013-05-08 20:44:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
/// Sets Line to the line number on which V appears and returns true. If a
|
|
|
|
/// line location for V is not found, returns false.
|
|
|
|
bool findLine(const Value *V, unsigned &Line) {
|
|
|
|
if (LineTable.getLine(V, Line))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (VMap) {
|
|
|
|
Value *mapped = VMap->lookup(V);
|
|
|
|
if (mapped && LineTable.getLine(mapped, Line))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getTypeName(Type *T) {
|
2014-06-26 22:52:05 +00:00
|
|
|
std::string TypeName;
|
|
|
|
raw_string_ostream TypeStream(TypeName);
|
2014-06-21 02:43:02 +00:00
|
|
|
if (T)
|
2014-06-26 22:52:05 +00:00
|
|
|
T->print(TypeStream);
|
2014-06-21 02:43:02 +00:00
|
|
|
else
|
2014-06-26 22:52:05 +00:00
|
|
|
TypeStream << "Printing <null> Type";
|
|
|
|
TypeStream.flush();
|
|
|
|
return TypeName;
|
2013-06-28 19:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the MDNode that represents type T if it is already created, or 0
|
|
|
|
/// if it is not.
|
|
|
|
MDNode *getType(const Type *T) {
|
|
|
|
typedef DenseMap<const Type *, MDNode *>::const_iterator TypeNodeIter;
|
|
|
|
TypeNodeIter i = TypeDescriptors.find(T);
|
|
|
|
if (i != TypeDescriptors.end())
|
|
|
|
return i->second;
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2013-06-28 19:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a DebugInfo type from an LLVM type T.
|
|
|
|
DIDerivedType getOrCreateType(Type *T) {
|
|
|
|
MDNode *N = getType(T);
|
|
|
|
if (N)
|
|
|
|
return DIDerivedType(N);
|
|
|
|
else if (T->isVoidTy())
|
2014-04-25 05:29:35 +00:00
|
|
|
return DIDerivedType(nullptr);
|
2013-06-28 19:05:23 +00:00
|
|
|
else if (T->isStructTy()) {
|
|
|
|
N = Builder.createStructType(
|
|
|
|
DIScope(LexicalBlockFileNode), T->getStructName(), DIFile(FileNode),
|
|
|
|
0, Layout.getTypeSizeInBits(T), Layout.getABITypeAlignment(T), 0,
|
2014-04-25 05:29:35 +00:00
|
|
|
DIType(nullptr), DIArray(nullptr)); // filled in later
|
2013-06-28 19:05:23 +00:00
|
|
|
|
|
|
|
// N is added to the map (early) so that element search below can find it,
|
|
|
|
// so as to avoid infinite recursion for structs that contain pointers to
|
|
|
|
// their own type.
|
|
|
|
TypeDescriptors[T] = N;
|
|
|
|
DICompositeType StructDescriptor(N);
|
|
|
|
|
|
|
|
SmallVector<Value *, 4> Elements;
|
|
|
|
for (unsigned i = 0; i < T->getStructNumElements(); ++i)
|
|
|
|
Elements.push_back(getOrCreateType(T->getStructElementType(i)));
|
|
|
|
|
|
|
|
// set struct elements
|
|
|
|
StructDescriptor.setTypeArray(Builder.getOrCreateArray(Elements));
|
|
|
|
} else if (T->isPointerTy()) {
|
|
|
|
Type *PointeeTy = T->getPointerElementType();
|
|
|
|
if (!(N = getType(PointeeTy)))
|
|
|
|
N = Builder.createPointerType(
|
2013-09-27 22:26:25 +00:00
|
|
|
getOrCreateType(PointeeTy), Layout.getPointerTypeSizeInBits(T),
|
2013-06-28 19:05:23 +00:00
|
|
|
Layout.getPrefTypeAlignment(T), getTypeName(T));
|
|
|
|
} else if (T->isArrayTy()) {
|
|
|
|
SmallVector<Value *, 1> Subrange;
|
|
|
|
Subrange.push_back(
|
|
|
|
Builder.getOrCreateSubrange(0, T->getArrayNumElements() - 1));
|
|
|
|
|
|
|
|
N = Builder.createArrayType(Layout.getTypeSizeInBits(T),
|
|
|
|
Layout.getPrefTypeAlignment(T),
|
|
|
|
getOrCreateType(T->getArrayElementType()),
|
|
|
|
Builder.getOrCreateArray(Subrange));
|
|
|
|
} else {
|
|
|
|
int encoding = llvm::dwarf::DW_ATE_signed;
|
|
|
|
if (T->isIntegerTy())
|
|
|
|
encoding = llvm::dwarf::DW_ATE_unsigned;
|
|
|
|
else if (T->isFloatingPointTy())
|
|
|
|
encoding = llvm::dwarf::DW_ATE_float;
|
|
|
|
|
|
|
|
N = Builder.createBasicType(getTypeName(T), T->getPrimitiveSizeInBits(),
|
|
|
|
0, encoding);
|
|
|
|
}
|
|
|
|
TypeDescriptors[T] = N;
|
|
|
|
return DIDerivedType(N);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a DebugInfo type that represents a function signature for Func.
|
|
|
|
DICompositeType createFunctionSignature(const Function *Func) {
|
|
|
|
SmallVector<Value *, 4> Params;
|
|
|
|
DIDerivedType ReturnType(getOrCreateType(Func->getReturnType()));
|
|
|
|
Params.push_back(ReturnType);
|
|
|
|
|
|
|
|
const Function::ArgumentListType &Args(Func->getArgumentList());
|
|
|
|
for (Function::ArgumentListType::const_iterator i = Args.begin(),
|
|
|
|
e = Args.end();
|
|
|
|
i != e; ++i) {
|
|
|
|
Type *T(i->getType());
|
|
|
|
Params.push_back(getOrCreateType(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
DIArray ParamArray = Builder.getOrCreateArray(Params);
|
|
|
|
return Builder.createSubroutineType(DIFile(FileNode), ParamArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Associates Instruction I with debug location Loc.
|
2013-05-08 20:44:14 +00:00
|
|
|
void addDebugLocation(Instruction &I, DebugLoc Loc) {
|
|
|
|
MDNode *MD = Loc.getAsMDNode(I.getContext());
|
|
|
|
I.setMetadata(LLVMContext::MD_dbg, MD);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
/// Sets Filename/Directory from the Module identifier and returns true, or
|
|
|
|
/// false if source information is not present.
|
|
|
|
bool getSourceInfoFromModule(const Module &M, std::string &Directory,
|
|
|
|
std::string &Filename) {
|
|
|
|
std::string PathStr(M.getModuleIdentifier());
|
|
|
|
if (PathStr.length() == 0 || PathStr == "<stdin>")
|
|
|
|
return false;
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
Filename = sys::path::filename(PathStr);
|
|
|
|
SmallVector<char, 16> Path(PathStr.begin(), PathStr.end());
|
|
|
|
sys::path::remove_filename(Path);
|
|
|
|
Directory = StringRef(Path.data(), Path.size());
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
// Sets Filename/Directory from debug information in M and returns true, or
|
|
|
|
// false if no debug information available, or cannot be parsed.
|
|
|
|
bool getSourceInfoFromDI(const Module &M, std::string &Directory,
|
|
|
|
std::string &Filename) {
|
|
|
|
NamedMDNode *CUNode = M.getNamedMetadata("llvm.dbg.cu");
|
|
|
|
if (!CUNode || CUNode->getNumOperands() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DICompileUnit CU(CUNode->getOperand(0));
|
|
|
|
if (!CU.Verify())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Filename = CU.getFilename();
|
|
|
|
Directory = CU.getDirectory();
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
} // anonymous namespace
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
namespace llvm {
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
bool DebugIR::getSourceInfo(const Module &M) {
|
|
|
|
ParsedPath = getSourceInfoFromDI(M, Directory, Filename) ||
|
|
|
|
getSourceInfoFromModule(M, Directory, Filename);
|
|
|
|
return ParsedPath;
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
bool DebugIR::updateExtension(StringRef NewExtension) {
|
|
|
|
size_t dot = Filename.find_last_of(".");
|
|
|
|
if (dot == std::string::npos)
|
|
|
|
return false;
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
Filename.erase(dot);
|
|
|
|
Filename += NewExtension.str();
|
|
|
|
return true;
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
void DebugIR::generateFilename(std::unique_ptr<int> &fd) {
|
2013-06-28 19:05:23 +00:00
|
|
|
SmallVector<char, 16> PathVec;
|
|
|
|
fd.reset(new int);
|
2013-07-05 20:14:52 +00:00
|
|
|
sys::fs::createTemporaryFile("debug-ir", "ll", *fd, PathVec);
|
2013-06-28 19:05:23 +00:00
|
|
|
StringRef Path(PathVec.data(), PathVec.size());
|
|
|
|
Filename = sys::path::filename(Path);
|
|
|
|
sys::path::remove_filename(PathVec);
|
|
|
|
Directory = StringRef(PathVec.data(), PathVec.size());
|
|
|
|
|
|
|
|
GeneratedPath = true;
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
std::string DebugIR::getPath() {
|
|
|
|
SmallVector<char, 16> Path;
|
|
|
|
sys::path::append(Path, Directory, Filename);
|
|
|
|
Path.resize(Filename.size() + Directory.size() + 2);
|
|
|
|
Path[Filename.size() + Directory.size() + 1] = '\0';
|
|
|
|
return std::string(Path.data());
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
void DebugIR::writeDebugBitcode(const Module *M, int *fd) {
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<raw_fd_ostream> Out;
|
2013-06-28 19:05:23 +00:00
|
|
|
std::string error;
|
|
|
|
|
|
|
|
if (!fd) {
|
|
|
|
std::string Path = getPath();
|
2014-02-24 18:20:12 +00:00
|
|
|
Out.reset(new raw_fd_ostream(Path.c_str(), error, sys::fs::F_Text));
|
2013-06-28 19:05:23 +00:00
|
|
|
DEBUG(dbgs() << "WRITING debug bitcode from Module " << M << " to file "
|
|
|
|
<< Path << "\n");
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << "WRITING debug bitcode from Module " << M << " to fd "
|
|
|
|
<< *fd << "\n");
|
|
|
|
Out.reset(new raw_fd_ostream(*fd, true));
|
2013-05-23 22:34:33 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
M->print(*Out, nullptr);
|
2013-06-28 19:05:23 +00:00
|
|
|
Out->close();
|
|
|
|
}
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
void DebugIR::createDebugInfo(Module &M, std::unique_ptr<Module> &DisplayM) {
|
2013-06-28 19:05:23 +00:00
|
|
|
if (M.getFunctionList().size() == 0)
|
|
|
|
// no functions -- no debug info needed
|
|
|
|
return;
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<ValueToValueMapTy> VMap;
|
2013-06-28 19:05:23 +00:00
|
|
|
|
|
|
|
if (WriteSourceToDisk && (HideDebugIntrinsics || HideDebugMetadata)) {
|
|
|
|
VMap.reset(new ValueToValueMapTy);
|
|
|
|
DisplayM.reset(CloneModule(&M, *VMap));
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
if (HideDebugIntrinsics)
|
|
|
|
DebugIntrinsicsRemover::process(*DisplayM);
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
if (HideDebugMetadata)
|
|
|
|
DebugMetadataRemover::process(*DisplayM);
|
|
|
|
}
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
DIUpdater R(M, Filename, Directory, DisplayM.get(), VMap.get());
|
|
|
|
}
|
2013-05-08 20:44:14 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
bool DebugIR::isMissingPath() { return Filename.empty() || Directory.empty(); }
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
bool DebugIR::runOnModule(Module &M) {
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<int> fd;
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
if (isMissingPath() && !getSourceInfo(M)) {
|
|
|
|
if (!WriteSourceToDisk)
|
|
|
|
report_fatal_error("DebugIR unable to determine file name in input. "
|
|
|
|
"Ensure Module contains an identifier, a valid "
|
|
|
|
"DICompileUnit, or construct DebugIR with "
|
|
|
|
"non-empty Filename/Directory parameters.");
|
|
|
|
else
|
|
|
|
generateFilename(fd);
|
2013-05-08 20:44:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
if (!GeneratedPath && WriteSourceToDisk)
|
|
|
|
updateExtension(".debug-ll");
|
|
|
|
|
|
|
|
// Clear line numbers. Keep debug info (if any) if we were able to read the
|
|
|
|
// file name from the DICompileUnit descriptor.
|
|
|
|
DebugMetadataRemover::process(M, !ParsedPath);
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2014-03-06 05:51:42 +00:00
|
|
|
std::unique_ptr<Module> DisplayM;
|
2013-06-28 19:05:23 +00:00
|
|
|
createDebugInfo(M, DisplayM);
|
|
|
|
if (WriteSourceToDisk) {
|
|
|
|
Module *OutputM = DisplayM.get() ? DisplayM.get() : &M;
|
|
|
|
writeDebugBitcode(OutputM, fd.get());
|
2013-05-08 20:44:14 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
DEBUG(M.dump());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DebugIR::runOnModule(Module &M, std::string &Path) {
|
|
|
|
bool result = runOnModule(M);
|
|
|
|
Path = getPath();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // llvm namespace
|
2013-05-08 20:44:14 +00:00
|
|
|
|
|
|
|
char DebugIR::ID = 0;
|
|
|
|
INITIALIZE_PASS(DebugIR, "debug-ir", "Enable debugging IR", false, false)
|
2013-05-23 22:34:33 +00:00
|
|
|
|
2013-06-28 19:05:23 +00:00
|
|
|
ModulePass *llvm::createDebugIRPass(bool HideDebugIntrinsics,
|
|
|
|
bool HideDebugMetadata, StringRef Directory,
|
|
|
|
StringRef Filename) {
|
|
|
|
return new DebugIR(HideDebugIntrinsics, HideDebugMetadata, Directory,
|
|
|
|
Filename);
|
2013-05-08 20:44:14 +00:00
|
|
|
}
|
2013-06-28 19:05:23 +00:00
|
|
|
|
|
|
|
ModulePass *llvm::createDebugIRPass() { return new DebugIR(); }
|