Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [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 | */ |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 17 | #include "utils.h" |
| 18 | |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 19 | #include <inttypes.h> |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 22 | #include <sys/syscall.h> |
| 23 | #include <sys/types.h> |
Brian Carlstrom | 4cf5e57 | 2014-02-25 11:47:48 -0800 | [diff] [blame] | 24 | #include <sys/wait.h> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 26 | #include <memory> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 27 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 28 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 29 | #include "art_method-inl.h" |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 30 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 31 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 32 | #include "dex_file-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 33 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/object-inl.h" |
| 36 | #include "mirror/object_array-inl.h" |
| 37 | #include "mirror/string.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 38 | #include "os.h" |
Kenny Root | 067d20f | 2014-03-05 14:57:21 -0800 | [diff] [blame] | 39 | #include "scoped_thread_state_change.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 40 | #include "utf-inl.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 41 | |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 42 | #if defined(__APPLE__) |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 43 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 44 | #include <sys/syscall.h> |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 45 | #endif |
| 46 | |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 47 | #include <backtrace/Backtrace.h> // For DumpNativeStack. |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 49 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 50 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 51 | #endif |
| 52 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 53 | namespace art { |
| 54 | |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 55 | #if defined(__linux__) |
| 56 | static constexpr bool kUseAddr2line = !kIsTargetBuild; |
| 57 | #endif |
| 58 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 59 | pid_t GetTid() { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 60 | #if defined(__APPLE__) |
| 61 | uint64_t owner; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 62 | CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__); // Requires Mac OS 10.6 |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 63 | return owner; |
Elliott Hughes | 323aa86 | 2014-08-20 15:00:04 -0700 | [diff] [blame] | 64 | #elif defined(__BIONIC__) |
| 65 | return gettid(); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 66 | #else |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 67 | return syscall(__NR_gettid); |
| 68 | #endif |
| 69 | } |
| 70 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 71 | std::string GetThreadName(pid_t tid) { |
| 72 | std::string result; |
| 73 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 74 | result.resize(result.size() - 1); // Lose the trailing '\n'. |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 75 | } else { |
| 76 | result = "<unknown>"; |
| 77 | } |
| 78 | return result; |
| 79 | } |
| 80 | |
Elliott Hughes | 6d3fc56 | 2014-08-27 11:47:01 -0700 | [diff] [blame] | 81 | void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size, size_t* guard_size) { |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 82 | #if defined(__APPLE__) |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 83 | *stack_size = pthread_get_stacksize_np(thread); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 84 | void* stack_addr = pthread_get_stackaddr_np(thread); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 85 | |
| 86 | // Check whether stack_addr is the base or end of the stack. |
| 87 | // (On Mac OS 10.7, it's the end.) |
| 88 | int stack_variable; |
| 89 | if (stack_addr > &stack_variable) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 90 | *stack_base = reinterpret_cast<uint8_t*>(stack_addr) - *stack_size; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 91 | } else { |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 92 | *stack_base = stack_addr; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 93 | } |
Elliott Hughes | 6d3fc56 | 2014-08-27 11:47:01 -0700 | [diff] [blame] | 94 | |
| 95 | // This is wrong, but there doesn't seem to be a way to get the actual value on the Mac. |
| 96 | pthread_attr_t attributes; |
| 97 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attributes), __FUNCTION__); |
| 98 | CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__); |
| 99 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 100 | #else |
| 101 | pthread_attr_t attributes; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 102 | CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 103 | CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__); |
Elliott Hughes | 6d3fc56 | 2014-08-27 11:47:01 -0700 | [diff] [blame] | 104 | CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 105 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
Elliott Hughes | 839cc30 | 2014-08-28 10:24:44 -0700 | [diff] [blame] | 106 | |
| 107 | #if defined(__GLIBC__) |
| 108 | // If we're the main thread, check whether we were run with an unlimited stack. In that case, |
| 109 | // glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection |
| 110 | // will be broken because we'll die long before we get close to 2GB. |
| 111 | bool is_main_thread = (::art::GetTid() == getpid()); |
| 112 | if (is_main_thread) { |
| 113 | rlimit stack_limit; |
| 114 | if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { |
| 115 | PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed"; |
| 116 | } |
| 117 | if (stack_limit.rlim_cur == RLIM_INFINITY) { |
| 118 | size_t old_stack_size = *stack_size; |
| 119 | |
| 120 | // Use the kernel default limit as our size, and adjust the base to match. |
| 121 | *stack_size = 8 * MB; |
| 122 | *stack_base = reinterpret_cast<uint8_t*>(*stack_base) + (old_stack_size - *stack_size); |
| 123 | |
| 124 | VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")" |
| 125 | << " to " << PrettySize(*stack_size) |
| 126 | << " with base " << *stack_base; |
| 127 | } |
| 128 | } |
| 129 | #endif |
| 130 | |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 131 | #endif |
| 132 | } |
| 133 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 134 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 135 | File file; |
| 136 | if (!file.Open(file_name, O_RDONLY)) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 139 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 140 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 141 | while (true) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 142 | int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[0], buf.size())); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 143 | if (n == -1) { |
| 144 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 145 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 146 | if (n == 0) { |
| 147 | return true; |
| 148 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 149 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 150 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 153 | bool PrintFileToLog(const std::string& file_name, LogSeverity level) { |
| 154 | File file; |
| 155 | if (!file.Open(file_name, O_RDONLY)) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings. |
| 160 | char buf[kBufSize + 1]; // +1 for terminator. |
| 161 | size_t filled_to = 0; |
| 162 | while (true) { |
| 163 | DCHECK_LT(filled_to, kBufSize); |
| 164 | int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to)); |
| 165 | if (n <= 0) { |
| 166 | // Print the rest of the buffer, if it exists. |
| 167 | if (filled_to > 0) { |
| 168 | buf[filled_to] = 0; |
| 169 | LOG(level) << buf; |
| 170 | } |
| 171 | return n == 0; |
| 172 | } |
| 173 | // Scan for '\n'. |
| 174 | size_t i = filled_to; |
| 175 | bool found_newline = false; |
| 176 | for (; i < filled_to + n; ++i) { |
| 177 | if (buf[i] == '\n') { |
| 178 | // Found a line break, that's something to print now. |
| 179 | buf[i] = 0; |
| 180 | LOG(level) << buf; |
| 181 | // Copy the rest to the front. |
| 182 | if (i + 1 < filled_to + n) { |
| 183 | memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1); |
| 184 | filled_to = filled_to + n - i - 1; |
| 185 | } else { |
| 186 | filled_to = 0; |
| 187 | } |
| 188 | found_newline = true; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | if (found_newline) { |
| 193 | continue; |
| 194 | } else { |
| 195 | filled_to += n; |
| 196 | // Check if we must flush now. |
| 197 | if (filled_to == kBufSize) { |
| 198 | buf[kBufSize] = 0; |
| 199 | LOG(level) << buf; |
| 200 | filled_to = 0; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 206 | std::string PrettyDescriptor(mirror::String* java_descriptor) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 207 | if (java_descriptor == nullptr) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 208 | return "null"; |
| 209 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 210 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8().c_str()); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 211 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 212 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 213 | std::string PrettyDescriptor(mirror::Class* klass) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 214 | if (klass == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 215 | return "null"; |
| 216 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 217 | std::string temp; |
| 218 | return PrettyDescriptor(klass->GetDescriptor(&temp)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 221 | std::string PrettyDescriptor(const char* descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 222 | // Count the number of '['s to get the dimensionality. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 223 | const char* c = descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 224 | size_t dim = 0; |
| 225 | while (*c == '[') { |
| 226 | dim++; |
| 227 | c++; |
| 228 | } |
| 229 | |
| 230 | // Reference or primitive? |
| 231 | if (*c == 'L') { |
| 232 | // "[[La/b/C;" -> "a.b.C[][]". |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 233 | c++; // Skip the 'L'. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 234 | } else { |
| 235 | // "[[B" -> "byte[][]". |
| 236 | // To make life easier, we make primitives look like unqualified |
| 237 | // reference types. |
| 238 | switch (*c) { |
| 239 | case 'B': c = "byte;"; break; |
| 240 | case 'C': c = "char;"; break; |
| 241 | case 'D': c = "double;"; break; |
| 242 | case 'F': c = "float;"; break; |
| 243 | case 'I': c = "int;"; break; |
| 244 | case 'J': c = "long;"; break; |
| 245 | case 'S': c = "short;"; break; |
| 246 | case 'Z': c = "boolean;"; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 247 | case 'V': c = "void;"; break; // Used when decoding return types. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 248 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 253 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 254 | std::string result; |
| 255 | const char* p = c; |
| 256 | while (*p != ';') { |
| 257 | char ch = *p++; |
| 258 | if (ch == '/') { |
| 259 | ch = '.'; |
| 260 | } |
| 261 | result.push_back(ch); |
| 262 | } |
| 263 | // ...and replace the semicolon with 'dim' "[]" pairs: |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 264 | for (size_t i = 0; i < dim; ++i) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 265 | result += "[]"; |
| 266 | } |
| 267 | return result; |
| 268 | } |
| 269 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 270 | std::string PrettyField(ArtField* f, bool with_type) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 271 | if (f == nullptr) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 272 | return "null"; |
| 273 | } |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 274 | std::string result; |
| 275 | if (with_type) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 276 | result += PrettyDescriptor(f->GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 277 | result += ' '; |
| 278 | } |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 279 | std::string temp; |
| 280 | result += PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor(&temp)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 281 | result += '.'; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 282 | result += f->GetName(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 283 | return result; |
| 284 | } |
| 285 | |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 286 | std::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 287 | if (field_idx >= dex_file.NumFieldIds()) { |
| 288 | return StringPrintf("<<invalid-field-idx-%d>>", field_idx); |
| 289 | } |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 290 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 291 | std::string result; |
| 292 | if (with_type) { |
| 293 | result += dex_file.GetFieldTypeDescriptor(field_id); |
| 294 | result += ' '; |
| 295 | } |
| 296 | result += PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(field_id)); |
| 297 | result += '.'; |
| 298 | result += dex_file.GetFieldName(field_id); |
| 299 | return result; |
| 300 | } |
| 301 | |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 302 | std::string PrettyType(uint32_t type_idx, const DexFile& dex_file) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 303 | if (type_idx >= dex_file.NumTypeIds()) { |
| 304 | return StringPrintf("<<invalid-type-idx-%d>>", type_idx); |
| 305 | } |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 306 | const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx); |
Mathieu Chartier | 4c70d77 | 2012-09-10 14:08:32 -0700 | [diff] [blame] | 307 | return PrettyDescriptor(dex_file.GetTypeDescriptor(type_id)); |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 310 | std::string PrettyArguments(const char* signature) { |
| 311 | std::string result; |
| 312 | result += '('; |
| 313 | CHECK_EQ(*signature, '('); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 314 | ++signature; // Skip the '('. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 315 | while (*signature != ')') { |
| 316 | size_t argument_length = 0; |
| 317 | while (signature[argument_length] == '[') { |
| 318 | ++argument_length; |
| 319 | } |
| 320 | if (signature[argument_length] == 'L') { |
| 321 | argument_length = (strchr(signature, ';') - signature + 1); |
| 322 | } else { |
| 323 | ++argument_length; |
| 324 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 325 | { |
| 326 | std::string argument_descriptor(signature, argument_length); |
| 327 | result += PrettyDescriptor(argument_descriptor.c_str()); |
| 328 | } |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 329 | if (signature[argument_length] != ')') { |
| 330 | result += ", "; |
| 331 | } |
| 332 | signature += argument_length; |
| 333 | } |
| 334 | CHECK_EQ(*signature, ')'); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 335 | ++signature; // Skip the ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 336 | result += ')'; |
| 337 | return result; |
| 338 | } |
| 339 | |
| 340 | std::string PrettyReturnType(const char* signature) { |
| 341 | const char* return_type = strchr(signature, ')'); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 342 | CHECK(return_type != nullptr); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 343 | ++return_type; // Skip ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 344 | return PrettyDescriptor(return_type); |
| 345 | } |
| 346 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 347 | std::string PrettyMethod(ArtMethod* m, bool with_signature) { |
Ian Rogers | 16ce092 | 2014-01-10 14:59:36 -0800 | [diff] [blame] | 348 | if (m == nullptr) { |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 349 | return "null"; |
| 350 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 351 | if (!m->IsRuntimeMethod()) { |
| 352 | m = m->GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 353 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 354 | std::string result(PrettyDescriptor(m->GetDeclaringClassDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 355 | result += '.'; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 356 | result += m->GetName(); |
Ian Rogers | 16ce092 | 2014-01-10 14:59:36 -0800 | [diff] [blame] | 357 | if (UNLIKELY(m->IsFastNative())) { |
| 358 | result += "!"; |
| 359 | } |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 360 | if (with_signature) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 361 | const Signature signature = m->GetSignature(); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 362 | std::string sig_as_string(signature.ToString()); |
| 363 | if (signature == Signature::NoSignature()) { |
| 364 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 365 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 366 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 367 | PrettyArguments(sig_as_string.c_str()); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 368 | } |
| 369 | return result; |
| 370 | } |
| 371 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 372 | std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 373 | if (method_idx >= dex_file.NumMethodIds()) { |
| 374 | return StringPrintf("<<invalid-method-idx-%d>>", method_idx); |
| 375 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 376 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 377 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 378 | result += '.'; |
| 379 | result += dex_file.GetMethodName(method_id); |
| 380 | if (with_signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 381 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 382 | std::string sig_as_string(signature.ToString()); |
| 383 | if (signature == Signature::NoSignature()) { |
| 384 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 385 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 386 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 387 | PrettyArguments(sig_as_string.c_str()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 388 | } |
| 389 | return result; |
| 390 | } |
| 391 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 392 | std::string PrettyTypeOf(mirror::Object* obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 393 | if (obj == nullptr) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 394 | return "null"; |
| 395 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 396 | if (obj->GetClass() == nullptr) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 397 | return "(raw)"; |
| 398 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 399 | std::string temp; |
| 400 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor(&temp))); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 401 | if (obj->IsClass()) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 402 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor(&temp)) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 403 | } |
| 404 | return result; |
| 405 | } |
| 406 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 407 | std::string PrettyClass(mirror::Class* c) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 408 | if (c == nullptr) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 409 | return "null"; |
| 410 | } |
| 411 | std::string result; |
| 412 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 413 | result += PrettyDescriptor(c); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 414 | result += ">"; |
| 415 | return result; |
| 416 | } |
| 417 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 418 | std::string PrettyClassAndClassLoader(mirror::Class* c) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 419 | if (c == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 420 | return "null"; |
| 421 | } |
| 422 | std::string result; |
| 423 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 424 | result += PrettyDescriptor(c); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 425 | result += ","; |
| 426 | result += PrettyTypeOf(c->GetClassLoader()); |
| 427 | // TODO: add an identifying hash value for the loader |
| 428 | result += ">"; |
| 429 | return result; |
| 430 | } |
| 431 | |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 432 | std::string PrettyJavaAccessFlags(uint32_t access_flags) { |
| 433 | std::string result; |
| 434 | if ((access_flags & kAccPublic) != 0) { |
| 435 | result += "public "; |
| 436 | } |
| 437 | if ((access_flags & kAccProtected) != 0) { |
| 438 | result += "protected "; |
| 439 | } |
| 440 | if ((access_flags & kAccPrivate) != 0) { |
| 441 | result += "private "; |
| 442 | } |
| 443 | if ((access_flags & kAccFinal) != 0) { |
| 444 | result += "final "; |
| 445 | } |
| 446 | if ((access_flags & kAccStatic) != 0) { |
| 447 | result += "static "; |
| 448 | } |
| 449 | if ((access_flags & kAccTransient) != 0) { |
| 450 | result += "transient "; |
| 451 | } |
| 452 | if ((access_flags & kAccVolatile) != 0) { |
| 453 | result += "volatile "; |
| 454 | } |
| 455 | if ((access_flags & kAccSynchronized) != 0) { |
| 456 | result += "synchronized "; |
| 457 | } |
| 458 | return result; |
| 459 | } |
| 460 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 461 | std::string PrettySize(int64_t byte_count) { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 462 | // The byte thresholds at which we display amounts. A byte count is displayed |
| 463 | // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 464 | static const int64_t kUnitThresholds[] = { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 465 | 0, // B up to... |
| 466 | 3*1024, // KB up to... |
| 467 | 2*1024*1024, // MB up to... |
| 468 | 1024*1024*1024 // GB from here. |
| 469 | }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 470 | static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB }; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 471 | static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 472 | const char* negative_str = ""; |
| 473 | if (byte_count < 0) { |
| 474 | negative_str = "-"; |
| 475 | byte_count = -byte_count; |
| 476 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 477 | int i = arraysize(kUnitThresholds); |
| 478 | while (--i > 0) { |
| 479 | if (byte_count >= kUnitThresholds[i]) { |
| 480 | break; |
| 481 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 482 | } |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 483 | return StringPrintf("%s%" PRId64 "%s", |
| 484 | negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 487 | std::string PrintableChar(uint16_t ch) { |
| 488 | std::string result; |
| 489 | result += '\''; |
| 490 | if (NeedsEscaping(ch)) { |
| 491 | StringAppendF(&result, "\\u%04x", ch); |
| 492 | } else { |
| 493 | result += ch; |
| 494 | } |
| 495 | result += '\''; |
| 496 | return result; |
| 497 | } |
| 498 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 499 | std::string PrintableString(const char* utf) { |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 500 | std::string result; |
| 501 | result += '"'; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 502 | const char* p = utf; |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 503 | size_t char_count = CountModifiedUtf8Chars(p); |
| 504 | for (size_t i = 0; i < char_count; ++i) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 505 | uint32_t ch = GetUtf16FromUtf8(&p); |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 506 | if (ch == '\\') { |
| 507 | result += "\\\\"; |
| 508 | } else if (ch == '\n') { |
| 509 | result += "\\n"; |
| 510 | } else if (ch == '\r') { |
| 511 | result += "\\r"; |
| 512 | } else if (ch == '\t') { |
| 513 | result += "\\t"; |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 514 | } else { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 515 | const uint16_t leading = GetLeadingUtf16Char(ch); |
| 516 | |
| 517 | if (NeedsEscaping(leading)) { |
| 518 | StringAppendF(&result, "\\u%04x", leading); |
| 519 | } else { |
| 520 | result += leading; |
| 521 | } |
| 522 | |
| 523 | const uint32_t trailing = GetTrailingUtf16Char(ch); |
| 524 | if (trailing != 0) { |
| 525 | // All high surrogates will need escaping. |
| 526 | StringAppendF(&result, "\\u%04x", trailing); |
| 527 | } |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | result += '"'; |
| 531 | return result; |
| 532 | } |
| 533 | |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 534 | // See http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp615 for the full rules. |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 535 | std::string MangleForJni(const std::string& s) { |
| 536 | std::string result; |
| 537 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 538 | const char* cp = &s[0]; |
| 539 | for (size_t i = 0; i < char_count; ++i) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 540 | uint32_t ch = GetUtf16FromUtf8(&cp); |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 541 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) { |
| 542 | result.push_back(ch); |
| 543 | } else if (ch == '.' || ch == '/') { |
| 544 | result += "_"; |
| 545 | } else if (ch == '_') { |
| 546 | result += "_1"; |
| 547 | } else if (ch == ';') { |
| 548 | result += "_2"; |
| 549 | } else if (ch == '[') { |
| 550 | result += "_3"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 551 | } else { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 552 | const uint16_t leading = GetLeadingUtf16Char(ch); |
| 553 | const uint32_t trailing = GetTrailingUtf16Char(ch); |
| 554 | |
| 555 | StringAppendF(&result, "_0%04x", leading); |
| 556 | if (trailing != 0) { |
| 557 | StringAppendF(&result, "_0%04x", trailing); |
| 558 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | return result; |
| 562 | } |
| 563 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 564 | std::string DotToDescriptor(const char* class_name) { |
| 565 | std::string descriptor(class_name); |
| 566 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 567 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 568 | descriptor = "L" + descriptor + ";"; |
| 569 | } |
| 570 | return descriptor; |
| 571 | } |
| 572 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 573 | std::string DescriptorToDot(const char* descriptor) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 574 | size_t length = strlen(descriptor); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 575 | if (length > 1) { |
| 576 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 577 | // Descriptors have the leading 'L' and trailing ';' stripped. |
| 578 | std::string result(descriptor + 1, length - 2); |
| 579 | std::replace(result.begin(), result.end(), '/', '.'); |
| 580 | return result; |
| 581 | } else { |
| 582 | // For arrays the 'L' and ';' remain intact. |
| 583 | std::string result(descriptor); |
| 584 | std::replace(result.begin(), result.end(), '/', '.'); |
| 585 | return result; |
| 586 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 587 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 588 | // Do nothing for non-class/array descriptors. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 589 | return descriptor; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | std::string DescriptorToName(const char* descriptor) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 593 | size_t length = strlen(descriptor); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 594 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 595 | std::string result(descriptor + 1, length - 2); |
| 596 | return result; |
| 597 | } |
| 598 | return descriptor; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 601 | std::string JniShortName(ArtMethod* m) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 602 | std::string class_name(m->GetDeclaringClassDescriptor()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 603 | // Remove the leading 'L' and trailing ';'... |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 604 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 605 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 606 | class_name.erase(0, 1); |
| 607 | class_name.erase(class_name.size() - 1, 1); |
| 608 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 609 | std::string method_name(m->GetName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 610 | |
| 611 | std::string short_name; |
| 612 | short_name += "Java_"; |
| 613 | short_name += MangleForJni(class_name); |
| 614 | short_name += "_"; |
| 615 | short_name += MangleForJni(method_name); |
| 616 | return short_name; |
| 617 | } |
| 618 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 619 | std::string JniLongName(ArtMethod* m) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 620 | std::string long_name; |
| 621 | long_name += JniShortName(m); |
| 622 | long_name += "__"; |
| 623 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 624 | std::string signature(m->GetSignature().ToString()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 625 | signature.erase(0, 1); |
| 626 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 627 | |
| 628 | long_name += MangleForJni(signature); |
| 629 | |
| 630 | return long_name; |
| 631 | } |
| 632 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 633 | // Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 634 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 635 | 0x00000000, // 00..1f low control characters; nothing valid |
| 636 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 637 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 638 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 639 | }; |
| 640 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 641 | // Helper for IsValidPartOfMemberNameUtf8(); do not call directly. |
| 642 | bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 643 | /* |
| 644 | * It's a multibyte encoded character. Decode it and analyze. We |
| 645 | * accept anything that isn't (a) an improperly encoded low value, |
| 646 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 647 | * control character, or (e) a high space, layout, or special |
| 648 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 649 | * U+fff0..U+ffff). This is all specified in the dex format |
| 650 | * document. |
| 651 | */ |
| 652 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 653 | const uint32_t pair = GetUtf16FromUtf8(pUtf8Ptr); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 654 | const uint16_t leading = GetLeadingUtf16Char(pair); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 655 | |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 656 | // We have a surrogate pair resulting from a valid 4 byte UTF sequence. |
| 657 | // No further checks are necessary because 4 byte sequences span code |
| 658 | // points [U+10000, U+1FFFFF], which are valid codepoints in a dex |
| 659 | // identifier. Furthermore, GetUtf16FromUtf8 guarantees that each of |
| 660 | // the surrogate halves are valid and well formed in this instance. |
| 661 | if (GetTrailingUtf16Char(pair) != 0) { |
| 662 | return true; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | // We've encountered a one, two or three byte UTF-8 sequence. The |
| 667 | // three byte UTF-8 sequence could be one half of a surrogate pair. |
| 668 | switch (leading >> 8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 669 | case 0x00: |
| 670 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 671 | return (leading > 0x00a0); |
| 672 | case 0xd8: |
| 673 | case 0xd9: |
| 674 | case 0xda: |
| 675 | case 0xdb: |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 676 | { |
| 677 | // We found a three byte sequence encoding one half of a surrogate. |
| 678 | // Look for the other half. |
| 679 | const uint32_t pair2 = GetUtf16FromUtf8(pUtf8Ptr); |
| 680 | const uint16_t trailing = GetLeadingUtf16Char(pair2); |
| 681 | |
| 682 | return (GetTrailingUtf16Char(pair2) == 0) && (0xdc00 <= trailing && trailing <= 0xdfff); |
| 683 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 684 | case 0xdc: |
| 685 | case 0xdd: |
| 686 | case 0xde: |
| 687 | case 0xdf: |
| 688 | // It's a trailing surrogate, which is not valid at this point. |
| 689 | return false; |
| 690 | case 0x20: |
| 691 | case 0xff: |
| 692 | // It's in the range that has spaces, controls, and specials. |
| 693 | switch (leading & 0xfff8) { |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 694 | case 0x2000: |
| 695 | case 0x2008: |
| 696 | case 0x2028: |
| 697 | case 0xfff0: |
| 698 | case 0xfff8: |
| 699 | return false; |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 700 | } |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 701 | return true; |
| 702 | default: |
| 703 | return true; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 704 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 705 | |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 706 | UNREACHABLE(); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 710 | * valid as part of a member name, updating the pointer to point past |
| 711 | * the consumed character. This will consume two encoded UTF-16 code |
| 712 | * points if the character is encoded as a surrogate pair. Also, if |
| 713 | * this function returns false, then the given pointer may only have |
| 714 | * been partially advanced. |
| 715 | */ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 716 | static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 717 | uint8_t c = (uint8_t) **pUtf8Ptr; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 718 | if (LIKELY(c <= 0x7f)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 719 | // It's low-ascii, so check the table. |
| 720 | uint32_t wordIdx = c >> 5; |
| 721 | uint32_t bitIdx = c & 0x1f; |
| 722 | (*pUtf8Ptr)++; |
| 723 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 724 | } |
| 725 | |
| 726 | // It's a multibyte encoded character. Call a non-inline function |
| 727 | // for the heavy lifting. |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 728 | return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr); |
| 729 | } |
| 730 | |
| 731 | bool IsValidMemberName(const char* s) { |
| 732 | bool angle_name = false; |
| 733 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 734 | switch (*s) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 735 | case '\0': |
| 736 | // The empty string is not a valid name. |
| 737 | return false; |
| 738 | case '<': |
| 739 | angle_name = true; |
| 740 | s++; |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | while (true) { |
| 745 | switch (*s) { |
| 746 | case '\0': |
| 747 | return !angle_name; |
| 748 | case '>': |
| 749 | return angle_name && s[1] == '\0'; |
| 750 | } |
| 751 | |
| 752 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
| 753 | return false; |
| 754 | } |
| 755 | } |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 758 | enum ClassNameType { kName, kDescriptor }; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 759 | template<ClassNameType kType, char kSeparator> |
| 760 | static bool IsValidClassName(const char* s) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 761 | int arrayCount = 0; |
| 762 | while (*s == '[') { |
| 763 | arrayCount++; |
| 764 | s++; |
| 765 | } |
| 766 | |
| 767 | if (arrayCount > 255) { |
| 768 | // Arrays may have no more than 255 dimensions. |
| 769 | return false; |
| 770 | } |
| 771 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 772 | ClassNameType type = kType; |
| 773 | if (type != kDescriptor && arrayCount != 0) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 774 | /* |
| 775 | * If we're looking at an array of some sort, then it doesn't |
| 776 | * matter if what is being asked for is a class name; the |
| 777 | * format looks the same as a type descriptor in that case, so |
| 778 | * treat it as such. |
| 779 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 780 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 783 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 784 | /* |
| 785 | * We are looking for a descriptor. Either validate it as a |
| 786 | * single-character primitive type, or continue on to check the |
| 787 | * embedded class name (bracketed by "L" and ";"). |
| 788 | */ |
| 789 | switch (*(s++)) { |
| 790 | case 'B': |
| 791 | case 'C': |
| 792 | case 'D': |
| 793 | case 'F': |
| 794 | case 'I': |
| 795 | case 'J': |
| 796 | case 'S': |
| 797 | case 'Z': |
| 798 | // These are all single-character descriptors for primitive types. |
| 799 | return (*s == '\0'); |
| 800 | case 'V': |
| 801 | // Non-array void is valid, but you can't have an array of void. |
| 802 | return (arrayCount == 0) && (*s == '\0'); |
| 803 | case 'L': |
| 804 | // Class name: Break out and continue below. |
| 805 | break; |
| 806 | default: |
| 807 | // Oddball descriptor character. |
| 808 | return false; |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | /* |
| 813 | * We just consumed the 'L' that introduces a class name as part |
| 814 | * of a type descriptor, or we are looking for an unadorned class |
| 815 | * name. |
| 816 | */ |
| 817 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 818 | bool sepOrFirst = true; // first character or just encountered a separator. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 819 | for (;;) { |
| 820 | uint8_t c = (uint8_t) *s; |
| 821 | switch (c) { |
| 822 | case '\0': |
| 823 | /* |
| 824 | * Premature end for a type descriptor, but valid for |
| 825 | * a class name as long as we haven't encountered an |
| 826 | * empty component (including the degenerate case of |
| 827 | * the empty string ""). |
| 828 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 829 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 830 | case ';': |
| 831 | /* |
| 832 | * Invalid character for a class name, but the |
| 833 | * legitimate end of a type descriptor. In the latter |
| 834 | * case, make sure that this is the end of the string |
| 835 | * and that it doesn't end with an empty component |
| 836 | * (including the degenerate case of "L;"). |
| 837 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 838 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 839 | case '/': |
| 840 | case '.': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 841 | if (c != kSeparator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 842 | // The wrong separator character. |
| 843 | return false; |
| 844 | } |
| 845 | if (sepOrFirst) { |
| 846 | // Separator at start or two separators in a row. |
| 847 | return false; |
| 848 | } |
| 849 | sepOrFirst = true; |
| 850 | s++; |
| 851 | break; |
| 852 | default: |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 853 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 854 | return false; |
| 855 | } |
| 856 | sepOrFirst = false; |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 862 | bool IsValidBinaryClassName(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 863 | return IsValidClassName<kName, '.'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | bool IsValidJniClassName(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 867 | return IsValidClassName<kName, '/'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | bool IsValidDescriptor(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 871 | return IsValidClassName<kDescriptor, '/'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 874 | void Split(const std::string& s, char separator, std::vector<std::string>* result) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 875 | const char* p = s.data(); |
| 876 | const char* end = p + s.size(); |
| 877 | while (p != end) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 878 | if (*p == separator) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 879 | ++p; |
| 880 | } else { |
| 881 | const char* start = p; |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 882 | while (++p != end && *p != separator) { |
| 883 | // Skip to the next occurrence of the separator. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 884 | } |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 885 | result->push_back(std::string(start, p - start)); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 890 | std::string Trim(const std::string& s) { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 891 | std::string result; |
| 892 | unsigned int start_index = 0; |
| 893 | unsigned int end_index = s.size() - 1; |
| 894 | |
| 895 | // Skip initial whitespace. |
| 896 | while (start_index < s.size()) { |
| 897 | if (!isspace(s[start_index])) { |
| 898 | break; |
| 899 | } |
| 900 | start_index++; |
| 901 | } |
| 902 | |
| 903 | // Skip terminating whitespace. |
| 904 | while (end_index >= start_index) { |
| 905 | if (!isspace(s[end_index])) { |
| 906 | break; |
| 907 | } |
| 908 | end_index--; |
| 909 | } |
| 910 | |
| 911 | // All spaces, no beef. |
| 912 | if (end_index < start_index) { |
| 913 | return ""; |
| 914 | } |
| 915 | // Start_index is the first non-space, end_index is the last one. |
| 916 | return s.substr(start_index, end_index - start_index + 1); |
| 917 | } |
| 918 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 919 | template <typename StringT> |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 920 | std::string Join(const std::vector<StringT>& strings, char separator) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 921 | if (strings.empty()) { |
| 922 | return ""; |
| 923 | } |
| 924 | |
| 925 | std::string result(strings[0]); |
| 926 | for (size_t i = 1; i < strings.size(); ++i) { |
| 927 | result += separator; |
| 928 | result += strings[i]; |
| 929 | } |
| 930 | return result; |
| 931 | } |
| 932 | |
| 933 | // Explicit instantiations. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 934 | template std::string Join<std::string>(const std::vector<std::string>& strings, char separator); |
| 935 | template std::string Join<const char*>(const std::vector<const char*>& strings, char separator); |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 936 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 937 | bool StartsWith(const std::string& s, const char* prefix) { |
| 938 | return s.compare(0, strlen(prefix), prefix) == 0; |
| 939 | } |
| 940 | |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 941 | bool EndsWith(const std::string& s, const char* suffix) { |
| 942 | size_t suffix_length = strlen(suffix); |
| 943 | size_t string_length = s.size(); |
| 944 | if (suffix_length > string_length) { |
| 945 | return false; |
| 946 | } |
| 947 | size_t offset = string_length - suffix_length; |
| 948 | return s.compare(offset, suffix_length, suffix) == 0; |
| 949 | } |
| 950 | |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 951 | void SetThreadName(const char* thread_name) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 952 | int hasAt = 0; |
| 953 | int hasDot = 0; |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 954 | const char* s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 955 | while (*s) { |
| 956 | if (*s == '.') { |
| 957 | hasDot = 1; |
| 958 | } else if (*s == '@') { |
| 959 | hasAt = 1; |
| 960 | } |
| 961 | s++; |
| 962 | } |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 963 | int len = s - thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 964 | if (len < 15 || hasAt || !hasDot) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 965 | s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 966 | } else { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 967 | s = thread_name + len - 15; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 968 | } |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 969 | #if defined(__linux__) |
Elliott Hughes | 7c6a61e | 2012-03-12 18:01:41 -0700 | [diff] [blame] | 970 | // pthread_setname_np fails rather than truncating long strings. |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 971 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 972 | strncpy(buf, s, sizeof(buf)-1); |
| 973 | buf[sizeof(buf)-1] = '\0'; |
| 974 | errno = pthread_setname_np(pthread_self(), buf); |
| 975 | if (errno != 0) { |
| 976 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 977 | } |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 978 | #else // __APPLE__ |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 979 | pthread_setname_np(thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 980 | #endif |
| 981 | } |
| 982 | |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 983 | void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) { |
| 984 | *utime = *stime = *task_cpu = 0; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 985 | std::string stats; |
Elliott Hughes | 8a31b50 | 2012-04-30 19:36:11 -0700 | [diff] [blame] | 986 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 987 | return; |
| 988 | } |
| 989 | // Skip the command, which may contain spaces. |
| 990 | stats = stats.substr(stats.find(')') + 2); |
| 991 | // Extract the three fields we care about. |
| 992 | std::vector<std::string> fields; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 993 | Split(stats, ' ', &fields); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 994 | *state = fields[0][0]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 995 | *utime = strtoull(fields[11].c_str(), nullptr, 10); |
| 996 | *stime = strtoull(fields[12].c_str(), nullptr, 10); |
| 997 | *task_cpu = strtoull(fields[36].c_str(), nullptr, 10); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1000 | std::string GetSchedulerGroupName(pid_t tid) { |
| 1001 | // /proc/<pid>/cgroup looks like this: |
| 1002 | // 2:devices:/ |
| 1003 | // 1:cpuacct,cpu:/ |
| 1004 | // We want the third field from the line whose second field contains the "cpu" token. |
| 1005 | std::string cgroup_file; |
| 1006 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) { |
| 1007 | return ""; |
| 1008 | } |
| 1009 | std::vector<std::string> cgroup_lines; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1010 | Split(cgroup_file, '\n', &cgroup_lines); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1011 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 1012 | std::vector<std::string> cgroup_fields; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1013 | Split(cgroup_lines[i], ':', &cgroup_fields); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1014 | std::vector<std::string> cgroups; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1015 | Split(cgroup_fields[1], ',', &cgroups); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1016 | for (size_t j = 0; j < cgroups.size(); ++j) { |
| 1017 | if (cgroups[j] == "cpu") { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1018 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | return ""; |
| 1023 | } |
| 1024 | |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1025 | #if defined(__linux__) |
Andreas Gampe | 00bd2da | 2015-01-09 15:05:46 -0800 | [diff] [blame] | 1026 | |
| 1027 | ALWAYS_INLINE |
| 1028 | static inline void WritePrefix(std::ostream* os, const char* prefix, bool odd) { |
| 1029 | if (prefix != nullptr) { |
| 1030 | *os << prefix; |
| 1031 | } |
| 1032 | *os << " "; |
| 1033 | if (!odd) { |
| 1034 | *os << " "; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1038 | static bool RunCommand(std::string cmd, std::ostream* os, const char* prefix) { |
| 1039 | FILE* stream = popen(cmd.c_str(), "r"); |
| 1040 | if (stream) { |
| 1041 | if (os != nullptr) { |
| 1042 | bool odd_line = true; // We indent them differently. |
Andreas Gampe | 00bd2da | 2015-01-09 15:05:46 -0800 | [diff] [blame] | 1043 | bool wrote_prefix = false; // Have we already written a prefix? |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1044 | constexpr size_t kMaxBuffer = 128; // Relatively small buffer. Should be OK as we're on an |
| 1045 | // alt stack, but just to be sure... |
| 1046 | char buffer[kMaxBuffer]; |
| 1047 | while (!feof(stream)) { |
| 1048 | if (fgets(buffer, kMaxBuffer, stream) != nullptr) { |
| 1049 | // Split on newlines. |
| 1050 | char* tmp = buffer; |
| 1051 | for (;;) { |
| 1052 | char* new_line = strchr(tmp, '\n'); |
| 1053 | if (new_line == nullptr) { |
| 1054 | // Print the rest. |
| 1055 | if (*tmp != 0) { |
Andreas Gampe | 00bd2da | 2015-01-09 15:05:46 -0800 | [diff] [blame] | 1056 | if (!wrote_prefix) { |
| 1057 | WritePrefix(os, prefix, odd_line); |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1058 | } |
Andreas Gampe | 00bd2da | 2015-01-09 15:05:46 -0800 | [diff] [blame] | 1059 | wrote_prefix = true; |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1060 | *os << tmp; |
| 1061 | } |
| 1062 | break; |
| 1063 | } |
Andreas Gampe | 00bd2da | 2015-01-09 15:05:46 -0800 | [diff] [blame] | 1064 | if (!wrote_prefix) { |
| 1065 | WritePrefix(os, prefix, odd_line); |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1066 | } |
| 1067 | char saved = *(new_line + 1); |
| 1068 | *(new_line + 1) = 0; |
| 1069 | *os << tmp; |
| 1070 | *(new_line + 1) = saved; |
| 1071 | tmp = new_line + 1; |
| 1072 | odd_line = !odd_line; |
Andreas Gampe | 00bd2da | 2015-01-09 15:05:46 -0800 | [diff] [blame] | 1073 | wrote_prefix = false; |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | pclose(stream); |
| 1079 | return true; |
| 1080 | } else { |
| 1081 | return false; |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | static void Addr2line(const std::string& map_src, uintptr_t offset, std::ostream& os, |
| 1086 | const char* prefix) { |
| 1087 | std::string cmdline(StringPrintf("addr2line --functions --inlines --demangle -e %s %zx", |
| 1088 | map_src.c_str(), offset)); |
| 1089 | RunCommand(cmdline.c_str(), &os, prefix); |
| 1090 | } |
| 1091 | #endif |
| 1092 | |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1093 | void DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1094 | ArtMethod* current_method, void* ucontext_ptr) { |
Ian Rogers | 83597d0 | 2014-11-20 10:29:00 -0800 | [diff] [blame] | 1095 | #if __linux__ |
Andreas Gampe | d757632 | 2014-10-24 22:13:45 -0700 | [diff] [blame] | 1096 | // b/18119146 |
| 1097 | if (RUNNING_ON_VALGRIND != 0) { |
| 1098 | return; |
| 1099 | } |
| 1100 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1101 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid)); |
Andreas Gampe | 628a61a | 2015-01-07 22:08:35 -0800 | [diff] [blame] | 1102 | if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) { |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1103 | os << prefix << "(backtrace::Unwind failed for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1104 | return; |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1105 | } else if (backtrace->NumFrames() == 0) { |
Elliott Hughes | 225f5a1 | 2012-06-11 11:23:48 -0700 | [diff] [blame] | 1106 | os << prefix << "(no native stack frames for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1107 | return; |
| 1108 | } |
| 1109 | |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1110 | // Check whether we have and should use addr2line. |
| 1111 | bool use_addr2line; |
| 1112 | if (kUseAddr2line) { |
| 1113 | // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out |
| 1114 | // and print to stderr. |
Andreas Gampe | 941c551 | 2015-01-15 10:38:19 -0800 | [diff] [blame] | 1115 | use_addr2line = (gAborting > 0) && RunCommand("addr2line -h", nullptr, nullptr); |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1116 | } else { |
| 1117 | use_addr2line = false; |
| 1118 | } |
| 1119 | |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 1120 | for (Backtrace::const_iterator it = backtrace->begin(); |
| 1121 | it != backtrace->end(); ++it) { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1122 | // We produce output like this: |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1123 | // ] #00 pc 000075bb8 /system/lib/libc.so (unwind_backtrace_thread+536) |
| 1124 | // In order for parsing tools to continue to function, the stack dump |
| 1125 | // format must at least adhere to this format: |
| 1126 | // #XX pc <RELATIVE_ADDR> <FULL_PATH_TO_SHARED_LIBRARY> ... |
| 1127 | // The parsers require a single space before and after pc, and two spaces |
| 1128 | // after the <RELATIVE_ADDR>. There can be any prefix data before the |
| 1129 | // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix. |
| 1130 | os << prefix << StringPrintf("#%02zu pc ", it->num); |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1131 | bool try_addr2line = false; |
Christopher Ferris | a1c9665 | 2015-02-06 13:18:58 -0800 | [diff] [blame] | 1132 | if (!BacktraceMap::IsValid(it->map)) { |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1133 | os << StringPrintf("%08" PRIxPTR " ???", it->pc); |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1134 | } else { |
Christopher Ferris | 5a1b2bf | 2015-05-01 16:03:49 -0700 | [diff] [blame] | 1135 | os << StringPrintf("%08" PRIxPTR " ", BacktraceMap::GetRelativePc(it->map, it->pc)); |
Christopher Ferris | a1c9665 | 2015-02-06 13:18:58 -0800 | [diff] [blame] | 1136 | os << it->map.name; |
Andreas Gampe | 3ef69b4 | 2015-01-26 10:38:34 -0800 | [diff] [blame] | 1137 | os << " ("; |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1138 | if (!it->func_name.empty()) { |
| 1139 | os << it->func_name; |
| 1140 | if (it->func_offset != 0) { |
| 1141 | os << "+" << it->func_offset; |
| 1142 | } |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1143 | try_addr2line = true; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 1144 | } else if ( |
| 1145 | current_method != nullptr && Locks::mutator_lock_->IsSharedHeld(Thread::Current()) && |
| 1146 | current_method->PcIsWithinQuickCode(it->pc)) { |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 1147 | const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode(); |
| 1148 | os << JniLongName(current_method) << "+" |
| 1149 | << (it->pc - reinterpret_cast<uintptr_t>(start_of_code)); |
Kenny Root | 067d20f | 2014-03-05 14:57:21 -0800 | [diff] [blame] | 1150 | } else { |
| 1151 | os << "???"; |
| 1152 | } |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1153 | os << ")"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1154 | } |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1155 | os << "\n"; |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1156 | if (try_addr2line && use_addr2line) { |
Christopher Ferris | a1c9665 | 2015-02-06 13:18:58 -0800 | [diff] [blame] | 1157 | Addr2line(it->map.name, it->pc - it->map.start, os, prefix); |
Andreas Gampe | 8e1cb91 | 2015-01-08 20:11:09 -0800 | [diff] [blame] | 1158 | } |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1159 | } |
Nicolas Geoffray | e6ac4fd | 2014-11-04 13:03:29 +0000 | [diff] [blame] | 1160 | #else |
Andreas Gampe | 9387c72 | 2015-01-08 11:32:22 -0800 | [diff] [blame] | 1161 | UNUSED(os, tid, prefix, current_method, ucontext_ptr); |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 1162 | #endif |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1165 | #if defined(__APPLE__) |
| 1166 | |
| 1167 | // TODO: is there any way to get the kernel stack on Mac OS? |
| 1168 | void DumpKernelStack(std::ostream&, pid_t, const char*, bool) {} |
| 1169 | |
| 1170 | #else |
| 1171 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1172 | void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) { |
Elliott Hughes | 12a9502 | 2012-05-24 21:41:38 -0700 | [diff] [blame] | 1173 | if (tid == GetTid()) { |
| 1174 | // There's no point showing that we're reading our stack out of /proc! |
| 1175 | return; |
| 1176 | } |
| 1177 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1178 | std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid)); |
| 1179 | std::string kernel_stack; |
| 1180 | if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) { |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1181 | os << prefix << "(couldn't read " << kernel_stack_filename << ")\n"; |
jeffhao | c4c3ee2 | 2012-05-25 16:16:32 -0700 | [diff] [blame] | 1182 | return; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | std::vector<std::string> kernel_stack_frames; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1186 | Split(kernel_stack, '\n', &kernel_stack_frames); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1187 | // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff", |
| 1188 | // which looking at the source appears to be the kernel's way of saying "that's all, folks!". |
| 1189 | kernel_stack_frames.pop_back(); |
| 1190 | for (size_t i = 0; i < kernel_stack_frames.size(); ++i) { |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 1191 | // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110" |
| 1192 | // into "futex_wait_queue_me+0xcd/0x110". |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1193 | const char* text = kernel_stack_frames[i].c_str(); |
| 1194 | const char* close_bracket = strchr(text, ']'); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1195 | if (close_bracket != nullptr) { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1196 | text = close_bracket + 2; |
| 1197 | } |
| 1198 | os << prefix; |
| 1199 | if (include_count) { |
| 1200 | os << StringPrintf("#%02zd ", i); |
| 1201 | } |
| 1202 | os << text << "\n"; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | #endif |
| 1207 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1208 | const char* GetAndroidRoot() { |
| 1209 | const char* android_root = getenv("ANDROID_ROOT"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1210 | if (android_root == nullptr) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1211 | if (OS::DirectoryExists("/system")) { |
| 1212 | android_root = "/system"; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1213 | } else { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1214 | LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist"; |
| 1215 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1216 | } |
| 1217 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1218 | if (!OS::DirectoryExists(android_root)) { |
| 1219 | LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1220 | return ""; |
| 1221 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1222 | return android_root; |
| 1223 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1224 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1225 | const char* GetAndroidData() { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1226 | std::string error_msg; |
| 1227 | const char* dir = GetAndroidDataSafe(&error_msg); |
| 1228 | if (dir != nullptr) { |
| 1229 | return dir; |
| 1230 | } else { |
| 1231 | LOG(FATAL) << error_msg; |
| 1232 | return ""; |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | const char* GetAndroidDataSafe(std::string* error_msg) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1237 | const char* android_data = getenv("ANDROID_DATA"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1238 | if (android_data == nullptr) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1239 | if (OS::DirectoryExists("/data")) { |
| 1240 | android_data = "/data"; |
| 1241 | } else { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1242 | *error_msg = "ANDROID_DATA not set and /data does not exist"; |
| 1243 | return nullptr; |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | if (!OS::DirectoryExists(android_data)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1247 | *error_msg = StringPrintf("Failed to find ANDROID_DATA directory %s", android_data); |
| 1248 | return nullptr; |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1249 | } |
| 1250 | return android_data; |
| 1251 | } |
| 1252 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1253 | void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string* dalvik_cache, |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1254 | bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1255 | CHECK(subdir != nullptr); |
| 1256 | std::string error_msg; |
| 1257 | const char* android_data = GetAndroidDataSafe(&error_msg); |
| 1258 | if (android_data == nullptr) { |
| 1259 | *have_android_data = false; |
| 1260 | *dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1261 | *is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1262 | return; |
| 1263 | } else { |
| 1264 | *have_android_data = true; |
| 1265 | } |
| 1266 | const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data)); |
| 1267 | *dalvik_cache = dalvik_cache_root + subdir; |
| 1268 | *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str()); |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1269 | *is_global_cache = strcmp(android_data, "/data") == 0; |
| 1270 | if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1271 | // Don't create the system's /data/dalvik-cache/... because it needs special permissions. |
| 1272 | *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) && |
| 1273 | (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST)); |
| 1274 | } |
| 1275 | } |
| 1276 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 1277 | static std::string GetDalvikCacheImpl(const char* subdir, |
| 1278 | const bool create_if_absent, |
| 1279 | const bool abort_on_error) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1280 | CHECK(subdir != nullptr); |
Brian Carlstrom | 41ccffd | 2014-05-06 10:37:30 -0700 | [diff] [blame] | 1281 | const char* android_data = GetAndroidData(); |
| 1282 | const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data)); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1283 | const std::string dalvik_cache = dalvik_cache_root + subdir; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 1284 | if (!OS::DirectoryExists(dalvik_cache.c_str())) { |
| 1285 | if (!create_if_absent) { |
| 1286 | // TODO: Check callers. Traditional behavior is to not to abort, even when abort_on_error. |
| 1287 | return ""; |
| 1288 | } |
| 1289 | |
Brian Carlstrom | 41ccffd | 2014-05-06 10:37:30 -0700 | [diff] [blame] | 1290 | // Don't create the system's /data/dalvik-cache/... because it needs special permissions. |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 1291 | if (strcmp(android_data, "/data") == 0) { |
| 1292 | if (abort_on_error) { |
| 1293 | LOG(FATAL) << "Failed to find dalvik-cache directory " << dalvik_cache |
| 1294 | << ", cannot create /data dalvik-cache."; |
| 1295 | UNREACHABLE(); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1296 | } |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 1297 | return ""; |
| 1298 | } |
| 1299 | |
| 1300 | int result = mkdir(dalvik_cache_root.c_str(), 0700); |
| 1301 | if (result != 0 && errno != EEXIST) { |
| 1302 | if (abort_on_error) { |
| 1303 | PLOG(FATAL) << "Failed to create dalvik-cache root directory " << dalvik_cache_root; |
| 1304 | UNREACHABLE(); |
| 1305 | } |
| 1306 | return ""; |
| 1307 | } |
| 1308 | |
| 1309 | result = mkdir(dalvik_cache.c_str(), 0700); |
| 1310 | if (result != 0) { |
| 1311 | if (abort_on_error) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1312 | PLOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 1313 | UNREACHABLE(); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1314 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1315 | return ""; |
| 1316 | } |
| 1317 | } |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1318 | return dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1319 | } |
| 1320 | |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 1321 | std::string GetDalvikCache(const char* subdir, const bool create_if_absent) { |
| 1322 | return GetDalvikCacheImpl(subdir, create_if_absent, false); |
| 1323 | } |
| 1324 | |
| 1325 | std::string GetDalvikCacheOrDie(const char* subdir, const bool create_if_absent) { |
| 1326 | return GetDalvikCacheImpl(subdir, create_if_absent, true); |
| 1327 | } |
| 1328 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1329 | bool GetDalvikCacheFilename(const char* location, const char* cache_location, |
| 1330 | std::string* filename, std::string* error_msg) { |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 1331 | if (location[0] != '/') { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1332 | *error_msg = StringPrintf("Expected path in location to be absolute: %s", location); |
| 1333 | return false; |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 1334 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1335 | std::string cache_file(&location[1]); // skip leading slash |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1336 | if (!EndsWith(location, ".dex") && !EndsWith(location, ".art") && !EndsWith(location, ".oat")) { |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 1337 | cache_file += "/"; |
| 1338 | cache_file += DexFile::kClassesDex; |
| 1339 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1340 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1341 | *filename = StringPrintf("%s/%s", cache_location, cache_file.c_str()); |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | std::string GetDalvikCacheFilenameOrDie(const char* location, const char* cache_location) { |
| 1346 | std::string ret; |
| 1347 | std::string error_msg; |
| 1348 | if (!GetDalvikCacheFilename(location, cache_location, &ret, &error_msg)) { |
| 1349 | LOG(FATAL) << error_msg; |
| 1350 | } |
| 1351 | return ret; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 1354 | static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) { |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 1355 | // in = /foo/bar/baz |
| 1356 | // out = /foo/bar/<isa>/baz |
| 1357 | size_t pos = filename->rfind('/'); |
| 1358 | CHECK_NE(pos, std::string::npos) << *filename << " " << isa; |
| 1359 | filename->insert(pos, "/", 1); |
| 1360 | filename->insert(pos + 1, GetInstructionSetString(isa)); |
| 1361 | } |
| 1362 | |
| 1363 | std::string GetSystemImageFilename(const char* location, const InstructionSet isa) { |
| 1364 | // location = /system/framework/boot.art |
| 1365 | // filename = /system/framework/<isa>/boot.art |
| 1366 | std::string filename(location); |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 1367 | InsertIsaDirectory(isa, &filename); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 1368 | return filename; |
| 1369 | } |
| 1370 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1371 | bool IsZipMagic(uint32_t magic) { |
| 1372 | return (('P' == ((magic >> 0) & 0xff)) && |
| 1373 | ('K' == ((magic >> 8) & 0xff))); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1376 | bool IsDexMagic(uint32_t magic) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1377 | return DexFile::IsMagicValid(reinterpret_cast<const uint8_t*>(&magic)); |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1380 | bool IsOatMagic(uint32_t magic) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1381 | return (memcmp(reinterpret_cast<const uint8_t*>(magic), |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1382 | OatHeader::kOatMagic, |
| 1383 | sizeof(OatHeader::kOatMagic)) == 0); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1386 | bool Exec(std::vector<std::string>& arg_vector, std::string* error_msg) { |
| 1387 | const std::string command_line(Join(arg_vector, ' ')); |
| 1388 | |
| 1389 | CHECK_GE(arg_vector.size(), 1U) << command_line; |
| 1390 | |
| 1391 | // Convert the args to char pointers. |
| 1392 | const char* program = arg_vector[0].c_str(); |
| 1393 | std::vector<char*> args; |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 1394 | for (size_t i = 0; i < arg_vector.size(); ++i) { |
| 1395 | const std::string& arg = arg_vector[i]; |
| 1396 | char* arg_str = const_cast<char*>(arg.c_str()); |
| 1397 | CHECK(arg_str != nullptr) << i; |
| 1398 | args.push_back(arg_str); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1399 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1400 | args.push_back(nullptr); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1401 | |
| 1402 | // fork and exec |
| 1403 | pid_t pid = fork(); |
| 1404 | if (pid == 0) { |
| 1405 | // no allocation allowed between fork and exec |
| 1406 | |
| 1407 | // change process groups, so we don't get reaped by ProcessManager |
| 1408 | setpgid(0, 0); |
| 1409 | |
| 1410 | execv(program, &args[0]); |
| 1411 | |
Brian Carlstrom | 13db9aa | 2014-02-27 12:44:32 -0800 | [diff] [blame] | 1412 | PLOG(ERROR) << "Failed to execv(" << command_line << ")"; |
| 1413 | exit(1); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1414 | } else { |
| 1415 | if (pid == -1) { |
| 1416 | *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s", |
| 1417 | command_line.c_str(), strerror(errno)); |
| 1418 | return false; |
| 1419 | } |
| 1420 | |
| 1421 | // wait for subprocess to finish |
| 1422 | int status; |
| 1423 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 1424 | if (got_pid != pid) { |
| 1425 | *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: " |
| 1426 | "wanted %d, got %d: %s", |
| 1427 | command_line.c_str(), pid, got_pid, strerror(errno)); |
| 1428 | return false; |
| 1429 | } |
| 1430 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1431 | *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status", |
| 1432 | command_line.c_str()); |
| 1433 | return false; |
| 1434 | } |
| 1435 | } |
| 1436 | return true; |
| 1437 | } |
| 1438 | |
Tong Shen | 547cdfd | 2014-08-05 01:54:19 -0700 | [diff] [blame] | 1439 | void EncodeUnsignedLeb128(uint32_t data, std::vector<uint8_t>* dst) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1440 | Leb128Encoder(dst).PushBackUnsigned(data); |
Tong Shen | 547cdfd | 2014-08-05 01:54:19 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | void EncodeSignedLeb128(int32_t data, std::vector<uint8_t>* dst) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1444 | Leb128Encoder(dst).PushBackSigned(data); |
Tong Shen | 547cdfd | 2014-08-05 01:54:19 -0700 | [diff] [blame] | 1445 | } |
| 1446 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 1447 | std::string PrettyDescriptor(Primitive::Type type) { |
| 1448 | return PrettyDescriptor(Primitive::Descriptor(type)); |
| 1449 | } |
| 1450 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 1451 | } // namespace art |