This is the mail archive of the mailing list for the GDB project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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

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
called "Sequences" is provided to make life easier. But unfortunately,
functionality is unexpectedly limited, for example, it's even not easy
compare a string variable with a user-provided string.
This package is intended to work around the lame functionality from gdb.
provides a new command "xsource" to gdb, you can enrich this command
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
from it's member and type, here is the mock using gdb-xsource:
File list_entry:

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

shift 3

cat <<EOF
set \$$RET = ($TYPE*)((unsigned char*)($PTR)-(unsigned
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
with linux kernel.
File ps:

cat <<\EOF
set $_ps_p = &init_task
set $_ps_end = 0
while $_ps_end != 1
    printf "%d\t\t%d\t\t\t%s\n", $_ps_p->pid, $_ps_p->parent->pid,
    xsource list_entry _ps_p $_ps_p-> tasks struct task_struct
    if $_ps_p == &init_task
        set $_ps_end = 1
This is also a shell script. It uses list_entry command introduced
Now in gdb, you simply type "xsource ps", then you will get what you can
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:
$ git-clone
A sweet gitweb interface is also available:

** Install it
After you clone the repository, you need these further steps to use
xsource in
your gdb session:
- Set environment parameters
export GDB_XSOURCE_ROOT=/path/to/your/gdb-xsource
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.
colon separated.
Set these in your ~/.bash_profile or other places.
- Set gdbinit file
Add this to your ~/.gdbinit file:
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
(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
from GDB_XSOURCE_ROOT/xsource.d, and stop searching whenever executable
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
command recursively in your scripts.

** Debugging your xsource commands
Whenever an error occurs, you can check the error information from gdb,
examining accordingly the generated gdb sequences from file

* 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
me, my contacts:
- Name: ZC Miao
- Email:

ZC Miao (

gpg --keyserver --recv-key 0x6B174C6F

we need to split main into"core" and "wtf-uses-this"

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]