From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 991923858402 for ; Tue, 14 Sep 2021 13:12:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 991923858402 Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.0.43) with SMTP id 18ED50Vo003223; Tue, 14 Sep 2021 09:12:03 -0400 Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3b2n922kka-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 14 Sep 2021 09:12:02 -0400 Received: from m0098409.ppops.net (m0098409.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 18ECH37I005746; Tue, 14 Sep 2021 09:12:02 -0400 Received: from ppma04dal.us.ibm.com (7a.29.35a9.ip4.static.sl-reverse.com [169.53.41.122]) by mx0a-001b2d01.pphosted.com with ESMTP id 3b2n922kjv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 14 Sep 2021 09:12:02 -0400 Received: from pps.filterd (ppma04dal.us.ibm.com [127.0.0.1]) by ppma04dal.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 18ED84Ls013088; Tue, 14 Sep 2021 13:12:01 GMT Received: from b03cxnp08025.gho.boulder.ibm.com (b03cxnp08025.gho.boulder.ibm.com [9.17.130.17]) by ppma04dal.us.ibm.com with ESMTP id 3b0m3anr2v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 14 Sep 2021 13:12:01 +0000 Received: from b03ledav004.gho.boulder.ibm.com (b03ledav004.gho.boulder.ibm.com [9.17.130.235]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 18EDC0on43450776 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 14 Sep 2021 13:12:00 GMT Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id E3F087806B; Tue, 14 Sep 2021 13:11:59 +0000 (GMT) Received: from b03ledav004.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2483478063; Tue, 14 Sep 2021 13:11:58 +0000 (GMT) Received: from [9.160.116.65] (unknown [9.160.116.65]) by b03ledav004.gho.boulder.ibm.com (Postfix) with ESMTP; Tue, 14 Sep 2021 13:11:57 +0000 (GMT) Subject: Re: [PATCH v2] libgcc: Add a backchain fallback to _Unwind_Backtrace() on PowerPC To: gcc-patches@gcc.gnu.org Cc: segher@kernel.crashing.org, dje.gcc@gmail.com, amodra@gmail.com, wschmidt@linux.ibm.com, tuliom@linux.ibm.com References: <20210826145324.19505-1-rzinsly@linux.ibm.com> From: Raphael M Zinsly Message-ID: <2248593e-6e3d-3a87-9636-a7e5b1053ce7@linux.ibm.com> Date: Tue, 14 Sep 2021 10:11:56 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210826145324.19505-1-rzinsly@linux.ibm.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: oOZyPHBVh5hh3Mb6d5LFyWfRnk_p1u4F X-Proofpoint-GUID: 7oY1_iw0NDZQDIlnS0QUx4Jx_NDXC1MW X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.182.1,Aquarius:18.0.687,Hydra:6.0.235,FMLib:17.0.607.475 definitions=2020-10-13_15,2020-10-13_02,2020-04-07_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1011 spamscore=0 adultscore=0 mlxscore=0 malwarescore=0 impostorscore=0 bulkscore=0 phishscore=0 priorityscore=1501 suspectscore=0 mlxlogscore=999 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109030001 definitions=main-2109140027 X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 14 Sep 2021 13:12:10 -0000 Ping On 26/08/2021 11:53, Raphael Moreira Zinsly wrote: > Without dwarf2 unwind tables available _Unwind_Backtrace() is not > able to return the full backtrace. > This patch adds a fallback function on powerpc to get the backtrace > by doing a backchain, this code was originally at glibc. > > libgcc/ChangeLog: > > * config/rs6000/linux-unwind.h (struct rt_sigframe): Move it to > outside of get_regs() in order to use it in another function, > this is done twice: for __powerpc64__ and for !__powerpc64__. > (struct trace_arg): New struct. > (struct layout): New struct. > (ppc_backchain_fallback): New function. > * unwind.inc (_Unwind_Backtrace): Look for _URC_NORMAL_STOP > code state and call MD_BACKCHAIN_FALLBACK. > > gcc/testsuite/ChangeLog: > > * gcc.target/powerpc/unwind-backchain.c: New test. > --- > .../gcc.target/powerpc/unwind-backchain.c | 22 ++++ > libgcc/config/rs6000/linux-unwind.h | 102 +++++++++++++++--- > libgcc/unwind.inc | 14 ++- > 3 files changed, 122 insertions(+), 16 deletions(-) > create mode 100644 gcc/testsuite/gcc.target/powerpc/unwind-backchain.c > > diff --git a/gcc/testsuite/gcc.target/powerpc/unwind-backchain.c b/gcc/testsuite/gcc.target/powerpc/unwind-backchain.c > new file mode 100644 > index 00000000000..fdce78a1f63 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/powerpc/unwind-backchain.c > @@ -0,0 +1,22 @@ > +/* { dg-do run { target { powerpc*-*-linux* } } } */ > +/* { dg-options "-fno-asynchronous-unwind-tables" } */ > + > +#include > + > +void > +test_backtrace() > +{ > + int addresses; > + void *buffer[10]; > + > + addresses = backtrace(buffer, 10); > + if(addresses != 4) > + __builtin_abort(); > +} > + > +int > +main() > +{ > + test_backtrace(); > + return 0; > +} > diff --git a/libgcc/config/rs6000/linux-unwind.h b/libgcc/config/rs6000/linux-unwind.h > index acdc948f85d..8deccc1d650 100644 > --- a/libgcc/config/rs6000/linux-unwind.h > +++ b/libgcc/config/rs6000/linux-unwind.h > @@ -94,6 +94,15 @@ struct gcc_ucontext > > enum { SIGNAL_FRAMESIZE = 128 }; > > +struct rt_sigframe { > + char gap[SIGNAL_FRAMESIZE]; > + struct gcc_ucontext uc; > + unsigned long pad[2]; > + int tramp[6]; > + void *pinfo; > + struct gcc_ucontext *puc; > +}; > + > /* If PC is at a sigreturn trampoline, return a pointer to the > regs. Otherwise return NULL. */ > > @@ -136,14 +145,7 @@ get_regs (struct _Unwind_Context *context) > #endif > { > /* This works for 2.4.21 and later kernels. */ > - struct rt_sigframe { > - char gap[SIGNAL_FRAMESIZE]; > - struct gcc_ucontext uc; > - unsigned long pad[2]; > - int tramp[6]; > - void *pinfo; > - struct gcc_ucontext *puc; > - } *frame = (struct rt_sigframe *) context->cfa; > + struct rt_sigframe *frame = (struct rt_sigframe *) context->cfa; > return frame->uc.regs; > } > } > @@ -154,6 +156,12 @@ get_regs (struct _Unwind_Context *context) > > enum { SIGNAL_FRAMESIZE = 64 }; > > +struct rt_sigframe { > + char gap[SIGNAL_FRAMESIZE + 16]; > + char siginfo[128]; > + struct gcc_ucontext uc; > +}; > + > static struct gcc_regs * > get_regs (struct _Unwind_Context *context) > { > @@ -176,11 +184,7 @@ get_regs (struct _Unwind_Context *context) > } > else if (pc[0] == 0x38006666 || pc[0] == 0x380000AC) > { > - struct rt_sigframe { > - char gap[SIGNAL_FRAMESIZE + 16]; > - char siginfo[128]; > - struct gcc_ucontext uc; > - } *frame = (struct rt_sigframe *) context->cfa; > + struct rt_sigframe *frame = (struct rt_sigframe *) context->cfa; > return frame->uc.regs; > } > return NULL; > @@ -203,7 +207,7 @@ ppc_fallback_frame_state (struct _Unwind_Context *context, > int i; > > if (regs == NULL) > - return _URC_END_OF_STACK; > + return _URC_NORMAL_STOP; > > new_cfa = regs->gpr[__LIBGCC_STACK_POINTER_REGNUM__]; > fs->regs.cfa_how = CFA_REG_OFFSET; > @@ -352,3 +356,73 @@ frob_update_context (struct _Unwind_Context *context, _Unwind_FrameState *fs ATT > } > #endif > } > + > +#define MD_BACKCHAIN_FALLBACK ppc_backchain_fallback > + > +struct trace_arg > +{ > + /* Stores the list of addresses. */ > + void **array; > + struct unwind_link *unwind_link; > + _Unwind_Word cfa; > + /* Number of addresses currently stored. */ > + int count; > + /* Maximum number of addresses. */ > + int size; > +}; > + > +/* This is the stack layout we see with every stack frame. > + Note that every routine is required by the ABI to lay out the stack > + like this. > + > + +----------------+ +-----------------+ > + %r1 -> | previous frame--------> | previous frame--->... --> NULL > + | | | | > + | cr save | | cr save | > + | | | | > + | (unused) | | lr save | > + +----------------+ +-----------------+ > + > + The CR save is only present on 64-bit ABIs. > +*/ > +struct frame_layout > +{ > + struct frame_layout *backchain; > +#ifdef __powerpc64__ > + long int cr_save; > +#endif > + void *lr_save; > +}; > + > + > +void ppc_backchain_fallback (struct _Unwind_Context *context, void *a) > +{ > + struct frame_layout *current; > + struct trace_arg *arg = a; > + int count; > + > + /* Get the last address computed and start with the next. */ > + current = context->cfa; > + current = current->backchain; > + > + for (count = arg->count; current != NULL; current = current->backchain) > + { > + arg->array[count] = current->lr_save; > + > + /* Check if the symbol is the signal trampoline and get the interrupted > + symbol address from the trampoline saved area. */ > + context->ra = current->lr_save; > + if (current->lr_save && get_regs (context)) > + { > + struct rt_sigframe *sigframe = (struct rt_sigframe *) current; > + if (count + 1 == arg->size) > + break; > + arg->array[++count] = (void *) sigframe->uc.rsave.nip; > + current = (void *) sigframe->uc.rsave.gpr[1]; > + } > + if (count++ >= arg->size) > + break; > + } > + > + arg->count = count-1; > +} > diff --git a/libgcc/unwind.inc b/libgcc/unwind.inc > index aa48d104fd0..456a5ee682f 100644 > --- a/libgcc/unwind.inc > +++ b/libgcc/unwind.inc > @@ -300,14 +300,24 @@ _Unwind_Backtrace(_Unwind_Trace_Fn trace, void * trace_argument) > > /* Set up fs to describe the FDE for the caller of context. */ > code = uw_frame_state_for (&context, &fs); > - if (code != _URC_NO_REASON && code != _URC_END_OF_STACK) > + if (code != _URC_NO_REASON && code != _URC_END_OF_STACK > + && code != _URC_NORMAL_STOP) > return _URC_FATAL_PHASE1_ERROR; > > /* Call trace function. */ > if ((*trace) (&context, trace_argument) != _URC_NO_REASON) > return _URC_FATAL_PHASE1_ERROR; > > - /* We're done at end of stack. */ > +#ifdef MD_BACKCHAIN_FALLBACK > + /* Do a backchain if there is no DWARF data. */ > + if (code == _URC_NORMAL_STOP) > + { > + MD_BACKCHAIN_FALLBACK(&context, trace_argument); > + break; > + } > +#endif > + > + /* We're done at end of stack. */ > if (code == _URC_END_OF_STACK) > break; > > -- Raphael Moreira Zinsly