blob: 6d0452bcb33edfc67efe78d753e5d2e28d741915 [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
Brian Carlstrom81b88712012-11-05 19:21:30 -080028bool gAborting = false;
29
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
142std::ostream& LogMessage::stream() {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700143 return data_->buffer;
Elliott Hughes13f5a582011-09-06 13:39:14 -0700144}
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700145
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700146HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses)
147 : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) {
148}
149
150void HexDump::Dump(std::ostream& os) const {
151 if (byte_count_ == 0) {
152 return;
153 }
154
Brian Carlstrom93235f72012-03-29 22:48:15 -0700155 if (address_ == NULL) {
156 os << "00000000:";
157 return;
158 }
159
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700160 static const char gHexDigit[] = "0123456789abcdef";
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700161 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
Elliott Hughes21f32d72011-11-09 17:44:13 -0800162 char out[76]; /* exact fit */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700163 unsigned int offset; /* offset to show while printing */
164
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700165 if (show_actual_addresses_) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700166 offset = reinterpret_cast<int>(addr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167 } else {
168 offset = 0;
169 }
170 memset(out, ' ', sizeof(out)-1);
171 out[8] = ':';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700172 out[sizeof(out)-1] = '\0';
173
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700174 size_t byte_count = byte_count_;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700175 int gap = static_cast<int>(offset & 0x0f);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700176 while (byte_count) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700177 unsigned int line_offset = offset & ~0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700178
179 char* hex = out;
180 char* asc = out + 59;
181
Elliott Hughes398f64b2012-03-26 18:05:48 -0700182 for (int i = 0; i < 8; i++) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700183 *hex++ = gHexDigit[line_offset >> 28];
184 line_offset <<= 4;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700185 }
186 hex++;
187 hex++;
188
Elliott Hughes398f64b2012-03-26 18:05:48 -0700189 int count = std::min(static_cast<int>(byte_count), 16 - gap);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190 CHECK_NE(count, 0);
191 CHECK_LE(count + gap, 16);
192
193 if (gap) {
194 /* only on first line */
195 hex += gap * 3;
196 asc += gap;
197 }
198
Elliott Hughes398f64b2012-03-26 18:05:48 -0700199 int i;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200 for (i = gap ; i < count+gap; i++) {
201 *hex++ = gHexDigit[*addr >> 4];
202 *hex++ = gHexDigit[*addr & 0x0f];
203 hex++;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700204 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
205 *asc++ = *addr;
206 } else {
207 *asc++ = '.';
208 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209 addr++;
210 }
Elliott Hughes398f64b2012-03-26 18:05:48 -0700211 for (; i < 16; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700212 /* erase extra stuff; only happens on last line */
213 *hex++ = ' ';
214 *hex++ = ' ';
215 hex++;
216 *asc++ = ' ';
217 }
218
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700219 os << out;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700220
221 gap = 0;
222 byte_count -= count;
223 offset += count;
224 }
225}
226
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700227std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
228 rhs.Dump(os);
229 return os;
230}
231
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700232} // namespace art