From 4180215b9db1549b88da2da2fcc320fe28233481 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Wed, 22 Oct 2014 13:51:07 +0100 Subject: [PATCH] x86 Linux/ptrace: fix offsetof usage in C++ mode MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit In C++ mode, we get: gdb/gdbserver/linux-x86-low.c: In function ‘void x86_linux_dr_set(ptid_t, int, long unsigned int)’: gdb/gdbserver/linux-x86-low.c:558:38: error: ‘regnum’ cannot appear in a constant-expression offsetof (struct user, u_debugreg[regnum]), value); ^ gdb/gdbserver/ChangeLog: 2015-02-27 Pedro Alves * linux-x86-low.c (u_debugreg_offset): New function. (x86_linux_dr_get, x86_linux_dr_set): Use it. gdb/ChangeLog: 2015-02-27 Pedro Alves * x86-linux-nat.c (u_debugreg_offset): New function. (x86_linux_dr_get, x86_linux_dr_set): Use it. --- gdb/ChangeLog | 5 +++++ gdb/gdbserver/ChangeLog | 5 +++++ gdb/gdbserver/linux-x86-low.c | 18 ++++++++++++++---- gdb/x86-linux-nat.c | 17 +++++++++++++---- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 92f85f4fae3..cd43e5a8a5d 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2015-02-27 Pedro Alves + + * x86-linux-nat.c (u_debugreg_offset): New function. + (x86_linux_dr_get, x86_linux_dr_set): Use it. + 2015-02-27 Pedro Alves * nat/x86-dregs.h (enum target_hw_bp_type): Remove forward diff --git a/gdb/gdbserver/ChangeLog b/gdb/gdbserver/ChangeLog index 7ba137b8249..c420c066b5f 100644 --- a/gdb/gdbserver/ChangeLog +++ b/gdb/gdbserver/ChangeLog @@ -1,3 +1,8 @@ +2015-02-27 Pedro Alves + + * linux-x86-low.c (u_debugreg_offset): New function. + (x86_linux_dr_get, x86_linux_dr_set): Use it. + 2015-02-27 Pedro Alves * gdb_proc_service.h: Wrap with EXTERN_C_PUSH/EXTERN_C_POP. diff --git a/gdb/gdbserver/linux-x86-low.c b/gdb/gdbserver/linux-x86-low.c index 171735c206a..c376cab10d5 100644 --- a/gdb/gdbserver/linux-x86-low.c +++ b/gdb/gdbserver/linux-x86-low.c @@ -530,6 +530,18 @@ x86_breakpoint_at (CORE_ADDR pc) return 0; } + +/* Return the offset of REGNUM in the u_debugreg field of struct + user. */ + +static int +u_debugreg_offset (int regnum) +{ + return (offsetof (struct user, u_debugreg) + + sizeof (((struct user *) 0)->u_debugreg[0]) * regnum); +} + + /* Support for debug registers. */ static unsigned long @@ -541,8 +553,7 @@ x86_linux_dr_get (ptid_t ptid, int regnum) tid = ptid_get_lwp (ptid); errno = 0; - value = ptrace (PTRACE_PEEKUSER, tid, - offsetof (struct user, u_debugreg[regnum]), 0); + value = ptrace (PTRACE_PEEKUSER, tid, u_debugreg_offset (regnum), 0); if (errno != 0) error ("Couldn't read debug register"); @@ -557,8 +568,7 @@ x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value) tid = ptid_get_lwp (ptid); errno = 0; - ptrace (PTRACE_POKEUSER, tid, - offsetof (struct user, u_debugreg[regnum]), value); + ptrace (PTRACE_POKEUSER, tid, u_debugreg_offset (regnum), value); if (errno != 0) error ("Couldn't write debug register"); } diff --git a/gdb/x86-linux-nat.c b/gdb/x86-linux-nat.c index 9d82be2017e..c58c01a2bb7 100644 --- a/gdb/x86-linux-nat.c +++ b/gdb/x86-linux-nat.c @@ -51,6 +51,16 @@ struct arch_lwp_info int have_ptrace_getregset = -1; +/* Return the offset of REGNUM in the u_debugreg field of struct + user. */ + +static int +u_debugreg_offset (int regnum) +{ + return (offsetof (struct user, u_debugreg) + + sizeof (((struct user *) 0)->u_debugreg[0]) * regnum); +} + /* Support for debug registers. */ /* Get debug register REGNUM value from only the one LWP of PTID. */ @@ -65,8 +75,8 @@ x86_linux_dr_get (ptid_t ptid, int regnum) tid = ptid_get_lwp (ptid); errno = 0; - value = ptrace (PTRACE_PEEKUSER, tid, - offsetof (struct user, u_debugreg[regnum]), 0); + value = ptrace (PTRACE_PEEKUSER, tid, u_debugreg_offset (regnum), 0); + if (errno != 0) perror_with_name (_("Couldn't read debug register")); @@ -84,8 +94,7 @@ x86_linux_dr_set (ptid_t ptid, int regnum, unsigned long value) tid = ptid_get_lwp (ptid); errno = 0; - ptrace (PTRACE_POKEUSER, tid, - offsetof (struct user, u_debugreg[regnum]), value); + ptrace (PTRACE_POKEUSER, tid, u_debugreg_offset (regnum), value); if (errno != 0) perror_with_name (_("Couldn't write debug register")); } -- 2.30.2