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
        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
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
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

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:

26 June 2009

Frank Gabron

The Penn State Mechanical and Nuclear Engineering department's last alumni newsletter had a small feature on Frank Gabron (article follows). I received one of his scholarships while I was an undergraduate, and it (along with my parents being very cool) was what allowed me to spend long enough on campus to pick up a double major with a double minor. Every year I wrote Gabron a thank you note, and every year he wrote back.

One fall his response said he was coming into State College and would like to have dinner. We ate at the Nittany Lion Inn and made small talk. He told some fantastic stories. After dinner we walked outside and shared that awkward I'm-going-to-smoke-dont-judge-me-oh-no-way-you-smoke-too moment. As we stood and talked he asked if I'd like to come to the Penn State game the following day.

I went back to the Inn for a pre-game pep rally with Graham Spanier firing up everyone. We rode over to the stadium and watched the game from the nicest seats I will ever have in Beaver Stadium—the president's box. At halftime we snuck out for a smoke at a place inside the stadium where he knew he could usually get away with one. He was telling me about his Helix Technology Corporation when a campus cop wandered into the outside area. Without missing a beat, the man said "Oh shit, here comes the gendarme!" and cupped his cigarette in his hand behind his back like a kid in a high school parking lot when the coach drives past.

After the game we rode back to the Inn. I fell asleep on the bus on the way back, woke up hurriedly, did a lousy job of saying thank you, and then ran off to flip burgers at Baby's for a closing shift. I don't remember who Penn State played that day, or even if we won. But I do and always will remember that as one of the two or three best weekends I spent on campus.

Update 3 June 2011: It seems Mr. Gabron passed away on the 17th of May at the age of 81. I still and always will smile every time I think of that football game and his generosity in helping me obtain my undergraduate degree.