I am working on a project using Eclipse CDT and am encountering what seems to be an infinite loop.
When debugging an application from within Eclipse, I can easily retrieve and examine the call stack when the process terminates abnormally (segfault being the most common one) or when I hit a breakpoint.
If I run into an infinite loop without knowing which statements are looping, a stack trace would give me a rough idea of the functions to look at. How can I tell Eclipse to get me a stack trace of whatever the process is doing right now (in the absence of a breakpoint or segfault)?
My hack for that so far is
killall -SIGSEGV $process_name
(replacing $process_name with the name of the process you are trying to debug). This will cause the process to behave as if it had segfaulted, i.e. stopping it and giving you the call stack of whatever the process was executing at the time.
Is there a cleaner way of achieving the same?
A slightly cleaner way is:
killall -SIGCONT $process_name
This sends a CONT (continue) signal to the process. The primary purpose of this signal is to continue a process after it has been sent a STOP signal. When sent to a process that isn’t stopped, it does nothing.
However, if the process is being debugged in Eclipse (which in turn relies on gdb for debugging), this will stop execution and cause a stack trace to displayed.
Unlike with -SIGSEGV (or other signals that tell the process to dump its core or terminate), you can then hit the Resume button and continue to run your process. The UX is close to what would happen after hitting a breakpoint, except Eclipse will report a different reason for suspending execution.
Note that if you are doing anything that messes with signal processing by your process, this might not work as expected.
No idea if there’s anything that can be triggered from the Eclipse UI, though.
Related
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
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.
I have started running a script on MATLAB that takes days to finish. Usually, if I changed my mind and I don't want wait for it to finish and I get content with the intermediate results, I highlight the command window and press Ctrl-C to break the code.
Now, I have run MATLAB. But its desktop got kinda stuck in the background. When I try to restore the desktop from the toolbar, it does not restore. But I know from the task manager that the process is running and is consuming Memory and CPU performance. So, I am kinda stuck. I don't want to kill the process because I need the intermediate values in the workspace, and I can't open the desktop to break the code using ctrl-c.
Is there any solution? For example, is there any command that can be used in the command prompt to act as ctrl-c for MATLAB?
I am using MATLAB R2012b and Windows 8.
Quick try to fix the recent Problem:
Try ty set a higher priority to matlab.exe in the Task Manager. (Right click -> Priority -> Higher than normal). Then see if you can get the window to front.
Some approaches to avoid this problem in future:
Try to optimize your code. For starters look at: http://de.mathworks.com/help/matlab/matlab_prog/vectorization.html
Use Matlab compiler for faster execution: http://de.mathworks.com/products/compiler/
Include some drawnow commands at stratetic positions in the code. This allows matlab to process the Event Queue and capture ctr-C commands.
Save intermediate results to output files. For example you could write an output file all 30 min with your intermediate results. Easyiest way would be just save(filename). Then a .matfile with all your workspace variables is generated. You can than kill the process in the task manager, without loosing too much results.
I would like to have fclose('all') get called whenever I force the script/function to exit with Ctrl+C. I know that for a given function I can add finishup = onCleanup(#() fclose('all')); at the beginning. However, it is tedious to do it for each function, and doesn't seem to me very neat. How can I configure MATLAB so that upon a Ctrl+C fclose('all') get called?
The motivation is to avoid this kind of messages after I ctrl+C some code:
I think the way to go here is changing your workflow a bit. Here is what I recommend:
try
% open, process and close file
catch
% close file
end
Assuming you have not abused try catch to guide your process flow you can now simply debug with dbstop if caught error, perhaps whilst mentioning the error. Now you can still interrupt the code with ctrl+C, and afterwards just let the code continue to close the file.
You can think of other solutions as well, like forcing all files to be closed, just before you require them all to be closed, but then you may need to worry about closing too many files.
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.