My first encounter with the gdb command
duelwas on some old IRIX about 15 years ago. I immediately loved how convenient it was for displaying various data structures during MySQL debugging, and I wished Linux had something similar. Later I found out that Duel was not something IRIX specific, but a public domain patch for gdb 4.6 written in ’93 by Michael Golan. Unfortunately, it never got into gdb (for licensing reasons, so I’ve heard). Now the gdb 8 is out, and the patch, obviously doesn’t apply. Instead of fixing the patch, I’ve re-implemented Duel in Python, usinggdb Python APIand theArpeggioparser. Now it can be loaded run-time into your gdb, no patching or compilation is needed. And, hopefully, it will work over many gdb versions, as it no longer relies on undocumented gdb internals. So, allow me to introduce…Duel.pyfor gdb!
Let’s start with a few examples, to get the taste of it. Michael Golan managed to create a concise but very naturally-looking language. For example, while
a->bmeans the element
bof the structure pointer
a, in Duel
a-->bmeans to walk the linked list, producing the sequence of values
a->b->b, and so on. In MariaDB, all tables of a query are stored in a linked list, and it can be printed with just one command:
Also, quite naturally, instead of a number, you can specify a range, great for printing arrays:
And instead of any expression, you can specify a set of expressions:
Duel is built on the idea that every expression can return multiple values. You’ve seen
a,bas examples of such expressions. Any such expression can be used normally with other operators. Like in
Besides operators that generate multiple values, there are operators that stop generation and grep-like operators that filter generated values. Let’s take a closer look.
Duel syntax is C-like, C operators work as expected, one can use C-style casts, refer to variables and invoke functions of the inferior (program, being debugged). But additionally one can use special Duel operators that deal with multiple values.
You’ve seen examples above. These operators look rather familiar, they exist in other languages too. A range can have both or only one end specified,
..xmeans a range of
xelements, same as
0..x-1. An open range
x..will generate values ad infinitum (or until the counter overflows, whatever comes first), one usually uses it with the
xwill keep generating values until
ybecomes true. For example,
will print elements of the
arr[i].countbecomes larger than ten. As a convenient shortcut, the second argument can be a constant, in this case the generation stops as soon as the generated value becomes equal to this constant. For example, to print all command-line arguments:
Although this can be also achieved with
x-->yproduces multiple values,
x->y->y->y, and so on, until it hits NULL. This can be obviously used to print the whole linked list in one command. But it works for trees just the same:
Curly braces work just like parentheses, but they replace the expression with its value in the output. It’s easier to explain with the example, assume that
i = 5:
This is useful in complex expressions around array indexes:
Note, how in the second example curly braces allow us to see what array elements satisfy the condition.
These operators work kind of like grep, they select values of left argument that satisfy the condition. In other words, the result of
x ==? yis
x, if it’s equal to
y, otherwise there is no result at all. The previous, rather complex, example could be simplified using filters:
There are also aliases (
:=), grouping operators (
#/, etc), conditional
ifoperator, and other rarely needed things.
Few more examples from the manual.
Print the cyclic list (start from
headand follow the
nextpointer until you see
Find the third positive element in the array (uses
[[ ]]operator that selects a value from a sequence):
Find the last element in the linked list (uses the counter
#/operator that returns the number of values in a sequence):
See if the array is sorted:
Duel in gdb works (again (after 24 years)). It’s ridiculously useful in debugging anything more complex than “Hello, world”. Practically you’ll only need to remember four constructs:
You can get it from my repository:https://github.com/vuvova/gdb-tools
Disclaimer: while Duel itself is very stable and time-proven language, Duel.py is new, expect bugs.