IntelliJ IDEA debugging skills





1. Conditional breakpoint

This technique is often used in loops, such as: in the process of traversing a large List, you want to stop the breakpoint at a certain value.

Insert picture description here
Refer to the figure above, right click on the small red dot next to the breakpoint at the breakpoint, and an interface will appear, fill in the breakpoint condition in Condition, so that it will automatically stop at i=10 when debugging

Insert picture description here


2. Return to "method calling place"

This technique is most suitable for the scenes of particularly complex methods. I finally got up to run. I accidentally shook my hand and the breakpoint passed. I want to look back at the variable value just now. If I don’t know this technique, I can only run again. Again.

Insert picture description here
Refer to the figure above, method1 method calls method2, the current breakpoint position j=100, after clicking the Drop Frame icon at the position of the red arrow in the figure above, time has passed

Insert picture description here
Back when method1 was first called, the variable i became 99.

Note: Curiosity is the ladder of human progress. If you want to know why this function is called Drop Frame, instead of something like Back To Previous, you can go to the JVM book. The JVM saves the running of threads in stack frames. State, drop frame means throwing away the currently running stack frame, so that the current "pointer" position will naturally reach the position of the previous frame.


3. Multi-threaded debugging

When multiple threads are running at the same time, whoever executes first and who executes later depends entirely on the mood of the CPU. You can’t control the order. There may be no problems when running, but debugging is more troublesome. The most obvious thing is that breakpoints jump randomly and stop for a while. This thread will stop in another thread for a while, as shown below:

Insert picture description here
If you want to hope that the next breakpoint is the second verse, you may be disappointed:

Insert picture description here
If you want a thread to stop when you want to debug, you can stop it on which thread it stops in which thread, you can right-click on the small red dots of the 3 breakpoints in the figure,

Insert picture description here
That is: Suspend suspends the condition for each thread, not All. After setting these 3 breakpoints in this way, try again

Insert picture description here
Pay attention to the position of the red box in the above figure. When the breakpoint stops, the drop-down box can see each thread (Note: It is a good habit to give the thread an easily recognizable name!), we can select the thread "birds in the sky"

Insert picture description here
The breakpoint stopped at the second poem as expected.


4. Temporarily execute the expression/modify the running value of the variable

When debugging, you can temporarily execute some expressions, refer to the figure below: click on any one of these two icons.

Insert picture description here
After clicking the + sign, you can enter the expression in the newly appeared input box, such as i+5

Insert picture description here
Then press Enter, you can see the result immediately

Insert picture description here
Of course, if you want to dynamically modify the value of a variable when debugging, it is also very easy to right-click on the variable and select Set Value. Everyone knows the rest.

Insert picture description here

Guess you like

Origin blog.csdn.net/QiuHaoqian/article/details/112307809