Skip to content

v: $dbg statement - native V debugger REPL#20533

Merged
spytheman merged 66 commits intovlang:masterfrom
felipensp:vdbg
Jan 19, 2024
Merged

v: $dbg statement - native V debugger REPL#20533
spytheman merged 66 commits intovlang:masterfrom
felipensp:vdbg

Conversation

@felipensp
Copy link
Copy Markdown
Member

@felipensp felipensp commented Jan 14, 2024

This idea is to provide an core debugger for V devs when using $dbg; on their code.

vdbg> h
vdbg commands:
  anon?                 check if the current context is anon
  bt                    prints a backtrace
  c, continue           continue debugging
  generic?              check if the current context is generic
  heap                  show heap memory usage
  h, help, ?            show this help
  l, list [lines]       show some lines from current break (default: 3)
  mem, memory           show memory usage
  method?               check if the current context is a method
  m, mod                show current module name
  p, print <var>        prints an variable
  q, quit               exits debugging session in the code
  scope                 show the vars in the current scope
  u, unwatch <var>      unwatches a variable
  w, watch <var>        watches a variable

Example

fn debugger(a string, b bool) {
    $dbg;
}

struct Test{}


fn (t Test) debugger[T](z T) {
    $dbg;
    dump(z)
}

fn test(params map[string]string) {
    $dbg;    
}

fn test2() {
    a := 1
    if a != 0 {
        b := 2 
        if b > 1 {
            $dbg;
        }
    }
}

fn main() {
    a := {'a': 'b', 'c': 'd'}
    d := Test{}
    d.debugger(a)

    b := 'foo'
    c := true
    println('testing vdbg')
    $dbg;
    println('after debugger;')
    test(a)
    println('after test() call')
    test2()
    debugger(b, c)
    $dbg;
}

When using $dbg in the V code, the debugger REPL will be invoked when such statement is reached in the execution, bringing to you a debugger REPL.

list

Break on [main] main in bug.v:35
bug.v:35 vdbg> l
0032      b := 'foo'
0033      c := true
0034      println('testing vdbg')
0035>     $dbg;
0036      println('after debugger;')
0037      test(a)
0038      println('after test() call')
bug.v:35 vdbg> l 2
0033      c := true
0034      println('testing vdbg')
0035>     $dbg;
0036      println('after debugger;')
0037      test(a)
bug.v:35 vdbg> l 1
0034      println('testing vdbg')
0035>     $dbg;
0036      println('after debugger;')

Inspecting variables

Break on [main] test2 in bug.v:22
bug.v:22 vdbg> l
0019      if a != 0 {
0020          b := 2 
0021          if b > 1 {
0022>             $dbg;
0023          }
0024      }
0025  }
bug.v:22 vdbg> p a
a = 1 (int)
bug.v:22 vdbg> p b
b = 2 (int)
bug.v:22 vdbg> scope
b = 2 (int)
a = 1 (int)

@felipensp felipensp changed the title v: debugger statement - triggering internal debug - for dev purpose [DRAFT] v: debugger statement - triggering internal debugger - for dev purpose [DRAFT] Jan 14, 2024
@felipensp felipensp changed the title v: debugger statement - triggering internal debugger - for dev purpose [DRAFT] v: debugger statement - internal V Debugger Jan 15, 2024
@felipensp felipensp changed the title v: debugger statement - internal V Debugger v: $dbg statement - native V debugger REPL Jan 16, 2024
@felipensp felipensp marked this pull request as ready for review January 16, 2024 15:44
Comment thread vlib/debug/debug.v Outdated
@spytheman spytheman merged commit 9f6448e into vlang:master Jan 19, 2024
@smartiniOnGitHub
Copy link
Copy Markdown
Contributor

Hi guys, I think this is a great feature.
Sorry didn't found related issue (if any), just a question (sorry to post here): would it be possible to have this feature disabled by default when the code has been built with the production flag (to avoid block running applications maybe for mistake) ? Thank s a lot. Bye

@felipensp felipensp deleted the vdbg branch January 19, 2024 11:16
@felipensp
Copy link
Copy Markdown
Member Author

Hi guys, I think this is a great feature. Sorry didn't found related issue (if any), just a question (sorry to post here): would it be possible to have this feature disabled by default when the code has been built with the production flag (to avoid block running applications maybe for mistake) ? Thank s a lot. Bye

File a feature request. 👍🏻

@odiroot
Copy link
Copy Markdown

odiroot commented Jan 19, 2024

anon? check if the current context is anon

May I ask, what's this?

@felipensp
Copy link
Copy Markdown
Member Author

anon? check if the current context is anon

May I ask, what's this?

Check if the current context is an anon function.

@odiroot
Copy link
Copy Markdown

odiroot commented Jan 19, 2024

anon? check if the current context is anon

May I ask, what's this?

Check if the current context is an anon function.

Thanks. That's useful!

kbkpbot pushed a commit to kbkpbot/v that referenced this pull request Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants