[Turn] with chestnuts GDB tutorial

GDB tutorial with chestnuts

Original link: http://www.cprogramming.com/gdb.html Author: Manasij Mukherjee

A good debugging software is a program ape toolbox one of the most important tools in the UNIX or Linux systems, GDB (GUN debugger) is a magnificent and popular debugging tool that allows you to run under GDB the program to do anything you love to do.
I should have read this article?

Assault learning: using gcc compiler

Gcc is Linux or * nix system comes with all the other compilers, it also has a Window interface, but on Windows or using Visual Studio's debugger is relatively simple.

Suppose your program file named main.cpp, you can compile it with the following command:

g++ main.cpp -o main

When this command is complete and generates a file called main (program error will be generated), you also need this command Riga -g, tells the compiler that you may later debug the application.

g++ main.cpp -g -Wall -Werror -o main

If this seems daunting, do not worry, you'll get used to! (If you have a lot of files, you can make or SCons to set up (build) them)
as if entering into what a strange thing, if you're curious -Wall and -Werror mean, please read -WallThis enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros. This also enables some language-specific warnings described in C ++ Dialect Options and Objective-C and Objective-C ++ Dialect Options.

-Werror

Make all warnings into errors.

GDB basis

If you have followed the above said method, compile your program in debug mode activated state, then you have to debug ready, in the following commands, please put angle brackets <> Replace the contents text corresponding with their own procedures.

Open GDB

An input terminal (terminal) in

gdb <executable name>

Do chestnuts program called the main mentioned above, the command is

gdb main

Set a breakpoint

To be able to check the procedure, you might want your program to stop at some point, you want to pause the line that the program is called breakpoint (I've kissed your face, you have both hands on my shoulders ~),

break <source code line number>

Enter the number of rows behind the break breakpoint in the source code.

Run the program

As you can guess

run

Check the code

When you pause the program, you can do a lot of important things, but the most important is undoubtedly check the contents of your neighborhood breakpoint. Related to this is the command "list", it displays 10 lines of code with breakpoints adjacent.

Next和Step

Just pause and not enough to make us a good control program, GDB also provides a single-step command "next" and "step". They both have a little different, that is "next" to ensure strict control points in the current range, and "step" will follow the implementation into the internal functions.

Please look carefully following chestnuts

value=display();
readinput();

If you use the next command After executing the first line of the second control point jump line readinput (), you can check to see the value of value of display () is working properly.

If you use the step command, you can directly track display () behavior, control point skip display () function in the first line

Check the variable

When you want to locate abnormal operation of the code which part in the program, check the value of the local variable is often a very effective method. Variables can be checked with the following command

print <var name to print>

You can also modify the value of a variable using the following command

set <var> = <value>

You can look at later modify variable values ​​if the issue is resolved, or if the program into another branch, in order to determine whether the bug comes from the wrong variable value.

Setting watchpoints

Set up monitoring points is like debugging software to make changes to the selected variables for live coverage whenever tag value is changed, the program pauses and provide detailed information to change
this command can be set to monitor a simple point:

watch <var>

The following chestnut when output variable changes GDB:

Continuing.
Hardware watchpoint 2: variable 

Old value = 0
New value = 10x08048754 at main.cpp:31
31        variable=isalpha(ch)

Tip: You can only set the scope of the variable monitoring point in it, therefore, want to monitor another function or variable inner block is that now that the scope set a breakpoint, when the program was suspended, set a watch point.

drop out

If you want to exit the program when your program is paused, use "kill" command, if you want to exit GDB, use the "quit" command

A chestnut

The following code function is to calculate the factorial of a number, but unfortunately it is wrong, our goal is to debug it to find the wrong reasons.

#include<iostream>
using namespace std;
long factorial(int n);
int main() {
    int n(0);
    cin>>n;
    long val=factorial(n);
    cout<<val;
    cin.get();
    return 0; 
}      
long factorial(int n) {
    long result(1);
    while(n--)     {
        result*=n;     
    }     
    return result; 
}

Start Debugging

Now please look carefully output of each command and, in particular monitoring point, these elements are very basic:

  • Set a breakpoint on that line function call
  • step into the function
  • The results set points and monitor the input digital
  • Finally, the results from the monitoring point and find the error
      1.$ g++ main.cpp -g -Wall -o main
      2.$ gdb main
      3.GNU gdb (GDB) Fedora (7.3-41.fc15)
      4.Copyright (C) 2011 Free Software Foundation, Inc.
      5.This GDB was configured as "i686-redhat-linux-gnu".
      6.For bug reporting instructions, please see:
      7.<http://www.gnu.org/software/gdb/bugs/>...
      8.Reading symbols from /home/manasij7479/Documents/main...done.
      9.<strong>(gdb) break 11</strong>
      10.Breakpoint 1 at 0x80485f9: file main.cpp, line 11.
      11.     <strong>(gdb) run</strong>
      12.     Starting program: /home/manasij7479/Documents/main
      13.     3
      14.     
      15.     Breakpoint 1, main () at main.cpp:11
      16.     11        long val=factorial(n);
      17.     <strong>(gdb) step</strong>
      18.     factorial (n=3) at main.cpp:1919.     
      19        long result(1);
      20.     <strong>(gdb) list</strong>
      21.     14        return 0;
      22.     15      }
      23.     16
      24.     17      long factorial(int n)
      25.     18      {
      26.     19        long result(1);
      27.     20        while(n--)
      28.     21        {
      29.     22          result*=n;
      30.     23        }
      31.     <strong>(gdb) watch n</strong>
      32.     Hardware watchpoint 2: n
      33.     <strong>(gdb) watch result</strong>
      34.     Hardware watchpoint 3: result
      35.     <strong>(gdb) continue</strong>
      36.     Continuing.
      37.     Hardware watchpoint 3: result
      38.     
      39.     Old value = 0
      40.     New value = 1

He noted that the result is 0 then begin initialized to 1.

41.     factorial (n=3) at main.cpp:20
42.     20        while(n--)43.     (gdb)

Note that not enter a new command, GDB will again hit Enter on a command execution

44.     Continuing.
45.     Hardware watchpoint 2: n
46.     
47.     Old value = 3
48.     New value = 2

N noticed immediately turned from 3 2
now become 2 result (because the previous value and the result is multiplied by n), we find the first bug! should result with 3 2 1. evaluated, but there is never started by 2. We should be slightly modified cycle to fix it, but you can take a look at the other part of the calculation is correct before you modify it.

57.     factorial (n=2) at main.cpp:20
58.     20        while(n--)
59.     (gdb)
60.     Continuing.
61.     Hardware watchpoint 2: n
62.     
63.     Old value = 2
64.     New value = 1

n from 2 becomes 1, result does not change (since. 1 = n)
n from 2 becomes 0

73.     0x08048654 in factorial (n=0) at main.cpp:20
74.     20        while(n--)
75.     (gdb)
76.     Continuing.
77.     Hardware watchpoint 3: result
78.     
79.     Old value = 2
80.     New value = 0

0 result now has become, as it multiplied with the value of n now. Another bug! Cycle should be aborted before it becomes 0 at n.
Now n = -1 so the loop is no longer running because n-- cycling conditions are not satisfied, the function returns the value of what happens when the current result of 0, let us look at the function exits.

93.     
94.     Watchpoint 2 deleted because the program has left the block in
95.     which its expression is valid.
96.     
97.     Watchpoint 3 deleted because the program has left the block in
98.     which its expression is valid.

This is what variables are monitored after leaving scope occur.
a print val garbage output value, as gdb points to a row before performing the function.

103.    (gdb) next
104.    12        cout<<val;
105.    (gdb) continue
106.    Continuing.
107.    0[Inferior 1 (process 2499) exited normally]
108.    (gdb) quit

Here is the corrected program:

while(n>0) //doesn't let n reach 0
{    
    result*=n;    
    n--;        //decrements only after the evaluation
}

GDB summary

Now you can own to try GDB, in order to simplify the tutorial, something important did not mention, for example, mistakes and errors some pointers or use Valgrind to find memory leaks.

Remember that GDB offers an excellent help system, enter help at the (gdb) mode, there will be help options. If you want to know specific commands, you can use the command:

help <command>

Another important point is to use (e.g., q represents quit) shortcuts, GDB in the case where the word order unambiguous command shortcuts.

After learning of the GDB, the next time your program go crazy you do not panic, and now you have an arsenal of weapons Cock Cock.

Guess you like

Origin www.cnblogs.com/tyner/p/11391001.html