Skip to content

Instantly share code, notes, and snippets.

@MuhammadYossry
Last active January 1, 2025 14:05
Show Gist options
  • Save MuhammadYossry/a26b73afa1c5680e795b0b89b4902a75 to your computer and use it in GitHub Desktop.
Save MuhammadYossry/a26b73afa1c5680e795b0b89b4902a75 to your computer and use it in GitHub Desktop.
Useful Go Delve Debugger commands

Particularly Useful Delve Commands for Go Debugging

You might find invaluable during Go debugging sessions:

  1. Breakpoint Commands:
b or break main.go:34    - Set breakpoint at line 34
bp or breakpoints        - List all breakpoints
clear 1                  - Remove breakpoint #1
clearall                 - Remove all breakpoints
condition <bp> <expr>    - Set breakpoint condition
  1. Execution Control:
c or continue           - Run until breakpoint
n or next              - Step over (execute current line)
s or step              - Step into function
stepout                - Step out of current function
restart                - Restart the debugging session
  1. Variable Inspection:
p or print <var>       - Print variable value
whatis <var>          - Show variable type
vars                  - Display local variables
locals                - Show all local variables
args                  - Show function arguments

examples

# Print a variable value
p myVariable
p users[0].Name

# Print struct with all fields
print %x myStruct

# Show type information
whatis myVariable

# Show all local variables in current function
locals

# Print slice/array with 4 elements per line
p -l 4 mySlice
  1. Goroutine Management:
goroutines            - List all goroutines
goroutine            - Show current goroutine
gr <id>              - Switch to specific goroutine

examples

# List all goroutines with their states
goroutines

# Show details of current goroutine
goroutine

# Switch to goroutine #12
gr 12

# Show goroutines with specific filter
goroutines -t    # Show only running goroutines
goroutines -s    # Show start location
  1. Stack Manipulation:
bt or stack           - Print stack trace
frame <n>            - Select frame by number
up                   - Move up one frame
down                 - Move down one frame
  1. Data Structure Navigation:
set <variable> = <value>  - Change variable value
examinemem <addr>         - Examine memory
display                   - Print value of an expression each time program stops
  1. Function-related:
funcs <pattern>      - List functions matching pattern
list                - Show source code around current point
disassemble         - Disassemble current function

examples

# List all functions containing "handler"
funcs handler

# List all methods of a struct
funcs MyStruct

# Show current function source
list

# Show specific lines of code
list 15:20
  1. Process Control:
exit or quit        - Exit debugger
restart             - Restart process
on <event> <cmd>    - Execute command when event occurs

Particularly useful advanced techniques:

# Conditional breakpoints
break main.go:34 -c "someVar == 5"

# Break on function
break MyFunction

# Watch variables
watch someVariable

# Print with format
p -f hex someValue    # Print in hexadecimal

Application Inspection

# Show stack trace with full argument values
stack -full

# Show all threads
threads

# Memory statistics
memstats

# Check current package/function
frame

# Register values
regs

Runtime Debugging

# Set dynamic breakpoint on function entry
b MyFunction

# Break when variable changes
watch myVariable

# Break with condition
b main.go:35 -c "err != nil"

# Print every time a variable changes
display myVariable

Quick Reference for Common Debugging Flow

# Start debugging
dlv debug ./cmd/myapp/main.go

# Set initial breakpoint
b main.main

# Run until breakpoint
continue (or c)

# Step through code
next (or n)    # Step over
step (or s)    # Step into
stepout        # Step out of function

# Examine state
bt             # Show stack trace
p variableName # Print variable
whatis v       # Show variable type

Pro Tips: 0. Use ctrl+d to exit delve

  1. You can use help <command> to get detailed information about any command
  2. Use config to set various debugger configurations
  3. The source command can load a file containing delve commands
  4. Use trace to set tracepoints that don't stop execution but print information
  5. The on command is powerful for automating debugging tasks
  6. Up/down arrow keys navigate command history And finally Use clear to remove breakpoints when they're no longer needed.

Happy Debugging!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment