diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /solenv/gdb/boost | |
parent | Initial commit. (diff) | |
download | libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'solenv/gdb/boost')
-rw-r--r-- | solenv/gdb/boost/__init__.py | 32 | ||||
-rw-r--r-- | solenv/gdb/boost/lib/__init__.py | 0 | ||||
-rw-r--r-- | solenv/gdb/boost/lib/unordered.py | 129 | ||||
-rw-r--r-- | solenv/gdb/boost/optional.py | 56 | ||||
-rw-r--r-- | solenv/gdb/boost/ptr_container.py | 248 | ||||
-rw-r--r-- | solenv/gdb/boost/smart_ptr.py | 76 | ||||
-rw-r--r-- | solenv/gdb/boost/unordered.py | 112 | ||||
-rw-r--r-- | solenv/gdb/boost/util/__init__.py | 3 | ||||
-rw-r--r-- | solenv/gdb/boost/util/compatibility.py | 32 | ||||
-rw-r--r-- | solenv/gdb/boost/util/printing.py | 137 |
10 files changed, 825 insertions, 0 deletions
diff --git a/solenv/gdb/boost/__init__.py b/solenv/gdb/boost/__init__.py new file mode 100644 index 000000000..353ee3b09 --- /dev/null +++ b/solenv/gdb/boost/__init__.py @@ -0,0 +1,32 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# GDB pretty printers for Boost. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import boost.optional +import boost.ptr_container +import boost.smart_ptr +import boost.unordered + +def register_pretty_printers(obj): + boost.optional.register_pretty_printers(obj) + boost.ptr_container.register_pretty_printers(obj) + boost.smart_ptr.register_pretty_printers(obj) + boost.unordered.register_pretty_printers(obj) + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/lib/__init__.py b/solenv/gdb/boost/lib/__init__.py new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/solenv/gdb/boost/lib/__init__.py diff --git a/solenv/gdb/boost/lib/unordered.py b/solenv/gdb/boost/lib/unordered.py new file mode 100644 index 000000000..ee58d0481 --- /dev/null +++ b/solenv/gdb/boost/lib/unordered.py @@ -0,0 +1,129 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# Helper classes for working with Boost.Unordered. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import gdb +import six + +class Unordered(object): + '''Common representation of Boost.Unordered types''' + + def __init__(self, value, extractor): + self.value = value + self.extractor = extractor + self.node_type = self._node_type() + + def __len__(self): + table = self.value['table_'] + if table['buckets_']: + return int(table['size_']) + else: + return 0 + + def __iter__(self): + table = self.value['table_'] + buckets = table['buckets_'] + if buckets: + first = table['cached_begin_bucket_'] + last = buckets + table['bucket_count_'] + else: + first = last = None + return self._iterator(first, last, self.node_type, self.extractor) + + def empty(self): + return not self.value['table_']['buckets_'] + + def _node_type(self): + hash_table = self.value['table_'].type.fields()[0] + assert hash_table.is_base_class + hash_buckets = hash_table.type.fields()[0] + assert hash_buckets.is_base_class + node_type = gdb.lookup_type("%s::node" % hash_buckets.type) + assert node_type != None + return node_type + + class _iterator(six.Iterator): + '''Iterator for Boost.Unordered types''' + + def __init__(self, first_bucket, last_bucket, node_type, extractor): + self.bucket = first_bucket + self.last_bucket = last_bucket + self.node = self.bucket + self.node_type = node_type + self.value_type = self._value_type() + self.extractor = extractor + + def __iter__(self): + return self + + def __next__(self): + if self.node: + self.node = self.node.dereference()['next_'] + + # we finished the current bucket: go on to the next non-empty one + if not self.node: + while not self.node and self.bucket != self.last_bucket: + self.bucket += 1 + self.node = self.bucket.dereference()['next_'] + + # sorry, no node available + if not self.node or self.node == self.bucket: + raise StopIteration() + + mapped = self._value() + return (self.extractor.key(mapped), self.extractor.value(mapped)) + + def _value(self): + assert self.node != self.bucket # bucket node has no value + assert self.node != None + node = self.node.dereference().cast(self.node_type) + return node['data_'].cast(self.value_type) + + def _value_type(self): + value_base = self.node_type.fields()[1] + assert value_base.is_base_class + return value_base.type.template_argument(0) + +class Map(Unordered): + + def __init__(self, value): + super(Map, self).__init__(value, self._extractor()) + + class _extractor(object): + + def key(self, node): + return node['first'] + + def value(self, node): + return node['second'] + +class Set(Unordered): + + def __init__(self, value): + super(Set, self).__init__(value, self._extractor()) + + class _extractor(object): + + def key(self, node): + return None + + def value(self, node): + return node + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/optional.py b/solenv/gdb/boost/optional.py new file mode 100644 index 000000000..08297254e --- /dev/null +++ b/solenv/gdb/boost/optional.py @@ -0,0 +1,56 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# GDB pretty printers for Boost.Optional. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import gdb + +import boost.util.printing as printing + +class OptionalPrinter: + + def __init__(self, typename, value): + self.typename = typename + self.value = value + + def to_string(self): + if self.value['m_initialized']: + data = self.value['m_storage']['dummy_']['data'] + ptr_type = self.value.type.template_argument(0).pointer() + return "%s %s" % (self.typename, data.cast(ptr_type).dereference()) + else: + return "empty " + self.typename + +printer = None + +def build_pretty_printers(): + global printer + + if printer != None: + return + + printer = printing.Printer("boost.optional") + + printer.add('boost::optional', OptionalPrinter) + +def register_pretty_printers(obj): + printing.register_pretty_printer(printer, obj) + +build_pretty_printers() + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/ptr_container.py b/solenv/gdb/boost/ptr_container.py new file mode 100644 index 000000000..425d812f5 --- /dev/null +++ b/solenv/gdb/boost/ptr_container.py @@ -0,0 +1,248 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# GDB pretty printers for Boost.Pointer Container. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import gdb +import six + +from boost.lib.unordered import Map, Set + +import boost.util.printing as printing + +std = None + +class PtrStdPrinterBase(object): + + def __init__(self, typename, value, seq_tag): + self._import_std() + self.typename = typename + self.value = value + + # (try to) init printer of underlying std sequence and get elements + printer = self._get_sequence_printer(seq_tag) + if printer: + seq = value['c_'] + if str(seq.type.strip_typedefs()).startswith('std::__debug::'): + seq_typename = 'std::__debug::%s' % seq_tag + else: + seq_typename = 'std::%s' % seq_tag + self.sequence = list(printer(seq_typename, seq).children()) + else: + self.sequence = None + + def to_string(self): + if self.sequence != None: + length = len(self.sequence) + if length: + return "%s %s" % (self.typename, self.print_size(length)) + else: + return "empty %s" % self.typename + else: + return "opaque %s" % self.typename + + def children(self): + return self._iterator(self.sequence, self.value.type.template_argument(0)) + + class _iterator(six.Iterator): + + def __init__(self, sequence, type): + self.impl = iter(sequence) + self.type = type.pointer() + + def __iter__(self): + return self + + def __next__(self): + (index, value) = six.advance_iterator(self.impl) + return (index, value.cast(self.type).dereference()) + + def _import_std(self): + global std + if not std: + try: + import libstdcxx.v6.printers + std = libstdcxx.v6.printers + except: + pass + + def _get_sequence_printer(self, typename): + if typename == "deque": + return std.StdDequePrinter + if typename == "list": + return std.StdListPrinter + if typename == "map": + return std.StdMapPrinter + if typename == "set": + return std.StdSetPrinter + if typename == "vector": + return std.StdVectorPrinter + +class PtrSequencePrinter(PtrStdPrinterBase): + + def __init__(self, typename, value, seq_tag): + super(PtrSequencePrinter, self).__init__(typename, value, seq_tag) + + def print_size(self, size): + return "of length %s" % size + + def display_hint(self): + return 'array' + +class PtrSetPrinter(PtrStdPrinterBase): + + def __init__(self, typename, value): + super(PtrSetPrinter, self).__init__(typename, value, 'set') + + def print_size(self, size): + return "with %s elements" % size + + def display_hint(self): + return 'array' + +class PtrMapPrinter(PtrStdPrinterBase): + + def __init__(self, typename, value): + super(PtrMapPrinter, self).__init__(typename, value, 'map') + + def children(self): + type = self.value.type + return self._iterator(self.sequence, type.template_argument(0), type.template_argument(1)) + + class _iterator(six.Iterator): + + def __init__(self, sequence, key_type, value_type): + self.impl = iter(sequence) + self.key_type = key_type + self.value_type = value_type.pointer() + self.key = True + + def __iter__(self): + return self + + def __next__(self): + (index, value) = six.advance_iterator(self.impl) + if self.key: + value = value.cast(self.key_type) + else: + value = value.cast(self.value_type).dereference() + self.key = not self.key + return (index, value) + + def display_hint(self): + return 'map' + + def print_size(self, size): + return "with %s elements" % (size / 2) + +class PtrBoostPrinterBase(object): + + def __init__(self, typename, value, container, iterator, value_type): + self.typename = typename + self.impl = container(value['c_']) + self.iterator = iterator + self.value_type = value_type.pointer() + + def to_string(self): + if self.impl.empty(): + return "empty " + self.typename + else: + return "%s with %s elements" % (self.typename, len(self.impl)) + + def children(self): + return self.iterator(iter(self.impl), self.value_type) + +class PtrUnorderedMapPrinter(PtrBoostPrinterBase): + + def __init__(self, typename, value): + super(PtrUnorderedMapPrinter, self).__init__(typename, value, Map, self._iterator, + value.type.template_argument(1)) + + def display_hint(self): + return 'map' + + class _iterator(six.Iterator): + + def __init__(self, impl, value_type): + self.impl = impl + self.step = True + self.value = None + self.value_type = value_type + + def __iter__(self): + return self + + def __next__(self): + if self.step: + self.value = six.advance_iterator(self.impl) + value = self.value[0] + else: + value = self.value[1].cast(self.value_type).dereference() + self.step = not self.step + return ("", value) + +class PtrUnorderedSetPrinter(PtrBoostPrinterBase): + + def __init__(self, typename, value): + super(PtrUnorderedSetPrinter, self).__init__(typename, value, Set, self._iterator, + value.type.template_argument(0)) + + def display_hint(self): + return 'array' + + class _iterator(six.Iterator): + + def __init__(self, impl, value_type): + self.impl = impl + self.value_type = value_type + + def __iter__(self): + return self + + def __next__(self): + return ("", six.advance_iterator(self.impl)[1].cast(self.value_type).dereference()) + +printer = None + +def build_pretty_printers(): + global printer + + if printer != None: + return + + printer = printing.Printer("boost.ptr_container") + + printer.add('boost::ptr_deque', (lambda t, v: PtrSequencePrinter(t, v, "deque"))) + printer.add('boost::ptr_list', (lambda t, v: PtrSequencePrinter(t, v, "list"))) + printer.add('boost::ptr_map', PtrMapPrinter) + printer.add('boost::ptr_multimap', PtrMapPrinter) + printer.add('boost::ptr_multiset', PtrSetPrinter) + printer.add('boost::ptr_set', PtrSetPrinter) + printer.add('boost::ptr_unordered_map', PtrUnorderedMapPrinter) + printer.add('boost::ptr_unordered_multimap', PtrUnorderedMapPrinter) + printer.add('boost::ptr_unordered_multiset', PtrUnorderedSetPrinter) + printer.add('boost::ptr_unordered_set', PtrUnorderedSetPrinter) + printer.add('boost::ptr_vector', (lambda t, v: PtrSequencePrinter(t, v, "vector"))) + +def register_pretty_printers(obj): + printing.register_pretty_printer(printer, obj) + +build_pretty_printers() + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/smart_ptr.py b/solenv/gdb/boost/smart_ptr.py new file mode 100644 index 000000000..60d88278a --- /dev/null +++ b/solenv/gdb/boost/smart_ptr.py @@ -0,0 +1,76 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# GDB pretty printers for Boost.Smart Ptr. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import gdb + +import boost.util.printing as printing + + +class SmartPtrPrinter: + """Prints smart pointers based on Boost.SmartPtr""" + + def __init__(self, typename, value): + self.typename = typename + self.value = value + + def to_string(self): + if self.value['px']: + print_object = gdb.parameter('print object') + value = self.value['px'].dereference() + if print_object: + dynamic_type = self.value['px'].dynamic_type + value = self.value['px'].cast(dynamic_type).dereference() + return "%s %s" % (self.typename, value) + else: + return "empty %s" % (self.typename) + +class WeakPtrPrinter: + """Prints boost::weak_ptr instances""" + + def __init__(self, typename, value): + self.typename = typename + self.value = value + + def to_string(self): + value = self.value['px'] + return "%s %s" % (self.typename, value) + +printer = None + +def build_pretty_printers(): + global printer + + if printer != None: + return + + printer = printing.Printer("boost.smart_ptr") + + printer.add('boost::shared_ptr', SmartPtrPrinter) + # printer.add('boost::shared_array', SmartPtrPrinter) + printer.add('boost::weak_ptr', WeakPtrPrinter) + printer.add('boost::scoped_ptr', SmartPtrPrinter) + # printer.add('boost::scoped_array', SmartPtrPrinter) + +def register_pretty_printers(obj): + printing.register_pretty_printer(printer, obj) + +build_pretty_printers() + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/unordered.py b/solenv/gdb/boost/unordered.py new file mode 100644 index 000000000..2c5672185 --- /dev/null +++ b/solenv/gdb/boost/unordered.py @@ -0,0 +1,112 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# GDB pretty printers for Boost.Unordered. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import gdb +import six + +from boost.lib.unordered import Map, Set + +import boost.util.printing as printing + +class PrinterBase(object): + '''Contains common functionality for printing Boost.Unordered types''' + + def __init__(self, typename, value, container, iterator): + self.typename = typename + self.impl = container(value) + self.iterator = iterator + + def to_string(self): + if self.impl.empty(): + return "empty " + self.typename + else: + return "%s with %s elements" % (self.typename, len(self.impl)) + + def children(self): + return self.iterator(iter(self.impl)) + +class UnorderedMapPrinter(PrinterBase): + + def __init__(self, typename, value): + super(UnorderedMapPrinter, self).__init__(typename, value, Map, self._iterator) + + def display_hint(self): + return 'map' + + class _iterator(six.Iterator): + + def __init__(self, impl): + self.impl = impl + self.value = None + self.step = True + + def __iter__(self): + return self + + def __next__(self): + if self.step: + self.value = six.advance_iterator(self.impl) + value = self.value[0] + else: + value = self.value[1] + self.step = not self.step + return ("", value) + +class UnorderedSetPrinter(PrinterBase): + + def __init__(self, typename, value): + super(UnorderedSetPrinter, self).__init__(typename, value, Set, self._iterator) + + def display_hint(self): + return 'array' + + class _iterator(six.Iterator): + + def __init__(self, impl): + self.impl = impl + + def __iter__(self): + return self + + def __next__(self): + return ("", six.advance_iterator(self.impl)[1]) + +printer = None + +def build_pretty_printers(): + global printer + + if printer != None: + return + + printer = printing.Printer("boost.unordered") + + printer.add('boost::unordered_map', UnorderedMapPrinter) + printer.add('boost::unordered_multimap', UnorderedMapPrinter) + printer.add('boost::unordered_multiset', UnorderedSetPrinter) + printer.add('boost::unordered_set', UnorderedSetPrinter) + +def register_pretty_printers(obj): + printing.register_pretty_printer(printer, obj) + +build_pretty_printers() + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/util/__init__.py b/solenv/gdb/boost/util/__init__.py new file mode 100644 index 000000000..e7992304d --- /dev/null +++ b/solenv/gdb/boost/util/__init__.py @@ -0,0 +1,3 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/util/compatibility.py b/solenv/gdb/boost/util/compatibility.py new file mode 100644 index 000000000..e704b1992 --- /dev/null +++ b/solenv/gdb/boost/util/compatibility.py @@ -0,0 +1,32 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# Compatibility with older versions of GDB. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +import gdb + +use_gdb_printing = True +try: + import gdb.printing +except ImportError: + use_gdb_printing = False + +use_lazy_string = hasattr(gdb.Value, 'lazy_string') + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: diff --git a/solenv/gdb/boost/util/printing.py b/solenv/gdb/boost/util/printing.py new file mode 100644 index 000000000..22bf29f80 --- /dev/null +++ b/solenv/gdb/boost/util/printing.py @@ -0,0 +1,137 @@ +# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- +# Printer interface adaptor. +# +# Copyright (C) 2012 Red Hat, Inc., David Tardon <dtardon@redhat.com> +# +# This file is part of boost-gdb-printers. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +try: + from collections.abc import Mapping +except: + from collections import Mapping +import gdb +import re +import six + +from boost.util.compatibility import use_gdb_printing + +class SimplePrinter(object): + + def __init__(self, name, function): + self.name = name + self.function = function + self.enabled = True + + def invoke(self, val): + if not self.enabled: + return None + return self.function(self.name, val) + +class NameLookup(Mapping): + + def __init__(self): + self.map = {} + self.name_regex = re.compile(r'^([\w:]+)(<.*>)?') + + def add(self, name, printer): + self.map[name] = printer + + def __len__(self): + return len(self.map) + + def __getitem__(self, type): + typename = self._basic_type(type) + if typename and typename in self.map: + return self.map[typename] + return None + + def __iter__(self): + return self.map + + def _basic_type(self, type): + basic_type = self.basic_type(type) + if basic_type: + match = self.name_regex.match(basic_type) + if match: + return match.group(1) + return None + + @staticmethod + def basic_type(type): + if type.code == gdb.TYPE_CODE_REF: + type = type.target() + type = type.unqualified().strip_typedefs() + return type.tag + +class FunctionLookup(Mapping): + + def __init__(self): + self.map = {} + + def add(self, test, printer): + self.map[test] = printer + + def __len__(self): + return len(self.map) + + def __getitem__(self, type): + for (test, printer) in six.iteritems(self.map): + if test(type): + return printer + return None + + def __iter__(self): + return self.map + +class Printer(object): + + def __init__(self, name): + self.name = name + self.subprinters = [] + self.name_lookup = NameLookup() + self.func_lookup = FunctionLookup() + self.enabled = True + + def add(self, name, function, lookup = None): + printer = SimplePrinter(name, function) + self.subprinters.append(printer) + if not lookup: + self.name_lookup.add(name, printer) + else: + self.func_lookup.add(lookup, printer) + + + def __call__(self, val): + printer = self.name_lookup[val.type] + if not printer: + printer = self.func_lookup[val.type] + + if printer: + return printer.invoke(val) + return None + +def register_pretty_printer(printer, obj): + '''Registers printer with objfile''' + + if use_gdb_printing: + gdb.printing.register_pretty_printer(obj, printer) + else: + if obj is None: + obj = gdb + obj.pretty_printers.append(printer) + +# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab: |