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