Debug CUDA Kernels Step by Step With cuda-gdb
Build with -g -G, set breakpoints inside a kernel, switch focus between blocks and threads, inspect variables and use conditional breakpoints with cuda-gdb.

The sanitizer tells you that a thread wrote out of bounds. It does not let you stop the kernel, look at the registers of thread 37 in block 5 and step one line at a time. For that you need a debugger that understands the GPU, and cuda-gdb is NVIDIA's extension of GDB for exactly this. It works as a normal GDB on the host code and adds commands to inspect kernels, blocks and threads. This post uses cuda-gdb from CUDA 12.9 to debug a small kernel with an off-by-one bug: build flags, breakpoints, switching focus, printing data, conditional breakpoints and the options that turn API errors into stops. The cheaper first step, the memory checker, is in the compute-sanitizer post.
Build a debug binary
Debug information for device code needs two flags. -g adds host debug information, as for any compiler, and -G adds device debug information and turns off device optimizations so variables stay where the debugger expects them:
nvcc -g -G -O0 -arch=sm_86 neighbor.cu -o neighborThe consequence is that a -G build is much slower than the release build, and some bugs, especially races and compiler-dependent behavior, change or vanish under it. Treat the debug binary as a way to inspect logic. If a bug appears only in release, fall back to the sanitizer and -lineinfo.
The program with a bug
The kernel below averages each element with its right neighbor. It forgets that the last element has no neighbor:
#include <cstdio>
#include <cuda_runtime.h>
__global__ void averageRight(const float *in, float *out, int n)
{
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < n) {
out[i] = 0.5f * (in[i] + in[i + 1]); // reads in[n] when i == n - 1
}
}
int main()
{
const int n = 64;
float h[n];
for (int i = 0; i < n; ++i) h[i] = (float)i;
float *dIn, *dOut;
cudaMalloc(&dIn, n * sizeof(float));
cudaMalloc(&dOut, n * sizeof(float));
cudaMemcpy(dIn, h, n * sizeof(float), cudaMemcpyHostToDevice);
averageRight<<<2, 32>>>(dIn, dOut, n);
cudaDeviceSynchronize();
printf("done\n");
return 0;
}Thread 63, the last one, reads in[64], one element past the 64-float allocation.
Start the session and stop in the kernel
Start the debugger on the binary and set a breakpoint on the kernel by name:
$ cuda-gdb ./neighbor
(cuda-gdb) break averageRight
Breakpoint 1 at 0x4a3c10: file neighbor.cu, line 6.
(cuda-gdb) run
[Switching focus to CUDA kernel 0, grid 1, block (0,0,0), thread (0,0,0), device 0, sm 0, warp 0, lane 0]
CUDA thread hit Breakpoint 1, averageRight (in=0x7fffb2000000, out=0x7fffb2000200, n=64) at neighbor.cu:6
6 int i = blockIdx.x * blockDim.x + threadIdx.x;The line Switching focus is the key. A kernel has thousands of threads, and the debugger shows one of them at a time, called the focus. It names the kernel, the grid, the block, the thread and the hardware location. Everything you print refers to the focused thread. The addresses and the line numbers will differ on your machine.
Look at all the threads
info cuda has subcommands for each level of the hierarchy:
(cuda-gdb) info cuda kernels
Kernel Parent Dev Grid Status SMs Mask GridDim BlockDim Name Args
0 - 0 1 Active 0x00000003 (2,1,1) (32,1,1) averageRight ...
(cuda-gdb) info cuda blocks
BlockIdx To BlockIdx Count State
(0,0,0) (1,0,0) 2 running
(cuda-gdb) info cuda threads
BlockIdx ThreadIdx To BlockIdx ThreadIdx Count Virtual PC Filename Line
Kernel 0
* (0,0,0) (0,0,0) (0,0,0) (31,0,0) 32 0x0000000000a3c1d0 neighbor.cu 6
(1,0,0) (0,0,0) (1,0,0) (31,0,0) 32 0x0000000000a3c1d0 neighbor.cu 6The thread listing groups threads that are at the same program counter, so a range such as (0,0,0) to (31,0,0) is 32 threads together. A kernel with divergence shows several ranges at different lines, which is a good way to see where a warp split. The asterisk marks the focus.
Move the focus
Use the cuda command to change which thread you inspect:
(cuda-gdb) cuda block 1 thread 31
[Switching focus to CUDA kernel 0, grid 1, block (1,0,0), thread (31,0,0), device 0, sm 1, warp 0, lane 31]
6 int i = blockIdx.x * blockDim.x + threadIdx.x;
(cuda-gdb) next
7 if (i < n) {
(cuda-gdb) print i
$1 = 63
(cuda-gdb) print n
$2 = 64Block 1, thread 31 is global index 63, the last thread with work. You can also move to a thread by coordinates in one command, cuda block (1,0,0) thread (31,0,0). Stepping with next or step advances the focused warp, and as a result the other threads in the same warp move with it, because the hardware executes them together.
Print the data
Variables of the kernel are visible by name. For arrays behind pointers, GDB's @ operator prints a range:
(cuda-gdb) next
8 out[i] = 0.5f * (in[i] + in[i + 1]);
(cuda-gdb) print i + 1
$3 = 64
(cuda-gdb) print *in@4
$4 = {0, 1, 2, 3}
(cuda-gdb) print in[i + 1]
Error: Failed to read local memory at address 0x7fffb2000100 on device 0 sm 1 warp 0 lane 31 (error=7).The last line is the bug showing itself. in[64] is not memory the program owns, and the debugger cannot read it. In a case where the address happens to be readable, the value would be garbage that the program would silently use, which is why such bugs hide. The exact error text varies with the driver version.
Shared memory variables are printed by name inside the kernel that declares them, print tile[3], and registers and local memory are the kernel's own variables. For raw memory, use x, as in x/8f in to examine eight floats.
Conditional breakpoints for one thread
Breaking in every thread of every block is noisy. Make the breakpoint conditional on the built-in variables:
(cuda-gdb) break neighbor.cu:8 if blockIdx.x == 1 && threadIdx.x == 31Now only the interesting thread stops. You can also have the debugger stop whenever the kernel faults, which is what you want when you do not know the line. By default, cuda-gdb stops on an exception such as an illegal address and reports the thread that caused it:
CUDA Exception: Warp Illegal Address
The exception was triggered at PC 0xa3c230 (neighbor.cu:8)Enable the memory checker inside the debugger with set cuda memcheck on before run to catch out-of-bounds accesses that the hardware would not fault on, which makes the bug in this example stop right at the faulty read.
Stop on API errors and kernel launches
Two settings help when the failure is not in the kernel body. set cuda api_failures stop makes the debugger stop when a CUDA API call returns an error, and set cuda break_on_launch application stops at the first instruction of each kernel launched by your code, which saves naming kernels one by one.
(cuda-gdb) set cuda api_failures stop
(cuda-gdb) set cuda break_on_launch applicationThe fix, and checking it
The kernel needs a guard for the last element:
if (i < n - 1) {
out[i] = 0.5f * (in[i] + in[i + 1]);
}Rebuild with the same flags and run again under the debugger with set cuda memcheck on. No exception means the access is now in bounds. Then rebuild without -G and run the sanitizer once more, because a debug build can behave differently from the optimized one.
Limits of this tool
Debugging on the GPU that drives your display can freeze the desktop while a kernel is stopped, because the GPU cannot refresh the screen. Use a second GPU for display, a headless machine or CUDA_VISIBLE_DEVICES to choose a device that is not the display one. A kernel stopped at a breakpoint also stops the whole context, so other CUDA processes on the same device wait. And -G changes timing, so a race that needs tight timing may never show. For races, use racecheck. For understanding which values a thread computed, cuda-gdb is the right tool, and the combination of the two covers most correctness work.
Debugging a failure you cannot reproduce under the debugger
Some failures happen only on a production machine, or only after hours of running. The debugger cannot sit in front of them, but CUDA can write a GPU core dump when a kernel faults, and cuda-gdb can open it afterward. Enable it through environment variables set before the program starts:
export CUDA_ENABLE_COREDUMP_ON_EXCEPTION=1
export CUDA_COREDUMP_FILE=/tmp/core_%h_%p.nvcudmp
./neighborWhen the kernel hits an exception, the driver writes a file whose name expands the host name and process id. Load it in the debugger on any machine that has the same binary and symbols:
(cuda-gdb) target cudacore /tmp/core_buildbox_4812.nvcudmp
Opening GPU coredump: /tmp/core_buildbox_4812.nvcudmp
[Current focus set to CUDA kernel 0, grid 1, block (1,0,0), thread (31,0,0), device 0, sm 1, warp 0, lane 31]The focus is set on the faulting thread, so print, info cuda threads and backtrace work as if you had stopped there live. The dump records the state of the device memory and registers at the moment of the fault, not the full history. A binary built with -lineinfo gives line numbers in the dump, and one built with -G gives variable values, so a release build with -lineinfo yields source locations but shows many variables as unavailable. For a field failure, ship builds with -lineinfo, keep the symbols and enable dumps in the environment of the failing job.
Reading the backtrace
Inside a kernel, backtrace shows the device call stack, which matters when the fault is in a __device__ helper called from the kernel:
(cuda-gdb) backtrace
#0 0x0000000000a3c230 in lookup (table=0x7fffb2000000, key=64) at neighbor.cu:3
#1 0x0000000000a3c1f8 in averageRight (in=0x7fffb2000000, out=0x7fffb2000200, n=64) at neighbor.cu:9Frame 0 is where the exception happened, and frame 1 is the caller with its arguments. Use frame 1 to move up and print the caller's variables. With -G the arguments are exact. Without it, inlined helper functions collapse into the caller, and the backtrace shows fewer frames than the source has, which is one more reason to reproduce under -G when you can.
A short list of commands to remember
| Command | Purpose |
|---|---|
break kernel_name | Stop at a kernel's first line |
info cuda kernels | List running kernels and their grid and block sizes |
info cuda threads | List threads grouped by program counter |
cuda block B thread T | Change the focused thread |
print expr and print *p@N | Show a value or N elements behind a pointer |
set cuda memcheck on | Stop on out-of-bounds accesses |
set cuda api_failures stop | Stop when a runtime call returns an error |
continue, next, step, finish | Normal GDB flow control |


