LLVM  10.0.0
DebugUtils.cpp
Go to the documentation of this file.
1 //===---------- DebugUtils.cpp - Utilities for debugging ORC JITs ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/Support/Debug.h"
12 #include "llvm/Support/Path.h"
14 
15 #define DEBUG_TYPE "orc"
16 
17 namespace llvm {
18 namespace orc {
19 
20 DumpObjects::DumpObjects(std::string DumpDir, std::string IdentifierOverride)
21  : DumpDir(std::move(DumpDir)),
22  IdentifierOverride(std::move(IdentifierOverride)) {
23 
24  /// Discard any trailing separators.
25  while (!this->DumpDir.empty() &&
26  sys::path::is_separator(this->DumpDir.back()))
27  this->DumpDir.pop_back();
28 }
29 
31 DumpObjects::operator()(std::unique_ptr<MemoryBuffer> Obj) {
32  size_t Idx = 1;
33 
34  std::string DumpPathStem;
35  raw_string_ostream(DumpPathStem)
36  << DumpDir << (DumpDir.empty() ? "" : "/") << getBufferIdentifier(*Obj);
37 
38  std::string DumpPath = DumpPathStem + ".o";
39  while (sys::fs::exists(DumpPath)) {
40  DumpPath.clear();
41  raw_string_ostream(DumpPath) << DumpPathStem << "." << (++Idx) << ".o";
42  }
43 
44  LLVM_DEBUG({
45  dbgs() << "Dumping object buffer [ " << (const void *)Obj->getBufferStart()
46  << " -- " << (const void *)(Obj->getBufferEnd() - 1) << " ] to "
47  << DumpPath << "\n";
48  });
49 
50  std::error_code EC;
51  raw_fd_ostream DumpStream(DumpPath, EC);
52  if (EC)
53  return errorCodeToError(EC);
54  DumpStream.write(Obj->getBufferStart(), Obj->getBufferSize());
55 
56  return std::move(Obj);
57 }
58 
59 StringRef DumpObjects::getBufferIdentifier(MemoryBuffer &B) {
60  if (!IdentifierOverride.empty())
61  return IdentifierOverride;
62  StringRef Identifier = B.getBufferIdentifier();
63  Identifier.consume_back(".o");
64  return Identifier;
65 }
66 
67 } // End namespace orc.
68 } // End namespace llvm.
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:69
bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
Definition: Path.cpp:616
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Expected< std::unique_ptr< MemoryBuffer > > operator()(std::unique_ptr< MemoryBuffer > Obj)
Dumps the given buffer to disk.
Definition: DebugUtils.cpp:31
Definition: BitVector.h:937
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
const char * getBufferStart() const
Return the beginning of the current stream buffer, or 0 if the stream is unbuffered.
Definition: raw_ostream.h:338
raw_ostream & write(unsigned char C)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:397
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition: StringRef.h:681
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:516
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1048
DumpObjects(std::string DumpDir="", std::string IdentifierOverride="")
Construct a DumpObjects transform that will dump objects to disk.
Definition: DebugUtils.cpp:20