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 | 87ff824 | 2017-04-11 15:18:38 -0700 | [diff] [blame] | 29 | // Relative paths so we can #include this .cpp file for testing. |
| 30 | #include "../private/CachedProperty.h" |
| 31 | #include "../private/libc_logging.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 32 | #include "../private/ScopedPthreadMutexLocker.h" |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 33 | |
Dan Albert | ce6b1ab | 2014-08-18 14:37:42 -0700 | [diff] [blame] | 34 | #include <android/set_abort_message.h> |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 35 | #include <assert.h> |
Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 36 | #include <ctype.h> |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 37 | #include <errno.h> |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 38 | #include <fcntl.h> |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 39 | #include <pthread.h> |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 40 | #include <stdarg.h> |
| 41 | #include <stddef.h> |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 42 | #include <stdlib.h> |
Elliott Hughes | eababde | 2012-12-20 18:59:05 -0800 | [diff] [blame] | 43 | #include <string.h> |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 44 | #include <sys/mman.h> |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 45 | #include <sys/socket.h> |
| 46 | #include <sys/types.h> |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 47 | #include <sys/uio.h> |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 48 | #include <sys/un.h> |
| 49 | #include <time.h> |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 50 | #include <unistd.h> |
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, |
Jeff Brown | 11331f6 | 2015-04-28 14:35:45 -0700 | [diff] [blame] | 58 | EVENT_TYPE_FLOAT = 4, |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 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: |
Chih-Hung Hsieh | 62e3a07 | 2016-05-03 12:08:05 -0700 | [diff] [blame] | 105 | explicit FdOutputStream(int fd) : total(0), fd_(fd) { |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 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 | |
Josh Gao | 273991c | 2017-02-15 11:46:55 -0800 | [diff] [blame] | 420 | int __libc_format_buffer_va_list(char* buffer, size_t buffer_size, const char* format, |
| 421 | va_list args) { |
| 422 | BufferOutputStream os(buffer, buffer_size); |
| 423 | out_vformat(os, format, args); |
| 424 | return os.total; |
| 425 | } |
| 426 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 427 | int __libc_format_fd(int fd, const char* format, ...) { |
| 428 | FdOutputStream os(fd); |
| 429 | va_list args; |
| 430 | va_start(args, format); |
| 431 | out_vformat(os, format, args); |
| 432 | va_end(args); |
| 433 | return os.total; |
| 434 | } |
| 435 | |
Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 436 | static int __libc_write_stderr(const char* tag, const char* msg) { |
Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 437 | iovec vec[4]; |
| 438 | vec[0].iov_base = const_cast<char*>(tag); |
| 439 | vec[0].iov_len = strlen(tag); |
| 440 | vec[1].iov_base = const_cast<char*>(": "); |
| 441 | vec[1].iov_len = 2; |
| 442 | vec[2].iov_base = const_cast<char*>(msg); |
Elliott Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 443 | vec[2].iov_len = strlen(msg); |
Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 444 | vec[3].iov_base = const_cast<char*>("\n"); |
| 445 | vec[3].iov_len = 1; |
| 446 | |
Josh Gao | 59bde2e | 2016-10-07 13:21:03 -0700 | [diff] [blame] | 447 | int result = TEMP_FAILURE_RETRY(writev(STDERR_FILENO, vec, 4)); |
Elliott Hughes | 0f395b7 | 2013-10-08 13:19:00 -0700 | [diff] [blame] | 448 | return result; |
| 449 | } |
| 450 | |
Elliott Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 451 | static int __libc_open_log_socket() { |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 452 | // ToDo: Ideally we want this to fail if the gid of the current |
| 453 | // process is AID_LOGD, but will have to wait until we have |
| 454 | // registered this in private/android_filesystem_config.h. We have |
| 455 | // found that all logd crashes thus far have had no problem stuffing |
| 456 | // the UNIX domain socket and moving on so not critical *today*. |
| 457 | |
Elliott Hughes | 0f67d5f | 2016-02-27 19:18:41 -0800 | [diff] [blame] | 458 | int log_fd = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)); |
| 459 | if (log_fd == -1) { |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 460 | return -1; |
| 461 | } |
| 462 | |
| 463 | union { |
| 464 | struct sockaddr addr; |
| 465 | struct sockaddr_un addrUn; |
| 466 | } u; |
| 467 | memset(&u, 0, sizeof(u)); |
| 468 | u.addrUn.sun_family = AF_UNIX; |
| 469 | strlcpy(u.addrUn.sun_path, "/dev/socket/logdw", sizeof(u.addrUn.sun_path)); |
| 470 | |
| 471 | if (TEMP_FAILURE_RETRY(connect(log_fd, &u.addr, sizeof(u.addrUn))) != 0) { |
| 472 | close(log_fd); |
| 473 | return -1; |
| 474 | } |
| 475 | |
| 476 | return log_fd; |
| 477 | } |
Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 478 | |
Elliott Hughes | 87ff824 | 2017-04-11 15:18:38 -0700 | [diff] [blame] | 479 | static clockid_t __android_log_clockid() { |
| 480 | static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 481 | ScopedPthreadMutexLocker locker(&mutex); |
Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 482 | |
Elliott Hughes | 87ff824 | 2017-04-11 15:18:38 -0700 | [diff] [blame] | 483 | static CachedProperty ro_logd_timestamp("ro.logd.timestamp"); |
| 484 | static CachedProperty persist_logd_timestamp("persist.logd.timestamp"); |
Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 485 | |
Elliott Hughes | 87ff824 | 2017-04-11 15:18:38 -0700 | [diff] [blame] | 486 | char ch = persist_logd_timestamp.Get()[0]; |
| 487 | if (ch == '\0') ch = ro_logd_timestamp.Get()[0]; |
Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 488 | |
Elliott Hughes | 87ff824 | 2017-04-11 15:18:38 -0700 | [diff] [blame] | 489 | return (tolower(ch) == 'm') ? CLOCK_MONOTONIC : CLOCK_REALTIME; |
Mark Salyzyn | 870f165 | 2015-11-30 16:23:15 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 492 | struct log_time { // Wire format |
| 493 | uint32_t tv_sec; |
| 494 | uint32_t tv_nsec; |
| 495 | }; |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 496 | |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 497 | int __libc_write_log(int priority, const char* tag, const char* msg) { |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 498 | int main_log_fd = __libc_open_log_socket(); |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 499 | if (main_log_fd == -1) { |
| 500 | // Try stderr instead. |
| 501 | return __libc_write_stderr(tag, msg); |
| 502 | } |
| 503 | |
Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 504 | iovec vec[6]; |
Elliott Hughes | 0111019 | 2014-05-07 16:35:59 -0700 | [diff] [blame] | 505 | 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] | 506 | vec[0].iov_base = &log_id; |
| 507 | vec[0].iov_len = sizeof(log_id); |
Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 508 | uint16_t tid = gettid(); |
| 509 | vec[1].iov_base = &tid; |
| 510 | vec[1].iov_len = sizeof(tid); |
Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 511 | timespec ts; |
Mark Salyzyn | 9da687e | 2015-12-08 13:42:41 -0800 | [diff] [blame] | 512 | clock_gettime(__android_log_clockid(), &ts); |
Mark Salyzyn | 9fc7602 | 2014-03-05 13:44:00 -0800 | [diff] [blame] | 513 | log_time realtime_ts; |
| 514 | realtime_ts.tv_sec = ts.tv_sec; |
| 515 | realtime_ts.tv_nsec = ts.tv_nsec; |
Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 516 | vec[2].iov_base = &realtime_ts; |
| 517 | vec[2].iov_len = sizeof(realtime_ts); |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 518 | |
Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 519 | vec[3].iov_base = &priority; |
| 520 | vec[3].iov_len = 1; |
| 521 | vec[4].iov_base = const_cast<char*>(tag); |
Elliott Hughes | aba6f71 | 2015-02-05 12:02:04 -0800 | [diff] [blame] | 522 | vec[4].iov_len = strlen(tag) + 1; |
Mark Salyzyn | 8664be5 | 2014-03-20 16:07:55 -0700 | [diff] [blame] | 523 | vec[5].iov_base = const_cast<char*>(msg); |
Elliott Hughes | aba6f71 | 2015-02-05 12:02:04 -0800 | [diff] [blame] | 524 | vec[5].iov_len = strlen(msg) + 1; |
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 | |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 546 | static void __libc_fatal_va_list(const char* prefix, const char* format, va_list args) { |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 547 | char msg[1024]; |
| 548 | BufferOutputStream os(msg, sizeof(msg)); |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 549 | |
| 550 | if (prefix) { |
| 551 | os.Send(prefix, strlen(prefix)); |
| 552 | os.Send(": ", 2); |
| 553 | } |
| 554 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 555 | out_vformat(os, format, args); |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 556 | |
Elliott Hughes | 5e6cf05 | 2017-02-06 10:26:20 -0800 | [diff] [blame] | 557 | // Log to stderr for the benefit of "adb shell" users and gtests. |
Dan Albert | 97e31de | 2014-07-20 11:49:46 -0700 | [diff] [blame] | 558 | struct iovec iov[2] = { |
Elliott Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 559 | { msg, os.total }, |
| 560 | { const_cast<char*>("\n"), 1 }, |
Dan Albert | 97e31de | 2014-07-20 11:49:46 -0700 | [diff] [blame] | 561 | }; |
Elliott Hughes | 42084a2 | 2015-02-02 12:24:46 -0800 | [diff] [blame] | 562 | TEMP_FAILURE_RETRY(writev(2, iov, 2)); |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 563 | |
| 564 | // 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] | 565 | __libc_write_log(ANDROID_LOG_FATAL, "libc", msg); |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 566 | |
Dan Albert | ce6b1ab | 2014-08-18 14:37:42 -0700 | [diff] [blame] | 567 | android_set_abort_message(msg); |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 568 | } |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 569 | |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 570 | void __libc_fatal(const char* fmt, ...) { |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 571 | va_list args; |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 572 | va_start(args, fmt); |
| 573 | __libc_fatal_va_list(nullptr, fmt, args); |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 574 | va_end(args); |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 575 | abort(); |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 578 | void __fortify_fatal(const char* fmt, ...) { |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 579 | va_list args; |
Elliott Hughes | b83d674 | 2016-02-25 20:33:47 -0800 | [diff] [blame] | 580 | va_start(args, fmt); |
| 581 | __libc_fatal_va_list("FORTIFY", fmt, args); |
Elliott Hughes | 2e3b710 | 2014-04-23 14:52:49 -0700 | [diff] [blame] | 582 | va_end(args); |
| 583 | abort(); |
| 584 | } |