blob: 15554ac811e569f20a2cf7957eb45903ef05b52c [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
Elliott Hughes76b61672012-12-12 17:47:30 -080019#include "base/mutex.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070020#include "runtime.h"
Brian Carlstroma3d27182013-11-05 23:22:27 -080021#include "thread-inl.h"
Ian Rogers1e363f92013-11-13 15:58:24 -080022#include "UniquePtr.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070023#include "utils.h"
24
Elliott Hughesf5a7a472011-10-07 14:31:02 -070025namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070026
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080027LogVerbosity gLogVerbosity;
28
Ian Rogersf08e4732013-04-09 09:45:49 -070029unsigned int gAborting = 0;
Brian Carlstrom81b88712012-11-05 19:21:30 -080030
Elliott Hughes72395bf2012-04-24 13:45:26 -070031static LogSeverity gMinimumLogSeverity = INFO;
Ian Rogers1e363f92013-11-13 15:58:24 -080032static UniquePtr<std::string> gCmdLine;
33static UniquePtr<std::string> gProgramInvocationName;
34static UniquePtr<std::string> gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070035
Elliott Hughes0d39c122012-06-06 16:41:17 -070036const char* GetCmdLine() {
Ian Rogers1e363f92013-11-13 15:58:24 -080037 return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
Elliott Hughes0d39c122012-06-06 16:41:17 -070038}
39
40const char* ProgramInvocationName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080041 return (gProgramInvocationName.get() != nullptr) ? gProgramInvocationName->c_str() : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070042}
43
44const char* ProgramInvocationShortName() {
Ian Rogers1e363f92013-11-13 15:58:24 -080045 return (gProgramInvocationShortName.get() != nullptr) ? gProgramInvocationShortName->c_str()
46 : "art";
Elliott Hughes0d39c122012-06-06 16:41:17 -070047}
48
Elliott Hughes72395bf2012-04-24 13:45:26 -070049// Configure logging based on ANDROID_LOG_TAGS environment variable.
50// We need to parse a string that looks like
51//
52// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
53//
54// The tag (or '*' for the global level) comes first, followed by a colon
55// and a letter indicating the minimum priority level we're expected to log.
56// This can be used to reveal or conceal logs with specific tags.
Elliott Hughes0d39c122012-06-06 16:41:17 -070057void InitLogging(char* argv[]) {
Ian Rogers1e363f92013-11-13 15:58:24 -080058 if (gCmdLine.get() != nullptr) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070059 return;
60 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070062 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063
Elliott Hughes0d39c122012-06-06 16:41:17 -070064 // 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.
Brian Carlstromfa42b442013-06-17 12:53:45 -070067 if (argv != NULL) {
Ian Rogers1e363f92013-11-13 15:58:24 -080068 gCmdLine.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070069 for (size_t i = 1; argv[i] != NULL; ++i) {
70 gCmdLine->append(" ");
71 gCmdLine->append(argv[i]);
72 }
Ian Rogers1e363f92013-11-13 15:58:24 -080073 gProgramInvocationName.reset(new std::string(argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070074 const char* last_slash = strrchr(argv[0], '/');
Ian Rogers1e363f92013-11-13 15:58:24 -080075 gProgramInvocationShortName.reset(new std::string((last_slash != NULL) ? last_slash + 1
76 : argv[0]));
Brian Carlstromfa42b442013-06-17 12:53:45 -070077 } else {
78 // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux
Ian Rogers1e363f92013-11-13 15:58:24 -080079 gCmdLine.reset(new std::string("<unset>"));
Elliott Hughes0d39c122012-06-06 16:41:17 -070080 }
Elliott Hughes72395bf2012-04-24 13:45:26 -070081 const char* tags = getenv("ANDROID_LOG_TAGS");
82 if (tags == NULL) {
83 return;
84 }
85
86 std::vector<std::string> specs;
87 Split(tags, ' ', specs);
88 for (size_t i = 0; i < specs.size(); ++i) {
89 // "tag-pattern:[vdiwefs]"
90 std::string spec(specs[i]);
91 if (spec.size() == 3 && StartsWith(spec, "*:")) {
92 switch (spec[2]) {
Brian Carlstromf69863b2013-07-17 21:53:13 -070093 case 'v':
94 gMinimumLogSeverity = VERBOSE;
95 continue;
96 case 'd':
97 gMinimumLogSeverity = DEBUG;
98 continue;
99 case 'i':
100 gMinimumLogSeverity = INFO;
101 continue;
102 case 'w':
103 gMinimumLogSeverity = WARNING;
104 continue;
105 case 'e':
106 gMinimumLogSeverity = ERROR;
107 continue;
108 case 'f':
109 gMinimumLogSeverity = FATAL;
110 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700111 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
Brian Carlstromf69863b2013-07-17 21:53:13 -0700112 case 's':
113 gMinimumLogSeverity = FATAL;
114 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700115 }
116 }
117 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
118 }
119}
120
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800121LogMessageData::LogMessageData(const char* file, int line, LogSeverity severity, int error)
122 : file(file),
123 line_number(line),
124 severity(severity),
125 error(error) {
126 const char* last_slash = strrchr(file, '/');
127 file = (last_slash == NULL) ? file : last_slash + 1;
128}
129
Elliott Hughes13f5a582011-09-06 13:39:14 -0700130LogMessage::~LogMessage() {
Elliott Hughes72395bf2012-04-24 13:45:26 -0700131 if (data_->severity < gMinimumLogSeverity) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700132 return; // No need to format something we're not going to output.
Elliott Hughes72395bf2012-04-24 13:45:26 -0700133 }
134
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700135 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700136 if (data_->error != -1) {
137 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -0700138 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700139 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700140
141 // Do the actual logging with the lock held.
142 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700143 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700144 if (msg.find('\n') == std::string::npos) {
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800145 LogLine(*data_, msg.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700146 } else {
147 msg += '\n';
148 size_t i = 0;
149 while (i < msg.size()) {
150 size_t nl = msg.find('\n', i);
151 msg[nl] = '\0';
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800152 LogLine(*data_, &msg[i]);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700153 i = nl + 1;
154 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700155 }
156 }
157
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700158 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700159 if (data_->severity == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700160 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700161 }
162}
163
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700164HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses)
165 : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) {
166}
167
168void HexDump::Dump(std::ostream& os) const {
169 if (byte_count_ == 0) {
170 return;
171 }
172
Brian Carlstrom93235f72012-03-29 22:48:15 -0700173 if (address_ == NULL) {
174 os << "00000000:";
175 return;
176 }
177
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700178 static const char gHexDigit[] = "0123456789abcdef";
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700179 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
Ian Rogersb400da02014-01-23 08:28:28 -0800180 // 01234560: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 0123456789abcdef
181 char out[(kBitsPerWord / 4) + /* offset */
182 1 + /* colon */
183 (16 * 3) + /* 16 hex digits and space */
184 2 + /* white space */
185 16 + /* 16 characters*/
186 1 /* \0 */ ];
187 size_t offset; /* offset to show while printing */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700188
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700189 if (show_actual_addresses_) {
Ian Rogersb400da02014-01-23 08:28:28 -0800190 offset = reinterpret_cast<size_t>(addr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191 } else {
192 offset = 0;
193 }
194 memset(out, ' ', sizeof(out)-1);
Ian Rogersb400da02014-01-23 08:28:28 -0800195 out[kBitsPerWord / 4] = ':';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196 out[sizeof(out)-1] = '\0';
197
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700198 size_t byte_count = byte_count_;
Ian Rogersb400da02014-01-23 08:28:28 -0800199 size_t gap = offset & 0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200 while (byte_count) {
Ian Rogersb400da02014-01-23 08:28:28 -0800201 size_t line_offset = offset & ~0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202
203 char* hex = out;
Ian Rogersb400da02014-01-23 08:28:28 -0800204 char* asc = out + (kBitsPerWord / 4) + /* offset */ 1 + /* colon */
205 (16 * 3) + /* 16 hex digits and space */ 2 /* white space */;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700206
Ian Rogersb400da02014-01-23 08:28:28 -0800207 for (int i = 0; i < (kBitsPerWord / 4); i++) {
208 *hex++ = gHexDigit[line_offset >> (kBitsPerWord - 4)];
Elliott Hughes24edeb52012-06-18 15:29:46 -0700209 line_offset <<= 4;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210 }
211 hex++;
212 hex++;
213
Ian Rogersb400da02014-01-23 08:28:28 -0800214 size_t count = std::min(byte_count, 16 - gap);
215 CHECK_NE(count, 0U);
216 CHECK_LE(count + gap, 16U);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700217
218 if (gap) {
219 /* only on first line */
220 hex += gap * 3;
221 asc += gap;
222 }
223
Ian Rogersb400da02014-01-23 08:28:28 -0800224 size_t i;
225 for (i = gap ; i < count + gap; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700226 *hex++ = gHexDigit[*addr >> 4];
227 *hex++ = gHexDigit[*addr & 0x0f];
228 hex++;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700229 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
230 *asc++ = *addr;
231 } else {
232 *asc++ = '.';
233 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700234 addr++;
235 }
Elliott Hughes398f64b2012-03-26 18:05:48 -0700236 for (; i < 16; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700237 /* erase extra stuff; only happens on last line */
238 *hex++ = ' ';
239 *hex++ = ' ';
240 hex++;
241 *asc++ = ' ';
242 }
243
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700244 os << out;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700245
246 gap = 0;
247 byte_count -= count;
248 offset += count;
249 }
250}
251
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700252std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
253 rhs.Dump(os);
254 return os;
255}
256
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700257} // namespace art