traditional GDB commands

April 20, 2026 ยท View on GitHub

  • gdb_fortran_tools

  • Table of contents :toc_3:noexport: :PROPERTIES: :CUSTOM_ID: toc :END:

  • [[#gdb_fortran_tools][gdb_fortran_tools]]
  • [[#introduction][Introduction]]
  • [[#installation][Installation]]
  • [[#usage-example][Usage example]]
  • [[#commands][Commands]]
  • [[#supported-types][Supported types]]
  • [[#warnings][Warnings]]
    • [[#array-indexing][Array indexing]]
    • [[#allocatable-arrays][Allocatable arrays]]
  • [[#additional-hints][Additional hints]]
  • [[#requirements][Requirements]]
  • [[#acknowledgements][Acknowledgements]]
  • Introduction :PROPERTIES: :CUSTOM_ID: intro :END:

Example command and output

#+BEGIN_SRC :exports both (gdb) logshow landice_roi_mod::landice_ds%roi(:,:) #+END_SRC

[[./example.png]]

Support more advanced =gdb= debugging of Fortran code

  • Generic access to many basic numpy array operators: sum, min, max, log10, etc.
  • Access to custom complex on-liners
  • Graphics: plot, imshow, scatter
  • Save data: pickle, CSV
  • Installation :PROPERTIES: :CUSTOM_ID: install :END:

You need a Python environment with =matplotlib=, =numpy=, and =gdb=.

Install this software with:

#+BEGIN_SRC bash :exports both :results verbatim git clone https://github.com/mankoff/gdb_fortran_tools #+END_SRC

Set an environment variable to that location (perhaps in your =.bashrc= or other init script).

#+BEGIN_SRC bash :exports both :results verbatim export GFT_DIR=/path/to/gdb_fortran_tools #+END_SRC

Edit your =~/.gdbinit= file to load =gdb_fortran_tools=

#+BEGIN_SRC python python import os import sys if 'GFT_DIR' not in os.environ: print(f'WARNING: environmental var GFT_DIR not found') else: sys.path.insert(0, os.path.expanduser(os.environ['GFT_DIR'])) try: import gdb_fortran_tools except: print("WARNING: Could not import gdb_fortran_tools") end #+END_SRC

  • Usage example :PROPERTIES: :CUSTOM_ID: example :END:

With the following code in =example.F90=

#+BEGIN_SRC f90 :exports both :tangle example.F90 program main real, allocatable, DIMENSION(:) :: x, y real, allocatable, DIMENSION(:,:) :: xy INTEGER :: im, jm, i,j

im = 4 jm = 5

allocate(x(im)) allocate(y(jm)) allocate(xy(im,jm))

do i=1,im x(i) = i+3 do j=1,jm y(j) = j+5 xy(i,j) = x(i)*y(j) end do end do

print *, x print *, y print *, xy end program main #+END_SRC

Compile it for debugging with

#+BEGIN_SRC bash :exports both :results verbatim gfortran -g example.F90 #+END_SRC

Run gdb:

#+BEGIN_SRC bash :exports both :results verbatim gdb ./a.out break 23 run

traditional GDB commands

p x ptype x

Simple numpy accessors

np sum x np max y np log10 xy

Plots

plot x imshow xy imshow xy x y imshow0 xy x y logshow xy logshow xy x y plot xy plot x y(2:5) scatter x y(2:5)

arbitrary Python commands

pycmd xy print() pycmd xy np.min([np.where(_ != 0)]) pycmd xy [item for item in _.flatten() if (item % 2) == 0]

save to CSV

savecsv xy.csv xy #+END_SRC

  • Commands
  • Graphical
    • imshow :: Display 2D array using =matplotlib.pyplot.imshow=
    • imshow0 :: Same as =imshow= but set 0 to NaN (colorbar scaling)
    • logshow :: Same as =imshow= but display =np.log10()= of data
    • scatter :: Display scatter plot of two vectors
    • plot3d :: Display 3D plot of 2D array
    • scatter3d :: Display 3D scatter plot
    • hist :: Display histogram of vector
    • fft :: Display FFT of vector
  • Numerical
    • np :: Call =np.function(variable)= for any numpy function
    • pycmd <statements()> :: Run any sequence of valid one-line Python =statements= on variable. Within =statement=, access variable via == (underscore)
  • I/O
    • savecsv <file.csv> :: Save =variable= to =file.csv=
    • savepy :: Save =variable= to =filename= in Python pickle format
    • save :: Save =variable= to =file= using numpy =tofile= function
  • Supported types

#+BEGIN_SRC f90 :exports both real*{4,8}, dimension(:), dimension(:,:), dimension(:,:,:) integer*{4,8}, dimension(:), dimension(:,:), dimension(:,:,:) logical #+END_SRC

  • Warnings :PROPERTIES: :CUSTOM_ID: warn :END:

** Array indexing

  • Fortran uses 1-based array indexing
  • Python uses 0-based array indexing

** Allocatable arrays Note: If gdb reports

#+BEGIN_EXAMPLE (gdb) ptype foo type = real(kind=8), allocatable (72,0:47) #+END_EXAMPLE

Then you need to use the syntax =imshow foo(:,:)=

  • Additional hints

You can create custom =gdb= commands that build on commands provided here. For example to find the range of an array, add this to your =~/.gdbinit=

#+BEGIN_SRC bash :exports both :results verbatim define mm np min arg0npmaxarg0 np max arg0 end
document mm print min and max of an array or vector. Uses gdb_fortran_tools. end #+END_SRC

  • Requirements :PROPERTIES: :CUSTOM_ID: req :END:
  • GDB >= 7.0
  • Python 3
    • NumPy
    • Matplotlib
    • gdb (the Python package)
  • Acknowledgements :PROPERTIES: :CUSTOM_ID: ack :END:

Thanks to [[https://github.com/X-Neon][X-Neon]] and [[https://github.com/X-Neon/gdbplotlib][gdbplotlib]].