2016-01-15 03:25:04 +08:00
|
|
|
//===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===//
|
2014-01-30 09:39:17 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2016-01-15 03:25:04 +08:00
|
|
|
// This file contains support for writing Microsoft CodeView debug info.
|
2014-01-30 09:39:17 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
#include "CodeViewDebug.h"
|
2016-01-14 07:44:57 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/CodeView.h"
|
2016-01-29 08:49:42 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/Line.h"
|
2016-01-14 07:44:57 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
|
2016-01-30 02:16:43 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeIndex.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
|
2014-01-30 09:39:17 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/COFF.h"
|
2016-02-11 04:55:49 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/Target/TargetFrameLowering.h"
|
2014-01-30 09:39:17 +08:00
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
using namespace llvm;
|
2016-01-14 07:44:57 +08:00
|
|
|
using namespace llvm::codeview;
|
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
|
|
|
|
: DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) {
|
|
|
|
// If module doesn't have named metadata anchors or COFF debug section
|
|
|
|
// is not available, skip any debug info related stuff.
|
|
|
|
if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
|
|
|
|
!AP->getObjFileLowering().getCOFFDebugSymbolsSection()) {
|
|
|
|
Asm = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tell MMI that we have debug info.
|
|
|
|
MMI->setDebugInfoAvailability(true);
|
|
|
|
}
|
2014-01-30 09:39:17 +08:00
|
|
|
|
2016-01-16 08:09:09 +08:00
|
|
|
StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
|
|
|
|
std::string &Filepath = FileToFilepathMap[File];
|
2015-12-03 06:34:30 +08:00
|
|
|
if (!Filepath.empty())
|
|
|
|
return Filepath;
|
2014-01-30 09:39:17 +08:00
|
|
|
|
2016-01-16 08:09:09 +08:00
|
|
|
StringRef Dir = File->getDirectory(), Filename = File->getFilename();
|
|
|
|
|
2014-01-30 09:39:17 +08:00
|
|
|
// Clang emits directory and relative filename info into the IR, but CodeView
|
|
|
|
// operates on full paths. We could change Clang to emit full paths too, but
|
|
|
|
// that would increase the IR size and probably not needed for other users.
|
|
|
|
// For now, just concatenate and canonicalize the path here.
|
|
|
|
if (Filename.find(':') == 1)
|
|
|
|
Filepath = Filename;
|
|
|
|
else
|
2015-03-28 01:51:30 +08:00
|
|
|
Filepath = (Dir + "\\" + Filename).str();
|
2014-01-30 09:39:17 +08:00
|
|
|
|
|
|
|
// Canonicalize the path. We have to do it textually because we may no longer
|
|
|
|
// have access the file in the filesystem.
|
|
|
|
// First, replace all slashes with backslashes.
|
|
|
|
std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
|
|
|
|
|
|
|
|
// Remove all "\.\" with "\".
|
|
|
|
size_t Cursor = 0;
|
|
|
|
while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
|
|
|
|
Filepath.erase(Cursor, 2);
|
|
|
|
|
|
|
|
// Replace all "\XXX\..\" with "\". Don't try too hard though as the original
|
|
|
|
// path should be well-formatted, e.g. start with a drive letter, etc.
|
|
|
|
Cursor = 0;
|
|
|
|
while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
|
|
|
|
// Something's wrong if the path starts with "\..\", abort.
|
|
|
|
if (Cursor == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
|
|
|
|
if (PrevSlash == std::string::npos)
|
|
|
|
// Something's wrong, abort.
|
|
|
|
break;
|
|
|
|
|
|
|
|
Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
|
|
|
|
// The next ".." might be following the one we've just erased.
|
|
|
|
Cursor = PrevSlash;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all duplicate backslashes.
|
|
|
|
Cursor = 0;
|
|
|
|
while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
|
|
|
|
Filepath.erase(Cursor, 1);
|
|
|
|
|
2015-12-03 06:34:30 +08:00
|
|
|
return Filepath;
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
|
|
|
|
unsigned NextId = FileIdMap.size() + 1;
|
|
|
|
auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
|
|
|
|
if (Insertion.second) {
|
|
|
|
// We have to compute the full filepath and emit a .cv_file directive.
|
|
|
|
StringRef FullPath = getFullFilepath(F);
|
2016-02-04 05:15:48 +08:00
|
|
|
NextId = OS.EmitCVFileDirective(NextId, FullPath);
|
2016-01-29 08:49:42 +08:00
|
|
|
assert(NextId == FileIdMap.size() && ".cv_file directive failed");
|
|
|
|
}
|
|
|
|
return Insertion.first->second;
|
|
|
|
}
|
|
|
|
|
2016-02-13 05:48:30 +08:00
|
|
|
CodeViewDebug::InlineSite &
|
|
|
|
CodeViewDebug::getInlineSite(const DILocation *InlinedAt,
|
|
|
|
const DISubprogram *Inlinee) {
|
2016-03-19 02:54:32 +08:00
|
|
|
auto SiteInsertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
|
|
|
|
InlineSite *Site = &SiteInsertion.first->second;
|
|
|
|
if (SiteInsertion.second) {
|
2016-02-11 04:55:49 +08:00
|
|
|
Site->SiteFuncId = NextFuncId++;
|
2016-02-13 05:48:30 +08:00
|
|
|
Site->Inlinee = Inlinee;
|
2016-03-19 02:54:32 +08:00
|
|
|
auto InlineeInsertion =
|
|
|
|
SubprogramIndices.insert({Inlinee, InlinedSubprograms.size()});
|
|
|
|
if (InlineeInsertion.second)
|
|
|
|
InlinedSubprograms.push_back(Inlinee);
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
2016-02-11 04:55:49 +08:00
|
|
|
return *Site;
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 05:48:30 +08:00
|
|
|
void CodeViewDebug::recordLocalVariable(LocalVariable &&Var,
|
|
|
|
const DILocation *InlinedAt) {
|
|
|
|
if (InlinedAt) {
|
|
|
|
// This variable was inlined. Associate it with the InlineSite.
|
|
|
|
const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram();
|
|
|
|
InlineSite &Site = getInlineSite(InlinedAt, Inlinee);
|
|
|
|
Site.InlinedLocals.emplace_back(Var);
|
|
|
|
} else {
|
|
|
|
// This variable goes in the main ProcSym.
|
|
|
|
CurFn->Locals.emplace_back(Var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-12 03:41:47 +08:00
|
|
|
static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs,
|
|
|
|
const DILocation *Loc) {
|
|
|
|
auto B = Locs.begin(), E = Locs.end();
|
|
|
|
if (std::find(B, E, Loc) == E)
|
|
|
|
Locs.push_back(Loc);
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
|
2016-01-16 08:09:09 +08:00
|
|
|
const MachineFunction *MF) {
|
|
|
|
// Skip this instruction if it has the same location as the previous one.
|
|
|
|
if (DL == CurFn->LastLoc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const DIScope *Scope = DL.get()->getScope();
|
2014-01-30 09:39:17 +08:00
|
|
|
if (!Scope)
|
|
|
|
return;
|
2016-01-16 08:09:09 +08:00
|
|
|
|
2016-01-13 09:05:23 +08:00
|
|
|
// Skip this line if it is longer than the maximum we can record.
|
2016-01-29 08:49:42 +08:00
|
|
|
LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
|
|
|
|
if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
|
|
|
|
LI.isNeverStepInto())
|
2016-01-13 09:05:23 +08:00
|
|
|
return;
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
|
|
|
|
if (CI.getStartColumn() != DL.getCol())
|
|
|
|
return;
|
2016-01-29 08:13:28 +08:00
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
if (!CurFn->HaveLineInfo)
|
|
|
|
CurFn->HaveLineInfo = true;
|
|
|
|
unsigned FileId = 0;
|
|
|
|
if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
|
|
|
|
FileId = CurFn->LastFileId;
|
|
|
|
else
|
|
|
|
FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
|
|
|
|
CurFn->LastLoc = DL;
|
2016-01-30 02:16:43 +08:00
|
|
|
|
|
|
|
unsigned FuncId = CurFn->FuncId;
|
2016-02-13 05:48:30 +08:00
|
|
|
if (const DILocation *SiteLoc = DL->getInlinedAt()) {
|
2016-02-12 03:41:47 +08:00
|
|
|
const DILocation *Loc = DL.get();
|
|
|
|
|
2016-01-30 02:16:43 +08:00
|
|
|
// If this location was actually inlined from somewhere else, give it the ID
|
|
|
|
// of the inline call site.
|
2016-02-13 05:48:30 +08:00
|
|
|
FuncId =
|
|
|
|
getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId;
|
2016-02-12 03:41:47 +08:00
|
|
|
|
2016-01-30 02:16:43 +08:00
|
|
|
// Ensure we have links in the tree of inline call sites.
|
2016-02-12 03:41:47 +08:00
|
|
|
bool FirstLoc = true;
|
|
|
|
while ((SiteLoc = Loc->getInlinedAt())) {
|
2016-02-13 05:48:30 +08:00
|
|
|
InlineSite &Site =
|
|
|
|
getInlineSite(SiteLoc, Loc->getScope()->getSubprogram());
|
2016-02-12 03:41:47 +08:00
|
|
|
if (!FirstLoc)
|
|
|
|
addLocIfNotPresent(Site.ChildSites, Loc);
|
|
|
|
FirstLoc = false;
|
|
|
|
Loc = SiteLoc;
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
2016-02-12 03:41:47 +08:00
|
|
|
addLocIfNotPresent(CurFn->ChildSites, Loc);
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(),
|
|
|
|
/*PrologueEnd=*/false,
|
|
|
|
/*IsStmt=*/false, DL->getFilename());
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::endModule() {
|
2014-10-11 00:05:32 +08:00
|
|
|
if (FnDebugInfo.empty())
|
|
|
|
return;
|
|
|
|
|
2016-01-30 02:16:43 +08:00
|
|
|
emitTypeInformation();
|
|
|
|
|
2016-01-14 07:44:57 +08:00
|
|
|
// FIXME: For functions that are comdat, we should emit separate .debug$S
|
|
|
|
// sections that are comdat associative with the main function instead of
|
|
|
|
// having one big .debug$S section.
|
2014-10-11 00:05:32 +08:00
|
|
|
assert(Asm != nullptr);
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
|
|
|
|
OS.AddComment("Debug section magic");
|
|
|
|
OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
|
2014-10-11 00:05:32 +08:00
|
|
|
|
|
|
|
// The COFF .debug$S section consists of several subsections, each starting
|
|
|
|
// with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
|
|
|
|
// of the payload followed by the payload itself. The subsections are 4-byte
|
|
|
|
// aligned.
|
|
|
|
|
2016-02-03 01:41:18 +08:00
|
|
|
// Make a subsection for all the inlined subprograms.
|
2016-03-19 02:54:32 +08:00
|
|
|
emitInlineeFuncIdsAndLines();
|
2016-02-03 01:41:18 +08:00
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
// Emit per-function debug information.
|
|
|
|
for (auto &P : FnDebugInfo)
|
|
|
|
emitDebugInfoForFunction(P.first, P.second);
|
2014-10-11 00:05:32 +08:00
|
|
|
|
|
|
|
// This subsection holds a file index to offset in string table table.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("File index to string table offset subsection");
|
|
|
|
OS.EmitCVFileChecksumsDirective();
|
2014-10-11 00:05:32 +08:00
|
|
|
|
|
|
|
// This subsection holds the string table.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("String table");
|
|
|
|
OS.EmitCVStringTableDirective();
|
2014-10-11 00:05:32 +08:00
|
|
|
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2016-03-14 13:15:09 +08:00
|
|
|
static void emitNullTerminatedSymbolName(MCStreamer &OS, StringRef S) {
|
|
|
|
// Microsoft's linker seems to have trouble with symbol names longer than
|
|
|
|
// 0xffd8 bytes.
|
|
|
|
S = S.substr(0, 0xffd8);
|
|
|
|
SmallString<32> NullTerminatedString(S);
|
|
|
|
NullTerminatedString.push_back('\0');
|
|
|
|
OS.EmitBytes(NullTerminatedString);
|
|
|
|
}
|
|
|
|
|
2016-01-30 02:16:43 +08:00
|
|
|
void CodeViewDebug::emitTypeInformation() {
|
2016-03-19 02:54:32 +08:00
|
|
|
// Do nothing if we have no debug info or no inlined subprograms. The types
|
|
|
|
// we currently emit exist only to support inlined call site info.
|
2016-01-30 02:16:43 +08:00
|
|
|
NamedMDNode *CU_Nodes =
|
2016-02-11 04:55:49 +08:00
|
|
|
MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
|
2016-01-30 02:16:43 +08:00
|
|
|
if (!CU_Nodes)
|
|
|
|
return;
|
2016-03-19 02:54:32 +08:00
|
|
|
if (InlinedSubprograms.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Start the .debug$T section with 0x4.
|
|
|
|
OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection());
|
|
|
|
OS.AddComment("Debug section magic");
|
|
|
|
OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4);
|
2016-01-30 02:16:43 +08:00
|
|
|
|
|
|
|
// This type info currently only holds function ids for use with inline call
|
|
|
|
// frame info. All functions are assigned a simple 'void ()' type. Emit that
|
|
|
|
// type here.
|
2016-03-19 02:54:32 +08:00
|
|
|
unsigned ArgListIndex = getNextTypeIndex();
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Type record length");
|
|
|
|
OS.EmitIntValue(2 + sizeof(ArgList), 2);
|
|
|
|
OS.AddComment("Leaf type: LF_ARGLIST");
|
|
|
|
OS.EmitIntValue(LF_ARGLIST, 2);
|
|
|
|
OS.AddComment("Number of arguments");
|
|
|
|
OS.EmitIntValue(0, 4);
|
2016-01-30 02:16:43 +08:00
|
|
|
|
2016-03-19 02:54:32 +08:00
|
|
|
unsigned VoidFnTyIdx = getNextTypeIndex();
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Type record length");
|
|
|
|
OS.EmitIntValue(2 + sizeof(ProcedureType), 2);
|
|
|
|
OS.AddComment("Leaf type: LF_PROCEDURE");
|
|
|
|
OS.EmitIntValue(LF_PROCEDURE, 2);
|
|
|
|
OS.AddComment("Return type index");
|
|
|
|
OS.EmitIntValue(TypeIndex::Void().getIndex(), 4);
|
|
|
|
OS.AddComment("Calling convention");
|
|
|
|
OS.EmitIntValue(char(CallingConvention::NearC), 1);
|
|
|
|
OS.AddComment("Function options");
|
|
|
|
OS.EmitIntValue(char(FunctionOptions::None), 1);
|
|
|
|
OS.AddComment("# of parameters");
|
|
|
|
OS.EmitIntValue(0, 2);
|
|
|
|
OS.AddComment("Argument list type index");
|
2016-03-19 02:54:32 +08:00
|
|
|
OS.EmitIntValue(ArgListIndex, 4);
|
|
|
|
|
|
|
|
// Emit LF_FUNC_ID records for all inlined subprograms to the type stream.
|
|
|
|
// Allocate one type index for each func id.
|
|
|
|
unsigned NextIdx = getNextTypeIndex(InlinedSubprograms.size());
|
2016-03-20 09:17:54 +08:00
|
|
|
(void)NextIdx;
|
2016-03-19 02:54:32 +08:00
|
|
|
assert(NextIdx == FuncIdTypeIndexStart && "func id type indices broken");
|
|
|
|
for (auto *SP : InlinedSubprograms) {
|
|
|
|
StringRef DisplayName = SP->getDisplayName();
|
|
|
|
OS.AddComment("Type record length");
|
|
|
|
MCSymbol *FuncBegin = MMI->getContext().createTempSymbol(),
|
|
|
|
*FuncEnd = MMI->getContext().createTempSymbol();
|
|
|
|
OS.emitAbsoluteSymbolDiff(FuncEnd, FuncBegin, 2);
|
|
|
|
OS.EmitLabel(FuncBegin);
|
|
|
|
OS.AddComment("Leaf type: LF_FUNC_ID");
|
|
|
|
OS.EmitIntValue(LF_FUNC_ID, 2);
|
|
|
|
|
|
|
|
OS.AddComment("Scope type index");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("Function type");
|
|
|
|
OS.EmitIntValue(VoidFnTyIdx, 4);
|
|
|
|
{
|
|
|
|
OS.AddComment("Function name");
|
|
|
|
emitNullTerminatedSymbolName(OS, DisplayName);
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
2016-03-19 02:54:32 +08:00
|
|
|
OS.EmitLabel(FuncEnd);
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 02:54:32 +08:00
|
|
|
void CodeViewDebug::emitInlineeFuncIdsAndLines() {
|
2016-02-03 01:41:18 +08:00
|
|
|
if (InlinedSubprograms.empty())
|
|
|
|
return;
|
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
|
|
|
|
*InlineEnd = MMI->getContext().createTempSymbol();
|
2016-02-03 01:41:18 +08:00
|
|
|
|
|
|
|
OS.AddComment("Inlinee lines subsection");
|
|
|
|
OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
|
2016-02-03 07:18:23 +08:00
|
|
|
OS.AddComment("Subsection size");
|
2016-02-03 01:41:18 +08:00
|
|
|
OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
|
|
|
|
OS.EmitLabel(InlineBegin);
|
|
|
|
|
|
|
|
// We don't provide any extra file info.
|
|
|
|
// FIXME: Find out if debuggers use this info.
|
2016-02-03 07:18:23 +08:00
|
|
|
OS.AddComment("Inlinee lines signature");
|
2016-02-03 01:41:18 +08:00
|
|
|
OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
|
|
|
|
|
2016-03-19 02:54:32 +08:00
|
|
|
unsigned InlineeIndex = FuncIdTypeIndexStart;
|
2016-02-03 01:41:18 +08:00
|
|
|
for (const DISubprogram *SP : InlinedSubprograms) {
|
2016-02-03 07:18:23 +08:00
|
|
|
OS.AddBlankLine();
|
2016-02-03 01:41:18 +08:00
|
|
|
unsigned FileId = maybeRecordFile(SP->getFile());
|
|
|
|
OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
|
|
|
|
SP->getFilename() + Twine(':') + Twine(SP->getLine()));
|
2016-02-03 07:18:23 +08:00
|
|
|
OS.AddBlankLine();
|
2016-02-03 01:41:18 +08:00
|
|
|
// The filechecksum table uses 8 byte entries for now, and file ids start at
|
|
|
|
// 1.
|
|
|
|
unsigned FileOffset = (FileId - 1) * 8;
|
2016-02-03 07:18:23 +08:00
|
|
|
OS.AddComment("Type index of inlined function");
|
2016-03-19 02:54:32 +08:00
|
|
|
OS.EmitIntValue(InlineeIndex, 4);
|
2016-02-03 07:18:23 +08:00
|
|
|
OS.AddComment("Offset into filechecksum table");
|
2016-02-03 01:41:18 +08:00
|
|
|
OS.EmitIntValue(FileOffset, 4);
|
2016-02-03 07:18:23 +08:00
|
|
|
OS.AddComment("Starting line number");
|
2016-02-03 01:41:18 +08:00
|
|
|
OS.EmitIntValue(SP->getLine(), 4);
|
2016-03-19 02:54:32 +08:00
|
|
|
|
|
|
|
// The next inlined subprogram has the next function id.
|
|
|
|
InlineeIndex++;
|
2016-02-03 01:41:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OS.EmitLabel(InlineEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeViewDebug::collectInlineSiteChildren(
|
|
|
|
SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
|
|
|
|
const InlineSite &Site) {
|
|
|
|
for (const DILocation *ChildSiteLoc : Site.ChildSites) {
|
|
|
|
auto I = FI.InlineSites.find(ChildSiteLoc);
|
|
|
|
const InlineSite &ChildSite = I->second;
|
|
|
|
Children.push_back(ChildSite.SiteFuncId);
|
|
|
|
collectInlineSiteChildren(Children, FI, ChildSite);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-30 02:16:43 +08:00
|
|
|
void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
|
|
|
|
const DILocation *InlinedAt,
|
|
|
|
const InlineSite &Site) {
|
2016-02-11 04:55:49 +08:00
|
|
|
MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(),
|
|
|
|
*InlineEnd = MMI->getContext().createTempSymbol();
|
2016-01-30 02:16:43 +08:00
|
|
|
|
2016-03-19 02:54:32 +08:00
|
|
|
assert(SubprogramIndices.count(Site.Inlinee));
|
|
|
|
unsigned InlineeIdx = FuncIdTypeIndexStart + SubprogramIndices[Site.Inlinee];
|
2016-01-30 02:16:43 +08:00
|
|
|
|
|
|
|
// SymbolRecord
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Record length");
|
2016-02-04 05:24:42 +08:00
|
|
|
OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength
|
2016-01-30 02:16:43 +08:00
|
|
|
OS.EmitLabel(InlineBegin);
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Record kind: S_INLINESITE");
|
|
|
|
OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind
|
2016-01-30 02:16:43 +08:00
|
|
|
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("PtrParent");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("PtrEnd");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("Inlinee type index");
|
2016-03-19 02:54:32 +08:00
|
|
|
OS.EmitIntValue(InlineeIdx, 4);
|
2016-01-30 02:16:43 +08:00
|
|
|
|
2016-02-03 01:41:18 +08:00
|
|
|
unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
|
|
|
|
unsigned StartLineNum = Site.Inlinee->getLine();
|
|
|
|
SmallVector<unsigned, 3> SecondaryFuncIds;
|
|
|
|
collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
|
|
|
|
|
|
|
|
OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
|
2016-02-03 03:22:34 +08:00
|
|
|
FI.Begin, FI.End, SecondaryFuncIds);
|
2016-01-30 02:16:43 +08:00
|
|
|
|
|
|
|
OS.EmitLabel(InlineEnd);
|
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
for (const LocalVariable &Var : Site.InlinedLocals)
|
|
|
|
emitLocalVariable(Var);
|
|
|
|
|
2016-01-30 02:16:43 +08:00
|
|
|
// Recurse on child inlined call sites before closing the scope.
|
|
|
|
for (const DILocation *ChildSite : Site.ChildSites) {
|
|
|
|
auto I = FI.InlineSites.find(ChildSite);
|
|
|
|
assert(I != FI.InlineSites.end() &&
|
|
|
|
"child site not in function inline site map");
|
|
|
|
emitInlinedCallSite(FI, ChildSite, I->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the scope.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Record length");
|
|
|
|
OS.EmitIntValue(2, 2); // RecordLength
|
|
|
|
OS.AddComment("Record kind: S_INLINESITE_END");
|
|
|
|
OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
|
|
|
|
FunctionInfo &FI) {
|
2014-01-30 09:39:17 +08:00
|
|
|
// For each function there is a separate subsection
|
|
|
|
// which holds the PC to file:line table.
|
|
|
|
const MCSymbol *Fn = Asm->getSymbol(GV);
|
|
|
|
assert(Fn);
|
2014-03-26 17:50:36 +08:00
|
|
|
|
2015-03-21 03:50:00 +08:00
|
|
|
StringRef FuncName;
|
2016-03-11 10:14:16 +08:00
|
|
|
if (auto *SP = GV->getSubprogram())
|
2015-04-14 11:40:37 +08:00
|
|
|
FuncName = SP->getDisplayName();
|
2015-03-21 03:50:00 +08:00
|
|
|
|
2016-01-14 08:12:54 +08:00
|
|
|
// If our DISubprogram name is empty, use the mangled name.
|
2016-01-14 03:32:35 +08:00
|
|
|
if (FuncName.empty())
|
|
|
|
FuncName = GlobalValue::getRealLinkageName(GV->getName());
|
2016-01-14 08:12:54 +08:00
|
|
|
|
2014-10-24 09:27:45 +08:00
|
|
|
// Emit a symbol subsection, required by VS2012+ to find function boundaries.
|
2016-02-11 04:55:49 +08:00
|
|
|
MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(),
|
|
|
|
*SymbolsEnd = MMI->getContext().createTempSymbol();
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Symbol subsection for " + Twine(FuncName));
|
|
|
|
OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4);
|
|
|
|
OS.AddComment("Subsection size");
|
2016-02-04 05:24:42 +08:00
|
|
|
OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4);
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.EmitLabel(SymbolsBegin);
|
2014-10-24 09:27:45 +08:00
|
|
|
{
|
2016-02-11 04:55:49 +08:00
|
|
|
MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(),
|
|
|
|
*ProcRecordEnd = MMI->getContext().createTempSymbol();
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Record length");
|
2016-02-04 05:24:42 +08:00
|
|
|
OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2);
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.EmitLabel(ProcRecordBegin);
|
2014-10-24 09:27:45 +08:00
|
|
|
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Record kind: S_GPROC32_ID");
|
|
|
|
OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2);
|
2016-01-14 07:44:57 +08:00
|
|
|
|
2016-02-03 07:18:23 +08:00
|
|
|
// These fields are filled in by tools like CVPACK which run after the fact.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("PtrParent");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("PtrEnd");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("PtrNext");
|
|
|
|
OS.EmitIntValue(0, 4);
|
2014-10-24 09:27:45 +08:00
|
|
|
// This is the important bit that tells the debugger where the function
|
|
|
|
// code is located and what's its size:
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Code size");
|
2016-02-04 05:24:42 +08:00
|
|
|
OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4);
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Offset after prologue");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("Offset before epilogue");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("Function type index");
|
|
|
|
OS.EmitIntValue(0, 4);
|
|
|
|
OS.AddComment("Function section relative address");
|
|
|
|
OS.EmitCOFFSecRel32(Fn);
|
|
|
|
OS.AddComment("Function section index");
|
|
|
|
OS.EmitCOFFSectionIndex(Fn);
|
|
|
|
OS.AddComment("Flags");
|
|
|
|
OS.EmitIntValue(0, 1);
|
2014-11-13 04:10:09 +08:00
|
|
|
// Emit the function display name as a null-terminated string.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Function name");
|
2016-03-13 18:53:30 +08:00
|
|
|
// Truncate the name so we won't overflow the record length field.
|
2016-03-14 13:15:09 +08:00
|
|
|
emitNullTerminatedSymbolName(OS, FuncName);
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.EmitLabel(ProcRecordEnd);
|
2014-10-24 09:27:45 +08:00
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
for (const LocalVariable &Var : FI.Locals)
|
|
|
|
emitLocalVariable(Var);
|
|
|
|
|
2016-01-30 02:16:43 +08:00
|
|
|
// Emit inlined call site information. Only emit functions inlined directly
|
|
|
|
// into the parent function. We'll emit the other sites recursively as part
|
|
|
|
// of their parent inline site.
|
2016-02-11 04:55:49 +08:00
|
|
|
for (const DILocation *InlinedAt : FI.ChildSites) {
|
|
|
|
auto I = FI.InlineSites.find(InlinedAt);
|
|
|
|
assert(I != FI.InlineSites.end() &&
|
|
|
|
"child site not in function inline site map");
|
|
|
|
emitInlinedCallSite(FI, InlinedAt, I->second);
|
2016-01-30 02:16:43 +08:00
|
|
|
}
|
|
|
|
|
2014-10-24 09:27:45 +08:00
|
|
|
// We're done with this function.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.AddComment("Record length");
|
|
|
|
OS.EmitIntValue(0x0002, 2);
|
|
|
|
OS.AddComment("Record kind: S_PROC_ID_END");
|
|
|
|
OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2);
|
2014-10-24 09:27:45 +08:00
|
|
|
}
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.EmitLabel(SymbolsEnd);
|
2014-10-24 09:27:45 +08:00
|
|
|
// Every subsection must be aligned to a 4-byte boundary.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.EmitValueToAlignment(4);
|
2014-10-24 09:27:45 +08:00
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
// We have an assembler directive that takes care of the whole line table.
|
2016-02-04 05:15:48 +08:00
|
|
|
OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 05:48:30 +08:00
|
|
|
CodeViewDebug::LocalVarDefRange
|
|
|
|
CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) {
|
|
|
|
LocalVarDefRange DR;
|
2016-02-16 23:35:51 +08:00
|
|
|
DR.InMemory = -1;
|
2016-02-13 05:48:30 +08:00
|
|
|
DR.DataOffset = Offset;
|
|
|
|
assert(DR.DataOffset == Offset && "truncation");
|
|
|
|
DR.StructOffset = 0;
|
|
|
|
DR.CVRegister = CVRegister;
|
|
|
|
return DR;
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeViewDebug::LocalVarDefRange
|
|
|
|
CodeViewDebug::createDefRangeReg(uint16_t CVRegister) {
|
|
|
|
LocalVarDefRange DR;
|
|
|
|
DR.InMemory = 0;
|
|
|
|
DR.DataOffset = 0;
|
|
|
|
DR.StructOffset = 0;
|
|
|
|
DR.CVRegister = CVRegister;
|
|
|
|
return DR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeViewDebug::collectVariableInfoFromMMITable(
|
|
|
|
DenseSet<InlinedVariable> &Processed) {
|
|
|
|
const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget();
|
|
|
|
const TargetFrameLowering *TFI = TSI.getFrameLowering();
|
|
|
|
const TargetRegisterInfo *TRI = TSI.getRegisterInfo();
|
|
|
|
|
|
|
|
for (const MachineModuleInfo::VariableDbgInfo &VI :
|
|
|
|
MMI->getVariableDbgInfo()) {
|
2016-02-11 04:55:49 +08:00
|
|
|
if (!VI.Var)
|
|
|
|
continue;
|
|
|
|
assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
|
|
|
|
"Expected inlined-at fields to agree");
|
|
|
|
|
2016-02-13 05:48:30 +08:00
|
|
|
Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt()));
|
2016-02-11 04:55:49 +08:00
|
|
|
LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
|
|
|
|
|
|
|
|
// If variable scope is not found then skip this variable.
|
|
|
|
if (!Scope)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get the frame register used and the offset.
|
|
|
|
unsigned FrameReg = 0;
|
2016-02-13 05:48:30 +08:00
|
|
|
int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg);
|
|
|
|
uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg);
|
2016-02-11 04:55:49 +08:00
|
|
|
|
|
|
|
// Calculate the label ranges.
|
2016-02-13 05:48:30 +08:00
|
|
|
LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset);
|
2016-02-11 04:55:49 +08:00
|
|
|
for (const InsnRange &Range : Scope->getRanges()) {
|
|
|
|
const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
|
|
|
|
const MCSymbol *End = getLabelAfterInsn(Range.second);
|
2016-02-13 05:48:30 +08:00
|
|
|
End = End ? End : Asm->getFunctionEnd();
|
|
|
|
DefRange.Ranges.emplace_back(Begin, End);
|
2016-02-11 04:55:49 +08:00
|
|
|
}
|
|
|
|
|
2016-02-13 05:48:30 +08:00
|
|
|
LocalVariable Var;
|
|
|
|
Var.DIVar = VI.Var;
|
|
|
|
Var.DefRanges.emplace_back(std::move(DefRange));
|
|
|
|
recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) {
|
|
|
|
DenseSet<InlinedVariable> Processed;
|
|
|
|
// Grab the variable info that was squirreled away in the MMI side-table.
|
|
|
|
collectVariableInfoFromMMITable(Processed);
|
|
|
|
|
|
|
|
const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo();
|
|
|
|
|
|
|
|
for (const auto &I : DbgValues) {
|
|
|
|
InlinedVariable IV = I.first;
|
|
|
|
if (Processed.count(IV))
|
|
|
|
continue;
|
|
|
|
const DILocalVariable *DIVar = IV.first;
|
|
|
|
const DILocation *InlinedAt = IV.second;
|
|
|
|
|
|
|
|
// Instruction ranges, specifying where IV is accessible.
|
|
|
|
const auto &Ranges = I.second;
|
|
|
|
|
|
|
|
LexicalScope *Scope = nullptr;
|
|
|
|
if (InlinedAt)
|
|
|
|
Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt);
|
|
|
|
else
|
|
|
|
Scope = LScopes.findLexicalScope(DIVar->getScope());
|
|
|
|
// If variable scope is not found then skip this variable.
|
|
|
|
if (!Scope)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LocalVariable Var;
|
|
|
|
Var.DIVar = DIVar;
|
|
|
|
|
|
|
|
// Calculate the definition ranges.
|
|
|
|
for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
|
|
|
|
const InsnRange &Range = *I;
|
|
|
|
const MachineInstr *DVInst = Range.first;
|
|
|
|
assert(DVInst->isDebugValue() && "Invalid History entry");
|
|
|
|
const DIExpression *DIExpr = DVInst->getDebugExpression();
|
|
|
|
|
|
|
|
// Bail if there is a complex DWARF expression for now.
|
|
|
|
if (DIExpr && DIExpr->getNumElements() > 0)
|
|
|
|
continue;
|
|
|
|
|
2016-02-17 05:49:26 +08:00
|
|
|
// Bail if operand 0 is not a valid register. This means the variable is a
|
|
|
|
// simple constant, or is described by a complex expression.
|
|
|
|
// FIXME: Find a way to represent constant variables, since they are
|
|
|
|
// relatively common.
|
|
|
|
unsigned Reg =
|
|
|
|
DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0;
|
|
|
|
if (Reg == 0)
|
2016-02-17 05:14:51 +08:00
|
|
|
continue;
|
|
|
|
|
2016-02-13 05:48:30 +08:00
|
|
|
// Handle the two cases we can handle: indirect in memory and in register.
|
|
|
|
bool IsIndirect = DVInst->getOperand(1).isImm();
|
|
|
|
unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg());
|
|
|
|
{
|
|
|
|
LocalVarDefRange DefRange;
|
|
|
|
if (IsIndirect) {
|
|
|
|
int64_t Offset = DVInst->getOperand(1).getImm();
|
|
|
|
DefRange = createDefRangeMem(CVReg, Offset);
|
|
|
|
} else {
|
|
|
|
DefRange = createDefRangeReg(CVReg);
|
|
|
|
}
|
|
|
|
if (Var.DefRanges.empty() ||
|
|
|
|
Var.DefRanges.back().isDifferentLocation(DefRange)) {
|
|
|
|
Var.DefRanges.emplace_back(std::move(DefRange));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the label range.
|
|
|
|
const MCSymbol *Begin = getLabelBeforeInsn(Range.first);
|
|
|
|
const MCSymbol *End = getLabelAfterInsn(Range.second);
|
|
|
|
if (!End) {
|
|
|
|
if (std::next(I) != E)
|
|
|
|
End = getLabelBeforeInsn(std::next(I)->first);
|
|
|
|
else
|
|
|
|
End = Asm->getFunctionEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the last range end is our begin, just extend the last range.
|
|
|
|
// Otherwise make a new range.
|
|
|
|
SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges =
|
|
|
|
Var.DefRanges.back().Ranges;
|
|
|
|
if (!Ranges.empty() && Ranges.back().second == Begin)
|
|
|
|
Ranges.back().second = End;
|
|
|
|
else
|
|
|
|
Ranges.emplace_back(Begin, End);
|
|
|
|
|
|
|
|
// FIXME: Do more range combining.
|
2016-02-11 04:55:49 +08:00
|
|
|
}
|
2016-02-13 05:48:30 +08:00
|
|
|
|
|
|
|
recordLocalVariable(std::move(Var), InlinedAt);
|
2016-02-11 04:55:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::beginFunction(const MachineFunction *MF) {
|
2014-01-30 09:39:17 +08:00
|
|
|
assert(!CurFn && "Can't process two functions at once!");
|
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
if (!Asm || !MMI->hasDebugInfo())
|
2014-01-30 09:39:17 +08:00
|
|
|
return;
|
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
DebugHandlerBase::beginFunction(MF);
|
|
|
|
|
2014-01-30 09:39:17 +08:00
|
|
|
const Function *GV = MF->getFunction();
|
|
|
|
assert(FnDebugInfo.count(GV) == false);
|
|
|
|
CurFn = &FnDebugInfo[GV];
|
2016-01-29 08:49:42 +08:00
|
|
|
CurFn->FuncId = NextFuncId++;
|
2016-02-03 01:41:18 +08:00
|
|
|
CurFn->Begin = Asm->getFunctionBegin();
|
2014-01-30 09:39:17 +08:00
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
// Find the end of the function prolog. First known non-DBG_VALUE and
|
|
|
|
// non-frame setup location marks the beginning of the function body.
|
2014-01-30 09:39:17 +08:00
|
|
|
// FIXME: is there a simpler a way to do this? Can we just search
|
|
|
|
// for the first instruction of the function, not the last of the prolog?
|
|
|
|
DebugLoc PrologEndLoc;
|
|
|
|
bool EmptyPrologue = true;
|
2014-05-01 06:17:38 +08:00
|
|
|
for (const auto &MBB : *MF) {
|
|
|
|
for (const auto &MI : MBB) {
|
2016-02-11 04:55:49 +08:00
|
|
|
if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
|
|
|
|
MI.getDebugLoc()) {
|
2014-05-01 06:17:38 +08:00
|
|
|
PrologEndLoc = MI.getDebugLoc();
|
2014-01-30 09:39:17 +08:00
|
|
|
break;
|
2016-02-11 04:55:49 +08:00
|
|
|
} else if (!MI.isDebugValue()) {
|
|
|
|
EmptyPrologue = false;
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 04:55:49 +08:00
|
|
|
|
2014-01-30 09:39:17 +08:00
|
|
|
// Record beginning of function if we have a non-empty prologue.
|
2015-03-31 03:14:47 +08:00
|
|
|
if (PrologEndLoc && !EmptyPrologue) {
|
|
|
|
DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
|
2014-01-30 09:39:17 +08:00
|
|
|
maybeRecordLocation(FnStartDL, MF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 04:55:49 +08:00
|
|
|
void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) {
|
|
|
|
// LocalSym record, see SymbolRecord.h for more info.
|
|
|
|
MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(),
|
|
|
|
*LocalEnd = MMI->getContext().createTempSymbol();
|
|
|
|
OS.AddComment("Record length");
|
|
|
|
OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2);
|
|
|
|
OS.EmitLabel(LocalBegin);
|
|
|
|
|
|
|
|
OS.AddComment("Record kind: S_LOCAL");
|
|
|
|
OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2);
|
|
|
|
|
|
|
|
uint16_t Flags = 0;
|
|
|
|
if (Var.DIVar->isParameter())
|
|
|
|
Flags |= LocalSym::IsParameter;
|
2016-02-13 05:48:30 +08:00
|
|
|
if (Var.DefRanges.empty())
|
|
|
|
Flags |= LocalSym::IsOptimizedOut;
|
2016-02-11 04:55:49 +08:00
|
|
|
|
|
|
|
OS.AddComment("TypeIndex");
|
|
|
|
OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4);
|
|
|
|
OS.AddComment("Flags");
|
|
|
|
OS.EmitIntValue(Flags, 2);
|
2016-03-13 18:53:30 +08:00
|
|
|
// Truncate the name so we won't overflow the record length field.
|
2016-03-14 13:15:09 +08:00
|
|
|
emitNullTerminatedSymbolName(OS, Var.DIVar->getName());
|
2016-02-11 04:55:49 +08:00
|
|
|
OS.EmitLabel(LocalEnd);
|
|
|
|
|
2016-02-13 05:48:30 +08:00
|
|
|
// Calculate the on disk prefix of the appropriate def range record. The
|
|
|
|
// records and on disk formats are described in SymbolRecords.h. BytePrefix
|
|
|
|
// should be big enough to hold all forms without memory allocation.
|
|
|
|
SmallString<20> BytePrefix;
|
|
|
|
for (const LocalVarDefRange &DefRange : Var.DefRanges) {
|
|
|
|
BytePrefix.clear();
|
|
|
|
// FIXME: Handle bitpieces.
|
|
|
|
if (DefRange.StructOffset != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (DefRange.InMemory) {
|
|
|
|
DefRangeRegisterRelSym Sym{};
|
|
|
|
ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL);
|
|
|
|
Sym.BaseRegister = DefRange.CVRegister;
|
|
|
|
Sym.Flags = 0; // Unclear what matters here.
|
|
|
|
Sym.BasePointerOffset = DefRange.DataOffset;
|
|
|
|
BytePrefix +=
|
|
|
|
StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
|
|
|
|
BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
|
|
|
|
sizeof(Sym) - sizeof(LocalVariableAddrRange));
|
|
|
|
} else {
|
|
|
|
assert(DefRange.DataOffset == 0 && "unexpected offset into register");
|
|
|
|
DefRangeRegisterSym Sym{};
|
|
|
|
ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER);
|
|
|
|
Sym.Register = DefRange.CVRegister;
|
|
|
|
Sym.MayHaveNoName = 0; // Unclear what matters here.
|
|
|
|
BytePrefix +=
|
|
|
|
StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind));
|
|
|
|
BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym),
|
|
|
|
sizeof(Sym) - sizeof(LocalVariableAddrRange));
|
|
|
|
}
|
|
|
|
OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix);
|
|
|
|
}
|
2016-02-11 04:55:49 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::endFunction(const MachineFunction *MF) {
|
2014-01-30 09:39:17 +08:00
|
|
|
if (!Asm || !CurFn) // We haven't created any debug info for this function.
|
|
|
|
return;
|
|
|
|
|
2014-03-26 19:24:36 +08:00
|
|
|
const Function *GV = MF->getFunction();
|
2014-06-20 18:26:56 +08:00
|
|
|
assert(FnDebugInfo.count(GV));
|
2014-03-26 19:24:36 +08:00
|
|
|
assert(CurFn == &FnDebugInfo[GV]);
|
|
|
|
|
2016-03-11 10:14:16 +08:00
|
|
|
collectVariableInfo(GV->getSubprogram());
|
2016-02-13 05:48:30 +08:00
|
|
|
|
|
|
|
DebugHandlerBase::endFunction(MF);
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
// Don't emit anything if we don't have any line tables.
|
|
|
|
if (!CurFn->HaveLineInfo) {
|
2014-03-26 19:24:36 +08:00
|
|
|
FnDebugInfo.erase(GV);
|
2016-02-11 04:55:49 +08:00
|
|
|
CurFn = nullptr;
|
|
|
|
return;
|
2014-03-26 17:50:36 +08:00
|
|
|
}
|
2016-02-11 04:55:49 +08:00
|
|
|
|
|
|
|
CurFn->End = Asm->getFunctionEnd();
|
|
|
|
|
2014-04-24 14:44:33 +08:00
|
|
|
CurFn = nullptr;
|
2014-01-30 09:39:17 +08:00
|
|
|
}
|
|
|
|
|
2016-01-15 03:25:04 +08:00
|
|
|
void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
|
2016-02-11 04:55:49 +08:00
|
|
|
DebugHandlerBase::beginInstruction(MI);
|
|
|
|
|
2014-01-30 09:39:17 +08:00
|
|
|
// Ignore DBG_VALUE locations and function prologue.
|
|
|
|
if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
|
|
|
|
return;
|
|
|
|
DebugLoc DL = MI->getDebugLoc();
|
2015-03-31 03:14:47 +08:00
|
|
|
if (DL == PrevInstLoc || !DL)
|
2014-01-30 09:39:17 +08:00
|
|
|
return;
|
|
|
|
maybeRecordLocation(DL, Asm->MF);
|
|
|
|
}
|