Debugging with GDB阅读[5]

Extending GDB

详情在http://sources.redhat.com/gdb/current/onlinedocs/gdb.html#Extending-GDB,具体还需要进一步实例应用

set script-extension off
All scripts are always evaluated as gdb Command Files.
set script-extension soft
The debugger determines the scripting language based on filename extension. If this scripting language is supported, gdb evaluates the script using that language. Otherwise, it evaluates the file as a gdb Command File.
set script-extension strict
The debugger determines the scripting language based on filename extension, and evaluates the script using that language. If the language is not supported, then the evaluation fails.
show script-extension
Display the current value of the script-extension option.

23.1 命令序列
除了breakpoint命令外,支持下面几种情况
Define: How to define your own commands
Hooks: Hooks for user-defined commands
Command Files: How to write scripts of commands to be stored in a file
Output: Commands for controlled output

23.1.1 How to define your own commands
User commands may accept up to 10 arguments separated by whitespace. Arguments are accessed within the user command via $arg0...$arg9.

define adder
if $argc == 2
print $arg0 + $arg1
end
if $argc == 3
print $arg0 + $arg1 + $arg2
end
end

具有如下:
define commandname
document commmandname
dont-repeat
help user-defined
show user
show user commandname
show max-user-call-depth
set max-user-call-depth

23.1.2 User-defined Command Hooks
You may define hooks, which are a special kind of user-defined command. Whenever you run the command `foo', if the user-defined command `hook-foo' exists, it is executed (with no arguments) before that command.

A hook may also be defined which is run after the command you executed. Whenever you run the command `foo', if the user-defined command `hookpost-foo' exists, it is executed (with no arguments) after that command. Post-execution hooks may exist simultaneously with pre-execution hooks, for the same command.

define hook-echo
echo <<<---
end

define hookpost-echo
echo --->>>\n
end

(gdb) echo Hello World
<<<---Hello World--->>>
(gdb)

23.1.3 Command Files
if
else
This command allows to include in your script conditionally executed commands. The if command takes a single argument, which is an expression to evaluate. It is followed by a series of commands that are executed only if the expression is true (its value is nonzero). There can then optionally be an else line, followed by a series of commands that are only executed if the expression was false. The end of the list is marked by a line containing end.

while
This command allows to write loops. Its syntax is similar to if: the command takes a single argument, which is an expression to evaluate, and must be followed by the commands to execute, one per line, terminated by an end. These commands are called the body of the loop. The commands in the body of while are executed repeatedly as long as the expression evaluates to true.

loop_break
This command exits the while loop in whose body it is included. Execution of the script continues after that whiles end line.

loop_continue
This command skips the execution of the rest of the body of commands in the while loop in whose body it is included. Execution branches to the beginning of the while loop, where it evaluates the controlling expression.

end
Terminate the block of commands that are the body of if, else, or while flow-control commands.


23.2 Scripting GDB using Python
23.2.1 Python Commands
This feature is available only if gdb was configured using --with-python.

(gdb) python
Type python script
End with a line saying just "end".
>print 23
>end
23

set python print-stack
By default, gdb will not print a stack trace when an error occurs in a Python script. This can be controlled using set python print-stack: if on, then Python stack printing is enabled; if off, the default, then Python stack printing is disabled.

source script-name
The script name must end with `.py' and gdb must be configured to recognize the script language based on filename extension using the script-extension setting.
python execfile ("script-name")
This method is based on the execfile Python built-in function, and thus is always available.

23.2.2 Python API
At startup, gdb overrides Python's sys.stdout and sys.stderr to print using gdb's output-paging streams. A Python program which outputs to one of these streams may have its output interrupted by the user (see Screen Size). In this situation, a Python KeyboardInterrupt exception is thrown.
http://sources.redhat.com/gdb/current/onlinedocs/gdb.html#Basic-Python

(gdb) python
>class HelloWorld (gdb.Command):
> """Greet the whole world."""
> def __init__ (self):
> super (HelloWorld, self).__init__ ("hello-world", gdb.COMMAND_OBSCURE)
> def invoke (self, args, from_tty):
> argv = gdb.string_to_argv (args)
> if len (argv) != 0:
> raise gdb.GdbError ("hello-world takes no arguments")
> print "Hello, World!"
>HelloWorld ()
>end
(gdb) hello-world 42
hello-world takes no arguments
posted @ 2011-12-14 10:50  yarpee  阅读(418)  评论(0编辑  收藏  举报