blob: 8d74a4ded11616069a8f8d90c18d8a41fb532a3e [file] [log] [blame]
Daniel Malea06734642013-06-28 19:07:59 +00001//===- llvm/Transforms/Instrumentation/DebugIR.h - Interface ----*- C++ -*-===//
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//
10// This file defines the interface of the DebugIR pass. For most users,
11// including Instrumentation.h and calling createDebugIRPass() is sufficient and
12// there is no need to include this file.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_TRANSFORMS_INSTRUMENTATION_DEBUGIR_H
17#define LLVM_LIB_TRANSFORMS_INSTRUMENTATION_DEBUGIR_H
Daniel Malea06734642013-06-28 19:07:59 +000018
Daniel Malea06734642013-06-28 19:07:59 +000019#include "llvm/Pass.h"
20
21namespace llvm {
22
Benjamin Kramer079b96e2013-09-11 18:05:11 +000023class DebugIR : public llvm::ModulePass {
Daniel Malea06734642013-06-28 19:07:59 +000024 /// If true, write a source file to disk.
25 bool WriteSourceToDisk;
26
27 /// Hide certain (non-essential) debug information (only relevant if
28 /// createSource is true.
29 bool HideDebugIntrinsics;
30 bool HideDebugMetadata;
31
32 /// The location of the source file.
33 std::string Directory;
34 std::string Filename;
35
36 /// True if a temporary file name was generated.
37 bool GeneratedPath;
38
39 /// True if the file name was read from the Module.
40 bool ParsedPath;
41
42public:
43 static char ID;
44
Craig Topper3e4c6972014-03-05 09:10:37 +000045 const char *getPassName() const override { return "DebugIR"; }
Daniel Malea06734642013-06-28 19:07:59 +000046
47 /// Generate a file on disk to be displayed in a debugger. If Filename and
48 /// Directory are empty, a temporary path will be generated.
49 DebugIR(bool HideDebugIntrinsics, bool HideDebugMetadata,
50 llvm::StringRef Directory, llvm::StringRef Filename)
51 : ModulePass(ID), WriteSourceToDisk(true),
52 HideDebugIntrinsics(HideDebugIntrinsics),
53 HideDebugMetadata(HideDebugMetadata), Directory(Directory),
54 Filename(Filename), GeneratedPath(false), ParsedPath(false) {}
55
56 /// Modify input in-place; do not generate additional files, and do not hide
57 /// any debug intrinsics/metadata that might be present.
58 DebugIR()
59 : ModulePass(ID), WriteSourceToDisk(false), HideDebugIntrinsics(false),
60 HideDebugMetadata(false), GeneratedPath(false), ParsedPath(false) {}
61
62 /// Run pass on M and set Path to the source file path in the output module.
63 bool runOnModule(llvm::Module &M, std::string &Path);
Craig Topper3e4c6972014-03-05 09:10:37 +000064 bool runOnModule(llvm::Module &M) override;
Daniel Malea06734642013-06-28 19:07:59 +000065
66private:
67
68 /// Returns the concatenated Directory + Filename, without error checking
69 std::string getPath();
70
71 /// Attempts to read source information from debug information in M, and if
72 /// that fails, from M's identifier. Returns true on success, false otherwise.
73 bool getSourceInfo(const llvm::Module &M);
74
75 /// Replace the extension of Filename with NewExtension, and return true if
76 /// successful. Return false if extension could not be found or Filename is
77 /// empty.
78 bool updateExtension(llvm::StringRef NewExtension);
79
80 /// Generate a temporary filename and open an fd
Ahmed Charles56440fd2014-03-06 05:51:42 +000081 void generateFilename(std::unique_ptr<int> &fd);
Daniel Malea06734642013-06-28 19:07:59 +000082
83 /// Creates DWARF CU/Subroutine metadata
84 void createDebugInfo(llvm::Module &M,
Ahmed Charles56440fd2014-03-06 05:51:42 +000085 std::unique_ptr<llvm::Module> &DisplayM);
Daniel Malea06734642013-06-28 19:07:59 +000086
87 /// Returns true if either Directory or Filename is missing, false otherwise.
88 bool isMissingPath();
89
90 /// Write M to disk, optionally passing in an fd to an open file which is
91 /// closed by this function after writing. If no fd is specified, a new file
92 /// is opened, written, and closed.
Craig Topperf40110f2014-04-25 05:29:35 +000093 void writeDebugBitcode(const llvm::Module *M, int *fd = nullptr);
Daniel Malea06734642013-06-28 19:07:59 +000094};
95
96} // llvm namespace
97
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000098#endif