Use GDB debugging procedures (2)

zhaozj2021-02-08  248

GDB command outline -------

After starting the GDB, you can use the GDB command to start the debugger using the GDB command, and the gdb command can use the HELP command to view, as shown below:

/ Home / hchen> gdb GNU gdb 5.1.1 Copyright 2002 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and / or 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. This GDB WAS Configured AS "i386-suse-linux". (GDB) Help List of classs of Commands:

aliases - Aliases of other commands breakpoints - Making program stop at certain points data - Examining data files - Specifying and examining files internals - Maintenance commands obscure - Obscure features running - Running the program stack - Examining the stack Status - Status Inquiries Support - Support Facilities TracePoints - Tracing of Program Execution WITHOUT Stopping The Program User-Defined - User-Defined Commands

Type "Help" Followed by a class name for a list of commists in That class. Type "Help" Followed by Command Name for Full Documentation. Command name abbreviations are allowed if unambiguous. (GDB)

There are a lot of GDB commands, and GDB is divided into many types. The HELP command is just a command type of GDB. If you want to see the command in the species, you can use the help command, such as: Help Breakpoints, view all commands for setting breakpoints. You can also directly help to view the help of the command.

In the GDB, when entering the command, you can use the first few characters that only use the command, of course, the first few characters of the command should mark a unique command, under Linux, you can knock The full name of the Tab key to make up the command, if there is repetitive, then GDB will come out. Example 1: Set a breakpoint when entering the function FUNC. Can be knocked into Break func, or is directly b func breakpoint 1 at 0x8048458: file hello.c, line 10. Example 2: Typing B Press twice Tab keys, you will see all B heads Command: (GDB) B Backtrace Break BT (GDB) Example 3: Remember the function's prefix, so that: (gdb) b beam_ , press the next Tab button, you will see :) make_a_section_from_file make_environ make_abs_section make_function_type make_blockvector make_pointer_type make_cleanup make_reference_type make_command make_symbol_completion_list (gdb) b make_ GDB all the functions that begin to make out all the cases you see.

Example 4: When debugging the C program, there is a function name. Such as: (GDB) b 'Bubble (INT, INT) B' BUBBLE (You can see all overload functions and parameters in C . (Note: m -? And "according to the Tab key" "

When you want to quit GDB, just send quit or command to Q.

Running UNIX in GDB ------------

In the GDB environment, you can execute the unix shell command, complete using the gdb's shell command:

Shell Call Unix's shell to perform , UNIX's shell defined in the environment variable shell will be used to perform if shell is not defined, then using UNIX standard shell: / BIN / SH. (Using Command.com or cmd.exe in Windows)

Another GDB command is make: make You can execute your Make command in GDB to re-build your own program. This order is equivalent to "shell make ".

Run out in GDB --------

When GDB is started in GDB , the GDB searches for the source files of in the Path path and the current directory. To confirm if the GDB reads the source file, you can use the L or List command to see if the GDB can list the source code.

In GDB, run the program using R or the run command. The operation of the program, you may need to set the following four aspects. 1, the program runs the parameters. Set Args Specifies the runtime parameter. (, Such as: SET ARGS 10 20 30 40 50) The show args command can view the setup run parameters.

2, the operating environment. Path

can set the running path of the program. Show paths View the runtime path of the program. Set Environment VarName [= value] Set the environment variable. Such as: set env user = hchen show environment [varName] View environment variables.

3, work catalog. CD

is equivalent to the CD command of the shell. The PWD displays the current directory.

4, the input and output of the program. INFO TERMINAL displays the mode of the terminal you use. Use the redirection control program to output. Such as: run> outfile tty command can write the terminal device input and output. Such as: TTY / DEV / TTYB

Debugging the running program --------

Two methods: 1. View the PID (process ID) of the running program with PS under UNIX, and then mount the running program with GDB PID format. 2, first use GDB to associate upward upward upward up, GDB, use the attach command in GDB to hook the PID of the process. And use DETACH to cancel the process.

Pause / recovery program run ---------

In the debugger, the suspension program is required, and GDB can easily pause the operation of the program. You can set the program where you have stopped, stop under what conditions, or stop when you receive a signal. To allow you to view runtime variables, as well as runtime processes.

When the process is parked by GDB, you can use Info Program to see if the program is running, the process number, and is suspended.

In GDB, we can have the following pauses: breakpoint, watchpoint, catchpoint, signal (Signals), thread stops. If you want to recover the program, you can use C or the Continue command.

First, set breakpoints, we use the break command to set the breakpoint. There are several ways to set breakpoints on the front: BREAK Announced when entering the specified function. You can specify the function name using the Class :: function or function (type, type, type) format in C .

Break Announced in the specified line number.

Break Offset Break -offset Announces the offset line in front of the current line number. OFFSET is natural number.

Break FileName: Linenum Announced on the Linenum line of the source file filename.

Break filename: Function is stopped at the entrance to the function function of the source file filename.

Break * address stops at the memory address running.

When the BREAK BREAK command does not have a parameter, it is said to stop at the next instruction.

Break ... if ... can be the above parameters, Condition represents the condition, stop when the condition is set up. For example, in the circulatory body, Break if i = 100 can be set, indicating that the program is stopped when I is 100. When you check the breakpoint, you can use the info command, as shown below: (Note: n) Info Breakpoints [N] Info Break [N]

Second, setting the WatchPoint observation point General to observe if a certain expression (variable is also an expression) value varies, if there is a change, stop the program immediately. We have the following ways to set the observation point: Watch sets an observation point (variable) EXPR. When the one-volume expression value changes, the program is stopped immediately. RWATCH When the expression (variable) expr is read, the program is stopped. AWATCH stops the program when the value of the expression (variable) is read or written. INFO WATCHPOINTS lists all the observation points currently set.

Third, set the capture point (CatchPoint)

You can set the capture point to make some events at runtime. Such as: Load shared library (dynamic link library) or C exception. Set the format of the capture point to: catch When Event occurs, stop the program. Event can be below: 1. Throw a C throw an exception. (Throw is key) 2, Catch a C capture exception. (CATCH is key) 3, the Exec call system calls EXEC. (EXEC is keyword, currently this feature is only useful under HP-UX) 4. When the Fork call system calls fork. (Fork is keyword, this feature is currently useful under HP-UX) 5, when the VFORK call system calls vfork. (Vfork is keyword, this feature is currently useful under HP-UX) 6, LOAD or LOAD When loading a shared library (dynamic link library). (LOAD is keyword, this function is currently useful under HP-UX) 7, unload or unload When you uninstall the shared library (dynamic link library). (Unload is keyword, currently this function is only useful under HP-UX)

Tcatch Only a capture point is set, and after the program is stopped, the should be automatically deleted.

<- Previous Next->

(All rights reserved, please indicate the author and the source when reproduced)

转载请注明原文地址:https://www.9cbs.com/read-1143.html

New Post(0)