debugging a dynamically loaded library

Mike Krogh
2001-08-29T13:45:00Z

Can someone explain how to debug subroutines that are loaded via
dlopen()/dlsym()? Specifically I'd like to compile and link dso.c
into dso.so and then compile main.c and have it dynamically load
dso.so. Once that is done, I'd like to get gdb to stop in
dso_init(int i) from ./dso.so.

I'm compiling with the following:

cc -g -c dso.c
cc -shared -g -o dso.so dso.o
cc -rdynamic -o main main.c -ldl

Executing ./main works fine. I just can't get gdb to stop anywhere
within dso.c. I've tried using 'add-symbol-file dso.so '
without success.

I'm running under RedHat 7.1 and RedHat 6.2. I've also tried the
8/29/01 gdb dev snapshot.

Thanks,

Mike

/********************** main.c **********************/
#include <stdio.h>
#include <dlfcn.h>

#include "dso.h"

int
main(int argc, char *argv[]) {

void *handle;
void (*init)(int);
void (*incr)(int);
char *error;
int i;

if (!(handle = dlopen("./dso.so", RTLD_NOW|RTLD_GLOBAL))) {
fputs(dlerror(), stderr);
exit(1);
}

init = dlsym(handle, "dso_init");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(1);
} else
fprintf(stderr, "Do `add-symbol-file dso.so 0x%x`", init);

incr = dlsym(handle, "dso_increment");
if ((error = dlerror()) != NULL) {
fprintf(stderr, "%s\n", error);
exit(1);
}

(*init)(5);

for (i=0; i<10; i++)
(*incr)(2);

dlclose(handle);

return 0;

}

/********************** dso.c **********************/
#include <stdio.h>

static int value;

void
dso_init(int i) {

value = i;
fprintf(stderr, "dso_init: initialized with %d\n", i);

}

void
dso_increment(int i) {

value += i;

fprintf(stderr, "dso_increment: i = %d value = %d\n", i, value);

}

/********************** dso.c **********************/
#ifndef DSO_H
#define DSO_H

void dso_init(int i);

void dso_increment(int i);

#endif /* DSO_H */

--
-
+-----------------------------------------------------------------+
| Mike Krogh .~. 919-363-0883 |
| Computational Engineering Intl. /V\ fax 919-363-0833 |
| 2166 N. Salem St., Suite 101 // \\ krogh@ceintl.com |
| Apex, NC 27502 /( )\ www.ceintl.com |
| EnSight/EnLiten/EnVideo ^`~'^ |
+-----------------------------------------------------------------+