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