blob: bc056cbb66ab480ae4e560c74f3c5fbc82e99885 [file] [log] [blame]
Mark Salyzyncf4aa032013-11-22 07:54:30 -08001/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07002**
Mark Salyzyn40b21552013-12-18 12:59:01 -08003** Copyright 2006-2014, The Android Open Source Project
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07004**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080018#ifndef __MINGW32__
19#define HAVE_STRSEP
20#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070021
Mark Salyzyna04464a2014-04-30 08:50:53 -070022#include <assert.h>
23#include <ctype.h>
24#include <errno.h>
Mark Salyzyn4fd05072016-10-18 11:30:11 -070025#include <inttypes.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080026#ifndef __MINGW32__
William Roberts8a5b9ca2016-04-08 12:13:17 -070027#include <pwd.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080028#endif
Pierre Zurekead88fc2010-10-17 22:39:37 +020029#include <stdbool.h>
Mark Salyzyna04464a2014-04-30 08:50:53 -070030#include <stdint.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020034#include <sys/param.h>
William Roberts8a5b9ca2016-04-08 12:13:17 -070035#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070036
Mark Salyzyn4cbed022015-08-31 15:53:41 -070037#include <cutils/list.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070038#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070039#include <log/logprint.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070040
Mark Salyzyn018a96d2016-03-01 13:45:42 -080041#include "log_portability.h"
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080042
Mark Salyzyn4cbed022015-08-31 15:53:41 -070043#define MS_PER_NSEC 1000000
44#define US_PER_NSEC 1000
45
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080046#ifndef MIN
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080047#define MIN(a, b) (((a) < (b)) ? (a) : (b))
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080048#endif
49
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070050typedef struct FilterInfo_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080051 char* mTag;
52 android_LogPriority mPri;
53 struct FilterInfo_t* p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070054} FilterInfo;
55
56struct AndroidLogFormat_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080057 android_LogPriority global_pri;
58 FilterInfo* filters;
59 AndroidLogPrintFormat format;
60 bool colored_output;
61 bool usec_time_output;
62 bool nsec_time_output;
63 bool printable_output;
64 bool year_output;
65 bool zone_output;
66 bool epoch_output;
67 bool monotonic_output;
68 bool uid_output;
69 bool descriptive_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070070};
71
Pierre Zurekead88fc2010-10-17 22:39:37 +020072/*
Mark Salyzyn4fd05072016-10-18 11:30:11 -070073 * API issues prevent us from exposing "descriptive" in AndroidLogFormat_t
74 * during android_log_processBinaryLogBuffer(), so we break layering.
75 */
76static bool descriptive_output = false;
77
78/*
Pierre Zurekead88fc2010-10-17 22:39:37 +020079 * gnome-terminal color tags
80 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
81 * for ideas on how to set the forground color of the text for xterm.
82 * The color manipulation character stream is defined as:
83 * ESC [ 3 8 ; 5 ; <color#> m
84 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080085#define ANDROID_COLOR_BLUE 75
Pierre Zurekead88fc2010-10-17 22:39:37 +020086#define ANDROID_COLOR_DEFAULT 231
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080087#define ANDROID_COLOR_GREEN 40
88#define ANDROID_COLOR_ORANGE 166
89#define ANDROID_COLOR_RED 196
90#define ANDROID_COLOR_YELLOW 226
Pierre Zurekead88fc2010-10-17 22:39:37 +020091
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080092static FilterInfo* filterinfo_new(const char* tag, android_LogPriority pri) {
93 FilterInfo* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070094
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080095 p_ret = (FilterInfo*)calloc(1, sizeof(FilterInfo));
96 p_ret->mTag = strdup(tag);
97 p_ret->mPri = pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070098
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080099 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700100}
101
Mark Salyzyna04464a2014-04-30 08:50:53 -0700102/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700103
104/*
105 * Note: also accepts 0-9 priorities
106 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
107 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800108static android_LogPriority filterCharToPri(char c) {
109 android_LogPriority pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700110
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800111 c = tolower(c);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800113 if (c >= '0' && c <= '9') {
114 if (c >= ('0' + ANDROID_LOG_SILENT)) {
115 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800117 pri = (android_LogPriority)(c - '0');
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700118 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800119 } else if (c == 'v') {
120 pri = ANDROID_LOG_VERBOSE;
121 } else if (c == 'd') {
122 pri = ANDROID_LOG_DEBUG;
123 } else if (c == 'i') {
124 pri = ANDROID_LOG_INFO;
125 } else if (c == 'w') {
126 pri = ANDROID_LOG_WARN;
127 } else if (c == 'e') {
128 pri = ANDROID_LOG_ERROR;
129 } else if (c == 'f') {
130 pri = ANDROID_LOG_FATAL;
131 } else if (c == 's') {
132 pri = ANDROID_LOG_SILENT;
133 } else if (c == '*') {
134 pri = ANDROID_LOG_DEFAULT;
135 } else {
136 pri = ANDROID_LOG_UNKNOWN;
137 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700138
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800139 return pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700140}
141
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800142static char filterPriToChar(android_LogPriority pri) {
143 switch (pri) {
144 /* clang-format off */
145 case ANDROID_LOG_VERBOSE: return 'V';
146 case ANDROID_LOG_DEBUG: return 'D';
147 case ANDROID_LOG_INFO: return 'I';
148 case ANDROID_LOG_WARN: return 'W';
149 case ANDROID_LOG_ERROR: return 'E';
150 case ANDROID_LOG_FATAL: return 'F';
151 case ANDROID_LOG_SILENT: return 'S';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800153 case ANDROID_LOG_DEFAULT:
154 case ANDROID_LOG_UNKNOWN:
155 default: return '?';
Tom Cherry71ba1642019-01-10 10:37:36 -0800156 /* clang-format on */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800157 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700158}
159
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800160static int colorFromPri(android_LogPriority pri) {
161 switch (pri) {
162 /* clang-format off */
163 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
164 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
165 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
166 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
167 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
168 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
169 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200170
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800171 case ANDROID_LOG_DEFAULT:
172 case ANDROID_LOG_UNKNOWN:
173 default: return ANDROID_COLOR_DEFAULT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800174 /* clang-format on */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800175 }
Pierre Zurekead88fc2010-10-17 22:39:37 +0200176}
177
Tom Cherry71ba1642019-01-10 10:37:36 -0800178static android_LogPriority filterPriForTag(AndroidLogFormat* p_format, const char* tag) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800179 FilterInfo* p_curFilter;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700180
Tom Cherry71ba1642019-01-10 10:37:36 -0800181 for (p_curFilter = p_format->filters; p_curFilter != NULL; p_curFilter = p_curFilter->p_next) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800182 if (0 == strcmp(tag, p_curFilter->mTag)) {
183 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
184 return p_format->global_pri;
185 } else {
186 return p_curFilter->mPri;
187 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700188 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800189 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700190
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800191 return p_format->global_pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700192}
193
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700194/**
195 * returns 1 if this log line should be printed based on its priority
196 * and tag, and 0 if it should not
197 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800198int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag,
199 android_LogPriority pri) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800200 return pri >= filterPriForTag(p_format, tag);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700201}
202
Tom Cherry2d9779e2019-02-08 11:46:19 -0800203AndroidLogFormat* android_log_format_new() {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800204 AndroidLogFormat* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205
Tom Cherry71ba1642019-01-10 10:37:36 -0800206 p_ret = static_cast<AndroidLogFormat*>(calloc(1, sizeof(AndroidLogFormat)));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700207
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800208 p_ret->global_pri = ANDROID_LOG_VERBOSE;
209 p_ret->format = FORMAT_BRIEF;
210 p_ret->colored_output = false;
211 p_ret->usec_time_output = false;
212 p_ret->nsec_time_output = false;
213 p_ret->printable_output = false;
214 p_ret->year_output = false;
215 p_ret->zone_output = false;
216 p_ret->epoch_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800217#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800218 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800219#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800220 p_ret->monotonic_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800221#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800222 p_ret->uid_output = false;
223 p_ret->descriptive_output = false;
224 descriptive_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700225
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800226 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700227}
228
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700229static list_declare(convertHead);
230
Tom Cherry2d9779e2019-02-08 11:46:19 -0800231void android_log_format_free(AndroidLogFormat* p_format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800232 FilterInfo *p_info, *p_info_old;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700233
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800234 p_info = p_format->filters;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700235
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800236 while (p_info != NULL) {
237 p_info_old = p_info;
238 p_info = p_info->p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800240 free(p_info_old);
241 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700242
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800243 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700244
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800245 /* Free conversion resource, can always be reconstructed */
246 while (!list_empty(&convertHead)) {
247 struct listnode* node = list_head(&convertHead);
248 list_remove(node);
Ting-Yuan Huang249bd052017-08-15 17:01:33 -0700249 LOG_ALWAYS_FATAL_IF(node == list_head(&convertHead), "corrupted list");
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800250 free(node);
251 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700252}
253
Tom Cherry2d9779e2019-02-08 11:46:19 -0800254int android_log_setPrintFormat(AndroidLogFormat* p_format, AndroidLogPrintFormat format) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800255 switch (format) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700256 case FORMAT_MODIFIER_COLOR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800257 p_format->colored_output = true;
258 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700259 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800260 p_format->usec_time_output = true;
261 return 0;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800262 case FORMAT_MODIFIER_TIME_NSEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800263 p_format->nsec_time_output = true;
264 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700265 case FORMAT_MODIFIER_PRINTABLE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800266 p_format->printable_output = true;
267 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700268 case FORMAT_MODIFIER_YEAR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800269 p_format->year_output = true;
270 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700271 case FORMAT_MODIFIER_ZONE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800272 p_format->zone_output = !p_format->zone_output;
273 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700274 case FORMAT_MODIFIER_EPOCH:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800275 p_format->epoch_output = true;
276 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700277 case FORMAT_MODIFIER_MONOTONIC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800278 p_format->monotonic_output = true;
279 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800280 case FORMAT_MODIFIER_UID:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800281 p_format->uid_output = true;
282 return 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700283 case FORMAT_MODIFIER_DESCRIPT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800284 p_format->descriptive_output = true;
285 descriptive_output = true;
286 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700287 default:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800288 break;
289 }
290 p_format->format = format;
291 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700292}
293
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700294static const char tz[] = "TZ";
295static const char utc[] = "UTC";
296
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700297/**
298 * Returns FORMAT_OFF on invalid string
299 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800300AndroidLogPrintFormat android_log_formatFromString(const char* formatString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800301 static AndroidLogPrintFormat format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700302
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800303 /* clang-format off */
304 if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF;
305 else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS;
306 else if (!strcmp(formatString, "tag")) format = FORMAT_TAG;
307 else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD;
308 else if (!strcmp(formatString, "raw")) format = FORMAT_RAW;
309 else if (!strcmp(formatString, "time")) format = FORMAT_TIME;
310 else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME;
311 else if (!strcmp(formatString, "long")) format = FORMAT_LONG;
312 else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR;
313 else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR;
314 else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC;
315 else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC;
316 else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE;
317 else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR;
318 else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE;
319 else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH;
320 else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
321 else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
322 else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800323 /* clang-format on */
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800324
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800325#ifndef __MINGW32__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800326 else {
327 extern char* tzname[2];
328 static const char gmt[] = "GMT";
329 char* cp = getenv(tz);
330 if (cp) {
331 cp = strdup(cp);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700332 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800333 setenv(tz, formatString, 1);
334 /*
335 * Run tzset here to determine if the timezone is legitimate. If the
336 * zone is GMT, check if that is what was asked for, if not then
337 * did not match any on the system; report an error to caller.
338 */
339 tzset();
340 if (!tzname[0] ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800341 ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
342 && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800343 if (cp) {
344 setenv(tz, cp, 1);
345 } else {
346 unsetenv(tz);
347 }
348 tzset();
349 format = FORMAT_OFF;
350 } else {
351 format = FORMAT_MODIFIER_ZONE;
352 }
353 free(cp);
354 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800355#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700356
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800357 return format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358}
359
360/**
361 * filterExpression: a single filter expression
362 * eg "AT:d"
363 *
364 * returns 0 on success and -1 on invalid expression
365 *
366 * Assumes single threaded execution
367 */
368
Tom Cherry2d9779e2019-02-08 11:46:19 -0800369int android_log_addFilterRule(AndroidLogFormat* p_format, const char* filterExpression) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800370 size_t tagNameLength;
371 android_LogPriority pri = ANDROID_LOG_DEFAULT;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700372
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800373 tagNameLength = strcspn(filterExpression, ":");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800375 if (tagNameLength == 0) {
376 goto error;
377 }
378
379 if (filterExpression[tagNameLength] == ':') {
380 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
381
382 if (pri == ANDROID_LOG_UNKNOWN) {
383 goto error;
384 }
385 }
386
387 if (0 == strncmp("*", filterExpression, tagNameLength)) {
388 /*
389 * This filter expression refers to the global filter
390 * The default level for this is DEBUG if the priority
391 * is unspecified
392 */
393 if (pri == ANDROID_LOG_DEFAULT) {
394 pri = ANDROID_LOG_DEBUG;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700395 }
396
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800397 p_format->global_pri = pri;
398 } else {
399 /*
400 * for filter expressions that don't refer to the global
401 * filter, the default is verbose if the priority is unspecified
402 */
403 if (pri == ANDROID_LOG_DEFAULT) {
404 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700405 }
406
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800407 char* tagName;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700408
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700409/*
410 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800411 * Darwin doesn't have strndup, everything else does
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700412 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700413#ifdef HAVE_STRNDUP
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800414 tagName = strndup(filterExpression, tagNameLength);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800416 /* a few extra bytes copied... */
417 tagName = strdup(filterExpression);
418 tagName[tagNameLength] = '\0';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419#endif /*HAVE_STRNDUP*/
420
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800421 FilterInfo* p_fi = filterinfo_new(tagName, pri);
422 free(tagName);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700423
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800424 p_fi->p_next = p_format->filters;
425 p_format->filters = p_fi;
426 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700427
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800428 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800430 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431}
432
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800433#ifndef HAVE_STRSEP
434/* KISS replacement helper for below */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800435static char* strsep(char** stringp, const char* delim) {
436 char* token;
437 char* ret = *stringp;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800438
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800439 if (!ret || !*ret) {
440 return NULL;
441 }
442 token = strpbrk(ret, delim);
443 if (token) {
444 *token = '\0';
445 ++token;
446 } else {
447 token = ret + strlen(ret);
448 }
449 *stringp = token;
450 return ret;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800451}
452#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700453
454/**
455 * filterString: a comma/whitespace-separated set of filter expressions
456 *
457 * eg "AT:d *:i"
458 *
459 * returns 0 on success and -1 on invalid expression
460 *
461 * Assumes single threaded execution
462 *
463 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800464int android_log_addFilterString(AndroidLogFormat* p_format, const char* filterString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800465 char* filterStringCopy = strdup(filterString);
466 char* p_cur = filterStringCopy;
467 char* p_ret;
468 int err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700469
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800470 /* Yes, I'm using strsep */
471 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
472 /* ignore whitespace-only entries */
473 if (p_ret[0] != '\0') {
474 err = android_log_addFilterRule(p_format, p_ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700475
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800476 if (err < 0) {
477 goto error;
478 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700479 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800480 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800482 free(filterStringCopy);
483 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700484error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800485 free(filterStringCopy);
486 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700487}
488
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489/**
490 * Splits a wire-format buffer into an AndroidLogEntry
491 * entry allocated by caller. Pointers will point directly into buf
492 *
493 * Returns 0 on success and -1 on invalid wire format (entry will be
494 * in unspecified state)
495 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800496int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800497 entry->message = NULL;
498 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800499
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800500 entry->tv_sec = buf->sec;
501 entry->tv_nsec = buf->nsec;
502 entry->uid = -1;
503 entry->pid = buf->pid;
504 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700505
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800506 /*
507 * format: <priority:1><tag:N>\0<message:N>\0
508 *
509 * tag str
510 * starts at buf->msg+1
511 * msg
512 * starts at buf->msg+1+len(tag)+1
513 *
514 * The message may have been truncated by the kernel log driver.
515 * When that happens, we must null-terminate the message ourselves.
516 */
517 if (buf->len < 3) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700518 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800519 * An well-formed entry must consist of at least a priority
520 * and two null characters
Kenny Root4bf3c022011-09-30 17:10:14 -0700521 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800522 fprintf(stderr, "+++ LOG: entry too small\n");
523 return -1;
524 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700525
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800526 int msgStart = -1;
527 int msgEnd = -1;
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700528
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800529 int i;
530 char* msg = buf->msg;
531 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
532 if (buf2->hdr_size) {
533 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
534 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
535 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
536 return -1;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800537 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800538 msg = ((char*)buf2) + buf2->hdr_size;
539 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
540 entry->uid = ((struct logger_entry_v4*)buf)->uid;
541 }
542 }
543 for (i = 1; i < buf->len; i++) {
544 if (msg[i] == '\0') {
545 if (msgStart == -1) {
546 msgStart = i + 1;
547 } else {
548 msgEnd = i;
549 break;
550 }
551 }
552 }
553
554 if (msgStart == -1) {
555 /* +++ LOG: malformed log message, DYB */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700556 for (i = 1; i < buf->len; i++) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800557 /* odd characters in tag? */
558 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
559 msg[i] = '\0';
560 msgStart = i + 1;
561 break;
562 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700563 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700564 if (msgStart == -1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800565 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich63f4a842011-10-17 10:45:03 -0700566 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800567 }
568 if (msgEnd == -1) {
569 /* incoming message not null-terminated; force it */
570 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
571 msg[msgEnd] = '\0';
572 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700573
Tom Cherry71ba1642019-01-10 10:37:36 -0800574 entry->priority = static_cast<android_LogPriority>(msg[0]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800575 entry->tag = msg + 1;
576 entry->tagLen = msgStart - 1;
577 entry->message = msg + msgStart;
578 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700579
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800580 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700581}
582
583/*
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000584 * Extract a 4-byte value from a byte stream.
585 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800586static inline uint32_t get4LE(const uint8_t* src) {
587 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000588}
589
590/*
591 * Extract an 8-byte value from a byte stream.
592 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800593static inline uint64_t get8LE(const uint8_t* src) {
594 uint32_t low, high;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000595
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800596 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
597 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
598 return ((uint64_t)high << 32) | (uint64_t)low;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000599}
600
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700601static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800602 while ((*len) && isspace(*(*cp))) {
603 ++(*cp);
604 --(*len);
605 }
606 if (c == INT_MAX) return *len;
607 if ((*len) && (*(*cp) == c)) {
608 ++(*cp);
609 --(*len);
610 return true;
611 }
612 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700613}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000614
615/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700616 * Recursively convert binary log data to printable form.
617 *
618 * This needs to be recursive because you can have lists of lists.
619 *
620 * If we run out of room, we stop processing immediately. It's important
621 * for us to check for space on every output element to avoid producing
622 * garbled output.
623 *
624 * Returns 0 on success, 1 on buffer full, -1 on failure.
625 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700626enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800627 TYPE_OBJECTS = '1',
628 TYPE_BYTES = '2',
629 TYPE_MILLISECONDS = '3',
630 TYPE_ALLOCATIONS = '4',
631 TYPE_ID = '5',
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800632 TYPE_PERCENT = '6',
633 TYPE_MONOTONIC = 's'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700634};
635
Tom Cherry71ba1642019-01-10 10:37:36 -0800636static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
637 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800638 size_t* fmtLen) {
639 const unsigned char* eventData = *pEventData;
640 size_t eventDataLen = *pEventDataLen;
641 char* outBuf = *pOutBuf;
642 char* outBufSave = outBuf;
643 size_t outBufLen = *pOutBufLen;
644 size_t outBufLenSave = outBufLen;
645 unsigned char type;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800646 size_t outCount = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800647 int result = 0;
648 const char* cp;
649 size_t len;
650 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700651
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800652 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800653
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800654 type = *eventData++;
655 eventDataLen--;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700656
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800657 cp = NULL;
658 len = 0;
659 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
660 cp = *fmtStr;
661 len = *fmtLen;
662 }
663 /*
664 * event.logtag format specification:
665 *
666 * Optionally, after the tag names can be put a description for the value(s)
667 * of the tag. Description are in the format
668 * (<name>|data type[|data unit])
669 * Multiple values are separated by commas.
670 *
671 * The data type is a number from the following values:
672 * 1: int
673 * 2: long
674 * 3: string
675 * 4: list
676 * 5: float
677 *
678 * The data unit is a number taken from the following list:
679 * 1: Number of objects
680 * 2: Number of bytes
681 * 3: Number of milliseconds
682 * 4: Number of allocations
683 * 5: Id
684 * 6: Percent
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800685 * s: Number of seconds (monotonic time)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800686 * Default value for data of type int/long is 2 (bytes).
687 */
688 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700689 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800690 } else {
691 char* outBufLastSpace = NULL;
692
693 findChar(&cp, &len, INT_MAX);
694 while (len && *cp && (*cp != '|') && (*cp != ')')) {
695 if (outBufLen <= 0) {
696 /* halt output */
697 goto no_room;
698 }
699 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
700 *outBuf = *cp;
701 ++outBuf;
702 ++cp;
703 --outBufLen;
704 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700705 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800706 if (outBufLastSpace) {
707 outBufLen += outBuf - outBufLastSpace;
708 outBuf = outBufLastSpace;
709 }
710 if (outBufLen <= 0) {
711 /* halt output */
712 goto no_room;
713 }
714 if (outBufSave != outBuf) {
715 *outBuf = '=';
716 ++outBuf;
717 --outBufLen;
718 }
719
720 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800721 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
722 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800723
Tom Cherry71ba1642019-01-10 10:37:36 -0800724 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800725 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700726 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700727
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800728 if (len) {
729 ++cp;
730 --len;
731 } else {
732 /* reset the format */
733 outBuf = outBufSave;
734 outBufLen = outBufLenSave;
735 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700736 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800737 }
738 outCount = 0;
739 lval = 0;
740 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700741 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800742 /* 32-bit signed int */
743 {
744 int32_t ival;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700745
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800746 if (eventDataLen < 4) return -1;
747 ival = get4LE(eventData);
748 eventData += 4;
749 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700750
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800751 lval = ival;
752 }
753 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700754 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800755 /* 64-bit signed long */
756 if (eventDataLen < 8) return -1;
757 lval = get8LE(eventData);
758 eventData += 8;
759 eventDataLen -= 8;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700760 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800761 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
762 if (outCount < outBufLen) {
763 outBuf += outCount;
764 outBufLen -= outCount;
765 } else {
766 /* halt output */
767 goto no_room;
768 }
769 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700770 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800771 /* float */
772 {
773 uint32_t ival;
774 float fval;
Jeff Brown44193d92015-04-28 12:47:02 -0700775
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800776 if (eventDataLen < 4) return -1;
777 ival = get4LE(eventData);
778 fval = *(float*)&ival;
779 eventData += 4;
780 eventDataLen -= 4;
Jeff Brown44193d92015-04-28 12:47:02 -0700781
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800782 outCount = snprintf(outBuf, outBufLen, "%f", fval);
783 if (outCount < outBufLen) {
784 outBuf += outCount;
785 outBufLen -= outCount;
786 } else {
787 /* halt output */
788 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700789 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800790 }
791 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700792 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800793 /* UTF-8 chars, not NULL-terminated */
794 {
795 unsigned int strLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700796
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800797 if (eventDataLen < 4) return -1;
798 strLen = get4LE(eventData);
799 eventData += 4;
800 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700801
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800802 if (eventDataLen < strLen) {
803 result = -1; /* mark truncated */
804 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700805 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800807 if (cp && (strLen == 0)) {
808 /* reset the format if no content */
809 outBuf = outBufSave;
810 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800812 if (strLen < outBufLen) {
813 memcpy(outBuf, eventData, strLen);
814 outBuf += strLen;
815 outBufLen -= strLen;
816 } else {
817 if (outBufLen > 0) {
818 /* copy what we can */
819 memcpy(outBuf, eventData, outBufLen);
820 outBuf += outBufLen;
821 outBufLen -= outBufLen;
822 }
823 if (!result) result = 1; /* if not truncated, return no room */
824 }
825 eventData += strLen;
826 eventDataLen -= strLen;
827 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700828 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800829 }
830 case EVENT_TYPE_LIST:
831 /* N items, all different types */
832 {
833 unsigned char count;
834 int i;
835
836 if (eventDataLen < 1) return -1;
837
838 count = *eventData++;
839 eventDataLen--;
840
841 if (outBufLen <= 0) goto no_room;
842
843 *outBuf++ = '[';
844 outBufLen--;
845
846 for (i = 0; i < count; i++) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800847 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
848 fmtStr, fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800849 if (result != 0) goto bail;
850
851 if (i < (count - 1)) {
852 if (outBufLen <= 0) goto no_room;
853 *outBuf++ = ',';
854 outBufLen--;
855 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700856 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800857
858 if (outBufLen <= 0) goto no_room;
859
860 *outBuf++ = ']';
861 outBufLen--;
862 }
863 break;
864 default:
865 fprintf(stderr, "Unknown binary event type %d\n", type);
866 return -1;
867 }
868 if (cp && len) {
869 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
870 switch (*cp) {
871 case TYPE_OBJECTS:
872 outCount = 0;
873 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
874 break;
875 case TYPE_BYTES:
876 if ((lval != 0) && ((lval % 1024) == 0)) {
877 /* repaint with multiplier */
Tom Cherry71ba1642019-01-10 10:37:36 -0800878 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800879 size_t idx = 0;
880 outBuf -= outCount;
881 outBufLen += outCount;
882 do {
883 lval /= 1024;
884 if ((lval % 1024) != 0) break;
Tom Cherry71ba1642019-01-10 10:37:36 -0800885 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
886 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800887 } else {
888 outCount = snprintf(outBuf, outBufLen, "B");
889 }
890 break;
891 case TYPE_MILLISECONDS:
Tom Cherry71ba1642019-01-10 10:37:36 -0800892 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800893 /* repaint as (fractional) seconds, possibly saving space */
894 if (outBufLen) outBuf[0] = outBuf[-1];
895 outBuf[-1] = outBuf[-2];
896 outBuf[-2] = outBuf[-3];
897 outBuf[-3] = '.';
898 while ((outBufLen == 0) || (*outBuf == '0')) {
899 --outBuf;
900 ++outBufLen;
901 }
902 if (*outBuf != '.') {
903 ++outBuf;
904 --outBufLen;
905 }
906 outCount = snprintf(outBuf, outBufLen, "s");
907 } else {
908 outCount = snprintf(outBuf, outBufLen, "ms");
909 }
910 break;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800911 case TYPE_MONOTONIC: {
912 static const uint64_t minute = 60;
913 static const uint64_t hour = 60 * minute;
914 static const uint64_t day = 24 * hour;
915
916 /* Repaint as unsigned seconds, minutes, hours ... */
917 outBuf -= outCount;
918 outBufLen += outCount;
919 uint64_t val = lval;
920 if (val >= day) {
921 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
922 if (outCount >= outBufLen) break;
923 outBuf += outCount;
924 outBufLen -= outCount;
925 val = (val % day) + day;
926 }
927 if (val >= minute) {
928 if (val >= hour) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800929 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800930 if (outCount >= outBufLen) break;
931 outBuf += outCount;
932 outBufLen -= outCount;
933 }
934 outCount =
Tom Cherry71ba1642019-01-10 10:37:36 -0800935 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800936 (val / minute) % (hour / minute));
937 if (outCount >= outBufLen) break;
938 outBuf += outCount;
939 outBufLen -= outCount;
940 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800941 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800942 val % minute);
943 } break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800944 case TYPE_ALLOCATIONS:
945 outCount = 0;
946 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
947 break;
948 case TYPE_ID:
949 outCount = 0;
950 break;
951 case TYPE_PERCENT:
952 outCount = snprintf(outBuf, outBufLen, "%%");
953 break;
954 default: /* ? */
955 outCount = 0;
956 break;
957 }
958 ++cp;
959 --len;
960 if (outCount < outBufLen) {
961 outBuf += outCount;
962 outBufLen -= outCount;
963 } else if (outCount) {
964 /* halt output */
965 goto no_room;
966 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700967 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800968 if (!findChar(&cp, &len, ')')) len = 0;
969 if (!findChar(&cp, &len, ',')) len = 0;
970 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700971
972bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800973 *pEventData = eventData;
974 *pEventDataLen = eventDataLen;
975 *pOutBuf = outBuf;
976 *pOutBufLen = outBufLen;
977 if (cp) {
978 *fmtStr = cp;
979 *fmtLen = len;
980 }
981 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700982
983no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800984 result = 1;
985 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700986}
987
988/**
989 * Convert a binary log entry to ASCII form.
990 *
991 * For convenience we mimic the processLogBuffer API. There is no
992 * pre-defined output length for the binary data, since we're free to format
993 * it however we choose, which means we can't really use a fixed-size buffer
994 * here.
995 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800996int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800997 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherry71ba1642019-01-10 10:37:36 -0800998 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800999 char* messageBuf, int messageBufLen) {
1000 size_t inCount;
1001 uint32_t tagIndex;
1002 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001003
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001004 entry->message = NULL;
1005 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001006
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001007 entry->tv_sec = buf->sec;
1008 entry->tv_nsec = buf->nsec;
1009 entry->priority = ANDROID_LOG_INFO;
1010 entry->uid = -1;
1011 entry->pid = buf->pid;
1012 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001013
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001014 /*
1015 * Pull the tag out, fill in some additional details based on incoming
1016 * buffer version (v3 adds lid, v4 adds uid).
1017 */
1018 eventData = (const unsigned char*)buf->msg;
1019 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
1020 if (buf2->hdr_size) {
1021 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
1022 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
1023 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
1024 return -1;
1025 }
1026 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
1027 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
1028 (((struct logger_entry_v3*)buf)->lid == LOG_ID_SECURITY)) {
1029 entry->priority = ANDROID_LOG_WARN;
1030 }
1031 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
1032 entry->uid = ((struct logger_entry_v4*)buf)->uid;
1033 }
1034 }
1035 inCount = buf->len;
1036 if (inCount < 4) return -1;
1037 tagIndex = get4LE(eventData);
1038 eventData += 4;
1039 inCount -= 4;
1040
1041 entry->tagLen = 0;
1042 entry->tag = NULL;
1043#ifdef __ANDROID__
1044 if (map != NULL) {
1045 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1046 }
1047#endif
1048
1049 /*
1050 * If we don't have a map, or didn't find the tag number in the map,
1051 * stuff a generated tag value into the start of the output buffer and
1052 * shift the buffer pointers down.
1053 */
1054 if (entry->tag == NULL) {
1055 size_t tagLen;
1056
1057 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1058 if (tagLen >= (size_t)messageBufLen) {
1059 tagLen = messageBufLen - 1;
1060 }
1061 entry->tag = messageBuf;
1062 entry->tagLen = tagLen;
1063 messageBuf += tagLen + 1;
1064 messageBufLen -= tagLen + 1;
1065 }
1066
1067 /*
1068 * Format the event log data into the buffer.
1069 */
1070 const char* fmtStr = NULL;
1071 size_t fmtLen = 0;
1072#ifdef __ANDROID__
1073 if (descriptive_output && map) {
1074 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1075 }
1076#endif
1077
1078 char* outBuf = messageBuf;
1079 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1080 int result = 0;
1081
1082 if ((inCount > 0) || fmtLen) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001083 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1084 &fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001085 }
1086 if ((result == 1) && fmtStr) {
1087 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001088 eventData = (const unsigned char*)buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001089 if (buf2->hdr_size) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001090 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001091 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001092 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001093 outBuf = messageBuf;
1094 outRemaining = messageBufLen - 1;
Tom Cherry71ba1642019-01-10 10:37:36 -08001095 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001096 }
1097 if (result < 0) {
1098 fprintf(stderr, "Binary log entry conversion failed\n");
1099 }
1100 if (result) {
1101 if (!outRemaining) {
1102 /* make space to leave an indicator */
1103 --outBuf;
1104 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001105 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001106 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1107 outRemaining--;
1108 /* pretend we ate all the data to prevent log stutter */
1109 inCount = 0;
1110 if (result > 0) result = 0;
1111 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001112
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001113 /* eat the silly terminating '\n' */
1114 if (inCount == 1 && *eventData == '\n') {
1115 eventData++;
1116 inCount--;
1117 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001118
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001119 if (inCount != 0) {
1120 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1121 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001122
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001123 /*
1124 * Terminate the buffer. The NUL byte does not count as part of
1125 * entry->messageLen.
1126 */
1127 *outBuf = '\0';
1128 entry->messageLen = outBuf - messageBuf;
1129 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001130
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001131 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001132
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001133 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001134}
1135
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001136/*
1137 * One utf8 character at a time
1138 *
1139 * Returns the length of the utf8 character in the buffer,
1140 * or -1 if illegal or truncated
1141 *
1142 * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(),
1143 * can not remove from here because of library circular dependencies.
1144 * Expect one-day utf8_character_length with the same signature could
1145 * _also_ be part of libutils/Unicode.cpp if its usefullness needs to
1146 * propagate globally.
1147 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001148LIBLOG_WEAK ssize_t utf8_character_length(const char* src, size_t len) {
1149 const char* cur = src;
1150 const char first_char = *cur++;
1151 static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF;
1152 int32_t mask, to_ignore_mask;
1153 size_t num_to_read;
1154 uint32_t utf32;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001155
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001156 if ((first_char & 0x80) == 0) { /* ASCII */
1157 return first_char ? 1 : -1;
1158 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001159
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001160 /*
1161 * (UTF-8's character must not be like 10xxxxxx,
1162 * but 110xxxxx, 1110xxxx, ... or 1111110x)
1163 */
1164 if ((first_char & 0x40) == 0) {
1165 return -1;
1166 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001167
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001168 for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
Tom Cherry71ba1642019-01-10 10:37:36 -08001169 num_to_read < 5 && (first_char & mask); num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001170 if (num_to_read > len) {
1171 return -1;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001172 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001173 if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */
1174 return -1;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001175 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001176 utf32 = (utf32 << 6) + (*cur++ & 0b00111111);
1177 }
1178 /* "first_char" must be (110xxxxx - 11110xxx) */
1179 if (num_to_read >= 5) {
1180 return -1;
1181 }
1182 to_ignore_mask |= mask;
1183 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
1184 if (utf32 > kUnicodeMaxCodepoint) {
1185 return -1;
1186 }
1187 return num_to_read;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001188}
1189
1190/*
1191 * Convert to printable from message to p buffer, return string length. If p is
1192 * NULL, do not copy, but still return the expected string length.
1193 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001194static size_t convertPrintable(char* p, const char* message, size_t messageLen) {
1195 char* begin = p;
1196 bool print = p != NULL;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001197
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001198 while (messageLen) {
1199 char buf[6];
1200 ssize_t len = sizeof(buf) - 1;
1201 if ((size_t)len > messageLen) {
1202 len = messageLen;
1203 }
1204 len = utf8_character_length(message, len);
1205
1206 if (len < 0) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001207 snprintf(buf, sizeof(buf), ((messageLen > 1) && isdigit(message[1])) ? "\\%03o" : "\\%o",
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001208 *message & 0377);
1209 len = 1;
1210 } else {
1211 buf[0] = '\0';
1212 if (len == 1) {
1213 if (*message == '\a') {
1214 strcpy(buf, "\\a");
1215 } else if (*message == '\b') {
1216 strcpy(buf, "\\b");
1217 } else if (*message == '\t') {
1218 strcpy(buf, "\t"); /* Do not escape tabs */
1219 } else if (*message == '\v') {
1220 strcpy(buf, "\\v");
1221 } else if (*message == '\f') {
1222 strcpy(buf, "\\f");
1223 } else if (*message == '\r') {
1224 strcpy(buf, "\\r");
1225 } else if (*message == '\\') {
1226 strcpy(buf, "\\\\");
1227 } else if ((*message < ' ') || (*message & 0x80)) {
1228 snprintf(buf, sizeof(buf), "\\%o", *message & 0377);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001229 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001230 }
1231 if (!buf[0]) {
1232 strncpy(buf, message, len);
1233 buf[len] = '\0';
1234 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001235 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001236 if (print) {
1237 strcpy(p, buf);
1238 }
1239 p += strlen(buf);
1240 message += len;
1241 messageLen -= len;
1242 }
1243 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001244}
1245
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001246static char* readSeconds(char* e, struct timespec* t) {
1247 unsigned long multiplier;
1248 char* p;
1249 t->tv_sec = strtoul(e, &p, 10);
1250 if (*p != '.') {
1251 return NULL;
1252 }
1253 t->tv_nsec = 0;
1254 multiplier = NS_PER_SEC;
1255 while (isdigit(*++p) && (multiplier /= 10)) {
1256 t->tv_nsec += (*p - '0') * multiplier;
1257 }
1258 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001259}
1260
Tom Cherry71ba1642019-01-10 10:37:36 -08001261static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001262 left->tv_nsec += right->tv_nsec;
1263 left->tv_sec += right->tv_sec;
1264 if (left->tv_nsec >= (long)NS_PER_SEC) {
1265 left->tv_nsec -= NS_PER_SEC;
1266 left->tv_sec += 1;
1267 }
1268 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001269}
1270
Tom Cherry71ba1642019-01-10 10:37:36 -08001271static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001272 struct timespec* right) {
1273 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1274 result->tv_sec = left->tv_sec - right->tv_sec;
1275 if (result->tv_nsec < 0) {
1276 result->tv_nsec += NS_PER_SEC;
1277 result->tv_sec -= 1;
1278 }
1279 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001280}
1281
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001282static long long nsecTimespec(struct timespec* now) {
1283 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001284}
1285
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001286#ifdef __ANDROID__
Tom Cherry71ba1642019-01-10 10:37:36 -08001287static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001288 struct listnode* node;
1289 struct conversionList {
1290 struct listnode node; /* first */
1291 struct timespec time;
1292 struct timespec convert;
1293 } * list, *next;
1294 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001295
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001296 /* If we do not have a conversion list, build one up */
1297 if (list_empty(&convertHead)) {
1298 bool suspended_pending = false;
Tom Cherry71ba1642019-01-10 10:37:36 -08001299 struct timespec suspended_monotonic = {0, 0};
1300 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001301
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001302 /*
1303 * Read dmesg for _some_ synchronization markers and insert
1304 * Anything in the Android Logger before the dmesg logging span will
1305 * be highly suspect regarding the monotonic time calculations.
1306 */
1307 FILE* p = popen("/system/bin/dmesg", "re");
1308 if (p) {
1309 char* line = NULL;
1310 size_t len = 0;
1311 while (getline(&line, &len, p) > 0) {
1312 static const char suspend[] = "PM: suspend entry ";
1313 static const char resume[] = "PM: suspend exit ";
1314 static const char healthd[] = "healthd";
1315 static const char battery[] = ": battery ";
1316 static const char suspended[] = "Suspended for ";
1317 struct timespec monotonic;
1318 struct tm tm;
1319 char *cp, *e = line;
1320 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001321
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001322 if (*e == '<') {
1323 while (*e && (*e != '>')) {
1324 ++e;
1325 }
1326 if (*e != '>') {
1327 continue;
1328 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001329 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001330 if (*e != '[') {
1331 continue;
1332 }
1333 while (*++e == ' ') {
1334 ;
1335 }
1336 e = readSeconds(e, &monotonic);
1337 if (!e || (*e != ']')) {
1338 continue;
1339 }
1340
1341 if ((e = strstr(e, suspend))) {
1342 e += sizeof(suspend) - 1;
1343 } else if ((e = strstr(line, resume))) {
1344 e += sizeof(resume) - 1;
1345 } else if (((e = strstr(line, healthd))) &&
1346 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1347 /* NB: healthd is roughly 150us late, worth the price to
1348 * deal with ntp-induced or hardware clock drift. */
1349 e += sizeof(battery) - 1;
1350 } else if ((e = strstr(line, suspended))) {
1351 e += sizeof(suspended) - 1;
1352 e = readSeconds(e, &time);
1353 if (!e) {
1354 continue;
1355 }
1356 add_entry = false;
1357 suspended_pending = true;
1358 suspended_monotonic = monotonic;
1359 suspended_diff = time;
1360 } else {
1361 continue;
1362 }
1363 if (add_entry) {
1364 /* look for "????-??-?? ??:??:??.????????? UTC" */
1365 cp = strstr(e, " UTC");
1366 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1367 continue;
1368 }
1369 e = cp - 29;
1370 cp = readSeconds(cp - 10, &time);
1371 if (!cp) {
1372 continue;
1373 }
1374 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1375 if (!cp) {
1376 continue;
1377 }
1378 cp = getenv(tz);
1379 if (cp) {
1380 cp = strdup(cp);
1381 }
1382 setenv(tz, utc, 1);
1383 time.tv_sec = mktime(&tm);
1384 if (cp) {
1385 setenv(tz, cp, 1);
1386 free(cp);
1387 } else {
1388 unsetenv(tz);
1389 }
Tom Cherry71ba1642019-01-10 10:37:36 -08001390 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001391 list_init(&list->node);
1392 list->time = time;
1393 subTimespec(&list->convert, &time, &monotonic);
1394 list_add_tail(&convertHead, &list->node);
1395 }
1396 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001397 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001398 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1399 &suspended_monotonic)
1400 ->tv_sec > 0) {
1401 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001402 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001403 } else {
1404 /* suspend */
1405 convert = list->convert;
1406 }
1407 time = suspended_monotonic;
1408 sumTimespec(&time, &convert);
1409 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001410 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001411 list_init(&list->node);
1412 list->time = time;
1413 list->convert = convert;
1414 list_add_tail(&convertHead, &list->node);
1415 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001416 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001417 list_init(&list->node);
1418 list->time = time;
1419 sumTimespec(&list->time, &suspended_diff);
1420 list->convert = convert;
1421 sumTimespec(&list->convert, &suspended_diff);
1422 list_add_tail(&convertHead, &list->node);
1423 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001424 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001425 }
1426 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001427 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001428 /* last entry is our current time conversion */
Tom Cherry71ba1642019-01-10 10:37:36 -08001429 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001430 list_init(&list->node);
1431 clock_gettime(CLOCK_REALTIME, &list->time);
1432 clock_gettime(CLOCK_MONOTONIC, &convert);
1433 clock_gettime(CLOCK_MONOTONIC, &time);
1434 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1435 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1436 /* Calculate conversion factor */
1437 subTimespec(&list->convert, &list->time, &time);
1438 list_add_tail(&convertHead, &list->node);
1439 if (suspended_pending) {
1440 /* manufacture a suspend @ point before */
1441 subTimespec(&convert, &list->convert, &suspended_diff);
1442 time = suspended_monotonic;
1443 sumTimespec(&time, &convert);
1444 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001445 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001446 list_init(&list->node);
1447 list->time = time;
1448 sumTimespec(&list->time, &suspended_diff);
1449 list->convert = convert;
1450 sumTimespec(&list->convert, &suspended_diff);
1451 list_add_head(&convertHead, &list->node);
1452 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001453 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001454 list_init(&list->node);
1455 list->time = time;
1456 list->convert = convert;
1457 list_add_head(&convertHead, &list->node);
1458 }
1459 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001460
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001461 /* Find the breakpoint in the conversion list */
1462 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1463 next = NULL;
1464 list_for_each(node, &convertHead) {
1465 next = node_to_item(node, struct conversionList, node);
1466 if (entry->tv_sec < next->time.tv_sec) {
1467 break;
1468 } else if (entry->tv_sec == next->time.tv_sec) {
1469 if (entry->tv_nsec < next->time.tv_nsec) {
1470 break;
1471 }
1472 }
1473 list = next;
1474 }
1475
1476 /* blend time from one breakpoint to the next */
1477 convert = list->convert;
1478 if (next) {
1479 unsigned long long total, run;
1480
1481 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1482 time.tv_sec = entry->tv_sec;
1483 time.tv_nsec = entry->tv_nsec;
1484 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1485 if (run < total) {
1486 long long crun;
1487
1488 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1489 f *= run;
1490 f /= total;
1491 crun = f;
1492 convert.tv_sec += crun / (long long)NS_PER_SEC;
1493 if (crun < 0) {
1494 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1495 if (convert.tv_nsec < 0) {
1496 convert.tv_nsec += NS_PER_SEC;
1497 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001498 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001499 } else {
1500 convert.tv_nsec += crun % NS_PER_SEC;
1501 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1502 convert.tv_nsec -= NS_PER_SEC;
1503 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001504 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001505 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001506 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001507 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001508
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001509 /* Apply the correction factor */
1510 result->tv_sec = entry->tv_sec;
1511 result->tv_nsec = entry->tv_nsec;
1512 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001513}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001514#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001515
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001516/**
1517 * Formats a log message into a buffer
1518 *
1519 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1520 * If return value != defaultBuffer, caller must call free()
1521 * Returns NULL on malloc error
1522 */
1523
Tom Cherry2d9779e2019-02-08 11:46:19 -08001524char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
1525 size_t defaultBufferSize, const AndroidLogEntry* entry,
1526 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001527#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001528 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001529#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001530 struct tm* ptm;
1531 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1532 char timeBuf[64];
1533 char prefixBuf[128], suffixBuf[128];
1534 char priChar;
1535 int prefixSuffixIsHeaderFooter = 0;
1536 char* ret;
1537 time_t now;
1538 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001539
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001540 priChar = filterPriToChar(entry->priority);
1541 size_t prefixLen = 0, suffixLen = 0;
1542 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001543
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001544 /*
1545 * Get the current date/time in pretty form
1546 *
1547 * It's often useful when examining a log with "less" to jump to
1548 * a specific point in the file by searching for the date/time stamp.
1549 * For this reason it's very annoying to have regexp meta characters
1550 * in the time stamp. Don't use forward slashes, parenthesis,
1551 * brackets, asterisks, or other special chars here.
1552 *
1553 * The caller may have affected the timezone environment, this is
1554 * expected to be sensitive to that.
1555 */
1556 now = entry->tv_sec;
1557 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001558#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001559 if (p_format->monotonic_output) {
1560 /* prevent convertMonotonic from being called if logd is monotonic */
1561 if (android_log_clockid() != CLOCK_MONOTONIC) {
1562 struct timespec time;
1563 convertMonotonic(&time, entry);
1564 now = time.tv_sec;
1565 nsec = time.tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001566 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001567 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001568#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001569 if (now < 0) {
1570 nsec = NS_PER_SEC - nsec;
1571 }
1572 if (p_format->epoch_output || p_format->monotonic_output) {
1573 ptm = NULL;
Tom Cherry71ba1642019-01-10 10:37:36 -08001574 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1575 (long long)now);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001576 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001577#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001578 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001579#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001580 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001581#endif
Tom Cherry71ba1642019-01-10 10:37:36 -08001582 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001583 }
1584 len = strlen(timeBuf);
1585 if (p_format->nsec_time_output) {
1586 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1587 } else if (p_format->usec_time_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001588 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001589 } else {
Tom Cherry71ba1642019-01-10 10:37:36 -08001590 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001591 }
1592 if (p_format->zone_output && ptm) {
1593 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1594 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001595
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001596 /*
1597 * Construct a buffer containing the log header and log message.
1598 */
1599 if (p_format->colored_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001600 prefixLen =
1601 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001602 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV487de272018-05-15 18:56:36 -07001603
1604 const char suffixContents[] = "\x1B[0m";
1605 strcpy(suffixBuf, suffixContents);
1606 suffixLen = strlen(suffixContents);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001607 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001608
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001609 char uid[16];
1610 uid[0] = '\0';
1611 if (p_format->uid_output) {
1612 if (entry->uid >= 0) {
1613/*
1614 * This code is Android specific, bionic guarantees that
1615 * calls to non-reentrant getpwuid() are thread safe.
1616 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001617#if !defined(__MINGW32__)
1618#if (FAKE_LOG_DEVICE == 0)
William Roberts8a5b9ca2016-04-08 12:13:17 -07001619#ifndef __BIONIC__
Tom Cherry71ba1642019-01-10 10:37:36 -08001620#warning "This code assumes that getpwuid is thread safe, only true with Bionic!"
William Roberts8a5b9ca2016-04-08 12:13:17 -07001621#endif
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001622#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001623 struct passwd* pwd = getpwuid(entry->uid);
1624 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1625 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1626 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001627#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001628 {
1629 /* Not worth parsing package list, names all longer than 5 */
1630 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1631 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001632 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001633 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001634 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001635 }
1636
1637 switch (p_format->format) {
1638 case FORMAT_TAG:
Tom Cherry71ba1642019-01-10 10:37:36 -08001639 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1640 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001641 strcpy(suffixBuf + suffixLen, "\n");
1642 ++suffixLen;
1643 break;
1644 case FORMAT_PROCESS:
Tom Cherry71ba1642019-01-10 10:37:36 -08001645 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1646 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001647 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherry71ba1642019-01-10 10:37:36 -08001648 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1649 uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001650 break;
1651 case FORMAT_THREAD:
Tom Cherry71ba1642019-01-10 10:37:36 -08001652 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1653 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001654 strcpy(suffixBuf + suffixLen, "\n");
1655 ++suffixLen;
1656 break;
1657 case FORMAT_RAW:
1658 prefixBuf[prefixLen] = 0;
1659 len = 0;
1660 strcpy(suffixBuf + suffixLen, "\n");
1661 ++suffixLen;
1662 break;
1663 case FORMAT_TIME:
1664 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001665 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1666 entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001667 strcpy(suffixBuf + suffixLen, "\n");
1668 ++suffixLen;
1669 break;
1670 case FORMAT_THREADTIME:
1671 ret = strchr(uid, ':');
1672 if (ret) {
1673 *ret = ' ';
1674 }
1675 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001676 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1677 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001678 strcpy(suffixBuf + suffixLen, "\n");
1679 ++suffixLen;
1680 break;
1681 case FORMAT_LONG:
1682 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001683 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1684 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001685 strcpy(suffixBuf + suffixLen, "\n\n");
1686 suffixLen += 2;
1687 prefixSuffixIsHeaderFooter = 1;
1688 break;
1689 case FORMAT_BRIEF:
1690 default:
Tom Cherry71ba1642019-01-10 10:37:36 -08001691 len =
1692 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1693 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001694 strcpy(suffixBuf + suffixLen, "\n");
1695 ++suffixLen;
1696 break;
1697 }
1698
1699 /* snprintf has a weird return value. It returns what would have been
1700 * written given a large enough buffer. In the case that the prefix is
1701 * longer then our buffer(128), it messes up the calculations below
1702 * possibly causing heap corruption. To avoid this we double check and
1703 * set the length at the maximum (size minus null byte)
1704 */
1705 prefixLen += len;
1706 if (prefixLen >= sizeof(prefixBuf)) {
1707 prefixLen = sizeof(prefixBuf) - 1;
1708 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1709 }
1710 if (suffixLen >= sizeof(suffixBuf)) {
1711 suffixLen = sizeof(suffixBuf) - 1;
1712 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1713 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1714 }
1715
1716 /* the following code is tragically unreadable */
1717
1718 size_t numLines;
1719 char* p;
1720 size_t bufferSize;
1721 const char* pm;
1722
1723 if (prefixSuffixIsHeaderFooter) {
1724 /* we're just wrapping message with a header/footer */
1725 numLines = 1;
1726 } else {
1727 pm = entry->message;
1728 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001729
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001730 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001731 * The line-end finding here must match the line-end finding
1732 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001733 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001734 while (pm < (entry->message + entry->messageLen)) {
1735 if (*pm++ == '\n') numLines++;
1736 }
1737 /* plus one line for anything not newline-terminated at the end */
1738 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1739 }
1740
1741 /*
1742 * this is an upper bound--newlines in message may be counted
1743 * extraneously
1744 */
1745 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1746 if (p_format->printable_output) {
1747 /* Calculate extra length to convert non-printable to printable */
1748 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1749 } else {
1750 bufferSize += entry->messageLen;
1751 }
1752
1753 if (defaultBufferSize >= bufferSize) {
1754 ret = defaultBuffer;
1755 } else {
1756 ret = (char*)malloc(bufferSize);
1757
1758 if (ret == NULL) {
1759 return ret;
1760 }
1761 }
1762
1763 ret[0] = '\0'; /* to start strcat off */
1764
1765 p = ret;
1766 pm = entry->message;
1767
1768 if (prefixSuffixIsHeaderFooter) {
1769 strcat(p, prefixBuf);
1770 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001771 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001772 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001773 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001774 strncat(p, entry->message, entry->messageLen);
1775 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001776 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001777 strcat(p, suffixBuf);
1778 p += suffixLen;
1779 } else {
1780 do {
1781 const char* lineStart;
1782 size_t lineLen;
1783 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001784
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001785 /* Find the next end-of-line in message */
1786 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1787 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001788
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001789 strcat(p, prefixBuf);
1790 p += prefixLen;
1791 if (p_format->printable_output) {
1792 p += convertPrintable(p, lineStart, lineLen);
1793 } else {
1794 strncat(p, lineStart, lineLen);
1795 p += lineLen;
1796 }
1797 strcat(p, suffixBuf);
1798 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001799
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001800 if (*pm == '\n') pm++;
1801 } while (pm < (entry->message + entry->messageLen));
1802 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001803
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001804 if (p_outLength != NULL) {
1805 *p_outLength = p - ret;
1806 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001807
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001808 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001809}
1810
1811/**
1812 * Either print or do not print log line, based on filter
1813 *
1814 * Returns count bytes written
1815 */
1816
Tom Cherry2d9779e2019-02-08 11:46:19 -08001817int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001818 int ret;
1819 char defaultBuffer[512];
1820 char* outBuffer = NULL;
1821 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001822
Tom Cherry71ba1642019-01-10 10:37:36 -08001823 outBuffer =
1824 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001825
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001826 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001827
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001828 do {
1829 ret = write(fd, outBuffer, totalLen);
1830 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001831
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001832 if (ret < 0) {
1833 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1834 ret = 0;
1835 goto done;
1836 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001837
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001838 if (((size_t)ret) < totalLen) {
1839 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1840 goto done;
1841 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001842
1843done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001844 if (outBuffer != defaultBuffer) {
1845 free(outBuffer);
1846 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001847
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001848 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001849}