blob: 54664d9fbdfd844c09a61fcdb5a9e8f2efad2096 [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 Hughes87ff8242017-04-11 15:18:38 -070029// Relative paths so we can #include this .cpp file for testing.
30#include "../private/CachedProperty.h"
31#include "../private/libc_logging.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070032#include "../private/ScopedPthreadMutexLocker.h"
Elliott Hugheseababde2012-12-20 18:59:05 -080033
Dan Albertce6b1ab2014-08-18 14:37:42 -070034#include <android/set_abort_message.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080035#include <assert.h>
Mark Salyzyn870f1652015-11-30 16:23:15 -080036#include <ctype.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080037#include <errno.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070038#include <fcntl.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070039#include <pthread.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080040#include <stdarg.h>
41#include <stddef.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070042#include <stdlib.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080043#include <string.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070044#include <sys/mman.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080045#include <sys/socket.h>
46#include <sys/types.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080047#include <sys/uio.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080048#include <sys/un.h>
49#include <time.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070050#include <unistd.h>
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,
Jeff Brown11331f62015-04-28 14:35:45 -070058 EVENT_TYPE_FLOAT = 4,
Elliott Hughes0d787c12013-04-04 13:46:46 -070059};
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:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700105 explicit FdOutputStream(int fd) : total(0), fd_(fd) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700106 }
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
Josh Gao273991c2017-02-15 11:46:55 -0800420int __libc_format_buffer_va_list(char* buffer, size_t buffer_size, const char* format,
421 va_list args) {
422 BufferOutputStream os(buffer, buffer_size);
423 out_vformat(os, format, args);
424 return os.total;
425}
426
Elliott Hughes0d787c12013-04-04 13:46:46 -0700427int __libc_format_fd(int fd, const char* format, ...) {
428 FdOutputStream os(fd);
429 va_list args;
430 va_start(args, format);
431 out_vformat(os, format, args);
432 va_end(args);
433 return os.total;
434}
435
Elliott Hughes0f395b72013-10-08 13:19:00 -0700436static int __libc_write_stderr(const char* tag, const char* msg) {
Elliott Hughes0f395b72013-10-08 13:19:00 -0700437 iovec vec[4];
438 vec[0].iov_base = const_cast<char*>(tag);
439 vec[0].iov_len = strlen(tag);
440 vec[1].iov_base = const_cast<char*>(": ");
441 vec[1].iov_len = 2;
442 vec[2].iov_base = const_cast<char*>(msg);
Elliott Hughes42084a22015-02-02 12:24:46 -0800443 vec[2].iov_len = strlen(msg);
Elliott Hughes0f395b72013-10-08 13:19:00 -0700444 vec[3].iov_base = const_cast<char*>("\n");
445 vec[3].iov_len = 1;
446
Josh Gao59bde2e2016-10-07 13:21:03 -0700447 int result = TEMP_FAILURE_RETRY(writev(STDERR_FILENO, vec, 4));
Elliott Hughes0f395b72013-10-08 13:19:00 -0700448 return result;
449}
450
Elliott Hughes42084a22015-02-02 12:24:46 -0800451static int __libc_open_log_socket() {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800452 // ToDo: Ideally we want this to fail if the gid of the current
453 // process is AID_LOGD, but will have to wait until we have
454 // registered this in private/android_filesystem_config.h. We have
455 // found that all logd crashes thus far have had no problem stuffing
456 // the UNIX domain socket and moving on so not critical *today*.
457
Elliott Hughes0f67d5f2016-02-27 19:18:41 -0800458 int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
459 if (log_fd == -1) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800460 return -1;
461 }
462
463 union {
464 struct sockaddr addr;
465 struct sockaddr_un addrUn;
466 } u;
467 memset(&u, 0, sizeof(u));
468 u.addrUn.sun_family = AF_UNIX;
469 strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path));
470
471 if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) {
472 close(log_fd);
473 return -1;
474 }
475
476 return log_fd;
477}
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800478
Elliott Hughes87ff8242017-04-11 15:18:38 -0700479static clockid_t __android_log_clockid() {
480 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
481 ScopedPthreadMutexLocker locker(&mutex);
Mark Salyzyn870f1652015-11-30 16:23:15 -0800482
Elliott Hughes87ff8242017-04-11 15:18:38 -0700483 static CachedProperty ro_logd_timestamp("ro.logd.timestamp");
484 static CachedProperty persist_logd_timestamp("persist.logd.timestamp");
Mark Salyzyn870f1652015-11-30 16:23:15 -0800485
Elliott Hughes87ff8242017-04-11 15:18:38 -0700486 char ch = persist_logd_timestamp.Get()[0];
487 if (ch == '\0') ch = ro_logd_timestamp.Get()[0];
Mark Salyzyn870f1652015-11-30 16:23:15 -0800488
Elliott Hughes87ff8242017-04-11 15:18:38 -0700489 return (tolower(ch) == 'm') ? CLOCK_MONOTONIC : CLOCK_REALTIME;
Mark Salyzyn870f1652015-11-30 16:23:15 -0800490}
491
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800492struct log_time { // Wire format
493 uint32_t tv_sec;
494 uint32_t tv_nsec;
495};
Mark Salyzyn0336e352013-11-08 06:58:01 -0800496
Colin Cross2c759912016-02-05 16:17:39 -0800497int __libc_write_log(int priority, const char* tag, const char* msg) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800498 int main_log_fd = __libc_open_log_socket();
Mark Salyzyn0336e352013-11-08 06:58:01 -0800499 if (main_log_fd == -1) {
500 // Try stderr instead.
501 return __libc_write_stderr(tag, msg);
502 }
503
Mark Salyzyn8664be52014-03-20 16:07:55 -0700504 iovec vec[6];
Elliott Hughes01110192014-05-07 16:35:59 -0700505 char log_id = (priority == ANDROID_LOG_FATAL) ? LOG_ID_CRASH : LOG_ID_MAIN;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800506 vec[0].iov_base = &log_id;
507 vec[0].iov_len = sizeof(log_id);
Mark Salyzyn8664be52014-03-20 16:07:55 -0700508 uint16_t tid = gettid();
509 vec[1].iov_base = &tid;
510 vec[1].iov_len = sizeof(tid);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800511 timespec ts;
Mark Salyzyn9da687e2015-12-08 13:42:41 -0800512 clock_gettime(__android_log_clockid(), &ts);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800513 log_time realtime_ts;
514 realtime_ts.tv_sec = ts.tv_sec;
515 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700516 vec[2].iov_base = &realtime_ts;
517 vec[2].iov_len = sizeof(realtime_ts);
Mark Salyzyn0336e352013-11-08 06:58:01 -0800518
Mark Salyzyn8664be52014-03-20 16:07:55 -0700519 vec[3].iov_base = &priority;
520 vec[3].iov_len = 1;
521 vec[4].iov_base = const_cast<char*>(tag);
Elliott Hughesaba6f712015-02-05 12:02:04 -0800522 vec[4].iov_len = strlen(tag) + 1;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700523 vec[5].iov_base = const_cast<char*>(msg);
Elliott Hughesaba6f712015-02-05 12:02:04 -0800524 vec[5].iov_len = strlen(msg) + 1;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700525
Mark Salyzyn0336e352013-11-08 06:58:01 -0800526 int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, sizeof(vec) / sizeof(vec[0])));
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700527 close(main_log_fd);
528 return result;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700529}
530
531int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) {
532 char buffer[1024];
533 BufferOutputStream os(buffer, sizeof(buffer));
534 out_vformat(os, format, args);
535 return __libc_write_log(priority, tag, buffer);
536}
537
538int __libc_format_log(int priority, const char* tag, const char* format, ...) {
539 va_list args;
540 va_start(args, format);
541 int result = __libc_format_log_va_list(priority, tag, format, args);
542 va_end(args);
543 return result;
544}
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700545
Elliott Hughesb83d6742016-02-25 20:33:47 -0800546static void __libc_fatal_va_list(const char* prefix, const char* format, va_list args) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700547 char msg[1024];
548 BufferOutputStream os(msg, sizeof(msg));
Elliott Hughesb83d6742016-02-25 20:33:47 -0800549
550 if (prefix) {
551 os.Send(prefix, strlen(prefix));
552 os.Send(": ", 2);
553 }
554
Elliott Hughes0d787c12013-04-04 13:46:46 -0700555 out_vformat(os, format, args);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700556
Elliott Hughes5e6cf052017-02-06 10:26:20 -0800557 // Log to stderr for the benefit of "adb shell" users and gtests.
Dan Albert97e31de2014-07-20 11:49:46 -0700558 struct iovec iov[2] = {
Elliott Hughes42084a22015-02-02 12:24:46 -0800559 { msg, os.total },
560 { const_cast<char*>("\n"), 1 },
Dan Albert97e31de2014-07-20 11:49:46 -0700561 };
Elliott Hughes42084a22015-02-02 12:24:46 -0800562 TEMP_FAILURE_RETRY(writev(2, iov, 2));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700563
564 // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed).
Elliott Hughesc78368f2014-05-06 20:37:22 -0700565 __libc_write_log(ANDROID_LOG_FATAL, "libc", msg);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700566
Dan Albertce6b1ab2014-08-18 14:37:42 -0700567 android_set_abort_message(msg);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700568}
Elliott Hughes0d787c12013-04-04 13:46:46 -0700569
Elliott Hughesb83d6742016-02-25 20:33:47 -0800570void __libc_fatal(const char* fmt, ...) {
Elliott Hughes61e699a2013-06-12 14:05:46 -0700571 va_list args;
Elliott Hughesb83d6742016-02-25 20:33:47 -0800572 va_start(args, fmt);
573 __libc_fatal_va_list(nullptr, fmt, args);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700574 va_end(args);
Elliott Hughesb83d6742016-02-25 20:33:47 -0800575 abort();
Elliott Hughes61e699a2013-06-12 14:05:46 -0700576}
577
Elliott Hughesb83d6742016-02-25 20:33:47 -0800578void __fortify_fatal(const char* fmt, ...) {
Elliott Hughes61e699a2013-06-12 14:05:46 -0700579 va_list args;
Elliott Hughesb83d6742016-02-25 20:33:47 -0800580 va_start(args, fmt);
581 __libc_fatal_va_list("FORTIFY", fmt, args);
Elliott Hughes2e3b7102014-04-23 14:52:49 -0700582 va_end(args);
583 abort();
584}