blob: d15b3679b999650f131004125aece6bce9194e80 [file] [log] [blame]
Mark Salyzyn018a96d2016-03-01 13:45:42 -08001/*
2 * Copyright (C) 2007-2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tom Cherry2ec6a532020-01-27 08:35:13 -080017#include "logger_write.h"
18
Mark Salyzyn018a96d2016-03-01 13:45:42 -080019#include <errno.h>
Tom Cherry349b0c42020-01-08 14:47:42 -080020#include <inttypes.h>
Tom Cherry69ee5dd2020-01-22 07:48:42 -080021#include <libgen.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080022#include <stdlib.h>
23#include <string.h>
24#include <sys/time.h>
25
26#ifdef __BIONIC__
27#include <android/set_abort_message.h>
28#endif
29
Tom Cherrybbb16022020-03-09 12:43:18 -070030#include <atomic>
Tom Cherry349b0c42020-01-08 14:47:42 -080031#include <shared_mutex>
32
Tom Cherryff464b12020-01-27 13:49:26 -080033#include <android-base/errno_restorer.h>
Tom Cherry121292d2020-01-14 09:52:10 -080034#include <android-base/macros.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080035#include <private/android_filesystem_config.h>
36#include <private/android_logger.h>
37
Tom Cherry349b0c42020-01-08 14:47:42 -080038#include "android/log.h"
Tom Cherry69ee5dd2020-01-22 07:48:42 -080039#include "log/log_read.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080040#include "logger.h"
Tom Cherry349b0c42020-01-08 14:47:42 -080041#include "rwlock.h"
Tom Cherry6f6ef392019-01-16 14:17:08 -080042#include "uio.h"
Mark Salyzyn018a96d2016-03-01 13:45:42 -080043
Tom Cherrye2187bf2020-01-27 15:45:52 -080044#ifdef __ANDROID__
Tom Cherry21bb36c2020-01-08 15:18:26 -080045#include "logd_writer.h"
46#include "pmsg_writer.h"
Tom Cherry97ec4ee2019-10-02 10:52:55 -070047#endif
Tom Cherry2beabe52019-10-01 13:05:58 -070048
Tom Cherry349b0c42020-01-08 14:47:42 -080049#if defined(__APPLE__)
50#include <pthread.h>
51#elif defined(__linux__) && !defined(__ANDROID__)
52#include <syscall.h>
53#elif defined(_WIN32)
54#include <windows.h>
55#endif
56
Tom Cherryff464b12020-01-27 13:49:26 -080057using android::base::ErrnoRestorer;
58
Tom Cherry21bb36c2020-01-08 15:18:26 -080059#define LOG_BUF_SIZE 1024
60
Dan Willemsen0910d2d2016-11-29 13:39:55 -080061#if defined(__ANDROID__)
Tom Cherry06e0fce2019-12-11 14:26:37 -080062static int check_log_uid_permissions() {
Tom Cherryb47aa2a2020-01-08 15:34:14 -080063 uid_t uid = getuid();
Mark Salyzync33103c2016-03-28 16:20:29 -070064
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080065 /* Matches clientHasLogCredentials() in logd */
66 if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
67 uid = geteuid();
Mark Salyzync33103c2016-03-28 16:20:29 -070068 if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080069 gid_t gid = getgid();
70 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
71 gid = getegid();
72 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
73 int num_groups;
74 gid_t* groups;
Mark Salyzync33103c2016-03-28 16:20:29 -070075
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080076 num_groups = getgroups(0, NULL);
77 if (num_groups <= 0) {
78 return -EPERM;
79 }
Tom Cherry71ba1642019-01-10 10:37:36 -080080 groups = static_cast<gid_t*>(calloc(num_groups, sizeof(gid_t)));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080081 if (!groups) {
82 return -ENOMEM;
83 }
84 num_groups = getgroups(num_groups, groups);
85 while (num_groups > 0) {
86 if (groups[num_groups - 1] == AID_LOG) {
87 break;
Mark Salyzync33103c2016-03-28 16:20:29 -070088 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080089 --num_groups;
90 }
91 free(groups);
92 if (num_groups <= 0) {
93 return -EPERM;
94 }
Mark Salyzync33103c2016-03-28 16:20:29 -070095 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080096 }
Mark Salyzync33103c2016-03-28 16:20:29 -070097 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080098 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080099 return 0;
Mark Salyzync33103c2016-03-28 16:20:29 -0700100}
Tom Cherry06e0fce2019-12-11 14:26:37 -0800101#endif
Mark Salyzync33103c2016-03-28 16:20:29 -0700102
Mark Salyzyndf7a4c62016-08-23 10:23:36 -0700103/*
104 * Release any logger resources. A new log write will immediately re-acquire.
105 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800106void __android_log_close() {
Tom Cherrye2187bf2020-01-27 15:45:52 -0800107#ifdef __ANDROID__
Tom Cherry21bb36c2020-01-08 15:18:26 -0800108 LogdClose();
109 PmsgClose();
Tom Cherry21bb36c2020-01-08 15:18:26 -0800110#endif
Mark Salyzyndf7a4c62016-08-23 10:23:36 -0700111}
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800112
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800113#if defined(__GLIBC__) || defined(_WIN32)
114static const char* getprogname() {
115#if defined(__GLIBC__)
116 return program_invocation_short_name;
117#elif defined(_WIN32)
118 static bool first = true;
119 static char progname[MAX_PATH] = {};
120
121 if (first) {
122 char path[PATH_MAX + 1];
123 DWORD result = GetModuleFileName(nullptr, path, sizeof(path) - 1);
124 if (result == 0 || result == sizeof(path) - 1) return "";
125 path[PATH_MAX - 1] = 0;
126
127 char* path_basename = basename(path);
128
129 snprintf(progname, sizeof(progname), "%s", path_basename);
130 first = false;
131 }
132
133 return progname;
134#endif
135}
136#endif
137
138// It's possible for logging to happen during static initialization before our globals are
139// initialized, so we place this std::string in a function such that it is initialized on the first
140// call.
Tom Cherry2ec6a532020-01-27 08:35:13 -0800141std::string& GetDefaultTag() {
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800142 static std::string default_tag = getprogname();
143 return default_tag;
144}
Tom Cherry2ec6a532020-01-27 08:35:13 -0800145RwLock default_tag_lock;
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800146
147void __android_log_set_default_tag(const char* tag) {
148 auto lock = std::unique_lock{default_tag_lock};
149 GetDefaultTag().assign(tag, 0, LOGGER_ENTRY_MAX_PAYLOAD);
150}
151
Tom Cherryf1a975b2020-03-12 11:07:07 -0700152static std::atomic_int32_t minimum_log_priority = ANDROID_LOG_DEFAULT;
153int32_t __android_log_set_minimum_priority(int32_t priority) {
Tom Cherrybbb16022020-03-09 12:43:18 -0700154 return minimum_log_priority.exchange(priority, std::memory_order_relaxed);
Tom Cherry0391a872020-01-16 15:58:02 -0800155}
156
Tom Cherryf1a975b2020-03-12 11:07:07 -0700157int32_t __android_log_get_minimum_priority() {
Tom Cherry0391a872020-01-16 15:58:02 -0800158 return minimum_log_priority;
159}
160
Tom Cherry349b0c42020-01-08 14:47:42 -0800161#ifdef __ANDROID__
162static __android_logger_function logger_function = __android_log_logd_logger;
163#else
164static __android_logger_function logger_function = __android_log_stderr_logger;
165#endif
166static RwLock logger_function_lock;
167
168void __android_log_set_logger(__android_logger_function logger) {
169 auto lock = std::unique_lock{logger_function_lock};
170 logger_function = logger;
171}
172
173void __android_log_default_aborter(const char* abort_message) {
174#ifdef __ANDROID__
175 android_set_abort_message(abort_message);
176#else
177 UNUSED(abort_message);
178#endif
179 abort();
180}
181
182static __android_aborter_function aborter_function = __android_log_default_aborter;
183static RwLock aborter_function_lock;
184
185void __android_log_set_aborter(__android_aborter_function aborter) {
186 auto lock = std::unique_lock{aborter_function_lock};
187 aborter_function = aborter;
188}
189
190void __android_log_call_aborter(const char* abort_message) {
191 auto lock = std::shared_lock{aborter_function_lock};
192 aborter_function(abort_message);
193}
194
195#ifdef __ANDROID__
Tom Cherry06e0fce2019-12-11 14:26:37 -0800196static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
Tom Cherryff464b12020-01-27 13:49:26 -0800197 int ret;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800198 struct timespec ts;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800199
Tom Cherry06e0fce2019-12-11 14:26:37 -0800200 if (log_id == LOG_ID_KERNEL) {
201 return -EINVAL;
202 }
203
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800204 clock_gettime(android_log_clockid(), &ts);
Mark Salyzyn142b43d2016-12-28 10:30:57 -0800205
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800206 if (log_id == LOG_ID_SECURITY) {
207 if (vec[0].iov_len < 4) {
208 return -EINVAL;
209 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800210
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800211 ret = check_log_uid_permissions();
212 if (ret < 0) {
213 return ret;
214 }
215 if (!__android_log_security()) {
216 /* If only we could reset downstream logd counter */
217 return -EPERM;
218 }
Yao Chen025f05a2017-12-01 15:48:19 -0800219 } else if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800220 if (vec[0].iov_len < 4) {
221 return -EINVAL;
222 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800223 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800224
Tom Cherry21bb36c2020-01-08 15:18:26 -0800225 ret = LogdWrite(log_id, &ts, vec, nr);
226 PmsgWrite(log_id, &ts, vec, nr);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800227
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800228 return ret;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800229}
Tom Cherry349b0c42020-01-08 14:47:42 -0800230#else
231static int write_to_log(log_id_t, struct iovec*, size_t) {
232 // Non-Android text logs should go to __android_log_stderr_logger, not here.
233 // Non-Android binary logs are always dropped.
234 return 1;
235}
236#endif
237
238// Copied from base/threads.cpp
239static uint64_t GetThreadId() {
240#if defined(__BIONIC__)
241 return gettid();
242#elif defined(__APPLE__)
243 uint64_t tid;
244 pthread_threadid_np(NULL, &tid);
245 return tid;
246#elif defined(__linux__)
247 return syscall(__NR_gettid);
248#elif defined(_WIN32)
249 return GetCurrentThreadId();
250#endif
251}
252
Tom Cherryebf43ad2020-03-11 11:07:13 -0700253void __android_log_stderr_logger(const struct __android_log_message* log_message) {
Tom Cherry349b0c42020-01-08 14:47:42 -0800254 struct tm now;
255 time_t t = time(nullptr);
256
257#if defined(_WIN32)
258 localtime_s(&now, &t);
259#else
260 localtime_r(&t, &now);
261#endif
262
263 char timestamp[32];
264 strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
265
266 static const char log_characters[] = "XXVDIWEF";
267 static_assert(arraysize(log_characters) - 1 == ANDROID_LOG_SILENT,
268 "Mismatch in size of log_characters and values in android_LogPriority");
Tom Cherryf1a975b2020-03-12 11:07:07 -0700269 int32_t priority =
Tom Cherryebf43ad2020-03-11 11:07:13 -0700270 log_message->priority > ANDROID_LOG_SILENT ? ANDROID_LOG_FATAL : log_message->priority;
Tom Cherry349b0c42020-01-08 14:47:42 -0800271 char priority_char = log_characters[priority];
272 uint64_t tid = GetThreadId();
273
Tom Cherryebf43ad2020-03-11 11:07:13 -0700274 if (log_message->file != nullptr) {
Tom Cherry349b0c42020-01-08 14:47:42 -0800275 fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s:%u] %s\n",
Tom Cherryebf43ad2020-03-11 11:07:13 -0700276 log_message->tag ? log_message->tag : "nullptr", priority_char, timestamp, getpid(),
277 tid, log_message->file, log_message->line, log_message->message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800278 } else {
279 fprintf(stderr, "%s %c %s %5d %5" PRIu64 " %s\n",
Tom Cherryebf43ad2020-03-11 11:07:13 -0700280 log_message->tag ? log_message->tag : "nullptr", priority_char, timestamp, getpid(),
281 tid, log_message->message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800282 }
283}
284
Tom Cherryebf43ad2020-03-11 11:07:13 -0700285void __android_log_logd_logger(const struct __android_log_message* log_message) {
286 int buffer_id = log_message->buffer_id == LOG_ID_DEFAULT ? LOG_ID_MAIN : log_message->buffer_id;
Tom Cherry349b0c42020-01-08 14:47:42 -0800287
288 struct iovec vec[3];
289 vec[0].iov_base =
Tom Cherryebf43ad2020-03-11 11:07:13 -0700290 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(&log_message->priority));
Tom Cherry349b0c42020-01-08 14:47:42 -0800291 vec[0].iov_len = 1;
Tom Cherryebf43ad2020-03-11 11:07:13 -0700292 vec[1].iov_base = const_cast<void*>(static_cast<const void*>(log_message->tag));
293 vec[1].iov_len = strlen(log_message->tag) + 1;
294 vec[2].iov_base = const_cast<void*>(static_cast<const void*>(log_message->message));
295 vec[2].iov_len = strlen(log_message->message) + 1;
Tom Cherry349b0c42020-01-08 14:47:42 -0800296
297 write_to_log(static_cast<log_id_t>(buffer_id), vec, 3);
298}
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800299
Tom Cherry2d9779e2019-02-08 11:46:19 -0800300int __android_log_write(int prio, const char* tag, const char* msg) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800301 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800302}
303
Tom Cherryebf43ad2020-03-11 11:07:13 -0700304void __android_log_write_log_message(__android_log_message* log_message) {
Tom Cherryff464b12020-01-27 13:49:26 -0800305 ErrnoRestorer errno_restorer;
306
Tom Cherryebf43ad2020-03-11 11:07:13 -0700307 if (log_message->buffer_id != LOG_ID_DEFAULT && log_message->buffer_id != LOG_ID_MAIN &&
308 log_message->buffer_id != LOG_ID_SYSTEM && log_message->buffer_id != LOG_ID_RADIO &&
309 log_message->buffer_id != LOG_ID_CRASH) {
Tom Cherryf48f6852020-01-28 13:06:29 -0800310 return;
311 }
312
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800313 auto tag_lock = std::shared_lock{default_tag_lock, std::defer_lock};
Tom Cherryebf43ad2020-03-11 11:07:13 -0700314 if (log_message->tag == nullptr) {
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800315 tag_lock.lock();
Tom Cherryebf43ad2020-03-11 11:07:13 -0700316 log_message->tag = GetDefaultTag().c_str();
Tom Cherry69ee5dd2020-01-22 07:48:42 -0800317 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800318
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800319#if __BIONIC__
Tom Cherryebf43ad2020-03-11 11:07:13 -0700320 if (log_message->priority == ANDROID_LOG_FATAL) {
321 android_set_abort_message(log_message->message);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800322 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800323#endif
324
Tom Cherry349b0c42020-01-08 14:47:42 -0800325 auto lock = std::shared_lock{logger_function_lock};
Tom Cherryebf43ad2020-03-11 11:07:13 -0700326 logger_function(log_message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800327}
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800328
Tom Cherry349b0c42020-01-08 14:47:42 -0800329int __android_log_buf_write(int bufID, int prio, const char* tag, const char* msg) {
Tom Cherryff464b12020-01-27 13:49:26 -0800330 ErrnoRestorer errno_restorer;
331
Tom Cherry96e7ef52020-01-22 08:20:03 -0800332 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700333 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800334 }
335
Tom Cherryebf43ad2020-03-11 11:07:13 -0700336 __android_log_message log_message = {
337 sizeof(__android_log_message), bufID, prio, tag, nullptr, 0, msg};
338 __android_log_write_log_message(&log_message);
Tom Cherry349b0c42020-01-08 14:47:42 -0800339 return 1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800340}
341
Tom Cherry2d9779e2019-02-08 11:46:19 -0800342int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap) {
Tom Cherryff464b12020-01-27 13:49:26 -0800343 ErrnoRestorer errno_restorer;
344
Tom Cherry96e7ef52020-01-22 08:20:03 -0800345 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700346 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800347 }
348
Tom Cherry3574c372020-02-21 15:06:46 -0800349 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800350
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800351 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800352
Tom Cherryebf43ad2020-03-11 11:07:13 -0700353 __android_log_message log_message = {
354 sizeof(__android_log_message), LOG_ID_MAIN, prio, tag, nullptr, 0, buf};
355 __android_log_write_log_message(&log_message);
Tom Cherry96e7ef52020-01-22 08:20:03 -0800356 return 1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800357}
358
Tom Cherry2d9779e2019-02-08 11:46:19 -0800359int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
Tom Cherryff464b12020-01-27 13:49:26 -0800360 ErrnoRestorer errno_restorer;
361
Tom Cherry96e7ef52020-01-22 08:20:03 -0800362 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700363 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800364 }
365
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800366 va_list ap;
Tom Cherry3574c372020-02-21 15:06:46 -0800367 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800368
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800369 va_start(ap, fmt);
370 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
371 va_end(ap);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800372
Tom Cherryebf43ad2020-03-11 11:07:13 -0700373 __android_log_message log_message = {
374 sizeof(__android_log_message), LOG_ID_MAIN, prio, tag, nullptr, 0, buf};
375 __android_log_write_log_message(&log_message);
Tom Cherry96e7ef52020-01-22 08:20:03 -0800376 return 1;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800377}
378
Tom Cherry2d9779e2019-02-08 11:46:19 -0800379int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) {
Tom Cherryff464b12020-01-27 13:49:26 -0800380 ErrnoRestorer errno_restorer;
381
Tom Cherry96e7ef52020-01-22 08:20:03 -0800382 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherry1ff17fc2020-04-16 11:20:29 -0700383 return -EPERM;
Tom Cherry96e7ef52020-01-22 08:20:03 -0800384 }
385
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800386 va_list ap;
Tom Cherry3574c372020-02-21 15:06:46 -0800387 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800388
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800389 va_start(ap, fmt);
390 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
391 va_end(ap);
392
Tom Cherryebf43ad2020-03-11 11:07:13 -0700393 __android_log_message log_message = {
394 sizeof(__android_log_message), bufID, prio, tag, nullptr, 0, buf};
395 __android_log_write_log_message(&log_message);
Tom Cherry96e7ef52020-01-22 08:20:03 -0800396 return 1;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800397}
398
Tom Cherry2d9779e2019-02-08 11:46:19 -0800399void __android_log_assert(const char* cond, const char* tag, const char* fmt, ...) {
Tom Cherry3574c372020-02-21 15:06:46 -0800400 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800401
402 if (fmt) {
403 va_list ap;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800404 va_start(ap, fmt);
405 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
406 va_end(ap);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800407 } else {
408 /* Msg not provided, log condition. N.B. Do not use cond directly as
409 * format string as it could contain spurious '%' syntax (e.g.
410 * "%d" in "blocks%devs == 0").
411 */
412 if (cond)
413 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
414 else
415 strcpy(buf, "Unspecified assertion failed");
416 }
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800417
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800418 // Log assertion failures to stderr for the benefit of "adb shell" users
419 // and gtests (http://b/23675822).
Tom Cherry6f6ef392019-01-16 14:17:08 -0800420 TEMP_FAILURE_RETRY(write(2, buf, strlen(buf)));
421 TEMP_FAILURE_RETRY(write(2, "\n", 1));
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800422
423 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Tom Cherry349b0c42020-01-08 14:47:42 -0800424 __android_log_call_aborter(buf);
425 abort();
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800426}
427
Tom Cherry2d9779e2019-02-08 11:46:19 -0800428int __android_log_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800429 ErrnoRestorer errno_restorer;
430
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800431 struct iovec vec[2];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800432
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800433 vec[0].iov_base = &tag;
434 vec[0].iov_len = sizeof(tag);
435 vec[1].iov_base = (void*)payload;
436 vec[1].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800437
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800438 return write_to_log(LOG_ID_EVENTS, vec, 2);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800439}
440
Tom Cherry2d9779e2019-02-08 11:46:19 -0800441int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800442 ErrnoRestorer errno_restorer;
443
Stefan Lafon701a0652017-08-24 20:14:06 -0700444 struct iovec vec[2];
445
446 vec[0].iov_base = &tag;
447 vec[0].iov_len = sizeof(tag);
448 vec[1].iov_base = (void*)payload;
449 vec[1].iov_len = len;
450
451 return write_to_log(LOG_ID_STATS, vec, 2);
452}
453
Tom Cherry2d9779e2019-02-08 11:46:19 -0800454int __android_log_security_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800455 ErrnoRestorer errno_restorer;
456
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800457 struct iovec vec[2];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800458
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800459 vec[0].iov_base = &tag;
460 vec[0].iov_len = sizeof(tag);
461 vec[1].iov_base = (void*)payload;
462 vec[1].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800463
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800464 return write_to_log(LOG_ID_SECURITY, vec, 2);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800465}
466
467/*
468 * Like __android_log_bwrite, but takes the type as well. Doesn't work
469 * for the general case where we're generating lists of stuff, but very
470 * handy if we just want to dump an integer into the log.
471 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800472int __android_log_btwrite(int32_t tag, char type, const void* payload, size_t len) {
Tom Cherryff464b12020-01-27 13:49:26 -0800473 ErrnoRestorer errno_restorer;
474
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800475 struct iovec vec[3];
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800476
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800477 vec[0].iov_base = &tag;
478 vec[0].iov_len = sizeof(tag);
479 vec[1].iov_base = &type;
480 vec[1].iov_len = sizeof(type);
481 vec[2].iov_base = (void*)payload;
482 vec[2].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800483
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800484 return write_to_log(LOG_ID_EVENTS, vec, 3);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800485}
486
487/*
488 * Like __android_log_bwrite, but used for writing strings to the
489 * event log.
490 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800491int __android_log_bswrite(int32_t tag, const char* payload) {
Tom Cherryff464b12020-01-27 13:49:26 -0800492 ErrnoRestorer errno_restorer;
493
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800494 struct iovec vec[4];
495 char type = EVENT_TYPE_STRING;
496 uint32_t len = strlen(payload);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800497
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800498 vec[0].iov_base = &tag;
499 vec[0].iov_len = sizeof(tag);
500 vec[1].iov_base = &type;
501 vec[1].iov_len = sizeof(type);
502 vec[2].iov_base = &len;
503 vec[2].iov_len = sizeof(len);
504 vec[3].iov_base = (void*)payload;
505 vec[3].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800506
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800507 return write_to_log(LOG_ID_EVENTS, vec, 4);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800508}
509
510/*
511 * Like __android_log_security_bwrite, but used for writing strings to the
512 * security log.
513 */
Tom Cherry2d9779e2019-02-08 11:46:19 -0800514int __android_log_security_bswrite(int32_t tag, const char* payload) {
Tom Cherryff464b12020-01-27 13:49:26 -0800515 ErrnoRestorer errno_restorer;
516
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800517 struct iovec vec[4];
518 char type = EVENT_TYPE_STRING;
519 uint32_t len = strlen(payload);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800520
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800521 vec[0].iov_base = &tag;
522 vec[0].iov_len = sizeof(tag);
523 vec[1].iov_base = &type;
524 vec[1].iov_len = sizeof(type);
525 vec[2].iov_base = &len;
526 vec[2].iov_len = sizeof(len);
527 vec[3].iov_base = (void*)payload;
528 vec[3].iov_len = len;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800529
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800530 return write_to_log(LOG_ID_SECURITY, vec, 4);
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800531}