views:

46

answers:

2

How can I debug a shared library in the this case:

A daemon is checking which job is set to run, if find one, the daemon will fork a process. This process will do dlopen/dlsym etc to use the shared library.

The shared library is under my control so I can put one with debug information. While the daemon is not under my control and cannot be stopped for some reason. There is no debug info available in daemon.

Here is how I debug: start gdb, attach to the daemon, set follow-fork-mode to "child", set a breakpoint with the entry point of shared library.

But it doesn't work. The debug session didn't break at the breakpoint i set at all. I use gdb 6.1.1. Thanks.

A: 

I don't know if this helps, but in Windows, when I have problems like this, I just insert the following code into my dll in some method that is called early.

static bool breakHere = true;
if (breakHere) _asm {int 3}

int 3 is the hardware break instruction for x86 CPUs

Then when my dll is loaded and this code gets hit. Windows brings up the "Do you want to debug your application" dialog and I say yes. Once I have the debugger running, I change breakHere to true and go.

John Knoeller
The question doesn't state the OS and processor; "int 3" (or "int3" as it is spelled for UNIX assemblers) assumes this is on Intel x86, but we don't know that. Also, this method will case the process to crash and burn when it is not executing under debugger.
Employed Russian
+2  A: 

You can put in a temporary variable here, followed by a infinite loop:

void my_shared_loopy()
{
  int loopy = 1;
  while (loopy) ;
}

Call that my_shared_loopy() somewhere in the function you are trying to debug...recompile your shared library and then when the debugger gets attached to the function you are debugging, it will hang on this code...simply set the value of loopy to 0 and then continue on debugging.

EDIT: As an added helper, I usually put

fprintf(stderr, "attach GDB to %d\n", getpid());

before the loop. Also, if you don't want to accidentally burn a lot of cycles, make the loop sleep like this:

void my_shared_loopy()
{
  int loopy = 1;
  fprintf(stderr, "attach GDB to %d\n", getpid());
  while (loopy) sleep(1);
}

When you attach GDB, you'll most likely be inside sleep. Do this to get out:

(gdb) finish
(gdb) set var loopy = 0
(gdb) break <wherever you want to debug>
(gdb) continue
tommieb75
@Employed Russian - Thanks for that minor Edit! :)
tommieb75