4

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.

  1. Why is that?
  2. How to properly setup and debug Nim variables consistently in VSCode?
  3. 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()
AlfredBaudisch
  • 760
  • 1
  • 11
  • 20
  • I haven't tested this, but does it have something to do with global variables or is it specifically with those Ptr types? – hola Apr 17 '23 at 23:16
  • @hola it seems to be specific to the Ptr types. Since the global TestObj from test.nim is debuggable. – AlfredBaudisch Apr 18 '23 at 06:16

0 Answers0