blob: 032f611f20e3a260437e288c2c6174893e7c5c71 [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 Kleckner2214ed82016-01-29 00:49:42 +000016#include "llvm/DebugInfo/CodeView/Line.h"
Reid Kleckner6b3faef2016-01-13 23:44:57 +000017#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000018#include "llvm/DebugInfo/CodeView/TypeIndex.h"
19#include "llvm/DebugInfo/CodeView/TypeRecord.h"
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000020#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCSymbol.h"
22#include "llvm/Support/COFF.h"
23
Reid Kleckner6b3faef2016-01-13 23:44:57 +000024using namespace llvm::codeview;
25
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000026namespace llvm {
27
Reid Kleckner9533af42016-01-16 00:09:09 +000028StringRef CodeViewDebug::getFullFilepath(const DIFile *File) {
29 std::string &Filepath = FileToFilepathMap[File];
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000030 if (!Filepath.empty())
31 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000032
Reid Kleckner9533af42016-01-16 00:09:09 +000033 StringRef Dir = File->getDirectory(), Filename = File->getFilename();
34
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000035 // Clang emits directory and relative filename info into the IR, but CodeView
36 // operates on full paths. We could change Clang to emit full paths too, but
37 // that would increase the IR size and probably not needed for other users.
38 // For now, just concatenate and canonicalize the path here.
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000039 if (Filename.find(':') == 1)
40 Filepath = Filename;
41 else
Yaron Keren75e0c4b2015-03-27 17:51:30 +000042 Filepath = (Dir + "\\" + Filename).str();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000043
44 // Canonicalize the path. We have to do it textually because we may no longer
45 // have access the file in the filesystem.
46 // First, replace all slashes with backslashes.
47 std::replace(Filepath.begin(), Filepath.end(), '/', '\\');
48
49 // Remove all "\.\" with "\".
50 size_t Cursor = 0;
51 while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos)
52 Filepath.erase(Cursor, 2);
53
54 // Replace all "\XXX\..\" with "\". Don't try too hard though as the original
55 // path should be well-formatted, e.g. start with a drive letter, etc.
56 Cursor = 0;
57 while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) {
58 // Something's wrong if the path starts with "\..\", abort.
59 if (Cursor == 0)
60 break;
61
62 size_t PrevSlash = Filepath.rfind('\\', Cursor - 1);
63 if (PrevSlash == std::string::npos)
64 // Something's wrong, abort.
65 break;
66
67 Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash);
68 // The next ".." might be following the one we've just erased.
69 Cursor = PrevSlash;
70 }
71
72 // Remove all duplicate backslashes.
73 Cursor = 0;
74 while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos)
75 Filepath.erase(Cursor, 1);
76
Reid Kleckner1f11b4e2015-12-02 22:34:30 +000077 return Filepath;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +000078}
79
Reid Kleckner2214ed82016-01-29 00:49:42 +000080unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) {
81 unsigned NextId = FileIdMap.size() + 1;
82 auto Insertion = FileIdMap.insert(std::make_pair(F, NextId));
83 if (Insertion.second) {
84 // We have to compute the full filepath and emit a .cv_file directive.
85 StringRef FullPath = getFullFilepath(F);
86 NextId = Asm->OutStreamer->EmitCVFileDirective(NextId, FullPath);
87 assert(NextId == FileIdMap.size() && ".cv_file directive failed");
88 }
89 return Insertion.first->second;
90}
91
Reid Klecknerf3b9ba42016-01-29 18:16:43 +000092CodeViewDebug::InlineSite &CodeViewDebug::getInlineSite(const DILocation *Loc) {
93 const DILocation *InlinedAt = Loc->getInlinedAt();
94 auto Insertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()});
95 if (Insertion.second) {
96 InlineSite &Site = Insertion.first->second;
97 Site.SiteFuncId = NextFuncId++;
98 Site.Inlinee = Loc->getScope()->getSubprogram();
Reid Kleckner1fcd6102016-02-02 17:41:18 +000099 InlinedSubprograms.insert(Loc->getScope()->getSubprogram());
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000100 }
101 return Insertion.first->second;
102}
103
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000104void CodeViewDebug::maybeRecordLocation(DebugLoc DL,
Reid Kleckner9533af42016-01-16 00:09:09 +0000105 const MachineFunction *MF) {
106 // Skip this instruction if it has the same location as the previous one.
107 if (DL == CurFn->LastLoc)
108 return;
109
110 const DIScope *Scope = DL.get()->getScope();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000111 if (!Scope)
112 return;
Reid Kleckner9533af42016-01-16 00:09:09 +0000113
David Majnemerc3340db2016-01-13 01:05:23 +0000114 // Skip this line if it is longer than the maximum we can record.
Reid Kleckner2214ed82016-01-29 00:49:42 +0000115 LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true);
116 if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() ||
117 LI.isNeverStepInto())
David Majnemerc3340db2016-01-13 01:05:23 +0000118 return;
119
Reid Kleckner2214ed82016-01-29 00:49:42 +0000120 ColumnInfo CI(DL.getCol(), /*EndColumn=*/0);
121 if (CI.getStartColumn() != DL.getCol())
122 return;
Reid Kleckner00d96392016-01-29 00:13:28 +0000123
Reid Kleckner2214ed82016-01-29 00:49:42 +0000124 if (!CurFn->HaveLineInfo)
125 CurFn->HaveLineInfo = true;
126 unsigned FileId = 0;
127 if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile())
128 FileId = CurFn->LastFileId;
129 else
130 FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile());
131 CurFn->LastLoc = DL;
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000132
133 unsigned FuncId = CurFn->FuncId;
134 if (const DILocation *Loc = DL->getInlinedAt()) {
135 // If this location was actually inlined from somewhere else, give it the ID
136 // of the inline call site.
137 FuncId = getInlineSite(DL.get()).SiteFuncId;
138 // Ensure we have links in the tree of inline call sites.
139 const DILocation *ChildLoc = nullptr;
140 while (Loc->getInlinedAt()) {
141 InlineSite &Site = getInlineSite(Loc);
142 if (ChildLoc) {
143 // Record the child inline site if not already present.
144 auto B = Site.ChildSites.begin(), E = Site.ChildSites.end();
145 if (std::find(B, E, Loc) != E)
146 break;
147 Site.ChildSites.push_back(Loc);
148 }
149 ChildLoc = Loc;
150 }
151 }
152
153 Asm->OutStreamer->EmitCVLocDirective(FuncId, FileId, DL.getLine(),
Reid Kleckner2214ed82016-01-29 00:49:42 +0000154 DL.getCol(), /*PrologueEnd=*/false,
155 /*IsStmt=*/false, DL->getFilename());
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000156}
157
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000158CodeViewDebug::CodeViewDebug(AsmPrinter *AP)
Craig Topper353eda42014-04-24 06:44:33 +0000159 : Asm(nullptr), CurFn(nullptr) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000160 MachineModuleInfo *MMI = AP->MMI;
161
162 // If module doesn't have named metadata anchors or COFF debug section
163 // is not available, skip any debug info related stuff.
164 if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") ||
165 !AP->getObjFileLowering().getCOFFDebugSymbolsSection())
166 return;
167
168 // Tell MMI that we have debug info.
169 MMI->setDebugInfoAvailability(true);
170 Asm = AP;
171}
172
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000173void CodeViewDebug::endModule() {
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000174 if (FnDebugInfo.empty())
175 return;
176
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000177 emitTypeInformation();
178
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000179 // FIXME: For functions that are comdat, we should emit separate .debug$S
180 // sections that are comdat associative with the main function instead of
181 // having one big .debug$S section.
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000182 assert(Asm != nullptr);
Lang Hames9ff69c82015-04-24 19:11:51 +0000183 Asm->OutStreamer->SwitchSection(
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000184 Asm->getObjFileLowering().getCOFFDebugSymbolsSection());
David Majnemer30579ec2016-02-02 23:18:23 +0000185 Asm->OutStreamer->AddComment("Debug section magic");
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000186 Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
187
188 // The COFF .debug$S section consists of several subsections, each starting
189 // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length
190 // of the payload followed by the payload itself. The subsections are 4-byte
191 // aligned.
192
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000193 // Make a subsection for all the inlined subprograms.
194 emitInlineeLinesSubsection();
195
Reid Kleckner2214ed82016-01-29 00:49:42 +0000196 // Emit per-function debug information.
197 for (auto &P : FnDebugInfo)
198 emitDebugInfoForFunction(P.first, P.second);
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000199
200 // This subsection holds a file index to offset in string table table.
Lang Hames9ff69c82015-04-24 19:11:51 +0000201 Asm->OutStreamer->AddComment("File index to string table offset subsection");
Reid Kleckner2214ed82016-01-29 00:49:42 +0000202 Asm->OutStreamer->EmitCVFileChecksumsDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000203
204 // This subsection holds the string table.
Lang Hames9ff69c82015-04-24 19:11:51 +0000205 Asm->OutStreamer->AddComment("String table");
Reid Kleckner2214ed82016-01-29 00:49:42 +0000206 Asm->OutStreamer->EmitCVStringTableDirective();
Timur Iskhodzhanov2cf8a1d2014-10-10 16:05:32 +0000207
208 clear();
209}
210
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000211void CodeViewDebug::emitTypeInformation() {
212 // Start the .debug$T section with 0x4.
213 Asm->OutStreamer->SwitchSection(
214 Asm->getObjFileLowering().getCOFFDebugTypesSection());
David Majnemer30579ec2016-02-02 23:18:23 +0000215 Asm->OutStreamer->AddComment("Debug section magic");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000216 Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC);
217
218 NamedMDNode *CU_Nodes =
219 Asm->MMI->getModule()->getNamedMetadata("llvm.dbg.cu");
220 if (!CU_Nodes)
221 return;
222
223 // This type info currently only holds function ids for use with inline call
224 // frame info. All functions are assigned a simple 'void ()' type. Emit that
225 // type here.
226 TypeIndex ArgListIdx = getNextTypeIndex();
David Majnemer30579ec2016-02-02 23:18:23 +0000227 Asm->OutStreamer->AddComment("Type record length");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000228 Asm->EmitInt16(2 + sizeof(ArgList));
David Majnemer30579ec2016-02-02 23:18:23 +0000229 Asm->OutStreamer->AddComment("Leaf type: LF_ARGLIST");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000230 Asm->EmitInt16(LF_ARGLIST);
David Majnemer30579ec2016-02-02 23:18:23 +0000231 Asm->OutStreamer->AddComment("Number of arguments");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000232 Asm->EmitInt32(0);
233
234 TypeIndex VoidProcIdx = getNextTypeIndex();
David Majnemer30579ec2016-02-02 23:18:23 +0000235 Asm->OutStreamer->AddComment("Type record length");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000236 Asm->EmitInt16(2 + sizeof(ProcedureType));
David Majnemer30579ec2016-02-02 23:18:23 +0000237 Asm->OutStreamer->AddComment("Leaf type: LF_PROCEDURE");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000238 Asm->EmitInt16(LF_PROCEDURE);
David Majnemer30579ec2016-02-02 23:18:23 +0000239 Asm->OutStreamer->AddComment("Return type index");
240 Asm->EmitInt32(TypeIndex::Void().getIndex());
241 Asm->OutStreamer->AddComment("Calling convention");
242 Asm->EmitInt8(char(CallingConvention::NearC));
243 Asm->OutStreamer->AddComment("Function options");
244 Asm->EmitInt8(char(FunctionOptions::None));
245 Asm->OutStreamer->AddComment("# of parameters");
246 Asm->EmitInt16(0);
247 Asm->OutStreamer->AddComment("Argument list type index");
248 Asm->EmitInt32(ArgListIdx.getIndex());
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000249
250 for (MDNode *N : CU_Nodes->operands()) {
251 auto *CUNode = cast<DICompileUnit>(N);
252 for (auto *SP : CUNode->getSubprograms()) {
253 StringRef DisplayName = SP->getDisplayName();
David Majnemer30579ec2016-02-02 23:18:23 +0000254 Asm->OutStreamer->AddComment("Type record length");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000255 Asm->EmitInt16(2 + sizeof(FuncId) + DisplayName.size() + 1);
David Majnemer30579ec2016-02-02 23:18:23 +0000256 Asm->OutStreamer->AddComment("Leaf type: LF_FUNC_ID");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000257 Asm->EmitInt16(LF_FUNC_ID);
258
David Majnemer30579ec2016-02-02 23:18:23 +0000259 Asm->OutStreamer->AddComment("Scope type index");
260 Asm->EmitInt32(TypeIndex().getIndex());
261 Asm->OutStreamer->AddComment("Function type");
262 Asm->EmitInt32(VoidProcIdx.getIndex());
263 {
264 SmallString<32> NullTerminatedString(DisplayName);
265 if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0')
266 NullTerminatedString.push_back('\0');
267 Asm->OutStreamer->AddComment("Function name");
268 Asm->OutStreamer->EmitBytes(NullTerminatedString);
269 }
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000270
271 TypeIndex FuncIdIdx = getNextTypeIndex();
272 SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx));
273 }
274 }
275}
276
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000277void CodeViewDebug::emitInlineeLinesSubsection() {
278 if (InlinedSubprograms.empty())
279 return;
280
281 MCStreamer &OS = *Asm->OutStreamer;
282 MCSymbol *InlineBegin = Asm->MMI->getContext().createTempSymbol(),
283 *InlineEnd = Asm->MMI->getContext().createTempSymbol();
284
285 OS.AddComment("Inlinee lines subsection");
286 OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000287 OS.AddComment("Subsection size");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000288 OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4);
289 OS.EmitLabel(InlineBegin);
290
291 // We don't provide any extra file info.
292 // FIXME: Find out if debuggers use this info.
David Majnemer30579ec2016-02-02 23:18:23 +0000293 OS.AddComment("Inlinee lines signature");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000294 OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4);
295
296 for (const DISubprogram *SP : InlinedSubprograms) {
David Majnemer30579ec2016-02-02 23:18:23 +0000297 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000298 TypeIndex TypeId = SubprogramToFuncId[SP];
299 unsigned FileId = maybeRecordFile(SP->getFile());
300 OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " +
301 SP->getFilename() + Twine(':') + Twine(SP->getLine()));
David Majnemer30579ec2016-02-02 23:18:23 +0000302 OS.AddBlankLine();
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000303 // The filechecksum table uses 8 byte entries for now, and file ids start at
304 // 1.
305 unsigned FileOffset = (FileId - 1) * 8;
David Majnemer30579ec2016-02-02 23:18:23 +0000306 OS.AddComment("Type index of inlined function");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000307 OS.EmitIntValue(TypeId.getIndex(), 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000308 OS.AddComment("Offset into filechecksum table");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000309 OS.EmitIntValue(FileOffset, 4);
David Majnemer30579ec2016-02-02 23:18:23 +0000310 OS.AddComment("Starting line number");
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000311 OS.EmitIntValue(SP->getLine(), 4);
312 }
313
314 OS.EmitLabel(InlineEnd);
315}
316
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000317static void EmitLabelDiff(MCStreamer &Streamer,
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000318 const MCSymbol *From, const MCSymbol *To,
319 unsigned int Size = 4) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000320 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
321 MCContext &Context = Streamer.getContext();
Jim Grosbach13760bd2015-05-30 01:25:56 +0000322 const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context),
323 *ToRef = MCSymbolRefExpr::create(To, Variant, Context);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000324 const MCExpr *AddrDelta =
Jim Grosbach13760bd2015-05-30 01:25:56 +0000325 MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000326 Streamer.EmitValue(AddrDelta, Size);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000327}
328
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000329void CodeViewDebug::collectInlineSiteChildren(
330 SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI,
331 const InlineSite &Site) {
332 for (const DILocation *ChildSiteLoc : Site.ChildSites) {
333 auto I = FI.InlineSites.find(ChildSiteLoc);
334 assert(I != FI.InlineSites.end());
335 const InlineSite &ChildSite = I->second;
336 Children.push_back(ChildSite.SiteFuncId);
337 collectInlineSiteChildren(Children, FI, ChildSite);
338 }
339}
340
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000341void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI,
342 const DILocation *InlinedAt,
343 const InlineSite &Site) {
344 MCStreamer &OS = *Asm->OutStreamer;
345
346 MCSymbol *InlineBegin = Asm->MMI->getContext().createTempSymbol(),
347 *InlineEnd = Asm->MMI->getContext().createTempSymbol();
348
349 assert(SubprogramToFuncId.count(Site.Inlinee));
350 TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee];
351
352 // SymbolRecord
David Majnemer30579ec2016-02-02 23:18:23 +0000353 Asm->OutStreamer->AddComment("Record length");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000354 EmitLabelDiff(OS, InlineBegin, InlineEnd, 2); // RecordLength
355 OS.EmitLabel(InlineBegin);
David Majnemer30579ec2016-02-02 23:18:23 +0000356 Asm->OutStreamer->AddComment("Record kind: S_INLINESITE");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000357 Asm->EmitInt16(SymbolRecordKind::S_INLINESITE); // RecordKind
358
David Majnemer30579ec2016-02-02 23:18:23 +0000359 Asm->OutStreamer->AddComment("PtrParent");
360 Asm->OutStreamer->EmitIntValue(0, 4);
361 Asm->OutStreamer->AddComment("PtrEnd");
362 Asm->OutStreamer->EmitIntValue(0, 4);
363 Asm->OutStreamer->AddComment("Inlinee type index");
364 Asm->EmitInt32(InlineeIdx.getIndex());
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000365
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000366 unsigned FileId = maybeRecordFile(Site.Inlinee->getFile());
367 unsigned StartLineNum = Site.Inlinee->getLine();
368 SmallVector<unsigned, 3> SecondaryFuncIds;
369 collectInlineSiteChildren(SecondaryFuncIds, FI, Site);
370
371 OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum,
David Majnemerc9911f22016-02-02 19:22:34 +0000372 FI.Begin, FI.End, SecondaryFuncIds);
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000373
374 OS.EmitLabel(InlineEnd);
375
376 // Recurse on child inlined call sites before closing the scope.
377 for (const DILocation *ChildSite : Site.ChildSites) {
378 auto I = FI.InlineSites.find(ChildSite);
379 assert(I != FI.InlineSites.end() &&
380 "child site not in function inline site map");
381 emitInlinedCallSite(FI, ChildSite, I->second);
382 }
383
384 // Close the scope.
David Majnemer30579ec2016-02-02 23:18:23 +0000385 Asm->OutStreamer->AddComment("Record length");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000386 Asm->EmitInt16(2); // RecordLength
David Majnemer30579ec2016-02-02 23:18:23 +0000387 Asm->OutStreamer->AddComment("Record kind: S_INLINESITE_END");
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000388 Asm->EmitInt16(SymbolRecordKind::S_INLINESITE_END); // RecordKind
389}
390
Reid Kleckner2214ed82016-01-29 00:49:42 +0000391void CodeViewDebug::emitDebugInfoForFunction(const Function *GV,
392 FunctionInfo &FI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000393 // For each function there is a separate subsection
394 // which holds the PC to file:line table.
395 const MCSymbol *Fn = Asm->getSymbol(GV);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000396 assert(Fn);
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000397
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000398 StringRef FuncName;
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000399 if (auto *SP = getDISubprogram(GV))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000400 FuncName = SP->getDisplayName();
Duncan P. N. Exon Smith23e56ec2015-03-20 19:50:00 +0000401
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000402 // If our DISubprogram name is empty, use the mangled name.
Reid Kleckner72e2ba72016-01-13 19:32:35 +0000403 if (FuncName.empty())
404 FuncName = GlobalValue::getRealLinkageName(GV->getName());
Reid Kleckner3c0ff982016-01-14 00:12:54 +0000405
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000406 // Emit a symbol subsection, required by VS2012+ to find function boundaries.
Jim Grosbach6f482002015-05-18 18:43:14 +0000407 MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(),
408 *SymbolsEnd = Asm->MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +0000409 Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName));
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000410 Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols));
David Majnemer30579ec2016-02-02 23:18:23 +0000411 Asm->OutStreamer->AddComment("Subsection size");
Lang Hames9ff69c82015-04-24 19:11:51 +0000412 EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd);
413 Asm->OutStreamer->EmitLabel(SymbolsBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000414 {
David Majnemer30579ec2016-02-02 23:18:23 +0000415 MCSymbol *ProcRecordBegin = Asm->MMI->getContext().createTempSymbol(),
416 *ProcRecordEnd = Asm->MMI->getContext().createTempSymbol();
417 Asm->OutStreamer->AddComment("Record length");
418 EmitLabelDiff(*Asm->OutStreamer, ProcRecordBegin, ProcRecordEnd, 2);
419 Asm->OutStreamer->EmitLabel(ProcRecordBegin);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000420
David Majnemer30579ec2016-02-02 23:18:23 +0000421 Asm->OutStreamer->AddComment("Record kind: S_GPROC32_ID");
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000422 Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID));
423
David Majnemer30579ec2016-02-02 23:18:23 +0000424 // These fields are filled in by tools like CVPACK which run after the fact.
425 Asm->OutStreamer->AddComment("PtrParent");
426 Asm->OutStreamer->EmitIntValue(0, 4);
427 Asm->OutStreamer->AddComment("PtrEnd");
428 Asm->OutStreamer->EmitIntValue(0, 4);
429 Asm->OutStreamer->AddComment("PtrNext");
430 Asm->OutStreamer->EmitIntValue(0, 4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000431 // This is the important bit that tells the debugger where the function
432 // code is located and what's its size:
David Majnemer30579ec2016-02-02 23:18:23 +0000433 Asm->OutStreamer->AddComment("Code size");
Lang Hames9ff69c82015-04-24 19:11:51 +0000434 EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End);
David Majnemer30579ec2016-02-02 23:18:23 +0000435 Asm->OutStreamer->AddComment("Offset after prologue");
436 Asm->OutStreamer->EmitIntValue(0, 4);
437 Asm->OutStreamer->AddComment("Offset before epilogue");
438 Asm->OutStreamer->EmitIntValue(0, 4);
439 Asm->OutStreamer->AddComment("Function type index");
440 Asm->OutStreamer->EmitIntValue(0, 4);
441 Asm->OutStreamer->AddComment("Function section relative address");
Lang Hames9ff69c82015-04-24 19:11:51 +0000442 Asm->OutStreamer->EmitCOFFSecRel32(Fn);
David Majnemer30579ec2016-02-02 23:18:23 +0000443 Asm->OutStreamer->AddComment("Function section index");
Lang Hames9ff69c82015-04-24 19:11:51 +0000444 Asm->OutStreamer->EmitCOFFSectionIndex(Fn);
David Majnemer30579ec2016-02-02 23:18:23 +0000445 Asm->OutStreamer->AddComment("Flags");
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000446 Asm->EmitInt8(0);
Timur Iskhodzhanova11b32b2014-11-12 20:10:09 +0000447 // Emit the function display name as a null-terminated string.
David Majnemer30579ec2016-02-02 23:18:23 +0000448 Asm->OutStreamer->AddComment("Function name");
449 {
450 SmallString<32> NullTerminatedString(FuncName);
451 if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0')
452 NullTerminatedString.push_back('\0');
453 Asm->OutStreamer->EmitBytes(NullTerminatedString);
454 }
455 Asm->OutStreamer->EmitLabel(ProcRecordEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000456
Reid Klecknerf3b9ba42016-01-29 18:16:43 +0000457 // Emit inlined call site information. Only emit functions inlined directly
458 // into the parent function. We'll emit the other sites recursively as part
459 // of their parent inline site.
460 for (auto &KV : FI.InlineSites) {
461 const DILocation *InlinedAt = KV.first;
462 if (!InlinedAt->getInlinedAt())
463 emitInlinedCallSite(FI, InlinedAt, KV.second);
464 }
465
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000466 // We're done with this function.
David Majnemer30579ec2016-02-02 23:18:23 +0000467 Asm->OutStreamer->AddComment("Record length");
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000468 Asm->EmitInt16(0x0002);
David Majnemer30579ec2016-02-02 23:18:23 +0000469 Asm->OutStreamer->AddComment("Record kind: S_PROC_ID_END");
Reid Kleckner6b3faef2016-01-13 23:44:57 +0000470 Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END));
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000471 }
Lang Hames9ff69c82015-04-24 19:11:51 +0000472 Asm->OutStreamer->EmitLabel(SymbolsEnd);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000473 // Every subsection must be aligned to a 4-byte boundary.
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000474 Asm->OutStreamer->EmitValueToAlignment(4);
Timur Iskhodzhanov2bc90fd2014-10-24 01:27:45 +0000475
Reid Kleckner2214ed82016-01-29 00:49:42 +0000476 // We have an assembler directive that takes care of the whole line table.
477 Asm->OutStreamer->EmitCVLinetableDirective(FI.FuncId, Fn, FI.End);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000478}
479
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000480void CodeViewDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000481 assert(!CurFn && "Can't process two functions at once!");
482
483 if (!Asm || !Asm->MMI->hasDebugInfo())
484 return;
485
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000486 const Function *GV = MF->getFunction();
487 assert(FnDebugInfo.count(GV) == false);
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000488 CurFn = &FnDebugInfo[GV];
Reid Kleckner2214ed82016-01-29 00:49:42 +0000489 CurFn->FuncId = NextFuncId++;
Reid Kleckner1fcd6102016-02-02 17:41:18 +0000490 CurFn->Begin = Asm->getFunctionBegin();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000491
492 // Find the end of the function prolog.
493 // FIXME: is there a simpler a way to do this? Can we just search
494 // for the first instruction of the function, not the last of the prolog?
495 DebugLoc PrologEndLoc;
496 bool EmptyPrologue = true;
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000497 for (const auto &MBB : *MF) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000498 if (PrologEndLoc)
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000499 break;
500 for (const auto &MI : MBB) {
501 if (MI.isDebugValue())
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000502 continue;
503
504 // First known non-DBG_VALUE and non-frame setup location marks
505 // the beginning of the function body.
506 // FIXME: do we need the first subcondition?
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000507 if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) {
Alexey Samsonovf74bde62014-04-30 22:17:38 +0000508 PrologEndLoc = MI.getDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000509 break;
510 }
511 EmptyPrologue = false;
512 }
513 }
514 // Record beginning of function if we have a non-empty prologue.
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000515 if (PrologEndLoc && !EmptyPrologue) {
516 DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc();
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000517 maybeRecordLocation(FnStartDL, MF);
518 }
519}
520
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000521void CodeViewDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000522 if (!Asm || !CurFn) // We haven't created any debug info for this function.
523 return;
524
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000525 const Function *GV = MF->getFunction();
Yaron Keren6d3194f2014-06-20 10:26:56 +0000526 assert(FnDebugInfo.count(GV));
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000527 assert(CurFn == &FnDebugInfo[GV]);
528
Reid Kleckner2214ed82016-01-29 00:49:42 +0000529 // Don't emit anything if we don't have any line tables.
530 if (!CurFn->HaveLineInfo) {
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000531 FnDebugInfo.erase(GV);
Timur Iskhodzhanovb5b7a612014-03-26 11:24:36 +0000532 } else {
Rafael Espindola07c03d32015-03-05 02:05:42 +0000533 CurFn->End = Asm->getFunctionEnd();
Timur Iskhodzhanov8499a122014-03-26 09:50:36 +0000534 }
Craig Topper353eda42014-04-24 06:44:33 +0000535 CurFn = nullptr;
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000536}
537
Reid Kleckner70f5bc92016-01-14 19:25:04 +0000538void CodeViewDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000539 // Ignore DBG_VALUE locations and function prologue.
540 if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup))
541 return;
542 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000543 if (DL == PrevInstLoc || !DL)
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +0000544 return;
545 maybeRecordLocation(DL, Asm->MF);
546}
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000547}