OSDN Git Service

Report errors to the log, not just stderr.
authorElliott Hughes <enh@google.com>
Sat, 28 Jul 2012 00:40:29 +0000 (17:40 -0700)
committerElliott Hughes <enh@google.com>
Sat, 28 Jul 2012 00:43:38 +0000 (17:43 -0700)
In particular this affects assert(3) and __cxa_pure_virtual, both of
which have managed to confuse people this week by apparently aborting
without reason. (Because stderr goes nowhere, normally.)

Bug: 6852995
Bug: 6840813
Change-Id: I7f5d17d5ddda439e217b7932096702dc013b9142

libc/bionic/stubs.c
libc/private/bionic_atomic_inline.h
libc/stdlib/assert.c
libc/unistd/abort.c
libstdc++/Android.mk
libstdc++/src/one_time_construction.cpp
libstdc++/src/pure_virtual.cpp

index cc4c04e..1aeb581 100644 (file)
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
+#include <ctype.h>
+#include <errno.h>
 #include <grp.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <pwd.h>
-#include <netdb.h>
 #include <mntent.h>
+#include <netdb.h>
 #include <private/android_filesystem_config.h>
+#include <private/logd.h>
 #include <pthread.h>
+#include <pwd.h>
+#include <stdio.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <ctype.h>
+#include <unistd.h>
 
 static int do_getpw_r(int by_name, const char* name, uid_t uid,
         struct passwd* dst, char* buf, size_t byte_count, struct passwd** result)
@@ -438,65 +440,62 @@ getgrnam(const char *name)
     return app_id_to_group( app_id_from_name(name), state );
 }
 
+static void unimplemented_stub(const char* function) {
+  const char* fmt = "%s(3) is not implemented on Android\n";
+  __libc_android_log_print(ANDROID_LOG_WARN, "libc", fmt, function);
+  fprintf(stderr, fmt, function);
+}
 
-struct netent* getnetbyname(const char *name)
-{
-    fprintf(stderr, "FIX ME! implement getgrnam() %s:%d\n", __FILE__, __LINE__);
-    return NULL;
+#define UNIMPLEMENTED unimplemented_stub(__PRETTY_FUNCTION__)
+
+struct netent* getnetbyname(const char* name) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-void endpwent(void)
-{
+void endpwent(void) {
+  UNIMPLEMENTED;
 }
 
-struct mntent* getmntent(FILE* f)
-{
-    fprintf(stderr, "FIX ME! implement getmntent() %s:%d\n", __FILE__, __LINE__);
-    return NULL;
+struct mntent* getmntent(FILE* f) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-char* ttyname(int fd)
-{
-    fprintf(stderr, "FIX ME! implement ttyname() %s:%d\n", __FILE__, __LINE__);
-    return NULL;
+char* ttyname(int fd) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-int ttyname_r(int fd, char *buf, size_t buflen)
-{
-    fprintf(stderr, "FIX ME! implement ttyname_r() %s:%d\n", __FILE__, __LINE__);
-    return -ERANGE;
+int ttyname_r(int fd, char* buf, size_t buflen) {
+  UNIMPLEMENTED;
+  return -ERANGE;
 }
 
-struct netent *getnetbyaddr(uint32_t net, int type)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+struct netent* getnetbyaddr(uint32_t net, int type) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-struct protoent *getprotobyname(const char *name)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+struct protoent* getprotobyname(const char* name) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-struct protoent *getprotobynumber(int proto)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+struct protoent* getprotobynumber(int proto) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-char* getusershell(void)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
-    return NULL;
+char* getusershell(void) {
+  UNIMPLEMENTED;
+  return NULL;
 }
 
-void setusershell(void)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
+void setusershell(void) {
+  UNIMPLEMENTED;
 }
 
-void endusershell(void)
-{
-    fprintf(stderr, "FIX ME! implement %s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__);
+void endusershell(void) {
+  UNIMPLEMENTED;
 }
index 0b348c3..6819af6 100644 (file)
@@ -50,13 +50,13 @@ extern "C" {
 #define  __ATOMIC_INLINE__  static __inline__ __attribute__((always_inline))
 
 #ifdef __arm__
-#  include <bionic_atomic_arm.h>
+#  include "bionic_atomic_arm.h"
 #elif defined(__i386__)
-#  include <bionic_atomic_x86.h>
+#  include "bionic_atomic_x86.h"
 #elif defined(__mips__)
-#  include <bionic_atomic_mips.h>
+#  include "bionic_atomic_mips.h"
 #else
-#  include <bionic_atomic_gcc_builtin.h>
+#  include "bionic_atomic_gcc_builtin.h"
 #endif
 
 #define ANDROID_MEMBAR_FULL  __bionic_memory_barrier
index 816b050..7c0a860 100644 (file)
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <private/logd.h>
 
-void
-__assert(const char *file, int line, const char *failedexpr)
-{
-       (void)fprintf(stderr,
-           "assertion \"%s\" failed: file \"%s\", line %d\n",
-           failedexpr, file, line);
-       abort();
-       /* NOTREACHED */
+// We log to stderr for the benefit of "adb shell" users, and the log for the benefit
+// of regular app developers who want to see their asserts.
+
+void __assert(const char* file, int line, const char* failed_expression) {
+  const char* fmt = "%s:%d: assertion \"%s\" failed\n";
+  __libc_android_log_print(ANDROID_LOG_FATAL, "libc", fmt, file, line, failed_expression);
+  fprintf(stderr, fmt, file, line, failed_expression);
+  abort();
+  /* NOTREACHED */
 }
 
-void
-__assert2(const char *file, int line, const char *func, const char *failedexpr)
-{
-       (void)fprintf(stderr,
-           "assertion \"%s\" failed: file \"%s\", line %d, function \"%s\"\n",
-           failedexpr, file, line, func);
-       abort();
-       /* NOTREACHED */
+void __assert2(const char* file, int line, const char* function, const char* failed_expression) {
+  const char* fmt = "%s:%d: %s: assertion \"%s\" failed\n";
+  __libc_android_log_print(ANDROID_LOG_FATAL, "libc", fmt, file, line, function, failed_expression);
+  fprintf(stderr, fmt, file, line, function, failed_expression);
+  abort();
+  /* NOTREACHED */
 }
index 58d620d..4dffbae 100644 (file)
 #include "thread_private.h"
 #include "atexit.h"
 
-/* temporary, for bug hunting */
-#include "logd.h"
-#define debug_log(format, ...)  \
-    __libc_android_log_print(ANDROID_LOG_DEBUG, "libc-abort", (format), ##__VA_ARGS__ )
-
 #ifdef __arm__
 __LIBC_HIDDEN__ void
 __libc_android_abort(void)
@@ -51,7 +46,7 @@ abort(void)
        static int cleanup_called = 0;
        sigset_t mask;
 
-  
+
        sigfillset(&mask);
        /*
         * don't block SIGABRT to give any handler a chance; we ignore
index 8bc181f..7d27aa8 100644 (file)
@@ -3,7 +3,7 @@ LOCAL_PATH:= $(call my-dir)
 # Common C++ flags to build this library.
 # Note that we need to access private Bionic headers
 # and define ANDROID_SMP accordingly.
-libstdc++_cflags := -Ibionic/libc/private
+libstdc++_cflags := -Ibionic/libc/
 ifeq ($(TARGET_CPU_SMP),true)
     libstdc++_cflags += -DANDROID_SMP=1
 else
index 1eac3b1..3cfb213 100644 (file)
@@ -11,8 +11,8 @@
 #include <stddef.h>
 #include <sys/atomics.h>
 #include <endian.h>
-#include <bionic_futex.h>
-#include <bionic_atomic_inline.h>
+#include <private/bionic_futex.h>
+#include <private/bionic_atomic_inline.h>
 
 // ARM C++ ABI and Itanium/x86 C++ ABI has different definition for
 // one time construction:
index 663c1e9..affb80f 100644 (file)
@@ -1,10 +1,8 @@
+#undef NDEBUG
+#include <assert.h>
 
-#include <stdio.h>
-#include <stdlib.h>
-
-extern "C" void __cxa_pure_virtual()
-{
-    fprintf(stderr, "Pure virtual function called.  Are you calling virtual methods from a destructor?\n");
-    abort();
+extern "C" void __cxa_pure_virtual() {
+  // We can't call __libc_android_log_write from libstdc++ because it's private, so cheat.
+  assert(!"Pure virtual function called. Are you calling virtual methods from a destructor?");
+  /* NOTREACHED */
 }
-