Re: [eigen] gdb Eigen Pretty printers

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi

I just fixed that bug. The problem was that the script did not handle
references.

Attached is a fixed version of the script. Feedback very welcome :)

Cheers
Benjamin
Benjamin Schindler wrote:
> Hi
>
>
> Benoit Jacob wrote:
>   
>> Hi,
>>
>> 2009/12/8 Benjamin Schindler <bschindler@xxxxxxxxxxx>:
>>   
>>     
>>> Hi
>>>
>>> Finally a real contribution to eigen - it was about time. I just wrote a
>>> pretty printer for Eigen::Matrix gdb-7.0 and later.
>>>     
>>>       
>> Very nice!
>>
>>   
>>     
>>> To use it:
>>>
>>> * create a directory and put the file as well as an empty __init__.py in
>>> that directory
>>> * Create a ~/.gdbinit file, that contains the following:
>>>
>>> python
>>> import sys
>>> sys.path.insert(0, '/path/to/eigen/printer/directory')
>>> from printers import register_eigen_printers
>>> register_eigen_printers (None)
>>> end
>>>     
>>>       
>> I tried it on the attached file a.cpp,
>>
>>   $ g++ a.cpp -I ../eigen -o a -g3
>>   $ gdb ./a
>>   > start
>>   > step
>>   (then press Enter several times)
>>
>> But eventually I get this python error message:
>>
>> (gdb)
>> main () at a.cpp:13
>> 13        f(m,n);
>> (gdb)
>> f (m=Traceback (most recent call last):
>>   File "/home/bjacob/cuisine/printers.py", line 105, in lookup_function
>>     return pretty_printers_dict[function](val)
>>   File "/home/bjacob/cuisine/printers.py", line 80, in <lambda>
>>     pretty_printers_dict[re.compile('^Eigen::Matrix<.*>$')] = lambda
>> val: EigenMatrixPrinter(val)
>>   File "/home/bjacob/cuisine/printers.py", line 24, in __init__
>>     m = regex.findall(tag)[0][1:-1]
>> TypeError: expected string or buffer
>> ..., n=Traceback (most recent call last):
>>   File "/home/bjacob/cuisine/printers.py", line 105, in lookup_function
>>     return pretty_printers_dict[function](val)
>>   File "/home/bjacob/cuisine/printers.py", line 80, in <lambda>
>>     pretty_printers_dict[re.compile('^Eigen::Matrix<.*>$')] = lambda
>> val: EigenMatrixPrinter(val)
>>   File "/home/bjacob/cuisine/printers.py", line 24, in __init__
>>     m = regex.findall(tag)[0][1:-1]
>> TypeError: expected string or buffer
>> ...) at a.cpp:7
>> 7         m = m * n;
>>
>>
>> Any idea?
>>   
>>     
> I just tested your file here and got that:
> bschindl@hundertwasser /local/bschindler/projects/test/build $ gdb ./test
>
> (gdb) start
> Temporary breakpoint 1 at 0x4007fa: file a.cpp, line 12.
> Starting program: /local/bschindler/projects/test/build/test
>
> Temporary breakpoint 1, main () at a.cpp:12
> 12        Matrix2d m, n;
> (gdb) n
> 13        f(m,n);
> (gdb) p m
> $1 = Eigen::Matrix<double,2,2> = {
>   [0, 0] = 2.0747911307212309e-317,
>   [1, 0] = 0,
>   [0, 1] = 6.9533558073425197e-310,
>   [1, 1] = 0
> }
> (gdb) p n
> $2 = Eigen::Matrix<double,2,2> = {
>   [0, 0] = 6.9533486693516308e-310,
>   [1, 0] = 2.0748148458722313e-317,
>   [0, 1] = 6.9533558073429149e-310,
>   [1, 1] = 6.9533491761380836e-310
> }
> (gdb)
>
> In your output I don't really see what produced the output you got there
> - did you try to print a variable or did you do something else?
>
> I assume this problem has to do with the fact you are on eigen-devel
> (You include <Eigen/Eigen> which is not available with stable).
> I'll retry with unstable in a sec
>   
>>   
>>     
>>> Notes:
>>>
>>> - It currently only supports Eigen::Matrix and no other type (such as
>>> quaternions etc). Patches welcome :)
>>> - I wrote it using the stable branch of eigen. I don't know whether it
>>> works with hg
>>> - It's still kind of hacky, mostly because of the limitations of the
>>> gdb.python framework
>>>     
>>>       
>> OK
>>
>>   
>>     
>>> It seems that the gdb hackers want the pretty printer scripts included
>>> in the respective patches, so my final goal would be to have it in eigen-hg
>>>     
>>>       
>> I don't understand what you mean by "in the respective patches" ?
>>   
>>     
> Sorry - I typed too fast. They want that the scripts are packaged with
> the respective libraries and not included in gdb as i.e. cmake does it.
>   
>> I agree that this is worth having in Hg. Maybe we should create a
>> top-level directory "debug" with a subdirectory "gdb" containing your
>> stuff. Then Hauke's work would go into another subdirectory "msvc".
>> Each would have a short README file, so Hauke's documentation on the
>> wiki could go there, or could be linked to from there. Opinions?
>>   
>>     
> Sounds good to me
>
> Benjamin
>   
>> Benoit
>>   
>>     
>
>
>
>   

# -*- coding: iso-8859-1 -*-
# Pretty printers for Eigen::Matrix
# Author: Benjamin Schindler <bschindler@xxxxxxxxxxx>
# This is still pretty basic as the python extension to gdb is still pretty basic. 
# It cannot handle complex eigen types and it doesn't support any of the other eigen types
# Such as quaternion or some other type. 
# This code supports fixed size as well as dynamic size matrices
# Licence - ment to be included in Eigen, so dual GPLv3 or LGPL
# NOTE: This code was only tested with the stable branch of eigen!

import gdb
import re
import itertools


class EigenMatrixPrinter:
	"Print Eigen Matrix of some kind"

	def __init__(self, val):
		"Extract all the necessary information"
		# The gdb extension does not support value template arguments - need to extract them by hand
		type = val.type
		if type.code == gdb.TYPE_CODE_REF:
			type = type.target()
		self.type = type.unqualified().strip_typedefs()
		tag = self.type.tag
		regex = re.compile('\<.*\>')
		m = regex.findall(tag)[0][1:-1]
		template_params = m.split(',')
		template_params = map(lambda x:x.replace(" ", ""), template_params)

		self.rows = int(template_params[1])
		self.cols = int(template_params[2])

		if self.rows == 10000:
			self.rows = val['m_storage']['m_rows']

		if self.cols == 10000:
			self.cols = val['m_storage']['m_cols']

		self.innerType = self.type.template_argument(0)

		self.val = val

	class _iterator:
		def __init__ (self, rows, cols, dataPtr):
			self.rows = rows
			self.cols = cols
			self.dataPtr = dataPtr
			self.currentRow = 0
			self.currentCol = 0

		def __iter__ (self):
			return self

		def next(self):
			if self.currentCol >= self.cols:
				raise StopIteration

			row = self.currentRow
			col = self.currentCol
			self.currentRow = self.currentRow + 1
			if self.currentRow >= self.rows:
				self.currentRow = 0
				self.currentCol = self.currentCol + 1

			item = self.dataPtr.dereference()
			self.dataPtr = self.dataPtr + 1

			return ('[%d, %d]' % (row, col), item)

	def children(self):
		data = self.val['m_storage']['m_data']
		# Fixed size matrices have a struct as their storage, so we need to walk through this
		if data.type.code == gdb.TYPE_CODE_STRUCT:
			data = data['array']
			data = data.cast(self.innerType.pointer())
		return self._iterator(self.rows, self.cols, data)

	def to_string(self):
		return "Eigen::Matrix<%s,%d,%d>" % (self.innerType, self.rows, self.cols)

def build_eigen_dictionary ():
	pretty_printers_dict[re.compile('^Eigen::Matrix<.*>$')] = lambda val: EigenMatrixPrinter(val)

def register_eigen_printers(obj):
	"Register eigen pretty-printers with objfile Obj"

	if obj == None:
		obj = gdb
	obj.pretty_printers.append(lookup_function)

def lookup_function(val):
	"Look-up and return a pretty-printer that can print va."

	type = val.type

	if type.code == gdb.TYPE_CODE_REF:
		type = type.target()
	
	type = type.unqualified().strip_typedefs()

	typename = type.tag
	if typename == None:
		return None

	for function in pretty_printers_dict:
		if function.search(typename):
			return pretty_printers_dict[function](val)

	return None

pretty_printers_dict = {}

build_eigen_dictionary ()


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/