| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 29 | #include "../private/libc_logging.h" // Relative path so we can #include this .cpp file for testing. | 
|  | 30 | #include "../private/ScopedPthreadMutexLocker.h" | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 31 |  | 
| Dan Albert | ce6b1ab | 2014-08-18 14:37:42 -0700 | [diff] [blame] | 32 | #include <android/set_abort_message.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 33 | #include <assert.h> | 
| Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 34 | #include <ctype.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 35 | #include <errno.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 36 | #include <fcntl.h> | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 37 | #include <pthread.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 38 | #include <stdarg.h> | 
|  | 39 | #include <stddef.h> | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 40 | #include <stdlib.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 41 | #include <string.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 42 | #include <sys/mman.h> | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 43 | #include <sys/socket.h> | 
|  | 44 | #include <sys/types.h> | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 45 | #include <sys/uio.h> | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 46 | #include <sys/un.h> | 
|  | 47 | #include <time.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 48 | #include <unistd.h> | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 49 |  | 
| Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 50 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ | 
|  | 51 | #include <sys/_system_properties.h> | 
|  | 52 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 53 | static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 54 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 55 | __LIBC_HIDDEN__ abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common. | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 56 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 57 | // Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java. | 
|  | 58 | enum AndroidEventLogType { | 
|  | 59 | EVENT_TYPE_INT      = 0, | 
|  | 60 | EVENT_TYPE_LONG     = 1, | 
|  | 61 | EVENT_TYPE_STRING   = 2, | 
|  | 62 | EVENT_TYPE_LIST     = 3, | 
| Jeff Brown | 11331f6 | 2015-04-28 14:35:45 -0700 | [diff] [blame] | 63 | EVENT_TYPE_FLOAT    = 4, | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 64 | }; | 
|  | 65 |  | 
|  | 66 | struct 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 Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 83 | total += len; | 
|  | 84 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 85 | while (len > 0) { | 
|  | 86 | int avail = end_ - pos_; | 
|  | 87 | if (avail == 0) { | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 88 | return; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 89 | } | 
|  | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 97 | } | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 98 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 99 |  | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 100 | size_t total; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 101 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 102 | private: | 
|  | 103 | char* buffer_; | 
|  | 104 | char* pos_; | 
|  | 105 | char* end_; | 
|  | 106 | }; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 107 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 108 | struct FdOutputStream { | 
|  | 109 | public: | 
|  | 110 | FdOutputStream(int fd) : total(0), fd_(fd) { | 
|  | 111 | } | 
|  | 112 |  | 
|  | 113 | void Send(const char* data, int len) { | 
|  | 114 | if (len < 0) { | 
|  | 115 | len = strlen(data); | 
|  | 116 | } | 
|  | 117 |  | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 118 | total += len; | 
|  | 119 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 120 | while (len > 0) { | 
|  | 121 | int rc = TEMP_FAILURE_RETRY(write(fd_, data, len)); | 
|  | 122 | if (rc == -1) { | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 123 | return; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 124 | } | 
|  | 125 | data += rc; | 
|  | 126 | len -= rc; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 127 | } | 
|  | 128 | } | 
|  | 129 |  | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 130 | size_t total; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 131 |  | 
|  | 132 | private: | 
|  | 133 | int fd_; | 
|  | 134 | }; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 135 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 136 | /*** 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 145 | static unsigned parse_decimal(const char *format, int *ppos) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 146 | const char* p = format + *ppos; | 
|  | 147 | unsigned result = 0; | 
|  | 148 |  | 
|  | 149 | for (;;) { | 
|  | 150 | int ch = *p; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 151 | unsigned d = static_cast<unsigned>(ch - '0'); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 152 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 153 | if (d >= 10U) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 154 | break; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 155 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 156 |  | 
|  | 157 | result = result*10 + d; | 
|  | 158 | p++; | 
|  | 159 | } | 
|  | 160 | *ppos = p - format; | 
|  | 161 | return result; | 
|  | 162 | } | 
|  | 163 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 164 | // Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes. | 
|  | 165 | // Assumes that buf_size > 0. | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 166 | static void format_unsigned(char* buf, size_t buf_size, uint64_t value, int base, bool caps) { | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 167 | char* p = buf; | 
|  | 168 | char* end = buf + buf_size - 1; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 169 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 170 | // 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 182 | } | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 183 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 184 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 185 | // Special case for 0. | 
|  | 186 | if (p == buf) { | 
|  | 187 | if (p != end) { | 
|  | 188 | *p++ = '0'; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 189 | } | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 190 | } | 
|  | 191 | *p = '\0'; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 192 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 193 | // 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 202 | static 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 212 |  | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 213 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 222 | template <typename Out> | 
|  | 223 | static 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 238 | /* Perform formatted output to an output target 'o' */ | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 239 | template <typename Out> | 
|  | 240 | static void out_vformat(Out& o, const char* format, va_list args) { | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 241 | int nn = 0; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 242 |  | 
|  | 243 | for (;;) { | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 244 | 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 McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 251 | int slen; | 
|  | 252 | char buffer[32];  /* temporary buffer used to format numbers */ | 
|  | 253 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 254 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 267 | o.Send(format+nn, mm-nn); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 268 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 283 | o.Send(&c, 1); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 284 | 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 Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 304 | width = static_cast<int>(parse_decimal(format, &nn)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 305 | c = format[nn++]; | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | /* parse precision */ | 
|  | 309 | if (c == '.') { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 310 | prec = static_cast<int>(parse_decimal(format, &nn)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 311 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 340 | default: | 
|  | 341 | ; | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | /* conversion specifier */ | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 345 | const char* str = buffer; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 346 | if (c == 's') { | 
|  | 347 | /* string */ | 
|  | 348 | str = va_arg(args, const char*); | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 349 | if (str == NULL) { | 
|  | 350 | str = "(null)"; | 
|  | 351 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 352 | } else if (c == 'c') { | 
|  | 353 | /* character */ | 
|  | 354 | /* NOTE: char is promoted to int when passed through the stack */ | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 355 | buffer[0] = static_cast<char>(va_arg(args, int)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 356 | buffer[1] = '\0'; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 357 | } else if (c == 'p') { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 358 | uint64_t  value = reinterpret_cast<uintptr_t>(va_arg(args, void*)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 359 | buffer[0] = '0'; | 
|  | 360 | buffer[1] = 'x'; | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 361 | format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x'); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 362 | } else if (c == 'd' || c == 'i' || c == 'o' || c == 'u' || c == 'x' || c == 'X') { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 363 | /* integers - first read value from stack */ | 
|  | 364 | uint64_t value; | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 365 | int is_signed = (c == 'd' || c == 'i' || c == 'o'); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 366 |  | 
|  | 367 | /* NOTE: int8_t and int16_t are promoted to int when passed | 
|  | 368 | *       through the stack | 
|  | 369 | */ | 
|  | 370 | switch (bytelen) { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 371 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 373 | 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 Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 379 | if (is_signed) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 380 | int shift = 64 - 8*bytelen; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 381 | value = static_cast<uint64_t>((static_cast<int64_t>(value << shift)) >> shift); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 382 | } | 
|  | 383 |  | 
|  | 384 | /* format the number properly into our buffer */ | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 385 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 391 | } | 
|  | 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 Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 398 | if (sign != '\0' || prec != -1) { | 
|  | 399 | __assert(__FILE__, __LINE__, "sign/precision unsupported"); | 
|  | 400 | } | 
|  | 401 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 402 | if (slen < width && !padLeft) { | 
|  | 403 | char padChar = padZero ? '0' : ' '; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 404 | SendRepeat(o, padChar, width - slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 405 | } | 
|  | 406 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 407 | o.Send(str, slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 408 |  | 
|  | 409 | if (slen < width && padLeft) { | 
|  | 410 | char padChar = padZero ? '0' : ' '; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 411 | SendRepeat(o, padChar, width - slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 412 | } | 
|  | 413 | } | 
|  | 414 | } | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 415 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 416 | int __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 |  | 
|  | 425 | int __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 Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 434 | static int __libc_write_stderr(const char* tag, const char* msg) { | 
| Yabin Cui | 28e69f7 | 2015-03-25 12:36:18 -0700 | [diff] [blame] | 435 | int fd = TEMP_FAILURE_RETRY(open("/dev/stderr", O_CLOEXEC | O_WRONLY | O_APPEND)); | 
| Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 436 | 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 Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 446 | vec[2].iov_len = strlen(msg); | 
| Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 447 | 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 Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 455 | static int __libc_open_log_socket() { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 456 | // 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 Hughes | 0f67d5f | 2016-02-27 19:18:41 -0800 | [diff] [blame] | 462 | int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); | 
|  | 463 | if (log_fd == -1) { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 464 | 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 Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 482 |  | 
| Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 483 | struct cache { | 
|  | 484 | const prop_info* pinfo; | 
|  | 485 | uint32_t serial; | 
|  | 486 | char c; | 
|  | 487 | }; | 
|  | 488 |  | 
|  | 489 | static 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. | 
|  | 510 | static pthread_mutex_t lock_clockid = PTHREAD_MUTEX_INITIALIZER; | 
|  | 511 |  | 
| Mark Salyzyn | 9da687e | 2015-12-08 13:42:41 -0800 | [diff] [blame] | 512 | static clockid_t __android_log_clockid() | 
| Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 513 | { | 
|  | 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 Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 541 | struct log_time { // Wire format | 
|  | 542 | uint32_t tv_sec; | 
|  | 543 | uint32_t tv_nsec; | 
|  | 544 | }; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 545 |  | 
| Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 546 | int __libc_write_log(int priority, const char* tag, const char* msg) { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 547 | int main_log_fd = __libc_open_log_socket(); | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 548 | if (main_log_fd == -1) { | 
|  | 549 | // Try stderr instead. | 
|  | 550 | return __libc_write_stderr(tag, msg); | 
|  | 551 | } | 
|  | 552 |  | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 553 | iovec vec[6]; | 
| Elliott Hughes | 0111019 | 2014-05-07 16:35:59 -0700 | [diff] [blame] | 554 | char log_id = (priority == ANDROID_LOG_FATAL) ? LOG_ID_CRASH : LOG_ID_MAIN; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 555 | vec[0].iov_base = &log_id; | 
|  | 556 | vec[0].iov_len = sizeof(log_id); | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 557 | uint16_t tid = gettid(); | 
|  | 558 | vec[1].iov_base = &tid; | 
|  | 559 | vec[1].iov_len = sizeof(tid); | 
| Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 560 | timespec ts; | 
| Mark Salyzyn | 9da687e | 2015-12-08 13:42:41 -0800 | [diff] [blame] | 561 | clock_gettime(__android_log_clockid(), &ts); | 
| Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 562 | log_time realtime_ts; | 
|  | 563 | realtime_ts.tv_sec = ts.tv_sec; | 
|  | 564 | realtime_ts.tv_nsec = ts.tv_nsec; | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 565 | vec[2].iov_base = &realtime_ts; | 
|  | 566 | vec[2].iov_len = sizeof(realtime_ts); | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 567 |  | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 568 | vec[3].iov_base = &priority; | 
|  | 569 | vec[3].iov_len = 1; | 
|  | 570 | vec[4].iov_base = const_cast<char*>(tag); | 
| Elliott Hughes | aba6f71 | 2015-02-05 12:02:04 -0800 | [diff] [blame] | 571 | vec[4].iov_len = strlen(tag) + 1; | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 572 | vec[5].iov_base = const_cast<char*>(msg); | 
| Elliott Hughes | aba6f71 | 2015-02-05 12:02:04 -0800 | [diff] [blame] | 573 | vec[5].iov_len = strlen(msg) + 1; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 574 |  | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 575 | int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, sizeof(vec) / sizeof(vec[0]))); | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 576 | close(main_log_fd); | 
|  | 577 | return result; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 578 | } | 
|  | 579 |  | 
|  | 580 | int __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 |  | 
|  | 587 | int __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 Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 594 |  | 
| Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 595 | static void __libc_fatal_va_list(const char* prefix, const char* format, va_list args) { | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 596 | char msg[1024]; | 
|  | 597 | BufferOutputStream os(msg, sizeof(msg)); | 
| Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 598 |  | 
|  | 599 | if (prefix) { | 
|  | 600 | os.Send(prefix, strlen(prefix)); | 
|  | 601 | os.Send(": ", 2); | 
|  | 602 | } | 
|  | 603 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 604 | out_vformat(os, format, args); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 605 |  | 
| Elliott Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 606 | // Log to stderr for the benefit of "adb shell" users. | 
| Dan Albert | 97e31de | 2014-07-20 11:49:46 -0700 | [diff] [blame] | 607 | struct iovec iov[2] = { | 
| Elliott Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 608 | { msg, os.total }, | 
|  | 609 | { const_cast<char*>("\n"), 1 }, | 
| Dan Albert | 97e31de | 2014-07-20 11:49:46 -0700 | [diff] [blame] | 610 | }; | 
| Elliott Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 611 | TEMP_FAILURE_RETRY(writev(2, iov, 2)); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 612 |  | 
|  | 613 | // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed). | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 614 | __libc_write_log(ANDROID_LOG_FATAL, "libc", msg); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 615 |  | 
| Dan Albert | ce6b1ab | 2014-08-18 14:37:42 -0700 | [diff] [blame] | 616 | android_set_abort_message(msg); | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 617 | } | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 618 |  | 
| Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 619 | void __libc_fatal(const char* fmt, ...) { | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 620 | va_list args; | 
| Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 621 | va_start(args, fmt); | 
|  | 622 | __libc_fatal_va_list(nullptr, fmt, args); | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 623 | va_end(args); | 
| Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 624 | abort(); | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 625 | } | 
|  | 626 |  | 
| Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 627 | void __fortify_fatal(const char* fmt, ...) { | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 628 | va_list args; | 
| Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 629 | va_start(args, fmt); | 
|  | 630 | __libc_fatal_va_list("FORTIFY", fmt, args); | 
| Elliott Hughes | 2e3b710 | 2014-04-23 14:52:49 -0700 | [diff] [blame] | 631 | va_end(args); | 
|  | 632 | abort(); | 
|  | 633 | } | 
|  | 634 |  | 
| Dan Albert | ce6b1ab | 2014-08-18 14:37:42 -0700 | [diff] [blame] | 635 | void android_set_abort_message(const char* msg) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 636 | ScopedPthreadMutexLocker locker(&g_abort_msg_lock); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 637 |  | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 638 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 649 | 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 Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 655 | // 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 659 | } | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 660 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 664 | } |