Use the Eclipse platform for debugging

Source: Internet
Author: User
Tags gdb debugger
This article describes how to use the built-in debugging function of the Eclipse platform to debug your software project.

Debugging is an unavoidable task for programmers. There are many debugging methods, but in the final analysis, it is to find the code that causes the error. For example, segment failure is considered one of the most common errors in Linux applications. This error occurs when the program attempts to access the memory not allocated to it and terminates due to a segment violation. To correct this error, you need to find the line of code that raises this behavior. Once a problematic code line is found, it is helpful to know the context of the Error and Its Related values, variables, and methods. Using the debugger makes searching for this information quite simple.

Eclipse debugger and Debug View

The Eclipse platform features a built-in Java debugger that provides all the standard debugging functions, including single-step execution, breakpoint and value setting, variable and value checking, and the ability to temporarily suspend and recover threads. In addition, you can debug applications running on a remote machine. The Eclipse platform is mainly a Java development environment, but its architecture is also open to other programming languages. As shown below, the Debug view of the same Eclipse can also be used in C and C ++ programming languages.

The Eclipse Platform Workbench and its tools are built based on the Java Development Tool (JDT) component. These components provide the following functions to Eclipse:

  • Project management tools
  • Perspective and view
  • Builder, editor, search, and build Functions
  • Debugger

The Eclipse debugger is a standard plug-in included in the Eclipse binary file. Eclipse also has a special Debug view that allows you to manage program debugging and running in the workbench. It displays the stack frame of the thread on hold for each target in the debugging. Each thread in the program appears as a node of the tree, while the Debug view displays the processes of each target in the running process. If a thread is temporarily suspended, its stack frame is displayed as a child element.

Before you start using the Eclipse debugger, assume that you have installed the appropriate Java SDK/JRE (I recommend that you use Java VM 1.4) and the Eclipse Platform SDK 2.0/2.1, both of them work normally. In general, it is a good idea to use the Eclipse sample to test the debugging options. If you want to develop and debug a C/C ++ project, you also need to obtain and install the C/C ++ Development Tool (C/C ++ Development Tool, CDT ). For links to Java SDK/JRE, Eclipse platform and samples, and CDT, see references later in this article. Figure 1 shows the general view of the Debug user interface.

Figure 1. General View of the eclipse debug view User Interface



Back to Top

Debug Java

Before you can debug your project, you must fully compile and run the code. First, you need to create a running configuration for your application and make sure it starts properly. Then, you need to useRun> Debug...Menu to set debugging configuration in the same way. You also need to select the class used by the debugger as the main Java class (see figure 2 ). For a project, you can debug several configurations. After the debugger is startedRun> Debug...In a new window. You can start debugging.

Figure 2. Set the main Java class of the project in the debugging Configuration

The following are examples of the most common Eclipse debugging operations:

Set breakpoints

Eclipse automatically switches to the Debug perspective when you start the application for debugging. There is no limit. The most common debugging process is to set breakpoints to allow checks on variables and values in conditional statements and loops. To set a breakpoint in the Package Explorer view of the Java perspective, double-click the selected source code file and open it in the editor. Traverse all the code and place the cursor on the markup bar of the line containing the suspicious Code (on the left side of the editor area ). Double-click to set the breakpoint (see figure 3 ).

Figure 3. Two breakpoint tags are displayed on the left edge of the editor.

Now passRun> Debug...Start the debugging session. It is important not to place several statements in the same row, because you cannot skip or set a line breakpoint on multiple statements in the same row (see figure 4 ).

Figure 4. The view uses the arrow at the left edge to indicate the row currently being executed

Conditional breakpoint

Once you find the error, you will want to know what the program is doing before the crash. One way to do this is to execute each statement in the program in one step, one sentence at a time until the problem occurs. Sometimes it is better to run only a piece of code and terminate it in the case of a problem, so that you can check the data at this location. To achieve this, you may need to declare the conditional breakpoint that is triggered whenever the value of the expression changes (see figure 5 ). In addition, you can also use code assistance when entering a conditional expression.

Figure 5. Set a conditional breakpoint trigger

Evaluate an expression

To evaluate the expression value in the Debug perspective Editor, select an entire line with breakpoints and select the Inspect option in the context menu (see figure 6 ). The expression is evaluated in the context of the current stack frame, and the result is displayed in the Expressions view of the Display window.

Figure 6. Using the Inspect option to evaluate the expression value

View Variables

The Variables view (in the Display window) displays the variable values in the selected stack frame (see figure 7 ). To view the requested variable, expand the tree in the Variables view until you see the requested element. You can also view the Variables in the Variables view while performing code in a single step in the Debug view.

Figure 7. View variables in the Display window

When the debugger stops at a breakpoint, you canRun> Debug...Select the Step Over option from the menu to continue the debugger session (see figure 8 ). This will skip the highlighted lines of code in one step and execute the next line in the same method (or it will continue in the method that calls the current method ). Variables Changed as the result of the last step are highlighted in Color (the default Value is red). The Color used can be specified in the "Changed Variable Value Color" preference (specified by Debug Variable Views).

Figure 8. Debugger commands in the Run... menu

To temporarily suspend thread execution in the Debug view, select a running thread and clickSuspendButton. The current call stack of the thread is displayed, and the currently executed line is highlighted in the Debug perspective editor.

When a thread is suspended, place the cursor on the variable in the Java editor. The value of this variable is displayed in a small floating window. Similarly, the top stack frame of the thread is automatically selected, and the visual Variables in the stack frame are displayed in the Variables view. You can check the corresponding Variables by clicking the variable name in the Variables view.



Back to Top

Hot Swap Error Correction: Real-Time code correction

If you are running JVM 1.4 (Java Virtual Machine, Java Virtual Machine), Eclipse 2.0.2 and 2.1 provide a Hotswap Bug fix) new features (not available for JVM 1.3 or earlier-see Figure 9 ). It allows you to change the source code during a debugger Session, which is much better than a series of steps to "exit the application, change the source code, re-compile, and start another debugging session. To use this function, you only need to change the code in the editor and resume debugging. JVM 1.4 is compatible with the Java Platform Debugger Architecture (JPDA), so it can be used. JPDA provides the ability to replace modified code in running applications. Of course, this feature is extremely useful when it takes a long time to start your application or locate an error point.

Figure 9. Hot Swap error correction cannot be used in JVM 1.3 or earlier versions

If the program has not been fully executed when debugging is completed, select the Terminate option from the context menu in the Debug view. A common error is that you use Debug or Run instead of Resume in a debugger session. This will start another debugger session instead of continuing the current session.



Back to Top

Remote debugging

The Eclipse debugger provides an interesting option for debugging remote applications. It can connect to a remote VM running a Java application and connect it to the internal debugger. Processing Remote debugging sessions is very similar to local debugging. However, the remote debugging configuration must beRun> Debug...Window. Select the Remote Java Application item in the left-side view, and clickNewButton. In this way, a new remote STARTUP configuration is created, and three tabs are displayed: Connect, Source, and Common.

In the Project Field on the connect tab, select the project used as the startup preference (for source code search ). In the host domain on the connect tab, enter the IP address or domain name of the remote host that runs the Java program. In the port field on the connect tab, enter the port on which the remote VM accepts the connection. Generally, this port is specified when the remote VM is started. If you want the debugger to determine whether the terminate command is available in a remote session, you can select the allow termination of remote VM option. Select this option if you want to terminate the connected VM. Now, when you select the debug option, the debugger tries to connect to the remote VM Based on the specified address and port, and displays the result in the debug view.

If the initiator cannot connect to the VM at the specified position, an error message is displayed. Generally, the availability of the remote debugging function depends entirely on the Java VM (Virtual Machine) running on the remote host ). Figure 10 shows the setting of the connection attribute of the remote debugging session.

Figure 10. Set the connection attribute of the remote debugging session



Back to Top

Debug other languages

Java is the main language of the eclipse platform. However, the eclipse platform is also an extensible platform that supports many other languages, and the most important of which is to support C/C ++ (because of its popularity ). Eclipse supports C/C ++ through C/C ++ development tools (CDT. For more information, see references. CDT extends the standard Eclipse debug view by debugging the C/C ++ code function. Meanwhile, the CDT debug view allows you to manage the debugging of C/C ++ projects in the workbench. CDT does not contain its internal debugger, but it provides a front-end to the GNU gdb debugger that must be available locally.

After downloading and installing CDT, you only need to switch to the debug view to start debugging the current C/C ++ project. (see references, to get a link to the article about how to install CDT ). In this way, you can set breakpoints in the Code and track variables and registers at any time during execution. The eclipse debugger displays the stack frames of the pending threads for each target in your debugging. Each thread in the program appears as a node of the tree. It displays the processes of each target in the running state. Remember that gnu gdb is most effective when debugging a program with a debugging symbolic link. This is a command line parameter during compilation.-g. For more information, use-ggdbSwitch. This parameter contains the debugging symbol specific to gnu gdb.

If you want to debug the servlet, use sysdeo eclipse Tomcat launcher. This plugin enables you to manage Tomcat 4.x/ 3.3 servlet containers by creating and importing a tomcat war project ). It also registers a tomcat process in an internal Java eclipse debugger, so that you can easily debug the Tomcat application. There are several other Eclipse plug-ins that enable us to use the internal eclipse Debugger for servlet, such as cactus Eclipse plug-ins, resin plug-ins, and X-Parrots ServletExec plug-ins. You can find links to these plug-ins in the following references.



Back to Top

Conclusion

The eclipse platform provides a built-in JAVA debugger that provides standard debugging functions, including single-step execution, breakpoint and value setting, variable and value checking, and the ability to temporarily suspend and recover threads. It can also be used to debug applications running on a remote machine. The eclipse platform is mainly a Java development environment, but the debug view of the same eclipse can also be used in C and C ++ programming languages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.