1 //===-- SystemInitializerCommon.cpp -----------------------------*- 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 #include "lldb/Initialization/SystemInitializerCommon.h"
11 
12 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
13 #include "Plugins/Instruction/MIPS/EmulateInstructionMIPS.h"
14 #include "Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.h"
15 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
16 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
17 #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
18 #include "lldb/Host/FileSystem.h"
19 #include "lldb/Host/Host.h"
20 #include "lldb/Host/HostInfo.h"
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/Reproducer.h"
23 #include "lldb/Utility/Timer.h"
24 
25 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
26 #include "Plugins/Process/POSIX/ProcessPOSIXLog.h"
27 #endif
28 
29 #if defined(_MSC_VER)
30 #include "Plugins/Process/Windows/Common/ProcessWindowsLog.h"
31 #include "lldb/Host/windows/windows.h"
32 #endif
33 
34 #include "llvm/Support/TargetSelect.h"
35 
36 #include <string>
37 
38 using namespace lldb_private;
39 using namespace lldb_private::repro;
40 
41 SystemInitializerCommon::SystemInitializerCommon() {}
42 
43 SystemInitializerCommon::~SystemInitializerCommon() {}
44 
45 llvm::Error
46 SystemInitializerCommon::Initialize(const InitializerOptions &options) {
47 #if defined(_MSC_VER)
48   const char *disable_crash_dialog_var = getenv("LLDB_DISABLE_CRASH_DIALOG");
49   if (disable_crash_dialog_var &&
50       llvm::StringRef(disable_crash_dialog_var).equals_lower("true")) {
51     // This will prevent Windows from displaying a dialog box requiring user
52     // interaction when
53     // LLDB crashes.  This is mostly useful when automating LLDB, for example
54     // via the test
55     // suite, so that a crash in LLDB does not prevent completion of the test
56     // suite.
57     ::SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS |
58                    SEM_NOGPFAULTERRORBOX);
59 
60     _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
61     _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
62     _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
63     _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
64     _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
65     _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
66   }
67 #endif
68 
69   ReproducerMode mode = ReproducerMode::Off;
70   if (options.reproducer_capture)
71     mode = ReproducerMode::Capture;
72   if (options.reproducer_replay)
73     mode = ReproducerMode::Replay;
74 
75   if (auto e = Reproducer::Initialize(mode, FileSpec(options.reproducer_path)))
76     return e;
77 
78   FileSystem::Initialize();
79   Log::Initialize();
80   HostInfo::Initialize();
81   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
82   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
83 
84   process_gdb_remote::ProcessGDBRemoteLog::Initialize();
85 
86   // Initialize plug-ins
87   ObjectContainerBSDArchive::Initialize();
88 
89   EmulateInstructionARM::Initialize();
90   EmulateInstructionMIPS::Initialize();
91   EmulateInstructionMIPS64::Initialize();
92 
93   //----------------------------------------------------------------------
94   // Apple/Darwin hosted plugins
95   //----------------------------------------------------------------------
96   ObjectContainerUniversalMachO::Initialize();
97 
98 #if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
99   ProcessPOSIXLog::Initialize();
100 #endif
101 #if defined(_MSC_VER)
102   ProcessWindowsLog::Initialize();
103 #endif
104 
105   return llvm::Error::success();
106 }
107 
108 void SystemInitializerCommon::Terminate() {
109   static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
110   Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
111   ObjectContainerBSDArchive::Terminate();
112 
113   EmulateInstructionARM::Terminate();
114   EmulateInstructionMIPS::Terminate();
115   EmulateInstructionMIPS64::Terminate();
116 
117   ObjectContainerUniversalMachO::Terminate();
118 
119 #if defined(_MSC_VER)
120   ProcessWindowsLog::Terminate();
121 #endif
122 
123   HostInfo::Terminate();
124   Log::DisableAllLogChannels();
125   FileSystem::Terminate();
126   Reproducer::Terminate();
127 }
128