blob: 7d54bafd9caddb3afc20fb7e3b574c2969dbf43a [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;
Brian Carlstromfa42b442013-06-17 12:53:45 -070031static std::string* gCmdLine = NULL;
32static std::string* gProgramInvocationName = NULL;
33static std::string* gProgramInvocationShortName = NULL;
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[]) {
Brian Carlstromfa42b442013-06-17 12:53:45 -070056 if (gCmdLine != NULL) {
57 return;
58 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070059 // TODO: Move this to a more obvious InitART...
Ian Rogersb726dcb2012-09-05 08:57:23 -070060 Locks::Init();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070061
Elliott Hughes0d39c122012-06-06 16:41:17 -070062 // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this,
63 // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are
64 // commonly used.
Brian Carlstromfa42b442013-06-17 12:53:45 -070065 if (argv != NULL) {
66 gCmdLine = new std::string(argv[0]);
67 for (size_t i = 1; argv[i] != NULL; ++i) {
68 gCmdLine->append(" ");
69 gCmdLine->append(argv[i]);
70 }
71 gProgramInvocationName = new std::string(argv[0]);
72 const char* last_slash = strrchr(argv[0], '/');
73 gProgramInvocationShortName = new std::string((last_slash != NULL) ? last_slash + 1 : argv[0]);
74 } else {
75 // TODO: fall back to /proc/self/cmdline when argv is NULL on Linux
76 gCmdLine = new std::string("<unset>");
Elliott Hughes0d39c122012-06-06 16:41:17 -070077 }
Elliott Hughes72395bf2012-04-24 13:45:26 -070078 const char* tags = getenv("ANDROID_LOG_TAGS");
79 if (tags == NULL) {
80 return;
81 }
82
83 std::vector<std::string> specs;
84 Split(tags, ' ', specs);
85 for (size_t i = 0; i < specs.size(); ++i) {
86 // "tag-pattern:[vdiwefs]"
87 std::string spec(specs[i]);
88 if (spec.size() == 3 && StartsWith(spec, "*:")) {
89 switch (spec[2]) {
Brian Carlstromf69863b2013-07-17 21:53:13 -070090 case 'v':
91 gMinimumLogSeverity = VERBOSE;
92 continue;
93 case 'd':
94 gMinimumLogSeverity = DEBUG;
95 continue;
96 case 'i':
97 gMinimumLogSeverity = INFO;
98 continue;
99 case 'w':
100 gMinimumLogSeverity = WARNING;
101 continue;
102 case 'e':
103 gMinimumLogSeverity = ERROR;
104 continue;
105 case 'f':
106 gMinimumLogSeverity = FATAL;
107 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700108 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
Brian Carlstromf69863b2013-07-17 21:53:13 -0700109 case 's':
110 gMinimumLogSeverity = FATAL;
111 continue;
Elliott Hughes72395bf2012-04-24 13:45:26 -0700112 }
113 }
114 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
115 }
116}
117
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800118LogMessageData::LogMessageData(const char* file, int line, LogSeverity severity, int error)
119 : file(file),
120 line_number(line),
121 severity(severity),
122 error(error) {
123 const char* last_slash = strrchr(file, '/');
124 file = (last_slash == NULL) ? file : last_slash + 1;
125}
126
Elliott Hughes13f5a582011-09-06 13:39:14 -0700127LogMessage::~LogMessage() {
Elliott Hughes72395bf2012-04-24 13:45:26 -0700128 if (data_->severity < gMinimumLogSeverity) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700129 return; // No need to format something we're not going to output.
Elliott Hughes72395bf2012-04-24 13:45:26 -0700130 }
131
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700132 // Finish constructing the message.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700133 if (data_->error != -1) {
134 data_->buffer << ": " << strerror(data_->error);
Elliott Hughes13f5a582011-09-06 13:39:14 -0700135 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700136 std::string msg(data_->buffer.str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700137
138 // Do the actual logging with the lock held.
139 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700140 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700141 if (msg.find('\n') == std::string::npos) {
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800142 LogLine(*data_, msg.c_str());
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700143 } else {
144 msg += '\n';
145 size_t i = 0;
146 while (i < msg.size()) {
147 size_t nl = msg.find('\n', i);
148 msg[nl] = '\0';
Brian Carlstromaf1b8922012-11-27 15:19:57 -0800149 LogLine(*data_, &msg[i]);
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700150 i = nl + 1;
151 }
Elliott Hughes13f5a582011-09-06 13:39:14 -0700152 }
153 }
154
Elliott Hughes5fe594f2011-09-08 12:33:17 -0700155 // Abort if necessary.
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700156 if (data_->severity == FATAL) {
Elliott Hughes8593fdb2012-04-21 20:53:44 -0700157 Runtime::Abort();
Elliott Hughes13f5a582011-09-06 13:39:14 -0700158 }
159}
160
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700161HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses)
162 : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) {
163}
164
165void HexDump::Dump(std::ostream& os) const {
166 if (byte_count_ == 0) {
167 return;
168 }
169
Brian Carlstrom93235f72012-03-29 22:48:15 -0700170 if (address_ == NULL) {
171 os << "00000000:";
172 return;
173 }
174
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700175 static const char gHexDigit[] = "0123456789abcdef";
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700176 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
Elliott Hughes21f32d72011-11-09 17:44:13 -0800177 char out[76]; /* exact fit */
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700178 unsigned int offset; /* offset to show while printing */
179
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700180 if (show_actual_addresses_) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700181 offset = reinterpret_cast<int>(addr);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700182 } else {
183 offset = 0;
184 }
185 memset(out, ' ', sizeof(out)-1);
186 out[8] = ':';
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700187 out[sizeof(out)-1] = '\0';
188
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700189 size_t byte_count = byte_count_;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700190 int gap = static_cast<int>(offset & 0x0f);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700191 while (byte_count) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700192 unsigned int line_offset = offset & ~0x0f;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700193
194 char* hex = out;
195 char* asc = out + 59;
196
Elliott Hughes398f64b2012-03-26 18:05:48 -0700197 for (int i = 0; i < 8; i++) {
Elliott Hughes24edeb52012-06-18 15:29:46 -0700198 *hex++ = gHexDigit[line_offset >> 28];
199 line_offset <<= 4;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700200 }
201 hex++;
202 hex++;
203
Elliott Hughes398f64b2012-03-26 18:05:48 -0700204 int count = std::min(static_cast<int>(byte_count), 16 - gap);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700205 CHECK_NE(count, 0);
206 CHECK_LE(count + gap, 16);
207
208 if (gap) {
209 /* only on first line */
210 hex += gap * 3;
211 asc += gap;
212 }
213
Elliott Hughes398f64b2012-03-26 18:05:48 -0700214 int i;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700215 for (i = gap ; i < count+gap; i++) {
216 *hex++ = gHexDigit[*addr >> 4];
217 *hex++ = gHexDigit[*addr & 0x0f];
218 hex++;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700219 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
220 *asc++ = *addr;
221 } else {
222 *asc++ = '.';
223 }
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700224 addr++;
225 }
Elliott Hughes398f64b2012-03-26 18:05:48 -0700226 for (; i < 16; i++) {
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700227 /* erase extra stuff; only happens on last line */
228 *hex++ = ' ';
229 *hex++ = ' ';
230 hex++;
231 *asc++ = ' ';
232 }
233
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700234 os << out;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700235
236 gap = 0;
237 byte_count -= count;
238 offset += count;
239 }
240}
241
Elliott Hughesbfbf0e22012-03-29 18:09:19 -0700242std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
243 rhs.Dump(os);
244 return os;
245}
246
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700247} // namespace art