samuelbrody1249
samuelbrody1249

Reputation: 4767

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])

Upvotes: 0

Views: 242

Answers (2)

yflelion
yflelion

Reputation: 1746

what you want is $argc and $arg0 $arg1 .... .
You can find how to implement User-defined Commands in https://sourceware.org/gdb/onlinedocs/gdb/Define.html .

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126120

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

Upvotes: 2

Related Questions