Solved Build debug system libraries -- libc, libthr, etc.


I'm a porting an open-source Linux-based software to FreeBSD. Since this is a fairly low-level systems software, I'd like to be able to debug into libc, libthr, and other system libraries.

I set up a FreeBSD-10.2-Release system (x86-64) using the live CD, and then set up basic utilities. I have tried to compile and link debug versions of the system libraries without success. The issues I'm running into seem to be related to setting up the correct paths, environment variables, and CFLAGS.

Here's what I have done so far. The first thing I tried, after a fair amount of search, is this:

$ svn co svn:// /usr/src
$ cd /usr/src/lib/libc && export MAKESYSPATH=.../share/mk
$ make cleandir obj
$ make DEBUG_FLAGS="-g3 -O0"

This results in errors compiling source files that include stdlib.h. The stdlib.h header file declares functions with attributes such as "__alloc_size(x)", "__null_sentinel", etc., which are defined as pre-processor macros in cdefs.h. I noticed that the compiler (clang, in this case) picks up the header file from the standard include path /usr/include/sys/cdefs.h which doesn't define these macros. I also noticed that the cdefs.h that comes with the source /usr/src/sys/sys/cdefs.h defines the required macros. So, the next thing I tried is to add the extra include path:

$ make DEBUG_FLAGS="-g3 -O0 -I../../sys"

This fixes some of the undefined macros but I still get the following error:
/usr/src/lib/libc/../../include/stdlib.h:176:6: error: '__alloc_size__' attribute
only applies to functions that return a pointer [-Werror,-Wignored-attributes]
  __alloc_size(3);  /* (ADV) */
../../sys/sys/cdefs.h:241:40: note: expanded from macro '__alloc_size'
#define __alloc_size(x) __attribute__((__alloc_size__(x)))
To fix this I added a conditional for clang in stdlib.h.
Index: /usr/src/lib/libc/../../include/stdlib.h
--- /usr/src/lib/libc/../../include/stdlib.h  (revision 291715)
+++ /usr/src/lib/libc/../../include/stdlib.h  (working copy)
@@ -172,8 +172,12 @@
 int  rand_r(unsigned *);  /* (TSF) */
 #if __POSIX_VISIBLE >= 200112
-int  posix_memalign(void **, size_t, size_t) __nonnull(1) __alloc_align(2)
+#ifndef __clang__
+int  posix_memalign(void **, size_t, size_t) __nonnull(1) __alloc_align(2);
  __alloc_size(3);  /* (ADV) */
+int  posix_memalign(void **, size_t, size_t) __nonnull(1) __alloc_align(2);
 int  setenv(const char *, const char *, int);
 int  unsetenv(const char *);
This allows me to move further until I hit the next issue about another undefined macro:
/usr/src/lib/libc/amd64/gen/_setjmp.S:66:27: error: expected ')' in parentheses expression
 WEAK_REFERENCE(___longjmp, _longjmp)
Again, this macro isn't defined in the header files in the standard include path (/usr/include), but is defined in a set of header files in the /usr/src directory. I could try to force clang to use the other header files or define the macro myself but it looks like that there is an obvious problem with my configuration/development environment.

I'd appreciate any help.


(I apologize for the long post; I am a FreeBSD newbie and wanted to be as clear and precise as possible.)