OSDN Git Service

gdb
[pf3gnuchains/pf3gnuchains4x.git] / gdb / testsuite / gdb.python / py-pp-maint.py
1 # Copyright (C) 2010-2012 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 python pretty
17 # printers.
18
19 import re
20 import gdb.types
21 import gdb.printing
22
23
24 def lookup_function_lookup_test(val):
25     class PrintFunctionLookup(object):
26         def __init__(self, val):
27             self.val = val
28
29         def to_string(self):
30             return ("x=<" + str(self.val["x"]) +
31                     "> y=<" + str(self.val["y"]) + ">")
32
33     typename = gdb.types.get_basic_type(val.type).tag
34     # Note: typename could be None.
35     if typename == "function_lookup_test":
36         return PrintFunctionLookup(val)
37     return None
38
39
40 class pp_s:
41     def __init__(self, val):
42         self.val = val
43
44     def to_string(self):
45         a = self.val["a"]
46         b = self.val["b"]
47         if a.address != b:
48             raise Exception("&a(%s) != b(%s)" % (str(a.address), str(b)))
49         return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
50
51
52 class pp_ss:
53     def __init__(self, val):
54         self.val = val
55
56     def to_string(self):
57         return "a=<" + str(self.val["a"]) + "> b=<" + str(self.val["b"]) + ">"
58
59
60 def build_pretty_printer():
61     pp = gdb.printing.RegexpCollectionPrettyPrinter("pp-test")
62
63     pp.add_printer('struct s', '^struct s$', pp_s)
64     pp.add_printer('s', '^s$', pp_s)
65
66     # Use a lambda this time to exercise doing things this way.
67     pp.add_printer('struct ss', '^struct ss$', lambda val: pp_ss(val))
68     pp.add_printer('ss', '^ss$', lambda val: pp_ss(val))
69
70     pp.add_printer('enum flag_enum', '^flag_enum$',
71                    gdb.printing.FlagEnumerationPrinter('enum flag_enum'))
72
73     return pp
74
75
76 gdb.printing.register_pretty_printer(gdb, lookup_function_lookup_test)
77 my_pretty_printer = build_pretty_printer()
78 gdb.printing.register_pretty_printer(gdb, my_pretty_printer)
79
80 # Exercise the "replace" argument to register pretty_printer.
81 saw_runtime_error = False
82 try:
83   gdb.printing.register_pretty_printer(gdb, my_pretty_printer, replace=False)
84 except RuntimeError:
85   saw_runtime_error = True
86   pass
87 if not saw_runtime_error:
88   raise RuntimeError("Missing RuntimeError from register_pretty_printer")
89 gdb.printing.register_pretty_printer(gdb, my_pretty_printer, replace=True)