Print vtbl functions of any class in GDB
<p>Newer version of gdb allow for convenient parsing of vtables in C++.</p>

<p>Say I have this sample code</p>

<pre><code>class Matcher {
public:
    virtual void match() { cout &lt;&lt; "base";}
};

class NMatcher: public Matcher{
public:
    void match() { cout &lt;&lt; "derived";}
};

int main() {
    Matcher* m = new Matcher();
    m-&gt;match();

    Matcher *m2 = new NMatcher();
    m2-&gt;match();
}
</code></pre>

<p>I can access the virtual table of both variables present in main via <code>info vtbl ...</code></p>

<pre><code>(gdb) info vtbl m
vtable for 'Matcher' @ 0x400ef0 (subobject @ 0x603010):
[0]: 0x400d9a &lt;Matcher::match()&gt;
</code></pre>

<p>Now imagine a situation where I have no variable in scope and I would like to inspect the vtable of any virtual object (or any pure virtual base class). For that I do not strictly need an object. The vtable is static and should be accessible.</p>

<p>Let's look at the symbols to locate the vtable:</p>

<pre><code>(gdb) info variables .*Matcher
All variables matching regular expression ".*Matcher":

Non-debugging symbols:
0x0000000000400ec0  vtable for NMatcher
0x0000000000400ee0  vtable for Matcher
0x0000000000400ef8  typeinfo name for NMatcher
0x0000000000400f10  typeinfo for NMatcher
0x0000000000400f28  typeinfo name for Matcher
0x0000000000400f40  typeinfo for Matcher
</code></pre>

<p>I can not use the listed memory location above directly. The virtual methods do not start at the beginning of the vtable. There is offset and RTI present for the first x bytes. In case of matcher it is 16, but it can be any arbitrary number:</p>

<pre><code>(gdb) p *m
$22 = {
   _vptr.Matcher = 0x400ef0 &lt;vtable for Matcher+16&gt;
}
</code></pre>

<p>In theory I can access the memory location of the vtable directly and inspect the bytes manually:</p>

<pre><code>(gdb) x /4a 0x0000000000400ee0
0x400ee0 &lt;_ZTV7Matcher&gt;:    0x0 0x400f40 &lt;_ZTI7Matcher&gt;
0x400ef0 &lt;_ZTV7Matcher+16&gt;: 0x400d9a &lt;Matcher::match()&gt;
</code></pre>

<p>But that is too painful and I would like to know a convenient way of doing something along the lines of <code>(gdb) info vtbl 'vtable for Matcher'</code></p>

<p>I am using GDB 7.8 but any version will do.</p>