blob: b57e24297a60ebe08d8982cf4a3e675cf08ee39a [file] [log] [blame]
David 'Digit' Turner5c734642010-01-20 12:36:51 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hugheseb847bc2013-10-09 15:50:50 -070029#include "../private/libc_logging.h" // Relative path so we can #include this .cpp file for testing.
30#include "../private/ScopedPthreadMutexLocker.h"
Elliott Hugheseababde2012-12-20 18:59:05 -080031
32#include <assert.h>
33#include <errno.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070034#include <fcntl.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070035#include <pthread.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080036#include <stdarg.h>
37#include <stddef.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070038#include <stdlib.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080039#include <string.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070040#include <sys/mman.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080041#include <sys/socket.h>
42#include <sys/types.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080043#include <sys/uio.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080044#include <sys/un.h>
45#include <time.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070046#include <unistd.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080047
Elliott Hughes1728b232014-05-14 10:02:03 -070048static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070049
Elliott Hughes0d787c12013-04-04 13:46:46 -070050__LIBC_HIDDEN__ abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common.
David 'Digit' Turner5c734642010-01-20 12:36:51 -080051
Elliott Hughes0d787c12013-04-04 13:46:46 -070052// Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java.
53enum AndroidEventLogType {
54 EVENT_TYPE_INT = 0,
55 EVENT_TYPE_LONG = 1,
56 EVENT_TYPE_STRING = 2,
57 EVENT_TYPE_LIST = 3,
58};
59
60struct BufferOutputStream {
61 public:
62 BufferOutputStream(char* buffer, size_t size) : total(0) {
63 buffer_ = buffer;
64 end_ = buffer + size - 1;
65 pos_ = buffer_;
66 pos_[0] = '\0';
67 }
68
69 ~BufferOutputStream() {
70 }
71
72 void Send(const char* data, int len) {
73 if (len < 0) {
74 len = strlen(data);
75 }
76
77 while (len > 0) {
78 int avail = end_ - pos_;
79 if (avail == 0) {
80 break;
81 }
82 if (avail > len) {
83 avail = len;
84 }
85 memcpy(pos_, data, avail);
86 pos_ += avail;
87 pos_[0] = '\0';
88 len -= avail;
89 total += avail;
David 'Digit' Turner5c734642010-01-20 12:36:51 -080090 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080091 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -080092
Elliott Hughes0d787c12013-04-04 13:46:46 -070093 int total;
David 'Digit' Turner5c734642010-01-20 12:36:51 -080094
Elliott Hughes0d787c12013-04-04 13:46:46 -070095 private:
96 char* buffer_;
97 char* pos_;
98 char* end_;
99};
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800100
Elliott Hughes0d787c12013-04-04 13:46:46 -0700101struct FdOutputStream {
102 public:
103 FdOutputStream(int fd) : total(0), fd_(fd) {
104 }
105
106 void Send(const char* data, int len) {
107 if (len < 0) {
108 len = strlen(data);
109 }
110
111 while (len > 0) {
112 int rc = TEMP_FAILURE_RETRY(write(fd_, data, len));
113 if (rc == -1) {
114 break;
115 }
116 data += rc;
117 len -= rc;
118 total += rc;
119 }
120 }
121
122 int total;
123
124 private:
125 int fd_;
126};
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800127
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800128/*** formatted output implementation
129 ***/
130
131/* Parse a decimal string from 'format + *ppos',
132 * return the value, and writes the new position past
133 * the decimal string in '*ppos' on exit.
134 *
135 * NOTE: Does *not* handle a sign prefix.
136 */
Elliott Hughes0d787c12013-04-04 13:46:46 -0700137static unsigned parse_decimal(const char *format, int *ppos) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800138 const char* p = format + *ppos;
139 unsigned result = 0;
140
141 for (;;) {
142 int ch = *p;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800143 unsigned d = static_cast<unsigned>(ch - '0');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800144
Elliott Hughes0d787c12013-04-04 13:46:46 -0700145 if (d >= 10U) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800146 break;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700147 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800148
149 result = result*10 + d;
150 p++;
151 }
152 *ppos = p - format;
153 return result;
154}
155
Elliott Hugheseababde2012-12-20 18:59:05 -0800156// Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes.
157// Assumes that buf_size > 0.
Elliott Hughes41b31792013-01-28 10:36:31 -0800158static void format_unsigned(char* buf, size_t buf_size, uint64_t value, int base, bool caps) {
Elliott Hugheseababde2012-12-20 18:59:05 -0800159 char* p = buf;
160 char* end = buf + buf_size - 1;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800161
Elliott Hugheseababde2012-12-20 18:59:05 -0800162 // Generate digit string in reverse order.
163 while (value) {
164 unsigned d = value % base;
165 value /= base;
166 if (p != end) {
167 char ch;
168 if (d < 10) {
169 ch = '0' + d;
170 } else {
171 ch = (caps ? 'A' : 'a') + (d - 10);
172 }
173 *p++ = ch;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800174 }
Elliott Hugheseababde2012-12-20 18:59:05 -0800175 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800176
Elliott Hugheseababde2012-12-20 18:59:05 -0800177 // Special case for 0.
178 if (p == buf) {
179 if (p != end) {
180 *p++ = '0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800181 }
Elliott Hugheseababde2012-12-20 18:59:05 -0800182 }
183 *p = '\0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800184
Elliott Hugheseababde2012-12-20 18:59:05 -0800185 // Reverse digit string in-place.
186 size_t length = p - buf;
187 for (size_t i = 0, j = length - 1; i < j; ++i, --j) {
188 char ch = buf[i];
189 buf[i] = buf[j];
190 buf[j] = ch;
191 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800192}
193
Elliott Hughes41b31792013-01-28 10:36:31 -0800194static void format_integer(char* buf, size_t buf_size, uint64_t value, char conversion) {
195 // Decode the conversion specifier.
196 int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o');
197 int base = 10;
198 if (conversion == 'x' || conversion == 'X') {
199 base = 16;
200 } else if (conversion == 'o') {
201 base = 8;
202 }
203 bool caps = (conversion == 'X');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800204
Elliott Hughes41b31792013-01-28 10:36:31 -0800205 if (is_signed && static_cast<int64_t>(value) < 0) {
206 buf[0] = '-';
207 buf += 1;
208 buf_size -= 1;
209 value = static_cast<uint64_t>(-static_cast<int64_t>(value));
210 }
211 format_unsigned(buf, buf_size, value, base, caps);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800212}
213
Elliott Hughes0d787c12013-04-04 13:46:46 -0700214template <typename Out>
215static void SendRepeat(Out& o, char ch, int count) {
216 char pad[8];
217 memset(pad, ch, sizeof(pad));
218
219 const int pad_size = static_cast<int>(sizeof(pad));
220 while (count > 0) {
221 int avail = count;
222 if (avail > pad_size) {
223 avail = pad_size;
224 }
225 o.Send(pad, avail);
226 count -= avail;
227 }
228}
229
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800230/* Perform formatted output to an output target 'o' */
Elliott Hughes0d787c12013-04-04 13:46:46 -0700231template <typename Out>
232static void out_vformat(Out& o, const char* format, va_list args) {
Elliott Hughesafe63602014-07-23 11:38:38 -0700233 int caller_errno = errno;
Andy McFaddenec92af82011-07-29 12:46:34 -0700234 int nn = 0;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800235
236 for (;;) {
Andy McFaddenec92af82011-07-29 12:46:34 -0700237 int mm;
238 int padZero = 0;
239 int padLeft = 0;
240 char sign = '\0';
241 int width = -1;
242 int prec = -1;
243 size_t bytelen = sizeof(int);
Andy McFaddenec92af82011-07-29 12:46:34 -0700244 int slen;
245 char buffer[32]; /* temporary buffer used to format numbers */
246
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800247 char c;
248
249 /* first, find all characters that are not 0 or '%' */
250 /* then send them to the output directly */
251 mm = nn;
252 do {
253 c = format[mm];
254 if (c == '\0' || c == '%')
255 break;
256 mm++;
257 } while (1);
258
259 if (mm > nn) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700260 o.Send(format+nn, mm-nn);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800261 nn = mm;
262 }
263
264 /* is this it ? then exit */
265 if (c == '\0')
266 break;
267
268 /* nope, we are at a '%' modifier */
269 nn++; // skip it
270
271 /* parse flags */
272 for (;;) {
273 c = format[nn++];
274 if (c == '\0') { /* single trailing '%' ? */
275 c = '%';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700276 o.Send(&c, 1);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800277 return;
278 }
279 else if (c == '0') {
280 padZero = 1;
281 continue;
282 }
283 else if (c == '-') {
284 padLeft = 1;
285 continue;
286 }
287 else if (c == ' ' || c == '+') {
288 sign = c;
289 continue;
290 }
291 break;
292 }
293
294 /* parse field width */
295 if ((c >= '0' && c <= '9')) {
296 nn --;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800297 width = static_cast<int>(parse_decimal(format, &nn));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800298 c = format[nn++];
299 }
300
301 /* parse precision */
302 if (c == '.') {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800303 prec = static_cast<int>(parse_decimal(format, &nn));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800304 c = format[nn++];
305 }
306
307 /* length modifier */
308 switch (c) {
309 case 'h':
310 bytelen = sizeof(short);
311 if (format[nn] == 'h') {
312 bytelen = sizeof(char);
313 nn += 1;
314 }
315 c = format[nn++];
316 break;
317 case 'l':
318 bytelen = sizeof(long);
319 if (format[nn] == 'l') {
320 bytelen = sizeof(long long);
321 nn += 1;
322 }
323 c = format[nn++];
324 break;
325 case 'z':
326 bytelen = sizeof(size_t);
327 c = format[nn++];
328 break;
329 case 't':
330 bytelen = sizeof(ptrdiff_t);
331 c = format[nn++];
332 break;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800333 default:
334 ;
335 }
336
337 /* conversion specifier */
Elliott Hughes41b31792013-01-28 10:36:31 -0800338 const char* str = buffer;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800339 if (c == 's') {
340 /* string */
341 str = va_arg(args, const char*);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800342 if (str == NULL) {
343 str = "(null)";
344 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800345 } else if (c == 'c') {
346 /* character */
347 /* NOTE: char is promoted to int when passed through the stack */
Mark Salyzyn0336e352013-11-08 06:58:01 -0800348 buffer[0] = static_cast<char>(va_arg(args, int));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800349 buffer[1] = '\0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800350 } else if (c == 'p') {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800351 uint64_t value = reinterpret_cast<uintptr_t>(va_arg(args, void*));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800352 buffer[0] = '0';
353 buffer[1] = 'x';
Elliott Hughes41b31792013-01-28 10:36:31 -0800354 format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x');
Christopher Ferris885f3b92013-05-21 17:48:01 -0700355 } else if (c == 'd' || c == 'i' || c == 'o' || c == 'u' || c == 'x' || c == 'X') {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800356 /* integers - first read value from stack */
357 uint64_t value;
Elliott Hughes41b31792013-01-28 10:36:31 -0800358 int is_signed = (c == 'd' || c == 'i' || c == 'o');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800359
360 /* NOTE: int8_t and int16_t are promoted to int when passed
361 * through the stack
362 */
363 switch (bytelen) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800364 case 1: value = static_cast<uint8_t>(va_arg(args, int)); break;
365 case 2: value = static_cast<uint16_t>(va_arg(args, int)); break;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800366 case 4: value = va_arg(args, uint32_t); break;
367 case 8: value = va_arg(args, uint64_t); break;
368 default: return; /* should not happen */
369 }
370
371 /* sign extension, if needed */
Elliott Hughes41b31792013-01-28 10:36:31 -0800372 if (is_signed) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800373 int shift = 64 - 8*bytelen;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800374 value = static_cast<uint64_t>((static_cast<int64_t>(value << shift)) >> shift);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800375 }
376
377 /* format the number properly into our buffer */
Elliott Hughes41b31792013-01-28 10:36:31 -0800378 format_integer(buffer, sizeof(buffer), value, c);
379 } else if (c == '%') {
380 buffer[0] = '%';
381 buffer[1] = '\0';
Elliott Hughes3ad8ecb2014-07-21 16:35:24 -0700382 } else if (c == 'm') {
383 // syslog-like %m for strerror(errno).
Elliott Hughesafe63602014-07-23 11:38:38 -0700384 str = strerror(caller_errno);
Elliott Hughes41b31792013-01-28 10:36:31 -0800385 } else {
386 __assert(__FILE__, __LINE__, "conversion specifier unsupported");
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800387 }
388
389 /* if we are here, 'str' points to the content that must be
390 * outputted. handle padding and alignment now */
391
392 slen = strlen(str);
393
Elliott Hughes18a206c2012-10-29 17:37:13 -0700394 if (sign != '\0' || prec != -1) {
395 __assert(__FILE__, __LINE__, "sign/precision unsupported");
396 }
397
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800398 if (slen < width && !padLeft) {
399 char padChar = padZero ? '0' : ' ';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700400 SendRepeat(o, padChar, width - slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800401 }
402
Elliott Hughes0d787c12013-04-04 13:46:46 -0700403 o.Send(str, slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800404
405 if (slen < width && padLeft) {
406 char padChar = padZero ? '0' : ' ';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700407 SendRepeat(o, padChar, width - slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800408 }
409 }
410}
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700411
Elliott Hughes0d787c12013-04-04 13:46:46 -0700412int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) {
413 BufferOutputStream os(buffer, buffer_size);
414 va_list args;
415 va_start(args, format);
416 out_vformat(os, format, args);
417 va_end(args);
418 return os.total;
419}
420
421int __libc_format_fd(int fd, const char* format, ...) {
422 FdOutputStream os(fd);
423 va_list args;
424 va_start(args, format);
425 out_vformat(os, format, args);
426 va_end(args);
427 return os.total;
428}
429
Elliott Hughes0f395b72013-10-08 13:19:00 -0700430static int __libc_write_stderr(const char* tag, const char* msg) {
431 int fd = TEMP_FAILURE_RETRY(open("/dev/stderr", O_CLOEXEC | O_WRONLY));
432 if (fd == -1) {
433 return -1;
434 }
435
436 iovec vec[4];
437 vec[0].iov_base = const_cast<char*>(tag);
438 vec[0].iov_len = strlen(tag);
439 vec[1].iov_base = const_cast<char*>(": ");
440 vec[1].iov_len = 2;
441 vec[2].iov_base = const_cast<char*>(msg);
442 vec[2].iov_len = strlen(msg) + 1;
443 vec[3].iov_base = const_cast<char*>("\n");
444 vec[3].iov_len = 1;
445
446 int result = TEMP_FAILURE_RETRY(writev(fd, vec, 4));
447 close(fd);
448 return result;
449}
450
Mark Salyzyn0336e352013-11-08 06:58:01 -0800451#ifdef TARGET_USES_LOGD
452static int __libc_open_log_socket()
453{
454 // ToDo: Ideally we want this to fail if the gid of the current
455 // process is AID_LOGD, but will have to wait until we have
456 // registered this in private/android_filesystem_config.h. We have
457 // found that all logd crashes thus far have had no problem stuffing
458 // the UNIX domain socket and moving on so not critical *today*.
459
Nick Kralevichbae5b1d2014-07-02 22:39:14 -0700460 int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
Mark Salyzyn0336e352013-11-08 06:58:01 -0800461 if (log_fd < 0) {
462 return -1;
463 }
464
465 if (fcntl(log_fd, F_SETFL, O_NONBLOCK) == -1) {
466 close(log_fd);
467 return -1;
468 }
469
470 union {
471 struct sockaddr addr;
472 struct sockaddr_un addrUn;
473 } u;
474 memset(&u, 0, sizeof(u));
475 u.addrUn.sun_family = AF_UNIX;
476 strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path));
477
478 if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) {
479 close(log_fd);
480 return -1;
481 }
482
483 return log_fd;
484}
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800485
486struct log_time { // Wire format
487 uint32_t tv_sec;
488 uint32_t tv_nsec;
489};
Mark Salyzyn0336e352013-11-08 06:58:01 -0800490#endif
491
Elliott Hughes0d787c12013-04-04 13:46:46 -0700492static int __libc_write_log(int priority, const char* tag, const char* msg) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800493#ifdef TARGET_USES_LOGD
494 int main_log_fd = __libc_open_log_socket();
495
496 if (main_log_fd == -1) {
497 // Try stderr instead.
498 return __libc_write_stderr(tag, msg);
499 }
500
Mark Salyzyn8664be52014-03-20 16:07:55 -0700501 iovec vec[6];
Elliott Hughes01110192014-05-07 16:35:59 -0700502 char log_id = (priority == ANDROID_LOG_FATAL) ? LOG_ID_CRASH : LOG_ID_MAIN;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800503 vec[0].iov_base = &log_id;
504 vec[0].iov_len = sizeof(log_id);
Mark Salyzyn8664be52014-03-20 16:07:55 -0700505 uint16_t tid = gettid();
506 vec[1].iov_base = &tid;
507 vec[1].iov_len = sizeof(tid);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800508 timespec ts;
509 clock_gettime(CLOCK_REALTIME, &ts);
510 log_time realtime_ts;
511 realtime_ts.tv_sec = ts.tv_sec;
512 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700513 vec[2].iov_base = &realtime_ts;
514 vec[2].iov_len = sizeof(realtime_ts);
Mark Salyzyn0336e352013-11-08 06:58:01 -0800515
Mark Salyzyn8664be52014-03-20 16:07:55 -0700516 vec[3].iov_base = &priority;
517 vec[3].iov_len = 1;
518 vec[4].iov_base = const_cast<char*>(tag);
519 vec[4].iov_len = strlen(tag) + 1;
520 vec[5].iov_base = const_cast<char*>(msg);
521 vec[5].iov_len = strlen(msg) + 1;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800522#else
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700523 int main_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/main", O_CLOEXEC | O_WRONLY));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700524 if (main_log_fd == -1) {
Elliott Hughes0f395b72013-10-08 13:19:00 -0700525 if (errno == ENOTDIR) {
526 // /dev/log isn't a directory? Maybe we're running on the host? Try stderr instead.
527 return __libc_write_stderr(tag, msg);
528 }
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700529 return -1;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700530 }
531
532 iovec vec[3];
533 vec[0].iov_base = &priority;
534 vec[0].iov_len = 1;
535 vec[1].iov_base = const_cast<char*>(tag);
536 vec[1].iov_len = strlen(tag) + 1;
537 vec[2].iov_base = const_cast<char*>(msg);
538 vec[2].iov_len = strlen(msg) + 1;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800539#endif
Elliott Hughes0d787c12013-04-04 13:46:46 -0700540
Mark Salyzyn0336e352013-11-08 06:58:01 -0800541 int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, sizeof(vec) / sizeof(vec[0])));
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700542 close(main_log_fd);
543 return result;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700544}
545
546int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) {
547 char buffer[1024];
548 BufferOutputStream os(buffer, sizeof(buffer));
549 out_vformat(os, format, args);
550 return __libc_write_log(priority, tag, buffer);
551}
552
553int __libc_format_log(int priority, const char* tag, const char* format, ...) {
554 va_list args;
555 va_start(args, format);
556 int result = __libc_format_log_va_list(priority, tag, format, args);
557 va_end(args);
558 return result;
559}
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700560
561static int __libc_android_log_event(int32_t tag, char type, const void* payload, size_t len) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800562#ifdef TARGET_USES_LOGD
Mark Salyzyn8664be52014-03-20 16:07:55 -0700563 iovec vec[6];
Mark Salyzyn0336e352013-11-08 06:58:01 -0800564 char log_id = LOG_ID_EVENTS;
565 vec[0].iov_base = &log_id;
566 vec[0].iov_len = sizeof(log_id);
Mark Salyzyn8664be52014-03-20 16:07:55 -0700567 uint16_t tid = gettid();
568 vec[1].iov_base = &tid;
569 vec[1].iov_len = sizeof(tid);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800570 timespec ts;
571 clock_gettime(CLOCK_REALTIME, &ts);
572 log_time realtime_ts;
573 realtime_ts.tv_sec = ts.tv_sec;
574 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700575 vec[2].iov_base = &realtime_ts;
576 vec[2].iov_len = sizeof(realtime_ts);
Mark Salyzyn0336e352013-11-08 06:58:01 -0800577
Mark Salyzyn8664be52014-03-20 16:07:55 -0700578 vec[3].iov_base = &tag;
579 vec[3].iov_len = sizeof(tag);
580 vec[4].iov_base = &type;
581 vec[4].iov_len = sizeof(type);
582 vec[5].iov_base = const_cast<void*>(payload);
583 vec[5].iov_len = len;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800584
585 int event_log_fd = __libc_open_log_socket();
586#else
Elliott Hughes0d787c12013-04-04 13:46:46 -0700587 iovec vec[3];
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700588 vec[0].iov_base = &tag;
589 vec[0].iov_len = sizeof(tag);
590 vec[1].iov_base = &type;
591 vec[1].iov_len = sizeof(type);
592 vec[2].iov_base = const_cast<void*>(payload);
593 vec[2].iov_len = len;
594
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700595 int event_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/events", O_CLOEXEC | O_WRONLY));
Mark Salyzyn0336e352013-11-08 06:58:01 -0800596#endif
597
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700598 if (event_log_fd == -1) {
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700599 return -1;
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700600 }
Mark Salyzyn0336e352013-11-08 06:58:01 -0800601 int result = TEMP_FAILURE_RETRY(writev(event_log_fd, vec, sizeof(vec) / sizeof(vec[0])));
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700602 close(event_log_fd);
603 return result;
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700604}
605
606void __libc_android_log_event_int(int32_t tag, int value) {
607 __libc_android_log_event(tag, EVENT_TYPE_INT, &value, sizeof(value));
608}
609
610void __libc_android_log_event_uid(int32_t tag) {
611 __libc_android_log_event_int(tag, getuid());
612}
613
Elliott Hughesd1eda332013-10-15 16:43:38 -0700614void __fortify_chk_fail(const char* msg, uint32_t tag) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700615 if (tag != 0) {
616 __libc_android_log_event_uid(tag);
617 }
Elliott Hughes0d787c12013-04-04 13:46:46 -0700618 __libc_fatal("FORTIFY_SOURCE: %s. Calling abort().", msg);
619}
620
Elliott Hughesc78368f2014-05-06 20:37:22 -0700621static void __libc_fatal(const char* format, va_list args) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700622 char msg[1024];
623 BufferOutputStream os(msg, sizeof(msg));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700624 out_vformat(os, format, args);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700625
Dmitriy Ivanov12bf3bc2014-07-01 14:24:45 -0700626 // log to stderr for the benefit of "adb shell" users.
Dan Albert97e31de2014-07-20 11:49:46 -0700627 struct iovec iov[2] = {
628 {msg, strlen(msg)},
629 {const_cast<void*>(static_cast<const void*>("\n")), 1},
630 };
631 writev(2, iov, 2);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700632
633 // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed).
Elliott Hughesc78368f2014-05-06 20:37:22 -0700634 __libc_write_log(ANDROID_LOG_FATAL, "libc", msg);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700635
Elliott Hughesc78368f2014-05-06 20:37:22 -0700636 __android_set_abort_message(msg);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700637}
Elliott Hughes0d787c12013-04-04 13:46:46 -0700638
Elliott Hughes61e699a2013-06-12 14:05:46 -0700639void __libc_fatal_no_abort(const char* format, ...) {
640 va_list args;
641 va_start(args, format);
Elliott Hughesc78368f2014-05-06 20:37:22 -0700642 __libc_fatal(format, args);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700643 va_end(args);
644}
645
646void __libc_fatal(const char* format, ...) {
647 va_list args;
648 va_start(args, format);
Elliott Hughesc78368f2014-05-06 20:37:22 -0700649 __libc_fatal(format, args);
Elliott Hughes2e3b7102014-04-23 14:52:49 -0700650 va_end(args);
651 abort();
652}
653
Elliott Hughesc78368f2014-05-06 20:37:22 -0700654void __android_set_abort_message(const char* msg) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700655 ScopedPthreadMutexLocker locker(&g_abort_msg_lock);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700656
Elliott Hughesc78368f2014-05-06 20:37:22 -0700657 if (__abort_message_ptr == NULL) {
658 // We must have crashed _very_ early.
659 return;
660 }
661
662 if (*__abort_message_ptr != NULL) {
663 // We already have an abort message.
664 // Assume that the first crash is the one most worth reporting.
665 return;
666 }
667
Elliott Hughes0d787c12013-04-04 13:46:46 -0700668 size_t size = sizeof(abort_msg_t) + strlen(msg) + 1;
669 void* map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
670 if (map == MAP_FAILED) {
671 return;
672 }
673
Elliott Hughesc78368f2014-05-06 20:37:22 -0700674 // TODO: if we stick to the current "one-shot" scheme, we can remove this code and
675 // stop storing the size.
676 if (*__abort_message_ptr != NULL) {
677 munmap(*__abort_message_ptr, (*__abort_message_ptr)->size);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700678 }
Elliott Hughesc78368f2014-05-06 20:37:22 -0700679 abort_msg_t* new_abort_message = reinterpret_cast<abort_msg_t*>(map);
680 new_abort_message->size = size;
681 strcpy(new_abort_message->msg, msg);
682 *__abort_message_ptr = new_abort_message;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700683}