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