1 /*
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 "base/mutex.h"
20 #include "runtime.h"
21 #include "thread.h"
22 #include "utils.h"
23
24 namespace art {
25
26 LogVerbosity gLogVerbosity;
27
28 unsigned int gAborting = 0;
29
30 static LogSeverity gMinimumLogSeverity = INFO;
31 static std::string* gCmdLine = NULL;
32 static std::string* gProgramInvocationName = NULL;
33 static std::string* gProgramInvocationShortName = NULL;
34
GetCmdLine()35 const char* GetCmdLine() {
36 return (gCmdLine != NULL) ? gCmdLine->c_str() : NULL;
37 }
38
ProgramInvocationName()39 const char* ProgramInvocationName() {
40 return (gProgramInvocationName != NULL) ? gProgramInvocationName->c_str() : "art";
41 }
42
ProgramInvocationShortName()43 const char* ProgramInvocationShortName() {
44 return (gProgramInvocationShortName != NULL) ? gProgramInvocationShortName->c_str() : "art";
45 }
46
47 // 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.
InitLogging(char * argv[])55 void InitLogging(char* argv[]) {
56 if (gCmdLine != NULL) {
57 return;
58 }
59 // TODO: Move this to a more obvious InitART...
60 Locks::Init();
61
62 // 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.
65 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>");
77 }
78 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]) {
90 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;
108 // liblog will even suppress FATAL if you say 's' for silent, but that's crazy!
109 case 's':
110 gMinimumLogSeverity = FATAL;
111 continue;
112 }
113 }
114 LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")";
115 }
116 }
117
LogMessageData(const char * file,int line,LogSeverity severity,int error)118 LogMessageData::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
~LogMessage()127 LogMessage::~LogMessage() {
128 if (data_->severity < gMinimumLogSeverity) {
129 return; // No need to format something we're not going to output.
130 }
131
132 // Finish constructing the message.
133 if (data_->error != -1) {
134 data_->buffer << ": " << strerror(data_->error);
135 }
136 std::string msg(data_->buffer.str());
137
138 // Do the actual logging with the lock held.
139 {
140 MutexLock mu(Thread::Current(), *Locks::logging_lock_);
141 if (msg.find('\n') == std::string::npos) {
142 LogLine(*data_, msg.c_str());
143 } 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';
149 LogLine(*data_, &msg[i]);
150 i = nl + 1;
151 }
152 }
153 }
154
155 // Abort if necessary.
156 if (data_->severity == FATAL) {
157 Runtime::Abort();
158 }
159 }
160
HexDump(const void * address,size_t byte_count,bool show_actual_addresses)161 HexDump::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
Dump(std::ostream & os) const165 void HexDump::Dump(std::ostream& os) const {
166 if (byte_count_ == 0) {
167 return;
168 }
169
170 if (address_ == NULL) {
171 os << "00000000:";
172 return;
173 }
174
175 static const char gHexDigit[] = "0123456789abcdef";
176 const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_);
177 char out[76]; /* exact fit */
178 unsigned int offset; /* offset to show while printing */
179
180 if (show_actual_addresses_) {
181 offset = reinterpret_cast<int>(addr);
182 } else {
183 offset = 0;
184 }
185 memset(out, ' ', sizeof(out)-1);
186 out[8] = ':';
187 out[sizeof(out)-1] = '\0';
188
189 size_t byte_count = byte_count_;
190 int gap = static_cast<int>(offset & 0x0f);
191 while (byte_count) {
192 unsigned int line_offset = offset & ~0x0f;
193
194 char* hex = out;
195 char* asc = out + 59;
196
197 for (int i = 0; i < 8; i++) {
198 *hex++ = gHexDigit[line_offset >> 28];
199 line_offset <<= 4;
200 }
201 hex++;
202 hex++;
203
204 int count = std::min(static_cast<int>(byte_count), 16 - gap);
205 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
214 int i;
215 for (i = gap ; i < count+gap; i++) {
216 *hex++ = gHexDigit[*addr >> 4];
217 *hex++ = gHexDigit[*addr & 0x0f];
218 hex++;
219 if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) {
220 *asc++ = *addr;
221 } else {
222 *asc++ = '.';
223 }
224 addr++;
225 }
226 for (; i < 16; i++) {
227 /* erase extra stuff; only happens on last line */
228 *hex++ = ' ';
229 *hex++ = ' ';
230 hex++;
231 *asc++ = ' ';
232 }
233
234 os << out;
235
236 gap = 0;
237 byte_count -= count;
238 offset += count;
239 }
240 }
241
operator <<(std::ostream & os,const HexDump & rhs)242 std::ostream& operator<<(std::ostream& os, const HexDump& rhs) {
243 rhs.Dump(os);
244 return os;
245 }
246
247 } // namespace art
248