| 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 |  | 
|  | 32 | #include <assert.h> | 
|  | 33 | #include <errno.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 34 | #include <fcntl.h> | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 35 | #include <pthread.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 36 | #include <stdarg.h> | 
|  | 37 | #include <stddef.h> | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 38 | #include <stdlib.h> | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 39 | #include <string.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 40 | #include <sys/mman.h> | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 41 | #include <sys/socket.h> | 
|  | 42 | #include <sys/types.h> | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 43 | #include <sys/uio.h> | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 44 | #include <sys/un.h> | 
|  | 45 | #include <time.h> | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 46 | #include <unistd.h> | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 47 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 48 | static pthread_mutex_t gAbortMsgLock = PTHREAD_MUTEX_INITIALIZER; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 49 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 50 | __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] | 51 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 52 | // Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java. | 
|  | 53 | enum AndroidEventLogType { | 
|  | 54 | EVENT_TYPE_INT      = 0, | 
|  | 55 | EVENT_TYPE_LONG     = 1, | 
|  | 56 | EVENT_TYPE_STRING   = 2, | 
|  | 57 | EVENT_TYPE_LIST     = 3, | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | struct BufferOutputStream { | 
|  | 61 | public: | 
|  | 62 | BufferOutputStream(char* buffer, size_t size) : total(0) { | 
|  | 63 | buffer_ = buffer; | 
|  | 64 | end_ = buffer + size - 1; | 
|  | 65 | pos_ = buffer_; | 
|  | 66 | pos_[0] = '\0'; | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | ~BufferOutputStream() { | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | void Send(const char* data, int len) { | 
|  | 73 | if (len < 0) { | 
|  | 74 | len = strlen(data); | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | while (len > 0) { | 
|  | 78 | int avail = end_ - pos_; | 
|  | 79 | if (avail == 0) { | 
|  | 80 | break; | 
|  | 81 | } | 
|  | 82 | if (avail > len) { | 
|  | 83 | avail = len; | 
|  | 84 | } | 
|  | 85 | memcpy(pos_, data, avail); | 
|  | 86 | pos_ += avail; | 
|  | 87 | pos_[0] = '\0'; | 
|  | 88 | len -= avail; | 
|  | 89 | total += avail; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 90 | } | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 91 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 92 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 93 | int total; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 94 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 95 | private: | 
|  | 96 | char* buffer_; | 
|  | 97 | char* pos_; | 
|  | 98 | char* end_; | 
|  | 99 | }; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 100 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 101 | struct FdOutputStream { | 
|  | 102 | public: | 
|  | 103 | FdOutputStream(int fd) : total(0), fd_(fd) { | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | void Send(const char* data, int len) { | 
|  | 107 | if (len < 0) { | 
|  | 108 | len = strlen(data); | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | while (len > 0) { | 
|  | 112 | int rc = TEMP_FAILURE_RETRY(write(fd_, data, len)); | 
|  | 113 | if (rc == -1) { | 
|  | 114 | break; | 
|  | 115 | } | 
|  | 116 | data += rc; | 
|  | 117 | len -= rc; | 
|  | 118 | total += rc; | 
|  | 119 | } | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | int total; | 
|  | 123 |  | 
|  | 124 | private: | 
|  | 125 | int fd_; | 
|  | 126 | }; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 127 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 128 | /*** formatted output implementation | 
|  | 129 | ***/ | 
|  | 130 |  | 
|  | 131 | /* Parse a decimal string from 'format + *ppos', | 
|  | 132 | * return the value, and writes the new position past | 
|  | 133 | * the decimal string in '*ppos' on exit. | 
|  | 134 | * | 
|  | 135 | * NOTE: Does *not* handle a sign prefix. | 
|  | 136 | */ | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 137 | static unsigned parse_decimal(const char *format, int *ppos) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 138 | const char* p = format + *ppos; | 
|  | 139 | unsigned result = 0; | 
|  | 140 |  | 
|  | 141 | for (;;) { | 
|  | 142 | int ch = *p; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 143 | unsigned d = static_cast<unsigned>(ch - '0'); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 144 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 145 | if (d >= 10U) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 146 | break; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 147 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | result = result*10 + d; | 
|  | 150 | p++; | 
|  | 151 | } | 
|  | 152 | *ppos = p - format; | 
|  | 153 | return result; | 
|  | 154 | } | 
|  | 155 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 156 | // Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes. | 
|  | 157 | // Assumes that buf_size > 0. | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 158 | 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] | 159 | char* p = buf; | 
|  | 160 | char* end = buf + buf_size - 1; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 161 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 162 | // Generate digit string in reverse order. | 
|  | 163 | while (value) { | 
|  | 164 | unsigned d = value % base; | 
|  | 165 | value /= base; | 
|  | 166 | if (p != end) { | 
|  | 167 | char ch; | 
|  | 168 | if (d < 10) { | 
|  | 169 | ch = '0' + d; | 
|  | 170 | } else { | 
|  | 171 | ch = (caps ? 'A' : 'a') + (d - 10); | 
|  | 172 | } | 
|  | 173 | *p++ = ch; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 174 | } | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 175 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 176 |  | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 177 | // Special case for 0. | 
|  | 178 | if (p == buf) { | 
|  | 179 | if (p != end) { | 
|  | 180 | *p++ = '0'; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 181 | } | 
| Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 182 | } | 
|  | 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 | // Reverse digit string in-place. | 
|  | 186 | size_t length = p - buf; | 
|  | 187 | for (size_t i = 0, j = length - 1; i < j; ++i, --j) { | 
|  | 188 | char ch = buf[i]; | 
|  | 189 | buf[i] = buf[j]; | 
|  | 190 | buf[j] = ch; | 
|  | 191 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 194 | static void format_integer(char* buf, size_t buf_size, uint64_t value, char conversion) { | 
|  | 195 | // Decode the conversion specifier. | 
|  | 196 | int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o'); | 
|  | 197 | int base = 10; | 
|  | 198 | if (conversion == 'x' || conversion == 'X') { | 
|  | 199 | base = 16; | 
|  | 200 | } else if (conversion == 'o') { | 
|  | 201 | base = 8; | 
|  | 202 | } | 
|  | 203 | bool caps = (conversion == 'X'); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 204 |  | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 205 | if (is_signed && static_cast<int64_t>(value) < 0) { | 
|  | 206 | buf[0] = '-'; | 
|  | 207 | buf += 1; | 
|  | 208 | buf_size -= 1; | 
|  | 209 | value = static_cast<uint64_t>(-static_cast<int64_t>(value)); | 
|  | 210 | } | 
|  | 211 | format_unsigned(buf, buf_size, value, base, caps); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 214 | template <typename Out> | 
|  | 215 | static void SendRepeat(Out& o, char ch, int count) { | 
|  | 216 | char pad[8]; | 
|  | 217 | memset(pad, ch, sizeof(pad)); | 
|  | 218 |  | 
|  | 219 | const int pad_size = static_cast<int>(sizeof(pad)); | 
|  | 220 | while (count > 0) { | 
|  | 221 | int avail = count; | 
|  | 222 | if (avail > pad_size) { | 
|  | 223 | avail = pad_size; | 
|  | 224 | } | 
|  | 225 | o.Send(pad, avail); | 
|  | 226 | count -= avail; | 
|  | 227 | } | 
|  | 228 | } | 
|  | 229 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 230 | /* Perform formatted output to an output target 'o' */ | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 231 | template <typename Out> | 
|  | 232 | static void out_vformat(Out& o, const char* format, va_list args) { | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 233 | int nn = 0; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 234 |  | 
|  | 235 | for (;;) { | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 236 | int mm; | 
|  | 237 | int padZero = 0; | 
|  | 238 | int padLeft = 0; | 
|  | 239 | char sign = '\0'; | 
|  | 240 | int width = -1; | 
|  | 241 | int prec  = -1; | 
|  | 242 | size_t bytelen = sizeof(int); | 
| Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 243 | int slen; | 
|  | 244 | char buffer[32];  /* temporary buffer used to format numbers */ | 
|  | 245 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 246 | char  c; | 
|  | 247 |  | 
|  | 248 | /* first, find all characters that are not 0 or '%' */ | 
|  | 249 | /* then send them to the output directly */ | 
|  | 250 | mm = nn; | 
|  | 251 | do { | 
|  | 252 | c = format[mm]; | 
|  | 253 | if (c == '\0' || c == '%') | 
|  | 254 | break; | 
|  | 255 | mm++; | 
|  | 256 | } while (1); | 
|  | 257 |  | 
|  | 258 | if (mm > nn) { | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 259 | o.Send(format+nn, mm-nn); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 260 | nn = mm; | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | /* is this it ? then exit */ | 
|  | 264 | if (c == '\0') | 
|  | 265 | break; | 
|  | 266 |  | 
|  | 267 | /* nope, we are at a '%' modifier */ | 
|  | 268 | nn++;  // skip it | 
|  | 269 |  | 
|  | 270 | /* parse flags */ | 
|  | 271 | for (;;) { | 
|  | 272 | c = format[nn++]; | 
|  | 273 | if (c == '\0') {  /* single trailing '%' ? */ | 
|  | 274 | c = '%'; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 275 | o.Send(&c, 1); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 276 | return; | 
|  | 277 | } | 
|  | 278 | else if (c == '0') { | 
|  | 279 | padZero = 1; | 
|  | 280 | continue; | 
|  | 281 | } | 
|  | 282 | else if (c == '-') { | 
|  | 283 | padLeft = 1; | 
|  | 284 | continue; | 
|  | 285 | } | 
|  | 286 | else if (c == ' ' || c == '+') { | 
|  | 287 | sign = c; | 
|  | 288 | continue; | 
|  | 289 | } | 
|  | 290 | break; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | /* parse field width */ | 
|  | 294 | if ((c >= '0' && c <= '9')) { | 
|  | 295 | nn --; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 296 | width = static_cast<int>(parse_decimal(format, &nn)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 297 | c = format[nn++]; | 
|  | 298 | } | 
|  | 299 |  | 
|  | 300 | /* parse precision */ | 
|  | 301 | if (c == '.') { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 302 | prec = static_cast<int>(parse_decimal(format, &nn)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 303 | c = format[nn++]; | 
|  | 304 | } | 
|  | 305 |  | 
|  | 306 | /* length modifier */ | 
|  | 307 | switch (c) { | 
|  | 308 | case 'h': | 
|  | 309 | bytelen = sizeof(short); | 
|  | 310 | if (format[nn] == 'h') { | 
|  | 311 | bytelen = sizeof(char); | 
|  | 312 | nn += 1; | 
|  | 313 | } | 
|  | 314 | c = format[nn++]; | 
|  | 315 | break; | 
|  | 316 | case 'l': | 
|  | 317 | bytelen = sizeof(long); | 
|  | 318 | if (format[nn] == 'l') { | 
|  | 319 | bytelen = sizeof(long long); | 
|  | 320 | nn += 1; | 
|  | 321 | } | 
|  | 322 | c = format[nn++]; | 
|  | 323 | break; | 
|  | 324 | case 'z': | 
|  | 325 | bytelen = sizeof(size_t); | 
|  | 326 | c = format[nn++]; | 
|  | 327 | break; | 
|  | 328 | case 't': | 
|  | 329 | bytelen = sizeof(ptrdiff_t); | 
|  | 330 | c = format[nn++]; | 
|  | 331 | break; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 332 | default: | 
|  | 333 | ; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | /* conversion specifier */ | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 337 | const char* str = buffer; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 338 | if (c == 's') { | 
|  | 339 | /* string */ | 
|  | 340 | str = va_arg(args, const char*); | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 341 | if (str == NULL) { | 
|  | 342 | str = "(null)"; | 
|  | 343 | } | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 344 | } else if (c == 'c') { | 
|  | 345 | /* character */ | 
|  | 346 | /* NOTE: char is promoted to int when passed through the stack */ | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 347 | buffer[0] = static_cast<char>(va_arg(args, int)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 348 | buffer[1] = '\0'; | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 349 | } else if (c == 'p') { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 350 | uint64_t  value = reinterpret_cast<uintptr_t>(va_arg(args, void*)); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 351 | buffer[0] = '0'; | 
|  | 352 | buffer[1] = 'x'; | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 353 | format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x'); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 354 | } 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] | 355 | /* integers - first read value from stack */ | 
|  | 356 | uint64_t value; | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 357 | int is_signed = (c == 'd' || c == 'i' || c == 'o'); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 358 |  | 
|  | 359 | /* NOTE: int8_t and int16_t are promoted to int when passed | 
|  | 360 | *       through the stack | 
|  | 361 | */ | 
|  | 362 | switch (bytelen) { | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 363 | case 1: value = static_cast<uint8_t>(va_arg(args, int)); break; | 
|  | 364 | 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] | 365 | case 4: value = va_arg(args, uint32_t); break; | 
|  | 366 | case 8: value = va_arg(args, uint64_t); break; | 
|  | 367 | default: return;  /* should not happen */ | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | /* sign extension, if needed */ | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 371 | if (is_signed) { | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 372 | int shift = 64 - 8*bytelen; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 373 | 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] | 374 | } | 
|  | 375 |  | 
|  | 376 | /* format the number properly into our buffer */ | 
| Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 377 | format_integer(buffer, sizeof(buffer), value, c); | 
|  | 378 | } else if (c == '%') { | 
|  | 379 | buffer[0] = '%'; | 
|  | 380 | buffer[1] = '\0'; | 
|  | 381 | } else { | 
|  | 382 | __assert(__FILE__, __LINE__, "conversion specifier unsupported"); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 383 | } | 
|  | 384 |  | 
|  | 385 | /* if we are here, 'str' points to the content that must be | 
|  | 386 | * outputted. handle padding and alignment now */ | 
|  | 387 |  | 
|  | 388 | slen = strlen(str); | 
|  | 389 |  | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 390 | if (sign != '\0' || prec != -1) { | 
|  | 391 | __assert(__FILE__, __LINE__, "sign/precision unsupported"); | 
|  | 392 | } | 
|  | 393 |  | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 394 | if (slen < width && !padLeft) { | 
|  | 395 | char padChar = padZero ? '0' : ' '; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 396 | SendRepeat(o, padChar, width - slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 397 | } | 
|  | 398 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 399 | o.Send(str, slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 400 |  | 
|  | 401 | if (slen < width && padLeft) { | 
|  | 402 | char padChar = padZero ? '0' : ' '; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 403 | SendRepeat(o, padChar, width - slen); | 
| David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 404 | } | 
|  | 405 | } | 
|  | 406 | } | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 407 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 408 | int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) { | 
|  | 409 | BufferOutputStream os(buffer, buffer_size); | 
|  | 410 | va_list args; | 
|  | 411 | va_start(args, format); | 
|  | 412 | out_vformat(os, format, args); | 
|  | 413 | va_end(args); | 
|  | 414 | return os.total; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | int __libc_format_fd(int fd, const char* format, ...) { | 
|  | 418 | FdOutputStream os(fd); | 
|  | 419 | va_list args; | 
|  | 420 | va_start(args, format); | 
|  | 421 | out_vformat(os, format, args); | 
|  | 422 | va_end(args); | 
|  | 423 | return os.total; | 
|  | 424 | } | 
|  | 425 |  | 
| Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 426 | static int __libc_write_stderr(const char* tag, const char* msg) { | 
|  | 427 | int fd = TEMP_FAILURE_RETRY(open("/dev/stderr", O_CLOEXEC | O_WRONLY)); | 
|  | 428 | if (fd == -1) { | 
|  | 429 | return -1; | 
|  | 430 | } | 
|  | 431 |  | 
|  | 432 | iovec vec[4]; | 
|  | 433 | vec[0].iov_base = const_cast<char*>(tag); | 
|  | 434 | vec[0].iov_len = strlen(tag); | 
|  | 435 | vec[1].iov_base = const_cast<char*>(": "); | 
|  | 436 | vec[1].iov_len = 2; | 
|  | 437 | vec[2].iov_base = const_cast<char*>(msg); | 
|  | 438 | vec[2].iov_len = strlen(msg) + 1; | 
|  | 439 | vec[3].iov_base = const_cast<char*>("\n"); | 
|  | 440 | vec[3].iov_len = 1; | 
|  | 441 |  | 
|  | 442 | int result = TEMP_FAILURE_RETRY(writev(fd, vec, 4)); | 
|  | 443 | close(fd); | 
|  | 444 | return result; | 
|  | 445 | } | 
|  | 446 |  | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 447 | #ifdef TARGET_USES_LOGD | 
|  | 448 | static int __libc_open_log_socket() | 
|  | 449 | { | 
|  | 450 | // ToDo: Ideally we want this to fail if the gid of the current | 
|  | 451 | // process is AID_LOGD, but will have to wait until we have | 
|  | 452 | // registered this in private/android_filesystem_config.h. We have | 
|  | 453 | // found that all logd crashes thus far have had no problem stuffing | 
|  | 454 | // the UNIX domain socket and moving on so not critical *today*. | 
|  | 455 |  | 
|  | 456 | int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM, 0)); | 
|  | 457 | if (log_fd < 0) { | 
|  | 458 | return -1; | 
|  | 459 | } | 
|  | 460 |  | 
|  | 461 | if (fcntl(log_fd, F_SETFL, O_NONBLOCK) == -1) { | 
|  | 462 | close(log_fd); | 
|  | 463 | return -1; | 
|  | 464 | } | 
|  | 465 |  | 
|  | 466 | union { | 
|  | 467 | struct sockaddr    addr; | 
|  | 468 | struct sockaddr_un addrUn; | 
|  | 469 | } u; | 
|  | 470 | memset(&u, 0, sizeof(u)); | 
|  | 471 | u.addrUn.sun_family = AF_UNIX; | 
|  | 472 | strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path)); | 
|  | 473 |  | 
|  | 474 | if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) { | 
|  | 475 | close(log_fd); | 
|  | 476 | return -1; | 
|  | 477 | } | 
|  | 478 |  | 
|  | 479 | return log_fd; | 
|  | 480 | } | 
|  | 481 | #endif | 
|  | 482 |  | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 483 | 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] | 484 | #ifdef TARGET_USES_LOGD | 
|  | 485 | int main_log_fd = __libc_open_log_socket(); | 
|  | 486 |  | 
|  | 487 | if (main_log_fd == -1) { | 
|  | 488 | // Try stderr instead. | 
|  | 489 | return __libc_write_stderr(tag, msg); | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | iovec vec[5]; | 
|  | 493 | char log_id = LOG_ID_MAIN; | 
|  | 494 | vec[0].iov_base = &log_id; | 
|  | 495 | vec[0].iov_len = sizeof(log_id); | 
|  | 496 | timespec realtime_ts; | 
|  | 497 | clock_gettime(CLOCK_REALTIME, &realtime_ts); | 
|  | 498 | vec[1].iov_base = &realtime_ts; | 
|  | 499 | vec[1].iov_len = sizeof(realtime_ts); | 
|  | 500 |  | 
|  | 501 | vec[2].iov_base = &priority; | 
|  | 502 | vec[2].iov_len = 1; | 
|  | 503 | vec[3].iov_base = const_cast<char*>(tag); | 
|  | 504 | vec[3].iov_len = strlen(tag) + 1; | 
|  | 505 | vec[4].iov_base = const_cast<char*>(msg); | 
|  | 506 | vec[4].iov_len = strlen(msg) + 1; | 
|  | 507 | #else | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 508 | 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] | 509 | if (main_log_fd == -1) { | 
| Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 510 | if (errno == ENOTDIR) { | 
|  | 511 | // /dev/log isn't a directory? Maybe we're running on the host? Try stderr instead. | 
|  | 512 | return __libc_write_stderr(tag, msg); | 
|  | 513 | } | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 514 | return -1; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 515 | } | 
|  | 516 |  | 
|  | 517 | iovec vec[3]; | 
|  | 518 | vec[0].iov_base = &priority; | 
|  | 519 | vec[0].iov_len = 1; | 
|  | 520 | vec[1].iov_base = const_cast<char*>(tag); | 
|  | 521 | vec[1].iov_len = strlen(tag) + 1; | 
|  | 522 | vec[2].iov_base = const_cast<char*>(msg); | 
|  | 523 | vec[2].iov_len = strlen(msg) + 1; | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 524 | #endif | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 525 |  | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 526 | 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] | 527 | close(main_log_fd); | 
|  | 528 | return result; | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 529 | } | 
|  | 530 |  | 
|  | 531 | int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) { | 
|  | 532 | char buffer[1024]; | 
|  | 533 | BufferOutputStream os(buffer, sizeof(buffer)); | 
|  | 534 | out_vformat(os, format, args); | 
|  | 535 | return __libc_write_log(priority, tag, buffer); | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | int __libc_format_log(int priority, const char* tag, const char* format, ...) { | 
|  | 539 | va_list args; | 
|  | 540 | va_start(args, format); | 
|  | 541 | int result = __libc_format_log_va_list(priority, tag, format, args); | 
|  | 542 | va_end(args); | 
|  | 543 | return result; | 
|  | 544 | } | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 545 |  | 
|  | 546 | 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] | 547 | #ifdef TARGET_USES_LOGD | 
|  | 548 | iovec vec[5]; | 
|  | 549 | char log_id = LOG_ID_EVENTS; | 
|  | 550 | vec[0].iov_base = &log_id; | 
|  | 551 | vec[0].iov_len = sizeof(log_id); | 
|  | 552 | timespec realtime_ts; | 
|  | 553 | clock_gettime(CLOCK_REALTIME, &realtime_ts); | 
|  | 554 | vec[1].iov_base = &realtime_ts; | 
|  | 555 | vec[1].iov_len = sizeof(realtime_ts); | 
|  | 556 |  | 
|  | 557 | vec[2].iov_base = &tag; | 
|  | 558 | vec[2].iov_len = sizeof(tag); | 
|  | 559 | vec[3].iov_base = &type; | 
|  | 560 | vec[3].iov_len = sizeof(type); | 
|  | 561 | vec[4].iov_base = const_cast<void*>(payload); | 
|  | 562 | vec[4].iov_len = len; | 
|  | 563 |  | 
|  | 564 | int event_log_fd = __libc_open_log_socket(); | 
|  | 565 | #else | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 566 | iovec vec[3]; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 567 | vec[0].iov_base = &tag; | 
|  | 568 | vec[0].iov_len = sizeof(tag); | 
|  | 569 | vec[1].iov_base = &type; | 
|  | 570 | vec[1].iov_len = sizeof(type); | 
|  | 571 | vec[2].iov_base = const_cast<void*>(payload); | 
|  | 572 | vec[2].iov_len = len; | 
|  | 573 |  | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 574 | 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] | 575 | #endif | 
|  | 576 |  | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 577 | if (event_log_fd == -1) { | 
| Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame] | 578 | return -1; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 579 | } | 
| Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 580 | 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] | 581 | close(event_log_fd); | 
|  | 582 | return result; | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 583 | } | 
|  | 584 |  | 
|  | 585 | void __libc_android_log_event_int(int32_t tag, int value) { | 
|  | 586 | __libc_android_log_event(tag, EVENT_TYPE_INT, &value, sizeof(value)); | 
|  | 587 | } | 
|  | 588 |  | 
|  | 589 | void __libc_android_log_event_uid(int32_t tag) { | 
|  | 590 | __libc_android_log_event_int(tag, getuid()); | 
|  | 591 | } | 
|  | 592 |  | 
| Elliott Hughes | d1eda33 | 2013-10-15 16:43:38 -0700 | [diff] [blame] | 593 | void __fortify_chk_fail(const char* msg, uint32_t tag) { | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 594 | if (tag != 0) { | 
|  | 595 | __libc_android_log_event_uid(tag); | 
|  | 596 | } | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 597 | __libc_fatal("FORTIFY_SOURCE: %s. Calling abort().", msg); | 
|  | 598 | } | 
|  | 599 |  | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 600 | static void __libc_fatal(const char* format, va_list args) { | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 601 | char msg[1024]; | 
|  | 602 | BufferOutputStream os(msg, sizeof(msg)); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 603 | out_vformat(os, format, args); | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 604 |  | 
|  | 605 | // TODO: log to stderr for the benefit of "adb shell" users. | 
|  | 606 |  | 
|  | 607 | // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed). | 
|  | 608 | __libc_write_log(ANDROID_LOG_FATAL, "libc", msg); | 
|  | 609 |  | 
|  | 610 | __libc_set_abort_message(msg); | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 611 | } | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 612 |  | 
| Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 613 | void __libc_fatal_no_abort(const char* format, ...) { | 
|  | 614 | va_list args; | 
|  | 615 | va_start(args, format); | 
|  | 616 | __libc_fatal(format, args); | 
|  | 617 | va_end(args); | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | void __libc_fatal(const char* format, ...) { | 
|  | 621 | va_list args; | 
|  | 622 | va_start(args, format); | 
|  | 623 | __libc_fatal(format, args); | 
|  | 624 | va_end(args); | 
| Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 625 | abort(); | 
|  | 626 | } | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 627 |  | 
|  | 628 | void __libc_set_abort_message(const char* msg) { | 
|  | 629 | size_t size = sizeof(abort_msg_t) + strlen(msg) + 1; | 
|  | 630 | void* map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); | 
|  | 631 | if (map == MAP_FAILED) { | 
|  | 632 | return; | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | if (__abort_message_ptr != NULL) { | 
|  | 636 | ScopedPthreadMutexLocker locker(&gAbortMsgLock); | 
|  | 637 | if (*__abort_message_ptr != NULL) { | 
|  | 638 | munmap(*__abort_message_ptr, (*__abort_message_ptr)->size); | 
|  | 639 | } | 
|  | 640 | abort_msg_t* new_abort_message = reinterpret_cast<abort_msg_t*>(map); | 
|  | 641 | new_abort_message->size = size; | 
|  | 642 | strcpy(new_abort_message->msg, msg); | 
|  | 643 | *__abort_message_ptr = new_abort_message; | 
|  | 644 | } | 
|  | 645 | } |