LLDB  mainline
SystemInitializerCommon.cpp
Go to the documentation of this file.
1 //===-- SystemInitializerCommon.cpp ---------------------------------------===//
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 
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Host/Host.h"
14 #include "lldb/Host/Socket.h"
15 #include "lldb/Utility/Log.h"
17 #include "lldb/Utility/Timer.h"
18 #include "lldb/lldb-private.h"
19 
20 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
22 #endif
23 
24 #if defined(_WIN32)
27 #include <crtdbg.h>
28 #endif
29 
30 #include "llvm/Support/TargetSelect.h"
31 
32 #include <string>
33 
34 using namespace lldb_private;
35 using namespace lldb_private::repro;
36 
38  HostInfo::SharedLibraryDirectoryHelper *helper)
39  : m_shlib_dir_helper(helper) {}
40 
42 
43 /// Initialize the FileSystem based on the current reproducer mode.
45  auto &r = repro::Reproducer::Instance();
46  if (repro::Loader *loader = r.GetLoader()) {
47  FileSpec vfs_mapping = loader->GetFile<FileProvider::Info>();
48  if (vfs_mapping) {
49  if (llvm::Error e = FileSystem::Initialize(vfs_mapping))
50  return e;
51  } else {
53  }
54 
55  // Set the current working directory form the reproducer.
56  llvm::Expected<std::string> working_dir =
57  repro::GetDirectoryFrom<WorkingDirectoryProvider>(loader);
58  if (!working_dir)
59  return working_dir.takeError();
60  if (std::error_code ec = FileSystem::Instance()
61  .GetVirtualFileSystem()
62  ->setCurrentWorkingDirectory(*working_dir)) {
63  return llvm::errorCodeToError(ec);
64  }
65 
66  // Set the home directory from the reproducer.
67  llvm::Expected<std::string> home_dir =
68  repro::GetDirectoryFrom<HomeDirectoryProvider>(loader);
69  if (!home_dir)
70  return home_dir.takeError();
72 
73  return llvm::Error::success();
74  }
75 
76  if (repro::Generator *g = r.GetGenerator()) {
77  repro::VersionProvider &vp = g->GetOrCreate<repro::VersionProvider>();
78  vp.SetVersion(lldb_private::GetVersion());
79 
80  repro::FileProvider &fp = g->GetOrCreate<repro::FileProvider>();
81  FileSystem::Initialize(fp.GetFileCollector());
82 
83  fp.RecordInterestingDirectory(
84  g->GetOrCreate<repro::WorkingDirectoryProvider>().GetDirectory());
85  fp.RecordInterestingDirectory(
86  g->GetOrCreate<repro::HomeDirectoryProvider>().GetDirectory());
87 
88  return llvm::Error::success();
89  }
90 
92  return llvm::Error::success();
93 }
94 
96 #if defined(_WIN32)
97  const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
98  if (disable_crash_dialog_var &&
99  llvm::StringRef(disable_crash_dialog_var).equals_insensitive("true")) {
100  // This will prevent Windows from displaying a dialog box requiring user
101  // interaction when
102  // LLDB crashes. This is mostly useful when automating LLDB, for example
103  // via the test
104  // suite, so that a crash in LLDB does not prevent completion of the test
105  // suite.
106  ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
107  SEM_NOGPFAULTERRORBOX);
108 
109  _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
110  _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
111  _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
112  _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
113  _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
114  _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
115  }
116 #endif
117 
118  // If the reproducer wasn't initialized before, we can safely assume it's
119  // off.
120  if (!Reproducer::Initialized()) {
121  if (auto e = Reproducer::Initialize(ReproducerMode::Off, llvm::None))
122  return e;
123  }
124 
125  if (auto e = InitializeFileSystem())
126  return e;
127 
128  Log::Initialize();
129  HostInfo::Initialize(m_shlib_dir_helper);
130 
132  if (error)
133  return error;
134 
136 
138 
139 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
141 #endif
142 #if defined(_WIN32)
144 #endif
145 
146  return llvm::Error::success();
147 }
148 
151 
152 #if defined(_WIN32)
154 #endif
155 
157  HostInfo::Terminate();
160  Reproducer::Terminate();
161 }
lldb_private::ProcessWindowsLog::Initialize
static void Initialize()
Definition: ProcessWindowsLog.cpp:26
FileSystem.h
lldb_private::process_gdb_remote::ProcessGDBRemoteLog::Initialize
static void Initialize()
Definition: ProcessGDBRemoteLog.cpp:38
lldb_private::SystemInitializerCommon::m_shlib_dir_helper
HostInfo::SharedLibraryDirectoryHelper * m_shlib_dir_helper
Definition: SystemInitializerCommon.h:33
lldb_private::ProcessPOSIXLog::Initialize
static void Initialize()
Definition: ProcessPOSIXLog.cpp:27
Host.h
SystemInitializerCommon.h
lldb_private::FileSystem::SetHomeDirectory
void SetHomeDirectory(std::string home_directory)
Definition: common/FileSystem.cpp:513
lldb_private::Socket::Initialize
static llvm::Error Initialize()
Definition: Socket.cpp:79
lldb_private::repro::Loader
Definition: Reproducer.h:158
lldb_private::repro::FileProvider
Definition: ReproducerProvider.h:111
ProcessWindowsLog.h
ReproducerProvider.h
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::SystemInitializerCommon::Initialize
llvm::Error Initialize() override
Definition: SystemInitializerCommon.cpp:95
lldb_private::FileSystem::Terminate
static void Terminate()
Definition: common/FileSystem.cpp:78
Log.h
lldb_private::Log::DisableAllLogChannels
static void DisableAllLogChannels()
Definition: Log.cpp:244
lldb_private::Log::Initialize
static void Initialize()
Definition: Log.cpp:182
Socket.h
Timer.h
ProcessPOSIXLog.h
LLDB_SCOPED_TIMER
#define LLDB_SCOPED_TIMER()
Definition: Timer.h:83
lldb_private::repro::FileProvider::Info
Definition: ReproducerProvider.h:113
lldb_private::repro::VersionProvider
Provider for the LLDB version number.
Definition: ReproducerProvider.h:144
lldb-private.h
lldb_private::SystemInitializerCommon::~SystemInitializerCommon
~SystemInitializerCommon() override
lldb_private::FileSystem::Initialize
static void Initialize()
Definition: common/FileSystem.cpp:47
lldb_private::repro::VersionProvider::SetVersion
void SetVersion(std::string version)
Definition: ReproducerProvider.h:151
lldb_private::ProcessWindowsLog::Terminate
static void Terminate()
Definition: ProcessWindowsLog.cpp:31
lldb_private::Socket::Terminate
static void Terminate()
Definition: Socket.cpp:98
lldb_private::SystemInitializerCommon::SystemInitializerCommon
SystemInitializerCommon(HostInfo::SharedLibraryDirectoryHelper *helper)
Definition: SystemInitializerCommon.cpp:37
lldb_private::repro
Definition: SBReproducer.h:15
InitializeFileSystem
static llvm::Error InitializeFileSystem()
Initialize the FileSystem based on the current reproducer mode.
Definition: SystemInitializerCommon.cpp:44
windows.h
ProcessGDBRemoteLog.h
lldb_private::FileSystem::Instance
static FileSystem & Instance()
Definition: common/FileSystem.cpp:45
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
lldb_private::SystemInitializerCommon::Terminate
void Terminate() override
Definition: SystemInitializerCommon.cpp:149
fp
@ fp
Definition: CompactUnwindInfo.cpp:1247
lldb_private::repro::HomeDirectoryProvider
Provider for the home directory.
Definition: ReproducerProvider.h:207
lldb_private::repro::Reproducer::Instance
static Reproducer & Instance()
Definition: Reproducer.cpp:23
lldb_private::repro::Generator
The generator is responsible for the logic needed to generate a reproducer.
Definition: Reproducer.h:91
lldb_private::repro::WorkingDirectoryProvider
Provider for the current working directory.
Definition: ReproducerProvider.h:186
lldb_private::repro::DirectoryProvider::GetDirectory
llvm::StringRef GetDirectory()
Definition: ReproducerProvider.h:167