kdb

Debugging a KDB script effectively


I have a KDB script which I want to run and debug. Essentially I want to run it line by line and also able to traverse through various function and sub-function called within a function and check variable values. Note I am looking for a way to enter into a sub-function and check for parameter value passed over to it and if needed move out of the function without executing that function line by line to speed up my debugging.

Is this possible ? Note that though I find the link https://code.kx.com/q/basics/debug/ helpful but not that effective as it does not allow me to achieve my objective.


Solution

  • Add a break point to the function you want to examine. This is no more than a variable that kdb+ will not recognize at the time of execution. This will enter the debugger and allow you to inspect the stack, defined variables and allow you to move up and down the function calls. For example:

    q)f1:{f2 x,4}
    q)f2:{break;f3 x}         // break is unknown
    q)f3:sum
    q)f1 1 2 3
    'break
      [2]  /home/pi/code/dbg.q:2: f2:{break;f3 x}
    q))x                       // Examine current x variable (inside f2)
    1 2 3 4
    q))`                       // backtick moves up the stack to the calling function (f1)
    q)x                        // Examine x as seen by f1
    1 2 3
    q)).                       // dot moves down the stack
    q))  [2]  /home/pi/code/dbg.q:2: f2:{break;f3 x}
                                      ^
    q))f3 x                    // inside f2, we can see that f3 x works
    10
    

    Hope this helps