Here is how to compile OpenCV with debugging symbols so that you can view OpenCV Library's source code as you debug.
When configuring with cmake, run with the following option:
$ cmake -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo ...
This option will add -O2 -g -DNDEBUG flags to the compiler when you build OpenCV.
For Ubuntu/Debian, take a look this post to see how to compile OpenCV from sources.
By the way, if you build OpenCV with clang, then you probably want to use lldb instead of gdb. If you compile with g++, then you may want to gdb instead of lldb. If you are having trouble running gdb on your Mac, check out this post.