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 | |
| 19 | #include "runtime.h" |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 20 | #include "thread.h" |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 21 | #include "utils.h" |
| 22 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 23 | namespace art { |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 25 | LogVerbosity gLogVerbosity; |
| 26 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 27 | static LogSeverity gMinimumLogSeverity = INFO; |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 28 | static std::string* gCmdLine; |
| 29 | static std::string* gProgramInvocationName; |
| 30 | static std::string* gProgramInvocationShortName; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 32 | const char* GetCmdLine() { |
| 33 | return (gCmdLine != NULL) ? gCmdLine->c_str() : NULL; |
| 34 | } |
| 35 | |
| 36 | const char* ProgramInvocationName() { |
| 37 | return (gProgramInvocationName != NULL) ? gProgramInvocationName->c_str() : "art"; |
| 38 | } |
| 39 | |
| 40 | const char* ProgramInvocationShortName() { |
| 41 | return (gProgramInvocationShortName != NULL) ? gProgramInvocationShortName->c_str() : "art"; |
| 42 | } |
| 43 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 44 | // Configure logging based on ANDROID_LOG_TAGS environment variable. |
| 45 | // We need to parse a string that looks like |
| 46 | // |
| 47 | // *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i |
| 48 | // |
| 49 | // The tag (or '*' for the global level) comes first, followed by a colon |
| 50 | // and a letter indicating the minimum priority level we're expected to log. |
| 51 | // This can be used to reveal or conceal logs with specific tags. |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 52 | void InitLogging(char* argv[]) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 53 | // TODO: Move this to a more obvious InitART... |
| 54 | GlobalSynchronization::Init(); |
| 55 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 56 | // Stash the command line for later use. We can use /proc/self/cmdline on Linux to recover this, |
| 57 | // but we don't have that luxury on the Mac, and there are a couple of argv[0] variants that are |
| 58 | // commonly used. |
| 59 | gCmdLine = new std::string(argv[0]); |
| 60 | for (size_t i = 1; argv[i] != NULL; ++i) { |
| 61 | gCmdLine->append(" "); |
| 62 | gCmdLine->append(argv[i]); |
| 63 | } |
| 64 | gProgramInvocationName = new std::string(argv[0]); |
| 65 | const char* last_slash = strrchr(argv[0], '/'); |
| 66 | gProgramInvocationShortName = new std::string((last_slash != NULL) ? last_slash + 1 : argv[0]); |
| 67 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 68 | const char* tags = getenv("ANDROID_LOG_TAGS"); |
| 69 | if (tags == NULL) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | std::vector<std::string> specs; |
| 74 | Split(tags, ' ', specs); |
| 75 | for (size_t i = 0; i < specs.size(); ++i) { |
| 76 | // "tag-pattern:[vdiwefs]" |
| 77 | std::string spec(specs[i]); |
| 78 | if (spec.size() == 3 && StartsWith(spec, "*:")) { |
| 79 | switch (spec[2]) { |
| 80 | case 'v': gMinimumLogSeverity = VERBOSE; continue; |
| 81 | case 'd': gMinimumLogSeverity = DEBUG; continue; |
| 82 | case 'i': gMinimumLogSeverity = INFO; continue; |
| 83 | case 'w': gMinimumLogSeverity = WARNING; continue; |
| 84 | case 'e': gMinimumLogSeverity = ERROR; continue; |
| 85 | case 'f': gMinimumLogSeverity = FATAL; continue; |
| 86 | // liblog will even suppress FATAL if you say 's' for silent, but that's crazy! |
| 87 | case 's': gMinimumLogSeverity = FATAL; continue; |
| 88 | } |
| 89 | } |
| 90 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")"; |
| 91 | } |
| 92 | } |
| 93 | |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 94 | LogMessage::~LogMessage() { |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 95 | if (data_->severity < gMinimumLogSeverity) { |
| 96 | return; // No need to format something we're not going to output. |
| 97 | } |
| 98 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 99 | // Finish constructing the message. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 100 | if (data_->error != -1) { |
| 101 | data_->buffer << ": " << strerror(data_->error); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 102 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 103 | std::string msg(data_->buffer.str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 104 | |
| 105 | // Do the actual logging with the lock held. |
| 106 | { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 107 | MutexLock mu(*GlobalSynchronization::logging_lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 108 | if (msg.find('\n') == std::string::npos) { |
| 109 | LogLine(msg.c_str()); |
| 110 | } else { |
| 111 | msg += '\n'; |
| 112 | size_t i = 0; |
| 113 | while (i < msg.size()) { |
| 114 | size_t nl = msg.find('\n', i); |
| 115 | msg[nl] = '\0'; |
| 116 | LogLine(&msg[i]); |
| 117 | i = nl + 1; |
| 118 | } |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 122 | // Abort if necessary. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 123 | if (data_->severity == FATAL) { |
Elliott Hughes | 8593fdb | 2012-04-21 20:53:44 -0700 | [diff] [blame] | 124 | Runtime::Abort(); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 125 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 126 | |
| 127 | delete data_; |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | std::ostream& LogMessage::stream() { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 131 | return data_->buffer; |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 132 | } |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 133 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 134 | HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses) |
| 135 | : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) { |
| 136 | } |
| 137 | |
| 138 | void HexDump::Dump(std::ostream& os) const { |
| 139 | if (byte_count_ == 0) { |
| 140 | return; |
| 141 | } |
| 142 | |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 143 | if (address_ == NULL) { |
| 144 | os << "00000000:"; |
| 145 | return; |
| 146 | } |
| 147 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 148 | static const char gHexDigit[] = "0123456789abcdef"; |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 149 | const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 150 | char out[76]; /* exact fit */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 151 | unsigned int offset; /* offset to show while printing */ |
| 152 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 153 | if (show_actual_addresses_) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 154 | offset = reinterpret_cast<int>(addr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 155 | } else { |
| 156 | offset = 0; |
| 157 | } |
| 158 | memset(out, ' ', sizeof(out)-1); |
| 159 | out[8] = ':'; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 160 | out[sizeof(out)-1] = '\0'; |
| 161 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 162 | size_t byte_count = byte_count_; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 163 | int gap = static_cast<int>(offset & 0x0f); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 164 | while (byte_count) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 165 | unsigned int line_offset = offset & ~0x0f; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 166 | |
| 167 | char* hex = out; |
| 168 | char* asc = out + 59; |
| 169 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 170 | for (int i = 0; i < 8; i++) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 171 | *hex++ = gHexDigit[line_offset >> 28]; |
| 172 | line_offset <<= 4; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 173 | } |
| 174 | hex++; |
| 175 | hex++; |
| 176 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 177 | int count = std::min(static_cast<int>(byte_count), 16 - gap); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 178 | CHECK_NE(count, 0); |
| 179 | CHECK_LE(count + gap, 16); |
| 180 | |
| 181 | if (gap) { |
| 182 | /* only on first line */ |
| 183 | hex += gap * 3; |
| 184 | asc += gap; |
| 185 | } |
| 186 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 187 | int i; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 188 | for (i = gap ; i < count+gap; i++) { |
| 189 | *hex++ = gHexDigit[*addr >> 4]; |
| 190 | *hex++ = gHexDigit[*addr & 0x0f]; |
| 191 | hex++; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 192 | if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) { |
| 193 | *asc++ = *addr; |
| 194 | } else { |
| 195 | *asc++ = '.'; |
| 196 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 197 | addr++; |
| 198 | } |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 199 | for (; i < 16; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 200 | /* erase extra stuff; only happens on last line */ |
| 201 | *hex++ = ' '; |
| 202 | *hex++ = ' '; |
| 203 | hex++; |
| 204 | *asc++ = ' '; |
| 205 | } |
| 206 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 207 | os << out; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 208 | |
| 209 | gap = 0; |
| 210 | byte_count -= count; |
| 211 | offset += count; |
| 212 | } |
| 213 | } |
| 214 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 215 | std::ostream& operator<<(std::ostream& os, const HexDump& rhs) { |
| 216 | rhs.Dump(os); |
| 217 | return os; |
| 218 | } |
| 219 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 220 | } // namespace art |