Ian Lance Taylor <i...@google.com> writes:

> On Fri, Oct 5, 2012 at 4:21 AM, Rainer Orth <r...@cebitec.uni-bielefeld.de> 
> wrote:
>>
>> I think this should work.  The only complication might be that users of
>> DEFAULT_PROCESS_FILENAME need to include <stdlib.h> on Solaris to get
>> the declaration of getexecname().  Given that this header is already
>> used unconditionally, its inclusion should probably be moved to either
>> internal.h (which doesn't yet include anything) or backtrace.h.  Ian,
>> what do you prefer here?
>
> Definitely not backtrace.h.  I think I would prefer that print.c
> simply #include <stdlib.h> directly.

Ok, here's the merged patch, tested on i386-pc-solaris2.10.  Ok for
mainline?

        Rainer


2012-10-05  Rainer Orth  <r...@cebitec.uni-bielefeld.de>
            Gerald Pfeifer  <ger...@pfeifer.com>

        libbacktrace:
        * configure.ac: Check for getexecname.
        * configure: Regenerate.
        * config.h.in: Regenerate.
        * internal.h (DEFAULT_PROCESS_FILENAME): Define.
        * fileline.c (fileline_initialize): Use it.
        * print.c (error_callback): Likewise.
        Include <stdlib.h>.

# HG changeset patch
# Parent e43e8e91417064d0dcfb3b9ec0c2833d7ce00792
Use getexecname() on Solaris

diff --git a/libbacktrace/configure.ac b/libbacktrace/configure.ac
--- a/libbacktrace/configure.ac
+++ b/libbacktrace/configure.ac
@@ -260,6 +260,19 @@ fi
 
 AC_CHECK_DECLS(strnlen)
 
+# Check for getexecname function.
+if test -n "${with_target_subdir}"; then
+   case "${host}" in
+   *-*-solaris2*) have_getexecname=yes ;;
+   *) have_getexecname=no ;;
+   esac
+else
+  AC_CHECK_FUNC(getexecname, [have_getexecname=yes], [have_getexecname=no])
+fi
+if test "$have_getexecname" = "yes"; then
+  AC_DEFINE(HAVE_GETEXECNAME, 1, [Define if getexecname is available.])
+fi
+
 AC_CACHE_CHECK([whether tests can run],
   [libbacktrace_cv_sys_native],
   [AC_RUN_IFELSE([AC_LANG_PROGRAM([], [return 0;])],
diff --git a/libbacktrace/fileline.c b/libbacktrace/fileline.c
--- a/libbacktrace/fileline.c
+++ b/libbacktrace/fileline.c
@@ -82,7 +82,8 @@ fileline_initialize (struct backtrace_st
   if (state->filename != NULL)
     descriptor = backtrace_open (state->filename, error_callback, data);
   else
-    descriptor = backtrace_open ("/proc/self/exe", error_callback, data);
+    descriptor = backtrace_open (DEFAULT_PROCESS_FILENAME, error_callback,
+				 data);
   if (descriptor < 0)
     failed = 1;
 
diff --git a/libbacktrace/internal.h b/libbacktrace/internal.h
--- a/libbacktrace/internal.h
+++ b/libbacktrace/internal.h
@@ -56,6 +56,14 @@ POSSIBILITY OF SUCH DAMAGE.  */
 # endif
 #endif
 
+#ifdef __FreeBSD__
+# define DEFAULT_PROCESS_FILENAME "/proc/curproc/file"
+#elif defined(HAVE_GETEXECNAME)
+# define DEFAULT_PROCESS_FILENAME getexecname ()
+#else
+# define DEFAULT_PROCESS_FILENAME "/proc/self/exe"
+#endif
+
 #ifndef HAVE_SYNC_FUNCTIONS
 
 /* Define out the sync functions.  These should never be called if
diff --git a/libbacktrace/print.c b/libbacktrace/print.c
--- a/libbacktrace/print.c
+++ b/libbacktrace/print.c
@@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE.  */
 #include <stdio.h>
 #include <string.h>
 #include <sys/types.h>
+#include <stdlib.h>
 
 #include "backtrace.h"
 #include "internal.h"
@@ -73,7 +74,7 @@ error_callback (void *data, const char *
 
   name = pdata->state->filename;
   if (name == NULL)
-    name = "/proc/self/exe";
+    name = DEFAULT_PROCESS_FILENAME;
   fprintf (stderr, "%s: libbacktrace: %s", name, msg);
   if (errnum > 0)
     fprintf (stderr, ": %s", strerror (errnum));

-- 
-----------------------------------------------------------------------------
Rainer Orth, Center for Biotechnology, Bielefeld University

Reply via email to