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 | 8f2a5a0 | 2013-03-15 15:30:25 -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> |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 41 | #include <sys/uio.h> |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 42 | #include <unistd.h> |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 43 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 44 | static pthread_mutex_t gAbortMsgLock = PTHREAD_MUTEX_INITIALIZER; |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 46 | __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] | 47 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 48 | // Must be kept in sync with frameworks/base/core/java/android/util/EventLog.java. |
| 49 | enum AndroidEventLogType { |
| 50 | EVENT_TYPE_INT = 0, |
| 51 | EVENT_TYPE_LONG = 1, |
| 52 | EVENT_TYPE_STRING = 2, |
| 53 | EVENT_TYPE_LIST = 3, |
| 54 | }; |
| 55 | |
| 56 | struct BufferOutputStream { |
| 57 | public: |
| 58 | BufferOutputStream(char* buffer, size_t size) : total(0) { |
| 59 | buffer_ = buffer; |
| 60 | end_ = buffer + size - 1; |
| 61 | pos_ = buffer_; |
| 62 | pos_[0] = '\0'; |
| 63 | } |
| 64 | |
| 65 | ~BufferOutputStream() { |
| 66 | } |
| 67 | |
| 68 | void Send(const char* data, int len) { |
| 69 | if (len < 0) { |
| 70 | len = strlen(data); |
| 71 | } |
| 72 | |
| 73 | while (len > 0) { |
| 74 | int avail = end_ - pos_; |
| 75 | if (avail == 0) { |
| 76 | break; |
| 77 | } |
| 78 | if (avail > len) { |
| 79 | avail = len; |
| 80 | } |
| 81 | memcpy(pos_, data, avail); |
| 82 | pos_ += avail; |
| 83 | pos_[0] = '\0'; |
| 84 | len -= avail; |
| 85 | total += avail; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 86 | } |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 87 | } |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 88 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 89 | int total; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 90 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 91 | private: |
| 92 | char* buffer_; |
| 93 | char* pos_; |
| 94 | char* end_; |
| 95 | }; |
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 | struct FdOutputStream { |
| 98 | public: |
| 99 | FdOutputStream(int fd) : total(0), fd_(fd) { |
| 100 | } |
| 101 | |
| 102 | void Send(const char* data, int len) { |
| 103 | if (len < 0) { |
| 104 | len = strlen(data); |
| 105 | } |
| 106 | |
| 107 | while (len > 0) { |
| 108 | int rc = TEMP_FAILURE_RETRY(write(fd_, data, len)); |
| 109 | if (rc == -1) { |
| 110 | break; |
| 111 | } |
| 112 | data += rc; |
| 113 | len -= rc; |
| 114 | total += rc; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | int total; |
| 119 | |
| 120 | private: |
| 121 | int fd_; |
| 122 | }; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 123 | |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 124 | /*** formatted output implementation |
| 125 | ***/ |
| 126 | |
| 127 | /* Parse a decimal string from 'format + *ppos', |
| 128 | * return the value, and writes the new position past |
| 129 | * the decimal string in '*ppos' on exit. |
| 130 | * |
| 131 | * NOTE: Does *not* handle a sign prefix. |
| 132 | */ |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 133 | static unsigned parse_decimal(const char *format, int *ppos) { |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 134 | const char* p = format + *ppos; |
| 135 | unsigned result = 0; |
| 136 | |
| 137 | for (;;) { |
| 138 | int ch = *p; |
| 139 | unsigned d = (unsigned)(ch - '0'); |
| 140 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 141 | if (d >= 10U) { |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 142 | break; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 143 | } |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 144 | |
| 145 | result = result*10 + d; |
| 146 | p++; |
| 147 | } |
| 148 | *ppos = p - format; |
| 149 | return result; |
| 150 | } |
| 151 | |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 152 | // Writes number 'value' in base 'base' into buffer 'buf' of size 'buf_size' bytes. |
| 153 | // Assumes that buf_size > 0. |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 154 | 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] | 155 | char* p = buf; |
| 156 | char* end = buf + buf_size - 1; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 157 | |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 158 | // Generate digit string in reverse order. |
| 159 | while (value) { |
| 160 | unsigned d = value % base; |
| 161 | value /= base; |
| 162 | if (p != end) { |
| 163 | char ch; |
| 164 | if (d < 10) { |
| 165 | ch = '0' + d; |
| 166 | } else { |
| 167 | ch = (caps ? 'A' : 'a') + (d - 10); |
| 168 | } |
| 169 | *p++ = ch; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 170 | } |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 171 | } |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 172 | |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 173 | // Special case for 0. |
| 174 | if (p == buf) { |
| 175 | if (p != end) { |
| 176 | *p++ = '0'; |
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 | } |
| 179 | *p = '\0'; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 180 | |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 181 | // Reverse digit string in-place. |
| 182 | size_t length = p - buf; |
| 183 | for (size_t i = 0, j = length - 1; i < j; ++i, --j) { |
| 184 | char ch = buf[i]; |
| 185 | buf[i] = buf[j]; |
| 186 | buf[j] = ch; |
| 187 | } |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 190 | static void format_integer(char* buf, size_t buf_size, uint64_t value, char conversion) { |
| 191 | // Decode the conversion specifier. |
| 192 | int is_signed = (conversion == 'd' || conversion == 'i' || conversion == 'o'); |
| 193 | int base = 10; |
| 194 | if (conversion == 'x' || conversion == 'X') { |
| 195 | base = 16; |
| 196 | } else if (conversion == 'o') { |
| 197 | base = 8; |
| 198 | } |
| 199 | bool caps = (conversion == 'X'); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 200 | |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 201 | if (is_signed && static_cast<int64_t>(value) < 0) { |
| 202 | buf[0] = '-'; |
| 203 | buf += 1; |
| 204 | buf_size -= 1; |
| 205 | value = static_cast<uint64_t>(-static_cast<int64_t>(value)); |
| 206 | } |
| 207 | format_unsigned(buf, buf_size, value, base, caps); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 210 | template <typename Out> |
| 211 | static void SendRepeat(Out& o, char ch, int count) { |
| 212 | char pad[8]; |
| 213 | memset(pad, ch, sizeof(pad)); |
| 214 | |
| 215 | const int pad_size = static_cast<int>(sizeof(pad)); |
| 216 | while (count > 0) { |
| 217 | int avail = count; |
| 218 | if (avail > pad_size) { |
| 219 | avail = pad_size; |
| 220 | } |
| 221 | o.Send(pad, avail); |
| 222 | count -= avail; |
| 223 | } |
| 224 | } |
| 225 | |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 226 | /* Perform formatted output to an output target 'o' */ |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 227 | template <typename Out> |
| 228 | static void out_vformat(Out& o, const char* format, va_list args) { |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 229 | int nn = 0; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 230 | |
| 231 | for (;;) { |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 232 | int mm; |
| 233 | int padZero = 0; |
| 234 | int padLeft = 0; |
| 235 | char sign = '\0'; |
| 236 | int width = -1; |
| 237 | int prec = -1; |
| 238 | size_t bytelen = sizeof(int); |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 239 | int slen; |
| 240 | char buffer[32]; /* temporary buffer used to format numbers */ |
| 241 | |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 242 | char c; |
| 243 | |
| 244 | /* first, find all characters that are not 0 or '%' */ |
| 245 | /* then send them to the output directly */ |
| 246 | mm = nn; |
| 247 | do { |
| 248 | c = format[mm]; |
| 249 | if (c == '\0' || c == '%') |
| 250 | break; |
| 251 | mm++; |
| 252 | } while (1); |
| 253 | |
| 254 | if (mm > nn) { |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 255 | o.Send(format+nn, mm-nn); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 256 | nn = mm; |
| 257 | } |
| 258 | |
| 259 | /* is this it ? then exit */ |
| 260 | if (c == '\0') |
| 261 | break; |
| 262 | |
| 263 | /* nope, we are at a '%' modifier */ |
| 264 | nn++; // skip it |
| 265 | |
| 266 | /* parse flags */ |
| 267 | for (;;) { |
| 268 | c = format[nn++]; |
| 269 | if (c == '\0') { /* single trailing '%' ? */ |
| 270 | c = '%'; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 271 | o.Send(&c, 1); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | else if (c == '0') { |
| 275 | padZero = 1; |
| 276 | continue; |
| 277 | } |
| 278 | else if (c == '-') { |
| 279 | padLeft = 1; |
| 280 | continue; |
| 281 | } |
| 282 | else if (c == ' ' || c == '+') { |
| 283 | sign = c; |
| 284 | continue; |
| 285 | } |
| 286 | break; |
| 287 | } |
| 288 | |
| 289 | /* parse field width */ |
| 290 | if ((c >= '0' && c <= '9')) { |
| 291 | nn --; |
| 292 | width = (int)parse_decimal(format, &nn); |
| 293 | c = format[nn++]; |
| 294 | } |
| 295 | |
| 296 | /* parse precision */ |
| 297 | if (c == '.') { |
| 298 | prec = (int)parse_decimal(format, &nn); |
| 299 | c = format[nn++]; |
| 300 | } |
| 301 | |
| 302 | /* length modifier */ |
| 303 | switch (c) { |
| 304 | case 'h': |
| 305 | bytelen = sizeof(short); |
| 306 | if (format[nn] == 'h') { |
| 307 | bytelen = sizeof(char); |
| 308 | nn += 1; |
| 309 | } |
| 310 | c = format[nn++]; |
| 311 | break; |
| 312 | case 'l': |
| 313 | bytelen = sizeof(long); |
| 314 | if (format[nn] == 'l') { |
| 315 | bytelen = sizeof(long long); |
| 316 | nn += 1; |
| 317 | } |
| 318 | c = format[nn++]; |
| 319 | break; |
| 320 | case 'z': |
| 321 | bytelen = sizeof(size_t); |
| 322 | c = format[nn++]; |
| 323 | break; |
| 324 | case 't': |
| 325 | bytelen = sizeof(ptrdiff_t); |
| 326 | c = format[nn++]; |
| 327 | break; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 328 | default: |
| 329 | ; |
| 330 | } |
| 331 | |
| 332 | /* conversion specifier */ |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 333 | const char* str = buffer; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 334 | if (c == 's') { |
| 335 | /* string */ |
| 336 | str = va_arg(args, const char*); |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 337 | if (str == NULL) { |
| 338 | str = "(null)"; |
| 339 | } |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 340 | } else if (c == 'c') { |
| 341 | /* character */ |
| 342 | /* NOTE: char is promoted to int when passed through the stack */ |
| 343 | buffer[0] = (char) va_arg(args, int); |
| 344 | buffer[1] = '\0'; |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 345 | } else if (c == 'p') { |
Andy McFadden | ec92af8 | 2011-07-29 12:46:34 -0700 | [diff] [blame] | 346 | uint64_t value = (uintptr_t) va_arg(args, void*); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 347 | buffer[0] = '0'; |
| 348 | buffer[1] = 'x'; |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 349 | format_integer(buffer + 2, sizeof(buffer) - 2, value, 'x'); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 350 | } 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] | 351 | /* integers - first read value from stack */ |
| 352 | uint64_t value; |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 353 | int is_signed = (c == 'd' || c == 'i' || c == 'o'); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 354 | |
| 355 | /* NOTE: int8_t and int16_t are promoted to int when passed |
| 356 | * through the stack |
| 357 | */ |
| 358 | switch (bytelen) { |
| 359 | case 1: value = (uint8_t) va_arg(args, int); break; |
| 360 | case 2: value = (uint16_t) va_arg(args, int); break; |
| 361 | case 4: value = va_arg(args, uint32_t); break; |
| 362 | case 8: value = va_arg(args, uint64_t); break; |
| 363 | default: return; /* should not happen */ |
| 364 | } |
| 365 | |
| 366 | /* sign extension, if needed */ |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 367 | if (is_signed) { |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 368 | int shift = 64 - 8*bytelen; |
| 369 | value = (uint64_t)(((int64_t)(value << shift)) >> shift); |
| 370 | } |
| 371 | |
| 372 | /* format the number properly into our buffer */ |
Elliott Hughes | 41b3179 | 2013-01-28 10:36:31 -0800 | [diff] [blame] | 373 | format_integer(buffer, sizeof(buffer), value, c); |
| 374 | } else if (c == '%') { |
| 375 | buffer[0] = '%'; |
| 376 | buffer[1] = '\0'; |
| 377 | } else { |
| 378 | __assert(__FILE__, __LINE__, "conversion specifier unsupported"); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* if we are here, 'str' points to the content that must be |
| 382 | * outputted. handle padding and alignment now */ |
| 383 | |
| 384 | slen = strlen(str); |
| 385 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 386 | if (sign != '\0' || prec != -1) { |
| 387 | __assert(__FILE__, __LINE__, "sign/precision unsupported"); |
| 388 | } |
| 389 | |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 390 | if (slen < width && !padLeft) { |
| 391 | char padChar = padZero ? '0' : ' '; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 392 | SendRepeat(o, padChar, width - slen); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 395 | o.Send(str, slen); |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 396 | |
| 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 | } |
| 402 | } |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 403 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 404 | int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) { |
| 405 | BufferOutputStream os(buffer, buffer_size); |
| 406 | va_list args; |
| 407 | va_start(args, format); |
| 408 | out_vformat(os, format, args); |
| 409 | va_end(args); |
| 410 | return os.total; |
| 411 | } |
| 412 | |
| 413 | int __libc_format_fd(int fd, const char* format, ...) { |
| 414 | FdOutputStream os(fd); |
| 415 | va_list args; |
| 416 | va_start(args, format); |
| 417 | out_vformat(os, format, args); |
| 418 | va_end(args); |
| 419 | return os.total; |
| 420 | } |
| 421 | |
| 422 | static int __libc_write_log(int priority, const char* tag, const char* msg) { |
Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame^] | 423 | 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] | 424 | if (main_log_fd == -1) { |
Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame^] | 425 | return -1; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | iovec vec[3]; |
| 429 | vec[0].iov_base = &priority; |
| 430 | vec[0].iov_len = 1; |
| 431 | vec[1].iov_base = const_cast<char*>(tag); |
| 432 | vec[1].iov_len = strlen(tag) + 1; |
| 433 | vec[2].iov_base = const_cast<char*>(msg); |
| 434 | vec[2].iov_len = strlen(msg) + 1; |
| 435 | |
Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame^] | 436 | int result = TEMP_FAILURE_RETRY(writev(main_log_fd, vec, 3)); |
| 437 | close(main_log_fd); |
| 438 | return result; |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | int __libc_format_log_va_list(int priority, const char* tag, const char* format, va_list args) { |
| 442 | char buffer[1024]; |
| 443 | BufferOutputStream os(buffer, sizeof(buffer)); |
| 444 | out_vformat(os, format, args); |
| 445 | return __libc_write_log(priority, tag, buffer); |
| 446 | } |
| 447 | |
| 448 | int __libc_format_log(int priority, const char* tag, const char* format, ...) { |
| 449 | va_list args; |
| 450 | va_start(args, format); |
| 451 | int result = __libc_format_log_va_list(priority, tag, format, args); |
| 452 | va_end(args); |
| 453 | return result; |
| 454 | } |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 455 | |
| 456 | static int __libc_android_log_event(int32_t tag, char type, const void* payload, size_t len) { |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 457 | iovec vec[3]; |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 458 | vec[0].iov_base = &tag; |
| 459 | vec[0].iov_len = sizeof(tag); |
| 460 | vec[1].iov_base = &type; |
| 461 | vec[1].iov_len = sizeof(type); |
| 462 | vec[2].iov_base = const_cast<void*>(payload); |
| 463 | vec[2].iov_len = len; |
| 464 | |
Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame^] | 465 | int event_log_fd = TEMP_FAILURE_RETRY(open("/dev/log/events", O_CLOEXEC | O_WRONLY)); |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 466 | if (event_log_fd == -1) { |
Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame^] | 467 | return -1; |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 468 | } |
Nick Kralevich | 17fc25d | 2013-06-21 13:28:42 -0700 | [diff] [blame^] | 469 | int result = TEMP_FAILURE_RETRY(writev(event_log_fd, vec, 3)); |
| 470 | close(event_log_fd); |
| 471 | return result; |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | void __libc_android_log_event_int(int32_t tag, int value) { |
| 475 | __libc_android_log_event(tag, EVENT_TYPE_INT, &value, sizeof(value)); |
| 476 | } |
| 477 | |
| 478 | void __libc_android_log_event_uid(int32_t tag) { |
| 479 | __libc_android_log_event_int(tag, getuid()); |
| 480 | } |
| 481 | |
| 482 | void __fortify_chk_fail(const char *msg, uint32_t tag) { |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 483 | if (tag != 0) { |
| 484 | __libc_android_log_event_uid(tag); |
| 485 | } |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 486 | __libc_fatal("FORTIFY_SOURCE: %s. Calling abort().", msg); |
| 487 | } |
| 488 | |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 489 | static void __libc_fatal(const char* format, va_list args) { |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 490 | char msg[1024]; |
| 491 | BufferOutputStream os(msg, sizeof(msg)); |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 492 | out_vformat(os, format, args); |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 493 | |
| 494 | // TODO: log to stderr for the benefit of "adb shell" users. |
| 495 | |
| 496 | // Log to the log for the benefit of regular app developers (whose stdout and stderr are closed). |
| 497 | __libc_write_log(ANDROID_LOG_FATAL, "libc", msg); |
| 498 | |
| 499 | __libc_set_abort_message(msg); |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 500 | } |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 501 | |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 502 | void __libc_fatal_no_abort(const char* format, ...) { |
| 503 | va_list args; |
| 504 | va_start(args, format); |
| 505 | __libc_fatal(format, args); |
| 506 | va_end(args); |
| 507 | } |
| 508 | |
| 509 | void __libc_fatal(const char* format, ...) { |
| 510 | va_list args; |
| 511 | va_start(args, format); |
| 512 | __libc_fatal(format, args); |
| 513 | va_end(args); |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 514 | abort(); |
| 515 | } |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 516 | |
| 517 | void __libc_set_abort_message(const char* msg) { |
| 518 | size_t size = sizeof(abort_msg_t) + strlen(msg) + 1; |
| 519 | void* map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); |
| 520 | if (map == MAP_FAILED) { |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (__abort_message_ptr != NULL) { |
| 525 | ScopedPthreadMutexLocker locker(&gAbortMsgLock); |
| 526 | if (*__abort_message_ptr != NULL) { |
| 527 | munmap(*__abort_message_ptr, (*__abort_message_ptr)->size); |
| 528 | } |
| 529 | abort_msg_t* new_abort_message = reinterpret_cast<abort_msg_t*>(map); |
| 530 | new_abort_message->size = size; |
| 531 | strcpy(new_abort_message->msg, msg); |
| 532 | *__abort_message_ptr = new_abort_message; |
| 533 | } |
| 534 | } |