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