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