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> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 21 | #include <sstream> |
| 22 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 23 | #include "base/mutex.h" |
Andreas Gampe | b486a98 | 2017-06-01 13:45:54 -0700 | [diff] [blame] | 24 | #include "thread-current-inl.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 25 | #include "utils.h" |
| 26 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 27 | // Headers for LogMessage::LogLine. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 28 | #ifdef ART_TARGET_ANDROID |
Mark Salyzyn | 46fc9d6 | 2017-01-10 15:12:38 -0800 | [diff] [blame] | 29 | #include <log/log.h> |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 30 | #else |
| 31 | #include <sys/types.h> |
| 32 | #include <unistd.h> |
| 33 | #endif |
| 34 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 35 | namespace art { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 36 | |
Andreas Gampe | 1c5b42f | 2017-06-15 18:20:45 -0700 | [diff] [blame] | 37 | // We test here that the runtime-debug-checks are actually a no-op constexpr false in release |
| 38 | // builds, as we can't check that in gtests (which are always debug). |
| 39 | |
| 40 | #ifdef NDEBUG |
| 41 | namespace { |
| 42 | DECLARE_RUNTIME_DEBUG_FLAG(kTestForConstexpr); |
| 43 | static_assert(!kTestForConstexpr, "Issue with DECLARE_RUNTIME_DEBUG_FLAG in NDEBUG."); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | // Implementation of runtime debug flags. This should be compile-time optimized away in release |
| 48 | // builds. |
| 49 | namespace { |
| 50 | bool gSlowEnabled = false; // Default for slow flags is "off." |
| 51 | |
| 52 | // Use a function with a static to ensure our vector storage doesn't have initialization order |
| 53 | // issues. |
| 54 | std::vector<bool*>& GetFlagPtrs() { |
| 55 | static std::vector<bool*> g_flag_ptrs; |
| 56 | return g_flag_ptrs; |
| 57 | } |
| 58 | |
| 59 | bool RegisterRuntimeDebugFlagImpl(bool* flag_ptr) { |
| 60 | GetFlagPtrs().push_back(flag_ptr); |
| 61 | return gSlowEnabled; |
| 62 | } |
| 63 | |
| 64 | void SetRuntimeDebugFlagsEnabledImpl(bool enabled) { |
| 65 | gSlowEnabled = enabled; |
| 66 | for (bool* flag_ptr : GetFlagPtrs()) { |
| 67 | *flag_ptr = enabled; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | } // namespace |
| 72 | |
| 73 | bool RegisterRuntimeDebugFlag(bool* flag_ptr) { |
| 74 | if (kIsDebugBuild) { |
| 75 | return RegisterRuntimeDebugFlagImpl(flag_ptr); |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | void SetRuntimeDebugFlagsEnabled(bool enabled) { |
| 81 | if (kIsDebugBuild) { |
| 82 | SetRuntimeDebugFlagsEnabledImpl(enabled); |
| 83 | } |
| 84 | } |
| 85 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 86 | LogVerbosity gLogVerbosity; |
| 87 | |
Andreas Gampe | 247fc33 | 2017-06-22 16:18:24 -0700 | [diff] [blame] | 88 | std::atomic<unsigned int> gAborting(0); |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 89 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 90 | static std::unique_ptr<std::string> gCmdLine; |
| 91 | static std::unique_ptr<std::string> gProgramInvocationName; |
| 92 | static std::unique_ptr<std::string> gProgramInvocationShortName; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 93 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 94 | const char* GetCmdLine() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 95 | return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | const char* ProgramInvocationName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 99 | return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | const char* ProgramInvocationShortName() { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 103 | return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str() |
| 104 | : "art"; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 105 | } |
| 106 | |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 107 | void InitLogging(char* argv[], AbortFunction& abort_function) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 108 | if (gCmdLine.get() != nullptr) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 109 | return; |
| 110 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 111 | // TODO: Move this to a more obvious InitART... |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 112 | Locks::Init(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 114 | // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this, |
| 115 | // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are |
| 116 | // commonly used. |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 117 | if (argv != nullptr) { |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 118 | gCmdLine.reset(new std::string(argv[0])); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 119 | for (size_t i = 1; argv[i] != nullptr; ++i) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 120 | gCmdLine->append(" "); |
| 121 | gCmdLine->append(argv[i]); |
| 122 | } |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 123 | gProgramInvocationName.reset(new std::string(argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 124 | const char* last_slash = strrchr(argv[0], '/'); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 125 | gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1 |
Ian Rogers | 1e363f9 | 2013-11-13 15:58:24 -0800 | [diff] [blame] | 126 | : argv[0])); |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame] | 127 | } else { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 128 | // 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] | 129 | gCmdLine.reset(new std::string("<unset>")); |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 130 | } |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 131 | |
Julien Duraj | 1af0c4f | 2016-11-16 14:05:48 +0000 | [diff] [blame] | 132 | #ifdef ART_TARGET_ANDROID |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 133 | #define INIT_LOGGING_DEFAULT_LOGGER android::base::LogdLogger() |
| 134 | #else |
| 135 | #define INIT_LOGGING_DEFAULT_LOGGER android::base::StderrLogger |
| 136 | #endif |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 137 | android::base::InitLogging(argv, INIT_LOGGING_DEFAULT_LOGGER, |
| 138 | std::move<AbortFunction>(abort_function)); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 139 | #undef INIT_LOGGING_DEFAULT_LOGGER |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 142 | #ifdef ART_TARGET_ANDROID |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 143 | static const android_LogPriority kLogSeverityToAndroidLogPriority[] = { |
| 144 | ANDROID_LOG_VERBOSE, ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, |
| 145 | ANDROID_LOG_ERROR, ANDROID_LOG_FATAL, ANDROID_LOG_FATAL |
| 146 | }; |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 147 | static_assert(arraysize(kLogSeverityToAndroidLogPriority) == ::android::base::FATAL + 1, |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 148 | "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity"); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 149 | #endif |
| 150 | |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 151 | void LogHelper::LogLineLowStack(const char* file, |
| 152 | unsigned int line, |
| 153 | LogSeverity log_severity, |
| 154 | const char* message) { |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 155 | #ifdef ART_TARGET_ANDROID |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 156 | // Use android_writeLog() to avoid stack-based buffers used by android_printLog(). |
| 157 | const char* tag = ProgramInvocationShortName(); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 158 | int priority = kLogSeverityToAndroidLogPriority[static_cast<size_t>(log_severity)]; |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 159 | char* buf = nullptr; |
| 160 | size_t buf_size = 0u; |
| 161 | if (priority == ANDROID_LOG_FATAL) { |
| 162 | // Allocate buffer for snprintf(buf, buf_size, "%s:%u] %s", file, line, message) below. |
| 163 | // If allocation fails, fall back to printing only the message. |
Andreas Gampe | 01f7743 | 2017-05-24 09:56:30 -0700 | [diff] [blame] | 164 | buf_size = strlen(file) + 1 /* ':' */ + std::numeric_limits<decltype(line)>::max_digits10 + |
Vladimir Marko | b8f2f63 | 2015-01-02 14:23:26 +0000 | [diff] [blame] | 165 | 2 /* "] " */ + strlen(message) + 1 /* terminating 0 */; |
| 166 | buf = reinterpret_cast<char*>(malloc(buf_size)); |
| 167 | } |
| 168 | if (buf != nullptr) { |
| 169 | snprintf(buf, buf_size, "%s:%u] %s", file, line, message); |
| 170 | android_writeLog(priority, tag, buf); |
| 171 | free(buf); |
| 172 | } else { |
| 173 | android_writeLog(priority, tag, message); |
| 174 | } |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 175 | #else |
Andreas Gampe | 5fd66d0 | 2016-09-12 20:22:19 -0700 | [diff] [blame] | 176 | static constexpr char kLogCharacters[] = { 'V', 'D', 'I', 'W', 'E', 'F', 'F' }; |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 177 | static_assert( |
| 178 | arraysize(kLogCharacters) == static_cast<size_t>(::android::base::FATAL) + 1, |
| 179 | "Wrong character array size"); |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 180 | |
| 181 | const char* program_name = ProgramInvocationShortName(); |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 182 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, program_name, strlen(program_name))); |
| 183 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1)); |
Andreas Gampe | 7fe3023 | 2016-03-25 16:58:00 -0700 | [diff] [blame] | 184 | 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] | 185 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1)); |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 186 | // TODO: pid and tid. |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 187 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, file, strlen(file))); |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 188 | // TODO: line. |
| 189 | UNUSED(line); |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 190 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, "] ", 2)); |
| 191 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, message, strlen(message))); |
| 192 | TEMP_FAILURE_RETRY(write(STDERR_FILENO, "\n", 1)); |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 193 | #endif // ART_TARGET_ANDROID |
Ian Rogers | f4d4da1 | 2014-11-11 16:10:33 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 196 | } // namespace art |