I'm trying to debug a shared library which run under check in unit-testing scenario.
The shared library is statically linked (no dlload
) and both the unit-testing application and the shared library are compiled with debugging symbols (-g
)
In gdb
I want to set a breakpoint in a function contained in the shared library.
I set a breakpoint successfully, but when it runs it just passes over the break-point.
What I've tried already:
I placed a printf
in the shared library, it is printed out, so the function is actually called.
Here's the Makefile I'm using for compilation:
SHELL = /bin/sh
CC = gcc
CFLAGS = -g -Wall -std=gnu99 -Iinclude
EXTRA_FLAGS = -fPIC -shared
LIBFLAGS = -fPIC -shared
LDFLAGS = -shared -pthread -lcheck
DEBUGFLAGS = -O0 -D _DEBUG
RELEASEFLAGS = -O2 -D NDEBUG -combine -fwhole-program
TEST_LDFLAGS = -lcheck -lllist -Llib -Wl,-rpath $(OBJDIR)
OBJDIR = lib
TARGET = $(OBJDIR)/libllist.so
TEST_TARGET = starttest
SOURCES = $(shell echo src/*.c)
HEADERS = $(shell echo inc/*.h)
TEST_SOURCES = $(shell echo tests/*.c)
TEST_OBJECTS = $(TEST_SOURCES:.c=.o)
OBJECTS = $(SOURCES:.c=.o)
PREFIX = $(DESTDIR)/usr/local
BINDIR = $(PREFIX)/bin
all: $(TARGET) tests
$(TARGET): $(OBJECTS)
mkdir -p $(OBJDIR)
$(CC) $(FLAGS) $(LIBFLAGS) $(DEBUGFLAGS) -o $(TARGET) $(OBJECTS)
tests: $(TEST_OBJECTS)
$(CC) $(FLAGS) -o $(TEST_TARGET) $(TEST_OBJECTS) $(TEST_LDFLAGS)
# Need a special rule to compile the lib to allow EXTRA_FLAGS
$(OBJECTS): $(SOURCES)
@echo [Compiling]: $<
$(CC) $(CFLAGS) $(EXTRA_FLAGS) -o $@ -c $<
clean:
rm -rf $(TEST_OBJECTS) $(OBJECTS) *~ $(TARGET) $(TEST_TARGET)
runtests:
./$(TEST_TARGET)
I don't know specifics of the check
framework you are using, but some such frameworks execute code-under-test in a child process. If check
does that, then your behavior is expected -- you are only debugging the parent process, but your code executes in a child.
You can easily confirm this guess: replace the printf
in your code with abort
. If GDB doesn't stop on SIGABRT
, then my guess is likely correct, and you'll want to (gdb) set follow-fork-mode child
.
Alternatively, read up on multi-inferior debugging. You can ask GDB to debug both the parent and the child with set detach-on-fork off
(documentation).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With