Introduction to gdb debugging (2): Use terminal to debug gdb

After installing gdb, you can try to use gdb to debug the program in the terminal. If you have doubts about installing gdb, please refer to gdb debugging entry (1): install gdb under Windows / Linux / Ubuntu .

1. Create or locate the .c file that needs to be debugged

First write a simple helloworld.c:

#include<stdio.h>
int main()
{
    printf("hello world! \n");
    return 0;
}

You can also locate the .c program you need to debug, and then start the next step.

2. Compile the file

Containing helloworld.cdirectory input terminal:

gcc -g helloworld.c -o helloworld

Then it can be found in this directory swells into a named helloworldexecutable file.

3. Start gdb debugging

In the terminal of the previous section, continue to enter the executable file name of the program to be debugged generated by gdb + the previous section:

gdb helloworld

You can find that the terminal has printed the following log information:

GNU gdb (GDB) 7.12
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are 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 was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from helloworld...done.

Then you can debug, such as input list, you can see the following log:

(gdb) list
1	#include<stdio.h>
2	int main()
3	{
4	    printf("hello world! \n");
5	    return 0;
6	}

Start debugging as much as you like ~

Published 56 original articles · 22 praises · 8470 visits

Guess you like

Origin blog.csdn.net/zztiger123/article/details/105544003