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"
39 #include "MICmnConfig.h"
40 #include "MICmnResources.h"
41 #include "MICmnStreamStdin.h"
43 #include "MIDriverMgr.h"
44 #include "MIUtilDebug.h"
45 #include "Platform.h" // Define signals - CODETAG_IOR_SIGNALS
49 once : 4530) // Warning C4530: C++ exception handler used, but unwind
50 // semantics are not enabled. Specify /EHsc
53 // CODETAG_IOR_SIGNALS
55 //------------------------------------------------------------------------------------
56 // Details: The SIGINT signal is sent to a process by its controlling terminal
58 // user wishes to interrupt the process. This is typically initiated by
60 // Control-C, but on some systems, the "delete" character or "break"
63 // Be aware this function may be called on another thread besides the
66 // Args: vSigno - (R) Signal number.
70 void sigint_handler(int vSigno) {
71 #ifdef _WIN32 // Restore handler as it is not persistent on Windows
72 signal(SIGINT, sigint_handler);
74 static bool g_interrupt_sent = false;
75 CMIDriverMgr &rDriverMgr = CMIDriverMgr::Instance();
76 lldb::SBDebugger *pDebugger = rDriverMgr.DriverGetTheDebugger();
77 if (pDebugger != nullptr) {
78 if (!g_interrupt_sent) {
79 g_interrupt_sent = true;
80 pDebugger->DispatchInputInterrupt();
81 g_interrupt_sent = false;
85 // Send signal to driver so that it can take suitable action
86 rDriverMgr.DeliverSignal(vSigno);
90 //------------------------------------------------------------------------------------
91 // Details: Init the MI driver system. Initialize the whole driver system which
93 // both the original LLDB driver and the MI driver.
96 // Return: MIstatus::success - Functional succeeded.
97 // MIstatus::failure - Functional failed.
100 bool DriverSystemInit() {
101 bool bOk = MIstatus::success;
102 CMIDriver &rMIDriver = CMIDriver::Instance();
103 CMIDriverMgr &rDriverMgr = CMIDriverMgr::Instance();
104 bOk = rDriverMgr.Initialize();
106 // Register MIDriver first as it needs to initialize and be ready
107 // for the Driver to get information from MIDriver when it initializes
108 // (LLDB Driver is registered with the Driver Manager in MI's Initialize())
110 rDriverMgr.RegisterDriver(rMIDriver, "MIDriver"); // Will be main driver
116 //------------------------------------------------------------------------------------
117 // Details: Shutdown the debugger system. Release / terminate resources external
119 // specifically the MI driver.
121 // Args: vbAppExitOk - (R) True = No problems, false = App exiting with
122 // problems (investigate!).
123 // Return: MIstatus::success - Functional succeeded.
124 // MIstatus::failure - Functional failed.
127 bool DriverSystemShutdown(const bool vbAppExitOk) {
128 bool bOk = MIstatus::success;
130 // *** Order is important here ***
131 CMIDriverMgr::Instance().Shutdown();
136 //------------------------------------------------------------------------------------
137 // Details: MI's application start point of execution. The application runs in
139 // An LLDB native driver mode where it acts no different from the LLDB
141 // The other mode is the MI when it finds on the command line
142 // the --interpreter option. Command line argument --help on its own
144 // help for the LLDB driver. If entered with --interpreter then
146 // help will provided.
148 // Args: argc - (R) An integer that contains the count of arguments that
150 // argv. The argc parameter is always greater than or
152 // argv - (R) An array of null-terminated strings representing
154 // arguments entered by the user of the program. By
156 // argv[0] is the command with which the program is
158 // Return: int - 0 = Normal exit, program success.
159 // >0 = Program success with status i.e. Control-C signal
161 // <0 = Program failed.
162 // -1 = Program failed reason not specified here, see MI log
164 // -1000 = Program failed did not initialize successfully.
167 int main(int argc, char const *argv[]) {
168 #if MICONFIG_DEBUG_SHOW_ATTACH_DBG_DLG
170 CMIUtilDebug::ShowDlgWaitForDbgAttach();
172 CMIUtilDebug::WaitForDbgAttachInfinteLoop();
174 #endif // MICONFIG_DEBUG_SHOW_ATTACH_DBG_DLG
176 // *** Order is important here ***
177 bool bOk = DriverSystemInit();
179 DriverSystemShutdown(bOk);
183 // CODETAG_IOR_SIGNALS
184 signal(SIGINT, sigint_handler);
186 bool bExiting = false;
187 CMIDriverMgr &rDriverMgr = CMIDriverMgr::Instance();
188 bOk = bOk && rDriverMgr.ParseArgs(argc, argv, bExiting);
189 if (bOk && !bExiting)
190 bOk = rDriverMgr.DriverParseArgs(argc, argv, stdout, bExiting);
191 if (bOk && !bExiting)
192 bOk = rDriverMgr.DriverMainLoop();
194 // Logger and other resources shutdown now
195 DriverSystemShutdown(bOk);
197 const int appResult = bOk ? 0 : -1;