2010-01-20 21:36:51 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2013-10-10 00:50:50 +02:00
|
|
|
#include "../private/libc_logging.h" // Relative path so we can #include this .cpp file for testing.
|
|
|
|
#include "../private/ScopedPthreadMutexLocker.h"
|
2012-12-21 03:59:05 +01:00
|
|
|
|
2014-08-18 23:37:42 +02:00
|
|
|
#include <android/set_abort_message.h>
|
2012-10-30 01:37:13 +01:00
|
|
|
#include <assert.h>
|
2015-12-01 01:23:15 +01:00
|
|
|
#include <ctype.h>
|
2012-12-21 03:59:05 +01:00
|
|
|
#include <errno.h>
|
2013-04-04 22:46:46 +02:00
|
|
|
#include <fcntl.h>
|
2013-03-15 23:30:25 +01:00
|
|
|
#include <pthread.h>
|
2010-01-20 21:36:51 +01:00
|
|
|
#include <stdarg.h>
|
2012-12-21 03:59:05 +01:00
|
|
|
#include <stddef.h>
|
2013-03-15 23:30:25 +01:00
|
|
|
#include <stdlib.h>
|
2010-01-20 21:36:51 +01:00
|
|
|
#include <string.h>
|
2013-04-04 22:46:46 +02:00
|
|
|
#include <sys/mman.h>
|
2013-11-08 15:58:01 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
2013-04-04 22:46:46 +02:00
|
|
|
#include <sys/uio.h>
|
2013-11-08 15:58:01 +01:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <time.h>
|
2010-01-20 21:36:51 +01:00
|
|
|
#include <unistd.h>
|
2012-12-21 03:59:05 +01:00
|
|
|
|
2015-12-01 01:23:15 +01:00
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
__LIBC_HIDDEN__ abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common.
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
// Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java.
|
|
|
|
enum AndroidEventLogType {
|
|
|
|
EVENT_TYPE_INT = 0,
|
|
|
|
EVENT_TYPE_LONG = 1,
|
|
|
|
EVENT_TYPE_STRING = 2,
|
|
|
|
EVENT_TYPE_LIST = 3,
|
2015-04-28 23:35:45 +02:00
|
|
|
EVENT_TYPE_FLOAT = 4,
|
2012-10-30 01:37:13 +01:00
|
|
|
};
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
struct BufferOutputStream {
|
|
|
|
public:
|
|
|
|
BufferOutputStream(char* buffer, size_t size) : total(0) {
|
|
|
|
buffer_ = buffer;
|
|
|
|
end_ = buffer + size - 1;
|
|
|
|
pos_ = buffer_;
|
|
|
|
pos_[0] = '\0';
|
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
~BufferOutputStream() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Send(const char* data, int len) {
|
2012-10-30 01:37:13 +01:00
|
|
|
if (len < 0) {
|
2013-04-04 22:46:46 +02:00
|
|
|
len = strlen(data);
|
2012-10-30 01:37:13 +01:00
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2014-08-19 02:28:32 +02:00
|
|
|
total += len;
|
|
|
|
|
2010-01-20 21:36:51 +01:00
|
|
|
while (len > 0) {
|
2013-04-04 22:46:46 +02:00
|
|
|
int avail = end_ - pos_;
|
|
|
|
if (avail == 0) {
|
2014-08-19 02:28:32 +02:00
|
|
|
return;
|
2013-04-04 22:46:46 +02:00
|
|
|
}
|
|
|
|
if (avail > len) {
|
|
|
|
avail = len;
|
|
|
|
}
|
|
|
|
memcpy(pos_, data, avail);
|
|
|
|
pos_ += avail;
|
|
|
|
pos_[0] = '\0';
|
|
|
|
len -= avail;
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
2013-04-04 22:46:46 +02:00
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2014-08-19 02:28:32 +02:00
|
|
|
size_t total;
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
private:
|
|
|
|
char* buffer_;
|
|
|
|
char* pos_;
|
|
|
|
char* end_;
|
|
|
|
};
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
struct FdOutputStream {
|
|
|
|
public:
|
|
|
|
FdOutputStream(int fd) : total(0), fd_(fd) {
|
2013-01-18 03:36:06 +01:00
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
void Send(const char* data, int len) {
|
|
|
|
if (len < 0) {
|
|
|
|
len = strlen(data);
|
|
|
|
}
|
2013-03-15 23:30:25 +01:00
|
|
|
|
2014-08-19 02:28:32 +02:00
|
|
|
total += len;
|
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
while (len > 0) {
|
|
|
|
int rc = TEMP_FAILURE_RETRY(write(fd_, data, len));
|
|
|
|
if (rc == -1) {
|
2014-08-19 02:28:32 +02:00
|
|
|
return;
|
2013-04-04 22:46:46 +02:00
|
|
|
}
|
|
|
|
data += rc;
|
|
|
|
len -= rc;
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
2013-01-18 03:36:06 +01:00
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2014-08-19 02:28:32 +02:00
|
|
|
size_t total;
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
private:
|
|
|
|
int fd_;
|
|
|
|
};
|
2010-01-20 21:36:51 +01:00
|
|
|
|
|
|
|
/*** formatted output implementation
|
|
|
|
***/
|
|
|
|
|
|
|
|
/* Parse a decimal string from 'format + *ppos',
|
|
|
|
* return the value, and writes the new position past
|
|
|
|
* the decimal string in '*ppos' on exit.
|
|
|
|
*
|
|
|
|
* NOTE: Does *not* handle a sign prefix.
|
|
|
|
*/
|
2013-04-04 22:46:46 +02:00
|
|
|
static unsigned parse_decimal(const char *format, int *ppos) {
|
2010-01-20 21:36:51 +01:00
|
|
|
const char* p = format + *ppos;
|
|
|
|
unsigned result = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int ch = *p;
|
2013-11-08 15:58:01 +01:00
|
|
|
unsigned d = static_cast<unsigned>(ch - '0');
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
if (d >= 10U) {
|
2010-01-20 21:36:51 +01:00
|
|
|
break;
|
2013-04-04 22:46:46 +02:00
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
|
|
|
|
result = result*10 + d;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
*ppos = p - format;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-12-21 03:59:05 +01:00
|
|
|
// Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes.
|
|
|
|
// Assumes that buf_size > 0.
|
2013-01-28 19:36:31 +01:00
|
|
|
static void format_unsigned(char* buf, size_t buf_size, uint64_t value, int base, bool caps) {
|
2012-12-21 03:59:05 +01:00
|
|
|
char* p = buf;
|
|
|
|
char* end = buf + buf_size - 1;
|
|
|
|
|
|
|
|
// Generate digit string in reverse order.
|
|
|
|
while (value) {
|
|
|
|
unsigned d = value % base;
|
|
|
|
value /= base;
|
|
|
|
if (p != end) {
|
|
|
|
char ch;
|
|
|
|
if (d < 10) {
|
|
|
|
ch = '0' + d;
|
|
|
|
} else {
|
|
|
|
ch = (caps ? 'A' : 'a') + (d - 10);
|
|
|
|
}
|
|
|
|
*p++ = ch;
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
2012-12-21 03:59:05 +01:00
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2012-12-21 03:59:05 +01:00
|
|
|
// Special case for 0.
|
|
|
|
if (p == buf) {
|
|
|
|
if (p != end) {
|
|
|
|
*p++ = '0';
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
2012-12-21 03:59:05 +01:00
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
// Reverse digit string in-place.
|
|
|
|
size_t length = p - buf;
|
|
|
|
for (size_t i = 0, j = length - 1; i < j; ++i, --j) {
|
|
|
|
char ch = buf[i];
|
|
|
|
buf[i] = buf[j];
|
|
|
|
buf[j] = ch;
|
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
|
|
|
|
2013-01-28 19:36:31 +01:00
|
|
|
static void format_integer(char* buf, size_t buf_size, uint64_t value, char conversion) {
|
|
|
|
// Decode the conversion specifier.
|
|
|
|
int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o');
|
|
|
|
int base = 10;
|
|
|
|
if (conversion == 'x' || conversion == 'X') {
|
|
|
|
base = 16;
|
|
|
|
} else if (conversion == 'o') {
|
|
|
|
base = 8;
|
|
|
|
}
|
|
|
|
bool caps = (conversion == 'X');
|
2010-01-20 21:36:51 +01:00
|
|
|
|
2013-01-28 19:36:31 +01:00
|
|
|
if (is_signed && static_cast<int64_t>(value) < 0) {
|
|
|
|
buf[0] = '-';
|
|
|
|
buf += 1;
|
|
|
|
buf_size -= 1;
|
|
|
|
value = static_cast<uint64_t>(-static_cast<int64_t>(value));
|
|
|
|
}
|
|
|
|
format_unsigned(buf, buf_size, value, base, caps);
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
template <typename Out>
|
|
|
|
static void SendRepeat(Out& o, char ch, int count) {
|
|
|
|
char pad[8];
|
|
|
|
memset(pad, ch, sizeof(pad));
|
|
|
|
|
|
|
|
const int pad_size = static_cast<int>(sizeof(pad));
|
|
|
|
while (count > 0) {
|
|
|
|
int avail = count;
|
|
|
|
if (avail > pad_size) {
|
|
|
|
avail = pad_size;
|
|
|
|
}
|
|
|
|
o.Send(pad, avail);
|
|
|
|
count -= avail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-20 21:36:51 +01:00
|
|
|
/* Perform formatted output to an output target 'o' */
|
2013-04-04 22:46:46 +02:00
|
|
|
template <typename Out>
|
|
|
|
static void out_vformat(Out& o, const char* format, va_list args) {
|
2011-07-29 21:46:34 +02:00
|
|
|
int nn = 0;
|
2010-01-20 21:36:51 +01:00
|
|
|
|
|
|
|
for (;;) {
|
2011-07-29 21:46:34 +02:00
|
|
|
int mm;
|
|
|
|
int padZero = 0;
|
|
|
|
int padLeft = 0;
|
|
|
|
char sign = '\0';
|
|
|
|
int width = -1;
|
|
|
|
int prec = -1;
|
|
|
|
size_t bytelen = sizeof(int);
|
|
|
|
int slen;
|
|
|
|
char buffer[32]; /* temporary buffer used to format numbers */
|
|
|
|
|
2010-01-20 21:36:51 +01:00
|
|
|
char c;
|
|
|
|
|
|
|
|
/* first, find all characters that are not 0 or '%' */
|
|
|
|
/* then send them to the output directly */
|
|
|
|
mm = nn;
|
|
|
|
do {
|
|
|
|
c = format[mm];
|
|
|
|
if (c == '\0' || c == '%')
|
|
|
|
break;
|
|
|
|
mm++;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
if (mm > nn) {
|
2013-04-04 22:46:46 +02:00
|
|
|
o.Send(format+nn, mm-nn);
|
2010-01-20 21:36:51 +01:00
|
|
|
nn = mm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* is this it ? then exit */
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* nope, we are at a '%' modifier */
|
|
|
|
nn++; // skip it
|
|
|
|
|
|
|
|
/* parse flags */
|
|
|
|
for (;;) {
|
|
|
|
c = format[nn++];
|
|
|
|
if (c == '\0') { /* single trailing '%' ? */
|
|
|
|
c = '%';
|
2013-04-04 22:46:46 +02:00
|
|
|
o.Send(&c, 1);
|
2010-01-20 21:36:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (c == '0') {
|
|
|
|
padZero = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (c == '-') {
|
|
|
|
padLeft = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (c == ' ' || c == '+') {
|
|
|
|
sign = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse field width */
|
|
|
|
if ((c >= '0' && c <= '9')) {
|
|
|
|
nn --;
|
2013-11-08 15:58:01 +01:00
|
|
|
width = static_cast<int>(parse_decimal(format, &nn));
|
2010-01-20 21:36:51 +01:00
|
|
|
c = format[nn++];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse precision */
|
|
|
|
if (c == '.') {
|
2013-11-08 15:58:01 +01:00
|
|
|
prec = static_cast<int>(parse_decimal(format, &nn));
|
2010-01-20 21:36:51 +01:00
|
|
|
c = format[nn++];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* length modifier */
|
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
bytelen = sizeof(short);
|
|
|
|
if (format[nn] == 'h') {
|
|
|
|
bytelen = sizeof(char);
|
|
|
|
nn += 1;
|
|
|
|
}
|
|
|
|
c = format[nn++];
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
bytelen = sizeof(long);
|
|
|
|
if (format[nn] == 'l') {
|
|
|
|
bytelen = sizeof(long long);
|
|
|
|
nn += 1;
|
|
|
|
}
|
|
|
|
c = format[nn++];
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
bytelen = sizeof(size_t);
|
|
|
|
c = format[nn++];
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
bytelen = sizeof(ptrdiff_t);
|
|
|
|
c = format[nn++];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* conversion specifier */
|
2013-01-28 19:36:31 +01:00
|
|
|
const char* str = buffer;
|
2010-01-20 21:36:51 +01:00
|
|
|
if (c == 's') {
|
|
|
|
/* string */
|
|
|
|
str = va_arg(args, const char*);
|
2013-01-26 02:13:45 +01:00
|
|
|
if (str == NULL) {
|
|
|
|
str = "(null)";
|
|
|
|
}
|
2010-01-20 21:36:51 +01:00
|
|
|
} else if (c == 'c') {
|
|
|
|
/* character */
|
|
|
|
/* NOTE: char is promoted to int when passed through the stack */
|
2013-11-08 15:58:01 +01:00
|
|
|
buffer[0] = static_cast<char>(va_arg(args, int));
|
2010-01-20 21:36:51 +01:00
|
|
|
buffer[1] = '\0';
|
|
|
|
} else if (c == 'p') {
|
2013-11-08 15:58:01 +01:00
|
|
|
uint64_t value = reinterpret_cast<uintptr_t>(va_arg(args, void*));
|
2010-01-20 21:36:51 +01:00
|
|
|
buffer[0] = '0';
|
|
|
|
buffer[1] = 'x';
|
2013-01-28 19:36:31 +01:00
|
|
|
format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x');
|
2013-05-22 02:48:01 +02:00
|
|
|
} else if (c == 'd' || c == 'i' || c == 'o' || c == 'u' || c == 'x' || c == 'X') {
|
2010-01-20 21:36:51 +01:00
|
|
|
/* integers - first read value from stack */
|
|
|
|
uint64_t value;
|
2013-01-28 19:36:31 +01:00
|
|
|
int is_signed = (c == 'd' || c == 'i' || c == 'o');
|
2010-01-20 21:36:51 +01:00
|
|
|
|
|
|
|
/* NOTE: int8_t and int16_t are promoted to int when passed
|
|
|
|
* through the stack
|
|
|
|
*/
|
|
|
|
switch (bytelen) {
|
2013-11-08 15:58:01 +01:00
|
|
|
case 1: value = static_cast<uint8_t>(va_arg(args, int)); break;
|
|
|
|
case 2: value = static_cast<uint16_t>(va_arg(args, int)); break;
|
2010-01-20 21:36:51 +01:00
|
|
|
case 4: value = va_arg(args, uint32_t); break;
|
|
|
|
case 8: value = va_arg(args, uint64_t); break;
|
|
|
|
default: return; /* should not happen */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sign extension, if needed */
|
2013-01-28 19:36:31 +01:00
|
|
|
if (is_signed) {
|
2010-01-20 21:36:51 +01:00
|
|
|
int shift = 64 - 8*bytelen;
|
2013-11-08 15:58:01 +01:00
|
|
|
value = static_cast<uint64_t>((static_cast<int64_t>(value << shift)) >> shift);
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* format the number properly into our buffer */
|
2013-01-28 19:36:31 +01:00
|
|
|
format_integer(buffer, sizeof(buffer), value, c);
|
|
|
|
} else if (c == '%') {
|
|
|
|
buffer[0] = '%';
|
|
|
|
buffer[1] = '\0';
|
|
|
|
} else {
|
|
|
|
__assert(__FILE__, __LINE__, "conversion specifier unsupported");
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if we are here, 'str' points to the content that must be
|
|
|
|
* outputted. handle padding and alignment now */
|
|
|
|
|
|
|
|
slen = strlen(str);
|
|
|
|
|
2012-10-30 01:37:13 +01:00
|
|
|
if (sign != '\0' || prec != -1) {
|
|
|
|
__assert(__FILE__, __LINE__, "sign/precision unsupported");
|
|
|
|
}
|
|
|
|
|
2010-01-20 21:36:51 +01:00
|
|
|
if (slen < width && !padLeft) {
|
|
|
|
char padChar = padZero ? '0' : ' ';
|
2013-04-04 22:46:46 +02:00
|
|
|
SendRepeat(o, padChar, width - slen);
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
o.Send(str, slen);
|
2010-01-20 21:36:51 +01:00
|
|
|
|
|
|
|
if (slen < width && padLeft) {
|
|
|
|
char padChar = padZero ? '0' : ' ';
|
2013-04-04 22:46:46 +02:00
|
|
|
SendRepeat(o, padChar, width - slen);
|
2010-01-20 21:36:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-15 23:30:25 +01:00
|
|
|
|
2013-04-04 22:46:46 +02:00
|
|
|
int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) {
|
|
|
|
BufferOutputStream os(buffer, buffer_size);
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
out_vformat(os, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return os.total;
|
|
|
|
}
|
|
|
|
|
|
|
|
int __libc_format_fd(int fd, const char* format, ...) {
|
|
|
|
FdOutputStream os(fd);
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
out_vformat(os, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return os.total;
|
|
|
|
}
|
|
|
|
|
2013-10-08 22:19:00 +02:00
|
|
|
static int __libc_write_stderr(const char* tag, const char* msg) {
|
2015-03-25 20:36:18 +01:00
|
|
|
int fd = TEMP_FAILURE_RETRY(open("/dev/stderr", O_CLOEXEC | O_WRONLY | O_APPEND));
|
2013-10-08 22:19:00 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iovec vec[4];
|
|
|
|
vec[0].iov_base = const_cast<char*>(tag);
|
|
|
|
vec[0].iov_len = strlen(tag);
|
|
|
|
vec[1].iov_base = const_cast<char*>(": ");
|
|
|
|
vec[1].iov_len = 2;
|
|
|
|
vec[2].iov_base = const_cast<char*>(msg);
|
2015-02-02 21:24:46 +01:00
|
|
|
vec[2].iov_len = strlen(msg);
|
2013-10-08 22:19:00 +02:00
|
|
|
vec[3].iov_base = const_cast<char*>("\n");
|
|
|
|
vec[3].iov_len = 1;
|
|
|
|
|
|
|
|
int result = TEMP_FAILURE_RETRY(writev(fd, vec, 4));
|
|
|
|
close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-02-02 21:24:46 +01:00
|
|
|
static int __libc_open_log_socket() {
|
2013-11-08 15:58:01 +01:00
|
|
|
// ToDo: Ideally we want this to fail if the gid of the current
|
|
|
|
// process is AID_LOGD, but will have to wait until we have
|
|
|
|
// registered this in private/android_filesystem_config.h. We have
|
|
|
|
// found that all logd crashes thus far have had no problem stuffing
|
|
|
|
// the UNIX domain socket and moving on so not critical *today*.
|
|
|
|
|
2016-02-28 04:18:41 +01:00
|
|
|
int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
|
|
|
|
if (log_fd == -1) {
|
2013-11-08 15:58:01 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct sockaddr addr;
|
|
|
|
struct sockaddr_un addrUn;
|
|
|
|
} u;
|
|
|
|
memset(&u, 0, sizeof(u));
|
|
|
|
u.addrUn.sun_family = AF_UNIX;
|
|
|
|
strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path));
|
|
|
|
|
|
|
|
if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) {
|
|
|
|
close(log_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return log_fd;
|
|
|
|
}
|
2014-03-05 22:44:00 +01:00
|
|
|
|
2015-12-01 01:23:15 +01:00
|
|
|
struct cache {
|
|
|
|
const prop_info* pinfo;
|
|
|
|
uint32_t serial;
|
|
|
|
char c;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void refresh_cache(struct cache *cache, const char *key)
|
|
|
|
{
|
|
|
|
if (!cache->pinfo) {
|
|
|
|
cache->pinfo = __system_property_find(key);
|
|
|
|
if (!cache->pinfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t serial = __system_property_serial(cache->pinfo);
|
|
|
|
if (serial == cache->serial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cache->serial = serial;
|
|
|
|
|
|
|
|
char buf[PROP_VALUE_MAX];
|
|
|
|
__system_property_read(cache->pinfo, 0, buf);
|
|
|
|
cache->c = buf[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timestamp state generally remains constant, since a change is
|
|
|
|
// rare, we can accept a trylock failure gracefully.
|
|
|
|
static pthread_mutex_t lock_clockid = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
2015-12-08 22:42:41 +01:00
|
|
|
static clockid_t __android_log_clockid()
|
2015-12-01 01:23:15 +01:00
|
|
|
{
|
|
|
|
static struct cache r_time_cache = { NULL, static_cast<uint32_t>(-1), 0 };
|
|
|
|
static struct cache p_time_cache = { NULL, static_cast<uint32_t>(-1), 0 };
|
|
|
|
char c;
|
|
|
|
|
|
|
|
if (pthread_mutex_trylock(&lock_clockid)) {
|
|
|
|
// We are willing to accept some race in this context
|
|
|
|
if (!(c = p_time_cache.c)) {
|
|
|
|
c = r_time_cache.c;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
static uint32_t serial;
|
|
|
|
uint32_t current_serial = __system_property_area_serial();
|
|
|
|
if (current_serial != serial) {
|
|
|
|
refresh_cache(&r_time_cache, "ro.logd.timestamp");
|
|
|
|
refresh_cache(&p_time_cache, "persist.logd.timestamp");
|
|
|
|
serial = current_serial;
|
|
|
|
}
|
|
|
|
if (!(c = p_time_cache.c)) {
|
|
|
|
c = r_time_cache.c;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&lock_clockid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (tolower(c) == 'm') ? CLOCK_MONOTONIC : CLOCK_REALTIME;
|
|
|
|
}
|
|
|
|
|
2014-03-05 22:44:00 +01:00
|
|
|
struct log_time { // Wire format
|
|
|
|
uint32_t tv_sec;
|
|
|
|
uint32_t tv_nsec;
|
|
|
|
};
|
2013-11-08 15:58:01 +01:00
|
|
|
|
2016-02-06 01:17:39 +01:00
|
|
|
int __libc_write_log(int priority, const char* tag, const char* msg) {
|
2013-11-08 15:58:01 +01:00
|
|
|
int main_log_fd = __libc_open_log_socket();
|
|
|
|
if (main_log_fd == -1) {
|
|
|
|
// Try stderr instead.
|
|
|
|
return __libc_write_stderr(tag, msg);
|
|
|
|
}
|
|
|
|
|
2014-03-21 00:07:55 +01:00
|
|
|
iovec vec[6];
|
2014-05-08 01:35:59 +02:00
|
|
|
char log_id = (priority == ANDROID_LOG_FATAL) ? LOG_ID_CRASH : LOG_ID_MAIN;
|
2013-11-08 15:58:01 +01:00
|
|
|
vec[0].iov_base = &log_id;
|
|
|
|
vec[0].iov_len = sizeof(log_id);
|
2014-03-21 00:07:55 +01:00
|
|
|
uint16_t tid = gettid();
|
|
|
|
vec[1].iov_base = &tid;
|
|
|
|
vec[1].iov_len = sizeof(tid);
|
2014-03-05 22:44:00 +01:00
|
|
|
timespec ts;
|
2015-12-08 22:42:41 +01:00
|
|
|
clock_gettime(__android_log_clockid(), &ts);
|
2014-03-05 22:44:00 +01:00
|
|
|
log_time realtime_ts;
|
|
|
|
realtime_ts.tv_sec = ts.tv_sec;
|
|
|
|
realtime_ts.tv_nsec = ts.tv_nsec;
|
2014-03-21 00:07:55 +01:00
|
|
|
vec[2].iov_base = &realtime_ts;
|
|
|
|
vec[2].iov_len = sizeof(realtime_ts);
|
|
|
|
|
|
|
|
vec[3].iov_base = &priority;
|
|
|
|
vec[3].iov_len = 1;
|
|
|
|
vec[4].iov_base = const_cast<char*>(tag);
|
2015-02-05 21:02:04 +01:00
|
|
|
vec[4].iov_len = strlen(tag) + 1;
|
2014-03-21 00:07:55 +01:00
|
|
|
vec[5].iov_base = const_cast<char*>(msg);
|
2015-02-05 21:02:04 +01:00
|
|
|
vec[5].iov_len = strlen(msg) + 1;
|
2013-04-04 22:46:46 +02:00
|
|
|
|
2013-11-08 15:58:01 +01:00
|
|
|
int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, sizeof(vec) / sizeof(vec[0])));
|
2013-06-21 22:28:42 +02:00
|
|
|
close(main_log_fd);
|
|
|
|
return result;
|
2013-04-04 22:46:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) {
|
|
|
|
char buffer[1024];
|
|
|
|
BufferOutputStream os(buffer, sizeof(buffer));
|
|
|
|
out_vformat(os, format, args);
|
|
|
|
return __libc_write_log(priority, tag, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
int __libc_format_log(int priority, const char* tag, const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
int result = __libc_format_log_va_list(priority, tag, format, args);
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
2013-03-15 23:30:25 +01:00
|
|
|
|
2016-02-26 05:33:47 +01:00
|
|
|
static void __libc_fatal_va_list(const char* prefix, const char* format, va_list args) {
|
|
|
|
char msg[1024];
|
|
|
|
BufferOutputStream os(msg, sizeof(msg));
|
2013-03-15 23:30:25 +01:00
|
|
|
|
2016-02-26 05:33:47 +01:00
|
|
|
if (prefix) {
|
|
|
|
os.Send(prefix, strlen(prefix));
|
|
|
|
os.Send(": ", 2);
|
2013-03-15 23:30:25 +01:00
|
|
|
}
|
2013-04-04 22:46:46 +02:00
|
|
|
|
|
|
|
out_vformat(os, format, args);
|
|
|
|
|
2015-02-02 21:24:46 +01:00
|
|
|
// Log to stderr for the benefit of "adb shell" users.
|
2014-07-20 20:49:46 +02:00
|
|
|
struct iovec iov[2] = {
|
2015-02-02 21:24:46 +01:00
|
|
|
{ msg, os.total },
|
|
|
|
{ const_cast<char*>("\n"), 1 },
|
2014-07-20 20:49:46 +02:00
|
|
|
};
|
2015-02-02 21:24:46 +01:00
|
|
|
TEMP_FAILURE_RETRY(writev(2, iov, 2));
|
2013-04-04 22:46:46 +02:00
|
|
|
|
|
|
|
// Log to the log for the benefit of regular app developers (whose stdout and stderr are closed).
|
2014-05-07 05:37:22 +02:00
|
|
|
__libc_write_log(ANDROID_LOG_FATAL, "libc", msg);
|
2013-04-04 22:46:46 +02:00
|
|
|
|
2014-08-18 23:37:42 +02:00
|
|
|
android_set_abort_message(msg);
|
2013-06-12 23:05:46 +02:00
|
|
|
}
|
|
|
|
|
2016-02-26 05:33:47 +01:00
|
|
|
void __libc_fatal(const char* fmt, ...) {
|
2013-06-12 23:05:46 +02:00
|
|
|
va_list args;
|
2016-02-26 05:33:47 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
__libc_fatal_va_list(nullptr, fmt, args);
|
2013-06-12 23:05:46 +02:00
|
|
|
va_end(args);
|
2016-02-26 05:33:47 +01:00
|
|
|
abort();
|
2013-06-12 23:05:46 +02:00
|
|
|
}
|
2013-04-04 22:46:46 +02:00
|
|
|
|
2016-02-26 05:33:47 +01:00
|
|
|
void __fortify_fatal(const char* fmt, ...) {
|
2013-06-12 23:05:46 +02:00
|
|
|
va_list args;
|
2016-02-26 05:33:47 +01:00
|
|
|
va_start(args, fmt);
|
|
|
|
__libc_fatal_va_list("FORTIFY", fmt, args);
|
2014-04-23 23:52:49 +02:00
|
|
|
va_end(args);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2014-08-18 23:37:42 +02:00
|
|
|
void android_set_abort_message(const char* msg) {
|
2014-05-14 19:02:03 +02:00
|
|
|
ScopedPthreadMutexLocker locker(&g_abort_msg_lock);
|
2014-05-07 05:37:22 +02:00
|
|
|
|
|
|
|
if (__abort_message_ptr == NULL) {
|
|
|
|
// We must have crashed _very_ early.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*__abort_message_ptr != NULL) {
|
|
|
|
// We already have an abort message.
|
|
|
|
// Assume that the first crash is the one most worth reporting.
|
|
|
|
return;
|
|
|
|
}
|
2013-04-04 22:46:46 +02:00
|
|
|
|
|
|
|
size_t size = sizeof(abort_msg_t) + strlen(msg) + 1;
|
|
|
|
void* map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|
|
|
if (map == MAP_FAILED) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-07 05:37:22 +02:00
|
|
|
// TODO: if we stick to the current "one-shot" scheme, we can remove this code and
|
|
|
|
// stop storing the size.
|
|
|
|
if (*__abort_message_ptr != NULL) {
|
|
|
|
munmap(*__abort_message_ptr, (*__abort_message_ptr)->size);
|
2013-04-04 22:46:46 +02:00
|
|
|
}
|
2014-05-07 05:37:22 +02:00
|
|
|
abort_msg_t* new_abort_message = reinterpret_cast<abort_msg_t*>(map);
|
|
|
|
new_abort_message->size = size;
|
|
|
|
strcpy(new_abort_message->msg, msg);
|
|
|
|
*__abort_message_ptr = new_abort_message;
|
2013-04-04 22:46:46 +02:00
|
|
|
}
|