Accept gdb.Value in more Python APIs

PR python/27000 points out that gdb.block_for_pc will accept a Python
integer, but not a gdb.Value.  This patch corrects this oversight.

I looked at all uses of GDB_PY_LLU_ARG and fixed these up to use
get_addr_from_python instead.  I also looked at uses of GDB_PY_LL_ARG,
but those seemed relatively unlikely to be useful with a gdb.Value, so
I didn't change them.  My thinking here is that a Value will typically
come from inferior memory, and something like a line number is not too
likely to be found this way.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27000
This commit is contained in:
Tom Tromey 2022-06-05 07:42:12 -06:00
parent fa17a68141
commit d19ca0b35c
6 changed files with 37 additions and 33 deletions

View file

@ -122,39 +122,26 @@ static PyObject *
archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw) archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw)
{ {
static const char *keywords[] = { "start_pc", "end_pc", "count", NULL }; static const char *keywords[] = { "start_pc", "end_pc", "count", NULL };
CORE_ADDR start, end = 0; CORE_ADDR start = 0, end = 0;
CORE_ADDR pc; CORE_ADDR pc;
gdb_py_ulongest start_temp;
long count = 0, i; long count = 0, i;
PyObject *end_obj = NULL, *count_obj = NULL; PyObject *start_obj = nullptr, *end_obj = nullptr, *count_obj = nullptr;
struct gdbarch *gdbarch = NULL; struct gdbarch *gdbarch = NULL;
ARCHPY_REQUIRE_VALID (self, gdbarch); ARCHPY_REQUIRE_VALID (self, gdbarch);
if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O|OO",
keywords, &start_temp, &end_obj, keywords, &start_obj, &end_obj,
&count_obj)) &count_obj))
return NULL; return NULL;
start = start_temp; if (get_addr_from_python (start_obj, &start) < 0)
if (end_obj) return nullptr;
{
/* Make a long logic check first. In Python 3.x, internally,
all integers are represented as longs. In Python 2.x, there
is still a differentiation internally between a PyInt and a
PyLong. Explicitly do this long check conversion first. In
GDB, for Python 3.x, we #ifdef PyInt = PyLong. This check has
to be done first to ensure we do not lose information in the
conversion process. */
if (PyLong_Check (end_obj))
end = PyLong_AsUnsignedLongLong (end_obj);
else
{
PyErr_SetString (PyExc_TypeError,
_("Argument 'end_pc' should be a (long) integer."));
return NULL; if (end_obj != nullptr)
} {
if (get_addr_from_python (end_obj, &end) < 0)
return nullptr;
if (end < start) if (end < start)
{ {

View file

@ -351,13 +351,17 @@ pspy_get_objfiles (PyObject *self_, PyObject *args)
static PyObject * static PyObject *
pspy_solib_name (PyObject *o, PyObject *args) pspy_solib_name (PyObject *o, PyObject *args)
{ {
gdb_py_ulongest pc; CORE_ADDR pc;
PyObject *pc_obj;
pspace_object *self = (pspace_object *) o; pspace_object *self = (pspace_object *) o;
PSPY_REQUIRE_VALID (self); PSPY_REQUIRE_VALID (self);
if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) if (!PyArg_ParseTuple (args, "O", &pc_obj))
return NULL; return NULL;
if (get_addr_from_python (pc_obj, &pc) < 0)
return nullptr;
const char *soname = solib_name_from_address (self->pspace, pc); const char *soname = solib_name_from_address (self->pspace, pc);
if (soname == nullptr) if (soname == nullptr)
@ -371,14 +375,17 @@ static PyObject *
pspy_block_for_pc (PyObject *o, PyObject *args) pspy_block_for_pc (PyObject *o, PyObject *args)
{ {
pspace_object *self = (pspace_object *) o; pspace_object *self = (pspace_object *) o;
gdb_py_ulongest pc; CORE_ADDR pc;
PyObject *pc_obj;
const struct block *block = NULL; const struct block *block = NULL;
struct compunit_symtab *cust = NULL; struct compunit_symtab *cust = NULL;
PSPY_REQUIRE_VALID (self); PSPY_REQUIRE_VALID (self);
if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) if (!PyArg_ParseTuple (args, "O", &pc_obj))
return NULL; return NULL;
if (get_addr_from_python (pc_obj, &pc) < 0)
return nullptr;
try try
{ {
@ -410,24 +417,25 @@ pspy_block_for_pc (PyObject *o, PyObject *args)
static PyObject * static PyObject *
pspy_find_pc_line (PyObject *o, PyObject *args) pspy_find_pc_line (PyObject *o, PyObject *args)
{ {
gdb_py_ulongest pc_llu; CORE_ADDR pc;
PyObject *result = NULL; /* init for gcc -Wall */ PyObject *result = NULL; /* init for gcc -Wall */
PyObject *pc_obj;
pspace_object *self = (pspace_object *) o; pspace_object *self = (pspace_object *) o;
PSPY_REQUIRE_VALID (self); PSPY_REQUIRE_VALID (self);
if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu)) if (!PyArg_ParseTuple (args, "O", &pc_obj))
return NULL; return NULL;
if (get_addr_from_python (pc_obj, &pc) < 0)
return nullptr;
try try
{ {
struct symtab_and_line sal; struct symtab_and_line sal;
CORE_ADDR pc;
scoped_restore_current_program_space saver; scoped_restore_current_program_space saver;
set_current_program_space (self->pspace); set_current_program_space (self->pspace);
pc = (CORE_ADDR) pc_llu;
sal = find_pc_line (pc, 0); sal = find_pc_line (pc, 0);
result = symtab_and_line_to_sal_object (sal); result = symtab_and_line_to_sal_object (sal);
} }

View file

@ -43,7 +43,7 @@ gdb_py_test_silent_cmd "python insn_list2 = arch.disassemble(pc, pc)" \
"disassemble no count" 0 "disassemble no count" 0
gdb_py_test_silent_cmd "python insn_list3 = arch.disassemble(pc, count=1)" \ gdb_py_test_silent_cmd "python insn_list3 = arch.disassemble(pc, count=1)" \
"disassemble no end" 0 "disassemble no end" 0
gdb_py_test_silent_cmd "python insn_list4 = arch.disassemble(pc)" \ gdb_py_test_silent_cmd "python insn_list4 = arch.disassemble(gdb.Value(pc))" \
"disassemble no end no count" 0 "disassemble no end no count" 0
gdb_test "python print (len(insn_list1))" "1" "test number of instructions 1" gdb_test "python print (len(insn_list1))" "1" "test number of instructions 1"

View file

@ -60,6 +60,9 @@ if {![runto_main]} {
set pc_val [get_integer_valueof "\$pc" 0] set pc_val [get_integer_valueof "\$pc" 0]
gdb_py_test_silent_cmd "python blk = gdb.current_progspace ().block_for_pc (${pc_val})" \ gdb_py_test_silent_cmd "python blk = gdb.current_progspace ().block_for_pc (${pc_val})" \
"get block for the current \$pc" 1 "get block for the current \$pc" 1
gdb_py_test_silent_cmd \
"python blk = gdb.current_progspace ().block_for_pc (gdb.Value(${pc_val}))" \
"get block for the current \$pc as value" 1
gdb_test "python print (blk.start <= ${pc_val})" "True" \ gdb_test "python print (blk.start <= ${pc_val})" "True" \
"block start is before \$pc" "block start is before \$pc"
gdb_test "python print (blk.end >= ${pc_val})" "True" \ gdb_test "python print (blk.end >= ${pc_val})" "True" \

View file

@ -57,7 +57,10 @@ runto [gdb_get_line_number "Break to end."]
# Test gdb.solib_name # Test gdb.solib_name
gdb_test "p &func1" "" "func1 address" gdb_test "p &func1" "" "func1 address"
gdb_py_test_silent_cmd "python func1 = gdb.history(0)" "Aquire func1 address" 1 gdb_py_test_silent_cmd "python func1 = gdb.history(0)" "Aquire func1 address" 1
gdb_test "python print (gdb.solib_name(int(func1)))" "py-shared-sl.sl" "test func1 solib location" gdb_test "python print (gdb.solib_name(int(func1)))" "py-shared-sl.sl" \
"test func1 solib location"
gdb_test "python print (gdb.solib_name(func1))" "py-shared-sl.sl" \
"test func1 solib location using Value"
gdb_test "p &main" "" "main address" gdb_test "p &main" "" "main address"
gdb_py_test_silent_cmd "python main = gdb.history(0)" "Aquire main address" 1 gdb_py_test_silent_cmd "python main = gdb.history(0)" "Aquire main address" 1

View file

@ -514,6 +514,9 @@ gdb_test "python print (pc_rtn > pc_call)" "True" \
gdb_test "python print (gdb.find_pc_line(pc_rtn).line >= line)" "True" \ gdb_test "python print (gdb.find_pc_line(pc_rtn).line >= line)" "True" \
"test find_pc_line with resume address" "test find_pc_line with resume address"
gdb_test "python print (gdb.find_pc_line(pc_rtn).line == gdb.find_pc_line(gdb.Value(pc_rtn)).line)" \
"True" \
"test find_pc_line using Value"
gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable" gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable"
gdb_test "python print(gdb.convenience_variable('cvar1'))" "23" gdb_test "python print(gdb.convenience_variable('cvar1'))" "23"