diff mbox

RFC: Make iterator printers fail more gracefully

Message ID 20161216134601.GF895@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely Dec. 16, 2016, 1:46 p.m. UTC
On 15/12/16 18:29 +0000, Jonathan Wakely wrote:
>On 15/12/16 13:11 -0500, David Malcolm wrote:
>>BTW, is it always a ValueError exception?
>>
>>(I'm a little wary of naked "except:" in Python, as it can catch
>>*anything*, including syntax errors in the try/except-guarded code).
>
>Good point. As far as I know, the gdb.lookup_type method will throw a
>ValueError in the case I'm trying to fix. If it can throw other things
>we can deal with them later by adding other handlers.

One case gets a ValueError from the find_type helper, and one case
gets a gdb.error (which is derived from RuntimeError) because it uses
gdb.lookup_type directly.

Here's a new patch without naked "except:"
diff mbox

Patch

commit 0f63cb5027505c26b2f90aeeb1275f459c3d2fbd
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Dec 16 13:42:48 2016 +0000

    Make iterator printers fail more gracefully
    
    	* python/libstdcxx/v6/printers.py (StdListIteratorPrinter.to_string):
    	Handle exception from failed type lookup and return user-friendly
    	string.
    	(StdRbtreeIteratorPrinter.__init__): Handle exception from failed
    	type lookup.
    	(StdRbtreeIteratorPrinter.to_string): Return user-friendly string.

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 86de1ca..1897154 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -202,10 +202,13 @@  class StdListIteratorPrinter:
     def to_string(self):
         if not self.val['_M_node']:
             return 'non-dereferenceable iterator for std::list'
-        nodetype = find_type(self.val.type, '_Node')
-        nodetype = nodetype.strip_typedefs().pointer()
-        node = self.val['_M_node'].cast(nodetype).dereference()
-        return str(get_value_from_list_node(node))
+        try:
+            nodetype = find_type(self.val.type, '_Node')
+            nodetype = nodetype.strip_typedefs().pointer()
+            node = self.val['_M_node'].cast(nodetype).dereference()
+            return str(get_value_from_list_node(node))
+        except ValueError:
+            return '<insufficient debuginfo for std::list iterator>'
 
 class StdSlistPrinter:
     "Print a __gnu_cxx::slist"
@@ -496,12 +499,18 @@  class StdRbtreeIteratorPrinter:
     def __init__ (self, typename, val):
         self.val = val
         valtype = self.val.type.template_argument(0).strip_typedefs()
-        nodetype = gdb.lookup_type('std::_Rb_tree_node<' + str(valtype) + '>')
-        self.link_type = nodetype.strip_typedefs().pointer()
+        try:
+            # Throws gdb.error if debuginfo for _Rb_tree_node is missing:
+            nodetype = gdb.lookup_type('std::_Rb_tree_node<%s>' % str(valtype))
+            self.link_type = nodetype.strip_typedefs().pointer()
+        except RuntimeError:
+            self.link_type = None
 
     def to_string (self):
         if not self.val['_M_node']:
             return 'non-dereferenceable iterator for associative container'
+        if self.link_type is None:
+            return "<insufficient debuginfo for associative container iterator>"
         node = self.val['_M_node'].cast(self.link_type).dereference()
         return str(get_value_from_Rb_tree_node(node))