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-x86_64/ABISysV_x86_64.h"
21 #include "Plugins/ABI/SysV-ppc/ABISysV_ppc.h"
22 #include "Plugins/ABI/SysV-ppc64/ABISysV_ppc64.h"
23 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
24 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
25 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
26 #include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h"
27 #include "Plugins/Instruction/MIPS64/EmulateInstructionMIPS64.h"
28 #include "Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.h"
29 #include "Plugins/JITLoader/GDB/JITLoaderGDB.h"
30 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
31 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
32 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
33 #include "Plugins/MemoryHistory/asan/MemoryHistoryASan.h"
34 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
35 #include "Plugins/Process/elf-core/ProcessElfCore.h"
36 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
37 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
38 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
39 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
40 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
41 #include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
42 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
43 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
44 
45 #if defined(__APPLE__)
46 #include "Plugins/Process/mach-core/ProcessMachCore.h"
47 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
48 #include "Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.h"
49 #endif
50 
51 #if defined(__FreeBSD__)
52 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
53 #endif
54 
55 #if defined(__linux__)
56 #include "Plugins/Process/Linux/ProcessLinux.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_x86_64::Initialize();
239     ABISysV_ppc::Initialize();
240     ABISysV_ppc64::Initialize();
241     DisassemblerLLVMC::Initialize();
242 
243     JITLoaderGDB::Initialize();
244     ProcessElfCore::Initialize();
245     MemoryHistoryASan::Initialize();
246     AddressSanitizerRuntime::Initialize();
247 
248     SymbolVendorELF::Initialize();
249     SymbolFileDWARF::Initialize();
250     SymbolFileSymtab::Initialize();
251     UnwindAssemblyInstEmulation::Initialize();
252     UnwindAssembly_x86::Initialize();
253     EmulateInstructionARM::Initialize();
254     EmulateInstructionARM64::Initialize();
255     EmulateInstructionMIPS64::Initialize();
256     SymbolFileDWARFDebugMap::Initialize();
257     ItaniumABILanguageRuntime::Initialize();
258     AppleObjCRuntimeV2::Initialize();
259     AppleObjCRuntimeV1::Initialize();
260     SystemRuntimeMacOSX::Initialize();
261 
262 #if defined(__linux__)
263     //----------------------------------------------------------------------
264     // Linux hosted plugins
265     //----------------------------------------------------------------------
266     process_linux::ProcessLinux::Initialize();
267 #endif
268 #if defined(_MSC_VER)
269     DynamicLoaderWindows::Initialize();
270     ProcessWindows::Initialize();
271 #endif
272 #if defined(__FreeBSD__)
273     ProcessFreeBSD::Initialize();
274 #endif
275 #if defined(__APPLE__)
276     SymbolVendorMacOSX::Initialize();
277     ProcessKDP::Initialize();
278     ProcessMachCore::Initialize();
279 #endif
280     //----------------------------------------------------------------------
281     // Platform agnostic plugins
282     //----------------------------------------------------------------------
283     platform_gdb_server::PlatformRemoteGDBServer::Initialize();
284 
285     process_gdb_remote::ProcessGDBRemote::Initialize();
286     DynamicLoaderStatic::Initialize();
287 
288     // Scan for any system or user LLDB plug-ins
289     PluginManager::Initialize();
290 
291     // The process settings need to know about installed plug-ins, so the Settings must be initialized
292     // AFTER PluginManager::Initialize is called.
293 
294     Debugger::SettingsInitialize();
295 }
296 
297 void SystemInitializerFull::InitializeSWIG()
298 {
299 #if !defined(LLDB_DISABLE_PYTHON)
300     ScriptInterpreterPython::InitializeInterpreter(
301         init_lldb,
302         LLDBSwigPythonBreakpointCallbackFunction,
303         LLDBSwigPythonWatchpointCallbackFunction,
304         LLDBSwigPythonCallTypeScript,
305         LLDBSwigPythonCreateSyntheticProvider,
306         LLDBSwigPythonCreateCommandObject,
307         LLDBSwigPython_CalculateNumChildren,
308         LLDBSwigPython_GetChildAtIndex,
309         LLDBSwigPython_GetIndexOfChildWithName,
310         LLDBSWIGPython_CastPyObjectToSBValue,
311         LLDBSWIGPython_GetValueObjectSPFromSBValue,
312         LLDBSwigPython_UpdateSynthProviderInstance,
313         LLDBSwigPython_MightHaveChildrenSynthProviderInstance,
314         LLDBSwigPython_GetValueSynthProviderInstance,
315         LLDBSwigPythonCallCommand,
316         LLDBSwigPythonCallCommandObject,
317         LLDBSwigPythonCallModuleInit,
318         LLDBSWIGPythonCreateOSPlugin,
319         LLDBSWIGPythonRunScriptKeywordProcess,
320         LLDBSWIGPythonRunScriptKeywordThread,
321         LLDBSWIGPythonRunScriptKeywordTarget,
322         LLDBSWIGPythonRunScriptKeywordFrame,
323         LLDBSWIGPythonRunScriptKeywordValue,
324         LLDBSWIGPython_GetDynamicSetting,
325         LLDBSwigPythonCreateScriptedThreadPlan,
326         LLDBSWIGPythonCallThreadPlan);
327 #endif
328 }
329 
330 void
331 SystemInitializerFull::Terminate()
332 {
333     Timer scoped_timer(__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
334 
335     Debugger::SettingsTerminate();
336 
337     // Terminate and unload and loaded system or user LLDB plug-ins
338     PluginManager::Terminate();
339     ABIMacOSX_i386::Terminate();
340     ABIMacOSX_arm::Terminate();
341     ABIMacOSX_arm64::Terminate();
342     ABISysV_x86_64::Terminate();
343     ABISysV_ppc::Terminate();
344     ABISysV_ppc64::Terminate();
345     DisassemblerLLVMC::Terminate();
346 
347     JITLoaderGDB::Terminate();
348     ProcessElfCore::Terminate();
349     MemoryHistoryASan::Terminate();
350     AddressSanitizerRuntime::Terminate();
351     SymbolVendorELF::Terminate();
352     SymbolFileDWARF::Terminate();
353     SymbolFileSymtab::Terminate();
354     UnwindAssembly_x86::Terminate();
355     UnwindAssemblyInstEmulation::Terminate();
356     EmulateInstructionARM::Terminate();
357     EmulateInstructionARM64::Terminate();
358     EmulateInstructionMIPS64::Terminate();
359     SymbolFileDWARFDebugMap::Terminate();
360     ItaniumABILanguageRuntime::Terminate();
361     AppleObjCRuntimeV2::Terminate();
362     AppleObjCRuntimeV1::Terminate();
363     SystemRuntimeMacOSX::Terminate();
364 
365 #if defined(__APPLE__)
366     ProcessMachCore::Terminate();
367     ProcessKDP::Terminate();
368     SymbolVendorMacOSX::Terminate();
369 #endif
370 #if defined(_MSC_VER)
371     DynamicLoaderWindows::Terminate();
372 #endif
373 
374 #if defined(__linux__)
375     process_linux::ProcessLinux::Terminate();
376 #endif
377 
378 #if defined(__FreeBSD__)
379     ProcessFreeBSD::Terminate();
380 #endif
381     Debugger::SettingsTerminate();
382 
383     platform_gdb_server::PlatformRemoteGDBServer::Terminate();
384     process_gdb_remote::ProcessGDBRemote::Terminate();
385     DynamicLoaderStatic::Terminate();
386 
387     // Now shutdown the common parts, in reverse order.
388     SystemInitializerCommon::Terminate();
389 }
390 
391 void SystemInitializerFull::TerminateSWIG()
392 {
393 
394 }
395