2014-04-24 02:54:00 +08:00
|
|
|
//===-- llvm/CodeGen/DwarfFile.h - Dwarf Debug Framework -------*- C++ -*--===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-14 00:26:38 +08:00
|
|
|
#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
|
|
|
|
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFFILE_H
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "AddressPool.h"
|
|
|
|
#include "DwarfStringPool.h"
|
2014-04-24 02:54:00 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
Make a DWARF generator so we can unit test DWARF APIs with gtest.
The only tests we have for the DWARF parser are the tests that use llvm-dwarfdump and expect output from textual dumps.
More DWARF parser modification are coming in the next few weeks and I wanted to add tests that can verify that we can encode and decode all form types, as well as test some other basic DWARF APIs where we ask DIE objects for their children and siblings.
DwarfGenerator.cpp was added in the lib/CodeGen directory. This file contains the code necessary to easily create DWARF for tests:
dwarfgen::Generator DG;
Triple Triple("x86_64--");
bool success = DG.init(Triple, Version);
if (!success)
return;
dwarfgen::CompileUnit &CU = DG.addCompileUnit();
dwarfgen::DIE CUDie = CU.getUnitDIE();
CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
// ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
StringRef FileBytes = DG.generate();
MemoryBufferRef FileBuffer(FileBytes, "dwarf");
auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
EXPECT_TRUE((bool)Obj);
DWARFContextInMemory DwarfContext(*Obj.get());
This code is backed by the AsmPrinter code that emits DWARF for the actual compiler.
While adding unit tests it was discovered that DIEValue that used DIEEntry as their values had bugs where DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref8, and DW_FORM_ref_udata forms were not supported. These are all now supported. Added support for DW_FORM_string so we can emit inlined C strings.
Centralized the code to unique abbreviations into a new DIEAbbrevSet class and made both the dwarfgen::Generator and the llvm::DwarfFile classes use the new class.
Fixed comments in the llvm::DIE class so that the Offset is known to be the compile/type unit offset.
DIEInteger now supports more DW_FORM values.
There are also unit tests that cover:
Encoding and decoding all form types and values
Encoding and decoding all reference types (DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8, DW_FORM_ref_udata, DW_FORM_ref_addr) including cross compile unit references with that go forward one compile unit and backward on compile unit.
Differential Revision: https://reviews.llvm.org/D27326
llvm-svn: 289010
2016-12-08 09:03:48 +08:00
|
|
|
#include "llvm/CodeGen/DIE.h"
|
2016-01-28 03:29:56 +08:00
|
|
|
#include "llvm/IR/Metadata.h"
|
2014-04-24 02:54:00 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class AsmPrinter;
|
2014-10-23 08:16:05 +08:00
|
|
|
class DbgVariable;
|
2016-02-12 03:57:46 +08:00
|
|
|
class DwarfCompileUnit;
|
2014-04-24 02:54:00 +08:00
|
|
|
class DwarfUnit;
|
|
|
|
class DIEAbbrev;
|
|
|
|
class MCSymbol;
|
|
|
|
class DIE;
|
2014-10-23 08:16:05 +08:00
|
|
|
class LexicalScope;
|
2014-04-24 02:54:00 +08:00
|
|
|
class StringRef;
|
|
|
|
class DwarfDebug;
|
|
|
|
class MCSection;
|
2015-05-25 00:54:59 +08:00
|
|
|
class MDNode;
|
2014-04-24 02:54:00 +08:00
|
|
|
class DwarfFile {
|
|
|
|
// Target of Dwarf emission, used for sizing of abbreviations.
|
|
|
|
AsmPrinter *Asm;
|
|
|
|
|
2015-05-28 06:31:41 +08:00
|
|
|
BumpPtrAllocator AbbrevAllocator;
|
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
// Used to uniquely define abbreviations.
|
Make a DWARF generator so we can unit test DWARF APIs with gtest.
The only tests we have for the DWARF parser are the tests that use llvm-dwarfdump and expect output from textual dumps.
More DWARF parser modification are coming in the next few weeks and I wanted to add tests that can verify that we can encode and decode all form types, as well as test some other basic DWARF APIs where we ask DIE objects for their children and siblings.
DwarfGenerator.cpp was added in the lib/CodeGen directory. This file contains the code necessary to easily create DWARF for tests:
dwarfgen::Generator DG;
Triple Triple("x86_64--");
bool success = DG.init(Triple, Version);
if (!success)
return;
dwarfgen::CompileUnit &CU = DG.addCompileUnit();
dwarfgen::DIE CUDie = CU.getUnitDIE();
CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
// ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
StringRef FileBytes = DG.generate();
MemoryBufferRef FileBuffer(FileBytes, "dwarf");
auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
EXPECT_TRUE((bool)Obj);
DWARFContextInMemory DwarfContext(*Obj.get());
This code is backed by the AsmPrinter code that emits DWARF for the actual compiler.
While adding unit tests it was discovered that DIEValue that used DIEEntry as their values had bugs where DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref8, and DW_FORM_ref_udata forms were not supported. These are all now supported. Added support for DW_FORM_string so we can emit inlined C strings.
Centralized the code to unique abbreviations into a new DIEAbbrevSet class and made both the dwarfgen::Generator and the llvm::DwarfFile classes use the new class.
Fixed comments in the llvm::DIE class so that the Offset is known to be the compile/type unit offset.
DIEInteger now supports more DW_FORM values.
There are also unit tests that cover:
Encoding and decoding all form types and values
Encoding and decoding all reference types (DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8, DW_FORM_ref_udata, DW_FORM_ref_addr) including cross compile unit references with that go forward one compile unit and backward on compile unit.
Differential Revision: https://reviews.llvm.org/D27326
llvm-svn: 289010
2016-12-08 09:03:48 +08:00
|
|
|
DIEAbbrevSet Abbrevs;
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
// A pointer to all units in the section.
|
2016-02-12 03:57:46 +08:00
|
|
|
SmallVector<std::unique_ptr<DwarfCompileUnit>, 1> CUs;
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2014-04-26 05:34:35 +08:00
|
|
|
DwarfStringPool StrPool;
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2014-10-25 01:57:34 +08:00
|
|
|
// Collection of dbg variables of a scope.
|
|
|
|
DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8>> ScopeVariables;
|
|
|
|
|
2014-11-02 01:21:26 +08:00
|
|
|
// Collection of abstract subprogram DIEs.
|
|
|
|
DenseMap<const MDNode *, DIE *> AbstractSPDies;
|
|
|
|
|
2014-11-05 06:12:18 +08:00
|
|
|
/// Maps MDNodes for type system with the corresponding DIEs. These DIEs can
|
|
|
|
/// be shared across CUs, that is why we keep the map here instead
|
|
|
|
/// of in DwarfCompileUnit.
|
2015-04-30 00:38:44 +08:00
|
|
|
DenseMap<const MDNode *, DIE *> DITypeNodeToDieMap;
|
2014-11-05 06:12:18 +08:00
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
public:
|
2015-03-04 10:30:17 +08:00
|
|
|
DwarfFile(AsmPrinter *AP, StringRef Pref, BumpPtrAllocator &DA);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2016-02-12 03:57:46 +08:00
|
|
|
const SmallVectorImpl<std::unique_ptr<DwarfCompileUnit>> &getUnits() {
|
|
|
|
return CUs;
|
|
|
|
}
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
/// \brief Compute the size and offset of a DIE given an incoming Offset.
|
|
|
|
unsigned computeSizeAndOffset(DIE &Die, unsigned Offset);
|
|
|
|
|
|
|
|
/// \brief Compute the size and offset of all the DIEs.
|
|
|
|
void computeSizeAndOffsets();
|
|
|
|
|
2016-02-12 03:57:46 +08:00
|
|
|
/// \brief Compute the size and offset of all the DIEs in the given unit.
|
|
|
|
/// \returns The size of the root DIE.
|
|
|
|
unsigned computeSizeAndOffsetsForUnit(DwarfUnit *TheU);
|
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
/// \brief Add a unit to the list of CUs.
|
2016-02-12 03:57:46 +08:00
|
|
|
void addUnit(std::unique_ptr<DwarfCompileUnit> U);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
/// \brief Emit all of the units to the section listed with the given
|
|
|
|
/// abbreviation section.
|
2015-03-11 00:58:10 +08:00
|
|
|
void emitUnits(bool UseOffsets);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
2016-02-12 03:57:46 +08:00
|
|
|
/// \brief Emit the given unit to its section.
|
|
|
|
void emitUnit(DwarfUnit *U, bool UseOffsets);
|
|
|
|
|
2014-04-24 02:54:00 +08:00
|
|
|
/// \brief Emit a set of abbreviations to the specific section.
|
2015-05-22 03:20:38 +08:00
|
|
|
void emitAbbrevs(MCSection *);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
/// \brief Emit all of the strings to the section given.
|
2015-05-22 03:20:38 +08:00
|
|
|
void emitStrings(MCSection *StrSection, MCSection *OffsetSection = nullptr);
|
2014-04-24 02:54:00 +08:00
|
|
|
|
|
|
|
/// \brief Returns the string pool.
|
2014-04-26 05:34:35 +08:00
|
|
|
DwarfStringPool &getStringPool() { return StrPool; }
|
2014-10-23 08:16:05 +08:00
|
|
|
|
2015-02-11 07:18:28 +08:00
|
|
|
/// \returns false if the variable was merged with a previous one.
|
|
|
|
bool addScopeVariable(LexicalScope *LS, DbgVariable *Var);
|
2014-10-25 01:57:34 +08:00
|
|
|
|
|
|
|
DenseMap<LexicalScope *, SmallVector<DbgVariable *, 8>> &getScopeVariables() {
|
|
|
|
return ScopeVariables;
|
|
|
|
}
|
2014-11-02 01:21:26 +08:00
|
|
|
|
|
|
|
DenseMap<const MDNode *, DIE *> &getAbstractSPDies() {
|
|
|
|
return AbstractSPDies;
|
|
|
|
}
|
2014-11-05 06:12:18 +08:00
|
|
|
|
|
|
|
void insertDIE(const MDNode *TypeMD, DIE *Die) {
|
2015-04-30 00:38:44 +08:00
|
|
|
DITypeNodeToDieMap.insert(std::make_pair(TypeMD, Die));
|
2014-11-05 06:12:18 +08:00
|
|
|
}
|
|
|
|
DIE *getDIE(const MDNode *TypeMD) {
|
2015-04-30 00:38:44 +08:00
|
|
|
return DITypeNodeToDieMap.lookup(TypeMD);
|
2014-11-05 06:12:18 +08:00
|
|
|
}
|
2014-04-24 02:54:00 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-04-24 02:54:00 +08:00
|
|
|
#endif
|