Knowledge Base
Authentication is disabled (development mode).

pretty-print Qt3 strings in gdb

Edit

gdb pretty print qstring qt qt3 string

import gdb.printing

class Qt3Printer:
	'''Print a QString as of Qt3.3.8b'''

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

	def to_string(self):
		len = self.val['d']['len']

		try:
			if not self.val['d']['unicode']:
				result = '0 ""'
			else:
				p = self.val['d']['unicode'].cast(gdb.lookup_type('wchar_t').pointer())
				s = p.string(encoding = 'UTF-16')
				result = str(len) + ' "' + s[:len] + '"'
		except Exception as e:
			result = str(e)
			pass

		return result

	def display_hint(self):
		return 'string'

def build_pretty_printer():
	pp = gdb.printing.RegexpCollectionPrettyPrinter("qt3")
	pp.add_printer('QString', '^QString$', Qt3Printer)
	return pp

def register_printers():
	gdb.printing.register_pretty_printer(
		gdb.current_objfile(),
		build_pretty_printer()
	)