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