current position:Home>It's great to use debug a!

It's great to use debug a!

2022-02-04 16:45:22 A Remus

1. Debug brief introduction

Why use Debug?

see 12 The month is about to pass , Zhang San is lying on the bed in the university dormitory , At the moment, he is both happy and worried .

I'm happy that I can spend the new year with my girlfriend soon , Worry about yourself java There are a lot of mistakes in the final homework .

Zhang San's English is not good , I can't understand the error reporting statement of the console , So the more you think about it, the more upset you get .

All of a sudden , Zhang San seems to have inspiration , open IDEA Knocked excitedly . The roommate who is participating in the five black , I'm curious , All ran to see his computer :

It turned out that Zhang San was copying and pasting a pile of printed statements . A roommate really can't see it anymore , Ask him :“ Why don't you debug the program Debug ?”

“ You write so much system sentence , No trouble ? Finally hand in your homework , You have to delete these printed statements one by one , It's a waste of time .”

So let's write a program and don't write a pile of... As soon as we encounter problems system Print statement , It's a real waste of time .

While using Debug You can track the execution of a program , Quickly locate the location of program exceptions , Help us quickly find the wrong code .

2. Turn on Deubg

2.1 Debug Interface in mode

Let's take a look first IDEA in Debug Interface in mode :

    1. With Debug Mode start service . In development , I usually just Debug Mode run program , Easy to debug code at any time .
    1. The breakpoint , We can directly click Settings... On the left side of the row number column , You can also use shortcut keys Ctrl+F8 Set or cancel breakpoints .
    1. Debug window : When the request reaches the first breakpoint ,Debug The window will be activated .
    1. debugging Debug Button : We mainly use these buttons during debugging , Shortcut keys can be displayed on the mouse hover button .
    1. Debug Service button : Here we can open 、 close Debug Service etc. .
    1. Method area : The methods executed during debugging are shown here .
    1. Parameter area : The values of all parameters before the current breakpoint will be displayed here .

2.2 Turn on Debug

First set a breakpoint , And then to Debug mode :

notes : We can also add / Delete breakpoints .

3. Debug Debugging buttons commonly used in

3.1 Jump to the line of currently executing code

Let's first set a breakpoint on a page , Then switch to other pages , Click this button , I found that I jumped to the line where the code was executed :

3.2 Step over

Step over Just go down step by step , Skip all methods :

In the above example, even if you encounter system Printing method and method test1 Method , Will also cross over and continue to carry out .

3.3 Stepping into

If you encounter Customize Methods , Access to method interior , Will not enter the JDK Methods in class libraries .

In the example above , Yes system Method will automatically cross over , But I encountered a custom method , Then it will enter the method and execute , After execution, it will return to the calling place of the method .

3.4 Forced entry

No matter encounter JDK Class library method or custom method , Will enter the method and execute .

In the above example, no matter what happens JDK Class library system The method is still user-defined test1 Method , Will enter the method and execute .

3.5 Step out

Step out Is to return from the inside of the incoming method to the method call .

In the above example, we enter test1 Inside the method , When you click Step out After button , Back to calling test1 Method place .

3.6 Fallback breakpoint

Fallback breakpoint It means that you can go back to the call of the specified method .

In the above example, we executed test1、test2、method2 Method , But we can choose to go back directly to test1 Method .

The difference between stepping out and fallback breakpoints :

  • They all go back to the call of the method
  • Stepping out can only return to the call of the current method
  • The fallback breakpoint can return to the call of the specified method , Provided that the method has been executed .

3.7 Position at the cursor

If we write hundreds of lines of code , It also takes time to implement step by step . At this time, we can first put the cursor in a specified position , And then click Position the cursor Button , At this time, the code will immediately execute to the cursor .

In the above example, we move the mouse cursor to a line below , And then click Position the cursor Button , The code immediately executes to this line .

3.8 Calculation expression

Calculation expression It can help us calculate the return value of some expressions .

As you can see from the example above , We can use some custom parameters to calculate the return value of some methods before calling them .

4. View parameters

4.1 The line where the parameter is located will be followed by

4.2 Hover the cursor to see

The cursor hovers over the parameter , Displays information about the current variable , I use this method a lot , Particularly convenient .

4.3 stay Variables Check it out

All variables in the current method are shown here .

4.4 stay Watches Check it out

stay Watches in , Click on New Watch, Enter variables to view :

5. Conditional breakpoints

Sometimes our code contains a lot of for sentence , But when using breakpoint debugging, it will be executed many times . At this time, we can choose the breakpoint , Right click to set a condition , Only when this condition is met , The breakpoint will execute here .

In the above example, we set a condition for the breakpoint :i==50, So Debug When running the program in mode, we found that at this time i Namely 50.

6. Debug Service setting button

6.1 Execute to the next breakpoint until the end

The function of this button : If there is a breakpoint below , Just jump to the next breakpoint . without , The program execution ends .

6.2 Break mute

Sometimes we know the result when we reach a certain step , But there are a lot of breakpoints behind . I want these breakpoints to fail , But I also want to use these breakpoints for the second tracking , It can be used at this time Break mute .

6.3 see / clear breakpoints

We need to clear all breakpoints after executing the code , But it's a waste of time to clean up one by one , At this time, you can use this button to view all the set breakpoints , Or clear all breakpoints .

6.4 Return to the first breakpoint

The code executes to a certain line and wants to return to the first breakpoint :

7 Multithreaded debugging

because CPU The order of execution threads is random , But we can customize the execution of the next thread by using breakpoint debugging .

First, set the breakpoints of both threads to thread mode :

Then select the specified thread in the method area to execute :

copyright notice
author[A Remus],Please bring the original link to reprint, thank you.