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 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 19 | #include "base/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 | |
Ian Rogers | f08e473 | 2013-04-09 09:45:49 -0700 | [diff] [blame] | 28 | unsigned int gAborting = 0; |
Brian Carlstrom | 81b8871 | 2012-11-05 19:21:30 -0800 | [diff] [blame] | 29 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 30 | static LogSeverity gMinimumLogSeverity = INFO; |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame^] | 31 | static std::string* gCmdLine = NULL; |
| 32 | static std::string* gProgramInvocationName = NULL; |
| 33 | static std::string* gProgramInvocationShortName = NULL; |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 34 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 35 | const char* GetCmdLine() { |
| 36 | return (gCmdLine != NULL) ? gCmdLine->c_str() : NULL; |
| 37 | } |
| 38 | |
| 39 | const char* ProgramInvocationName() { |
| 40 | return (gProgramInvocationName != NULL) ? gProgramInvocationName->c_str() : "art"; |
| 41 | } |
| 42 | |
| 43 | const char* ProgramInvocationShortName() { |
| 44 | return (gProgramInvocationShortName != NULL) ? gProgramInvocationShortName->c_str() : "art"; |
| 45 | } |
| 46 | |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 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. |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 55 | void InitLogging(char* argv[]) { |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame^] | 56 | if (gCmdLine != NULL) { |
| 57 | return; |
| 58 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 59 | // TODO: Move this to a more obvious InitART... |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 60 | Locks::Init(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 61 | |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 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. |
Brian Carlstrom | fa42b44 | 2013-06-17 12:53:45 -0700 | [diff] [blame^] | 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>"); |
Elliott Hughes | 0d39c12 | 2012-06-06 16:41:17 -0700 | [diff] [blame] | 77 | } |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 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': gMinimumLogSeverity = VERBOSE; continue; |
| 91 | case 'd': gMinimumLogSeverity = DEBUG; continue; |
| 92 | case 'i': gMinimumLogSeverity = INFO; continue; |
| 93 | case 'w': gMinimumLogSeverity = WARNING; continue; |
| 94 | case 'e': gMinimumLogSeverity = ERROR; continue; |
| 95 | case 'f': gMinimumLogSeverity = FATAL; continue; |
| 96 | // liblog will even suppress FATAL if you say 's' for silent, but that's crazy! |
| 97 | case 's': gMinimumLogSeverity = FATAL; continue; |
| 98 | } |
| 99 | } |
| 100 | LOG(FATAL) << "unsupported '" << spec << "' in ANDROID_LOG_TAGS (" << tags << ")"; |
| 101 | } |
| 102 | } |
| 103 | |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 104 | LogMessageData::LogMessageData(const char* file, int line, LogSeverity severity, int error) |
| 105 | : file(file), |
| 106 | line_number(line), |
| 107 | severity(severity), |
| 108 | error(error) { |
| 109 | const char* last_slash = strrchr(file, '/'); |
| 110 | file = (last_slash == NULL) ? file : last_slash + 1; |
| 111 | } |
| 112 | |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 113 | LogMessage::~LogMessage() { |
Elliott Hughes | 72395bf | 2012-04-24 13:45:26 -0700 | [diff] [blame] | 114 | if (data_->severity < gMinimumLogSeverity) { |
| 115 | return; // No need to format something we're not going to output. |
| 116 | } |
| 117 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 118 | // Finish constructing the message. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 119 | if (data_->error != -1) { |
| 120 | data_->buffer << ": " << strerror(data_->error); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 121 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 122 | std::string msg(data_->buffer.str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 123 | |
| 124 | // Do the actual logging with the lock held. |
| 125 | { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 126 | MutexLock mu(Thread::Current(), *Locks::logging_lock_); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 127 | if (msg.find('\n') == std::string::npos) { |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 128 | LogLine(*data_, msg.c_str()); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 129 | } else { |
| 130 | msg += '\n'; |
| 131 | size_t i = 0; |
| 132 | while (i < msg.size()) { |
| 133 | size_t nl = msg.find('\n', i); |
| 134 | msg[nl] = '\0'; |
Brian Carlstrom | af1b892 | 2012-11-27 15:19:57 -0800 | [diff] [blame] | 135 | LogLine(*data_, &msg[i]); |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 136 | i = nl + 1; |
| 137 | } |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Elliott Hughes | 5fe594f | 2011-09-08 12:33:17 -0700 | [diff] [blame] | 141 | // Abort if necessary. |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 142 | if (data_->severity == FATAL) { |
Elliott Hughes | 8593fdb | 2012-04-21 20:53:44 -0700 | [diff] [blame] | 143 | Runtime::Abort(); |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 144 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 145 | |
| 146 | delete data_; |
Elliott Hughes | 13f5a58 | 2011-09-06 13:39:14 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 149 | HexDump::HexDump(const void* address, size_t byte_count, bool show_actual_addresses) |
| 150 | : address_(address), byte_count_(byte_count), show_actual_addresses_(show_actual_addresses) { |
| 151 | } |
| 152 | |
| 153 | void HexDump::Dump(std::ostream& os) const { |
| 154 | if (byte_count_ == 0) { |
| 155 | return; |
| 156 | } |
| 157 | |
Brian Carlstrom | 93235f7 | 2012-03-29 22:48:15 -0700 | [diff] [blame] | 158 | if (address_ == NULL) { |
| 159 | os << "00000000:"; |
| 160 | return; |
| 161 | } |
| 162 | |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 163 | static const char gHexDigit[] = "0123456789abcdef"; |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 164 | const unsigned char* addr = reinterpret_cast<const unsigned char*>(address_); |
Elliott Hughes | 21f32d7 | 2011-11-09 17:44:13 -0800 | [diff] [blame] | 165 | char out[76]; /* exact fit */ |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 166 | unsigned int offset; /* offset to show while printing */ |
| 167 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 168 | if (show_actual_addresses_) { |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 169 | offset = reinterpret_cast<int>(addr); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 170 | } else { |
| 171 | offset = 0; |
| 172 | } |
| 173 | memset(out, ' ', sizeof(out)-1); |
| 174 | out[8] = ':'; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 175 | out[sizeof(out)-1] = '\0'; |
| 176 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 177 | size_t byte_count = byte_count_; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 178 | int gap = static_cast<int>(offset & 0x0f); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 179 | while (byte_count) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 180 | unsigned int line_offset = offset & ~0x0f; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 181 | |
| 182 | char* hex = out; |
| 183 | char* asc = out + 59; |
| 184 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 185 | for (int i = 0; i < 8; i++) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 186 | *hex++ = gHexDigit[line_offset >> 28]; |
| 187 | line_offset <<= 4; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 188 | } |
| 189 | hex++; |
| 190 | hex++; |
| 191 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 192 | int count = std::min(static_cast<int>(byte_count), 16 - gap); |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 193 | CHECK_NE(count, 0); |
| 194 | CHECK_LE(count + gap, 16); |
| 195 | |
| 196 | if (gap) { |
| 197 | /* only on first line */ |
| 198 | hex += gap * 3; |
| 199 | asc += gap; |
| 200 | } |
| 201 | |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 202 | int i; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 203 | for (i = gap ; i < count+gap; i++) { |
| 204 | *hex++ = gHexDigit[*addr >> 4]; |
| 205 | *hex++ = gHexDigit[*addr & 0x0f]; |
| 206 | hex++; |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 207 | if (*addr >= 0x20 && *addr < 0x7f /*isprint(*addr)*/) { |
| 208 | *asc++ = *addr; |
| 209 | } else { |
| 210 | *asc++ = '.'; |
| 211 | } |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 212 | addr++; |
| 213 | } |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 214 | for (; i < 16; i++) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 215 | /* erase extra stuff; only happens on last line */ |
| 216 | *hex++ = ' '; |
| 217 | *hex++ = ' '; |
| 218 | hex++; |
| 219 | *asc++ = ' '; |
| 220 | } |
| 221 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 222 | os << out; |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 223 | |
| 224 | gap = 0; |
| 225 | byte_count -= count; |
| 226 | offset += count; |
| 227 | } |
| 228 | } |
| 229 | |
Elliott Hughes | bfbf0e2 | 2012-03-29 18:09:19 -0700 | [diff] [blame] | 230 | std::ostream& operator<<(std::ostream& os, const HexDump& rhs) { |
| 231 | rhs.Dump(os); |
| 232 | return os; |
| 233 | } |
| 234 | |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 235 | } // namespace art |