From patchwork Thu Feb 1 15:21:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Toby Lloyd Davies X-Patchwork-Id: 85139 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C263238582BC for ; Thu, 1 Feb 2024 15:24:53 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 0EE4338582A7 for ; Thu, 1 Feb 2024 15:23:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0EE4338582A7 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=undo.io Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=undo.io ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0EE4338582A7 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706801011; cv=none; b=ODtR1o1wxDRUAdMaDDRkLzFOZS5xRcWHnv03nCLXLkDoXvCHSudoLUyn6FQrIRj5SJc5EPa/FoyVQL5wavwkWC/bKVV6BPp86yX5sCEvFUfGgCF7Kn1bdnyCHhWkRAi1LyIllWtJ5E7XwTGnYTKRvXZsoTcZMT9Y6gcwZsYC0Nk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706801011; c=relaxed/simple; bh=CtBdLoHrTe7QixbdAlJBF3YmRWkns2UsRYHdBARKQ18=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=mXENNnEW1HSjRuza6Kn/rEYTxWkdBDnQCd2sHoynjwR/qGXquo6SqIvKZTMZspxIldgwh6YpqyEsjL/pCPlcRNXoxt+4ioHqkfchmgTluq0bRLsB3g02F5EfbIkixtImwIir7zHohsuFIKui8K7pxqMF6fbZYeGYfEMvtiZwiJk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-40fb94d42e4so8322215e9.2 for ; Thu, 01 Feb 2024 07:23:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=undo.io; s=google2; t=1706801006; x=1707405806; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=doI3kBbeDXhwM9GHcT6nVhXCOCJfM0qnkYeMcKaNd0I=; b=HnU4utwJ3VU4VZhkgaDP7QYmGk1dR1UlAtT9gp1gZl8ex2jo25mjGB87U4/aXuSk5D MdoyUZPgVmsRqUsqweDu2hsukj0uH85iMnV232FhgvV48jbCAcMwLFjakfrHxOaCyL62 U0MAX6MGrV45R3rfIw5sBnqwN5GJj3rfDfVko+wydy1bjA3YkuD0UP8Qip168VLo8J8w mPKVFafo4JcJMsxTLhCP0QAVakrdLYo/oC7XpFKQpQFVU2Epv8r4N0yuM/mIUN7sV1J7 RjFqIs/gmkeOYWC3c80PnkQ6/Tw64p7r0R6H8nfQgiS5UsWoN6R9agtRCI8U5S5qBUAG GSlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706801006; x=1707405806; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=doI3kBbeDXhwM9GHcT6nVhXCOCJfM0qnkYeMcKaNd0I=; b=MrB2A7/E4dD7UfPiTnU//KTTe9Q9t5CYVAtH9yBVxSDtDVaiOhANTKzHhPD6XF05M9 sfCo8ofuUcjwyssbSbI2UOStiLkcPbekBvDhN/ero7BNr49KgF4qyLV80k37cvAveWld 9+Q2fmUt75IdfPHOGghnNtUW584cE7IDCyj8jwAbobi86hNOQZ7xf5uodMcR4h2I5gD7 puVWPKdTtuz8Foenb7cg8YXYJ1vYe7cqhgTE5Xya+d6HXaxUqWRnC7PoC9FOMlw4wZnA 92K/nKkQCvkynKfoHaNSlm5Vsy5/WZVImzX0/uz43kpo7oFBCRsemR8IvR+D/SVhgbvV Vb/Q== X-Gm-Message-State: AOJu0YzoLqMCdOpIqsPqUTwY3H927MwezGFuOpOvOhqMLSlAoOXNmBq5 K49VXgrGc0jM8dGrebNxTtLfNyPVxNF8k7o/K+zzg9DBVhCZIPqLK78bXtvRrEaFT4/OYEp4Y7o Rr+6WWuNwQzaMbT+L96WQ1qylBekp3F4HNChIDK93M6fcFenvvUCmi8RJYkdMZss1p/4jVgjGLT Tjes4P4R+xGluRi3MnPcFLMQwtAy4TiasdMoUuBwzTJBU= X-Google-Smtp-Source: AGHT+IFXFBNB27MAwijSwHU19BK1aiQObEY7+2rPefwHLo8HdEWPTGiRUlxTDDooMT2YC50YA7LpGg== X-Received: by 2002:a05:600c:3c8a:b0:40f:be00:1465 with SMTP id bg10-20020a05600c3c8a00b0040fbe001465mr1316179wmb.39.1706801005806; Thu, 01 Feb 2024 07:23:25 -0800 (PST) Received: from redhawk-thinkpad.home (cfbb000029.b.cam.camfibre.uk. [185.219.110.154]) by smtp.gmail.com with ESMTPSA id r2-20020a05600c320200b0040ef04987e7sm4598093wmp.16.2024.02.01.07.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Feb 2024 07:23:25 -0800 (PST) From: Toby Lloyd Davies To: gdb-patches@sourceware.org Cc: Toby Lloyd Davies Subject: [PATCH 3/3] gdb: Fix 'next' skipping over remainder of inline function Date: Thu, 1 Feb 2024 15:21:18 +0000 Message-Id: <20240201152118.598375-4-tlloyddavies@undo.io> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240201152118.598375-1-tlloyddavies@undo.io> References: <20240201152118.598375-1-tlloyddavies@undo.io> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 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, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org An inline function's instructions are often interleaved with it's callers. When stepping out of the inline function and then back into it again we would treat this as stepping into a new inline function. Thus a 'next' command from within a inline function could skip over the rest of the inline function. This occurred because the step_frame_id was refreshed at the end of process_event_stop_test when it detected that we had changed frame. Fix this by refreshing step info only when the stack frame has changed. However, not refreshing the step info when we step out of an inline frame poses a problem if we subsequently step into a different inline frame. The code wouldn't be able to determine whether we had stepped into or out to this inline function, and thus whether a 'next' command should skip it. If that inline function was in the backtrace when we started stepping then we don't want to skip it but if it was not in the backtrace then we want to skip it. Therefore, to correctly determine when we have stepped into an inline frame requires knowledge of every inline frame that was being stepped through when the command was issued. So store these inline frame ids in a vector. Then we can treat us as having stepped into an inline function if we can't find the current frame in the vector and we haven't stepped into a different stack frame. Additionally, this fixes another bug. When stepping out of an inline function and immediately entering into another inline function we would not detect this as stepping into an inline function and thus a 'next' command wouldn't step over the new inline function. Note that in this case we would normally detect this as the entry point to the inline function and create a inline callsite here, but if we didn't detect this as an entry point then we would incorrectly step into the function. --- gdb/gdbthread.h | 6 + gdb/infrun.c | 57 +++--- .../gdb.dwarf2/dw2-inline-stepping-4.c | 59 +++++++ .../gdb.dwarf2/dw2-inline-stepping-4.exp | 167 ++++++++++++++++++ 4 files changed, 260 insertions(+), 29 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.exp diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h index e7035d40ad4..ddfd4ecc965 100644 --- a/gdb/gdbthread.h +++ b/gdb/gdbthread.h @@ -146,6 +146,12 @@ struct thread_control_state any inlined frames). */ struct frame_id step_stack_frame_id {}; + /* Frame IDs of inline frames as of when stepping command was issued. + From newest inline frame until newest non-inline frame + (exclusive). This is how we know when we step into an inlined + subroutine call. */ + std::vector step_inline_frame_ids {}; + /* True if the the thread is presently stepping over a breakpoint or a watchpoint, either with an inline step over or a displaced (out of line) step, and we're now expecting it to report a trap for diff --git a/gdb/infrun.c b/gdb/infrun.c index 927e464e479..845341b8fff 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -4780,6 +4780,13 @@ set_step_info (thread_info *tp, frame_info_ptr frame, tp->control.step_frame_id = get_frame_id (frame); tp->control.step_stack_frame_id = get_stack_frame_id (frame); + tp->control.step_inline_frame_ids.clear (); + for (frame_info_ptr f = frame; + f && get_frame_type (f) == INLINE_FRAME; + f = get_prev_frame (f)) + { + tp->control.step_inline_frame_ids.push_back (get_frame_id (f)); + } tp->current_symtab = sal.symtab; tp->current_line = sal.line; @@ -5002,23 +5009,6 @@ adjust_pc_after_break (struct thread_info *thread, } } -static bool -stepped_in_from (frame_info_ptr frame, struct frame_id step_frame_id) -{ - for (frame = get_prev_frame (frame); - frame != nullptr; - frame = get_prev_frame (frame)) - { - if (get_frame_id (frame) == step_frame_id) - return true; - - if (get_frame_type (frame) != INLINE_FRAME) - break; - } - - return false; -} - /* Look for an inline frame that is marked for skip. If PREV_FRAME is TRUE start at the previous frame, otherwise start at the current frame. Stop at the @@ -5039,11 +5029,16 @@ inline_frame_is_marked_for_skip (bool prev_frame, struct thread_info *tp) symtab_and_line sal; struct symbol *sym; - if (get_frame_id (frame) == tp->control.step_frame_id) - break; if (get_frame_type (frame) != INLINE_FRAME) break; + /* Don't skip frames we were already stepping through. */ + if (std::find (tp->control.step_inline_frame_ids.begin (), + tp->control.step_inline_frame_ids.end (), + get_frame_id (frame)) + != tp->control.step_inline_frame_ids.end ()) + break; + sal = find_frame_sal (frame); sym = get_frame_function (frame); @@ -8091,14 +8086,17 @@ process_event_stop_test (struct execution_control_state *ecs) } /* Look for "calls" to inlined functions, part one. If we are still - in the same real function we were stepping through, but we have - to go further up to find the exact frame ID, we are stepping + in the same real function we were stepping through, but we were not + previously stepping through this inline function, we are stepping through a more inlined call beyond its call site. */ if (get_frame_type (get_current_frame ()) == INLINE_FRAME - && (*curr_frame_id != original_frame_id) - && stepped_in_from (get_current_frame (), - ecs->event_thread->control.step_frame_id)) + && (std::find (ecs->event_thread->control.step_inline_frame_ids.begin (), + ecs->event_thread->control.step_inline_frame_ids.end (), + *curr_frame_id) + == ecs->event_thread->control.step_inline_frame_ids.end ()) + && (get_stack_frame_id (get_current_frame ()) + == ecs->event_thread->control.step_stack_frame_id)) { infrun_debug_printf ("stepping through inlined function"); @@ -8208,10 +8206,11 @@ process_event_stop_test (struct execution_control_state *ecs) end_stepping_range (ecs); return; } - else if (*curr_frame_id == original_frame_id) + else if (get_stack_frame_id (get_current_frame ()) + == ecs->event_thread->control.step_stack_frame_id) { /* We are not at the start of a statement, and we have not changed - frame. + stack frame. We ignore this line table entry, and continue stepping forward, looking for a better place to stop. */ @@ -8221,15 +8220,15 @@ process_event_stop_test (struct execution_control_state *ecs) } else { - /* We are not the start of a statement, and we have changed frame. + /* We are not the start of a statement, and we have changed stack frame. We ignore this line table entry, and continue stepping forward, looking for a better place to stop. Keep refresh_step_info at - true to note that the frame has changed, but ignore the line + true to note that the stack frame has changed, but ignore the line number to make sure we don't ignore a subsequent entry with the same line number. */ stop_pc_sal.line = 0; - infrun_debug_printf ("stepped to a different frame, but " + infrun_debug_printf ("stepped to a different stack frame, but " "it's not the start of a statement"); } } diff --git a/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.c b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.c new file mode 100644 index 00000000000..3097fed5131 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.c @@ -0,0 +1,59 @@ +/* Copyright 2019-2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the FOOU 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 + FOOU General Public License for more details. + + You should have received a copy of the FOOU General Public License + along with this program. If not, see . */ + +/* This test relies on foo_a, foo_b and bar being inlined into main */ + +volatile int global_var; + +static inline void __attribute__ ((always_inline)) +foo_a () +{ + asm ("foo_label: .globl foo_label"); + global_var++; /* foo inc global_var */ +} + +static inline void __attribute__ ((always_inline)) +foo_b () +{ + asm ("foo_label2: .globl foo_label2"); + asm ("nop"); /* foo nop */ + asm ("foo_label3: .globl foo_label3"); + asm ("jmp bar_label2"); /* foo jmp bar */ +} /* foo end */ + +static inline void __attribute__ ((always_inline)) +bar () +{ + asm ("bar_label: .globl bar_label"); + asm ("nop"); /* bar nop */ + asm ("bar_label2: .globl bar_label2"); + global_var++; /* bar inc global_var */ +} /* bar end */ + +int +main () +{ /* main prologue */ + asm ("main_label: .globl main_label"); + global_var = 0; /* main set global_var */ + asm ("main_label2: .globl main_label2"); + foo_a (); /* main call foo */ + asm ("main_label3: .globl main_label3"); + asm ("jmp foo_label3"); /* main jmp foo */ + foo_b (); + asm ("main_label4: .globl main_label4"); + bar(); /* main call bar */ + asm ("main_label5: .globl main_label5"); + return global_var; /* main return global_var */ +} /* main end */ diff --git a/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.exp b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.exp new file mode 100644 index 00000000000..6abba731c88 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-inline-stepping-4.exp @@ -0,0 +1,167 @@ +# Copyright 2019-2024 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 . +# +# Inline functions often have instructions interleaved with their +# callers. Test that when the 'next' command steps out of an inline +# function and then back into it again we do not skip the rest of the +# inline function. +# +# Additionally, test that when the 'next' command steps out of one +# inline function and immediately enters another inline function +# (without going to the callsite), we skip over the inline function. +# +# The testfile uses jumps into inline functions so that we do not stop +# at the inline callsite lines. + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use gas. +require dwarf2_support + +# The .c files use __attribute__. +require is_c_compiler_gcc + +standard_testfile .c .S + +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + global srcdir subdir srcfile srcfile2 + declare_labels cu_ranges_label foo_ranges_label lines_label bar_prog foo_prog + + lassign [function_range main [list ${srcdir}/${subdir}/$srcfile]] \ + main_start main_len + set main_end "$main_start + $main_len" + + set bar_call_line [gdb_get_line_number "main call bar"] + set foo_call_line [gdb_get_line_number "main call foo"] + + cu {} { + compile_unit { + {language @DW_LANG_C} + {name dw2-inline-stepping-2.c} + {low_pc 0 addr} + {stmt_list ${lines_label} DW_FORM_sec_offset} + {ranges ${cu_ranges_label} DW_FORM_sec_offset} + } { + bar_prog: subprogram { + {name bar} + {inline 3 data1} + } + foo_prog: subprogram { + {name bar} + {inline 3 data1} + } + subprogram { + {external 1 flag} + {name main} + {low_pc $main_start addr} + {high_pc "$main_start + $main_len" addr} + } { + inlined_subroutine { + {abstract_origin %$foo_prog} + {ranges ${foo_ranges_label} DW_FORM_sec_offset} + {call_file 1 data1} + {call_line $foo_call_line data1} + } + inlined_subroutine { + {abstract_origin %$bar_prog} + {low_pc bar_label addr} + {high_pc main_label5 addr} + {call_file 1 data1} + {call_line $bar_call_line data1} + } + } + } + } + + lines {version 2} lines_label { + include_dir "${srcdir}/${subdir}" + file_name "$srcfile" 1 + + program { + + DW_LNE_set_address main_label + line [gdb_get_line_number "main set global_var"] + DW_LNS_copy + + DW_LNE_set_address main_label2 + line [gdb_get_line_number "main call foo"] + DW_LNS_copy + + DW_LNE_set_address foo_label + line [gdb_get_line_number "foo inc global_var"] + DW_LNS_copy + + DW_LNE_set_address main_label3 + line [gdb_get_line_number "main jmp foo"] + DW_LNS_negate_stmt + DW_LNS_copy + DW_LNS_negate_stmt + + + DW_LNE_set_address foo_label2 + line [gdb_get_line_number "bar nop"] + DW_LNS_copy + + DW_LNE_set_address foo_label3 + line [gdb_get_line_number "foo jmp bar"] + DW_LNS_copy + + DW_LNE_set_address main_label4 + line [gdb_get_line_number "main call bar"] + DW_LNS_copy + + DW_LNE_set_address bar_label + line [gdb_get_line_number "bar nop"] + DW_LNS_copy + + DW_LNE_set_address bar_label2 + line [gdb_get_line_number "bar inc global_var"] + DW_LNS_copy + + DW_LNE_set_address main_label5 + line [gdb_get_line_number "main return global_var"] + DW_LNS_copy + DW_LNE_set_address $main_end + DW_LNE_end_sequence + } + } + + ranges {is_64 [is_64_target]} { + cu_ranges_label: sequence { + range ${main_start} ${main_end} + } + foo_ranges_label: sequence { + range {foo_label} {main_label3} + range {foo_label2} {main_label4} + } + } +} + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +gdb_test "step" ".*main call foo.*" "step to foo callsite" +gdb_test "step" ".*foo inc global_var.*" "step into foo" + +gdb_test "next" ".foo jmp bar.*" "next within foo" + +gdb_test "next" ".*return global_var.*" "next over bar"