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