Considering the two different programs below, test.nim
and sdl_test.nim
, debugging in VSCode using the NativeDebug extension with nim c --out:sdl_test -d:debug --debugger:native sdl_test.nim
.
When debugging the test.nim
program, I can normally evaluate variable values. But when debugging the sdl_test.nim
program, I get "No symbol XXX in current context. (from data-evaluate-expression --thread 1 --frame 0 tex)" all over the place.
- Why is that?
- How to properly setup and debug Nim variables consistently in VSCode?
- Is it because in the SDL example, they are actually C pointers (
var win: WindowPtr
->WindowPtr* = ptr object
), so there's no way to introspect them?
Examples
SDL variables (not able to inspect):
Dummy variables (able to inspect):
VSCode
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Nim Project",
"type": "gdb",
"request": "launch",
"target": "test",
"cwd": "${workspaceRoot}",
"valuesFormatting": "parseText"
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Nim Project",
"type": "shell",
"command": "nim c --out:test -d:debug --debugger:native test.nim",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
test.nim
type
TestObj = object
num: int
val: float
str: string
proc initTestObj(num: int): TestObj =
TestObj(num: num, val: 3.141, str: "TestObj")
proc foo(x: int): int =
let y = x + 2
return y * 10
proc bar(x: int): int =
if x == 3:
return foo(x)
return x * 100
proc main =
var a = 1
a += 3
let str = "foobar"
var seq1 = @[0, 1, 2, 3, 4]
a = bar(1)
a = bar(2)
a = bar(3)
let tobj = initTestObj(11)
main()
sdl_test.nim
import sdl2
var
win: WindowPtr
ren: RendererPtr
bmp: SurfacePtr
tex: TexturePtr
discard init(INIT_EVERYTHING)
proc main() =
win = createWindow("Hello World!", 100, 100, 620, 387, SDL_WINDOW_SHOWN)
if win == nil:
echo("createWindow Error: ", getError())
quit(1)
ren = createRenderer(win, -1, Renderer_Accelerated or Renderer_PresentVsync)
if ren == nil:
echo("createRenderer Error: ", getError())
quit(1)
bmp = loadBMP("grumpy-cat.bmp")
if bmp == nil:
echo("loadBMP Error: ", getError())
quit(1)
tex = createTextureFromSurface(ren, bmp)
if tex == nil:
echo("createTextureFromSurface Error: ", getError())
quit(1)
freeSurface(bmp)
for i in countup(1, 20):
ren.clear
copy(ren, tex, nil, nil)
ren.present
delay(100)
destroy tex
destroy ren
destroy win
sdl2.quit()
main()