From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id DE6823858D39 for ; Sat, 27 Nov 2021 06:58:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DE6823858D39 Received: by mail-wr1-x435.google.com with SMTP id c4so23362870wrd.9 for ; Fri, 26 Nov 2021 22:58:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=WA9ZUO/K109ljfic41gYSp5Zl51NqDB7fqGqrp408DY=; b=gEwpWGhEmhp4lxB/cCAtORdXww/1I/750yDx0YTLJLO4OyAvjANDiU8zNGVio6mVXg nmw50cUCVkhC1iKdn71RPqCbu37QVs8d6J+/WMiEGLL6BEA77IhFHLAT2vfQzMVMnfSU gU1XNde8OLPfjCrfNzuz9USt0VMFL9quAgAd0l8LHvzV6N2E52W/5jJCVqV74N6fkM3V 2itNv61PDrq274X/uUHwFUitPVmI3zwU42YEMF7QN+UpmT0DjWtR3/scGqHLBDuj1in0 Xk2U2gnv5dtBES9hty3qARdqLNG8c6lpuVMe5ETKPp4dpUtFdBd3GtMvpEzc+waxi4U3 nVyw== X-Gm-Message-State: AOAM532XscbNwWiBW0IQ51RkniJiXruCvRgQAZk2F9hr1RnqSH1Xapfm E5gY3G0ckqMRMo5lVTMdCKDQ X-Google-Smtp-Source: ABdhPJyCuILjn/eXxFkhofyXIo0+PxYNxcAHl0LUij6D4avq8ZiwDxX7YNzNx5g4feEH5bltCSyvVg== X-Received: by 2002:a5d:4563:: with SMTP id a3mr18827237wrc.130.1637996337832; Fri, 26 Nov 2021 22:58:57 -0800 (PST) Received: from takamaka.home (lfbn-reu-1-483-157.w92-130.abo.wanadoo.fr. [92.130.72.157]) by smtp.gmail.com with ESMTPSA id bg34sm12088378wmb.47.2021.11.26.22.58.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Nov 2021 22:58:57 -0800 (PST) Received: by takamaka.home (Postfix, from userid 1000) id 803A1A4A80; Sat, 27 Nov 2021 10:58:54 +0400 (+04) Date: Sat, 27 Nov 2021 10:58:54 +0400 From: Joel Brobecker To: Jan Vrany via Gdb-patches Cc: lsix@lancelotsix.com, Jan Vrany , Joel Brobecker Subject: Re: [PATCH v2 2/2] ppc: recognize all program traps Message-ID: References: <20211123154237.2335848-1-jan.vrany@labware.com> <20211124130926.2412617-1-jan.vrany@labware.com> <20211124130926.2412617-3-jan.vrany@labware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211124130926.2412617-3-jan.vrany@labware.com> X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Nov 2021 06:59:01 -0000 On Wed, Nov 24, 2021 at 01:09:26PM +0000, Jan Vrany via Gdb-patches wrote: > Permanent program breakpoints (ones inserted into the code) other than > the one GDB uses for POWER (0x7fe00008) did not result in stop but > caused GDB to loop infinitely. > > This was because GDB did not recognize trap instructions other than > "trap". For example, "tw 12, 4, 4" was not be recognized, causing GDB > to loop forever. > > This commit fixes this by providing POWER specific hook > (gdbarch_program_breakpoint_here_p) recognizing all tw, twi, td and tdi > instructions. > > Tested on Linux on PowerPC e500 and on QEMU PPC64le. > --- > gdb/rs6000-tdep.c | 65 ++++++++++++++++++++++++ > gdb/testsuite/gdb.arch/powerpc-trap.exp | 67 +++++++++++++++++++++++++ > gdb/testsuite/gdb.arch/powerpc-trap.s | 30 +++++++++++ > gdb/testsuite/gdb.arch/ppc64-trap.exp | 67 +++++++++++++++++++++++++ > gdb/testsuite/gdb.arch/ppc64-trap.s | 32 ++++++++++++ > 5 files changed, 261 insertions(+) > create mode 100644 gdb/testsuite/gdb.arch/powerpc-trap.exp > create mode 100644 gdb/testsuite/gdb.arch/powerpc-trap.s > create mode 100644 gdb/testsuite/gdb.arch/ppc64-trap.exp > create mode 100644 gdb/testsuite/gdb.arch/ppc64-trap.s > > diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c > index 43880fa4426..ed6838b9e16 100644 > --- a/gdb/rs6000-tdep.c > +++ b/gdb/rs6000-tdep.c > @@ -6247,6 +6247,69 @@ ppc_process_record (struct gdbarch *gdbarch, struct regcache *regcache, > return 0; > } > > +/* Used for matching tw, twi, td and tdi instructions for POWER. */ > + > +static constexpr uint32_t TX_INSN_MASK = 0xFC0007FF; > +static constexpr uint32_t TW_INSN = 0x7C000008; > +static constexpr uint32_t TD_INSN = 0x7C000088; > + > +static constexpr uint32_t TXI_INSN_MASK = 0xFC000000; > +static constexpr uint32_t TWI_INSN = 0x0C000000; > +static constexpr uint32_t TDI_INSN = 0x08000000; > + > +static inline bool > +is_tw_insn (uint32_t insn) > +{ > + return (insn & TX_INSN_MASK) == TW_INSN; > +} > + > +static inline bool > +is_twi_insn (uint32_t insn) > +{ > + return (insn & TXI_INSN_MASK) == TWI_INSN; > +} > + > +static inline bool > +is_td_insn (uint32_t insn) > +{ > + return (insn & TX_INSN_MASK) == TD_INSN; > +} > + > +static inline bool > +is_tdi_insn (uint32_t insn) > +{ > + return (insn & TXI_INSN_MASK) == TDI_INSN; > +} > + > +/* Implementation of gdbarch_program_breakpoint_here_p for POWER. */ > + > +static bool > +rs6000_program_breakpoint_here_p (gdbarch *gdbarch, CORE_ADDR address) > +{ > + gdb_byte target_mem[PPC_INSN_SIZE]; > + > + /* Enable the automatic memory restoration from breakpoints while > + we read the memory. Otherwise we may find temporary breakpoints, ones > + inserted by GDB, and flag them as permanent breakpoints. */ > + scoped_restore restore_memory > + = make_scoped_restore_show_memory_breakpoints (0); Small coding standard violation: Indentation in this case should remain 2 characters. > + > + if (target_read_memory (address, target_mem, PPC_INSN_SIZE) == 0) > + { > + uint32_t insn = (uint32_t)extract_unsigned_integer ( > + target_mem, PPC_INSN_SIZE, gdbarch_byte_order_for_code (gdbarch)); Small coding standard violations: - You need a space after the "(uint32_t)" cast - For the opening parenthesis in the call to extract_unsigned_integer, it should go on the next line, indented by 2 characters: uint32_t insn = (uint32_t) extract_unsigned_integer (target_mem, PPC_INSN_SIZE, gdbarch_byte_order_for_code (gdbarch)); Other than that, the patch looks good to me. > + /* Check if INSN is a TW, TWI, TD or TDI instruction. There > + are multiple choices of such instructions with different registers > + and / or immediate values but they all cause a break. */ > + if (is_tw_insn (insn) || is_twi_insn (insn) || is_td_insn (insn) > + || is_tdi_insn (insn)) > + return true; > + } > + > + return false; > +} > + > /* Initialize the current architecture based on INFO. If possible, re-use an > architecture from ARCHES, which is a list of architectures already created > during this debugging session. > @@ -7109,6 +7172,8 @@ rs6000_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches) > rs6000_breakpoint::kind_from_pc); > set_gdbarch_sw_breakpoint_from_kind (gdbarch, > rs6000_breakpoint::bp_from_kind); > + set_gdbarch_program_breakpoint_here_p (gdbarch, > + rs6000_program_breakpoint_here_p); > > /* The value of symbols of type N_SO and N_FUN maybe null when > it shouldn't be. */ > diff --git a/gdb/testsuite/gdb.arch/powerpc-trap.exp b/gdb/testsuite/gdb.arch/powerpc-trap.exp > new file mode 100644 > index 00000000000..86f8e61b89b > --- /dev/null > +++ b/gdb/testsuite/gdb.arch/powerpc-trap.exp > @@ -0,0 +1,67 @@ > +# Copyright 2020-2021 Free Software Foundation, Inc. > +# > +# This program is free software; you can redistribute it and/or modify > +# it under the terms of the GNU General Public License as published by > +# the Free Software Foundation; either version 3 of the License, or > +# (at your option) any later version. > +# > +# This program is distributed in the hope that it will be useful, > +# but WITHOUT ANY WARRANTY; without even the implied warranty of > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > +# GNU General Public License for more details. > +# > +# You should have received a copy of the GNU General Public License > +# along with this program. If not, see . > +# > +# This file is part of the gdb testsuite. > + > +# Test if GDB stops at various trap instructions inserted into > +# the code. > + > +if { ![istarget powerpc-*] } { > + verbose "Skipping ${gdb_test_file_name}." > + return > +} > + > +standard_testfile .s > +if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile}]} { > + return -1 > +} > + > +if {![runto_main]} { > + untested "could not run to main" > + return -1 > +} > + > +# Number of expected SIGTRAP's to get. This needs to be kept in sync > +# with the source file. > +set expected_traps 3 > +set keep_going 1 > +set count 0 > + > +# Make sure we have a lower timeout in case GDB doesn't support a particular > +# instruction. Such instruction will cause GDB to loop infinitely. > +while {$keep_going} { > + # Continue to next program breakpoint instruction. > + gdb_test_multiple "continue" "trap instruction $count causes SIGTRAP" { > + -re "Program received signal SIGTRAP, Trace/breakpoint trap.*$gdb_prompt $" { > + pass $gdb_test_name > + > + # Advance PC to next instruction > + gdb_test "set \$pc = \$pc + 4" "" "advance past trap instruction $count" > + > + incr count > + } > + # We've reached the end of the test. > + -re "exited with code 01.*$gdb_prompt $" { > + set keep_going 0 > + } > + timeout { > + fail $gdb_test_name > + set keep_going 0 > + } > + } > +} > + > +# Verify we stopped at the expected number of SIGTRAP's. > +gdb_assert {$count == $expected_traps} "all trap instructions triggered" > diff --git a/gdb/testsuite/gdb.arch/powerpc-trap.s b/gdb/testsuite/gdb.arch/powerpc-trap.s > new file mode 100644 > index 00000000000..c503cb1e33e > --- /dev/null > +++ b/gdb/testsuite/gdb.arch/powerpc-trap.s > @@ -0,0 +1,30 @@ > +/* This testcase is part of GDB, the GNU debugger. > + > + Copyright 2021 Free Software Foundation, Inc. > + > + This program is free software; you can redistribute it and/or modify > + it under the terms of the GNU General Public License as published by > + the Free Software Foundation; either version 3 of the License, or > + (at your option) any later version. > + > + This program is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + GNU General Public License for more details. > + > + You should have received a copy of the GNU General Public License > + along with this program. If not, see . */ > + > +/* To test if GDB stops at various trap instructions inserted into > + the code. */ > + > +.global main > +.type main,function > +main: > + ori 0, 0, 0 > + trap > + tw 12, 2, 2 > + twi 31, 3, 3 > + ori 0, 0, 0 > + blr > + > diff --git a/gdb/testsuite/gdb.arch/ppc64-trap.exp b/gdb/testsuite/gdb.arch/ppc64-trap.exp > new file mode 100644 > index 00000000000..ffd640c681d > --- /dev/null > +++ b/gdb/testsuite/gdb.arch/ppc64-trap.exp > @@ -0,0 +1,67 @@ > +# Copyright 2020-2021 Free Software Foundation, Inc. > +# > +# This program is free software; you can redistribute it and/or modify > +# it under the terms of the GNU General Public License as published by > +# the Free Software Foundation; either version 3 of the License, or > +# (at your option) any later version. > +# > +# This program is distributed in the hope that it will be useful, > +# but WITHOUT ANY WARRANTY; without even the implied warranty of > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > +# GNU General Public License for more details. > +# > +# You should have received a copy of the GNU General Public License > +# along with this program. If not, see . > +# > +# This file is part of the gdb testsuite. > + > +# Test if GDB stops at various trap instructions inserted into > +# the code. > + > +if { ![istarget powerpc64-*] } { > + verbose "Skipping ${gdb_test_file_name}." > + return > +} > + > +standard_testfile .s > +if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile}]} { > + return -1 > +} > + > +if {![runto_main]} { > + untested "could not run to main" > + return -1 > +} > + > +# Number of expected SIGTRAP's to get. This needs to be kept in sync > +# with the source file. > +set expected_traps 5 > +set keep_going 1 > +set count 0 > + > +# Make sure we have a lower timeout in case GDB doesn't support a particular > +# instruction. Such instruction will cause GDB to loop infinitely. > +while {$keep_going} { > + # Continue to next program breakpoint instruction. > + gdb_test_multiple "continue" "trap instruction $count causes SIGTRAP" { > + -re "Program received signal SIGTRAP, Trace/breakpoint trap.*$gdb_prompt $" { > + pass $gdb_test_name > + > + # Advance PC to next instruction > + gdb_test "set \$pc = \$pc + 4" "" "advance past trap instruction $count" > + > + incr count > + } > + # We've reached the end of the test. > + -re "exited with code 01.*$gdb_prompt $" { > + set keep_going 0 > + } > + timeout { > + fail $gdb_test_name > + set keep_going 0 > + } > + } > +} > + > +# Verify we stopped at the expected number of SIGTRAP's. > +gdb_assert {$count == $expected_traps} "all trap instructions triggered" > diff --git a/gdb/testsuite/gdb.arch/ppc64-trap.s b/gdb/testsuite/gdb.arch/ppc64-trap.s > new file mode 100644 > index 00000000000..b307018f3ec > --- /dev/null > +++ b/gdb/testsuite/gdb.arch/ppc64-trap.s > @@ -0,0 +1,32 @@ > +/* This testcase is part of GDB, the GNU debugger. > + > + Copyright 2021 Free Software Foundation, Inc. > + > + This program is free software; you can redistribute it and/or modify > + it under the terms of the GNU General Public License as published by > + the Free Software Foundation; either version 3 of the License, or > + (at your option) any later version. > + > + This program is distributed in the hope that it will be useful, > + but WITHOUT ANY WARRANTY; without even the implied warranty of > + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + GNU General Public License for more details. > + > + You should have received a copy of the GNU General Public License > + along with this program. If not, see . */ > + > +/* To test if GDB stops at various trap instructions inserted into > + the code. */ > + > +.global main > +.type main,function > +main: > + ori 0, 0, 0 > + trap > + tw 12, 2, 2 > + twi 31, 3, 3 > + td 12, 2, 2 > + tdi 31, 3, 3 > + ori 0, 0, 0 > + blr > + > -- > 2.30.2 > -- Joel