From 364832195d0d333910cf63cf98ac16fdb82ebd55 Mon Sep 17 00:00:00 2001 From: Date: Fri, 17 Oct 2008 11:52:32 +0000 Subject: [PATCH] Use dladdr to get symbols for environments which doesn't use ELF and has execinfo.h (e.g., MacOSX 10.5). Though dladdr may not be async signal safe, it's OK since glog's stacktrace doesn't depend on signals. git-svn-id: https://google-glog.googlecode.com/svn/trunk@7 eb4d4688-79bd-11dd-afb4-1d65580434c0 --- INSTALL | 70 ++++++++++++++------------- Makefile.in | 4 +- aclocal.m4 | 1 + configure | 97 +++++++++++++++++++++++++++++++++++++ configure.ac | 3 ++ src/config.h.in | 6 +++ src/symbolize.cc | 119 ++++++++++++++++++++++++++++++---------------- src/symbolize.h | 21 +++++--- src/symbolize_unittest.cc | 4 ++ src/utilities.cc | 2 +- src/utilities.h | 7 +++ 11 files changed, 254 insertions(+), 80 deletions(-) diff --git a/INSTALL b/INSTALL index 316e397..23e5f25 100644 --- a/INSTALL +++ b/INSTALL @@ -1,14 +1,16 @@ -Copyright 1994, 1995, 1996, 1999, 2000, 2001, 2002 Free Software -Foundation, Inc. +Installation Instructions +************************* - This file is free documentation; the Free Software Foundation gives -unlimited permission to copy, distribute and modify it. +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005 Free +Software Foundation, Inc. +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. Basic Installation ================== - These are generic installation instructions. +These are generic installation instructions. The `configure' shell script attempts to guess correct values for various system-dependent variables used during compilation. It uses @@ -68,9 +70,9 @@ The simplest way to compile this package is: Compilers and Options ===================== - Some systems require unusual options for compilation or linking that -the `configure' script does not know about. Run `./configure --help' -for details on some of the pertinent environment variables. +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. You can give `configure' initial values for configuration parameters by setting variables in the command line or in the environment. Here @@ -83,7 +85,7 @@ is an example: Compiling For Multiple Architectures ==================================== - You can compile the package for more than one kind of computer at the +You can compile the package for more than one kind of computer at the same time, by placing the object files for each architecture in their own directory. To do this, you must use a version of `make' that supports the `VPATH' variable, such as GNU `make'. `cd' to the @@ -100,19 +102,19 @@ for another architecture. Installation Names ================== - By default, `make install' will install the package's files in -`/usr/local/bin', `/usr/local/man', etc. You can specify an -installation prefix other than `/usr/local' by giving `configure' the -option `--prefix=PATH'. +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. You can specify separate installation prefixes for architecture-specific files and architecture-independent files. If you -give `configure' the option `--exec-prefix=PATH', the package will use -PATH as the prefix for installing programs and libraries. -Documentation and other data files will still use the regular prefix. +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. In addition, if you use an unusual directory layout you can give -options like `--bindir=PATH' to specify different values for particular +options like `--bindir=DIR' to specify different values for particular kinds of files. Run `configure --help' for a list of the directories you can set and what kinds of files go in them. @@ -123,7 +125,7 @@ option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. Optional Features ================= - Some packages pay attention to `--enable-FEATURE' options to +Some packages pay attention to `--enable-FEATURE' options to `configure', where FEATURE indicates an optional part of the package. They may also pay attention to `--with-PACKAGE' options, where PACKAGE is something like `gnu-as' or `x' (for the X Window System). The @@ -138,11 +140,11 @@ you can use the `configure' options `--x-includes=DIR' and Specifying the System Type ========================== - There may be some features `configure' cannot figure out -automatically, but needs to determine by the type of machine the package -will run on. Usually, assuming the package is built to be run on the -_same_ architectures, `configure' can figure that out, but if it prints -a message saying it cannot guess the machine type, give it the +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the `--build=TYPE' option. TYPE can either be a short name for the system type, such as `sun4', or a canonical name which has the form: @@ -157,7 +159,7 @@ where SYSTEM can have one of these forms: need to know the machine type. If you are _building_ compiler tools for cross-compiling, you should -use the `--target=TYPE' option to select the type of system they will +use the option `--target=TYPE' to select the type of system they will produce code for. If you want to _use_ a cross compiler, that generates code for a @@ -168,9 +170,9 @@ eventually be run) with `--host=TYPE'. Sharing Defaults ================ - If you want to set default values for `configure' scripts to share, -you can create a site shell script called `config.site' that gives -default values for variables like `CC', `cache_file', and `prefix'. +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. `configure' looks for `PREFIX/share/config.site' if it exists, then `PREFIX/etc/config.site' if it exists. Or, you can set the `CONFIG_SITE' environment variable to the location of the site script. @@ -179,7 +181,7 @@ A warning: not all `configure' scripts look for a site script. Defining Variables ================== - Variables not defined in a site shell script can be set in the +Variables not defined in a site shell script can be set in the environment passed to `configure'. However, some packages may run configure again during the build, and the customized values of these variables may be lost. In order to avoid this problem, you should set @@ -187,14 +189,18 @@ them in the `configure' command line, using `VAR=value'. For example: ./configure CC=/usr/local2/bin/gcc -will cause the specified gcc to be used as the C compiler (unless it is -overridden in the site shell script). +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). Here is a another example: + + /bin/bash ./configure CONFIG_SHELL=/bin/bash + +Here the `CONFIG_SHELL=/bin/bash' operand causes subsequent +configuration-related scripts to be executed by `/bin/bash'. `configure' Invocation ====================== - `configure' recognizes the following options to control how it -operates. +`configure' recognizes the following options to control how it operates. `--help' `-h' diff --git a/Makefile.in b/Makefile.in index 4419ed7..2079059 100644 --- a/Makefile.in +++ b/Makefile.in @@ -65,7 +65,8 @@ am__aclocal_m4_deps = $(top_srcdir)/m4/ac_have_attribute.m4 \ $(top_srcdir)/m4/ac_rwlock.m4 $(top_srcdir)/m4/acx_pthread.m4 \ $(top_srcdir)/m4/google_namespace.m4 \ $(top_srcdir)/m4/namespaces.m4 \ - $(top_srcdir)/m4/stl_namespace.m4 $(top_srcdir)/configure.ac + $(top_srcdir)/m4/stl_namespace.m4 \ + $(top_srcdir)/m4/using_operator.m4 $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ $(ACLOCAL_M4) am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log \ @@ -266,6 +267,7 @@ ac_ct_RANLIB = @ac_ct_RANLIB@ ac_ct_STRIP = @ac_ct_STRIP@ ac_cv___attribute___noreturn = @ac_cv___attribute___noreturn@ ac_cv___attribute___printf_4_5 = @ac_cv___attribute___printf_4_5@ +ac_cv_cxx_using_operator = @ac_cv_cxx_using_operator@ ac_cv_have___builtin_expect = @ac_cv_have___builtin_expect@ ac_cv_have___uint16 = @ac_cv_have___uint16@ ac_cv_have_inttypes_h = @ac_cv_have_inttypes_h@ diff --git a/aclocal.m4 b/aclocal.m4 index 80f53e0..38cbab7 100644 --- a/aclocal.m4 +++ b/aclocal.m4 @@ -7272,3 +7272,4 @@ m4_include([m4/acx_pthread.m4]) m4_include([m4/google_namespace.m4]) m4_include([m4/namespaces.m4]) m4_include([m4/stl_namespace.m4]) +m4_include([m4/using_operator.m4]) diff --git a/configure b/configure index 785339a..8ac5409 100755 --- a/configure +++ b/configure @@ -20325,6 +20325,103 @@ _ACEOF fi +echo "$as_me:$LINENO: checking for dladdr" >&5 +echo $ECHO_N "checking for dladdr... $ECHO_C" >&6 +if test "${ac_cv_func_dladdr+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ +/* Define dladdr to an innocuous variant, in case declares dladdr. + For example, HP-UX 11i declares gettimeofday. */ +#define dladdr innocuous_dladdr + +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char dladdr (); below. + Prefer to if __STDC__ is defined, since + exists even on freestanding compilers. */ + +#ifdef __STDC__ +# include +#else +# include +#endif + +#undef dladdr + +/* Override any gcc2 internal prototype to avoid an error. */ +#ifdef __cplusplus +extern "C" +{ +#endif +/* We use char because int might match the return type of a gcc2 + builtin and then its argument prototype would still apply. */ +char dladdr (); +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined (__stub_dladdr) || defined (__stub___dladdr) +choke me +#else +char (*f) () = dladdr; +#endif +#ifdef __cplusplus +} +#endif + +int +main () +{ +return f != dladdr; + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5 + (eval $ac_link) 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; } && + { ac_try='test -s conftest$ac_exeext' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + ac_cv_func_dladdr=yes +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + +ac_cv_func_dladdr=no +fi +rm -f conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +fi +echo "$as_me:$LINENO: result: $ac_cv_func_dladdr" >&5 +echo "${ECHO_T}$ac_cv_func_dladdr" >&6 +if test $ac_cv_func_dladdr = yes; then + +cat >>confdefs.h <<\_ACEOF +#define HAVE_DLADDR 1 +_ACEOF + +fi + echo "$as_me:$LINENO: checking for __attribute__" >&5 diff --git a/configure.ac b/configure.ac index 683247a..1012276 100644 --- a/configure.ac +++ b/configure.ac @@ -45,6 +45,9 @@ AC_CHECK_TYPE(__uint16, ac_cv_have___uint16=1, ac_cv_have___uint16=0) AC_CHECK_FUNC(sigaltstack, AC_DEFINE(HAVE_SIGALTSTACK, 1, [Define if you have the `sigaltstack' function])) +AC_CHECK_FUNC(dladdr, + AC_DEFINE(HAVE_DLADDR, 1, + [Define if you have the `dladdr' function])) AX_C___ATTRIBUTE__ # We only care about these two attributes. diff --git a/src/config.h.in b/src/config.h.in index fe27e72..41d061a 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -3,6 +3,9 @@ /* Namespace for Google classes */ #undef GOOGLE_NAMESPACE +/* Define if you have the `dladdr' function */ +#undef HAVE_DLADDR + /* Define to 1 if you have the header file. */ #undef HAVE_DLFCN_H @@ -57,6 +60,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_UNISTD_H +/* define if the compiler supports using expression for operator */ +#undef HAVE_USING_OPERATOR + /* define if your compiler has __attribute__ */ #undef HAVE___ATTRIBUTE__ diff --git a/src/symbolize.cc b/src/symbolize.cc index 30c6df4..e6e3e70 100644 --- a/src/symbolize.cc +++ b/src/symbolize.cc @@ -18,7 +18,51 @@ // and memmove(). We assume they are async-signal-safe. // -#if defined(__ELF__) // defined by gcc on Linux +#include "utilities.h" + +#if defined(HAVE_SYMBOLIZE) + +#include "symbolize.h" +#include "demangle.h" + +_START_GOOGLE_NAMESPACE_ + +// We don't use assert() since it's not guaranteed to be +// async-signal-safe. Instead we define a minimal assertion +// macro. So far, we don't need pretty printing for __FILE__, etc. + +// A wrapper for abort() to make it callable in ? :. +static int AssertFail() { + abort(); + return 0; // Should not reach. +} + +#define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail()) + +static SymbolizeCallback g_symbolize_callback = NULL; +void InstallSymbolizeCallback(SymbolizeCallback callback) { + g_symbolize_callback = callback; +} + +// This function wraps the Demangle function to provide an interface +// where the input symbol is demangled in-place. +// To keep stack consumption low, we would like this function to not +// get inlined. +static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) { + char demangled[256]; // Big enough for sane demangled symbols. + if (Demangle(out, demangled, sizeof(demangled))) { + // Demangling succeeded. Copy to out if the space allows. + int len = strlen(demangled); + if (len + 1 <= out_size) { // +1 for '\0'. + SAFE_ASSERT(len < sizeof(demangled)); + memmove(out, demangled, len + 1); + } + } +} + +_END_GOOGLE_NAMESPACE_ + +#if defined(__ELF__) #include #include @@ -36,9 +80,7 @@ #include #include "symbolize.h" -#include "demangle.h" #include "config.h" -#include "utilities.h" #include "glog/raw_logging.h" // Re-runs fn until it doesn't cause EINTR. @@ -46,18 +88,6 @@ _START_GOOGLE_NAMESPACE_ -// We don't use assert() since it's not guaranteed to be -// async-signal-safe. Instead we define a minimal assertion -// macro. So far, we don't need pretty printing for __FILE__, etc. - -// A wrapper for abort() to make it callable in ? :. -static int AssertFail() { - abort(); - return 0; // Should not reach. -} - -#define SAFE_ASSERT(expr) ((expr) ? 0 : AssertFail()) - // Read up to "count" bytes from file descriptor "fd" into the buffer // starting at "buf" while handling short reads and EINTR. On // success, return the number of bytes read. Otherwise, return -1. @@ -519,28 +549,6 @@ OpenObjectFileContainingPcAndGetStartAddress(uint64_t pc, } } - -SymbolizeCallback g_symbolize_callback = NULL; -void InstallSymbolizeCallback(SymbolizeCallback callback) { - g_symbolize_callback = callback; -} - -// This function wraps the Demangle function to provide an interface -// where the input symbol is demangled in-place. -// To keep stack consumption low, we would like this function to not -// get inlined. -static ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size) { - char demangled[256]; // Big enough for sane demangled symbols. - if (Demangle(out, demangled, sizeof(demangled))) { - // Demangling succeeded. Copy to out if the space allows. - int len = strlen(demangled); - if (len + 1 <= out_size) { // +1 for '\0'. - SAFE_ASSERT(len < sizeof(demangled)); - memmove(out, demangled, len + 1); - } - } -} - // The implementation of our symbolization routine. If it // successfully finds the symbol containing "pc" and obtains the // symbol name, returns true and write the symbol name to "out". @@ -581,11 +589,43 @@ static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out, out, out_size, start_address)) { return false; } + // Symbolization succeeded. Now we try to demangle the symbol. DemangleInplace(out, out_size); return true; } +_END_GOOGLE_NAMESPACE_ + +#elif defined(OS_MACOSX) && defined(HAVE_DLADDR) + +#include +#include + +_START_GOOGLE_NAMESPACE_ + +static ATTRIBUTE_NOINLINE bool SymbolizeAndDemangle(void *pc, char *out, + int out_size) { + Dl_info info; + if (dladdr(pc, &info)) { + if (strlen(info.dli_sname) < out_size) { + strcpy(out, info.dli_sname); + // Symbolization succeeded. Now we try to demangle the symbol. + DemangleInplace(out, out_size); + return true; + } + } + return false; +} + +_END_GOOGLE_NAMESPACE_ + +#else +# error BUG: HAVE_SYMBOLIZE was wrongly set +#endif + +_START_GOOGLE_NAMESPACE_ + bool Symbolize(void *pc, char *out, int out_size) { SAFE_ASSERT(out_size >= 0); return SymbolizeAndDemangle(pc, out, out_size); @@ -593,7 +633,7 @@ bool Symbolize(void *pc, char *out, int out_size) { _END_GOOGLE_NAMESPACE_ -#else /* __ELF__ */ +#else /* HAVE_SYMBOLIZE */ #include @@ -601,8 +641,7 @@ _END_GOOGLE_NAMESPACE_ _START_GOOGLE_NAMESPACE_ -// TODO: osx-port-incomplete. An alternative is Brakepad, but I don't -// think we want that mixed up in google3. +// TODO: Support other environments. bool Symbolize(void *pc, char *out, int out_size) { assert(0); return false; diff --git a/src/symbolize.h b/src/symbolize.h index 94c9fd8..086af0e 100644 --- a/src/symbolize.h +++ b/src/symbolize.h @@ -26,9 +26,12 @@ #ifndef BASE_SYMBOLIZE_H_ #define BASE_SYMBOLIZE_H_ +#include "utilities.h" #include "config.h" #include "glog/logging.h" +#ifdef HAVE_SYMBOLIZE + #if defined(__ELF__) // defined by gcc on Linux #include #include // For ElfW() macro. @@ -46,6 +49,17 @@ _START_GOOGLE_NAMESPACE_ +// Gets the section header for the given name, if it exists. Returns true on +// success. Otherwise, returns false. +bool GetSectionHeaderByName(int fd, const char *name, size_t name_len, + ElfW(Shdr) *out); + +_END_GOOGLE_NAMESPACE_ + +#endif /* __ELF__ */ + +_START_GOOGLE_NAMESPACE_ + // Installs a callback function, which will be called right before a symbol name // is printed. The callback is intended to be used for showing a file name and a // line number preceding a symbol name. @@ -57,14 +71,9 @@ typedef int (*SymbolizeCallback)(int fd, void *pc, char *out, size_t out_size, uint64 relocation); void InstallSymbolizeCallback(SymbolizeCallback callback); -// Gets the section header for the given name, if it exists. Returns true on -// success. Otherwise, returns false. -bool GetSectionHeaderByName(int fd, const char *name, size_t name_len, - ElfW(Shdr) *out); - _END_GOOGLE_NAMESPACE_ -#endif /* __ELF__ */ +#endif _START_GOOGLE_NAMESPACE_ diff --git a/src/symbolize_unittest.cc b/src/symbolize_unittest.cc index e172af9..b11372d 100644 --- a/src/symbolize_unittest.cc +++ b/src/symbolize_unittest.cc @@ -325,7 +325,11 @@ int main(int argc, char **argv) { #else int main() { +#ifdef HAVE_SYMBOLIZE + printf("PASS (no symbolize_unittest support)\n"); +#else printf("PASS (no symbolize support)\n"); +#endif return 0; } #endif // HAVE_STACKTRACE diff --git a/src/utilities.cc b/src/utilities.cc index 57c030e..a28e104 100644 --- a/src/utilities.cc +++ b/src/utilities.cc @@ -80,7 +80,7 @@ static void DumpStackTrace(int skip_count, DebugWriter *writerfn, void *arg) { void* stack[32]; int depth = GetStackTrace(stack, sizeof(stack)/sizeof(*stack), skip_count+1); for (int i = 0; i < depth; i++) { -#if defined(__ELF__) +#if defined(HAVE_SYMBOLIZE) if (FLAGS_symbolize_stacktrace) { DumpPCAndSymbol(writerfn, arg, stack[i], " "); } else { diff --git a/src/utilities.h b/src/utilities.h index a2e846f..c0fa5fb 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -46,6 +46,13 @@ # define HAVE_STACKTRACE #endif +#if defined(__ELF__) // defined by gcc on Linux +# define HAVE_SYMBOLIZE +#elif defined(OS_MACOSX) && defined(HAVE_DLADDR) +// Use dladdr to symbolize. +# define HAVE_SYMBOLIZE +#endif + _START_GOOGLE_NAMESPACE_ namespace glog_internal_namespace_ { -- 2.7.4