Search code examples
cgdbgdbinit

How to do basic parameter passing in gdb


I have the following defined in my .gdbinit to make it easier to print "the stack" when I want to see it in decimal format:

define s
   x/5gd $rsp
end

Now I can type in something like:

>>> s
0x7fffffffe408: 10  8
0x7fffffffe418: 6   4
0x7fffffffe428: 2

By default it will print 5 8-byte values. How can I use an input parameter to use the number I pass instead of 5? For example, something like:

define s(d=5)
   x/%sgd $rsp % d
end

Also, I'm familiar with python, so as long as I can access the input param, I could use that as well, i.e:

def stack():
    return "x/%sgd" % ('5' if not argv[1].isdigit() else argv[1])

Solution

  • The arguments to a define command in gdb are accessable as $arg0, $arg1, etc. The number of arguments is in $argc. Note that $arg0 is the first argument (not the command like in C command line arguments.) So you could write

    define s
        if $argc == 0
            x/5gd $rsp
        else
            x/$arg0 $rsp
        end
    end