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