Debug the GCC program using GDB in Linux (1)

Source: Internet
Author: User
Article title: debug GCC program with GDB in Linux (1 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Linux contains a GNU debugging program called gdb. gdb is a powerful debugger used to debug C and C ++ programs. it enables you to observe the internal structure and memory usage of the program while the program is running. the following are some functions provided by gdb:
  
It enables you to monitor the value of variables in your program.
  
It enables you to set breakpoints so that the program stops running on the specified code line.
  
It enables you to execute your code in one row.
  
Type gdb on the command line and press the Enter key to run gdb. if everything works properly, gdb will be started and you will see similar content on the screen:
  
GDB is free software and you are welcome to distribute copies of it
Under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.14 (i486-slakware-linux), Copyright 1995 Free Software Foundation, Inc.
(Gdb)
  
After you start gdb, you can specify many options on the command line. you can also run gdb in the following ways:
Gdb
  
When you run gdb in this way, you can directly specify the program to be debugged. this tells gdb to mount the executable file named fname. you can also use gdb to check a core file generated due to program termination exceptions, or connect it to a running program. you can refer to the gdb guide or type gdb-h on the command line to get a simple list of instructions on these options.
  
Compile the Code for Debugging (Compiling Code for Debugging)
  
To make gdb work normally, you must make your program contain debugging information during compilation. the debugging information includes the type of each variable in your program, the address ing in the executable file, and the source code line number. gdb uses this information to associate the source code with the machine code. enable the debugging option with the-g option during compilation.
  
Gdb basic commands
  
Gdb supports many commands to enable different functions. these commands are loaded from simple files to complex commands that allow you to check the stack content you call. Table 1 lists some of the commands you will use when debugging with gdb.
  
Table 1. basic gdb commands.
  
Description
File: load the executable file to be debugged.
Kill to terminate the program being debugged
List part of the source code that generates the execution file
Next, execute a line of source code but do not enter the function.
Step: execute a line of source code and enter the function.
Run to execute the program currently being debugged
Quit terminate gdb
Watch enables you to monitor the value of a variable, regardless of when it is changed.
Break sets breakpoints in the code, which causes the program to be suspended when it is executed here.
Make enables you to re-generate executable files without exiting gdb.
Shell enables you to run UNIX shell commands without leaving gdb
  
Gdb supports many command editing features similar to those of UNIX shell programs. you can press the Tab key like in bash or tcsh to allow gdb to help you complete a unique command. if it is not unique, gdb will list all matching commands. you can also use the mouse key to flip up and down History commands.

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.