Kaydet (Commit) 101e4494 authored tarafından David Tardon's avatar David Tardon

add pretty printers for boost

üst 464a3a27
......@@ -39,4 +39,10 @@ if gdb.current_objfile():
from %MODULE% import register_pretty_printers
register_pretty_printers(gdb.current_objfile())
try:
import boost
boost.register_pretty_printers(gdb)
except:
pass
# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab:
# -*- 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_printer(obj)
boost.ptr_container.register_pretty_printer(obj)
boost.smart_ptr.register_pretty_printer(obj)
boost.unordered.register_pretty_printer(obj)
# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab:
# -*- 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
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(object):
'''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:
# -*- 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:
# -*- 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
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(object):
def __init__(self, sequence, type):
self.impl = iter(sequence)
self.type = type.pointer()
def __iter__(self):
return self
def next(self):
(index, value) = self.impl.next()
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(object):
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) = self.impl.next()
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(object):
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 = self.impl.next()
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(object):
def __init__(self, impl, value_type):
self.impl = impl
self.value_type = value_type
def __iter__(self):
return self
def next(self):
return ("", self.impl.next()[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:
# -*- 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']:
return "%s %s" % (self.typename, self.value['px'].dereference())
else:
return "empty %s" % (self.typename,)
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', SmartPtrPrinter)
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:
# -*- 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
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(object):
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 = self.impl.next()
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(object):
def __init__(self, impl):
self.impl = impl
def __iter__(self):
return self
def next(self):
return ("", self.impl.next()[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:
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
# vim:set filetype=python shiftwidth=4 softtabstop=4 expandtab:
# -*- 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:
# -*- 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/>.
from collections import Mapping
import gdb
import re
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('^([\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 self.map.iteritems():
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:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment