Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
| 17 | #include "logging.h" |
| 18 | |
Andreas Gampe | ed95754 | 2015-01-07 18:01:29 -0800 | [diff] [blame] | 19 | #include <iostream> |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 20 | #include <limits> |
Tom Cherry | 0240d2b | 2020-04-23 15:45:22 -0700 | [diff] [blame] | 21 | #include <mutex> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 22 | #include <sstream> |
| 23 | |
Andreas Gampe | 39b378c | 2017-12-07 15:44:13 -0800 | [diff] [blame] | 24 | #include "aborting.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 25 | #include "os.h" |
| 26 | #include "unix_file/fd_file.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 27 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 28 | // Headers for LogMessage::LogLine. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 29 | #ifdef ART_TARGET_ANDROID |
Mark Salyzyn | 46fc9d6 | 2017-01-10 15:12:38 -0800 | [diff] [blame] | 30 | #include <log/log.h> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 31 | #else |
| 32 | #include <sys/types.h> |
| 33 | #include <unistd.h> |
| 34 | #endif |
| 35 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 36 | namespace art { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 37 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 38 | LogVerbosity gLogVerbosity; |
| 39 | |
Andreas Gampe | 247fc33 | 2017-06-22 16:18:24 -0700 | [diff] [blame] | 40 | std::atomic<unsigned int> gAborting(0); |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 41 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 42 | static std::unique_ptr<std::string> gCmdLine; |
| 43 | static std::unique_ptr<std::string> gProgramInvocationName; |
| 44 | static std::unique_ptr<std::string> gProgramInvocationShortName; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 46 | const char* GetCmdLine() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 47 | return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | const char* ProgramInvocationName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 51 | return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | const char* ProgramInvocationShortName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 55 | return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str() |
| 56 | : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 57 | } |
| 58 | |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 59 | void InitLogging(char* argv[], AbortFunction& abort_function) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 60 | if (gCmdLine.get() != nullptr) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 64 | // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this, |
| 65 | // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are |
| 66 | // commonly used. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 67 | if (argv != nullptr) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 68 | gCmdLine.reset(new std::string(argv[0])); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 69 | for (size_t i = 1; argv[i] != nullptr; ++i) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 70 | gCmdLine->append(" "); |
| 71 | gCmdLine->append(argv[i]); |
| 72 | } |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 73 | gProgramInvocationName.reset(new std::string(argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 74 | const char* last_slash = strrchr(argv[0], '/'); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 75 | gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1 |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 76 | : argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 77 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 78 | // TODO: fall back to /proc/self/cmdline when argv is null on Linux. |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 79 | gCmdLine.reset(new std::string("<unset>")); |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 80 | } |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 81 | |
Julien Duraj | 1af0c4f | 2016-11-16 14:05:48 +0000 | [diff] [blame] | 82 | #ifdef ART_TARGET_ANDROID |
Tom Cherry | 0240d2b | 2020-04-23 15:45:22 -0700 | [diff] [blame] | 83 | // android::base::LogdLogger breaks messages up into line delimited 4K chunks, since that is the |
| 84 | // most that logd can handle per message. To prevent other threads from interleaving their |
| 85 | // messages, LogdLoggerLocked uses a mutex to ensure that only one ART thread is logging at a |
| 86 | // time. |
| 87 | // Note that this lock makes logging after fork() unsafe in multi-threaded programs, which is part |
| 88 | // of the motivation that this lock is not a part of libbase logging. Zygote guarantees that no |
| 89 | // threads are running before calling fork() via ZygoteHooks.waitUntilAllThreadsStopped(). |
| 90 | class LogdLoggerLocked { |
| 91 | public: |
| 92 | LogdLoggerLocked() {} |
| 93 | void operator()(android::base::LogId id, |
| 94 | android::base::LogSeverity severity, |
| 95 | const char* tag, |
| 96 | const char* file, |
| 97 | unsigned int line, |
| 98 | const char* message) { |
| 99 | static std::mutex* logging_lock_ = new std::mutex(); |
| 100 | std::lock_guard<std::mutex> guard(*logging_lock_); |
| 101 | logd_logger_(id, severity, tag, file, line, message); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | android::base::LogdLogger logd_logger_; |
| 106 | }; |
| 107 | #define INIT_LOGGING_DEFAULT_LOGGER LogdLoggerLocked() |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 108 | #else |
| 109 | #define INIT_LOGGING_DEFAULT_LOGGER android::base::StderrLogger |
| 110 | #endif |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 111 | android::base::InitLogging(argv, INIT_LOGGING_DEFAULT_LOGGER, |
| 112 | std::move<AbortFunction>(abort_function)); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 113 | #undef INIT_LOGGING_DEFAULT_LOGGER |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 116 | #ifdef ART_TARGET_ANDROID |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 117 | static const android_LogPriority kLogSeverityToAndroidLogPriority[] = { |
| 118 | ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, |
| 119 | ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, ANDROID_LOG_FATAL |
| 120 | }; |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 121 | static_assert(arraysize(kLogSeverityToAndroidLogPriority) == ::android::base::FATAL + 1, |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 122 | "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity"); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 123 | #endif |
| 124 | |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 125 | void LogHelper::LogLineLowStack(const char* file, |
| 126 | unsigned int line, |
| 127 | LogSeverity log_severity, |
| 128 | const char* message) { |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 129 | #ifdef ART_TARGET_ANDROID |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 130 | // Use android_writeLog() to avoid stack-based buffers used by android_printLog(). |
| 131 | const char* tag = ProgramInvocationShortName(); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 132 | int priority = kLogSeverityToAndroidLogPriority[static_cast<size_t>(log_severity)]; |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 133 | char* buf = nullptr; |
| 134 | size_t buf_size = 0u; |
| 135 | if (priority == ANDROID_LOG_FATAL) { |
| 136 | // Allocate buffer for snprintf(buf, buf_size, "%s:%u] %s", file, line, message) below. |
| 137 | // If allocation fails, fall back to printing only the message. |
Andreas Gampe | 01f7743 | 2017-05-24 09:56:30 -0700 | [diff] [blame] | 138 | buf_size = strlen(file) + 1 /* ':' */ + std::numeric_limits<decltype(line)>::max_digits10 + |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 139 | 2 /* "] " */ + strlen(message) + 1 /* terminating 0 */; |
| 140 | buf = reinterpret_cast<char*>(malloc(buf_size)); |
| 141 | } |
| 142 | if (buf != nullptr) { |
| 143 | snprintf(buf, buf_size, "%s:%u] %s", file, line, message); |
| 144 | android_writeLog(priority, tag, buf); |
| 145 | free(buf); |
| 146 | } else { |
| 147 | android_writeLog(priority, tag, message); |
| 148 | } |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 149 | #else |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 150 | static constexpr char kLogCharacters[] = { 'V', 'D', 'I', 'W', 'E', 'F', 'F' }; |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 151 | static_assert( |
| 152 | arraysize(kLogCharacters) == static_cast<size_t>(::android::base::FATAL) + 1, |
| 153 | "Wrong character array size"); |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 154 | |
| 155 | const char* program_name = ProgramInvocationShortName(); |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 156 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, program_name, strlen(program_name))); |
| 157 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1)); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 158 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, &kLogCharacters[static_cast<size_t>(log_severity)], 1)); |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 159 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1)); |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 160 | // TODO: pid and tid. |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 161 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, file, strlen(file))); |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 162 | // TODO: line. |
| 163 | UNUSED(line); |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 164 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, "] ", 2)); |
| 165 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, message, strlen(message))); |
| 166 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, "\n", 1)); |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 167 | #endif // ART_TARGET_ANDROID |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 168 | } |
| 169 | |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 170 | bool PrintFileToLog(const std::string& file_name, android::base::LogSeverity level) { |
| 171 | File file(file_name, O_RDONLY, false); |
| 172 | if (!file.IsOpened()) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings. |
| 177 | char buf[kBufSize + 1]; // +1 for terminator. |
| 178 | size_t filled_to = 0; |
| 179 | while (true) { |
| 180 | DCHECK_LT(filled_to, kBufSize); |
| 181 | int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to)); |
| 182 | if (n <= 0) { |
| 183 | // Print the rest of the buffer, if it exists. |
| 184 | if (filled_to > 0) { |
| 185 | buf[filled_to] = 0; |
| 186 | LOG(level) << buf; |
| 187 | } |
| 188 | return n == 0; |
| 189 | } |
| 190 | // Scan for '\n'. |
| 191 | size_t i = filled_to; |
| 192 | bool found_newline = false; |
| 193 | for (; i < filled_to + n; ++i) { |
| 194 | if (buf[i] == '\n') { |
| 195 | // Found a line break, that's something to print now. |
| 196 | buf[i] = 0; |
| 197 | LOG(level) << buf; |
| 198 | // Copy the rest to the front. |
| 199 | if (i + 1 < filled_to + n) { |
| 200 | memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1); |
| 201 | filled_to = filled_to + n - i - 1; |
| 202 | } else { |
| 203 | filled_to = 0; |
| 204 | } |
| 205 | found_newline = true; |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | if (found_newline) { |
| 210 | continue; |
| 211 | } else { |
| 212 | filled_to += n; |
| 213 | // Check if we must flush now. |
| 214 | if (filled_to == kBufSize) { |
| 215 | buf[kBufSize] = 0; |
| 216 | LOG(level) << buf; |
| 217 | filled_to = 0; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 223 | } // namespace art |