Unable to properly debug inside vscode

Hello all, after some searching I was able to debug a very simple kernel via VSCode, I can enter the kernel, step trough it and even see the output of some variable. However I’m unable to see what’s inside arrays. And this is only inside the vscode, if I try to debug the same binary (built from the VSCode with CMake) directly via cuda-gdb, it works as expected.
Just to ease the understanding, here’s some snippets:

kernel

__global__ void sampleKernel(unsigned int *dA, unsigned int *dB, unsigned int size)
{
    unsigned int tid = threadIdx.x + blockIdx.x * blockDim.x;
    if (tid < size)
    {
        dA[tid] = dB[tid] * 2;
    }
}

Allocation, setup and kernell calls:

    unsigned int *hA, *hB, *dA, *dB;
    unsigned int length = 10240;
    ssize_t vecSize = sizeof(unsigned int) * length;

    hB = (unsigned int *)malloc(vecSize);
    hA = (unsigned int *)malloc(vecSize);

    cudaMalloc((void **)&dA, vecSize);
    cudaMalloc((void **)&dB, vecSize);

    memset(hA, 0, vecSize);
    cudaMemset(dA, 0, vecSize);

    for (int i = 0; i < length; i++)
        hB[i] = i;

    fprintf(stdout, "Sending vectors to GPU...\n");
    cudaMemcpy(dB, hB, vecSize, cudaMemcpyHostToDevice);

    sampleKernel<<<512, 512>>>(dA, dB, length);

If i put a break on the dA[tid] = dB[tid] * 2; line, on the vscode I can see correctly the tid variable, however the dB is always set to zero. And if I try to see it on the debbuger console, it gives me an error (pasting the complete console):

NVIDIA (R) CUDA Debugger
12.0 release
Portions Copyright (C) 2007-2022 NVIDIA Corporation
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff4fff000 (LWP 435916)]
[Detaching after fork from child process 435917]
[New Thread 0x7fffe9fff000 (LWP 435926)]
[New Thread 0x7fffe97fe000 (LWP 435927)]
[New Thread 0x7fffe8ffd000 (LWP 435928)]

Thread 1 "helloWorld_cuda" hit Breakpoint 2, execute_kernel () at /home/minterciso/Projects/snippets/ide_tests/vscode/helloWorld_cuda_v2/hello.cu:47
47	    sampleKernel<<<512, 512>>>(dA, dB, length);

Thread 1 "helloWorld_cuda" hit Breakpoint 1, sampleKernel<<<(512,1,1),(512,1,1)>>> (dA=0x7fffc7200000, dB=0x7fffc720a000, size=10240) at /home/minterciso/Projects/snippets/ide_tests/vscode/helloWorld_cuda_v2/hello.cu:14
14	        dA[tid] = dB[tid] * 2;
cuda block (0, 0, 0) thread (0, 0, 0)
CUDA focus unchanged.
cuda block (0, 0, 0) thread (0, 0, 0)
CUDA focus unchanged.
p tid
<Not available>
p dB
<Not available>
p dB[20]
<Not available>

And if I do this via the cuda-gdb, it works normally:

> cuda-gdb ./helloWorld_cuda_v2 
NVIDIA (R) CUDA Debugger
12.0 release
Portions Copyright (C) 2007-2022 NVIDIA Corporation
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./helloWorld_cuda_v2...
(cuda-gdb) b hello.cu:12
Breakpoint 1 at 0xb0e4: file /home/minterciso/Projects/snippets/ide_tests/vscode/helloWorld_cuda_v2/hello.cu, line 16.
(cuda-gdb) r
Starting program: /home/minterciso/Projects/snippets/ide_tests/vscode/helloWorld_cuda_v2/build/helloWorld_cuda_v2 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Hello, world!
Calling kernel execution...
[New Thread 0x7ffff4fff000 (LWP 428313)]
[Detaching after fork from child process 428314]
[New Thread 0x7fffe9fff000 (LWP 428325)]
[New Thread 0x7fffe97fe000 (LWP 428326)]
[New Thread 0x7fffe8ffd000 (LWP 428327)]
Populating vectors...
Sending vectors to GPU...
[Switching focus to CUDA kernel 0, grid 1, block (0,0,0), thread (0,0,0), device 0, sm 0, warp 0, lane 0]

Thread 1 "helloWorld_cuda" hit Breakpoint 1, sampleKernel<<<(512,1,1),(512,1,1)>>> (dA=0x7fffc7200000, dB=0x7fffc7200200, size=100) at /home/minterciso/Projects/snippets/ide_tests/vscode/helloWorld_cuda_v2/hello.cu:12
12          if (tid < size)
(cuda-gdb) p tid
$1 = 0
(cuda-gdb) p dB
$2 = (@generic unsigned int * @parameter) 0x7fffc7200200
(cuda-gdb) p dB[20]
$3 = 20

Also, here’s my CMakeLists.txt:

cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
    set(CMAKE_CUDA_ARCHITECTURES 52)
endif()

project(helloWorld_cuda_v2 VERSION 0.1.0)
set(CMAKE_BUILD_TYPE Debug)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_CUDA_FLAGS ${CMAKE_CUDA_FLAGS} "-g -G")  # enable cuda-gdb
endif()

include(CTest)
enable_testing()
enable_language(CUDA)

add_executable(helloWorld_cuda_v2 
                main.cpp
                hello.cu)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

And the launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "CUDA C++: Launch",
            "type": "cuda-gdb",
            "request": "launch",
            "program": "${workspaceFolder}/build/helloWorld_cuda_v2"
        },
        {
            "name": "CUDA C++: Attach",
            "type": "cuda-gdb",
            "request": "attach",
            "processId": "${command:cuda.pickProcess}"            
        }
    ]
}

Has someone found this issue?
Thank you

Hi! When using the debug console to issue commands to cuda-gdb you want to prefix them with “-exec” or “`”. So you would want to issue “`p db[20]” or “-exec p db[20]”. Hope this helps!

Sorry for the extreme late reply, it works, but it still shows everything as zeroed:

48	    cudaMemcpy(hA, dA, vecSize, cudaMemcpyDeviceToHost);
` p dA[10]
p dA[10]
$1 = 0
` p dB[10]
p dB[10]
$2 = 0
` p hB[10]
p hB[10]
$5 = 10
` p hA[10]
p hA[10]
$6 = 100

The only debug output I can see is the host, not the GPU memory…

To be clear, you are suggesting that in VS Code, you are unable to see debug output for the GPU memory but it works on cuda-gdb?

If that’s the case, maybe you could set ‘debuggerPath’ in your launch.json to be the path of cuda-gdb on your machine? This should make sure VS code is directed to the same cuda-gdb binary you are using on command line.

Thank you, it seems that this worked as expected, I’m still able to look at the variables only if I actually work on the cuda-gdbprompt, instead of the VSCode interface, but it seems that it’s working.

Thank you all for your help.