Plotting Data in GDB

Plotting Data in GDB

Tags
Data Visualization
Computer Science
Tips & Tricks
Published
October 9, 2014
Author
Randall Hand
URL
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 https://sourceware.org/gdb/wiki/PlottingFromGDB # 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, __plot1d.gp, 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 <expr> <opt> <range> # # Plot the points of <expr> passing <opt> as plot options using # <range> in the set yrange command. define plot1d_opt_range shell rm -f /tmp/__plot1d.dump /tmp/__plot1d.dat /tmp/__plot1d.gp 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/__plot1d.gp shell gnuplot /tmp/__plot1d.gp # shell rm -f /tmp/__plot1d.dump /tmp/__plot1d.dat /tmp/__plot1d.gp end # plot1d <expr> # # Just plot the points of <expr>. define plot1d plot1d_opt_range $arg0 "" "[*:*]" end # plot1d_opt <expr> <opt> # # Plot the points of <expr> passing <opt> to the # plot command after the datafile. So, one can pass # "with lines" here. define plot1d_opt plot1d_opt_range $arg0 $arg1 "[*:*]" end
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