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