am b1bcd9c3: am 6798914b: Merge "Improve liblog\'s fatal logging."

* commit 'b1bcd9c3ae89f9060fcd8468d5a04f3710e2b1a6':
  Improve liblog's fatal logging.
This commit is contained in:
Elliott Hughes 2014-04-24 20:47:00 +00:00 committed by Android Git Automerger
commit 6d7a2ee4e8
4 changed files with 45 additions and 26 deletions

View file

@ -1,20 +1,17 @@
#include <assert.h>
//#include <cutils/misc.h> #include <errno.h>
#include <pthread.h>
#include <unistd.h> #include <sched.h>
#include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sched.h>
#include <errno.h>
#include <signal.h>
#include <sys/ptrace.h> #include <sys/ptrace.h>
#include <sys/wait.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h> #include <cutils/log.h>
#include <cutils/sockets.h> #include <cutils/sockets.h>
extern const char* __progname; extern const char* __progname;
@ -23,8 +20,8 @@ void crash1(void);
void crashnostack(void); void crashnostack(void);
static int do_action(const char* arg); static int do_action(const char* arg);
static void maybeabort() { static void maybe_abort() {
if(time(0) != 42) { if (time(0) != 42) {
abort(); abort();
} }
} }
@ -119,20 +116,28 @@ static int do_action(const char* arg)
if (!strncmp(arg, "thread-", strlen("thread-"))) { if (!strncmp(arg, "thread-", strlen("thread-"))) {
return do_action_on_thread(arg + strlen("thread-")); return do_action_on_thread(arg + strlen("thread-"));
} else if (!strcmp(arg,"smash-stack")) { } else if (!strcmp(arg, "smash-stack")) {
return smash_stack(42); return smash_stack(42);
} else if (!strcmp(arg,"stack-overflow")) { } else if (!strcmp(arg, "stack-overflow")) {
overflow_stack(NULL); overflow_stack(NULL);
} else if (!strcmp(arg,"nostack")) { } else if (!strcmp(arg, "nostack")) {
crashnostack(); crashnostack();
} else if (!strcmp(arg,"ctest")) { } else if (!strcmp(arg, "ctest")) {
return ctest(); return ctest();
} else if (!strcmp(arg,"exit")) { } else if (!strcmp(arg, "exit")) {
exit(1); exit(1);
} else if (!strcmp(arg,"crash")) { } else if (!strcmp(arg, "crash")) {
return crash(42); return crash(42);
} else if (!strcmp(arg,"abort")) { } else if (!strcmp(arg, "abort")) {
maybeabort(); maybe_abort();
} else if (!strcmp(arg, "assert")) {
__assert("some_file.c", 123, "false");
} else if (!strcmp(arg, "assert2")) {
__assert2("some_file.c", 123, "some_function", "false");
} else if (!strcmp(arg, "LOG_ALWAYS_FATAL")) {
LOG_ALWAYS_FATAL("hello %s", "world");
} else if (!strcmp(arg, "LOG_ALWAYS_FATAL_IF")) {
LOG_ALWAYS_FATAL_IF(true, "hello %s", "world");
} else if (!strcmp(arg, "heap-usage")) { } else if (!strcmp(arg, "heap-usage")) {
abuse_heap(); abuse_heap();
} }
@ -148,6 +153,8 @@ static int do_action(const char* arg)
fprintf(stderr, " exit call exit(1)\n"); fprintf(stderr, " exit call exit(1)\n");
fprintf(stderr, " crash cause a SIGSEGV\n"); fprintf(stderr, " crash cause a SIGSEGV\n");
fprintf(stderr, " abort call abort()\n"); fprintf(stderr, " abort call abort()\n");
fprintf(stderr, " assert call assert() without a function\n");
fprintf(stderr, " assert2 call assert() with a function\n");
fprintf(stderr, "prefix any of the above with 'thread-' to not run\n"); fprintf(stderr, "prefix any of the above with 'thread-' to not run\n");
fprintf(stderr, "on the process' main thread.\n"); fprintf(stderr, "on the process' main thread.\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;

View file

@ -110,11 +110,11 @@ int __android_log_vprint(int prio, const char *tag,
const char *fmt, va_list ap); const char *fmt, va_list ap);
/* /*
* Log an assertion failure and SIGTRAP the process to have a chance * Log an assertion failure and abort the process to have a chance
* to inspect it, if a debugger is attached. This uses the FATAL priority. * to inspect it if a debugger is attached. This uses the FATAL priority.
*/ */
void __android_log_assert(const char *cond, const char *tag, void __android_log_assert(const char *cond, const char *tag,
const char *fmt, ...) const char *fmt, ...)
#if defined(__GNUC__) #if defined(__GNUC__)
__attribute__ ((noreturn)) __attribute__ ((noreturn))
__attribute__ ((format(printf, 3, 4))) __attribute__ ((format(printf, 3, 4)))

View file

@ -407,9 +407,15 @@ void __android_log_assert(const char *cond, const char *tag,
strcpy(buf, "Unspecified assertion failed"); strcpy(buf, "Unspecified assertion failed");
} }
#if __BIONIC__
// Ensure debuggerd gets to see what went wrong by keeping the C library in the loop.
extern __noreturn void __android_fatal(const char* tag, const char* format, ...) __printflike(2, 3);
__android_fatal(tag ? tag : "", "%s", buf);
#else
__android_log_write(ANDROID_LOG_FATAL, tag, buf); __android_log_write(ANDROID_LOG_FATAL, tag, buf);
__builtin_trap(); /* trap so we have a chance to debug the situation */ __builtin_trap(); /* trap so we have a chance to debug the situation */
#endif
/* NOTREACHED */
} }
int __android_log_bwrite(int32_t tag, const void *payload, size_t len) int __android_log_bwrite(int32_t tag, const void *payload, size_t len)

View file

@ -272,9 +272,15 @@ void __android_log_assert(const char *cond, const char *tag,
strcpy(buf, "Unspecified assertion failed"); strcpy(buf, "Unspecified assertion failed");
} }
#if __BIONIC__
// Ensure debuggerd gets to see what went wrong by keeping the C library in the loop.
extern __noreturn void __android_fatal(const char* tag, const char* format, ...) __printflike(2, 3);
__android_fatal(tag ? tag : "", "%s", buf);
#else
__android_log_write(ANDROID_LOG_FATAL, tag, buf); __android_log_write(ANDROID_LOG_FATAL, tag, buf);
__builtin_trap(); /* trap so we have a chance to debug the situation */ __builtin_trap(); /* trap so we have a chance to debug the situation */
#endif
/* NOTREACHED */
} }
int __android_log_bwrite(int32_t tag, const void *payload, size_t len) int __android_log_bwrite(int32_t tag, const void *payload, size_t len)