Is there a way to print the Armadillo matrix in gdb?

I use gdb to debug my C ++ program. I use the Armadillo Numerical Library to define my matrices. I have an armadillo matrix defined like this:

mat A = randu<mat>(5,5); 

Is it possible to print the entire matrix when using the gdb debugger?

+7
source share
5 answers

The question may be old, but stumbling over it, I found a solution for my work.

Due to the template-based nature of the Armadillo library, you need to provide some helpers:

 #include <iostream> #include <armadillo> template<class Matrix> void print_matrix(Matrix matrix) { matrix.print(std::cout); } //provide explicit instantiations of the template function for //every matrix type you use somewhere in your program. template void print_matrix<arma::mat>(arma::mat matrix); template void print_matrix<arma::cx_mat>(arma::cx_mat matrix); int main() { arma::mat matrix = arma::randu(10,10); return 0; } 

Now you can easily call print_matrix from gdb :

 (gdb) call print_matrix<arma::Mat<double> >(matrix) 0.8402 0.4774 0.0163 0.5129 0.5267 0.5260 0.2383 0.5316 0.6879 0.9565 0.3944 0.6289 0.2429 0.8391 0.7699 0.0861 0.9706 0.0393 0.1660 0.5886 0.7831 0.3648 0.1372 0.6126 0.4002 0.1922 0.9022 0.4376 0.4401 0.6573 0.7984 0.5134 0.8042 0.2960 0.8915 0.6632 0.8509 0.9318 0.8801 0.8587 0.9116 0.9522 0.1567 0.6376 0.2833 0.8902 0.2667 0.9308 0.8292 0.4396 0.1976 0.9162 0.4009 0.5243 0.3525 0.3489 0.5398 0.7210 0.3303 0.9240 0.3352 0.6357 0.1298 0.4936 0.8077 0.0642 0.3752 0.2843 0.2290 0.3984 0.7682 0.7173 0.1088 0.9728 0.9190 0.0200 0.7602 0.7385 0.8934 0.8148 0.2778 0.1416 0.9989 0.2925 0.0698 0.4577 0.5125 0.6400 0.3504 0.6842 0.5540 0.6070 0.2183 0.7714 0.9493 0.0631 0.6677 0.3540 0.6867 0.9110 

Thanks to the completion of the tab, you only need to type a few characters print_matrix<arma::Mat<double> > .

+8
source

You can call C functions in gdb , so you just need a function that prints your objects. For example:

 (gdb) call printf("%.2f", 3.1428) $7 = 4 (gdb) call fflush(stdout) 3.14$8 = 0 
+2
source

For those using QtCreator , you can check the values ​​from your IDE for the GDB extension using the Python helpers for debugging (perhaps other IDEs also support this function).

Put the following script in e.g. ~/debugHelpers.py

 #!/usr/bin/python import gdb # gdb.Value() import dumper # dumper.Children() def qdump__arma__Mat(d, value): array = value["mem"] cols = value["n_cols"] rows = value["n_rows"] maxDisplayItems = 50 innerType = d.templateArgument(value.type, 0) p = gdb.Value(array.cast(innerType.pointer())) d.putItemCount(cols) d.putNumChild(cols) if d.isExpanded(): numDisplayItems = min(maxDisplayItems, cols) with dumper.Children(d, numChild=cols, maxNumChild=numDisplayItems, childType="<column>", addrBase=p, addrStep=p.dereference().__sizeof__): for i in range(0, int(numDisplayItems)): with dumper.Children(d): d.putItemCount(rows) d.putNumChild(rows) if d.isExpanded(): numDisplayItems = min(maxDisplayItems, rows) with dumper.Children(d, numChild=rows, maxNumChild=numDisplayItems, childType=innerType, addrBase=p, addrStep=p.dereference().__sizeof__): for j in range(0, int(numDisplayItems)): d.putSubItem(j, p.dereference()) p += 1 

And name it by adding this line to your ~/.gdbinit :

 python exec(open('/<full_path>/debugHelpers.py').read()) 

Or add it from the IDE in QtCreator use Tools> Options> Debugger> GDB (tab)> Additional debugging helpers (bottom).

This particular script returns a column sorted matrix (natural memory in my architecture):

enter image description here

Sources: Writing Debugging Documents for GDB / QtCreator 2.8

+1
source

The easiest way is to type directly in gdb , unfortunately there is no fancy format

 > print * A.mem@5 @5 $1 = {{0.84018771715470952, 0.39438292681909304,0.78309922375860586,0.79844003347607329, 0.91164735793678431}, {0.19755136929338396, 0.33522275571488902, 0.768229594811904, 0.27777471080318777, 0.55396995579543051}, {0.47739705186216025, 0.62887092476192441, 0.36478447279184334, 0.51340091019561551, 0.95222972517471283}, {0.91619506800370065, 0.63571172795990094, 0.71729692943268308, 0.14160255535580338, 0.60696887625705864}, {0.016300571624329581, 0.24288677062973696, 0.13723157678601872, 0.80417675422699042, 0.15667908925408455}} 
0
source

The best option is to use the gdb python API to create beautiful printers for armadillo classes. It works better than calling a C / C ++ function because it is always available (even when debugging from a kernel file where the program is not running). Moreover, there is no risk that the linker will abandon the function when it is not used in your program (which makes it inaccessible when debugging in gdb).

A beautiful printer code is downloaded (from the source) from the .gdbinit file in your home folder. It will work in gdb, working both in the terminal and from the IDE (provided that the IDE does not avoid downloading the .gdbinit file).

As an example, suppose you have the following two matrices

 arma::mat m1{{1.1, 2.2, 3}, { 4, 5, 6}, { 7, 8, 9}, { 10, 11, 12}, { 13, 14, 15}, { 16, 17, 18}}; arma::cx_mat m2{{1.1 - 1.1j, 2.2 - 7.7j, 3}, { 4, 5, 6}, { 7, 8, 9}, { 10, 11, 12}, { 13, 14, 15}, { 16, 17, 18}}; 

A beautiful printer can show them as

m1

m2

Please note that in a complex m2 matrix, elements are displayed both in a rectangular and in a polar form (the display of the polar form can be turned off). It also works better with the rest of GDB. For example, GDB allows you the maximum number of elements to display in an array. A beautiful printer will respect this if it is implemented to display elements as standard arrays in GDB.


This is how the matrices m1 and m2 displayed in CLion.

m1 in CLion

m2 in CLion


This cute printer can be obtained from here . There are several other things, such as some xmethods (reimplementing Python of some C ++ methods) and converting to empty arrays.

Disclaimer : I am the author of these beautiful printers.

0
source

All Articles