Debian Codegraph @

libstdc++-v3/python/libstdcxx/v6/xmethods.py

raw
1 # Xmethods for libstdc++.
2
3 # Copyright (C) 2014-2025 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 import gdb
19 import gdb.xmethod
20 import re
21
22 matcher_name_prefix = 'libstdc++::'
23
24
25 def get_bool_type():
26 return gdb.lookup_type('bool')
27
28 def get_std_size_type():
29 return gdb.lookup_type('std::size_t')
30
31 def is_specialization_of(x, template_name):
32 """
33 Test whether a type is a specialization of the named class template.
34 The type can be specified as a string or a gdb.Type object.
35 The template should be the name of a class template as a string,
36 without any 'std' qualification.
37 """
38 if isinstance(x, gdb.Type):
39 x = x.tag
40 return re.match(r'^std::(__\d::|__debug::)?%s<.*>$' % template_name, x) is not None
41
42 class LibStdCxxXMethod(gdb.xmethod.XMethod):
43 def __init__(self, name, worker_class):
44 gdb.xmethod.XMethod.__init__(self, name)
45 self.worker_class = worker_class
46
47 # Xmethods for std::array
48
49
50 class ArrayWorkerBase(gdb.xmethod.XMethodWorker):
51 def __init__(self, val_type, size):
52 self._val_type = val_type
53 self._size = size
54
55 def null_value(self):
56 nullptr = gdb.parse_and_eval('(void *) 0')
57 return nullptr.cast(self._val_type.pointer()).dereference()
58
59
60 class ArraySizeWorker(ArrayWorkerBase):
61 def __init__(self, val_type, size):
62 ArrayWorkerBase.__init__(self, val_type, size)
63
64 def get_arg_types(self):
65 return None
66
67 def get_result_type(self, obj):
68 return get_std_size_type()
69
70 def __call__(self, obj):
71 return self._size
72
73
74 class ArrayEmptyWorker(ArrayWorkerBase):
75 def __init__(self, val_type, size):
76 ArrayWorkerBase.__init__(self, val_type, size)
77
78 def get_arg_types(self):
79 return None
80
81 def get_result_type(self, obj):
82 return get_bool_type()
83
84 def __call__(self, obj):
85 return (int(self._size) == 0)
86
87
88 class ArrayFrontWorker(ArrayWorkerBase):
89 def __init__(self, val_type, size):
90 ArrayWorkerBase.__init__(self, val_type, size)
91
92 def get_arg_types(self):
93 return None
94
95 def get_result_type(self, obj):
96 return self._val_type
97
98 def __call__(self, obj):
99 if int(self._size) > 0:
100 return obj['_M_elems'][0]
101 else:
102 return self.null_value()
103
104
105 class ArrayBackWorker(ArrayWorkerBase):
106 def __init__(self, val_type, size):
107 ArrayWorkerBase.__init__(self, val_type, size)
108
109 def get_arg_types(self):
110 return None
111
112 def get_result_type(self, obj):
113 return self._val_type
114
115 def __call__(self, obj):
116 if int(self._size) > 0:
117 return obj['_M_elems'][self._size - 1]
118 else:
119 return self.null_value()
120
121
122 class ArrayAtWorker(ArrayWorkerBase):
123 def __init__(self, val_type, size):
124 ArrayWorkerBase.__init__(self, val_type, size)
125
126 def get_arg_types(self):
127 return get_std_size_type()
128
129 def get_result_type(self, obj, index):
130 return self._val_type
131
132 def __call__(self, obj, index):
133 if int(index) >= int(self._size):
134 raise IndexError('Array index "%d" should not be >= %d.' %
135 ((int(index), self._size)))
136 return obj['_M_elems'][index]
137
138
139 class ArraySubscriptWorker(ArrayWorkerBase):
140 def __init__(self, val_type, size):
141 ArrayWorkerBase.__init__(self, val_type, size)
142
143 def get_arg_types(self):
144 return get_std_size_type()
145
146 def get_result_type(self, obj, index):
147 return self._val_type
148
149 def __call__(self, obj, index):
150 if int(self._size) > 0:
151 return obj['_M_elems'][index]
152 else:
153 return self.null_value()
154
155
156 class ArrayMethodsMatcher(gdb.xmethod.XMethodMatcher):
157 def __init__(self):
158 gdb.xmethod.XMethodMatcher.__init__(self,
159 matcher_name_prefix + 'array')
160 self._method_dict = {
161 'size': LibStdCxxXMethod('size', ArraySizeWorker),
162 'empty': LibStdCxxXMethod('empty', ArrayEmptyWorker),
163 'front': LibStdCxxXMethod('front', ArrayFrontWorker),
164 'back': LibStdCxxXMethod('back', ArrayBackWorker),
165 'at': LibStdCxxXMethod('at', ArrayAtWorker),
166 'operator[]': LibStdCxxXMethod('operator[]', ArraySubscriptWorker),
167 }
168 self.methods = [self._method_dict[m] for m in self._method_dict]
169
170 def match(self, class_type, method_name):
171 if not is_specialization_of(class_type, 'array'):
172 return None
173 method = self._method_dict.get(method_name)
174 if method is None or not method.enabled:
175 return None
176 try:
177 value_type = class_type.template_argument(0)
178 size = class_type.template_argument(1)
179 except:
180 return None
181 return method.worker_class(value_type, size)
182
183
184 # Xmethods for std::deque
185
186
187 class DequeWorkerBase(gdb.xmethod.XMethodWorker):
188 def __init__(self, val_type):
189 self._val_type = val_type
190 self._bufsize = 512 // val_type.sizeof or 1
191
192 def size(self, obj):
193 start = obj['_M_impl']['_M_start']
194 finish = obj['_M_impl']['_M_finish']
195 if start['_M_cur'] == finish['_M_cur']:
196 return 0
197 return (self._bufsize
198 * (finish['_M_node'] - start['_M_node'] - 1)
199 + (finish['_M_cur'] - finish['_M_first'])
200 + (start['_M_last'] - start['_M_cur']))
201
202 def index(self, obj, idx):
203 start = obj['_M_impl']['_M_start']
204 first_node_size = start['_M_last'] - start['_M_cur']
205 if idx < first_node_size:
206 return start['_M_cur'][idx]
207 idx = idx - first_node_size
208 index_node = start['_M_node'][1 + int(idx) // self._bufsize]
209 return index_node[idx % self._bufsize]
210
211
212 class DequeEmptyWorker(DequeWorkerBase):
213 def get_arg_types(self):
214 return None
215
216 def get_result_type(self, obj):
217 return get_bool_type()
218
219 def __call__(self, obj):
220 return (obj['_M_impl']['_M_start']['_M_cur'] ==
221 obj['_M_impl']['_M_finish']['_M_cur'])
222
223
224 class DequeSizeWorker(DequeWorkerBase):
225 def get_arg_types(self):
226 return None
227
228 def get_result_type(self, obj):
229 return get_std_size_type()
230
231 def __call__(self, obj):
232 return self.size(obj)
233
234
235 class DequeFrontWorker(DequeWorkerBase):
236 def get_arg_types(self):
237 return None
238
239 def get_result_type(self, obj):
240 return self._val_type
241
242 def __call__(self, obj):
243 return obj['_M_impl']['_M_start']['_M_cur'][0]
244
245
246 class DequeBackWorker(DequeWorkerBase):
247 def get_arg_types(self):
248 return None
249
250 def get_result_type(self, obj):
251 return self._val_type
252
253 def __call__(self, obj):
254 if (obj['_M_impl']['_M_finish']['_M_cur'] ==
255 obj['_M_impl']['_M_finish']['_M_first']):
256 prev_node = obj['_M_impl']['_M_finish']['_M_node'] - 1
257 return prev_node[0][self._bufsize - 1]
258 else:
259 return obj['_M_impl']['_M_finish']['_M_cur'][-1]
260
261
262 class DequeSubscriptWorker(DequeWorkerBase):
263 def get_arg_types(self):
264 return get_std_size_type()
265
266 def get_result_type(self, obj, subscript):
267 return self._val_type
268
269 def __call__(self, obj, subscript):
270 return self.index(obj, subscript)
271
272
273 class DequeAtWorker(DequeWorkerBase):
274 def get_arg_types(self):
275 return get_std_size_type()
276
277 def get_result_type(self, obj, index):
278 return self._val_type
279
280 def __call__(self, obj, index):
281 deque_size = int(self.size(obj))
282 if int(index) >= deque_size:
283 raise IndexError('Deque index "%d" should not be >= %d.' %
284 (int(index), deque_size))
285 else:
286 return self.index(obj, index)
287
288
289 class DequeMethodsMatcher(gdb.xmethod.XMethodMatcher):
290 def __init__(self):
291 gdb.xmethod.XMethodMatcher.__init__(self,
292 matcher_name_prefix + 'deque')
293 self._method_dict = {
294 'empty': LibStdCxxXMethod('empty', DequeEmptyWorker),
295 'size': LibStdCxxXMethod('size', DequeSizeWorker),
296 'front': LibStdCxxXMethod('front', DequeFrontWorker),
297 'back': LibStdCxxXMethod('back', DequeBackWorker),
298 'operator[]': LibStdCxxXMethod('operator[]', DequeSubscriptWorker),
299 'at': LibStdCxxXMethod('at', DequeAtWorker)
300 }
301 self.methods = [self._method_dict[m] for m in self._method_dict]
302
303 def match(self, class_type, method_name):
304 if not is_specialization_of(class_type, 'deque'):
305 return None
306 method = self._method_dict.get(method_name)
307 if method is None or not method.enabled:
308 return None
309 return method.worker_class(class_type.template_argument(0))
310
311 # Xmethods for std::forward_list
312
313
314 class ForwardListWorkerBase(gdb.xmethod.XMethodMatcher):
315 def __init__(self, val_type, node_type):
316 self._val_type = val_type
317 self._node_type = node_type
318
319 def get_arg_types(self):
320 return None
321
322
323 class ForwardListEmptyWorker(ForwardListWorkerBase):
324 def get_result_type(self, obj):
325 return get_bool_type()
326
327 def __call__(self, obj):
328 return obj['_M_impl']['_M_head']['_M_next'] == 0
329
330
331 class ForwardListFrontWorker(ForwardListWorkerBase):
332 def get_result_type(self, obj):
333 return self._val_type
334
335 def __call__(self, obj):
336 node = obj['_M_impl']['_M_head']['_M_next'].cast(self._node_type)
337 val_address = node['_M_storage']['_M_storage'].address
338 return val_address.cast(self._val_type.pointer()).dereference()
339
340
341 class ForwardListMethodsMatcher(gdb.xmethod.XMethodMatcher):
342 def __init__(self):
343 matcher_name = matcher_name_prefix + 'forward_list'
344 gdb.xmethod.XMethodMatcher.__init__(self, matcher_name)
345 self._method_dict = {
346 'empty': LibStdCxxXMethod('empty', ForwardListEmptyWorker),
347 'front': LibStdCxxXMethod('front', ForwardListFrontWorker)
348 }
349 self.methods = [self._method_dict[m] for m in self._method_dict]
350
351 def match(self, class_type, method_name):
352 if not is_specialization_of(class_type, 'forward_list'):
353 return None
354 method = self._method_dict.get(method_name)
355 if method is None or not method.enabled:
356 return None
357 val_type = class_type.template_argument(0)
358 node_type = gdb.lookup_type(str(class_type) + '::_Node').pointer()
359 return method.worker_class(val_type, node_type)
360
361 # Xmethods for std::list
362
363
364 class ListWorkerBase(gdb.xmethod.XMethodWorker):
365 def __init__(self, val_type, node_type):
366 self._val_type = val_type
367 self._node_type = node_type
368
369 def get_arg_types(self):
370 return None
371
372 def get_value_from_node(self, node):
373 node = node.dereference()
374 if node.type.fields()[1].name == '_M_data':
375 # C++03 implementation, node contains the value as a member
376 return node['_M_data']
377 # C++11 implementation, node stores value in __aligned_membuf
378 addr = node['_M_storage'].address
379 return addr.cast(self._val_type.pointer()).dereference()
380
381
382 class ListEmptyWorker(ListWorkerBase):
383 def get_result_type(self, obj):
384 return get_bool_type()
385
386 def __call__(self, obj):
387 base_node = obj['_M_impl']['_M_node']
388 if base_node['_M_next'] == base_node.address:
389 return True
390 else:
391 return False
392
393
394 class ListSizeWorker(ListWorkerBase):
395 def get_result_type(self, obj):
396 return get_std_size_type()
397
398 def __call__(self, obj):
399 begin_node = obj['_M_impl']['_M_node']['_M_next']
400 end_node = obj['_M_impl']['_M_node'].address
401 size = 0
402 while begin_node != end_node:
403 begin_node = begin_node['_M_next']
404 size += 1
405 return size
406
407
408 class ListFrontWorker(ListWorkerBase):
409 def get_result_type(self, obj):
410 return self._val_type
411
412 def __call__(self, obj):
413 node = obj['_M_impl']['_M_node']['_M_next'].cast(self._node_type)
414 return self.get_value_from_node(node)
415
416
417 class ListBackWorker(ListWorkerBase):
418 def get_result_type(self, obj):
419 return self._val_type
420
421 def __call__(self, obj):
422 prev_node = obj['_M_impl']['_M_node']['_M_prev'].cast(self._node_type)
423 return self.get_value_from_node(prev_node)
424
425
426 class ListMethodsMatcher(gdb.xmethod.XMethodMatcher):
427 def __init__(self):
428 gdb.xmethod.XMethodMatcher.__init__(self,
429 matcher_name_prefix + 'list')
430 self._method_dict = {
431 'empty': LibStdCxxXMethod('empty', ListEmptyWorker),
432 'size': LibStdCxxXMethod('size', ListSizeWorker),
433 'front': LibStdCxxXMethod('front', ListFrontWorker),
434 'back': LibStdCxxXMethod('back', ListBackWorker)
435 }
436 self.methods = [self._method_dict[m] for m in self._method_dict]
437
438 def match(self, class_type, method_name):
439 if not is_specialization_of(class_type, '(__cxx11::)?list'):
440 return None
441 method = self._method_dict.get(method_name)
442 if method is None or not method.enabled:
443 return None
444 val_type = class_type.template_argument(0)
445 node_type = gdb.lookup_type(str(class_type) + '::_Node_ptr')
446 return method.worker_class(val_type, node_type)
447
448 # Xmethods for std::vector
449
450
451 class VectorWorkerBase(gdb.xmethod.XMethodWorker):
452 def __init__(self, val_type):
453 self._val_type = val_type
454
455 def size(self, obj):
456 if self._val_type.code == gdb.TYPE_CODE_BOOL:
457 start = obj['_M_impl']['_M_start']['_M_p']
458 finish = obj['_M_impl']['_M_finish']['_M_p']
459 finish_offset = obj['_M_impl']['_M_finish']['_M_offset']
460 bit_size = start.dereference().type.sizeof * 8
461 return (finish - start) * bit_size + finish_offset
462 else:
463 return obj['_M_impl']['_M_finish'] - obj['_M_impl']['_M_start']
464
465 def get(self, obj, index):
466 if self._val_type.code == gdb.TYPE_CODE_BOOL:
467 start = obj['_M_impl']['_M_start']['_M_p']
468 bit_size = start.dereference().type.sizeof * 8
469 valp = start + index // bit_size
470 offset = index % bit_size
471 return (valp.dereference() & (1 << offset)) > 0
472 else:
473 return obj['_M_impl']['_M_start'][index]
474
475
476 class VectorEmptyWorker(VectorWorkerBase):
477 def get_arg_types(self):
478 return None
479
480 def get_result_type(self, obj):
481 return get_bool_type()
482
483 def __call__(self, obj):
484 return int(self.size(obj)) == 0
485
486
487 class VectorSizeWorker(VectorWorkerBase):
488 def get_arg_types(self):
489 return None
490
491 def get_result_type(self, obj):
492 return get_std_size_type()
493
494 def __call__(self, obj):
495 return self.size(obj)
496
497
498 class VectorFrontWorker(VectorWorkerBase):
499 def get_arg_types(self):
500 return None
501
502 def get_result_type(self, obj):
503 return self._val_type
504
505 def __call__(self, obj):
506 return self.get(obj, 0)
507
508
509 class VectorBackWorker(VectorWorkerBase):
510 def get_arg_types(self):
511 return None
512
513 def get_result_type(self, obj):
514 return self._val_type
515
516 def __call__(self, obj):
517 return self.get(obj, int(self.size(obj)) - 1)
518
519
520 class VectorAtWorker(VectorWorkerBase):
521 def get_arg_types(self):
522 return get_std_size_type()
523
524 def get_result_type(self, obj, index):
525 return self._val_type
526
527 def __call__(self, obj, index):
528 size = int(self.size(obj))
529 if int(index) >= size:
530 raise IndexError('Vector index "%d" should not be >= %d.' %
531 ((int(index), size)))
532 return self.get(obj, int(index))
533
534
535 class VectorSubscriptWorker(VectorWorkerBase):
536 def get_arg_types(self):
537 return get_std_size_type()
538
539 def get_result_type(self, obj, subscript):
540 return self._val_type
541
542 def __call__(self, obj, subscript):
543 return self.get(obj, int(subscript))
544
545
546 class VectorMethodsMatcher(gdb.xmethod.XMethodMatcher):
547 def __init__(self):
548 gdb.xmethod.XMethodMatcher.__init__(self,
549 matcher_name_prefix + 'vector')
550 self._method_dict = {
551 'size': LibStdCxxXMethod('size', VectorSizeWorker),
552 'empty': LibStdCxxXMethod('empty', VectorEmptyWorker),
553 'front': LibStdCxxXMethod('front', VectorFrontWorker),
554 'back': LibStdCxxXMethod('back', VectorBackWorker),
555 'at': LibStdCxxXMethod('at', VectorAtWorker),
556 'operator[]': LibStdCxxXMethod('operator[]',
557 VectorSubscriptWorker),
558 }
559 self.methods = [self._method_dict[m] for m in self._method_dict]
560
561 def match(self, class_type, method_name):
562 if not is_specialization_of(class_type, 'vector'):
563 return None
564 method = self._method_dict.get(method_name)
565 if method is None or not method.enabled:
566 return None
567 return method.worker_class(class_type.template_argument(0))
568
569 # Xmethods for associative containers
570
571
572 class AssociativeContainerWorkerBase(gdb.xmethod.XMethodWorker):
573 def __init__(self, unordered):
574 self._unordered = unordered
575
576 def node_count(self, obj):
577 if self._unordered:
578 return obj['_M_h']['_M_element_count']
579 else:
580 return obj['_M_t']['_M_impl']['_M_node_count']
581
582 def get_arg_types(self):
583 return None
584
585
586 class AssociativeContainerEmptyWorker(AssociativeContainerWorkerBase):
587 def get_result_type(self, obj):
588 return get_bool_type()
589
590 def __call__(self, obj):
591 return int(self.node_count(obj)) == 0
592
593
594 class AssociativeContainerSizeWorker(AssociativeContainerWorkerBase):
595 def get_result_type(self, obj):
596 return get_std_size_type()
597
598 def __call__(self, obj):
599 return self.node_count(obj)
600
601
602 class AssociativeContainerMethodsMatcher(gdb.xmethod.XMethodMatcher):
603 def __init__(self, name):
604 gdb.xmethod.XMethodMatcher.__init__(self,
605 matcher_name_prefix + name)
606 self._name = name
607 self._method_dict = {
608 'size': LibStdCxxXMethod('size', AssociativeContainerSizeWorker),
609 'empty': LibStdCxxXMethod('empty',
610 AssociativeContainerEmptyWorker),
611 }
612 self.methods = [self._method_dict[m] for m in self._method_dict]
613
614 def match(self, class_type, method_name):
615 if not is_specialization_of(class_type, self._name):
616 return None
617 method = self._method_dict.get(method_name)
618 if method is None or not method.enabled:
619 return None
620 unordered = 'unordered' in self._name
621 return method.worker_class(unordered)
622
623 # Xmethods for std::unique_ptr
624
625
626 class UniquePtrGetWorker(gdb.xmethod.XMethodWorker):
627 """
628 Implement std::unique_ptr<T>::get() and std::unique_ptr<T>::operator->().
629 """
630
631 def __init__(self, elem_type):
632 self._is_array = elem_type.code == gdb.TYPE_CODE_ARRAY
633 if self._is_array:
634 self._elem_type = elem_type.target()
635 else:
636 self._elem_type = elem_type
637
638 def get_arg_types(self):
639 return None
640
641 def get_result_type(self, obj):
642 return self._elem_type.pointer()
643
644 def _supports(self, method_name):
645 # operator-> is not supported for unique_ptr<T[]>
646 return method_name == 'get' or not self._is_array
647
648 def __call__(self, obj):
649 impl_type = obj.dereference().type.fields()[0].type.tag
650 # Check for new implementations first:
651 if is_specialization_of(impl_type, '__uniq_ptr_(data|impl)'):
652 tuple_member = obj['_M_t']['_M_t']
653 elif is_specialization_of(impl_type, 'tuple'):
654 tuple_member = obj['_M_t']
655 else:
656 return None
657 tuple_impl_type = tuple_member.type.fields()[0].type # _Tuple_impl
658 tuple_head_type = tuple_impl_type.fields()[1].type # _Head_base
659 head_field = tuple_head_type.fields()[0]
660 if head_field.name == '_M_head_impl':
661 return tuple_member.cast(tuple_head_type)['_M_head_impl']
662 elif head_field.is_base_class:
663 return tuple_member.cast(head_field.type)
664 else:
665 return None
666
667
668 class UniquePtrDerefWorker(UniquePtrGetWorker):
669 """Implement std::unique_ptr<T>::operator*()."""
670
671 def __init__(self, elem_type):
672 UniquePtrGetWorker.__init__(self, elem_type)
673
674 def get_result_type(self, obj):
675 return self._elem_type
676
677 def _supports(self, method_name):
678 # operator* is not supported for unique_ptr<T[]>
679 return not self._is_array
680
681 def __call__(self, obj):
682 return UniquePtrGetWorker.__call__(self, obj).dereference()
683
684
685 class UniquePtrSubscriptWorker(UniquePtrGetWorker):
686 """Implement std::unique_ptr<T>::operator[](size_t)."""
687
688 def __init__(self, elem_type):
689 UniquePtrGetWorker.__init__(self, elem_type)
690
691 def get_arg_types(self):
692 return get_std_size_type()
693
694 def get_result_type(self, obj, index):
695 return self._elem_type
696
697 def _supports(self, method_name):
698 # operator[] is only supported for unique_ptr<T[]>
699 return self._is_array
700
701 def __call__(self, obj, index):
702 return UniquePtrGetWorker.__call__(self, obj)[index]
703
704
705 class UniquePtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
706 def __init__(self):
707 gdb.xmethod.XMethodMatcher.__init__(self,
708 matcher_name_prefix + 'unique_ptr')
709 self._method_dict = {
710 'get': LibStdCxxXMethod('get', UniquePtrGetWorker),
711 'operator->': LibStdCxxXMethod('operator->', UniquePtrGetWorker),
712 'operator*': LibStdCxxXMethod('operator*', UniquePtrDerefWorker),
713 'operator[]': LibStdCxxXMethod('operator[]', UniquePtrSubscriptWorker),
714 }
715 self.methods = [self._method_dict[m] for m in self._method_dict]
716
717 def match(self, class_type, method_name):
718 if not is_specialization_of(class_type, 'unique_ptr'):
719 return None
720 method = self._method_dict.get(method_name)
721 if method is None or not method.enabled:
722 return None
723 worker = method.worker_class(class_type.template_argument(0))
724 if worker._supports(method_name):
725 return worker
726 return None
727
728 # Xmethods for std::shared_ptr
729
730
731 class SharedPtrGetWorker(gdb.xmethod.XMethodWorker):
732 """
733 Implements std::shared_ptr<T>::get() and std::shared_ptr<T>::operator->().
734 """
735
736 def __init__(self, elem_type):
737 self._is_array = elem_type.code == gdb.TYPE_CODE_ARRAY
738 if self._is_array:
739 self._elem_type = elem_type.target()
740 else:
741 self._elem_type = elem_type
742
743 def get_arg_types(self):
744 return None
745
746 def get_result_type(self, obj):
747 return self._elem_type.pointer()
748
749 def _supports(self, method_name):
750 # operator-> is not supported for shared_ptr<T[]>
751 return method_name == 'get' or not self._is_array
752
753 def __call__(self, obj):
754 return obj['_M_ptr']
755
756
757 class SharedPtrDerefWorker(SharedPtrGetWorker):
758 """Implement std::shared_ptr<T>::operator*()."""
759
760 def __init__(self, elem_type):
761 SharedPtrGetWorker.__init__(self, elem_type)
762
763 def get_result_type(self, obj):
764 return self._elem_type
765
766 def _supports(self, method_name):
767 # operator* is not supported for shared_ptr<T[]>
768 return not self._is_array
769
770 def __call__(self, obj):
771 return SharedPtrGetWorker.__call__(self, obj).dereference()
772
773
774 class SharedPtrSubscriptWorker(SharedPtrGetWorker):
775 """Implement std::shared_ptr<T>::operator[](size_t)."""
776
777 def __init__(self, elem_type):
778 SharedPtrGetWorker.__init__(self, elem_type)
779
780 def get_arg_types(self):
781 return get_std_size_type()
782
783 def get_result_type(self, obj, index):
784 return self._elem_type
785
786 def _supports(self, method_name):
787 # operator[] is only supported for shared_ptr<T[]>
788 return self._is_array
789
790 def __call__(self, obj, index):
791 # Check bounds if _elem_type is an array of known bound
792 m = re.match(r'.*\[(\d+)]$', str(self._elem_type))
793 if m and index >= int(m.group(1)):
794 raise IndexError('shared_ptr<%s> index "%d" should not be >= %d.' %
795 (self._elem_type, int(index), int(m.group(1))))
796 return SharedPtrGetWorker.__call__(self, obj)[index]
797
798
799 class SharedPtrUseCountWorker(gdb.xmethod.XMethodWorker):
800 """Implement std::shared_ptr<T>::use_count()."""
801
802 def __init__(self, elem_type):
803 pass
804
805 def get_arg_types(self):
806 return None
807
808 def get_result_type(self, obj):
809 return gdb.lookup_type('long')
810
811 def _supports(self, method_name):
812 return True
813
814 def __call__(self, obj):
815 refcounts = obj['_M_refcount']['_M_pi']
816 return refcounts['_M_use_count'] if refcounts else 0
817
818
819 class SharedPtrUniqueWorker(SharedPtrUseCountWorker):
820 """Implement std::shared_ptr<T>::unique()."""
821
822 def __init__(self, elem_type):
823 SharedPtrUseCountWorker.__init__(self, elem_type)
824
825 def get_result_type(self, obj):
826 return gdb.lookup_type('bool')
827
828 def __call__(self, obj):
829 return SharedPtrUseCountWorker.__call__(self, obj) == 1
830
831
832 class SharedPtrMethodsMatcher(gdb.xmethod.XMethodMatcher):
833 def __init__(self):
834 gdb.xmethod.XMethodMatcher.__init__(self,
835 matcher_name_prefix + 'shared_ptr')
836 self._method_dict = {
837 'get': LibStdCxxXMethod('get', SharedPtrGetWorker),
838 'operator->': LibStdCxxXMethod('operator->', SharedPtrGetWorker),
839 'operator*': LibStdCxxXMethod('operator*', SharedPtrDerefWorker),
840 'operator[]': LibStdCxxXMethod('operator[]', SharedPtrSubscriptWorker),
841 'use_count': LibStdCxxXMethod('use_count', SharedPtrUseCountWorker),
842 'unique': LibStdCxxXMethod('unique', SharedPtrUniqueWorker),
843 }
844 self.methods = [self._method_dict[m] for m in self._method_dict]
845
846 def match(self, class_type, method_name):
847 if not is_specialization_of(class_type, 'shared_ptr'):
848 return None
849 method = self._method_dict.get(method_name)
850 if method is None or not method.enabled:
851 return None
852 worker = method.worker_class(class_type.template_argument(0))
853 if worker._supports(method_name):
854 return worker
855 return None
856
857
858 def register_libstdcxx_xmethods(locus):
859 gdb.xmethod.register_xmethod_matcher(locus, ArrayMethodsMatcher())
860 gdb.xmethod.register_xmethod_matcher(locus, ForwardListMethodsMatcher())
861 gdb.xmethod.register_xmethod_matcher(locus, DequeMethodsMatcher())
862 gdb.xmethod.register_xmethod_matcher(locus, ListMethodsMatcher())
863 gdb.xmethod.register_xmethod_matcher(locus, VectorMethodsMatcher())
864 gdb.xmethod.register_xmethod_matcher(
865 locus, AssociativeContainerMethodsMatcher('set'))
866 gdb.xmethod.register_xmethod_matcher(
867 locus, AssociativeContainerMethodsMatcher('map'))
868 gdb.xmethod.register_xmethod_matcher(
869 locus, AssociativeContainerMethodsMatcher('multiset'))
870 gdb.xmethod.register_xmethod_matcher(
871 locus, AssociativeContainerMethodsMatcher('multimap'))
872 gdb.xmethod.register_xmethod_matcher(
873 locus, AssociativeContainerMethodsMatcher('unordered_set'))
874 gdb.xmethod.register_xmethod_matcher(
875 locus, AssociativeContainerMethodsMatcher('unordered_map'))
876 gdb.xmethod.register_xmethod_matcher(
877 locus, AssociativeContainerMethodsMatcher('unordered_multiset'))
878 gdb.xmethod.register_xmethod_matcher(
879 locus, AssociativeContainerMethodsMatcher('unordered_multimap'))
880 gdb.xmethod.register_xmethod_matcher(locus, UniquePtrMethodsMatcher())
881 gdb.xmethod.register_xmethod_matcher(locus, SharedPtrMethodsMatcher())