First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

Time:2020-5-29

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!


As a program, we all know that the time of writing code and debugging is almost 9:1, that is to say, 90% of the time everyone is debugging code. How can we find bugs as soon as possible?

Debugging is the most important way to solve problems in development. In development, many colleagues only use debugging, so write this article. It is said that if you want to be good at something, you must first make use of the tools. Good debugging skills make you feel like a fish in water in development.

Debugging is mainly local debugging and remote debugging. Local debugging is to start a project locally by using the IDE’s debug button,

Remote debugging generally refers to debugging programs on other machines. Let’s talk about these two aspects separately. Because I use idea in my development, the following examples and screenshots are all from idea. The debugging skills are the same. Students who use eclipse can migrate to eclipse themselves.

1. Breakpoint classification

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

  1. Line breakpoint

    As the name implies, a line breakpoint is a click on the left side of the code. A red dot indicates that the breakpoint is successful. It will be triggered when the code runs to this line.

  2. Method Breakpoint

    Method breakpoint is the breakpoint on the method signature, which will trigger when the method is called. As shown in the diamond red dot above

  3. Field breakpoint

    Field breakpoint is to add a breakpoint to the definition of a field. Any access or write will trigger the breakpoint.

  4. Exception Breakpoint

    Exception breakpoint in the debug view, click two small dots to open the breakpoint interface, then click the “+” sign, select Java exception breakpoints, and enter the exception name of the breakpoint you want. For example, the commonly used NullPointerException will trigger breakpoints as long as the exception is thrown when the program is running.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

2. Manage breakpoints

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

  1. Click position 1 in the figure to cancel all breakpoints, that is, all breakpoints will not be triggered.
  2. Click position 2 in the figure to open the breakpoint management interface. If the breakpoint is checked, it means the breakpoint works. If the breakpoint is not checked, it means the breakpoint does not work.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

3. Conditional breakpoint

Conditional breakpoints are too good. Everyone who writes code at the previous breakpoints should know that they are entry-level. This part is the focus of today, and also the essence.

1. Whether the enable breakpoint works has nothing to say. It is usually enabled.

2. Suspend. After that, there are two options. All means to suspend all threads of the current application, and thread means to suspend the currently running thread. Generally, when debugging multithreading, select thread, which will not affect other threads, and you can debug multithreading problems.

3. Those who know a little English know the meaning of this word,condition。 The core of this part.

When this option is checked, the code in the condition box will be run, and only true or false can be returned. A breakpoint is triggered when it is true. False is not triggered. The code of the current context can be used when entering the condition. As shown in the figure below, I = = 1 is the bar. , the breakpoint is triggered when I = 1. When debugging, you can break problems in specific situations. It is also the most commonly used function.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

4. Log is also very simple – log, that is, executing the code in evaluate and log at breakpoint. In the code box, you can call the log system or direct console to print to avoid modifying the code print output. If you only want to print and do not want to suspend the thread, you can cancel suspend, so the breakpoint will only trigger and the application will not be suspended. If you check the stack trace, the call stack will be printed, and you can see the trigger source of the breakpoint. Isn’t it great?

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

5. Remove once hit is very simple. If a breakpoint is triggered once, it will automatically fail to be deleted next time.

6. Disable until setting the following breakpoint means that the current breakpoint will not be activated until the selected breakpoint is triggered, mainly for debugging sequence. You can select a breakpoint that has already been hit.

7. The operation after the after hit breakpoint is still unavailable or always available, depending on the requirements.

8. After the instance filter is checked, only the entered instanceid runs to the code will trigger, which is not very useful. Because first you need to know the instance ID. This is the ID after the runtime observation variable area @.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

9. Class filter filters classes. Only specific classes can break points. Not often.

10. How many times does pass count run to trigger the breakpoint? It is generally used in the loop, and the breakpoint is triggered after a specific number of times. Commonly used. Avoid looking at loop code that you don’t want to see.

11. Caller filter. Triggered only when a method is called. When a method is called in multiple places, but only wants to break individual calls. Although this seems to be easy to use, it needs full signature of the input method, which can be solved by using conditional breakpoints instead of using. similar: org.pdool.reflect . Player.addString (ljava / Lang / string;) V, as shown below:

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

4. Skills

  1. Drop frame fallback stack.

    In the development process, when calling the protocol with the client, sometimes the client operates once and the breakpoint is reached, but some variables are not noticed. At this time, the breakpoint can be maintained. Then, go to frames and select the function you want to see again, and right-click drop frame. You can Backstack and run the function again. When using, please note that the global variables that have been modified last time cannot be rolled back.

    First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

  2. Alt + F8 runtime calculation

When running to a breakpoint code, if you want to see the values of other variables, you can use this shortcut key. You can also use this method to change the value of memory. For example this.setName (“coriander”), as long as the code you enter is not wrong.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

3. Modify variable value

When debugging, some special conditions are not easy to appear, but you want to observe the program behavior under special conditions. At this time, you can adjust the code running process by modifying variables.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

Select the variable to be modified, right-click setValue or F2, enter the value to be modified, and press enter, then the value of the object will be modified. This modification affects the entire life cycle of the object.

4. Move breakpoint

Select the breakpoint, then hold alt, drag the breakpoint with the mouse, you can move the location of the breakpoint, and the properties of the whole breakpoint will be migrated. Perfect!

5. Hot update

Hot update is to apply the new code without restarting the application. There are the following restrictions:

1. Function parameter format cannot be modified.

2. Cannot add function for class.

3. Cannot add variable for class.

4. Only the logic inside the function can be modified.

5. The method of class cannot be reduced.

After modifying the code, you can click Ctrl + Shift + F9 to update the code. In the information bar, you can see that the update is successful and several classes have been updated.

This is a very common function in development, which saves the pain of restarting all the time.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

5. Remote debugging

When the code is deployed to the test server, some test problems will pop up, but they are not found locally, or because of the data environment, or because of the special problems of the account. I want to debug at this time. It’s time for remote debugging.

To debug remotely, the first step is to open the debug port on the remote application, add it to the startup command, and then start the application.

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000

Step 2: in the local machine, in idea, run the edit configurations + number, select remote from the drop-down list, and then enter the IP address of the remote machine and the starting port.

Step 3: after the remote code and local code are connected successfully, they can be the same as debugging the local code.

First learn these debugging skills, then write code, ten times more efficient. Hematemesis!

You also know what debugging skills, remember to leave a message to me.

It’s not easy to be original. You can write an article in two weeks, share it, read it and pay attention to it. Thank you for your support.

One sentence a day

After thinking about it, a good man can't live well. All right, all right, all right. --Cangyangjiacuo

Recommended Today

The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]