blob: 2ade7b02c1e8d15634c1b78cf0f1534856824583 [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
18#define _GNU_SOURCE /* for asprintf */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080019#ifndef __MINGW32__
20#define HAVE_STRSEP
21#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070022
Mark Salyzyna04464a2014-04-30 08:50:53 -070023#include <assert.h>
24#include <ctype.h>
25#include <errno.h>
Mark Salyzyn4fd05072016-10-18 11:30:11 -070026#include <inttypes.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080027#ifndef __MINGW32__
William Roberts8a5b9ca2016-04-08 12:13:17 -070028#include <pwd.h>
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080029#endif
Pierre Zurekead88fc2010-10-17 22:39:37 +020030#include <stdbool.h>
Mark Salyzyna04464a2014-04-30 08:50:53 -070031#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
Pierre Zurekead88fc2010-10-17 22:39:37 +020035#include <sys/param.h>
William Roberts8a5b9ca2016-04-08 12:13:17 -070036#include <sys/types.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070037
Mark Salyzyn4cbed022015-08-31 15:53:41 -070038#include <cutils/list.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070039#include <log/log.h>
Colin Cross9227bd32013-07-23 16:59:20 -070040#include <log/logprint.h>
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070041
Mark Salyzyn018a96d2016-03-01 13:45:42 -080042#include "log_portability.h"
Mark Salyzynbe1d3c22016-03-10 08:25:33 -080043
Mark Salyzyn4cbed022015-08-31 15:53:41 -070044#define MS_PER_NSEC 1000000
45#define US_PER_NSEC 1000
46
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080047#ifndef MIN
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080048#define MIN(a, b) (((a) < (b)) ? (a) : (b))
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -080049#endif
50
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070051typedef struct FilterInfo_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080052 char* mTag;
53 android_LogPriority mPri;
54 struct FilterInfo_t* p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070055} FilterInfo;
56
57struct AndroidLogFormat_t {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080058 android_LogPriority global_pri;
59 FilterInfo* filters;
60 AndroidLogPrintFormat format;
61 bool colored_output;
62 bool usec_time_output;
63 bool nsec_time_output;
64 bool printable_output;
65 bool year_output;
66 bool zone_output;
67 bool epoch_output;
68 bool monotonic_output;
69 bool uid_output;
70 bool descriptive_output;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070071};
72
Pierre Zurekead88fc2010-10-17 22:39:37 +020073/*
Mark Salyzyn4fd05072016-10-18 11:30:11 -070074 * API issues prevent us from exposing "descriptive" in AndroidLogFormat_t
75 * during android_log_processBinaryLogBuffer(), so we break layering.
76 */
77static bool descriptive_output = false;
78
79/*
Pierre Zurekead88fc2010-10-17 22:39:37 +020080 * gnome-terminal color tags
81 * See http://misc.flogisoft.com/bash/tip_colors_and_formatting
82 * for ideas on how to set the forground color of the text for xterm.
83 * The color manipulation character stream is defined as:
84 * ESC [ 3 8 ; 5 ; <color#> m
85 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080086#define ANDROID_COLOR_BLUE 75
Pierre Zurekead88fc2010-10-17 22:39:37 +020087#define ANDROID_COLOR_DEFAULT 231
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080088#define ANDROID_COLOR_GREEN 40
89#define ANDROID_COLOR_ORANGE 166
90#define ANDROID_COLOR_RED 196
91#define ANDROID_COLOR_YELLOW 226
Pierre Zurekead88fc2010-10-17 22:39:37 +020092
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080093static FilterInfo* filterinfo_new(const char* tag, android_LogPriority pri) {
94 FilterInfo* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070095
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080096 p_ret = (FilterInfo*)calloc(1, sizeof(FilterInfo));
97 p_ret->mTag = strdup(tag);
98 p_ret->mPri = pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -070099
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800100 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700101}
102
Mark Salyzyna04464a2014-04-30 08:50:53 -0700103/* balance to above, filterinfo_free left unimplemented */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104
105/*
106 * Note: also accepts 0-9 priorities
107 * returns ANDROID_LOG_UNKNOWN if the character is unrecognized
108 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800109static android_LogPriority filterCharToPri(char c) {
110 android_LogPriority pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700111
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800112 c = tolower(c);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700113
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800114 if (c >= '0' && c <= '9') {
115 if (c >= ('0' + ANDROID_LOG_SILENT)) {
116 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700117 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800118 pri = (android_LogPriority)(c - '0');
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700119 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800120 } else if (c == 'v') {
121 pri = ANDROID_LOG_VERBOSE;
122 } else if (c == 'd') {
123 pri = ANDROID_LOG_DEBUG;
124 } else if (c == 'i') {
125 pri = ANDROID_LOG_INFO;
126 } else if (c == 'w') {
127 pri = ANDROID_LOG_WARN;
128 } else if (c == 'e') {
129 pri = ANDROID_LOG_ERROR;
130 } else if (c == 'f') {
131 pri = ANDROID_LOG_FATAL;
132 } else if (c == 's') {
133 pri = ANDROID_LOG_SILENT;
134 } else if (c == '*') {
135 pri = ANDROID_LOG_DEFAULT;
136 } else {
137 pri = ANDROID_LOG_UNKNOWN;
138 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800140 return pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700141}
142
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800143static char filterPriToChar(android_LogPriority pri) {
144 switch (pri) {
145 /* clang-format off */
146 case ANDROID_LOG_VERBOSE: return 'V';
147 case ANDROID_LOG_DEBUG: return 'D';
148 case ANDROID_LOG_INFO: return 'I';
149 case ANDROID_LOG_WARN: return 'W';
150 case ANDROID_LOG_ERROR: return 'E';
151 case ANDROID_LOG_FATAL: return 'F';
152 case ANDROID_LOG_SILENT: return 'S';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700153
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800154 case ANDROID_LOG_DEFAULT:
155 case ANDROID_LOG_UNKNOWN:
156 default: return '?';
157 /* clang-format on */
158 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700159}
160
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800161static int colorFromPri(android_LogPriority pri) {
162 switch (pri) {
163 /* clang-format off */
164 case ANDROID_LOG_VERBOSE: return ANDROID_COLOR_DEFAULT;
165 case ANDROID_LOG_DEBUG: return ANDROID_COLOR_BLUE;
166 case ANDROID_LOG_INFO: return ANDROID_COLOR_GREEN;
167 case ANDROID_LOG_WARN: return ANDROID_COLOR_ORANGE;
168 case ANDROID_LOG_ERROR: return ANDROID_COLOR_RED;
169 case ANDROID_LOG_FATAL: return ANDROID_COLOR_RED;
170 case ANDROID_LOG_SILENT: return ANDROID_COLOR_DEFAULT;
Pierre Zurekead88fc2010-10-17 22:39:37 +0200171
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800172 case ANDROID_LOG_DEFAULT:
173 case ANDROID_LOG_UNKNOWN:
174 default: return ANDROID_COLOR_DEFAULT;
175 /* clang-format on */
176 }
Pierre Zurekead88fc2010-10-17 22:39:37 +0200177}
178
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800179static android_LogPriority filterPriForTag(AndroidLogFormat* p_format,
180 const char* tag) {
181 FilterInfo* p_curFilter;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700182
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800183 for (p_curFilter = p_format->filters; p_curFilter != NULL;
184 p_curFilter = p_curFilter->p_next) {
185 if (0 == strcmp(tag, p_curFilter->mTag)) {
186 if (p_curFilter->mPri == ANDROID_LOG_DEFAULT) {
187 return p_format->global_pri;
188 } else {
189 return p_curFilter->mPri;
190 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700191 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800192 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700193
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800194 return p_format->global_pri;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700195}
196
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700197/**
198 * returns 1 if this log line should be printed based on its priority
199 * and tag, and 0 if it should not
200 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800201LIBLOG_ABI_PUBLIC int android_log_shouldPrintLine(AndroidLogFormat* p_format,
202 const char* tag,
203 android_LogPriority pri) {
204 return pri >= filterPriForTag(p_format, tag);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700205}
206
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800207LIBLOG_ABI_PUBLIC AndroidLogFormat* android_log_format_new() {
208 AndroidLogFormat* p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700209
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800210 p_ret = calloc(1, sizeof(AndroidLogFormat));
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700211
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800212 p_ret->global_pri = ANDROID_LOG_VERBOSE;
213 p_ret->format = FORMAT_BRIEF;
214 p_ret->colored_output = false;
215 p_ret->usec_time_output = false;
216 p_ret->nsec_time_output = false;
217 p_ret->printable_output = false;
218 p_ret->year_output = false;
219 p_ret->zone_output = false;
220 p_ret->epoch_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800221#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800222 p_ret->monotonic_output = android_log_clockid() == CLOCK_MONOTONIC;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800223#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800224 p_ret->monotonic_output = false;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800225#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800226 p_ret->uid_output = false;
227 p_ret->descriptive_output = false;
228 descriptive_output = false;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700229
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800230 return p_ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700231}
232
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700233static list_declare(convertHead);
234
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800235LIBLOG_ABI_PUBLIC void android_log_format_free(AndroidLogFormat* p_format) {
236 FilterInfo *p_info, *p_info_old;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700237
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800238 p_info = p_format->filters;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700239
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800240 while (p_info != NULL) {
241 p_info_old = p_info;
242 p_info = p_info->p_next;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700243
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800244 free(p_info_old);
245 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700246
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800247 free(p_format);
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700248
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800249 /* Free conversion resource, can always be reconstructed */
250 while (!list_empty(&convertHead)) {
251 struct listnode* node = list_head(&convertHead);
252 list_remove(node);
253 free(node);
254 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700255}
256
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800257LIBLOG_ABI_PUBLIC int android_log_setPrintFormat(AndroidLogFormat* p_format,
258 AndroidLogPrintFormat format) {
259 switch (format) {
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700260 case FORMAT_MODIFIER_COLOR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800261 p_format->colored_output = true;
262 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700263 case FORMAT_MODIFIER_TIME_USEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800264 p_format->usec_time_output = true;
265 return 0;
Mark Salyzyn9b4d7e12017-01-30 09:16:09 -0800266 case FORMAT_MODIFIER_TIME_NSEC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800267 p_format->nsec_time_output = true;
268 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700269 case FORMAT_MODIFIER_PRINTABLE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800270 p_format->printable_output = true;
271 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700272 case FORMAT_MODIFIER_YEAR:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800273 p_format->year_output = true;
274 return 0;
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700275 case FORMAT_MODIFIER_ZONE:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800276 p_format->zone_output = !p_format->zone_output;
277 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700278 case FORMAT_MODIFIER_EPOCH:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800279 p_format->epoch_output = true;
280 return 0;
Mark Salyzyn4cbed022015-08-31 15:53:41 -0700281 case FORMAT_MODIFIER_MONOTONIC:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800282 p_format->monotonic_output = true;
283 return 0;
Mark Salyzyn90e7af32015-12-07 16:52:42 -0800284 case FORMAT_MODIFIER_UID:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800285 p_format->uid_output = true;
286 return 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700287 case FORMAT_MODIFIER_DESCRIPT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800288 p_format->descriptive_output = true;
289 descriptive_output = true;
290 return 0;
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700291 default:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800292 break;
293 }
294 p_format->format = format;
295 return 1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700296}
297
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700298static const char tz[] = "TZ";
299static const char utc[] = "UTC";
300
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700301/**
302 * Returns FORMAT_OFF on invalid string
303 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800304LIBLOG_ABI_PUBLIC AndroidLogPrintFormat
305android_log_formatFromString(const char* formatString) {
306 static AndroidLogPrintFormat format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700307
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800308 /* clang-format off */
309 if (!strcmp(formatString, "brief")) format = FORMAT_BRIEF;
310 else if (!strcmp(formatString, "process")) format = FORMAT_PROCESS;
311 else if (!strcmp(formatString, "tag")) format = FORMAT_TAG;
312 else if (!strcmp(formatString, "thread")) format = FORMAT_THREAD;
313 else if (!strcmp(formatString, "raw")) format = FORMAT_RAW;
314 else if (!strcmp(formatString, "time")) format = FORMAT_TIME;
315 else if (!strcmp(formatString, "threadtime")) format = FORMAT_THREADTIME;
316 else if (!strcmp(formatString, "long")) format = FORMAT_LONG;
317 else if (!strcmp(formatString, "color")) format = FORMAT_MODIFIER_COLOR;
318 else if (!strcmp(formatString, "colour")) format = FORMAT_MODIFIER_COLOR;
319 else if (!strcmp(formatString, "usec")) format = FORMAT_MODIFIER_TIME_USEC;
320 else if (!strcmp(formatString, "nsec")) format = FORMAT_MODIFIER_TIME_NSEC;
321 else if (!strcmp(formatString, "printable")) format = FORMAT_MODIFIER_PRINTABLE;
322 else if (!strcmp(formatString, "year")) format = FORMAT_MODIFIER_YEAR;
323 else if (!strcmp(formatString, "zone")) format = FORMAT_MODIFIER_ZONE;
324 else if (!strcmp(formatString, "epoch")) format = FORMAT_MODIFIER_EPOCH;
325 else if (!strcmp(formatString, "monotonic")) format = FORMAT_MODIFIER_MONOTONIC;
326 else if (!strcmp(formatString, "uid")) format = FORMAT_MODIFIER_UID;
327 else if (!strcmp(formatString, "descriptive")) format = FORMAT_MODIFIER_DESCRIPT;
328 /* clang-format on */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800329#ifndef __MINGW32__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800330 else {
331 extern char* tzname[2];
332 static const char gmt[] = "GMT";
333 char* cp = getenv(tz);
334 if (cp) {
335 cp = strdup(cp);
Mark Salyzynf28f6a92015-08-31 08:01:33 -0700336 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800337 setenv(tz, formatString, 1);
338 /*
339 * Run tzset here to determine if the timezone is legitimate. If the
340 * zone is GMT, check if that is what was asked for, if not then
341 * did not match any on the system; report an error to caller.
342 */
343 tzset();
344 if (!tzname[0] ||
345 ((!strcmp(tzname[0], utc) || !strcmp(tzname[0], gmt)) /* error? */
346 && strcasecmp(formatString, utc) &&
347 strcasecmp(formatString, gmt))) { /* ok */
348 if (cp) {
349 setenv(tz, cp, 1);
350 } else {
351 unsetenv(tz);
352 }
353 tzset();
354 format = FORMAT_OFF;
355 } else {
356 format = FORMAT_MODIFIER_ZONE;
357 }
358 free(cp);
359 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800360#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700361
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800362 return format;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700363}
364
365/**
366 * filterExpression: a single filter expression
367 * eg "AT:d"
368 *
369 * returns 0 on success and -1 on invalid expression
370 *
371 * Assumes single threaded execution
372 */
373
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800374LIBLOG_ABI_PUBLIC int android_log_addFilterRule(AndroidLogFormat* p_format,
375 const char* filterExpression) {
376 size_t tagNameLength;
377 android_LogPriority pri = ANDROID_LOG_DEFAULT;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700378
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800379 tagNameLength = strcspn(filterExpression, ":");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700380
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800381 if (tagNameLength == 0) {
382 goto error;
383 }
384
385 if (filterExpression[tagNameLength] == ':') {
386 pri = filterCharToPri(filterExpression[tagNameLength + 1]);
387
388 if (pri == ANDROID_LOG_UNKNOWN) {
389 goto error;
390 }
391 }
392
393 if (0 == strncmp("*", filterExpression, tagNameLength)) {
394 /*
395 * This filter expression refers to the global filter
396 * The default level for this is DEBUG if the priority
397 * is unspecified
398 */
399 if (pri == ANDROID_LOG_DEFAULT) {
400 pri = ANDROID_LOG_DEBUG;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700401 }
402
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800403 p_format->global_pri = pri;
404 } else {
405 /*
406 * for filter expressions that don't refer to the global
407 * filter, the default is verbose if the priority is unspecified
408 */
409 if (pri == ANDROID_LOG_DEFAULT) {
410 pri = ANDROID_LOG_VERBOSE;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700411 }
412
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800413 char* tagName;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700414
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700415/*
416 * Presently HAVE_STRNDUP is never defined, so the second case is always taken
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800417 * Darwin doesn't have strndup, everything else does
Mark Salyzynb932b2f2015-05-15 09:01:58 -0700418 */
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700419#ifdef HAVE_STRNDUP
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800420 tagName = strndup(filterExpression, tagNameLength);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800422 /* a few extra bytes copied... */
423 tagName = strdup(filterExpression);
424 tagName[tagNameLength] = '\0';
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700425#endif /*HAVE_STRNDUP*/
426
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800427 FilterInfo* p_fi = filterinfo_new(tagName, pri);
428 free(tagName);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700429
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800430 p_fi->p_next = p_format->filters;
431 p_format->filters = p_fi;
432 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700433
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800434 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700435error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800436 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700437}
438
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800439#ifndef HAVE_STRSEP
440/* KISS replacement helper for below */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800441static char* strsep(char** stringp, const char* delim) {
442 char* token;
443 char* ret = *stringp;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800444
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800445 if (!ret || !*ret) {
446 return NULL;
447 }
448 token = strpbrk(ret, delim);
449 if (token) {
450 *token = '\0';
451 ++token;
452 } else {
453 token = ret + strlen(ret);
454 }
455 *stringp = token;
456 return ret;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -0800457}
458#endif
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700459
460/**
461 * filterString: a comma/whitespace-separated set of filter expressions
462 *
463 * eg "AT:d *:i"
464 *
465 * returns 0 on success and -1 on invalid expression
466 *
467 * Assumes single threaded execution
468 *
469 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800470LIBLOG_ABI_PUBLIC int android_log_addFilterString(AndroidLogFormat* p_format,
471 const char* filterString) {
472 char* filterStringCopy = strdup(filterString);
473 char* p_cur = filterStringCopy;
474 char* p_ret;
475 int err;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700476
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800477 /* Yes, I'm using strsep */
478 while (NULL != (p_ret = strsep(&p_cur, " \t,"))) {
479 /* ignore whitespace-only entries */
480 if (p_ret[0] != '\0') {
481 err = android_log_addFilterRule(p_format, p_ret);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700482
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800483 if (err < 0) {
484 goto error;
485 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700486 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800487 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700488
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800489 free(filterStringCopy);
490 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700491error:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800492 free(filterStringCopy);
493 return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700494}
495
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700496/**
497 * Splits a wire-format buffer into an AndroidLogEntry
498 * entry allocated by caller. Pointers will point directly into buf
499 *
500 * Returns 0 on success and -1 on invalid wire format (entry will be
501 * in unspecified state)
502 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800503LIBLOG_ABI_PUBLIC int android_log_processLogBuffer(struct logger_entry* buf,
504 AndroidLogEntry* entry) {
505 entry->message = NULL;
506 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800507
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800508 entry->tv_sec = buf->sec;
509 entry->tv_nsec = buf->nsec;
510 entry->uid = -1;
511 entry->pid = buf->pid;
512 entry->tid = buf->tid;
Kenny Root4bf3c022011-09-30 17:10:14 -0700513
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800514 /*
515 * format: <priority:1><tag:N>\0<message:N>\0
516 *
517 * tag str
518 * starts at buf->msg+1
519 * msg
520 * starts at buf->msg+1+len(tag)+1
521 *
522 * The message may have been truncated by the kernel log driver.
523 * When that happens, we must null-terminate the message ourselves.
524 */
525 if (buf->len < 3) {
Kenny Root4bf3c022011-09-30 17:10:14 -0700526 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800527 * An well-formed entry must consist of at least a priority
528 * and two null characters
Kenny Root4bf3c022011-09-30 17:10:14 -0700529 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800530 fprintf(stderr, "+++ LOG: entry too small\n");
531 return -1;
532 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700533
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800534 int msgStart = -1;
535 int msgEnd = -1;
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700536
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800537 int i;
538 char* msg = buf->msg;
539 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
540 if (buf2->hdr_size) {
541 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
542 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
543 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
544 return -1;
Mark Salyzyn40b21552013-12-18 12:59:01 -0800545 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800546 msg = ((char*)buf2) + buf2->hdr_size;
547 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
548 entry->uid = ((struct logger_entry_v4*)buf)->uid;
549 }
550 }
551 for (i = 1; i < buf->len; i++) {
552 if (msg[i] == '\0') {
553 if (msgStart == -1) {
554 msgStart = i + 1;
555 } else {
556 msgEnd = i;
557 break;
558 }
559 }
560 }
561
562 if (msgStart == -1) {
563 /* +++ LOG: malformed log message, DYB */
Nick Kraleviche1ede152011-10-18 15:23:33 -0700564 for (i = 1; i < buf->len; i++) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800565 /* odd characters in tag? */
566 if ((msg[i] <= ' ') || (msg[i] == ':') || (msg[i] >= 0x7f)) {
567 msg[i] = '\0';
568 msgStart = i + 1;
569 break;
570 }
Nick Kraleviche1ede152011-10-18 15:23:33 -0700571 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700572 if (msgStart == -1) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800573 msgStart = buf->len - 1; /* All tag, no message, print truncates */
Nick Kralevich63f4a842011-10-17 10:45:03 -0700574 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800575 }
576 if (msgEnd == -1) {
577 /* incoming message not null-terminated; force it */
578 msgEnd = buf->len - 1; /* may result in msgEnd < msgStart */
579 msg[msgEnd] = '\0';
580 }
Jeff Sharkeya820a0e2011-10-26 18:40:39 -0700581
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800582 entry->priority = msg[0];
583 entry->tag = msg + 1;
584 entry->tagLen = msgStart - 1;
585 entry->message = msg + msgStart;
586 entry->messageLen = (msgEnd < msgStart) ? 0 : (msgEnd - msgStart);
Nick Kralevich63f4a842011-10-17 10:45:03 -0700587
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800588 return 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700589}
590
591/*
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000592 * Extract a 4-byte value from a byte stream.
593 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800594static inline uint32_t get4LE(const uint8_t* src) {
595 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000596}
597
598/*
599 * Extract an 8-byte value from a byte stream.
600 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800601static inline uint64_t get8LE(const uint8_t* src) {
602 uint32_t low, high;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000603
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800604 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
605 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
606 return ((uint64_t)high << 32) | (uint64_t)low;
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000607}
608
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700609static bool findChar(const char** cp, size_t* len, int c) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800610 while ((*len) && isspace(*(*cp))) {
611 ++(*cp);
612 --(*len);
613 }
614 if (c == INT_MAX) return *len;
615 if ((*len) && (*(*cp) == c)) {
616 ++(*cp);
617 --(*len);
618 return true;
619 }
620 return false;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700621}
Mark Salyzyn8470dad2015-03-06 20:42:57 +0000622
623/*
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700624 * Recursively convert binary log data to printable form.
625 *
626 * This needs to be recursive because you can have lists of lists.
627 *
628 * If we run out of room, we stop processing immediately. It's important
629 * for us to check for space on every output element to avoid producing
630 * garbled output.
631 *
632 * Returns 0 on success, 1 on buffer full, -1 on failure.
633 */
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700634enum objectType {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800635 TYPE_OBJECTS = '1',
636 TYPE_BYTES = '2',
637 TYPE_MILLISECONDS = '3',
638 TYPE_ALLOCATIONS = '4',
639 TYPE_ID = '5',
640 TYPE_PERCENT = '6'
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700641};
642
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700643static int android_log_printBinaryEvent(const unsigned char** pEventData,
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800644 size_t* pEventDataLen, char** pOutBuf,
645 size_t* pOutBufLen, const char** fmtStr,
646 size_t* fmtLen) {
647 const unsigned char* eventData = *pEventData;
648 size_t eventDataLen = *pEventDataLen;
649 char* outBuf = *pOutBuf;
650 char* outBufSave = outBuf;
651 size_t outBufLen = *pOutBufLen;
652 size_t outBufLenSave = outBufLen;
653 unsigned char type;
654 size_t outCount;
655 int result = 0;
656 const char* cp;
657 size_t len;
658 int64_t lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700659
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800660 if (eventDataLen < 1) return -1;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -0800661
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800662 type = *eventData++;
663 eventDataLen--;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700664
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800665 cp = NULL;
666 len = 0;
667 if (fmtStr && *fmtStr && fmtLen && *fmtLen && **fmtStr) {
668 cp = *fmtStr;
669 len = *fmtLen;
670 }
671 /*
672 * event.logtag format specification:
673 *
674 * Optionally, after the tag names can be put a description for the value(s)
675 * of the tag. Description are in the format
676 * (<name>|data type[|data unit])
677 * Multiple values are separated by commas.
678 *
679 * The data type is a number from the following values:
680 * 1: int
681 * 2: long
682 * 3: string
683 * 4: list
684 * 5: float
685 *
686 * The data unit is a number taken from the following list:
687 * 1: Number of objects
688 * 2: Number of bytes
689 * 3: Number of milliseconds
690 * 4: Number of allocations
691 * 5: Id
692 * 6: Percent
693 * Default value for data of type int/long is 2 (bytes).
694 */
695 if (!cp || !findChar(&cp, &len, '(')) {
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700696 len = 0;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800697 } else {
698 char* outBufLastSpace = NULL;
699
700 findChar(&cp, &len, INT_MAX);
701 while (len && *cp && (*cp != '|') && (*cp != ')')) {
702 if (outBufLen <= 0) {
703 /* halt output */
704 goto no_room;
705 }
706 outBufLastSpace = isspace(*cp) ? outBuf : NULL;
707 *outBuf = *cp;
708 ++outBuf;
709 ++cp;
710 --outBufLen;
711 --len;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700712 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800713 if (outBufLastSpace) {
714 outBufLen += outBuf - outBufLastSpace;
715 outBuf = outBufLastSpace;
716 }
717 if (outBufLen <= 0) {
718 /* halt output */
719 goto no_room;
720 }
721 if (outBufSave != outBuf) {
722 *outBuf = '=';
723 ++outBuf;
724 --outBufLen;
725 }
726
727 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
728 static const unsigned char typeTable[] = {
729 EVENT_TYPE_INT, EVENT_TYPE_LONG, EVENT_TYPE_STRING, EVENT_TYPE_LIST,
730 EVENT_TYPE_FLOAT
731 };
732
733 if ((*cp >= '1') &&
734 (*cp < (char)('1' + (sizeof(typeTable) / sizeof(typeTable[0])))) &&
735 (type != typeTable[(size_t)(*cp - '1')]))
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700736 len = 0;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700737
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800738 if (len) {
739 ++cp;
740 --len;
741 } else {
742 /* reset the format */
743 outBuf = outBufSave;
744 outBufLen = outBufLenSave;
745 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700746 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800747 }
748 outCount = 0;
749 lval = 0;
750 switch (type) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700751 case EVENT_TYPE_INT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800752 /* 32-bit signed int */
753 {
754 int32_t ival;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700755
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800756 if (eventDataLen < 4) return -1;
757 ival = get4LE(eventData);
758 eventData += 4;
759 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700760
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800761 lval = ival;
762 }
763 goto pr_lval;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700764 case EVENT_TYPE_LONG:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800765 /* 64-bit signed long */
766 if (eventDataLen < 8) return -1;
767 lval = get8LE(eventData);
768 eventData += 8;
769 eventDataLen -= 8;
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700770 pr_lval:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800771 outCount = snprintf(outBuf, outBufLen, "%" PRId64, lval);
772 if (outCount < outBufLen) {
773 outBuf += outCount;
774 outBufLen -= outCount;
775 } else {
776 /* halt output */
777 goto no_room;
778 }
779 break;
Jeff Brown44193d92015-04-28 12:47:02 -0700780 case EVENT_TYPE_FLOAT:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800781 /* float */
782 {
783 uint32_t ival;
784 float fval;
Jeff Brown44193d92015-04-28 12:47:02 -0700785
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800786 if (eventDataLen < 4) return -1;
787 ival = get4LE(eventData);
788 fval = *(float*)&ival;
789 eventData += 4;
790 eventDataLen -= 4;
Jeff Brown44193d92015-04-28 12:47:02 -0700791
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800792 outCount = snprintf(outBuf, outBufLen, "%f", fval);
793 if (outCount < outBufLen) {
794 outBuf += outCount;
795 outBufLen -= outCount;
796 } else {
797 /* halt output */
798 goto no_room;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700799 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800800 }
801 break;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700802 case EVENT_TYPE_STRING:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800803 /* UTF-8 chars, not NULL-terminated */
804 {
805 unsigned int strLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700806
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800807 if (eventDataLen < 4) return -1;
808 strLen = get4LE(eventData);
809 eventData += 4;
810 eventDataLen -= 4;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700811
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800812 if (eventDataLen < strLen) {
813 result = -1; /* mark truncated */
814 strLen = eventDataLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700815 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700816
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800817 if (cp && (strLen == 0)) {
818 /* reset the format if no content */
819 outBuf = outBufSave;
820 outBufLen = outBufLenSave;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700821 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800822 if (strLen < outBufLen) {
823 memcpy(outBuf, eventData, strLen);
824 outBuf += strLen;
825 outBufLen -= strLen;
826 } else {
827 if (outBufLen > 0) {
828 /* copy what we can */
829 memcpy(outBuf, eventData, outBufLen);
830 outBuf += outBufLen;
831 outBufLen -= outBufLen;
832 }
833 if (!result) result = 1; /* if not truncated, return no room */
834 }
835 eventData += strLen;
836 eventDataLen -= strLen;
837 if (result != 0) goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700838 break;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800839 }
840 case EVENT_TYPE_LIST:
841 /* N items, all different types */
842 {
843 unsigned char count;
844 int i;
845
846 if (eventDataLen < 1) return -1;
847
848 count = *eventData++;
849 eventDataLen--;
850
851 if (outBufLen <= 0) goto no_room;
852
853 *outBuf++ = '[';
854 outBufLen--;
855
856 for (i = 0; i < count; i++) {
857 result = android_log_printBinaryEvent(
858 &eventData, &eventDataLen, &outBuf, &outBufLen, fmtStr, fmtLen);
859 if (result != 0) goto bail;
860
861 if (i < (count - 1)) {
862 if (outBufLen <= 0) goto no_room;
863 *outBuf++ = ',';
864 outBufLen--;
865 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700866 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800867
868 if (outBufLen <= 0) goto no_room;
869
870 *outBuf++ = ']';
871 outBufLen--;
872 }
873 break;
874 default:
875 fprintf(stderr, "Unknown binary event type %d\n", type);
876 return -1;
877 }
878 if (cp && len) {
879 if (findChar(&cp, &len, '|') && findChar(&cp, &len, INT_MAX)) {
880 switch (*cp) {
881 case TYPE_OBJECTS:
882 outCount = 0;
883 /* outCount = snprintf(outBuf, outBufLen, " objects"); */
884 break;
885 case TYPE_BYTES:
886 if ((lval != 0) && ((lval % 1024) == 0)) {
887 /* repaint with multiplier */
888 static const char suffixTable[] = { 'K', 'M', 'G', 'T' };
889 size_t idx = 0;
890 outBuf -= outCount;
891 outBufLen += outCount;
892 do {
893 lval /= 1024;
894 if ((lval % 1024) != 0) break;
895 } while (++idx <
896 ((sizeof(suffixTable) / sizeof(suffixTable[0])) - 1));
897 outCount = snprintf(outBuf, outBufLen, "%" PRId64 "%cB", lval,
898 suffixTable[idx]);
899 } else {
900 outCount = snprintf(outBuf, outBufLen, "B");
901 }
902 break;
903 case TYPE_MILLISECONDS:
904 if (((lval <= -1000) || (1000 <= lval)) &&
905 (outBufLen || (outBuf[-1] == '0'))) {
906 /* repaint as (fractional) seconds, possibly saving space */
907 if (outBufLen) outBuf[0] = outBuf[-1];
908 outBuf[-1] = outBuf[-2];
909 outBuf[-2] = outBuf[-3];
910 outBuf[-3] = '.';
911 while ((outBufLen == 0) || (*outBuf == '0')) {
912 --outBuf;
913 ++outBufLen;
914 }
915 if (*outBuf != '.') {
916 ++outBuf;
917 --outBufLen;
918 }
919 outCount = snprintf(outBuf, outBufLen, "s");
920 } else {
921 outCount = snprintf(outBuf, outBufLen, "ms");
922 }
923 break;
924 case TYPE_ALLOCATIONS:
925 outCount = 0;
926 /* outCount = snprintf(outBuf, outBufLen, " allocations"); */
927 break;
928 case TYPE_ID:
929 outCount = 0;
930 break;
931 case TYPE_PERCENT:
932 outCount = snprintf(outBuf, outBufLen, "%%");
933 break;
934 default: /* ? */
935 outCount = 0;
936 break;
937 }
938 ++cp;
939 --len;
940 if (outCount < outBufLen) {
941 outBuf += outCount;
942 outBufLen -= outCount;
943 } else if (outCount) {
944 /* halt output */
945 goto no_room;
946 }
Mark Salyzyn4fd05072016-10-18 11:30:11 -0700947 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800948 if (!findChar(&cp, &len, ')')) len = 0;
949 if (!findChar(&cp, &len, ',')) len = 0;
950 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700951
952bail:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800953 *pEventData = eventData;
954 *pEventDataLen = eventDataLen;
955 *pOutBuf = outBuf;
956 *pOutBufLen = outBufLen;
957 if (cp) {
958 *fmtStr = cp;
959 *fmtLen = len;
960 }
961 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700962
963no_room:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800964 result = 1;
965 goto bail;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700966}
967
968/**
969 * Convert a binary log entry to ASCII form.
970 *
971 * For convenience we mimic the processLogBuffer API. There is no
972 * pre-defined output length for the binary data, since we're free to format
973 * it however we choose, which means we can't really use a fixed-size buffer
974 * here.
975 */
Mark Salyzynbe1d3c22016-03-10 08:25:33 -0800976LIBLOG_ABI_PUBLIC int android_log_processBinaryLogBuffer(
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800977 struct logger_entry* buf, AndroidLogEntry* entry,
978 const EventTagMap* map __unused, /* only on !__ANDROID__ */
979 char* messageBuf, int messageBufLen) {
980 size_t inCount;
981 uint32_t tagIndex;
982 const unsigned char* eventData;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700983
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800984 entry->message = NULL;
985 entry->messageLen = 0;
Mark Salyzynb1d150b2017-03-02 07:47:21 -0800986
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800987 entry->tv_sec = buf->sec;
988 entry->tv_nsec = buf->nsec;
989 entry->priority = ANDROID_LOG_INFO;
990 entry->uid = -1;
991 entry->pid = buf->pid;
992 entry->tid = buf->tid;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700993
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800994 /*
995 * Pull the tag out, fill in some additional details based on incoming
996 * buffer version (v3 adds lid, v4 adds uid).
997 */
998 eventData = (const unsigned char*)buf->msg;
999 struct logger_entry_v2* buf2 = (struct logger_entry_v2*)buf;
1000 if (buf2->hdr_size) {
1001 if ((buf2->hdr_size < sizeof(((struct log_msg*)NULL)->entry_v1)) ||
1002 (buf2->hdr_size > sizeof(((struct log_msg*)NULL)->entry))) {
1003 fprintf(stderr, "+++ LOG: entry illegal hdr_size\n");
1004 return -1;
1005 }
1006 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
1007 if ((buf2->hdr_size >= sizeof(struct logger_entry_v3)) &&
1008 (((struct logger_entry_v3*)buf)->lid == LOG_ID_SECURITY)) {
1009 entry->priority = ANDROID_LOG_WARN;
1010 }
1011 if (buf2->hdr_size >= sizeof(struct logger_entry_v4)) {
1012 entry->uid = ((struct logger_entry_v4*)buf)->uid;
1013 }
1014 }
1015 inCount = buf->len;
1016 if (inCount < 4) return -1;
1017 tagIndex = get4LE(eventData);
1018 eventData += 4;
1019 inCount -= 4;
1020
1021 entry->tagLen = 0;
1022 entry->tag = NULL;
1023#ifdef __ANDROID__
1024 if (map != NULL) {
1025 entry->tag = android_lookupEventTag_len(map, &entry->tagLen, tagIndex);
1026 }
1027#endif
1028
1029 /*
1030 * If we don't have a map, or didn't find the tag number in the map,
1031 * stuff a generated tag value into the start of the output buffer and
1032 * shift the buffer pointers down.
1033 */
1034 if (entry->tag == NULL) {
1035 size_t tagLen;
1036
1037 tagLen = snprintf(messageBuf, messageBufLen, "[%" PRIu32 "]", tagIndex);
1038 if (tagLen >= (size_t)messageBufLen) {
1039 tagLen = messageBufLen - 1;
1040 }
1041 entry->tag = messageBuf;
1042 entry->tagLen = tagLen;
1043 messageBuf += tagLen + 1;
1044 messageBufLen -= tagLen + 1;
1045 }
1046
1047 /*
1048 * Format the event log data into the buffer.
1049 */
1050 const char* fmtStr = NULL;
1051 size_t fmtLen = 0;
1052#ifdef __ANDROID__
1053 if (descriptive_output && map) {
1054 fmtStr = android_lookupEventFormat_len(map, &fmtLen, tagIndex);
1055 }
1056#endif
1057
1058 char* outBuf = messageBuf;
1059 size_t outRemaining = messageBufLen - 1; /* leave one for nul byte */
1060 int result = 0;
1061
1062 if ((inCount > 0) || fmtLen) {
1063 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
1064 &outRemaining, &fmtStr, &fmtLen);
1065 }
1066 if ((result == 1) && fmtStr) {
1067 /* We overflowed :-(, let's repaint the line w/o format dressings */
Mark Salyzynb1d150b2017-03-02 07:47:21 -08001068 eventData = (const unsigned char*)buf->msg;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001069 if (buf2->hdr_size) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001070 eventData = ((unsigned char*)buf2) + buf2->hdr_size;
Mark Salyzyn40b21552013-12-18 12:59:01 -08001071 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001072 eventData += 4;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001073 outBuf = messageBuf;
1074 outRemaining = messageBufLen - 1;
1075 result = android_log_printBinaryEvent(&eventData, &inCount, &outBuf,
1076 &outRemaining, NULL, NULL);
1077 }
1078 if (result < 0) {
1079 fprintf(stderr, "Binary log entry conversion failed\n");
1080 }
1081 if (result) {
1082 if (!outRemaining) {
1083 /* make space to leave an indicator */
1084 --outBuf;
1085 ++outRemaining;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001086 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001087 *outBuf++ = (result < 0) ? '!' : '^'; /* Error or Truncation? */
1088 outRemaining--;
1089 /* pretend we ate all the data to prevent log stutter */
1090 inCount = 0;
1091 if (result > 0) result = 0;
1092 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001093
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001094 /* eat the silly terminating '\n' */
1095 if (inCount == 1 && *eventData == '\n') {
1096 eventData++;
1097 inCount--;
1098 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001099
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001100 if (inCount != 0) {
1101 fprintf(stderr, "Warning: leftover binary log data (%zu bytes)\n", inCount);
1102 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001103
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001104 /*
1105 * Terminate the buffer. The NUL byte does not count as part of
1106 * entry->messageLen.
1107 */
1108 *outBuf = '\0';
1109 entry->messageLen = outBuf - messageBuf;
1110 assert(entry->messageLen == (messageBufLen - 1) - outRemaining);
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001111
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001112 entry->message = messageBuf;
Mark Salyzyn1a57ae32016-11-11 14:41:30 -08001113
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001114 return result;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001115}
1116
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001117/*
1118 * One utf8 character at a time
1119 *
1120 * Returns the length of the utf8 character in the buffer,
1121 * or -1 if illegal or truncated
1122 *
1123 * Open coded from libutils/Unicode.cpp, borrowed from utf8_length(),
1124 * can not remove from here because of library circular dependencies.
1125 * Expect one-day utf8_character_length with the same signature could
1126 * _also_ be part of libutils/Unicode.cpp if its usefullness needs to
1127 * propagate globally.
1128 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001129LIBLOG_WEAK ssize_t utf8_character_length(const char* src, size_t len) {
1130 const char* cur = src;
1131 const char first_char = *cur++;
1132 static const uint32_t kUnicodeMaxCodepoint = 0x0010FFFF;
1133 int32_t mask, to_ignore_mask;
1134 size_t num_to_read;
1135 uint32_t utf32;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001136
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001137 if ((first_char & 0x80) == 0) { /* ASCII */
1138 return first_char ? 1 : -1;
1139 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001140
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001141 /*
1142 * (UTF-8's character must not be like 10xxxxxx,
1143 * but 110xxxxx, 1110xxxx, ... or 1111110x)
1144 */
1145 if ((first_char & 0x40) == 0) {
1146 return -1;
1147 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001148
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001149 for (utf32 = 1, num_to_read = 1, mask = 0x40, to_ignore_mask = 0x80;
1150 num_to_read < 5 && (first_char & mask);
1151 num_to_read++, to_ignore_mask |= mask, mask >>= 1) {
1152 if (num_to_read > len) {
1153 return -1;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001154 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001155 if ((*cur & 0xC0) != 0x80) { /* can not be 10xxxxxx? */
1156 return -1;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001157 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001158 utf32 = (utf32 << 6) + (*cur++ & 0b00111111);
1159 }
1160 /* "first_char" must be (110xxxxx - 11110xxx) */
1161 if (num_to_read >= 5) {
1162 return -1;
1163 }
1164 to_ignore_mask |= mask;
1165 utf32 |= ((~to_ignore_mask) & first_char) << (6 * (num_to_read - 1));
1166 if (utf32 > kUnicodeMaxCodepoint) {
1167 return -1;
1168 }
1169 return num_to_read;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001170}
1171
1172/*
1173 * Convert to printable from message to p buffer, return string length. If p is
1174 * NULL, do not copy, but still return the expected string length.
1175 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001176static size_t convertPrintable(char* p, const char* message, size_t messageLen) {
1177 char* begin = p;
1178 bool print = p != NULL;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001179
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001180 while (messageLen) {
1181 char buf[6];
1182 ssize_t len = sizeof(buf) - 1;
1183 if ((size_t)len > messageLen) {
1184 len = messageLen;
1185 }
1186 len = utf8_character_length(message, len);
1187
1188 if (len < 0) {
1189 snprintf(buf, sizeof(buf),
1190 ((messageLen > 1) && isdigit(message[1])) ? "\\%03o" : "\\%o",
1191 *message & 0377);
1192 len = 1;
1193 } else {
1194 buf[0] = '\0';
1195 if (len == 1) {
1196 if (*message == '\a') {
1197 strcpy(buf, "\\a");
1198 } else if (*message == '\b') {
1199 strcpy(buf, "\\b");
1200 } else if (*message == '\t') {
1201 strcpy(buf, "\t"); /* Do not escape tabs */
1202 } else if (*message == '\v') {
1203 strcpy(buf, "\\v");
1204 } else if (*message == '\f') {
1205 strcpy(buf, "\\f");
1206 } else if (*message == '\r') {
1207 strcpy(buf, "\\r");
1208 } else if (*message == '\\') {
1209 strcpy(buf, "\\\\");
1210 } else if ((*message < ' ') || (*message & 0x80)) {
1211 snprintf(buf, sizeof(buf), "\\%o", *message & 0377);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001212 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001213 }
1214 if (!buf[0]) {
1215 strncpy(buf, message, len);
1216 buf[len] = '\0';
1217 }
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001218 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001219 if (print) {
1220 strcpy(p, buf);
1221 }
1222 p += strlen(buf);
1223 message += len;
1224 messageLen -= len;
1225 }
1226 return p - begin;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001227}
1228
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001229static char* readSeconds(char* e, struct timespec* t) {
1230 unsigned long multiplier;
1231 char* p;
1232 t->tv_sec = strtoul(e, &p, 10);
1233 if (*p != '.') {
1234 return NULL;
1235 }
1236 t->tv_nsec = 0;
1237 multiplier = NS_PER_SEC;
1238 while (isdigit(*++p) && (multiplier /= 10)) {
1239 t->tv_nsec += (*p - '0') * multiplier;
1240 }
1241 return p;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001242}
1243
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001244static struct timespec* sumTimespec(struct timespec* left,
1245 struct timespec* right) {
1246 left->tv_nsec += right->tv_nsec;
1247 left->tv_sec += right->tv_sec;
1248 if (left->tv_nsec >= (long)NS_PER_SEC) {
1249 left->tv_nsec -= NS_PER_SEC;
1250 left->tv_sec += 1;
1251 }
1252 return left;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001253}
1254
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001255static struct timespec* subTimespec(struct timespec* result,
1256 struct timespec* left,
1257 struct timespec* right) {
1258 result->tv_nsec = left->tv_nsec - right->tv_nsec;
1259 result->tv_sec = left->tv_sec - right->tv_sec;
1260 if (result->tv_nsec < 0) {
1261 result->tv_nsec += NS_PER_SEC;
1262 result->tv_sec -= 1;
1263 }
1264 return result;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001265}
1266
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001267static long long nsecTimespec(struct timespec* now) {
1268 return (long long)now->tv_sec * NS_PER_SEC + now->tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001269}
1270
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001271#ifdef __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001272static void convertMonotonic(struct timespec* result,
1273 const AndroidLogEntry* entry) {
1274 struct listnode* node;
1275 struct conversionList {
1276 struct listnode node; /* first */
1277 struct timespec time;
1278 struct timespec convert;
1279 } * list, *next;
1280 struct timespec time, convert;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001281
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001282 /* If we do not have a conversion list, build one up */
1283 if (list_empty(&convertHead)) {
1284 bool suspended_pending = false;
1285 struct timespec suspended_monotonic = { 0, 0 };
1286 struct timespec suspended_diff = { 0, 0 };
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001287
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001288 /*
1289 * Read dmesg for _some_ synchronization markers and insert
1290 * Anything in the Android Logger before the dmesg logging span will
1291 * be highly suspect regarding the monotonic time calculations.
1292 */
1293 FILE* p = popen("/system/bin/dmesg", "re");
1294 if (p) {
1295 char* line = NULL;
1296 size_t len = 0;
1297 while (getline(&line, &len, p) > 0) {
1298 static const char suspend[] = "PM: suspend entry ";
1299 static const char resume[] = "PM: suspend exit ";
1300 static const char healthd[] = "healthd";
1301 static const char battery[] = ": battery ";
1302 static const char suspended[] = "Suspended for ";
1303 struct timespec monotonic;
1304 struct tm tm;
1305 char *cp, *e = line;
1306 bool add_entry = true;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001307
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001308 if (*e == '<') {
1309 while (*e && (*e != '>')) {
1310 ++e;
1311 }
1312 if (*e != '>') {
1313 continue;
1314 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001315 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001316 if (*e != '[') {
1317 continue;
1318 }
1319 while (*++e == ' ') {
1320 ;
1321 }
1322 e = readSeconds(e, &monotonic);
1323 if (!e || (*e != ']')) {
1324 continue;
1325 }
1326
1327 if ((e = strstr(e, suspend))) {
1328 e += sizeof(suspend) - 1;
1329 } else if ((e = strstr(line, resume))) {
1330 e += sizeof(resume) - 1;
1331 } else if (((e = strstr(line, healthd))) &&
1332 ((e = strstr(e + sizeof(healthd) - 1, battery)))) {
1333 /* NB: healthd is roughly 150us late, worth the price to
1334 * deal with ntp-induced or hardware clock drift. */
1335 e += sizeof(battery) - 1;
1336 } else if ((e = strstr(line, suspended))) {
1337 e += sizeof(suspended) - 1;
1338 e = readSeconds(e, &time);
1339 if (!e) {
1340 continue;
1341 }
1342 add_entry = false;
1343 suspended_pending = true;
1344 suspended_monotonic = monotonic;
1345 suspended_diff = time;
1346 } else {
1347 continue;
1348 }
1349 if (add_entry) {
1350 /* look for "????-??-?? ??:??:??.????????? UTC" */
1351 cp = strstr(e, " UTC");
1352 if (!cp || ((cp - e) < 29) || (cp[-10] != '.')) {
1353 continue;
1354 }
1355 e = cp - 29;
1356 cp = readSeconds(cp - 10, &time);
1357 if (!cp) {
1358 continue;
1359 }
1360 cp = strptime(e, "%Y-%m-%d %H:%M:%S.", &tm);
1361 if (!cp) {
1362 continue;
1363 }
1364 cp = getenv(tz);
1365 if (cp) {
1366 cp = strdup(cp);
1367 }
1368 setenv(tz, utc, 1);
1369 time.tv_sec = mktime(&tm);
1370 if (cp) {
1371 setenv(tz, cp, 1);
1372 free(cp);
1373 } else {
1374 unsetenv(tz);
1375 }
1376 list = calloc(1, sizeof(struct conversionList));
1377 list_init(&list->node);
1378 list->time = time;
1379 subTimespec(&list->convert, &time, &monotonic);
1380 list_add_tail(&convertHead, &list->node);
1381 }
1382 if (suspended_pending && !list_empty(&convertHead)) {
1383 list = node_to_item(list_tail(&convertHead), struct conversionList,
1384 node);
1385 if (subTimespec(&time, subTimespec(&time, &list->time, &list->convert),
1386 &suspended_monotonic)
1387 ->tv_sec > 0) {
1388 /* resume, what is convert factor before? */
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001389 subTimespec(&convert, &list->convert, &suspended_diff);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001390 } else {
1391 /* suspend */
1392 convert = list->convert;
1393 }
1394 time = suspended_monotonic;
1395 sumTimespec(&time, &convert);
1396 /* breakpoint just before sleep */
1397 list = calloc(1, sizeof(struct conversionList));
1398 list_init(&list->node);
1399 list->time = time;
1400 list->convert = convert;
1401 list_add_tail(&convertHead, &list->node);
1402 /* breakpoint just after sleep */
1403 list = calloc(1, sizeof(struct conversionList));
1404 list_init(&list->node);
1405 list->time = time;
1406 sumTimespec(&list->time, &suspended_diff);
1407 list->convert = convert;
1408 sumTimespec(&list->convert, &suspended_diff);
1409 list_add_tail(&convertHead, &list->node);
1410 suspended_pending = false;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001411 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001412 }
1413 pclose(p);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001414 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001415 /* last entry is our current time conversion */
1416 list = calloc(1, sizeof(struct conversionList));
1417 list_init(&list->node);
1418 clock_gettime(CLOCK_REALTIME, &list->time);
1419 clock_gettime(CLOCK_MONOTONIC, &convert);
1420 clock_gettime(CLOCK_MONOTONIC, &time);
1421 /* Correct for instant clock_gettime latency (syscall or ~30ns) */
1422 subTimespec(&time, &convert, subTimespec(&time, &time, &convert));
1423 /* Calculate conversion factor */
1424 subTimespec(&list->convert, &list->time, &time);
1425 list_add_tail(&convertHead, &list->node);
1426 if (suspended_pending) {
1427 /* manufacture a suspend @ point before */
1428 subTimespec(&convert, &list->convert, &suspended_diff);
1429 time = suspended_monotonic;
1430 sumTimespec(&time, &convert);
1431 /* breakpoint just after sleep */
1432 list = calloc(1, sizeof(struct conversionList));
1433 list_init(&list->node);
1434 list->time = time;
1435 sumTimespec(&list->time, &suspended_diff);
1436 list->convert = convert;
1437 sumTimespec(&list->convert, &suspended_diff);
1438 list_add_head(&convertHead, &list->node);
1439 /* breakpoint just before sleep */
1440 list = calloc(1, sizeof(struct conversionList));
1441 list_init(&list->node);
1442 list->time = time;
1443 list->convert = convert;
1444 list_add_head(&convertHead, &list->node);
1445 }
1446 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001447
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001448 /* Find the breakpoint in the conversion list */
1449 list = node_to_item(list_head(&convertHead), struct conversionList, node);
1450 next = NULL;
1451 list_for_each(node, &convertHead) {
1452 next = node_to_item(node, struct conversionList, node);
1453 if (entry->tv_sec < next->time.tv_sec) {
1454 break;
1455 } else if (entry->tv_sec == next->time.tv_sec) {
1456 if (entry->tv_nsec < next->time.tv_nsec) {
1457 break;
1458 }
1459 }
1460 list = next;
1461 }
1462
1463 /* blend time from one breakpoint to the next */
1464 convert = list->convert;
1465 if (next) {
1466 unsigned long long total, run;
1467
1468 total = nsecTimespec(subTimespec(&time, &next->time, &list->time));
1469 time.tv_sec = entry->tv_sec;
1470 time.tv_nsec = entry->tv_nsec;
1471 run = nsecTimespec(subTimespec(&time, &time, &list->time));
1472 if (run < total) {
1473 long long crun;
1474
1475 float f = nsecTimespec(subTimespec(&time, &next->convert, &convert));
1476 f *= run;
1477 f /= total;
1478 crun = f;
1479 convert.tv_sec += crun / (long long)NS_PER_SEC;
1480 if (crun < 0) {
1481 convert.tv_nsec -= (-crun) % NS_PER_SEC;
1482 if (convert.tv_nsec < 0) {
1483 convert.tv_nsec += NS_PER_SEC;
1484 convert.tv_sec -= 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001485 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001486 } else {
1487 convert.tv_nsec += crun % NS_PER_SEC;
1488 if (convert.tv_nsec >= (long)NS_PER_SEC) {
1489 convert.tv_nsec -= NS_PER_SEC;
1490 convert.tv_sec += 1;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001491 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001492 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001493 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001494 }
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001495
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001496 /* Apply the correction factor */
1497 result->tv_sec = entry->tv_sec;
1498 result->tv_nsec = entry->tv_nsec;
1499 subTimespec(result, result, &convert);
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001500}
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001501#endif
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001502
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001503/**
1504 * Formats a log message into a buffer
1505 *
1506 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
1507 * If return value != defaultBuffer, caller must call free()
1508 * Returns NULL on malloc error
1509 */
1510
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001511LIBLOG_ABI_PUBLIC char* android_log_formatLogLine(AndroidLogFormat* p_format,
1512 char* defaultBuffer,
1513 size_t defaultBufferSize,
1514 const AndroidLogEntry* entry,
1515 size_t* p_outLength) {
Yabin Cui8a985352014-11-13 10:02:08 -08001516#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001517 struct tm tmBuf;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001518#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001519 struct tm* ptm;
1520 /* good margin, 23+nul for msec, 26+nul for usec, 29+nul to nsec */
1521 char timeBuf[64];
1522 char prefixBuf[128], suffixBuf[128];
1523 char priChar;
1524 int prefixSuffixIsHeaderFooter = 0;
1525 char* ret;
1526 time_t now;
1527 unsigned long nsec;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001528
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001529 priChar = filterPriToChar(entry->priority);
1530 size_t prefixLen = 0, suffixLen = 0;
1531 size_t len;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001532
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001533 /*
1534 * Get the current date/time in pretty form
1535 *
1536 * It's often useful when examining a log with "less" to jump to
1537 * a specific point in the file by searching for the date/time stamp.
1538 * For this reason it's very annoying to have regexp meta characters
1539 * in the time stamp. Don't use forward slashes, parenthesis,
1540 * brackets, asterisks, or other special chars here.
1541 *
1542 * The caller may have affected the timezone environment, this is
1543 * expected to be sensitive to that.
1544 */
1545 now = entry->tv_sec;
1546 nsec = entry->tv_nsec;
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001547#if __ANDROID__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001548 if (p_format->monotonic_output) {
1549 /* prevent convertMonotonic from being called if logd is monotonic */
1550 if (android_log_clockid() != CLOCK_MONOTONIC) {
1551 struct timespec time;
1552 convertMonotonic(&time, entry);
1553 now = time.tv_sec;
1554 nsec = time.tv_nsec;
Mark Salyzyn4cbed022015-08-31 15:53:41 -07001555 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001556 }
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001557#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001558 if (now < 0) {
1559 nsec = NS_PER_SEC - nsec;
1560 }
1561 if (p_format->epoch_output || p_format->monotonic_output) {
1562 ptm = NULL;
1563 snprintf(timeBuf, sizeof(timeBuf),
1564 p_format->monotonic_output ? "%6lld" : "%19lld", (long long)now);
1565 } else {
Yabin Cui8a985352014-11-13 10:02:08 -08001566#if !defined(_WIN32)
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001567 ptm = localtime_r(&now, &tmBuf);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001568#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001569 ptm = localtime(&now);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001570#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001571 strftime(timeBuf, sizeof(timeBuf),
1572 &"%Y-%m-%d %H:%M:%S"[p_format->year_output ? 0 : 3], ptm);
1573 }
1574 len = strlen(timeBuf);
1575 if (p_format->nsec_time_output) {
1576 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%09ld", nsec);
1577 } else if (p_format->usec_time_output) {
1578 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%06ld",
1579 nsec / US_PER_NSEC);
1580 } else {
1581 len += snprintf(timeBuf + len, sizeof(timeBuf) - len, ".%03ld",
1582 nsec / MS_PER_NSEC);
1583 }
1584 if (p_format->zone_output && ptm) {
1585 strftime(timeBuf + len, sizeof(timeBuf) - len, " %z", ptm);
1586 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001587
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001588 /*
1589 * Construct a buffer containing the log header and log message.
1590 */
1591 if (p_format->colored_output) {
1592 prefixLen = snprintf(prefixBuf, sizeof(prefixBuf), "\x1B[38;5;%dm",
1593 colorFromPri(entry->priority));
1594 prefixLen = MIN(prefixLen, sizeof(prefixBuf));
1595 suffixLen = snprintf(suffixBuf, sizeof(suffixBuf), "\x1B[0m");
1596 suffixLen = MIN(suffixLen, sizeof(suffixBuf));
1597 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001598
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001599 char uid[16];
1600 uid[0] = '\0';
1601 if (p_format->uid_output) {
1602 if (entry->uid >= 0) {
1603/*
1604 * This code is Android specific, bionic guarantees that
1605 * calls to non-reentrant getpwuid() are thread safe.
1606 */
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001607#if !defined(__MINGW32__)
1608#if (FAKE_LOG_DEVICE == 0)
William Roberts8a5b9ca2016-04-08 12:13:17 -07001609#ifndef __BIONIC__
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001610#warning \
1611 "This code assumes that getpwuid is thread safe, only true with Bionic!"
William Roberts8a5b9ca2016-04-08 12:13:17 -07001612#endif
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001613#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001614 struct passwd* pwd = getpwuid(entry->uid);
1615 if (pwd && (strlen(pwd->pw_name) <= 5)) {
1616 snprintf(uid, sizeof(uid), "%5s:", pwd->pw_name);
1617 } else
Mark Salyzyn62d0d2d2016-03-08 16:18:26 -08001618#endif
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001619 {
1620 /* Not worth parsing package list, names all longer than 5 */
1621 snprintf(uid, sizeof(uid), "%5d:", entry->uid);
1622 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001623 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001624 snprintf(uid, sizeof(uid), " ");
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001625 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001626 }
1627
1628 switch (p_format->format) {
1629 case FORMAT_TAG:
1630 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1631 "%c/%-8.*s: ", priChar, (int)entry->tagLen, entry->tag);
1632 strcpy(suffixBuf + suffixLen, "\n");
1633 ++suffixLen;
1634 break;
1635 case FORMAT_PROCESS:
1636 len = snprintf(suffixBuf + suffixLen, sizeof(suffixBuf) - suffixLen,
1637 " (%.*s)\n", (int)entry->tagLen, entry->tag);
1638 suffixLen += MIN(len, sizeof(suffixBuf) - suffixLen);
1639 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1640 "%c(%s%5d) ", priChar, uid, entry->pid);
1641 break;
1642 case FORMAT_THREAD:
1643 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1644 "%c(%s%5d:%5d) ", priChar, uid, entry->pid, entry->tid);
1645 strcpy(suffixBuf + suffixLen, "\n");
1646 ++suffixLen;
1647 break;
1648 case FORMAT_RAW:
1649 prefixBuf[prefixLen] = 0;
1650 len = 0;
1651 strcpy(suffixBuf + suffixLen, "\n");
1652 ++suffixLen;
1653 break;
1654 case FORMAT_TIME:
1655 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1656 "%s %c/%-8.*s(%s%5d): ", timeBuf, priChar,
1657 (int)entry->tagLen, entry->tag, uid, entry->pid);
1658 strcpy(suffixBuf + suffixLen, "\n");
1659 ++suffixLen;
1660 break;
1661 case FORMAT_THREADTIME:
1662 ret = strchr(uid, ':');
1663 if (ret) {
1664 *ret = ' ';
1665 }
1666 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1667 "%s %s%5d %5d %c %-8.*s: ", timeBuf, uid, entry->pid,
1668 entry->tid, priChar, (int)entry->tagLen, entry->tag);
1669 strcpy(suffixBuf + suffixLen, "\n");
1670 ++suffixLen;
1671 break;
1672 case FORMAT_LONG:
1673 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1674 "[ %s %s%5d:%5d %c/%-8.*s ]\n", timeBuf, uid, entry->pid,
1675 entry->tid, priChar, (int)entry->tagLen, entry->tag);
1676 strcpy(suffixBuf + suffixLen, "\n\n");
1677 suffixLen += 2;
1678 prefixSuffixIsHeaderFooter = 1;
1679 break;
1680 case FORMAT_BRIEF:
1681 default:
1682 len = snprintf(prefixBuf + prefixLen, sizeof(prefixBuf) - prefixLen,
1683 "%c/%-8.*s(%s%5d): ", priChar, (int)entry->tagLen,
1684 entry->tag, uid, entry->pid);
1685 strcpy(suffixBuf + suffixLen, "\n");
1686 ++suffixLen;
1687 break;
1688 }
1689
1690 /* snprintf has a weird return value. It returns what would have been
1691 * written given a large enough buffer. In the case that the prefix is
1692 * longer then our buffer(128), it messes up the calculations below
1693 * possibly causing heap corruption. To avoid this we double check and
1694 * set the length at the maximum (size minus null byte)
1695 */
1696 prefixLen += len;
1697 if (prefixLen >= sizeof(prefixBuf)) {
1698 prefixLen = sizeof(prefixBuf) - 1;
1699 prefixBuf[sizeof(prefixBuf) - 1] = '\0';
1700 }
1701 if (suffixLen >= sizeof(suffixBuf)) {
1702 suffixLen = sizeof(suffixBuf) - 1;
1703 suffixBuf[sizeof(suffixBuf) - 2] = '\n';
1704 suffixBuf[sizeof(suffixBuf) - 1] = '\0';
1705 }
1706
1707 /* the following code is tragically unreadable */
1708
1709 size_t numLines;
1710 char* p;
1711 size_t bufferSize;
1712 const char* pm;
1713
1714 if (prefixSuffixIsHeaderFooter) {
1715 /* we're just wrapping message with a header/footer */
1716 numLines = 1;
1717 } else {
1718 pm = entry->message;
1719 numLines = 0;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001720
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001721 /*
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001722 * The line-end finding here must match the line-end finding
1723 * in for ( ... numLines...) loop below
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001724 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001725 while (pm < (entry->message + entry->messageLen)) {
1726 if (*pm++ == '\n') numLines++;
1727 }
1728 /* plus one line for anything not newline-terminated at the end */
1729 if (pm > entry->message && *(pm - 1) != '\n') numLines++;
1730 }
1731
1732 /*
1733 * this is an upper bound--newlines in message may be counted
1734 * extraneously
1735 */
1736 bufferSize = (numLines * (prefixLen + suffixLen)) + 1;
1737 if (p_format->printable_output) {
1738 /* Calculate extra length to convert non-printable to printable */
1739 bufferSize += convertPrintable(NULL, entry->message, entry->messageLen);
1740 } else {
1741 bufferSize += entry->messageLen;
1742 }
1743
1744 if (defaultBufferSize >= bufferSize) {
1745 ret = defaultBuffer;
1746 } else {
1747 ret = (char*)malloc(bufferSize);
1748
1749 if (ret == NULL) {
1750 return ret;
1751 }
1752 }
1753
1754 ret[0] = '\0'; /* to start strcat off */
1755
1756 p = ret;
1757 pm = entry->message;
1758
1759 if (prefixSuffixIsHeaderFooter) {
1760 strcat(p, prefixBuf);
1761 p += prefixLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001762 if (p_format->printable_output) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001763 p += convertPrintable(p, entry->message, entry->messageLen);
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001764 } else {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001765 strncat(p, entry->message, entry->messageLen);
1766 p += entry->messageLen;
Mark Salyzynb932b2f2015-05-15 09:01:58 -07001767 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001768 strcat(p, suffixBuf);
1769 p += suffixLen;
1770 } else {
1771 do {
1772 const char* lineStart;
1773 size_t lineLen;
1774 lineStart = pm;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001775
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001776 /* Find the next end-of-line in message */
1777 while (pm < (entry->message + entry->messageLen) && *pm != '\n') pm++;
1778 lineLen = pm - lineStart;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001779
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001780 strcat(p, prefixBuf);
1781 p += prefixLen;
1782 if (p_format->printable_output) {
1783 p += convertPrintable(p, lineStart, lineLen);
1784 } else {
1785 strncat(p, lineStart, lineLen);
1786 p += lineLen;
1787 }
1788 strcat(p, suffixBuf);
1789 p += suffixLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001790
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001791 if (*pm == '\n') pm++;
1792 } while (pm < (entry->message + entry->messageLen));
1793 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001794
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001795 if (p_outLength != NULL) {
1796 *p_outLength = p - ret;
1797 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001798
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001799 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001800}
1801
1802/**
1803 * Either print or do not print log line, based on filter
1804 *
1805 * Returns count bytes written
1806 */
1807
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001808LIBLOG_ABI_PUBLIC int android_log_printLogLine(AndroidLogFormat* p_format,
1809 int fd,
1810 const AndroidLogEntry* entry) {
1811 int ret;
1812 char defaultBuffer[512];
1813 char* outBuffer = NULL;
1814 size_t totalLen;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001815
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001816 outBuffer = android_log_formatLogLine(
1817 p_format, defaultBuffer, sizeof(defaultBuffer), entry, &totalLen);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001818
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001819 if (!outBuffer) return -1;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001820
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001821 do {
1822 ret = write(fd, outBuffer, totalLen);
1823 } while (ret < 0 && errno == EINTR);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001824
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001825 if (ret < 0) {
1826 fprintf(stderr, "+++ LOG: write failed (errno=%d)\n", errno);
1827 ret = 0;
1828 goto done;
1829 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001830
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001831 if (((size_t)ret) < totalLen) {
1832 fprintf(stderr, "+++ LOG: write partial (%d of %d)\n", ret, (int)totalLen);
1833 goto done;
1834 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001835
1836done:
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001837 if (outBuffer != defaultBuffer) {
1838 free(outBuffer);
1839 }
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001840
Mark Salyzyn2ed51d72017-03-09 08:09:43 -08001841 return ret;
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001842}