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