intellij - not stopping on all breakpoints in multithreaded code - multithreading

I am trying to run the following code with break points as follows:
new Thread(new Runnable() {
#Override
public void run() {
System.out.println("Starting"); //breakpoint here
}
}).start();
int i = 10;
i++; //breakpoint here
when this code runs ONLY the i++ breakpoint is hit... If I remove that one, the other thread's breakpoint would be hit correctly. Why is this weird behaviour occuring?

This is documented in http://www.jetbrains.com/idea/webhelp/breakpoints-2.html:
There are certain cases when IntelliJ IDEA will not stop at a breakpoint. Consider the following situation:
Two breakpoints are set at the different methods of a class, and there suspend policy is set to All.
When one of the breakpoints is hit, some step actions are performed.
If at the time of stepping another thread hits the second breakpoint, IntelliJ IDEA will not stop there.
I copied your code example and recreated the situation.
Sure enough, like it says in the documentation, after stopping at the i++ breakpoint, if I hit F8 (step over) the program doesn't stop on the other breakpoint. But if I hit F9 (resume) the program does stop again on the other breakpoint.

I just had this problem and for the sake of others that run into this, here is the reason for this behavior and how to change it.
As Doron pointed out, there is documentation concerning this. However, the IMPORTANT thing to notice is that by default, all threads in the JVM are suspended when a breakpoint is reached.
What you are expecting (and what I was expecting) is that only the thread with the breakpoint is suspended.
This isn't what you want, nor is it what I wanted.
To change this behavior (and provide the desired behavior).
1) Create a breakpoint by left clicking in margin.
2) Press ctrl+shift+F8 (to bring up the breakpoint menu).
3) Select your breakpoint. You will see options for it.
4) Make sure "Suspend" is checked and "Thread" radio option is selected.
5) Click the "Make default" button.
Now, when you run, you'll see that breakpoints in different threads are hit.

Because the other thread is scheduled to run in the background, and when the OS thread scheduler decides to run it, it will run. When you do have a breakpoint in it, it will be hit.
It won't have started necessarily when you just run through the code, so the breakpoint at i++ is hit immediately.

Related

Modifying the conditions of all breakpoints in Visual Studio 2015+

I am debugging a large application, with multiple threads going through the parts I am interested in. I would like to use breakpoints to follow just one of these threads.
In order to do this I am using the breakpoint Filter condition, ThreadId=#. However when stopping and starting the solution, the ThreadId of the thread I would like to follow changes. I currently need to change all of my breakpoint conditions manually by right clicking them in the Breakpoint window, selecting Settings, then changing the ThreadId condition to point to the right thread.
Is there a way to automate this process?
EDIT: For my application, the best way to resolve this was to set Conditional breakpoints monitoring a particular variable which was consistent in the thread I wanted to follow, rather than filtering by thread ID.
If you mean that you want to change the Filter condition breakpoint automatically, it would have a limitation for it.
We could set it using two ways:
(1)Using the breakpoint windows as yours.
(2) Hover over the breakpoint to bring up the breakpoint’s toolbar and click the “Settings…” icon.

step without breakpoints in android studio?

I want to get a better idea how a program I have operates, and rather than print the code out and try to see which method calls which method, it'd be easier to just run a debugger that steps through every line.
But manually putting a breakpoint on every line seems a bit laborious.
Is there any option to step through every line of the program as it runs(without manually putting in all the breakpoints)?
So that I could then see, ah MainActivity launches and runs onCreate and I could see it running through the code of onCreate how it then goes to a fragment class's onCreateView method.. and so on.. I want to see who is calling who as it happens and step through.
You don't need to add breakpoints on each line. Just add a breakpoint to the entry point of your application and then simply use the step-in and step-out buttons of the debugger to execute the code line by line.
In order to prevent the debugger from stepping through the code that wasn't written by you, go to File > Settings > Debugger > Stepping and then on the right, click the plus icon with the question mark that says Add Pattern to add the following patterns:
android.*
dalvik.*
com.android.*

C++ program crashes before theApp::InitInstance()

Visual C++ 2005 Professional
Debug/Win32
Windows 7 Enterprise 64-bit
I set a breakpoint at my program's entrypoint. My program crashes before reaching that breakpoint. When the unhandled exception occurs (Access violation reading location 0x00000000.), I click on Break, and set a breakpoint where the current instruction pointer is located. Restart the debugging session and the program stops on the new breakpoint. I scroll up to the top of the current function, __tmainCRTStartup(), and set a breakpoint at the opening brace for the function.
I stop/restart the debugger again, this time stopping at __tmainCRTStartup(). By this time, all of the DLL's have been loaded, and if they needed to, run their load functions. Press [F5] to run to the original breakpoint. The Next Statement pointer is pointing at a call to WinMain(...).
Stepping over this statement causes the unhandled exception. Stepping into the statement takes me to this statement: "return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);". Stepping into once again takes me to the Disassembly window, where I see the next statement to run: "jmp AfxWinMain (563AFAh)".
I can step through the assembly code, but I cannot make out what it is trying to do before it tries to dereference a pointer in one of the registers, which holds a value of zero (0).
I read another post about a C program crashing before reaching main(). Even though tagged with "C", many responders mentioned constructors of a static instance of a class being a possible culprit. Others mentioned DLL's loading and running their own startup code that might crash the program, but the DLL's all seem to load and I have evidence of at least one DLL running its startup code, before the program gets to __tmainCRTSetup() .
Can anyone help me understand this? Knowing why it's doing this should help me understand how to fix it.
Thank you!

Debugging Multithreaded C++ program in Visual Studio 2008

I am debugging a multi-threaded program in Visual Studio 2008.
When I break in the main thread, what is the status of the other threads in the process ?
They keep executing or they break as well ?
What happens to them when I do a F10/F11 in the main thread ?
How should I proceed if I need to check which thread has changed a particular variable ?
If the debugger breaks into the process (for example via Breakpoints or "Break All"), then all threads a get suspended.
If the press F10 or F11, then a new "temporary" breakpoint is added to the next line and the process is started (resumed) again. Therefore all threads runt (for a very short time), until the breakpoint is hit.
If you need to detect how is changing a variable, you can set a "Data-Breakpoint". For a reference see What are data breakpoints?
Also take a look at the documentation: How to: Set a Data Breakpoint
Also please be aware, that you can see the list of threads by opening the "Threads"-Window (Debug|Windows|Threads)!

Thread window is empty

I am debugging console application, it has several threads running. Then why Thread Window is empty in VS 2010? I do not see any thread listed here, even main thread is not here. Do I need to enable something?
Seen in HansPassant's comment above: This is normal, please try to set a breakpoint or utilize Debug + Break All.

Resources