blob: 2ed4887b54f59edba99387eba42bb543589b8537 [file] [log] [blame]
Mark Salyzyndd9094a2016-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
Elliott Hughesbb9d0642023-06-30 10:21:02 -070017#define _POSIX_THREAD_SAFE_FUNCTIONS // For mingw localtime_r().
18
Tom Cherry77ddcd32020-01-27 08:35:13 -080019#include "logger_write.h"
20
Mark Salyzyndd9094a2016-03-01 13:45:42 -080021#include <errno.h>
Tom Cherryc17613c2020-01-08 14:47:42 -080022#include <inttypes.h>
Tom Cherry03abfe32020-01-22 07:48:42 -080023#include <libgen.h>
Mark Salyzyndd9094a2016-03-01 13:45:42 -080024#include <stdlib.h>
25#include <string.h>
26#include <sys/time.h>
27
28#ifdef __BIONIC__
29#include <android/set_abort_message.h>
30#endif
31
Tom Cherryaca59902020-03-09 12:43:18 -070032#include <atomic>
Tom Cherryc17613c2020-01-08 14:47:42 -080033
Tom Cherrye6830da2020-01-27 13:49:26 -080034#include <android-base/errno_restorer.h>
Tom Cherry5bdb9c92020-01-14 09:52:10 -080035#include <android-base/macros.h>
Mark Salyzyndd9094a2016-03-01 13:45:42 -080036#include <private/android_filesystem_config.h>
37#include <private/android_logger.h>
38
Tom Cherryc17613c2020-01-08 14:47:42 -080039#include "android/log.h"
Tom Cherry03abfe32020-01-22 07:48:42 -080040#include "log/log_read.h"
Mark Salyzyndd9094a2016-03-01 13:45:42 -080041#include "logger.h"
Tom Cherryebebf692019-01-16 14:17:08 -080042#include "uio.h"
Mark Salyzyndd9094a2016-03-01 13:45:42 -080043
Tom Cherryc377c7d2020-01-27 15:45:52 -080044#ifdef __ANDROID__
Tom Cherryd7df0982020-01-08 15:18:26 -080045#include "logd_writer.h"
46#include "pmsg_writer.h"
Tom Cherry0bb01042019-10-02 10:52:55 -070047#endif
Tom Cherry519b6112019-10-01 13:05:58 -070048
Tom Cherryc17613c2020-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
Jiyong Park52bd8122022-09-07 22:53:28 +090057// The preferred way to access system properties is using android::base::GetProperty in libbase.
58// However, adding dependency to libbase requires that if liblog was statically linked to a client,
59// that client now has additional dependency to libbase as well because static dependencies of
60// static library is not exported. (users of liblog.so however is fine).
61#ifdef __ANDROID__
62#include <sys/system_properties.h>
63#endif
64
Tom Cherrye6830da2020-01-27 13:49:26 -080065using android::base::ErrnoRestorer;
66
Tom Cherryd7df0982020-01-08 15:18:26 -080067#define LOG_BUF_SIZE 1024
68
Dan Willemsena00f44a2016-11-29 13:39:55 -080069#if defined(__ANDROID__)
Tom Cherrybbe34292019-12-11 14:26:37 -080070static int check_log_uid_permissions() {
Tom Cherry366afd82020-01-08 15:34:14 -080071 uid_t uid = getuid();
Mark Salyzynb3db3cf2016-03-28 16:20:29 -070072
Rubin Xu24f69a12022-05-11 21:41:26 +010073 /* Matches clientCanWriteSecurityLog() in logd */
Mark Salyzyn6e315682017-03-09 08:09:43 -080074 if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
75 uid = geteuid();
Mark Salyzynb3db3cf2016-03-28 16:20:29 -070076 if ((uid != AID_SYSTEM) && (uid != AID_ROOT) && (uid != AID_LOG)) {
Mark Salyzyn6e315682017-03-09 08:09:43 -080077 gid_t gid = getgid();
78 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
79 gid = getegid();
80 if ((gid != AID_SYSTEM) && (gid != AID_ROOT) && (gid != AID_LOG)) {
81 int num_groups;
82 gid_t* groups;
Mark Salyzynb3db3cf2016-03-28 16:20:29 -070083
Mark Salyzyn6e315682017-03-09 08:09:43 -080084 num_groups = getgroups(0, NULL);
85 if (num_groups <= 0) {
86 return -EPERM;
87 }
Tom Cherryf623f022019-01-10 10:37:36 -080088 groups = static_cast<gid_t*>(calloc(num_groups, sizeof(gid_t)));
Mark Salyzyn6e315682017-03-09 08:09:43 -080089 if (!groups) {
90 return -ENOMEM;
91 }
92 num_groups = getgroups(num_groups, groups);
93 while (num_groups > 0) {
Rubin Xu24f69a12022-05-11 21:41:26 +010094 if (groups[num_groups - 1] == AID_LOG ||
95 groups[num_groups - 1] == AID_SECURITY_LOG_WRITER) {
Mark Salyzyn6e315682017-03-09 08:09:43 -080096 break;
Mark Salyzynb3db3cf2016-03-28 16:20:29 -070097 }
Mark Salyzyn6e315682017-03-09 08:09:43 -080098 --num_groups;
99 }
100 free(groups);
101 if (num_groups <= 0) {
102 return -EPERM;
103 }
Mark Salyzynb3db3cf2016-03-28 16:20:29 -0700104 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800105 }
Mark Salyzynb3db3cf2016-03-28 16:20:29 -0700106 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800107 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800108 return 0;
Mark Salyzynb3db3cf2016-03-28 16:20:29 -0700109}
Tom Cherrybbe34292019-12-11 14:26:37 -0800110#endif
Mark Salyzynb3db3cf2016-03-28 16:20:29 -0700111
Mark Salyzynacf1f662016-08-23 10:23:36 -0700112/*
113 * Release any logger resources. A new log write will immediately re-acquire.
114 */
Tom Cherry3d6a8782019-02-08 11:46:19 -0800115void __android_log_close() {
Tom Cherryc377c7d2020-01-27 15:45:52 -0800116#ifdef __ANDROID__
Tom Cherryd7df0982020-01-08 15:18:26 -0800117 LogdClose();
118 PmsgClose();
Tom Cherryd7df0982020-01-08 15:18:26 -0800119#endif
Mark Salyzynacf1f662016-08-23 10:23:36 -0700120}
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800121
Colin Crossda4d6df2021-07-14 15:04:20 -0700122// BSD-based systems like Android/macOS have getprogname(). Others need us to provide one.
123#if !defined(__APPLE__) && !defined(__BIONIC__)
Tom Cherry03abfe32020-01-22 07:48:42 -0800124static const char* getprogname() {
Colin Crossda4d6df2021-07-14 15:04:20 -0700125#ifdef _WIN32
Tom Cherry03abfe32020-01-22 07:48:42 -0800126 static bool first = true;
127 static char progname[MAX_PATH] = {};
128
129 if (first) {
130 char path[PATH_MAX + 1];
131 DWORD result = GetModuleFileName(nullptr, path, sizeof(path) - 1);
132 if (result == 0 || result == sizeof(path) - 1) return "";
133 path[PATH_MAX - 1] = 0;
134
135 char* path_basename = basename(path);
136
137 snprintf(progname, sizeof(progname), "%s", path_basename);
138 first = false;
139 }
140
141 return progname;
Colin Crossda4d6df2021-07-14 15:04:20 -0700142#else
143 return program_invocation_short_name;
Tom Cherry03abfe32020-01-22 07:48:42 -0800144#endif
145}
146#endif
147
148// It's possible for logging to happen during static initialization before our globals are
149// initialized, so we place this std::string in a function such that it is initialized on the first
Elliott Hughesf5c76cd2021-03-04 16:17:53 -0800150// call. We use a pointer to avoid exit time destructors.
Tom Cherry77ddcd32020-01-27 08:35:13 -0800151std::string& GetDefaultTag() {
Elliott Hughesf5c76cd2021-03-04 16:17:53 -0800152 static std::string* default_tag = new std::string(getprogname());
153 return *default_tag;
Tom Cherry03abfe32020-01-22 07:48:42 -0800154}
Tom Cherry03abfe32020-01-22 07:48:42 -0800155
156void __android_log_set_default_tag(const char* tag) {
Tom Cherry03abfe32020-01-22 07:48:42 -0800157 GetDefaultTag().assign(tag, 0, LOGGER_ENTRY_MAX_PAYLOAD);
158}
159
Tom Cherry6be0c702020-03-12 11:07:07 -0700160static std::atomic_int32_t minimum_log_priority = ANDROID_LOG_DEFAULT;
161int32_t __android_log_set_minimum_priority(int32_t priority) {
Tom Cherryaca59902020-03-09 12:43:18 -0700162 return minimum_log_priority.exchange(priority, std::memory_order_relaxed);
Tom Cherry40044602020-01-16 15:58:02 -0800163}
164
Tom Cherry6be0c702020-03-12 11:07:07 -0700165int32_t __android_log_get_minimum_priority() {
Tom Cherry40044602020-01-16 15:58:02 -0800166 return minimum_log_priority;
167}
168
Tom Cherryc17613c2020-01-08 14:47:42 -0800169#ifdef __ANDROID__
Jiyong Parke68cbee2023-06-19 13:41:26 +0900170static __android_logger_function logger_function = __android_log_logd_logger;
Jiyong Park52bd8122022-09-07 22:53:28 +0900171#else
Jiyong Parke68cbee2023-06-19 13:41:26 +0900172static __android_logger_function logger_function = __android_log_stderr_logger;
Jiyong Park52bd8122022-09-07 22:53:28 +0900173#endif
Jiyong Park52bd8122022-09-07 22:53:28 +0900174
Tom Cherryc17613c2020-01-08 14:47:42 -0800175void __android_log_set_logger(__android_logger_function logger) {
Jiyong Parke68cbee2023-06-19 13:41:26 +0900176 logger_function = logger;
Tom Cherryc17613c2020-01-08 14:47:42 -0800177}
178
179void __android_log_default_aborter(const char* abort_message) {
180#ifdef __ANDROID__
181 android_set_abort_message(abort_message);
182#else
183 UNUSED(abort_message);
184#endif
185 abort();
186}
187
188static __android_aborter_function aborter_function = __android_log_default_aborter;
Tom Cherryc17613c2020-01-08 14:47:42 -0800189
190void __android_log_set_aborter(__android_aborter_function aborter) {
Tom Cherryc17613c2020-01-08 14:47:42 -0800191 aborter_function = aborter;
192}
193
194void __android_log_call_aborter(const char* abort_message) {
Tom Cherryc17613c2020-01-08 14:47:42 -0800195 aborter_function(abort_message);
196}
197
198#ifdef __ANDROID__
Tom Cherrybbe34292019-12-11 14:26:37 -0800199static int write_to_log(log_id_t log_id, struct iovec* vec, size_t nr) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800200 int ret;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800201 struct timespec ts;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800202
Tom Cherrybbe34292019-12-11 14:26:37 -0800203 if (log_id == LOG_ID_KERNEL) {
204 return -EINVAL;
205 }
206
Tom Cherrya5ff7ae2020-04-08 14:36:05 -0700207 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn2bc10482016-12-28 10:30:57 -0800208
Mark Salyzyn6e315682017-03-09 08:09:43 -0800209 if (log_id == LOG_ID_SECURITY) {
210 if (vec[0].iov_len < 4) {
211 return -EINVAL;
212 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800213
Mark Salyzyn6e315682017-03-09 08:09:43 -0800214 ret = check_log_uid_permissions();
215 if (ret < 0) {
216 return ret;
217 }
218 if (!__android_log_security()) {
219 /* If only we could reset downstream logd counter */
220 return -EPERM;
221 }
Yao Chen36eacf82017-12-01 15:48:19 -0800222 } else if (log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800223 if (vec[0].iov_len < 4) {
224 return -EINVAL;
225 }
Mark Salyzyn6e315682017-03-09 08:09:43 -0800226 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800227
Tom Cherryd7df0982020-01-08 15:18:26 -0800228 ret = LogdWrite(log_id, &ts, vec, nr);
229 PmsgWrite(log_id, &ts, vec, nr);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800230
Mark Salyzyn6e315682017-03-09 08:09:43 -0800231 return ret;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800232}
Tom Cherryc17613c2020-01-08 14:47:42 -0800233#else
234static int write_to_log(log_id_t, struct iovec*, size_t) {
235 // Non-Android text logs should go to __android_log_stderr_logger, not here.
236 // Non-Android binary logs are always dropped.
237 return 1;
238}
239#endif
240
241// Copied from base/threads.cpp
242static uint64_t GetThreadId() {
243#if defined(__BIONIC__)
244 return gettid();
245#elif defined(__APPLE__)
246 uint64_t tid;
247 pthread_threadid_np(NULL, &tid);
248 return tid;
249#elif defined(__linux__)
250 return syscall(__NR_gettid);
251#elif defined(_WIN32)
252 return GetCurrentThreadId();
253#endif
254}
255
Jiyong Park52bd8122022-09-07 22:53:28 +0900256static void filestream_logger(const struct __android_log_message* log_message, FILE* stream) {
Jiyong Park207934d2023-06-23 13:51:53 +0900257 struct timespec ts;
258 clock_gettime(CLOCK_REALTIME, &ts);
Tom Cherryc17613c2020-01-08 14:47:42 -0800259
Elliott Hughesbb9d0642023-06-30 10:21:02 -0700260 struct tm now;
Jiyong Park207934d2023-06-23 13:51:53 +0900261 localtime_r(&ts.tv_sec, &now);
Tom Cherryc17613c2020-01-08 14:47:42 -0800262
Jiyong Park207934d2023-06-23 13:51:53 +0900263 char timestamp[sizeof("mm-DD HH::MM::SS.mmm\0")];
264 size_t n = strftime(timestamp, sizeof(timestamp), "%m-%d %H:%M:%S", &now);
265 snprintf(timestamp + n, sizeof(timestamp) - n, ".%03ld", ts.tv_nsec / (1000 * 1000));
Tom Cherryc17613c2020-01-08 14:47:42 -0800266
267 static const char log_characters[] = "XXVDIWEF";
268 static_assert(arraysize(log_characters) - 1 == ANDROID_LOG_SILENT,
269 "Mismatch in size of log_characters and values in android_LogPriority");
Tom Cherry6be0c702020-03-12 11:07:07 -0700270 int32_t priority =
Tom Cherry781ee662020-03-11 11:07:13 -0700271 log_message->priority > ANDROID_LOG_SILENT ? ANDROID_LOG_FATAL : log_message->priority;
Tom Cherryc17613c2020-01-08 14:47:42 -0800272 char priority_char = log_characters[priority];
273 uint64_t tid = GetThreadId();
Jiyong Park207934d2023-06-23 13:51:53 +0900274 const char* tag = log_message->tag ? log_message->tag : " nullptr";
Tom Cherryc17613c2020-01-08 14:47:42 -0800275
Tom Cherry781ee662020-03-11 11:07:13 -0700276 if (log_message->file != nullptr) {
Jiyong Park207934d2023-06-23 13:51:53 +0900277 fprintf(stream, "%s %5d %5" PRIu64 " %c %-8s: %s:%u %s\n", timestamp, getpid(), tid,
278 priority_char, tag, log_message->file, log_message->line, log_message->message);
Tom Cherryc17613c2020-01-08 14:47:42 -0800279 } else {
Jiyong Park207934d2023-06-23 13:51:53 +0900280 fprintf(stream, "%s %5d %5" PRIu64 " %c %-8s: %s\n", timestamp, getpid(), tid, priority_char,
281 tag, log_message->message);
Tom Cherryc17613c2020-01-08 14:47:42 -0800282 }
283}
284
Jiyong Parke68cbee2023-06-19 13:41:26 +0900285static const char* get_file_logger_path() {
Jiyong Park52bd8122022-09-07 22:53:28 +0900286#ifdef __ANDROID__
Jiyong Parke68cbee2023-06-19 13:41:26 +0900287 static const char* file_logger_path = []() {
288 static char path[PROP_VALUE_MAX] = {};
289 if (__system_property_get("ro.log.file_logger.path", path) > 0) {
290 return path;
Jiyong Park52bd8122022-09-07 22:53:28 +0900291 }
Jiyong Parke68cbee2023-06-19 13:41:26 +0900292 return static_cast<char*>(nullptr); // means file_logger should not be used
293 }();
294 return file_logger_path;
295#else
296 return nullptr;
Jiyong Park52bd8122022-09-07 22:53:28 +0900297#endif
Jiyong Parke68cbee2023-06-19 13:41:26 +0900298}
299
300/*
301 * If ro.log.file_logger.path is set to a file, send log_message to the file instead. This is for
302 * Android-like environments where logd is not available; e.g. Microdroid. If the file is not
303 * accessible (but ro.log.file_logger.path is set anyway), stderr is chosen as the fallback.
304 *
305 * Returns true if log was sent to file. false, if not.
306 */
307static bool log_to_file_if_overridden(const struct __android_log_message* log_message) {
308 const char* file_logger_path = get_file_logger_path();
309 if (file_logger_path == nullptr) return false;
310
311 static FILE* stream = [&file_logger_path]() {
312 FILE* f = fopen(file_logger_path, "ae");
313 if (f != nullptr) return f;
314 using namespace std::string_literals;
315 std::string err_msg = "Cannot open "s + file_logger_path + " for logging: (" + strerror(errno) +
316 "). Falling back to stderr";
317 __android_log_message m = {sizeof(__android_log_message),
318 LOG_ID_DEFAULT,
319 ANDROID_LOG_WARN,
320 "liblog",
321 __FILE__,
322 __LINE__,
323 err_msg.c_str()};
324 filestream_logger(&m, stderr);
Jiyong Park52bd8122022-09-07 22:53:28 +0900325 return stderr;
326 }();
327 filestream_logger(log_message, stream);
Jiyong Parke68cbee2023-06-19 13:41:26 +0900328 return true;
Jiyong Park52bd8122022-09-07 22:53:28 +0900329}
330
331void __android_log_stderr_logger(const struct __android_log_message* log_message) {
332 filestream_logger(log_message, stderr);
333}
334
Tom Cherry781ee662020-03-11 11:07:13 -0700335void __android_log_logd_logger(const struct __android_log_message* log_message) {
Jiyong Parke68cbee2023-06-19 13:41:26 +0900336 if (log_to_file_if_overridden(log_message)) return;
337
Tom Cherry781ee662020-03-11 11:07:13 -0700338 int buffer_id = log_message->buffer_id == LOG_ID_DEFAULT ? LOG_ID_MAIN : log_message->buffer_id;
Tom Cherryc17613c2020-01-08 14:47:42 -0800339
340 struct iovec vec[3];
341 vec[0].iov_base =
Tom Cherry781ee662020-03-11 11:07:13 -0700342 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(&log_message->priority));
Tom Cherryc17613c2020-01-08 14:47:42 -0800343 vec[0].iov_len = 1;
Tom Cherry781ee662020-03-11 11:07:13 -0700344 vec[1].iov_base = const_cast<void*>(static_cast<const void*>(log_message->tag));
345 vec[1].iov_len = strlen(log_message->tag) + 1;
346 vec[2].iov_base = const_cast<void*>(static_cast<const void*>(log_message->message));
347 vec[2].iov_len = strlen(log_message->message) + 1;
Tom Cherryc17613c2020-01-08 14:47:42 -0800348
349 write_to_log(static_cast<log_id_t>(buffer_id), vec, 3);
350}
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800351
Tom Cherry3d6a8782019-02-08 11:46:19 -0800352int __android_log_write(int prio, const char* tag, const char* msg) {
Mark Salyzyn6e315682017-03-09 08:09:43 -0800353 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800354}
355
Tom Cherry781ee662020-03-11 11:07:13 -0700356void __android_log_write_log_message(__android_log_message* log_message) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800357 ErrnoRestorer errno_restorer;
358
Tom Cherry781ee662020-03-11 11:07:13 -0700359 if (log_message->buffer_id != LOG_ID_DEFAULT && log_message->buffer_id != LOG_ID_MAIN &&
360 log_message->buffer_id != LOG_ID_SYSTEM && log_message->buffer_id != LOG_ID_RADIO &&
361 log_message->buffer_id != LOG_ID_CRASH) {
Tom Cherrycbad0b92020-01-28 13:06:29 -0800362 return;
363 }
364
Tom Cherry781ee662020-03-11 11:07:13 -0700365 if (log_message->tag == nullptr) {
Tom Cherry781ee662020-03-11 11:07:13 -0700366 log_message->tag = GetDefaultTag().c_str();
Tom Cherry03abfe32020-01-22 07:48:42 -0800367 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800368
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800369#if __BIONIC__
Tom Cherry781ee662020-03-11 11:07:13 -0700370 if (log_message->priority == ANDROID_LOG_FATAL) {
371 android_set_abort_message(log_message->message);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800372 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800373#endif
374
Jiyong Parke68cbee2023-06-19 13:41:26 +0900375 logger_function(log_message);
Tom Cherryc17613c2020-01-08 14:47:42 -0800376}
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800377
Tom Cherryc17613c2020-01-08 14:47:42 -0800378int __android_log_buf_write(int bufID, int prio, const char* tag, const char* msg) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800379 ErrnoRestorer errno_restorer;
380
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800381 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherrybc593cb2020-04-16 11:20:29 -0700382 return -EPERM;
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800383 }
384
Tom Cherry781ee662020-03-11 11:07:13 -0700385 __android_log_message log_message = {
386 sizeof(__android_log_message), bufID, prio, tag, nullptr, 0, msg};
387 __android_log_write_log_message(&log_message);
Tom Cherryc17613c2020-01-08 14:47:42 -0800388 return 1;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800389}
390
Tom Cherry3d6a8782019-02-08 11:46:19 -0800391int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800392 ErrnoRestorer errno_restorer;
393
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800394 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherrybc593cb2020-04-16 11:20:29 -0700395 return -EPERM;
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800396 }
397
Tom Cherry99d7cc32020-02-21 15:06:46 -0800398 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800399
Mark Salyzyn6e315682017-03-09 08:09:43 -0800400 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800401
Tom Cherry781ee662020-03-11 11:07:13 -0700402 __android_log_message log_message = {
403 sizeof(__android_log_message), LOG_ID_MAIN, prio, tag, nullptr, 0, buf};
404 __android_log_write_log_message(&log_message);
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800405 return 1;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800406}
407
Tom Cherry3d6a8782019-02-08 11:46:19 -0800408int __android_log_print(int prio, const char* tag, const char* fmt, ...) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800409 ErrnoRestorer errno_restorer;
410
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800411 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherrybc593cb2020-04-16 11:20:29 -0700412 return -EPERM;
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800413 }
414
Mark Salyzyn6e315682017-03-09 08:09:43 -0800415 va_list ap;
Tom Cherry99d7cc32020-02-21 15:06:46 -0800416 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800417
Mark Salyzyn6e315682017-03-09 08:09:43 -0800418 va_start(ap, fmt);
419 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
420 va_end(ap);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800421
Tom Cherry781ee662020-03-11 11:07:13 -0700422 __android_log_message log_message = {
423 sizeof(__android_log_message), LOG_ID_MAIN, prio, tag, nullptr, 0, buf};
424 __android_log_write_log_message(&log_message);
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800425 return 1;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800426}
427
Tom Cherry3d6a8782019-02-08 11:46:19 -0800428int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800429 ErrnoRestorer errno_restorer;
430
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800431 if (!__android_log_is_loggable(prio, tag, ANDROID_LOG_VERBOSE)) {
Tom Cherrybc593cb2020-04-16 11:20:29 -0700432 return -EPERM;
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800433 }
434
Mark Salyzyn6e315682017-03-09 08:09:43 -0800435 va_list ap;
Tom Cherry99d7cc32020-02-21 15:06:46 -0800436 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800437
Mark Salyzyn6e315682017-03-09 08:09:43 -0800438 va_start(ap, fmt);
439 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
440 va_end(ap);
441
Tom Cherry781ee662020-03-11 11:07:13 -0700442 __android_log_message log_message = {
443 sizeof(__android_log_message), bufID, prio, tag, nullptr, 0, buf};
444 __android_log_write_log_message(&log_message);
Tom Cherryb9e0d5e2020-01-22 08:20:03 -0800445 return 1;
Mark Salyzyn6e315682017-03-09 08:09:43 -0800446}
447
Tom Cherry3d6a8782019-02-08 11:46:19 -0800448void __android_log_assert(const char* cond, const char* tag, const char* fmt, ...) {
Tom Cherry99d7cc32020-02-21 15:06:46 -0800449 __attribute__((uninitialized)) char buf[LOG_BUF_SIZE];
Mark Salyzyn6e315682017-03-09 08:09:43 -0800450
451 if (fmt) {
452 va_list ap;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800453 va_start(ap, fmt);
454 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
455 va_end(ap);
Mark Salyzyn6e315682017-03-09 08:09:43 -0800456 } else {
457 /* Msg not provided, log condition. N.B. Do not use cond directly as
458 * format string as it could contain spurious '%' syntax (e.g.
459 * "%d" in "blocks%devs == 0").
460 */
461 if (cond)
462 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
463 else
464 strcpy(buf, "Unspecified assertion failed");
465 }
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800466
Mark Salyzyn6e315682017-03-09 08:09:43 -0800467 // Log assertion failures to stderr for the benefit of "adb shell" users
468 // and gtests (http://b/23675822).
Tom Cherryebebf692019-01-16 14:17:08 -0800469 TEMP_FAILURE_RETRY(write(2, buf, strlen(buf)));
470 TEMP_FAILURE_RETRY(write(2, "\n", 1));
Mark Salyzyn6e315682017-03-09 08:09:43 -0800471
472 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Tom Cherryc17613c2020-01-08 14:47:42 -0800473 __android_log_call_aborter(buf);
474 abort();
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800475}
476
Tom Cherry3d6a8782019-02-08 11:46:19 -0800477int __android_log_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800478 ErrnoRestorer errno_restorer;
479
Mark Salyzyn6e315682017-03-09 08:09:43 -0800480 struct iovec vec[2];
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800481
Mark Salyzyn6e315682017-03-09 08:09:43 -0800482 vec[0].iov_base = &tag;
483 vec[0].iov_len = sizeof(tag);
484 vec[1].iov_base = (void*)payload;
485 vec[1].iov_len = len;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800486
Mark Salyzyn6e315682017-03-09 08:09:43 -0800487 return write_to_log(LOG_ID_EVENTS, vec, 2);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800488}
489
Tom Cherry3d6a8782019-02-08 11:46:19 -0800490int __android_log_stats_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800491 ErrnoRestorer errno_restorer;
492
Stefan Lafon5f25ab92017-08-24 20:14:06 -0700493 struct iovec vec[2];
494
495 vec[0].iov_base = &tag;
496 vec[0].iov_len = sizeof(tag);
497 vec[1].iov_base = (void*)payload;
498 vec[1].iov_len = len;
499
500 return write_to_log(LOG_ID_STATS, vec, 2);
501}
502
Tom Cherry3d6a8782019-02-08 11:46:19 -0800503int __android_log_security_bwrite(int32_t tag, const void* payload, size_t len) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800504 ErrnoRestorer errno_restorer;
505
Mark Salyzyn6e315682017-03-09 08:09:43 -0800506 struct iovec vec[2];
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800507
Mark Salyzyn6e315682017-03-09 08:09:43 -0800508 vec[0].iov_base = &tag;
509 vec[0].iov_len = sizeof(tag);
510 vec[1].iov_base = (void*)payload;
511 vec[1].iov_len = len;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800512
Mark Salyzyn6e315682017-03-09 08:09:43 -0800513 return write_to_log(LOG_ID_SECURITY, vec, 2);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800514}
515
516/*
517 * Like __android_log_bwrite, but takes the type as well. Doesn't work
518 * for the general case where we're generating lists of stuff, but very
519 * handy if we just want to dump an integer into the log.
520 */
Tom Cherry3d6a8782019-02-08 11:46:19 -0800521int __android_log_btwrite(int32_t tag, char type, const void* payload, size_t len) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800522 ErrnoRestorer errno_restorer;
523
Mark Salyzyn6e315682017-03-09 08:09:43 -0800524 struct iovec vec[3];
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800525
Mark Salyzyn6e315682017-03-09 08:09:43 -0800526 vec[0].iov_base = &tag;
527 vec[0].iov_len = sizeof(tag);
528 vec[1].iov_base = &type;
529 vec[1].iov_len = sizeof(type);
530 vec[2].iov_base = (void*)payload;
531 vec[2].iov_len = len;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800532
Mark Salyzyn6e315682017-03-09 08:09:43 -0800533 return write_to_log(LOG_ID_EVENTS, vec, 3);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800534}
535
536/*
537 * Like __android_log_bwrite, but used for writing strings to the
538 * event log.
539 */
Tom Cherry3d6a8782019-02-08 11:46:19 -0800540int __android_log_bswrite(int32_t tag, const char* payload) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800541 ErrnoRestorer errno_restorer;
542
Mark Salyzyn6e315682017-03-09 08:09:43 -0800543 struct iovec vec[4];
544 char type = EVENT_TYPE_STRING;
545 uint32_t len = strlen(payload);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800546
Mark Salyzyn6e315682017-03-09 08:09:43 -0800547 vec[0].iov_base = &tag;
548 vec[0].iov_len = sizeof(tag);
549 vec[1].iov_base = &type;
550 vec[1].iov_len = sizeof(type);
551 vec[2].iov_base = &len;
552 vec[2].iov_len = sizeof(len);
553 vec[3].iov_base = (void*)payload;
554 vec[3].iov_len = len;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800555
Mark Salyzyn6e315682017-03-09 08:09:43 -0800556 return write_to_log(LOG_ID_EVENTS, vec, 4);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800557}
558
559/*
560 * Like __android_log_security_bwrite, but used for writing strings to the
561 * security log.
562 */
Tom Cherry3d6a8782019-02-08 11:46:19 -0800563int __android_log_security_bswrite(int32_t tag, const char* payload) {
Tom Cherrye6830da2020-01-27 13:49:26 -0800564 ErrnoRestorer errno_restorer;
565
Mark Salyzyn6e315682017-03-09 08:09:43 -0800566 struct iovec vec[4];
567 char type = EVENT_TYPE_STRING;
568 uint32_t len = strlen(payload);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800569
Mark Salyzyn6e315682017-03-09 08:09:43 -0800570 vec[0].iov_base = &tag;
571 vec[0].iov_len = sizeof(tag);
572 vec[1].iov_base = &type;
573 vec[1].iov_len = sizeof(type);
574 vec[2].iov_base = &len;
575 vec[2].iov_len = sizeof(len);
576 vec[3].iov_base = (void*)payload;
577 vec[3].iov_len = len;
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800578
Mark Salyzyn6e315682017-03-09 08:09:43 -0800579 return write_to_log(LOG_ID_SECURITY, vec, 4);
Mark Salyzyndd9094a2016-03-01 13:45:42 -0800580}