[Lldb-commits] [lldb] r261536 - Refactor GetSoftwareBreakpointTrapOpcode

Aidan Dodds via lldb-commits lldb-commits at lists.llvm.org
Mon Feb 22 09:29:57 PST 2016


Author: aidandodds
Date: Mon Feb 22 11:29:56 2016
New Revision: 261536

URL: http://llvm.org/viewvc/llvm-project?rev=261536&view=rev
Log:
Refactor GetSoftwareBreakpointTrapOpcode

This patch aims to reduce the code duplication among all of the platforms in GetSoftwareBreakpointTrapOpcode by pushing all common code into the Platform base class.

Differential Revision: http://reviews.llvm.org/D17395

Modified:
    lldb/trunk/include/lldb/Target/Platform.h
    lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
    lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp
    lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.h
    lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
    lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
    lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
    lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
    lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.h
    lldb/trunk/source/Target/Platform.cpp

Modified: lldb/trunk/include/lldb/Target/Platform.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Target/Platform.h?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Target/Platform.h (original)
+++ lldb/trunk/include/lldb/Target/Platform.h Mon Feb 22 11:29:56 2016
@@ -427,7 +427,7 @@ class ModuleCache;
 
         virtual size_t
         GetSoftwareBreakpointTrapOpcode (Target &target,
-                                         BreakpointSite *bp_site) = 0;
+                                         BreakpointSite *bp_site);
 
         //------------------------------------------------------------------
         /// Launch a new process on a platform, not necessarily for 

Modified: lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp Mon Feb 22 11:29:56 2016
@@ -614,84 +614,32 @@ PlatformFreeBSD::GetStatus (Stream &strm
 size_t
 PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
 {
-    ArchSpec arch = target.GetArchitecture();
-    const uint8_t *trap_opcode = NULL;
-    size_t trap_opcode_size = 0;
-
-    switch (arch.GetMachine())
+    switch (target.GetArchitecture().GetMachine())
     {
-    default:
-        assert(false && "Unhandled architecture in PlatformFreeBSD::GetSoftwareBreakpointTrapOpcode()");
-        break;
-    case llvm::Triple::aarch64:
-        {
-            static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
-            trap_opcode = g_aarch64_opcode;
-            trap_opcode_size = sizeof(g_aarch64_opcode);
-        }
-        break;
-    // TODO: support big-endian arm and thumb trap codes.
     case llvm::Triple::arm:
         {
-            static const uint8_t g_arm_breakpoint_opcode[] = { 0xfe, 0xde, 0xff, 0xe7 };
-            static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
-
-            lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
+            lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
             AddressClass addr_class = eAddressClassUnknown;
 
             if (bp_loc_sp)
-                addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
+            {
+                addr_class = bp_loc_sp->GetAddress().GetAddressClass();
+                if (addr_class == eAddressClassUnknown && (bp_loc_sp->GetAddress().GetFileAddress() & 1))
+                    addr_class = eAddressClassCodeAlternateISA;
+            }
 
-            if (addr_class == eAddressClassCodeAlternateISA
-                || (addr_class == eAddressClassUnknown && (bp_site->GetLoadAddress() & 1)))
+            if (addr_class == eAddressClassCodeAlternateISA)
             {
                 // TODO: Enable when FreeBSD supports thumb breakpoints.
                 // FreeBSD kernel as of 10.x, does not support thumb breakpoints
-                trap_opcode = g_thumb_breakpoint_opcode;
-                trap_opcode_size = 0;
+                return 0;
             }
-            else
-            {
-                trap_opcode = g_arm_breakpoint_opcode;
-                trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
-            }
-        }
-        break;
-    case llvm::Triple::mips64:
-        {
-            static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
-            trap_opcode = g_hex_opcode;
-            trap_opcode_size = sizeof(g_hex_opcode);
-        }
-        break;
-    case llvm::Triple::mips64el:
-        {
-            static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
-            trap_opcode = g_hex_opcode;
-            trap_opcode_size = sizeof(g_hex_opcode);
-        }
-        break;
-    case llvm::Triple::ppc:
-    case llvm::Triple::ppc64:
-        {
-            static const uint8_t g_ppc_opcode[] = { 0x7f, 0xe0, 0x00, 0x08 };
-            trap_opcode = g_ppc_opcode;
-            trap_opcode_size = sizeof(g_ppc_opcode);
         }
-        break;
-    case llvm::Triple::x86:
-    case llvm::Triple::x86_64:
-        {
-            static const uint8_t g_i386_opcode[] = { 0xCC };
-            trap_opcode = g_i386_opcode;
-            trap_opcode_size = sizeof(g_i386_opcode);
-        }
-        break;
-    }
 
-    if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
-        return trap_opcode_size;
-    return 0;
+        // Fall through...
+    default:
+        return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site);
+    }
 }
 
 

Modified: lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.cpp Mon Feb 22 11:29:56 2016
@@ -522,98 +522,6 @@ PlatformLinux::GetStatus (Stream &strm)
 #endif
 }
 
-size_t
-PlatformLinux::GetSoftwareBreakpointTrapOpcode (Target &target,
-                                                BreakpointSite *bp_site)
-{
-    ArchSpec arch = target.GetArchitecture();
-    const uint8_t *trap_opcode = NULL;
-    size_t trap_opcode_size = 0;
-
-    switch (arch.GetMachine())
-    {
-    default:
-        assert(false && "CPU type not supported!");
-        break;
-
-    case llvm::Triple::aarch64:
-        {
-            static const uint8_t g_aarch64_opcode[] = { 0x00, 0x00, 0x20, 0xd4 };
-            trap_opcode = g_aarch64_opcode;
-            trap_opcode_size = sizeof(g_aarch64_opcode);
-        }
-        break;
-    case llvm::Triple::x86:
-    case llvm::Triple::x86_64:
-        {
-            static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
-            trap_opcode = g_i386_breakpoint_opcode;
-            trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
-        }
-        break;
-    case llvm::Triple::hexagon:
-        {
-            static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
-            trap_opcode = g_hex_opcode;
-            trap_opcode_size = sizeof(g_hex_opcode);
-        }
-        break;
-    case llvm::Triple::arm:
-        {
-            // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
-            // but the linux kernel does otherwise.
-            static const uint8_t g_arm_breakpoint_opcode[] = { 0xf0, 0x01, 0xf0, 0xe7 };
-            static const uint8_t g_thumb_breakpoint_opcode[] = { 0x01, 0xde };
-
-            lldb::BreakpointLocationSP bp_loc_sp (bp_site->GetOwnerAtIndex (0));
-            AddressClass addr_class = eAddressClassUnknown;
-
-            if (bp_loc_sp)
-            {
-                addr_class = bp_loc_sp->GetAddress ().GetAddressClass ();
-
-                if (addr_class == eAddressClassUnknown &&
-                    (bp_loc_sp->GetAddress ().GetFileAddress () & 1))
-                {
-                    addr_class = eAddressClassCodeAlternateISA;
-                }
-            }
-
-            if (addr_class == eAddressClassCodeAlternateISA)
-            {
-                trap_opcode = g_thumb_breakpoint_opcode;
-                trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
-            }
-            else
-            {
-                trap_opcode = g_arm_breakpoint_opcode;
-                trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
-            }
-        }
-        break;
-    case llvm::Triple::mips:
-    case llvm::Triple::mips64:
-        {
-            static const uint8_t g_hex_opcode[] = { 0x00, 0x00, 0x00, 0x0d };
-            trap_opcode = g_hex_opcode;
-            trap_opcode_size = sizeof(g_hex_opcode);
-        }
-        break;
-    case llvm::Triple::mipsel:
-    case llvm::Triple::mips64el:
-        {
-            static const uint8_t g_hex_opcode[] = { 0x0d, 0x00, 0x00, 0x00 };
-            trap_opcode = g_hex_opcode;
-            trap_opcode_size = sizeof(g_hex_opcode);
-        }
-        break;
-    }
-
-    if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
-        return trap_opcode_size;
-    return 0;
-}
-
 int32_t
 PlatformLinux::GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info)
 {

Modified: lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.h?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.h (original)
+++ lldb/trunk/source/Plugins/Platform/Linux/PlatformLinux.h Mon Feb 22 11:29:56 2016
@@ -87,10 +87,6 @@ namespace platform_linux {
         bool
         GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) override;
 
-        size_t
-        GetSoftwareBreakpointTrapOpcode (Target &target,
-                                         BreakpointSite *bp_site) override;
-
         int32_t
         GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info) override;
 

Modified: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp Mon Feb 22 11:29:56 2016
@@ -583,22 +583,13 @@ PlatformDarwin::GetSharedModule (const M
 size_t
 PlatformDarwin::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
 {
-    const uint8_t *trap_opcode = NULL;
+    const uint8_t *trap_opcode = nullptr;
     uint32_t trap_opcode_size = 0;
     bool bp_is_thumb = false;
-        
+
     llvm::Triple::ArchType machine = target.GetArchitecture().GetMachine();
     switch (machine)
     {
-    case llvm::Triple::x86:
-    case llvm::Triple::x86_64:
-        {
-            static const uint8_t g_i386_breakpoint_opcode[] = { 0xCC };
-            trap_opcode = g_i386_breakpoint_opcode;
-            trap_opcode_size = sizeof(g_i386_breakpoint_opcode);
-        }
-        break;
-
     case llvm::Triple::aarch64:
         {
             // TODO: fix this with actual darwin breakpoint opcode for arm64.
@@ -635,7 +626,7 @@ PlatformDarwin::GetSoftwareBreakpointTra
             trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
         }
         break;
-        
+
     case llvm::Triple::ppc:
     case llvm::Triple::ppc64:
         {
@@ -644,12 +635,11 @@ PlatformDarwin::GetSoftwareBreakpointTra
             trap_opcode_size = sizeof(g_ppc_breakpoint_opcode);
         }
         break;
-        
+
     default:
-        assert(!"Unhandled architecture in PlatformDarwin::GetSoftwareBreakpointTrapOpcode()");
-        break;
+        return Platform::GetSoftwareBreakpointTrapOpcode(target, bp_site);
     }
-    
+
     if (trap_opcode && trap_opcode_size)
     {
         if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))

Modified: lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp Mon Feb 22 11:29:56 2016
@@ -585,34 +585,6 @@ PlatformNetBSD::GetStatus (Stream &strm)
     Platform::GetStatus(strm);
 }
 
-size_t
-PlatformNetBSD::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
-{
-    ArchSpec arch = target.GetArchitecture();
-    const uint8_t *trap_opcode = NULL;
-    size_t trap_opcode_size = 0;
-
-    switch (arch.GetMachine())
-    {
-    default:
-        assert(false && "Unhandled architecture in PlatformNetBSD::GetSoftwareBreakpointTrapOpcode()");
-        break;
-    case llvm::Triple::x86:
-    case llvm::Triple::x86_64:
-        {
-            static const uint8_t g_i386_opcode[] = { 0xCC };
-            trap_opcode = g_i386_opcode;
-            trap_opcode_size = sizeof(g_i386_opcode);
-        }
-        break;
-    }
-
-    if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
-        return trap_opcode_size;
-    return 0;
-}
-
-
 void
 PlatformNetBSD::CalculateTrapHandlerSymbolNames ()
 {

Modified: lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.h?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.h (original)
+++ lldb/trunk/source/Plugins/Platform/NetBSD/PlatformNetBSD.h Mon Feb 22 11:29:56 2016
@@ -86,10 +86,6 @@ namespace platform_netbsd {
                           lldb::ModuleSP &module_sp,
                           const FileSpecList *module_search_paths_ptr) override;
 
-        size_t
-        GetSoftwareBreakpointTrapOpcode(Target &target,
-                                        BreakpointSite *bp_site) override;
-
         bool
         GetRemoteOSVersion () override;
 

Modified: lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.cpp Mon Feb 22 11:29:56 2016
@@ -321,42 +321,6 @@ PlatformWindows::ResolveExecutable (cons
     return error;
 }
 
-size_t
-PlatformWindows::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
-{
-    ArchSpec arch = target.GetArchitecture();
-    const uint8_t *trap_opcode = nullptr;
-    size_t trap_opcode_size = 0;
-
-    switch (arch.GetMachine())
-    {
-    case llvm::Triple::x86:
-    case llvm::Triple::x86_64:
-        {
-            static const uint8_t g_i386_opcode[] = { 0xCC };
-            trap_opcode = g_i386_opcode;
-            trap_opcode_size = sizeof(g_i386_opcode);
-        }
-        break;
-
-    case llvm::Triple::hexagon:
-        {
-            static const uint8_t g_hex_opcode[] = { 0x0c, 0xdb, 0x00, 0x54 };
-            trap_opcode = g_hex_opcode;
-            trap_opcode_size = sizeof(g_hex_opcode);
-        }
-        break;
-    default:
-        llvm_unreachable("Unhandled architecture in PlatformWindows::GetSoftwareBreakpointTrapOpcode()");
-        break;
-    }
-
-    if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
-        return trap_opcode_size;
-
-    return 0;
-}
-
 bool
 PlatformWindows::GetRemoteOSVersion ()
 {

Modified: lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.h?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.h (original)
+++ lldb/trunk/source/Plugins/Platform/Windows/PlatformWindows.h Mon Feb 22 11:29:56 2016
@@ -72,10 +72,6 @@ public:
         return GetPluginDescriptionStatic(IsHost());
     }
 
-    size_t
-    GetSoftwareBreakpointTrapOpcode(lldb_private::Target &target,
-                                    lldb_private::BreakpointSite *bp_site) override;
-
     bool
     GetRemoteOSVersion() override;
 

Modified: lldb/trunk/source/Target/Platform.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Target/Platform.cpp?rev=261536&r1=261535&r2=261536&view=diff
==============================================================================
--- lldb/trunk/source/Target/Platform.cpp (original)
+++ lldb/trunk/source/Target/Platform.cpp Mon Feb 22 11:29:56 2016
@@ -20,6 +20,7 @@
 // Project includes
 #include "lldb/Target/Platform.h"
 #include "lldb/Breakpoint/BreakpointIDList.h"
+#include "lldb/Breakpoint/BreakpointLocation.h"
 #include "lldb/Core/DataBufferHeap.h"
 #include "lldb/Core/Debugger.h"
 #include "lldb/Core/Error.h"
@@ -2046,3 +2047,105 @@ Platform::ConnectToWaitingProcesses(lldb
     error.Clear();
     return 0;
 }
+
+size_t
+Platform::GetSoftwareBreakpointTrapOpcode(Target &target, BreakpointSite *bp_site)
+{
+    ArchSpec arch = target.GetArchitecture();
+    const uint8_t *trap_opcode = nullptr;
+    size_t trap_opcode_size = 0;
+
+    switch (arch.GetMachine())
+    {
+    case llvm::Triple::aarch64:
+        {
+            static const uint8_t g_aarch64_opcode[] = {0x00, 0x00, 0x20, 0xd4};
+            trap_opcode = g_aarch64_opcode;
+            trap_opcode_size = sizeof(g_aarch64_opcode);
+        }
+        break;
+
+    // TODO: support big-endian arm and thumb trap codes.
+    case llvm::Triple::arm:
+        {
+            // The ARM reference recommends the use of 0xe7fddefe and 0xdefe
+            // but the linux kernel does otherwise.
+            static const uint8_t g_arm_breakpoint_opcode[] = {0xf0, 0x01, 0xf0, 0xe7};
+            static const uint8_t g_thumb_breakpoint_opcode[] = {0x01, 0xde};
+
+            lldb::BreakpointLocationSP bp_loc_sp(bp_site->GetOwnerAtIndex(0));
+            AddressClass addr_class = eAddressClassUnknown;
+
+            if (bp_loc_sp)
+            {
+                addr_class = bp_loc_sp->GetAddress().GetAddressClass();
+                if (addr_class == eAddressClassUnknown && (bp_loc_sp->GetAddress().GetFileAddress() & 1))
+                    addr_class = eAddressClassCodeAlternateISA;
+            }
+
+            if (addr_class == eAddressClassCodeAlternateISA)
+            {
+                trap_opcode = g_thumb_breakpoint_opcode;
+                trap_opcode_size = sizeof(g_thumb_breakpoint_opcode);
+            }
+            else
+            {
+                trap_opcode = g_arm_breakpoint_opcode;
+                trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
+            }
+        }
+        break;
+
+    case llvm::Triple::mips64:
+        {
+            static const uint8_t g_hex_opcode[] = {0x00, 0x00, 0x00, 0x0d};
+            trap_opcode = g_hex_opcode;
+            trap_opcode_size = sizeof(g_hex_opcode);
+        }
+        break;
+
+    case llvm::Triple::mips64el:
+        {
+            static const uint8_t g_hex_opcode[] = {0x0d, 0x00, 0x00, 0x00};
+            trap_opcode = g_hex_opcode;
+            trap_opcode_size = sizeof(g_hex_opcode);
+        }
+        break;
+
+    case llvm::Triple::hexagon:
+        {
+            static const uint8_t g_hex_opcode[] = {0x0c, 0xdb, 0x00, 0x54};
+            trap_opcode = g_hex_opcode;
+            trap_opcode_size = sizeof(g_hex_opcode);
+        }
+        break;
+
+    case llvm::Triple::ppc:
+    case llvm::Triple::ppc64:
+        {
+            static const uint8_t g_ppc_opcode[] = {0x7f, 0xe0, 0x00, 0x08};
+            trap_opcode = g_ppc_opcode;
+            trap_opcode_size = sizeof(g_ppc_opcode);
+        }
+        break;
+
+    case llvm::Triple::x86:
+    case llvm::Triple::x86_64:
+        {
+            static const uint8_t g_i386_opcode[] = {0xCC};
+            trap_opcode = g_i386_opcode;
+            trap_opcode_size = sizeof(g_i386_opcode);
+        }
+        break;
+
+    default:
+        assert(!"Unhandled architecture in Platform::GetSoftwareBreakpointTrapOpcode");
+        break;
+    }
+
+    assert(bp_site);
+    if (bp_site->SetTrapOpcode(trap_opcode, trap_opcode_size))
+        return trap_opcode_size;
+
+    return 0;
+}




More information about the lldb-commits mailing list