From patchwork Mon Feb 6 17:01:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Malcomson X-Patchwork-Id: 19121 Received: (qmail 100738 invoked by alias); 6 Feb 2017 17:02:01 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 100704 invoked by uid 89); 6 Feb 2017 17:02:00 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=disassembly, Architecture, UD:release, mentions X-HELO: mail-wj0-f194.google.com Received: from mail-wj0-f194.google.com (HELO mail-wj0-f194.google.com) (209.85.210.194) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 06 Feb 2017 17:01:50 +0000 Received: by mail-wj0-f194.google.com with SMTP id kq3so3806632wjc.3 for ; Mon, 06 Feb 2017 09:01:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding; bh=tG58/qJVjZcC3MRUyMxDRLitWEedoUPX3DncWS0kOhA=; b=IMWbI6mrgXKfHih3MBNYYQcflVrcwnpUsGohUwG40s8D44TAMp9r34WWS2OCVCYFv9 6F4bzHqSfaAHDkI3JWvrnNAMLIfIVkm7maHvQ6V5ag7FdlBgy2CPuEiBa4YWuqJbApG9 P+8aBcXiB3HA2kdSkB0DlVlfwRKlwVTH4k7nO6U04LnhNJDShau+OAHsNjNsQ+ZAoDyB vl0haWYMKY/46phPNqEW7Z75ZfgKjXSliHCL1UpnUAXuBNhDqVrCSLi+V0IUEbeN8UyJ kyEvv6RuyrNYGiSr2uDUddPRjO3uAFiFXQKcRU4Bwo9vtL2ne61Wg8EgBK7DxM6I0hYR VxIg== X-Gm-Message-State: AMke39l/2xtbHFXyPrPKWUKrZ/ulsv7wJO+Bwk4k0za9dfMcZoF/6Wlp5HfIlMbI098Viw== X-Received: by 10.28.7.7 with SMTP id 7mr10369778wmh.55.1486400507865; Mon, 06 Feb 2017 09:01:47 -0800 (PST) Received: from [10.0.0.57] (host86-142-187-79.range86-142.btcentralplus.com. [86.142.187.79]) by smtp.gmail.com with ESMTPSA id i73sm13962019wmd.11.2017.02.06.09.01.46 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Feb 2017 09:01:47 -0800 (PST) To: gdb-patches@sourceware.org From: Matthew Malcomson Subject: [PATCH] Add gdb.current_arch() function to python module -- equivalent of guile (current-arch) Message-ID: Date: Mon, 6 Feb 2017 17:01:46 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 Hello there, I noticed that while there is a guile function to get the current architecture `(current-arch)`, there isn't a python equivalent. I've been finding that awkward in some extensions I've been writing, mainly when I want to work with the disassembly of a function before my target program has started. This patch adds a python equivalent: `gdb.current_arch()` that returns an architecture object representing the current architecture (taken from the `get_current_arch ()` function). NOTES: To get gdb to build on my machine, I've applied the patch from bug 21057 so it can work with newer versions of flex and bison. This is the reason the sha hashes of the patch below don't match any in the main tree. I believe this change is small enough to not be legally significant (under 15 lines ignoring comments & documentation). I'm currently requesting a copyright assignment form for future changes so if it isn't small enough I should be able to provide documentation soon. --------------------- CHANGELOG: The gdb/CONTRIBUTE file mentions I should include a ChangeLog entry. I may have gotten the wrong format here -- If so I apologise. 2017-02-06 Matthew Malcomson (tiny change) * python/python.c (python_GdbMethods): Update. python/python-internal.h (gdbpy_current_arch): Update. python/py-arch.c (gdbpy_current_arch): New. doc/python.texi (gdb.current_arch): Document. ------------------------------------ PATCH: commit 59048e5c4241b2641815456741eee46be1de035c Author: Matthew Malcomson Date: Sun Feb 5 15:40:05 2017 +0000 Add gdb.current_arch() python function This provides the equivalent of the guile (current-arch) function. Return the newest frame object." }, diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index fae45aa5d9..5f15746f47 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4898,6 +4898,14 @@ writable. number of its various computations. An architecture is represented by an instance of the @code{gdb.Architecture} class. +The following architecture-related functions are available in the @code{gdb} +module: + +@findex gdb.current_arch +@defun gdb.current_arch () +Return the current architecture object. +@end defun + A @code{gdb.Architecture} class has the following methods: @defun Architecture.name () diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index 8d0ec3330c..0f86ef80fe 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -230,6 +230,14 @@ archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw) return result_list.release (); } +/* Implementation of gdb.current_architecture () -> gdb.Architecture. + Returns the current architecture object. */ +PyObject * +gdbpy_current_arch (PyObject *self, PyObject *args) +{ + return gdbarch_to_arch_object (get_current_arch ()); +} + /* Initializes the Architecture class in the gdb module. */ int diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index e2ebc1b8a2..3b9ac65c12 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -411,6 +411,7 @@ PyObject *objfpy_get_frame_unwinders (PyObject *, void *); PyObject *objfpy_get_xmethods (PyObject *, void *); PyObject *gdbpy_lookup_objfile (PyObject *self, PyObject *args, PyObject *kw); +PyObject *gdbpy_current_arch (PyObject *self, PyObject *args); PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch); thread_object *create_thread_object (struct thread_info *tp); diff --git a/gdb/python/python.c b/gdb/python/python.c index ab5a6a416d..db3461d9c0 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1893,6 +1893,13 @@ set to True." }, { "objfiles", gdbpy_objfiles, METH_NOARGS, "Return a sequence of all loaded objfiles." }, + { "current_arch", gdbpy_current_arch, METH_NOARGS, + "current_arch () -> gdb.Architecture.\n\ +Return the gdb.Architecture object representing the architecture of the\n\ +currently selected stack frame, if there is one, or the architecture of the\n\ +current target if there isn't.\n\ +" }, + { "newest_frame", gdbpy_newest_frame, METH_NOARGS, "newest_frame () -> gdb.Frame.\n\