From 4aea001fd84a05f1e552c5dea1025e3f62dd2d7e Mon Sep 17 00:00:00 2001 From: Hannes Domani Date: Fri, 18 Dec 2020 17:23:06 +0100 Subject: [PATCH] Add address keyword to Value.format_string This makes it possible to disable the address in the result string: const char *str = "alpha"; (gdb) py print(gdb.parse_and_eval("str").format_string()) 0x404000 "alpha" (gdb) py print(gdb.parse_and_eval("str").format_string(address=False)) "alpha" gdb/ChangeLog: 2020-12-18 Hannes Domani * python/py-value.c (valpy_format_string): Implement address keyword. gdb/doc/ChangeLog: 2020-12-18 Hannes Domani * python.texi (Values From Inferior): Document the address keyword. gdb/testsuite/ChangeLog: 2020-12-18 Hannes Domani * gdb.python/py-format-string.exp: Add tests for address keyword. --- gdb/ChangeLog | 4 ++ gdb/doc/ChangeLog | 4 ++ gdb/doc/python.texi | 5 +++ gdb/python/py-value.c | 7 ++- gdb/testsuite/ChangeLog | 4 ++ gdb/testsuite/gdb.python/py-format-string.exp | 43 +++++++++++++++++++ 6 files changed, 66 insertions(+), 1 deletion(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 1a104b1f339..7891f491906 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,7 @@ +2020-12-18 Hannes Domani + + * python/py-value.c (valpy_format_string): Implement address keyword. + 2020-12-18 Hannes Domani * python/py-type.c (typy_get_composite): Add TYPE_CODE_METHOD. diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index 223c8fcfe40..f5ab5432b98 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,7 @@ +2020-12-18 Hannes Domani + + * python.texi (Values From Inferior): Document the address keyword. + 2020-12-17 Simon Marchi PR gdb/27088 diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index c107d4506d6..ad7df2cdd32 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -906,6 +906,11 @@ corresponding symbol name (if one exists), @code{False} if it shouldn't should be expanded, @code{False} if they shouldn't (see @code{set print union} in @ref{Print Settings}). +@item address +@code{True} if the string representation of a pointer should include the +address, @code{False} if it shouldn't (see @code{set print address} in +@ref{Print Settings}). + @item deref_refs @code{True} if C@t{++} references should be resolved to the value they refer to, @code{False} (the default) if they shouldn't. Note that, unlike diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index c1ff8e8eca9..a6afcad4f2b 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -617,6 +617,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw) "array_indexes", /* See set print array-indexes on|off. */ "symbols", /* See set print symbol on|off. */ "unions", /* See set print union on|off. */ + "address", /* See set print address on|off. */ /* C++ options. */ "deref_refs", /* No corresponding setting. */ "actual_objects", /* See set print object on|off. */ @@ -660,13 +661,14 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw) PyObject *array_indexes_obj = NULL; PyObject *symbols_obj = NULL; PyObject *unions_obj = NULL; + PyObject *address_obj = NULL; PyObject *deref_refs_obj = NULL; PyObject *actual_objects_obj = NULL; PyObject *static_members_obj = NULL; char *format = NULL; if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, - "|O!O!O!O!O!O!O!O!O!IIIs", + "|O!O!O!O!O!O!O!O!O!O!IIIs", keywords, &PyBool_Type, &raw_obj, &PyBool_Type, &pretty_arrays_obj, @@ -674,6 +676,7 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw) &PyBool_Type, &array_indexes_obj, &PyBool_Type, &symbols_obj, &PyBool_Type, &unions_obj, + &PyBool_Type, &address_obj, &PyBool_Type, &deref_refs_obj, &PyBool_Type, &actual_objects_obj, &PyBool_Type, &static_members_obj, @@ -696,6 +699,8 @@ valpy_format_string (PyObject *self, PyObject *args, PyObject *kw) return NULL; if (!copy_py_bool_obj (&opts.unionprint, unions_obj)) return NULL; + if (!copy_py_bool_obj (&opts.addressprint, address_obj)) + return NULL; if (!copy_py_bool_obj (&opts.deref_ref, deref_refs_obj)) return NULL; if (!copy_py_bool_obj (&opts.objectprint, actual_objects_obj)) diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 73425638a23..551dd7192da 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2020-12-18 Hannes Domani + + * gdb.python/py-format-string.exp: Add tests for address keyword. + 2020-12-18 Hannes Domani * gdb.python/py-type.exp: Add tests for TYPE_CODE_METHOD. diff --git a/gdb/testsuite/gdb.python/py-format-string.exp b/gdb/testsuite/gdb.python/py-format-string.exp index b7acc242ed3..f4eaad04504 100644 --- a/gdb/testsuite/gdb.python/py-format-string.exp +++ b/gdb/testsuite/gdb.python/py-format-string.exp @@ -487,6 +487,48 @@ proc test_unions {} { } } +# Test the address option for gdb.Value.format_string. +proc test_address {} { + global undefined + global current_lang + + check_var_with_bool_opt "address" "a_point_t" + check_var_with_bool_opt "address" "a_point_t_pointer" \ + $undefined \ + "" + check_var_with_bool_opt "address" "another_point" + check_var_with_bool_opt "symbols" "a_struct_with_union" + check_var_with_bool_opt "address" "an_enum" + check_var_with_bool_opt "address" "a_string" \ + $undefined \ + "\"hello world\"" + check_var_with_bool_opt "address" "a_binary_string" \ + $undefined \ + "\"hello\"" + check_var_with_bool_opt "address" "a_binary_string_array" + check_var_with_bool_opt "address" "a_big_string" + check_var_with_bool_opt "address" "an_array" + check_var_with_bool_opt "address" "an_array_with_repetition" + check_var_with_bool_opt "address" "a_symbol_pointer" \ + $undefined \ + "" + + if { $current_lang == "c++" } { + check_var_with_bool_opt "address" "a_point_t_ref" + check_var_with_bool_opt "address" "a_base_ref" \ + $undefined \ + "" + } + + with_temp_option "set print address off" "set print address on" { + check_var_with_no_opts "a_string" \ + "\"hello world\"" + check_var_with_bool_opt "address" "a_string" \ + $undefined \ + "\"hello world\"" + } +} + # Test the deref_refs option for gdb.Value.format_string. proc test_deref_refs {} { global current_lang @@ -943,6 +985,7 @@ proc test_all_common {} { test_array_indexes test_symbols test_unions + test_address test_deref_refs test_actual_objects test_static_members -- 2.30.2