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