blob: 4776357fdff04c700526a4491d9801469d262157 [file] [log] [blame]
Elliott Hughes13f5a582011-09-06 13:39:14 -07001/*
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 Gampeed957542015-01-07 18:01:29 -080019#include <iostream>
Vladimir Markob8f2f632015-01-02 14:23:26 +000020#include <limits>
Ian Rogersc7dd2952014-10-21 23:31:19 -070021#include <sstream>
22
Elliott Hughes76b61672012-12-12 17:47:30 -080023#include "base/mutex.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070024#include "thread-current-inl.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070025#include "utils.h"
26
Ian Rogersc7dd2952014-10-21 23:31:19 -070027// Headers for LogMessage::LogLine.
Bilyan Borisovbb661c02016-04-04 16:27:32 +010028#ifdef ART_TARGET_ANDROID
Mark Salyzyn46fc9d62017-01-10 15:12:38 -080029#include <log/log.h>
Ian Rogersc7dd2952014-10-21 23:31:19 -070030#else
31#include <sys/types.h>
32#include <unistd.h>
33#endif
34
Elliott Hughesf5a7a472011-10-07 14:31:02 -070035namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070036
Andreas Gampe1c5b42f2017-06-15 18:20:45 -070037// 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
41namespace {
42DECLARE_RUNTIME_DEBUG_FLAG(kTestForConstexpr);
43static_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.
49namespace {
50bool 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.
54std::vector<bool*>& GetFlagPtrs() {
55 static std::vector<bool*> g_flag_ptrs;
56 return g_flag_ptrs;
57}
58
59bool RegisterRuntimeDebugFlagImpl(bool* flag_ptr) {
60 GetFlagPtrs().push_back(flag_ptr);
61 return gSlowEnabled;
62}
63
64void SetRuntimeDebugFlagsEnabledImpl(bool enabled) {
65 gSlowEnabled = enabled;
66 for (bool* flag_ptr : GetFlagPtrs()) {
67 *flag_ptr = enabled;
68 }
69}
70
71} // namespace
72
73bool RegisterRuntimeDebugFlag(bool* flag_ptr) {
74 if (kIsDebugBuild) {
75 return RegisterRuntimeDebugFlagImpl(flag_ptr);
76 }
77 return false;
78}
79
80void SetRuntimeDebugFlagsEnabled(bool enabled) {
81 if (kIsDebugBuild) {
82 SetRuntimeDebugFlagsEnabledImpl(enabled);
83 }
84}
85
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080086LogVerbosity gLogVerbosity;
87
Andreas Gampe247fc332017-06-22 16:18:24 -070088std::atomic<unsigned int> gAborting(0);
Nicolas Geoffraydb978712014-12-09 13:33:38 +000089
Ian Rogers700a4022014-05-19 16:49:03 -070090static std::unique_ptr<std::string> gCmdLine;
91static std::unique_ptr<std::string> gProgramInvocationName;
92static std::unique_ptr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070093
Elliott Hughes0d39c122012-06-06 16:41:17 -070094const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080095 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070096}
97
98const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080099 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -0700100}
101
102const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -0800103 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
104 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -0700105}
106
David Sehrf57589f2016-10-17 10:09:33 -0700107void InitLogging(char* argv[], AbortFunction& abort_function) {
Ian Rogers1e363f92013-11-13 15:58:24 -0800108 if (gCmdLine.get() != nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -0700109 return;
110 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700111 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -0700112 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700113
Elliott Hughes0d39c122012-06-06 16:41:17 -0700114 // 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 Rogersc7dd2952014-10-21 23:31:19 -0700117 if (argv != nullptr) {
Ian Rogers1e363f92013-11-13 15:58:24 -0800118 gCmdLine.reset(new std::string(argv[0]));
Ian Rogersc7dd2952014-10-21 23:31:19 -0700119 for (size_t i = 1; argv[i] != nullptr; ++i) {
Brian Carlstromfa42b442013-06-17 12:53:45 -0700120 gCmdLine->append(" ");
121 gCmdLine->append(argv[i]);
122 }
Ian Rogers1e363f92013-11-13 15:58:24 -0800123 gProgramInvocationName.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -0700124 const char* last_slash = strrchr(argv[0], '/');
Ian Rogersc7dd2952014-10-21 23:31:19 -0700125 gProgramInvocationShortName.reset(new std::string((last_slash != nullptr) ? last_slash + 1
Ian Rogers1e363f92013-11-13 15:58:24 -0800126 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -0700127 } else {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700128 // TODO: fall back to /proc/self/cmdline when argv is null on Linux.
Ian Rogers1e363f92013-11-13 15:58:24 -0800129 gCmdLine.reset(new std::string("<unset>"));
Elliott Hughes0d39c122012-06-06 16:41:17 -0700130 }
Elliott Hughes72395bf2012-04-24 13:45:26 -0700131
Julien Duraj1af0c4f2016-11-16 14:05:48 +0000132#ifdef ART_TARGET_ANDROID
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700133#define INIT_LOGGING_DEFAULT_LOGGER android::base::LogdLogger()
134#else
135#define INIT_LOGGING_DEFAULT_LOGGER android::base::StderrLogger
136#endif
David Sehrf57589f2016-10-17 10:09:33 -0700137 android::base::InitLogging(argv, INIT_LOGGING_DEFAULT_LOGGER,
138 std::move<AbortFunction>(abort_function));
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700139#undef INIT_LOGGING_DEFAULT_LOGGER
Ian Rogersc7dd2952014-10-21 23:31:19 -0700140}
141
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100142#ifdef ART_TARGET_ANDROID
Ian Rogersc7dd2952014-10-21 23:31:19 -0700143static 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 Gampe3fec9ac2016-09-13 10:47:28 -0700147static_assert(arraysize(kLogSeverityToAndroidLogPriority) == ::android::base::FATAL + 1,
Andreas Gampe575e78c2014-11-03 23:41:03 -0800148 "Mismatch in size of kLogSeverityToAndroidLogPriority and values in LogSeverity");
Ian Rogersc7dd2952014-10-21 23:31:19 -0700149#endif
150
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700151void LogHelper::LogLineLowStack(const char* file,
152 unsigned int line,
153 LogSeverity log_severity,
154 const char* message) {
Bilyan Borisovbb661c02016-04-04 16:27:32 +0100155#ifdef ART_TARGET_ANDROID
Vladimir Markob8f2f632015-01-02 14:23:26 +0000156 // Use android_writeLog() to avoid stack-based buffers used by android_printLog().
157 const char* tag = ProgramInvocationShortName();
Andreas Gampe7fe30232016-03-25 16:58:00 -0700158 int priority = kLogSeverityToAndroidLogPriority[static_cast<size_t>(log_severity)];
Vladimir Markob8f2f632015-01-02 14:23:26 +0000159 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 Gampe01f77432017-05-24 09:56:30 -0700164 buf_size = strlen(file) + 1 /* ':' */ + std::numeric_limits<decltype(line)>::max_digits10 +
Vladimir Markob8f2f632015-01-02 14:23:26 +0000165 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 Rogersf4d4da12014-11-11 16:10:33 -0800175#else
Andreas Gampe5fd66d02016-09-12 20:22:19 -0700176 static constexpr char kLogCharacters[] = { 'V', 'D', 'I', 'W', 'E', 'F', 'F' };
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700177 static_assert(
178 arraysize(kLogCharacters) == static_cast<size_t>(::android::base::FATAL) + 1,
179 "Wrong character array size");
Ian Rogersf4d4da12014-11-11 16:10:33 -0800180
181 const char* program_name = ProgramInvocationShortName();
Elliott Hughes06f08e42015-05-12 21:25:36 -0700182 TEMP_FAILURE_RETRY(write(STDERR_FILENO, program_name, strlen(program_name)));
183 TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1));
Andreas Gampe7fe30232016-03-25 16:58:00 -0700184 TEMP_FAILURE_RETRY(write(STDERR_FILENO, &kLogCharacters[static_cast<size_t>(log_severity)], 1));
Elliott Hughes06f08e42015-05-12 21:25:36 -0700185 TEMP_FAILURE_RETRY(write(STDERR_FILENO, " ", 1));
Ian Rogersf4d4da12014-11-11 16:10:33 -0800186 // TODO: pid and tid.
Elliott Hughes06f08e42015-05-12 21:25:36 -0700187 TEMP_FAILURE_RETRY(write(STDERR_FILENO, file, strlen(file)));
Ian Rogersf4d4da12014-11-11 16:10:33 -0800188 // TODO: line.
189 UNUSED(line);
Elliott Hughes06f08e42015-05-12 21:25:36 -0700190 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 Borisovbb661c02016-04-04 16:27:32 +0100193#endif // ART_TARGET_ANDROID
Ian Rogersf4d4da12014-11-11 16:10:33 -0800194}
195
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700196} // namespace art