"""Pretty printers for libqi.
Add python execfile("/path/to/this/file") in your ~/.gdbinit or /etc/gdb/gdbinit
Your gdb need to be compile with python and version > 7.0
"""

import gdb
import gdb.types
import re

class QiFuturePrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        fut = self.val['_p']['px'].dereference()
        _fut = fut['_p']
        state = str(_fut['_state'].cast(gdb.lookup_type("qi::FutureState")))
        if state == "qi::FutureState_Running":
            return "Running qi::Future<>"
        elif state == "qi::FutureState_Canceled":
            return "Cancelled qi::Future<>"
        elif state == "qi::FutureState_FinishedWithError":
            return "qi::Future<> finished with error %s" % _fut['_error']
        elif state == "qi::FutureState_FinishedWithValue":
            return "qi::Future<> finished with value %s" % fut['_value']
        elif state == "qi::FutureState_None":
            return "qi::Future<> not associated with a promise"

class QiBufferPrinter:
    def __init__(self, val):
        self.val = val

    def to_string(self):
        buf = self.val['_p']['px'].dereference()
        return "qi::Buffer of length %i, capacity %i, sub-buffers %s" % (buf["used"], buf["available"], buf['_subBuffers'])

def lookup_type(val):
    type = str(gdb.types.get_basic_type(val.type))
    if type == 'qi::Buffer':
        return QiBufferPrinter(val)
    regex = re.compile("^qi::Future<.*>$")
    m = regex.match(type)
    if m:
        return QiFuturePrinter(val)
    return None

if __name__ == "__main__":
    gdb.pretty_printers.append(lookup_type)

@@ -458,16 +458,66 @@
 ate%22)))%0A
+        type = fut.type.template_argument(0).name%0A
         
@@ -585,35 +585,46 @@
 nning qi::Future
-%3C%3E%22
+ of %25s%22 %25 type
 %0A        elif st
@@ -702,11 +702,22 @@
 ture
-%3C%3E%22
+ of %25s%22 %25 type
 %0A   
@@ -794,34 +794,38 @@
 turn %22qi::Future
-%3C%3E
+ of %25s
  finished with e
@@ -834,16 +834,23 @@
 or %25s%22 %25
+ (type,
  _fut%5B'_
@@ -856,16 +856,17 @@
 _error'%5D
+)
 %0A       
@@ -947,18 +947,22 @@
 ::Future
-%3C%3E
+ of %25s
  finishe
@@ -979,16 +979,23 @@
 ue %25s%22 %25
+ (type,
  fut%5B'_v
@@ -1000,16 +1000,17 @@
 _value'%5D
+)
 %0A       
@@ -1082,10 +1082,14 @@
 ture
-%3C%3E
+ of %25s
  not
@@ -1115,16 +1115,23 @@
 promise%22
+ %25 type
 %0A%0Aclass 

Improve pretty printers of Future