blob: 49a37628fce6dd0ccb9fdcbe02107baa552603db [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
Dan Albertce6b1ab2014-08-18 14:37:42 -070032#include <android/set_abort_message.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080033#include <assert.h>
34#include <errno.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070035#include <fcntl.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070036#include <pthread.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080037#include <stdarg.h>
38#include <stddef.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070039#include <stdlib.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080040#include <string.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070041#include <sys/mman.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080042#include <sys/socket.h>
43#include <sys/types.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080044#include <sys/uio.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080045#include <sys/un.h>
46#include <time.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070047#include <unistd.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080048
Elliott Hughes1728b232014-05-14 10:02:03 -070049static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070050
Elliott Hughes0d787c12013-04-04 13:46:46 -070051__LIBC_HIDDEN__ abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common.
David 'Digit' Turner5c734642010-01-20 12:36:51 -080052
Elliott Hughes0d787c12013-04-04 13:46:46 -070053// Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java.
54enum AndroidEventLogType {
55 EVENT_TYPE_INT = 0,
56 EVENT_TYPE_LONG = 1,
57 EVENT_TYPE_STRING = 2,
58 EVENT_TYPE_LIST = 3,
59};
60
61struct BufferOutputStream {
62 public:
63 BufferOutputStream(char* buffer, size_t size) : total(0) {
64 buffer_ = buffer;
65 end_ = buffer + size - 1;
66 pos_ = buffer_;
67 pos_[0] = '\0';
68 }
69
70 ~BufferOutputStream() {
71 }
72
73 void Send(const char* data, int len) {
74 if (len < 0) {
75 len = strlen(data);
76 }
77
Elliott Hughes416d7dd2014-08-18 17:28:32 -070078 total += len;
79
Elliott Hughes0d787c12013-04-04 13:46:46 -070080 while (len > 0) {
81 int avail = end_ - pos_;
82 if (avail == 0) {
Elliott Hughes416d7dd2014-08-18 17:28:32 -070083 return;
Elliott Hughes0d787c12013-04-04 13:46:46 -070084 }
85 if (avail > len) {
86 avail = len;
87 }
88 memcpy(pos_, data, avail);
89 pos_ += avail;
90 pos_[0] = '\0';
91 len -= avail;
David 'Digit' Turner5c734642010-01-20 12:36:51 -080092 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080093 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -080094
Elliott Hughes416d7dd2014-08-18 17:28:32 -070095 size_t total;
David 'Digit' Turner5c734642010-01-20 12:36:51 -080096
Elliott Hughes0d787c12013-04-04 13:46:46 -070097 private:
98 char* buffer_;
99 char* pos_;
100 char* end_;
101};
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800102
Elliott Hughes0d787c12013-04-04 13:46:46 -0700103struct FdOutputStream {
104 public:
105 FdOutputStream(int fd) : total(0), fd_(fd) {
106 }
107
108 void Send(const char* data, int len) {
109 if (len < 0) {
110 len = strlen(data);
111 }
112
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700113 total += len;
114
Elliott Hughes0d787c12013-04-04 13:46:46 -0700115 while (len > 0) {
116 int rc = TEMP_FAILURE_RETRY(write(fd_, data, len));
117 if (rc == -1) {
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700118 return;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700119 }
120 data += rc;
121 len -= rc;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700122 }
123 }
124
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700125 size_t total;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700126
127 private:
128 int fd_;
129};
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800130
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800131/*** formatted output implementation
132 ***/
133
134/* Parse a decimal string from 'format + *ppos',
135 * return the value, and writes the new position past
136 * the decimal string in '*ppos' on exit.
137 *
138 * NOTE: Does *not* handle a sign prefix.
139 */
Elliott Hughes0d787c12013-04-04 13:46:46 -0700140static unsigned parse_decimal(const char *format, int *ppos) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800141 const char* p = format + *ppos;
142 unsigned result = 0;
143
144 for (;;) {
145 int ch = *p;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800146 unsigned d = static_cast<unsigned>(ch - '0');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800147
Elliott Hughes0d787c12013-04-04 13:46:46 -0700148 if (d >= 10U) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800149 break;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700150 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800151
152 result = result*10 + d;
153 p++;
154 }
155 *ppos = p - format;
156 return result;
157}
158
Elliott Hugheseababde2012-12-20 18:59:05 -0800159// Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes.
160// Assumes that buf_size > 0.
Elliott Hughes41b31792013-01-28 10:36:31 -0800161static void format_unsigned(char* buf, size_t buf_size, uint64_t value, int base, bool caps) {
Elliott Hugheseababde2012-12-20 18:59:05 -0800162 char* p = buf;
163 char* end = buf + buf_size - 1;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800164
Elliott Hugheseababde2012-12-20 18:59:05 -0800165 // Generate digit string in reverse order.
166 while (value) {
167 unsigned d = value % base;
168 value /= base;
169 if (p != end) {
170 char ch;
171 if (d < 10) {
172 ch = '0' + d;
173 } else {
174 ch = (caps ? 'A' : 'a') + (d - 10);
175 }
176 *p++ = ch;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800177 }
Elliott Hugheseababde2012-12-20 18:59:05 -0800178 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800179
Elliott Hugheseababde2012-12-20 18:59:05 -0800180 // Special case for 0.
181 if (p == buf) {
182 if (p != end) {
183 *p++ = '0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800184 }
Elliott Hugheseababde2012-12-20 18:59:05 -0800185 }
186 *p = '\0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800187
Elliott Hugheseababde2012-12-20 18:59:05 -0800188 // Reverse digit string in-place.
189 size_t length = p - buf;
190 for (size_t i = 0, j = length - 1; i < j; ++i, --j) {
191 char ch = buf[i];
192 buf[i] = buf[j];
193 buf[j] = ch;
194 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800195}
196
Elliott Hughes41b31792013-01-28 10:36:31 -0800197static void format_integer(char* buf, size_t buf_size, uint64_t value, char conversion) {
198 // Decode the conversion specifier.
199 int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o');
200 int base = 10;
201 if (conversion == 'x' || conversion == 'X') {
202 base = 16;
203 } else if (conversion == 'o') {
204 base = 8;
205 }
206 bool caps = (conversion == 'X');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800207
Elliott Hughes41b31792013-01-28 10:36:31 -0800208 if (is_signed && static_cast<int64_t>(value) < 0) {
209 buf[0] = '-';
210 buf += 1;
211 buf_size -= 1;
212 value = static_cast<uint64_t>(-static_cast<int64_t>(value));
213 }
214 format_unsigned(buf, buf_size, value, base, caps);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800215}
216
Elliott Hughes0d787c12013-04-04 13:46:46 -0700217template <typename Out>
218static void SendRepeat(Out& o, char ch, int count) {
219 char pad[8];
220 memset(pad, ch, sizeof(pad));
221
222 const int pad_size = static_cast<int>(sizeof(pad));
223 while (count > 0) {
224 int avail = count;
225 if (avail > pad_size) {
226 avail = pad_size;
227 }
228 o.Send(pad, avail);
229 count -= avail;
230 }
231}
232
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800233/* Perform formatted output to an output target 'o' */
Elliott Hughes0d787c12013-04-04 13:46:46 -0700234template <typename Out>
235static void out_vformat(Out& o, const char* format, va_list args) {
Andy McFaddenec92af82011-07-29 12:46:34 -0700236 int nn = 0;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800237
238 for (;;) {
Andy McFaddenec92af82011-07-29 12:46:34 -0700239 int mm;
240 int padZero = 0;
241 int padLeft = 0;
242 char sign = '\0';
243 int width = -1;
244 int prec = -1;
245 size_t bytelen = sizeof(int);
Andy McFaddenec92af82011-07-29 12:46:34 -0700246 int slen;
247 char buffer[32]; /* temporary buffer used to format numbers */
248
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800249 char c;
250
251 /* first, find all characters that are not 0 or '%' */
252 /* then send them to the output directly */
253 mm = nn;
254 do {
255 c = format[mm];
256 if (c == '\0' || c == '%')
257 break;
258 mm++;
259 } while (1);
260
261 if (mm > nn) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700262 o.Send(format+nn, mm-nn);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800263 nn = mm;
264 }
265
266 /* is this it ? then exit */
267 if (c == '\0')
268 break;
269
270 /* nope, we are at a '%' modifier */
271 nn++; // skip it
272
273 /* parse flags */
274 for (;;) {
275 c = format[nn++];
276 if (c == '\0') { /* single trailing '%' ? */
277 c = '%';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700278 o.Send(&c, 1);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800279 return;
280 }
281 else if (c == '0') {
282 padZero = 1;
283 continue;
284 }
285 else if (c == '-') {
286 padLeft = 1;
287 continue;
288 }
289 else if (c == ' ' || c == '+') {
290 sign = c;
291 continue;
292 }
293 break;
294 }
295
296 /* parse field width */
297 if ((c >= '0' && c <= '9')) {
298 nn --;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800299 width = static_cast<int>(parse_decimal(format, &nn));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800300 c = format[nn++];
301 }
302
303 /* parse precision */
304 if (c == '.') {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800305 prec = static_cast<int>(parse_decimal(format, &nn));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800306 c = format[nn++];
307 }
308
309 /* length modifier */
310 switch (c) {
311 case 'h':
312 bytelen = sizeof(short);
313 if (format[nn] == 'h') {
314 bytelen = sizeof(char);
315 nn += 1;
316 }
317 c = format[nn++];
318 break;
319 case 'l':
320 bytelen = sizeof(long);
321 if (format[nn] == 'l') {
322 bytelen = sizeof(long long);
323 nn += 1;
324 }
325 c = format[nn++];
326 break;
327 case 'z':
328 bytelen = sizeof(size_t);
329 c = format[nn++];
330 break;
331 case 't':
332 bytelen = sizeof(ptrdiff_t);
333 c = format[nn++];
334 break;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800335 default:
336 ;
337 }
338
339 /* conversion specifier */
Elliott Hughes41b31792013-01-28 10:36:31 -0800340 const char* str = buffer;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800341 if (c == 's') {
342 /* string */
343 str = va_arg(args, const char*);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800344 if (str == NULL) {
345 str = "(null)";
346 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800347 } else if (c == 'c') {
348 /* character */
349 /* NOTE: char is promoted to int when passed through the stack */
Mark Salyzyn0336e352013-11-08 06:58:01 -0800350 buffer[0] = static_cast<char>(va_arg(args, int));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800351 buffer[1] = '\0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800352 } else if (c == 'p') {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800353 uint64_t value = reinterpret_cast<uintptr_t>(va_arg(args, void*));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800354 buffer[0] = '0';
355 buffer[1] = 'x';
Elliott Hughes41b31792013-01-28 10:36:31 -0800356 format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x');
Christopher Ferris885f3b92013-05-21 17:48:01 -0700357 } else if (c == 'd' || c == 'i' || c == 'o' || c == 'u' || c == 'x' || c == 'X') {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800358 /* integers - first read value from stack */
359 uint64_t value;
Elliott Hughes41b31792013-01-28 10:36:31 -0800360 int is_signed = (c == 'd' || c == 'i' || c == 'o');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800361
362 /* NOTE: int8_t and int16_t are promoted to int when passed
363 * through the stack
364 */
365 switch (bytelen) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800366 case 1: value = static_cast<uint8_t>(va_arg(args, int)); break;
367 case 2: value = static_cast<uint16_t>(va_arg(args, int)); break;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800368 case 4: value = va_arg(args, uint32_t); break;
369 case 8: value = va_arg(args, uint64_t); break;
370 default: return; /* should not happen */
371 }
372
373 /* sign extension, if needed */
Elliott Hughes41b31792013-01-28 10:36:31 -0800374 if (is_signed) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800375 int shift = 64 - 8*bytelen;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800376 value = static_cast<uint64_t>((static_cast<int64_t>(value << shift)) >> shift);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800377 }
378
379 /* format the number properly into our buffer */
Elliott Hughes41b31792013-01-28 10:36:31 -0800380 format_integer(buffer, sizeof(buffer), value, c);
381 } else if (c == '%') {
382 buffer[0] = '%';
383 buffer[1] = '\0';
384 } else {
385 __assert(__FILE__, __LINE__, "conversion specifier unsupported");
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800386 }
387
388 /* if we are here, 'str' points to the content that must be
389 * outputted. handle padding and alignment now */
390
391 slen = strlen(str);
392
Elliott Hughes18a206c2012-10-29 17:37:13 -0700393 if (sign != '\0' || prec != -1) {
394 __assert(__FILE__, __LINE__, "sign/precision unsupported");
395 }
396
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800397 if (slen < width && !padLeft) {
398 char padChar = padZero ? '0' : ' ';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700399 SendRepeat(o, padChar, width - slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800400 }
401
Elliott Hughes0d787c12013-04-04 13:46:46 -0700402 o.Send(str, slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800403
404 if (slen < width && padLeft) {
405 char padChar = padZero ? '0' : ' ';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700406 SendRepeat(o, padChar, width - slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800407 }
408 }
409}
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700410
Elliott Hughes0d787c12013-04-04 13:46:46 -0700411int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) {
412 BufferOutputStream os(buffer, buffer_size);
413 va_list args;
414 va_start(args, format);
415 out_vformat(os, format, args);
416 va_end(args);
417 return os.total;
418}
419
420int __libc_format_fd(int fd, const char* format, ...) {
421 FdOutputStream os(fd);
422 va_list args;
423 va_start(args, format);
424 out_vformat(os, format, args);
425 va_end(args);
426 return os.total;
427}
428
Elliott Hughes0f395b72013-10-08 13:19:00 -0700429static int __libc_write_stderr(const char* tag, const char* msg) {
430 int fd = TEMP_FAILURE_RETRY(open("/dev/stderr", O_CLOEXEC | O_WRONLY));
431 if (fd == -1) {
432 return -1;
433 }
434
435 iovec vec[4];
436 vec[0].iov_base = const_cast<char*>(tag);
437 vec[0].iov_len = strlen(tag);
438 vec[1].iov_base = const_cast<char*>(": ");
439 vec[1].iov_len = 2;
440 vec[2].iov_base = const_cast<char*>(msg);
441 vec[2].iov_len = strlen(msg) + 1;
442 vec[3].iov_base = const_cast<char*>("\n");
443 vec[3].iov_len = 1;
444
445 int result = TEMP_FAILURE_RETRY(writev(fd, vec, 4));
446 close(fd);
447 return result;
448}
449
Mark Salyzyn0336e352013-11-08 06:58:01 -0800450#ifdef TARGET_USES_LOGD
451static int __libc_open_log_socket()
452{
453 // ToDo: Ideally we want this to fail if the gid of the current
454 // process is AID_LOGD, but will have to wait until we have
455 // registered this in private/android_filesystem_config.h. We have
456 // found that all logd crashes thus far have had no problem stuffing
457 // the UNIX domain socket and moving on so not critical *today*.
458
Nick Kralevichbae5b1d2014-07-02 22:39:14 -0700459 int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
Mark Salyzyn0336e352013-11-08 06:58:01 -0800460 if (log_fd < 0) {
461 return -1;
462 }
463
464 if (fcntl(log_fd, F_SETFL, O_NONBLOCK) == -1) {
465 close(log_fd);
466 return -1;
467 }
468
469 union {
470 struct sockaddr addr;
471 struct sockaddr_un addrUn;
472 } u;
473 memset(&u, 0, sizeof(u));
474 u.addrUn.sun_family = AF_UNIX;
475 strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path));
476
477 if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) {
478 close(log_fd);
479 return -1;
480 }
481
482 return log_fd;
483}
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800484
485struct log_time { // Wire format
486 uint32_t tv_sec;
487 uint32_t tv_nsec;
488};
Mark Salyzyn0336e352013-11-08 06:58:01 -0800489#endif
490
Elliott Hughes0d787c12013-04-04 13:46:46 -0700491static int __libc_write_log(int priority, const char* tag, const char* msg) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800492#ifdef TARGET_USES_LOGD
493 int main_log_fd = __libc_open_log_socket();
494
495 if (main_log_fd == -1) {
496 // Try stderr instead.
497 return __libc_write_stderr(tag, msg);
498 }
499
Mark Salyzyn8664be52014-03-20 16:07:55 -0700500 iovec vec[6];
Elliott Hughes01110192014-05-07 16:35:59 -0700501 char log_id = (priority == ANDROID_LOG_FATAL) ? LOG_ID_CRASH : LOG_ID_MAIN;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800502 vec[0].iov_base = &log_id;
503 vec[0].iov_len = sizeof(log_id);
Mark Salyzyn8664be52014-03-20 16:07:55 -0700504 uint16_t tid = gettid();
505 vec[1].iov_base = &tid;
506 vec[1].iov_len = sizeof(tid);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800507 timespec ts;
508 clock_gettime(CLOCK_REALTIME, &ts);
509 log_time realtime_ts;
510 realtime_ts.tv_sec = ts.tv_sec;
511 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700512 vec[2].iov_base = &realtime_ts;
513 vec[2].iov_len = sizeof(realtime_ts);
Mark Salyzyn0336e352013-11-08 06:58:01 -0800514
Mark Salyzyn8664be52014-03-20 16:07:55 -0700515 vec[3].iov_base = &priority;
516 vec[3].iov_len = 1;
517 vec[4].iov_base = const_cast<char*>(tag);
518 vec[4].iov_len = strlen(tag) + 1;
519 vec[5].iov_base = const_cast<char*>(msg);
520 vec[5].iov_len = strlen(msg) + 1;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800521#else
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700522 int main_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/main", O_CLOEXEC | O_WRONLY));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700523 if (main_log_fd == -1) {
Elliott Hughes0f395b72013-10-08 13:19:00 -0700524 if (errno == ENOTDIR) {
525 // /dev/log isn't a directory? Maybe we're running on the host? Try stderr instead.
526 return __libc_write_stderr(tag, msg);
527 }
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700528 return -1;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700529 }
530
531 iovec vec[3];
532 vec[0].iov_base = &priority;
533 vec[0].iov_len = 1;
534 vec[1].iov_base = const_cast<char*>(tag);
535 vec[1].iov_len = strlen(tag) + 1;
536 vec[2].iov_base = const_cast<char*>(msg);
537 vec[2].iov_len = strlen(msg) + 1;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800538#endif
Elliott Hughes0d787c12013-04-04 13:46:46 -0700539
Mark Salyzyn0336e352013-11-08 06:58:01 -0800540 int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, sizeof(vec) / sizeof(vec[0])));
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700541 close(main_log_fd);
542 return result;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700543}
544
545int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) {
546 char buffer[1024];
547 BufferOutputStream os(buffer, sizeof(buffer));
548 out_vformat(os, format, args);
549 return __libc_write_log(priority, tag, buffer);
550}
551
552int __libc_format_log(int priority, const char* tag, const char* format, ...) {
553 va_list args;
554 va_start(args, format);
555 int result = __libc_format_log_va_list(priority, tag, format, args);
556 va_end(args);
557 return result;
558}
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700559
560static int __libc_android_log_event(int32_t tag, char type, const void* payload, size_t len) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800561#ifdef TARGET_USES_LOGD
Mark Salyzyn8664be52014-03-20 16:07:55 -0700562 iovec vec[6];
Mark Salyzyn0336e352013-11-08 06:58:01 -0800563 char log_id = LOG_ID_EVENTS;
564 vec[0].iov_base = &log_id;
565 vec[0].iov_len = sizeof(log_id);
Mark Salyzyn8664be52014-03-20 16:07:55 -0700566 uint16_t tid = gettid();
567 vec[1].iov_base = &tid;
568 vec[1].iov_len = sizeof(tid);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800569 timespec ts;
570 clock_gettime(CLOCK_REALTIME, &ts);
571 log_time realtime_ts;
572 realtime_ts.tv_sec = ts.tv_sec;
573 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700574 vec[2].iov_base = &realtime_ts;
575 vec[2].iov_len = sizeof(realtime_ts);
Mark Salyzyn0336e352013-11-08 06:58:01 -0800576
Mark Salyzyn8664be52014-03-20 16:07:55 -0700577 vec[3].iov_base = &tag;
578 vec[3].iov_len = sizeof(tag);
579 vec[4].iov_base = &type;
580 vec[4].iov_len = sizeof(type);
581 vec[5].iov_base = const_cast<void*>(payload);
582 vec[5].iov_len = len;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800583
584 int event_log_fd = __libc_open_log_socket();
585#else
Elliott Hughes0d787c12013-04-04 13:46:46 -0700586 iovec vec[3];
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700587 vec[0].iov_base = &tag;
588 vec[0].iov_len = sizeof(tag);
589 vec[1].iov_base = &type;
590 vec[1].iov_len = sizeof(type);
591 vec[2].iov_base = const_cast<void*>(payload);
592 vec[2].iov_len = len;
593
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700594 int event_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/events", O_CLOEXEC | O_WRONLY));
Mark Salyzyn0336e352013-11-08 06:58:01 -0800595#endif
596
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700597 if (event_log_fd == -1) {
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700598 return -1;
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700599 }
Mark Salyzyn0336e352013-11-08 06:58:01 -0800600 int result = TEMP_FAILURE_RETRY(writev(event_log_fd, vec, sizeof(vec) / sizeof(vec[0])));
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700601 close(event_log_fd);
602 return result;
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700603}
604
605void __libc_android_log_event_int(int32_t tag, int value) {
606 __libc_android_log_event(tag, EVENT_TYPE_INT, &value, sizeof(value));
607}
608
609void __libc_android_log_event_uid(int32_t tag) {
610 __libc_android_log_event_int(tag, getuid());
611}
612
Elliott Hughesd1eda332013-10-15 16:43:38 -0700613void __fortify_chk_fail(const char* msg, uint32_t tag) {
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700614 if (tag != 0) {
615 __libc_android_log_event_uid(tag);
616 }
Elliott Hughes0d787c12013-04-04 13:46:46 -0700617 __libc_fatal("FORTIFY_SOURCE: %s. Calling abort().", msg);
618}
619
Elliott Hughesc78368f2014-05-06 20:37:22 -0700620static void __libc_fatal(const char* format, va_list args) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700621 char msg[1024];
622 BufferOutputStream os(msg, sizeof(msg));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700623 out_vformat(os, format, args);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700624
Dmitriy Ivanov12bf3bc2014-07-01 14:24:45 -0700625 // log to stderr for the benefit of "adb shell" users.
Dan Albert97e31de2014-07-20 11:49:46 -0700626 struct iovec iov[2] = {
627 {msg, strlen(msg)},
628 {const_cast<void*>(static_cast<const void*>("\n")), 1},
629 };
630 writev(2, iov, 2);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700631
632 // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed).
Elliott Hughesc78368f2014-05-06 20:37:22 -0700633 __libc_write_log(ANDROID_LOG_FATAL, "libc", msg);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700634
Dan Albertce6b1ab2014-08-18 14:37:42 -0700635 android_set_abort_message(msg);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700636}
Elliott Hughes0d787c12013-04-04 13:46:46 -0700637
Elliott Hughes61e699a2013-06-12 14:05:46 -0700638void __libc_fatal_no_abort(const char* format, ...) {
639 va_list args;
640 va_start(args, format);
Elliott Hughesc78368f2014-05-06 20:37:22 -0700641 __libc_fatal(format, args);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700642 va_end(args);
643}
644
645void __libc_fatal(const char* format, ...) {
646 va_list args;
647 va_start(args, format);
Elliott Hughesc78368f2014-05-06 20:37:22 -0700648 __libc_fatal(format, args);
Elliott Hughes2e3b7102014-04-23 14:52:49 -0700649 va_end(args);
650 abort();
651}
652
Dan Albertce6b1ab2014-08-18 14:37:42 -0700653void android_set_abort_message(const char* msg) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700654 ScopedPthreadMutexLocker locker(&g_abort_msg_lock);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700655
Elliott Hughesc78368f2014-05-06 20:37:22 -0700656 if (__abort_message_ptr == NULL) {
657 // We must have crashed _very_ early.
658 return;
659 }
660
661 if (*__abort_message_ptr != NULL) {
662 // We already have an abort message.
663 // Assume that the first crash is the one most worth reporting.
664 return;
665 }
666
Elliott Hughes0d787c12013-04-04 13:46:46 -0700667 size_t size = sizeof(abort_msg_t) + strlen(msg) + 1;
668 void* map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
669 if (map == MAP_FAILED) {
670 return;
671 }
672
Elliott Hughesc78368f2014-05-06 20:37:22 -0700673 // TODO: if we stick to the current "one-shot" scheme, we can remove this code and
674 // stop storing the size.
675 if (*__abort_message_ptr != NULL) {
676 munmap(*__abort_message_ptr, (*__abort_message_ptr)->size);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700677 }
Elliott Hughesc78368f2014-05-06 20:37:22 -0700678 abort_msg_t* new_abort_message = reinterpret_cast<abort_msg_t*>(map);
679 new_abort_message->size = size;
680 strcpy(new_abort_message->msg, msg);
681 *__abort_message_ptr = new_abort_message;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700682}