blob: a19580cbdec80f6e6ea0219a779621b6cd90f5e8 [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"
Elliott Hughes5fe594f2011-09-08 12:33:17 -070021#include "thread.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070022#include "utils.h"
23
Elliott Hughesf5a7a472011-10-07 14:31:02 -070024namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070025
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080026LogVerbosity gLogVerbosity;
27
Ian Rogersf08e4732013-04-09 09:45:49 -070028unsigned int gAborting = 0;
Brian Carlstrom81b88712012-11-05 19:21:30 -080029
Elliott Hughes72395bf2012-04-24 13:45:26 -070030static LogSeverity gMinimumLogSeverity = INFO;
Elliott Hughes0d39c122012-06-06 16:41:17 -070031static std::string* gCmdLine;
32static std::string* gProgramInvocationName;
33static std::string* gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070034
Elliott Hughes0d39c122012-06-06 16:41:17 -070035const char* GetCmdLine() {
36 return (gCmdLine != NULL) ? gCmdLine->c_str() : NULL;
37}
38
39const char* ProgramInvocationName() {
40 return (gProgramInvocationName != NULL) ? gProgramInvocationName->c_str() : "art";
41}
42
43const char* ProgramInvocationShortName() {
44 return (gProgramInvocationShortName != NULL) ? gProgramInvocationShortName->c_str() : "art";
45}
46
Elliott Hughes72395bf2012-04-24 13:45:26 -070047// Configure logging based on ANDROID_LOG_TAGS environment variable.
48// We need to parse a string that looks like
49//
50// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
51//
52// The tag (or '*' for the global level) comes first, followed by a colon
53// and a letter indicating the minimum priority level we're expected to log.
54// This can be used to reveal or conceal logs with specific tags.
Elliott Hughes0d39c122012-06-06 16:41:17 -070055void InitLogging(char* argv[]) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070057 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058
Elliott Hughes0d39c122012-06-06 16:41:17 -070059 // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this,
60 // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are
61 // commonly used.
62 gCmdLine = new std::string(argv[0]);
63 for (size_t i = 1; argv[i] != NULL; ++i) {
64 gCmdLine->append(" ");
65 gCmdLine->append(argv[i]);
66 }
67 gProgramInvocationName = new std::string(argv[0]);
68 const char* last_slash = strrchr(argv[0], '/');
69 gProgramInvocationShortName = new std::string((last_slash != NULL) ? last_slash + 1 : argv[0]);
70
Elliott Hughes72395bf2012-04-24 13:45:26 -070071 const char* tags = getenv("ANDROID_LOG_TAGS");
72 if (tags == NULL) {
73 return;
74 }
75
76 std::vector<std::string> specs;
77 Split(tags, ' ', specs);
78 for (size_t i = 0; i < specs.size(); ++i) {
79 // "tag-pattern:[vdiwefs]"
80 std::string spec(specs[i]);
81 if (spec.size() == 3 && StartsWith(spec, "*:")) {
82 switch (spec[2]) {
83 case 'v': gMinimumLogSeverity = VERBOSE; continue;
84 case 'd': gMinimumLogSeverity = DEBUG; continue;
85 case 'i': gMinimumLogSeverity = INFO; continue;
86 case 'w': gMinimumLogSeverity = WARNING; continue;
87 case 'e': gMinimumLogSeverity = ERROR; continue;
88 case 'f': gMinimumLogSeverity = FATAL; continue;
89 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
90 case 's': gMinimumLogSeverity = FATAL; continue;
91 }
92 }
93 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
94 }
95}
96
Brian Carlstromaf1b8922012-11-27 15:19:57 -080097LogMessageData::LogMessageData(const char* file, int line, LogSeverity severity, int error)
98 : file(file),
99 line_number(line),
100 severity(severity),
101 error(error) {
102 const char* last_slash = strrchr(file, '/');
103 file = (last_slash == NULL) ? file : last_slash + 1;
104}
105
Elliott Hughes13f5a582011-09-06 13:39:14 -0700106LogMessage::~LogMessage() {
Elliott Hughes72395bf2012-04-24 13:45:26 -0700107 if (data_->severity < gMinimumLogSeverity) {
108 return; // No need to format something we're not going to output.
109 }
110
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700111 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700112 if (data_->error != -1) {
113 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -0700114 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700115 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700116
117 // Do the actual logging with the lock held.
118 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700119 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700120 if (msg.find('\n') == std::string::npos) {
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800121 LogLine(*data_, msg.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700122 } else {
123 msg += '\n';
124 size_t i = 0;
125 while (i < msg.size()) {
126 size_t nl = msg.find('\n', i);
127 msg[nl] = '\0';
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800128 LogLine(*data_, &msg[i]);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700129 i = nl + 1;
130 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700131 }
132 }
133
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700134 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700135 if (data_->severity == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700136 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700137 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700138
139 delete data_;
Elliott Hughes13f5a582011-09-06 13:39:14 -0700140}
141
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700142HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses)
143 : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) {
144}
145
146void HexDump::Dump(std::ostream& os) const {
147 if (byte_count_ == 0) {
148 return;
149 }
150
Brian Carlstrom93235f72012-03-29 22:48:15 -0700151 if (address_ == NULL) {
152 os << "00000000:";
153 return;
154 }
155
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156 static const char gHexDigit[] = "0123456789abcdef";
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700157 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
Elliott Hughes21f32d72011-11-09 17:44:13 -0800158 char out[76]; /* exact fit */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700159 unsigned int offset; /* offset to show while printing */
160
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700161 if (show_actual_addresses_) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700162 offset = reinterpret_cast<int>(addr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700163 } else {
164 offset = 0;
165 }
166 memset(out, ' ', sizeof(out)-1);
167 out[8] = ':';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168 out[sizeof(out)-1] = '\0';
169
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700170 size_t byte_count = byte_count_;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700171 int gap = static_cast<int>(offset & 0x0f);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 while (byte_count) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700173 unsigned int line_offset = offset & ~0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174
175 char* hex = out;
176 char* asc = out + 59;
177
Elliott Hughes398f64b2012-03-26 18:05:48 -0700178 for (int i = 0; i < 8; i++) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700179 *hex++ = gHexDigit[line_offset >> 28];
180 line_offset <<= 4;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700181 }
182 hex++;
183 hex++;
184
Elliott Hughes398f64b2012-03-26 18:05:48 -0700185 int count = std::min(static_cast<int>(byte_count), 16 - gap);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700186 CHECK_NE(count, 0);
187 CHECK_LE(count + gap, 16);
188
189 if (gap) {
190 /* only on first line */
191 hex += gap * 3;
192 asc += gap;
193 }
194
Elliott Hughes398f64b2012-03-26 18:05:48 -0700195 int i;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700196 for (i = gap ; i < count+gap; i++) {
197 *hex++ = gHexDigit[*addr >> 4];
198 *hex++ = gHexDigit[*addr & 0x0f];
199 hex++;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700200 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
201 *asc++ = *addr;
202 } else {
203 *asc++ = '.';
204 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205 addr++;
206 }
Elliott Hughes398f64b2012-03-26 18:05:48 -0700207 for (; i < 16; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700208 /* erase extra stuff; only happens on last line */
209 *hex++ = ' ';
210 *hex++ = ' ';
211 hex++;
212 *asc++ = ' ';
213 }
214
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700215 os << out;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700216
217 gap = 0;
218 byte_count -= count;
219 offset += count;
220 }
221}
222
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700223std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
224 rhs.Dump(os);
225 return os;
226}
227
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700228} // namespace art