blob: b503f1dc31e45d0119cc2cec83b54d11cc3cf489 [file] [log] [blame]
Reid Kleckner70f5bc92016-01-14 19:25:04 +00001//===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===//
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Kleckner70f5bc92016-01-14 19:25:04 +000010// This file contains support for writing Microsoft CodeView debug info.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Kleckner70f5bc92016-01-14 19:25:04 +000014#include "CodeViewDebug.h"
Reid Kleckner6b3faef2016-01-13 23:44:57 +000015#include "llvm/DebugInfo/CodeView/CodeView.h"
Reid Klecknerc62e3792016-01-28 23:31:52 +000016#include "llvm/DebugInfo/CodeView/Line.h"
Reid Kleckner6b3faef2016-01-13 23:44:57 +000017#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000018#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCSymbol.h"
20#include "llvm/Support/COFF.h"
21
Reid Kleckner6b3faef2016-01-13 23:44:57 +000022using namespace llvm::codeview;
23
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000024namespace llvm {
25
Reid Kleckner9533af42016-01-16 00:09:09 +000026StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
27 std::string &Filepath = FileToFilepathMap[File];
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000028 if (!Filepath.empty())
29 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000030
Reid Kleckner9533af42016-01-16 00:09:09 +000031 StringRef Dir = File->getDirectory(), Filename = File->getFilename();
32
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000033 // Clang emits directory and relative filename info into the IR, but CodeView
34 // operates on full paths. We could change Clang to emit full paths too, but
35 // that would increase the IR size and probably not needed for other users.
36 // For now, just concatenate and canonicalize the path here.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000037 if (Filename.find(':') == 1)
38 Filepath = Filename;
39 else
Yaron Keren75e0c4b2015-03-27 17:51:30 +000040 Filepath = (Dir + "\\" + Filename).str();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000041
42 // Canonicalize the path. We have to do it textually because we may no longer
43 // have access the file in the filesystem.
44 // First, replace all slashes with backslashes.
45 std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
46
47 // Remove all "\.\" with "\".
48 size_t Cursor = 0;
49 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
50 Filepath.erase(Cursor, 2);
51
52 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original
53 // path should be well-formatted, e.g. start with a drive letter, etc.
54 Cursor = 0;
55 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
56 // Something's wrong if the path starts with "\..\", abort.
57 if (Cursor == 0)
58 break;
59
60 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
61 if (PrevSlash == std::string::npos)
62 // Something's wrong, abort.
63 break;
64
65 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
66 // The next ".." might be following the one we've just erased.
67 Cursor = PrevSlash;
68 }
69
70 // Remove all duplicate backslashes.
71 Cursor = 0;
72 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
73 Filepath.erase(Cursor, 1);
74
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000075 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000076}
77
Reid Klecknerc62e3792016-01-28 23:31:52 +000078unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
79 unsigned NextId = FileIdMap.size() + 1;
80 auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
81 if (Insertion.second) {
82 // We have to compute the full filepath and emit a .cv_file directive.
83 StringRef FullPath = getFullFilepath(F);
84 NextId = Asm->OutStreamer->EmitCVFileDirective(NextId, FullPath);
85 assert(NextId == FileIdMap.size() && ".cv_file directive failed");
86 }
87 return Insertion.first->second;
88}
89
Reid Kleckner70f5bc92016-01-14 19:25:04 +000090void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
Reid Kleckner9533af42016-01-16 00:09:09 +000091 const MachineFunction *MF) {
92 // Skip this instruction if it has the same location as the previous one.
93 if (DL == CurFn->LastLoc)
94 return;
95
96 const DIScope *Scope = DL.get()->getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000097 if (!Scope)
98 return;
Reid Kleckner9533af42016-01-16 00:09:09 +000099
David Majnemerc3340db2016-01-13 01:05:23 +0000100 // Skip this line if it is longer than the maximum we can record.
Reid Klecknerc62e3792016-01-28 23:31:52 +0000101 LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
102 if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
103 LI.isNeverStepInto())
David Majnemerc3340db2016-01-13 01:05:23 +0000104 return;
105
Reid Klecknerc62e3792016-01-28 23:31:52 +0000106 ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
107 if (CI.getStartColumn() != DL.getCol())
108 return;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000109
Reid Klecknerc62e3792016-01-28 23:31:52 +0000110 if (!CurFn->HaveLineInfo)
111 CurFn->HaveLineInfo = true;
112 unsigned FileId = 0;
113 if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
114 FileId = CurFn->LastFileId;
115 else
116 FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
117 CurFn->LastLoc = DL;
118 Asm->OutStreamer->EmitCVLocDirective(CurFn->FuncId, FileId, DL.getLine(),
119 DL.getCol(), /*PrologueEnd=*/false,
120 /*IsStmt=*/false, DL->getFilename());
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000121}
122
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000123CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
Craig Topper353eda42014-04-24 06:44:33 +0000124 : Asm(nullptr), CurFn(nullptr) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000125 MachineModuleInfo *MMI = AP->MMI;
126
127 // If module doesn't have named metadata anchors or COFF debug section
128 // is not available, skip any debug info related stuff.
129 if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
130 !AP->getObjFileLowering().getCOFFDebugSymbolsSection())
131 return;
132
133 // Tell MMI that we have debug info.
134 MMI->setDebugInfoAvailability(true);
135 Asm = AP;
136}
137
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000138void CodeViewDebug::endModule() {
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000139 if (FnDebugInfo.empty())
140 return;
141
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000142 // FIXME: For functions that are comdat, we should emit separate .debug$S
143 // sections that are comdat associative with the main function instead of
144 // having one big .debug$S section.
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000145 assert(Asm != nullptr);
Lang Hames9ff69c82015-04-24 19:11:51 +0000146 Asm->OutStreamer->SwitchSection(
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000147 Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
148 Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
149
150 // The COFF .debug$S section consists of several subsections, each starting
151 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
152 // of the payload followed by the payload itself. The subsections are 4-byte
153 // aligned.
154
Reid Klecknerc62e3792016-01-28 23:31:52 +0000155 // Emit per-function debug information.
156 for (auto &P : FnDebugInfo)
157 emitDebugInfoForFunction(P.first, P.second);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000158
159 // This subsection holds a file index to offset in string table table.
Lang Hames9ff69c82015-04-24 19:11:51 +0000160 Asm->OutStreamer->AddComment("File index to string table offset subsection");
Reid Klecknerc62e3792016-01-28 23:31:52 +0000161 Asm->OutStreamer->EmitCVFileChecksumsDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000162
163 // This subsection holds the string table.
Lang Hames9ff69c82015-04-24 19:11:51 +0000164 Asm->OutStreamer->AddComment("String table");
Reid Klecknerc62e3792016-01-28 23:31:52 +0000165 Asm->OutStreamer->EmitCVStringTableDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000166
167 clear();
168}
169
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000170static void EmitLabelDiff(MCStreamer &Streamer,
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000171 const MCSymbol *From, const MCSymbol *To,
172 unsigned int Size = 4) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000173 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
174 MCContext &Context = Streamer.getContext();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000175 const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context),
176 *ToRef = MCSymbolRefExpr::create(To, Variant, Context);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000177 const MCExpr *AddrDelta =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000178 MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000179 Streamer.EmitValue(AddrDelta, Size);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000180}
181
Reid Klecknerc62e3792016-01-28 23:31:52 +0000182void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
183 FunctionInfo &FI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000184 // For each function there is a separate subsection
185 // which holds the PC to file:line table.
186 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000187 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000188
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000189 StringRef FuncName;
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000190 if (auto *SP = getDISubprogram(GV))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000191 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000192
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000193 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000194 if (FuncName.empty())
195 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000196
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000197 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Jim Grosbach6f482002015-05-18 18:43:14 +0000198 MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(),
199 *SymbolsEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000200 Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName));
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000201 Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols));
Lang Hames9ff69c82015-04-24 19:11:51 +0000202 EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd);
203 Asm->OutStreamer->EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000204 {
Jim Grosbach6f482002015-05-18 18:43:14 +0000205 MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(),
206 *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000207 EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2);
208 Asm->OutStreamer->EmitLabel(ProcSegmentBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000209
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000210 Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID));
211
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000212 // Some bytes of this segment don't seem to be required for basic debugging,
213 // so just fill them with zeroes.
Lang Hames9ff69c82015-04-24 19:11:51 +0000214 Asm->OutStreamer->EmitFill(12, 0);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000215 // This is the important bit that tells the debugger where the function
216 // code is located and what's its size:
Lang Hames9ff69c82015-04-24 19:11:51 +0000217 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
218 Asm->OutStreamer->EmitFill(12, 0);
219 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
220 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000221 Asm->EmitInt8(0);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000222 // Emit the function display name as a null-terminated string.
Lang Hames9ff69c82015-04-24 19:11:51 +0000223 Asm->OutStreamer->EmitBytes(FuncName);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000224 Asm->EmitInt8(0);
Lang Hames9ff69c82015-04-24 19:11:51 +0000225 Asm->OutStreamer->EmitLabel(ProcSegmentEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000226
227 // We're done with this function.
228 Asm->EmitInt16(0x0002);
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000229 Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END));
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000230 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000231 Asm->OutStreamer->EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000232 // Every subsection must be aligned to a 4-byte boundary.
Lang Hames9ff69c82015-04-24 19:11:51 +0000233 Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000234
Reid Klecknerc62e3792016-01-28 23:31:52 +0000235 // We have an assembler directive that takes care of the whole line table.
236 Asm->OutStreamer->EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000237}
238
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000239void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000240 assert(!CurFn && "Can't process two functions at once!");
241
242 if (!Asm || !Asm->MMI->hasDebugInfo())
243 return;
244
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000245 const Function *GV = MF->getFunction();
246 assert(FnDebugInfo.count(GV) == false);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000247 CurFn = &FnDebugInfo[GV];
Reid Klecknerc62e3792016-01-28 23:31:52 +0000248 CurFn->FuncId = NextFuncId++;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000249
250 // Find the end of the function prolog.
251 // FIXME: is there a simpler a way to do this? Can we just search
252 // for the first instruction of the function, not the last of the prolog?
253 DebugLoc PrologEndLoc;
254 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000255 for (const auto &MBB : *MF) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000256 if (PrologEndLoc)
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000257 break;
258 for (const auto &MI : MBB) {
259 if (MI.isDebugValue())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000260 continue;
261
262 // First known non-DBG_VALUE and non-frame setup location marks
263 // the beginning of the function body.
264 // FIXME: do we need the first subcondition?
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000265 if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000266 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000267 break;
268 }
269 EmptyPrologue = false;
270 }
271 }
272 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000273 if (PrologEndLoc && !EmptyPrologue) {
274 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000275 maybeRecordLocation(FnStartDL, MF);
276 }
277}
278
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000279void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000280 if (!Asm || !CurFn) // We haven't created any debug info for this function.
281 return;
282
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000283 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000284 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000285 assert(CurFn == &FnDebugInfo[GV]);
286
Reid Klecknerc62e3792016-01-28 23:31:52 +0000287 // Don't emit anything if we don't have any line tables.
288 if (!CurFn->HaveLineInfo) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000289 FnDebugInfo.erase(GV);
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000290 } else {
Rafael Espindola07c03d32015-03-05 02:05:42 +0000291 CurFn->End = Asm->getFunctionEnd();
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000292 }
Craig Topper353eda42014-04-24 06:44:33 +0000293 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000294}
295
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000296void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000297 // Ignore DBG_VALUE locations and function prologue.
298 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
299 return;
300 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000301 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000302 return;
303 maybeRecordLocation(DL, Asm->MF);
304}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000305}