]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MIDriverMain.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r308421, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-mi / MIDriverMain.cpp
1 //===-- MIDriverMain.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 // 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
13 //              LLDB driver.
14 //              The other mode is the MI when it finds on the command line
15 //              the --interpreter option. Command line argument --help on its
16 //              own will give
17 //              help for the LLDB driver. If entered with --interpreter then MI
18 //              help will
19 //              provided.
20 //              To implement new MI commands derive a new command class from the
21 //              command base
22 //              class. To enable the new command for interpretation add the new
23 //              command class
24 //              to the command factory. The files of relevance are:
25 //                  MICmdCommands.cpp
26 //                  MICmdBase.h / .cpp
27 //                  MICmdCmd.h / .cpp
28
29 #if defined(_MSC_VER)
30 #define _INC_SIGNAL // Stop window's signal.h being included -
31                     // CODETAG_IOR_SIGNALS
32 #endif              // _MSC_VER
33
34 // Third party headers:
35 #include "lldb/API/SBHostOS.h"
36 #include <csignal>
37 #include <stdio.h>
38
39 // In house headers:
40 #include "MICmnConfig.h"
41 #include "MICmnResources.h"
42 #include "MICmnStreamStdin.h"
43 #include "MIDriver.h"
44 #include "MIDriverMgr.h"
45 #include "MIUtilDebug.h"
46 #include "Platform.h" // Define signals - CODETAG_IOR_SIGNALS
47
48 #if defined(_MSC_VER)
49 #pragma warning(                                                               \
50     once : 4530) // Warning C4530: C++ exception handler used, but unwind
51                  // semantics are not enabled. Specify /EHsc
52 #endif           // _MSC_VER
53
54 // CODETAG_IOR_SIGNALS
55 //++
56 //------------------------------------------------------------------------------------
57 // Details: The SIGINT signal is sent to a process by its controlling terminal
58 // when a
59 //          user wishes to interrupt the process. This is typically initiated by
60 //          pressing
61 //          Control-C, but on some systems, the "delete" character or "break"
62 //          key can be
63 //          used.
64 //          Be aware this function may be called on another thread besides the
65 //          main thread.
66 // Type:    Function.
67 // Args:    vSigno  - (R) Signal number.
68 // Return:  None.
69 // Throws:  None.
70 //--
71 void sigint_handler(int vSigno) {
72 #ifdef _WIN32 // Restore handler as it is not persistent on Windows
73   signal(SIGINT, sigint_handler);
74 #endif
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;
83     }
84   }
85
86   // Send signal to driver so that it can take suitable action
87   rDriverMgr.DeliverSignal(vSigno);
88 }
89
90 //++
91 //------------------------------------------------------------------------------------
92 // Details: Init the MI driver system. Initialize the whole driver system which
93 // includes
94 //          both the original LLDB driver and the MI driver.
95 // Type:    Function.
96 // Args:    None.
97 // Return:  MIstatus::success - Functional succeeded.
98 //          MIstatus::failure - Functional failed.
99 // Throws:  None.
100 //--
101 bool DriverSystemInit() {
102   bool bOk = MIstatus::success;
103   CMIDriver &rMIDriver = CMIDriver::Instance();
104   CMIDriverMgr &rDriverMgr = CMIDriverMgr::Instance();
105   bOk = rDriverMgr.Initialize();
106
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())
110   bOk = bOk &&
111         rDriverMgr.RegisterDriver(rMIDriver, "MIDriver"); // Will be main driver
112
113   return bOk;
114 }
115
116 //++
117 //------------------------------------------------------------------------------------
118 // Details: Shutdown the debugger system. Release / terminate resources external
119 // to
120 //          specifically the MI driver.
121 // Type:    Function.
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.
126 // Throws:  None.
127 //--
128 bool DriverSystemShutdown(const bool vbAppExitOk) {
129   bool bOk = MIstatus::success;
130
131   // *** Order is important here ***
132   CMIDriverMgr::Instance().Shutdown();
133   return bOk;
134 }
135
136 //++
137 //------------------------------------------------------------------------------------
138 // Details: MI's application start point of execution. The application runs in
139 // two modes.
140 //          An LLDB native driver mode where it acts no different from the LLDB
141 //          driver.
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
144 //          will give
145 //          help for the LLDB driver. If entered with --interpreter then
146 //          application
147 //          help will provided.
148 // Type:    Method.
149 // Args:    argc    - (R) An integer that contains the count of arguments that
150 // follow in
151 //                        argv. The argc parameter is always greater than or
152 //                        equal to 1.
153 //          argv    - (R) An array of null-terminated strings representing
154 //          command-line
155 //                        arguments entered by the user of the program. By
156 //                        convention,
157 //                        argv[0] is the command with which the program is
158 //                        invoked.
159 // Return:  int -  0 =   Normal exit, program success.
160 //                >0    = Program success with status i.e. Control-C signal
161 //                status
162 //                <0    = Program failed.
163 //              -1      = Program failed reason not specified here, see MI log
164 //              file.
165 //              -1000   = Program failed did not initialize successfully.
166 // Throws:  None.
167 //--
168 int main(int argc, char const *argv[]) {
169 #if MICONFIG_DEBUG_SHOW_ATTACH_DBG_DLG
170 #ifdef _WIN32
171   CMIUtilDebug::ShowDlgWaitForDbgAttach();
172 #else
173   CMIUtilDebug::WaitForDbgAttachInfinteLoop();
174 #endif //  _WIN32
175 #endif // MICONFIG_DEBUG_SHOW_ATTACH_DBG_DLG
176
177   // *** Order is important here ***
178   bool bOk = DriverSystemInit();
179   if (!bOk) {
180     DriverSystemShutdown(bOk);
181     return -1000;
182   }
183
184   // CODETAG_IOR_SIGNALS
185   signal(SIGINT, sigint_handler);
186
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();
194
195   // Logger and other resources shutdown now
196   DriverSystemShutdown(bOk);
197
198   const int appResult = bOk ? 0 : -1;
199
200   return appResult;
201 }