GDB Usage Tutorial

Source: Internet
Author: User

GDB (GNU Debugger) is a command-line , powerful program debugging tool released by the GNU Open Source organization, Unix/linux operating system. GDB is a lot of commands, but we only need to master about 10 of the commands, we can basically complete the day-to-day Basic program debugging work.

Below we proceed from an example, detailed Linux under the debugging process of GDB.

#include <stdio.h>

int nglobalvar = 0;

int tempfunction (int a, int b)
{
    printf ("Tempfunction is called, a =%d, B =%d/n", A, b);
    return (A + b);
}

int main ()
{
    int n;
    n = 1;
    n++;
    n--;

    Nglobalvar + +;
    Nglobalvar =;

    printf ("n =%d, Nglobalvar =%d/n", n, Nglobalvar);

    n = tempfunction (1, 2);
    printf ("n =%d\n", n);

    return 0;
}

Save the above code in file gdb_sample.c and compile it with GCC:

GCC Gdb_sample.c-o gdb_sample-g
Using the-o parameter specifies that the compiled generated executable file name is gdb-sample and that the source code information is compiled into the executable file using the parameter-G notation. If you do not use the parameter-G, it will cause inconvenience to the following GDB debugging. Of course, if we do not have the source code of the program, naturally also cannot use the-G parameter, debugging/tracing can only be assembly code level debugging/tracking.

The following "GDB" command starts GDB, which will first display the GDB description, regardless of whether it:

The GNU gdb Red Hat Enterprise Linux (7.2-60.el6)
Copyright (C) is free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later 

The last line above (GDB) is the GDB internal command prompt, which waits for the user to enter the GDB command.

(1) Loading the debugger using the file command gdb-sample

The gdb-sample here is the executable file of the previous GCC compiler output, and GDB debugged the executable file that was eventually compiled by GCC (with the-G parameter at compile time):

(gdb) file gdb_sample
Reading symbols from/home/zhangchunhui/workspaces/c-c++/gdb_test/gdb_sample...done.

The last line indicated above has been loaded successfully;

(2) using the R command (run) to execute the debugged file,

Because no breakpoints have been set, normal execution to the end of the program;

Reading symbols From/home/zhangchunhui/workspaces/c-c++/gdb_test/gdb_sample...done.

(3) The following uses the B command (breakpoint) to set a breakpoint at the beginning of the main function, just to set a breakpoint, as if the F9 was pressed in a position in the IDE.

(GDB) b main
breakpoint 1 at 0x400510:file gdb_sample.c, line 12.
The last line above indicates that the breakpoint has been successfully set. and gives the breakpoint information: Set the breakpoint at the 12th line of the source file gdb-sample.c; This is the first breakpoint (ordinal 1) of the program; The code address at the breakpoint is 0x804835c (this value may only be valid during the debugging process).

(4) Use the R command again to execute (run) the debugger

(GDB) r 
starting program:/home/zhangchunhui/workspaces/c-c++/gdb_test/gdb_sample 

Breakpoint 1, Main () at GDB _sample.c:12  {

(5) Follow the next line of code using the S command (step)

(6) Use p + variable to print variable value

(7) Set a breakpoint at the beginning of line 21st and tempfunction function (not only the line number, but also the symbol name, depending on the-g parameter specified when GCC compiles)

(GDB) b
Breakpoint 2 at 0x40051a:file gdb_sample.c.
(GDB) b tempfunction 
Breakpoint 3 at 0x4004d0:file gdb_sample.c, line 6.

(8) using the C command to continue (Continue) to execute the debugged program

(9) D: Remove all breakpoints

(gdb) d
Delete all breakpoints? (Y or N) y

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.