Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is DeBug?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is DeBug". In daily operation, I believe many people have doubts about what is DeBug. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is DeBug?" Next, please follow the editor to study!

First, the beginning of DeBug

First take a look at the interface in Debug mode in IDEA. The following is to start Debug mode in IDEA and enter the interface after entering the breakpoint. Here is Windows, which may be a little different from the icon of Mac. Let's briefly say the eight places marked in the following figure:

Start the service in Debug mode, and the button on the left starts in Run mode. In development, I usually start Debug mode directly to make it easy to debug the code at any time.

Breakpoint: click the left button in the left line number bar, or the shortcut key Ctrl+F8 to hit / cancel the breakpoint, the color of the breakpoint line can be set by yourself.

Debug window: when the access request reaches * breakpoints, the Debug window is automatically activated. If it is not activated automatically, you can set it in the settings, as shown in figure 1.2.

Debug button: there are a total of 8 buttons, the main function of debugging corresponds to these buttons, mouse over the button to view the corresponding shortcut keys. The same function can be found in the menu bar Run, as shown in figure 1.4.

Service button: you can turn off / start the service, set breakpoints, and so on.

Method call stack: all the methods used for debugging this thread are shown here. Check the [Show All Frames] button in the upper right corner, and the methods of other libraries will not be displayed, otherwise there will be a lot of methods here.

[1] Variables: you can view variables in the current method before the current breakpoint in the variables area.

[2] Watches: view variables. You can drag variables from Variables area to Watches to view them.

[figure 1.1]

Check Show debug window on breakpoint in the settings to request that the Debug window be automatically activated after entering the breakpoint.

[figure 1.2]

If there is no toolbar or status bar at the bottom of your IDEA, you can open it in View and show the toolbar for us to use. You can try these four options yourself.

[figure 1.3]

There is a corresponding debugging function in the menu bar Run.

[figure 1.4]

Second, the basic usage & shortcut keys

The function of Debug debugging mainly corresponds to two sets of buttons in figure 1: 4 and 5:

1. First of all, let's talk about the * * group buttons, with a total of 8 buttons, from left to right as follows:

[figure 2.1]

Show Execution Point (Alt + F10): if your cursor is on another line or page, click this button to jump to the current line of code execution.

Step Over (F8): step by step, go down line by line, if there is a method in this line, it will not enter the method.

Step Into (F7): step into, if there is a method in the current line, you can enter inside the method, which is generally used to enter the custom method and will not enter the official class library, such as the put method in line 25.

Force Step Into (Alt + Shift + F7): forced entry, you can enter any method, you can use this method to enter the official class library when viewing the underlying source code.

Step Out (Shift + F8): step out, exit from the inbound method to the method call, when the method has been executed, but the assignment has not been completed.

Drop Frame (none by default): return the breakpoint, which is described in more detail in later chapters.

Run to Cursor (Alt + F9): run to the cursor, you can locate the cursor to the line you need to view, and then use this feature, the code will run to the cursor line without the need for a break point.

Evaluate _ Expression (Alt + F8): evaluate expressions, described in more detail in later chapters.

2. The second group of buttons, a total of 7 buttons, from top to bottom are as follows:

[figure 2.2]

Rerun 'xxxx': reruns the program and restarts the program after shutting down the service.

Update 'tech' application (Ctrl + F5): update program, which can be performed after your code has been changed. The corresponding operation for this function is in the service configuration, as shown in figure 2.3.

Resume Program (F9): restore the program, for example, you have two breakpoints on lines 20 and 25, currently run to line 20, press F9, then run to the next breakpoint (that is, line 25), and then press F9, you will run the whole process, because there are no breakpoints.

Pause Program: pause the program and enable Debug. No specific usage has been found so far.

Stop 'xxx' (Ctrl + F2): press twice in a row to close the program. Sometimes you will find that the report port is occupied when you shut down the service and start it again. This is because you do not shut down the service completely, so you need to kill all JVM processes.

View Breakpoints (Ctrl + Shift + F8): view all breakpoints, which will be covered in later chapters.

Mute Breakpoints: dumb breakpoint, after selecting this, all breakpoints turn gray, breakpoints fail, press F9 to run the program directly. Click again, and the breakpoint turns red and valid. If you want to invalidate only one breakpoint, you can right-click the breakpoint to cancel Enabled, as shown in figure 2.4, the breakpoint will fail.

Update program, On 'Update' actions, what you do when you perform an update operation, you usually choose' Update classes and resources','to update classes and resource files.

It is generally better to work with hot deployment plug-ins, such as JRebel, so that you do not have to restart the service every time you change the code. How to activate JRebel is attached in the * section.

The following On frame deactivation is triggered when the IDEA window loses focus, that is, when you switch from idea to the browser, idea will automatically help you do things, generally can set Do nothing, frequent switching will be more resource-consuming.

[figure 2.3]

[figure 2.4]

Third, variable view

During the Debug process, it is necessary to track and check the changes of variables. Here are a few places where variables can be viewed in IDEA, which I believe most people know.

1. As shown below, in IDEA, the value of the current variable is displayed after the line of the parameter.

[figure 3.1]

2. Hover the cursor over the parameter to display the current variable information. Click to open the details as shown in figure 3.3. I usually use this way, which is fast and convenient.

[figure 3.2]

[figure 3.3]

3. Check in Variables, where all the variables in the current method are displayed.

[figure 3.4]

4. In Watches, click New Watch and enter the variables you want to view. Or you can drag it from Variables to Watche.

[figure 3.5]

If you find that you don't have a Watches, it may be where the picture below is.

[figure 3.6]

[figure 3.7]

Fourth, calculate the expression

The evaluation expression mentioned earlier is shown in the button in figure 4.1, Evaluate _ Expression (Alt + F8). You can use this operation to evaluate the value of an expression during debugging without having to print information.

[figure 4.1]

1, press Alt + F8 or button, or, you can select an expression and then Alt + F8, pop-up window to calculate the expression, as follows, enter or click Evaluate to calculate the value of the expression.

This expression can be not only a general variable or parameter, but also a method, and when you call several methods in a line of code, you can view the return value of a method in this way.

[figure 4.2]

2, set variables, in the calculation of the expression box, you can change the value of variables, so that sometimes it is very convenient for us to debug a variety of values.

[figure 4.3]

Fifth, intelligent step into

Think about it, there are several methods in a line of code, how to choose only one method to enter. It was mentioned earlier that using Step Into (Alt + F7) or Force Step Into (Alt + Shift + F7) to enter the inside of the method, but these two operations will enter in turn according to the order of the method calls, which is troublesome.

Then the smart step is very convenient, and this function can be seen in Run, Smart Step Into (Shift + F7), as shown in figure 5.1

[figure 5.1]

Pressing Shift + F7 will automatically navigate to the current breakpoint line and list the methods that need to be entered, as shown in figure 5.2. Click the method to enter the method.

If there is only one method, enter directly, similar to Force Step Into.

[figure 5.2]

VI. Breakpoint condition setting

By setting the breakpoint condition, it stops at the breakpoint when the condition is met, otherwise it runs directly.

Usually, when we traverse a large set or array and set a breakpoint within the loop, do we want to look at the values of the variables one by one? That must be tiring. Maybe you missed it and it's worth doing it all over again.

1. Right-click on the breakpoint to directly set the condition of the current breakpoint, as shown in figure 6.1. The breakpoint will not take effect until I set exist to true.

[figure 6.1]

2. Click View Breakpoints (Ctrl + Shift + F8) to view all breakpoints.

Java Line Breakpoints shows all breakpoints. Check Condition on the right to set the conditions for breakpoints.

Check Log message to console and the current breakpoint line will be output to the console, as shown in figure 6.3

Check Evaluate and log to execute this line of code to evaluate the value of the expression and output the result to the console.

[figure 6.2]

[figure 6.3]

3. Let's talk about the Filters filter on the right, which is not commonly used in general.

Instance filters: instance filter, enter instance ID (example ID in figure 6.5), but I didn't succeed here. I don't know why. Leave a message if you know it.

Class filters: class filtering, filtered by class name, is also unsuccessful.

Pass count: used in a loop. If the breakpoint is in the loop, you can set this value. If the loop stops at the breakpoint no matter how many times, the loop will stop at the breakpoint.

[figure 6.4]

[figure 6.5]

4. Exception breakpoint. By setting exception breakpoint, when an exception that needs to be intercepted occurs in the program, it will automatically locate the exception line.

As shown in figure 6.6, click the + sign to add Java Exception Breakpoints and add exception breakpoints. Then enter the exception class that needs the breakpoint, as shown in figure 6.7, and then you can see the added exception breakpoint in Java Exception Breakpoints.

I have added a PointerException exception breakpoint here, as shown in figure 6.8. When a null pointer exception occurs, it is automatically located on the null pointer exception line.

[figure 6.6]

[figure 6.7]

[figure 6.8]

VII. Multithreading debugging

In general, when we debug, we are in a thread, step by step. But sometimes you find that in Debug, you can't even make another request?

That's because the default blocking level of IDEA in Debug is ALL, which blocks other threads and leaves other threads only when the current debug thread is finished. You can select Thread in View Breakpoints, as shown in figure 7.1, and then click Make Default to set it as the default option.

[figure 7.1]

Switch threads. In the drop-down list of Frames in figure 7.2, you can switch the current thread, as follows: I have two threads of Debug here, and the other thread goes into another Debug.

[figure 7.2]

8. Fall back to break point

When debugging, do you want to go back to the process without having to initiate another request?

1. First recognize the method call stack, as shown in figure 8.1. First, request to enter the insertDemo method of DemoController, and then call the insert method. Let's ignore the other invoke. The top method is the method where the current breakpoint is located.

[figure 8.1]

2. Breakpoint fallback

The so-called breakpoint fallback is actually going back to the beginning of the last method call, and the test in IDEA cannot fall back line by line or go back to the previous breakpoint, but back to the previous method.

There are two ways to fallback, one is the Drop Frame button (figure 8.2), step by step by the method called, including other methods of the tripartite class library (canceling the Show All Frames button will display the methods of the tripartite class library, as shown in figure 8.3).

In the second way, select the method to be rolled back on the call stack method, right-select Drop Frame (figure 8.4), go back to the last method call of the method, press F9 (Resume Program) at this time, you can see that the program has entered the breakpoint of the method.

However, it is important to note that breakpoint fallback can only go back to the process, and the state of some previous parameters / data has changed and cannot be reversed to the previous state, such as objects, collections, updated database data, and so on.

Figure [8.2]

Figure [8.3]

Figure [8.4]

9. Interrupt Debug

When you want to interrupt the request in Debug, don't go through the rest of the process?

Sometimes, when we see that the passed parameters are incorrect, we don't want to go to the later process. How to interrupt this request (the later process is to delete database data.), do we want to shut down the service restart program? Well, that's what I used to do.

Specifically, I don't see a way to interrupt the request directly (except to shut down the service), but I can avoid subsequent processes through Force Return, that is, forced return, as shown in figure 9.1.

Click Force Return, the window of Return Value pops up, and the return type of my method is Map, so I return results directly here to force the return, so there is no further process. Or you can new HashMap.

[figure 9.1]

[figure 9.2] at this point, the study of "what is DeBug" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report