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" |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 24 | #include "llvm/Target/TargetRegisterInfo.h" |
| 25 | #include "llvm/Target/TargetFrameLowering.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 26 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 28 | using namespace llvm::codeview; |
| 29 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 30 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
| 31 | : DebugHandlerBase(AP), OS(*Asm->OutStreamer), CurFn(nullptr) { |
| 32 | // If module doesn't have named metadata anchors or COFF debug section |
| 33 | // is not available, skip any debug info related stuff. |
| 34 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 35 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) { |
| 36 | Asm = nullptr; |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | // Tell MMI that we have debug info. |
| 41 | MMI->setDebugInfoAvailability(true); |
| 42 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 43 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 44 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 45 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 46 | if (!Filepath.empty()) |
| 47 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 48 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 49 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 50 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 51 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 52 | // operates on full paths. We could change Clang to emit full paths too, but |
| 53 | // that would increase the IR size and probably not needed for other users. |
| 54 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 55 | if (Filename.find(':') == 1) |
| 56 | Filepath = Filename; |
| 57 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 58 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 59 | |
| 60 | // Canonicalize the path. We have to do it textually because we may no longer |
| 61 | // have access the file in the filesystem. |
| 62 | // First, replace all slashes with backslashes. |
| 63 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 64 | |
| 65 | // Remove all "\.\" with "\". |
| 66 | size_t Cursor = 0; |
| 67 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 68 | Filepath.erase(Cursor, 2); |
| 69 | |
| 70 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 71 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 72 | Cursor = 0; |
| 73 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 74 | // Something's wrong if the path starts with "\..\", abort. |
| 75 | if (Cursor == 0) |
| 76 | break; |
| 77 | |
| 78 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 79 | if (PrevSlash == std::string::npos) |
| 80 | // Something's wrong, abort. |
| 81 | break; |
| 82 | |
| 83 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 84 | // The next ".." might be following the one we've just erased. |
| 85 | Cursor = PrevSlash; |
| 86 | } |
| 87 | |
| 88 | // Remove all duplicate backslashes. |
| 89 | Cursor = 0; |
| 90 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 91 | Filepath.erase(Cursor, 1); |
| 92 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 93 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 96 | unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { |
| 97 | unsigned NextId = FileIdMap.size() + 1; |
| 98 | auto Insertion = FileIdMap.insert(std::make_pair(F, NextId)); |
| 99 | if (Insertion.second) { |
| 100 | // We have to compute the full filepath and emit a .cv_file directive. |
| 101 | StringRef FullPath = getFullFilepath(F); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 102 | NextId = OS.EmitCVFileDirective(NextId, FullPath); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 103 | assert(NextId == FileIdMap.size() && ".cv_file directive failed"); |
| 104 | } |
| 105 | return Insertion.first->second; |
| 106 | } |
| 107 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 108 | CodeViewDebug::InlineSite &CodeViewDebug::getInlineSite(const DILocation *Loc) { |
| 109 | const DILocation *InlinedAt = Loc->getInlinedAt(); |
| 110 | auto Insertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()}); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 111 | InlineSite *Site = &Insertion.first->second; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 112 | if (Insertion.second) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 113 | Site->SiteFuncId = NextFuncId++; |
| 114 | Site->Inlinee = Loc->getScope()->getSubprogram(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 115 | InlinedSubprograms.insert(Loc->getScope()->getSubprogram()); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 116 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 117 | return *Site; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 120 | static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs, |
| 121 | const DILocation *Loc) { |
| 122 | auto B = Locs.begin(), E = Locs.end(); |
| 123 | if (std::find(B, E, Loc) == E) |
| 124 | Locs.push_back(Loc); |
| 125 | } |
| 126 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 127 | void CodeViewDebug::maybeRecordLocation(DebugLoc DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 128 | const MachineFunction *MF) { |
| 129 | // Skip this instruction if it has the same location as the previous one. |
| 130 | if (DL == CurFn->LastLoc) |
| 131 | return; |
| 132 | |
| 133 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 134 | if (!Scope) |
| 135 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 136 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 137 | // 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] | 138 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 139 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 140 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 141 | return; |
| 142 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 143 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 144 | if (CI.getStartColumn() != DL.getCol()) |
| 145 | return; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame] | 146 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 147 | if (!CurFn->HaveLineInfo) |
| 148 | CurFn->HaveLineInfo = true; |
| 149 | unsigned FileId = 0; |
| 150 | if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile()) |
| 151 | FileId = CurFn->LastFileId; |
| 152 | else |
| 153 | FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); |
| 154 | CurFn->LastLoc = DL; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 155 | |
| 156 | unsigned FuncId = CurFn->FuncId; |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 157 | if (DL->getInlinedAt()) { |
| 158 | const DILocation *Loc = DL.get(); |
| 159 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 160 | // If this location was actually inlined from somewhere else, give it the ID |
| 161 | // of the inline call site. |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 162 | FuncId = getInlineSite(Loc).SiteFuncId; |
| 163 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 164 | // Ensure we have links in the tree of inline call sites. |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 165 | const DILocation *SiteLoc; |
| 166 | bool FirstLoc = true; |
| 167 | while ((SiteLoc = Loc->getInlinedAt())) { |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 168 | InlineSite &Site = getInlineSite(Loc); |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 169 | if (!FirstLoc) |
| 170 | addLocIfNotPresent(Site.ChildSites, Loc); |
| 171 | FirstLoc = false; |
| 172 | Loc = SiteLoc; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 173 | } |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 174 | addLocIfNotPresent(CurFn->ChildSites, Loc); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 177 | OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(), |
| 178 | /*PrologueEnd=*/false, |
| 179 | /*IsStmt=*/false, DL->getFilename()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 182 | void CodeViewDebug::endModule() { |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 183 | if (FnDebugInfo.empty()) |
| 184 | return; |
| 185 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 186 | emitTypeInformation(); |
| 187 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 188 | // FIXME: For functions that are comdat, we should emit separate .debug$S |
| 189 | // sections that are comdat associative with the main function instead of |
| 190 | // having one big .debug$S section. |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 191 | assert(Asm != nullptr); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 192 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 193 | OS.AddComment("Debug section magic"); |
| 194 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 195 | |
| 196 | // The COFF .debug$S section consists of several subsections, each starting |
| 197 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 198 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 199 | // aligned. |
| 200 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 201 | // Make a subsection for all the inlined subprograms. |
| 202 | emitInlineeLinesSubsection(); |
| 203 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 204 | // Emit per-function debug information. |
| 205 | for (auto &P : FnDebugInfo) |
| 206 | emitDebugInfoForFunction(P.first, P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 207 | |
| 208 | // This subsection holds a file index to offset in string table table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 209 | OS.AddComment("File index to string table offset subsection"); |
| 210 | OS.EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 211 | |
| 212 | // This subsection holds the string table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 213 | OS.AddComment("String table"); |
| 214 | OS.EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 215 | |
| 216 | clear(); |
| 217 | } |
| 218 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 219 | void CodeViewDebug::emitTypeInformation() { |
| 220 | // Start the .debug$T section with 0x4. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 221 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
| 222 | OS.AddComment("Debug section magic"); |
| 223 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 224 | |
| 225 | NamedMDNode *CU_Nodes = |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 226 | MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 227 | if (!CU_Nodes) |
| 228 | return; |
| 229 | |
| 230 | // This type info currently only holds function ids for use with inline call |
| 231 | // frame info. All functions are assigned a simple 'void ()' type. Emit that |
| 232 | // type here. |
| 233 | TypeIndex ArgListIdx = getNextTypeIndex(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 234 | OS.AddComment("Type record length"); |
| 235 | OS.EmitIntValue(2 + sizeof(ArgList), 2); |
| 236 | OS.AddComment("Leaf type: LF_ARGLIST"); |
| 237 | OS.EmitIntValue(LF_ARGLIST, 2); |
| 238 | OS.AddComment("Number of arguments"); |
| 239 | OS.EmitIntValue(0, 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 240 | |
| 241 | TypeIndex VoidProcIdx = getNextTypeIndex(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 242 | OS.AddComment("Type record length"); |
| 243 | OS.EmitIntValue(2 + sizeof(ProcedureType), 2); |
| 244 | OS.AddComment("Leaf type: LF_PROCEDURE"); |
| 245 | OS.EmitIntValue(LF_PROCEDURE, 2); |
| 246 | OS.AddComment("Return type index"); |
| 247 | OS.EmitIntValue(TypeIndex::Void().getIndex(), 4); |
| 248 | OS.AddComment("Calling convention"); |
| 249 | OS.EmitIntValue(char(CallingConvention::NearC), 1); |
| 250 | OS.AddComment("Function options"); |
| 251 | OS.EmitIntValue(char(FunctionOptions::None), 1); |
| 252 | OS.AddComment("# of parameters"); |
| 253 | OS.EmitIntValue(0, 2); |
| 254 | OS.AddComment("Argument list type index"); |
| 255 | OS.EmitIntValue(ArgListIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 256 | |
| 257 | for (MDNode *N : CU_Nodes->operands()) { |
| 258 | auto *CUNode = cast<DICompileUnit>(N); |
| 259 | for (auto *SP : CUNode->getSubprograms()) { |
| 260 | StringRef DisplayName = SP->getDisplayName(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 261 | OS.AddComment("Type record length"); |
| 262 | OS.EmitIntValue(2 + sizeof(FuncId) + DisplayName.size() + 1, 2); |
| 263 | OS.AddComment("Leaf type: LF_FUNC_ID"); |
| 264 | OS.EmitIntValue(LF_FUNC_ID, 2); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 265 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 266 | OS.AddComment("Scope type index"); |
| 267 | OS.EmitIntValue(TypeIndex().getIndex(), 4); |
| 268 | OS.AddComment("Function type"); |
| 269 | OS.EmitIntValue(VoidProcIdx.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 270 | { |
| 271 | SmallString<32> NullTerminatedString(DisplayName); |
| 272 | if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0') |
| 273 | NullTerminatedString.push_back('\0'); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 274 | OS.AddComment("Function name"); |
| 275 | OS.EmitBytes(NullTerminatedString); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 276 | } |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 277 | |
| 278 | TypeIndex FuncIdIdx = getNextTypeIndex(); |
| 279 | SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx)); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 284 | void CodeViewDebug::emitInlineeLinesSubsection() { |
| 285 | if (InlinedSubprograms.empty()) |
| 286 | return; |
| 287 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 288 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 289 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 290 | |
| 291 | OS.AddComment("Inlinee lines subsection"); |
| 292 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 293 | OS.AddComment("Subsection size"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 294 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4); |
| 295 | OS.EmitLabel(InlineBegin); |
| 296 | |
| 297 | // We don't provide any extra file info. |
| 298 | // FIXME: Find out if debuggers use this info. |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 299 | OS.AddComment("Inlinee lines signature"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 300 | OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4); |
| 301 | |
| 302 | for (const DISubprogram *SP : InlinedSubprograms) { |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 303 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 304 | TypeIndex TypeId = SubprogramToFuncId[SP]; |
| 305 | unsigned FileId = maybeRecordFile(SP->getFile()); |
| 306 | OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " + |
| 307 | SP->getFilename() + Twine(':') + Twine(SP->getLine())); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 308 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 309 | // The filechecksum table uses 8 byte entries for now, and file ids start at |
| 310 | // 1. |
| 311 | unsigned FileOffset = (FileId - 1) * 8; |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 312 | OS.AddComment("Type index of inlined function"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 313 | OS.EmitIntValue(TypeId.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 314 | OS.AddComment("Offset into filechecksum table"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 315 | OS.EmitIntValue(FileOffset, 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 316 | OS.AddComment("Starting line number"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 317 | OS.EmitIntValue(SP->getLine(), 4); |
| 318 | } |
| 319 | |
| 320 | OS.EmitLabel(InlineEnd); |
| 321 | } |
| 322 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 323 | void CodeViewDebug::collectInlineSiteChildren( |
| 324 | SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI, |
| 325 | const InlineSite &Site) { |
| 326 | for (const DILocation *ChildSiteLoc : Site.ChildSites) { |
| 327 | auto I = FI.InlineSites.find(ChildSiteLoc); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 328 | const InlineSite &ChildSite = I->second; |
| 329 | Children.push_back(ChildSite.SiteFuncId); |
| 330 | collectInlineSiteChildren(Children, FI, ChildSite); |
| 331 | } |
| 332 | } |
| 333 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 334 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
| 335 | const DILocation *InlinedAt, |
| 336 | const InlineSite &Site) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 337 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 338 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 339 | |
| 340 | assert(SubprogramToFuncId.count(Site.Inlinee)); |
| 341 | TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee]; |
| 342 | |
| 343 | // SymbolRecord |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 344 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 345 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 346 | OS.EmitLabel(InlineBegin); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 347 | OS.AddComment("Record kind: S_INLINESITE"); |
| 348 | OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 349 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 350 | OS.AddComment("PtrParent"); |
| 351 | OS.EmitIntValue(0, 4); |
| 352 | OS.AddComment("PtrEnd"); |
| 353 | OS.EmitIntValue(0, 4); |
| 354 | OS.AddComment("Inlinee type index"); |
| 355 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 356 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 357 | unsigned FileId = maybeRecordFile(Site.Inlinee->getFile()); |
| 358 | unsigned StartLineNum = Site.Inlinee->getLine(); |
| 359 | SmallVector<unsigned, 3> SecondaryFuncIds; |
| 360 | collectInlineSiteChildren(SecondaryFuncIds, FI, Site); |
| 361 | |
| 362 | OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum, |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 363 | FI.Begin, FI.End, SecondaryFuncIds); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 364 | |
| 365 | OS.EmitLabel(InlineEnd); |
| 366 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 367 | for (const LocalVariable &Var : Site.InlinedLocals) |
| 368 | emitLocalVariable(Var); |
| 369 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 370 | // Recurse on child inlined call sites before closing the scope. |
| 371 | for (const DILocation *ChildSite : Site.ChildSites) { |
| 372 | auto I = FI.InlineSites.find(ChildSite); |
| 373 | assert(I != FI.InlineSites.end() && |
| 374 | "child site not in function inline site map"); |
| 375 | emitInlinedCallSite(FI, ChildSite, I->second); |
| 376 | } |
| 377 | |
| 378 | // Close the scope. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 379 | OS.AddComment("Record length"); |
| 380 | OS.EmitIntValue(2, 2); // RecordLength |
| 381 | OS.AddComment("Record kind: S_INLINESITE_END"); |
| 382 | OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 385 | static void emitNullTerminatedString(MCStreamer &OS, StringRef S) { |
| 386 | SmallString<32> NullTerminatedString(S); |
| 387 | if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0') |
| 388 | NullTerminatedString.push_back('\0'); |
| 389 | OS.EmitBytes(NullTerminatedString); |
| 390 | } |
| 391 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 392 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 393 | FunctionInfo &FI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 394 | // For each function there is a separate subsection |
| 395 | // which holds the PC to file:line table. |
| 396 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 397 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 398 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 399 | StringRef FuncName; |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 400 | if (auto *SP = getDISubprogram(GV)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 401 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 402 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 403 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 404 | if (FuncName.empty()) |
| 405 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 406 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 407 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 408 | MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(), |
| 409 | *SymbolsEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 410 | OS.AddComment("Symbol subsection for " + Twine(FuncName)); |
| 411 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4); |
| 412 | OS.AddComment("Subsection size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 413 | OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 414 | OS.EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 415 | { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 416 | MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(), |
| 417 | *ProcRecordEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 418 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 419 | OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 420 | OS.EmitLabel(ProcRecordBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 421 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 422 | OS.AddComment("Record kind: S_GPROC32_ID"); |
| 423 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 424 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 425 | // 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] | 426 | OS.AddComment("PtrParent"); |
| 427 | OS.EmitIntValue(0, 4); |
| 428 | OS.AddComment("PtrEnd"); |
| 429 | OS.EmitIntValue(0, 4); |
| 430 | OS.AddComment("PtrNext"); |
| 431 | OS.EmitIntValue(0, 4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 432 | // This is the important bit that tells the debugger where the function |
| 433 | // code is located and what's its size: |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 434 | OS.AddComment("Code size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 435 | OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 436 | OS.AddComment("Offset after prologue"); |
| 437 | OS.EmitIntValue(0, 4); |
| 438 | OS.AddComment("Offset before epilogue"); |
| 439 | OS.EmitIntValue(0, 4); |
| 440 | OS.AddComment("Function type index"); |
| 441 | OS.EmitIntValue(0, 4); |
| 442 | OS.AddComment("Function section relative address"); |
| 443 | OS.EmitCOFFSecRel32(Fn); |
| 444 | OS.AddComment("Function section index"); |
| 445 | OS.EmitCOFFSectionIndex(Fn); |
| 446 | OS.AddComment("Flags"); |
| 447 | OS.EmitIntValue(0, 1); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 448 | // Emit the function display name as a null-terminated string. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 449 | OS.AddComment("Function name"); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 450 | emitNullTerminatedString(OS, FuncName); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 451 | OS.EmitLabel(ProcRecordEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 452 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 453 | for (const LocalVariable &Var : FI.Locals) |
| 454 | emitLocalVariable(Var); |
| 455 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 456 | // Emit inlined call site information. Only emit functions inlined directly |
| 457 | // into the parent function. We'll emit the other sites recursively as part |
| 458 | // of their parent inline site. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 459 | for (const DILocation *InlinedAt : FI.ChildSites) { |
| 460 | auto I = FI.InlineSites.find(InlinedAt); |
| 461 | assert(I != FI.InlineSites.end() && |
| 462 | "child site not in function inline site map"); |
| 463 | emitInlinedCallSite(FI, InlinedAt, I->second); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 466 | // We're done with this function. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 467 | OS.AddComment("Record length"); |
| 468 | OS.EmitIntValue(0x0002, 2); |
| 469 | OS.AddComment("Record kind: S_PROC_ID_END"); |
| 470 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 471 | } |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 472 | OS.EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 473 | // Every subsection must be aligned to a 4-byte boundary. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 474 | OS.EmitValueToAlignment(4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 475 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 476 | // 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] | 477 | OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 480 | void CodeViewDebug::collectVariableInfoFromMMITable() { |
| 481 | for (const auto &VI : MMI->getVariableDbgInfo()) { |
| 482 | if (!VI.Var) |
| 483 | continue; |
| 484 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 485 | "Expected inlined-at fields to agree"); |
| 486 | |
| 487 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
| 488 | |
| 489 | // If variable scope is not found then skip this variable. |
| 490 | if (!Scope) |
| 491 | continue; |
| 492 | |
| 493 | LocalVariable Var; |
| 494 | Var.DIVar = VI.Var; |
| 495 | |
| 496 | // Get the frame register used and the offset. |
| 497 | unsigned FrameReg = 0; |
| 498 | const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget(); |
| 499 | const TargetFrameLowering *TFI = TSI.getFrameLowering(); |
| 500 | const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); |
| 501 | Var.RegisterOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg); |
| 502 | Var.CVRegister = TRI->getCodeViewRegNum(FrameReg); |
| 503 | |
| 504 | // Calculate the label ranges. |
| 505 | for (const InsnRange &Range : Scope->getRanges()) { |
| 506 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 507 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
| 508 | Var.Ranges.push_back({Begin, End}); |
| 509 | } |
| 510 | |
| 511 | if (VI.Loc->getInlinedAt()) { |
| 512 | // This variable was inlined. Associate it with the InlineSite. |
| 513 | InlineSite &Site = getInlineSite(VI.Loc); |
| 514 | Site.InlinedLocals.emplace_back(std::move(Var)); |
| 515 | } else { |
| 516 | // This variable goes in the main ProcSym. |
| 517 | CurFn->Locals.emplace_back(std::move(Var)); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 522 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 523 | assert(!CurFn && "Can't process two functions at once!"); |
| 524 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 525 | if (!Asm || !MMI->hasDebugInfo()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 526 | return; |
| 527 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 528 | DebugHandlerBase::beginFunction(MF); |
| 529 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 530 | const Function *GV = MF->getFunction(); |
| 531 | assert(FnDebugInfo.count(GV) == false); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 532 | CurFn = &FnDebugInfo[GV]; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 533 | CurFn->FuncId = NextFuncId++; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 534 | CurFn->Begin = Asm->getFunctionBegin(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 535 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 536 | // Find the end of the function prolog. First known non-DBG_VALUE and |
| 537 | // non-frame setup location marks the beginning of the function body. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 538 | // FIXME: is there a simpler a way to do this? Can we just search |
| 539 | // for the first instruction of the function, not the last of the prolog? |
| 540 | DebugLoc PrologEndLoc; |
| 541 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 542 | for (const auto &MBB : *MF) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 543 | for (const auto &MI : MBB) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 544 | if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) && |
| 545 | MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 546 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 547 | break; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 548 | } else if (!MI.isDebugValue()) { |
| 549 | EmptyPrologue = false; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 550 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 553 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 554 | // 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] | 555 | if (PrologEndLoc && !EmptyPrologue) { |
| 556 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 557 | maybeRecordLocation(FnStartDL, MF); |
| 558 | } |
| 559 | } |
| 560 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 561 | void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) { |
| 562 | // LocalSym record, see SymbolRecord.h for more info. |
| 563 | MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(), |
| 564 | *LocalEnd = MMI->getContext().createTempSymbol(); |
| 565 | OS.AddComment("Record length"); |
| 566 | OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2); |
| 567 | OS.EmitLabel(LocalBegin); |
| 568 | |
| 569 | OS.AddComment("Record kind: S_LOCAL"); |
| 570 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2); |
| 571 | |
| 572 | uint16_t Flags = 0; |
| 573 | if (Var.DIVar->isParameter()) |
| 574 | Flags |= LocalSym::IsParameter; |
| 575 | |
| 576 | OS.AddComment("TypeIndex"); |
| 577 | OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4); |
| 578 | OS.AddComment("Flags"); |
| 579 | OS.EmitIntValue(Flags, 2); |
| 580 | emitNullTerminatedString(OS, Var.DIVar->getName()); |
| 581 | OS.EmitLabel(LocalEnd); |
| 582 | |
| 583 | // DefRangeRegisterRelSym record, see SymbolRecord.h for more info. Omit the |
| 584 | // LocalVariableAddrRange field from the record. The directive will emit that. |
| 585 | DefRangeRegisterRelSym Sym{}; |
| 586 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL); |
| 587 | Sym.BaseRegister = Var.CVRegister; |
| 588 | Sym.Flags = 0; // Unclear what matters here. |
| 589 | Sym.BasePointerOffset = Var.RegisterOffset; |
| 590 | SmallString<sizeof(Sym) + sizeof(SymKind) - sizeof(LocalVariableAddrRange)> |
| 591 | BytePrefix; |
| 592 | BytePrefix += StringRef(reinterpret_cast<const char *>(&SymKind), |
| 593 | sizeof(SymKind)); |
| 594 | BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym), |
| 595 | sizeof(Sym) - sizeof(LocalVariableAddrRange)); |
| 596 | |
| 597 | OS.EmitCVDefRangeDirective(Var.Ranges, BytePrefix); |
| 598 | } |
| 599 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 600 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 601 | collectVariableInfoFromMMITable(); |
| 602 | |
| 603 | DebugHandlerBase::endFunction(MF); |
| 604 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 605 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 606 | return; |
| 607 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 608 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 609 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 610 | assert(CurFn == &FnDebugInfo[GV]); |
| 611 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 612 | // Don't emit anything if we don't have any line tables. |
| 613 | if (!CurFn->HaveLineInfo) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 614 | FnDebugInfo.erase(GV); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 615 | CurFn = nullptr; |
| 616 | return; |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 617 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 618 | |
| 619 | CurFn->End = Asm->getFunctionEnd(); |
| 620 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 621 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 624 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 625 | DebugHandlerBase::beginInstruction(MI); |
| 626 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 627 | // Ignore DBG_VALUE locations and function prologue. |
| 628 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 629 | return; |
| 630 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 631 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 632 | return; |
| 633 | maybeRecordLocation(DL, Asm->MF); |
| 634 | } |