From patchwork Fri Mar 10 14:55:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 66221 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 53731385559A for ; Fri, 10 Mar 2023 14:57:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53731385559A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1678460241; bh=8o4zp0EY7mnI95BmBWFvE2ddWETu3vN69sjbs++o6CU=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=UxcPhFO+uykKVntMb5LuKat4s4CtPWaviMXKx0sCvVL3/pYMjCYt8U9HYUJvuLK24 JHKZm0NvaGRyV0DqsG61FFB/au4an3lwIGT837W7BlMy0W8rHLU8TAuscJT5vgqIVc 2+graZOUhhbZwrIgaf13EK1komcz+jXk/rAJMC2A= 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 ESMTPS id E27AD3858C36 for ; Fri, 10 Mar 2023 14:56:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E27AD3858C36 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-670-QOPNght4N3CSVXQ3gTfwGQ-1; Fri, 10 Mar 2023 09:56:04 -0500 X-MC-Unique: QOPNght4N3CSVXQ3gTfwGQ-1 Received: by mail-wm1-f69.google.com with SMTP id k36-20020a05600c1ca400b003eac86e4387so3958979wms.8 for ; Fri, 10 Mar 2023 06:56:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678460163; 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=8o4zp0EY7mnI95BmBWFvE2ddWETu3vN69sjbs++o6CU=; b=SwNjYFPc/lwI674uxptgYZDltUZPvrhAzy1tYqK+qTuzQOg99ddvbWi3//Q7/m5ssy oSEQHgdPEU/gp56DuhOJiQcJ4GsFD8ac7oX5ckfrqGf9T9eELjkhtzrBAFfjM6igrzmf DSvBjbfXp6tTcFMQiKlFN9xsoUshmbFc+TYj6kQKoV12aBlGOVmjvknCsuWgnplU3Y7l XGGCZATrdqcegeaQRZzVPodoBh8UjUn3buIqSuseWuDNZ2OZOJ6afBNVKtHIpizoL0DM yY6Khyd0in/oWnFQPvqGX1WDAx0YTlSeexI7AWml0n46l6Q3RKK6HMnfBHW2+jrxI+8i DI5w== X-Gm-Message-State: AO0yUKVtMncSQ4nU/KB2A6oe2pLz77UyqNK+WMrV5+GLaFPeuWfTVJwp 9ljcD8LvDpldeJ4Cm1Rr/1T2VGbqb4kWsukmho6p9B0xuCxdpY5mvMBB8PdkoTmJC3rFSE+Kaci mqVsDbJ5CTgtceV9dIcgjtYKDfRR7GtusrxqIkm4FzOQyAp88Z5e3YUJ20OFluCCxjfwSt+uQKC AWWnO3HQ== X-Received: by 2002:a05:600c:ccf:b0:3eb:36fa:b78d with SMTP id fk15-20020a05600c0ccf00b003eb36fab78dmr2935638wmb.23.1678460162910; Fri, 10 Mar 2023 06:56:02 -0800 (PST) X-Google-Smtp-Source: AK7set/01e+YmLWjw6gOy3ehJcNsVf37mw9xMSLJmH8X6nM3t7wDGWHNp89u31P6pNPAw3oE7jpoeg== X-Received: by 2002:a05:600c:ccf:b0:3eb:36fa:b78d with SMTP id fk15-20020a05600c0ccf00b003eb36fab78dmr2935625wmb.23.1678460162658; Fri, 10 Mar 2023 06:56:02 -0800 (PST) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id m8-20020a05600c4f4800b003db01178b62sm87504wmq.40.2023.03.10.06.56.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Mar 2023 06:56:02 -0800 (PST) To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 10/10] gdb/python: Add new gdb.unwinder.FrameId class Date: Fri, 10 Mar 2023 14:55:27 +0000 Message-Id: <3f0b625f18141d66fe610cf06bc82cd644c50751.1678460067.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" When writing an unwinder it is necessary to create a new class to act as a frame-id. This new class is almost certainly just going to set a 'sp' and 'pc' attribute within the instance. This commit adds a little helper class gdb.unwinder.FrameId that does this job. Users can make use of this to avoid having to write out standard boilerplate code any time they write an unwinder. Of course, if the user wants their FrameId class to be more complicated in some way, then they can still write their own class, just like they could before. I've simplified the example code in the documentation to now use the new helper class, and I've also made use of this helper within the testsuite. Any existing user code will continue to work just as it did before after this change. Reviewed-By: Eli Zaretskii --- gdb/NEWS | 5 +++ gdb/doc/python.texi | 49 ++++++++++++++++++++++----- gdb/python/lib/gdb/unwinder.py | 26 ++++++++++++++ gdb/testsuite/gdb.python/py-unwind.py | 16 +-------- 4 files changed, 73 insertions(+), 23 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index c4f7de11c6e..c5aa256c1bd 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -134,6 +134,11 @@ show always-read-ctf now use either an integer or a gdb.Value object for each of its 'sp', 'pc', and 'special' attributes. + ** A new class gdb.unwinder.FrameId has been added. Instrances of + this class are constructed with 'sp' (stack-pointer) and 'pc' + (program-counter) values, and can be used as the frame-id when + calling gdb.PendingFrame.create_unwind_info. + *** Changes in GDB 13 * MI version 1 is deprecated, and will be removed in GDB 14. diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 3074b5250a3..bbcaf3fd166 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -2782,6 +2782,7 @@ It also provides a factory method to create a @code{gdb.UnwindInfo} instance to be returned to @value{GDBN}: +@anchor{gdb.PendingFrame.create_unwind_info} @defun PendingFrame.create_unwind_info (frame_id) Returns a new @code{gdb.UnwindInfo} instance identified by given @var{frame_id}. The @var{frame_id} is used internally by @value{GDBN} @@ -2818,6 +2819,10 @@ Each attribute value should either be an instance of @code{gdb.Value} or an integer. +A helper class is provided in the @code{gdb.unwinder} module that can +be used to represent a frame-id +(@pxref{gdb.unwinder.FrameId,,@code{gdb.unwinder.FrameId}}). + @end defun @defun PendingFrame.architecture () @@ -2886,7 +2891,7 @@ @subheading The @code{gdb.unwinder} Module @value{GDBN} comes with a @code{gdb.unwinder} module which contains -the following class: +the following classes: @deftp {class} gdb.unwinder.Unwinder The @code{Unwinder} class is a base class from which user created @@ -2910,6 +2915,40 @@ @end defvar @end deftp +@anchor{gdb.unwinder.FrameId} +@deftp {class} gdb.unwinder.FrameId +This is a class suitable for being used as the frame-id when calling +@code{gdb.PendingFrame.create_unwind_info}. It is not required to use +this class, any class with the required attribute +(@pxref{gdb.PendingFrame.create_unwind_info,,@code{gdb.PendingFrame.create_unwind_info}}) +will be accepted, but in most cases this class will be sufficient. + +@code{gdb.unwinder.FrameId} has the following method: + +@defun gdb.unwinder.FrameId.__init__(@var{sp}, @var{pc}, @var{special} = @code{None}) +The @var{sp} and @var{pc} arguments are required and should be either +a @code{gdb.Value} object, or an integer. + +The @var{special} argument is optional, if specified it should be a +@code{gdb.Value} object, or an integer. +@end defun + +@code{gdb.unwinder.FrameId} has the following read-only attributes: + +@defvar gdb.unwinder.sp +The @var{sp} value passed to the constructor. +@end defvar + +@defvar gdb.unwinder.pc +The @var{pc} value passed to the constructor. +@end defvar + +@defvar gdb.unwinder.special +The @var{special} value passed to the constructor, or @code{None} if +no such value was passed. +@end defvar +@end deftp + @subheading Registering an Unwinder Object files and program spaces can have unwinders registered with @@ -2943,13 +2982,7 @@ Here is an example of how and structure a user created unwinder: @smallexample -from gdb.unwinder import Unwinder - -class FrameId(object): - def __init__(self, sp, pc): - self.sp = sp - self.pc = pc - +from gdb.unwinder import Unwinder, FrameId class MyUnwinder(Unwinder): def __init__(self): diff --git a/gdb/python/lib/gdb/unwinder.py b/gdb/python/lib/gdb/unwinder.py index 1303245c054..140b84d3374 100644 --- a/gdb/python/lib/gdb/unwinder.py +++ b/gdb/python/lib/gdb/unwinder.py @@ -69,6 +69,32 @@ class Unwinder(object): raise NotImplementedError("Unwinder __call__.") +class FrameId(object): + """A Frame-ID class for use when creating gdb.UnwindInfo objects. + + Attributes (all read-only): + pc: Program counter value. + sp: The stack-pointer value. + special: An alternative stack-pointer value, can be None.""" + + def __init__(self, sp, pc, special=None): + self._sp = sp + self._pc = pc + self._special = special + + @property + def sp(self): + return self._sp + + @property + def pc(self): + return self._pc + + @property + def special(self): + return self._special + + def register_unwinder(locus, unwinder, replace=False): """Register unwinder in given locus. diff --git a/gdb/testsuite/gdb.python/py-unwind.py b/gdb/testsuite/gdb.python/py-unwind.py index dbabb006e4b..4e110c51e3b 100644 --- a/gdb/testsuite/gdb.python/py-unwind.py +++ b/gdb/testsuite/gdb.python/py-unwind.py @@ -14,7 +14,7 @@ # along with this program. If not, see . import gdb -from gdb.unwinder import Unwinder +from gdb.unwinder import Unwinder, FrameId # These are set to test whether invalid register names cause an error. @@ -22,20 +22,6 @@ add_saved_register_error = False read_register_error = False -class FrameId(object): - def __init__(self, sp, pc): - self._sp = sp - self._pc = pc - - @property - def sp(self): - return self._sp - - @property - def pc(self): - return self._pc - - class TestUnwinder(Unwinder): AMD64_RBP = 6 AMD64_RSP = 7