From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>
Subject: [RFC 3/9] gdb/python: do not hold on gdb.Symtab object from gdb.Symtab_and_line
Date: Mon, 27 Jan 2025 10:44:29 +0000 [thread overview]
Message-ID: <20250127104435.823519-4-jan.vrany@labware.com> (raw)
In-Reply-To: <20250127104435.823519-1-jan.vrany@labware.com>
Previous commit changed symtab_to_symtab_object() so each time it is
called with particula struct symtab* it returns the same object.
Therefore there's no longer need to hold on symtab object (gdb.Symtab)
from struct sal_object in order to preserve identity of Symtab object
held in gdb.Symtab_and_line.symtab property. This in turn allowed for
some simplification in various functions.
While at it I changed a couple of NULLs to nullptrs.
---
gdb/python/py-symbol.c | 2 +-
gdb/python/py-symtab.c | 73 ++++++++++++------------------------------
2 files changed, 22 insertions(+), 53 deletions(-)
diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c
index 2abac6553a0..c157bd7f5d7 100644
--- a/gdb/python/py-symbol.c
+++ b/gdb/python/py-symbol.c
@@ -368,7 +368,7 @@ symbol_to_symbol_object (struct symbol *sym)
{
symbol_object *sym_obj;
- /* Look if there's already a gdb.Symtab object for given SYMTAB
+ /* Look if there's already a gdb.Symbol object for given SYMBOL
and if so, return it. */
if (sym->is_objfile_owned ())
sym_obj = sympy_objfile_data_key.get (sym->objfile ());
diff --git a/gdb/python/py-symtab.c b/gdb/python/py-symtab.c
index 7b51e211daf..177028ecf01 100644
--- a/gdb/python/py-symtab.c
+++ b/gdb/python/py-symtab.c
@@ -77,8 +77,6 @@ static const registry<objfile>::key<symtab_object, stpy_deleter>
struct sal_object {
PyObject_HEAD
- /* The GDB Symbol table structure. */
- PyObject *symtab;
/* The GDB Symbol table and line structure. */
struct symtab_and_line *sal;
/* A Symtab and line object is associated with an objfile, so keep
@@ -104,14 +102,10 @@ struct salpy_deleter
{
sal_object *next = obj->next;
- gdbpy_ref<> tmp (obj->symtab);
- obj->symtab = Py_None;
- Py_INCREF (Py_None);
-
- obj->next = NULL;
- obj->prev = NULL;
+ obj->next = nullptr;
+ obj->prev = nullptr;
xfree (obj->sal);
- obj->sal = NULL;
+ obj->sal = nullptr;
obj = next;
}
@@ -272,18 +266,15 @@ salpy_str (PyObject *self)
{
const char *filename;
sal_object *sal_obj;
- struct symtab_and_line *sal = NULL;
+ struct symtab_and_line *sal = nullptr;
SALPY_REQUIRE_VALID (self, sal);
sal_obj = (sal_object *) self;
- if (sal_obj->symtab == Py_None)
+ if (sal_obj->sal->symtab == nullptr)
filename = "<unknown>";
else
- {
- symtab *symtab = symtab_object_to_symtab (sal_obj->symtab);
- filename = symtab_to_filename_for_display (symtab);
- }
+ filename = symtab_to_filename_for_display (sal_obj->sal->symtab);
return PyUnicode_FromFormat ("symbol and line for %s, line %d", filename,
sal->line);
@@ -346,13 +337,13 @@ static PyObject *
salpy_get_symtab (PyObject *self, void *closure)
{
struct symtab_and_line *sal;
- sal_object *self_sal = (sal_object *) self;
SALPY_REQUIRE_VALID (self, sal);
- Py_INCREF (self_sal->symtab);
-
- return (PyObject *) self_sal->symtab;
+ if (sal->symtab == nullptr)
+ Py_RETURN_NONE;
+ else
+ return symtab_to_symtab_object (sal->symtab);
}
/* Implementation of gdb.Symtab_and_line.is_valid (self) -> Boolean.
@@ -377,15 +368,14 @@ salpy_dealloc (PyObject *self)
if (self_sal->prev)
self_sal->prev->next = self_sal->next;
- else if (self_sal->symtab != Py_None)
+ else if (self_sal->sal != nullptr && self_sal->sal->symtab != nullptr)
salpy_objfile_data_key.set
- (symtab_object_to_symtab (self_sal->symtab)->compunit ()->objfile (),
+ (self_sal->sal->symtab->compunit ()->objfile (),
self_sal->next);
if (self_sal->next)
self_sal->next->prev = self_sal->prev;
- Py_DECREF (self_sal->symtab);
xfree (self_sal->sal);
Py_TYPE (self)->tp_free (self);
}
@@ -395,37 +385,19 @@ salpy_dealloc (PyObject *self)
Also, register the sal_object life-cycle with the life-cycle of the
object file associated with this sal, if needed. If a failure
occurs during the sal population, this function will return -1. */
-static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
+static void
set_sal (sal_object *sal_obj, struct symtab_and_line sal)
{
- PyObject *symtab_obj;
-
- if (sal.symtab)
- {
- symtab_obj = symtab_to_symtab_object (sal.symtab);
- /* If a symtab existed in the sal, but it cannot be duplicated,
- we exit. */
- if (symtab_obj == NULL)
- return -1;
- }
- else
- {
- symtab_obj = Py_None;
- Py_INCREF (Py_None);
- }
-
sal_obj->sal = ((struct symtab_and_line *)
xmemdup (&sal, sizeof (struct symtab_and_line),
sizeof (struct symtab_and_line)));
- sal_obj->symtab = symtab_obj;
sal_obj->prev = NULL;
/* If the SAL does not have a symtab, we do not add it to the
objfile cleanup observer linked list. */
- if (sal_obj->symtab != Py_None)
+ symtab *symtab = sal_obj->sal->symtab;
+ if (symtab != nullptr)
{
- symtab *symtab = symtab_object_to_symtab (sal_obj->symtab);
-
sal_obj->next
= salpy_objfile_data_key.get (symtab->compunit ()->objfile ());
if (sal_obj->next)
@@ -435,8 +407,6 @@ set_sal (sal_object *sal_obj, struct symtab_and_line sal)
}
else
sal_obj->next = NULL;
-
- return 0;
}
/* Given a symtab, and a symtab_object that has previously been
@@ -495,14 +465,13 @@ symtab_to_symtab_object (struct symtab *symtab)
PyObject *
symtab_and_line_to_sal_object (struct symtab_and_line sal)
{
- gdbpy_ref<sal_object> sal_obj (PyObject_New (sal_object, &sal_object_type));
- if (sal_obj != NULL)
- {
- if (set_sal (sal_obj.get (), sal) < 0)
- return NULL;
- }
+ sal_object *sal_obj;
+
+ sal_obj = PyObject_New (sal_object, &sal_object_type);
+ if (sal_obj != nullptr)
+ set_sal (sal_obj, sal);
- return (PyObject *) sal_obj.release ();
+ return (PyObject *) sal_obj;
}
/* Return struct symtab_and_line reference that is wrapped by this
--
2.45.2
next prev parent reply other threads:[~2025-01-27 10:47 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-27 10:44 [RFC 0/9] Attempt to unify Python object's lifecycle Jan Vrany
2025-01-27 10:44 ` [RFC 1/9] gdb/python: preserve identity for gdb.Symtab objects Jan Vrany
2025-01-27 10:44 ` [RFC 2/9] gdb/python: preserve identity for gdb.Symbol objects Jan Vrany
2025-02-20 19:22 ` Tom Tromey
2025-01-27 10:44 ` Jan Vrany [this message]
2025-01-27 10:44 ` [RFC 4/9] gdb/python: preserve identity for gdb.Type objects Jan Vrany
2025-01-27 10:44 ` [RFC 5/9] gdb/python: introduce gdbpy_registry Jan Vrany
2025-02-20 19:28 ` Tom Tromey
2025-01-27 10:44 ` [RFC 6/9] gdb/python: convert gdb.Symbol to use gdbpy_registry Jan Vrany
2025-01-27 10:44 ` [RFC 7/9] gdb/python: convert gdb.Type " Jan Vrany
2025-01-27 10:44 ` [RFC 8/9] gdb/python: convert gdb.Symtab " Jan Vrany
2025-01-27 10:44 ` [RFC 9/9] gdb/python: convert gdb.Symtab_and_line " Jan Vrany
2025-02-18 11:15 ` [PING] Re: [RFC 0/9] Attempt to unify Python object's lifecycle Jan Vraný
2025-02-19 21:00 ` Simon Marchi
2025-02-20 17:50 ` Jan Vraný
2025-02-20 19:18 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250127104435.823519-4-jan.vrany@labware.com \
--to=jan.vrany@labware.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox