2

I've read multiple posts here relating to dynamic libraries on os x and debugging with gdb. But I still can't figure out why I can't debug a simple test case.

The main issue is that when I start up GDB it never loads any shared libraries.

Update: I've tried this with GDB from macports, from homebrew, and built from source and the behavior is the same.

I have a class that I compile into a library.

Test.hpp

class Test {
public:
  void set(int i);
  void out() const;
private:
  int i;
};

Test.cpp

#include "Test.hpp"
#include <iostream>

void Test::set(int ii) { i = ii; }

void Test::out() const {
  auto j = i * 100;
  std::cout << i << ", " << j << "\n";
  ++j;
  std::cout << i << ", " << j << "\n";
}

I compile it and create a library with g++. Note: the behavior is the same with macports gcc and the gcc from xcode.

/opt/local/bin/g++-mp-4.8 -O0 -g -ggdb -Wall -c -std=c++11 -o Test.o Test.cpp
/opt/local/bin/g++-mp-4.8 -dynamiclib -o libTest.dylib Test.o

Then I test it with this simple main

#include "Test.hpp"

int main() {
  Test t;
  auto x = 4;
  t.set(x);
  t.out();
  return 0;
}

This is compiled and linked with

/opt/local/bin/g++-mp-4.8 -O0 -g -ggdb -Wall -c -std=c++11 -o main.o main.cpp
/opt/local/bin/g++-mp-4.8 -L . -o testing main.o -lTest

Everything compiles and runs as expected. But when I try to debug this with gdb (installed from macports, or installed from source, the behavior is the same), I have problems.

As I step through main, if I call info sharedlibrary it always says "No shared libraries loaded at this time.", so it apparently never loads libTest.dylib. Therefore, I can't step into any of the Test member functions or create breakpoints anywhere in libTest.dylib.

Sean Lynch
  • 2,852
  • 4
  • 32
  • 46
  • `> sharedlibrary libTest` might do the trick. `set solib-search-path ` might be useful too. – Brett Hale Feb 08 '14 at 15:00
  • @BrettHale This doesn't seem to work `(gdb) set solib-search-path /Users/slynch/Programming/cxx_testing/` `(gdb) sharedlibrary libTest No loaded shared libraries match the pattern libTest.` `(gdb) sharedlibrary libTest.dylib No loaded shared libraries match the pattern libTest.dylib.` – Sean Lynch Feb 08 '14 at 15:07
  • What version of GDB are you using? New GCC often calls for new GDB. – Potatoswatter Feb 08 '14 at 15:24
  • @Potatoswatter I'm using gcc and gdb installed by macports. gcc is version 4.8.1. gdb is version 7.6 and the version information also lists "This GDB was configured as "x86_64-apple-darwin13.0.0". – Sean Lynch Feb 08 '14 at 15:28
  • Long shot, but try linking with: `main.o -o testing -Wl,-rpath -Wl,. -L. -lTest` – Brett Hale Feb 08 '14 at 16:16
  • @BrettHale Good thinking but that didn't work either – Sean Lynch Feb 08 '14 at 16:51
  • Hmm. Maybe you would have more luck with LLDB (Clang/LLVM debugger) on OS X. – Brett Hale Feb 08 '14 at 17:15
  • @BrettHale I thought about that but I'm mostly debugging through eclipse. I know eclipse has some support for LLVM but I've decided not to go that route and from what I've read lldb isnt supported at all. – Sean Lynch Feb 08 '14 at 17:20
  • If `info sharedlibrary` is failing to show any libraries at all, the problem is with gdb. You can use `vmmap` as a source of confirmation when your process is running -- it will list all of the shared libraries that are loaded into the process space. Eclipse does not have any support for lldb AFAIK but lldb is the best way to go if at all possible when debugging on Mac OS X... You might try lldb from the cmd line to confirm the gdb behavior, see the gdb/lldb cmd cheatsheet at http://lldb.llvm.org/lldb-gdb.html – Jason Molenda Feb 08 '14 at 20:38
  • @JasonMolenda Yes this does in fact look like a problem with gdb after following your suggestions. It's strange that I even installed gdb from source and still got the same problems. LLDB is really nice to use and correctly debugs this library. LLVM support for eclipse is minimal right now so I'm thinking I'll transition this project from eclipse to XCode and just use clang. Thanks for the help. – Sean Lynch Feb 09 '14 at 18:49

1 Answers1

1

Indeed ggdb installed from macports for some reason does not respect the DYLD_LIBRARY_PATH. However, if you "patch" your executable with the correct paths for the .dylibs you should be able to debug with ggdb. Take a look at this question and especially the answer by Akos Cz.

Community
  • 1
  • 1
FFox
  • 1,550
  • 2
  • 17
  • 26