blob: e32878acf3118b2f7b886072b69ed82dda9eb12c [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
Mark Salyzyna04464a2014-04-30 08:50:53 -070029#include <stdint.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020033#include <sys/param.h>
William Roberts8a5b9ca2016-04-08 12:13:17 -070034#include <sys/types.h>
Tom Cherry91589842019-04-25 13:10:30 -070035#include <wchar.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070036
Mark Salyzyn4cbed022015-08-31 15:53:41 -070037#include <cutils/list.h>
Tom Cherrybbbf0892019-10-09 10:53:37 -070038
Mark Salyzynaeaaf812016-09-30 13:30:33 -070039#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070040#include <log/logprint.h>
Tom Cherrybbbf0892019-10-09 10:53:37 -070041#include <private/android_logger.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070042
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
Tom Cherry7d045f62019-09-30 12:58:55 -0700294#ifndef __MINGW32__
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700295static const char tz[] = "TZ";
296static const char utc[] = "UTC";
Tom Cherry7d045f62019-09-30 12:58:55 -0700297#endif
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700298
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700299/**
300 * Returns FORMAT_OFF on invalid string
301 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800302AndroidLogPrintFormat android_log_formatFromString(const char* formatString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800303 static AndroidLogPrintFormat format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700304
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800305 /* clang-format off */
306 if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF;
307 else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS;
308 else if (!strcmp(formatString, "tag")) format = FORMAT_TAG;
309 else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD;
310 else if (!strcmp(formatString, "raw")) format = FORMAT_RAW;
311 else if (!strcmp(formatString, "time")) format = FORMAT_TIME;
312 else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME;
313 else if (!strcmp(formatString, "long")) format = FORMAT_LONG;
314 else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR;
315 else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR;
316 else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC;
317 else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC;
318 else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE;
319 else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR;
320 else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE;
321 else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH;
322 else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
323 else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
324 else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
Tom Cherry71ba1642019-01-10 10:37:36 -0800325 /* clang-format on */
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800326
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800327#ifndef __MINGW32__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800328 else {
329 extern char* tzname[2];
330 static const char gmt[] = "GMT";
331 char* cp = getenv(tz);
332 if (cp) {
333 cp = strdup(cp);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700334 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800335 setenv(tz, formatString, 1);
336 /*
337 * Run tzset here to determine if the timezone is legitimate. If the
338 * zone is GMT, check if that is what was asked for, if not then
339 * did not match any on the system; report an error to caller.
340 */
341 tzset();
342 if (!tzname[0] ||
Tom Cherry71ba1642019-01-10 10:37:36 -0800343 ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
344 && strcasecmp(formatString, utc) && strcasecmp(formatString, gmt))) { /* ok */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800345 if (cp) {
346 setenv(tz, cp, 1);
347 } else {
348 unsetenv(tz);
349 }
350 tzset();
351 format = FORMAT_OFF;
352 } else {
353 format = FORMAT_MODIFIER_ZONE;
354 }
355 free(cp);
356 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800357#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700358
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800359 return format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700360}
361
362/**
363 * filterExpression: a single filter expression
364 * eg "AT:d"
365 *
366 * returns 0 on success and -1 on invalid expression
367 *
368 * Assumes single threaded execution
369 */
370
Tom Cherry2d9779e2019-02-08 11:46:19 -0800371int android_log_addFilterRule(AndroidLogFormat* p_format, const char* filterExpression) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800372 size_t tagNameLength;
373 android_LogPriority pri = ANDROID_LOG_DEFAULT;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700374
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800375 tagNameLength = strcspn(filterExpression, ":");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700376
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800377 if (tagNameLength == 0) {
378 goto error;
379 }
380
381 if (filterExpression[tagNameLength] == ':') {
382 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
383
384 if (pri == ANDROID_LOG_UNKNOWN) {
385 goto error;
386 }
387 }
388
389 if (0 == strncmp("*", filterExpression, tagNameLength)) {
390 /*
391 * This filter expression refers to the global filter
392 * The default level for this is DEBUG if the priority
393 * is unspecified
394 */
395 if (pri == ANDROID_LOG_DEFAULT) {
396 pri = ANDROID_LOG_DEBUG;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700397 }
398
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800399 p_format->global_pri = pri;
400 } else {
401 /*
402 * for filter expressions that don't refer to the global
403 * filter, the default is verbose if the priority is unspecified
404 */
405 if (pri == ANDROID_LOG_DEFAULT) {
406 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700407 }
408
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800409 char* tagName;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700410
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700411/*
412 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800413 * Darwin doesn't have strndup, everything else does
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700414 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415#ifdef HAVE_STRNDUP
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800416 tagName = strndup(filterExpression, tagNameLength);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700417#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800418 /* a few extra bytes copied... */
419 tagName = strdup(filterExpression);
420 tagName[tagNameLength] = '\0';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421#endif /*HAVE_STRNDUP*/
422
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800423 FilterInfo* p_fi = filterinfo_new(tagName, pri);
424 free(tagName);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800426 p_fi->p_next = p_format->filters;
427 p_format->filters = p_fi;
428 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800430 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700431error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800432 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433}
434
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800435#ifndef HAVE_STRSEP
436/* KISS replacement helper for below */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800437static char* strsep(char** stringp, const char* delim) {
438 char* token;
439 char* ret = *stringp;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800440
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800441 if (!ret || !*ret) {
442 return NULL;
443 }
444 token = strpbrk(ret, delim);
445 if (token) {
446 *token = '\0';
447 ++token;
448 } else {
449 token = ret + strlen(ret);
450 }
451 *stringp = token;
452 return ret;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800453}
454#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700455
456/**
457 * filterString: a comma/whitespace-separated set of filter expressions
458 *
459 * eg "AT:d *:i"
460 *
461 * returns 0 on success and -1 on invalid expression
462 *
463 * Assumes single threaded execution
464 *
465 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800466int android_log_addFilterString(AndroidLogFormat* p_format, const char* filterString) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800467 char* filterStringCopy = strdup(filterString);
468 char* p_cur = filterStringCopy;
469 char* p_ret;
470 int err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700471
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800472 /* Yes, I'm using strsep */
473 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
474 /* ignore whitespace-only entries */
475 if (p_ret[0] != '\0') {
476 err = android_log_addFilterRule(p_format, p_ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700477
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800478 if (err < 0) {
479 goto error;
480 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700481 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800482 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700483
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800484 free(filterStringCopy);
485 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700486error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800487 free(filterStringCopy);
488 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700489}
490
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491/**
492 * Splits a wire-format buffer into an AndroidLogEntry
493 * entry allocated by caller. Pointers will point directly into buf
494 *
495 * Returns 0 on success and -1 on invalid wire format (entry will be
496 * in unspecified state)
497 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800498int android_log_processLogBuffer(struct logger_entry* buf, AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800499 entry->message = NULL;
500 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800501
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800502 entry->tv_sec = buf->sec;
503 entry->tv_nsec = buf->nsec;
504 entry->uid = -1;
505 entry->pid = buf->pid;
506 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700507
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800508 /*
509 * format: <priority:1><tag:N>\0<message:N>\0
510 *
511 * tag str
512 * starts at buf->msg+1
513 * msg
514 * starts at buf->msg+1+len(tag)+1
515 *
516 * The message may have been truncated by the kernel log driver.
517 * When that happens, we must null-terminate the message ourselves.
518 */
519 if (buf->len < 3) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700520 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800521 * An well-formed entry must consist of at least a priority
522 * and two null characters
Kenny Root4bf3c022011-09-30 17:10:14 -0700523 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800524 fprintf(stderr, "+++ LOG: entry too small\n");
525 return -1;
526 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700527
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800528 int msgStart = -1;
529 int msgEnd = -1;
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700530
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800531 int i;
532 char* msg = buf->msg;
Tom Cherry441054a2019-10-15 16:53:11 -0700533 if (buf->hdr_size != sizeof(struct logger_entry)) {
534 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
535 return -1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800536 }
Tom Cherry441054a2019-10-15 16:53:11 -0700537 entry->uid = buf->uid;
538
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800539 for (i = 1; i < buf->len; i++) {
540 if (msg[i] == '\0') {
541 if (msgStart == -1) {
542 msgStart = i + 1;
543 } else {
544 msgEnd = i;
545 break;
546 }
547 }
548 }
549
550 if (msgStart == -1) {
551 /* +++ LOG: malformed log message, DYB */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700552 for (i = 1; i < buf->len; i++) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800553 /* odd characters in tag? */
554 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
555 msg[i] = '\0';
556 msgStart = i + 1;
557 break;
558 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700559 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700560 if (msgStart == -1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800561 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich63f4a842011-10-17 10:45:03 -0700562 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800563 }
564 if (msgEnd == -1) {
565 /* incoming message not null-terminated; force it */
566 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
567 msg[msgEnd] = '\0';
568 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700569
Tom Cherry71ba1642019-01-10 10:37:36 -0800570 entry->priority = static_cast<android_LogPriority>(msg[0]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800571 entry->tag = msg + 1;
572 entry->tagLen = msgStart - 1;
573 entry->message = msg + msgStart;
574 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700575
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800576 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700577}
578
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700579static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800580 while ((*len) && isspace(*(*cp))) {
581 ++(*cp);
582 --(*len);
583 }
584 if (c == INT_MAX) return *len;
585 if ((*len) && (*(*cp) == c)) {
586 ++(*cp);
587 --(*len);
588 return true;
589 }
590 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700591}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000592
593/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700594 * Recursively convert binary log data to printable form.
595 *
596 * This needs to be recursive because you can have lists of lists.
597 *
598 * If we run out of room, we stop processing immediately. It's important
599 * for us to check for space on every output element to avoid producing
600 * garbled output.
601 *
602 * Returns 0 on success, 1 on buffer full, -1 on failure.
603 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700604enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800605 TYPE_OBJECTS = '1',
606 TYPE_BYTES = '2',
607 TYPE_MILLISECONDS = '3',
608 TYPE_ALLOCATIONS = '4',
609 TYPE_ID = '5',
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800610 TYPE_PERCENT = '6',
611 TYPE_MONOTONIC = 's'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700612};
613
Tom Cherry71ba1642019-01-10 10:37:36 -0800614static int android_log_printBinaryEvent(const unsigned char** pEventData, size_t* pEventDataLen,
615 char** pOutBuf, size_t* pOutBufLen, const char** fmtStr,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800616 size_t* fmtLen) {
617 const unsigned char* eventData = *pEventData;
618 size_t eventDataLen = *pEventDataLen;
619 char* outBuf = *pOutBuf;
620 char* outBufSave = outBuf;
621 size_t outBufLen = *pOutBufLen;
622 size_t outBufLenSave = outBufLen;
623 unsigned char type;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800624 size_t outCount = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800625 int result = 0;
626 const char* cp;
627 size_t len;
628 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700629
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800630 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800631
Tom Cherrybbbf0892019-10-09 10:53:37 -0700632 type = *eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700633
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800634 cp = NULL;
635 len = 0;
636 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
637 cp = *fmtStr;
638 len = *fmtLen;
639 }
640 /*
641 * event.logtag format specification:
642 *
643 * Optionally, after the tag names can be put a description for the value(s)
644 * of the tag. Description are in the format
645 * (<name>|data type[|data unit])
646 * Multiple values are separated by commas.
647 *
648 * The data type is a number from the following values:
649 * 1: int
650 * 2: long
651 * 3: string
652 * 4: list
653 * 5: float
654 *
655 * The data unit is a number taken from the following list:
656 * 1: Number of objects
657 * 2: Number of bytes
658 * 3: Number of milliseconds
659 * 4: Number of allocations
660 * 5: Id
661 * 6: Percent
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800662 * s: Number of seconds (monotonic time)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800663 * Default value for data of type int/long is 2 (bytes).
664 */
665 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700666 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800667 } else {
668 char* outBufLastSpace = NULL;
669
670 findChar(&cp, &len, INT_MAX);
671 while (len && *cp && (*cp != '|') && (*cp != ')')) {
672 if (outBufLen <= 0) {
673 /* halt output */
674 goto no_room;
675 }
676 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
677 *outBuf = *cp;
678 ++outBuf;
679 ++cp;
680 --outBufLen;
681 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700682 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800683 if (outBufLastSpace) {
684 outBufLen += outBuf - outBufLastSpace;
685 outBuf = outBufLastSpace;
686 }
687 if (outBufLen <= 0) {
688 /* halt output */
689 goto no_room;
690 }
691 if (outBufSave != outBuf) {
692 *outBuf = '=';
693 ++outBuf;
694 --outBufLen;
695 }
696
697 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800698 static const unsigned char typeTable[] = {EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING,
699 EVENT_TYPE_LIST, EVENT_TYPE_FLOAT};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800700
Tom Cherry71ba1642019-01-10 10:37:36 -0800701 if ((*cp >= '1') && (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800702 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700703 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700704
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800705 if (len) {
706 ++cp;
707 --len;
708 } else {
709 /* reset the format */
710 outBuf = outBufSave;
711 outBufLen = outBufLenSave;
712 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700713 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800714 }
715 outCount = 0;
716 lval = 0;
717 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700718 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800719 /* 32-bit signed int */
720 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700721 if (eventDataLen < sizeof(android_event_int_t)) return -1;
722 auto* event_int = reinterpret_cast<const android_event_int_t*>(eventData);
723 lval = event_int->data;
724 eventData += sizeof(android_event_int_t);
725 eventDataLen -= sizeof(android_event_int_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800726 }
727 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700728 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800729 /* 64-bit signed long */
Tom Cherrybbbf0892019-10-09 10:53:37 -0700730 if (eventDataLen < sizeof(android_event_long_t)) {
731 return -1;
732 }
733 {
734 auto* event_long = reinterpret_cast<const android_event_long_t*>(eventData);
735 lval = event_long->data;
736 }
737 eventData += sizeof(android_event_long_t);
738 eventDataLen -= sizeof(android_event_long_t);
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700739 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800740 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
741 if (outCount < outBufLen) {
742 outBuf += outCount;
743 outBufLen -= outCount;
744 } else {
745 /* halt output */
746 goto no_room;
747 }
748 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700749 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800750 /* float */
751 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700752 if (eventDataLen < sizeof(android_event_float_t)) return -1;
753 auto* event_float = reinterpret_cast<const android_event_float_t*>(eventData);
754 float fval = event_float->data;
755 eventData += sizeof(android_event_int_t);
756 eventDataLen -= sizeof(android_event_int_t);
Jeff Brown44193d92015-04-28 12:47:02 -0700757
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800758 outCount = snprintf(outBuf, outBufLen, "%f", fval);
759 if (outCount < outBufLen) {
760 outBuf += outCount;
761 outBufLen -= outCount;
762 } else {
763 /* halt output */
764 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700765 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800766 }
767 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700768 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800769 /* UTF-8 chars, not NULL-terminated */
770 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700771 if (eventDataLen < sizeof(android_event_string_t)) return -1;
772 auto* event_string = reinterpret_cast<const android_event_string_t*>(eventData);
773 unsigned int strLen = event_string->length;
774 eventData += sizeof(android_event_string_t);
775 eventDataLen -= sizeof(android_event_string_t);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700776
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800777 if (eventDataLen < strLen) {
778 result = -1; /* mark truncated */
779 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700780 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700781
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800782 if (cp && (strLen == 0)) {
783 /* reset the format if no content */
784 outBuf = outBufSave;
785 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700786 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800787 if (strLen < outBufLen) {
788 memcpy(outBuf, eventData, strLen);
789 outBuf += strLen;
790 outBufLen -= strLen;
791 } else {
792 if (outBufLen > 0) {
793 /* copy what we can */
794 memcpy(outBuf, eventData, outBufLen);
795 outBuf += outBufLen;
796 outBufLen -= outBufLen;
797 }
798 if (!result) result = 1; /* if not truncated, return no room */
799 }
800 eventData += strLen;
801 eventDataLen -= strLen;
802 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700803 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800804 }
805 case EVENT_TYPE_LIST:
806 /* N items, all different types */
807 {
Tom Cherrybbbf0892019-10-09 10:53:37 -0700808 if (eventDataLen < sizeof(android_event_list_t)) return -1;
809 auto* event_list = reinterpret_cast<const android_event_list_t*>(eventData);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800810
Tom Cherrybbbf0892019-10-09 10:53:37 -0700811 int8_t count = event_list->element_count;
812 eventData += sizeof(android_event_list_t);
813 eventDataLen -= sizeof(android_event_list_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800814
815 if (outBufLen <= 0) goto no_room;
816
817 *outBuf++ = '[';
818 outBufLen--;
819
Tom Cherrybbbf0892019-10-09 10:53:37 -0700820 for (int i = 0; i < count; i++) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800821 result = android_log_printBinaryEvent(&eventData, &eventDataLen, &outBuf, &outBufLen,
822 fmtStr, fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800823 if (result != 0) goto bail;
824
825 if (i < (count - 1)) {
826 if (outBufLen <= 0) goto no_room;
827 *outBuf++ = ',';
828 outBufLen--;
829 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700830 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800831
832 if (outBufLen <= 0) goto no_room;
833
834 *outBuf++ = ']';
835 outBufLen--;
836 }
837 break;
838 default:
839 fprintf(stderr, "Unknown binary event type %d\n", type);
840 return -1;
841 }
842 if (cp && len) {
843 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
844 switch (*cp) {
845 case TYPE_OBJECTS:
846 outCount = 0;
847 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
848 break;
849 case TYPE_BYTES:
850 if ((lval != 0) && ((lval % 1024) == 0)) {
851 /* repaint with multiplier */
Tom Cherry71ba1642019-01-10 10:37:36 -0800852 static const char suffixTable[] = {'K', 'M', 'G', 'T'};
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800853 size_t idx = 0;
854 outBuf -= outCount;
855 outBufLen += outCount;
856 do {
857 lval /= 1024;
858 if ((lval % 1024) != 0) break;
Tom Cherry71ba1642019-01-10 10:37:36 -0800859 } while (++idx < ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
860 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval, suffixTable[idx]);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800861 } else {
862 outCount = snprintf(outBuf, outBufLen, "B");
863 }
864 break;
865 case TYPE_MILLISECONDS:
Tom Cherry71ba1642019-01-10 10:37:36 -0800866 if (((lval <= -1000) || (1000 <= lval)) && (outBufLen || (outBuf[-1] == '0'))) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800867 /* repaint as (fractional) seconds, possibly saving space */
868 if (outBufLen) outBuf[0] = outBuf[-1];
869 outBuf[-1] = outBuf[-2];
870 outBuf[-2] = outBuf[-3];
871 outBuf[-3] = '.';
872 while ((outBufLen == 0) || (*outBuf == '0')) {
873 --outBuf;
874 ++outBufLen;
875 }
876 if (*outBuf != '.') {
877 ++outBuf;
878 --outBufLen;
879 }
880 outCount = snprintf(outBuf, outBufLen, "s");
881 } else {
882 outCount = snprintf(outBuf, outBufLen, "ms");
883 }
884 break;
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800885 case TYPE_MONOTONIC: {
886 static const uint64_t minute = 60;
887 static const uint64_t hour = 60 * minute;
888 static const uint64_t day = 24 * hour;
889
890 /* Repaint as unsigned seconds, minutes, hours ... */
891 outBuf -= outCount;
892 outBufLen += outCount;
893 uint64_t val = lval;
894 if (val >= day) {
895 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 "d ", val / day);
896 if (outCount >= outBufLen) break;
897 outBuf += outCount;
898 outBufLen -= outCount;
899 val = (val % day) + day;
900 }
901 if (val >= minute) {
902 if (val >= hour) {
Tom Cherry71ba1642019-01-10 10:37:36 -0800903 outCount = snprintf(outBuf, outBufLen, "%" PRIu64 ":", (val / hour) % (day / hour));
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800904 if (outCount >= outBufLen) break;
905 outBuf += outCount;
906 outBufLen -= outCount;
907 }
908 outCount =
Tom Cherry71ba1642019-01-10 10:37:36 -0800909 snprintf(outBuf, outBufLen, (val >= hour) ? "%02" PRIu64 ":" : "%" PRIu64 ":",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800910 (val / minute) % (hour / minute));
911 if (outCount >= outBufLen) break;
912 outBuf += outCount;
913 outBufLen -= outCount;
914 }
Tom Cherry71ba1642019-01-10 10:37:36 -0800915 outCount = snprintf(outBuf, outBufLen, (val >= minute) ? "%02" PRIu64 : "%" PRIu64 "s",
Mark Salyzyn5768d3d2016-11-10 10:24:44 -0800916 val % minute);
917 } break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800918 case TYPE_ALLOCATIONS:
919 outCount = 0;
920 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
921 break;
922 case TYPE_ID:
923 outCount = 0;
924 break;
925 case TYPE_PERCENT:
926 outCount = snprintf(outBuf, outBufLen, "%%");
927 break;
928 default: /* ? */
929 outCount = 0;
930 break;
931 }
932 ++cp;
933 --len;
934 if (outCount < outBufLen) {
935 outBuf += outCount;
936 outBufLen -= outCount;
937 } else if (outCount) {
938 /* halt output */
939 goto no_room;
940 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700941 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800942 if (!findChar(&cp, &len, ')')) len = 0;
943 if (!findChar(&cp, &len, ',')) len = 0;
944 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700945
946bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800947 *pEventData = eventData;
948 *pEventDataLen = eventDataLen;
949 *pOutBuf = outBuf;
950 *pOutBufLen = outBufLen;
951 if (cp) {
952 *fmtStr = cp;
953 *fmtLen = len;
954 }
955 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700956
957no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800958 result = 1;
959 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700960}
961
962/**
963 * Convert a binary log entry to ASCII form.
964 *
965 * For convenience we mimic the processLogBuffer API. There is no
966 * pre-defined output length for the binary data, since we're free to format
967 * it however we choose, which means we can't really use a fixed-size buffer
968 * here.
969 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800970int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800971 struct logger_entry* buf, AndroidLogEntry* entry,
Tom Cherry71ba1642019-01-10 10:37:36 -0800972 [[maybe_unused]] const EventTagMap* map, /* only on !__ANDROID__ */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800973 char* messageBuf, int messageBufLen) {
974 size_t inCount;
975 uint32_t tagIndex;
976 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700977
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800978 entry->message = NULL;
979 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800980
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800981 entry->tv_sec = buf->sec;
982 entry->tv_nsec = buf->nsec;
983 entry->priority = ANDROID_LOG_INFO;
984 entry->uid = -1;
985 entry->pid = buf->pid;
986 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700987
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800988 eventData = (const unsigned char*)buf->msg;
Tom Cherry441054a2019-10-15 16:53:11 -0700989 if (buf->hdr_size != sizeof(struct logger_entry)) {
990 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
991 return -1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800992 }
Tom Cherry441054a2019-10-15 16:53:11 -0700993 if (buf->lid == LOG_ID_SECURITY) {
994 entry->priority = ANDROID_LOG_WARN;
995 }
996 entry->uid = buf->uid;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800997 inCount = buf->len;
Tom Cherrybbbf0892019-10-09 10:53:37 -0700998 if (inCount < sizeof(android_event_header_t)) return -1;
999 auto* event_header = reinterpret_cast<const android_event_header_t*>(eventData);
1000 tagIndex = event_header->tag;
1001 eventData += sizeof(android_event_header_t);
1002 inCount -= sizeof(android_event_header_t);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001003
1004 entry->tagLen = 0;
1005 entry->tag = NULL;
1006#ifdef __ANDROID__
1007 if (map != NULL) {
1008 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1009 }
1010#endif
1011
1012 /*
1013 * If we don't have a map, or didn't find the tag number in the map,
1014 * stuff a generated tag value into the start of the output buffer and
1015 * shift the buffer pointers down.
1016 */
1017 if (entry->tag == NULL) {
1018 size_t tagLen;
1019
1020 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1021 if (tagLen >= (size_t)messageBufLen) {
1022 tagLen = messageBufLen - 1;
1023 }
1024 entry->tag = messageBuf;
1025 entry->tagLen = tagLen;
1026 messageBuf += tagLen + 1;
1027 messageBufLen -= tagLen + 1;
1028 }
1029
1030 /*
1031 * Format the event log data into the buffer.
1032 */
1033 const char* fmtStr = NULL;
1034 size_t fmtLen = 0;
1035#ifdef __ANDROID__
1036 if (descriptive_output && map) {
1037 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1038 }
1039#endif
1040
1041 char* outBuf = messageBuf;
1042 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1043 int result = 0;
1044
1045 if ((inCount > 0) || fmtLen) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001046 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, &fmtStr,
1047 &fmtLen);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001048 }
1049 if ((result == 1) && fmtStr) {
1050 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001051 eventData = (const unsigned char*)buf->msg;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001052 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001053 outBuf = messageBuf;
1054 outRemaining = messageBufLen - 1;
Tom Cherry71ba1642019-01-10 10:37:36 -08001055 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf, &outRemaining, NULL, NULL);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001056 }
1057 if (result < 0) {
1058 fprintf(stderr, "Binary log entry conversion failed\n");
1059 }
1060 if (result) {
1061 if (!outRemaining) {
1062 /* make space to leave an indicator */
1063 --outBuf;
1064 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001065 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001066 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1067 outRemaining--;
1068 /* pretend we ate all the data to prevent log stutter */
1069 inCount = 0;
1070 if (result > 0) result = 0;
1071 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001072
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001073 /* eat the silly terminating '\n' */
1074 if (inCount == 1 && *eventData == '\n') {
1075 eventData++;
1076 inCount--;
1077 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001078
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001079 if (inCount != 0) {
1080 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1081 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001082
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001083 /*
1084 * Terminate the buffer. The NUL byte does not count as part of
1085 * entry->messageLen.
1086 */
1087 *outBuf = '\0';
1088 entry->messageLen = outBuf - messageBuf;
1089 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001090
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001091 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001092
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001093 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001094}
1095
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001096/*
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001097 * Convert to printable from message to p buffer, return string length. If p is
1098 * NULL, do not copy, but still return the expected string length.
1099 */
Tom Cherry91589842019-04-25 13:10:30 -07001100size_t convertPrintable(char* p, const char* message, size_t messageLen) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001101 char* begin = p;
1102 bool print = p != NULL;
Tom Cherry91589842019-04-25 13:10:30 -07001103 mbstate_t mb_state = {};
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001104
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001105 while (messageLen) {
1106 char buf[6];
1107 ssize_t len = sizeof(buf) - 1;
1108 if ((size_t)len > messageLen) {
1109 len = messageLen;
1110 }
Tom Cherry91589842019-04-25 13:10:30 -07001111 len = mbrtowc(nullptr, message, len, &mb_state);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001112
1113 if (len < 0) {
Tom Cherry91589842019-04-25 13:10:30 -07001114 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001115 len = 1;
1116 } else {
1117 buf[0] = '\0';
1118 if (len == 1) {
1119 if (*message == '\a') {
1120 strcpy(buf, "\\a");
1121 } else if (*message == '\b') {
1122 strcpy(buf, "\\b");
1123 } else if (*message == '\t') {
1124 strcpy(buf, "\t"); /* Do not escape tabs */
1125 } else if (*message == '\v') {
1126 strcpy(buf, "\\v");
1127 } else if (*message == '\f') {
1128 strcpy(buf, "\\f");
1129 } else if (*message == '\r') {
1130 strcpy(buf, "\\r");
1131 } else if (*message == '\\') {
1132 strcpy(buf, "\\\\");
1133 } else if ((*message < ' ') || (*message & 0x80)) {
Tom Cherry91589842019-04-25 13:10:30 -07001134 snprintf(buf, sizeof(buf), "\\x%02X", static_cast<unsigned char>(*message));
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001135 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001136 }
1137 if (!buf[0]) {
1138 strncpy(buf, message, len);
1139 buf[len] = '\0';
1140 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001141 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001142 if (print) {
1143 strcpy(p, buf);
1144 }
1145 p += strlen(buf);
1146 message += len;
1147 messageLen -= len;
1148 }
1149 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001150}
1151
Tom Cherry7d045f62019-09-30 12:58:55 -07001152#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001153static char* readSeconds(char* e, struct timespec* t) {
1154 unsigned long multiplier;
1155 char* p;
1156 t->tv_sec = strtoul(e, &p, 10);
1157 if (*p != '.') {
1158 return NULL;
1159 }
1160 t->tv_nsec = 0;
1161 multiplier = NS_PER_SEC;
1162 while (isdigit(*++p) && (multiplier /= 10)) {
1163 t->tv_nsec += (*p - '0') * multiplier;
1164 }
1165 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001166}
1167
Tom Cherry71ba1642019-01-10 10:37:36 -08001168static struct timespec* sumTimespec(struct timespec* left, struct timespec* right) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001169 left->tv_nsec += right->tv_nsec;
1170 left->tv_sec += right->tv_sec;
1171 if (left->tv_nsec >= (long)NS_PER_SEC) {
1172 left->tv_nsec -= NS_PER_SEC;
1173 left->tv_sec += 1;
1174 }
1175 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001176}
1177
Tom Cherry71ba1642019-01-10 10:37:36 -08001178static struct timespec* subTimespec(struct timespec* result, struct timespec* left,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001179 struct timespec* right) {
1180 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1181 result->tv_sec = left->tv_sec - right->tv_sec;
1182 if (result->tv_nsec < 0) {
1183 result->tv_nsec += NS_PER_SEC;
1184 result->tv_sec -= 1;
1185 }
1186 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001187}
1188
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001189static long long nsecTimespec(struct timespec* now) {
1190 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001191}
1192
Tom Cherry71ba1642019-01-10 10:37:36 -08001193static void convertMonotonic(struct timespec* result, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001194 struct listnode* node;
1195 struct conversionList {
1196 struct listnode node; /* first */
1197 struct timespec time;
1198 struct timespec convert;
1199 } * list, *next;
1200 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001201
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001202 /* If we do not have a conversion list, build one up */
1203 if (list_empty(&convertHead)) {
1204 bool suspended_pending = false;
Tom Cherry71ba1642019-01-10 10:37:36 -08001205 struct timespec suspended_monotonic = {0, 0};
1206 struct timespec suspended_diff = {0, 0};
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001207
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001208 /*
1209 * Read dmesg for _some_ synchronization markers and insert
1210 * Anything in the Android Logger before the dmesg logging span will
1211 * be highly suspect regarding the monotonic time calculations.
1212 */
1213 FILE* p = popen("/system/bin/dmesg", "re");
1214 if (p) {
1215 char* line = NULL;
1216 size_t len = 0;
1217 while (getline(&line, &len, p) > 0) {
1218 static const char suspend[] = "PM: suspend entry ";
1219 static const char resume[] = "PM: suspend exit ";
1220 static const char healthd[] = "healthd";
1221 static const char battery[] = ": battery ";
1222 static const char suspended[] = "Suspended for ";
1223 struct timespec monotonic;
1224 struct tm tm;
1225 char *cp, *e = line;
1226 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001227
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001228 if (*e == '<') {
1229 while (*e && (*e != '>')) {
1230 ++e;
1231 }
1232 if (*e != '>') {
1233 continue;
1234 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001235 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001236 if (*e != '[') {
1237 continue;
1238 }
1239 while (*++e == ' ') {
1240 ;
1241 }
1242 e = readSeconds(e, &monotonic);
1243 if (!e || (*e != ']')) {
1244 continue;
1245 }
1246
1247 if ((e = strstr(e, suspend))) {
1248 e += sizeof(suspend) - 1;
1249 } else if ((e = strstr(line, resume))) {
1250 e += sizeof(resume) - 1;
1251 } else if (((e = strstr(line, healthd))) &&
1252 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1253 /* NB: healthd is roughly 150us late, worth the price to
1254 * deal with ntp-induced or hardware clock drift. */
1255 e += sizeof(battery) - 1;
1256 } else if ((e = strstr(line, suspended))) {
1257 e += sizeof(suspended) - 1;
1258 e = readSeconds(e, &time);
1259 if (!e) {
1260 continue;
1261 }
1262 add_entry = false;
1263 suspended_pending = true;
1264 suspended_monotonic = monotonic;
1265 suspended_diff = time;
1266 } else {
1267 continue;
1268 }
1269 if (add_entry) {
1270 /* look for "????-??-?? ??:??:??.????????? UTC" */
1271 cp = strstr(e, " UTC");
1272 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1273 continue;
1274 }
1275 e = cp - 29;
1276 cp = readSeconds(cp - 10, &time);
1277 if (!cp) {
1278 continue;
1279 }
1280 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1281 if (!cp) {
1282 continue;
1283 }
1284 cp = getenv(tz);
1285 if (cp) {
1286 cp = strdup(cp);
1287 }
1288 setenv(tz, utc, 1);
1289 time.tv_sec = mktime(&tm);
1290 if (cp) {
1291 setenv(tz, cp, 1);
1292 free(cp);
1293 } else {
1294 unsetenv(tz);
1295 }
Tom Cherry71ba1642019-01-10 10:37:36 -08001296 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001297 list_init(&list->node);
1298 list->time = time;
1299 subTimespec(&list->convert, &time, &monotonic);
1300 list_add_tail(&convertHead, &list->node);
1301 }
1302 if (suspended_pending && !list_empty(&convertHead)) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001303 list = node_to_item(list_tail(&convertHead), struct conversionList, node);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001304 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1305 &suspended_monotonic)
1306 ->tv_sec > 0) {
1307 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001308 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001309 } else {
1310 /* suspend */
1311 convert = list->convert;
1312 }
1313 time = suspended_monotonic;
1314 sumTimespec(&time, &convert);
1315 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001316 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001317 list_init(&list->node);
1318 list->time = time;
1319 list->convert = convert;
1320 list_add_tail(&convertHead, &list->node);
1321 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001322 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001323 list_init(&list->node);
1324 list->time = time;
1325 sumTimespec(&list->time, &suspended_diff);
1326 list->convert = convert;
1327 sumTimespec(&list->convert, &suspended_diff);
1328 list_add_tail(&convertHead, &list->node);
1329 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001330 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001331 }
1332 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001333 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001334 /* last entry is our current time conversion */
Tom Cherry71ba1642019-01-10 10:37:36 -08001335 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001336 list_init(&list->node);
1337 clock_gettime(CLOCK_REALTIME, &list->time);
1338 clock_gettime(CLOCK_MONOTONIC, &convert);
1339 clock_gettime(CLOCK_MONOTONIC, &time);
1340 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1341 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1342 /* Calculate conversion factor */
1343 subTimespec(&list->convert, &list->time, &time);
1344 list_add_tail(&convertHead, &list->node);
1345 if (suspended_pending) {
1346 /* manufacture a suspend @ point before */
1347 subTimespec(&convert, &list->convert, &suspended_diff);
1348 time = suspended_monotonic;
1349 sumTimespec(&time, &convert);
1350 /* breakpoint just after sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001351 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001352 list_init(&list->node);
1353 list->time = time;
1354 sumTimespec(&list->time, &suspended_diff);
1355 list->convert = convert;
1356 sumTimespec(&list->convert, &suspended_diff);
1357 list_add_head(&convertHead, &list->node);
1358 /* breakpoint just before sleep */
Tom Cherry71ba1642019-01-10 10:37:36 -08001359 list = static_cast<conversionList*>(calloc(1, sizeof(conversionList)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001360 list_init(&list->node);
1361 list->time = time;
1362 list->convert = convert;
1363 list_add_head(&convertHead, &list->node);
1364 }
1365 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001366
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001367 /* Find the breakpoint in the conversion list */
1368 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1369 next = NULL;
1370 list_for_each(node, &convertHead) {
1371 next = node_to_item(node, struct conversionList, node);
1372 if (entry->tv_sec < next->time.tv_sec) {
1373 break;
1374 } else if (entry->tv_sec == next->time.tv_sec) {
1375 if (entry->tv_nsec < next->time.tv_nsec) {
1376 break;
1377 }
1378 }
1379 list = next;
1380 }
1381
1382 /* blend time from one breakpoint to the next */
1383 convert = list->convert;
1384 if (next) {
1385 unsigned long long total, run;
1386
1387 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1388 time.tv_sec = entry->tv_sec;
1389 time.tv_nsec = entry->tv_nsec;
1390 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1391 if (run < total) {
1392 long long crun;
1393
1394 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1395 f *= run;
1396 f /= total;
1397 crun = f;
1398 convert.tv_sec += crun / (long long)NS_PER_SEC;
1399 if (crun < 0) {
1400 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1401 if (convert.tv_nsec < 0) {
1402 convert.tv_nsec += NS_PER_SEC;
1403 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001404 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001405 } else {
1406 convert.tv_nsec += crun % NS_PER_SEC;
1407 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1408 convert.tv_nsec -= NS_PER_SEC;
1409 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001410 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001411 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001412 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001413 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001414
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001415 /* Apply the correction factor */
1416 result->tv_sec = entry->tv_sec;
1417 result->tv_nsec = entry->tv_nsec;
1418 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001419}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001420#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001421
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001422/**
1423 * Formats a log message into a buffer
1424 *
1425 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1426 * If return value != defaultBuffer, caller must call free()
1427 * Returns NULL on malloc error
1428 */
1429
Tom Cherry2d9779e2019-02-08 11:46:19 -08001430char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
1431 size_t defaultBufferSize, const AndroidLogEntry* entry,
1432 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001433#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001434 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001435#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001436 struct tm* ptm;
1437 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1438 char timeBuf[64];
1439 char prefixBuf[128], suffixBuf[128];
1440 char priChar;
1441 int prefixSuffixIsHeaderFooter = 0;
1442 char* ret;
1443 time_t now;
1444 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001445
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001446 priChar = filterPriToChar(entry->priority);
1447 size_t prefixLen = 0, suffixLen = 0;
1448 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001449
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001450 /*
1451 * Get the current date/time in pretty form
1452 *
1453 * It's often useful when examining a log with "less" to jump to
1454 * a specific point in the file by searching for the date/time stamp.
1455 * For this reason it's very annoying to have regexp meta characters
1456 * in the time stamp. Don't use forward slashes, parenthesis,
1457 * brackets, asterisks, or other special chars here.
1458 *
1459 * The caller may have affected the timezone environment, this is
1460 * expected to be sensitive to that.
1461 */
1462 now = entry->tv_sec;
1463 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001464#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001465 if (p_format->monotonic_output) {
1466 /* prevent convertMonotonic from being called if logd is monotonic */
1467 if (android_log_clockid() != CLOCK_MONOTONIC) {
1468 struct timespec time;
1469 convertMonotonic(&time, entry);
1470 now = time.tv_sec;
1471 nsec = time.tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001472 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001473 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001474#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001475 if (now < 0) {
1476 nsec = NS_PER_SEC - nsec;
1477 }
1478 if (p_format->epoch_output || p_format->monotonic_output) {
1479 ptm = NULL;
Tom Cherry71ba1642019-01-10 10:37:36 -08001480 snprintf(timeBuf, sizeof(timeBuf), p_format->monotonic_output ? "%6lld" : "%19lld",
1481 (long long)now);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001482 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001483#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001484 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001485#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001486 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001487#endif
Tom Cherry71ba1642019-01-10 10:37:36 -08001488 strftime(timeBuf, sizeof(timeBuf), &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001489 }
1490 len = strlen(timeBuf);
1491 if (p_format->nsec_time_output) {
1492 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1493 } else if (p_format->usec_time_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001494 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld", nsec / US_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001495 } else {
Tom Cherry71ba1642019-01-10 10:37:36 -08001496 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld", nsec / MS_PER_NSEC);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001497 }
1498 if (p_format->zone_output && ptm) {
1499 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1500 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001501
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001502 /*
1503 * Construct a buffer containing the log header and log message.
1504 */
1505 if (p_format->colored_output) {
Tom Cherry71ba1642019-01-10 10:37:36 -08001506 prefixLen =
1507 snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm", colorFromPri(entry->priority));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001508 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
George Burgess IV487de272018-05-15 18:56:36 -07001509
1510 const char suffixContents[] = "\x1B[0m";
1511 strcpy(suffixBuf, suffixContents);
1512 suffixLen = strlen(suffixContents);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001513 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001514
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001515 char uid[16];
1516 uid[0] = '\0';
1517 if (p_format->uid_output) {
1518 if (entry->uid >= 0) {
1519/*
1520 * This code is Android specific, bionic guarantees that
1521 * calls to non-reentrant getpwuid() are thread safe.
1522 */
Tom Cherrye2187bf2020-01-27 15:45:52 -08001523#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001524 struct passwd* pwd = getpwuid(entry->uid);
1525 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1526 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1527 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001528#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001529 {
1530 /* Not worth parsing package list, names all longer than 5 */
1531 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1532 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001533 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001534 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001535 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001536 }
1537
1538 switch (p_format->format) {
1539 case FORMAT_TAG:
Tom Cherry71ba1642019-01-10 10:37:36 -08001540 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c/%-8.*s: ", priChar,
1541 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001542 strcpy(suffixBuf + suffixLen, "\n");
1543 ++suffixLen;
1544 break;
1545 case FORMAT_PROCESS:
Tom Cherry71ba1642019-01-10 10:37:36 -08001546 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen, " (%.*s)\n",
1547 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001548 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
Tom Cherry71ba1642019-01-10 10:37:36 -08001549 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d) ", priChar,
1550 uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001551 break;
1552 case FORMAT_THREAD:
Tom Cherry71ba1642019-01-10 10:37:36 -08001553 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen, "%c(%s%5d:%5d) ",
1554 priChar, uid, entry->pid, entry->tid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001555 strcpy(suffixBuf + suffixLen, "\n");
1556 ++suffixLen;
1557 break;
1558 case FORMAT_RAW:
1559 prefixBuf[prefixLen] = 0;
1560 len = 0;
1561 strcpy(suffixBuf + suffixLen, "\n");
1562 ++suffixLen;
1563 break;
1564 case FORMAT_TIME:
1565 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001566 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar, (int)entry->tagLen, entry->tag, uid,
1567 entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001568 strcpy(suffixBuf + suffixLen, "\n");
1569 ++suffixLen;
1570 break;
1571 case FORMAT_THREADTIME:
1572 ret = strchr(uid, ':');
1573 if (ret) {
1574 *ret = ' ';
1575 }
1576 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001577 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid, entry->tid, priChar,
1578 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001579 strcpy(suffixBuf + suffixLen, "\n");
1580 ++suffixLen;
1581 break;
1582 case FORMAT_LONG:
1583 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
Tom Cherry71ba1642019-01-10 10:37:36 -08001584 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid, entry->tid, priChar,
1585 (int)entry->tagLen, entry->tag);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001586 strcpy(suffixBuf + suffixLen, "\n\n");
1587 suffixLen += 2;
1588 prefixSuffixIsHeaderFooter = 1;
1589 break;
1590 case FORMAT_BRIEF:
1591 default:
Tom Cherry71ba1642019-01-10 10:37:36 -08001592 len =
1593 snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1594 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen, entry->tag, uid, entry->pid);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001595 strcpy(suffixBuf + suffixLen, "\n");
1596 ++suffixLen;
1597 break;
1598 }
1599
1600 /* snprintf has a weird return value. It returns what would have been
1601 * written given a large enough buffer. In the case that the prefix is
1602 * longer then our buffer(128), it messes up the calculations below
1603 * possibly causing heap corruption. To avoid this we double check and
1604 * set the length at the maximum (size minus null byte)
1605 */
1606 prefixLen += len;
1607 if (prefixLen >= sizeof(prefixBuf)) {
1608 prefixLen = sizeof(prefixBuf) - 1;
1609 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1610 }
1611 if (suffixLen >= sizeof(suffixBuf)) {
1612 suffixLen = sizeof(suffixBuf) - 1;
1613 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1614 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1615 }
1616
1617 /* the following code is tragically unreadable */
1618
1619 size_t numLines;
1620 char* p;
1621 size_t bufferSize;
1622 const char* pm;
1623
1624 if (prefixSuffixIsHeaderFooter) {
1625 /* we're just wrapping message with a header/footer */
1626 numLines = 1;
1627 } else {
1628 pm = entry->message;
1629 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001630
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001631 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001632 * The line-end finding here must match the line-end finding
1633 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001634 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001635 while (pm < (entry->message + entry->messageLen)) {
1636 if (*pm++ == '\n') numLines++;
1637 }
1638 /* plus one line for anything not newline-terminated at the end */
1639 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1640 }
1641
1642 /*
1643 * this is an upper bound--newlines in message may be counted
1644 * extraneously
1645 */
1646 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1647 if (p_format->printable_output) {
1648 /* Calculate extra length to convert non-printable to printable */
1649 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1650 } else {
1651 bufferSize += entry->messageLen;
1652 }
1653
1654 if (defaultBufferSize >= bufferSize) {
1655 ret = defaultBuffer;
1656 } else {
1657 ret = (char*)malloc(bufferSize);
1658
1659 if (ret == NULL) {
1660 return ret;
1661 }
1662 }
1663
1664 ret[0] = '\0'; /* to start strcat off */
1665
1666 p = ret;
1667 pm = entry->message;
1668
1669 if (prefixSuffixIsHeaderFooter) {
1670 strcat(p, prefixBuf);
1671 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001672 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001673 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001674 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001675 strncat(p, entry->message, entry->messageLen);
1676 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001677 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001678 strcat(p, suffixBuf);
1679 p += suffixLen;
1680 } else {
1681 do {
1682 const char* lineStart;
1683 size_t lineLen;
1684 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001685
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001686 /* Find the next end-of-line in message */
1687 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1688 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001689
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001690 strcat(p, prefixBuf);
1691 p += prefixLen;
1692 if (p_format->printable_output) {
1693 p += convertPrintable(p, lineStart, lineLen);
1694 } else {
1695 strncat(p, lineStart, lineLen);
1696 p += lineLen;
1697 }
1698 strcat(p, suffixBuf);
1699 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001700
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001701 if (*pm == '\n') pm++;
1702 } while (pm < (entry->message + entry->messageLen));
1703 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001704
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001705 if (p_outLength != NULL) {
1706 *p_outLength = p - ret;
1707 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001708
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001709 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001710}
1711
1712/**
1713 * Either print or do not print log line, based on filter
1714 *
1715 * Returns count bytes written
1716 */
1717
Tom Cherry2d9779e2019-02-08 11:46:19 -08001718int android_log_printLogLine(AndroidLogFormat* p_format, int fd, const AndroidLogEntry* entry) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001719 int ret;
1720 char defaultBuffer[512];
1721 char* outBuffer = NULL;
1722 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001723
Tom Cherry71ba1642019-01-10 10:37:36 -08001724 outBuffer =
1725 android_log_formatLogLine(p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001726
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001727 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001728
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001729 do {
1730 ret = write(fd, outBuffer, totalLen);
1731 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001732
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001733 if (ret < 0) {
1734 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1735 ret = 0;
1736 goto done;
1737 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001738
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001739 if (((size_t)ret) < totalLen) {
1740 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1741 goto done;
1742 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001743
1744done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001745 if (outBuffer != defaultBuffer) {
1746 free(outBuffer);
1747 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001748
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001749 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001750}