GNU Binutils with patches for OS216
Révision | f2f3ccb9f81255fd1b4f877600f39979c2d7ece5 (tree) |
---|---|
l'heure | 2016-03-11 07:12:30 |
Auteur | Simon Marchi <simon.marchi@poly...> |
Commiter | Simon Marchi |
Add $_as_string convenience function
This patch is a follow-up to "Add printf format specifier for printing
enumerator":
Instead of having a solution specific to the printf command, Pedro
suggested adding a general purpose function $_as_string() that would
cover this use case and more.
So, in order to print the textual label of an enum, one can use:
gdb/ChangeLog:
* data-directory/Makefile.in (PYTHON_FILE_LIST): Install
gdb/function/as_string.py.
* python/lib/gdb/function/as_string.py: New file.
* NEWS: Mention the new $_as_string function.
gdb/testsuite/ChangeLog:
* gdb.python/py-as-string.exp: New file.
* gdb.python/py-as-string.c: New file.
gdb/doc/ChangeLog:
* gdb.texinfo (Convenience Functions): Document $_as_string.
@@ -1,3 +1,10 @@ | ||
1 | +2016-03-10 Simon Marchi <simon.marchi@polymtl.ca> | |
2 | + | |
3 | + * data-directory/Makefile.in (PYTHON_FILE_LIST): Install | |
4 | + gdb/function/as_string.py. | |
5 | + * python/lib/gdb/function/as_string.py: New file. | |
6 | + * NEWS: Mention the new $_as_string function. | |
7 | + | |
1 | 8 | 2016-03-09 Jose E. Marchesi <jose.marchesi@oracle.com> |
2 | 9 | |
3 | 10 | * target.h: Fix doc string of target_can_use_hardware_watchpoint. |
@@ -3,6 +3,10 @@ | ||
3 | 3 | |
4 | 4 | *** Changes since GDB 7.11 |
5 | 5 | |
6 | +* New Python-based convenience function $_as_string(val), which returns | |
7 | + the textual representation of a value. This function is especially | |
8 | + useful to obtain the text label of an enum value. | |
9 | + | |
6 | 10 | * Intel MPX bound violation handling. |
7 | 11 | |
8 | 12 | Segmentation faults caused by a Intel MPX boundary violation |
@@ -74,6 +74,7 @@ PYTHON_FILE_LIST = \ | ||
74 | 74 | gdb/command/prompt.py \ |
75 | 75 | gdb/command/explore.py \ |
76 | 76 | gdb/function/__init__.py \ |
77 | + gdb/function/as_string.py \ | |
77 | 78 | gdb/function/caller_is.py \ |
78 | 79 | gdb/function/strfns.py \ |
79 | 80 | gdb/printer/__init__.py \ |
@@ -1,3 +1,7 @@ | ||
1 | +2016-03-10 Simon Marchi <simon.marchi@polymtl.ca> | |
2 | + | |
3 | + * gdb.texinfo (Convenience Functions): Document $_as_string. | |
4 | + | |
1 | 5 | 2016-02-23 Doug Evans <dje@google.com> |
2 | 6 | |
3 | 7 | * gdb.texinfo (Skipping Over Functions and Files): Document new |
@@ -10760,6 +10760,19 @@ checks all stack frames from the immediate caller to the frame specified | ||
10760 | 10760 | by @var{number_of_frames}, whereas @code{$_caller_matches} only checks the |
10761 | 10761 | frame specified by @var{number_of_frames}. |
10762 | 10762 | |
10763 | +@item $_as_string(@var{value}) | |
10764 | +@findex $_as_string@r{, convenience function} | |
10765 | +Return the string representation of @var{value}. | |
10766 | + | |
10767 | +This function is useful to obtain the textual label (enumerator) of an | |
10768 | +enumeration value. For example, assuming the variable @var{node} is of | |
10769 | +an enumerated type: | |
10770 | + | |
10771 | +@smallexample | |
10772 | +(gdb) printf "Visiting node of type %s\n", $_as_string(node) | |
10773 | +Visiting node of type NODE_INTEGER | |
10774 | +@end smallexample | |
10775 | + | |
10763 | 10776 | @end table |
10764 | 10777 | |
10765 | 10778 | @value{GDBN} provides the ability to list and get help on |
@@ -0,0 +1,39 @@ | ||
1 | +# Copyright (C) 2016 Free Software Foundation, Inc. | |
2 | + | |
3 | +# This program is free software; you can redistribute it and/or modify | |
4 | +# it under the terms of the GNU General Public License as published by | |
5 | +# the Free Software Foundation; either version 3 of the License, or | |
6 | +# (at your option) any later version. | |
7 | +# | |
8 | +# This program is distributed in the hope that it will be useful, | |
9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | +# GNU General Public License for more details. | |
12 | +# | |
13 | +# You should have received a copy of the GNU General Public License | |
14 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | + | |
16 | +import gdb | |
17 | + | |
18 | + | |
19 | +class _AsString(gdb.Function): | |
20 | + """Return the string representation of a value. | |
21 | + | |
22 | +Usage: | |
23 | + $_as_string(value) | |
24 | + | |
25 | +Arguments: | |
26 | + | |
27 | + value: A gdb.Value. | |
28 | + | |
29 | +Returns: | |
30 | + The string representation of the value. | |
31 | +""" | |
32 | + | |
33 | + def __init__(self): | |
34 | + super(_AsString, self).__init__("_as_string") | |
35 | + | |
36 | + def invoke(self, val): | |
37 | + return str(val) | |
38 | + | |
39 | +_AsString() |
@@ -1,3 +1,8 @@ | ||
1 | +2016-03-10 Simon Marchi <simon.marchi@polymtl.ca> | |
2 | + | |
3 | + * gdb.python/py-as-string.exp: New file. | |
4 | + * gdb.python/py-as-string.c: New file. | |
5 | + | |
1 | 6 | 2016-03-09 Pedro Alves <palves@redhat.com> |
2 | 7 | |
3 | 8 | * gdb.threads/attach-into-signal.exp: Adjust to "Program received |
@@ -0,0 +1,32 @@ | ||
1 | +/* This testcase is part of GDB, the GNU debugger. | |
2 | + | |
3 | + Copyright 2016 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 | +enum EnumType { | |
19 | + ENUM_VALUE_A, | |
20 | + ENUM_VALUE_B, | |
21 | + ENUM_VALUE_C, | |
22 | + ENUM_VALUE_D, | |
23 | +}; | |
24 | + | |
25 | +static enum EnumType enum_valid = ENUM_VALUE_B; | |
26 | +static enum EnumType enum_invalid = 20; | |
27 | + | |
28 | +int | |
29 | +main () | |
30 | +{ | |
31 | + return 0; | |
32 | +} |
@@ -0,0 +1,40 @@ | ||
1 | +# Copyright (C) 2016 Free Software Foundation, Inc. | |
2 | + | |
3 | +# This program is free software; you can redistribute it and/or modify | |
4 | +# it under the terms of the GNU General Public License as published by | |
5 | +# the Free Software Foundation; either version 3 of the License, or | |
6 | +# (at your option) any later version. | |
7 | +# | |
8 | +# This program is distributed in the hope that it will be useful, | |
9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | +# GNU General Public License for more details. | |
12 | +# | |
13 | +# You should have received a copy of the GNU General Public License | |
14 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
15 | + | |
16 | +# This file is part of the GDB testsuite. It tests the convenience | |
17 | +# functions in as_string.py. | |
18 | + | |
19 | +load_lib gdb-python.exp | |
20 | + | |
21 | +standard_testfile | |
22 | + | |
23 | +if { [prepare_for_testing ${testfile}.exp ${testfile} ${srcfile}] } { | |
24 | + return -1 | |
25 | +} | |
26 | + | |
27 | +if ![runto_main] { | |
28 | + fail "Couldn't run to main." | |
29 | + return 0 | |
30 | +} | |
31 | + | |
32 | +if { [skip_python_tests] } { continue } | |
33 | + | |
34 | +proc test_as_string { } { | |
35 | + gdb_test "p \$_as_string(2)" "\"2\"" | |
36 | + gdb_test "p \$_as_string(enum_valid)" "\"ENUM_VALUE_B\"" | |
37 | + gdb_test "p \$_as_string(enum_invalid)" "\"20\"" | |
38 | +} | |
39 | + | |
40 | +test_as_string |