1 # Copyright 2014-2015 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It test the xmethods support
17 # in the Python extension language.
22 from gdb
.xmethod
import XMethod
23 from gdb
.xmethod
import XMethodMatcher
, XMethodWorker
24 from gdb
.xmethod
import SimpleXMethodMatcher
27 def A_plus_A(obj
, opr
):
28 print('From Python <A_plus_A>:')
29 return obj
['a'] + opr
['a']
33 print('From Python <plus_plus_A>:')
38 print('From Python <A_geta>:')
42 def A_getarrayind(obj
, index
):
43 print('From Python <A_getarrayind>:')
44 return obj
['array'][index
]
47 type_A
= gdb
.parse_and_eval('(dop::A *) 0').type.target()
48 type_B
= gdb
.parse_and_eval('(dop::B *) 0').type.target()
49 type_int
= gdb
.parse_and_eval('(int *) 0').type.target()
52 # The E class matcher and worker test two things:
53 # 1. xmethod returning None.
54 # 2. Matcher returning a list of workers.
56 class E_method_char_worker(XMethodWorker
):
60 def get_arg_types(self
):
61 return gdb
.lookup_type('char')
63 def get_result_type(self
, obj
, arg
):
64 return gdb
.lookup_type('void')
66 def __call__(self
, obj
, arg
):
67 print('From Python <E_method_char>')
71 class E_method_int_worker(XMethodWorker
):
75 def get_arg_types(self
):
76 return gdb
.lookup_type('int')
78 # Note: get_result_type method elided on purpose
80 def __call__(self
, obj
, arg
):
81 print('From Python <E_method_int>')
85 class E_method_matcher(XMethodMatcher
):
87 XMethodMatcher
.__init
__(self
, 'E_methods')
88 self
.methods
= [XMethod('method_int'), XMethod('method_char')]
90 def match(self
, class_type
, method_name
):
91 class_tag
= class_type
.unqualified().tag
92 if not re
.match('^dop::E$', class_tag
):
94 if not re
.match('^method$', method_name
):
97 if self
.methods
[0].enabled
:
98 workers
.append(E_method_int_worker())
99 if self
.methods
[1].enabled
:
100 workers
.append(E_method_char_worker())
104 # The G class method matcher and worker illustrate how to write
105 # xmethod matchers and workers for template classes and template
108 class G_size_diff_worker(XMethodWorker
):
109 def __init__(self
, class_template_type
, method_template_type
):
110 self
._class
_template
_type
= class_template_type
111 self
._method
_template
_type
= method_template_type
113 def get_arg_types(self
):
116 def __call__(self
, obj
):
117 print('From Python G<>::size_diff()')
118 return (self
._method
_template
_type
.sizeof
-
119 self
._class
_template
_type
.sizeof
)
122 class G_size_mul_worker(XMethodWorker
):
123 def __init__(self
, class_template_type
, method_template_val
):
124 self
._class
_template
_type
= class_template_type
125 self
._method
_template
_val
= method_template_val
127 def get_arg_types(self
):
130 def __call__(self
, obj
):
131 print('From Python G<>::size_mul()')
132 return self
._class
_template
_type
.sizeof
* self
._method
_template
_val
135 class G_mul_worker(XMethodWorker
):
136 def __init__(self
, class_template_type
, method_template_type
):
137 self
._class
_template
_type
= class_template_type
138 self
._method
_template
_type
= method_template_type
140 def get_arg_types(self
):
141 return self
._method
_template
_type
143 def __call__(self
, obj
, arg
):
144 print('From Python G<>::mul()')
145 return obj
['t'] * arg
148 class G_methods_matcher(XMethodMatcher
):
150 XMethodMatcher
.__init
__(self
, 'G_methods')
151 self
.methods
= [XMethod('size_diff'),
155 def _is_enabled(self
, name
):
156 for method
in self
.methods
:
157 if method
.name
== name
and method
.enabled
:
160 def match(self
, class_type
, method_name
):
161 class_tag
= class_type
.unqualified().tag
162 if not re
.match('^dop::G<[ ]*[_a-zA-Z][ _a-zA-Z0-9]*>$',
165 t_name
= class_tag
[7:-1]
167 t_type
= gdb
.lookup_type(t_name
)
170 if re
.match('^size_diff<[ ]*[_a-zA-Z][ _a-zA-Z0-9]*>$', method_name
):
171 if not self
._is
_enabled
('size_diff'):
173 t1_name
= method_name
[10:-1]
175 t1_type
= gdb
.lookup_type(t1_name
)
176 return G_size_diff_worker(t_type
, t1_type
)
179 if re
.match('^size_mul<[ ]*[0-9]+[ ]*>$', method_name
):
180 if not self
._is
_enabled
('size_mul'):
182 m_val
= int(method_name
[9:-1])
183 return G_size_mul_worker(t_type
, m_val
)
184 if re
.match('^mul<[ ]*[_a-zA-Z][ _a-zA-Z0-9]*>$', method_name
):
185 if not self
._is
_enabled
('mul'):
187 t1_name
= method_name
[4:-1]
189 t1_type
= gdb
.lookup_type(t1_name
)
190 return G_mul_worker(t_type
, t1_type
)
196 SimpleXMethodMatcher('A_plus_A',
200 # This is a replacement, hence match the arg type
202 type_A
.const().reference()),
203 SimpleXMethodMatcher('plus_plus_A',
207 SimpleXMethodMatcher('A_geta',
211 SimpleXMethodMatcher('A_getarrayind',
218 for matcher
in global_dm_list
:
219 gdb
.xmethod
.register_xmethod_matcher(gdb
, matcher
)
220 gdb
.xmethod
.register_xmethod_matcher(gdb
.current_progspace(),
222 gdb
.xmethod
.register_xmethod_matcher(gdb
.current_progspace(),
This page took 0.033784 seconds and 4 git commands to generate.