Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 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 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 19 | #include "mutex.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 20 | #include "runtime.h" |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 21 | #include "thread.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 22 | #include "utils.h" |
| 23 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 24 | namespace art { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 26 | LogVerbosity gLogVerbosity; |
| 27 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 28 | static LogSeverity gMinimumLogSeverity = INFO; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 29 | static std::string* gCmdLine; |
| 30 | static std::string* gProgramInvocationName; |
| 31 | static std::string* gProgramInvocationShortName; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 33 | const char* GetCmdLine() { |
| 34 | return (gCmdLine != NULL) ? gCmdLine->c_str() : NULL; |
| 35 | } |
| 36 | |
| 37 | const char* ProgramInvocationName() { |
| 38 | return (gProgramInvocationName != NULL) ? gProgramInvocationName->c_str() : "art"; |
| 39 | } |
| 40 | |
| 41 | const char* ProgramInvocationShortName() { |
| 42 | return (gProgramInvocationShortName != NULL) ? gProgramInvocationShortName->c_str() : "art"; |
| 43 | } |
| 44 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 45 | // 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 Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 53 | void InitLogging(char* argv[]) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 54 | // TODO: Move this to a more obvious InitART... |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 55 | Locks::Init(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 56 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 57 | // 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 Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 69 | 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 Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 95 | LogMessage::~LogMessage() { |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 96 | if (data_->severity < gMinimumLogSeverity) { |
| 97 | return; // No need to format something we're not going to output. |
| 98 | } |
| 99 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 100 | // Finish constructing the message. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 101 | if (data_->error != -1) { |
| 102 | data_->buffer << ": " << strerror(data_->error); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 103 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 104 | std::string msg(data_->buffer.str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 105 | |
| 106 | // Do the actual logging with the lock held. |
| 107 | { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 108 | MutexLock mu(*Locks::logging_lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 109 | 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 Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 123 | // Abort if necessary. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 124 | if (data_->severity == FATAL) { |
Elliott Hughes | 8593fdb | 2012-04-21 20:53:44 -0700 | [diff] [blame] | 125 | Runtime::Abort(); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 126 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 127 | |
| 128 | delete data_; |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | std::ostream& LogMessage::stream() { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 132 | return data_->buffer; |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 133 | } |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 134 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 135 | HexDump::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 | |
| 139 | void HexDump::Dump(std::ostream& os) const { |
| 140 | if (byte_count_ == 0) { |
| 141 | return; |
| 142 | } |
| 143 | |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 144 | if (address_ == NULL) { |
| 145 | os << "00000000:"; |
| 146 | return; |
| 147 | } |
| 148 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 149 | static const char gHexDigit[] = "0123456789abcdef"; |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 150 | const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 151 | char out[76]; /* exact fit */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 152 | unsigned int offset; /* offset to show while printing */ |
| 153 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 154 | if (show_actual_addresses_) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 155 | offset = reinterpret_cast<int>(addr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 156 | } else { |
| 157 | offset = 0; |
| 158 | } |
| 159 | memset(out, ' ', sizeof(out)-1); |
| 160 | out[8] = ':'; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 161 | out[sizeof(out)-1] = '\0'; |
| 162 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 163 | size_t byte_count = byte_count_; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 164 | int gap = static_cast<int>(offset & 0x0f); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 165 | while (byte_count) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 166 | unsigned int line_offset = offset & ~0x0f; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 167 | |
| 168 | char* hex = out; |
| 169 | char* asc = out + 59; |
| 170 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 171 | for (int i = 0; i < 8; i++) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 172 | *hex++ = gHexDigit[line_offset >> 28]; |
| 173 | line_offset <<= 4; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 174 | } |
| 175 | hex++; |
| 176 | hex++; |
| 177 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 178 | int count = std::min(static_cast<int>(byte_count), 16 - gap); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 179 | 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 Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 188 | int i; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 189 | for (i = gap ; i < count+gap; i++) { |
| 190 | *hex++ = gHexDigit[*addr >> 4]; |
| 191 | *hex++ = gHexDigit[*addr & 0x0f]; |
| 192 | hex++; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 193 | if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) { |
| 194 | *asc++ = *addr; |
| 195 | } else { |
| 196 | *asc++ = '.'; |
| 197 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 198 | addr++; |
| 199 | } |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 200 | for (; i < 16; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 201 | /* erase extra stuff; only happens on last line */ |
| 202 | *hex++ = ' '; |
| 203 | *hex++ = ' '; |
| 204 | hex++; |
| 205 | *asc++ = ' '; |
| 206 | } |
| 207 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 208 | os << out; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 209 | |
| 210 | gap = 0; |
| 211 | byte_count -= count; |
| 212 | offset += count; |
| 213 | } |
| 214 | } |
| 215 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 216 | std::ostream& operator<<(std::ostream& os, const HexDump& rhs) { |
| 217 | rhs.Dump(os); |
| 218 | return os; |
| 219 | } |
| 220 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 221 | } // namespace art |