close
  • Compile the program with the -g option, this causes the compiler to generate an augmented symbol table.
  • $gdb [program] [core-dump]
  • gdb options:

-d dir

Tell gdb to look in dir for source files. Ex: gdb –d /work/phase1 –d /work/phase2 foo.

-x file

Before accepting any commands, gdb reads and executes the commands in file, multiple -x options are allowed and the different files are executed in order.

-nx

Don't execute commands from the initialization file (normally gdbinit).

-q

Don't print the introductory and copyright messages upon startup.

-help, h

Print a help message showing all command-line options and quit.

-batch

Run in batch mode; execute any startup files (unless they're suppressed), followed by any files specified with -x options, and then exit with status zero.

  • gdb commands:  /* support auto completion */

list, l

List the contents of the source file. Ex: list line1,line2, list routine-name

run

Run the program. Ex: run –b < invalues > outtable

args

Change the default arguments that are passed to the program. Ex: set args –b –x, show args

CTRL-C

Terminate a program while it is running under gdb control, return control to gdb.

CTRL-Z

Pause gdb.

backtrace

The backtrace command gives a stack backtrace showing exactly what the program was doing when it bombed out.

whatis

Identify the type of an array or variable.

print, p

Print the value of a variable and assign a value to a variable. There will be a history identifier ($n) in front of the printed value, this identifier can be used as the identity of the printed value and for further use. Ex: print find_entry(1.0) /* call the function directly */, print *table start /* print the contents of a complex objcet */, print $1-1, print base@length, print $13[6], set print array /* list addresses in vertically arranged columns */

display

Print an expression whenever the program stops.

undisplay number

Delete a display.

break, b

Ex: break [filename:]line-number or function-name[ if expr], break routine-name

tbreak

Temporary breakpoints are disabled as soon as they stop the program, they can be turned on again.

watch

Watch-points are sort of like the "break-if" breakpoints. Ex: watch testsize > 100000

continue, c

Continuing execution from a breakpoint. Ex: c 5 /* ignores the next 5 break-points and stops at the sixth */

info, i

Ex: info breakpoints or watch or display or line or register or signal. Ex: info line 121.

delete, d

Ex: delete /* delete all breakpoints and watch-points */, delete breakpoint-number

clear

Ex: clear /* delete all breakpoints on the current line */, clear line-number /* delete all breakpoints on the specified line */, clear function-name /* deletes any breakpoints at the entry to that function */

disable

Disable breakpoint. Ex: disable number1[, number2…]

enable [once]

Enable breakpoint. Ex: enable number1[, number2…]

set variable=value

Assign a value to a variable or an expression. Convenience variable names have the form $name, where name can be anything except a number. If = expression is omitted, the variable is defined, but not assigned any value. Ex: $foo++ prints the value of $foo, and increments it.

where

Print backtrace of all stack frames, or innermost COUNT frames. With a negative argument, print outermost -COUNT frames.

ptype, pt

Print definition of type TYPE.

next, n

Single-step execution, the next command executes an entire function when it encounters a call.

nexti

Same as next but work on the level of machine-language instructions.

step

Single-step execution, the step command enters the function and keeps going one statement at a time.

stepi

Same as step but work on the level of machine-language instructions.

call name

Call and execute a function.

finish

Finish executing the current function and print its return value (if any).

return value

Cancel execution of the current function, returning value to the caller.

commands number

The list of commands is applied to the breakpoint (or watchpoint) given by number.

up/down n

The commands up n and down n move you up or down n levels in the stack.

frame, f

Summarizes the current stack frame.

disassemble

Disassemble the source code. Ex: disassemble line1 line2, disassemble function-name

x (examine)

Prints the contents of memory, see below. Ex: x/nfu addr, x addr, x/4dw s.

handle

Control signal handling, see below.

signal

Issue the signal to the program. Ex: signal 2

[reverse-]search text

Prints the previous/next line containing text. text may be any UNIX regular expression.

directory list-of-directories

Modifies the list of directories through which gdb searches. If given without any arguments, the  directory command resets the search list to the default

jump

Start execution at another point in the source file.

kill

Abort the process running under gdb's control.

pwd

Show the current working directory.

until

Finish the current loop.

Enter

Repeat the previous command.

  • gdb can't take into account changes to the code produced by conditional compilation (#ifdef or file inclusion (#include statements).
  • The running program gets a full shell environment, set/unset environment variables, io redirection (<,>), shell wildcards (*, ?, [, ]) are all allowed operations.
  • If you give the run command without arguments, gdb reuses the arguments you gave for the previous run command.
  • A full variable name has the form: file-or-function::name. Ex: print trans::foo, print ‘trans.c’::foo.
  • x/nfu addr: Prints the contents of memory. Formatting information is given by nfu, which is a sequence of three items:
    • n is a repeat count that specifies how many data items to print.
    • f specifies what format to use for the output.
    • u specifies the size of the data unit (e.g., byte, word, etc.).

Format code

Output format

x

Hexadecimal

s

Signed decimal

u

Unsigned decimal

o

Octal

t

Binary ("t" stands for "two")

a

Address; prints both as a hex number and as an offset from the nearest preceding symbol

c

Character constant

f

Floating point (interprets the data as a floating-point number)

Size code

Data size

b

Byte

h

Halfword (two bytes)

w

Word (four bytes)

g

Giant word (two words, eight bytes)

  • Standard Register Names

Name

Register

$pc

Program counter

$fp

Frame pointer (current stack frame)

$sp

Stack pointer

$ps

Processor status

  • The command handle controls signal handling. It takes two arguments: a signal name and what should be done when the signal arrives. UNIX signal names are always capital letters. You may use signal numbers instead of signal names. Ex: handle SIGPIPE stop print

nostop

When the signal arrives, don't pass it on to the program and don't stop the program.

stop

When the signal arrives, stop the program, allowing you to debug. Print a message showing that the signal has arrived (unless messages have been disabled).

print

Print a message when the signal arrives.

noprint

Don't print a message when the signal arrives (and, implicitly, don't stop the program).

pass

Pass the signal to the program, allowing your program to handle it, die, or take any other action.

nopass

Stop the program, but don't pass the signal to the program.

  • Whenever gdb begins execution, it looks for the file .gdbinit in the current working directory; then it looks for the file .gdbinit in the home directory. .gdbinit can contain a list of gdb commands that gdb will execute as it starts up and is an ideal place for defining  additional commands or hooks.
  • User-defined commands
(gdb) define command-name
           command1
           command2
          
           end
  • A "hook" is a sequence of gdb commands that are executed prior to some "regular" command.
(gdb) define hook-name
           command1
           command2
          
           end
  • Scripts are sequences of gdb commands that you can execute when you give the command source filename.
  • The shell command starts a UNIX shell. When you exit this shell by typing CTRL-D, control returns to gdb. Ex: shell date.
  • Attach to a running program:
$gdb program-name PID
(gdb) attach/detach program-name PID
  • Debug a remote system:
(gdb) target remote /dev/device-name
(gdb) load file

arrow
arrow
    全站熱搜

    nix 發表在 痞客邦 留言(0) 人氣()