From patchwork Tue Jun 3 13:11:42 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pedro Alves X-Patchwork-Id: 1267 Received: (qmail 17997 invoked by alias); 3 Jun 2014 13:11:49 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 17987 invoked by uid 89); 3 Jun 2014 13:11:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 03 Jun 2014 13:11:46 +0000 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s53DBibO029490 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 3 Jun 2014 09:11:44 -0400 Received: from [127.0.0.1] (ovpn01.gateway.prod.ext.ams2.redhat.com [10.39.146.11]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s53DBgiW004074; Tue, 3 Jun 2014 09:11:43 -0400 Message-ID: <538DC98E.9050004@redhat.com> Date: Tue, 03 Jun 2014 14:11:42 +0100 From: Pedro Alves User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: Joel Brobecker CC: gdb-patches@sourceware.org Subject: Re: [RFA/7.8] user breakpoint not inserted if software-single-step at same location References: <1401394280-14999-1-git-send-email-brobecker@adacore.com> <5387BFF0.6010208@redhat.com> <20140530122253.GC4289@adacore.com> <53887ED5.5050603@redhat.com> <20140530132659.GD4289@adacore.com> <20140530193549.GF4289@adacore.com> <538D05CC.8050608@redhat.com> <538D85A9.5010004@redhat.com> In-Reply-To: <538D85A9.5010004@redhat.com> On 06/03/2014 09:22 AM, Pedro Alves wrote: > On 06/03/2014 12:16 AM, Pedro Alves wrote: > >> Let me know what you think, and feel free to push if it >> looks OK. Tested on all combinations of >> native|gdbserver X hardware-|software- stepping. At least, >> I think I did. If not this exact version, some other minor >> variation. :-) > > Bah, I woke up realizing that the version I posted forgets to > clone the shadow buffer! Let me fix that and repost... > Here is the fixed version. Retested on all combinations of native|gdbserver X hardware-|software- stepping on x86_64 Fedora 20. Fixes both new tests. 8<------------ Subject: [PATCH] User breakpoint ignored if software-single-step at same location with the following code... 12 Nested; -- break #1 13 return I; -- break #2 14 end; (line 12 is a call to function Nested) ... we have noticed the following errorneous behavior on ppc-aix, where, after having inserted a breakpoint at line 12 and line 13, and continuing from the breakpoint at line 12, the program never stops at line 13, running away until the program terminates: % gdb -q func (gdb) b func.adb:12 Breakpoint 1 at 0x10000a24: file func.adb, line 12. (gdb) b func.adb:13 Breakpoint 2 at 0x10000a28: file func.adb, line 13. (gdb) run Starting program: /[...]/func Breakpoint 1, func () at func.adb:12 12 Nested; -- break #1 (gdb) c Continuing. [Inferior 1 (process 4128872) exited with code 02] When resuming from the first breakpoint, GDB first tries to step out of that first breakpoint. We rely on software single-stepping on this platform, and it just so happens that the address of the first software single-step breakpoint is the same as the user's breakpoint #2 (0x10000a28). So, with infrun and target traces turned on (but uninteresting traces snip'ed off), the "continue" operation looks like this: (gdb) c ### First, we insert the user breakpoints (the second one is an internal ### breakpoint on __pthread_init). The first user breakpoint is not ### inserted as we need to step out of it first. target_insert_breakpoint (0x0000000010000a28, xxx) = 0 target_insert_breakpoint (0x00000000d03f3800, xxx) = 0 ### Then we proceed with the step-out-of-breakpoint... infrun: resume (step=1, signal=GDB_SIGNAL_0), trap_expected=1, current thread [process 15335610] at 0x10000a24 ### That's when we insert the SSS breakpoints... target_insert_breakpoint (0x0000000010000a28, xxx) = 0 target_insert_breakpoint (0x00000000100009ac, xxx) = 0 ### ... then let the inferior resume... target_resume (15335610, continue, 0) infrun: wait_for_inferior () target_wait (-1, status, options={}) = 15335610, status->kind = stopped, signal = GDB_SIGNAL_TRAP infrun: target_wait (-1, status) = infrun: 15335610 [process 15335610], infrun: status->kind = stopped, signal = GDB_SIGNAL_TRAP infrun: infwait_normal_state infrun: TARGET_WAITKIND_STOPPED infrun: stop_pc = 0x100009ac ### At this point, we stopped at the second SSS breakpoint... target_stopped_by_watchpoint () = 0 ### We remove the SSS breakpoints... target_remove_breakpoint (0x0000000010000a28, xxx) = 0 target_remove_breakpoint (0x00000000100009ac, xxx) = 0 target_stopped_by_watchpoint () = 0 ### We find that we're not done, so we resume.... infrun: no stepping, continue ### And thus insert the user breakpoints again, except we're not ### inserting the second breakpoint?!? target_insert_breakpoint (0x0000000010000a24, xxx) = 0 infrun: resume (step=0, signal=GDB_SIGNAL_0), trap_expected=0, current thread [process 15335610] at 0x100009ac target_resume (-1, continue, 0) infrun: prepare_to_wait target_wait (-1, status, options={}) = 15335610, status->kind = exited, status = 2 What happens is that the removal of the software single-step breakpoints effectively removed the breakpoint instruction from inferior memory. But because such breakpoints are inserted directly as raw breakpoints rather than through the normal chain of breakpoints, we fail to notice that one of the user breakpoints points to the same address and that this user breakpoint is therefore effectively un-inserted. When resuming after the single-step, GDB thinks that the user breakpoint is still inserted and therefore does not need to insert it again. This patch teaches the insert and remove routines of both regular and raw breakpoints to be aware of each other. Special care needs to be applied in case the target supports evaluation of breakpoint conditions or commands. gdb/ChangeLog: PR breakpoints/17000 * breakpoint.c (find_non_raw_software_breakpoint_inserted_here): New function, extracted from software_breakpoint_inserted_here_p. (software_breakpoint_inserted_here_p): Replace factored out code by call to find_non_raw_software_breakpoint_inserted_here. (bp_target_info_copy_insertion_state): New function. (bkpt_insert_location): Handle the case of a single-step breakpoint already inserted at the same address. (bkpt_remove_location): Handle the case of a single-step breakpoint still inserted at the same address. (deprecated_insert_raw_breakpoint): Handle the case of non-raw breakpoint already inserted at the same address. (deprecated_remove_raw_breakpoint): Handle the case of a non-raw breakpoint still inserted at the same address. (find_single_step_breakpoint): New function, extracted from single_step_breakpoint_inserted_here_p. (find_single_step_breakpoint): New function, factored out from single_step_breakpoint_inserted_here_p. (single_step_breakpoint_inserted_here_p): Reimplement. gdb/testsuite/ChangeLog: PR breakpoints/17000 * gdb.base/sss-bp-on-user-bp.exp: Remove kfail. * gdb.base/sss-bp-on-user-bp-2.exp: Remove kfail. Tested on ppc-aix with AdaCore's testsuite. Tested on x86_64-linux, (native and gdbserver) with the official testsuite. Also tested on x86_64-linux through Pedro's branch enabling software single-stepping on that platform (native and gdbserver). --- gdb/breakpoint.c | 141 ++++++++++++++++++++++--- gdb/testsuite/gdb.base/sss-bp-on-user-bp-2.exp | 1 - gdb/testsuite/gdb.base/sss-bp-on-user-bp.exp | 3 +- 3 files changed, 125 insertions(+), 20 deletions(-) diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c index 4e6c627..23c8895 100644 --- a/gdb/breakpoint.c +++ b/gdb/breakpoint.c @@ -229,6 +229,9 @@ static void tcatch_command (char *arg, int from_tty); static void detach_single_step_breakpoints (void); +static int find_single_step_breakpoint (struct address_space *aspace, + CORE_ADDR pc); + static void free_bp_location (struct bp_location *loc); static void incref_bp_location (struct bp_location *loc); static void decref_bp_location (struct bp_location **loc); @@ -4165,12 +4168,12 @@ breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc) return 0; } -/* This function returns non-zero iff there is a software breakpoint - inserted at PC. */ +/* Ignoring deprecated raw breakpoints, return non-zero iff there is a + software breakpoint inserted at PC. */ -int -software_breakpoint_inserted_here_p (struct address_space *aspace, - CORE_ADDR pc) +static struct bp_location * +find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace, + CORE_ADDR pc) { struct bp_location *bl, **blp_tmp; @@ -4188,10 +4191,23 @@ software_breakpoint_inserted_here_p (struct address_space *aspace, && !section_is_mapped (bl->section)) continue; /* unmapped overlay -- can't be a match */ else - return 1; + return bl; } } + return NULL; +} + +/* This function returns non-zero iff there is a software breakpoint + inserted at PC. */ + +int +software_breakpoint_inserted_here_p (struct address_space *aspace, + CORE_ADDR pc) +{ + if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL) + return 1; + /* Also check for software single-step breakpoints. */ if (single_step_breakpoint_inserted_here_p (aspace, pc)) return 1; @@ -13112,6 +13128,19 @@ bkpt_re_set (struct breakpoint *b) breakpoint_re_set_default (b); } +/* Copy SRC's shadow buffer and whatever else we'd set if we actually + inserted DEST, so we can remove it later, in case SRC is removed + first. */ + +static void +bp_target_info_copy_insertion_state (struct bp_target_info *dest, + const struct bp_target_info *src) +{ + dest->shadow_len = src->shadow_len; + memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len); + dest->placed_size = src->placed_size; +} + static int bkpt_insert_location (struct bp_location *bl) { @@ -13119,8 +13148,25 @@ bkpt_insert_location (struct bp_location *bl) return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info); else - return target_insert_breakpoint (bl->gdbarch, - &bl->target_info); + { + struct bp_target_info *bp_tgt = &bl->target_info; + int ret; + int sss_slot; + + /* There is no need to insert a breakpoint if an unconditional + raw/sss breakpoint is already inserted at that location. */ + sss_slot = find_single_step_breakpoint (bp_tgt->placed_address_space, + bp_tgt->placed_address); + if (sss_slot >= 0) + { + struct bp_target_info *sss_bp_tgt = single_step_breakpoints[sss_slot]; + + bp_target_info_copy_insertion_state (bp_tgt, sss_bp_tgt); + return 0; + } + + return target_insert_breakpoint (bl->gdbarch, bp_tgt); + } } static int @@ -13129,7 +13175,19 @@ bkpt_remove_location (struct bp_location *bl) if (bl->loc_type == bp_loc_hardware_breakpoint) return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info); else - return target_remove_breakpoint (bl->gdbarch, &bl->target_info); + { + struct bp_target_info *bp_tgt = &bl->target_info; + struct address_space *aspace = bp_tgt->placed_address_space; + CORE_ADDR address = bp_tgt->placed_address; + + /* Only remove the breakpoint if there is no raw/sss breakpoint + still inserted at this location. Otherwise, we would be + effectively disabling the raw/sss breakpoint. */ + if (single_step_breakpoint_inserted_here_p (aspace, address)) + return 0; + + return target_remove_breakpoint (bl->gdbarch, bp_tgt); + } } static int @@ -15138,12 +15196,30 @@ deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch, struct address_space *aspace, CORE_ADDR pc) { struct bp_target_info *bp_tgt; + struct bp_location *bl; bp_tgt = XCNEW (struct bp_target_info); bp_tgt->placed_address_space = aspace; bp_tgt->placed_address = pc; + /* If an unconditional non-raw breakpoint is already inserted at + that location, there's no need to insert another. However, with + target-side evaluation of breakpoint conditions, if the + breakpoint that is currently inserted on the target is + conditional, we need to make it unconditional. Note that a + breakpoint with target-side commands is not reported even if + unconditional, so we need to remove the commands from the target + as well. */ + bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc); + if (bl != NULL + && VEC_empty (agent_expr_p, bl->target_info.conditions) + && VEC_empty (agent_expr_p, bl->target_info.tcommands)) + { + bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info); + return bp_tgt; + } + if (target_insert_breakpoint (gdbarch, bp_tgt) != 0) { /* Could not insert the breakpoint. */ @@ -15161,9 +15237,30 @@ int deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp) { struct bp_target_info *bp_tgt = bp; + struct address_space *aspace = bp_tgt->placed_address_space; + CORE_ADDR address = bp_tgt->placed_address; + struct bp_location *bl; int ret; - ret = target_remove_breakpoint (gdbarch, bp_tgt); + bl = find_non_raw_software_breakpoint_inserted_here (aspace, address); + + /* Only remove the raw breakpoint if there are no other non-raw + breakpoints still inserted at this location. Otherwise, we would + be effectively disabling those breakpoints. */ + if (bl == NULL) + ret = target_remove_breakpoint (gdbarch, bp_tgt); + else if (!VEC_empty (agent_expr_p, bl->target_info.conditions) + || !VEC_empty (agent_expr_p, bl->target_info.tcommands)) + { + /* The target is evaluating conditions, and when we inserted the + software single-step breakpoint, we had made the breakpoint + unconditional and command-less on the target side. Reinsert + to restore the conditions/commands. */ + ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info); + } + else + ret = 0; + xfree (bp_tgt); return ret; @@ -15269,12 +15366,12 @@ detach_single_step_breakpoints (void) single_step_breakpoints[i]); } -/* Check whether a software single-step breakpoint is inserted at - PC. */ +/* Find the software single-step breakpoint that inserted at PC. + Returns its slot if found, and -1 if not found. */ -int -single_step_breakpoint_inserted_here_p (struct address_space *aspace, - CORE_ADDR pc) +static int +find_single_step_breakpoint (struct address_space *aspace, + CORE_ADDR pc) { int i; @@ -15285,10 +15382,20 @@ single_step_breakpoint_inserted_here_p (struct address_space *aspace, && breakpoint_address_match (bp_tgt->placed_address_space, bp_tgt->placed_address, aspace, pc)) - return 1; + return i; } - return 0; + return -1; +} + +/* Check whether a software single-step breakpoint is inserted at + PC. */ + +int +single_step_breakpoint_inserted_here_p (struct address_space *aspace, + CORE_ADDR pc) +{ + return find_single_step_breakpoint (aspace, pc) >= 0; } /* Returns 0 if 'bp' is NOT a syscall catchpoint, diff --git a/gdb/testsuite/gdb.base/sss-bp-on-user-bp-2.exp b/gdb/testsuite/gdb.base/sss-bp-on-user-bp-2.exp index b41f86e..87fa5f8 100644 --- a/gdb/testsuite/gdb.base/sss-bp-on-user-bp-2.exp +++ b/gdb/testsuite/gdb.base/sss-bp-on-user-bp-2.exp @@ -121,7 +121,6 @@ gdb_test_multiple $test $test { set command "stepi_del_break" set test $command -setup_kfail "breakpoints/17000" "*-*-*" gdb_test_multiple $command $test { -re "^$command\r\n$gdb_prompt " { # Note no end anchor, because "si&" finishes and prints the diff --git a/gdb/testsuite/gdb.base/sss-bp-on-user-bp.exp b/gdb/testsuite/gdb.base/sss-bp-on-user-bp.exp index 62415e7..2a12ad6 100644 --- a/gdb/testsuite/gdb.base/sss-bp-on-user-bp.exp +++ b/gdb/testsuite/gdb.base/sss-bp-on-user-bp.exp @@ -47,6 +47,5 @@ gdb_test "si" "Breakpoint .* bar break .*" # If the breakpoint is still correctly inserted, then this jump should # re-trigger it. Otherwise, GDB will lose control and the program -# will exit. -setup_kfail "breakpoints/17000" "*-*-*" +# will exit. See PR breakpoints/17000. gdb_test "jump *\$pc" "Breakpoint .* bar break .*"