LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1812|回复: 1

gdb-xsource, 增强gdb的脚本能力

[复制链接]
发表于 2008-8-13 06:52:03 | 显示全部楼层 |阅读模式
http://blog.chinaunix.net/u/8057/showart_1129423.html

gdb-xsource - A gdb command for enhanced debugging scripting.

* Motivation
When debugging some complicated applications such as linux kernel, using gdb to
examining data is quite useful. And most of the time, the data structures of
these applications is also complicated, so the scripting facility from gdb
called "Sequences" is provided to make life easier. But unfortunately, that
functionality is unexpectedly limited, for example, it's even not easy to
compare a string variable with a user-provided string.
This package is intended to work around the lame functionality from gdb. It
provides a new command "xsource" to gdb, you can enrich this command with
whatever executable files as long as they return valid gdb sequences.


* Sample use
Here provides some sample use to show you more what is gdb-xsource :

** Mimic of list_entry
In linux kernel there's a well-known trick called list_entry to get list entry
from it's member and type, here is the mock using gdb-xsource:
File list_entry:

  1. #!/bin/sh

  2. [ $# -lt 4 ] && echo "printf "Usage : list_entry ret ptr member type...\n"" && exit 1

  3. RET=$1
  4. PTR=$2
  5. MEMBER=$3
  6. shift 3
  7. TYPE="$@"

  8. cat <<EOF
  9. set \$$RET = ($TYPE*)((unsigned char*)($PTR)-(unsigned int)&((($TYPE*)0)->$MEMBER))
  10. EOF
复制代码

This is a shell script which is provided by gdb-xsource package. It store the
list entry to parameter 'ret' of which parameter 'ptr' is its 'member'
(parameter), and the type of 'ret' is 'type'(parameter).
In order to see how to use this, let's see the following example:

**  Implement ps command when debugging linux
By using gdb-xsource, you can directly show a simple ps command when debugging
with linux kernel.
File ps:

  1. #!/bin/sh

  2. cat <<\EOF
  3. set $_ps_p = &init_task
  4. set $_ps_end = 0
  5. while $_ps_end != 1
  6.     printf "%d\t\t%d\t\t\t%s\n", $_ps_p->pid, $_ps_p->parent->pid, $_ps_p->comm
  7.     xsource list_entry _ps_p $_ps_p->tasks.next tasks struct task_struct
  8.     if $_ps_p == &init_task
  9.         set $_ps_end = 1
  10.     end
  11. end
  12. EOF
复制代码

This is also a shell script. It uses list_entry command introduced above.
Now in gdb, you simply type "xsource ps", then you will get what you can expect
from ps command on a running linux system.

* Get & Install & Use it
** Get from git repository
You can clone it by using the following command in your shell:

  1. $ git-clone http://linuxfire.com.cn/~hellwolf/git/gdb-xsource.git
复制代码

A sweet gitweb interface is also available:
http://linuxfire.com.cn/~hellwol ... i?p=gdb-xsource.git

** Install it
After you clone the repository, you need these further steps to use xsource in
your gdb session:
- Set environment parameters

  1. export GDB_XSOURCE_ROOT=/path/to/your/gdb-xsource
  2. export GDB_XSOURCE_DIRS=:/path1:/path2
复制代码

GDB_XSOURCE_ROOT is the root directory of the package.
GDB_XSOURCE_DIRS is the available directories to find xsource commands. It's
colon separated.
Set these in your ~/.bash_profile or other places.
- Set gdbinit file
Add this to your ~/.gdbinit file:

  1. source /path/to/gdb-xsource/xsource.gdb
复制代码


** Create and run xsource commands
Put executable file in GDB_XSOURCE_DIRS, then in gdb you can use them by:

  1. (gdb) xsource name args...
复制代码

Where 'name' is the executable file name, and up to 9 args is supported
currently. The xsource command will firstly search from GDB_XSOURCE_DIRS, then
from GDB_XSOURCE_ROOT/xsource.d, and stop searching whenever executable file
called 'name' is found. If the xsource command find the executable file, it will
call the executable file with args, and execute its output as gdb
sequences. Here recursions are supported, which means you can call 'xsource'
command recursively in your scripts.

** Debugging your xsource commands
Whenever an error occurs, you can check the error information from gdb, and
examining accordingly the generated gdb sequences from file
'/tmp/xsource.current'.


* Contact me
This package is published under the licenses under WTFPL, do what ever you want
to the codes. And if you have any suggestion and advice, feel free to cantact
me, my contacts:
- Name: ZC Miao
- Email: hellwolf.misty@gmail.com
发表于 2008-8-13 09:23:41 | 显示全部楼层
gdb还有这样的扩展功能,hellowolf兄又给俺上了一课。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表