Printing arrays and gsl_matrix data in 2D form within gdb
Because I'm lousy with indexing, I often have to watch loops unfurl live in gdb. Here are several quick user-defined gdb commands that make inspecting 2-dimensional data stored in a 1-dimensional array much easier.
Place the following commands in your ~/.gdbinit
:
Now you can use
# Modified from http://sourceware.org/ml/gdb/2007-07/msg00140.html
define p_array2d_generic
set $array=$arg0
set $ibound=$arg1
set $jbound=$arg2
set $ilda=$arg3
set $jlda=$arg4
set $i = 0
set $j = 0
while $i<$ibound
while $j<$jbound
printf " "
printf "%9.4g", $array[$i*$ilda+$j*$jlda]
set $j = $j + 1
end
printf "\n"
set $j = 0
set $i = $i + 1
end
end
document p_array2d_generic
Print an array in 2D form: p_array2d array ibound jbound ilda jlda
end
define p_array2d_cm
p_array2d_generic $arg0 $arg1 $arg2 1 $arg1
end
document p_array2d_cm
Print an array in column-major 2D form: p_array2d array ibound jbound
end
define p_array2d_rm
p_array2d_generic $arg0 $arg1 $arg2 $arg2 1
end
document p_array2d_rm
Print an array in row-major 2D form: p_array2d array ibound jbound
end
p_array2d_cm
and p_array2d_rm
to print out column- and row-major double arrays, respectively.
Using the above and some information about the memory layout of gsl_matrix
instances, it's now easy to write one line function to dump a gsl_matrix
:
define p_gsl_matrix
p_array2d_generic $arg0->data $arg0->size1 $arg0->size2 $arg0->tda 1
end
document p_gsl_matrix
Print a gsl_matrix in 2D form: p_gsl_matrix (gsl_matrix *)
end
Question for you: I would like to have the p_array2d_generic take an additional argument which is a format specifier. A check using $argc
would allow a reasonable, double
-based default but would allow the user to specify anything he or she desires. However, I cannot get code like
to work the way I think it should. I always run into a gdb error like <<Bad format string, missing '"'.>>. Anyone have any ideas how to provide a printf template parameter from a set variable in gdb?
set $format="%3.2g"
printf $format, 123