Why does the debugger in KDB sometimes not show in 'interactive' mode? - kdb

I've got a function where I'm throwing a deliberate error to pause execution and enter interactive debug.
I have set \e 1.
When the KDB REPL encounters it, it pauses execution as expected, but the debugger doesn't actually appear. When I finally enter \, it prints the debugger output, like it was printing to an invisible terminal, but then drops to the normal namespace.
What am I doing wrong?
Video of the problem:
https://www.loom.com/share/db9ee83e7d8e4fb7bb6b454b49ec9cdc

Related

forcing matlab not to pause on warnings

I have encountered a weird situation recently: when I am running my programs, sometimes the execution pauses when it reaches some warning statements (yet, sometimes it doesn't stop in identical situations, and just outputs the warning statement). I cant force the program not to stop. when I click on the small triangle below the "run" or "Breakpoints" in the editor "stop on warning" is sometimes checked, If I remove its tick, it becomes checked again after a while and the program stops on some warnings!
Has anybody encountered similar issue? is there a way to force the program not stop (maybe using some code)
Programmatically, you can use dbstop and dbclear to set and clear break conditions.
In your case, use dbclear if warning.
And see https://www.mathworks.com/help/matlab/ref/dbclear.html

step out and continue in MATLAB debugging

What is the difference between "step out (shift+F11)" and "continue (F)" in MATLAB debugger? When I debug a MATLAB function, both are triggering the cursor to come out of the loop. What is the exact difference between these two?
Continue (dbcont) will cause the program to resume execution and will only stop once it encounters another breakpoint.
Step-Out (dbstep out) will cause the currently executing function to continue and will automatically pause in the calling function regardless of whether you have a breakpoint there or not. Notice that this is referring to stepping out of a function and not a loop.
In your case, I'm assuming you only have a single function (or script) and therefore if you have no manual breakpoints after calling dbcont or dbstep out, they are going to have the same result since they will both just run the rest of the script to completion.

Show Line Number in Error Message

On most instances of MATLAB I've used, whenever I had a bug in my code the error message in the command window would show the line number.
However on the computer I am currently using, it shows me only the following:
??? Subscripted assignment dimension mismatch.
Is there anyway to get the line number to show again instead of ?????
What you are doing is using Run Section or Run and Advance as #canzar said. If you run scripts like this there is no 'line number', just as when you copy-paste code and run it in the command-window will not show you the line number on the error.
If you run the script using run or by pressing F5 it does know the line number and then prints that on the error statement. Good to know for debugging is to go to the tab editor->breakpoints->dbstop on error. If you press that it will keep your variables as present when the memory occurs, as opposed to throwing everything out when debugging functions.

Difference between script and matlab command window

I wonder what the difference is between entering a few lines in the command window, or letting a script execute them.
In the question Escape from nested try - catch statement I have an example function. I have put the selected code in a script and called it, however then it does not work properly. On the other hand, when I
select the lines and hit f9, it works as expected.
The lines are:
dbclear all
dbquit
dbstop if caught error
I call the example function as such:
dbstop if caught error
mytestmain
And the example function is:
function mytestmain
try
mytestsub
catch
end
% Definition of subfunction, may or may not be in the same .m file
function mytestsub
try
a=b; %Intentionally generate an error as b is not defined
catch
end
I think it's related to MATLAB's just-in-time (JIT) compiler, which compiles functions before it runs them.
It seems that it compiles functions differently if dbstop is set or not (see here for reference). As it currently stands, MATLAB can not recompile a function while it is run (just try saving a changed function during a dbstop, and you will get a message informing you). As you can add and remove breakpoints during a dbstop I think you can also do so programmatically, but it should be impossible to "turn on" debugging if it wasn't turned on at "compile time"
So in your cases:
Using F9 it's just pasted and parsed as if you input it manually. So first dbstop is set, then mytestmain gets compiled and executed.
Running as a script will first compile the script and mytestmain and then execute it - so dbstop would be set after compilation and therefore not in effect.
Depending on what you mean by "doesn't work", it could just be because the debugger is a special context and certain debugger commands - dbup, dbdown, and dbquit - only work when you're at a debugger "K>>" prompt. Once you call a script, you're no longer at the debugger prompt but in normal code execution - inside a nested M-code call stack - and they just don't work there. When you F9, it does the lines individually, so each one is done from the prompt.
As a workaround, if you really want to execute a sequence of debugger commands like this, you could write a little Java Swing widget to enter the text in to the command window just as though you were typing it in.

debug the last sentence of a program

I have used Eclipse and VS. When I insert a breakpoint and debug the program, it will stop before the breakpoint.
But what if I want to debug the effect of the last sentence of the program? Inserting a meaningless sentence(say print 'pause' in Python) is OK but seems awkward. Is there any alternatives?
In Visual Studio you can put break point on closing bracket in main (or any) method of Program (or any) class (default naming, may vary), then debugger stops just before closing application.
Is there any reason not to use a breakpoint on the last statement, like you said, then manually proceed one step? Depending on the debugger, this can also be automated. In GDB, one can use commands N, where N is the breakpoint number, to set a list of (debugger) commands to be executed immediately after a breakpoint is hit.