From patchwork Tue Jan 14 14:31:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 104751 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 239F63856DF9 for ; Tue, 14 Jan 2025 14:33:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 239F63856DF9 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=I8XADTEU X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 402963856DF9 for ; Tue, 14 Jan 2025 14:32:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 402963856DF9 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 402963856DF9 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736865121; cv=none; b=U+Ot0he5J01DsbIkDXhJYJG5aRstk3RIDgTUIhsrLn+YdXePGoGvlhrmuqTgQgqJJqUoX0IrZrnDKC//sjku2paRVR9EVbkyWNZpObl5oM8v1A9G2WBYCTJh3ZKUYuUIyXfs6xHr9PYqrm+2HoEMmV9BBkuvU1WYBoV29NfxU1c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736865121; c=relaxed/simple; bh=+HKDzIJg3Xld+XJosE7cEQqxF/TAkbfqylf3xsXl0dM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=V6Ae6tNtEQO8HO7c6kzikT+Xq9AfTt+i70xXWmsjMSHzPRraf6CaFW02e8IixcGPnLNef56aL3UlqnU/4d1uATi0eCP+GcIl8yR1OVWdywVjBSld9EE1suZ17sevf7actjfsOLMXx/3TzibjbJ9WsU3bFkp68VOiDZlsVIvIYKw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 402963856DF9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736865121; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LWniYTiTKoPGU2L0vyYC13+MBgkXxRrnw1g63HY3Bq4=; b=I8XADTEULcWSIRSizeyARZyDRLs9e2sjRIKw/o324jIuA03A+JF5J7YvwOPeQjbTZE02/0 V9PL1kl6g6ElPFVraXy20Wxl49417UhdxA02kVr8ED0GREXJLZTBN2igBRD/6P6UMTpBiq whAKMyZeXa6ZChDhbFPyOBGsvTdLz8A= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-630-cNRUipF8Nu-KT_6jTplC6g-1; Tue, 14 Jan 2025 09:31:59 -0500 X-MC-Unique: cNRUipF8Nu-KT_6jTplC6g-1 X-Mimecast-MFC-AGG-ID: cNRUipF8Nu-KT_6jTplC6g Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-385d51ba2f5so2785444f8f.2 for ; Tue, 14 Jan 2025 06:31:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736865117; x=1737469917; 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=LWniYTiTKoPGU2L0vyYC13+MBgkXxRrnw1g63HY3Bq4=; b=MBfyH71I84geic71vBqd9X+XaJZshaYC2cM42FAjspD3jzZDH8ix647tZQiWHi5nOE 7o/+7xluHG2ioezqI3gVW+iY4Ci6C1HqSl5WmRYfsRuxPND1tGEjiphr+w6iORADOOus NQVtI7TTTyegLePKUaOVlxFF6ojyeWn92CnozcNA6ZNz6bplT9gXlDLzdoRRs0VIXx5+ /D7dP8Sos+kTTvbral2y/Kf9EbEu7MZOIs2QOI6t/jJ9H7igHkzyp8MpN2tyy8o6km9q 6NwlZSn8dwdKlZc5imMAVj8LyU9/mu/Qb4gl1i7XqhSoWN54M6VJ94c3KZSW9pujEz61 Mw+g== X-Gm-Message-State: AOJu0YzmXmUm7gUdNhP7A0PM9dx8TMEjJTkI5XXE6TBlCloj3pZz88KV XvnwSxg+Ols9cWvF3IcET5sAGvlZHCzedUA0dDCkqCz33j3qYjLE1e2f9EB5zKiumpXHnrU8RvL c1b3JeZl5jGapIibUQDt7uKaL/vvDsh6JUzIMN/vK+/vC16rUnm7O8RIZUGqg028kaa3QOiOQwi 7kfrgAgzBeh0hHT0DYWtQ/fVtf39NlV8J6MgA1UAbZSuk= X-Gm-Gg: ASbGncsw1jvkgNlsADizvQV9XntFORvt91HgJe9dcDJPnlv5kRzOb3fn99X6Y2qo66c MQJ/A+1hFRpmBS1sAgvQhFwJKjYUEi28QYBNJNRIAZr4EGIdBT0Urb2A3+Yf1Ov4f9FMNcr/nJd GQNS/H1dl4z4gPwXC+1h/kMQTSjhjl2VfPDm3vEWCuHUa8+VO2MOzhBYknTv0qrwTX3QFtLLGQx lhypYYYGpt17QrnVfa+7BK1UgJZ5qzWo6vAP+9NIV6lJdRP3bD9KdtHECJm4N3LJvUYDtfz79GN RQ0oNA== X-Received: by 2002:a05:6000:18a8:b0:386:3403:7b63 with SMTP id ffacd0b85a97d-38a8733a319mr18859491f8f.36.1736865115332; Tue, 14 Jan 2025 06:31:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IEfDD42eeC6FpF7kw+GxhbpnDviMY3Z8hmtOxF7WYbr2/J+HElrbVCP10ThTpFxEYRhLBotDg== X-Received: by 2002:a05:6000:18a8:b0:386:3403:7b63 with SMTP id ffacd0b85a97d-38a8733a319mr18859380f8f.36.1736865113815; Tue, 14 Jan 2025 06:31:53 -0800 (PST) Received: from localhost (44.226.159.143.dyn.plus.net. [143.159.226.44]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38a8e4c1cf2sm15138994f8f.99.2025.01.14.06.31.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jan 2025 06:31:52 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Bernd Edlinger , Andrew Burgess Subject: [PATCHv3 1/2] gdb: handle empty ranges for inline subroutines Date: Tue, 14 Jan 2025 14:31:48 +0000 Message-ID: <930cd5145ebb8127f59c0475f6942832d15a2bbb.1736865029.git.aburgess@redhat.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: JGTiqetvWqNoJRBzRwKrvhP7ufp_y3oujF1OvpNhDQg_1736865118 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP 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 The work in this patch is based on changes found in this series: https://inbox.sourceware.org/gdb-patches/AS1PR01MB946510286FBF2497A6F03E83E4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com That series has the fixes here merged along with other changes, and takes a different approach for how to handle the issue addressed here. Credit for identifying the original issue belongs with Bernd, the author of the original patch, who I have included as a co-author on this patch. A brief description of how the approach taken in this patch differs from the approach Bernd took can be found at the end of this commit message. When compiling with optimisation, it can often happen that gcc will emit an inline function instance with an empty range associated. This can happen in two ways. The inline function might have a DW_AT_low_pc and DW_AT_high_pc, where the high-pc is an offset from the low-pc, but the high-pc offset is given as 0 by gcc. Alternatively, the inline function might have a DW_AT_ranges, and one of the sub-ranges might be empty, though usually in this case, other ranges will be non-empty. The second case is made worse in that sometimes gcc will specify a DW_AT_entry_pc value which points to the address of the empty sub-range. My understanding of the DWARF spec is that empty ranges as seen in these examples indicate that no instructions are associated with the inline function, and indeed, this is how GDB handles these cases, rejecting blocks and sub-ranges which are empty. DWARF-5, 2.17.2, Contiguous Address Range: The value of the DW_AT_low_pc attribute is the address of the first instruction associated with the entity. If the value of the DW_AT_high_pc is of class address, it is the address of the first location past the last instruction associated with the entity... DWARF-5, 2.17.3, Non-Contiguous Address Ranges: A bounded range entry whose beginning and ending address offsets are equal (including zero) indicates an empty range and may be ignored. As a consequence, an attempt by the user to place a breakpoint on an inline function with an empty low/high address range will trigger GDB's pending breakpoint message: (gdb) b foo Function "foo" not defined. Make breakpoint pending on future shared library load? (y or [n]) n While, having the entry-pc point at an empty range forces GDB to ignore the given entry-pc and select a suitable alternative. If instead of ignoring these empty ranges, we instead teach GDB to treat these as non-empty, what we find is that, in all the cases I've seen, the debug experience is improved. As a minimum, in the low/high case, GDB now knows about the inline function, and can place breakpoints that will be hit. Further, in most cases, local variables from the inline function can be accessed. If we do start treating empty address ranges as non-empty then we are deviating from the DWARF spec. It is not clear if we are working around a gcc bug (I suspect so), or if gcc actually considers the inline function gone, and we're just getting lucky that the debug experience seems improved. My proposed strategy for handling these empty address ranges is to only perform this work around if the compiler is gcc, so far I've not seen this issue with Clang (the only other compiler I've tested), though extending this to other compilers in the future would be trivial. Additionally, I only apply the work around for DW_TAG_inlined_subroutine DIEs, as I've only seen the issue for inline functions. If we find a suitable empty address range then the fix-up is to give the address range a length of 1 byte. Now clearly, in most cases, 1 byte isn't even going to cover a single instruction, but so far this doesn't seem to be a problem. An alternative to using a 1-byte range would be to try and disassemble the code at the given address, calculate the instruction length, and use that, the length of one instruction. But this means that the DWARF parser now needs to make use of the disassembler, which feels like a big change that I'd rather avoid if possible. The other alternative is to allow blocks to be created with zero length address ranges and then change the rest of GDB to allow for lookup of zero sized blocks to succeed. This is the approach taken by the original patch series that I linked above. The results achieved by the original patch are impressive, and Bernd, the original patch author, makes a good argument that at least some of the problems relating to empty ranges are a result of deficiencies in the DWARF specification rather than issues with gcc. However, I remain unconvinced. But even if I accept that the issue is with DWARF itself rather than gcc, the question still remains; should we fix the problem by synthesising new DWARF attributes and/or accept non-standard DWARF during the dwarf2/read.c phase, and then update GDB to handle the new reality, or should we modify the incoming DWARF as we read it to make it fit GDB's existing algorithms. The original patch, I believe, took the former approach, while I favour the later, and so, for now, I propose that the single byte range proposal is good enough, at least until we find counter examples where this doesn't work. This leaves just one question: what about the remaining work in the original patch. That work deals with problems around the end address of non-empty ranges. The original patch handled that case using the same algorithm changes, which is neat, but I think there are alternative solutions that should be investigated. If the alternatives don't end up working out, then it's trivial to revert this patch in the future and adopt the original proposal. For testing I have two approaches, C/C++ test compiled with optimisation that show the problems discussed. These are good because they show that these issues do crop up in compiled code. But they are bad in that the next compiler version might change the way the test is optimised such that the problem no longer shows. And so I've backed up the real code tests with DWARF assembler tests which reproduce each issue. The DWARF assembler tests are not really impacted by which gcc version is used, but I've run all of these tests using gcc versions 8.4.0, 9.5.0, 10.5.0, 11.5.0, 12.2.0, and 14.2.0. I see failures in all of the new tests when using an unpatched GDB, and no failures when using a patched GDB. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25987 Co-Authored-By: Bernd Edlinger --- gdb/dwarf2/read.c | 71 ++++- gdb/testsuite/gdb.cp/step-and-next-inline.exp | 145 +++++----- .../gdb.dwarf2/dw2-empty-inline-low-high.c | 39 +++ .../gdb.dwarf2/dw2-empty-inline-low-high.exp | 128 +++++++++ .../gdb.dwarf2/dw2-empty-inline-ranges.c | 54 ++++ .../gdb.dwarf2/dw2-empty-inline-ranges.exp | 261 ++++++++++++++++++ .../gdb.dwarf2/dw2-unexpected-entry-pc.exp | 67 +++-- gdb/testsuite/gdb.opt/empty-inline-cxx.cc | 65 +++++ gdb/testsuite/gdb.opt/empty-inline-cxx.exp | 96 +++++++ gdb/testsuite/gdb.opt/empty-inline.c | 40 +++ gdb/testsuite/gdb.opt/empty-inline.exp | 116 ++++++++ 11 files changed, 998 insertions(+), 84 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.exp create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.exp create mode 100644 gdb/testsuite/gdb.opt/empty-inline-cxx.cc create mode 100644 gdb/testsuite/gdb.opt/empty-inline-cxx.exp create mode 100644 gdb/testsuite/gdb.opt/empty-inline.c create mode 100644 gdb/testsuite/gdb.opt/empty-inline.exp diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index e52e26f340c..c7b487a8cd6 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -10704,6 +10704,17 @@ read_variable (struct die_info *die, struct dwarf2_cu *cu) } } +/* Return true if an empty range associated with an entry of type TAG in + CU should be "fixed", that is, converted to a single byte, non-empty + range. */ + +static bool +dwarf_fixup_empty_range (struct dwarf2_cu *cu, dwarf_tag tag) +{ + return (tag == DW_TAG_inlined_subroutine + && producer_is_gcc (cu->producer, nullptr, nullptr)); +} + /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_rnglists. Callback's type should be: @@ -10866,7 +10877,12 @@ dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu, /* Empty range entries have no effect. */ if (range_beginning == range_end) - continue; + { + if (dwarf_fixup_empty_range (cu, tag)) + range_end = (unrelocated_addr) ((CORE_ADDR) range_end + 1); + else + continue; + } /* Only DW_RLE_offset_pair needs the base address added. */ if (rlet == DW_RLE_offset_pair) @@ -10988,7 +11004,12 @@ dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag, /* Empty range entries have no effect. */ if (range_beginning == range_end) - continue; + { + if (dwarf_fixup_empty_range (cu, tag)) + range_end = (unrelocated_addr) ((CORE_ADDR) range_end + 1); + else + continue; + } range_beginning = (unrelocated_addr) ((CORE_ADDR) range_beginning + (CORE_ADDR) *base); @@ -11211,9 +11232,24 @@ dwarf2_get_pc_bounds (struct die_info *die, unrelocated_addr *lowpc, if (ret == PC_BOUNDS_NOT_PRESENT || ret == PC_BOUNDS_INVALID) return ret; - /* partial_die_info::read has also the strict LOW < HIGH requirement. */ + /* These LOW and HIGH values will be used to create a block. A block's + high address is the first address after the block's address range, so + if 'high <= low' then the block has no code associated with it. */ if (high <= low) - return PC_BOUNDS_INVALID; + { + /* In some cases though, when the blocks LOW / HIGH were defined with + the DW_AT_low_pc and DW_AT_high_pc, we see some compilers create + an empty block when we can provide a better debug experience by + having a non-empty block. We do this by "fixing" the block to be + a single byte in length. See dwarf_fixup_empty_range for when + this fixup is performed. */ + if (high == low + && ret == PC_BOUNDS_HIGH_LOW + && dwarf_fixup_empty_range (cu, die->tag)) + high = (unrelocated_addr) (((ULONGEST) low) + 1); + else + return PC_BOUNDS_INVALID; + } /* When using the GNU linker, .gnu.linkonce. sections are used to eliminate duplicate copies of functions and vtables and such. @@ -11500,8 +11536,33 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, CORE_ADDR low = per_objfile->relocate (unrel_low); CORE_ADDR high = per_objfile->relocate (unrel_high); + fixup_low_high_pc (cu, die, &low, &high); - cu->get_builder ()->record_block_range (block, low, high - 1); + + /* Blocks where 'high < low' should be rejected earlier in the + process, e.g. see dwarf2_get_pc_bounds. */ + gdb_assert (high >= low); + + /* The value of HIGH is the first address past the end, but + GDB stores ranges with the high value as last inclusive + address, so in most cases we need to decrement HIGH here. + + Blocks where 'high == low' represent an empty block (i.e. a + block with no associated code). + + When 'high == low' and dwarf_fixup_empty_range returns true we + "fix" the empty range into a single byte range, which we can + do by leaving HIGH untouched. Otherwise we decrement HIGH, + which might result in 'high < low'. */ + if (high > low || !dwarf_fixup_empty_range (cu, die->tag)) + high -= 1; + + /* If the above decrement resulted in 'high < low' then this + represents an empty range. There's little point storing this + in GDB's internal structures, it's just more to search + through, and it will never match any address. */ + if (high >= low) + cu->get_builder ()->record_block_range (block, low, high); } attr = dwarf2_attr (die, DW_AT_ranges, cu); diff --git a/gdb/testsuite/gdb.cp/step-and-next-inline.exp b/gdb/testsuite/gdb.cp/step-and-next-inline.exp index e16c2cca82d..fd47285fb88 100644 --- a/gdb/testsuite/gdb.cp/step-and-next-inline.exp +++ b/gdb/testsuite/gdb.cp/step-and-next-inline.exp @@ -86,18 +86,16 @@ proc do_test { use_header } { } gdb_test "bt" "\\s*\\#0\\s+main.*" "in main" - set line1 {\t\{} - set line2 {\t if \(t != NULL} - gdb_test_multiple "step" "step into get_alias_set" { - -re -wrap $line1 { - gdb_test "next" $line2 $gdb_test_name - } - -re -wrap $line2 { - pass $gdb_test_name - } - } + gdb_test "step" \ + [multi_line \ + "get_alias_set \\(t=\[^\r\n\]+\\) at \[^\r\n\]+:$::decimal" \ + "$::decimal\\s+if \\(t != NULL\\s*"] \ + "step into get_alias_set" gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ "not in inline 1" + gdb_test "next" ".*TREE_TYPE.*" "next step 1" + gdb_test "bt" "#0\\s+get_alias_set\[^\r\n\]*${srcfile}:.*" \ + "not in inline 2" # It's possible that this first failure (when not using a header # file) is GCC's fault, though the remaining failures would best @@ -107,27 +105,6 @@ proc do_test { use_header } { # having location view support, so for now it is tagged as such. set have_kfail [expr [test_compiler_info gcc*] && !$use_header] - set ok 1 - gdb_test_multiple "next" "next step 1" { - -re -wrap "if \\(t->x != i\\)" { - set ok 0 - send_gdb "next\n" - exp_continue - } - -re -wrap ".*TREE_TYPE.* != 1" { - if { $ok } { - pass $gdb_test_name - } else { - if { $have_kfail } { - setup_kfail "*-*-*" symtab/25507 - } - fail $gdb_test_name - } - } - } - gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ - "not in inline 2" - set ok 1 gdb_test_multiple "next" "next step 2" { -re -wrap "return x;" { @@ -196,12 +173,49 @@ proc do_test { use_header } { "\\s*\\#0\\s+(main|get_alias_set)\[^\r\]*${srcfile}:.*" \ "not in inline 5" - if {!$use_header} { - # With the debug from GCC 10.x (and earlier) GDB is currently - # unable to successfully complete the following tests when we - # are not using a header file. - kfail symtab/25507 "stepping tests" - return + # Clang at least upto v16 doesn't include line number information + # for anything but the first line of the 'tree_check' inline + # function. As a result, these tests will fail. + if { ![test_compiler_info "clang-*" "c++"] } { + clean_restart ${executable} + + if ![runto_main] { + return + } + + gdb_test "bt" "\\s*\\#0\\s+main.*" "in main pass 2" + gdb_test "step" \ + [multi_line \ + "get_alias_set \\(t=\[^\r\n\]+\\) at \[^\r\n\]+:$::decimal" \ + "$::decimal\\s+if \\(t != NULL\\s*"] \ + "step into get_alias_set, pass 2" + gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ + "in get_alias_set pass 2" + gdb_test "step" ".*TREE_TYPE.*" "step 1" + gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ + "not in inline 1 pass 2" + gdb_test "step" ".*if \\(t->x != i\\).*" "step 2" + gdb_test "bt" "\\s*\\#0\\s+\[^\r\]*tree_check\[^\r\]*${hdrfile}:.*" \ + "in inline 1 pass 2" + gdb_test "step" ".*return x.*" "step 3" + gdb_test "step" ".*TREE_TYPE.*" "step 4" + gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ + "not in inline 2 pass 2" + gdb_test "step" ".*if \\(t->x != i\\).*" "step 5" + gdb_test "bt" "\\s*\\#0\\s+\[^\r\]*tree_check\[^\r\]*${hdrfile}:.*" \ + "in inline 2 pass 2" + gdb_test "step" ".*return x.*" "step 6" + gdb_test "step" ".*TREE_TYPE.*" "step 7" + gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ + "not in inline 3 pass 2" + gdb_test "step" ".*if \\(t->x != i\\).*" "step 8" + gdb_test "bt" "\\s*\\#0\\s+\[^\r\]*tree_check\[^\r\]*${hdrfile}:.*" \ + "in inline 3 pass 2" + gdb_test "step" "return x.*" "step 9" + gdb_test "step" "return 0.*" "step 10" + gdb_test "bt" \ + "\\s*\\#0\\s+(main|get_alias_set)\[^\r\]*${srcfile}:.*" \ + "not in inline 4 pass 2" } clean_restart ${executable} @@ -210,32 +224,37 @@ proc do_test { use_header } { return } - gdb_test "bt" "\\s*\\#0\\s+main.*" "in main pass 2" - gdb_test "step" ".*" "step into get_alias_set pass 2" - gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ - "in get_alias_set pass 2" - gdb_test "step" ".*TREE_TYPE.*" "step 1" - gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ - "not in inline 1 pass 2" - gdb_test "step" ".*if \\(t->x != i\\).*" "step 2" - gdb_test "bt" "\\s*\\#0\\s+\[^\r\]*tree_check\[^\r\]*${hdrfile}:.*" \ - "in inline 1 pass 2" - gdb_test "step" ".*TREE_TYPE.*" "step 3" - gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ - "not in inline 2 pass 2" - gdb_test "step" ".*if \\(t->x != i\\).*" "step 4" - gdb_test "bt" "\\s*\\#0\\s+\[^\r\]*tree_check\[^\r\]*${hdrfile}:.*" \ - "in inline 2 pass 2" - gdb_test "step" ".*TREE_TYPE.*" "step 5" - gdb_test "bt" "\\s*\\#0\\s+get_alias_set\[^\r\]*${srcfile}:.*" \ - "not in inline 3 pass 2" - gdb_test "step" ".*if \\(t->x != i\\).*" "step 6" - gdb_test "bt" "\\s*\\#0\\s+\[^\r\]*tree_check\[^\r\]*${hdrfile}:.*" \ - "in inline 3 pass 2" - gdb_test "step" "return 0.*" "step 7" - gdb_test "bt" \ - "\\s*\\#0\\s+(main|get_alias_set)\[^\r\]*${srcfile}:.*" \ - "not in inline 4 pass 2" + gdb_test "bt" "#0\\s+main.*" "in main pass 3" + gdb_test "step" \ + [multi_line \ + "get_alias_set \\(t=\[^\r\n\]+\\) at \[^\r\n\]+:$::decimal" \ + "$::decimal\\s+if \\(t != NULL\\s*"] \ + "step into get_alias_set, pass 3" + gdb_test "bt" "#0\\s+get_alias_set\[^\r\n\]*${srcfile}:.*" \ + "in get_alias_set pass 3" + gdb_test "step" ".*TREE_TYPE.*" "step 1 pass 3" + gdb_test "bt" "#0\\s+get_alias_set\[^\r\n\]*${srcfile}:.*" \ + "not in inline 1 pass 3" + gdb_test "step" ".*if \\(t->x != i\\).*" "step 2 pass 3" + gdb_test "bt" "#0\\s+\[^\r\n\]*tree_check\[^\r\n\]*${hdrfile}:.*" \ + "in inline 1 pass 3" + gdb_test_multiple "p t->x = 2" "change value pass 3" { + -re ".*value has been optimized out.*$::gdb_prompt $" { + gdb_test "p xx.x = 2" ".* = 2.*" $gdb_test_name + } + -re ".* = 2.*$::gdb_prompt $" { + pass $gdb_test_name + } + } + + # Clang at least upto v16 doesn't include line number information + # for anything but the first line of the 'tree_check' inline + # function. As a result, this test will fail. + if { ![test_compiler_info "clang-*" "c++"] } { + gdb_test "step" ".*abort.*" "step 3, pass 3" + gdb_test "bt" "#0\\s+\[^\r\n\]*tree_check\[^\r\n\]*${hdrfile}:.*" \ + "abort from inline 1 pass 3" + } } } diff --git a/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.c b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.c new file mode 100644 index 00000000000..335995b48f3 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.c @@ -0,0 +1,39 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 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 . */ + +volatile int global_var = 0; + +int +main (void) +{ /* main decl line */ + asm ("main_label: .globl main_label"); + ++global_var; + + asm ("main_0: .globl main_0"); + ++global_var; + + asm ("main_1: .globl main_1"); + ++global_var; /* foo call line */ + + asm ("main_2: .globl main_2"); + ++global_var; + + asm ("main_3: .globl main_3"); + ++global_var; + + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.exp b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.exp new file mode 100644 index 00000000000..4b13ced9780 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-low-high.exp @@ -0,0 +1,128 @@ +# Copyright 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 . + +# Define an inline function `foo` within the function `main`. The +# function `foo` uses DW_AT_low_pc and DW_AT_high_pc to define its +# range, except that DW_AT_high_pc is the constant 0. +# +# This should indicate that there is no code associated with `foo`, +# however, with gcc versions at least between 8.x and 14.x (latest at +# the time of writing this comment), it is observed that when these +# empty inline functions are created, if GDB stops at the address +# given in DW_AT_low_pc, then locals associated with the inline +# function can usually be read. +# +# At the very least, stopping at the location of the inline function +# means that the user can place a breakpoint on the inline function +# and have GDB stop in a suitable location, that alone is helpful. +# +# This test defines an inline function, places a breakpoint, and then +# runs and expects GDB to stop, and report the stop as being inside +# the inline function. +# +# We then check that the next outer frame is `main` as expected, and +# that the block for `foo` has been extended to a single byte, which +# is how GDB gives the previously empty block some range. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile .c .S + +# Lines we reference in the generated DWARF. +set main_decl_line [gdb_get_line_number "main decl line"] +set foo_call_line [gdb_get_line_number "foo call line"] + +get_func_info main + +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + upvar entry_label entry_label + + declare_labels lines_table inline_func + + cu { } { + compile_unit { + {producer "GNU C 14.1.0"} + {language @DW_LANG_C} + {name $::srcfile} + {comp_dir /tmp} + {low_pc 0 addr} + {DW_AT_stmt_list $lines_table DW_FORM_sec_offset} + } { + inline_func: subprogram { + {name foo} + {inline @DW_INL_declared_inlined} + } + subprogram { + {name main} + {decl_file 1 data1} + {decl_line $::main_decl_line data1} + {decl_column 1 data1} + {low_pc $::main_start addr} + {high_pc $::main_len data4} + {external 1 flag} + } { + inlined_subroutine { + {abstract_origin %$inline_func} + {call_file 1 data1} + {call_line $::foo_call_line data1} + {low_pc main_1 addr} + {high_pc 0 data4} + } + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$::srcfile" 1 + } +} + +if {[prepare_for_testing "failed to prepare" $testfile \ + [list $srcfile $asm_file] {nodebug}]} { + return +} + +if {![runto_main]} { + return +} + +gdb_breakpoint foo +gdb_test "continue" \ + "Breakpoint $decimal, $hex in foo \\(\\)" \ + "continue to b/p in foo" + +set foo_start [get_hexadecimal_valueof "&main_1" "*UNKNOWN*" \ + "get address of foo start"] +set foo_end [get_hexadecimal_valueof "&main_1 + 1" "*UNKNOWN*" \ + "get address of foo end"] + +gdb_test "maintenance info blocks" \ + [multi_line \ + "\\\[\\(block \\*\\) $hex\\\] $foo_start\\.\\.$foo_end" \ + " entry pc: $foo_start" \ + " inline function: foo" \ + " symbol count: $decimal" \ + " is contiguous"] \ + "block for foo has some content" + +gdb_test "frame 1" \ + [multi_line \ + "#1 main \\(\\) at \[^\r\n\]+/$srcfile:$foo_call_line" \ + "$foo_call_line\\s+\[^\r\n\]+/\\* foo call line \\*/"] \ + "frame 1 is for main" diff --git a/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.c b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.c new file mode 100644 index 00000000000..80462b7bad2 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.c @@ -0,0 +1,54 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 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 . */ + +volatile int global_var = 0; + +int +main (void) +{ /* main decl line */ + asm ("main_label: .globl main_label"); + ++global_var; + + asm ("main_0: .globl main_0"); + ++global_var; + + asm ("main_1: .globl main_1"); + ++global_var; /* foo call line */ + + asm ("main_2: .globl main_2"); + ++global_var; + + asm ("main_3: .globl main_3"); + ++global_var; + + asm ("main_4: .globl main_4"); + ++global_var; + + asm ("main_5: .globl main_5"); + ++global_var; + + asm ("main_6: .globl main_6"); + ++global_var; + + asm ("main_7: .globl main_7"); + ++global_var; + + asm ("main_8: .globl main_9"); + ++global_var; + + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.exp b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.exp new file mode 100644 index 00000000000..79fa9acbd4d --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-empty-inline-ranges.exp @@ -0,0 +1,261 @@ +# Copyright 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 . + +# Define an inline function `foo` within the function `main`. The +# function `foo` uses DW_AT_ranges to define its ranges. One of the +# sub-ranges for foo will be empty. +# +# An empty sub-rnage should indicate that there is no code associated +# with `foo` at that address, however, with gcc versions at least +# between 8.x and 14.x (latest at the time of writing this comment), +# it is observed that when these empty sub-ranges are created for an +# inline function, if GDB treats the sub-range as non-empty, and stops +# at that location, then this generally gives a better debug +# experience. It is often still possible to read local variables at +# that address. +# +# This function defines an inline function, places a breakpoint on its +# entry-pc, and then runs and expects GDB to stop, and report the stop +# as being inside the inline function. +# +# We then check that the next outer frame is `main` as expected, and +# that the block for `foo` has the expected sub-ranges. +# +# We compile a variety of different configurations, broadly there are +# two variables, the location of the empty sub-range, and whether the +# entry-pc points at the empty sub-range or not. +# +# The the empty sub-range location, the empty sub-range can be the +# sub-range at the lowest address, highest address, or can be +# somewhere between a blocks low and high addresses. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile .c .S + +# Lines we reference in the generated DWARF. +set main_decl_line [gdb_get_line_number "main decl line"] +set foo_call_line [gdb_get_line_number "foo call line"] + +get_func_info main + +# Compile the source file and load the executable into GDB so we can +# extract some addresses needed for creating the DWARF. +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if {![runto_main]} { + return -1 +} + +# Some addresses that we need when generating the DWARF. +for { set i 0 } { $i < 9 } { incr i } { + set main_$i [get_hexadecimal_valueof "&main_$i" "UNKNOWN" \ + "get address for main_$i"] +} + +# Create the DWARF assembler file into ASM_FILE. Using DWARF_VERSION +# to define which style of ranges to create. FUNC_RANGES is a list of +# 6 entries, each of which is an address, used to create the ranges +# for the inline function DIE. The ENTRY_PC is also an address and is +# used for the DW_AT_entry_pc of the inlined function. +proc write_asm_file { asm_file dwarf_version func_ranges entry_pc } { + Dwarf::assemble $asm_file { + upvar entry_label entry_label + upvar dwarf_version dwarf_version + upvar func_ranges func_ranges + upvar entry_pc entry_pc + + declare_labels lines_table inline_func ranges_label + + cu { version $dwarf_version } { + compile_unit { + {producer "GNU C 14.1.0"} + {language @DW_LANG_C} + {name $::srcfile} + {comp_dir /tmp} + {low_pc 0 addr} + {DW_AT_stmt_list $lines_table DW_FORM_sec_offset} + } { + inline_func: subprogram { + {name foo} + {inline @DW_INL_declared_inlined} + } + subprogram { + {name main} + {decl_file 1 data1} + {decl_line $::main_decl_line data1} + {decl_column 1 data1} + {low_pc $::main_start addr} + {high_pc $::main_len data4} + {external 1 flag} + } { + inlined_subroutine { + {abstract_origin %$inline_func} + {call_file 1 data1} + {call_line $::foo_call_line data1} + {entry_pc $entry_pc addr} + {ranges $ranges_label DW_FORM_sec_offset} + } + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$::srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end [lindex $func_ranges 0] [lindex $func_ranges 1] + start_end [lindex $func_ranges 2] [lindex $func_ranges 3] + start_end [lindex $func_ranges 4] [lindex $func_ranges 5] + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range [lindex $func_ranges 0] [lindex $func_ranges 1] + range [lindex $func_ranges 2] [lindex $func_ranges 3] + range [lindex $func_ranges 4] [lindex $func_ranges 5] + } + } + } + } +} + +# Gobal used to give each generated binary a unique name. +set test_id 0 + +proc run_test { dwarf_version empty_loc entry_pc_type } { + incr ::test_id + + set this_testfile $::testfile-$::test_id + + set asm_file [standard_output_file $this_testfile.S] + + if { $empty_loc eq "start" } { + set ranges { main_1 main_1 \ + main_3 main_4 \ + main_6 main_7 } + set entry_pc_choices { main_1 main_3 } + } elseif { $empty_loc eq "middle" } { + set ranges { main_1 main_2 \ + main_4 main_4 \ + main_6 main_7 } + set entry_pc_choices { main_4 main_1 } + } elseif { $empty_loc eq "end" } { + set ranges { main_1 main_2 \ + main_4 main_5 \ + main_7 main_7 } + set entry_pc_choices { main_7 main_1 } + } else { + error "unknown location for empty range '$empty_loc'" + } + + if { $entry_pc_type eq "empty" } { + set entry_pc [lindex $entry_pc_choices 0] + } elseif { $entry_pc_type eq "non_empty" } { + set entry_pc [lindex $entry_pc_choices 1] + } else { + error "unknown entry-pc type '$entry_pc_type'" + } + + write_asm_file $asm_file $dwarf_version $ranges $entry_pc + + if {[prepare_for_testing "failed to prepare" $this_testfile \ + [list $::srcfile $asm_file] {nodebug}]} { + return + } + + if {![runto_main]} { + return + } + + # Continue until we stop in 'foo'. + gdb_breakpoint foo + gdb_test "continue" \ + "Breakpoint $::decimal, $::hex in foo \\(\\)" \ + "continue to b/p in foo" + + # Check we stopped at the entry-pc. Convert ENTRY_PC, which is + # currently a label to its actual address. + set entry_pc [set "::${entry_pc}"] + set pc [get_hexadecimal_valueof "\$pc" "*UNKNOWN*" \ + "get \$pc at breakpoint"] + gdb_assert { $pc == $entry_pc } "stopped at entry-pc" + + # The block's expected overall low/high addresses. The RANGES list + # contains label names, so we convert these to the corresponding + # addresses. + set block_start [set "::[lindex $ranges 0]"] + set block_end [set "::[lindex $ranges 5]"] + + # Setup variables r{0,1,2}s, r{0,1,2}e, to represent ranges start + # and end addresses. These are extracted from the RANGES + # variable. However, RANGES includes the empty ranges, so spot + # the empty ranges and update the end address as GDB does. + # + # Also, if the empty range is at the end of the block, then the + # block's overall end address also needs adjusting. + for { set i 0 } { $i < 3 } { incr i } { + set start [set "::[lindex $ranges [expr $i * 2]]"] + set end [set "::[lindex $ranges [expr $i * 2 + 1]]"] + if { $start == $end } { + set end [format "0x%x" [expr $end + 1]] + } + if { $block_end == $start } { + set block_end $end + } + set r${i}s $start + set r${i}e $end + } + + # Check the block 'foo' has the expected ranges. + gdb_test "maintenance info blocks" \ + [multi_line \ + "\\\[\\(block \\*\\) $::hex\\\] $block_start\\.\\.$block_end" \ + " entry pc: $entry_pc" \ + " inline function: foo" \ + " symbol count: $::decimal" \ + " address ranges:" \ + " $r0s\\.\\.$r0e" \ + " $r1s\\.\\.$r1e" \ + " $r2s\\.\\.$r2e"] \ + "block for foo has some content" + + # Check the outer frame is 'main' as expected. + gdb_test "frame 1" \ + [multi_line \ + "#1 main \\(\\) at \[^\r\n\]+/$::srcfile:$::foo_call_line" \ + "$::foo_call_line\\s+\[^\r\n\]+/\\* foo call line \\*/"] \ + "frame 1 is for main" +} + +foreach_with_prefix dwarf_version { 4 5 } { + foreach_with_prefix empty_loc { start middle end } { + foreach_with_prefix entry_pc_type { empty non_empty } { + run_test $dwarf_version $empty_loc $entry_pc_type + } + } +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-unexpected-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-unexpected-entry-pc.exp index 69e1ce67cb9..8a77d209f95 100644 --- a/gdb/testsuite/gdb.dwarf2/dw2-unexpected-entry-pc.exp +++ b/gdb/testsuite/gdb.dwarf2/dw2-unexpected-entry-pc.exp @@ -56,6 +56,9 @@ foreach foo {foo_1 foo_2 foo_3 foo_4 foo_5 foo_6} { "get address for $foo label"] } +set foo_3_end [get_hexadecimal_valueof "&foo_3 + 1" "UNKNOWN" \ + "get address for 'foo_3 + 1'"] + # Some line numbers needed in the generated DWARF. set foo_decl_line [gdb_get_line_number "foo decl line"] set bar_call_line [gdb_get_line_number "bar call line"] @@ -85,24 +88,40 @@ if [is_ilp32_target] { # generated which covers some parts of the inlined function. This # makes most sense when being tested with the 'foo_6' label, as that # label is all about handling the end of the inline function case. - -proc run_test { entry_label dwarf_version with_line_table } { - set dw_testname "${::testfile}-${dwarf_version}-${entry_label}" +# +# The PRODUCER is the string used to control the DW_AT_producer string +# in the CU. When PRODUCER is 'gcc' then a string is used that +# represents the gcc compiler. When PRODUCER is 'other' then a string +# that will not be interpreted as gcc is used. The gcc compiler will +# sometimes generate empty ranges for inline functions (from at least +# gcc 8.x through to the currently latest release 14.x), and so GDB +# has code in place to convert empty ranges to non-empty. This fix is +# not applied to other compilers at this time. + +proc run_test { producer entry_label dwarf_version with_line_table } { + set dw_testname "${::testfile}-${producer}-${dwarf_version}-${entry_label}" if { $with_line_table } { set dw_testname ${dw_testname}-lt } + if { $producer eq "other" } { + set producer_str "ACME C 1.0.0" + } else { + set producer_str "GNU C 10.0.0" + } + set asm_file [standard_output_file "${dw_testname}.S"] Dwarf::assemble $asm_file { upvar dwarf_version dwarf_version upvar entry_label entry_label + upvar producer_str producer_str declare_labels lines_table inline_func ranges_label cu { version $dwarf_version } { compile_unit { - {producer "gcc"} + {producer $producer_str} {language @DW_LANG_C} {name $::srcfile} {comp_dir /tmp} @@ -157,6 +176,10 @@ proc run_test { entry_label dwarf_version with_line_table } { line 2 DW_LNS_copy + DW_LNE_set_address foo_3 + line 3 + DW_LNS_copy + DW_LNE_set_address foo_6 line 10 DW_LNS_copy @@ -206,6 +229,16 @@ proc run_test { entry_label dwarf_version with_line_table } { return false } + if { $producer eq "gcc" } { + set entry_pc $::foo_3 + set empty_range_re "\r\n $::foo_3\\.\\.$::foo_3_end" + set line_num 3 + } else { + set entry_pc $::foo_1 + set empty_range_re "" + set line_num 1 + } + # Place a breakpoint on `bar` and run to the breakpoint. Use # gdb_test as we want full pattern matching against the stop # location. @@ -215,8 +248,8 @@ proc run_test { entry_label dwarf_version with_line_table } { if { $with_line_table } { set re \ [multi_line \ - "Breakpoint $::decimal, bar \\(\\) at \[^\r\n\]+/$::srcfile:1" \ - "1\\s+\[^\r\n\]+"] + "Breakpoint $::decimal, bar \\(\\) at \[^\r\n\]+/$::srcfile:$line_num" \ + "$line_num\\s+\[^\r\n\]+"] } else { set re "Breakpoint $::decimal, $::hex in bar \\(\\)" } @@ -230,21 +263,23 @@ proc run_test { entry_label dwarf_version with_line_table } { gdb_test "maint info blocks" \ [multi_line \ "\\\[\\(block \\*\\) $::hex\\\] $::foo_1\\.\\.$::foo_6" \ - " entry pc: $::foo_1" \ + " entry pc: $entry_pc" \ " inline function: bar" \ " symbol count: $::decimal" \ - " address ranges:" \ + " address ranges:$empty_range_re" \ " $::foo_1\\.\\.$::foo_2" \ " $::foo_5\\.\\.$::foo_6"] } -foreach_with_prefix dwarf_version { 4 5 } { - # Test various labels without any line table present. - foreach_with_prefix entry_label { foo_3 foo_4 foo_2 foo_6 } { - run_test $entry_label $dwarf_version false - } +foreach_with_prefix producer { other gcc } { + foreach_with_prefix dwarf_version { 4 5 } { + # Test various labels without any line table present. + foreach_with_prefix entry_label { foo_3 foo_4 foo_2 foo_6 } { + run_test $producer $entry_label $dwarf_version false + } - # Now test what happens if we use the end address of the block, - # but also supply a line table. Does GDB do anything different? - run_test foo_6 $dwarf_version true + # Now test what happens if we use the end address of the block, + # but also supply a line table. Does GDB do anything different? + run_test $producer foo_6 $dwarf_version true + } } diff --git a/gdb/testsuite/gdb.opt/empty-inline-cxx.cc b/gdb/testsuite/gdb.opt/empty-inline-cxx.cc new file mode 100644 index 00000000000..ca36424989d --- /dev/null +++ b/gdb/testsuite/gdb.opt/empty-inline-cxx.cc @@ -0,0 +1,65 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 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 . */ + +#include "attributes.h" + +/* A global to do some work on. This being volatile is important. Without + this the compiler might optimise the whole program away. */ +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +breakpt () +{ + /* Some filler work. */ + global++; +} + +struct MyClass; + +struct ptr +{ + /* The following line is a single line to aid matching in the test + script. Sometimes the DWARF will point GDB at the '{' and sometimes + at the body of the function. We don't really care for this test, so + placing everything on one line removes this variability. */ + MyClass* get_myclass () { return t; } + + MyClass* t; +}; + +struct MyClass +{ + void call(); +}; + +void +MyClass::call () +{ + breakpt (); /* Final breakpoint. */ +} + +static void +intermediate (ptr p) +{ + p.get_myclass ()->call (); +} + +int +main () +{ + intermediate (ptr {new MyClass}); +} diff --git a/gdb/testsuite/gdb.opt/empty-inline-cxx.exp b/gdb/testsuite/gdb.opt/empty-inline-cxx.exp new file mode 100644 index 00000000000..1de41428f38 --- /dev/null +++ b/gdb/testsuite/gdb.opt/empty-inline-cxx.exp @@ -0,0 +1,96 @@ +# Copyright 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 . + +standard_testfile .cc + +require {expr ![test_compiler_info gcc* c++] \ + || [supports_statement_frontiers] } + +set options {c++ debug optimize=-Og} +lappend_include_file options $srcdir/lib/attributes.h +if {[supports_statement_frontiers]} { + lappend options additional_flags=-gstatement-frontiers +} + +# Some line numbers we need for the test. +set get_myclass_line [gdb_get_line_number "MyClass* get_myclass ()"] +set call_get_line [gdb_get_line_number "p.get_myclass ()"] +set final_bp_line [gdb_get_line_number "Final breakpoint"] + +# Build the test executable adding "-OPT_LEVEL" to the compilation +# flags. The break on the small function which is likely to have been +# inlined, check we stop where we expect, and that the backtrace looks +# correct. +# +# Then return from the inline function and call to another function, +# check the backtrace from this second function also looks good, +# specifically, we're checking that the backtrace doesn't incorrectly +# place frame #1 on the line for the inline function. +proc run_test { opt_level } { + + set opts $::options + lappend opts "additional_flags=-${opt_level}" + + if { [prepare_for_testing "failed to prepare" "$::testfile-$opt_level" \ + $::srcfile $opts] } { + return + } + + if { ![runto_main] } { + return + } + + gdb_test "bt" "#0\\s+main \\(\\) \[^\r\n\]+/$::srcfile:$::decimal" \ + "backtrace in main" + + # Break at the empty inline function ptr::get_myclass. + gdb_breakpoint get_myclass + gdb_continue_to_breakpoint "continue to get_myclass" \ + [multi_line \ + ".*/$::srcfile:$::get_myclass_line" \ + "$::get_myclass_line\\s+MyClass\\* get_myclass \\(\\) \[^\r\n\]+"] + + # Backtrace. + gdb_test "bt" \ + [multi_line \ + "#0\\s+ptr::get_myclass\[^\r\n\]+/$::srcfile:$::get_myclass_line" \ + "#1\\s+intermediate\[^\r\n\]+/$::srcfile:$::call_get_line" \ + "#2\\s+\[^\r\n\]+main \\(\\) \[^\r\n\]+/$::srcfile:$::decimal"] \ + "at get_myclass" + + # Print a class member variable, this should be in scope, but is often + # reported as optimised out. + gdb_test "p t" \ + "(?:\\\$1 = \\(MyClass \\*\\) $::hex|value has been optimized out)" \ + "print ptr::t" + + gdb_breakpoint $::srcfile:$::final_bp_line + gdb_continue_to_breakpoint "continue to final breakpoint" + + # Backtrace. Check frame #1 looks right. Bug gdb/25987 would report + # frame #1 as being the correct function, but would report the line for + # ptr::get_myclass(), which is not correct. + setup_xfail *-*-* gdb/25987 + gdb_test "bt" \ + [multi_line \ + "#0\\s+MyClass::call\[^\r\n\]+/$::srcfile:$::final_bp_line" \ + "#1\\s+\[^\r\n\]+ intermediate\[^\r\n\]+/$::srcfile:$::call_get_line" \ + "#2\\s+\[^\r\n\]+ main \\(\\) \[^\r\n\]+/$::srcfile:$::decimal"] \ + "at call" +} + +foreach_with_prefix opt_level { Og O0 O1 O2 } { + run_test ${opt_level} +} diff --git a/gdb/testsuite/gdb.opt/empty-inline.c b/gdb/testsuite/gdb.opt/empty-inline.c new file mode 100644 index 00000000000..595cb9c322f --- /dev/null +++ b/gdb/testsuite/gdb.opt/empty-inline.c @@ -0,0 +1,40 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 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 . */ + +#include "attributes.h" + +static int +test0 (void) +{ + asm (""); /* First line of test0. */ + return 1; /* Second line of test0. */ +} + +int __attribute__((noinline)) ATTRIBUTE_NOCLONE +test1 (int x) +{ + asm (""); + return x + 1; /* Second line of test1. */ +} + +int +main (void) +{ + test1 (test0 ()); /* First line of main. */ + test1 (test0 ()); /* Second line of main. */ + return 0; /* Third line of main. */ +} diff --git a/gdb/testsuite/gdb.opt/empty-inline.exp b/gdb/testsuite/gdb.opt/empty-inline.exp new file mode 100644 index 00000000000..4b3758a05ad --- /dev/null +++ b/gdb/testsuite/gdb.opt/empty-inline.exp @@ -0,0 +1,116 @@ +# Copyright 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 . + +standard_testfile + +require {expr ![test_compiler_info gcc* c++] \ + || [supports_statement_frontiers] } + +set options {debug nowarnings optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h +if {[supports_statement_frontiers]} { + lappend options additional_flags=-gstatement-frontiers +} + +# Some line numbers we need. +set lineno_main_1 [gdb_get_line_number "First line of main"] +set lineno_main_2 [gdb_get_line_number "Second line of main"] +set lineno_main_3 [gdb_get_line_number "Third line of main"] +set lineno_test0_1 [gdb_get_line_number "First line of test0"] +set lineno_test0_2 [gdb_get_line_number "Second line of test0"] +set lineno_test1_2 [gdb_get_line_number "Second line of test1"] + +# Compile and run the test. OPT_LEVEL is a string, e.g. "O2", which +# is the optimisation level to apply. +# +# The test runs to main, and then steps through an inline function, +# and then into a non-inline function. At various stages the +# backtrace is checked. +proc run_test { opt_level } { + + set opts $::options + lappend opts "additional_flags=-${opt_level}" + + if { [prepare_for_testing "failed to prepare" "$::testfile-$opt_level" \ + $::srcfile $opts] } { + return + } + + if { ![runto_main] } { + return + } + + gdb_test "frame 0" \ + [multi_line \ + "#0\\s+main \[^\r\n\]+/$::srcfile:$::lineno_main_1" \ + "$::lineno_main_1\\s+\[^\r\n\]+"] \ + "frame 0 while in main" + + gdb_test_multiple "step" "step into test0" { + -re -wrap ".*test0.*$::srcfile:$::lineno_test0_1.*" { + gdb_test "step" "$::lineno_test0_2\\s+.*" $gdb_test_name + } + -re -wrap ".*test0.*$::srcfile:$::lineno_test0_2.*" { + pass $gdb_test_name + } + } + + gdb_test "frame 1" \ + [multi_line \ + "#1\\s+main \[^\r\n\]+/$::srcfile:$::lineno_main_1" \ + "$::lineno_main_1\\s+\[^\r\n\]+"] \ + "inspect frame 1, main" + + # Step into test1() function. + gdb_test "step" \ + [multi_line \ + "test1 \\(\[^)\]+\\) at \[^\r\n\]+/$::srcfile:$::lineno_test1_2" \ + "$::lineno_test1_2\\s+\[^\r\n\]+"] \ + "step into test1" + + # Check frame #1 looks right. Bug gdb/25987 would report frame #1 as + # being the correct function, but would report the line for a nearby + # inlined function. + setup_xfail *-*-* gdb/25987 + gdb_test "frame 1" \ + [multi_line \ + "#1\\s+\[^\r\n\]*main \\(\\) \[^\r\n\]+/$::srcfile:$::lineno_main_1" \ + "$::lineno_main_1\\s+\[^\r\n\]+"] \ + "inspect frame 1 again, still main" + + # Step from the last line of test1 back into main. + gdb_test "step" \ + [multi_line \ + "main \\(\\) at \[^\r\n\]+/$::srcfile:$::lineno_main_2" \ + "$::lineno_main_2\\s+\[^\r\n\]+"] \ + "step back to main" + + # Use next to step to the last line of main. This skips over the inline + # call to test0, and the non-inline call to test1. + gdb_test "next" \ + "$::lineno_main_3\\s+return 0;\\s+\[^\r\n\]+" \ + "step over test0+1" + + # Sanity check that we are in main like we expect. + gdb_test "frame 0" \ + [multi_line \ + "#0\\s+main \[^\r\n\]+/$::srcfile:$::lineno_main_3" \ + "$::lineno_main_3\\s+return 0;\\s+\[^\r\n\]+"] \ + "confirm expected frame in main" +} + +foreach_with_prefix opt_level { Og O0 O1 O2 } { + run_test ${opt_level} +} From patchwork Tue Jan 14 14:31:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 104752 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 C38FA3856950 for ; Tue, 14 Jan 2025 14:35:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C38FA3856950 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=hCmzUOih X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id CC428385694A for ; Tue, 14 Jan 2025 14:32:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CC428385694A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CC428385694A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736865122; cv=none; b=HGW/nOtSzfrm6P6PjsT0JU6DwjUkod7atwA6dRzecGwXQdYmTf2WZAqcBbYCfcAa3TUHL/jAhzEUykLSSmISJGo0nKK7Y8c5fbYDuXP3IknjRkDGI7hM4VOLJ32XdKJk9gKknZ+B8U9bk6X5b19ugEccGXhkI6gnHEdkzckDUpQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1736865122; c=relaxed/simple; bh=FBxB3evV+OEDGYo83qliSsoBPzqskO+hyPqu5VR0XIM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qfnPIrhg7Y5uJBYh+9j/bSEip6p7dtLE5HEn6KOSmOaBq3YUZ/Jp0nBKPsdoeqM5cnqyDPd33oc3fMSwKhdHjlnHLNztZXlMOQu6/CmxCgG8rhg+S0pgemw4ohsxIJa3UTBSAC4KDfqepUt/a/lPnARr+k0OpIK/eG6G2Vd8Rn4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC428385694A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1736865122; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZKXCodnwYpBw3fLRUMvECcF9WX7cvFZbLGt33qi7pnA=; b=hCmzUOihVTb0pAO0jaQ6ToAHNL54CV1yVZ3m7P0bJuDS2wVjUtKMa8UuLbfkJfp6RzDEXx wJNGG4WJL/uY56sopPnCK6myPaRQt/ow+w18cLMNbdOMQi0nd0m9jfNGvUsXss5aV0us72 GCB6WKBgedsiihyA8pMoD20Kl0q0woI= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-549-ncw5RGWANTeF-4MYBMBqUQ-1; Tue, 14 Jan 2025 09:32:00 -0500 X-MC-Unique: ncw5RGWANTeF-4MYBMBqUQ-1 X-Mimecast-MFC-AGG-ID: ncw5RGWANTeF-4MYBMBqUQ Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-436fbe0d0b3so9252395e9.0 for ; Tue, 14 Jan 2025 06:32:00 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736865117; x=1737469917; 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=ZKXCodnwYpBw3fLRUMvECcF9WX7cvFZbLGt33qi7pnA=; b=QNIGlqVLqjNqJAvYfuP4qH9PzHTK+/BNjcrG/C3R+vL5GGalVzVLkmpz3VmnFMEmU/ kEAbmZpWBd6CtF5kIrCwg2yIlm6W+IMVld7D4GrBvNSU9/de8NSSr1Vhgv27AIYQSkjA lBdcqcAr9bwC61IocTSjrSF7lj2zqQBQEE9sjzmDbZVN1LodmiSMi8ZCnanFkml/UnjM MArw9hnS09b9cr+Wg44ULTDCtyE2pLJlXqpoLM+gA/cSaXV5uVdqG2/YvYl8givYAe6Q heIlqDJLMSUXm+JRHp3qlfYRZa+kQ4frOs1zxlYAm5JCEfoXEqRsjl3PXlQYgTmyuRMZ 8rhg== X-Gm-Message-State: AOJu0YyHsQk3+g7VK9lQTfnIPpeoTq6/1dZoA7KfPRZQuH78SgRRtHo3 p+lwQvC7AD+OIuejX50ONbn4YWi2Ve5Nu57j9GW9DIyg34bYD8DKuRCjNhBtkRc9KTeJuuD4PMA MO6JhkSLGEc+yrwBdZqVAUN48GQaoWEXC8f9L8GlW9jG/MUwcUnvEbEBaPil0UzpkHVKWOvjR6j 5hJNiGFWxdOvj+cuYREABBOGwRIoCEv8RYmTwRjieLBts= X-Gm-Gg: ASbGnctPMJr4KrPDUZpZZlWTUI1el6Vh6xeFsiKyFiwR6wpQhWCQRnqgIyVjGrPWxVs ug5br12JJDyBwmhGZutEdBdeuzxyv/s8kILbs0myr5dvKw5PNFqufAbCpy+lRN74Gg/KB8C+z52 p/OmoGnlrY2Qd0xolLCLPIztkNv7g6FCHbPA4m2bFGdnWBhvAWdm8l2evW6ksR6pGcEFp3mTSs0 NULPjSDOZ+od/LEf7eM5UlHxdF4erh2zYo0g7pcCELTYx9OGzwK8+oTsLw++KmaF66XvDOPliHR R0pqbw== X-Received: by 2002:a05:600c:492f:b0:434:ea1a:e30c with SMTP id 5b1f17b1804b1-436e8849debmr177344695e9.13.1736865116056; Tue, 14 Jan 2025 06:31:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IHg6Sl4h3IcTRjt8UOpHgiNMYyNc+krdPdoFDlmLz8aTCj4w0NhjmQV374oG/UkG5dJoEtHLw== X-Received: by 2002:a05:600c:492f:b0:434:ea1a:e30c with SMTP id 5b1f17b1804b1-436e8849debmr177343815e9.13.1736865115090; Tue, 14 Jan 2025 06:31:55 -0800 (PST) Received: from localhost (44.226.159.143.dyn.plus.net. [143.159.226.44]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-436e9dc860bsm178036705e9.9.2025.01.14.06.31.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jan 2025 06:31:54 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Bernd Edlinger , Andrew Burgess Subject: [PATCHv3 2/2] gdb: improve line number lookup around inline functions Date: Tue, 14 Jan 2025 14:31:49 +0000 Message-ID: X-Mailer: git-send-email 2.47.1 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Jdam9TaWwAmGsta4YDfOByl1BTml8Pf_PDSKYVkC0TQ_1736865119 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP 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 This commit aims to fix an issue where GDB would report the wrong line for frames other than #0 if a previous frame had just left an inline function. Consider this example which is compiled at -Og: volatile int global = 0; static inline int bar (void) { asm (""); return 1; } static void foo (int count) { global += count; } int main (void) { foo (bar ()); return 0; } Used in this GDB session: (gdb) break foo Breakpoint 1 at 0x401106: file test.c, line 6. (gdb) run Starting program: /tmp/inline-bt/test.x Breakpoint 1, foo (count=count@entry=1) at test.c:6 6 { global += count; } (gdb) frame 1 #1 0x0000000000401121 in main () at test.c:3 3 static inline int bar (void) { asm (""); return 1; } Notice that GDB incorrectly reports frame #1 as being at line 3 when it should really be reporting this line: foo (bar ()); The cause of this problem is in find_pc_sect_line (symtab.c). This function is passed a PC for which GDB must find the symtab_and_line information. The function can be called in two modes based on the NOTCURRENT argument. When NOTCURRENT is false then we are looking for information about the current PC, i.e. the PC at which the inferior is currently stopped at. When NOTCURRENT is true we are looking for information about a PC that it not the current PC, but is instead the PC for a previous frame. The interesting thing in this case is that the PC passed in will be the address after the address we actually want to lookup information for, this is because as we unwind the program counter from frame #0 what we get is the return address in frame #1. The return address is often (or sometimes) on the line after the calling line, and so in find_pc_sect_line, when NOTCURRENT is true, we subtract 1 from PC and then proceed as normal looking for information about this new PC value. Now lets look at the x86-64 disassembly for 'main' from the above example. The location marker (=>) represents the return address in 'main' after calling 'foo': (gdb) run Starting program: /tmp/inline-bt/test.x Breakpoint 1, foo (count=count@entry=1) at test.c:6 6 { global += count; } #0 foo (count=count@entry=1) at test.c:6 #1 0x000000000040111f in main () at test.c:3 (gdb) up #1 0x000000000040111f in main () at test.c:3 3 static inline int bar (void) { asm (""); return 1; } (gdb) disassemble Dump of assembler code for function main: 0x0000000000401115 <+0>: mov $0x1,%edi 0x000000000040111a <+5>: call 0x401106 => 0x000000000040111f <+10>: mov $0x0,%eax 0x0000000000401124 <+15>: ret End of assembler dump. And the corresponding line table: (gdb) maintenance info line-table objfile: /tmp/inline-bt/test.x ((struct objfile *) 0x59405a0) compunit_symtab: test.c ((struct compunit_symtab *) 0x53ad320) symtab: /tmp/inline-bt/test.c ((struct symtab *) 0x53ad3a0) linetable: ((struct linetable *) 0x53adc90): INDEX LINE REL-ADDRESS UNREL-ADDRESS IS-STMT PROLOGUE-END EPILOGUE-BEGIN 0 6 0x0000000000401106 0x0000000000401106 Y 1 6 0x0000000000401106 0x0000000000401106 Y 2 6 0x0000000000401106 0x0000000000401106 3 6 0x0000000000401114 0x0000000000401114 4 9 0x0000000000401115 0x0000000000401115 Y 5 10 0x0000000000401115 0x0000000000401115 Y 6 3 0x0000000000401115 0x0000000000401115 Y 7 3 0x0000000000401115 0x0000000000401115 Y 8 3 0x0000000000401115 0x0000000000401115 Y 9 10 0x0000000000401115 0x0000000000401115 10 11 0x000000000040111f 0x000000000040111f Y 11 12 0x000000000040111f 0x000000000040111f 12 END 0x0000000000401125 0x0000000000401125 Y When looking for the line information of frame #1 we start with the return address 0x40111f, however, as this is not the current program counter value we subtract one and look for line information for 0x40111e. We will find the entry at index 9, this is the last entry with an address less than the address we're looking for, the next entry has an address greater than the one we're looking for. The entry at index 9 is for line 10 which is the correct line, but GDB reports line 3, so what's going on? Having found a matching entry GDB checks to see if the entry is marked as is-stmt (is statement). In our case index 9 (line 10) is not a statement, and so GDB looks backwards for entries at the same address, if any of these are marked is-stmt then GDB will use the last of these instead. In our case the previous entry at index 8 is marked is-stmt, and so GDB uses that. The entry at index 8 is for line 3, and that is why GDB reports the wrong line. So why perform the backward is-stmt check? When NOTCURRENT is false (not our case) the backward scan makes sense. If the inferior has just stopped at some new location, and we want to report that location to the user, then it is better (I think) to select an is-stmt entry. In this way we will report a line number for a line which the inferior is just about to start executing, and non of the side effects of that line have yet taken place. The line GDB prints will correspond with the reported line, and if the user queries the inferior state, the inferior should (assuming the compiler emitted correct is-stmt markers) correspond to the line in question having not yet been started. However, in our case NOTCURRENT is true. We're looking back to previous frames that are currently in-progress. If upon return to the previous frame we are about to execute the next line then (is seems to me) that this indicates we must be performing the very last action from the previous line. As such, looking back through the line table in order to report a line that has not yet started is the wrong thing to do. We really want to report the very last line table entry for the previous address as this is (I think) most likely to represent the previous line that is just about to complete. Further, in the NOTCURRENT case, we should care less about reporting an is-stmt line. When a user looks back to a previous frame I don't think they expect the line being reported to have not yet started. In fact I think the expectation is the reverse ... after all, the previous line must have executed enough to call the current frame. So my proposal is that the backward scan of the line table looking for an is-stmt entry should not be performed when NOTCURRENT is true. In the case above this means we will report the entry at index 9, which is for line 10, which is correct. For testing this commit I have: 1. Extended the existing gdb.opt/inline-bt.exp test. I've extended the source code to include a test similar to the example above. I have also extended the script so that the test is compiled at a variety of optimisation levels (O0, Og, O1, O2). 2. Added a new DWARF assembler test which hard codes a line table similar to the example given above. My hope is that even if test case (1) changes (due to compiler changes) this test will continue to test the specific case I'm interested in. I have tested the gdb.opt/inline-bt.exp test with gcc versions 8.4.0, 9.3.1, 10.5.0, 11.5.0, 12.2.0, and 14.2.0, in each case the test will fail (with the expected error) without this patch applied, and will pass with this patch applied. I was inspired to write this patch while reviewing these patches: https://inbox.sourceware.org/gdb-patches/AS8P193MB1285C58F6F09502252CEC16FE4DF2@AS8P193MB1285.EURP193.PROD.OUTLOOK.COM https://inbox.sourceware.org/gdb-patches/AS8P193MB12855708DFF59A5309F5B19EE4DF2@AS8P193MB1285.EURP193.PROD.OUTLOOK.COM though this patch only covers one of the issues addressed by these patches, and the approach taken is quite different. Still, those patches are worth reading for the history of this fix. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=25987 --- gdb/symtab.c | 25 ++- gdb/testsuite/gdb.dwarf2/dw2-inline-bt.c | 79 +++++++ gdb/testsuite/gdb.dwarf2/dw2-inline-bt.exp | 227 +++++++++++++++++++++ gdb/testsuite/gdb.opt/empty-inline-cxx.exp | 1 - gdb/testsuite/gdb.opt/empty-inline.exp | 1 - gdb/testsuite/gdb.opt/inline-bt.c | 28 +++ gdb/testsuite/gdb.opt/inline-bt.exp | 127 ++++++++---- 7 files changed, 439 insertions(+), 49 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-inline-bt.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-inline-bt.exp diff --git a/gdb/symtab.c b/gdb/symtab.c index ba421267b9a..20f4aeb7a58 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -3271,14 +3271,23 @@ find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent) best = prev; best_symtab = iter_s; - /* If during the binary search we land on a non-statement entry, - scan backward through entries at the same address to see if - there is an entry marked as is-statement. In theory this - duplication should have been removed from the line table - during construction, this is just a double check. If the line - table has had the duplication removed then this should be - pretty cheap. */ - if (!best->is_stmt) + /* If NOTCURRENT is false then the address we are looking for is + the address the inferior is currently stopped at. In this + case our preference is to report a stop at a line marked as + is_stmt. If BEST is not marked as a statement then scan + backwards through entries at this address looking for one that + is marked as a statement; if one is found then use that. + + If NOTCURRENT is true then the address we're looking for is + not the inferior's current address, but is an address from a + previous stack frame (i.e. frames 1, 2, 3, ... etc). In this + case scanning backwards for an is_stmt line table entry is not + the desired behaviour. If an inline function terminated at + this address then the last is_stmt line will be within the + inline function, while the following non-statement line will + be for the outer function. When looking up the stack we + expect to see the outer function. */ + if (!best->is_stmt && !notcurrent) { const linetable_entry *tmp = best; while (tmp > first diff --git a/gdb/testsuite/gdb.dwarf2/dw2-inline-bt.c b/gdb/testsuite/gdb.dwarf2/dw2-inline-bt.c new file mode 100644 index 00000000000..1321726ff9a --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-inline-bt.c @@ -0,0 +1,79 @@ +/* Copyright 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 . */ + +/* Used to insert labels with which we can build a fake line table. */ +#define LL(N) asm ("line_label_" #N ": .globl line_label_" #N) + +/* The following non-compiled code exists for the generated line table to + point at. */ + +#if 0 + +volatile int global = 0; + +__attribute__((noinline, noclone)) void +foo (int arg) +{ /* foo prologue */ + asm (""); + global += arg; +} + +inline __attribute__((always_inline)) int +bar (void) +{ + return 1; /* bar body */ +} + +int +main (void) +{ /* main prologue */ + foo (bar ()); /* call line */ + return 0; +} + +#endif /* 0 */ + +volatile int var; + +/* Generate some code to take up some space. */ +#define FILLER do { \ + var = 99; \ +} while (0) + +void +func (void) +{ + asm ("func_label: .globl func_label"); + FILLER; + LL (1); + FILLER; + LL (2); + return; +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); + FILLER; + LL (4); + FILLER; + LL (5); + func (); + FILLER; + LL (6); + FILLER; + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-inline-bt.exp b/gdb/testsuite/gdb.dwarf2/dw2-inline-bt.exp new file mode 100644 index 00000000000..3e237fc4cf2 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-inline-bt.exp @@ -0,0 +1,227 @@ +# Copyright 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 . + +# Setup a line table where: +# +# | | | | Func | Func | Func | +# | Addr | Line | Stmt | main | foo | bar | +# |------|------|------|------|------|------| +# | 1 | 28 | Y | | X | | +# | 2 | 30 | Y | | X | | +# | 3 | 31 | N | | X | | +# | 4 | 41 | Y | X | | | +# | 5 | 42 | Y | X | | | +# | 5 | 36 | Y | X | | X | +# | 5 | 42 | N | X | | | +# | 6 | 43 | Y | X | | | +# | 7 | END | Y | X | | | +# |------|------|------|------|------|------| +# +# +# The function 'bar' is inline within 'main' while 'foo' is not +# inline. Function 'foo' is called from 'main' immediately after the +# inlined call to bar. The C code can be found within a '#if 0' block +# inside the test's .c file. The line table is similar to that +# generated by compiling the source code at optimisation level -Og. +# +# Place a breakpoint in 'foo', run to the breakpoint, and then examine +# frame #1, that is, the frame for 'main'. At one point, bugs in GDB +# meant that the user would be shown the inline line from 'bar' rather +# than the line from 'main'. In the example above the user expects to +# see line 42 from 'main', but instead would be shown line '36'. +# +# The cause of the bug is this: to find the line for frame #1 GDB +# first finds an address in frame #1 by unwinding frame #0. This +# provides the return address in frame #1. GDB subtracts 1 from this +# address and looks for a line matching this address. In this case +# that would be line 42. +# +# However, buggy GDB would then scan backward through the line table +# looking for a line table entry that is marked as is-stmt. In this +# case, the first matching entry is that for line 36, and so that is +# what is reported. This backward scan makes sense for frame #0, but +# not for outer frames. +# +# This has now been fixed to prevent the backward scan for frames +# other than frame #0. + +load_lib dwarf.exp + +# This test can only be run on targets which support DWARF-2 and use +# gas. +require dwarf2_support + +standard_testfile .c .S + +# Lines in the source code that we need to reference. +set call_line [gdb_get_line_number "call line" $srcfile] +set foo_prologue [gdb_get_line_number "foo prologue" $srcfile] +set main_prologue [gdb_get_line_number "main prologue" $srcfile] +set bar_body [gdb_get_line_number "bar body" $srcfile] + +# We need the return address in 'main' after the call to 'func' so +# that we can build the line table. Compile the .c file with debug, +# and figure out the address. This works so long as the only +# difference in build flags between this compile and the later compile +# is that this is debug on, and the later compile is debug off. +if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } { + return +} + +if {![runto func]} { + return +} + +set func_call_line [gdb_get_line_number "func ();"] +gdb_test "up" \ + [multi_line \ + "#1\\s*$hex in main \\(\\) at \[^\r\n\]+" \ + "$func_call_line\\s+ func \\(\\);"] \ + "move up from func to main" + +set return_addr_in_main [get_hexadecimal_valueof "\$pc" "*UNKNOWN*" \ + "get pc after return from func"] + +# Prepare and run the test. Placed into a proc in case we ever want +# to parameterise this test in the future. + +proc do_test { } { + set build_options {nodebug} + + set asm_file [standard_output_file $::srcfile2] + Dwarf::assemble $asm_file { + upvar build_options build_options + + declare_labels lines_label foo_label bar_label + + get_func_info main $build_options + get_func_info func $build_options + + cu {} { + compile_unit { + {producer "gcc" } + {language @DW_LANG_C} + {name $::srcfile} + {low_pc 0 addr} + {stmt_list ${lines_label} DW_FORM_sec_offset} + } { + foo_label: subprogram { + {external 1 flag} + {name foo} + {low_pc $func_start addr} + {high_pc "$func_start + $func_len" addr} + } + bar_label: subprogram { + {external 1 flag} + {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 %$bar_label} + {low_pc line_label_4 addr} + {high_pc line_label_5 addr} + {call_file 1 data1} + {call_line $::call_line data1} + } + } + } + } + + lines {version 2 default_is_stmt 1} lines_label { + include_dir "${::srcdir}/${::subdir}" + file_name "$::srcfile" 1 + + program { + DW_LNE_set_address func + line $::foo_prologue + DW_LNS_copy + + DW_LNE_set_address line_label_1 + DW_LNS_advance_line 2 + DW_LNS_copy + + DW_LNE_set_address line_label_2 + DW_LNS_advance_line 1 + DW_LNS_negate_stmt + DW_LNS_copy + + DW_LNE_set_address main + DW_LNS_advance_line [expr $::main_prologue - $::foo_prologue - 3] + DW_LNS_negate_stmt + DW_LNS_copy + + DW_LNE_set_address line_label_4 + DW_LNS_advance_line 1 + DW_LNS_copy + + DW_LNE_set_address line_label_4 + line $::bar_body + DW_LNS_copy + + DW_LNE_set_address line_label_4 + line $::call_line + DW_LNS_negate_stmt + DW_LNS_copy + + # Skip line_label_5, this is used as the end of `bar` + # the inline function. + + DW_LNE_set_address $::return_addr_in_main + DW_LNS_advance_line 1 + DW_LNS_negate_stmt + DW_LNS_copy + + DW_LNE_set_address "$main_start + $main_len" + DW_LNE_end_sequence + } + } + } + + if { [prepare_for_testing "failed to prepare" $::testfile \ + [list $::srcfile $asm_file] $build_options] } { + return + } + + if ![runto foo] { + return + } + + # For this backtrace we don't really care which line number in foo + # is reported. We might get different line numbers depending on + # how the architectures skip prologue function works. This test + # is all about how frame #1 is reported. + set foo_body_1 [expr $::foo_prologue + 1] + set foo_body_2 [expr $::foo_prologue + 2] + gdb_test "bt" \ + [multi_line \ + "^#0\\s+foo \\(\\) at \[^\r\n\]+$::srcfile:(?:$::foo_prologue|$foo_body_1|$foo_body_2)" \ + "#1\\s+$::hex in main \\(\\) at \[^\r\n\]+$::srcfile:$::call_line"] \ + "backtrace show correct line number in main" + + gdb_test "frame 1" \ + [multi_line \ + "^#1\\s+$::hex in main \\(\\) at \[^\r\n\]+$::srcfile:$::call_line" \ + "$::call_line\\s+foo \\(bar \\(\\)\\);\[^\r\n\]+"] \ + "correct lines are shown for frame 1" +} + +# Run the test. +do_test diff --git a/gdb/testsuite/gdb.opt/empty-inline-cxx.exp b/gdb/testsuite/gdb.opt/empty-inline-cxx.exp index 1de41428f38..fcff66659b1 100644 --- a/gdb/testsuite/gdb.opt/empty-inline-cxx.exp +++ b/gdb/testsuite/gdb.opt/empty-inline-cxx.exp @@ -82,7 +82,6 @@ proc run_test { opt_level } { # Backtrace. Check frame #1 looks right. Bug gdb/25987 would report # frame #1 as being the correct function, but would report the line for # ptr::get_myclass(), which is not correct. - setup_xfail *-*-* gdb/25987 gdb_test "bt" \ [multi_line \ "#0\\s+MyClass::call\[^\r\n\]+/$::srcfile:$::final_bp_line" \ diff --git a/gdb/testsuite/gdb.opt/empty-inline.exp b/gdb/testsuite/gdb.opt/empty-inline.exp index 4b3758a05ad..bcd5956f355 100644 --- a/gdb/testsuite/gdb.opt/empty-inline.exp +++ b/gdb/testsuite/gdb.opt/empty-inline.exp @@ -83,7 +83,6 @@ proc run_test { opt_level } { # Check frame #1 looks right. Bug gdb/25987 would report frame #1 as # being the correct function, but would report the line for a nearby # inlined function. - setup_xfail *-*-* gdb/25987 gdb_test "frame 1" \ [multi_line \ "#1\\s+\[^\r\n\]*main \\(\\) \[^\r\n\]+/$::srcfile:$::lineno_main_1" \ diff --git a/gdb/testsuite/gdb.opt/inline-bt.c b/gdb/testsuite/gdb.opt/inline-bt.c index 3999104dfeb..d4192b3130b 100644 --- a/gdb/testsuite/gdb.opt/inline-bt.c +++ b/gdb/testsuite/gdb.opt/inline-bt.c @@ -13,6 +13,8 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +#include "attributes.h" + /* This is only ever run if it is compiled with a new-enough GCC, but we don't want the compilation to fail if compiled by some other compiler. */ @@ -39,6 +41,30 @@ inline ATTR int func2(void) return x * func1 (1); } +inline ATTR int +return_one (void) +{ + /* The following empty asm() statement prevents older (< 11.x) versions + of gcc from completely optimising away this function. And for newer + versions of gcc (>= 11.x) this ensures that we have two line table + entries in main for the inline call to this function, with the second + of these lines being a non-statement, which is critical for this + test. These two behaviours have been checked for versions of gcc + between 8.4.0 and 14.2.0. */ + asm (""); + return 1; +} + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +not_inline_func (int count) +{ + global += count; + global += count; /* b/p in not_inline_func */ + global += count; +} + int main (void) { int val; @@ -53,5 +79,7 @@ int main (void) val = func2 (); result = val; + not_inline_func (return_one ()); /* bt line in main */ + return 0; } diff --git a/gdb/testsuite/gdb.opt/inline-bt.exp b/gdb/testsuite/gdb.opt/inline-bt.exp index 9e1fb195f9b..9228edefc3f 100644 --- a/gdb/testsuite/gdb.opt/inline-bt.exp +++ b/gdb/testsuite/gdb.opt/inline-bt.exp @@ -15,9 +15,11 @@ standard_testfile .c inline-markers.c +set opts {debug additional_flags=-Winline} +lappend_include_file opts $srcdir/lib/attributes.h + if {[prepare_for_testing "failed to prepare" $testfile \ - [list $srcfile $srcfile2] \ - {debug additional_flags=-Winline}]} { + [list $srcfile $srcfile2] $opts]} { return -1 } @@ -29,40 +31,87 @@ if { [skip_inline_frame_tests] } { return } -set line1 [gdb_get_line_number "set breakpoint 1 here" ${srcfile2}] -gdb_breakpoint $srcfile2:$line1 - -gdb_test "continue" ".*set breakpoint 1 here.*" "continue to bar, 1" -gdb_test "backtrace" "#0 bar.*#1 .*main.*" "backtrace from bar, 1" -gdb_test "info frame" ".*called by frame.*" "bar not inlined" - -gdb_test "continue" ".*set breakpoint 1 here.*" "continue to bar, 2" -gdb_test "backtrace" "#0 bar.*#1 .*func1.*#2 .*main.*" \ - "backtrace from bar, 2" -gdb_test "up" "#1 .*func1.*" "up from bar, 2" -gdb_test "info frame" ".*inlined into frame.*" "func1 inlined, 2" - -gdb_test "continue" ".*set breakpoint 1 here.*" "continue to bar, 3" -gdb_test "backtrace" "#0 bar.*#1 .*func1.*#2 .*func2.*#3 .*main.*" \ - "backtrace from bar, 3" -gdb_test "up" "#1 .*func1.*" "up from bar, 3" -gdb_test "info frame" ".*inlined into frame.*" "func1 inlined, 3" -gdb_test "up" "#2 .*func2.*" "up from func1, 3" -gdb_test "info frame" ".*inlined into frame.*" "func2 inlined, 3" - -# A regression test for having a backtrace limit that forces unwinding -# to stop after an inline frame. GDB needs to compute the frame_id of -# the inline frame, which requires unwinding past all the inline -# frames to the real stack frame, even if that means bypassing the -# user visible backtrace limit. See PR backtrace/15558. -# -# Set a backtrace limit that forces an unwind stop after an inline -# function. -gdb_test_no_output "set backtrace limit 2" -# Force flushing the frame cache. -gdb_test "maint flush register-cache" "Register cache flushed." -gdb_test "up" "#1 .*func1.*" "up from bar, 4" -gdb_test "info frame" ".*in func1.*" "info frame still works" -# Verify the user visible limit works as expected. -gdb_test "up" "Initial frame selected; you cannot go up." "up hits limit" -gdb_test "backtrace" "#0 bar.*#1 .*func1.*" "backtrace hits limit" +# Run inline function backtrace tests, compile with binary with OPT_LEVEL +# optimisation level. OPT_LEVEL should be a string like 'O0', 'O1', etc. +# No leading '-' is needed on OPT_LEVEL, that is added in this proc. +proc run_test { opt_level } { + + set local_opts $::opts + lappend local_opts "additional_flags=-$opt_level" + + if {[prepare_for_testing "failed to prepare" ${::testfile}-${opt_level} \ + [list $::srcfile $::srcfile2] $local_opts]} { + return + } + + runto_main + + set line1 [gdb_get_line_number "set breakpoint 1 here" ${::srcfile2}] + gdb_breakpoint $::srcfile2:$line1 + + with_test_prefix "first stop at bar" { + gdb_continue_to_breakpoint "continue to bar" \ + ".*set breakpoint 1 here.*" + gdb_test "backtrace" "#0 bar.*#1 .*main.*" "backtrace from bar" + gdb_test "info frame" ".*called by frame.*" "bar not inlined" + } + + with_test_prefix "second stop at bar" { + gdb_continue_to_breakpoint "continue to bar" \ + ".*set breakpoint 1 here.*" + gdb_test "backtrace" "#0 bar.*#1 .*func1.*#2 .*main.*" \ + "backtrace from bar" + gdb_test "up" "#1 .*func1.*" "up from bar" + gdb_test "info frame" ".*inlined into frame.*" "func1 inlined" + } + + with_test_prefix "third stop at bar" { + gdb_continue_to_breakpoint "continue to bar" \ + ".*set breakpoint 1 here.*" + gdb_test "backtrace" "#0 bar.*#1 .*func1.*#2 .*func2.*#3 .*main.*" \ + "backtrace from bar" + gdb_test "up" "#1 .*func1.*" "up from bar" + gdb_test "info frame" ".*inlined into frame.*" "func1 inlined" + gdb_test "up" "#2 .*func2.*" "up from func1" + gdb_test "info frame" ".*inlined into frame.*" "func2 inlined" + } + + # A regression test for having a backtrace limit that forces unwinding + # to stop after an inline frame. GDB needs to compute the frame_id of + # the inline frame, which requires unwinding past all the inline + # frames to the real stack frame, even if that means bypassing the + # user visible backtrace limit. See PR backtrace/15558. + # + # Set a backtrace limit that forces an unwind stop after an inline + # function. + gdb_test_no_output "set backtrace limit 2" + # Force flushing the frame cache. + gdb_test "maint flush register-cache" "Register cache flushed." + gdb_test "up" "#1 .*func1.*" "up from bar" + gdb_test "info frame" ".*in func1.*" "info frame still works" + # Verify the user visible limit works as expected. + gdb_test "up" "Initial frame selected; you cannot go up." "up hits limit" + gdb_test "backtrace" "#0 bar.*#1 .*func1.*" "backtrace hits limit" + + set line2 [gdb_get_line_number "b/p in not_inline_func" $::srcfile] + set line3 [gdb_get_line_number "bt line in main" $::srcfile] + + gdb_breakpoint $::srcfile:$line2 + + gdb_continue_to_breakpoint "stop in not_inline_func" \ + ".*b/p in not_inline_func.*" + gdb_test "bt" \ + [multi_line \ + "^#0\\s+not_inline_func \\(\[^)\]+\\) at \[^\r\n\]+$::srcfile:$line2" \ + "#1\\s+$::hex in main \\(\\) at \[^\r\n\]+$::srcfile:$line3"] \ + "bt from not_inline_func to main" + gdb_test "frame 1" \ + [multi_line \ + "^#1\\s+$::hex in main \\(\\) at \[^\r\n\]+$::srcfile:$line3" \ + "$line3\\s+not_inline_func \\(return_one \\(\\)\\);\[^\r\n\]+"] \ + "select frame for main from not_inline_func" +} + +foreach_with_prefix opt_level { O0 Og O1 O2 } { + run_test $opt_level +}