16 July 2009

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:

# 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
        printf "\n"
        set $j = 0
        set $i = $i + 1

document p_array2d_generic
Print an array in 2D form: p_array2d array ibound jbound ilda jlda

define p_array2d_cm
    p_array2d_generic $arg0 $arg1 $arg2 1 $arg1

document p_array2d_cm
Print an array in column-major 2D form: p_array2d array ibound jbound

define p_array2d_rm
    p_array2d_generic $arg0 $arg1 $arg2 $arg2 1

document p_array2d_rm
Print an array in row-major 2D form: p_array2d array ibound jbound
Now you can use 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

document p_gsl_matrix
Print a gsl_matrix in 2D form: p_gsl_matrix (gsl_matrix *)

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

set $format="%3.2g"
printf $format, 123
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?

07 July 2009

My favorite South Austin photo

Quick! Find the following items in the South Austin picture to the right:
  1. tiki torch
  2. pina colada
  3. high volume bangs
  4. proto-dreads
  5. man purse
  6. brick in the middle of the grass
  7. patriotic broach
  8. aviator sunglasses
  9. cheap Mexican beer
  10. a chihuahua wearing a lei
  11. a massive inflatable pool complete with pump
I need to cross the river (lake?) more often.

01 July 2009

Skateboarding (Injury)

Eight days ago I purchased a skateboard at the age of 27. Three days ago I fell at Mabel Davis park at the age of 27. I messed up an elbow badly enough that I've been in a sling for several days now. I see an orthopedic surgeon tomorrow morning to figure out why I cannot straighten my right arm completely. No harm though, I can still type and use a mouse.

I'm primarily annoyed that I can't roll around again until this injury is healed. I'm awful at the sport, but damn it is fun stuff. Behold shameless YouTubage of the guy who's name is on the bottom of my deck:

Subscribe Subscribe to The Return of Agent Zlerich