Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <inttypes.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
Mark Salyzyn | 472245d | 2016-11-18 10:45:45 -0800 | [diff] [blame] | 25 | #include <log/log_event_list.h> |
Mark Salyzyn | 6debf98 | 2016-10-05 08:13:56 -0700 | [diff] [blame] | 26 | #include <private/android_logger.h> |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 27 | |
| 28 | #define MAX_EVENT_PAYLOAD (LOGGER_ENTRY_MAX_PAYLOAD - sizeof(int32_t)) |
| 29 | |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 30 | enum ReadWriteFlag { |
| 31 | kAndroidLoggerRead = 1, |
| 32 | kAndroidLoggerWrite = 2, |
| 33 | }; |
| 34 | |
| 35 | struct android_log_context_internal { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 36 | uint32_t tag; |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 37 | unsigned pos; /* Read/write position into buffer */ |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 38 | unsigned count[ANDROID_MAX_LIST_NEST_DEPTH + 1]; /* Number of elements */ |
| 39 | unsigned list[ANDROID_MAX_LIST_NEST_DEPTH + 1]; /* pos for list counter */ |
| 40 | unsigned list_nest_depth; |
| 41 | unsigned len; /* Length or raw buffer. */ |
| 42 | bool overflow; |
| 43 | bool list_stop; /* next call decrement list_nest_depth and issue a stop */ |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 44 | ReadWriteFlag read_write_flag; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 45 | uint8_t storage[LOGGER_ENTRY_MAX_PAYLOAD]; |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 48 | static void init_context(android_log_context_internal* context, uint32_t tag) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 49 | context->tag = tag; |
| 50 | context->read_write_flag = kAndroidLoggerWrite; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 51 | size_t needed = sizeof(android_event_list_t); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 52 | if ((context->pos + needed) > MAX_EVENT_PAYLOAD) { |
| 53 | context->overflow = true; |
| 54 | } |
| 55 | /* Everything is a list */ |
| 56 | context->storage[context->pos + 0] = EVENT_TYPE_LIST; |
| 57 | context->list[0] = context->pos + 1; |
| 58 | context->pos += needed; |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 61 | static void init_parser_context(android_log_context_internal* context, const char* msg, |
| 62 | size_t len) { |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 63 | len = (len <= MAX_EVENT_PAYLOAD) ? len : MAX_EVENT_PAYLOAD; |
| 64 | context->len = len; |
| 65 | memcpy(context->storage, msg, len); |
| 66 | context->read_write_flag = kAndroidLoggerRead; |
| 67 | } |
| 68 | |
Tom Cherry | 2d9779e | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 69 | android_log_context create_android_logger(uint32_t tag) { |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 70 | android_log_context_internal* context; |
| 71 | |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 72 | context = |
| 73 | static_cast<android_log_context_internal*>(calloc(1, sizeof(android_log_context_internal))); |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 74 | if (!context) { |
| 75 | return NULL; |
| 76 | } |
| 77 | init_context(context, tag); |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 78 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 79 | return (android_log_context)context; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Tom Cherry | 2d9779e | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 82 | android_log_context create_android_log_parser(const char* msg, size_t len) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 83 | android_log_context_internal* context; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 84 | |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 85 | context = |
| 86 | static_cast<android_log_context_internal*>(calloc(1, sizeof(android_log_context_internal))); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 87 | if (!context) { |
| 88 | return NULL; |
| 89 | } |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 90 | init_parser_context(context, msg, len); |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 91 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 92 | return (android_log_context)context; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Tom Cherry | 2d9779e | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 95 | int android_log_destroy(android_log_context* ctx) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 96 | android_log_context_internal* context; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 97 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 98 | context = (android_log_context_internal*)*ctx; |
| 99 | if (!context) { |
| 100 | return -EBADF; |
| 101 | } |
| 102 | memset(context, 0, sizeof(*context)); |
| 103 | free(context); |
| 104 | *ctx = NULL; |
| 105 | return 0; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 108 | int android_log_reset(android_log_context context) { |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 109 | uint32_t tag; |
| 110 | |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 111 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 112 | return -EBADF; |
| 113 | } |
| 114 | |
| 115 | tag = context->tag; |
| 116 | memset(context, 0, sizeof(*context)); |
| 117 | init_context(context, tag); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 122 | int android_log_parser_reset(android_log_context context, const char* msg, size_t len) { |
Suren Baghdasaryan | bb20b9e | 2018-07-27 12:47:26 -0700 | [diff] [blame] | 123 | if (!context || (kAndroidLoggerRead != context->read_write_flag)) { |
| 124 | return -EBADF; |
| 125 | } |
| 126 | |
| 127 | memset(context, 0, sizeof(*context)); |
| 128 | init_parser_context(context, msg, len); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 133 | int android_log_write_list_begin(android_log_context context) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 134 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 135 | return -EBADF; |
| 136 | } |
| 137 | if (context->list_nest_depth > ANDROID_MAX_LIST_NEST_DEPTH) { |
| 138 | context->overflow = true; |
| 139 | return -EOVERFLOW; |
| 140 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 141 | size_t needed = sizeof(android_event_list_t); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 142 | if ((context->pos + needed) > MAX_EVENT_PAYLOAD) { |
| 143 | context->overflow = true; |
| 144 | return -EIO; |
| 145 | } |
| 146 | context->count[context->list_nest_depth]++; |
| 147 | context->list_nest_depth++; |
| 148 | if (context->list_nest_depth > ANDROID_MAX_LIST_NEST_DEPTH) { |
| 149 | context->overflow = true; |
| 150 | return -EOVERFLOW; |
| 151 | } |
| 152 | if (context->overflow) { |
| 153 | return -EIO; |
| 154 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 155 | auto* event_list = reinterpret_cast<android_event_list_t*>(&context->storage[context->pos]); |
| 156 | event_list->type = EVENT_TYPE_LIST; |
| 157 | event_list->element_count = 0; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 158 | context->list[context->list_nest_depth] = context->pos + 1; |
| 159 | context->count[context->list_nest_depth] = 0; |
| 160 | context->pos += needed; |
| 161 | return 0; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 164 | int android_log_write_int32(android_log_context context, int32_t value) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 165 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 166 | return -EBADF; |
| 167 | } |
| 168 | if (context->overflow) { |
| 169 | return -EIO; |
| 170 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 171 | size_t needed = sizeof(android_event_int_t); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 172 | if ((context->pos + needed) > MAX_EVENT_PAYLOAD) { |
| 173 | context->overflow = true; |
| 174 | return -EIO; |
| 175 | } |
| 176 | context->count[context->list_nest_depth]++; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 177 | auto* event_int = reinterpret_cast<android_event_int_t*>(&context->storage[context->pos]); |
| 178 | event_int->type = EVENT_TYPE_INT; |
| 179 | event_int->data = value; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 180 | context->pos += needed; |
| 181 | return 0; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 184 | int android_log_write_int64(android_log_context context, int64_t value) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 185 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 186 | return -EBADF; |
| 187 | } |
| 188 | if (context->overflow) { |
| 189 | return -EIO; |
| 190 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 191 | size_t needed = sizeof(android_event_long_t); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 192 | if ((context->pos + needed) > MAX_EVENT_PAYLOAD) { |
| 193 | context->overflow = true; |
| 194 | return -EIO; |
| 195 | } |
| 196 | context->count[context->list_nest_depth]++; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 197 | auto* event_long = reinterpret_cast<android_event_long_t*>(&context->storage[context->pos]); |
| 198 | event_long->type = EVENT_TYPE_LONG; |
| 199 | event_long->data = value; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 200 | context->pos += needed; |
| 201 | return 0; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 204 | int android_log_write_string8_len(android_log_context context, const char* value, size_t maxlen) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 205 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 206 | return -EBADF; |
| 207 | } |
| 208 | if (context->overflow) { |
| 209 | return -EIO; |
| 210 | } |
| 211 | if (!value) { |
| 212 | value = ""; |
| 213 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 214 | int32_t len = strnlen(value, maxlen); |
| 215 | size_t needed = sizeof(android_event_string_t) + len; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 216 | if ((context->pos + needed) > MAX_EVENT_PAYLOAD) { |
| 217 | /* Truncate string for delivery */ |
| 218 | len = MAX_EVENT_PAYLOAD - context->pos - 1 - sizeof(int32_t); |
| 219 | if (len <= 0) { |
| 220 | context->overflow = true; |
| 221 | return -EIO; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 222 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 223 | } |
| 224 | context->count[context->list_nest_depth]++; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 225 | auto* event_string = reinterpret_cast<android_event_string_t*>(&context->storage[context->pos]); |
| 226 | event_string->type = EVENT_TYPE_STRING; |
| 227 | event_string->length = len; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 228 | if (len) { |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 229 | memcpy(&event_string->data, value, len); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 230 | } |
| 231 | context->pos += needed; |
| 232 | return len; |
Mark Salyzyn | 1d5afc9 | 2016-02-25 08:41:25 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Tom Cherry | 2d9779e | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 235 | int android_log_write_string8(android_log_context ctx, const char* value) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 236 | return android_log_write_string8_len(ctx, value, MAX_EVENT_PAYLOAD); |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 239 | int android_log_write_float32(android_log_context context, float value) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 240 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 241 | return -EBADF; |
| 242 | } |
| 243 | if (context->overflow) { |
| 244 | return -EIO; |
| 245 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 246 | size_t needed = sizeof(android_event_float_t); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 247 | if ((context->pos + needed) > MAX_EVENT_PAYLOAD) { |
| 248 | context->overflow = true; |
| 249 | return -EIO; |
| 250 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 251 | context->count[context->list_nest_depth]++; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 252 | auto* event_float = reinterpret_cast<android_event_float_t*>(&context->storage[context->pos]); |
| 253 | event_float->type = EVENT_TYPE_FLOAT; |
| 254 | event_float->data = value; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 255 | context->pos += needed; |
| 256 | return 0; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 259 | int android_log_write_list_end(android_log_context context) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 260 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 261 | return -EBADF; |
| 262 | } |
| 263 | if (context->list_nest_depth > ANDROID_MAX_LIST_NEST_DEPTH) { |
| 264 | context->overflow = true; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 265 | context->list_nest_depth--; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 266 | return -EOVERFLOW; |
| 267 | } |
| 268 | if (!context->list_nest_depth) { |
| 269 | context->overflow = true; |
| 270 | return -EOVERFLOW; |
| 271 | } |
| 272 | if (context->list[context->list_nest_depth] <= 0) { |
| 273 | context->list_nest_depth--; |
| 274 | context->overflow = true; |
| 275 | return -EOVERFLOW; |
| 276 | } |
| 277 | context->storage[context->list[context->list_nest_depth]] = |
| 278 | context->count[context->list_nest_depth]; |
| 279 | context->list_nest_depth--; |
| 280 | return 0; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Logs the list of elements to the event log. |
| 285 | */ |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 286 | int android_log_write_list(android_log_context context, log_id_t id) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 287 | const char* msg; |
| 288 | ssize_t len; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 289 | |
Stefan Lafon | 701a065 | 2017-08-24 20:14:06 -0700 | [diff] [blame] | 290 | if ((id != LOG_ID_EVENTS) && (id != LOG_ID_SECURITY) && (id != LOG_ID_STATS)) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 291 | return -EINVAL; |
| 292 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 293 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 294 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 295 | return -EBADF; |
| 296 | } |
| 297 | if (context->list_nest_depth) { |
| 298 | return -EIO; |
| 299 | } |
| 300 | /* NB: if there was overflow, then log is truncated. Nothing reported */ |
| 301 | context->storage[1] = context->count[0]; |
| 302 | len = context->len = context->pos; |
| 303 | msg = (const char*)context->storage; |
| 304 | /* it's not a list */ |
| 305 | if (context->count[0] <= 1) { |
| 306 | len -= sizeof(uint8_t) + sizeof(uint8_t); |
| 307 | if (len < 0) { |
| 308 | len = 0; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 309 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 310 | msg += sizeof(uint8_t) + sizeof(uint8_t); |
| 311 | } |
| 312 | return (id == LOG_ID_EVENTS) |
| 313 | ? __android_log_bwrite(context->tag, msg, len) |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 314 | : ((id == LOG_ID_STATS) ? __android_log_stats_bwrite(context->tag, msg, len) |
| 315 | : __android_log_security_bwrite(context->tag, msg, len)); |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 318 | int android_log_write_list_buffer(android_log_context context, const char** buffer) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 319 | const char* msg; |
| 320 | ssize_t len; |
Mark Salyzyn | 2ca4f49 | 2016-09-12 11:54:57 -0700 | [diff] [blame] | 321 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 322 | if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { |
| 323 | return -EBADF; |
| 324 | } |
| 325 | if (context->list_nest_depth) { |
| 326 | return -EIO; |
| 327 | } |
| 328 | if (buffer == NULL) { |
| 329 | return -EFAULT; |
| 330 | } |
| 331 | /* NB: if there was overflow, then log is truncated. Nothing reported */ |
| 332 | context->storage[1] = context->count[0]; |
| 333 | len = context->len = context->pos; |
| 334 | msg = (const char*)context->storage; |
| 335 | /* it's not a list */ |
| 336 | if (context->count[0] <= 1) { |
| 337 | len -= sizeof(uint8_t) + sizeof(uint8_t); |
| 338 | if (len < 0) { |
| 339 | len = 0; |
Mark Salyzyn | 2ca4f49 | 2016-09-12 11:54:57 -0700 | [diff] [blame] | 340 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 341 | msg += sizeof(uint8_t) + sizeof(uint8_t); |
| 342 | } |
| 343 | *buffer = msg; |
| 344 | return len; |
Mark Salyzyn | 2ca4f49 | 2016-09-12 11:54:57 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 347 | /* |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 348 | * Gets the next element. Parsing errors result in an EVENT_TYPE_UNKNOWN type. |
| 349 | * If there is nothing to process, the complete field is set to non-zero. If |
| 350 | * an EVENT_TYPE_UNKNOWN type is returned once, and the caller does not check |
| 351 | * this and continues to call this function, the behavior is undefined |
| 352 | * (although it won't crash). |
| 353 | */ |
Tom Cherry | 828db1a | 2019-11-14 10:39:40 -0800 | [diff] [blame] | 354 | static android_log_list_element android_log_read_next_internal(android_log_context context, |
| 355 | int peek) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 356 | android_log_list_element elem; |
| 357 | unsigned pos; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 358 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 359 | memset(&elem, 0, sizeof(elem)); |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 360 | |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 361 | /* Nothing to parse from this context, so return complete. */ |
| 362 | if (!context || (kAndroidLoggerRead != context->read_write_flag) || |
| 363 | (context->list_nest_depth > ANDROID_MAX_LIST_NEST_DEPTH) || |
| 364 | (context->count[context->list_nest_depth] >= |
| 365 | (MAX_EVENT_PAYLOAD / (sizeof(uint8_t) + sizeof(uint8_t))))) { |
| 366 | elem.type = EVENT_TYPE_UNKNOWN; |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 367 | if (context && |
| 368 | (context->list_stop || ((context->list_nest_depth <= ANDROID_MAX_LIST_NEST_DEPTH) && |
| 369 | !context->count[context->list_nest_depth]))) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 370 | elem.type = EVENT_TYPE_LIST_STOP; |
| 371 | } |
| 372 | elem.complete = true; |
| 373 | return elem; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Use a different variable to update the position in case this |
| 378 | * operation is a "peek". |
| 379 | */ |
| 380 | pos = context->pos; |
| 381 | if (context->list_stop) { |
| 382 | elem.type = EVENT_TYPE_LIST_STOP; |
Tom Cherry | 71ba164 | 2019-01-10 10:37:36 -0800 | [diff] [blame] | 383 | elem.complete = !context->count[0] && (!context->list_nest_depth || |
| 384 | ((context->list_nest_depth == 1) && !context->count[1])); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 385 | if (!peek) { |
| 386 | /* Suck in superfluous stop */ |
| 387 | if (context->storage[pos] == EVENT_TYPE_LIST_STOP) { |
| 388 | context->pos = pos + 1; |
| 389 | } |
| 390 | if (context->list_nest_depth) { |
| 391 | --context->list_nest_depth; |
| 392 | if (context->count[context->list_nest_depth]) { |
| 393 | context->list_stop = false; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 394 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 395 | } else { |
| 396 | context->list_stop = false; |
| 397 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 398 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 399 | return elem; |
| 400 | } |
| 401 | if ((pos + 1) > context->len) { |
| 402 | elem.type = EVENT_TYPE_UNKNOWN; |
| 403 | elem.complete = true; |
| 404 | return elem; |
| 405 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 406 | |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 407 | elem.type = static_cast<AndroidEventLogType>(context->storage[pos]); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 408 | switch ((int)elem.type) { |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 409 | case EVENT_TYPE_FLOAT: |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 410 | /* Rely on union to translate elem.data.int32 into elem.data.float32 */ |
| 411 | /* FALLTHRU */ |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 412 | case EVENT_TYPE_INT: { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 413 | elem.len = sizeof(int32_t); |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 414 | if ((pos + sizeof(android_event_int_t)) > context->len) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 415 | elem.type = EVENT_TYPE_UNKNOWN; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 416 | return elem; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 417 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 418 | |
| 419 | auto* event_int = reinterpret_cast<android_event_int_t*>(&context->storage[pos]); |
| 420 | pos += sizeof(android_event_int_t); |
| 421 | elem.data.int32 = event_int->data; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 422 | /* common tangeable object suffix */ |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 423 | elem.complete = !context->list_nest_depth && !context->count[0]; |
| 424 | if (!peek) { |
| 425 | if (!context->count[context->list_nest_depth] || |
| 426 | !--(context->count[context->list_nest_depth])) { |
| 427 | context->list_stop = true; |
| 428 | } |
| 429 | context->pos = pos; |
| 430 | } |
| 431 | return elem; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 432 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 433 | |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 434 | case EVENT_TYPE_LONG: { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 435 | elem.len = sizeof(int64_t); |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 436 | if ((pos + sizeof(android_event_long_t)) > context->len) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 437 | elem.type = EVENT_TYPE_UNKNOWN; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 438 | return elem; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 439 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 440 | |
| 441 | auto* event_long = reinterpret_cast<android_event_long_t*>(&context->storage[pos]); |
| 442 | pos += sizeof(android_event_long_t); |
| 443 | elem.data.int64 = event_long->data; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 444 | /* common tangeable object suffix */ |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 445 | elem.complete = !context->list_nest_depth && !context->count[0]; |
| 446 | if (!peek) { |
| 447 | if (!context->count[context->list_nest_depth] || |
| 448 | !--(context->count[context->list_nest_depth])) { |
| 449 | context->list_stop = true; |
| 450 | } |
| 451 | context->pos = pos; |
| 452 | } |
| 453 | return elem; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 454 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 455 | |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 456 | case EVENT_TYPE_STRING: { |
| 457 | if ((pos + sizeof(android_event_string_t)) > context->len) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 458 | elem.type = EVENT_TYPE_UNKNOWN; |
| 459 | elem.complete = true; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 460 | return elem; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 461 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 462 | auto* event_string = reinterpret_cast<android_event_string_t*>(&context->storage[pos]); |
| 463 | pos += sizeof(android_event_string_t); |
| 464 | // Wire format is int32_t, but elem.len is uint16_t... |
| 465 | if (event_string->length >= UINT16_MAX) { |
| 466 | elem.type = EVENT_TYPE_UNKNOWN; |
| 467 | return elem; |
| 468 | } |
| 469 | elem.len = event_string->length; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 470 | if ((pos + elem.len) > context->len) { |
| 471 | elem.len = context->len - pos; /* truncate string */ |
| 472 | elem.complete = true; |
| 473 | if (!elem.len) { |
| 474 | elem.type = EVENT_TYPE_UNKNOWN; |
| 475 | return elem; |
| 476 | } |
| 477 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 478 | elem.data.string = event_string->data; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 479 | /* common tangeable object suffix */ |
| 480 | pos += elem.len; |
| 481 | elem.complete = !context->list_nest_depth && !context->count[0]; |
| 482 | if (!peek) { |
| 483 | if (!context->count[context->list_nest_depth] || |
| 484 | !--(context->count[context->list_nest_depth])) { |
| 485 | context->list_stop = true; |
| 486 | } |
| 487 | context->pos = pos; |
| 488 | } |
| 489 | return elem; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 490 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 491 | |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 492 | case EVENT_TYPE_LIST: { |
| 493 | if ((pos + sizeof(android_event_list_t)) > context->len) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 494 | elem.type = EVENT_TYPE_UNKNOWN; |
| 495 | elem.complete = true; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 496 | return elem; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 497 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 498 | auto* event_list = reinterpret_cast<android_event_list_t*>(&context->storage[pos]); |
| 499 | pos += sizeof(android_event_list_t); |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 500 | elem.complete = context->list_nest_depth >= ANDROID_MAX_LIST_NEST_DEPTH; |
| 501 | if (peek) { |
| 502 | return elem; |
| 503 | } |
| 504 | if (context->count[context->list_nest_depth]) { |
| 505 | context->count[context->list_nest_depth]--; |
| 506 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 507 | context->list_stop = event_list->element_count == 0; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 508 | context->list_nest_depth++; |
| 509 | if (context->list_nest_depth <= ANDROID_MAX_LIST_NEST_DEPTH) { |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 510 | context->count[context->list_nest_depth] = event_list->element_count; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 511 | } |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 512 | context->pos = pos; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 513 | return elem; |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 514 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 515 | |
| 516 | case EVENT_TYPE_LIST_STOP: /* Suprise Newline terminates lists. */ |
Tom Cherry | bbbf089 | 2019-10-09 10:53:37 -0700 | [diff] [blame] | 517 | pos++; |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 518 | if (!peek) { |
| 519 | context->pos = pos; |
| 520 | } |
| 521 | elem.type = EVENT_TYPE_UNKNOWN; |
| 522 | elem.complete = !context->list_nest_depth; |
| 523 | if (context->list_nest_depth > 0) { |
| 524 | elem.type = EVENT_TYPE_LIST_STOP; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 525 | if (!peek) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 526 | context->list_nest_depth--; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 527 | } |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 528 | } |
| 529 | return elem; |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 530 | |
| 531 | default: |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 532 | elem.type = EVENT_TYPE_UNKNOWN; |
| 533 | return elem; |
| 534 | } |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 535 | } |
| 536 | |
Tom Cherry | 2d9779e | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 537 | android_log_list_element android_log_read_next(android_log_context ctx) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 538 | return android_log_read_next_internal(ctx, 0); |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 539 | } |
| 540 | |
Tom Cherry | 2d9779e | 2019-02-08 11:46:19 -0800 | [diff] [blame] | 541 | android_log_list_element android_log_peek_next(android_log_context ctx) { |
Mark Salyzyn | 2ed51d7 | 2017-03-09 08:09:43 -0800 | [diff] [blame] | 542 | return android_log_read_next_internal(ctx, 1); |
Mark Salyzyn | 9dd6510 | 2016-02-17 16:08:13 -0800 | [diff] [blame] | 543 | } |