From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by sourceware.org (Postfix) with ESMTPS id DDA713854838 for ; Fri, 30 Sep 2022 00:10:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DDA713854838 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 28TMitY5026770; Fri, 30 Sep 2022 00:10:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2022-7-12; bh=4m0ROxRIULf+bdRpa1kKpxkqf+d/3EAwaD+6vMVOqmY=; b=BDCg4njMQAO/ilqM0WfPa6WRaqiu9mhxICkC0l/o1ArUXeL/LFRWw+MY1ltvn/dBtsws aJQIcRyZK343sgtA/xjBvPCk6jM4+oGWnZaU8FOngwHRM2Zu/zV5QY9rsyMsB8dhcHl9 RC8Zel5EWBBVMJo75RsvFFQZedQivMp5zainl+Ocso8IokGiTIpk8KO7dwIRrhH6/LNG apnRizP6OuuA4mHs1FkCSDn4aE8Du8l1Utkivdfu0Oyw0qcKdr5DjbldQ1Gx2q/KnAfe dUs5Va4WD6a4DRlmRjvceFkFQykJbZleKsgW4Ss/Q/3CTbx1zU4Kn5yNmDsuXVnUp+A6 VA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3jst0kx3wy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 30 Sep 2022 00:10:28 +0000 Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.5/8.17.1.5) with ESMTP id 28U00k6b037126; Fri, 30 Sep 2022 00:10:27 GMT Received: from nam10-mw2-obe.outbound.protection.outlook.com (mail-mw2nam10lp2102.outbound.protection.outlook.com [104.47.55.102]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3jtpvh84es-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 30 Sep 2022 00:10:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DkkFU+PSXEZucZgcFYHg9vQG9d4TtsLv78m/aXjTePTZxyQmIOsZY5PksBXlGuQ8oZMZQGf+tBVHHRFq6+JXe+7kqflopznQCjVdnWdsYClW02KYSuEnx8tY6ZYdvRKaGYl4PAfuzsgg6l+fqjaxswv9LaoYh7cCnuXe0+w++aMm8uZHTVxXFb8nW1gghEwflovWilgNv+h0O7X+jazzspj84QcfB4plb2MG0oqJC2BigqxFBprnFKQG5uP7wI1vre/NX75G8W6WKtaASzP1aPoZEko4PT8wrjQRYy2T3KYvFJr8dinwomj6AY2fuWXsQrCp3cIESB/D6KuDrfM84A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=4m0ROxRIULf+bdRpa1kKpxkqf+d/3EAwaD+6vMVOqmY=; b=MzRcOI9LAe+MnYt4iKehNIqXS7qYhqeyJTFrnxHk6NUfKXfYcCTx8riOLY0m0GSVdPtsUlo/cGbngg2eq9FT5Vkq1e5io4kMr54MuDMdLZxAbw7EQvVeFnnq3ofHGwA3abfhSP8M9MaOX4/aFDrMAA30i2ZaU5JKTLOXAjN4+oKdg4mJWbeeDN3Sj83bUWkkfVZAd7Z4YFaNcz0hWt6Q8C/US/uf001zLC5l3Rpgm51rWKAZur1ZFIYQHuinVgbuEsnRgYqhX9hYvyBJcWuS5hkVJjsJ++s3SZpFF8cfItUxbfowEEaTPtRPkwlG60UBXdZP2gQCsPUuIEZarAsrpQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=4m0ROxRIULf+bdRpa1kKpxkqf+d/3EAwaD+6vMVOqmY=; b=nwQj21Ij0Yd8JheG7vDcg9+tog2ANCvysDUcpJux+ieiEgN4P6P3fX/r5H+OVLuwz4mTwj04RZKN27fqg03cMC3CLDV9gEl95j0AlkM/zUD3D5IXv5/mv5J91e95w+iGrWf5F8Gl98vebwympcYy3jh6Zt9AUssDotzHz1qUHJk= Received: from MWHPR1001MB2158.namprd10.prod.outlook.com (2603:10b6:301:2d::17) by BLAPR10MB5251.namprd10.prod.outlook.com (2603:10b6:208:332::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.19; Fri, 30 Sep 2022 00:10:23 +0000 Received: from MWHPR1001MB2158.namprd10.prod.outlook.com ([fe80::80a7:f7f3:4303:54e4]) by MWHPR1001MB2158.namprd10.prod.outlook.com ([fe80::80a7:f7f3:4303:54e4%5]) with mapi id 15.20.5676.017; Fri, 30 Sep 2022 00:10:23 +0000 From: Indu Bhagat To: binutils@sourceware.org Cc: nickc@redhat.com, weimin.pan@oracle.com Subject: [PATCH,V1 08/14] unwinder: generate backtrace using SFrame format Date: Thu, 29 Sep 2022 17:04:34 -0700 Message-Id: <20220930000440.1672106-9-indu.bhagat@oracle.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220930000440.1672106-1-indu.bhagat@oracle.com> References: <20220930000440.1672106-1-indu.bhagat@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: MW3PR05CA0026.namprd05.prod.outlook.com (2603:10b6:303:2b::31) To MWHPR1001MB2158.namprd10.prod.outlook.com (2603:10b6:301:2d::17) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWHPR1001MB2158:EE_|BLAPR10MB5251:EE_ X-MS-Office365-Filtering-Correlation-Id: a58aa26a-e835-4703-7b40-08daa2782b68 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VN91N9B/ghB2AjXxzLLeB5R3Ouc9IaC2o/d9CZu65uUxuDbLXqOoSDqMfZl9yDv+g+cfZBDDUBPnQ1q+7dzJdycdPliv/wjzjbPG5hpHAOOUx+kTE3Rm44XpkYEILhUkNlrtu5Wt9nJ6AsO7wfbBopFanv53wSipGGPC2pYXHeBDJZtrH4bnBNBpdwx5qIHuEZndzl6g8YG8zS3rvI1L4o7LNuHmc5IobNXYTDksjJIMKzTuOzOWSpwgdAnamHB1hIXhTCMR1cgNaJn1Pw+x5PWoaOGLnWuyjdaiDJDwzfSQM6eIqP+VqdHXH4rNCduBeSTTlVbI9I0Y5yCGx+VsiKv54V+EOlVWZtnZNA5IgY8LRb/wClVO9HAdIwoix5AvRCWMmQvywemIjdXZl3sPd12EFZitYrovCjMbyL0l5o3ezEWwGEmC4070l8zggeXr6//oAx+udfWD5nA8PWmNQt3/Y2yqxe2a9QUmp8PQahsJQcqmsRWOQ/IRWddR+vUfpl+gKwETAyUWF3gtNPPZGwujlgHVT4YJW4w1bcjmXgEPuVOy9RXFFc+PGK3wdYX0/WtclFqlmVuVfyw0KPdZK86yXci/dMahFoluaasrSsvNLKkhrMX4WhXzjCMS/Yas+T37UivC0FvaZVV39Jite8zwlgBJZv8qNvkeO0WHhqFxVfJgMdzNwNQMsftrI7Xzl0ooGjdi6RgKaccvyzSWvwz8dcCgWImZuthLHGaOocCJSnOoiaMjlFWExNC1/kt/qgDKyjU2j2VPH4zkfJ08yHN8OQCyvN3/IyUdmbUUoka728q/LeSZMfpmYceriDou X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MWHPR1001MB2158.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(346002)(39860400002)(376002)(366004)(396003)(136003)(451199015)(6512007)(26005)(6486002)(38100700002)(478600001)(107886003)(86362001)(41300700001)(8936002)(316002)(6666004)(6916009)(2906002)(6506007)(8676002)(83380400001)(5660300002)(44832011)(30864003)(1076003)(2616005)(186003)(4326008)(36756003)(66476007)(66556008)(66946007)(2004002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?Sfv37GJ29huumXsGqmcpbpQdBhOoGXLI3jb+TmvdZ8Ba+vT+ibOWIHj9kAU7?= =?us-ascii?Q?sNrIob9ORGXxpbNaaNg9f7YPS7ELgJJjpFOliBreAorzSUBmA7Ho3AXK7gUk?= =?us-ascii?Q?uZyB6RVa8QcznJsOzKXFHuevxvTJbWRunMHOj7kdeeBzMR+h92bGoOTAzyf6?= =?us-ascii?Q?koLVqrH8FNMtEXRBpVmkz8AyE+hSZ0oshgGzw+QrGNp5MUzs/WG7IFZKoBdz?= =?us-ascii?Q?/IF7e1jwmDbHZZSDhEdQ6DTEIhZC4HWHRHNfPAwf3aCaJFbQ2ugZZ7V6tePv?= =?us-ascii?Q?PBajhFAyIU2RTNUK1uL6OF11smSF3N/RPTFvuGvKwMXnrlUX1Y/bo9qPDeZl?= =?us-ascii?Q?+cyjyfTZiv2uXK1VH7Dbjst1j4JeKBbwE1+gyVJ/qXlUp/etZuyERP/veGPN?= =?us-ascii?Q?aq6gXHIYdJFv09fs8csYIJBBvZBabeeS7z0f9JFVUkIqbqvBRq7wthOSdW1a?= =?us-ascii?Q?uDD001ozV+5jgfI6/+Fyo5vsHOff+bLx4E5QWERectO7tQxx6uwx6DqD/e1a?= =?us-ascii?Q?Ky0B6tXJ7kbSVy06Wa9LtWS36jGbl4n9sn++HJf3ZzytFbvwQxtiOhJDumef?= =?us-ascii?Q?9fmGoEuhFFsnQykARuCmLZVTL2mgaREdZ9CTW8YSQgQmhGdzVJfnFR0iT3ns?= =?us-ascii?Q?XMIVeuWwDLQMq9eoUdJj1F9GFhf60MA4J5KVal/N3Ec7l71BfUjcnQsI8irH?= =?us-ascii?Q?r8IamO970tQ846q/H6LDIpEPl1jj5Z/T0hw0RBmY8qRzlBToE4HBomtGh2Wm?= =?us-ascii?Q?vho8DX6LPcXIUmCAeSWnNh2QBXFEj6H8uIT3mBjUt0oTGBUodo72svQ2sL4h?= =?us-ascii?Q?N+FGi4mus7aHUoPGKWg6Y2bvhOzaFscFP1DgMYTC2DCA2cPLzIST6sOJusbw?= =?us-ascii?Q?7fX+WMIZrB4KRkhA+oPX9sc3p9liQXuqmFxhrD5eqpfvlT2VM4CUkhRfGDAg?= =?us-ascii?Q?2e8KnVjanHVo+QmbJQc7ydnDJC7kjO9vATVEwkP/GLCZx5n8bL1YZVzsiAoM?= =?us-ascii?Q?Z6jyrCdKRA8fCfnGhJdMvRDelssnJOawXfAKI+slARmu5f5oPdNyyv8AxKfE?= =?us-ascii?Q?j6mA9YwM+YN9gzqKIqku0GfUnf/3eh4T5ukkRk9XYOhZqaldGV3VB60JrCJU?= =?us-ascii?Q?bk28qsDJEstNE9O8ARMG03aMGF5R8dxLH1b7CxDkLOvjB2z4vmUQAQSHmF55?= =?us-ascii?Q?q29ClS4/nwo2q+Vlw9IOp+/aG6FwTIZcAH3VADTZX7a3wsMMkye/Hh5kGYs+?= =?us-ascii?Q?JRJrhMffFdPxMMewRvFMtZzOsQJJiq0n+5TV+nO/J21Acd9dSQ/YwZPWGJKc?= =?us-ascii?Q?svB1dPDfJTBxpftIXBVfRRrIfm01BlnwOZ/OjVL0In+ltN//ZxVEx3SnR1jr?= =?us-ascii?Q?K3BHHbemg485tvVdIPhiqjO74aZqrLQjJnNQpQZb9PG7LI68HW6nuCB9aQ88?= =?us-ascii?Q?yTDiODVj402tsYityMJMQL0F4VqbQfASVCBQN6Uh8ARIQKeFVdP6QlQhriso?= =?us-ascii?Q?5McL5cmzZnKPq0EH6OexicfXdTkdmZuSRQ4F5BICemN8hPvXwgxjJ934FyeA?= =?us-ascii?Q?sz+FYDw/RtWBdHqKSE+FlZDvK/hnNjVpuF6Gwo5+?= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: a58aa26a-e835-4703-7b40-08daa2782b68 X-MS-Exchange-CrossTenant-AuthSource: MWHPR1001MB2158.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Sep 2022 00:10:23.6420 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: mFl1yswAEq+FteQiKe5BwtTQxB4nkS8muCEg8inRP1S9OVluCTvjTrnaV7HbkuW4WQGCcukJ+nt+SD0MlQB08A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BLAPR10MB5251 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.528,FMLib:17.11.122.1 definitions=2022-09-29_13,2022-09-29_03,2022-06-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 malwarescore=0 suspectscore=0 adultscore=0 mlxscore=0 spamscore=0 bulkscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2209290150 X-Proofpoint-GUID: rW7h8Om2zPB1pAhy7XYRlcS1ZMGgHa2E X-Proofpoint-ORIG-GUID: rW7h8Om2zPB1pAhy7XYRlcS1ZMGgHa2E X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_LOW,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 List-Id: From: Weimin Pan A simple unwinder based on SFrame format. The unwinder is made available via libsframebt library. Buildsystem changes have been made to build libsframebt only when --gsframe support is available in the assembler. These buildsystem changes are necessary because the SFrame based unwinder the SFrame unwind info for itself to work. PS: libsframe/configure has NOT been included in the patch. Please regenerate. config/ChangeLog: * sframe.m4: New file. include/ChangeLog: * sframe-backtrace-api.h: New file. ChangeLog: * libsframe/Makefile.am: Build backtrace functionality in its own library. Install libsframebt conditionally. * libsframe/Makefile.in: Regenerate. * libsframe/aclocal.m4: Regenerate. * libsframe/configure: Regenerate. <-- [REMOVED FROM THE PATCH. PLEASE REGENERATE.] * libsframe/configure.ac: Check if gas supports --gsframe command line option. * libsframe/sframe-backtrace-err.c: New file. * libsframe/sframe-backtrace.c: New file. --- config/sframe.m4 | 16 + include/sframe-backtrace-api.h | 57 ++ libsframe/Makefile.am | 12 + libsframe/Makefile.in | 58 +- libsframe/aclocal.m4 | 1 + libsframe/configure.ac | 7 + libsframe/sframe-backtrace-err.c | 46 ++ libsframe/sframe-backtrace.c | 619 ++++++++++++++++++ libsframe/testsuite/Makefile.in | 1 + .../testsuite/libsframe.decode/Makefile.in | 1 + .../testsuite/libsframe.encode/Makefile.in | 1 + 11 files changed, 812 insertions(+), 7 deletions(-) create mode 100644 config/sframe.m4 create mode 100644 include/sframe-backtrace-api.h create mode 100644 libsframe/sframe-backtrace-err.c create mode 100644 libsframe/sframe-backtrace.c diff --git a/config/sframe.m4 b/config/sframe.m4 new file mode 100644 index 00000000000..9f149fda9ef --- /dev/null +++ b/config/sframe.m4 @@ -0,0 +1,16 @@ +# SFRAME_CHECK_AS_SFRAME +# ---------------------- +# Check whether the assembler supports generation of SFrame +# unwind information. +# +# Defines: +# ac_cv_have_sframe + +AC_DEFUN([SFRAME_CHECK_AS_SFRAME],[ + ac_save_CFLAGS="$CFLAGS" + CFLAGS="$CFLAGS -Wa,--gsframe" + AC_MSG_CHECKING([for as that supports --gsframe]) + AC_TRY_COMPILE([], [return 0;], [ac_cv_have_sframe=yes], [ac_cv_have_sframe=no]) + AC_MSG_RESULT($ac_cv_have_sframe) + CFLAGS="$ac_save_CFLAGS" +]) diff --git a/include/sframe-backtrace-api.h b/include/sframe-backtrace-api.h new file mode 100644 index 00000000000..ad8f6bed024 --- /dev/null +++ b/include/sframe-backtrace-api.h @@ -0,0 +1,57 @@ +/* Public API to SFrame backtrace. + + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of libsframebt. + + 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 . */ + +#ifndef _SFRAME_BACKTRACE_API_H +#define _SFRAME_BACKTRACE_API_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +enum sframe_bt_errcode +{ + SFRAME_BT_OK, + SFRAME_BT_ERR_NOTPRESENT, + SFRAME_BT_ERR_PHDR, + SFRAME_BT_ERR_ARG, + SFRAME_BT_ERR_MALLOC, + SFRAME_BT_ERR_REALLOC, + SFRAME_BT_ERR_OPEN, + SFRAME_BT_ERR_READLINK, + SFRAME_BT_ERR_LSEEK, + SFRAME_BT_ERR_READ, + SFRAME_BT_ERR_GETCONTEXT, + SFRAME_BT_ERR_DECODE, + SFRAME_BT_ERR_CFA_OFFSET, +}; + +/* Get the backtrace of the calling program by storing return addresses + in BUFFER. The SIZE argument specifies the maximum number of addresses + that can be stored in the buffer. Return the number of return addresses + collected or -1 if there is any error. */ +extern int sframe_backtrace (void **buffer, int size, int *errp); + +extern const char *sframe_bt_errmsg (enum sframe_bt_errcode ecode); + +#ifdef __cplusplus +} +#endif + +#endif /* _SFRAME_BACKTRACE_API_H */ diff --git a/libsframe/Makefile.am b/libsframe/Makefile.am index 31576429816..1b8a1081975 100644 --- a/libsframe/Makefile.am +++ b/libsframe/Makefile.am @@ -40,3 +40,15 @@ endif libsframe_la_SOURCES = sframe.c sframe-dump.c sframe-error.c libsframe_la_CPPFLAGS = -D_GNU_SOURCE -I$(srcdir) -I$(srcdir)/../include \ -I$(srcdir)/../libctf + +if HAVE_SFRAME_AS + libsframebt_la_SOURCES = sframe-backtrace.c sframe-backtrace-err.c + libsframebt_la_CPPFLAGS = -I$(srcdir) -I$(srcdir)/../include + libsframebt_la_CFLAGS = -Wa,--gsframe +if INSTALL_LIBBFD + lib_LTLIBRARIES += libsframebt.la + include_HEADERS += $(INCDIR)/sframe-backtrace-api.h +else + noinst_LTLIBRARIES += libsframebt.la +endif +endif diff --git a/libsframe/Makefile.in b/libsframe/Makefile.in index 0db5ff954b9..f5c32b7c9b9 100644 --- a/libsframe/Makefile.in +++ b/libsframe/Makefile.in @@ -107,6 +107,9 @@ PRE_UNINSTALL = : POST_UNINSTALL = : build_triplet = @build@ host_triplet = @host@ +@HAVE_SFRAME_AS_TRUE@@INSTALL_LIBBFD_TRUE@am__append_1 = libsframebt.la +@HAVE_SFRAME_AS_TRUE@@INSTALL_LIBBFD_TRUE@am__append_2 = $(INCDIR)/sframe-backtrace-api.h +@HAVE_SFRAME_AS_TRUE@@INSTALL_LIBBFD_FALSE@am__append_3 = libsframebt.la subdir = . ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/../bfd/acinclude.m4 \ @@ -115,6 +118,7 @@ am__aclocal_m4_deps = $(top_srcdir)/../bfd/acinclude.m4 \ $(top_srcdir)/../config/jobserver.m4 \ $(top_srcdir)/../config/lead-dot.m4 \ $(top_srcdir)/../config/override.m4 \ + $(top_srcdir)/../config/sframe.m4 \ $(top_srcdir)/../config/warnings.m4 \ $(top_srcdir)/../libtool.m4 $(top_srcdir)/../ltoptions.m4 \ $(top_srcdir)/../ltsugar.m4 $(top_srcdir)/../ltversion.m4 \ @@ -169,6 +173,20 @@ am__v_lt_0 = --silent am__v_lt_1 = @INSTALL_LIBBFD_FALSE@am_libsframe_la_rpath = @INSTALL_LIBBFD_TRUE@am_libsframe_la_rpath = -rpath $(libdir) +libsframebt_la_LIBADD = +am__libsframebt_la_SOURCES_DIST = sframe-backtrace.c \ + sframe-backtrace-err.c +@HAVE_SFRAME_AS_TRUE@am_libsframebt_la_OBJECTS = \ +@HAVE_SFRAME_AS_TRUE@ libsframebt_la-sframe-backtrace.lo \ +@HAVE_SFRAME_AS_TRUE@ libsframebt_la-sframe-backtrace-err.lo +libsframebt_la_OBJECTS = $(am_libsframebt_la_OBJECTS) +libsframebt_la_LINK = $(LIBTOOL) $(AM_V_lt) --tag=CC \ + $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=link $(CCLD) \ + $(libsframebt_la_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o \ + $@ +@HAVE_SFRAME_AS_TRUE@@INSTALL_LIBBFD_FALSE@am_libsframebt_la_rpath = +@HAVE_SFRAME_AS_TRUE@@INSTALL_LIBBFD_TRUE@am_libsframebt_la_rpath = \ +@HAVE_SFRAME_AS_TRUE@@INSTALL_LIBBFD_TRUE@ -rpath $(libdir) AM_V_P = $(am__v_P_@AM_V@) am__v_P_ = $(am__v_P_@AM_DEFAULT_V@) am__v_P_0 = false @@ -203,8 +221,9 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@) am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@) am__v_CCLD_0 = @echo " CCLD " $@; am__v_CCLD_1 = -SOURCES = $(libsframe_la_SOURCES) -DIST_SOURCES = $(libsframe_la_SOURCES) +SOURCES = $(libsframe_la_SOURCES) $(libsframebt_la_SOURCES) +DIST_SOURCES = $(libsframe_la_SOURCES) \ + $(am__libsframebt_la_SOURCES_DIST) RECURSIVE_TARGETS = all-recursive check-recursive cscopelist-recursive \ ctags-recursive dvi-recursive html-recursive info-recursive \ install-data-recursive install-dvi-recursive \ @@ -218,7 +237,8 @@ am__can_run_installinfo = \ n|no|NO) false;; \ *) (install-info --version) >/dev/null 2>&1;; \ esac -am__include_HEADERS_DIST = $(INCDIR)/sframe.h $(INCDIR)/sframe-api.h +am__include_HEADERS_DIST = $(INCDIR)/sframe-backtrace-api.h \ + $(INCDIR)/sframe.h $(INCDIR)/sframe-api.h HEADERS = $(include_HEADERS) RECURSIVE_CLEAN_TARGETS = mostlyclean-recursive clean-recursive \ distclean-recursive maintainer-clean-recursive @@ -426,14 +446,19 @@ INCDIR = $(srcdir)/../include # include libctf for swap.h AM_CPPFLAGS = -D_GNU_SOURCE -I$(srcdir) -I$(srcdir)/../include -I$(srcdir)/../libctf AM_CFLAGS = -std=gnu99 @ac_libsframe_warn_cflags@ @warn@ @c_warn@ @WARN_PEDANTIC@ @WERROR@ -@INSTALL_LIBBFD_TRUE@lib_LTLIBRARIES = libsframe.la -@INSTALL_LIBBFD_FALSE@include_HEADERS = -@INSTALL_LIBBFD_TRUE@include_HEADERS = $(INCDIR)/sframe.h $(INCDIR)/sframe-api.h -@INSTALL_LIBBFD_FALSE@noinst_LTLIBRARIES = libsframe.la +@INSTALL_LIBBFD_TRUE@lib_LTLIBRARIES = libsframe.la $(am__append_1) +@INSTALL_LIBBFD_FALSE@include_HEADERS = $(am__append_2) +@INSTALL_LIBBFD_TRUE@include_HEADERS = $(INCDIR)/sframe.h \ +@INSTALL_LIBBFD_TRUE@ $(INCDIR)/sframe-api.h $(am__append_2) +@INSTALL_LIBBFD_FALSE@noinst_LTLIBRARIES = libsframe.la \ +@INSTALL_LIBBFD_FALSE@ $(am__append_3) libsframe_la_SOURCES = sframe.c sframe-dump.c sframe-error.c libsframe_la_CPPFLAGS = -D_GNU_SOURCE -I$(srcdir) -I$(srcdir)/../include \ -I$(srcdir)/../libctf +@HAVE_SFRAME_AS_TRUE@libsframebt_la_SOURCES = sframe-backtrace.c sframe-backtrace-err.c +@HAVE_SFRAME_AS_TRUE@libsframebt_la_CPPFLAGS = -I$(srcdir) -I$(srcdir)/../include +@HAVE_SFRAME_AS_TRUE@libsframebt_la_CFLAGS = -Wa,--gsframe all: config.h $(MAKE) $(AM_MAKEFLAGS) all-recursive @@ -537,6 +562,9 @@ clean-noinstLTLIBRARIES: libsframe.la: $(libsframe_la_OBJECTS) $(libsframe_la_DEPENDENCIES) $(EXTRA_libsframe_la_DEPENDENCIES) $(AM_V_CCLD)$(LINK) $(am_libsframe_la_rpath) $(libsframe_la_OBJECTS) $(libsframe_la_LIBADD) $(LIBS) +libsframebt.la: $(libsframebt_la_OBJECTS) $(libsframebt_la_DEPENDENCIES) $(EXTRA_libsframebt_la_DEPENDENCIES) + $(AM_V_CCLD)$(libsframebt_la_LINK) $(am_libsframebt_la_rpath) $(libsframebt_la_OBJECTS) $(libsframebt_la_LIBADD) $(LIBS) + mostlyclean-compile: -rm -f *.$(OBJEXT) @@ -546,6 +574,8 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsframe_la-sframe-dump.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsframe_la-sframe-error.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsframe_la-sframe.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsframebt_la-sframe-backtrace-err.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsframebt_la-sframe-backtrace.Plo@am__quote@ .c.o: @am__fastdepCC_TRUE@ $(AM_V_CC)$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< @@ -589,6 +619,20 @@ libsframe_la-sframe-error.lo: sframe-error.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsframe_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libsframe_la-sframe-error.lo `test -f 'sframe-error.c' || echo '$(srcdir)/'`sframe-error.c +libsframebt_la-sframe-backtrace.lo: sframe-backtrace.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsframebt_la_CPPFLAGS) $(CPPFLAGS) $(libsframebt_la_CFLAGS) $(CFLAGS) -MT libsframebt_la-sframe-backtrace.lo -MD -MP -MF $(DEPDIR)/libsframebt_la-sframe-backtrace.Tpo -c -o libsframebt_la-sframe-backtrace.lo `test -f 'sframe-backtrace.c' || echo '$(srcdir)/'`sframe-backtrace.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libsframebt_la-sframe-backtrace.Tpo $(DEPDIR)/libsframebt_la-sframe-backtrace.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='sframe-backtrace.c' object='libsframebt_la-sframe-backtrace.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsframebt_la_CPPFLAGS) $(CPPFLAGS) $(libsframebt_la_CFLAGS) $(CFLAGS) -c -o libsframebt_la-sframe-backtrace.lo `test -f 'sframe-backtrace.c' || echo '$(srcdir)/'`sframe-backtrace.c + +libsframebt_la-sframe-backtrace-err.lo: sframe-backtrace-err.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsframebt_la_CPPFLAGS) $(CPPFLAGS) $(libsframebt_la_CFLAGS) $(CFLAGS) -MT libsframebt_la-sframe-backtrace-err.lo -MD -MP -MF $(DEPDIR)/libsframebt_la-sframe-backtrace-err.Tpo -c -o libsframebt_la-sframe-backtrace-err.lo `test -f 'sframe-backtrace-err.c' || echo '$(srcdir)/'`sframe-backtrace-err.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libsframebt_la-sframe-backtrace-err.Tpo $(DEPDIR)/libsframebt_la-sframe-backtrace-err.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='sframe-backtrace-err.c' object='libsframebt_la-sframe-backtrace-err.lo' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsframebt_la_CPPFLAGS) $(CPPFLAGS) $(libsframebt_la_CFLAGS) $(CFLAGS) -c -o libsframebt_la-sframe-backtrace-err.lo `test -f 'sframe-backtrace-err.c' || echo '$(srcdir)/'`sframe-backtrace-err.c + mostlyclean-libtool: -rm -f *.lo diff --git a/libsframe/aclocal.m4 b/libsframe/aclocal.m4 index 3a0b3426ebc..d2f540bb073 100644 --- a/libsframe/aclocal.m4 +++ b/libsframe/aclocal.m4 @@ -1233,6 +1233,7 @@ m4_include([../config/depstand.m4]) m4_include([../config/jobserver.m4]) m4_include([../config/lead-dot.m4]) m4_include([../config/override.m4]) +m4_include([../config/sframe.m4]) m4_include([../config/warnings.m4]) m4_include([../libtool.m4]) m4_include([../ltoptions.m4]) diff --git a/libsframe/configure.ac b/libsframe/configure.ac index d2f32ae2ebd..82fafee5068 100644 --- a/libsframe/configure.ac +++ b/libsframe/configure.ac @@ -57,6 +57,13 @@ ACX_PROG_CC_WARNING_ALMOST_PEDANTIC([-Wno-long-long]) # corrected. ACX_PROG_CC_WARNINGS_ARE_ERRORS([manual]) +dnl The libsframebt library needs to be built with SFrame info. +dnl If the build assembler is not capable of generate SFrame then +dnl the library is not built. + +SFRAME_CHECK_AS_SFRAME +AM_CONDITIONAL([HAVE_SFRAME_AS], [test "x$ac_cv_have_sframe" = "xyes"]) + AM_MAINTAINER_MODE AM_INSTALL_LIBBFD ACX_PROG_CC_WARNING_OPTS([-Wall], [ac_libsframe_warn_cflags]) diff --git a/libsframe/sframe-backtrace-err.c b/libsframe/sframe-backtrace-err.c new file mode 100644 index 00000000000..70bd55ccf35 --- /dev/null +++ b/libsframe/sframe-backtrace-err.c @@ -0,0 +1,46 @@ +/* sframe-backtrace-err.c - SFrame Backtrace Error table. + + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of libsframebt. + + 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 "config.h" +#include "sframe-backtrace-api.h" + +/* SFrame backtrace error messages. */ +static const char *const sframe_bt_errlist[] = +{ + "", + "File does not contain SFrame data", + "Iterating shared object reading error", + "Failed to malloc memory space", + "Failed to realloc memory space", + "Failed to open file", + "Failed on resolve canonical file name", + "Failed to reposition file offset", + "Failed to read from a file descriptor", + "Failed to get the user context", + "Failed to set up decode data", + "Illegal CFA offset" +}; + +/* Return the error message associated with the error code. */ + +const char * +sframe_bt_errmsg (enum sframe_bt_errcode ecode) +{ + return sframe_bt_errlist[ecode]; +} diff --git a/libsframe/sframe-backtrace.c b/libsframe/sframe-backtrace.c new file mode 100644 index 00000000000..dcee7c486b5 --- /dev/null +++ b/libsframe/sframe-backtrace.c @@ -0,0 +1,619 @@ +/* sframe-backtrace.c - The SFrame backtracer. + + Copyright (C) 2022 Free Software Foundation, Inc. + + This file is part of libsframebt. + + 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 . */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ansidecl.h" +#include "sframe-api.h" +#include "sframe-backtrace-api.h" + +#ifndef PT_SFRAME +#define PT_SFRAME 0x6474e554 /* FIXME. */ +#endif + +#define _sf_printflike_(string_index,first_to_check) \ + __attribute__ ((__format__ (__printf__, (string_index), (first_to_check)))) + +static int _sframe_unwind_debug; /* Control for printing out debug info. */ +static int no_of_entries = 32; + +/* SFrame decode data for the main module or a DSO. */ +struct sframe_decode_data +{ + char *sfdd_data; /* SFrame decode data. */ + int sfdd_data_size; /* SFrame decode data size. */ + uint64_t sfdd_text_vma; /* Text segment's virtual address. */ + int sfdd_text_size; /* Text segment's size. */ + uint64_t sfdd_sframe_vma; /* SFrame segment's virtual address. */ + sframe_decoder_ctx *sfdd_sframe_ctx; /* SFrame decoder context. */ +}; + +/* List that holds SFrame info for the shared libraries. */ +struct dso_cfi_list +{ + int alloced; /* Entries allocated. */ + int used; /* Entries used. */ + struct sframe_decode_data *entry; /* DSO's decode data. */ +}; + +/* Data that's passed through sframe_callback. */ +struct sframe_unwind_info +{ + int sui_fd; /* File descriptor. */ + struct sframe_decode_data sui_ctx; /* The decode data. */ + struct dso_cfi_list sui_dsos; /* The DSO list. */ +}; + +static void +sframe_unwind_init_debug (void) +{ + static int inited; + + if (!inited) + { + _sframe_unwind_debug = getenv ("SFRAME_UNWIND_DEBUG") != NULL; + inited = 1; + } +} + +_sf_printflike_ (1, 2) +static void +debug_printf (const char *format, ...) +{ + if (_sframe_unwind_debug) + { + va_list args; + + va_start (args, format); + __builtin_vprintf (format, args); + va_end (args); + } +} + +/* sframe_bt_errno - Check if there is error code in ERRP. */ + +static int +sframe_bt_errno (int *errp) +{ + if (errp == NULL) + return 0; + + return (*errp != SFRAME_BT_OK); +} + +/* sframe_bt_set_errno - Store the specified error code ERROR into ERRP if + it is non-NULL. */ + +static void +sframe_bt_set_errno (int *errp, int error) +{ + if (errp != NULL) + *errp = error; +} + +/* sframe_add_dso - Add .sframe info in D_DATA, which is associated with + a dynamic shared object, to D_LIST. */ + +static void +sframe_add_dso (struct dso_cfi_list *d_list, + struct sframe_decode_data d_data, + int *errp) +{ + if (d_list->alloced == 0) + { + d_list->entry = malloc (no_of_entries * sizeof (struct sframe_decode_data)); + if (d_list->entry == NULL) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_MALLOC); + return; + } + memset (d_list->entry, 0, + no_of_entries * sizeof (struct sframe_decode_data)); + d_list->alloced = no_of_entries; + } + else if (d_list->used == d_list->alloced) + { + d_list->entry = realloc (d_list->entry, + (d_list->alloced + no_of_entries) * + sizeof (struct sframe_decode_data)); + if (d_list->entry == NULL) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_REALLOC); + return; + } + + memset (&d_list->entry[d_list->alloced], 0, + no_of_entries * sizeof (struct sframe_decode_data)); + d_list->alloced += no_of_entries; + } + + sframe_bt_set_errno (errp, SFRAME_BT_OK); + d_list->entry[d_list->used++] = d_data; +} + +/* sframe_free_cfi - Free up space allocated for .sframe info for CF. */ + +static void +sframe_free_cfi (struct sframe_unwind_info *sf) +{ + struct dso_cfi_list *d_list; + int i; + + if (sf == NULL) + return; + + free (sf->sui_ctx.sfdd_data); + sframe_decoder_free (&sf->sui_ctx.sfdd_sframe_ctx); + close (sf->sui_fd); + + d_list = &sf-> sui_dsos; + if (d_list == NULL) + return; + + for (i = 0; i < d_list->used; ++i) + { + free (d_list->entry[i].sfdd_data); + sframe_decoder_free (&d_list->entry[i].sfdd_sframe_ctx); + } + + free (d_list->entry); +} + +/* sframe_find_context - Find the decode data that contains ADDR from CF. + Return the pointer to the decode data or NULL. */ + +static struct sframe_decode_data * +sframe_find_context (struct sframe_unwind_info *sf, uint64_t addr) +{ + struct dso_cfi_list *d_list; + int i; + + if (sf == NULL) + return NULL; + + if (sf->sui_ctx.sfdd_text_vma < addr + && sf->sui_ctx.sfdd_text_vma + sf->sui_ctx.sfdd_text_size > addr) + return &sf->sui_ctx; + + d_list = &sf->sui_dsos; + for (i = 0; i < sf->sui_dsos.used; ++i) + { + if (d_list->entry[i].sfdd_text_vma <= addr && + d_list->entry[i].sfdd_text_vma + + d_list->entry[i].sfdd_text_size >= addr) + return &d_list->entry[i]; + } + + return NULL; +} + +/* sframe_valid_addr - Check if ADDR is valid in CF. The address is considered + invalid, with regards to SFrame, if it's not in any address range of the + main module or any of its DSO's. Return 1 if valid, 0 otherwise. */ + +static int +sframe_valid_addr (struct sframe_unwind_info *sf, uint64_t addr) +{ + struct sframe_decode_data *cdp; + + if (sf == NULL) + return 0; + + cdp = sframe_find_context (sf, addr); + return cdp ? 1 : 0; +} + +/* sframe_load_ctx - Call decoder to create and set up the SFrame info for + either the main module or one of the DSOs from CF, based on the input + RADDR argument. Return the newly created decode context or NULL. */ + +static sframe_decoder_ctx * +sframe_load_ctx (struct sframe_unwind_info *sf, uint64_t raddr) +{ + sframe_decoder_ctx *nctx; + struct sframe_decode_data *cdp; + + if (sf == NULL) + return NULL; + + cdp = sframe_find_context (sf, raddr); + if (cdp == NULL) + return NULL; + + if (cdp->sfdd_sframe_ctx == NULL) + { + int err; + nctx = sframe_decode (cdp->sfdd_data, cdp->sfdd_data_size, &err); + if (nctx == NULL) + return NULL; + cdp->sfdd_sframe_ctx = nctx; + return nctx; + } + + return NULL; +} + +/* sframe_update_ctx - Check if need to do a decode context switch, based on + the input RADDR argument, from CF. A new decode context will be created + and set up if it isn't already done so. Return the new decode context in + CTX and vma in CFI_VMA. */ + +static void +sframe_update_ctx (struct sframe_unwind_info *sf, uint64_t raddr, + sframe_decoder_ctx **ctx, uint64_t *cfi_vma) +{ + sframe_decoder_ctx *nctx; + struct sframe_decode_data *cdp; + + cdp = sframe_find_context (sf, raddr); + if (cdp != NULL) + { + if (cdp->sfdd_sframe_ctx == NULL) + { + int err; + nctx = sframe_decode (cdp->sfdd_data, cdp->sfdd_data_size, &err); + if (nctx == NULL) + { + *ctx = NULL; + return; + } + cdp->sfdd_sframe_ctx = nctx; + } + *ctx = cdp->sfdd_sframe_ctx; + *cfi_vma = cdp->sfdd_sframe_vma; + } +} + +/* get_contents - Return contents at ADDR from file descriptor FD. */ + +static uint64_t +get_contents (int fd, uint64_t addr, int *errp) +{ + uint64_t data; + size_t sz; + + sframe_bt_set_errno (errp, SFRAME_BT_OK); + if (lseek (fd, addr, SEEK_SET) == -1) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_LSEEK); + return 0; + } + sz = read (fd, &data, sizeof (uint64_t)); + if (sz != sizeof (uint64_t)) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_READ); + return 0; + } + + return data; +} + +/* sframe_fd_open - Open /proc image associated with the process id and return + the file descriptor. */ + +static int +sframe_fd_open (int *errp) +{ + char filename[PATH_MAX]; + pid_t pid; + int fd; + + pid = getpid (); + snprintf (filename, sizeof filename, "/proc/%d/task/%d/mem", pid, pid); + if ((fd = open (filename, O_RDONLY)) == -1) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_OPEN); + return -1; + } + + return fd; +} + +/* sframe_callback - The callback from dl_iterate_phdr with header info + in INFO. + Return SFrame info for either the main module or a DSO in DATA. */ + +static int +sframe_callback (struct dl_phdr_info *info, + size_t size ATTRIBUTE_UNUSED, + void *data) +{ + struct sframe_unwind_info *sf = (struct sframe_unwind_info *) data; + int p_type, i, fd, sframe_err; + ssize_t len; + uint64_t text_vma = 0; + int text_size = 0; + + if (data == NULL || info == NULL) + return 1; + + debug_printf ("-- name: %s %14p\n", info->dlpi_name, (void *)info->dlpi_addr); + + for (i = 0; i < info->dlpi_phnum; i++) + { + debug_printf(" %2d: [%14p; memsz:%7lx] flags: 0x%x; \n", i, + (void *) info->dlpi_phdr[i].p_vaddr, + info->dlpi_phdr[i].p_memsz, + info->dlpi_phdr[i].p_flags); + + p_type = info->dlpi_phdr[i].p_type; + if (p_type == PT_LOAD && info->dlpi_phdr[i].p_flags & PF_X) + { + text_vma = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr; + text_size = info->dlpi_phdr[i].p_memsz; + continue; + } + if (p_type != PT_SFRAME) + continue; + + if (info->dlpi_name[0] == '\0') /* the main module. */ + { + fd = sframe_fd_open (&sframe_err); + if (fd == -1) + return 1; + if (lseek (fd, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, + SEEK_SET) == -1) + { + sframe_bt_set_errno (&sframe_err, SFRAME_BT_ERR_LSEEK); + return 1; + } + + sf->sui_ctx.sfdd_data = (char *) malloc (info->dlpi_phdr[i].p_memsz); + if (sf->sui_ctx.sfdd_data == NULL) + { + sframe_bt_set_errno (&sframe_err, SFRAME_BT_ERR_MALLOC); + return 1; + } + + len = read (fd, sf->sui_ctx.sfdd_data, info->dlpi_phdr[i].p_memsz); + if (len == -1 || len != (ssize_t) info->dlpi_phdr[i].p_memsz) + { + sframe_bt_set_errno (&sframe_err, SFRAME_BT_ERR_READ); + return 1; + } + + assert (text_vma); + sf->sui_ctx.sfdd_data_size = len; + sf->sui_ctx.sfdd_sframe_vma = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr; + sf->sui_fd = fd; + sf->sui_ctx.sfdd_text_vma = text_vma; + sf->sui_ctx.sfdd_text_size = text_size; + text_vma = 0; + return 0; + } + else + { /* a dynamic shared object. */ + struct sframe_decode_data dt; + memset (&dt, 0, sizeof (struct sframe_decode_data)); + assert (sf->sui_fd); + if (lseek (sf->sui_fd, info->dlpi_addr + info->dlpi_phdr[i].p_vaddr, + SEEK_SET) == -1) + { + sframe_bt_set_errno (&sframe_err, SFRAME_BT_ERR_LSEEK); + return 1; + } + + dt.sfdd_data = (char *) malloc (info->dlpi_phdr[i].p_memsz); + if (dt.sfdd_data == NULL) + { + sframe_bt_set_errno (&sframe_err, SFRAME_BT_ERR_MALLOC); + return 1; + } + + len = read (sf->sui_fd, dt.sfdd_data, info->dlpi_phdr[i].p_memsz); + if (len == -1 || len != (ssize_t) info->dlpi_phdr[i].p_memsz) + { + sframe_bt_set_errno (&sframe_err, SFRAME_BT_ERR_READ); + return 1; + } + + assert (text_vma); + dt.sfdd_data_size = len; + dt.sfdd_sframe_vma = info->dlpi_addr + info->dlpi_phdr[i].p_vaddr; + dt.sfdd_text_vma = text_vma; + dt.sfdd_text_size = text_size; + text_vma = 0; + sframe_add_dso (&sf->sui_dsos, dt, &sframe_err); + if (sframe_err != SFRAME_BT_OK) + return 1; + return 0; + } + } + + return 0; +} + +/* sframe_unwind - Unwind the stack backtrace for CF. If successful, + store the return addresses in RA_LST. The RA_SIZE argument specifies + the maximum number of return addresses that can be stored in RA_LST + and contains the number of the addresses collected. */ + +static void +sframe_unwind (struct sframe_unwind_info *sf, void **ra_lst, + int *ra_size, int *errp) +{ + uint64_t cfa, return_addr, ra_stack_loc, rfp_stack_loc; + sframe_decoder_ctx *ctx; + int cfa_offset, rfp_offset, errnum, i, count; + sframe_frame_row_entry fred, *frep = &fred; + uint64_t pc, rfp, rsp, cfi_vma; + ucontext_t context, *cp = &context; + + if (sf == NULL || ra_lst == NULL || ra_size == NULL) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_ARG); + return; + } + + /* Get the user context for its registers. */ + if (getcontext (cp) != 0) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_GETCONTEXT); + return; + } + sframe_bt_set_errno (errp, SFRAME_BT_OK); + +#ifdef __x86_64__ + pc = cp->uc_mcontext.gregs[REG_RIP]; + rsp = cp->uc_mcontext.gregs[REG_RSP]; + rfp = cp->uc_mcontext.gregs[REG_RBP]; +#else +#ifdef __aarch64__ +#define UNWIND_AARCH64_X29 29 /* 64-bit frame pointer. */ +#define UNWIND_AARCH64_X30 30 /* 64-bit link pointer. */ + pc = cp->uc_mcontext.pc; + rsp = cp->uc_mcontext.sp; + rfp = cp->uc_mcontext.regs[UNWIND_AARCH64_X29]; + uint64_t ra = cp->uc_mcontext.regs[UNWIND_AARCH64_X30]; +#endif +#endif + + /* Load and set up the decoder. */ + ctx = sframe_load_ctx (sf, pc); + if (ctx == NULL) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_DECODE); + return; + } + cfi_vma = sf->sui_ctx.sfdd_sframe_vma; + count = *ra_size; + + for (i = 0; i < count; ++i) + { + pc -= cfi_vma; + errnum = sframe_find_fre (ctx, pc, frep); + if (errnum == 0) + { + cfa_offset = sframe_fre_get_cfa_offset (frep, &errnum); + if (errnum == ESFRAME_FREOFFSET_NOPRESENT) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_CFA_OFFSET); + return; + } + + cfa = ((frep->fre_info & 0x1) == SFRAME_BASE_REG_SP + ? rsp : rfp) + cfa_offset; + +#ifdef __x86_64__ + /* For x86, read the return address from cfa - 8. */ + ra_stack_loc = cfa - 8; + return_addr = get_contents (sf->sui_fd, ra_stack_loc, errp); + if (sframe_bt_errno (errp)) + return; +#else +#ifdef __aarch64__ + int ra_offset = sframe_fre_get_ra_offset (frep, &errnum); + if (errnum == 0) + { + ra_stack_loc = cfa + ra_offset; + return_addr = get_contents (sf->sui_fd, ra_stack_loc, errp); + if (sframe_bt_errno (errp)) + return; + } + else + return_addr = ra; +#endif +#endif + + /* Validate and add return address to the list. */ + if (sframe_valid_addr (sf, return_addr) == 0) + { + i -= 1; + goto find_fre_ra_err; + } + if (i != 0) /* exclude self. */ + ra_lst[i-1] = (void *)return_addr; + + /* Set up for the next frame. */ + rfp_offset = sframe_fre_get_fp_offset (frep, &errnum); + if (errnum == 0) + { + rfp_stack_loc = cfa + rfp_offset; + rfp = get_contents (sf->sui_fd, rfp_stack_loc, errp); + if (sframe_bt_errno (errp)) + return; + } + rsp = cfa; + pc = return_addr; + + /* Check if need to update the decoder context and vma. */ + sframe_update_ctx (sf, return_addr, &ctx, &cfi_vma); + if (ctx == NULL) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_DECODE); + return; + } + } + else + { + i -= 1; + goto find_fre_ra_err; + } + } + +find_fre_ra_err: + *ra_size = i; +} + +/* sframe_backtrace - Main API that user program calls to get a backtrace. + The BUFFER argument provides space for the list of the return addresses + and the SIZE argument specifies the maximum number of addresses that + can be stored in the buffer. Return the number of return addresses + collected or -1 if there is any error. */ + +int +sframe_backtrace (void **buffer, int size, int *errp) +{ + struct sframe_unwind_info sframeinfo; + + sframe_unwind_init_debug (); + + memset (&sframeinfo, 0, sizeof (struct sframe_unwind_info)); + + /* find and set up the .sframe sections. */ + (void) dl_iterate_phdr (sframe_callback, (void *)&sframeinfo); + if (sframeinfo.sui_fd == 0) + { + sframe_bt_set_errno (errp, SFRAME_BT_ERR_NOTPRESENT); + return -1; + } + + /* Do the stack unwinding. */ + sframe_unwind (&sframeinfo, buffer, &size, errp); + if (sframe_bt_errno (errp)) + return -1; + + sframe_free_cfi (&sframeinfo); + + return size; +} diff --git a/libsframe/testsuite/Makefile.in b/libsframe/testsuite/Makefile.in index f97c4330eb7..b89a4a4d052 100644 --- a/libsframe/testsuite/Makefile.in +++ b/libsframe/testsuite/Makefile.in @@ -95,6 +95,7 @@ am__aclocal_m4_deps = $(top_srcdir)/../bfd/acinclude.m4 \ $(top_srcdir)/../config/jobserver.m4 \ $(top_srcdir)/../config/lead-dot.m4 \ $(top_srcdir)/../config/override.m4 \ + $(top_srcdir)/../config/sframe.m4 \ $(top_srcdir)/../config/warnings.m4 \ $(top_srcdir)/../libtool.m4 $(top_srcdir)/../ltoptions.m4 \ $(top_srcdir)/../ltsugar.m4 $(top_srcdir)/../ltversion.m4 \ diff --git a/libsframe/testsuite/libsframe.decode/Makefile.in b/libsframe/testsuite/libsframe.decode/Makefile.in index f4a1e43c374..210d9ae6856 100644 --- a/libsframe/testsuite/libsframe.decode/Makefile.in +++ b/libsframe/testsuite/libsframe.decode/Makefile.in @@ -97,6 +97,7 @@ am__aclocal_m4_deps = $(top_srcdir)/../bfd/acinclude.m4 \ $(top_srcdir)/../config/jobserver.m4 \ $(top_srcdir)/../config/lead-dot.m4 \ $(top_srcdir)/../config/override.m4 \ + $(top_srcdir)/../config/sframe.m4 \ $(top_srcdir)/../config/warnings.m4 \ $(top_srcdir)/../libtool.m4 $(top_srcdir)/../ltoptions.m4 \ $(top_srcdir)/../ltsugar.m4 $(top_srcdir)/../ltversion.m4 \ diff --git a/libsframe/testsuite/libsframe.encode/Makefile.in b/libsframe/testsuite/libsframe.encode/Makefile.in index 678a9ce9b43..f72a378263d 100644 --- a/libsframe/testsuite/libsframe.encode/Makefile.in +++ b/libsframe/testsuite/libsframe.encode/Makefile.in @@ -96,6 +96,7 @@ am__aclocal_m4_deps = $(top_srcdir)/../bfd/acinclude.m4 \ $(top_srcdir)/../config/jobserver.m4 \ $(top_srcdir)/../config/lead-dot.m4 \ $(top_srcdir)/../config/override.m4 \ + $(top_srcdir)/../config/sframe.m4 \ $(top_srcdir)/../config/warnings.m4 \ $(top_srcdir)/../libtool.m4 $(top_srcdir)/../ltoptions.m4 \ $(top_srcdir)/../ltsugar.m4 $(top_srcdir)/../ltversion.m4 \ -- 2.37.2