]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/lldb.cpp
Update paths for FreeBSD build infrastructure
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / lldb.cpp
1 //===-- lldb.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/lldb-python.h"
11
12 #include "lldb/lldb-private.h"
13 #include "lldb/lldb-private-log.h"
14 #include "lldb/Core/ArchSpec.h"
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/Log.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/RegularExpression.h"
19 #include "lldb/Core/Timer.h"
20 #include "lldb/Host/Host.h"
21 #include "lldb/Host/HostInfo.h"
22 #include "lldb/Host/Mutex.h"
23 #include "lldb/Interpreter/ScriptInterpreterPython.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Support/TargetSelect.h"
29
30 #include "Plugins/ABI/MacOSX-arm64/ABIMacOSX_arm64.h"
31 #include "Plugins/ABI/SysV-x86_64/ABISysV_x86_64.h"
32 #include "Plugins/Disassembler/llvm/DisassemblerLLVMC.h"
33 #include "Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.h"
34 #include "Plugins/Instruction/ARM/EmulateInstructionARM.h"
35 #include "Plugins/Instruction/ARM64/EmulateInstructionARM64.h"
36 #include "Plugins/JITLoader/GDB/JITLoaderGDB.h"
37 #include "Plugins/LanguageRuntime/CPlusPlus/ItaniumABI/ItaniumABILanguageRuntime.h"
38 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
39 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
40 #include "Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h"
41 #include "Plugins/Platform/FreeBSD/PlatformFreeBSD.h"
42 #include "Plugins/Platform/Linux/PlatformLinux.h"
43 #include "Plugins/Platform/POSIX/PlatformPOSIX.h"
44 #include "Plugins/Platform/Windows/PlatformWindows.h"
45 #include "Plugins/Platform/Kalimba/PlatformKalimba.h"
46 #include "Plugins/Process/elf-core/ProcessElfCore.h"
47 #include "Plugins/SymbolVendor/MacOSX/SymbolVendorMacOSX.h"
48 #include "Plugins/SymbolVendor/ELF/SymbolVendorELF.h"
49 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h"
50 #include "Plugins/SymbolFile/DWARF/SymbolFileDWARFDebugMap.h"
51 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
52 #include "Plugins/UnwindAssembly/x86/UnwindAssembly-x86.h"
53 #include "Plugins/UnwindAssembly/InstEmulation/UnwindAssemblyInstEmulation.h"
54
55 #ifndef LLDB_DISABLE_PYTHON
56 #include "Plugins/OperatingSystem/Python/OperatingSystemPython.h"
57 #endif
58 #if defined (__APPLE__)
59 #include "Plugins/DynamicLoader/MacOSX-DYLD/DynamicLoaderMacOSXDYLD.h"
60 #include "Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.h"
61 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV1.h"
62 #include "Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.h"
63 #include "Plugins/ObjectContainer/Universal-Mach-O/ObjectContainerUniversalMachO.h"
64 #include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h"
65 #include "Plugins/Process/MacOSX-Kernel/ProcessKDP.h"
66 #include "Plugins/Platform/MacOSX/PlatformMacOSX.h"
67 #include "Plugins/Platform/MacOSX/PlatformRemoteiOS.h"
68 #include "Plugins/Platform/MacOSX/PlatformDarwinKernel.h"
69 #include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
70 #include "Plugins/SystemRuntime/MacOSX/SystemRuntimeMacOSX.h"
71 #endif
72
73
74 #if defined (__linux__)
75 #include "Plugins/Process/Linux/ProcessLinux.h"
76 #endif
77
78 #if defined (_WIN32)
79 #include "Plugins/Process/Windows/ProcessWindows.h"
80 #endif
81
82 #if defined (__FreeBSD__)
83 #include "Plugins/Process/POSIX/ProcessPOSIX.h"
84 #include "Plugins/Process/FreeBSD/ProcessFreeBSD.h"
85 #endif
86
87 #include "Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h"
88 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
89 #include "Plugins/DynamicLoader/Static/DynamicLoaderStatic.h"
90
91 using namespace lldb;
92 using namespace lldb_private;
93
94 static void fatal_error_handler(void *user_data, const std::string& reason,
95                                 bool gen_crash_diag) {
96     Host::SetCrashDescription(reason.c_str());
97     ::abort();
98 }
99
100 void
101 lldb_private::Initialize ()
102 {
103     // Make sure we inialize only once
104     static Mutex g_inited_mutex(Mutex::eMutexTypeRecursive);
105     static bool g_inited = false;
106
107     Mutex::Locker locker(g_inited_mutex);
108     if (!g_inited)
109     {
110         g_inited = true;
111         Log::Initialize();
112         HostInfo::Initialize();
113         Timer::Initialize ();
114         Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
115
116         // Initialize LLVM and Clang
117         llvm::InitializeAllTargets();
118         llvm::InitializeAllAsmPrinters();
119         llvm::InitializeAllTargetMCs();
120         llvm::InitializeAllDisassemblers();
121         llvm::install_fatal_error_handler(fatal_error_handler, 0);
122
123         // Initialize plug-ins
124         ABIMacOSX_arm64::Initialize();
125         ABISysV_x86_64::Initialize();
126         DisassemblerLLVMC::Initialize();
127         ObjectContainerBSDArchive::Initialize();
128         ObjectFileELF::Initialize();
129         SymbolVendorELF::Initialize();
130         SymbolFileDWARF::Initialize();
131         SymbolFileSymtab::Initialize();
132         UnwindAssemblyInstEmulation::Initialize();
133         UnwindAssembly_x86::Initialize();
134         EmulateInstructionARM::Initialize ();
135         EmulateInstructionARM64::Initialize ();
136         DynamicLoaderPOSIXDYLD::Initialize ();
137         PlatformFreeBSD::Initialize();
138         PlatformKalimba::Initialize();
139         SymbolFileDWARFDebugMap::Initialize();
140         ItaniumABILanguageRuntime::Initialize();
141 #ifndef LLDB_DISABLE_PYTHON
142         ScriptInterpreterPython::InitializePrivate();
143         OperatingSystemPython::Initialize();
144 #endif
145         JITLoaderGDB::Initialize();
146         ProcessElfCore::Initialize();
147         
148 #if defined (__APPLE__)
149         //----------------------------------------------------------------------
150         // Apple/Darwin hosted plugins
151         //----------------------------------------------------------------------
152         DynamicLoaderMacOSXDYLD::Initialize();
153         DynamicLoaderDarwinKernel::Initialize();
154         AppleObjCRuntimeV2::Initialize();
155         AppleObjCRuntimeV1::Initialize();
156         ObjectContainerUniversalMachO::Initialize();
157         ObjectFileMachO::Initialize();
158         ProcessKDP::Initialize();
159         PlatformDarwinKernel::Initialize();
160         PlatformRemoteiOS::Initialize();
161         PlatformMacOSX::Initialize();
162         PlatformiOSSimulator::Initialize();
163         SystemRuntimeMacOSX::Initialize();
164 #endif
165 #if defined (__linux__)
166         //----------------------------------------------------------------------
167         // Linux hosted plugins
168         //----------------------------------------------------------------------
169         ProcessLinux::Initialize();
170 #endif
171 #if defined(_WIN32)
172         ProcessWindows::Initialize();
173 #endif
174 #if defined (__FreeBSD__)
175         ProcessFreeBSD::Initialize();
176 #endif
177
178         //----------------------------------------------------------------------
179         // Platform agnostic plugins
180         //----------------------------------------------------------------------
181         PlatformRemoteGDBServer::Initialize ();
182         ProcessGDBRemote::Initialize();
183         DynamicLoaderStatic::Initialize();
184
185         // Scan for any system or user LLDB plug-ins
186         PluginManager::Initialize();
187
188         // The process settings need to know about installed plug-ins, so the Settings must be initialized
189         // AFTER PluginManager::Initialize is called.
190         
191         Debugger::SettingsInitialize();
192     }
193 }
194
195 void
196 lldb_private::WillTerminate()
197 {
198     Host::WillTerminate();
199 }
200
201 void
202 lldb_private::Terminate ()
203 {
204     Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
205     
206     // Terminate and unload and loaded system or user LLDB plug-ins
207     PluginManager::Terminate();
208     ABIMacOSX_arm64::Terminate();
209     ABISysV_x86_64::Terminate();
210     DisassemblerLLVMC::Terminate();
211     ObjectContainerBSDArchive::Terminate();
212     ObjectFileELF::Terminate();
213     SymbolVendorELF::Terminate();
214     SymbolFileDWARF::Terminate();
215     SymbolFileSymtab::Terminate();
216     UnwindAssembly_x86::Terminate();
217     UnwindAssemblyInstEmulation::Terminate();
218     EmulateInstructionARM::Terminate ();
219     EmulateInstructionARM64::Terminate ();
220     DynamicLoaderPOSIXDYLD::Terminate ();
221     PlatformFreeBSD::Terminate();
222     PlatformKalimba::Terminate();
223     SymbolFileDWARFDebugMap::Terminate();
224     ItaniumABILanguageRuntime::Terminate();
225 #ifndef LLDB_DISABLE_PYTHON
226     OperatingSystemPython::Terminate();
227 #endif
228     JITLoaderGDB::Terminate();
229     ProcessElfCore::Terminate();
230     
231 #if defined (__APPLE__)
232     DynamicLoaderMacOSXDYLD::Terminate();
233     DynamicLoaderDarwinKernel::Terminate();
234     AppleObjCRuntimeV2::Terminate();
235     AppleObjCRuntimeV1::Terminate();
236     ObjectContainerUniversalMachO::Terminate();
237     ObjectFileMachO::Terminate();
238     ProcessKDP::Terminate();
239     PlatformMacOSX::Terminate();
240     PlatformDarwinKernel::Terminate();
241     PlatformRemoteiOS::Terminate();
242     PlatformiOSSimulator::Terminate();
243     SystemRuntimeMacOSX::Terminate();
244 #endif
245
246     Debugger::SettingsTerminate ();
247
248 #if defined (__linux__)
249     ProcessLinux::Terminate();
250 #endif
251
252 #if defined (__FreeBSD__)
253     ProcessFreeBSD::Terminate();
254 #endif
255
256     ProcessGDBRemote::Terminate();
257     DynamicLoaderStatic::Terminate();
258
259     Log::Terminate();
260 }
261
262 #if defined (__APPLE__)
263 extern "C" const unsigned char liblldb_coreVersionString[];
264 #else
265
266 #include "clang/Basic/Version.h"
267
268 static const char *
269 GetLLDBRevision()
270 {
271 #ifdef LLDB_REVISION
272     return LLDB_REVISION;
273 #else
274     return NULL;
275 #endif
276 }
277
278 static const char *
279 GetLLDBRepository()
280 {
281 #ifdef LLDB_REPOSITORY
282     return LLDB_REPOSITORY;
283 #else
284     return NULL;
285 #endif
286 }
287
288 #endif
289
290 const char *
291 lldb_private::GetVersion ()
292 {
293 #if defined (__APPLE__)
294     static char g_version_string[32];
295     if (g_version_string[0] == '\0')
296     {
297         const char *version_string = ::strstr ((const char *)liblldb_coreVersionString, "PROJECT:");
298         
299         if (version_string)
300             version_string += sizeof("PROJECT:") - 1;
301         else
302             version_string = "unknown";
303         
304         const char *newline_loc = strchr(version_string, '\n');
305         
306         size_t version_len = sizeof(g_version_string);
307         
308         if (newline_loc &&
309             (newline_loc - version_string < static_cast<ptrdiff_t>(version_len)))
310             version_len = newline_loc - version_string;
311         
312         ::strncpy(g_version_string, version_string, version_len);
313     }
314
315     return g_version_string;
316 #else
317     // On Linux/FreeBSD/Windows, report a version number in the same style as the clang tool.
318     static std::string g_version_str;
319     if (g_version_str.empty())
320     {
321         g_version_str += "lldb version ";
322         g_version_str += CLANG_VERSION_STRING;
323         const char * lldb_repo = GetLLDBRepository();
324         if (lldb_repo)
325         {
326             g_version_str += " (";
327             g_version_str += lldb_repo;
328         }
329
330         const char *lldb_rev = GetLLDBRevision();
331         if (lldb_rev)
332         {
333             g_version_str += " revision ";
334             g_version_str += lldb_rev;
335         }
336         std::string clang_rev (clang::getClangRevision());
337         if (clang_rev.length() > 0)
338         {
339             g_version_str += " clang revision ";
340             g_version_str += clang_rev;
341         }
342         std::string llvm_rev (clang::getLLVMRevision());
343         if (llvm_rev.length() > 0)
344         {
345             g_version_str += " llvm revision ";
346             g_version_str += llvm_rev;
347         }
348
349         if (lldb_repo)
350             g_version_str += ")";
351     }
352     return g_version_str.c_str();
353 #endif
354 }
355
356 const char *
357 lldb_private::GetVoteAsCString (Vote vote)
358 {
359     switch (vote)
360     {
361     case eVoteNo:           return "no";
362     case eVoteNoOpinion:    return "no opinion";
363     case eVoteYes:          return "yes";
364     }
365     return "invalid";
366 }
367
368
369 const char *
370 lldb_private::GetSectionTypeAsCString (SectionType sect_type)
371 {
372     switch (sect_type)
373     {
374     case eSectionTypeInvalid: return "invalid";
375     case eSectionTypeCode: return "code";
376     case eSectionTypeContainer: return "container";
377     case eSectionTypeData: return "data";
378     case eSectionTypeDataCString: return "data-cstr";
379     case eSectionTypeDataCStringPointers: return "data-cstr-ptr";
380     case eSectionTypeDataSymbolAddress: return "data-symbol-addr";
381     case eSectionTypeData4: return "data-4-byte";
382     case eSectionTypeData8: return "data-8-byte";
383     case eSectionTypeData16: return "data-16-byte";
384     case eSectionTypeDataPointers: return "data-ptrs";
385     case eSectionTypeDebug: return "debug";
386     case eSectionTypeZeroFill: return "zero-fill";
387     case eSectionTypeDataObjCMessageRefs: return "objc-message-refs";
388     case eSectionTypeDataObjCCFStrings: return "objc-cfstrings";
389     case eSectionTypeDWARFDebugAbbrev: return "dwarf-abbrev";
390     case eSectionTypeDWARFDebugAranges: return "dwarf-aranges";
391     case eSectionTypeDWARFDebugFrame: return "dwarf-frame";
392     case eSectionTypeDWARFDebugInfo: return "dwarf-info";
393     case eSectionTypeDWARFDebugLine: return "dwarf-line";
394     case eSectionTypeDWARFDebugLoc: return "dwarf-loc";
395     case eSectionTypeDWARFDebugMacInfo: return "dwarf-macinfo";
396     case eSectionTypeDWARFDebugPubNames: return "dwarf-pubnames";
397     case eSectionTypeDWARFDebugPubTypes: return "dwarf-pubtypes";
398     case eSectionTypeDWARFDebugRanges: return "dwarf-ranges";
399     case eSectionTypeDWARFDebugStr: return "dwarf-str";
400     case eSectionTypeELFSymbolTable: return "elf-symbol-table";
401     case eSectionTypeELFDynamicSymbols: return "elf-dynamic-symbols";
402     case eSectionTypeELFRelocationEntries: return "elf-relocation-entries";
403     case eSectionTypeELFDynamicLinkInfo: return "elf-dynamic-link-info";
404     case eSectionTypeDWARFAppleNames: return "apple-names";
405     case eSectionTypeDWARFAppleTypes: return "apple-types";
406     case eSectionTypeDWARFAppleNamespaces: return "apple-namespaces";
407     case eSectionTypeDWARFAppleObjC: return "apple-objc";
408     case eSectionTypeEHFrame: return "eh-frame";
409     case eSectionTypeOther: return "regular";
410     }
411     return "unknown";
412
413 }
414
415 bool
416 lldb_private::NameMatches (const char *name, 
417                            NameMatchType match_type, 
418                            const char *match)
419 {
420     if (match_type == eNameMatchIgnore)
421         return true;
422
423     if (name == match)
424         return true;
425
426     if (name && match)
427     {
428         llvm::StringRef name_sref(name);
429         llvm::StringRef match_sref(match);
430         switch (match_type)
431         {
432         case eNameMatchIgnore: // This case cannot occur: tested before
433             return true;
434         case eNameMatchEquals:      return name_sref == match_sref;
435         case eNameMatchContains:    return name_sref.find (match_sref) != llvm::StringRef::npos;
436         case eNameMatchStartsWith:  return name_sref.startswith (match_sref);
437         case eNameMatchEndsWith:    return name_sref.endswith (match_sref);
438         case eNameMatchRegularExpression:
439             {
440                 RegularExpression regex (match);
441                 return regex.Execute (name);
442             }
443             break;
444         }
445     }
446     return false;
447 }