From nobody@FreeBSD.org  Tue Dec 14 20:40:41 2010
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 98D45106564A
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 14 Dec 2010 20:40:41 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (unknown [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 85C9F8FC1B
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 14 Dec 2010 20:40:41 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id oBEKef24085539
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 14 Dec 2010 20:40:41 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id oBEKefJ5085538;
	Tue, 14 Dec 2010 20:40:41 GMT
	(envelope-from nobody)
Message-Id: <201012142040.oBEKefJ5085538@red.freebsd.org>
Date: Tue, 14 Dec 2010 20:40:41 GMT
From: Mark Johnston <markjdb@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] Add support for generating userland debug symbols during installworld
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         153157
>Category:       misc
>Synopsis:       [build] [patch] Add support for generating userland debug symbols during installworld
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Tue Dec 14 20:50:07 UTC 2010
>Closed-Date:    
>Last-Modified:  Tue Nov  6 17:40:00 UTC 2012
>Originator:     Mark Johnston
>Release:        STABLE-8
>Organization:
>Environment:
FreeBSD mark-laptop-bsd.mark-home 8.2-PRERELEASE FreeBSD 8.2-PRERELEASE #7 r+46c51f0-dirty: Tue Dec  7 11:17:13 EST 2010     mark@mark-laptop-bsd.mark-home:/usr/obj/usr/home/mark/src/freebsd_git/src/sys/IPFW_GENERIC  i386
>Description:
I posted about this in a few threads on -hackers:

http://lists.freebsd.org/pipermail/freebsd-hackers/2010-November/033474.html

I just thought I'd open a PR in case someone is still interested.

The details are in my posts, but the basic idea behind this change is to allow users to have a 'make installworld' generate a directory (/usr/lib/debug) containing debug symbols for all of the base FreeBSD binaries. With my change, this can be done by adding an option to src.conf.

At the moment, my change adds a script to src/tools/ which is the script invoked by install(1) to strip and extract the debug symbols from each binary that gets installed. I'm not sure that this is the best place for it, but it seems ok to me.
>How-To-Repeat:

>Fix:
I have a patch which applies against HEAD (as of a few days ago).

Patch attached with submission follows:

diff --git a/gnu/usr.bin/gdb/arch/amd64/config.h b/gnu/usr.bin/gdb/arch/amd64/config.h
index 29f2d95..026117e 100644
--- a/gnu/usr.bin/gdb/arch/amd64/config.h
+++ b/gnu/usr.bin/gdb/arch/amd64/config.h
@@ -440,7 +440,7 @@
 #define PACKAGE "gdb"
 
 /* Global directory for separate debug files.  */
-#define DEBUGDIR "/usr/local/lib/debug"
+#define DEBUGDIR "/usr/lib/debug"
 
 /* Define to BFD's default architecture.  */
 #define DEFAULT_BFD_ARCH bfd_i386_arch
diff --git a/gnu/usr.bin/gdb/arch/arm/config.h b/gnu/usr.bin/gdb/arch/arm/config.h
index 535bd6f..fc5f895 100644
--- a/gnu/usr.bin/gdb/arch/arm/config.h
+++ b/gnu/usr.bin/gdb/arch/arm/config.h
@@ -452,7 +452,7 @@
 #define PACKAGE "gdb"
 
 /* Global directory for separate debug files.  */
-#define DEBUGDIR "/usr/local/lib/debug"
+#define DEBUGDIR "/usr/lib/debug"
 
 /* Define to BFD's default architecture.  */
 #define DEFAULT_BFD_ARCH bfd_arm_arch
diff --git a/gnu/usr.bin/gdb/arch/i386/config.h b/gnu/usr.bin/gdb/arch/i386/config.h
index 683f683..1d4b243 100644
--- a/gnu/usr.bin/gdb/arch/i386/config.h
+++ b/gnu/usr.bin/gdb/arch/i386/config.h
@@ -440,7 +440,7 @@
 #define PACKAGE "gdb"
 
 /* Global directory for separate debug files.  */
-#define DEBUGDIR "/usr/local/lib/debug"
+#define DEBUGDIR "/usr/lib/debug"
 
 /* Define to BFD's default architecture.  */
 #define DEFAULT_BFD_ARCH bfd_i386_arch
diff --git a/gnu/usr.bin/gdb/arch/ia64/config.h b/gnu/usr.bin/gdb/arch/ia64/config.h
index 0e284d5..72878ae 100644
--- a/gnu/usr.bin/gdb/arch/ia64/config.h
+++ b/gnu/usr.bin/gdb/arch/ia64/config.h
@@ -440,7 +440,7 @@
 #define PACKAGE "gdb"
 
 /* Global directory for separate debug files.  */
-#define DEBUGDIR "/usr/local/lib/debug"
+#define DEBUGDIR "/usr/lib/debug"
 
 /* Define to BFD's default architecture.  */
 #define DEFAULT_BFD_ARCH bfd_ia64_arch
diff --git a/gnu/usr.bin/gdb/arch/mips/config.h b/gnu/usr.bin/gdb/arch/mips/config.h
index 114a0f9..39dcc31 100644
--- a/gnu/usr.bin/gdb/arch/mips/config.h
+++ b/gnu/usr.bin/gdb/arch/mips/config.h
@@ -440,7 +440,7 @@
 #define PACKAGE "gdb"
 
 /* Global directory for separate debug files.  */
-#define DEBUGDIR "/usr/local/lib/debug"
+#define DEBUGDIR "/usr/lib/debug"
 
 /* Define to BFD's default architecture.  */
 #define DEFAULT_BFD_ARCH bfd_mips_arch
diff --git a/gnu/usr.bin/gdb/arch/powerpc/config.h b/gnu/usr.bin/gdb/arch/powerpc/config.h
index 7bc963d..c916419 100644
--- a/gnu/usr.bin/gdb/arch/powerpc/config.h
+++ b/gnu/usr.bin/gdb/arch/powerpc/config.h
@@ -440,7 +440,7 @@
 #define PACKAGE "gdb"
 
 /* Global directory for separate debug files.  */
-#define DEBUGDIR "/usr/local/lib/debug"
+#define DEBUGDIR "/usr/lib/debug"
 
 /* Define to BFD's default architecture.  */
 #define DEFAULT_BFD_ARCH bfd_rs6000_arch
diff --git a/gnu/usr.bin/gdb/arch/sparc64/config.h b/gnu/usr.bin/gdb/arch/sparc64/config.h
index 66fcb1f..a407d06 100644
--- a/gnu/usr.bin/gdb/arch/sparc64/config.h
+++ b/gnu/usr.bin/gdb/arch/sparc64/config.h
@@ -440,7 +440,7 @@
 #define PACKAGE "gdb"
 
 /* Global directory for separate debug files.  */
-#define DEBUGDIR "/usr/local/lib/debug"
+#define DEBUGDIR "/usr/lib/debug"
 
 /* Define to BFD's default architecture.  */
 #define DEFAULT_BFD_ARCH bfd_sparc_arch
diff --git a/share/man/man5/src.conf.5 b/share/man/man5/src.conf.5
index 180d333..7335fe0 100644
--- a/share/man/man5/src.conf.5
+++ b/share/man/man5/src.conf.5
@@ -283,6 +283,15 @@ Set to not build CVS.
 Set to not build
 .Xr g++ 1
 and related libraries.
+.It Va WITH_DEBUG_SYMBOLS_DIR
+Set this to have userland debugging symbols placed in a separate directory.
+By default, they will be placed in
+.Pa /usr/local/lib/debug/ .
+A different location can be specified by defining
+.Va SYMBOLS_DIR
+when running make installworld.
+Define this variable before running make buildworld to ensure that
+the userland binaries will be built with debug symbols in the first place.
 .It Va WITHOUT_DICT
 .\" from FreeBSD: stable/8/tools/build/options/WITHOUT_DICT 156932 2006-03-21 07:50:50Z ru
 Set to not build the Webster dictionary files.
diff --git a/share/mk/bsd.own.mk b/share/mk/bsd.own.mk
index 3361295..74d52b1 100644
--- a/share/mk/bsd.own.mk
+++ b/share/mk/bsd.own.mk
@@ -541,6 +541,12 @@ MK_${vv:H}:=	${MK_${vv:T}}
 .endif
 .endfor
 
+.if defined(WITH_DEBUG_SYMBOLS_DIR)
+DEBUG_FLAGS+=	-g
+STRIPBIN=	/usr/src/tools/stripbin.sh
+SYMBOLS_DIR?=	${DESTDIR}${LIBDIR}/debug
+.endif
+
 .endif # !_WITHOUT_SRCCONF
 
 .endif	# !target(__<bsd.own.mk>__)
diff --git a/share/mk/bsd.prog.mk b/share/mk/bsd.prog.mk
index bc3f15c..0be07be 100644
--- a/share/mk/bsd.prog.mk
+++ b/share/mk/bsd.prog.mk
@@ -29,6 +29,11 @@ CFLAGS+=${CRUNCH_CFLAGS}
 
 .if !defined(DEBUG_FLAGS)
 STRIP?=	-s
+.else
+.if defined(STRIPBIN)
+STRIP?= -s
+INSTALL:= /usr/bin/env SYMBOLS_DIR=${SYMBOLS_DIR} STRIPBIN=${STRIPBIN} ${INSTALL}
+.endif
 .endif
 
 .if defined(NO_SHARED) && (${NO_SHARED} != "no" && ${NO_SHARED} != "NO")
diff --git a/tools/stripbin.sh b/tools/stripbin.sh
new file mode 100755
index 0000000..7efe6b1
--- /dev/null
+++ b/tools/stripbin.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+# This script is invoked by install(1) on all binaries when installing world.
+# It determines whether any debug info is present in the binary; if so, it
+# will extract the debug symbols to $SYMBOLS_FULLPATH, strip the binary and
+# add a link to the binary which points to the file containing the debugging
+# symbols.
+
+PATH=${PATH}:/usr/bin
+
+SYMBOLS_FULLPATH="${SYMBOLS_DIR}$(dirname ${1})"
+
+case $1 in
+/*INS@*)
+        exit 0
+	;;
+esac
+
+# Make sure that some debug info is actually present.
+[ -z "$(readelf -wi $1)" ] && exit 0
+
+mkdir -p $SYMBOLS_FULLPATH
+
+strip --only-keep-debug -o ${SYMBOLS_DIR}${1}.symbols $1
+strip --strip-debug $1
+objcopy --add-gnu-debuglink=${SYMBOLS_DIR}${1}.symbols $1


>Release-Note:
>Audit-Trail:

From: Ed Maste <emaste@freebsd.org>
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: misc/153157: [build] [patch] Add support for generating userland
 debug symbols during installworld
Date: Wed, 17 Oct 2012 10:59:27 -0400

 Note, it appears an equivalent change is needed in bsd.lib.mk.  It
 looks like the current patch will add symbols for binaries only.
 
 Work is ongoing by brooks@ to implement an install process that can
 produce a tar file or other output directly, which will likely be
 incompatible with this patch.

From: Ed Maste <emaste@freebsd.org>
To: bug-followup@FreeBSD.org, markjdb@gmail.com
Cc:  
Subject: Re: misc/153157: [build] [patch] Add support for generating userland
 debug symbols during installworld
Date: Tue, 6 Nov 2012 12:32:32 -0500

 Now that I've had a chance to look at the patch in detail I see
 another issue - STRIPBIN is set to the absolute path
 /usr/src/tools/stripbin.sh and will not use the file in the source
 tree.
 
 To address this and more easily accommodate changes that brooks@ has
 in the pipeline we could follow the model used in kern.post.mk:
 explicitly invoke objcopy --only-keep-debug and --add-gnu-debuglink.
 It does mean we'd need to build ${PROG}.debug though, and objcopy to
 ${PROG} and ${PROG}.symbols in the objdir.
>Unformatted:
