blob: 86c8bdf7c5af180f640f584b51629934b9cb41c2 [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
19#include "runtime.h"
Elliott Hughes5fe594f2011-09-08 12:33:17 -070020#include "thread.h"
Elliott Hughes13f5a582011-09-06 13:39:14 -070021#include "utils.h"
22
Elliott Hughesf5a7a472011-10-07 14:31:02 -070023namespace art {
Elliott Hughes5fe594f2011-09-08 12:33:17 -070024
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -080025LogVerbosity gLogVerbosity;
26
Elliott Hughes72395bf2012-04-24 13:45:26 -070027static LogSeverity gMinimumLogSeverity = INFO;
Elliott Hughes0d39c122012-06-06 16:41:17 -070028static std::string* gCmdLine;
29static std::string* gProgramInvocationName;
30static std::string* gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070031
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080032static Mutex& GetLoggingLock() {
33 static Mutex logging_lock("LogMessage lock");
34 return logging_lock;
Elliott Hughes5fe594f2011-09-08 12:33:17 -070035}
36
Elliott Hughes0d39c122012-06-06 16:41:17 -070037const char* GetCmdLine() {
38 return (gCmdLine != NULL) ? gCmdLine->c_str() : NULL;
39}
40
41const char* ProgramInvocationName() {
42 return (gProgramInvocationName != NULL) ? gProgramInvocationName->c_str() : "art";
43}
44
45const char* ProgramInvocationShortName() {
46 return (gProgramInvocationShortName != NULL) ? gProgramInvocationShortName->c_str() : "art";
47}
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[]) {
58 // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this,
59 // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are
60 // commonly used.
61 gCmdLine = new std::string(argv[0]);
62 for (size_t i = 1; argv[i] != NULL; ++i) {
63 gCmdLine->append(" ");
64 gCmdLine->append(argv[i]);
65 }
66 gProgramInvocationName = new std::string(argv[0]);
67 const char* last_slash = strrchr(argv[0], '/');
68 gProgramInvocationShortName = new std::string((last_slash != NULL) ? last_slash + 1 : argv[0]);
69
Elliott Hughes72395bf2012-04-24 13:45:26 -070070 const char* tags = getenv("ANDROID_LOG_TAGS");
71 if (tags == NULL) {
72 return;
73 }
74
75 std::vector<std::string> specs;
76 Split(tags, ' ', specs);
77 for (size_t i = 0; i < specs.size(); ++i) {
78 // "tag-pattern:[vdiwefs]"
79 std::string spec(specs[i]);
80 if (spec.size() == 3 && StartsWith(spec, "*:")) {
81 switch (spec[2]) {
82 case 'v': gMinimumLogSeverity = VERBOSE; continue;
83 case 'd': gMinimumLogSeverity = DEBUG; continue;
84 case 'i': gMinimumLogSeverity = INFO; continue;
85 case 'w': gMinimumLogSeverity = WARNING; continue;
86 case 'e': gMinimumLogSeverity = ERROR; continue;
87 case 'f': gMinimumLogSeverity = FATAL; continue;
88 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
89 case 's': gMinimumLogSeverity = FATAL; continue;
90 }
91 }
92 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
93 }
94}
95
Elliott Hughes13f5a582011-09-06 13:39:14 -070096LogMessage::~LogMessage() {
Elliott Hughes72395bf2012-04-24 13:45:26 -070097 if (data_->severity < gMinimumLogSeverity) {
98 return; // No need to format something we're not going to output.
99 }
100
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700101 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700102 if (data_->error != -1) {
103 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -0700104 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700105 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700106
107 // Do the actual logging with the lock held.
108 {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800109 MutexLock mu(GetLoggingLock());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700110 if (msg.find('\n') == std::string::npos) {
111 LogLine(msg.c_str());
112 } else {
113 msg += '\n';
114 size_t i = 0;
115 while (i < msg.size()) {
116 size_t nl = msg.find('\n', i);
117 msg[nl] = '\0';
118 LogLine(&msg[i]);
119 i = nl + 1;
120 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700121 }
122 }
123
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700124 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700125 if (data_->severity == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700126 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700127 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700128
129 delete data_;
Elliott Hughes13f5a582011-09-06 13:39:14 -0700130}
131
132std::ostream& LogMessage::stream() {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700133 return data_->buffer;
Elliott Hughes13f5a582011-09-06 13:39:14 -0700134}
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700135
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700136HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses)
137 : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) {
138}
139
140void HexDump::Dump(std::ostream& os) const {
141 if (byte_count_ == 0) {
142 return;
143 }
144
Brian Carlstrom93235f72012-03-29 22:48:15 -0700145 if (address_ == NULL) {
146 os << "00000000:";
147 return;
148 }
149
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700150 static const char gHexDigit[] = "0123456789abcdef";
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700151 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
Elliott Hughes21f32d72011-11-09 17:44:13 -0800152 char out[76]; /* exact fit */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700153 unsigned int offset; /* offset to show while printing */
154
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700155 if (show_actual_addresses_) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700156 offset = reinterpret_cast<int>(addr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700157 } else {
158 offset = 0;
159 }
160 memset(out, ' ', sizeof(out)-1);
161 out[8] = ':';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700162 out[sizeof(out)-1] = '\0';
163
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700164 size_t byte_count = byte_count_;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700165 int gap = static_cast<int>(offset & 0x0f);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700166 while (byte_count) {
167 unsigned int lineOffset = offset & ~0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700168
169 char* hex = out;
170 char* asc = out + 59;
171
Elliott Hughes398f64b2012-03-26 18:05:48 -0700172 for (int i = 0; i < 8; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700173 *hex++ = gHexDigit[lineOffset >> 28];
174 lineOffset <<= 4;
175 }
176 hex++;
177 hex++;
178
Elliott Hughes398f64b2012-03-26 18:05:48 -0700179 int count = std::min(static_cast<int>(byte_count), 16 - gap);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700180 CHECK_NE(count, 0);
181 CHECK_LE(count + gap, 16);
182
183 if (gap) {
184 /* only on first line */
185 hex += gap * 3;
186 asc += gap;
187 }
188
Elliott Hughes398f64b2012-03-26 18:05:48 -0700189 int i;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700190 for (i = gap ; i < count+gap; i++) {
191 *hex++ = gHexDigit[*addr >> 4];
192 *hex++ = gHexDigit[*addr & 0x0f];
193 hex++;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700194 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
195 *asc++ = *addr;
196 } else {
197 *asc++ = '.';
198 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700199 addr++;
200 }
Elliott Hughes398f64b2012-03-26 18:05:48 -0700201 for (; i < 16; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700202 /* erase extra stuff; only happens on last line */
203 *hex++ = ' ';
204 *hex++ = ' ';
205 hex++;
206 *asc++ = ' ';
207 }
208
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700209 os << out;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700210
211 gap = 0;
212 byte_count -= count;
213 offset += count;
214 }
215}
216
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700217std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
218 rhs.Dump(os);
219 return os;
220}
221
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700222} // namespace art