From: Luis Machado Date: Fri, 24 Dec 2021 14:37:52 +0000 (-0300) Subject: Improve error messages for capability reads/writes to memory X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1372df2e14384c009565e84062430fbb37a2d67e;p=thirdparty%2Fbinutils-gdb.git Improve error messages for capability reads/writes to memory Improve the messages a little so the errors are more clear. One case in particular is when we attempt to write a tagged capability to a shared mapping area that doesn't support tags. GDB and GDBserver share the same error messages now. --- diff --git a/gdb/aarch64-linux-nat.c b/gdb/aarch64-linux-nat.c index a2f2f82b370..b98ecf80eb2 100644 --- a/gdb/aarch64-linux-nat.c +++ b/gdb/aarch64-linux-nat.c @@ -985,7 +985,10 @@ aarch64_linux_nat_target::read_capability (CORE_ADDR addr) struct user_cap cap; if (!aarch64_linux_read_capability (tid, addr, cap)) - perror_with_name (_("Unable to read capability from address.")); + { + gdb::byte_vector cap_vec; + return cap_vec; + } gdb::byte_vector cap_vec (17); memcpy (cap_vec.data (), &cap.tag, 1); @@ -1008,8 +1011,7 @@ aarch64_linux_nat_target::write_capability (CORE_ADDR addr, memcpy (&cap.val, buffer.data () + 1, 16); if (!aarch64_linux_write_capability (tid, addr, cap)) - perror_with_name (_("Unable to write capability to address.\n" - "Please run \"sysctl cheri.ptrace_forge_cap=1\".")); + return false; return true; } diff --git a/gdb/nat/aarch64-cap-linux.c b/gdb/nat/aarch64-cap-linux.c index 1b427803c16..34aa5ec1ac0 100644 --- a/gdb/nat/aarch64-cap-linux.c +++ b/gdb/nat/aarch64-cap-linux.c @@ -19,6 +19,50 @@ #include "aarch64-cap-linux.h" #include "gdb_ptrace.h" +/* Helper function to display various possible errors when reading + Morello capabilities from memory. */ + +static void +morello_linux_peekcap_error (int error) +{ + switch (error) + { + case EIO: + case EFAULT: + warning (_("PTRACE_PEEKCAP: Couldn't fetch capability")); + break; + default: + warning (_("PTRACE_PEEKCAP: Unknown error")); + break; + } +} + +/* Helper function to display various possible errors when writing + Morello capabilities to memory. */ + +static void +morello_linux_pokecap_error (int error) +{ + switch (error) + { + case EIO: + case EFAULT: + warning (_("PTRACE_POKECAP: Couldn't store capability")); + break; + case EPERM: + warning (_("PTRACE_POKECAP:: Couldn't store capability.\n" + "Make sure the sysctl flag cheri.ptrace_forge_cap is 1")); + break; + case EOPNOTSUPP: + warning (_("PTRACE_POKECAP: Capability tags not enabled for" + " requested address")); + break; + default: + warning (_("PTRACE_POKECAP: Unknown error")); + break; + } +} + /* See aarch64-cap-linux.h */ bool @@ -31,7 +75,10 @@ aarch64_linux_read_capability (int tid, CORE_ADDR address, /* Fetch the tag from ptrace. */ if (ptrace (PTRACE_PEEKCAP, tid, address, &cap) < 0) - return false; + { + morello_linux_peekcap_error (errno); + return false; + } return true; } @@ -44,7 +91,10 @@ aarch64_linux_write_capability (int tid, CORE_ADDR address, { /* Fetch the tag from ptrace. */ if (ptrace (PTRACE_POKECAP, tid, address, &cap) < 0) - return false; + { + morello_linux_pokecap_error (errno); + return false; + } return true; } diff --git a/gdbserver/linux-aarch64-low.cc b/gdbserver/linux-aarch64-low.cc index ec0f40cc840..f1708d0c06f 100644 --- a/gdbserver/linux-aarch64-low.cc +++ b/gdbserver/linux-aarch64-low.cc @@ -3495,10 +3495,7 @@ aarch64_target::qxfer_capability (const CORE_ADDR address, if (readbuf != nullptr) { if (!aarch64_linux_read_capability (tid, address, cap)) - { - warning (_("Unable to read capability from address.")); - return 0; - } + return 0; /* Copy data to readbuf. */ memcpy (readbuf, &cap.tag, 1); @@ -3512,11 +3509,7 @@ aarch64_target::qxfer_capability (const CORE_ADDR address, memset (&cap.__reserved, 0, 15); if (!aarch64_linux_write_capability (tid, address, cap)) - { - warning (_("Unable to write capability to address.\n" - "Please run \"sysctl cheri.ptrace_forge_cap=1\".")); - return 0; - } + return 0; } return sizeof (cap.val) + 1;