admin 管理员组

文章数量: 887021


2023年12月17日发(作者:gulp使用教程)

gdb调试方法说明(GDB debug method instructions)

GDB debugging essence and examples

List of column files

(GDB) list, Line1, line2

Two: executive procedure

To run the program for debugging, you can use the run command,

behind it can follow to any of the parameters of the program,

including the standard input and output descriptor (< and >)

and shell wildcards (* and? [[]]).

If you use the run command without arguments, it is useful for

GDB to use the parameters you gave to the previous run command

again.

Using the set args command, you can modify the parameters that

are sent to the program, and you can use the show args command

to see a list of its default parameters.

(GDB) set args - B - x

(GDB) show args

The backtrace command provides a backward tracking function for

the stack.

The Backtrace command generates a list that contains the

parameters that start with the recent process, so the effective

procedure and the parameters that call them.

Three: display data

Using the print command, you can check the values of each

variable.

(GDB) print p (P is a variable name)

The whatis command displays the type of a variable

(GDB) whatis p

Type = int *

Print is a powerful command of GDB that uses it to display any

valid expressions in the language being debugged. In addition

to containing variables in your program, expressions can

include the following:

L calls to functions in a program

(GDB) print find_entry (1,0)

L data structures and other complex objects

(GDB) print *table_start

$8={e=reference= '000', location=0x0, next=0x0}

Historical components of L values

(GDB) print $1 ($1 is a history variable, which can be

referenced directly later on $1)

L artificial array

A human array provides a way to display the contents of a memory

block (array, section, or dynamically allocated storage). The

early debugger didn't have a good way of changing any pointer

to an array. Just as with arguments, let's look at the 10

integers in memory after the variable H, and the syntax of a

dynamic array, as shown below:

Base@length

Therefore, you can use h@10 to display the 10 elements behind

h:

(GDB) print h@10

$13= (-1345,23, -234,0,0,0,98345,10)

Four: breakpoint (breakpoint)

The break command, which can be abbreviated as B, can be used

to set breakpoints in the debugger, which has the following four

forms:

L break line-number stops the program just before executing a

given row.

The L break function-name stops the program just before

entering the specified function.

L break line-or-function if condition, if condition (condition)

is true, the program stops when it arrives at the specified row

or function.

L break routine-name sets the breakpoint at the entrance of the

specified routine

If the program is made up of many original files, you can set

breakpoints in each of the original files instead of setting

breakpoints in the current original file:

(GDB) break filename:line-number

(GDB) break filename:function-name

To set a conditional breakpoint, you can use the break if

command as follows:

(GDB) break, line-or-function, if, expr

Cases:

(GDB) break 46, if, testsize==100

Continue running from breakpoint: countinue command

Five. Breakpoint management

1. displays the breakpoint information for the current gdb:

(GDB) info break

He displays all breakpoint information in the form as follows:

Num, Type, Disp, Enb, Address, What

"Breakpoint keep y 0x000028bc in init_random at qsort2.c:155"

"Breakpoint keep y 0x0000291c in init_organ at qsort2.c:168"

(GDB)

2. deletes a specified breakpoint:

(GDB) delete breakpoint 1

该命令将会删除编号为1的断点, 如果不带编号参数, 将删除所有的断点

Delete breakpoint (GDB)

3.禁止使用某个断点

(GDB) disable breakpoint 1

该命令将禁止断点 1, 同时断点信息的 (ENB) 域将变为 n

4.允许使用某个断点

(GDB) enable breakpoint 1

该命令将允许断点 1, 同时断点信息的 (ENB) 域将变为 y

5.清除原文件中某一代码行上的所有断点

The clean number (GDB)

注: number 为原文件的某个代码行的行号

六.变量的检查和赋值

L: 识别数组或变量的类型 Whatis

L ptype: 比whatis的功能更强, 他可以提供一个结构的定义

L set variable: 将值赋予变量

L print 除了显示一个变量的值外, 还可以用来赋值

七.单步执行

L next

不进入的单步执行

L step

进入的单步执行

如果已经进入了某函数, 而想退出该函数返回到它的调用函数中,

可使用命令finish

八.函数的调用

L call name 调用和执行一个函数

(GDB) call Gen _ and _ sork (1234,1,0)

(GDB) call printf ("ABCD")

$1 = 4

L finish 结束执行当前函数, 显示其返回值 (如果有的话)

九.机器语言工具

有一组专用的gdb变量可以用来检查和修改计算机的通用寄存器,

gdb提供了目前每一台计算机中实际使用的4个寄存器的标准名字:

L $PC: 程序计数器

L $FP: 帧指针 (当前堆栈帧)

L $SP: 栈指针

L $PS: 处理器状态

十.信号

Gdb通常可以捕捉到发送给它的大多数信号, 通过捕捉信号, 它就可决定对于正在运行的进程要做些什么工作.例如, 按ctrl - c将中断信号发送给gdb, 通常就会终止gdb.但是你或许不想中断gdb, 真正的目的是要中断gdb正在运行的程序, 因此, gdb要抓住该信号并停止它正在运行的程序, 这样就可以执行某些调试操作.

Handle命令可控制信号的处理, 他有两个参数, 一个是信号名, 另一个是接受到信号时该作什么.几种可能的参数是:

L Nostop 接收到信号时, 不要将它发送给程序, 也不要停止程序.

L stop 接受到信号时停止程序的执行, 从而允许程序调试; 显示一条表示已接受到信号的消息 (禁止使用消息除外)

L print 接受到信号时显示一条消息

L noprint 接受到信号时不要显示消息 (而且隐含着不停止程序运行)

L pass 将信号发送给程序, 从而允许你的程序去处理它、停止运行或采取别的动作.

L nopass 停止程序运行, 但不要将信号发送给程序.

例如, 假定你截获sigpipe信号, 以防止正在调试的程序接受到该信号, 而且只要该信号一到达, 就要求该程序停止, 并通知你.要完成这一任务, 可利用如下命令:

(GDB) handle SIGPIPE stop print

请注意, 你可以用信号编号替代信号名 unix的信号名总是采用大写

字母!

如果你的程序要执行任何信号处理操作, 就需要能够测试其信号处理程序, 为此, 就需要一种能将信号发送给程序的简便方法, 这就是signal命令的任务.该 命令的参数是一个数字或者一个名字, 如sigint.假定你的程序已将一个专用的sigint (键盘输入, 或ctrl -

C;

Signal 2) the signal handler is programmed to take a cleanup

action. If you want to test the signal handler, you can set a

breakpoint and use the following command:

(GDB) signal 2

Continuing, with, signal, SIGINT (2)

The program continues, but immediately transmits the signal,

and the handler starts running

Eleven. Search for original files

Search text:, this command can be displayed in the current file,

including the next line of the text string.

Reverse-search text:, this command displays the previous line

containing text.

interface

The shell command starts the UNIX shell, and the CTRL-D exits

the shell and returns to gdb.

Thirteen. The history of command

To allow the use of historical commands, use the set history

expansion on command

(GDB) set, history, expansion, on

Summary: common GDB commands

The backtrace displays the current location in the program and

the stack trace indicating how to reach the current location

(synonyms: where)

Breakpoint sets a breakpoint in the program

CD changes the current working directory

Clear deletes the breakpoint at the stop just now

When the commands hits the breakpoint, list the commands that

will be executed

Continue starts from breakpoint and continues execution

Delete deletes a breakpoint or monitoring point; it can also

be used with other commands

When the display program stops, variables and expressions are

displayed

Down moves down the stack frame so that another function becomes

the current function

Frame selects the frame for the next continue command

Info displays various information related to the program

Jump starts running at another point in the source program

Kill abort the program running under GDB control

List lists the contents of the original file corresponding to

the program being executed

Next executes the next source line, thus executing a function

in its entirety

Print displays the value of a variable or expression

PWD displays the current working directory

Pype displays the content of a data structure, such as a

structure or C++ class

Quit quit GDB

Reverse-search searches the source file in reverse for regular

expressions

Run executes the program

Search searches for regular expressions in source files

Set variable assign values to variables

Signal sends a signal to a running process

Step executes the next source line and, if necessary, goes to

the next function

Undisplay display command counter command, do not display the

expression

Until ends the current loop

Up moves up the stack frame so that another function becomes

the current function

Watch sets up a monitoring point (i.e., data breakpoint) in the

program

Whatis displays variables or function types

****************************************************

The debugger for GNU, called GDB, is an interactive tool that

works in character mode. In the X Window system, there is a GDB

front end graphical tool called xxgdb. GDB is a powerful

debugger that performs the following debugging tasks:

* setting breakpoints;

* monitoring the value of program variables;

* a single step of the program;

* modify the value of a variable.

Before you can use the GDB debugger, you must compile the source

file using the -g option. You can define the CFLAGS variable

in makefile as follows:

CFLAGS = -g

When running the GDB debugger, you use the following command:

GDB progname

Typing help at the GDB prompt lists the categories of commands,

and the main categories are:

* aliases: Command alias

* breakpoints: breakpoint definition;

* data: data view;

* files: specify and view files;

* internals: maintenance command;

* running: program execution;

* stack: call stack view;

* statu: status view;

* tracepoints: trace program execution.

Type the category name of the help followed by the command to

obtain a detailed list of the class commands.

Common commands for GDB

Command explanation

Break NUM sets breakpoints on the specified row.

BT shows all the call stack frames. This command can be used

to display the order in which the function is called.

Clear deletes a breakpoint set on a particular source file or

a particular line. Its usage is clear FILENAME:NUM

Continue continues executing the program being debugged. This

command is used when the program stops operating because of

processing signals or breakpoints.

Display EXPR displays the value of the expression every time

the program stops. Expressions are made up of variables that

are defined by the program.

File FILE loads the specified executable file for debugging.

Help NAME displays help information for the specified command.

Info break displays the current breakpoint list, including the

number of times the breakpoint is reached.

Info files displays detailed information about the debugged

files.

Info func displays all the function names.

Info local displays local variable information when functions

are used.

Info prog displays the execution state of the debugger.

Info var displays all global and static variable names.

Kill terminates the program being debugged.

List displays the source code segment.

Make runs the make tool without exiting the gdb.

Next performs a single line of source code without stepping into

other functions.

Print EXPR displays the value of the expression EXPR.

The example uses ******gdb ************************

Round and round

Listing 1 a C source with error bugging.c

Code:

Round and round

1, I, nclude

Two

3, static, char, buff, [256];

4 static char* string;

5 int main ()

{6

7 printf ("Please input a string:");

8 gets (string);

9 printf (nYour, string,%sn, is:, string);

10}

Round and round

The program above is very simple, the purpose is to accept user

input, and then print out the user's input. The program uses

an uninitialized string address string, so after compiling and

running, there will be a Segment Fault error:

$GCC, -o, bugging, -g, bugging.c

$./bugging

Please, input, a, string:, ASFD

Segmentation fault (core dumped)

To find the problems in the program, we use GDB and follow these

steps:

1. run the GDB bugging command and load the bugging executable

file;

2. execute the loaded bugging command run;

3. use the where command to see where the program went wrong;

4. use the list command to view the code that calls the gets

function;

5. the only factor that can cause errors in the gets function

is the variable string. View the value of string with the print

command;

6. in GDB, we can directly modify the value of a variable, as

long as you take a valid pointer value from string, and for this

reason, we set breakpoint break 8 at the eighth line;

7., the program rerun to stop at the eighth line, then we can

use the set variable command to modify the value of string;

8., and then continue to run, will see the correct program

operation results.


本文标签: 信号 程序 停止 变量