Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 1 | //===-- llvm/lib/CodeGen/AsmPrinter/CodeViewDebug.cpp --*- C++ -*--===// |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 10 | // This file contains support for writing Microsoft CodeView debug info. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 14 | #include "CodeViewDebug.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/Line.h" |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/SymbolRecord.h" |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame^] | 18 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
| 19 | #include "llvm/DebugInfo/CodeView/TypeRecord.h" |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/MC/MCSymbol.h" |
| 22 | #include "llvm/Support/COFF.h" |
| 23 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 24 | using namespace llvm::codeview; |
| 25 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 26 | namespace llvm { |
| 27 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 28 | StringRef CodeViewDebug::getFullFilepath(const DIFile *File) { |
| 29 | std::string &Filepath = FileToFilepathMap[File]; |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 30 | if (!Filepath.empty()) |
| 31 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 32 | |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 33 | StringRef Dir = File->getDirectory(), Filename = File->getFilename(); |
| 34 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 35 | // Clang emits directory and relative filename info into the IR, but CodeView |
| 36 | // operates on full paths. We could change Clang to emit full paths too, but |
| 37 | // that would increase the IR size and probably not needed for other users. |
| 38 | // For now, just concatenate and canonicalize the path here. |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 39 | if (Filename.find(':') == 1) |
| 40 | Filepath = Filename; |
| 41 | else |
Yaron Keren | 75e0c4b | 2015-03-27 17:51:30 +0000 | [diff] [blame] | 42 | Filepath = (Dir + "\\" + Filename).str(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 43 | |
| 44 | // Canonicalize the path. We have to do it textually because we may no longer |
| 45 | // have access the file in the filesystem. |
| 46 | // First, replace all slashes with backslashes. |
| 47 | std::replace(Filepath.begin(), Filepath.end(), '/', '\\'); |
| 48 | |
| 49 | // Remove all "\.\" with "\". |
| 50 | size_t Cursor = 0; |
| 51 | while ((Cursor = Filepath.find("\\.\\", Cursor)) != std::string::npos) |
| 52 | Filepath.erase(Cursor, 2); |
| 53 | |
| 54 | // Replace all "\XXX\..\" with "\". Don't try too hard though as the original |
| 55 | // path should be well-formatted, e.g. start with a drive letter, etc. |
| 56 | Cursor = 0; |
| 57 | while ((Cursor = Filepath.find("\\..\\", Cursor)) != std::string::npos) { |
| 58 | // Something's wrong if the path starts with "\..\", abort. |
| 59 | if (Cursor == 0) |
| 60 | break; |
| 61 | |
| 62 | size_t PrevSlash = Filepath.rfind('\\', Cursor - 1); |
| 63 | if (PrevSlash == std::string::npos) |
| 64 | // Something's wrong, abort. |
| 65 | break; |
| 66 | |
| 67 | Filepath.erase(PrevSlash, Cursor + 3 - PrevSlash); |
| 68 | // The next ".." might be following the one we've just erased. |
| 69 | Cursor = PrevSlash; |
| 70 | } |
| 71 | |
| 72 | // Remove all duplicate backslashes. |
| 73 | Cursor = 0; |
| 74 | while ((Cursor = Filepath.find("\\\\", Cursor)) != std::string::npos) |
| 75 | Filepath.erase(Cursor, 1); |
| 76 | |
Reid Kleckner | 1f11b4e | 2015-12-02 22:34:30 +0000 | [diff] [blame] | 77 | return Filepath; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 80 | unsigned CodeViewDebug::maybeRecordFile(const DIFile *F) { |
| 81 | unsigned NextId = FileIdMap.size() + 1; |
| 82 | auto Insertion = FileIdMap.insert(std::make_pair(F, NextId)); |
| 83 | if (Insertion.second) { |
| 84 | // We have to compute the full filepath and emit a .cv_file directive. |
| 85 | StringRef FullPath = getFullFilepath(F); |
| 86 | NextId = Asm->OutStreamer->EmitCVFileDirective(NextId, FullPath); |
| 87 | assert(NextId == FileIdMap.size() && ".cv_file directive failed"); |
| 88 | } |
| 89 | return Insertion.first->second; |
| 90 | } |
| 91 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame^] | 92 | CodeViewDebug::InlineSite &CodeViewDebug::getInlineSite(const DILocation *Loc) { |
| 93 | const DILocation *InlinedAt = Loc->getInlinedAt(); |
| 94 | auto Insertion = CurFn->InlineSites.insert({InlinedAt, InlineSite()}); |
| 95 | if (Insertion.second) { |
| 96 | InlineSite &Site = Insertion.first->second; |
| 97 | Site.SiteFuncId = NextFuncId++; |
| 98 | Site.Inlinee = Loc->getScope()->getSubprogram(); |
| 99 | } |
| 100 | return Insertion.first->second; |
| 101 | } |
| 102 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 103 | void CodeViewDebug::maybeRecordLocation(DebugLoc DL, |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 104 | const MachineFunction *MF) { |
| 105 | // Skip this instruction if it has the same location as the previous one. |
| 106 | if (DL == CurFn->LastLoc) |
| 107 | return; |
| 108 | |
| 109 | const DIScope *Scope = DL.get()->getScope(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 110 | if (!Scope) |
| 111 | return; |
Reid Kleckner | 9533af4 | 2016-01-16 00:09:09 +0000 | [diff] [blame] | 112 | |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 113 | // 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] | 114 | LineInfo LI(DL.getLine(), DL.getLine(), /*IsStatement=*/true); |
| 115 | if (LI.getStartLine() != DL.getLine() || LI.isAlwaysStepInto() || |
| 116 | LI.isNeverStepInto()) |
David Majnemer | c3340db | 2016-01-13 01:05:23 +0000 | [diff] [blame] | 117 | return; |
| 118 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 119 | ColumnInfo CI(DL.getCol(), /*EndColumn=*/0); |
| 120 | if (CI.getStartColumn() != DL.getCol()) |
| 121 | return; |
Reid Kleckner | 00d9639 | 2016-01-29 00:13:28 +0000 | [diff] [blame] | 122 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 123 | if (!CurFn->HaveLineInfo) |
| 124 | CurFn->HaveLineInfo = true; |
| 125 | unsigned FileId = 0; |
| 126 | if (CurFn->LastLoc.get() && CurFn->LastLoc->getFile() == DL->getFile()) |
| 127 | FileId = CurFn->LastFileId; |
| 128 | else |
| 129 | FileId = CurFn->LastFileId = maybeRecordFile(DL->getFile()); |
| 130 | CurFn->LastLoc = DL; |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame^] | 131 | |
| 132 | unsigned FuncId = CurFn->FuncId; |
| 133 | if (const DILocation *Loc = DL->getInlinedAt()) { |
| 134 | // If this location was actually inlined from somewhere else, give it the ID |
| 135 | // of the inline call site. |
| 136 | FuncId = getInlineSite(DL.get()).SiteFuncId; |
| 137 | // Ensure we have links in the tree of inline call sites. |
| 138 | const DILocation *ChildLoc = nullptr; |
| 139 | while (Loc->getInlinedAt()) { |
| 140 | InlineSite &Site = getInlineSite(Loc); |
| 141 | if (ChildLoc) { |
| 142 | // Record the child inline site if not already present. |
| 143 | auto B = Site.ChildSites.begin(), E = Site.ChildSites.end(); |
| 144 | if (std::find(B, E, Loc) != E) |
| 145 | break; |
| 146 | Site.ChildSites.push_back(Loc); |
| 147 | } |
| 148 | ChildLoc = Loc; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | Asm->OutStreamer->EmitCVLocDirective(FuncId, FileId, DL.getLine(), |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 153 | DL.getCol(), /*PrologueEnd=*/false, |
| 154 | /*IsStmt=*/false, DL->getFilename()); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 157 | CodeViewDebug::CodeViewDebug(AsmPrinter *AP) |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 158 | : Asm(nullptr), CurFn(nullptr) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 159 | MachineModuleInfo *MMI = AP->MMI; |
| 160 | |
| 161 | // If module doesn't have named metadata anchors or COFF debug section |
| 162 | // is not available, skip any debug info related stuff. |
| 163 | if (!MMI->getModule()->getNamedMetadata("llvm.dbg.cu") || |
| 164 | !AP->getObjFileLowering().getCOFFDebugSymbolsSection()) |
| 165 | return; |
| 166 | |
| 167 | // Tell MMI that we have debug info. |
| 168 | MMI->setDebugInfoAvailability(true); |
| 169 | Asm = AP; |
| 170 | } |
| 171 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 172 | void CodeViewDebug::endModule() { |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 173 | if (FnDebugInfo.empty()) |
| 174 | return; |
| 175 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame^] | 176 | emitTypeInformation(); |
| 177 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 178 | // FIXME: For functions that are comdat, we should emit separate .debug$S |
| 179 | // sections that are comdat associative with the main function instead of |
| 180 | // having one big .debug$S section. |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 181 | assert(Asm != nullptr); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 182 | Asm->OutStreamer->SwitchSection( |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 183 | Asm->getObjFileLowering().getCOFFDebugSymbolsSection()); |
| 184 | Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC); |
| 185 | |
| 186 | // The COFF .debug$S section consists of several subsections, each starting |
| 187 | // with a 4-byte control code (e.g. 0xF1, 0xF2, etc) and then a 4-byte length |
| 188 | // of the payload followed by the payload itself. The subsections are 4-byte |
| 189 | // aligned. |
| 190 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 191 | // Emit per-function debug information. |
| 192 | for (auto &P : FnDebugInfo) |
| 193 | emitDebugInfoForFunction(P.first, P.second); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 194 | |
| 195 | // This subsection holds a file index to offset in string table table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 196 | Asm->OutStreamer->AddComment("File index to string table offset subsection"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 197 | Asm->OutStreamer->EmitCVFileChecksumsDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 198 | |
| 199 | // This subsection holds the string table. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 200 | Asm->OutStreamer->AddComment("String table"); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 201 | Asm->OutStreamer->EmitCVStringTableDirective(); |
Timur Iskhodzhanov | 2cf8a1d | 2014-10-10 16:05:32 +0000 | [diff] [blame] | 202 | |
| 203 | clear(); |
| 204 | } |
| 205 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame^] | 206 | template <typename T> static void emitRecord(MCStreamer &OS, const T &Rec) { |
| 207 | OS.EmitBytes(StringRef(reinterpret_cast<const char *>(&Rec), sizeof(Rec))); |
| 208 | } |
| 209 | |
| 210 | void CodeViewDebug::emitTypeInformation() { |
| 211 | // Start the .debug$T section with 0x4. |
| 212 | Asm->OutStreamer->SwitchSection( |
| 213 | Asm->getObjFileLowering().getCOFFDebugTypesSection()); |
| 214 | Asm->EmitInt32(COFF::DEBUG_SECTION_MAGIC); |
| 215 | |
| 216 | NamedMDNode *CU_Nodes = |
| 217 | Asm->MMI->getModule()->getNamedMetadata("llvm.dbg.cu"); |
| 218 | if (!CU_Nodes) |
| 219 | return; |
| 220 | |
| 221 | // This type info currently only holds function ids for use with inline call |
| 222 | // frame info. All functions are assigned a simple 'void ()' type. Emit that |
| 223 | // type here. |
| 224 | TypeIndex ArgListIdx = getNextTypeIndex(); |
| 225 | Asm->EmitInt16(2 + sizeof(ArgList)); |
| 226 | Asm->EmitInt16(LF_ARGLIST); |
| 227 | Asm->EmitInt32(0); |
| 228 | |
| 229 | TypeIndex VoidProcIdx = getNextTypeIndex(); |
| 230 | Asm->EmitInt16(2 + sizeof(ProcedureType)); |
| 231 | Asm->EmitInt16(LF_PROCEDURE); |
| 232 | ProcedureType Proc{}; // Zero initialize. |
| 233 | Proc.ReturnType = TypeIndex::Void(); |
| 234 | Proc.CallConv = CallingConvention::NearC; |
| 235 | Proc.Options = FunctionOptions::None; |
| 236 | Proc.NumParameters = 0; |
| 237 | Proc.ArgListType = ArgListIdx; |
| 238 | emitRecord(*Asm->OutStreamer, Proc); |
| 239 | |
| 240 | for (MDNode *N : CU_Nodes->operands()) { |
| 241 | auto *CUNode = cast<DICompileUnit>(N); |
| 242 | for (auto *SP : CUNode->getSubprograms()) { |
| 243 | StringRef DisplayName = SP->getDisplayName(); |
| 244 | Asm->EmitInt16(2 + sizeof(FuncId) + DisplayName.size() + 1); |
| 245 | Asm->EmitInt16(LF_FUNC_ID); |
| 246 | |
| 247 | FuncId Func{}; // Zero initialize. |
| 248 | Func.ParentScope = TypeIndex(); |
| 249 | Func.FunctionType = VoidProcIdx; |
| 250 | emitRecord(*Asm->OutStreamer, Func); |
| 251 | Asm->OutStreamer->EmitBytes(DisplayName); |
| 252 | Asm->EmitInt8(0); |
| 253 | |
| 254 | TypeIndex FuncIdIdx = getNextTypeIndex(); |
| 255 | SubprogramToFuncId.insert(std::make_pair(SP, FuncIdIdx)); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 260 | static void EmitLabelDiff(MCStreamer &Streamer, |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 261 | const MCSymbol *From, const MCSymbol *To, |
| 262 | unsigned int Size = 4) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 263 | MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; |
| 264 | MCContext &Context = Streamer.getContext(); |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 265 | const MCExpr *FromRef = MCSymbolRefExpr::create(From, Variant, Context), |
| 266 | *ToRef = MCSymbolRefExpr::create(To, Variant, Context); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 267 | const MCExpr *AddrDelta = |
Jim Grosbach | 13760bd | 2015-05-30 01:25:56 +0000 | [diff] [blame] | 268 | MCBinaryExpr::create(MCBinaryExpr::Sub, ToRef, FromRef, Context); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 269 | Streamer.EmitValue(AddrDelta, Size); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame^] | 272 | void CodeViewDebug::emitInlinedCallSite(const FunctionInfo &FI, |
| 273 | const DILocation *InlinedAt, |
| 274 | const InlineSite &Site) { |
| 275 | MCStreamer &OS = *Asm->OutStreamer; |
| 276 | |
| 277 | MCSymbol *InlineBegin = Asm->MMI->getContext().createTempSymbol(), |
| 278 | *InlineEnd = Asm->MMI->getContext().createTempSymbol(); |
| 279 | |
| 280 | assert(SubprogramToFuncId.count(Site.Inlinee)); |
| 281 | TypeIndex InlineeIdx = SubprogramToFuncId[Site.Inlinee]; |
| 282 | |
| 283 | // SymbolRecord |
| 284 | EmitLabelDiff(OS, InlineBegin, InlineEnd, 2); // RecordLength |
| 285 | OS.EmitLabel(InlineBegin); |
| 286 | Asm->EmitInt16(SymbolRecordKind::S_INLINESITE); // RecordKind |
| 287 | |
| 288 | InlineSiteSym SiteBytes{}; |
| 289 | SiteBytes.Inlinee = InlineeIdx; |
| 290 | Asm->OutStreamer->EmitBytes( |
| 291 | StringRef(reinterpret_cast<const char *>(&SiteBytes), sizeof(SiteBytes))); |
| 292 | |
| 293 | // FIXME: annotations |
| 294 | |
| 295 | OS.EmitLabel(InlineEnd); |
| 296 | |
| 297 | // Recurse on child inlined call sites before closing the scope. |
| 298 | for (const DILocation *ChildSite : Site.ChildSites) { |
| 299 | auto I = FI.InlineSites.find(ChildSite); |
| 300 | assert(I != FI.InlineSites.end() && |
| 301 | "child site not in function inline site map"); |
| 302 | emitInlinedCallSite(FI, ChildSite, I->second); |
| 303 | } |
| 304 | |
| 305 | // Close the scope. |
| 306 | Asm->EmitInt16(2); // RecordLength |
| 307 | Asm->EmitInt16(SymbolRecordKind::S_INLINESITE_END); // RecordKind |
| 308 | } |
| 309 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 310 | void CodeViewDebug::emitDebugInfoForFunction(const Function *GV, |
| 311 | FunctionInfo &FI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 312 | // For each function there is a separate subsection |
| 313 | // which holds the PC to file:line table. |
| 314 | const MCSymbol *Fn = Asm->getSymbol(GV); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 315 | assert(Fn); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 316 | |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 317 | StringRef FuncName; |
Duncan P. N. Exon Smith | 2fbe135 | 2015-04-20 22:10:08 +0000 | [diff] [blame] | 318 | if (auto *SP = getDISubprogram(GV)) |
Duncan P. N. Exon Smith | 537b4a8 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 319 | FuncName = SP->getDisplayName(); |
Duncan P. N. Exon Smith | 23e56ec | 2015-03-20 19:50:00 +0000 | [diff] [blame] | 320 | |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 321 | // If our DISubprogram name is empty, use the mangled name. |
Reid Kleckner | 72e2ba7 | 2016-01-13 19:32:35 +0000 | [diff] [blame] | 322 | if (FuncName.empty()) |
| 323 | FuncName = GlobalValue::getRealLinkageName(GV->getName()); |
Reid Kleckner | 3c0ff98 | 2016-01-14 00:12:54 +0000 | [diff] [blame] | 324 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 325 | // Emit a symbol subsection, required by VS2012+ to find function boundaries. |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 326 | MCSymbol *SymbolsBegin = Asm->MMI->getContext().createTempSymbol(), |
| 327 | *SymbolsEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 328 | Asm->OutStreamer->AddComment("Symbol subsection for " + Twine(FuncName)); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 329 | Asm->EmitInt32(unsigned(ModuleSubstreamKind::Symbols)); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 330 | EmitLabelDiff(*Asm->OutStreamer, SymbolsBegin, SymbolsEnd); |
| 331 | Asm->OutStreamer->EmitLabel(SymbolsBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 332 | { |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 333 | MCSymbol *ProcSegmentBegin = Asm->MMI->getContext().createTempSymbol(), |
| 334 | *ProcSegmentEnd = Asm->MMI->getContext().createTempSymbol(); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 335 | EmitLabelDiff(*Asm->OutStreamer, ProcSegmentBegin, ProcSegmentEnd, 2); |
| 336 | Asm->OutStreamer->EmitLabel(ProcSegmentBegin); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 337 | |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 338 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_GPROC32_ID)); |
| 339 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 340 | // Some bytes of this segment don't seem to be required for basic debugging, |
| 341 | // so just fill them with zeroes. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 342 | Asm->OutStreamer->EmitFill(12, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 343 | // This is the important bit that tells the debugger where the function |
| 344 | // code is located and what's its size: |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 345 | EmitLabelDiff(*Asm->OutStreamer, Fn, FI.End); |
| 346 | Asm->OutStreamer->EmitFill(12, 0); |
| 347 | Asm->OutStreamer->EmitCOFFSecRel32(Fn); |
| 348 | Asm->OutStreamer->EmitCOFFSectionIndex(Fn); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 349 | Asm->EmitInt8(0); |
Timur Iskhodzhanov | a11b32b | 2014-11-12 20:10:09 +0000 | [diff] [blame] | 350 | // Emit the function display name as a null-terminated string. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 351 | Asm->OutStreamer->EmitBytes(FuncName); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 352 | Asm->EmitInt8(0); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 353 | Asm->OutStreamer->EmitLabel(ProcSegmentEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 354 | |
Reid Kleckner | f3b9ba4 | 2016-01-29 18:16:43 +0000 | [diff] [blame^] | 355 | // Emit inlined call site information. Only emit functions inlined directly |
| 356 | // into the parent function. We'll emit the other sites recursively as part |
| 357 | // of their parent inline site. |
| 358 | for (auto &KV : FI.InlineSites) { |
| 359 | const DILocation *InlinedAt = KV.first; |
| 360 | if (!InlinedAt->getInlinedAt()) |
| 361 | emitInlinedCallSite(FI, InlinedAt, KV.second); |
| 362 | } |
| 363 | |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 364 | // We're done with this function. |
| 365 | Asm->EmitInt16(0x0002); |
Reid Kleckner | 6b3faef | 2016-01-13 23:44:57 +0000 | [diff] [blame] | 366 | Asm->EmitInt16(unsigned(SymbolRecordKind::S_PROC_ID_END)); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 367 | } |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 368 | Asm->OutStreamer->EmitLabel(SymbolsEnd); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 369 | // Every subsection must be aligned to a 4-byte boundary. |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 370 | Asm->OutStreamer->EmitFill((-FuncName.size()) % 4, 0); |
Timur Iskhodzhanov | 2bc90fd | 2014-10-24 01:27:45 +0000 | [diff] [blame] | 371 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 372 | // We have an assembler directive that takes care of the whole line table. |
| 373 | Asm->OutStreamer->EmitCVLinetableDirective(FI.FuncId, Fn, FI.End); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 376 | void CodeViewDebug::beginFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 377 | assert(!CurFn && "Can't process two functions at once!"); |
| 378 | |
| 379 | if (!Asm || !Asm->MMI->hasDebugInfo()) |
| 380 | return; |
| 381 | |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 382 | const Function *GV = MF->getFunction(); |
| 383 | assert(FnDebugInfo.count(GV) == false); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 384 | CurFn = &FnDebugInfo[GV]; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 385 | CurFn->FuncId = NextFuncId++; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 386 | |
| 387 | // Find the end of the function prolog. |
| 388 | // FIXME: is there a simpler a way to do this? Can we just search |
| 389 | // for the first instruction of the function, not the last of the prolog? |
| 390 | DebugLoc PrologEndLoc; |
| 391 | bool EmptyPrologue = true; |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 392 | for (const auto &MBB : *MF) { |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 393 | if (PrologEndLoc) |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 394 | break; |
| 395 | for (const auto &MI : MBB) { |
| 396 | if (MI.isDebugValue()) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 397 | continue; |
| 398 | |
| 399 | // First known non-DBG_VALUE and non-frame setup location marks |
| 400 | // the beginning of the function body. |
| 401 | // FIXME: do we need the first subcondition? |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 402 | if (!MI.getFlag(MachineInstr::FrameSetup) && MI.getDebugLoc()) { |
Alexey Samsonov | f74bde6 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 403 | PrologEndLoc = MI.getDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | EmptyPrologue = false; |
| 407 | } |
| 408 | } |
| 409 | // 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] | 410 | if (PrologEndLoc && !EmptyPrologue) { |
| 411 | DebugLoc FnStartDL = PrologEndLoc.getFnDebugLoc(); |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 412 | maybeRecordLocation(FnStartDL, MF); |
| 413 | } |
| 414 | } |
| 415 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 416 | void CodeViewDebug::endFunction(const MachineFunction *MF) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 417 | if (!Asm || !CurFn) // We haven't created any debug info for this function. |
| 418 | return; |
| 419 | |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 420 | const Function *GV = MF->getFunction(); |
Yaron Keren | 6d3194f | 2014-06-20 10:26:56 +0000 | [diff] [blame] | 421 | assert(FnDebugInfo.count(GV)); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 422 | assert(CurFn == &FnDebugInfo[GV]); |
| 423 | |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 424 | // Don't emit anything if we don't have any line tables. |
| 425 | if (!CurFn->HaveLineInfo) { |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 426 | FnDebugInfo.erase(GV); |
Timur Iskhodzhanov | b5b7a61 | 2014-03-26 11:24:36 +0000 | [diff] [blame] | 427 | } else { |
Rafael Espindola | 07c03d3 | 2015-03-05 02:05:42 +0000 | [diff] [blame] | 428 | CurFn->End = Asm->getFunctionEnd(); |
Timur Iskhodzhanov | 8499a12 | 2014-03-26 09:50:36 +0000 | [diff] [blame] | 429 | } |
Craig Topper | 353eda4 | 2014-04-24 06:44:33 +0000 | [diff] [blame] | 430 | CurFn = nullptr; |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Reid Kleckner | 70f5bc9 | 2016-01-14 19:25:04 +0000 | [diff] [blame] | 433 | void CodeViewDebug::beginInstruction(const MachineInstr *MI) { |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 434 | // Ignore DBG_VALUE locations and function prologue. |
| 435 | if (!Asm || MI->isDebugValue() || MI->getFlag(MachineInstr::FrameSetup)) |
| 436 | return; |
| 437 | DebugLoc DL = MI->getDebugLoc(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 438 | if (DL == PrevInstLoc || !DL) |
Timur Iskhodzhanov | f166f6c | 2014-01-30 01:39:17 +0000 | [diff] [blame] | 439 | return; |
| 440 | maybeRecordLocation(DL, Asm->MF); |
| 441 | } |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 442 | } |