C/C + Basic Debugging Technology in Linux 2 -- Program Control

Source: Internet
Author: User
Tags grep regular expression

1. Three Modes for stopping the program

Breakpoint: stops a program at a specific location.
Watchpoint: stops a program when the value of a specific memory address (or an expression involving multiple addresses) changes. Note: you cannot set an observation point for an expression or variable that is not in the stack frame. In other words, you often set the observation point after the program stops. When no monitored variable exists in the stack frame after the observation point is set, the observation point is automatically deleted.
Catchpoint: stops execution when a specific event occurs.
Note:

In the GDB document, we collectively refer to the three pause methods as breakpoint. For example, this is described in the help manual of GDB's delete command. It actually refers to the three pause methods, in this article, breakpoints is collectively referred to as three modes, which are called separately in Chinese.
When GDB executes a program to a breakpoint (which is hit), it does not execute the line pointed to by the breakpoint, but will point to the line pointed to by the breakpoint.
GDB is executed in the unit of machine commands, not in the line of program code. This may cause some confusion. The following is an example.
2. View GDB breakpoints

Command: I B = info breakpoints. The meaning of each column in the returned list is as follows:

Identi fi er: the unique identifier of breakpoints.
Type: Which of the preceding three modes does this breakpoints belong to? (breakpoint, watchpoint, catchpoint)
Disposition: The status after the breakpoints is hit next time (keep, del, dis correspond to keep, delete, not enable)
Enable Status: whether the breakpoints is enabled.
Address: the physical Address of breakpoints.
Location: indicates the row number of the file where the breakpoint is located. If it is a breakpoint, it indicates the observed variable.
3. settings of GDB Program Control

Breakpoint settings:
Set a normal breakpoint: break function/line_number/filename: function. This breakpoint is valid until it is deleted or disabled.
Set a temporary breakpoint: tbreak function/line_number/filename: function. The breakpoint is automatically deleted after it is hit once.
Set one-time breakpoint: enable once breakpoint-list. The difference between this and temporary breakpoint is that the breakpoint will not be able to be deleted after being hit once.
Set a regular expression breakpoint: rbreak regexp. Note that this regular expression is a grep regular expression, not a perl or shell regular syntax.
Set the condition breakpoint: break-args if (condition), for example, break main if argc> 1.
This is different from the observation point. As long as the observed expression or variable value changes, the program stops, and the condition breakpoint must meet the specified conditions. Conditional breakpoint is useful in debugging loops, such as break if (I = 70000 ).
Add the condition cond id condition to the added breakpoint, for example, cond 3 I = 3; to remove the condition and convert it to a normal breakpoint, use the cond id directly, for example, cond 3.
Note: Are there any parentheses outside of the condition? <, <=, =, And! Can Be Used in the condition ,! =,>, >=, &, |, &, |, ^, >>, <, +,-, X,/, %, and other operators, you can also use this method, for example, break test. c: myfunc if! Check_variable_sanity (I). The return value of the method here must be int. Otherwise, this condition will be misunderstood.
Delete breakpoint:
The delete breakpoint_list is the breakpoint ID, separated by spaces.
Delete all breakpoints
Clear deletes the breakpoint at the next GDB command to be executed
Clear function/filename: function/line_number/filename: line_number delete a breakpoint at a specific location
Enable breakpoint: enable breakpoint-list
Do not enable breakpoint: disable breakpoint-list
Skip breakpoint: ignore id numbers indicates that the breakpoint numbers times indicated by id are skipped.
Note:
If a breakpoint is set with the function name, the function overloading in C ++ will cause trouble, and the functions with the same name will be set with the breakpoint. Use the following format to set function breakpoints in C ++: TestClass: testFunc (int)
The Set breakpoint may not be the row you want to place. For example:
1: int main (void)

2 :{

3: int I;

4: I = 3;

5: return 0;

6 :}
We do not use Compiler Optimization for compilation and then load it into GDB, as shown below:
$ Gcc-g3-Wall-Wextra-o test1 test1.c
$ Gdb test1
(Gdb) break main
Breakpoint 1 at 0x6: file test1.c, line 4.
We found that #4 is not the main function entry, because this line is the first line of the function, although the machine code is generated, but GDB does not think this is helpful for debugging, so it sets the breakpoint on the code that is helpful for debugging in the first line.

The situation is even more confusing when we use Compiler Optimization to compile again, as shown below:

$ Gcc-O9-g3-Wall-Wextra-o test1 test1.c
$ Gdb test1
(Gdb) break main
Breakpoint 1 at 0x3: file test1.c, line 6.

GCC finds that the I variable has never been used, so it is ignored directly through optimization. Therefore, the code generated in the first line of the program's machine code is exactly the last line of the main function.

Therefore, we recommend that you disable the compiler optimization option during program debugging without any impact.

 

When a row has multiple breakpoints, the program will only stop once. In fact, GDB uses the one with the smallest ID.
During Multi-file debugging, GDB is often expected to set breakpoints in part of the current file, while GDB focuses on files containing the main function by default, in this case, you can use the list functionname or one-step debugging method to enter the source code of another file and set it. At this time, the row number you set is the source code for this file.
When you use code lines to set breakpoints, recompile the program and reload the program in GDB, the breakpoint may change the relative position (the number of lines pointed to by GDB) due to the change in the number of lines of your code ), in this case, using DDD to directly drag the original breakpoint is the most convenient method. It does not affect the status and conditions of the breakpoint, but only changes the position indicated by it, this saves the trouble of adding and setting a new breakpoint in a new location after del is set.
In DDD, you can also perform Redo and Undo operations on breakpoints.
Observation Point settings:
Set write observation points: watch I; watch (I | j> 12) & I> 24 & strlen (name)> 6, these are two ways to set observation points (variables or expressions ). Write observation points stop immediately after the variable value is modified by the program. Note: many platforms have hardware-supported observation points. By default, GDB is preferred. If it is temporarily unavailable, GDB will use the VM technology to implement observation points, the advantage is that the hardware speed is fast.
Set read observation point: rwatch.
Set read/write observation point: awatch
For example, in the following simple program, you can set a breakpoint at the main function entry and set an observation point when the breakpoint is hit. 1: # include <stdio. h>

2:

3: int main (int argc, char ** argv)

4 :{

5: int x = 30;

6: int y = 10;

7:

8: x = y;

9:

10: return 0;

11 :}
This is a very simple program. After a breakpoint is hit at the entrance of the main function, we can set rwatch x for variable monitoring.


Program recovery:
One-step execution:
Skip in one step: n = next skips the details of the call method and treats this row as a line of code for execution. Next 3 indicates that the next operation is performed three times in a row.
Single step: s = step to the details of the call method.
Execute to the next breakpoint: c = continue, and the program continues to execute until the next breakpoint of hit.
Execute to the next stack frame: fin = finish, and the program continues until the current stack frame is complete. This is often used to complete the so-called step-out work. When you accidentally press the step (you actually want to skip the step), you can use finish to jump out of this method. Of course, if you enter multiple layers of an iterative function, a temporary breakpoint + continue or until may be more useful. See the following for the latter.
Run the command to a machine with a higher memory address: u = until (you can keep up with funtionname/linenumber later). For the application scenario, see the following code, after we enter this loop, we want to jump out and execute the code after the loop. At this point, we can certainly set a temporary breakpoint for the first line of code after the loop, and then continue to that, however, this operation will be troublesome. The best way is to use until. This command causes the program to continue running and stops when it encounters a machine command with a higher memory address, when the loop is compiled into a machine instruction, the loop condition is placed at the bottom of the loop body, so the until is used to jump out of the loop and enter the next machine instruction (P.S. you can use the-s command of GCC to view the generated machine commands to better understand the running mode of this command): 1 :... previous code...

2: int I = 9999;

3: while (I --){

4: printf ("I is % d", I );

5:... lots of code...

6 :}

7:... future code...
Reverse program debugging:
This is a new function added after GDB7. If you find that you have missed the part you want to debug, this operation allows you to directly return the executed code for debugging without re-debugging. The following simple program is used to describe the functions of this new version: 1: # include <stdio. h>

2: void foo (){

3: printf ("inside foo ()");

4: int x = 6;

5: x + = 2;

6 :}

7:

8: int main (){

9: int x = 0;

10: x = x + 2;

11: foo ();

12: printf ("x = % d", x); & n

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.