blob: 517d047ff21cf5abf9b8224995aa2987d71fa2fb [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>
Mark Salyzyn870f1652015-11-30 16:23:15 -080034#include <ctype.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080035#include <errno.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070036#include <fcntl.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070037#include <pthread.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080038#include <stdarg.h>
39#include <stddef.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070040#include <stdlib.h>
Elliott Hugheseababde2012-12-20 18:59:05 -080041#include <string.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070042#include <sys/mman.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080043#include <sys/socket.h>
44#include <sys/types.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080045#include <sys/uio.h>
Mark Salyzyn0336e352013-11-08 06:58:01 -080046#include <sys/un.h>
47#include <time.h>
Elliott Hughes0d787c12013-04-04 13:46:46 -070048#include <unistd.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080049
Mark Salyzyn870f1652015-11-30 16:23:15 -080050#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
51#include <sys/_system_properties.h>
52
Elliott Hughes1728b232014-05-14 10:02:03 -070053static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070054
Elliott Hughes0d787c12013-04-04 13:46:46 -070055__LIBC_HIDDEN__ abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common.
David 'Digit' Turner5c734642010-01-20 12:36:51 -080056
Elliott Hughes0d787c12013-04-04 13:46:46 -070057// Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java.
58enum AndroidEventLogType {
59 EVENT_TYPE_INT = 0,
60 EVENT_TYPE_LONG = 1,
61 EVENT_TYPE_STRING = 2,
62 EVENT_TYPE_LIST = 3,
Jeff Brown11331f62015-04-28 14:35:45 -070063 EVENT_TYPE_FLOAT = 4,
Elliott Hughes0d787c12013-04-04 13:46:46 -070064};
65
66struct BufferOutputStream {
67 public:
68 BufferOutputStream(char* buffer, size_t size) : total(0) {
69 buffer_ = buffer;
70 end_ = buffer + size - 1;
71 pos_ = buffer_;
72 pos_[0] = '\0';
73 }
74
75 ~BufferOutputStream() {
76 }
77
78 void Send(const char* data, int len) {
79 if (len < 0) {
80 len = strlen(data);
81 }
82
Elliott Hughes416d7dd2014-08-18 17:28:32 -070083 total += len;
84
Elliott Hughes0d787c12013-04-04 13:46:46 -070085 while (len > 0) {
86 int avail = end_ - pos_;
87 if (avail == 0) {
Elliott Hughes416d7dd2014-08-18 17:28:32 -070088 return;
Elliott Hughes0d787c12013-04-04 13:46:46 -070089 }
90 if (avail > len) {
91 avail = len;
92 }
93 memcpy(pos_, data, avail);
94 pos_ += avail;
95 pos_[0] = '\0';
96 len -= avail;
David 'Digit' Turner5c734642010-01-20 12:36:51 -080097 }
Elliott Hughes1e980b62013-01-17 18:36:06 -080098 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -080099
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700100 size_t total;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800101
Elliott Hughes0d787c12013-04-04 13:46:46 -0700102 private:
103 char* buffer_;
104 char* pos_;
105 char* end_;
106};
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800107
Elliott Hughes0d787c12013-04-04 13:46:46 -0700108struct FdOutputStream {
109 public:
Chih-Hung Hsieh62e3a072016-05-03 12:08:05 -0700110 explicit FdOutputStream(int fd) : total(0), fd_(fd) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700111 }
112
113 void Send(const char* data, int len) {
114 if (len < 0) {
115 len = strlen(data);
116 }
117
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700118 total += len;
119
Elliott Hughes0d787c12013-04-04 13:46:46 -0700120 while (len > 0) {
121 int rc = TEMP_FAILURE_RETRY(write(fd_, data, len));
122 if (rc == -1) {
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700123 return;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700124 }
125 data += rc;
126 len -= rc;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700127 }
128 }
129
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700130 size_t total;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700131
132 private:
133 int fd_;
134};
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800135
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800136/*** formatted output implementation
137 ***/
138
139/* Parse a decimal string from 'format + *ppos',
140 * return the value, and writes the new position past
141 * the decimal string in '*ppos' on exit.
142 *
143 * NOTE: Does *not* handle a sign prefix.
144 */
Elliott Hughes0d787c12013-04-04 13:46:46 -0700145static unsigned parse_decimal(const char *format, int *ppos) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800146 const char* p = format + *ppos;
147 unsigned result = 0;
148
149 for (;;) {
150 int ch = *p;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800151 unsigned d = static_cast<unsigned>(ch - '0');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800152
Elliott Hughes0d787c12013-04-04 13:46:46 -0700153 if (d >= 10U) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800154 break;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700155 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800156
157 result = result*10 + d;
158 p++;
159 }
160 *ppos = p - format;
161 return result;
162}
163
Elliott Hugheseababde2012-12-20 18:59:05 -0800164// Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes.
165// Assumes that buf_size > 0.
Elliott Hughes41b31792013-01-28 10:36:31 -0800166static void format_unsigned(char* buf, size_t buf_size, uint64_t value, int base, bool caps) {
Elliott Hugheseababde2012-12-20 18:59:05 -0800167 char* p = buf;
168 char* end = buf + buf_size - 1;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800169
Elliott Hugheseababde2012-12-20 18:59:05 -0800170 // Generate digit string in reverse order.
171 while (value) {
172 unsigned d = value % base;
173 value /= base;
174 if (p != end) {
175 char ch;
176 if (d < 10) {
177 ch = '0' + d;
178 } else {
179 ch = (caps ? 'A' : 'a') + (d - 10);
180 }
181 *p++ = ch;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800182 }
Elliott Hugheseababde2012-12-20 18:59:05 -0800183 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800184
Elliott Hugheseababde2012-12-20 18:59:05 -0800185 // Special case for 0.
186 if (p == buf) {
187 if (p != end) {
188 *p++ = '0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800189 }
Elliott Hugheseababde2012-12-20 18:59:05 -0800190 }
191 *p = '\0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800192
Elliott Hugheseababde2012-12-20 18:59:05 -0800193 // Reverse digit string in-place.
194 size_t length = p - buf;
195 for (size_t i = 0, j = length - 1; i < j; ++i, --j) {
196 char ch = buf[i];
197 buf[i] = buf[j];
198 buf[j] = ch;
199 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800200}
201
Elliott Hughes41b31792013-01-28 10:36:31 -0800202static void format_integer(char* buf, size_t buf_size, uint64_t value, char conversion) {
203 // Decode the conversion specifier.
204 int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o');
205 int base = 10;
206 if (conversion == 'x' || conversion == 'X') {
207 base = 16;
208 } else if (conversion == 'o') {
209 base = 8;
210 }
211 bool caps = (conversion == 'X');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800212
Elliott Hughes41b31792013-01-28 10:36:31 -0800213 if (is_signed && static_cast<int64_t>(value) < 0) {
214 buf[0] = '-';
215 buf += 1;
216 buf_size -= 1;
217 value = static_cast<uint64_t>(-static_cast<int64_t>(value));
218 }
219 format_unsigned(buf, buf_size, value, base, caps);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800220}
221
Elliott Hughes0d787c12013-04-04 13:46:46 -0700222template <typename Out>
223static void SendRepeat(Out& o, char ch, int count) {
224 char pad[8];
225 memset(pad, ch, sizeof(pad));
226
227 const int pad_size = static_cast<int>(sizeof(pad));
228 while (count > 0) {
229 int avail = count;
230 if (avail > pad_size) {
231 avail = pad_size;
232 }
233 o.Send(pad, avail);
234 count -= avail;
235 }
236}
237
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800238/* Perform formatted output to an output target 'o' */
Elliott Hughes0d787c12013-04-04 13:46:46 -0700239template <typename Out>
240static void out_vformat(Out& o, const char* format, va_list args) {
Andy McFaddenec92af82011-07-29 12:46:34 -0700241 int nn = 0;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800242
243 for (;;) {
Andy McFaddenec92af82011-07-29 12:46:34 -0700244 int mm;
245 int padZero = 0;
246 int padLeft = 0;
247 char sign = '\0';
248 int width = -1;
249 int prec = -1;
250 size_t bytelen = sizeof(int);
Andy McFaddenec92af82011-07-29 12:46:34 -0700251 int slen;
252 char buffer[32]; /* temporary buffer used to format numbers */
253
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800254 char c;
255
256 /* first, find all characters that are not 0 or '%' */
257 /* then send them to the output directly */
258 mm = nn;
259 do {
260 c = format[mm];
261 if (c == '\0' || c == '%')
262 break;
263 mm++;
264 } while (1);
265
266 if (mm > nn) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700267 o.Send(format+nn, mm-nn);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800268 nn = mm;
269 }
270
271 /* is this it ? then exit */
272 if (c == '\0')
273 break;
274
275 /* nope, we are at a '%' modifier */
276 nn++; // skip it
277
278 /* parse flags */
279 for (;;) {
280 c = format[nn++];
281 if (c == '\0') { /* single trailing '%' ? */
282 c = '%';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700283 o.Send(&c, 1);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800284 return;
285 }
286 else if (c == '0') {
287 padZero = 1;
288 continue;
289 }
290 else if (c == '-') {
291 padLeft = 1;
292 continue;
293 }
294 else if (c == ' ' || c == '+') {
295 sign = c;
296 continue;
297 }
298 break;
299 }
300
301 /* parse field width */
302 if ((c >= '0' && c <= '9')) {
303 nn --;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800304 width = static_cast<int>(parse_decimal(format, &nn));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800305 c = format[nn++];
306 }
307
308 /* parse precision */
309 if (c == '.') {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800310 prec = static_cast<int>(parse_decimal(format, &nn));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800311 c = format[nn++];
312 }
313
314 /* length modifier */
315 switch (c) {
316 case 'h':
317 bytelen = sizeof(short);
318 if (format[nn] == 'h') {
319 bytelen = sizeof(char);
320 nn += 1;
321 }
322 c = format[nn++];
323 break;
324 case 'l':
325 bytelen = sizeof(long);
326 if (format[nn] == 'l') {
327 bytelen = sizeof(long long);
328 nn += 1;
329 }
330 c = format[nn++];
331 break;
332 case 'z':
333 bytelen = sizeof(size_t);
334 c = format[nn++];
335 break;
336 case 't':
337 bytelen = sizeof(ptrdiff_t);
338 c = format[nn++];
339 break;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800340 default:
341 ;
342 }
343
344 /* conversion specifier */
Elliott Hughes41b31792013-01-28 10:36:31 -0800345 const char* str = buffer;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800346 if (c == 's') {
347 /* string */
348 str = va_arg(args, const char*);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800349 if (str == NULL) {
350 str = "(null)";
351 }
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800352 } else if (c == 'c') {
353 /* character */
354 /* NOTE: char is promoted to int when passed through the stack */
Mark Salyzyn0336e352013-11-08 06:58:01 -0800355 buffer[0] = static_cast<char>(va_arg(args, int));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800356 buffer[1] = '\0';
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800357 } else if (c == 'p') {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800358 uint64_t value = reinterpret_cast<uintptr_t>(va_arg(args, void*));
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800359 buffer[0] = '0';
360 buffer[1] = 'x';
Elliott Hughes41b31792013-01-28 10:36:31 -0800361 format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x');
Christopher Ferris885f3b92013-05-21 17:48:01 -0700362 } else if (c == 'd' || c == 'i' || c == 'o' || c == 'u' || c == 'x' || c == 'X') {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800363 /* integers - first read value from stack */
364 uint64_t value;
Elliott Hughes41b31792013-01-28 10:36:31 -0800365 int is_signed = (c == 'd' || c == 'i' || c == 'o');
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800366
367 /* NOTE: int8_t and int16_t are promoted to int when passed
368 * through the stack
369 */
370 switch (bytelen) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800371 case 1: value = static_cast<uint8_t>(va_arg(args, int)); break;
372 case 2: value = static_cast<uint16_t>(va_arg(args, int)); break;
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800373 case 4: value = va_arg(args, uint32_t); break;
374 case 8: value = va_arg(args, uint64_t); break;
375 default: return; /* should not happen */
376 }
377
378 /* sign extension, if needed */
Elliott Hughes41b31792013-01-28 10:36:31 -0800379 if (is_signed) {
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800380 int shift = 64 - 8*bytelen;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800381 value = static_cast<uint64_t>((static_cast<int64_t>(value << shift)) >> shift);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800382 }
383
384 /* format the number properly into our buffer */
Elliott Hughes41b31792013-01-28 10:36:31 -0800385 format_integer(buffer, sizeof(buffer), value, c);
386 } else if (c == '%') {
387 buffer[0] = '%';
388 buffer[1] = '\0';
389 } else {
390 __assert(__FILE__, __LINE__, "conversion specifier unsupported");
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800391 }
392
393 /* if we are here, 'str' points to the content that must be
394 * outputted. handle padding and alignment now */
395
396 slen = strlen(str);
397
Elliott Hughes18a206c2012-10-29 17:37:13 -0700398 if (sign != '\0' || prec != -1) {
399 __assert(__FILE__, __LINE__, "sign/precision unsupported");
400 }
401
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800402 if (slen < width && !padLeft) {
403 char padChar = padZero ? '0' : ' ';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700404 SendRepeat(o, padChar, width - slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800405 }
406
Elliott Hughes0d787c12013-04-04 13:46:46 -0700407 o.Send(str, slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800408
409 if (slen < width && padLeft) {
410 char padChar = padZero ? '0' : ' ';
Elliott Hughes0d787c12013-04-04 13:46:46 -0700411 SendRepeat(o, padChar, width - slen);
David 'Digit' Turner5c734642010-01-20 12:36:51 -0800412 }
413 }
414}
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700415
Elliott Hughes0d787c12013-04-04 13:46:46 -0700416int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) {
417 BufferOutputStream os(buffer, buffer_size);
418 va_list args;
419 va_start(args, format);
420 out_vformat(os, format, args);
421 va_end(args);
422 return os.total;
423}
424
425int __libc_format_fd(int fd, const char* format, ...) {
426 FdOutputStream os(fd);
427 va_list args;
428 va_start(args, format);
429 out_vformat(os, format, args);
430 va_end(args);
431 return os.total;
432}
433
Elliott Hughes0f395b72013-10-08 13:19:00 -0700434static int __libc_write_stderr(const char* tag, const char* msg) {
Yabin Cui28e69f72015-03-25 12:36:18 -0700435 int fd = TEMP_FAILURE_RETRY(open("/dev/stderr", O_CLOEXEC | O_WRONLY | O_APPEND));
Elliott Hughes0f395b72013-10-08 13:19:00 -0700436 if (fd == -1) {
437 return -1;
438 }
439
440 iovec vec[4];
441 vec[0].iov_base = const_cast<char*>(tag);
442 vec[0].iov_len = strlen(tag);
443 vec[1].iov_base = const_cast<char*>(": ");
444 vec[1].iov_len = 2;
445 vec[2].iov_base = const_cast<char*>(msg);
Elliott Hughes42084a22015-02-02 12:24:46 -0800446 vec[2].iov_len = strlen(msg);
Elliott Hughes0f395b72013-10-08 13:19:00 -0700447 vec[3].iov_base = const_cast<char*>("\n");
448 vec[3].iov_len = 1;
449
450 int result = TEMP_FAILURE_RETRY(writev(fd, vec, 4));
451 close(fd);
452 return result;
453}
454
Elliott Hughes42084a22015-02-02 12:24:46 -0800455static int __libc_open_log_socket() {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800456 // ToDo: Ideally we want this to fail if the gid of the current
457 // process is AID_LOGD, but will have to wait until we have
458 // registered this in private/android_filesystem_config.h. We have
459 // found that all logd crashes thus far have had no problem stuffing
460 // the UNIX domain socket and moving on so not critical *today*.
461
Elliott Hughes0f67d5f2016-02-27 19:18:41 -0800462 int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0));
463 if (log_fd == -1) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800464 return -1;
465 }
466
467 union {
468 struct sockaddr addr;
469 struct sockaddr_un addrUn;
470 } u;
471 memset(&u, 0, sizeof(u));
472 u.addrUn.sun_family = AF_UNIX;
473 strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path));
474
475 if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) {
476 close(log_fd);
477 return -1;
478 }
479
480 return log_fd;
481}
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800482
Mark Salyzyn870f1652015-11-30 16:23:15 -0800483struct cache {
484 const prop_info* pinfo;
485 uint32_t serial;
486 char c;
487};
488
489static void refresh_cache(struct cache *cache, const char *key)
490{
491 if (!cache->pinfo) {
492 cache->pinfo = __system_property_find(key);
493 if (!cache->pinfo) {
494 return;
495 }
496 }
497 uint32_t serial = __system_property_serial(cache->pinfo);
498 if (serial == cache->serial) {
499 return;
500 }
501 cache->serial = serial;
502
503 char buf[PROP_VALUE_MAX];
504 __system_property_read(cache->pinfo, 0, buf);
505 cache->c = buf[0];
506}
507
508// Timestamp state generally remains constant, since a change is
509// rare, we can accept a trylock failure gracefully.
510static pthread_mutex_t lock_clockid = PTHREAD_MUTEX_INITIALIZER;
511
Mark Salyzyn9da687e2015-12-08 13:42:41 -0800512static clockid_t __android_log_clockid()
Mark Salyzyn870f1652015-11-30 16:23:15 -0800513{
514 static struct cache r_time_cache = { NULL, static_cast<uint32_t>(-1), 0 };
515 static struct cache p_time_cache = { NULL, static_cast<uint32_t>(-1), 0 };
516 char c;
517
518 if (pthread_mutex_trylock(&lock_clockid)) {
519 // We are willing to accept some race in this context
520 if (!(c = p_time_cache.c)) {
521 c = r_time_cache.c;
522 }
523 } else {
524 static uint32_t serial;
525 uint32_t current_serial = __system_property_area_serial();
526 if (current_serial != serial) {
527 refresh_cache(&r_time_cache, "ro.logd.timestamp");
528 refresh_cache(&p_time_cache, "persist.logd.timestamp");
529 serial = current_serial;
530 }
531 if (!(c = p_time_cache.c)) {
532 c = r_time_cache.c;
533 }
534
535 pthread_mutex_unlock(&lock_clockid);
536 }
537
538 return (tolower(c) == 'm') ? CLOCK_MONOTONIC : CLOCK_REALTIME;
539}
540
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800541struct log_time { // Wire format
542 uint32_t tv_sec;
543 uint32_t tv_nsec;
544};
Mark Salyzyn0336e352013-11-08 06:58:01 -0800545
Colin Cross2c759912016-02-05 16:17:39 -0800546int __libc_write_log(int priority, const char* tag, const char* msg) {
Mark Salyzyn0336e352013-11-08 06:58:01 -0800547 int main_log_fd = __libc_open_log_socket();
Mark Salyzyn0336e352013-11-08 06:58:01 -0800548 if (main_log_fd == -1) {
549 // Try stderr instead.
550 return __libc_write_stderr(tag, msg);
551 }
552
Mark Salyzyn8664be52014-03-20 16:07:55 -0700553 iovec vec[6];
Elliott Hughes01110192014-05-07 16:35:59 -0700554 char log_id = (priority == ANDROID_LOG_FATAL) ? LOG_ID_CRASH : LOG_ID_MAIN;
Mark Salyzyn0336e352013-11-08 06:58:01 -0800555 vec[0].iov_base = &log_id;
556 vec[0].iov_len = sizeof(log_id);
Mark Salyzyn8664be52014-03-20 16:07:55 -0700557 uint16_t tid = gettid();
558 vec[1].iov_base = &tid;
559 vec[1].iov_len = sizeof(tid);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800560 timespec ts;
Mark Salyzyn9da687e2015-12-08 13:42:41 -0800561 clock_gettime(__android_log_clockid(), &ts);
Mark Salyzyn9fc76022014-03-05 13:44:00 -0800562 log_time realtime_ts;
563 realtime_ts.tv_sec = ts.tv_sec;
564 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700565 vec[2].iov_base = &realtime_ts;
566 vec[2].iov_len = sizeof(realtime_ts);
Mark Salyzyn0336e352013-11-08 06:58:01 -0800567
Mark Salyzyn8664be52014-03-20 16:07:55 -0700568 vec[3].iov_base = &priority;
569 vec[3].iov_len = 1;
570 vec[4].iov_base = const_cast<char*>(tag);
Elliott Hughesaba6f712015-02-05 12:02:04 -0800571 vec[4].iov_len = strlen(tag) + 1;
Mark Salyzyn8664be52014-03-20 16:07:55 -0700572 vec[5].iov_base = const_cast<char*>(msg);
Elliott Hughesaba6f712015-02-05 12:02:04 -0800573 vec[5].iov_len = strlen(msg) + 1;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700574
Mark Salyzyn0336e352013-11-08 06:58:01 -0800575 int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, sizeof(vec) / sizeof(vec[0])));
Nick Kralevich17fc25d2013-06-21 13:28:42 -0700576 close(main_log_fd);
577 return result;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700578}
579
580int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) {
581 char buffer[1024];
582 BufferOutputStream os(buffer, sizeof(buffer));
583 out_vformat(os, format, args);
584 return __libc_write_log(priority, tag, buffer);
585}
586
587int __libc_format_log(int priority, const char* tag, const char* format, ...) {
588 va_list args;
589 va_start(args, format);
590 int result = __libc_format_log_va_list(priority, tag, format, args);
591 va_end(args);
592 return result;
593}
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700594
Elliott Hughesb83d6742016-02-25 20:33:47 -0800595static void __libc_fatal_va_list(const char* prefix, const char* format, va_list args) {
Elliott Hughes0d787c12013-04-04 13:46:46 -0700596 char msg[1024];
597 BufferOutputStream os(msg, sizeof(msg));
Elliott Hughesb83d6742016-02-25 20:33:47 -0800598
599 if (prefix) {
600 os.Send(prefix, strlen(prefix));
601 os.Send(": ", 2);
602 }
603
Elliott Hughes0d787c12013-04-04 13:46:46 -0700604 out_vformat(os, format, args);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700605
Elliott Hughes42084a22015-02-02 12:24:46 -0800606 // Log to stderr for the benefit of "adb shell" users.
Dan Albert97e31de2014-07-20 11:49:46 -0700607 struct iovec iov[2] = {
Elliott Hughes42084a22015-02-02 12:24:46 -0800608 { msg, os.total },
609 { const_cast<char*>("\n"), 1 },
Dan Albert97e31de2014-07-20 11:49:46 -0700610 };
Elliott Hughes42084a22015-02-02 12:24:46 -0800611 TEMP_FAILURE_RETRY(writev(2, iov, 2));
Elliott Hughes0d787c12013-04-04 13:46:46 -0700612
613 // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed).
Elliott Hughesc78368f2014-05-06 20:37:22 -0700614 __libc_write_log(ANDROID_LOG_FATAL, "libc", msg);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700615
Dan Albertce6b1ab2014-08-18 14:37:42 -0700616 android_set_abort_message(msg);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700617}
Elliott Hughes0d787c12013-04-04 13:46:46 -0700618
Elliott Hughesb83d6742016-02-25 20:33:47 -0800619void __libc_fatal(const char* fmt, ...) {
Elliott Hughes61e699a2013-06-12 14:05:46 -0700620 va_list args;
Elliott Hughesb83d6742016-02-25 20:33:47 -0800621 va_start(args, fmt);
622 __libc_fatal_va_list(nullptr, fmt, args);
Elliott Hughes61e699a2013-06-12 14:05:46 -0700623 va_end(args);
Elliott Hughesb83d6742016-02-25 20:33:47 -0800624 abort();
Elliott Hughes61e699a2013-06-12 14:05:46 -0700625}
626
Elliott Hughesb83d6742016-02-25 20:33:47 -0800627void __fortify_fatal(const char* fmt, ...) {
Elliott Hughes61e699a2013-06-12 14:05:46 -0700628 va_list args;
Elliott Hughesb83d6742016-02-25 20:33:47 -0800629 va_start(args, fmt);
630 __libc_fatal_va_list("FORTIFY", fmt, args);
Elliott Hughes2e3b7102014-04-23 14:52:49 -0700631 va_end(args);
632 abort();
633}
634
Dan Albertce6b1ab2014-08-18 14:37:42 -0700635void android_set_abort_message(const char* msg) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700636 ScopedPthreadMutexLocker locker(&g_abort_msg_lock);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700637
Elliott Hughesc78368f2014-05-06 20:37:22 -0700638 if (__abort_message_ptr == NULL) {
639 // We must have crashed _very_ early.
640 return;
641 }
642
643 if (*__abort_message_ptr != NULL) {
644 // We already have an abort message.
645 // Assume that the first crash is the one most worth reporting.
646 return;
647 }
648
Elliott Hughes0d787c12013-04-04 13:46:46 -0700649 size_t size = sizeof(abort_msg_t) + strlen(msg) + 1;
650 void* map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
651 if (map == MAP_FAILED) {
652 return;
653 }
654
Elliott Hughesc78368f2014-05-06 20:37:22 -0700655 // TODO: if we stick to the current "one-shot" scheme, we can remove this code and
656 // stop storing the size.
657 if (*__abort_message_ptr != NULL) {
658 munmap(*__abort_message_ptr, (*__abort_message_ptr)->size);
Elliott Hughes0d787c12013-04-04 13:46:46 -0700659 }
Elliott Hughesc78368f2014-05-06 20:37:22 -0700660 abort_msg_t* new_abort_message = reinterpret_cast<abort_msg_t*>(map);
661 new_abort_message->size = size;
662 strcpy(new_abort_message->msg, msg);
663 *__abort_message_ptr = new_abort_message;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700664}