blob: 5680ed0a3473413cfbcb422f730ddd717f39daf6 [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
Ian Rogers81d425b2012-09-27 16:03:43 -070019#include "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
Elliott Hughes72395bf2012-04-24 13:45:26 -070028static LogSeverity gMinimumLogSeverity = INFO;
Elliott Hughes0d39c122012-06-06 16:41:17 -070029static std::string* gCmdLine;
30static std::string* gProgramInvocationName;
31static std::string* gProgramInvocationShortName;
Elliott Hughes72395bf2012-04-24 13:45:26 -070032
Elliott Hughes0d39c122012-06-06 16:41:17 -070033const char* GetCmdLine() {
34 return (gCmdLine != NULL) ? gCmdLine->c_str() : NULL;
35}
36
37const char* ProgramInvocationName() {
38 return (gProgramInvocationName != NULL) ? gProgramInvocationName->c_str() : "art";
39}
40
41const char* ProgramInvocationShortName() {
42 return (gProgramInvocationShortName != NULL) ? gProgramInvocationShortName->c_str() : "art";
43}
44
Elliott Hughes72395bf2012-04-24 13:45:26 -070045// Configure logging based on ANDROID_LOG_TAGS environment variable.
46// We need to parse a string that looks like
47//
48// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
49//
50// The tag (or '*' for the global level) comes first, followed by a colon
51// and a letter indicating the minimum priority level we're expected to log.
52// This can be used to reveal or conceal logs with specific tags.
Elliott Hughes0d39c122012-06-06 16:41:17 -070053void InitLogging(char* argv[]) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070055 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070056
Elliott Hughes0d39c122012-06-06 16:41:17 -070057 // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this,
58 // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are
59 // commonly used.
60 gCmdLine = new std::string(argv[0]);
61 for (size_t i = 1; argv[i] != NULL; ++i) {
62 gCmdLine->append(" ");
63 gCmdLine->append(argv[i]);
64 }
65 gProgramInvocationName = new std::string(argv[0]);
66 const char* last_slash = strrchr(argv[0], '/');
67 gProgramInvocationShortName = new std::string((last_slash != NULL) ? last_slash + 1 : argv[0]);
68
Elliott Hughes72395bf2012-04-24 13:45:26 -070069 const char* tags = getenv("ANDROID_LOG_TAGS");
70 if (tags == NULL) {
71 return;
72 }
73
74 std::vector<std::string> specs;
75 Split(tags, ' ', specs);
76 for (size_t i = 0; i < specs.size(); ++i) {
77 // "tag-pattern:[vdiwefs]"
78 std::string spec(specs[i]);
79 if (spec.size() == 3 && StartsWith(spec, "*:")) {
80 switch (spec[2]) {
81 case 'v': gMinimumLogSeverity = VERBOSE; continue;
82 case 'd': gMinimumLogSeverity = DEBUG; continue;
83 case 'i': gMinimumLogSeverity = INFO; continue;
84 case 'w': gMinimumLogSeverity = WARNING; continue;
85 case 'e': gMinimumLogSeverity = ERROR; continue;
86 case 'f': gMinimumLogSeverity = FATAL; continue;
87 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
88 case 's': gMinimumLogSeverity = FATAL; continue;
89 }
90 }
91 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
92 }
93}
94
Elliott Hughes13f5a582011-09-06 13:39:14 -070095LogMessage::~LogMessage() {
Elliott Hughes72395bf2012-04-24 13:45:26 -070096 if (data_->severity < gMinimumLogSeverity) {
97 return; // No need to format something we're not going to output.
98 }
99
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700100 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700101 if (data_->error != -1) {
102 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -0700103 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700104 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700105
106 // Do the actual logging with the lock held.
107 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700108 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700109 if (msg.find('\n') == std::string::npos) {
110 LogLine(msg.c_str());
111 } else {
112 msg += '\n';
113 size_t i = 0;
114 while (i < msg.size()) {
115 size_t nl = msg.find('\n', i);
116 msg[nl] = '\0';
117 LogLine(&msg[i]);
118 i = nl + 1;
119 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700120 }
121 }
122
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700123 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700124 if (data_->severity == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700125 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700126 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700127
128 delete data_;
Elliott Hughes13f5a582011-09-06 13:39:14 -0700129}
130
131std::ostream& LogMessage::stream() {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700132 return data_->buffer;
Elliott Hughes13f5a582011-09-06 13:39:14 -0700133}
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700134
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700135HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses)
136 : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) {
137}
138
139void HexDump::Dump(std::ostream& os) const {
140 if (byte_count_ == 0) {
141 return;
142 }
143
Brian Carlstrom93235f72012-03-29 22:48:15 -0700144 if (address_ == NULL) {
145 os << "00000000:";
146 return;
147 }
148
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700149 static const char gHexDigit[] = "0123456789abcdef";
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700150 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
Elliott Hughes21f32d72011-11-09 17:44:13 -0800151 char out[76]; /* exact fit */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700152 unsigned int offset; /* offset to show while printing */
153
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700154 if (show_actual_addresses_) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700155 offset = reinterpret_cast<int>(addr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700156 } else {
157 offset = 0;
158 }
159 memset(out, ' ', sizeof(out)-1);
160 out[8] = ':';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700161 out[sizeof(out)-1] = '\0';
162
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700163 size_t byte_count = byte_count_;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700164 int gap = static_cast<int>(offset & 0x0f);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700165 while (byte_count) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700166 unsigned int line_offset = offset & ~0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700167
168 char* hex = out;
169 char* asc = out + 59;
170
Elliott Hughes398f64b2012-03-26 18:05:48 -0700171 for (int i = 0; i < 8; i++) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700172 *hex++ = gHexDigit[line_offset >> 28];
173 line_offset <<= 4;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700174 }
175 hex++;
176 hex++;
177
Elliott Hughes398f64b2012-03-26 18:05:48 -0700178 int count = std::min(static_cast<int>(byte_count), 16 - gap);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700179 CHECK_NE(count, 0);
180 CHECK_LE(count + gap, 16);
181
182 if (gap) {
183 /* only on first line */
184 hex += gap * 3;
185 asc += gap;
186 }
187
Elliott Hughes398f64b2012-03-26 18:05:48 -0700188 int i;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700189 for (i = gap ; i < count+gap; i++) {
190 *hex++ = gHexDigit[*addr >> 4];
191 *hex++ = gHexDigit[*addr & 0x0f];
192 hex++;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700193 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
194 *asc++ = *addr;
195 } else {
196 *asc++ = '.';
197 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700198 addr++;
199 }
Elliott Hughes398f64b2012-03-26 18:05:48 -0700200 for (; i < 16; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700201 /* erase extra stuff; only happens on last line */
202 *hex++ = ' ';
203 *hex++ = ' ';
204 hex++;
205 *asc++ = ' ';
206 }
207
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700208 os << out;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700209
210 gap = 0;
211 byte_count -= count;
212 offset += count;
213 }
214}
215
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700216std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
217 rhs.Dump(os);
218 return os;
219}
220
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700221} // namespace art