Skip to content

Commit 52093e1

Browse files
committed
Add gdb.Value.format_string ()
The str () function, called on a gdb.Value instance, produces a string representation similar to what can be achieved with the print command, but it doesn't allow to specify additional formatting settings, for instance disabling pretty printers. This patch introduces a new format_string () method to gdb.Value which allows specifying more formatting options, thus giving access to more features provided by the internal C function common_val_print (). gdb/ChangeLog: 2019-04-01 Marco Barisione <[email protected]> Add gdb.Value.format_string (). * python/py-value.c (copy_py_bool_obj): (valpy_format_string): Add gdb.Value.format_string (). * NEWS: Document the addition of gdb.Value.format_string (). gdb/doc/ChangeLog: 2019-04-01 Marco Barisione <[email protected]> * python.texi (Values From Inferior): Document gdb.Value.format_string (). gdb/testsuite/ChangeLog: 2019-04-01 Marco Barisione <[email protected]> Test gdb.Value.format_string (). * gdb.python/py-format-string.exp: New test. * gdb.python/py-format-string.c: New file. * gdb.python/py-format-string.py: New file.
1 parent 8828efd commit 52093e1

File tree

6 files changed

+1377
-0
lines changed

6 files changed

+1377
-0
lines changed

gdb/NEWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616

1717
* Support for Pointer Authentication on AArch64 Linux.
1818

19+
* Python API
20+
21+
** The gdb.Value type has a new method 'format_string' which returns a
22+
string representing the value. The formatting is controlled by the
23+
optional keyword arguments: 'raw', 'pretty_arrays', 'pretty_structs',
24+
'array_indexes', 'symbols', 'unions', 'deref_refs', 'actual_objects',
25+
'static_members', 'max_elements', 'repeat_threshold', and 'format'.
26+
27+
1928
*** Changes in GDB 8.3
2029

2130
* GDB and GDBserver now support access to additional registers on

gdb/doc/python.texi

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,86 @@ Like @code{Value.cast}, but works as if the C@t{++} @code{reinterpret_cast}
864864
operator were used. Consult a C@t{++} reference for details.
865865
@end defun
866866

867+
@defun Value.format_string (...)
868+
Convert a @code{gdb.Value} to a string, similarly to what the @code{print}
869+
command does. Invoked with no arguments, this is equivalent to calling
870+
the @code{str} function on the @code{gdb.Value}. The representation of
871+
the same value may change across different versions of @value{GDBN}, so
872+
you shouldn't, for instance, parse the strings returned by this method.
873+
874+
All the arguments are keyword only. If an argument is not specified, the
875+
current global default setting is used.
876+
877+
@table @code
878+
@item raw
879+
@code{True} if pretty-printers (@pxref{Pretty Printing}) should not be
880+
used to format the value. @code{False} if enabled pretty-printers
881+
matching the type represented by the @code{gdb.Value} should be used to
882+
format it.
883+
884+
@item pretty_arrays
885+
@code{True} if arrays should be pretty printed to be more convenient to
886+
read, @code{False} if they shouldn't (see @code{set print array} in
887+
@ref{Print Settings}).
888+
889+
@item pretty_structs
890+
@code{True} if structs should be pretty printed to be more convenient to
891+
read, @code{False} if they shouldn't (see @code{set print pretty} in
892+
@ref{Print Settings}).
893+
894+
@item array_indexes
895+
@code{True} if array indexes should be included in the string
896+
representation of arrays, @code{False} if they shouldn't (see @code{set
897+
print array-indexes} in @ref{Print Settings}).
898+
899+
@item symbols
900+
@code{True} if the string representation of a pointer should include the
901+
corresponding symbol name (if one exists), @code{False} if it shouldn't
902+
(see @code{set print symbol} in @ref{Print Settings}).
903+
904+
@item unions
905+
@code{True} if unions which are contained in other structures or unions
906+
should be expanded, @code{False} if they shouldn't (see @code{set print
907+
union} in @ref{Print Settings}).
908+
909+
@item deref_refs
910+
@code{True} if C@t{++} references should be resolved to the value they
911+
refer to, @code{False} (the default) if they shouldn't. Note that, unlike
912+
for the @code{print} command, references are not automatically expanded
913+
when using the @code{format_string} method or the @code{str}
914+
function. There is no global @code{print} setting to change the default
915+
behaviour.
916+
917+
@item actual_objects
918+
@code{True} if the representation of a pointer to an object should
919+
identify the @emph{actual} (derived) type of the object rather than the
920+
@emph{declared} type, using the virtual function table. @code{False} if
921+
the @emph{declared} type should be used. (See @code{set print object} in
922+
@ref{Print Settings}).
923+
924+
@item static_fields
925+
@code{True} if static members should be included in the string
926+
representation of a C@t{++} object, @code{False} if they shouldn't (see
927+
@code{set print static-members} in @ref{Print Settings}).
928+
929+
@item max_elements
930+
Number of array elements to print, or @code{0} to print an unlimited
931+
number of elements (see @code{set print elements} in @ref{Print
932+
Settings}).
933+
934+
@item repeat_threshold
935+
Set the threshold for suppressing display of repeated array elements, or
936+
@code{0} to represent all elements, even if repeated. (See @code{set
937+
print repeats} in @ref{Print Settings}).
938+
939+
@item format
940+
A string containing a single character representing the format to use for
941+
the returned string. For instance, @code{'x'} is equivalent to using the
942+
@value{GDBN} command @code{print} with the @code{/x} option and formats
943+
the value as a hexadecimal number.
944+
@end table
945+
@end defun
946+
867947
@defun Value.string (@r{[}encoding@r{[}, errors@r{[}, length@r{]]]})
868948
If this @code{gdb.Value} represents a string, then this method
869949
converts the contents to a Python string. Otherwise, this method will

gdb/python/py-value.c

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,165 @@ valpy_string (PyObject *self, PyObject *args, PyObject *kw)
588588
encoding, errors);
589589
}
590590

591+
/* Given a Python object, copy its truth value to a C int (the value
592+
pointed by dest).
593+
If src_obj is NULL, then *dest is not modified.
594+
595+
Return true in case of success (including src_obj being NULL), false
596+
in case of error. */
597+
598+
static bool
599+
copy_py_bool_obj (int *dest, PyObject *src_obj)
600+
{
601+
if (src_obj)
602+
{
603+
int cmp = PyObject_IsTrue (src_obj);
604+
if (cmp < 0)
605+
return false;
606+
*dest = cmp;
607+
}
608+
609+
return true;
610+
}
611+
612+
/* Implementation of gdb.Value.format_string (...) -> string.
613+
Return Unicode string with value contents formatted using the
614+
keyword-only arguments. */
615+
616+
static PyObject *
617+
valpy_format_string (PyObject *self, PyObject *args, PyObject *kw)
618+
{
619+
static const char *keywords[] =
620+
{
621+
/* Basic C/C++ options. */
622+
"raw", /* See the /r option to print. */
623+
"pretty_arrays", /* See set print array on|off. */
624+
"pretty_structs", /* See set print pretty on|off. */
625+
"array_indexes", /* See set print array-indexes on|off. */
626+
"symbols", /* See set print symbol on|off. */
627+
"unions", /* See set print union on|off. */
628+
/* C++ options. */
629+
"deref_refs", /* No corresponding setting. */
630+
"actual_objects", /* See set print object on|off. */
631+
"static_members", /* See set print static-members on|off. */
632+
/* C non-bool options. */
633+
"max_elements", /* See set print elements N. */
634+
"repeat_threshold", /* See set print repeats. */
635+
"format", /* The format passed to the print command. */
636+
NULL
637+
};
638+
639+
/* This function has too many arguments to be useful as positionals, so
640+
the user should specify them all as keyword arguments.
641+
Python 3.3 and later have a way to specify it (both in C and Python
642+
itself), but we could be compiled with older versions, so we just
643+
check that the args tuple is empty. */
644+
Py_ssize_t positional_count = PyObject_Length (args);
645+
if (positional_count < 0)
646+
return NULL;
647+
else if (positional_count > 0)
648+
{
649+
/* This matches the error message that Python 3.3 raises when
650+
passing positionals to functions expecting keyword-only
651+
arguments. */
652+
PyErr_Format (PyExc_TypeError,
653+
"format_string() takes 0 positional arguments but %zu were given",
654+
positional_count);
655+
return NULL;
656+
}
657+
658+
struct value_print_options opts;
659+
get_user_print_options (&opts);
660+
opts.deref_ref = 0;
661+
662+
/* We need objects for booleans as the "p" flag for bools is new in
663+
Python 3.3. */
664+
PyObject *raw_obj = NULL;
665+
PyObject *pretty_arrays_obj = NULL;
666+
PyObject *pretty_structs_obj = NULL;
667+
PyObject *array_indexes_obj = NULL;
668+
PyObject *symbols_obj = NULL;
669+
PyObject *unions_obj = NULL;
670+
PyObject *deref_refs_obj = NULL;
671+
PyObject *actual_objects_obj = NULL;
672+
PyObject *static_members_obj = NULL;
673+
char *format = NULL;
674+
if (!gdb_PyArg_ParseTupleAndKeywords (args,
675+
kw,
676+
"|O!O!O!O!O!O!O!O!O!IIs",
677+
keywords,
678+
&PyBool_Type, &raw_obj,
679+
&PyBool_Type, &pretty_arrays_obj,
680+
&PyBool_Type, &pretty_structs_obj,
681+
&PyBool_Type, &array_indexes_obj,
682+
&PyBool_Type, &symbols_obj,
683+
&PyBool_Type, &unions_obj,
684+
&PyBool_Type, &deref_refs_obj,
685+
&PyBool_Type, &actual_objects_obj,
686+
&PyBool_Type, &static_members_obj,
687+
&opts.print_max,
688+
&opts.repeat_count_threshold,
689+
&format))
690+
return NULL;
691+
692+
/* Set boolean arguments. */
693+
if (!copy_py_bool_obj (&opts.raw, raw_obj))
694+
return NULL;
695+
if (!copy_py_bool_obj (&opts.prettyformat_arrays, pretty_arrays_obj))
696+
return NULL;
697+
if (!copy_py_bool_obj (&opts.prettyformat_structs, pretty_structs_obj))
698+
return NULL;
699+
if (!copy_py_bool_obj (&opts.print_array_indexes, array_indexes_obj))
700+
return NULL;
701+
if (!copy_py_bool_obj (&opts.symbol_print, symbols_obj))
702+
return NULL;
703+
if (!copy_py_bool_obj (&opts.unionprint, unions_obj))
704+
return NULL;
705+
if (!copy_py_bool_obj (&opts.deref_ref, deref_refs_obj))
706+
return NULL;
707+
if (!copy_py_bool_obj (&opts.objectprint, actual_objects_obj))
708+
return NULL;
709+
if (!copy_py_bool_obj (&opts.static_field_print, static_members_obj))
710+
return NULL;
711+
712+
/* Numeric arguments for which 0 means unlimited (which we represent as
713+
UINT_MAX). */
714+
if (opts.print_max == 0)
715+
opts.print_max = UINT_MAX;
716+
if (opts.repeat_count_threshold == 0)
717+
opts.repeat_count_threshold = UINT_MAX;
718+
719+
/* Other arguments. */
720+
if (format != NULL)
721+
{
722+
if (strlen (format) == 1)
723+
opts.format = format[0];
724+
else
725+
{
726+
/* Mimic the message on standard Python ones for similar
727+
errors. */
728+
PyErr_SetString (PyExc_ValueError,
729+
"a single character is required");
730+
return NULL;
731+
}
732+
}
733+
734+
string_file stb;
735+
736+
TRY
737+
{
738+
common_val_print (((value_object *) self)->value, &stb, 0,
739+
&opts, python_language);
740+
}
741+
CATCH (except, RETURN_MASK_ALL)
742+
{
743+
GDB_PY_HANDLE_EXCEPTION (except);
744+
}
745+
END_CATCH
746+
747+
return PyUnicode_Decode (stb.c_str (), stb.size (), host_charset (), NULL);
748+
}
749+
591750
/* A helper function that implements the various cast operators. */
592751

593752
static PyObject *
@@ -1944,6 +2103,11 @@ Return a lazy string representation of the value." },
19442103
Return Unicode string representation of the value." },
19452104
{ "fetch_lazy", valpy_fetch_lazy, METH_NOARGS,
19462105
"Fetches the value from the inferior, if it was lazy." },
2106+
{ "format_string", (PyCFunction) valpy_format_string,
2107+
METH_VARARGS | METH_KEYWORDS,
2108+
"format_string (...) -> string\n\
2109+
Return a string representation of the value using the specified\n\
2110+
formatting options" },
19472111
{NULL} /* Sentinel */
19482112
};
19492113

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* This testcase is part of GDB, the GNU debugger.
2+
3+
Copyright 2019 Free Software Foundation, Inc.
4+
5+
This program is free software; you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation; either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>. */
17+
18+
typedef struct point
19+
{
20+
int x;
21+
int y;
22+
} point_t;
23+
24+
typedef union
25+
{
26+
int an_int;
27+
char a_char;
28+
} union_t;
29+
30+
typedef struct
31+
{
32+
union_t the_union;
33+
} struct_union_t;
34+
35+
typedef enum
36+
{
37+
ENUM_FOO,
38+
ENUM_BAR,
39+
} enum_t;
40+
41+
typedef void (*function_t) (int);
42+
43+
static void
44+
my_function(int n)
45+
{
46+
}
47+
48+
#ifdef __cplusplus
49+
50+
struct Base
51+
{
52+
Base (int a_) : a (a_) {}
53+
54+
virtual int get_number () { return a; }
55+
56+
int a;
57+
58+
static int a_static_member;
59+
};
60+
61+
int Base::a_static_member = 2019;
62+
63+
struct Deriv : Base
64+
{
65+
Deriv (int b_) : Base (42), b (b_) {}
66+
67+
virtual int get_number () { return b; }
68+
69+
int b;
70+
};
71+
72+
#endif
73+
74+
int global_symbol = 42;
75+
76+
int
77+
main ()
78+
{
79+
point_t a_point_t = { 42, 12 };
80+
point_t *a_point_t_pointer = &a_point_t;
81+
#ifdef __cplusplus
82+
point_t &a_point_t_ref = a_point_t;
83+
#endif
84+
struct point another_point = { 123, 456 };
85+
86+
struct_union_t a_struct_with_union;
87+
a_struct_with_union.the_union.an_int = 42;
88+
89+
enum_t an_enum = ENUM_BAR;
90+
91+
const char *a_string = "hello world";
92+
const char *a_binary_string = "hello\0world";
93+
const char a_binary_string_array[] = "hello\0world";
94+
95+
const int letters_repeat = 10;
96+
char a_big_string[26 * letters_repeat + 1];
97+
a_big_string[26 * letters_repeat] = '\0';
98+
for (int i = 0; i < letters_repeat; i++)
99+
for (char c = 'A'; c <= 'Z'; c++)
100+
a_big_string[i * 26 + c - 'A'] = c;
101+
102+
int an_array[] = { 2, 3, 5 };
103+
104+
int an_array_with_repetition[] = {
105+
1, /* 1 time. */
106+
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 12 times. */
107+
5, 5, 5, /* 3 times */
108+
};
109+
110+
int *a_symbol_pointer = &global_symbol;
111+
112+
#ifdef __cplusplus
113+
Deriv a_deriv (123);
114+
Base &a_base_ref = a_deriv;
115+
#endif
116+
117+
return 0; /* break here */
118+
}

0 commit comments

Comments
 (0)