Don't use gdb_py_long_from_ulongest

Remove the gdb_py_long_from_ulongest defines and change the Python
layer to prefer gdb_py_object_from_ulongest.  While writing this I
noticed that the error handling in archpy_disassemble was incorrect --
it could call PyDict_SetItemString with a NULL value.  This patch also
fixes this bug.

gdb/ChangeLog
2020-09-15  Tom Tromey  <tromey@adacore.com>

	* python/python-internal.h (gdb_py_long_from_ulongest): Remove
	defines.
	* python/py-value.c (valpy_long): Use
	gdb_py_object_from_ulongest.
	* python/py-symtab.c (salpy_get_pc): Use
	gdb_py_object_from_ulongest.
	(salpy_get_last): Likewise.
	* python/py-record-btrace.c (recpy_bt_insn_pc): Use
	gdb_py_object_from_ulongest.
	* python/py-lazy-string.c (stpy_get_address): Use
	gdb_py_object_from_ulongest.
	* python/py-frame.c (frapy_pc): Use gdb_py_object_from_ulongest.
	* python/py-arch.c (archpy_disassemble): Use
	gdb_py_object_from_ulongest and gdb_py_object_from_longest.  Fix
	error handling.
This commit is contained in:
Tom Tromey 2020-09-15 11:08:56 -06:00
parent 4bde49dc81
commit d1cab9876d
8 changed files with 41 additions and 16 deletions

View file

@ -1,3 +1,21 @@
2020-09-15 Tom Tromey <tromey@adacore.com>
* python/python-internal.h (gdb_py_long_from_ulongest): Remove
defines.
* python/py-value.c (valpy_long): Use
gdb_py_object_from_ulongest.
* python/py-symtab.c (salpy_get_pc): Use
gdb_py_object_from_ulongest.
(salpy_get_last): Likewise.
* python/py-record-btrace.c (recpy_bt_insn_pc): Use
gdb_py_object_from_ulongest.
* python/py-lazy-string.c (stpy_get_address): Use
gdb_py_object_from_ulongest.
* python/py-frame.c (frapy_pc): Use gdb_py_object_from_ulongest.
* python/py-arch.c (archpy_disassemble): Use
gdb_py_object_from_ulongest and gdb_py_object_from_longest. Fix
error handling.
2020-09-15 Tom Tromey <tromey@adacore.com> 2020-09-15 Tom Tromey <tromey@adacore.com>
* python/python-internal.h (gdb_py_long_from_longest): Remove * python/python-internal.h (gdb_py_long_from_longest): Remove

View file

@ -209,14 +209,23 @@ archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
return NULL; return NULL;
} }
if (PyDict_SetItemString (insn_dict.get (), "addr", gdbpy_ref<> pc_obj = gdb_py_object_from_ulongest (pc);
gdb_py_long_from_ulongest (pc)) if (pc_obj == nullptr)
|| PyDict_SetItemString (insn_dict.get (), "asm", return nullptr;
PyString_FromString (!stb.empty ()
? stb.c_str () gdbpy_ref<> asm_obj (PyString_FromString (!stb.empty ()
: "<unknown>")) ? stb.c_str ()
|| PyDict_SetItemString (insn_dict.get (), "length", : "<unknown>"));
PyInt_FromLong (insn_len))) if (asm_obj == nullptr)
return nullptr;
gdbpy_ref<> len_obj = gdb_py_object_from_longest (insn_len);
if (len_obj == nullptr)
return nullptr;
if (PyDict_SetItemString (insn_dict.get (), "addr", pc_obj.get ())
|| PyDict_SetItemString (insn_dict.get (), "asm", asm_obj.get ())
|| PyDict_SetItemString (insn_dict.get (), "length", len_obj.get ()))
return NULL; return NULL;
pc += insn_len; pc += insn_len;

View file

@ -232,7 +232,7 @@ frapy_pc (PyObject *self, PyObject *args)
GDB_PY_HANDLE_EXCEPTION (except); GDB_PY_HANDLE_EXCEPTION (except);
} }
return gdb_py_long_from_ulongest (pc); return gdb_py_object_from_ulongest (pc).release ();
} }
/* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value. /* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.

View file

@ -61,7 +61,7 @@ stpy_get_address (PyObject *self, void *closure)
{ {
lazy_string_object *self_string = (lazy_string_object *) self; lazy_string_object *self_string = (lazy_string_object *) self;
return gdb_py_long_from_ulongest (self_string->address); return gdb_py_object_from_ulongest (self_string->address).release ();
} }
static PyObject * static PyObject *

View file

@ -232,7 +232,7 @@ recpy_bt_insn_pc (PyObject *self, void *closure)
if (insn == NULL) if (insn == NULL)
return NULL; return NULL;
return gdb_py_long_from_ulongest (insn->pc); return gdb_py_object_from_ulongest (insn->pc).release ();
} }
/* Implementation of RecordInstruction.size [int] for btrace. /* Implementation of RecordInstruction.size [int] for btrace.

View file

@ -264,7 +264,7 @@ salpy_get_pc (PyObject *self, void *closure)
SALPY_REQUIRE_VALID (self, sal); SALPY_REQUIRE_VALID (self, sal);
return gdb_py_long_from_ulongest (sal->pc); return gdb_py_object_from_ulongest (sal->pc).release ();
} }
/* Implementation of the get method for the 'last' attribute of /* Implementation of the get method for the 'last' attribute of
@ -278,7 +278,7 @@ salpy_get_last (PyObject *self, void *closure)
SALPY_REQUIRE_VALID (self, sal); SALPY_REQUIRE_VALID (self, sal);
if (sal->end > 0) if (sal->end > 0)
return gdb_py_long_from_ulongest (sal->end - 1); return gdb_py_object_from_ulongest (sal->end - 1).release ();
else else
Py_RETURN_NONE; Py_RETURN_NONE;
} }

View file

@ -1731,7 +1731,7 @@ valpy_long (PyObject *self)
} }
if (type->is_unsigned ()) if (type->is_unsigned ())
return gdb_py_long_from_ulongest (l); return gdb_py_object_from_ulongest (l).release ();
else else
return gdb_py_object_from_longest (l).release (); return gdb_py_object_from_longest (l).release ();
} }

View file

@ -126,7 +126,6 @@
#define GDB_PY_LLU_ARG "K" #define GDB_PY_LLU_ARG "K"
typedef PY_LONG_LONG gdb_py_longest; typedef PY_LONG_LONG gdb_py_longest;
typedef unsigned PY_LONG_LONG gdb_py_ulongest; typedef unsigned PY_LONG_LONG gdb_py_ulongest;
#define gdb_py_long_from_ulongest PyLong_FromUnsignedLongLong
#define gdb_py_long_as_ulongest PyLong_AsUnsignedLongLong #define gdb_py_long_as_ulongest PyLong_AsUnsignedLongLong
#else /* HAVE_LONG_LONG */ #else /* HAVE_LONG_LONG */
@ -135,7 +134,6 @@ typedef unsigned PY_LONG_LONG gdb_py_ulongest;
#define GDB_PY_LLU_ARG "K" #define GDB_PY_LLU_ARG "K"
typedef long gdb_py_longest; typedef long gdb_py_longest;
typedef unsigned long gdb_py_ulongest; typedef unsigned long gdb_py_ulongest;
#define gdb_py_long_from_ulongest PyLong_FromUnsignedLong
#define gdb_py_long_as_ulongest PyLong_AsUnsignedLong #define gdb_py_long_as_ulongest PyLong_AsUnsignedLong
#endif /* HAVE_LONG_LONG */ #endif /* HAVE_LONG_LONG */