Using GDB to debug C program

Source: Internet
Author: User

#include <stdio.h>static void display (int i, int *ptr); int main (void) {   int x = 5;   int *xptr = &x;   printf ("in Main (): \ n");   printf ("   x is%d and was stored at%p.\n", X, &x);   printf ("   xptr points to%p which holds%d.\n", xptr, *xptr);   Display (x, xptr);   return 0;} void display (int z, int *zptr) {   printf ("in display (): \ n");   printf ("   Z is%d and was stored at%p.\n", Z, &z);   printf ("   zptr points to%p which holds%d.\n", zptr, *zptr);}


Use the file above, such as test.c

1, compile. Gcc-g-O testx test.c

2, run. GDB TESTX

GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I686-linux-gnu".
Type "Show Configuration" for configuration details.
For bugs reporting instructions, please see:
Find the GDB manual and other documentation resources online at:
For help, type ' help '.
Type "Apropos word" to search for commands related to "word" ...
Reading symbols from Testx...done.
(GDB)


3, set the breakpoint. Gdb> B 8

Breakpoint 1 at 0x8048472:file test.c, line 8.

4, set breakpoint 2. Gdb> B 10

Breakpoint 2 at 0x80484ac:file test.c, line 10.

5, execute. Gdb> Run

Starting program:/HOME/WANG/MYTEST/GDBTEST/TESTX
In Main ():


Breakpoint 1, Main () at Test.c:8
8 printf ("X is%d and was stored at%p.\n", X, &x);

6, continue. Gdb> C

Continuing.
X is 5 and was stored at 0xbfffeb58.
Xptr points to 0xbfffeb58 which holds 5.


Breakpoint 2, Main () at Test.c:10
Ten display (x, xptr);

7, continue. Gdb> C

Continuing.
In Display ():
Z is 5 and was stored at 0XBFFFEB40.
Zptr points to 0xbfffeb58 which holds 5.
[Inferior 1 (process 11616) exited normally]




9, for step, using step

For BackTrace, using BT


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Using GDB to debug C program

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.