| 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> | 
|  | 34 | #include <errno.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 35 | #include <fcntl.h> | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 36 | #include <pthread.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 37 | #include <stdarg.h> | 
|  | 38 | #include <stddef.h> | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 39 | #include <stdlib.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 40 | #include <string.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 41 | #include <sys/mman.h> | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 42 | #include <sys/socket.h> | 
|  | 43 | #include <sys/types.h> | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 44 | #include <sys/uio.h> | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 45 | #include <sys/un.h> | 
|  | 46 | #include <time.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 47 | #include <unistd.h> | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 48 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 49 | static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 50 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 51 | __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] | 52 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 53 | // Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java. | 
|  | 54 | enum AndroidEventLogType { | 
|  | 55 | EVENT_TYPE_INT      = 0, | 
|  | 56 | EVENT_TYPE_LONG     = 1, | 
|  | 57 | EVENT_TYPE_STRING   = 2, | 
|  | 58 | EVENT_TYPE_LIST     = 3, | 
|  | 59 | }; | 
|  | 60 |  | 
|  | 61 | struct 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 Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 78 | total += len; | 
|  | 79 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 80 | while (len > 0) { | 
|  | 81 | int avail = end_ - pos_; | 
|  | 82 | if (avail == 0) { | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 83 | return; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 84 | } | 
|  | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 92 | } | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 93 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 94 |  | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 95 | size_t total; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 96 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 97 | private: | 
|  | 98 | char* buffer_; | 
|  | 99 | char* pos_; | 
|  | 100 | char* end_; | 
|  | 101 | }; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 102 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 103 | struct 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 Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 113 | total += len; | 
|  | 114 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 115 | while (len > 0) { | 
|  | 116 | int rc = TEMP_FAILURE_RETRY(write(fd_, data, len)); | 
|  | 117 | if (rc == -1) { | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 118 | return; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 119 | } | 
|  | 120 | data += rc; | 
|  | 121 | len -= rc; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
| Elliott Hughes | 416d7dd | 2014-08-18 17:28:32 -0700 | [diff] [blame] | 125 | size_t total; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 126 |  | 
|  | 127 | private: | 
|  | 128 | int fd_; | 
|  | 129 | }; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 130 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 131 | /*** 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 140 | static unsigned parse_decimal(const char *format, int *ppos) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 141 | const char* p = format + *ppos; | 
|  | 142 | unsigned result = 0; | 
|  | 143 |  | 
|  | 144 | for (;;) { | 
|  | 145 | int ch = *p; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 146 | unsigned d = static_cast<unsigned>(ch - '0'); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 147 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 148 | if (d >= 10U) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 149 | break; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 150 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 151 |  | 
|  | 152 | result = result*10 + d; | 
|  | 153 | p++; | 
|  | 154 | } | 
|  | 155 | *ppos = p - format; | 
|  | 156 | return result; | 
|  | 157 | } | 
|  | 158 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 159 | // Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes. | 
|  | 160 | // Assumes that buf_size > 0. | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 161 | 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] | 162 | char* p = buf; | 
|  | 163 | char* end = buf + buf_size - 1; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 164 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 165 | // 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 177 | } | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 178 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 179 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 180 | // Special case for 0. | 
|  | 181 | if (p == buf) { | 
|  | 182 | if (p != end) { | 
|  | 183 | *p++ = '0'; | 
| 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 | } | 
|  | 186 | *p = '\0'; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 187 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 188 | // 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 197 | static 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 207 |  | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 208 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 217 | template <typename Out> | 
|  | 218 | static 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 233 | /* Perform formatted output to an output target 'o' */ | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 234 | template <typename Out> | 
|  | 235 | static void out_vformat(Out& o, const char* format, va_list args) { | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 236 | int nn = 0; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 237 |  | 
|  | 238 | for (;;) { | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 239 | 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 McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 246 | int slen; | 
|  | 247 | char buffer[32];  /* temporary buffer used to format numbers */ | 
|  | 248 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 249 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 262 | o.Send(format+nn, mm-nn); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 263 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 278 | o.Send(&c, 1); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 279 | 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 Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 299 | width = static_cast<int>(parse_decimal(format, &nn)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 300 | c = format[nn++]; | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | /* parse precision */ | 
|  | 304 | if (c == '.') { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 305 | prec = static_cast<int>(parse_decimal(format, &nn)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 306 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 335 | default: | 
|  | 336 | ; | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | /* conversion specifier */ | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 340 | const char* str = buffer; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 341 | if (c == 's') { | 
|  | 342 | /* string */ | 
|  | 343 | str = va_arg(args, const char*); | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 344 | if (str == NULL) { | 
|  | 345 | str = "(null)"; | 
|  | 346 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 347 | } else if (c == 'c') { | 
|  | 348 | /* character */ | 
|  | 349 | /* NOTE: char is promoted to int when passed through the stack */ | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 350 | buffer[0] = static_cast<char>(va_arg(args, int)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 351 | buffer[1] = '\0'; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 352 | } else if (c == 'p') { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 353 | uint64_t  value = reinterpret_cast<uintptr_t>(va_arg(args, void*)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 354 | buffer[0] = '0'; | 
|  | 355 | buffer[1] = 'x'; | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 356 | format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x'); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 357 | } 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] | 358 | /* integers - first read value from stack */ | 
|  | 359 | uint64_t value; | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 360 | int is_signed = (c == 'd' || c == 'i' || c == 'o'); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 361 |  | 
|  | 362 | /* NOTE: int8_t and int16_t are promoted to int when passed | 
|  | 363 | *       through the stack | 
|  | 364 | */ | 
|  | 365 | switch (bytelen) { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 366 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 368 | 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 Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 374 | if (is_signed) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 375 | int shift = 64 - 8*bytelen; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 376 | 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] | 377 | } | 
|  | 378 |  | 
|  | 379 | /* format the number properly into our buffer */ | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 380 | 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' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 386 | } | 
|  | 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 Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 393 | if (sign != '\0' || prec != -1) { | 
|  | 394 | __assert(__FILE__, __LINE__, "sign/precision unsupported"); | 
|  | 395 | } | 
|  | 396 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 397 | if (slen < width && !padLeft) { | 
|  | 398 | char padChar = padZero ? '0' : ' '; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 399 | SendRepeat(o, padChar, width - slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 400 | } | 
|  | 401 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 402 | o.Send(str, slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 403 |  | 
|  | 404 | if (slen < width && padLeft) { | 
|  | 405 | char padChar = padZero ? '0' : ' '; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 406 | SendRepeat(o, padChar, width - slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 407 | } | 
|  | 408 | } | 
|  | 409 | } | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 410 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 411 | int __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 |  | 
|  | 420 | int __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 Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 429 | static 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 Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 450 | #ifdef TARGET_USES_LOGD | 
|  | 451 | static 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 Kralevich | bae5b1d | 2014-07-02 22:39:14 -0700 | [diff] [blame] | 459 | int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0)); | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 460 | 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 Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 484 |  | 
|  | 485 | struct log_time { // Wire format | 
|  | 486 | uint32_t tv_sec; | 
|  | 487 | uint32_t tv_nsec; | 
|  | 488 | }; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 489 | #endif | 
|  | 490 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 491 | static int __libc_write_log(int priority, const char* tag, const char* msg) { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 492 | #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 Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 500 | iovec vec[6]; | 
| Elliott Hughes | 0111019 | 2014-05-07 16:35:59 -0700 | [diff] [blame] | 501 | 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] | 502 | vec[0].iov_base = &log_id; | 
|  | 503 | vec[0].iov_len = sizeof(log_id); | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 504 | uint16_t tid = gettid(); | 
|  | 505 | vec[1].iov_base = &tid; | 
|  | 506 | vec[1].iov_len = sizeof(tid); | 
| Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 507 | 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 Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 512 | vec[2].iov_base = &realtime_ts; | 
|  | 513 | vec[2].iov_len = sizeof(realtime_ts); | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 514 |  | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 515 | 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 Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 521 | #else | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 522 | int main_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/main", O_CLOEXEC | O_WRONLY)); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 523 | if (main_log_fd == -1) { | 
| Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 524 | 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 Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 528 | return -1; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 529 | } | 
|  | 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 Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 538 | #endif | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 539 |  | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 540 | 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] | 541 | close(main_log_fd); | 
|  | 542 | return result; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 543 | } | 
|  | 544 |  | 
|  | 545 | int __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 |  | 
|  | 552 | int __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 Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 559 |  | 
|  | 560 | static int __libc_android_log_event(int32_t tag, char type, const void* payload, size_t len) { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 561 | #ifdef TARGET_USES_LOGD | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 562 | iovec vec[6]; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 563 | char log_id = LOG_ID_EVENTS; | 
|  | 564 | vec[0].iov_base = &log_id; | 
|  | 565 | vec[0].iov_len = sizeof(log_id); | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 566 | uint16_t tid = gettid(); | 
|  | 567 | vec[1].iov_base = &tid; | 
|  | 568 | vec[1].iov_len = sizeof(tid); | 
| Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 569 | 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 Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 574 | vec[2].iov_base = &realtime_ts; | 
|  | 575 | vec[2].iov_len = sizeof(realtime_ts); | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 576 |  | 
| Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 577 | 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 Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 583 |  | 
|  | 584 | int event_log_fd = __libc_open_log_socket(); | 
|  | 585 | #else | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 586 | iovec vec[3]; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 587 | 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 Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 594 | int event_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/events", O_CLOEXEC | O_WRONLY)); | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 595 | #endif | 
|  | 596 |  | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 597 | if (event_log_fd == -1) { | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 598 | return -1; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 599 | } | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 600 | int result = TEMP_FAILURE_RETRY(writev(event_log_fd, vec, sizeof(vec) / sizeof(vec[0]))); | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 601 | close(event_log_fd); | 
|  | 602 | return result; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 603 | } | 
|  | 604 |  | 
|  | 605 | void __libc_android_log_event_int(int32_t tag, int value) { | 
|  | 606 | __libc_android_log_event(tag, EVENT_TYPE_INT, &value, sizeof(value)); | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | void __libc_android_log_event_uid(int32_t tag) { | 
|  | 610 | __libc_android_log_event_int(tag, getuid()); | 
|  | 611 | } | 
|  | 612 |  | 
| Elliott Hughes | d1eda33 | 2013-10-15 16:43:38 -0700 | [diff] [blame] | 613 | void __fortify_chk_fail(const char* msg, uint32_t tag) { | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 614 | if (tag != 0) { | 
|  | 615 | __libc_android_log_event_uid(tag); | 
|  | 616 | } | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 617 | __libc_fatal("FORTIFY_SOURCE: %s. Calling abort().", msg); | 
|  | 618 | } | 
|  | 619 |  | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 620 | static void __libc_fatal(const char* format, va_list args) { | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 621 | char msg[1024]; | 
|  | 622 | BufferOutputStream os(msg, sizeof(msg)); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 623 | out_vformat(os, format, args); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 624 |  | 
| Dmitriy Ivanov | 12bf3bc | 2014-07-01 14:24:45 -0700 | [diff] [blame] | 625 | // log to stderr for the benefit of "adb shell" users. | 
| Dan Albert | 97e31de | 2014-07-20 11:49:46 -0700 | [diff] [blame] | 626 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 631 |  | 
|  | 632 | // 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] | 633 | __libc_write_log(ANDROID_LOG_FATAL, "libc", msg); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 634 |  | 
| Dan Albert | ce6b1ab | 2014-08-18 14:37:42 -0700 | [diff] [blame] | 635 | android_set_abort_message(msg); | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 636 | } | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 637 |  | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 638 | void __libc_fatal_no_abort(const char* format, ...) { | 
|  | 639 | va_list args; | 
|  | 640 | va_start(args, format); | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 641 | __libc_fatal(format, args); | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 642 | va_end(args); | 
|  | 643 | } | 
|  | 644 |  | 
|  | 645 | void __libc_fatal(const char* format, ...) { | 
|  | 646 | va_list args; | 
|  | 647 | va_start(args, format); | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 648 | __libc_fatal(format, args); | 
| Elliott Hughes | 2e3b710 | 2014-04-23 14:52:49 -0700 | [diff] [blame] | 649 | va_end(args); | 
|  | 650 | abort(); | 
|  | 651 | } | 
|  | 652 |  | 
| Dan Albert | ce6b1ab | 2014-08-18 14:37:42 -0700 | [diff] [blame] | 653 | void android_set_abort_message(const char* msg) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 654 | ScopedPthreadMutexLocker locker(&g_abort_msg_lock); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 655 |  | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 656 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 667 | 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 Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 673 | // 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 677 | } | 
| Elliott Hughes | c78368f | 2014-05-06 20:37:22 -0700 | [diff] [blame] | 678 | 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 Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 682 | } |