Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gdb doesn't stop on breakpoint

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)
like image 380
stdcall Avatar asked Jul 21 '13 17:07

stdcall


1 Answers

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).

like image 111
Employed Russian Avatar answered Sep 24 '22 21:09

Employed Russian