Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===// |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2 | // |
| 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 Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 10 | // This file contains support for writing Microsoft CodeView debug info. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 14 | #include "CodeViewDebug.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/Line.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 19 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/MC/MCSymbol.h" |
| 22 | #include "llvm/Support/COFF.h" |
| 23 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 24 | using namespace llvm::codeview; |
| 25 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 28 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 29 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 30 | if (!Filepath.empty()) |
| 31 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 32 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 33 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 34 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 35 | // 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 Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 39 | if (Filename.find(':') == 1) |
| 40 | Filepath = Filename; |
| 41 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 42 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 43 | |
| 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 Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 77 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 80 | unsigned 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); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 86 | NextId = OS.EmitCVFileDirective(NextId, FullPath); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 87 | assert(NextId == FileIdMap.size() && ".cv_file directive failed"); |
| 88 | } |
| 89 | return Insertion.first->second; |
| 90 | } |
| 91 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 92 | CodeViewDebug::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 Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 99 | InlinedSubprograms.insert(Loc->getScope()->getSubprogram()); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 100 | } |
| 101 | return Insertion.first->second; |
| 102 | } |
| 103 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 104 | void CodeViewDebug::maybeRecordLocation(DebugLoc DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 105 | 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 Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 111 | if (!Scope) |
| 112 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 113 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 114 | // Skip this line if it is longer than the maximum we can record. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 115 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 116 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 117 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 118 | return; |
| 119 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 120 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 121 | if (CI.getStartColumn() != DL.getCol()) |
| 122 | return; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame] | 123 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 124 | 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 Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 132 | |
| 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 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 153 | OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(), |
| 154 | /*PrologueEnd=*/false, |
| 155 | /*IsStmt=*/false, DL->getFilename()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 158 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 159 | : Asm(AP), OS(*Asm->OutStreamer), CurFn(nullptr) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 160 | 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") || |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 165 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) { |
| 166 | Asm = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 167 | return; |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 168 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 169 | |
| 170 | // Tell MMI that we have debug info. |
| 171 | MMI->setDebugInfoAvailability(true); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 174 | void CodeViewDebug::endModule() { |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 175 | if (FnDebugInfo.empty()) |
| 176 | return; |
| 177 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 178 | emitTypeInformation(); |
| 179 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 180 | // FIXME: For functions that are comdat, we should emit separate .debug$S |
| 181 | // sections that are comdat associative with the main function instead of |
| 182 | // having one big .debug$S section. |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 183 | assert(Asm != nullptr); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 184 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 185 | OS.AddComment("Debug section magic"); |
| 186 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 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 Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 193 | // Make a subsection for all the inlined subprograms. |
| 194 | emitInlineeLinesSubsection(); |
| 195 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 196 | // Emit per-function debug information. |
| 197 | for (auto &P : FnDebugInfo) |
| 198 | emitDebugInfoForFunction(P.first, P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 199 | |
| 200 | // This subsection holds a file index to offset in string table table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 201 | OS.AddComment("File index to string table offset subsection"); |
| 202 | OS.EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 203 | |
| 204 | // This subsection holds the string table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 205 | OS.AddComment("String table"); |
| 206 | OS.EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 207 | |
| 208 | clear(); |
| 209 | } |
| 210 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 211 | void CodeViewDebug::emitTypeInformation() { |
| 212 | // Start the .debug$T section with 0x4. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 213 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
| 214 | OS.AddComment("Debug section magic"); |
| 215 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 216 | |
| 217 | NamedMDNode *CU_Nodes = |
| 218 | Asm->MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 219 | if (!CU_Nodes) |
| 220 | return; |
| 221 | |
| 222 | // This type info currently only holds function ids for use with inline call |
| 223 | // frame info. All functions are assigned a simple 'void ()' type. Emit that |
| 224 | // type here. |
| 225 | TypeIndex ArgListIdx = getNextTypeIndex(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 226 | OS.AddComment("Type record length"); |
| 227 | OS.EmitIntValue(2 + sizeof(ArgList), 2); |
| 228 | OS.AddComment("Leaf type: LF_ARGLIST"); |
| 229 | OS.EmitIntValue(LF_ARGLIST, 2); |
| 230 | OS.AddComment("Number of arguments"); |
| 231 | OS.EmitIntValue(0, 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 232 | |
| 233 | TypeIndex VoidProcIdx = getNextTypeIndex(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 234 | OS.AddComment("Type record length"); |
| 235 | OS.EmitIntValue(2 + sizeof(ProcedureType), 2); |
| 236 | OS.AddComment("Leaf type: LF_PROCEDURE"); |
| 237 | OS.EmitIntValue(LF_PROCEDURE, 2); |
| 238 | OS.AddComment("Return type index"); |
| 239 | OS.EmitIntValue(TypeIndex::Void().getIndex(), 4); |
| 240 | OS.AddComment("Calling convention"); |
| 241 | OS.EmitIntValue(char(CallingConvention::NearC), 1); |
| 242 | OS.AddComment("Function options"); |
| 243 | OS.EmitIntValue(char(FunctionOptions::None), 1); |
| 244 | OS.AddComment("# of parameters"); |
| 245 | OS.EmitIntValue(0, 2); |
| 246 | OS.AddComment("Argument list type index"); |
| 247 | OS.EmitIntValue(ArgListIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 248 | |
| 249 | for (MDNode *N : CU_Nodes->operands()) { |
| 250 | auto *CUNode = cast<DICompileUnit>(N); |
| 251 | for (auto *SP : CUNode->getSubprograms()) { |
| 252 | StringRef DisplayName = SP->getDisplayName(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 253 | OS.AddComment("Type record length"); |
| 254 | OS.EmitIntValue(2 + sizeof(FuncId) + DisplayName.size() + 1, 2); |
| 255 | OS.AddComment("Leaf type: LF_FUNC_ID"); |
| 256 | OS.EmitIntValue(LF_FUNC_ID, 2); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 257 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 258 | OS.AddComment("Scope type index"); |
| 259 | OS.EmitIntValue(TypeIndex().getIndex(), 4); |
| 260 | OS.AddComment("Function type"); |
| 261 | OS.EmitIntValue(VoidProcIdx.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 262 | { |
| 263 | SmallString<32> NullTerminatedString(DisplayName); |
| 264 | if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0') |
| 265 | NullTerminatedString.push_back('\0'); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 266 | OS.AddComment("Function name"); |
| 267 | OS.EmitBytes(NullTerminatedString); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 268 | } |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 269 | |
| 270 | TypeIndex FuncIdIdx = getNextTypeIndex(); |
| 271 | SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx)); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 276 | void CodeViewDebug::emitInlineeLinesSubsection() { |
| 277 | if (InlinedSubprograms.empty()) |
| 278 | return; |
| 279 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 280 | MCSymbol *InlineBegin = Asm->MMI->getContext().createTempSymbol(), |
| 281 | *InlineEnd = Asm->MMI->getContext().createTempSymbol(); |
| 282 | |
| 283 | OS.AddComment("Inlinee lines subsection"); |
| 284 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 285 | OS.AddComment("Subsection size"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 286 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4); |
| 287 | OS.EmitLabel(InlineBegin); |
| 288 | |
| 289 | // We don't provide any extra file info. |
| 290 | // FIXME: Find out if debuggers use this info. |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 291 | OS.AddComment("Inlinee lines signature"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 292 | OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4); |
| 293 | |
| 294 | for (const DISubprogram *SP : InlinedSubprograms) { |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 295 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 296 | TypeIndex TypeId = SubprogramToFuncId[SP]; |
| 297 | unsigned FileId = maybeRecordFile(SP->getFile()); |
| 298 | OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " + |
| 299 | SP->getFilename() + Twine(':') + Twine(SP->getLine())); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 300 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 301 | // The filechecksum table uses 8 byte entries for now, and file ids start at |
| 302 | // 1. |
| 303 | unsigned FileOffset = (FileId - 1) * 8; |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 304 | OS.AddComment("Type index of inlined function"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 305 | OS.EmitIntValue(TypeId.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 306 | OS.AddComment("Offset into filechecksum table"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 307 | OS.EmitIntValue(FileOffset, 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 308 | OS.AddComment("Starting line number"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 309 | OS.EmitIntValue(SP->getLine(), 4); |
| 310 | } |
| 311 | |
| 312 | OS.EmitLabel(InlineEnd); |
| 313 | } |
| 314 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 315 | void CodeViewDebug::collectInlineSiteChildren( |
| 316 | SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI, |
| 317 | const InlineSite &Site) { |
| 318 | for (const DILocation *ChildSiteLoc : Site.ChildSites) { |
| 319 | auto I = FI.InlineSites.find(ChildSiteLoc); |
| 320 | assert(I != FI.InlineSites.end()); |
| 321 | const InlineSite &ChildSite = I->second; |
| 322 | Children.push_back(ChildSite.SiteFuncId); |
| 323 | collectInlineSiteChildren(Children, FI, ChildSite); |
| 324 | } |
| 325 | } |
| 326 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 327 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
| 328 | const DILocation *InlinedAt, |
| 329 | const InlineSite &Site) { |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 330 | MCSymbol *InlineBegin = Asm->MMI->getContext().createTempSymbol(), |
| 331 | *InlineEnd = Asm->MMI->getContext().createTempSymbol(); |
| 332 | |
| 333 | assert(SubprogramToFuncId.count(Site.Inlinee)); |
| 334 | TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee]; |
| 335 | |
| 336 | // SymbolRecord |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 337 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 338 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 339 | OS.EmitLabel(InlineBegin); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 340 | OS.AddComment("Record kind: S_INLINESITE"); |
| 341 | OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 342 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 343 | OS.AddComment("PtrParent"); |
| 344 | OS.EmitIntValue(0, 4); |
| 345 | OS.AddComment("PtrEnd"); |
| 346 | OS.EmitIntValue(0, 4); |
| 347 | OS.AddComment("Inlinee type index"); |
| 348 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 349 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 350 | unsigned FileId = maybeRecordFile(Site.Inlinee->getFile()); |
| 351 | unsigned StartLineNum = Site.Inlinee->getLine(); |
| 352 | SmallVector<unsigned, 3> SecondaryFuncIds; |
| 353 | collectInlineSiteChildren(SecondaryFuncIds, FI, Site); |
| 354 | |
| 355 | OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum, |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 356 | FI.Begin, FI.End, SecondaryFuncIds); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 357 | |
| 358 | OS.EmitLabel(InlineEnd); |
| 359 | |
| 360 | // Recurse on child inlined call sites before closing the scope. |
| 361 | for (const DILocation *ChildSite : Site.ChildSites) { |
| 362 | auto I = FI.InlineSites.find(ChildSite); |
| 363 | assert(I != FI.InlineSites.end() && |
| 364 | "child site not in function inline site map"); |
| 365 | emitInlinedCallSite(FI, ChildSite, I->second); |
| 366 | } |
| 367 | |
| 368 | // Close the scope. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 369 | OS.AddComment("Record length"); |
| 370 | OS.EmitIntValue(2, 2); // RecordLength |
| 371 | OS.AddComment("Record kind: S_INLINESITE_END"); |
| 372 | OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 375 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 376 | FunctionInfo &FI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 377 | // For each function there is a separate subsection |
| 378 | // which holds the PC to file:line table. |
| 379 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 380 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 381 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 382 | StringRef FuncName; |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 383 | if (auto *SP = getDISubprogram(GV)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 384 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 385 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 386 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 387 | if (FuncName.empty()) |
| 388 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 389 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 390 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 391 | MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(), |
| 392 | *SymbolsEnd = Asm->MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 393 | OS.AddComment("Symbol subsection for " + Twine(FuncName)); |
| 394 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4); |
| 395 | OS.AddComment("Subsection size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 396 | OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 397 | OS.EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 398 | { |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 399 | MCSymbol *ProcRecordBegin = Asm->MMI->getContext().createTempSymbol(), |
| 400 | *ProcRecordEnd = Asm->MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 401 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 402 | OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 403 | OS.EmitLabel(ProcRecordBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 404 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 405 | OS.AddComment("Record kind: S_GPROC32_ID"); |
| 406 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 407 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 408 | // These fields are filled in by tools like CVPACK which run after the fact. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 409 | OS.AddComment("PtrParent"); |
| 410 | OS.EmitIntValue(0, 4); |
| 411 | OS.AddComment("PtrEnd"); |
| 412 | OS.EmitIntValue(0, 4); |
| 413 | OS.AddComment("PtrNext"); |
| 414 | OS.EmitIntValue(0, 4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 415 | // This is the important bit that tells the debugger where the function |
| 416 | // code is located and what's its size: |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 417 | OS.AddComment("Code size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 418 | OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 419 | OS.AddComment("Offset after prologue"); |
| 420 | OS.EmitIntValue(0, 4); |
| 421 | OS.AddComment("Offset before epilogue"); |
| 422 | OS.EmitIntValue(0, 4); |
| 423 | OS.AddComment("Function type index"); |
| 424 | OS.EmitIntValue(0, 4); |
| 425 | OS.AddComment("Function section relative address"); |
| 426 | OS.EmitCOFFSecRel32(Fn); |
| 427 | OS.AddComment("Function section index"); |
| 428 | OS.EmitCOFFSectionIndex(Fn); |
| 429 | OS.AddComment("Flags"); |
| 430 | OS.EmitIntValue(0, 1); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 431 | // Emit the function display name as a null-terminated string. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 432 | OS.AddComment("Function name"); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 433 | { |
| 434 | SmallString<32> NullTerminatedString(FuncName); |
| 435 | if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0') |
| 436 | NullTerminatedString.push_back('\0'); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 437 | OS.EmitBytes(NullTerminatedString); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 438 | } |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 439 | OS.EmitLabel(ProcRecordEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 440 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 441 | // Emit inlined call site information. Only emit functions inlined directly |
| 442 | // into the parent function. We'll emit the other sites recursively as part |
| 443 | // of their parent inline site. |
| 444 | for (auto &KV : FI.InlineSites) { |
| 445 | const DILocation *InlinedAt = KV.first; |
| 446 | if (!InlinedAt->getInlinedAt()) |
| 447 | emitInlinedCallSite(FI, InlinedAt, KV.second); |
| 448 | } |
| 449 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 450 | // We're done with this function. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 451 | OS.AddComment("Record length"); |
| 452 | OS.EmitIntValue(0x0002, 2); |
| 453 | OS.AddComment("Record kind: S_PROC_ID_END"); |
| 454 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 455 | } |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 456 | OS.EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 457 | // Every subsection must be aligned to a 4-byte boundary. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 458 | OS.EmitValueToAlignment(4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 459 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 460 | // We have an assembler directive that takes care of the whole line table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 461 | OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 464 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 465 | assert(!CurFn && "Can't process two functions at once!"); |
| 466 | |
| 467 | if (!Asm || !Asm->MMI->hasDebugInfo()) |
| 468 | return; |
| 469 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 470 | const Function *GV = MF->getFunction(); |
| 471 | assert(FnDebugInfo.count(GV) == false); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 472 | CurFn = &FnDebugInfo[GV]; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 473 | CurFn->FuncId = NextFuncId++; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 474 | CurFn->Begin = Asm->getFunctionBegin(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 475 | |
| 476 | // Find the end of the function prolog. |
| 477 | // FIXME: is there a simpler a way to do this? Can we just search |
| 478 | // for the first instruction of the function, not the last of the prolog? |
| 479 | DebugLoc PrologEndLoc; |
| 480 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 481 | for (const auto &MBB : *MF) { |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 482 | if (PrologEndLoc) |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 483 | break; |
| 484 | for (const auto &MI : MBB) { |
| 485 | if (MI.isDebugValue()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 486 | continue; |
| 487 | |
| 488 | // First known non-DBG_VALUE and non-frame setup location marks |
| 489 | // the beginning of the function body. |
| 490 | // FIXME: do we need the first subcondition? |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 491 | if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 492 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 493 | break; |
| 494 | } |
| 495 | EmptyPrologue = false; |
| 496 | } |
| 497 | } |
| 498 | // Record beginning of function if we have a non-empty prologue. |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 499 | if (PrologEndLoc && !EmptyPrologue) { |
| 500 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 501 | maybeRecordLocation(FnStartDL, MF); |
| 502 | } |
| 503 | } |
| 504 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 505 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 506 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 507 | return; |
| 508 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 509 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 510 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 511 | assert(CurFn == &FnDebugInfo[GV]); |
| 512 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 513 | // Don't emit anything if we don't have any line tables. |
| 514 | if (!CurFn->HaveLineInfo) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 515 | FnDebugInfo.erase(GV); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 516 | } else { |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 517 | CurFn->End = Asm->getFunctionEnd(); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 518 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 519 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 522 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 523 | // Ignore DBG_VALUE locations and function prologue. |
| 524 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 525 | return; |
| 526 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 527 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 528 | return; |
| 529 | maybeRecordLocation(DL, Asm->MF); |
| 530 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 531 | } |