1 2010-05-31 Chris Moller <cmoller@redhat.com>
3 * python/py-prettyprint.c (print_children): Add formatting for
4 matrices. (apply_val_pretty_printer): Detect and deal with matrix
8 2010-05-31 Chris Moller <cmoller@redhat.com>
10 * gdb.python/Makefile.in (EXECUTABLES): Added pr10659.
11 * gdb.python/pr10659.cc: New file.
12 * gdb.python/pr10659.exp. New file.
13 * gdb.python/pr10659.py: New file.
15 Index: gdb-7.2.50.20110218/gdb/valprint.h
16 ===================================================================
17 --- gdb-7.2.50.20110218.orig/gdb/valprint.h 2011-02-14 12:35:45.000000000 +0100
18 +++ gdb-7.2.50.20110218/gdb/valprint.h 2011-02-18 10:44:32.000000000 +0100
19 @@ -90,6 +90,9 @@ struct value_print_options
21 /* If nonzero, print the value in "summary" form. */
24 + /* Affects pretty printing of matrices. */
25 + int prettyprint_matrix;
28 /* The global print options set by the user. In general this should
29 Index: gdb-7.2.50.20110218/gdb/python/py-prettyprint.c
30 ===================================================================
31 --- gdb-7.2.50.20110218.orig/gdb/python/py-prettyprint.c 2011-02-14 12:10:53.000000000 +0100
32 +++ gdb-7.2.50.20110218/gdb/python/py-prettyprint.c 2011-02-18 10:45:02.000000000 +0100
33 @@ -501,7 +501,7 @@ print_children (PyObject *printer, const
35 /* Use the prettyprint_arrays option if we are printing an array,
36 and the pretty option otherwise. */
38 + if (is_array || options->prettyprint_matrix)
39 pretty = options->prettyprint_arrays;
42 @@ -521,6 +521,9 @@ print_children (PyObject *printer, const
45 make_cleanup_py_decref (frame);
47 + if (options->prettyprint_matrix && recurse == 0)
48 + fputs_filtered ("\n", stream);
51 for (i = 0; i < options->print_max; ++i)
52 @@ -555,12 +558,23 @@ print_children (PyObject *printer, const
53 3. Other. Always print a ",". */
57 - fputs_filtered ("{", stream);
59 - fputs_filtered (" = {", stream);
60 + if (options->prettyprint_matrix && recurse == 0)
61 + print_spaces_filtered (2 + 2 * recurse, stream);
64 + if (options->prettyprint_matrix && strcmp (hint, "array"))
66 + fputs_filtered ("{\n", stream);
67 + print_spaces_filtered (4 + 2 * recurse, stream);
70 + fputs_filtered ("{", stream);
73 + fputs_filtered (" = {", stream);
76 + else if (options->prettyprint_matrix)
77 + print_spaces_filtered (4 + 2 * recurse, stream);
78 else if (! is_map || i % 2 == 0)
79 fputs_filtered (pretty ? "," : ", ", stream);
81 @@ -589,6 +603,10 @@ print_children (PyObject *printer, const
83 if (is_map && i % 2 == 0)
84 fputs_filtered ("[", stream);
85 + else if (options->prettyprint_matrix)
87 + /* Force a do-nothing. */
91 /* We print the index, not whatever the child method
92 @@ -667,7 +685,12 @@ print_children (PyObject *printer, const
93 fputs_filtered ("\n", stream);
94 print_spaces_filtered (2 * recurse, stream);
96 - fputs_filtered ("}", stream);
97 + if (options->prettyprint_matrix)
99 + print_spaces_filtered (4 * recurse, stream);
100 + fputs_filtered ("}\n", stream);
102 + else fputs_filtered ("}", stream);
106 @@ -689,6 +712,7 @@ apply_val_pretty_printer (struct type *t
108 struct cleanup *cleanups;
110 + struct value_print_options *options_copy;
111 enum string_repr_result print_result;
113 /* No pretty-printer support for unavailable values. */
114 @@ -726,9 +750,21 @@ apply_val_pretty_printer (struct type *t
116 /* If we are printing a map, we want some special formatting. */
117 hint = gdbpy_get_display_hint (printer);
121 + options_copy = alloca (sizeof (struct value_print_options));
122 + memcpy (options_copy, options, sizeof (struct value_print_options));
123 + options_copy->prettyprint_matrix = hint && !strcmp (hint, "matrix");
125 + else options_copy = (struct value_print_options *)options;
127 make_cleanup (free_current_contents, &hint);
129 /* Print the section */
130 + if (options_copy->prettyprint_matrix)
131 + print_result = string_repr_none;
132 +else /* Red Hat 2D matrix patch */
133 print_result = print_string_repr (printer, hint, stream, recurse,
134 options, language, gdbarch);
135 if (print_result != string_repr_error)
136 Index: gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.cc
137 ===================================================================
138 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
139 +++ gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.cc 2011-02-18 10:44:32.000000000 +0100
142 +#include <vector> // /usr/include/c++/4.4.1/bits/vector.tcc
145 +using namespace std;
147 +int use_windows = 9999;
151 + vector<int> test1(2,0);
155 + vector< vector<int> > test2(3, vector<int>(2,0));
166 + vector<int> rows(NR_ROWS, 0);
167 + vector< vector<int> > columns(NR_COLS, rows);
168 + vector< vector < vector<int> > > test3(NR_PLANES, columns);
170 + cout << "rows.size() = " << rows.size()
171 + << ", columns.size() = " << columns.size()
172 + << ", test3.size() = " << test3.size() << "\n";
174 + for (int i = 0; i < rows.size(); i++) {
175 + for (int j = 0; j < columns.size(); j++) {
176 + for (int k = 0; k < test3.size(); k++) {
177 + test3[k][j][i] = k * 100 + j * 10 + i;
184 Index: gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.exp
185 ===================================================================
186 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
187 +++ gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.exp 2011-02-18 10:44:32.000000000 +0100
189 +#Copyright 2010 Free Software Foundation, Inc.
191 +# This program is free software; you can redistribute it and/or modify
192 +# it under the terms of the GNU General Public License as published by
193 +# the Free Software Foundation; either version 3 of the License, or
194 +# (at your option) any later version.
196 +# This program is distributed in the hope that it will be useful,
197 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
198 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
199 +# GNU General Public License for more details.
201 +# You should have received a copy of the GNU General Public License
202 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
206 +set testfile pr10659
207 +set srcfile ${testfile}.cc
208 +if [prepare_for_testing $testfile.exp $testfile $srcfile {debug c++}] {
212 +#if { [skip_python_tests] } { continue }
214 +gdb_test "python execfile(\"$srcdir/$subdir/pr10659.py\")" ""
215 +gdb_test "python gdb.pretty_printers = \[lookup_function\]" ""
217 +if ![runto_main] then {
218 + fail "Can't run to main"
222 +gdb_breakpoint [gdb_get_line_number "break"]
223 +gdb_continue_to_breakpoint "break"
225 +gdb_test "p test1" "vector of length 2, capacity 2 =.*"
227 +gdb_test "p test2" "= $nl {$nl {.*"
229 +# Complete result is:
240 +gdb_test "p test3" "= $nl {$nl {$nl {.*"
242 +# Complete result is:
271 Index: gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.py
272 ===================================================================
273 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
274 +++ gdb-7.2.50.20110218/gdb/testsuite/gdb.python/pr10659.py 2011-02-18 10:44:32.000000000 +0100
276 +# Copyright (C) 2008, 2009 Free Software Foundation, Inc.
278 +# This program is free software; you can redistribute it and/or modify
279 +# it under the terms of the GNU General Public License as published by
280 +# the Free Software Foundation; either version 3 of the License, or
281 +# (at your option) any later version.
283 +# This program is distributed in the hope that it will be useful,
284 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
285 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
286 +# GNU General Public License for more details.
288 +# You should have received a copy of the GNU General Public License
289 +# along with this program. If not, see <http://www.gnu.org/licenses/>.
295 +vector_sig = 'std::vector'
296 +vector_regex = re.compile('^' + vector_sig + '<.*>$')
298 +class FakeVectorPrinter:
299 + "Print a std::vector"
302 + def __init__ (self, start, finish):
304 + self.finish = finish
307 + def __iter__(self):
311 + if self.item == self.finish:
312 + raise StopIteration
314 + self.count = self.count + 1
315 + elt = self.item.dereference()
316 + self.item = self.item + 1
317 + return ('[%d]' % count, elt)
319 + def __init__(self, typename, val):
320 + self.typename = typename
323 + def children(self):
324 + return self._iterator(self.val['_M_impl']['_M_start'],
325 + self.val['_M_impl']['_M_finish'])
327 + def to_string(self):
328 + start = self.val['_M_impl']['_M_start']
329 + finish = self.val['_M_impl']['_M_finish']
330 + end = self.val['_M_impl']['_M_end_of_storage']
331 + return ('std::vector of length %d, capacity %d'
332 + % (int (finish - start), int (end - start)))
334 + def display_hint(self):
335 + itype0 = self.val.type.template_argument(0)
337 + if itag and re.match(vector_regex, itag):
343 +def register_libstdcxx_printers (obj):
344 + "Register libstdc++ pretty-printers with objfile Obj."
349 + obj.pretty_printers.append (lookup_function)
351 +def lookup_function (val):
352 + "Look-up and return a pretty-printer that can print val."
357 + # If it points to a reference, get the reference.
358 + if type.code == gdb.TYPE_CODE_REF:
359 + type = type.target ()
361 + # Get the unqualified type, stripped of typedefs.
362 + type = type.unqualified ().strip_typedefs ()
364 + # Get the type name.
365 + typename = type.tag
366 + if typename == None:
369 + # Iterate over local dictionary of types to determine
370 + # if a printer is registered for that type. Return an
371 + # instantiation of the printer if found.
372 + for function in fake_pretty_printers_dict:
373 + if function.search (typename):
374 + return fake_pretty_printers_dict[function] (val)
376 + # Cannot find a pretty printer. Return None.
379 +def build_libfakecxx_dictionary ():
380 + fake_pretty_printers_dict[vector_regex] = lambda val: FakeVectorPrinter(vector_sig, val)
382 +fake_pretty_printers_dict = {}
384 +build_libfakecxx_dictionary ()
385 Index: gdb-7.2.50.20110218/gdb/valprint.c
386 ===================================================================
387 --- gdb-7.2.50.20110218.orig/gdb/valprint.c 2011-02-18 10:44:16.000000000 +0100
388 +++ gdb-7.2.50.20110218/gdb/valprint.c 2011-02-18 10:44:32.000000000 +0100
389 @@ -85,7 +85,8 @@ struct value_print_options user_print_op
390 1, /* static_field_print */
391 1, /* pascal_static_field_print */
395 + 0 /* prettyprint_matrix */
398 /* Initialize *OPTS to be a copy of the user print options. */