1//===- 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
16#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_DEBUGIR_H
17#define LLVM_TRANSFORMS_INSTRUMENTATION_DEBUGIR_H
18
19#include "llvm/Pass.h"
20
21namespace llvm {
22
23class DebugIR : public llvm::ModulePass {
24  /// 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
45  const char *getPassName() const override { return "DebugIR"; }
46
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);
64  bool runOnModule(llvm::Module &M) override;
65
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
81  void generateFilename(std::unique_ptr<int> &fd);
82
83  /// Creates DWARF CU/Subroutine metadata
84  void createDebugInfo(llvm::Module &M,
85                       std::unique_ptr<llvm::Module> &DisplayM);
86
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.
93  void writeDebugBitcode(const llvm::Module *M, int *fd = nullptr);
94};
95
96} // llvm namespace
97
98#endif // LLVM_TRANSFORMS_INSTRUMENTATION_DEBUGIR_H
99