From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.freebsd.org (mx2.freebsd.org [96.47.72.81]) by sourceware.org (Postfix) with ESMTPS id 89005385840C for ; Tue, 9 Apr 2024 18:06:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 89005385840C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=FreeBSD.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=FreeBSD.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 89005385840C Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=96.47.72.81 ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712685969; cv=pass; b=e3XbNe+wfmlN7lY4ih/n/I/Khve6SCQYmRydqLgA4Hi6bgH/PPAKa7EK6ORua2LFzKHYIlkaubabQpo+OF4rIW84Juyf+8wT5TGYo6C55nPzb+Q1ouMhBCdMi3iNITH3Ry/tzwXgn826kQLK+bR9pFQUQ/7ZCOIePVNhPd9eCqw= ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1712685969; c=relaxed/simple; bh=wdG2y81/LSHRV+bdc3Lapp91DZfV/3en/KJ2QYPNjW4=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=JeJhaE4NOWEGksQJyGa/kEURNSLRys8MN0YOiVdezYd4yDtKRN/BJatEYhR5YmT7WlnKhPgqADUeO1W8ItbNKR1bZB9tFDgJCqXGUY5oh2F2Pnw2O1kQ65JzXGCJepl/RVF+3nh3lxS8KyRtY+hjySllsuZIHsccztSUiP+MN5w= ARC-Authentication-Results: i=2; server2.sourceware.org Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits)) (Client CN "mx1.freebsd.org", Issuer "R3" (verified OK)) by mx2.freebsd.org (Postfix) with ESMTPS id 4VDYmb2hwtz4V6h; Tue, 9 Apr 2024 18:06:07 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [96.47.72.83]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VDYmb1tlGz4mHC; Tue, 9 Apr 2024 18:06:07 +0000 (UTC) (envelope-from jhb@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712685967; 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=kXnhRv6VCfq2dzw+a8WoQGg3xak38uyXchBVptZwu1Q=; b=wHVORVszTyD2FqOzAe3aQHeBJalvFLlBvePeP31ABFOXlr6++zohCslgGOuLQW1EnYTRN5 YVJvCwFM6x5Vgn2+h0DqR0X9WqBTnqhXd+TfFncH14MoVxA7akFB90kLTqqyfmqx061wEc 3uLrqMyLcNgFn+Lg3nSmvTajGuu4SSQDGAyF5ES1VZ/KlUKFH8R2AYjPpH2zeTV5qzSfbN xsNISjxUrJFxyeRYLWZTxX6ZqvBASlrlaZ75Ty7Lx76koPd8DVo4W6tF8kZy/cQtv46TRJ onRPnA17HJoLSbubzVVJoghE9Tlfza1NNTyWPzPg2IbMYikyuWSBCWfOBB/ekg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1712685967; a=rsa-sha256; cv=none; b=wH2DThIoJ4qeLLoGt7GO4pThfHe+oTEsbMSmvGksFJk+VyDUFag68HkBP9rzPRX+WlasFi mB3gdjTJwDXfeacLpN6WJVTkYWDaF32LZj9ONKt5Xm4VE1zKlGfwCkzUpECclMQVijaFYx jHl2NPJJBWa6L6ESvHBmGCgVV7MF+yvBtHLRJYWj/GmqilTcOVSLwj3lRFyFD/EiaDpDQV joW3oriLyOctrCBI2s9jEdR/h3+ykV5+KxMq4uKf2RFb3XBw9OSpSICT1wa6ZeguJyGZCC aRR8lRB8mGulZnk1gLHtUiuC6hUyuky4Wba9lzpvNvA6Adw4eWQlKhm2i3GXzQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1712685967; 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=kXnhRv6VCfq2dzw+a8WoQGg3xak38uyXchBVptZwu1Q=; b=hvepk3xW6saNRP86gAX1i/uL+7b/YIE6+SPQK8ujxF49zpg5pcLLULKurNzV+kS/WN3Q00 NDcM/+VDF5wuYPFmB1PoGc2uPa0p55Agf6ByDcKC17YsWSiZ1Us33rNOBtoqjBVW57PPKV qTMG6te82SrQLHjB/Jc2GG4OGd3mrbyYdOpjZRXUd0P/4m56CrreKcLFfrT3jv3lacGfL4 B5ghH6GbRMVp9FEaBV7R0S7gs4pS7IAVnyZSvawrqR2ONff1Ii+3KR+NIdDa9s8R7dS954 Ol/EYRtnIGPsKgmHDtNTJtvfJS+wYAyxN3qjInPT2Jne8xw7z9b2/X+7w8Hd2g== Received: from [IPV6:2601:644:937f:4c50:edc2:c5b9:7ac9:bfc7] (unknown [IPv6:2601:644:937f:4c50:edc2:c5b9:7ac9:bfc7]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: jhb) by smtp.freebsd.org (Postfix) with ESMTPSA id 4VDYmZ4tfnz1FCT; Tue, 9 Apr 2024 18:06:06 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Message-ID: <57194ec5-998f-4fff-994b-3b3711afc5e4@FreeBSD.org> Date: Tue, 9 Apr 2024 11:06:05 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] gdb: xtensa: fix truncated backtrace for nested noreturn functions Content-Language: en-US To: Alexey Lapshin , "gdb-patches@sourceware.org" Cc: Alexey Gerenkov , Ivan Grokhotkov , "jcmvbkbc@gmail.com" References: <74194ea6e213b8e757f303e04ba36a8e36053222.camel@espressif.com> From: John Baldwin In-Reply-To: <74194ea6e213b8e757f303e04ba36a8e36053222.camel@espressif.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.8 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_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS,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 List-Id: On 4/3/24 11:04 AM, Alexey Lapshin wrote: > The problem appears when break in nested noreturn calls. > panic_abort() and esp_system_abort() are noreturn functions: > > #0 0x4008779f in panic_abort () > #1 0x40087a78 in esp_system_abort () > Backtrace stopped: previous frame identical to this frame (corrupt stack?) > > Assembly listing: > > 40081ad4 : > 40081ad4: 004136 entry a1, 32 > 40081aeb: ffff06 j 40081aeb > ... > > 40085614 : > 40085614: 004136 entry a1, 32 > 40085619: fc4ba5 call8 40081ad4 > > 4008561c <__ubsan_include>: > 4008561c: 004136 entry a1, 32 > > PC register for frame esp_system_abort points to the next instruction after > instruction with address 40085619. > It is ENTRY instruction for __ubsan_include. This caused wrong unwinding > because we are not in __ubsan_include at this frame. In general for > noreturn functions there should be RET instruction. This is why it works > in all other cases. > > PC register can point to entry instruction only for the innermost frame. > It is not possible otherwise. > > The fix is making it not possible to go with not innermost frame into > the code block which collects frame cache for frames when PC is on entry > instruction. > --- > gdb/testsuite/gdb.base/backtrace-noreturn.c | 42 ++++++++++++++++ > gdb/testsuite/gdb.base/backtrace-noreturn.exp | 48 +++++++++++++++++++ > gdb/xtensa-tdep.c | 3 +- > 3 files changed, 92 insertions(+), 1 deletion(-) > create mode 100644 gdb/testsuite/gdb.base/backtrace-noreturn.c > create mode 100644 gdb/testsuite/gdb.base/backtrace-noreturn.exp > > diff --git a/gdb/testsuite/gdb.base/backtrace-noreturn.c b/gdb/testsuite/gdb.base/backtrace-noreturn.c > new file mode 100644 > index 00000000000..bd492013ee8 > --- /dev/null > +++ b/gdb/testsuite/gdb.base/backtrace-noreturn.c > @@ -0,0 +1,42 @@ > +/* This testcase is part of GDB, the GNU debugger. > + > + Copyright 2019-2022 Free Software Foundation, Inc. This should probably be 2024? > + > + 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 "../lib/attributes.h" > + > +void __attribute__((noreturn)) ATTRIBUTE_NOCLONE > +baz () > +{ > + while(1); /* Break here. */ > +} > + > +void __attribute__((noreturn)) ATTRIBUTE_NOCLONE > +bar () > +{ > + baz (); > +} > + > +void __attribute__((noinline)) ATTRIBUTE_NOCLONE > +foo () > +{ > + bar (); > +} > + > +int > +main () > +{ > + foo (); > +} > diff --git a/gdb/testsuite/gdb.base/backtrace-noreturn.exp b/gdb/testsuite/gdb.base/backtrace-noreturn.exp > new file mode 100644 > index 00000000000..e89efc0241b > --- /dev/null > +++ b/gdb/testsuite/gdb.base/backtrace-noreturn.exp > @@ -0,0 +1,48 @@ > +# Copyright 2019-2022 Free Software Foundation, Inc. 2024 here as well? > +# 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 . > + > +# A place for miscellaneous tests related to backtrace. > + > +standard_testfile > + > +if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } { > + return -1 > +} > + > +if ![runto_main] then { > + fail "can't run to main" > + return 0 > +} > + > +# Run to the breakpoint at return. > +gdb_breakpoint [gdb_get_line_number "Break here."] > +gdb_continue_to_breakpoint "Break here." > + > +# Backtrace with the default options. > +gdb_test "bt" \ > + [multi_line \ > + "#0\[ \t\]*baz \\(\\) at \[^\r\n\]+" \ > + "#1\[ \t\]*$hex in bar \\(\\) at \[^\r\n\]+" \ > + "#2\[ \t\]*$hex in foo \\(\\) at \[^\r\n\]+" \ > + "#3\[ \t\]*$hex in main \\(\\) at \[^\r\n\]+" ] > + > +# Backtrace with 'set disassemble-next-line on'. This shouldn't make > +# any difference to the backtrace. > +gdb_test "with disassemble-next-line on -- bt" \ > + [multi_line \ > + "#0\[ \t\]*baz \\(\\) at \[^\r\n\]+" \ > + "#1\[ \t\]*$hex in bar \\(\\) at \[^\r\n\]+" \ > + "#2\[ \t\]*$hex in foo \\(\\) at \[^\r\n\]+" \ > + "#3\[ \t\]*$hex in main \\(\\) at \[^\r\n\]+" ] > diff --git a/gdb/xtensa-tdep.c b/gdb/xtensa-tdep.c > index 5444ebb7f6a..e8a143fadab 100644 > --- a/gdb/xtensa-tdep.c > +++ b/gdb/xtensa-tdep.c > @@ -1262,7 +1262,8 @@ xtensa_frame_cache (frame_info_ptr this_frame, void **this_cache) > ws = get_frame_register_unsigned (this_frame, > tdep->ws_regnum); > > - if (safe_read_memory_integer (pc, 1, byte_order, &op1) > + if (frame_relative_level (this_frame) == 0 > + && safe_read_memory_integer (pc, 1, byte_order, &op1) > && XTENSA_IS_ENTRY (gdbarch, op1)) > { > int callinc = CALLINC (ps); I think this is ok. In other places I believe the strategy to handle tail calling frames where the return address is beyond the end of the function is to use 'PC - 1' to lookup debug info instead, but you would not want to do that for the first frame either in case you are stopped at a function entry at the time of a stop. -- John Baldwin