Use of GDB

Source: Internet
Author: User
Use of GDB-general Linux technology-Linux programming and kernel information. The following is a detailed description. To use GDB for debugging, you must specify the-g or-ggdb compilation options when compiling the program.

For example:

Gcc? G main. c

Gcc? Ggdb main. c

In this way, gcc generates debugging messages when generating executable files. -G is used to generate general debugging messages, while-ggdb is used to generate debugging messages unique to GDB.

--------------------------------------------------------------------------------

Basic commands of GDB:

F (ile): Specifies an executable file for debugging. gdb reads the debugging messages of some files, such as f a.exe.

L (ist): The column program outputs the source file.

R (un): After loading the executable file to be debugged, you can run the executable file using the run Command.

B (reak): sets a breakpoint (break point). For example, B 25 sets a breakpoint in line 2 of the source program. When the program is executed to line 2, it will interrupt; you can also use B funcname and funcname as the function name. When a program calls some functions, an interruption occurs.

C (ontinue): The c command can be executed on another interrupted program until the next breakpoint or program ends.

P (rint): Enter the value of a variable. If the program defines an int aa, p aa will output the current value of aa.

N (ext): the execution is interrupted when the program is executed to the breakpoint. You can use n commands for one-step execution.

S (tep): the execution is interrupted when the program is executed to the breakpoint. You can use the s command to execute a single step into a function.

Q (uit): Exit GDB

--------------------------------------------------------------------------------

Record output logs

You may want to save the output of the GDB command to a file. Here are several commands to control the record function of GDB.

Set logging on enables the logging function.
Set logging off to Disable logging.
Set logging file to change the name of the current log file. The log file is 'db.txt '.
Set logging overwrite [on | off]
By default, GDB is added to the log file. If you set logging on to overwrite the log file, set overwrite.

Set logging redirect [on | off]
By default, GDB outputs data to terminals and log files. If you want to output only to the log file, set redirect.

Show logging
Displays the current value of log settings.

--------------------------------------------------------------------------------

Compile for debugging

To effectively call a program, you need to generate debugging information during compilation. the debugging information is stored in the target file. It describes the data types of various variables and functions, and the correspondence between the source code line number and the executable code address.

To add debugging information, you must add the '-G' option when running the gcc compiler.

Generally, the program delivered to the customer needs to be optimized and compiled using the '-O' compiler option. however, many compilers cannot process '-G' and'-O' options together. therefore, when using these compilers, it is impossible to generate optimized executable files with debugging information.

GCC, gnu c/C ++ compiler, supports '-G' with and without'-O', making debugging optimization code possible. we recommend that you always use '-G' when compiling the program '. maybe you think the program is correct, but there is no need to make this bet.

When you debug a program compiled with '-g-O', remember: The optimizer reassembles your code, the debugger shows you the actual existence (optimized code ). don't be surprised. If the actual execution path does not exactly correspond to your source file. an extreme example is: if you define a variable but don't use it, GDB won't be able to see it because the compiler has optimized it.

In some cases, '-g-O' is not like'-G', especially on a machine with command scheduling. If you have any questions, recompile and only use '-G'

--------------------------------------------------------------------------------

Program environment

The environment consists of a group of environment variables and their values. Environment variables usually record these: your user name, your home directory, your terminal type, and the search path for your program to run. Generally, you use shell to set environment variables, which will be inherited by other programs you run. During debugging, it is useful to try to run your program in a modified environment without restarting GDB.

Add directory to the beginning of the path environment variable (Executable File Search PATH) that will be passed to your program. Show paths
Display the list of executable file search paths (PATH environment variable value)

Show environment [varname] prints the value of the environment variable varname, which will be passed to your program at startup. If you do not provide the varname parameter, all the names and values of environment variables that will be passed to your program will be printed. You can abbreviated environment as env.

Set environment varname [= value] sets the value of environment variable varname to value. This change only targets your program and does not affect GDB itself. Value can be any string. The value of the environment variable is only a string. Any interpretation is your own program. The value parameter is optional. If not, the variable is set to a null value.

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.