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