Plotting Data in GDB

Using GDB to debug programs is great and all, but if you work with Scientific data then one really critical function is the ability to plot a long array or vector, to see how the solution is evolving at various stages.

After a bit of tinkering, I was able to take this code snippet and modify it for my needs. It had just a few problems:
The syntax they used for labeling things fell apart when I was using Eigen vectors, the -> became shell redirects.
It didn’t link very long (100+) entry arrays.

So I modified it like so:

# plot1d.gdb

# Copyright (C) 2008 Florian Lorenzen
# - Taken from
# Modified by Randall Hand to actually make it work.
# original version seemed to badly handle variable names
# like esf->data[0]@1024 (turning the > into a pipe redirect)
# Plot an expression that expands to {x1, x2, ..., xN}, i. e.
# N numbers using gnuplot.
# This file is for the GNU debugger 6.x.
# It writes temporary files named __plot1d.dump, __plot1d.dat,, so
# you should not have files of the same name in the working directory.
# It requires sed, awk, and gnuplot available in the $PATH.

# plot1d_opt_range   
# Plot the points of  passing  as plot options using
#  in the set yrange command.
define plot1d_opt_range
shell rm -f /tmp/__plot1d.dump /tmp/__plot1d.dat /tmp/
set logging file /tmp/__plot1d.dump
set logging on
set logging redirect on
set height 0
set print elements unlimited
output $arg0
set logging off
set logging redirect off
shell awk '{printf("%s", $0)}' < /tmp/__plot1d.dump | \
sed 's/^{\(.*\)}$/\1/;s/, */\n/g' > /tmp/__plot1d.dat
shell echo 'plot "/tmp/__plot1d.dat" title "$arg0"; pause -1 "Press enter to continue"' > /tmp/
shell gnuplot /tmp/
# shell rm -f /tmp/__plot1d.dump /tmp/__plot1d.dat /tmp/

# plot1d 
# Just plot the points of .
define plot1d
plot1d_opt_range $arg0 "" "[*:*]"

# plot1d_opt  
# Plot the points of  passing  to the
# plot command after the datafile. So, one can pass
# "with lines" here.
define plot1d_opt
plot1d_opt_range $arg0 $arg1 "[*:*]"


With this file placed somewhere and executed, you can then simply do plot1d data[0]@128 to get a nice plot of the first 128 values of data.

And if you like it so much you want to keep it, simply add source plot1d.gdb (which a path to wherever you put it) to your ~/.gdbinit

comments powered by Disqus