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 | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 108 | CodeViewDebug::InlineSite & |
| 109 | CodeViewDebug::getInlineSite(const DILocation *InlinedAt, |
| 110 | const DISubprogram *Inlinee) { |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 111 | auto Insertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()}); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 112 | InlineSite *Site = &Insertion.first->second; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 113 | if (Insertion.second) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 114 | Site->SiteFuncId = NextFuncId++; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 115 | Site->Inlinee = Inlinee; |
| 116 | InlinedSubprograms.insert(Inlinee); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 117 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 118 | return *Site; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 121 | void CodeViewDebug::recordLocalVariable(LocalVariable &&Var, |
| 122 | const DILocation *InlinedAt) { |
| 123 | if (InlinedAt) { |
| 124 | // This variable was inlined. Associate it with the InlineSite. |
| 125 | const DISubprogram *Inlinee = Var.DIVar->getScope()->getSubprogram(); |
| 126 | InlineSite &Site = getInlineSite(InlinedAt, Inlinee); |
| 127 | Site.InlinedLocals.emplace_back(Var); |
| 128 | } else { |
| 129 | // This variable goes in the main ProcSym. |
| 130 | CurFn->Locals.emplace_back(Var); |
| 131 | } |
| 132 | } |
| 133 | |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 134 | static void addLocIfNotPresent(SmallVectorImpl<const DILocation *> &Locs, |
| 135 | const DILocation *Loc) { |
| 136 | auto B = Locs.begin(), E = Locs.end(); |
| 137 | if (std::find(B, E, Loc) == E) |
| 138 | Locs.push_back(Loc); |
| 139 | } |
| 140 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 141 | void CodeViewDebug::maybeRecordLocation(DebugLoc DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 142 | const MachineFunction *MF) { |
| 143 | // Skip this instruction if it has the same location as the previous one. |
| 144 | if (DL == CurFn->LastLoc) |
| 145 | return; |
| 146 | |
| 147 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 148 | if (!Scope) |
| 149 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 150 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 151 | // 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] | 152 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 153 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 154 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 155 | return; |
| 156 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 157 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 158 | if (CI.getStartColumn() != DL.getCol()) |
| 159 | return; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame] | 160 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 161 | if (!CurFn->HaveLineInfo) |
| 162 | CurFn->HaveLineInfo = true; |
| 163 | unsigned FileId = 0; |
| 164 | if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile()) |
| 165 | FileId = CurFn->LastFileId; |
| 166 | else |
| 167 | FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); |
| 168 | CurFn->LastLoc = DL; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 169 | |
| 170 | unsigned FuncId = CurFn->FuncId; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 171 | if (const DILocation *SiteLoc = DL->getInlinedAt()) { |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 172 | const DILocation *Loc = DL.get(); |
| 173 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 174 | // If this location was actually inlined from somewhere else, give it the ID |
| 175 | // of the inline call site. |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 176 | FuncId = |
| 177 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()).SiteFuncId; |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 178 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 179 | // Ensure we have links in the tree of inline call sites. |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 180 | bool FirstLoc = true; |
| 181 | while ((SiteLoc = Loc->getInlinedAt())) { |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 182 | InlineSite &Site = |
| 183 | getInlineSite(SiteLoc, Loc->getScope()->getSubprogram()); |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 184 | if (!FirstLoc) |
| 185 | addLocIfNotPresent(Site.ChildSites, Loc); |
| 186 | FirstLoc = false; |
| 187 | Loc = SiteLoc; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 188 | } |
Reid Kleckner | 829365a | 2016-02-11 19:41:47 +0000 | [diff] [blame] | 189 | addLocIfNotPresent(CurFn->ChildSites, Loc); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 192 | OS.EmitCVLocDirective(FuncId, FileId, DL.getLine(), DL.getCol(), |
| 193 | /*PrologueEnd=*/false, |
| 194 | /*IsStmt=*/false, DL->getFilename()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 197 | void CodeViewDebug::endModule() { |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 198 | if (FnDebugInfo.empty()) |
| 199 | return; |
| 200 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 201 | emitTypeInformation(); |
| 202 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 203 | // FIXME: For functions that are comdat, we should emit separate .debug$S |
| 204 | // sections that are comdat associative with the main function instead of |
| 205 | // having one big .debug$S section. |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 206 | assert(Asm != nullptr); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 207 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 208 | OS.AddComment("Debug section magic"); |
| 209 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 210 | |
| 211 | // The COFF .debug$S section consists of several subsections, each starting |
| 212 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 213 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 214 | // aligned. |
| 215 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 216 | // Make a subsection for all the inlined subprograms. |
| 217 | emitInlineeLinesSubsection(); |
| 218 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 219 | // Emit per-function debug information. |
| 220 | for (auto &P : FnDebugInfo) |
| 221 | emitDebugInfoForFunction(P.first, P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 222 | |
| 223 | // This subsection holds a file index to offset in string table table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 224 | OS.AddComment("File index to string table offset subsection"); |
| 225 | OS.EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 226 | |
| 227 | // This subsection holds the string table. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 228 | OS.AddComment("String table"); |
| 229 | OS.EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 230 | |
| 231 | clear(); |
| 232 | } |
| 233 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 234 | void CodeViewDebug::emitTypeInformation() { |
| 235 | // Start the .debug$T section with 0x4. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 236 | OS.SwitchSection(Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
| 237 | OS.AddComment("Debug section magic"); |
| 238 | OS.EmitIntValue(COFF::DEBUG_SECTION_MAGIC, 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 239 | |
| 240 | NamedMDNode *CU_Nodes = |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 241 | MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 242 | if (!CU_Nodes) |
| 243 | return; |
| 244 | |
| 245 | // This type info currently only holds function ids for use with inline call |
| 246 | // frame info. All functions are assigned a simple 'void ()' type. Emit that |
| 247 | // type here. |
| 248 | TypeIndex ArgListIdx = getNextTypeIndex(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 249 | OS.AddComment("Type record length"); |
| 250 | OS.EmitIntValue(2 + sizeof(ArgList), 2); |
| 251 | OS.AddComment("Leaf type: LF_ARGLIST"); |
| 252 | OS.EmitIntValue(LF_ARGLIST, 2); |
| 253 | OS.AddComment("Number of arguments"); |
| 254 | OS.EmitIntValue(0, 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 255 | |
| 256 | TypeIndex VoidProcIdx = getNextTypeIndex(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 257 | OS.AddComment("Type record length"); |
| 258 | OS.EmitIntValue(2 + sizeof(ProcedureType), 2); |
| 259 | OS.AddComment("Leaf type: LF_PROCEDURE"); |
| 260 | OS.EmitIntValue(LF_PROCEDURE, 2); |
| 261 | OS.AddComment("Return type index"); |
| 262 | OS.EmitIntValue(TypeIndex::Void().getIndex(), 4); |
| 263 | OS.AddComment("Calling convention"); |
| 264 | OS.EmitIntValue(char(CallingConvention::NearC), 1); |
| 265 | OS.AddComment("Function options"); |
| 266 | OS.EmitIntValue(char(FunctionOptions::None), 1); |
| 267 | OS.AddComment("# of parameters"); |
| 268 | OS.EmitIntValue(0, 2); |
| 269 | OS.AddComment("Argument list type index"); |
| 270 | OS.EmitIntValue(ArgListIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 271 | |
| 272 | for (MDNode *N : CU_Nodes->operands()) { |
| 273 | auto *CUNode = cast<DICompileUnit>(N); |
| 274 | for (auto *SP : CUNode->getSubprograms()) { |
| 275 | StringRef DisplayName = SP->getDisplayName(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 276 | OS.AddComment("Type record length"); |
| 277 | OS.EmitIntValue(2 + sizeof(FuncId) + DisplayName.size() + 1, 2); |
| 278 | OS.AddComment("Leaf type: LF_FUNC_ID"); |
| 279 | OS.EmitIntValue(LF_FUNC_ID, 2); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 280 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 281 | OS.AddComment("Scope type index"); |
| 282 | OS.EmitIntValue(TypeIndex().getIndex(), 4); |
| 283 | OS.AddComment("Function type"); |
| 284 | OS.EmitIntValue(VoidProcIdx.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 285 | { |
| 286 | SmallString<32> NullTerminatedString(DisplayName); |
| 287 | if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0') |
| 288 | NullTerminatedString.push_back('\0'); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 289 | OS.AddComment("Function name"); |
| 290 | OS.EmitBytes(NullTerminatedString); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 291 | } |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 292 | |
| 293 | TypeIndex FuncIdIdx = getNextTypeIndex(); |
| 294 | SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx)); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 299 | void CodeViewDebug::emitInlineeLinesSubsection() { |
| 300 | if (InlinedSubprograms.empty()) |
| 301 | return; |
| 302 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 303 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 304 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 305 | |
| 306 | OS.AddComment("Inlinee lines subsection"); |
| 307 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::InlineeLines), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 308 | OS.AddComment("Subsection size"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 309 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 4); |
| 310 | OS.EmitLabel(InlineBegin); |
| 311 | |
| 312 | // We don't provide any extra file info. |
| 313 | // FIXME: Find out if debuggers use this info. |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 314 | OS.AddComment("Inlinee lines signature"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 315 | OS.EmitIntValue(unsigned(InlineeLinesSignature::Normal), 4); |
| 316 | |
| 317 | for (const DISubprogram *SP : InlinedSubprograms) { |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 318 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 319 | TypeIndex TypeId = SubprogramToFuncId[SP]; |
| 320 | unsigned FileId = maybeRecordFile(SP->getFile()); |
| 321 | OS.AddComment("Inlined function " + SP->getDisplayName() + " starts at " + |
| 322 | SP->getFilename() + Twine(':') + Twine(SP->getLine())); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 323 | OS.AddBlankLine(); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 324 | // The filechecksum table uses 8 byte entries for now, and file ids start at |
| 325 | // 1. |
| 326 | unsigned FileOffset = (FileId - 1) * 8; |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 327 | OS.AddComment("Type index of inlined function"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 328 | OS.EmitIntValue(TypeId.getIndex(), 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 329 | OS.AddComment("Offset into filechecksum table"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 330 | OS.EmitIntValue(FileOffset, 4); |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 331 | OS.AddComment("Starting line number"); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 332 | OS.EmitIntValue(SP->getLine(), 4); |
| 333 | } |
| 334 | |
| 335 | OS.EmitLabel(InlineEnd); |
| 336 | } |
| 337 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 338 | void CodeViewDebug::collectInlineSiteChildren( |
| 339 | SmallVectorImpl<unsigned> &Children, const FunctionInfo &FI, |
| 340 | const InlineSite &Site) { |
| 341 | for (const DILocation *ChildSiteLoc : Site.ChildSites) { |
| 342 | auto I = FI.InlineSites.find(ChildSiteLoc); |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 343 | const InlineSite &ChildSite = I->second; |
| 344 | Children.push_back(ChildSite.SiteFuncId); |
| 345 | collectInlineSiteChildren(Children, FI, ChildSite); |
| 346 | } |
| 347 | } |
| 348 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 349 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
| 350 | const DILocation *InlinedAt, |
| 351 | const InlineSite &Site) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 352 | MCSymbol *InlineBegin = MMI->getContext().createTempSymbol(), |
| 353 | *InlineEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 354 | |
| 355 | assert(SubprogramToFuncId.count(Site.Inlinee)); |
| 356 | TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee]; |
| 357 | |
| 358 | // SymbolRecord |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 359 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 360 | OS.emitAbsoluteSymbolDiff(InlineEnd, InlineBegin, 2); // RecordLength |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 361 | OS.EmitLabel(InlineBegin); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 362 | OS.AddComment("Record kind: S_INLINESITE"); |
| 363 | OS.EmitIntValue(SymbolRecordKind::S_INLINESITE, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 364 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 365 | OS.AddComment("PtrParent"); |
| 366 | OS.EmitIntValue(0, 4); |
| 367 | OS.AddComment("PtrEnd"); |
| 368 | OS.EmitIntValue(0, 4); |
| 369 | OS.AddComment("Inlinee type index"); |
| 370 | OS.EmitIntValue(InlineeIdx.getIndex(), 4); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 371 | |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 372 | unsigned FileId = maybeRecordFile(Site.Inlinee->getFile()); |
| 373 | unsigned StartLineNum = Site.Inlinee->getLine(); |
| 374 | SmallVector<unsigned, 3> SecondaryFuncIds; |
| 375 | collectInlineSiteChildren(SecondaryFuncIds, FI, Site); |
| 376 | |
| 377 | OS.EmitCVInlineLinetableDirective(Site.SiteFuncId, FileId, StartLineNum, |
David Majnemer | c9911f2 | 2016-02-02 19:22:34 +0000 | [diff] [blame] | 378 | FI.Begin, FI.End, SecondaryFuncIds); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 379 | |
| 380 | OS.EmitLabel(InlineEnd); |
| 381 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 382 | for (const LocalVariable &Var : Site.InlinedLocals) |
| 383 | emitLocalVariable(Var); |
| 384 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 385 | // Recurse on child inlined call sites before closing the scope. |
| 386 | for (const DILocation *ChildSite : Site.ChildSites) { |
| 387 | auto I = FI.InlineSites.find(ChildSite); |
| 388 | assert(I != FI.InlineSites.end() && |
| 389 | "child site not in function inline site map"); |
| 390 | emitInlinedCallSite(FI, ChildSite, I->second); |
| 391 | } |
| 392 | |
| 393 | // Close the scope. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 394 | OS.AddComment("Record length"); |
| 395 | OS.EmitIntValue(2, 2); // RecordLength |
| 396 | OS.AddComment("Record kind: S_INLINESITE_END"); |
| 397 | OS.EmitIntValue(SymbolRecordKind::S_INLINESITE_END, 2); // RecordKind |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 400 | static void emitNullTerminatedString(MCStreamer &OS, StringRef S) { |
| 401 | SmallString<32> NullTerminatedString(S); |
| 402 | if (NullTerminatedString.empty() || NullTerminatedString.back() != '\0') |
| 403 | NullTerminatedString.push_back('\0'); |
| 404 | OS.EmitBytes(NullTerminatedString); |
| 405 | } |
| 406 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 407 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 408 | FunctionInfo &FI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 409 | // For each function there is a separate subsection |
| 410 | // which holds the PC to file:line table. |
| 411 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 412 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 413 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 414 | StringRef FuncName; |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 415 | if (auto *SP = getDISubprogram(GV)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 416 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 417 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 418 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 419 | if (FuncName.empty()) |
| 420 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 421 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 422 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 423 | MCSymbol *SymbolsBegin = MMI->getContext().createTempSymbol(), |
| 424 | *SymbolsEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 425 | OS.AddComment("Symbol subsection for " + Twine(FuncName)); |
| 426 | OS.EmitIntValue(unsigned(ModuleSubstreamKind::Symbols), 4); |
| 427 | OS.AddComment("Subsection size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 428 | OS.emitAbsoluteSymbolDiff(SymbolsEnd, SymbolsBegin, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 429 | OS.EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 430 | { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 431 | MCSymbol *ProcRecordBegin = MMI->getContext().createTempSymbol(), |
| 432 | *ProcRecordEnd = MMI->getContext().createTempSymbol(); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 433 | OS.AddComment("Record length"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 434 | OS.emitAbsoluteSymbolDiff(ProcRecordEnd, ProcRecordBegin, 2); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 435 | OS.EmitLabel(ProcRecordBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 436 | |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 437 | OS.AddComment("Record kind: S_GPROC32_ID"); |
| 438 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_GPROC32_ID), 2); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 439 | |
David Majnemer | 30579ec | 2016-02-02 23:18:23 +0000 | [diff] [blame] | 440 | // 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] | 441 | OS.AddComment("PtrParent"); |
| 442 | OS.EmitIntValue(0, 4); |
| 443 | OS.AddComment("PtrEnd"); |
| 444 | OS.EmitIntValue(0, 4); |
| 445 | OS.AddComment("PtrNext"); |
| 446 | OS.EmitIntValue(0, 4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 447 | // This is the important bit that tells the debugger where the function |
| 448 | // code is located and what's its size: |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 449 | OS.AddComment("Code size"); |
Reid Kleckner | eb3bcdd | 2016-02-03 21:24:42 +0000 | [diff] [blame] | 450 | OS.emitAbsoluteSymbolDiff(FI.End, Fn, 4); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 451 | OS.AddComment("Offset after prologue"); |
| 452 | OS.EmitIntValue(0, 4); |
| 453 | OS.AddComment("Offset before epilogue"); |
| 454 | OS.EmitIntValue(0, 4); |
| 455 | OS.AddComment("Function type index"); |
| 456 | OS.EmitIntValue(0, 4); |
| 457 | OS.AddComment("Function section relative address"); |
| 458 | OS.EmitCOFFSecRel32(Fn); |
| 459 | OS.AddComment("Function section index"); |
| 460 | OS.EmitCOFFSectionIndex(Fn); |
| 461 | OS.AddComment("Flags"); |
| 462 | OS.EmitIntValue(0, 1); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 463 | // Emit the function display name as a null-terminated string. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 464 | OS.AddComment("Function name"); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 465 | emitNullTerminatedString(OS, FuncName); |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 466 | OS.EmitLabel(ProcRecordEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 467 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 468 | for (const LocalVariable &Var : FI.Locals) |
| 469 | emitLocalVariable(Var); |
| 470 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 471 | // Emit inlined call site information. Only emit functions inlined directly |
| 472 | // into the parent function. We'll emit the other sites recursively as part |
| 473 | // of their parent inline site. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 474 | for (const DILocation *InlinedAt : FI.ChildSites) { |
| 475 | auto I = FI.InlineSites.find(InlinedAt); |
| 476 | assert(I != FI.InlineSites.end() && |
| 477 | "child site not in function inline site map"); |
| 478 | emitInlinedCallSite(FI, InlinedAt, I->second); |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 481 | // We're done with this function. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 482 | OS.AddComment("Record length"); |
| 483 | OS.EmitIntValue(0x0002, 2); |
| 484 | OS.AddComment("Record kind: S_PROC_ID_END"); |
| 485 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_PROC_ID_END), 2); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 486 | } |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 487 | OS.EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 488 | // Every subsection must be aligned to a 4-byte boundary. |
Reid Kleckner | dac21b4 | 2016-02-03 21:15:48 +0000 | [diff] [blame] | 489 | OS.EmitValueToAlignment(4); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 490 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 491 | // 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] | 492 | OS.EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 495 | CodeViewDebug::LocalVarDefRange |
| 496 | CodeViewDebug::createDefRangeMem(uint16_t CVRegister, int Offset) { |
| 497 | LocalVarDefRange DR; |
Aaron Ballman | c6a2f21 | 2016-02-16 15:35:51 +0000 | [diff] [blame] | 498 | DR.InMemory = -1; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 499 | DR.DataOffset = Offset; |
| 500 | assert(DR.DataOffset == Offset && "truncation"); |
| 501 | DR.StructOffset = 0; |
| 502 | DR.CVRegister = CVRegister; |
| 503 | return DR; |
| 504 | } |
| 505 | |
| 506 | CodeViewDebug::LocalVarDefRange |
| 507 | CodeViewDebug::createDefRangeReg(uint16_t CVRegister) { |
| 508 | LocalVarDefRange DR; |
| 509 | DR.InMemory = 0; |
| 510 | DR.DataOffset = 0; |
| 511 | DR.StructOffset = 0; |
| 512 | DR.CVRegister = CVRegister; |
| 513 | return DR; |
| 514 | } |
| 515 | |
| 516 | void CodeViewDebug::collectVariableInfoFromMMITable( |
| 517 | DenseSet<InlinedVariable> &Processed) { |
| 518 | const TargetSubtargetInfo &TSI = Asm->MF->getSubtarget(); |
| 519 | const TargetFrameLowering *TFI = TSI.getFrameLowering(); |
| 520 | const TargetRegisterInfo *TRI = TSI.getRegisterInfo(); |
| 521 | |
| 522 | for (const MachineModuleInfo::VariableDbgInfo &VI : |
| 523 | MMI->getVariableDbgInfo()) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 524 | if (!VI.Var) |
| 525 | continue; |
| 526 | assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) && |
| 527 | "Expected inlined-at fields to agree"); |
| 528 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 529 | Processed.insert(InlinedVariable(VI.Var, VI.Loc->getInlinedAt())); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 530 | LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc); |
| 531 | |
| 532 | // If variable scope is not found then skip this variable. |
| 533 | if (!Scope) |
| 534 | continue; |
| 535 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 536 | // Get the frame register used and the offset. |
| 537 | unsigned FrameReg = 0; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 538 | int FrameOffset = TFI->getFrameIndexReference(*Asm->MF, VI.Slot, FrameReg); |
| 539 | uint16_t CVReg = TRI->getCodeViewRegNum(FrameReg); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 540 | |
| 541 | // Calculate the label ranges. |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 542 | LocalVarDefRange DefRange = createDefRangeMem(CVReg, FrameOffset); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 543 | for (const InsnRange &Range : Scope->getRanges()) { |
| 544 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 545 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 546 | End = End ? End : Asm->getFunctionEnd(); |
| 547 | DefRange.Ranges.emplace_back(Begin, End); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 550 | LocalVariable Var; |
| 551 | Var.DIVar = VI.Var; |
| 552 | Var.DefRanges.emplace_back(std::move(DefRange)); |
| 553 | recordLocalVariable(std::move(Var), VI.Loc->getInlinedAt()); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | void CodeViewDebug::collectVariableInfo(const DISubprogram *SP) { |
| 558 | DenseSet<InlinedVariable> Processed; |
| 559 | // Grab the variable info that was squirreled away in the MMI side-table. |
| 560 | collectVariableInfoFromMMITable(Processed); |
| 561 | |
| 562 | const TargetRegisterInfo *TRI = Asm->MF->getSubtarget().getRegisterInfo(); |
| 563 | |
| 564 | for (const auto &I : DbgValues) { |
| 565 | InlinedVariable IV = I.first; |
| 566 | if (Processed.count(IV)) |
| 567 | continue; |
| 568 | const DILocalVariable *DIVar = IV.first; |
| 569 | const DILocation *InlinedAt = IV.second; |
| 570 | |
| 571 | // Instruction ranges, specifying where IV is accessible. |
| 572 | const auto &Ranges = I.second; |
| 573 | |
| 574 | LexicalScope *Scope = nullptr; |
| 575 | if (InlinedAt) |
| 576 | Scope = LScopes.findInlinedScope(DIVar->getScope(), InlinedAt); |
| 577 | else |
| 578 | Scope = LScopes.findLexicalScope(DIVar->getScope()); |
| 579 | // If variable scope is not found then skip this variable. |
| 580 | if (!Scope) |
| 581 | continue; |
| 582 | |
| 583 | LocalVariable Var; |
| 584 | Var.DIVar = DIVar; |
| 585 | |
| 586 | // Calculate the definition ranges. |
| 587 | for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) { |
| 588 | const InsnRange &Range = *I; |
| 589 | const MachineInstr *DVInst = Range.first; |
| 590 | assert(DVInst->isDebugValue() && "Invalid History entry"); |
| 591 | const DIExpression *DIExpr = DVInst->getDebugExpression(); |
| 592 | |
| 593 | // Bail if there is a complex DWARF expression for now. |
| 594 | if (DIExpr && DIExpr->getNumElements() > 0) |
| 595 | continue; |
| 596 | |
Reid Kleckner | 9a593ee | 2016-02-16 21:49:26 +0000 | [diff] [blame^] | 597 | // Bail if operand 0 is not a valid register. This means the variable is a |
| 598 | // simple constant, or is described by a complex expression. |
| 599 | // FIXME: Find a way to represent constant variables, since they are |
| 600 | // relatively common. |
| 601 | unsigned Reg = |
| 602 | DVInst->getOperand(0).isReg() ? DVInst->getOperand(0).getReg() : 0; |
| 603 | if (Reg == 0) |
Reid Kleckner | 6e0d5f5 | 2016-02-16 21:14:51 +0000 | [diff] [blame] | 604 | continue; |
| 605 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 606 | // Handle the two cases we can handle: indirect in memory and in register. |
| 607 | bool IsIndirect = DVInst->getOperand(1).isImm(); |
| 608 | unsigned CVReg = TRI->getCodeViewRegNum(DVInst->getOperand(0).getReg()); |
| 609 | { |
| 610 | LocalVarDefRange DefRange; |
| 611 | if (IsIndirect) { |
| 612 | int64_t Offset = DVInst->getOperand(1).getImm(); |
| 613 | DefRange = createDefRangeMem(CVReg, Offset); |
| 614 | } else { |
| 615 | DefRange = createDefRangeReg(CVReg); |
| 616 | } |
| 617 | if (Var.DefRanges.empty() || |
| 618 | Var.DefRanges.back().isDifferentLocation(DefRange)) { |
| 619 | Var.DefRanges.emplace_back(std::move(DefRange)); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // Compute the label range. |
| 624 | const MCSymbol *Begin = getLabelBeforeInsn(Range.first); |
| 625 | const MCSymbol *End = getLabelAfterInsn(Range.second); |
| 626 | if (!End) { |
| 627 | if (std::next(I) != E) |
| 628 | End = getLabelBeforeInsn(std::next(I)->first); |
| 629 | else |
| 630 | End = Asm->getFunctionEnd(); |
| 631 | } |
| 632 | |
| 633 | // If the last range end is our begin, just extend the last range. |
| 634 | // Otherwise make a new range. |
| 635 | SmallVectorImpl<std::pair<const MCSymbol *, const MCSymbol *>> &Ranges = |
| 636 | Var.DefRanges.back().Ranges; |
| 637 | if (!Ranges.empty() && Ranges.back().second == Begin) |
| 638 | Ranges.back().second = End; |
| 639 | else |
| 640 | Ranges.emplace_back(Begin, End); |
| 641 | |
| 642 | // FIXME: Do more range combining. |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 643 | } |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 644 | |
| 645 | recordLocalVariable(std::move(Var), InlinedAt); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 649 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 650 | assert(!CurFn && "Can't process two functions at once!"); |
| 651 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 652 | if (!Asm || !MMI->hasDebugInfo()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 653 | return; |
| 654 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 655 | DebugHandlerBase::beginFunction(MF); |
| 656 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 657 | const Function *GV = MF->getFunction(); |
| 658 | assert(FnDebugInfo.count(GV) == false); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 659 | CurFn = &FnDebugInfo[GV]; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 660 | CurFn->FuncId = NextFuncId++; |
Reid Kleckner | 1fcd610 | 2016-02-02 17:41:18 +0000 | [diff] [blame] | 661 | CurFn->Begin = Asm->getFunctionBegin(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 662 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 663 | // Find the end of the function prolog. First known non-DBG_VALUE and |
| 664 | // non-frame setup location marks the beginning of the function body. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 665 | // FIXME: is there a simpler a way to do this? Can we just search |
| 666 | // for the first instruction of the function, not the last of the prolog? |
| 667 | DebugLoc PrologEndLoc; |
| 668 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 669 | for (const auto &MBB : *MF) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 670 | for (const auto &MI : MBB) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 671 | if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) && |
| 672 | MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 673 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 674 | break; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 675 | } else if (!MI.isDebugValue()) { |
| 676 | EmptyPrologue = false; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 677 | } |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 678 | } |
| 679 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 680 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 681 | // 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] | 682 | if (PrologEndLoc && !EmptyPrologue) { |
| 683 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 684 | maybeRecordLocation(FnStartDL, MF); |
| 685 | } |
| 686 | } |
| 687 | |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 688 | void CodeViewDebug::emitLocalVariable(const LocalVariable &Var) { |
| 689 | // LocalSym record, see SymbolRecord.h for more info. |
| 690 | MCSymbol *LocalBegin = MMI->getContext().createTempSymbol(), |
| 691 | *LocalEnd = MMI->getContext().createTempSymbol(); |
| 692 | OS.AddComment("Record length"); |
| 693 | OS.emitAbsoluteSymbolDiff(LocalEnd, LocalBegin, 2); |
| 694 | OS.EmitLabel(LocalBegin); |
| 695 | |
| 696 | OS.AddComment("Record kind: S_LOCAL"); |
| 697 | OS.EmitIntValue(unsigned(SymbolRecordKind::S_LOCAL), 2); |
| 698 | |
| 699 | uint16_t Flags = 0; |
| 700 | if (Var.DIVar->isParameter()) |
| 701 | Flags |= LocalSym::IsParameter; |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 702 | if (Var.DefRanges.empty()) |
| 703 | Flags |= LocalSym::IsOptimizedOut; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 704 | |
| 705 | OS.AddComment("TypeIndex"); |
| 706 | OS.EmitIntValue(TypeIndex::Int32().getIndex(), 4); |
| 707 | OS.AddComment("Flags"); |
| 708 | OS.EmitIntValue(Flags, 2); |
| 709 | emitNullTerminatedString(OS, Var.DIVar->getName()); |
| 710 | OS.EmitLabel(LocalEnd); |
| 711 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 712 | // Calculate the on disk prefix of the appropriate def range record. The |
| 713 | // records and on disk formats are described in SymbolRecords.h. BytePrefix |
| 714 | // should be big enough to hold all forms without memory allocation. |
| 715 | SmallString<20> BytePrefix; |
| 716 | for (const LocalVarDefRange &DefRange : Var.DefRanges) { |
| 717 | BytePrefix.clear(); |
| 718 | // FIXME: Handle bitpieces. |
| 719 | if (DefRange.StructOffset != 0) |
| 720 | continue; |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 721 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 722 | if (DefRange.InMemory) { |
| 723 | DefRangeRegisterRelSym Sym{}; |
| 724 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER_REL); |
| 725 | Sym.BaseRegister = DefRange.CVRegister; |
| 726 | Sym.Flags = 0; // Unclear what matters here. |
| 727 | Sym.BasePointerOffset = DefRange.DataOffset; |
| 728 | BytePrefix += |
| 729 | StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind)); |
| 730 | BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym), |
| 731 | sizeof(Sym) - sizeof(LocalVariableAddrRange)); |
| 732 | } else { |
| 733 | assert(DefRange.DataOffset == 0 && "unexpected offset into register"); |
| 734 | DefRangeRegisterSym Sym{}; |
| 735 | ulittle16_t SymKind = ulittle16_t(S_DEFRANGE_REGISTER); |
| 736 | Sym.Register = DefRange.CVRegister; |
| 737 | Sym.MayHaveNoName = 0; // Unclear what matters here. |
| 738 | BytePrefix += |
| 739 | StringRef(reinterpret_cast<const char *>(&SymKind), sizeof(SymKind)); |
| 740 | BytePrefix += StringRef(reinterpret_cast<const char *>(&Sym), |
| 741 | sizeof(Sym) - sizeof(LocalVariableAddrRange)); |
| 742 | } |
| 743 | OS.EmitCVDefRangeDirective(DefRange.Ranges, BytePrefix); |
| 744 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 747 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 748 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 749 | return; |
| 750 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 751 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 752 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 753 | assert(CurFn == &FnDebugInfo[GV]); |
| 754 | |
Reid Kleckner | 876330d | 2016-02-12 21:48:30 +0000 | [diff] [blame] | 755 | collectVariableInfo(getDISubprogram(GV)); |
| 756 | |
| 757 | DebugHandlerBase::endFunction(MF); |
| 758 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 759 | // Don't emit anything if we don't have any line tables. |
| 760 | if (!CurFn->HaveLineInfo) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 761 | FnDebugInfo.erase(GV); |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 762 | CurFn = nullptr; |
| 763 | return; |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 764 | } |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 765 | |
| 766 | CurFn->End = Asm->getFunctionEnd(); |
| 767 | |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 768 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 771 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Reid Kleckner | f9c275f | 2016-02-10 20:55:49 +0000 | [diff] [blame] | 772 | DebugHandlerBase::beginInstruction(MI); |
| 773 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 774 | // Ignore DBG_VALUE locations and function prologue. |
| 775 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 776 | return; |
| 777 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 778 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 779 | return; |
| 780 | maybeRecordLocation(DL, Asm->MF); |
| 781 | } |