1 //===-- MIDriverMain.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Overview: Defines the entry point for the console application.
11 // The MI application (project name MI) runs in two modes:
12 // An LLDB native driver mode where it acts no different from the
14 // The other mode is the MI when it finds on the command line
15 // the --interpreter option. Command line argument --help on its
17 // help for the LLDB driver. If entered with --interpreter then MI
20 // To implement new MI commands derive a new command class from the
22 // class. To enable the new command for interpretation add the new
24 // to the command factory. The files of relevance are:
30 #define _INC_SIGNAL // Stop window's signal.h being included -
31 // CODETAG_IOR_SIGNALS
34 // Third party headers:
35 #include "lldb/API/SBHostOS.h"
40 #include "MICmnConfig.h"
41 #include "MICmnResources.h"
42 #include "MICmnStreamStdin.h"
44 #include "MIDriverMgr.h"
45 #include "MIUtilDebug.h"
46 #include "Platform.h" // Define signals - CODETAG_IOR_SIGNALS
50 once : 4530) // Warning C4530: C++ exception handler used, but unwind
51 // semantics are not enabled. Specify /EHsc
54 // CODETAG_IOR_SIGNALS
56 //------------------------------------------------------------------------------------
57 // Details: The SIGINT signal is sent to a process by its controlling terminal
59 // user wishes to interrupt the process. This is typically initiated by
61 // Control-C, but on some systems, the "delete" character or "break"
64 // Be aware this function may be called on another thread besides the
67 // Args: vSigno - (R) Signal number.
71 void sigint_handler(int vSigno) {
72 #ifdef _WIN32 // Restore handler as it is not persistent on Windows
73 signal(SIGINT, sigint_handler);
75 static bool g_interrupt_sent = false;
76 CMIDriverMgr &rDriverMgr = CMIDriverMgr::Instance();
77 lldb::SBDebugger *pDebugger = rDriverMgr.DriverGetTheDebugger();
78 if (pDebugger != nullptr) {
79 if (!g_interrupt_sent) {
80 g_interrupt_sent = true;
81 pDebugger->DispatchInputInterrupt();
82 g_interrupt_sent = false;
86 // Send signal to driver so that it can take suitable action
87 rDriverMgr.DeliverSignal(vSigno);
91 //------------------------------------------------------------------------------------
92 // Details: Init the MI driver system. Initialize the whole driver system which
94 // both the original LLDB driver and the MI driver.
97 // Return: MIstatus::success - Functional succeeded.
98 // MIstatus::failure - Functional failed.
101 bool DriverSystemInit() {
102 bool bOk = MIstatus::success;
103 CMIDriver &rMIDriver = CMIDriver::Instance();
104 CMIDriverMgr &rDriverMgr = CMIDriverMgr::Instance();
105 bOk = rDriverMgr.Initialize();
107 // Register MIDriver first as it needs to initialize and be ready
108 // for the Driver to get information from MIDriver when it initializes
109 // (LLDB Driver is registered with the Driver Manager in MI's Initialize())
111 rDriverMgr.RegisterDriver(rMIDriver, "MIDriver"); // Will be main driver
117 //------------------------------------------------------------------------------------
118 // Details: Shutdown the debugger system. Release / terminate resources external
120 // specifically the MI driver.
122 // Args: vbAppExitOk - (R) True = No problems, false = App exiting with
123 // problems (investigate!).
124 // Return: MIstatus::success - Functional succeeded.
125 // MIstatus::failure - Functional failed.
128 bool DriverSystemShutdown(const bool vbAppExitOk) {
129 bool bOk = MIstatus::success;
131 // *** Order is important here ***
132 CMIDriverMgr::Instance().Shutdown();
137 //------------------------------------------------------------------------------------
138 // Details: MI's application start point of execution. The application runs in
140 // An LLDB native driver mode where it acts no different from the LLDB
142 // The other mode is the MI when it finds on the command line
143 // the --interpreter option. Command line argument --help on its own
145 // help for the LLDB driver. If entered with --interpreter then
147 // help will provided.
149 // Args: argc - (R) An integer that contains the count of arguments that
151 // argv. The argc parameter is always greater than or
153 // argv - (R) An array of null-terminated strings representing
155 // arguments entered by the user of the program. By
157 // argv[0] is the command with which the program is
159 // Return: int - 0 = Normal exit, program success.
160 // >0 = Program success with status i.e. Control-C signal
162 // <0 = Program failed.
163 // -1 = Program failed reason not specified here, see MI log
165 // -1000 = Program failed did not initialize successfully.
168 int main(int argc, char const *argv[]) {
169 #if MICONFIG_DEBUG_SHOW_ATTACH_DBG_DLG
171 CMIUtilDebug::ShowDlgWaitForDbgAttach();
173 CMIUtilDebug::WaitForDbgAttachInfinteLoop();
175 #endif // MICONFIG_DEBUG_SHOW_ATTACH_DBG_DLG
177 // *** Order is important here ***
178 bool bOk = DriverSystemInit();
180 DriverSystemShutdown(bOk);
184 // CODETAG_IOR_SIGNALS
185 signal(SIGINT, sigint_handler);
187 bool bExiting = false;
188 CMIDriverMgr &rDriverMgr = CMIDriverMgr::Instance();
189 bOk = bOk && rDriverMgr.ParseArgs(argc, argv, bExiting);
190 if (bOk && !bExiting)
191 bOk = rDriverMgr.DriverParseArgs(argc, argv, stdout, bExiting);
192 if (bOk && !bExiting)
193 bOk = rDriverMgr.DriverMainLoop();
195 // Logger and other resources shutdown now
196 DriverSystemShutdown(bOk);
198 const int appResult = bOk ? 0 : -1;