ADodds created this revision.
ADodds added reviewers: clayborg, emaste, ted, zturner, jasonmolenda.
ADodds added a subscriber: lldb-commits.
ADodds set the repository for this revision to rL LLVM.
Herald added a subscriber: emaste.

This patch aims to reduce the code duplication among all of the platforms in 
GetSoftwareBreakpointTrapOpcode.  Common code has been pushed into the Platform 
base class, and only special case platform code has been left in the derived 
platforms.

When remote debugging it can be the case that your current host platform will 
be queried for the trap opcode to use on the target, which currently lead to 
different results depending on the host lldb is running on.  With this patch, 
platforms have a more unified view of trap opcodes for targets.

Some platforms however have specific/special case requirements so I should not 
have effected their functionality.

Do others think this is a usefull refactor, or have suggestions/feedback?

Repository:
  rL LLVM

http://reviews.llvm.org/D17395

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

Index: source/Target/Platform.cpp
===================================================================
--- source/Target/Platform.cpp
+++ source/Target/Platform.cpp
@@ -19,6 +19,7 @@
 // Other libraries and framework includes
 // Project includes
 #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"
@@ -2087,3 +2088,105 @@
     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;
+}
Index: source/Plugins/Platform/Windows/PlatformWindows.h
===================================================================
--- source/Plugins/Platform/Windows/PlatformWindows.h
+++ source/Plugins/Platform/Windows/PlatformWindows.h
@@ -72,10 +72,6 @@
         return GetPluginDescriptionStatic(IsHost());
     }
 
-    size_t
-    GetSoftwareBreakpointTrapOpcode(lldb_private::Target &target,
-                                    lldb_private::BreakpointSite *bp_site) override;
-
     bool
     GetRemoteOSVersion() override;
 
Index: source/Plugins/Platform/Windows/PlatformWindows.cpp
===================================================================
--- source/Plugins/Platform/Windows/PlatformWindows.cpp
+++ source/Plugins/Platform/Windows/PlatformWindows.cpp
@@ -321,42 +321,6 @@
     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 ()
 {
Index: source/Plugins/Platform/NetBSD/PlatformNetBSD.h
===================================================================
--- source/Plugins/Platform/NetBSD/PlatformNetBSD.h
+++ source/Plugins/Platform/NetBSD/PlatformNetBSD.h
@@ -86,10 +86,6 @@
                           lldb::ModuleSP &module_sp,
                           const FileSpecList *module_search_paths_ptr) override;
 
-        size_t
-        GetSoftwareBreakpointTrapOpcode(Target &target,
-                                        BreakpointSite *bp_site) override;
-
         bool
         GetRemoteOSVersion () override;
 
Index: source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
===================================================================
--- source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
+++ source/Plugins/Platform/NetBSD/PlatformNetBSD.cpp
@@ -585,34 +585,6 @@
     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 ()
 {
Index: source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
===================================================================
--- source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -583,22 +583,13 @@
 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.
@@ -634,21 +625,20 @@
             trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
         }
         break;
-        
+
     case llvm::Triple::ppc:
     case llvm::Triple::ppc64:
         {
             static const uint8_t g_ppc_breakpoint_opcode[] = { 0x7F, 0xC0, 0x00, 0x08 };
             trap_opcode = g_ppc_breakpoint_opcode;
             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))
Index: source/Plugins/Platform/Linux/PlatformLinux.h
===================================================================
--- source/Plugins/Platform/Linux/PlatformLinux.h
+++ source/Plugins/Platform/Linux/PlatformLinux.h
@@ -87,10 +87,6 @@
         bool
         GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch) override;
 
-        size_t
-        GetSoftwareBreakpointTrapOpcode (Target &target,
-                                         BreakpointSite *bp_site) override;
-
         int32_t
         GetResumeCountForLaunchInfo (ProcessLaunchInfo &launch_info) override;
 
Index: source/Plugins/Platform/Linux/PlatformLinux.cpp
===================================================================
--- source/Plugins/Platform/Linux/PlatformLinux.cpp
+++ source/Plugins/Platform/Linux/PlatformLinux.cpp
@@ -522,98 +522,6 @@
 #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)
 {
Index: source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
===================================================================
--- source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
+++ source/Plugins/Platform/FreeBSD/PlatformFreeBSD.cpp
@@ -614,84 +614,32 @@
 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;
-            }
-            else
-            {
-                trap_opcode = g_arm_breakpoint_opcode;
-                trap_opcode_size = sizeof(g_arm_breakpoint_opcode);
+                return 0;
             }
         }
-        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);
+    }
 }
 
 
Index: include/lldb/Target/Platform.h
===================================================================
--- include/lldb/Target/Platform.h
+++ include/lldb/Target/Platform.h
@@ -427,7 +427,7 @@
 
         virtual size_t
         GetSoftwareBreakpointTrapOpcode (Target &target,
-                                         BreakpointSite *bp_site) = 0;
+                                         BreakpointSite *bp_site);
 
         //------------------------------------------------------------------
         /// Launch a new process on a platform, not necessarily for 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to