Debugger Macros

rocky edited this page Oct 8, 2011 · 1 revision

Diverging from gdb's "macro" Command.

Generally I try to stay close to the gdb command set. However encountering macros, I diverged a bit. Let me describe why.

First, Perl has a rich programming language in of its own; it can create anonymous functions and closures dynamically. So writing code to emulate the restricted language of gdb's macro language is a bit of work for less-good results. (In fairness to gdb, it is written in C which is less dynamic than Perl, and gdb has to support several programming languages).

Secondly, gdb's macro while not super powerful is also not super lightweight. In a good deal of cases, all that is desired is to give an alternate command name to an existing one. So here, I have added a very simple alias mechanism.

How to use.

The macro (really a Perl anonymous subroutine) should return either a string or an array reference to a list of strings. The string in both cases are strings of debugger commands. If the return is a string, that gets tokenized by a simple split(/ /, $string). Note that macro processing is done right after splitting on ;; so if the macro returns a string containing ;;, this will not be handled on the string returned.

If instead, a reference to a list of strings is returned, then the first string is shifted from the array and executed. The remaining strings are pushed onto the command queue. In contrast to the first string, subsequent strings can contain other macros. Any ;; in those strings will be split into separate commands.

Here is an example. The below creates a macro called fin+ which issues two commands finish followed by step:

macro fin+ sub{ ['finish', 'step']}

If you wanted to parameterize the argument of the finish command you could do that this way:

macro fin+ sub{ ['finish ' . shift, 'step']}

Invoking with fin+ 3

would expand to ["finish 3", "step"]

If you were to add another parameter for 'step', the note that the invocation might be

fin+ 3 2

rather than fin+(3,2) or fin+ 3, 2.