]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/python/libstdcxx/v6/xmethods.py
1 # Xmethods for libstdc++.
3 # Copyright (C) 2014 Free Software Foundation, Inc.
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.
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.
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/>.
22 matcher_name_prefix
= 'libstdc++::'
24 class LibStdCxxXMethod(gdb
.xmethod
.XMethod
):
25 def __init__(self
, name
, worker_class
):
26 gdb
.xmethod
.XMethod
.__init
__(self
, name
)
27 self
.worker_class
= worker_class
29 # Xmethods for std::array
31 class ArrayWorkerBase(gdb
.xmethod
.XMethodWorker
):
32 def __init__(self
, valtype
, size
):
33 self
._valtype
= valtype
37 nullptr
= gdb
.parse_and_eval('(void *) 0')
38 return nullptr
.cast(self
._valtype
.pointer()).dereference()
40 class ArraySizeWorker(ArrayWorkerBase
):
41 def __init__(self
, valtype
, size
):
42 ArrayWorkerBase
.__init
__(self
, valtype
, size
)
44 def get_arg_types(self
):
47 def __call__(self
, obj
):
50 class ArrayEmptyWorker(ArrayWorkerBase
):
51 def __init__(self
, valtype
, size
):
52 ArrayWorkerBase
.__init
__(self
, valtype
, size
)
54 def get_arg_types(self
):
57 def __call__(self
, obj
):
58 return (int(self
._size
) == 0)
60 class ArrayFrontWorker(ArrayWorkerBase
):
61 def __init__(self
, valtype
, size
):
62 ArrayWorkerBase
.__init
__(self
, valtype
, size
)
64 def get_arg_types(self
):
67 def __call__(self
, obj
):
68 if int(self
._size
) > 0:
69 return obj
['_M_elems'][0]
71 return self
.null_value()
73 class ArrayBackWorker(ArrayWorkerBase
):
74 def __init__(self
, valtype
, size
):
75 ArrayWorkerBase
.__init
__(self
, valtype
, size
)
77 def get_arg_types(self
):
80 def __call__(self
, obj
):
81 if int(self
._size
) > 0:
82 return obj
['_M_elems'][self
._size
- 1]
84 return self
.null_value()
86 class ArrayAtWorker(ArrayWorkerBase
):
87 def __init__(self
, valtype
, size
):
88 ArrayWorkerBase
.__init
__(self
, valtype
, size
)
90 def get_arg_types(self
):
91 return gdb
.lookup_type('std::size_t')
93 def __call__(self
, obj
, index
):
94 if int(index
) >= int(self
._size
):
95 raise IndexError('Array index "%d" should not be >= %d.' %
96 ((int(index
), self
._size
)))
97 return obj
['_M_elems'][index
]
99 class ArraySubscriptWorker(ArrayWorkerBase
):
100 def __init__(self
, valtype
, size
):
101 ArrayWorkerBase
.__init
__(self
, valtype
, size
)
103 def get_arg_types(self
):
104 return gdb
.lookup_type('std::size_t')
106 def __call__(self
, obj
, index
):
107 if int(self
._size
) > 0:
108 return obj
['_M_elems'][index
]
110 return self
.null_value()
112 class ArrayMethodsMatcher(gdb
.xmethod
.XMethodMatcher
):
114 gdb
.xmethod
.XMethodMatcher
.__init
__(self
,
115 matcher_name_prefix
+ 'array')
116 self
._method
_dict
= {
117 'size': LibStdCxxXMethod('size', ArraySizeWorker
),
118 'empty': LibStdCxxXMethod('empty', ArrayEmptyWorker
),
119 'front': LibStdCxxXMethod('front', ArrayFrontWorker
),
120 'back': LibStdCxxXMethod('back', ArrayBackWorker
),
121 'at': LibStdCxxXMethod('at', ArrayAtWorker
),
122 'operator[]': LibStdCxxXMethod('operator[]', ArraySubscriptWorker
),
124 self
.methods
= [self
._method
_dict
[m
] for m
in self
._method
_dict
]
126 def match(self
, class_type
, method_name
):
127 if not re
.match('^std::array<.*>$', class_type
.tag
):
129 method
= self
._method
_dict
.get(method_name
)
130 if method
is None or not method
.enabled
:
133 value_type
= class_type
.template_argument(0)
134 size
= class_type
.template_argument(1)
137 return method
.worker_class(value_type
, size
)
139 # Xmethods for std::deque
141 class DequeWorkerBase(gdb
.xmethod
.XMethodWorker
):
142 def __init__(self
, elemtype
):
143 self
._bufsize
= (512 / elemtype
.sizeof
) or 1
146 first_node
= obj
['_M_impl']['_M_start']['_M_node']
147 last_node
= obj
['_M_impl']['_M_finish']['_M_node']
148 cur
= obj
['_M_impl']['_M_finish']['_M_cur']
149 first
= obj
['_M_impl']['_M_finish']['_M_first']
150 return (last_node
- first_node
) * self
._bufsize
+ (cur
- first
)
152 def index(self
, obj
, index
):
153 first_node
= obj
['_M_impl']['_M_start']['_M_node']
154 index_node
= first_node
+ index
/ self
._bufsize
155 return index_node
[0][index
% self
._bufsize
]
157 class DequeEmptyWorker(DequeWorkerBase
):
158 def get_arg_types(self
):
161 def __call__(self
, obj
):
162 return (obj
['_M_impl']['_M_start']['_M_cur'] ==
163 obj
['_M_impl']['_M_finish']['_M_cur'])
165 class DequeSizeWorker(DequeWorkerBase
):
166 def get_arg_types(self
):
169 def __call__(self
, obj
):
170 return self
.size(obj
)
172 class DequeFrontWorker(DequeWorkerBase
):
173 def get_arg_types(self
):
176 def __call__(self
, obj
):
177 return obj
['_M_impl']['_M_start']['_M_cur'][0]
179 class DequeBackWorker(DequeWorkerBase
):
180 def get_arg_types(self
):
183 def __call__(self
, obj
):
184 if (obj
['_M_impl']['_M_finish']['_M_cur'] ==
185 obj
['_M_impl']['_M_finish']['_M_first']):
186 prev_node
= obj
['_M_impl']['_M_finish']['_M_node'] - 1
187 return prev_node
[0][self
._bufsize
- 1]
189 return obj
['_M_impl']['_M_finish']['_M_cur'][-1]
191 class DequeSubscriptWorker(DequeWorkerBase
):
192 def get_arg_types(self
):
193 return gdb
.lookup_type('std::size_t')
195 def __call__(self
, obj
, subscript
):
196 return self
.index(obj
, subscript
)
198 class DequeAtWorker(DequeWorkerBase
):
199 def get_arg_types(self
):
200 return gdb
.lookup_type('std::size_t')
202 def __call__(self
, obj
, index
):
203 deque_size
= int(self
.size(obj
))
204 if int(index
) >= deque_size
:
205 raise IndexError('Deque index "%d" should not be >= %d.' %
206 (int(index
), deque_size
))
208 return self
.index(obj
, index
)
210 class DequeMethodsMatcher(gdb
.xmethod
.XMethodMatcher
):
212 gdb
.xmethod
.XMethodMatcher
.__init
__(self
,
213 matcher_name_prefix
+ 'deque')
214 self
._method
_dict
= {
215 'empty': LibStdCxxXMethod('empty', DequeEmptyWorker
),
216 'size': LibStdCxxXMethod('size', DequeSizeWorker
),
217 'front': LibStdCxxXMethod('front', DequeFrontWorker
),
218 'back': LibStdCxxXMethod('back', DequeBackWorker
),
219 'operator[]': LibStdCxxXMethod('operator[]', DequeSubscriptWorker
),
220 'at': LibStdCxxXMethod('at', DequeAtWorker
)
222 self
.methods
= [self
._method
_dict
[m
] for m
in self
._method
_dict
]
224 def match(self
, class_type
, method_name
):
225 if not re
.match('^std::deque<.*>$', class_type
.tag
):
227 method
= self
._method
_dict
.get(method_name
)
228 if method
is None or not method
.enabled
:
230 return method
.worker_class(class_type
.template_argument(0))
232 # Xmethods for std::forward_list
234 class ForwardListWorkerBase(gdb
.xmethod
.XMethodMatcher
):
235 def __init__(self
, elem_type
, node_type
):
236 self
._elem
_type
= elem_type
237 self
._node
_type
= node_type
239 def get_arg_types(self
):
242 class ForwardListEmptyWorker(ForwardListWorkerBase
):
243 def __call__(self
, obj
):
244 return obj
['_M_impl']['_M_head']['_M_next'] == 0
246 class ForwardListFrontWorker(ForwardListWorkerBase
):
247 def __call__(self
, obj
):
248 node
= obj
['_M_impl']['_M_head']['_M_next'].cast(self
._node
_type
)
249 elem_address
= node
['_M_storage']['_M_storage'].address
250 return elem_address
.cast(self
._elem
_type
.pointer()).dereference()
252 class ForwardListMethodsMatcher(gdb
.xmethod
.XMethodMatcher
):
254 matcher_name
= matcher_name_prefix
+ 'forward_list'
255 gdb
.xmethod
.XMethodMatcher
.__init
__(self
, matcher_name
)
256 self
._method
_dict
= {
257 'empty': LibStdCxxXMethod('empty', ForwardListEmptyWorker
),
258 'front': LibStdCxxXMethod('front', ForwardListFrontWorker
)
260 self
.methods
= [self
._method
_dict
[m
] for m
in self
._method
_dict
]
262 def match(self
, class_type
, method_name
):
263 if not re
.match('^std::forward_list<.*>$', class_type
.tag
):
265 method
= self
._method
_dict
.get(method_name
)
266 if method
is None or not method
.enabled
:
268 elem_type
= class_type
.template_argument(0)
269 node_type
= gdb
.lookup_type(str(class_type
) + '::_Node').pointer()
270 return method
.worker_class(elem_type
, node_type
)
272 # Xmethods for std::list
274 class ListWorkerBase(gdb
.xmethod
.XMethodWorker
):
275 def __init__(self
, node_type
):
276 self
._node
_type
= node_type
278 def get_arg_types(self
):
281 class ListEmptyWorker(ListWorkerBase
):
282 def __call__(self
, obj
):
283 base_node
= obj
['_M_impl']['_M_node']
284 if base_node
['_M_next'] == base_node
.address
:
289 class ListSizeWorker(ListWorkerBase
):
290 def __call__(self
, obj
):
291 begin_node
= obj
['_M_impl']['_M_node']['_M_next']
292 end_node
= obj
['_M_impl']['_M_node'].address
294 while begin_node
!= end_node
:
295 begin_node
= begin_node
['_M_next']
299 class ListFrontWorker(ListWorkerBase
):
300 def __call__(self
, obj
):
301 node
= obj
['_M_impl']['_M_node']['_M_next'].cast(self
._node
_type
)
302 return node
['_M_data']
304 class ListBackWorker(ListWorkerBase
):
305 def __call__(self
, obj
):
306 prev_node
= obj
['_M_impl']['_M_node']['_M_prev'].cast(self
._node
_type
)
307 return prev_node
['_M_data']
309 class ListMethodsMatcher(gdb
.xmethod
.XMethodMatcher
):
311 gdb
.xmethod
.XMethodMatcher
.__init
__(self
,
312 matcher_name_prefix
+ 'list')
313 self
._method
_dict
= {
314 'empty': LibStdCxxXMethod('empty', ListEmptyWorker
),
315 'size': LibStdCxxXMethod('size', ListSizeWorker
),
316 'front': LibStdCxxXMethod('front', ListFrontWorker
),
317 'back': LibStdCxxXMethod('back', ListBackWorker
)
319 self
.methods
= [self
._method
_dict
[m
] for m
in self
._method
_dict
]
321 def match(self
, class_type
, method_name
):
322 if not re
.match('^std::list<.*>$', class_type
.tag
):
324 method
= self
._method
_dict
.get(method_name
)
325 if method
is None or not method
.enabled
:
327 node_type
= gdb
.lookup_type(str(class_type
) + '::_Node').pointer()
328 return method
.worker_class(node_type
)
330 # Xmethods for std::vector
332 class VectorWorkerBase(gdb
.xmethod
.XMethodWorker
):
333 def __init__(self
, elemtype
):
334 self
._elemtype
= elemtype
337 if self
._elemtype
.code
== gdb
.TYPE_CODE_BOOL
:
338 start
= obj
['_M_impl']['_M_start']['_M_p']
339 finish
= obj
['_M_impl']['_M_finish']['_M_p']
340 finish_offset
= obj
['_M_impl']['_M_finish']['_M_offset']
341 bit_size
= start
.dereference().type.sizeof
* 8
342 return (finish
- start
) * bit_size
+ finish_offset
344 return obj
['_M_impl']['_M_finish'] - obj
['_M_impl']['_M_start']
346 def get(self
, obj
, index
):
347 if self
._elemtype
.code
== gdb
.TYPE_CODE_BOOL
:
348 start
= obj
['_M_impl']['_M_start']['_M_p']
349 bit_size
= start
.dereference().type.sizeof
* 8
350 valp
= start
+ index
/ bit_size
351 offset
= index
% bit_size
352 return (valp
.dereference() & (1 << offset
)) > 0
354 return obj
['_M_impl']['_M_start'][index
]
356 class VectorEmptyWorker(VectorWorkerBase
):
357 def get_arg_types(self
):
360 def __call__(self
, obj
):
361 return int(self
.size(obj
)) == 0
363 class VectorSizeWorker(VectorWorkerBase
):
364 def get_arg_types(self
):
367 def __call__(self
, obj
):
368 return self
.size(obj
)
370 class VectorFrontWorker(VectorWorkerBase
):
371 def get_arg_types(self
):
374 def __call__(self
, obj
):
375 return self
.get(obj
, 0)
377 class VectorBackWorker(VectorWorkerBase
):
378 def get_arg_types(self
):
381 def __call__(self
, obj
):
382 return self
.get(obj
, int(self
.size(obj
)) - 1)
384 class VectorAtWorker(VectorWorkerBase
):
385 def get_arg_types(self
):
386 return gdb
.lookup_type('std::size_t')
388 def __call__(self
, obj
, index
):
389 size
= int(self
.size(obj
))
390 if int(index
) >= size
:
391 raise IndexError('Vector index "%d" should not be >= %d.' %
392 ((int(index
), size
)))
393 return self
.get(obj
, int(index
))
395 class VectorSubscriptWorker(VectorWorkerBase
):
396 def get_arg_types(self
):
397 return gdb
.lookup_type('std::size_t')
399 def __call__(self
, obj
, subscript
):
400 return self
.get(obj
, int(subscript
))
402 class VectorMethodsMatcher(gdb
.xmethod
.XMethodMatcher
):
404 gdb
.xmethod
.XMethodMatcher
.__init
__(self
,
405 matcher_name_prefix
+ 'vector')
406 self
._method
_dict
= {
407 'size': LibStdCxxXMethod('size', VectorSizeWorker
),
408 'empty': LibStdCxxXMethod('empty', VectorEmptyWorker
),
409 'front': LibStdCxxXMethod('front', VectorFrontWorker
),
410 'back': LibStdCxxXMethod('back', VectorBackWorker
),
411 'at': LibStdCxxXMethod('at', VectorAtWorker
),
412 'operator[]': LibStdCxxXMethod('operator[]',
413 VectorSubscriptWorker
),
415 self
.methods
= [self
._method
_dict
[m
] for m
in self
._method
_dict
]
417 def match(self
, class_type
, method_name
):
418 if not re
.match('^std::vector<.*>$', class_type
.tag
):
420 method
= self
._method
_dict
.get(method_name
)
421 if method
is None or not method
.enabled
:
423 return method
.worker_class(class_type
.template_argument(0))
425 # Xmethods for std::unique_ptr
427 class UniquePtrGetWorker(gdb
.xmethod
.XMethodWorker
):
432 def get_arg_types(self
):
435 def __call__(self
, obj
):
436 return obj
['_M_t']['_M_head_impl']
438 class UniquePtrDerefWorker(UniquePtrGetWorker
):
440 UniquePtrGetWorker
.__init
__(self
)
441 self
.name
= 'operator*'
443 def __call__(self
, obj
):
444 return UniquePtrGetWorker
.__call
__(self
, obj
).dereference()
446 class UniquePtrMethodsMatcher(gdb
.xmethod
.XMethodMatcher
):
448 gdb
.xmethod
.XMethodMatcher
.__init
__(self
,
449 matcher_name_prefix
+ 'unique_ptr')
450 self
._get
_worker
= UniquePtrGetWorker()
451 self
._deref
_worker
= UniquePtrDerefWorker()
452 self
.methods
= [self
._get
_worker
, self
._deref
_worker
]
454 def match(self
, class_type
, method_name
):
455 if not re
.match('^std::unique_ptr<.*>$', class_type
.tag
):
457 if method_name
== 'operator*' and self
._deref
_worker
.enabled
:
458 return self
._deref
_worker
459 elif method_name
== 'get' and self
._get
_worker
.enabled
:
460 return self
._get
_worker
462 def register_libstdcxx_xmethods(locus
):
463 gdb
.xmethod
.register_xmethod_matcher(locus
, ArrayMethodsMatcher())
464 gdb
.xmethod
.register_xmethod_matcher(locus
, ForwardListMethodsMatcher())
465 gdb
.xmethod
.register_xmethod_matcher(locus
, DequeMethodsMatcher())
466 gdb
.xmethod
.register_xmethod_matcher(locus
, ListMethodsMatcher())
467 gdb
.xmethod
.register_xmethod_matcher(locus
, VectorMethodsMatcher())
468 gdb
.xmethod
.register_xmethod_matcher(locus
, UniquePtrMethodsMatcher())