1 //===-- SystemInitializerFull.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/API/SystemInitializerFull.h"
11 
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/Timer.h"
14 #include "lldb/Host/Host.h"
15 #include "lldb/Initialization/SystemInitializerCommon.h"
16 
17 #include "Plugins/ABI/MacOSX-i386/ABIMacOSX_i386.h"
18 #include "Plugins/ABI/MacOSX-arm/ABIMacOSX_arm.h"
19 #include "Plugins/ABI/MacOSX-arm64/ABIMacOSX_arm64.h"
20 #include "Plugins/ABI/SysV-arm/ABISysV_arm.h"
21 #include "Plugins/ABI/SysV-arm64/ABISysV_arm64.h"
22 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
23 #include "Plugins/ABI/SysV-ppc/ABISysV_ppc.h"
24 #include "Plugins/ABI/SysV-ppc64/ABISysV_ppc64.h"
25 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
26 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
27 #include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h"
28 #include "Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h"
29 #include "Plugins/JITLoader/GDB/JITLoaderGDB.h"
30 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
31 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
32 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
33 #include "Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.h"
34 #include "Plugins/MemoryHistory/asan/MemoryHistoryASan.h"
35 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
36 #include "Plugins/Process/elf-core/ProcessElfCore.h"
37 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
38 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
39 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
40 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
41 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
42 #include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
43 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
44 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
45 
46 #if defined(__APPLE__)
47 #include "Plugins/Process/mach-core/ProcessMachCore.h"
48 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
49 #include "Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.h"
50 #endif
51 
52 #if defined(__FreeBSD__)
53 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
54 #endif
55 
56 #if defined(__linux__)
57 #include "Plugins/Process/Linux/ProcessLinux.h"
58 #endif
59 
60 #if defined(_MSC_VER)
61 #include "lldb/Host/windows/windows.h"
62 #include "Plugins/Process/Windows/DynamicLoaderWindows.h"
63 #include "Plugins/Process/Windows/ProcessWindows.h"
64 #endif
65 
66 #if !defined(LLDB_DISABLE_PYTHON)
67 #include "lldb/Interpreter/ScriptInterpreterPython.h"
68 #endif
69 
70 #include "llvm/Support/TargetSelect.h"
71 
72 #include <string>
73 
74 using namespace lldb_private;
75 
76 #ifndef LLDB_DISABLE_PYTHON
77 
78 // Defined in the SWIG source file
79 extern "C" void
80 init_lldb(void);
81 
82 // these are the Pythonic implementations of the required callbacks
83 // these are scripting-language specific, which is why they belong here
84 // we still need to use function pointers to them instead of relying
85 // on linkage-time resolution because the SWIG stuff and this file
86 // get built at different times
87 extern "C" bool
88 LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name,
89                                           const char *session_dictionary_name,
90                                           const lldb::StackFrameSP& sb_frame,
91                                           const lldb::BreakpointLocationSP& sb_bp_loc);
92 
93 extern "C" bool
94 LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name,
95                                           const char *session_dictionary_name,
96                                           const lldb::StackFrameSP& sb_frame,
97                                           const lldb::WatchpointSP& sb_wp);
98 
99 extern "C" bool
100 LLDBSwigPythonCallTypeScript (const char *python_function_name,
101                               void *session_dictionary,
102                               const lldb::ValueObjectSP& valobj_sp,
103                               void** pyfunct_wrapper,
104                               const lldb::TypeSummaryOptionsSP& options_sp,
105                               std::string& retval);
106 
107 extern "C" void*
108 LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name,
109                                        const char *session_dictionary_name,
110                                        const lldb::ValueObjectSP& valobj_sp);
111 
112 extern "C" void*
113 LLDBSwigPythonCreateCommandObject (const char *python_class_name,
114                                    const char *session_dictionary_name,
115                                    const lldb::DebuggerSP debugger_sp);
116 
117 extern "C" void*
118 LLDBSwigPythonCreateScriptedThreadPlan (const char *python_class_name,
119                                         const char *session_dictionary_name,
120                                         const lldb::ThreadPlanSP& thread_plan_sp);
121 
122 extern "C" bool
123 LLDBSWIGPythonCallThreadPlan (void *implementor,
124                               const char *method_name,
125                               Event *event_sp,
126                               bool &got_error);
127 
128 extern "C" size_t
129 LLDBSwigPython_CalculateNumChildren (void *implementor);
130 
131 extern "C" void *
132 LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
133 
134 extern "C" int
135 LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
136 
137 extern "C" void *
138 LLDBSWIGPython_CastPyObjectToSBValue (void* data);
139 
140 extern lldb::ValueObjectSP
141 LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data);
142 
143 extern "C" bool
144 LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
145 
146 extern "C" bool
147 LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor);
148 
149 extern "C" void *
150 LLDBSwigPython_GetValueSynthProviderInstance (void* implementor);
151 
152 extern "C" bool
153 LLDBSwigPythonCallCommand (const char *python_function_name,
154                            const char *session_dictionary_name,
155                            lldb::DebuggerSP& debugger,
156                            const char* args,
157                            lldb_private::CommandReturnObject &cmd_retobj,
158                            lldb::ExecutionContextRefSP exe_ctx_ref_sp);
159 
160 extern "C" bool
161 LLDBSwigPythonCallCommandObject (void *implementor,
162                                  lldb::DebuggerSP& debugger,
163                                  const char* args,
164                                  lldb_private::CommandReturnObject& cmd_retobj,
165                                  lldb::ExecutionContextRefSP exe_ctx_ref_sp);
166 
167 extern "C" bool
168 LLDBSwigPythonCallModuleInit (const char *python_module_name,
169                               const char *session_dictionary_name,
170                               lldb::DebuggerSP& debugger);
171 
172 extern "C" void*
173 LLDBSWIGPythonCreateOSPlugin (const char *python_class_name,
174                               const char *session_dictionary_name,
175                               const lldb::ProcessSP& process_sp);
176 
177 extern "C" bool
178 LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name,
179                                        const char* session_dictionary_name,
180                                        lldb::ProcessSP& process,
181                                        std::string& output);
182 
183 extern "C" bool
184 LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name,
185                                       const char* session_dictionary_name,
186                                       lldb::ThreadSP& thread,
187                                       std::string& output);
188 
189 extern "C" bool
190 LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name,
191                                       const char* session_dictionary_name,
192                                       lldb::TargetSP& target,
193                                       std::string& output);
194 
195 extern "C" bool
196 LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name,
197                                      const char* session_dictionary_name,
198                                      lldb::StackFrameSP& frame,
199                                      std::string& output);
200 
201 extern "C" bool
202 LLDBSWIGPythonRunScriptKeywordValue (const char* python_function_name,
203                                      const char* session_dictionary_name,
204                                      lldb::ValueObjectSP& value,
205                                      std::string& output);
206 
207 extern "C" void*
208 LLDBSWIGPython_GetDynamicSetting (void* module,
209                                   const char* setting,
210                                   const lldb::TargetSP& target_sp);
211 
212 
213 #endif
214 
215 SystemInitializerFull::SystemInitializerFull()
216 {
217 }
218 
219 SystemInitializerFull::~SystemInitializerFull()
220 {
221 }
222 
223 void
224 SystemInitializerFull::Initialize()
225 {
226     InitializeSWIG();
227 
228     SystemInitializerCommon::Initialize();
229 
230     // Initialize LLVM and Clang
231     llvm::InitializeAllTargets();
232     llvm::InitializeAllAsmPrinters();
233     llvm::InitializeAllTargetMCs();
234     llvm::InitializeAllDisassemblers();
235 
236     ABIMacOSX_i386::Initialize();
237     ABIMacOSX_arm::Initialize();
238     ABIMacOSX_arm64::Initialize();
239     ABISysV_arm::Initialize();
240     ABISysV_arm64::Initialize();
241     ABISysV_x86_64::Initialize();
242     ABISysV_ppc::Initialize();
243     ABISysV_ppc64::Initialize();
244     DisassemblerLLVMC::Initialize();
245 
246     JITLoaderGDB::Initialize();
247     ProcessElfCore::Initialize();
248     MemoryHistoryASan::Initialize();
249     AddressSanitizerRuntime::Initialize();
250 
251     SymbolVendorELF::Initialize();
252     SymbolFileDWARF::Initialize();
253     SymbolFileSymtab::Initialize();
254     UnwindAssemblyInstEmulation::Initialize();
255     UnwindAssembly_x86::Initialize();
256     EmulateInstructionARM64::Initialize();
257     SymbolFileDWARFDebugMap::Initialize();
258     ItaniumABILanguageRuntime::Initialize();
259     AppleObjCRuntimeV2::Initialize();
260     AppleObjCRuntimeV1::Initialize();
261     SystemRuntimeMacOSX::Initialize();
262     RenderScriptRuntime::Initialize();
263 
264 #if defined(__linux__)
265     //----------------------------------------------------------------------
266     // Linux hosted plugins
267     //----------------------------------------------------------------------
268     process_linux::ProcessLinux::Initialize();
269 #endif
270 #if defined(_MSC_VER)
271     DynamicLoaderWindows::Initialize();
272     ProcessWindows::Initialize();
273 #endif
274 #if defined(__FreeBSD__)
275     ProcessFreeBSD::Initialize();
276 #endif
277 #if defined(__APPLE__)
278     SymbolVendorMacOSX::Initialize();
279     ProcessKDP::Initialize();
280     ProcessMachCore::Initialize();
281 #endif
282     //----------------------------------------------------------------------
283     // Platform agnostic plugins
284     //----------------------------------------------------------------------
285     platform_gdb_server::PlatformRemoteGDBServer::Initialize();
286 
287     process_gdb_remote::ProcessGDBRemote::Initialize();
288     DynamicLoaderStatic::Initialize();
289 
290     // Scan for any system or user LLDB plug-ins
291     PluginManager::Initialize();
292 
293     // The process settings need to know about installed plug-ins, so the Settings must be initialized
294     // AFTER PluginManager::Initialize is called.
295 
296     Debugger::SettingsInitialize();
297 }
298 
299 void SystemInitializerFull::InitializeSWIG()
300 {
301 #if !defined(LLDB_DISABLE_PYTHON)
302     ScriptInterpreterPython::InitializeInterpreter(
303         init_lldb,
304         LLDBSwigPythonBreakpointCallbackFunction,
305         LLDBSwigPythonWatchpointCallbackFunction,
306         LLDBSwigPythonCallTypeScript,
307         LLDBSwigPythonCreateSyntheticProvider,
308         LLDBSwigPythonCreateCommandObject,
309         LLDBSwigPython_CalculateNumChildren,
310         LLDBSwigPython_GetChildAtIndex,
311         LLDBSwigPython_GetIndexOfChildWithName,
312         LLDBSWIGPython_CastPyObjectToSBValue,
313         LLDBSWIGPython_GetValueObjectSPFromSBValue,
314         LLDBSwigPython_UpdateSynthProviderInstance,
315         LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
316         LLDBSwigPython_GetValueSynthProviderInstance,
317         LLDBSwigPythonCallCommand,
318         LLDBSwigPythonCallCommandObject,
319         LLDBSwigPythonCallModuleInit,
320         LLDBSWIGPythonCreateOSPlugin,
321         LLDBSWIGPythonRunScriptKeywordProcess,
322         LLDBSWIGPythonRunScriptKeywordThread,
323         LLDBSWIGPythonRunScriptKeywordTarget,
324         LLDBSWIGPythonRunScriptKeywordFrame,
325         LLDBSWIGPythonRunScriptKeywordValue,
326         LLDBSWIGPython_GetDynamicSetting,
327         LLDBSwigPythonCreateScriptedThreadPlan,
328         LLDBSWIGPythonCallThreadPlan);
329 #endif
330 }
331 
332 void
333 SystemInitializerFull::Terminate()
334 {
335     Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
336 
337     Debugger::SettingsTerminate();
338 
339     // Terminate and unload and loaded system or user LLDB plug-ins
340     PluginManager::Terminate();
341     ABIMacOSX_i386::Terminate();
342     ABIMacOSX_arm::Terminate();
343     ABIMacOSX_arm64::Terminate();
344     ABISysV_arm::Terminate();
345     ABISysV_arm64::Terminate();
346     ABISysV_x86_64::Terminate();
347     ABISysV_ppc::Terminate();
348     ABISysV_ppc64::Terminate();
349     DisassemblerLLVMC::Terminate();
350 
351     JITLoaderGDB::Terminate();
352     ProcessElfCore::Terminate();
353     MemoryHistoryASan::Terminate();
354     AddressSanitizerRuntime::Terminate();
355     SymbolVendorELF::Terminate();
356     SymbolFileDWARF::Terminate();
357     SymbolFileSymtab::Terminate();
358     UnwindAssembly_x86::Terminate();
359     UnwindAssemblyInstEmulation::Terminate();
360     EmulateInstructionARM64::Terminate();
361     SymbolFileDWARFDebugMap::Terminate();
362     ItaniumABILanguageRuntime::Terminate();
363     AppleObjCRuntimeV2::Terminate();
364     AppleObjCRuntimeV1::Terminate();
365     SystemRuntimeMacOSX::Terminate();
366     RenderScriptRuntime::Terminate();
367 
368 #if defined(__APPLE__)
369     ProcessMachCore::Terminate();
370     ProcessKDP::Terminate();
371     SymbolVendorMacOSX::Terminate();
372 #endif
373 #if defined(_MSC_VER)
374     DynamicLoaderWindows::Terminate();
375 #endif
376 
377 #if defined(__linux__)
378     process_linux::ProcessLinux::Terminate();
379 #endif
380 
381 #if defined(__FreeBSD__)
382     ProcessFreeBSD::Terminate();
383 #endif
384     Debugger::SettingsTerminate();
385 
386     platform_gdb_server::PlatformRemoteGDBServer::Terminate();
387     process_gdb_remote::ProcessGDBRemote::Terminate();
388     DynamicLoaderStatic::Terminate();
389 
390     // Now shutdown the common parts, in reverse order.
391     SystemInitializerCommon::Terminate();
392 }
393 
394 void SystemInitializerFull::TerminateSWIG()
395 {
396 
397 }
398