Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd functions in gdb at runtime
    text
    copied!<p>I'm trying to debug some STL based C++ code in gdb. The code has something like </p> <pre><code>int myfunc() { std::map&lt;int,int&gt; m; ... } </code></pre> <p>Now in gdb, inside myfunc using "print m" gives something very ugly. What I've seen recommended is compiling something like</p> <pre><code>void printmap( std::map&lt;int,int&gt; m ) { for( std::map&lt;int,int&gt;::iterator it = ... ) { printf("%d : %d", it-&gt;first, it-&gt;second ); } } </code></pre> <p>Then in gdb doing</p> <pre><code>(gdb) call printmap( m ) </code></pre> <p>This seems like a good way to handle the issue... but can I put printmap into a seperate object file (or even dynamic library) that I then load into gdb at runtime rather than compiling it into my binary - as recompiling the binary every time I want to look at another STL variable is not fun .. while compiling and loading a single .o file for the print routine may be acceptable.</p> <hr> <p>UPDATE:</p> <p>Prompted by Nikolais suggestion I'm looking at dlopen/dlsym.</p> <p>So I haven't yet got this working but it feels like I'm getting closer.</p> <p>In printit.cpp</p> <pre><code>#include &lt;stdio.h&gt; extern "C" void printit() { printf("OMG Fuzzies"); } </code></pre> <p>Compile to a .so using </p> <pre><code>g++ -Wall -g -fPIC -c printit.cpp g++ -shared -Wl,-undefined,dynamic_lookup -o printit.so printit.o </code></pre> <p>Start my test application and load the .so using dlopen ( 2 = RTLD_NOW ) then try to get the symbol for a debugging function using dlsym.</p> <pre><code>(gdb) break main (gdb) run (gdb) print (void*) dlopen("printit.so", 2 ) $1 = (void *) 0x100270 (gdb) print (void*) dlsym( 0x100270, "_printit" ) $2 = (void *) 0x0 </code></pre> <p>So close but for some reason I cant get that symbol... ( I cant even get it if I put the dlopen/dlsym calls in my executable) I'm guessing I'm either compiling the lib wrong or using dlsym incorrectly.</p> <p>If I can get the symbol I'm assuming I can call the function using something like</p> <pre><code>(gdb) print (( void(*)() )(0x....))() </code></pre> <p>I'm compiling this on OS X 10.4, which might be causing some of my .so woes... any pointers would be appreciated.</p> <hr> <p>Found out how to get all this working. Have posted as a solution below.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload