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" |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 33 | #include "dex_instruction.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 34 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 36 | #include "mirror/object-inl.h" |
| 37 | #include "mirror/object_array-inl.h" |
| 38 | #include "mirror/string.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 39 | #include "oat_quick_method_header.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 40 | #include "os.h" |
Kenny Root | 067d20f | 2014-03-05 14:57:21 -0800 | [diff] [blame] | 41 | #include "scoped_thread_state_change.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 42 | #include "utf-inl.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 43 | |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 44 | #if defined(__APPLE__) |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 45 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 46 | #include <sys/syscall.h> |
David Sehr | fa44200 | 2016-08-22 18:42:08 -0700 | [diff] [blame^] | 47 | #include <crt_externs.h> |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 48 | #endif |
| 49 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 50 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 51 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 52 | #endif |
| 53 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 54 | namespace art { |
| 55 | |
David Sehr | 1488ff8 | 2016-08-16 19:22:57 -0700 | [diff] [blame] | 56 | namespace { |
| 57 | #ifdef __APPLE__ |
| 58 | inline char** GetEnviron() { |
| 59 | // When Google Test is built as a framework on MacOS X, the environ variable |
| 60 | // is unavailable. Apple's documentation (man environ) recommends using |
| 61 | // _NSGetEnviron() instead. |
| 62 | return *_NSGetEnviron(); |
| 63 | } |
| 64 | #else |
| 65 | // Some POSIX platforms expect you to declare environ. extern "C" makes |
| 66 | // it reside in the global namespace. |
| 67 | extern "C" char** environ; |
| 68 | inline char** GetEnviron() { return environ; } |
| 69 | #endif |
| 70 | } // namespace |
| 71 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 72 | pid_t GetTid() { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 73 | #if defined(__APPLE__) |
| 74 | uint64_t owner; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 75 | 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] | 76 | return owner; |
Elliott Hughes | 323aa86 | 2014-08-20 15:00:04 -0700 | [diff] [blame] | 77 | #elif defined(__BIONIC__) |
| 78 | return gettid(); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 79 | #else |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 80 | return syscall(__NR_gettid); |
| 81 | #endif |
| 82 | } |
| 83 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 84 | std::string GetThreadName(pid_t tid) { |
| 85 | std::string result; |
| 86 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 87 | result.resize(result.size() - 1); // Lose the trailing '\n'. |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 88 | } else { |
| 89 | result = "<unknown>"; |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
Elliott Hughes | 6d3fc56 | 2014-08-27 11:47:01 -0700 | [diff] [blame] | 94 | 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] | 95 | #if defined(__APPLE__) |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 96 | *stack_size = pthread_get_stacksize_np(thread); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 97 | void* stack_addr = pthread_get_stackaddr_np(thread); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 98 | |
| 99 | // Check whether stack_addr is the base or end of the stack. |
| 100 | // (On Mac OS 10.7, it's the end.) |
| 101 | int stack_variable; |
| 102 | if (stack_addr > &stack_variable) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 103 | *stack_base = reinterpret_cast<uint8_t*>(stack_addr) - *stack_size; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 104 | } else { |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 105 | *stack_base = stack_addr; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 106 | } |
Elliott Hughes | 6d3fc56 | 2014-08-27 11:47:01 -0700 | [diff] [blame] | 107 | |
| 108 | // This is wrong, but there doesn't seem to be a way to get the actual value on the Mac. |
| 109 | pthread_attr_t attributes; |
| 110 | CHECK_PTHREAD_CALL(pthread_attr_init, (&attributes), __FUNCTION__); |
| 111 | CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__); |
| 112 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 113 | #else |
| 114 | pthread_attr_t attributes; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 115 | CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 116 | CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__); |
Elliott Hughes | 6d3fc56 | 2014-08-27 11:47:01 -0700 | [diff] [blame] | 117 | CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 118 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
Elliott Hughes | 839cc30 | 2014-08-28 10:24:44 -0700 | [diff] [blame] | 119 | |
| 120 | #if defined(__GLIBC__) |
| 121 | // If we're the main thread, check whether we were run with an unlimited stack. In that case, |
| 122 | // glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection |
| 123 | // will be broken because we'll die long before we get close to 2GB. |
| 124 | bool is_main_thread = (::art::GetTid() == getpid()); |
| 125 | if (is_main_thread) { |
| 126 | rlimit stack_limit; |
| 127 | if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) { |
| 128 | PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed"; |
| 129 | } |
| 130 | if (stack_limit.rlim_cur == RLIM_INFINITY) { |
| 131 | size_t old_stack_size = *stack_size; |
| 132 | |
| 133 | // Use the kernel default limit as our size, and adjust the base to match. |
| 134 | *stack_size = 8 * MB; |
| 135 | *stack_base = reinterpret_cast<uint8_t*>(*stack_base) + (old_stack_size - *stack_size); |
| 136 | |
| 137 | VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")" |
| 138 | << " to " << PrettySize(*stack_size) |
| 139 | << " with base " << *stack_base; |
| 140 | } |
| 141 | } |
| 142 | #endif |
| 143 | |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 144 | #endif |
| 145 | } |
| 146 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 147 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
Andreas Gampe | df87892 | 2015-08-13 16:44:54 -0700 | [diff] [blame] | 148 | File file(file_name, O_RDONLY, false); |
| 149 | if (!file.IsOpened()) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 150 | return false; |
| 151 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 152 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 153 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 154 | while (true) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 155 | 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] | 156 | if (n == -1) { |
| 157 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 158 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 159 | if (n == 0) { |
| 160 | return true; |
| 161 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 162 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 163 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 166 | bool PrintFileToLog(const std::string& file_name, LogSeverity level) { |
Andreas Gampe | df87892 | 2015-08-13 16:44:54 -0700 | [diff] [blame] | 167 | File file(file_name, O_RDONLY, false); |
| 168 | if (!file.IsOpened()) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 169 | return false; |
| 170 | } |
| 171 | |
| 172 | constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings. |
| 173 | char buf[kBufSize + 1]; // +1 for terminator. |
| 174 | size_t filled_to = 0; |
| 175 | while (true) { |
| 176 | DCHECK_LT(filled_to, kBufSize); |
| 177 | int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to)); |
| 178 | if (n <= 0) { |
| 179 | // Print the rest of the buffer, if it exists. |
| 180 | if (filled_to > 0) { |
| 181 | buf[filled_to] = 0; |
| 182 | LOG(level) << buf; |
| 183 | } |
| 184 | return n == 0; |
| 185 | } |
| 186 | // Scan for '\n'. |
| 187 | size_t i = filled_to; |
| 188 | bool found_newline = false; |
| 189 | for (; i < filled_to + n; ++i) { |
| 190 | if (buf[i] == '\n') { |
| 191 | // Found a line break, that's something to print now. |
| 192 | buf[i] = 0; |
| 193 | LOG(level) << buf; |
| 194 | // Copy the rest to the front. |
| 195 | if (i + 1 < filled_to + n) { |
| 196 | memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1); |
| 197 | filled_to = filled_to + n - i - 1; |
| 198 | } else { |
| 199 | filled_to = 0; |
| 200 | } |
| 201 | found_newline = true; |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | if (found_newline) { |
| 206 | continue; |
| 207 | } else { |
| 208 | filled_to += n; |
| 209 | // Check if we must flush now. |
| 210 | if (filled_to == kBufSize) { |
| 211 | buf[kBufSize] = 0; |
| 212 | LOG(level) << buf; |
| 213 | filled_to = 0; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 219 | std::string PrettyDescriptor(mirror::String* java_descriptor) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 220 | if (java_descriptor == nullptr) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 221 | return "null"; |
| 222 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 223 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8().c_str()); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 224 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 225 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 226 | std::string PrettyDescriptor(mirror::Class* klass) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 227 | if (klass == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 228 | return "null"; |
| 229 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 230 | std::string temp; |
| 231 | return PrettyDescriptor(klass->GetDescriptor(&temp)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 234 | std::string PrettyDescriptor(const char* descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 235 | // Count the number of '['s to get the dimensionality. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 236 | const char* c = descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 237 | size_t dim = 0; |
| 238 | while (*c == '[') { |
| 239 | dim++; |
| 240 | c++; |
| 241 | } |
| 242 | |
| 243 | // Reference or primitive? |
| 244 | if (*c == 'L') { |
| 245 | // "[[La/b/C;" -> "a.b.C[][]". |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 246 | c++; // Skip the 'L'. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 247 | } else { |
| 248 | // "[[B" -> "byte[][]". |
| 249 | // To make life easier, we make primitives look like unqualified |
| 250 | // reference types. |
| 251 | switch (*c) { |
| 252 | case 'B': c = "byte;"; break; |
| 253 | case 'C': c = "char;"; break; |
| 254 | case 'D': c = "double;"; break; |
| 255 | case 'F': c = "float;"; break; |
| 256 | case 'I': c = "int;"; break; |
| 257 | case 'J': c = "long;"; break; |
| 258 | case 'S': c = "short;"; break; |
| 259 | case 'Z': c = "boolean;"; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 260 | case 'V': c = "void;"; break; // Used when decoding return types. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 261 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 266 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 267 | std::string result; |
| 268 | const char* p = c; |
| 269 | while (*p != ';') { |
| 270 | char ch = *p++; |
| 271 | if (ch == '/') { |
| 272 | ch = '.'; |
| 273 | } |
| 274 | result.push_back(ch); |
| 275 | } |
| 276 | // ...and replace the semicolon with 'dim' "[]" pairs: |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 277 | for (size_t i = 0; i < dim; ++i) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 278 | result += "[]"; |
| 279 | } |
| 280 | return result; |
| 281 | } |
| 282 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 283 | std::string PrettyField(ArtField* f, bool with_type) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 284 | if (f == nullptr) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 285 | return "null"; |
| 286 | } |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 287 | std::string result; |
| 288 | if (with_type) { |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 289 | result += PrettyDescriptor(f->GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 290 | result += ' '; |
| 291 | } |
Ian Rogers | 08f1f50 | 2014-12-02 15:04:37 -0800 | [diff] [blame] | 292 | std::string temp; |
| 293 | result += PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor(&temp)); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 294 | result += '.'; |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 295 | result += f->GetName(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 296 | return result; |
| 297 | } |
| 298 | |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 299 | 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] | 300 | if (field_idx >= dex_file.NumFieldIds()) { |
| 301 | return StringPrintf("<<invalid-field-idx-%d>>", field_idx); |
| 302 | } |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 303 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 304 | std::string result; |
| 305 | if (with_type) { |
| 306 | result += dex_file.GetFieldTypeDescriptor(field_id); |
| 307 | result += ' '; |
| 308 | } |
| 309 | result += PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(field_id)); |
| 310 | result += '.'; |
| 311 | result += dex_file.GetFieldName(field_id); |
| 312 | return result; |
| 313 | } |
| 314 | |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 315 | std::string PrettyType(uint32_t type_idx, const DexFile& dex_file) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 316 | if (type_idx >= dex_file.NumTypeIds()) { |
| 317 | return StringPrintf("<<invalid-type-idx-%d>>", type_idx); |
| 318 | } |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 319 | const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx); |
Mathieu Chartier | 4c70d77 | 2012-09-10 14:08:32 -0700 | [diff] [blame] | 320 | return PrettyDescriptor(dex_file.GetTypeDescriptor(type_id)); |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 323 | std::string PrettyArguments(const char* signature) { |
| 324 | std::string result; |
| 325 | result += '('; |
| 326 | CHECK_EQ(*signature, '('); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 327 | ++signature; // Skip the '('. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 328 | while (*signature != ')') { |
| 329 | size_t argument_length = 0; |
| 330 | while (signature[argument_length] == '[') { |
| 331 | ++argument_length; |
| 332 | } |
| 333 | if (signature[argument_length] == 'L') { |
| 334 | argument_length = (strchr(signature, ';') - signature + 1); |
| 335 | } else { |
| 336 | ++argument_length; |
| 337 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 338 | { |
| 339 | std::string argument_descriptor(signature, argument_length); |
| 340 | result += PrettyDescriptor(argument_descriptor.c_str()); |
| 341 | } |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 342 | if (signature[argument_length] != ')') { |
| 343 | result += ", "; |
| 344 | } |
| 345 | signature += argument_length; |
| 346 | } |
| 347 | CHECK_EQ(*signature, ')'); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 348 | ++signature; // Skip the ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 349 | result += ')'; |
| 350 | return result; |
| 351 | } |
| 352 | |
| 353 | std::string PrettyReturnType(const char* signature) { |
| 354 | const char* return_type = strchr(signature, ')'); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 355 | CHECK(return_type != nullptr); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 356 | ++return_type; // Skip ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 357 | return PrettyDescriptor(return_type); |
| 358 | } |
| 359 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 360 | std::string PrettyMethod(ArtMethod* m, bool with_signature) { |
Ian Rogers | 16ce092 | 2014-01-10 14:59:36 -0800 | [diff] [blame] | 361 | if (m == nullptr) { |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 362 | return "null"; |
| 363 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 364 | if (!m->IsRuntimeMethod()) { |
| 365 | m = m->GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize()); |
| 366 | } |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 367 | std::string result(PrettyDescriptor(m->GetDeclaringClassDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 368 | result += '.'; |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 369 | result += m->GetName(); |
Ian Rogers | 16ce092 | 2014-01-10 14:59:36 -0800 | [diff] [blame] | 370 | if (UNLIKELY(m->IsFastNative())) { |
| 371 | result += "!"; |
| 372 | } |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 373 | if (with_signature) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 374 | const Signature signature = m->GetSignature(); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 375 | std::string sig_as_string(signature.ToString()); |
| 376 | if (signature == Signature::NoSignature()) { |
| 377 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 378 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 379 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 380 | PrettyArguments(sig_as_string.c_str()); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 381 | } |
| 382 | return result; |
| 383 | } |
| 384 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 385 | 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] | 386 | if (method_idx >= dex_file.NumMethodIds()) { |
| 387 | return StringPrintf("<<invalid-method-idx-%d>>", method_idx); |
| 388 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 389 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 390 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 391 | result += '.'; |
| 392 | result += dex_file.GetMethodName(method_id); |
| 393 | if (with_signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 394 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 395 | std::string sig_as_string(signature.ToString()); |
| 396 | if (signature == Signature::NoSignature()) { |
| 397 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 398 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 399 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 400 | PrettyArguments(sig_as_string.c_str()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 401 | } |
| 402 | return result; |
| 403 | } |
| 404 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 405 | std::string PrettyTypeOf(mirror::Object* obj) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 406 | if (obj == nullptr) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 407 | return "null"; |
| 408 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 409 | if (obj->GetClass() == nullptr) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 410 | return "(raw)"; |
| 411 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 412 | std::string temp; |
| 413 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor(&temp))); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 414 | if (obj->IsClass()) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 415 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor(&temp)) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 416 | } |
| 417 | return result; |
| 418 | } |
| 419 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 420 | std::string PrettyClass(mirror::Class* c) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 421 | if (c == nullptr) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 422 | return "null"; |
| 423 | } |
| 424 | std::string result; |
| 425 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 426 | result += PrettyDescriptor(c); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 427 | result += ">"; |
| 428 | return result; |
| 429 | } |
| 430 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 431 | std::string PrettyClassAndClassLoader(mirror::Class* c) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 432 | if (c == nullptr) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 433 | return "null"; |
| 434 | } |
| 435 | std::string result; |
| 436 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 437 | result += PrettyDescriptor(c); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 438 | result += ","; |
| 439 | result += PrettyTypeOf(c->GetClassLoader()); |
| 440 | // TODO: add an identifying hash value for the loader |
| 441 | result += ">"; |
| 442 | return result; |
| 443 | } |
| 444 | |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 445 | std::string PrettyJavaAccessFlags(uint32_t access_flags) { |
| 446 | std::string result; |
| 447 | if ((access_flags & kAccPublic) != 0) { |
| 448 | result += "public "; |
| 449 | } |
| 450 | if ((access_flags & kAccProtected) != 0) { |
| 451 | result += "protected "; |
| 452 | } |
| 453 | if ((access_flags & kAccPrivate) != 0) { |
| 454 | result += "private "; |
| 455 | } |
| 456 | if ((access_flags & kAccFinal) != 0) { |
| 457 | result += "final "; |
| 458 | } |
| 459 | if ((access_flags & kAccStatic) != 0) { |
| 460 | result += "static "; |
| 461 | } |
| 462 | if ((access_flags & kAccTransient) != 0) { |
| 463 | result += "transient "; |
| 464 | } |
| 465 | if ((access_flags & kAccVolatile) != 0) { |
| 466 | result += "volatile "; |
| 467 | } |
| 468 | if ((access_flags & kAccSynchronized) != 0) { |
| 469 | result += "synchronized "; |
| 470 | } |
| 471 | return result; |
| 472 | } |
| 473 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 474 | std::string PrettySize(int64_t byte_count) { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 475 | // The byte thresholds at which we display amounts. A byte count is displayed |
| 476 | // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 477 | static const int64_t kUnitThresholds[] = { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 478 | 0, // B up to... |
| 479 | 3*1024, // KB up to... |
| 480 | 2*1024*1024, // MB up to... |
| 481 | 1024*1024*1024 // GB from here. |
| 482 | }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 483 | static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB }; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 484 | static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 485 | const char* negative_str = ""; |
| 486 | if (byte_count < 0) { |
| 487 | negative_str = "-"; |
| 488 | byte_count = -byte_count; |
| 489 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 490 | int i = arraysize(kUnitThresholds); |
| 491 | while (--i > 0) { |
| 492 | if (byte_count >= kUnitThresholds[i]) { |
| 493 | break; |
| 494 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 495 | } |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 496 | return StringPrintf("%s%" PRId64 "%s", |
| 497 | negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 500 | std::string PrintableChar(uint16_t ch) { |
| 501 | std::string result; |
| 502 | result += '\''; |
| 503 | if (NeedsEscaping(ch)) { |
| 504 | StringAppendF(&result, "\\u%04x", ch); |
| 505 | } else { |
| 506 | result += ch; |
| 507 | } |
| 508 | result += '\''; |
| 509 | return result; |
| 510 | } |
| 511 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 512 | std::string PrintableString(const char* utf) { |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 513 | std::string result; |
| 514 | result += '"'; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 515 | const char* p = utf; |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 516 | size_t char_count = CountModifiedUtf8Chars(p); |
| 517 | for (size_t i = 0; i < char_count; ++i) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 518 | uint32_t ch = GetUtf16FromUtf8(&p); |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 519 | if (ch == '\\') { |
| 520 | result += "\\\\"; |
| 521 | } else if (ch == '\n') { |
| 522 | result += "\\n"; |
| 523 | } else if (ch == '\r') { |
| 524 | result += "\\r"; |
| 525 | } else if (ch == '\t') { |
| 526 | result += "\\t"; |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 527 | } else { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 528 | const uint16_t leading = GetLeadingUtf16Char(ch); |
| 529 | |
| 530 | if (NeedsEscaping(leading)) { |
| 531 | StringAppendF(&result, "\\u%04x", leading); |
| 532 | } else { |
| 533 | result += leading; |
| 534 | } |
| 535 | |
| 536 | const uint32_t trailing = GetTrailingUtf16Char(ch); |
| 537 | if (trailing != 0) { |
| 538 | // All high surrogates will need escaping. |
| 539 | StringAppendF(&result, "\\u%04x", trailing); |
| 540 | } |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | result += '"'; |
| 544 | return result; |
| 545 | } |
| 546 | |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 547 | // 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] | 548 | std::string MangleForJni(const std::string& s) { |
| 549 | std::string result; |
| 550 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 551 | const char* cp = &s[0]; |
| 552 | for (size_t i = 0; i < char_count; ++i) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 553 | uint32_t ch = GetUtf16FromUtf8(&cp); |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 554 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) { |
| 555 | result.push_back(ch); |
| 556 | } else if (ch == '.' || ch == '/') { |
| 557 | result += "_"; |
| 558 | } else if (ch == '_') { |
| 559 | result += "_1"; |
| 560 | } else if (ch == ';') { |
| 561 | result += "_2"; |
| 562 | } else if (ch == '[') { |
| 563 | result += "_3"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 564 | } else { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 565 | const uint16_t leading = GetLeadingUtf16Char(ch); |
| 566 | const uint32_t trailing = GetTrailingUtf16Char(ch); |
| 567 | |
| 568 | StringAppendF(&result, "_0%04x", leading); |
| 569 | if (trailing != 0) { |
| 570 | StringAppendF(&result, "_0%04x", trailing); |
| 571 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | return result; |
| 575 | } |
| 576 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 577 | std::string DotToDescriptor(const char* class_name) { |
| 578 | std::string descriptor(class_name); |
| 579 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 580 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 581 | descriptor = "L" + descriptor + ";"; |
| 582 | } |
| 583 | return descriptor; |
| 584 | } |
| 585 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 586 | std::string DescriptorToDot(const char* descriptor) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 587 | size_t length = strlen(descriptor); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 588 | if (length > 1) { |
| 589 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 590 | // Descriptors have the leading 'L' and trailing ';' stripped. |
| 591 | std::string result(descriptor + 1, length - 2); |
| 592 | std::replace(result.begin(), result.end(), '/', '.'); |
| 593 | return result; |
| 594 | } else { |
| 595 | // For arrays the 'L' and ';' remain intact. |
| 596 | std::string result(descriptor); |
| 597 | std::replace(result.begin(), result.end(), '/', '.'); |
| 598 | return result; |
| 599 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 600 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 601 | // Do nothing for non-class/array descriptors. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 602 | return descriptor; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | std::string DescriptorToName(const char* descriptor) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 606 | size_t length = strlen(descriptor); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 607 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 608 | std::string result(descriptor + 1, length - 2); |
| 609 | return result; |
| 610 | } |
| 611 | return descriptor; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 614 | std::string JniShortName(ArtMethod* m) { |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 615 | std::string class_name(m->GetDeclaringClassDescriptor()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 616 | // Remove the leading 'L' and trailing ';'... |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 617 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 618 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 619 | class_name.erase(0, 1); |
| 620 | class_name.erase(class_name.size() - 1, 1); |
| 621 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 622 | std::string method_name(m->GetName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 623 | |
| 624 | std::string short_name; |
| 625 | short_name += "Java_"; |
| 626 | short_name += MangleForJni(class_name); |
| 627 | short_name += "_"; |
| 628 | short_name += MangleForJni(method_name); |
| 629 | return short_name; |
| 630 | } |
| 631 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 632 | std::string JniLongName(ArtMethod* m) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 633 | std::string long_name; |
| 634 | long_name += JniShortName(m); |
| 635 | long_name += "__"; |
| 636 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 637 | std::string signature(m->GetSignature().ToString()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 638 | signature.erase(0, 1); |
| 639 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 640 | |
| 641 | long_name += MangleForJni(signature); |
| 642 | |
| 643 | return long_name; |
| 644 | } |
| 645 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 646 | // Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 647 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 648 | 0x00000000, // 00..1f low control characters; nothing valid |
| 649 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 650 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 651 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 652 | }; |
| 653 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 654 | // Helper for IsValidPartOfMemberNameUtf8(); do not call directly. |
| 655 | bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 656 | /* |
| 657 | * It's a multibyte encoded character. Decode it and analyze. We |
| 658 | * accept anything that isn't (a) an improperly encoded low value, |
| 659 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 660 | * control character, or (e) a high space, layout, or special |
| 661 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 662 | * U+fff0..U+ffff). This is all specified in the dex format |
| 663 | * document. |
| 664 | */ |
| 665 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 666 | const uint32_t pair = GetUtf16FromUtf8(pUtf8Ptr); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 667 | const uint16_t leading = GetLeadingUtf16Char(pair); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 668 | |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 669 | // We have a surrogate pair resulting from a valid 4 byte UTF sequence. |
| 670 | // No further checks are necessary because 4 byte sequences span code |
| 671 | // points [U+10000, U+1FFFFF], which are valid codepoints in a dex |
| 672 | // identifier. Furthermore, GetUtf16FromUtf8 guarantees that each of |
| 673 | // the surrogate halves are valid and well formed in this instance. |
| 674 | if (GetTrailingUtf16Char(pair) != 0) { |
| 675 | return true; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | // We've encountered a one, two or three byte UTF-8 sequence. The |
| 680 | // three byte UTF-8 sequence could be one half of a surrogate pair. |
| 681 | switch (leading >> 8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 682 | case 0x00: |
| 683 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 684 | return (leading > 0x00a0); |
| 685 | case 0xd8: |
| 686 | case 0xd9: |
| 687 | case 0xda: |
| 688 | case 0xdb: |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 689 | { |
| 690 | // We found a three byte sequence encoding one half of a surrogate. |
| 691 | // Look for the other half. |
| 692 | const uint32_t pair2 = GetUtf16FromUtf8(pUtf8Ptr); |
| 693 | const uint16_t trailing = GetLeadingUtf16Char(pair2); |
| 694 | |
| 695 | return (GetTrailingUtf16Char(pair2) == 0) && (0xdc00 <= trailing && trailing <= 0xdfff); |
| 696 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 697 | case 0xdc: |
| 698 | case 0xdd: |
| 699 | case 0xde: |
| 700 | case 0xdf: |
| 701 | // It's a trailing surrogate, which is not valid at this point. |
| 702 | return false; |
| 703 | case 0x20: |
| 704 | case 0xff: |
| 705 | // It's in the range that has spaces, controls, and specials. |
| 706 | switch (leading & 0xfff8) { |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 707 | case 0x2000: |
| 708 | case 0x2008: |
| 709 | case 0x2028: |
| 710 | case 0xfff0: |
| 711 | case 0xfff8: |
| 712 | return false; |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 713 | } |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 714 | return true; |
| 715 | default: |
| 716 | return true; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 717 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 718 | |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 719 | UNREACHABLE(); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 723 | * valid as part of a member name, updating the pointer to point past |
| 724 | * the consumed character. This will consume two encoded UTF-16 code |
| 725 | * points if the character is encoded as a surrogate pair. Also, if |
| 726 | * this function returns false, then the given pointer may only have |
| 727 | * been partially advanced. |
| 728 | */ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 729 | static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 730 | uint8_t c = (uint8_t) **pUtf8Ptr; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 731 | if (LIKELY(c <= 0x7f)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 732 | // It's low-ascii, so check the table. |
| 733 | uint32_t wordIdx = c >> 5; |
| 734 | uint32_t bitIdx = c & 0x1f; |
| 735 | (*pUtf8Ptr)++; |
| 736 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 737 | } |
| 738 | |
| 739 | // It's a multibyte encoded character. Call a non-inline function |
| 740 | // for the heavy lifting. |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 741 | return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr); |
| 742 | } |
| 743 | |
| 744 | bool IsValidMemberName(const char* s) { |
| 745 | bool angle_name = false; |
| 746 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 747 | switch (*s) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 748 | case '\0': |
| 749 | // The empty string is not a valid name. |
| 750 | return false; |
| 751 | case '<': |
| 752 | angle_name = true; |
| 753 | s++; |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | while (true) { |
| 758 | switch (*s) { |
| 759 | case '\0': |
| 760 | return !angle_name; |
| 761 | case '>': |
| 762 | return angle_name && s[1] == '\0'; |
| 763 | } |
| 764 | |
| 765 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
| 766 | return false; |
| 767 | } |
| 768 | } |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 771 | enum ClassNameType { kName, kDescriptor }; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 772 | template<ClassNameType kType, char kSeparator> |
| 773 | static bool IsValidClassName(const char* s) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 774 | int arrayCount = 0; |
| 775 | while (*s == '[') { |
| 776 | arrayCount++; |
| 777 | s++; |
| 778 | } |
| 779 | |
| 780 | if (arrayCount > 255) { |
| 781 | // Arrays may have no more than 255 dimensions. |
| 782 | return false; |
| 783 | } |
| 784 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 785 | ClassNameType type = kType; |
| 786 | if (type != kDescriptor && arrayCount != 0) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 787 | /* |
| 788 | * If we're looking at an array of some sort, then it doesn't |
| 789 | * matter if what is being asked for is a class name; the |
| 790 | * format looks the same as a type descriptor in that case, so |
| 791 | * treat it as such. |
| 792 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 793 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 796 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 797 | /* |
| 798 | * We are looking for a descriptor. Either validate it as a |
| 799 | * single-character primitive type, or continue on to check the |
| 800 | * embedded class name (bracketed by "L" and ";"). |
| 801 | */ |
| 802 | switch (*(s++)) { |
| 803 | case 'B': |
| 804 | case 'C': |
| 805 | case 'D': |
| 806 | case 'F': |
| 807 | case 'I': |
| 808 | case 'J': |
| 809 | case 'S': |
| 810 | case 'Z': |
| 811 | // These are all single-character descriptors for primitive types. |
| 812 | return (*s == '\0'); |
| 813 | case 'V': |
| 814 | // Non-array void is valid, but you can't have an array of void. |
| 815 | return (arrayCount == 0) && (*s == '\0'); |
| 816 | case 'L': |
| 817 | // Class name: Break out and continue below. |
| 818 | break; |
| 819 | default: |
| 820 | // Oddball descriptor character. |
| 821 | return false; |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | /* |
| 826 | * We just consumed the 'L' that introduces a class name as part |
| 827 | * of a type descriptor, or we are looking for an unadorned class |
| 828 | * name. |
| 829 | */ |
| 830 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 831 | bool sepOrFirst = true; // first character or just encountered a separator. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 832 | for (;;) { |
| 833 | uint8_t c = (uint8_t) *s; |
| 834 | switch (c) { |
| 835 | case '\0': |
| 836 | /* |
| 837 | * Premature end for a type descriptor, but valid for |
| 838 | * a class name as long as we haven't encountered an |
| 839 | * empty component (including the degenerate case of |
| 840 | * the empty string ""). |
| 841 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 842 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 843 | case ';': |
| 844 | /* |
| 845 | * Invalid character for a class name, but the |
| 846 | * legitimate end of a type descriptor. In the latter |
| 847 | * case, make sure that this is the end of the string |
| 848 | * and that it doesn't end with an empty component |
| 849 | * (including the degenerate case of "L;"). |
| 850 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 851 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 852 | case '/': |
| 853 | case '.': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 854 | if (c != kSeparator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 855 | // The wrong separator character. |
| 856 | return false; |
| 857 | } |
| 858 | if (sepOrFirst) { |
| 859 | // Separator at start or two separators in a row. |
| 860 | return false; |
| 861 | } |
| 862 | sepOrFirst = true; |
| 863 | s++; |
| 864 | break; |
| 865 | default: |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 866 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 867 | return false; |
| 868 | } |
| 869 | sepOrFirst = false; |
| 870 | break; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 875 | bool IsValidBinaryClassName(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 876 | return IsValidClassName<kName, '.'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | bool IsValidJniClassName(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 880 | return IsValidClassName<kName, '/'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | bool IsValidDescriptor(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 884 | return IsValidClassName<kDescriptor, '/'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 887 | 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] | 888 | const char* p = s.data(); |
| 889 | const char* end = p + s.size(); |
| 890 | while (p != end) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 891 | if (*p == separator) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 892 | ++p; |
| 893 | } else { |
| 894 | const char* start = p; |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 895 | while (++p != end && *p != separator) { |
| 896 | // Skip to the next occurrence of the separator. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 897 | } |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 898 | result->push_back(std::string(start, p - start)); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
| 902 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 903 | std::string Trim(const std::string& s) { |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 904 | std::string result; |
| 905 | unsigned int start_index = 0; |
| 906 | unsigned int end_index = s.size() - 1; |
| 907 | |
| 908 | // Skip initial whitespace. |
| 909 | while (start_index < s.size()) { |
| 910 | if (!isspace(s[start_index])) { |
| 911 | break; |
| 912 | } |
| 913 | start_index++; |
| 914 | } |
| 915 | |
| 916 | // Skip terminating whitespace. |
| 917 | while (end_index >= start_index) { |
| 918 | if (!isspace(s[end_index])) { |
| 919 | break; |
| 920 | } |
| 921 | end_index--; |
| 922 | } |
| 923 | |
| 924 | // All spaces, no beef. |
| 925 | if (end_index < start_index) { |
| 926 | return ""; |
| 927 | } |
| 928 | // Start_index is the first non-space, end_index is the last one. |
| 929 | return s.substr(start_index, end_index - start_index + 1); |
| 930 | } |
| 931 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 932 | template <typename StringT> |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 933 | std::string Join(const std::vector<StringT>& strings, char separator) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 934 | if (strings.empty()) { |
| 935 | return ""; |
| 936 | } |
| 937 | |
| 938 | std::string result(strings[0]); |
| 939 | for (size_t i = 1; i < strings.size(); ++i) { |
| 940 | result += separator; |
| 941 | result += strings[i]; |
| 942 | } |
| 943 | return result; |
| 944 | } |
| 945 | |
| 946 | // Explicit instantiations. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 947 | template std::string Join<std::string>(const std::vector<std::string>& strings, char separator); |
| 948 | 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] | 949 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 950 | bool StartsWith(const std::string& s, const char* prefix) { |
| 951 | return s.compare(0, strlen(prefix), prefix) == 0; |
| 952 | } |
| 953 | |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 954 | bool EndsWith(const std::string& s, const char* suffix) { |
| 955 | size_t suffix_length = strlen(suffix); |
| 956 | size_t string_length = s.size(); |
| 957 | if (suffix_length > string_length) { |
| 958 | return false; |
| 959 | } |
| 960 | size_t offset = string_length - suffix_length; |
| 961 | return s.compare(offset, suffix_length, suffix) == 0; |
| 962 | } |
| 963 | |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 964 | void SetThreadName(const char* thread_name) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 965 | int hasAt = 0; |
| 966 | int hasDot = 0; |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 967 | const char* s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 968 | while (*s) { |
| 969 | if (*s == '.') { |
| 970 | hasDot = 1; |
| 971 | } else if (*s == '@') { |
| 972 | hasAt = 1; |
| 973 | } |
| 974 | s++; |
| 975 | } |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 976 | int len = s - thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 977 | if (len < 15 || hasAt || !hasDot) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 978 | s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 979 | } else { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 980 | s = thread_name + len - 15; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 981 | } |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 982 | #if defined(__linux__) |
Elliott Hughes | 7c6a61e | 2012-03-12 18:01:41 -0700 | [diff] [blame] | 983 | // pthread_setname_np fails rather than truncating long strings. |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 984 | 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] | 985 | strncpy(buf, s, sizeof(buf)-1); |
| 986 | buf[sizeof(buf)-1] = '\0'; |
| 987 | errno = pthread_setname_np(pthread_self(), buf); |
| 988 | if (errno != 0) { |
| 989 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 990 | } |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 991 | #else // __APPLE__ |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 992 | pthread_setname_np(thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 993 | #endif |
| 994 | } |
| 995 | |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 996 | void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) { |
| 997 | *utime = *stime = *task_cpu = 0; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 998 | std::string stats; |
Elliott Hughes | 8a31b50 | 2012-04-30 19:36:11 -0700 | [diff] [blame] | 999 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1000 | return; |
| 1001 | } |
| 1002 | // Skip the command, which may contain spaces. |
| 1003 | stats = stats.substr(stats.find(')') + 2); |
| 1004 | // Extract the three fields we care about. |
| 1005 | std::vector<std::string> fields; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1006 | Split(stats, ' ', &fields); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 1007 | *state = fields[0][0]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1008 | *utime = strtoull(fields[11].c_str(), nullptr, 10); |
| 1009 | *stime = strtoull(fields[12].c_str(), nullptr, 10); |
| 1010 | *task_cpu = strtoull(fields[36].c_str(), nullptr, 10); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1013 | std::string GetSchedulerGroupName(pid_t tid) { |
| 1014 | // /proc/<pid>/cgroup looks like this: |
| 1015 | // 2:devices:/ |
| 1016 | // 1:cpuacct,cpu:/ |
| 1017 | // We want the third field from the line whose second field contains the "cpu" token. |
| 1018 | std::string cgroup_file; |
| 1019 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) { |
| 1020 | return ""; |
| 1021 | } |
| 1022 | std::vector<std::string> cgroup_lines; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1023 | Split(cgroup_file, '\n', &cgroup_lines); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1024 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 1025 | std::vector<std::string> cgroup_fields; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1026 | Split(cgroup_lines[i], ':', &cgroup_fields); |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1027 | std::vector<std::string> cgroups; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 1028 | Split(cgroup_fields[1], ',', &cgroups); |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1029 | for (size_t j = 0; j < cgroups.size(); ++j) { |
| 1030 | if (cgroups[j] == "cpu") { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1031 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | return ""; |
| 1036 | } |
| 1037 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1038 | const char* GetAndroidRoot() { |
| 1039 | const char* android_root = getenv("ANDROID_ROOT"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1040 | if (android_root == nullptr) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1041 | if (OS::DirectoryExists("/system")) { |
| 1042 | android_root = "/system"; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1043 | } else { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1044 | LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist"; |
| 1045 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1046 | } |
| 1047 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1048 | if (!OS::DirectoryExists(android_root)) { |
| 1049 | LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1050 | return ""; |
| 1051 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1052 | return android_root; |
| 1053 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1054 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1055 | const char* GetAndroidData() { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1056 | std::string error_msg; |
| 1057 | const char* dir = GetAndroidDataSafe(&error_msg); |
| 1058 | if (dir != nullptr) { |
| 1059 | return dir; |
| 1060 | } else { |
| 1061 | LOG(FATAL) << error_msg; |
| 1062 | return ""; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | const char* GetAndroidDataSafe(std::string* error_msg) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1067 | const char* android_data = getenv("ANDROID_DATA"); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1068 | if (android_data == nullptr) { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1069 | if (OS::DirectoryExists("/data")) { |
| 1070 | android_data = "/data"; |
| 1071 | } else { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1072 | *error_msg = "ANDROID_DATA not set and /data does not exist"; |
| 1073 | return nullptr; |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | if (!OS::DirectoryExists(android_data)) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1077 | *error_msg = StringPrintf("Failed to find ANDROID_DATA directory %s", android_data); |
| 1078 | return nullptr; |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1079 | } |
| 1080 | return android_data; |
| 1081 | } |
| 1082 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1083 | 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] | 1084 | bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1085 | CHECK(subdir != nullptr); |
| 1086 | std::string error_msg; |
| 1087 | const char* android_data = GetAndroidDataSafe(&error_msg); |
| 1088 | if (android_data == nullptr) { |
| 1089 | *have_android_data = false; |
| 1090 | *dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1091 | *is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1092 | return; |
| 1093 | } else { |
| 1094 | *have_android_data = true; |
| 1095 | } |
| 1096 | const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data)); |
| 1097 | *dalvik_cache = dalvik_cache_root + subdir; |
| 1098 | *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str()); |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 1099 | *is_global_cache = strcmp(android_data, "/data") == 0; |
| 1100 | if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1101 | // Don't create the system's /data/dalvik-cache/... because it needs special permissions. |
| 1102 | *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) && |
| 1103 | (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST)); |
| 1104 | } |
| 1105 | } |
| 1106 | |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 1107 | std::string GetDalvikCache(const char* subdir) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1108 | CHECK(subdir != nullptr); |
Brian Carlstrom | 41ccffd | 2014-05-06 10:37:30 -0700 | [diff] [blame] | 1109 | const char* android_data = GetAndroidData(); |
| 1110 | const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data)); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1111 | const std::string dalvik_cache = dalvik_cache_root + subdir; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 1112 | if (!OS::DirectoryExists(dalvik_cache.c_str())) { |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 1113 | // TODO: Check callers. Traditional behavior is to not abort. |
| 1114 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1115 | } |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1116 | return dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1119 | bool GetDalvikCacheFilename(const char* location, const char* cache_location, |
| 1120 | std::string* filename, std::string* error_msg) { |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 1121 | if (location[0] != '/') { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1122 | *error_msg = StringPrintf("Expected path in location to be absolute: %s", location); |
| 1123 | return false; |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 1124 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1125 | std::string cache_file(&location[1]); // skip leading slash |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 1126 | if (!EndsWith(location, ".dex") && !EndsWith(location, ".art") && !EndsWith(location, ".oat")) { |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 1127 | cache_file += "/"; |
| 1128 | cache_file += DexFile::kClassesDex; |
| 1129 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1130 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 1131 | *filename = StringPrintf("%s/%s", cache_location, cache_file.c_str()); |
| 1132 | return true; |
| 1133 | } |
| 1134 | |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 1135 | static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) { |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 1136 | // in = /foo/bar/baz |
| 1137 | // out = /foo/bar/<isa>/baz |
| 1138 | size_t pos = filename->rfind('/'); |
| 1139 | CHECK_NE(pos, std::string::npos) << *filename << " " << isa; |
| 1140 | filename->insert(pos, "/", 1); |
| 1141 | filename->insert(pos + 1, GetInstructionSetString(isa)); |
| 1142 | } |
| 1143 | |
| 1144 | std::string GetSystemImageFilename(const char* location, const InstructionSet isa) { |
| 1145 | // location = /system/framework/boot.art |
| 1146 | // filename = /system/framework/<isa>/boot.art |
| 1147 | std::string filename(location); |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 1148 | InsertIsaDirectory(isa, &filename); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 1149 | return filename; |
| 1150 | } |
| 1151 | |
David Sehr | 1488ff8 | 2016-08-16 19:22:57 -0700 | [diff] [blame] | 1152 | const EnvSnapshot* TakeEnvSnapshot() { |
| 1153 | EnvSnapshot* snapshot = new EnvSnapshot(); |
| 1154 | char** env = GetEnviron(); |
| 1155 | for (size_t i = 0; env[i] != nullptr; ++i) { |
| 1156 | snapshot->name_value_pairs_.emplace_back(new std::string(env[i])); |
| 1157 | } |
| 1158 | return snapshot; |
| 1159 | } |
| 1160 | |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1161 | int ExecAndReturnCode(std::vector<std::string>& arg_vector, std::string* error_msg) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1162 | const std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1163 | CHECK_GE(arg_vector.size(), 1U) << command_line; |
| 1164 | |
| 1165 | // Convert the args to char pointers. |
| 1166 | const char* program = arg_vector[0].c_str(); |
| 1167 | std::vector<char*> args; |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 1168 | for (size_t i = 0; i < arg_vector.size(); ++i) { |
| 1169 | const std::string& arg = arg_vector[i]; |
| 1170 | char* arg_str = const_cast<char*>(arg.c_str()); |
| 1171 | CHECK(arg_str != nullptr) << i; |
| 1172 | args.push_back(arg_str); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1173 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1174 | args.push_back(nullptr); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1175 | |
| 1176 | // fork and exec |
| 1177 | pid_t pid = fork(); |
| 1178 | if (pid == 0) { |
| 1179 | // no allocation allowed between fork and exec |
| 1180 | |
| 1181 | // change process groups, so we don't get reaped by ProcessManager |
| 1182 | setpgid(0, 0); |
| 1183 | |
David Sehr | 1488ff8 | 2016-08-16 19:22:57 -0700 | [diff] [blame] | 1184 | // The child inherits the environment unless the caller overrides it. |
| 1185 | if (Runtime::Current() == nullptr || Runtime::Current()->GetEnvSnapshot() == nullptr) { |
| 1186 | execv(program, &args[0]); |
| 1187 | } else { |
| 1188 | const EnvSnapshot* saved_snapshot = Runtime::Current()->GetEnvSnapshot(); |
| 1189 | // Allocation between fork and exec is not well-behaved. Use a variable-length array instead. |
| 1190 | char* envp[saved_snapshot->name_value_pairs_.size() + 1]; |
| 1191 | for (size_t i = 0; i < saved_snapshot->name_value_pairs_.size(); ++i) { |
| 1192 | envp[i] = const_cast<char*>(saved_snapshot->name_value_pairs_[i]->c_str()); |
| 1193 | } |
| 1194 | envp[saved_snapshot->name_value_pairs_.size()] = nullptr; |
| 1195 | execve(program, &args[0], envp); |
| 1196 | } |
| 1197 | PLOG(ERROR) << "Failed to execve(" << command_line << ")"; |
Tobias Lindskog | ae35c37 | 2015-11-04 19:41:21 +0100 | [diff] [blame] | 1198 | // _exit to avoid atexit handlers in child. |
| 1199 | _exit(1); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1200 | } else { |
| 1201 | if (pid == -1) { |
| 1202 | *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s", |
| 1203 | command_line.c_str(), strerror(errno)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1204 | return -1; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | // wait for subprocess to finish |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1208 | int status = -1; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1209 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 1210 | if (got_pid != pid) { |
| 1211 | *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: " |
| 1212 | "wanted %d, got %d: %s", |
| 1213 | command_line.c_str(), pid, got_pid, strerror(errno)); |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1214 | return -1; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1215 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1216 | if (WIFEXITED(status)) { |
| 1217 | return WEXITSTATUS(status); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1218 | } |
Calin Juravle | 2e2db78 | 2016-02-23 12:00:03 +0000 | [diff] [blame] | 1219 | return -1; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | bool Exec(std::vector<std::string>& arg_vector, std::string* error_msg) { |
| 1224 | int status = ExecAndReturnCode(arg_vector, error_msg); |
| 1225 | if (status != 0) { |
| 1226 | const std::string command_line(Join(arg_vector, ' ')); |
| 1227 | *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status", |
| 1228 | command_line.c_str()); |
| 1229 | return false; |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1230 | } |
| 1231 | return true; |
| 1232 | } |
| 1233 | |
Calin Juravle | 5e2b971 | 2015-12-18 14:10:00 +0200 | [diff] [blame] | 1234 | bool FileExists(const std::string& filename) { |
| 1235 | struct stat buffer; |
| 1236 | return stat(filename.c_str(), &buffer) == 0; |
| 1237 | } |
| 1238 | |
Calin Juravle | b9c1b9b | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 1239 | bool FileExistsAndNotEmpty(const std::string& filename) { |
| 1240 | struct stat buffer; |
| 1241 | if (stat(filename.c_str(), &buffer) != 0) { |
| 1242 | return false; |
| 1243 | } |
| 1244 | return buffer.st_size > 0; |
| 1245 | } |
| 1246 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 1247 | std::string PrettyDescriptor(Primitive::Type type) { |
| 1248 | return PrettyDescriptor(Primitive::Descriptor(type)); |
| 1249 | } |
| 1250 | |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1251 | static void DumpMethodCFGImpl(const DexFile* dex_file, |
| 1252 | uint32_t dex_method_idx, |
| 1253 | const DexFile::CodeItem* code_item, |
| 1254 | std::ostream& os) { |
| 1255 | os << "digraph {\n"; |
| 1256 | os << " # /* " << PrettyMethod(dex_method_idx, *dex_file, true) << " */\n"; |
| 1257 | |
| 1258 | std::set<uint32_t> dex_pc_is_branch_target; |
| 1259 | { |
| 1260 | // Go and populate. |
| 1261 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 1262 | for (uint32_t dex_pc = 0; |
| 1263 | dex_pc < code_item->insns_size_in_code_units_; |
| 1264 | dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) { |
| 1265 | if (inst->IsBranch()) { |
| 1266 | dex_pc_is_branch_target.insert(dex_pc + inst->GetTargetOffset()); |
| 1267 | } else if (inst->IsSwitch()) { |
| 1268 | const uint16_t* insns = code_item->insns_ + dex_pc; |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1269 | int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1270 | const uint16_t* switch_insns = insns + switch_offset; |
| 1271 | uint32_t switch_count = switch_insns[1]; |
| 1272 | int32_t targets_offset; |
| 1273 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 1274 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1275 | targets_offset = 4; |
| 1276 | } else { |
| 1277 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 1278 | targets_offset = 2 + 2 * switch_count; |
| 1279 | } |
| 1280 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1281 | int32_t offset = |
| 1282 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1283 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1284 | dex_pc_is_branch_target.insert(dex_pc + offset); |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | // Create nodes for "basic blocks." |
| 1291 | std::map<uint32_t, uint32_t> dex_pc_to_node_id; // This only has entries for block starts. |
| 1292 | std::map<uint32_t, uint32_t> dex_pc_to_incl_id; // This has entries for all dex pcs. |
| 1293 | |
| 1294 | { |
| 1295 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 1296 | bool first_in_block = true; |
| 1297 | bool force_new_block = false; |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1298 | for (uint32_t dex_pc = 0; |
| 1299 | dex_pc < code_item->insns_size_in_code_units_; |
| 1300 | dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) { |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1301 | if (dex_pc == 0 || |
| 1302 | (dex_pc_is_branch_target.find(dex_pc) != dex_pc_is_branch_target.end()) || |
| 1303 | force_new_block) { |
| 1304 | uint32_t id = dex_pc_to_node_id.size(); |
| 1305 | if (id > 0) { |
| 1306 | // End last node. |
| 1307 | os << "}\"];\n"; |
| 1308 | } |
| 1309 | // Start next node. |
| 1310 | os << " node" << id << " [shape=record,label=\"{"; |
| 1311 | dex_pc_to_node_id.insert(std::make_pair(dex_pc, id)); |
| 1312 | first_in_block = true; |
| 1313 | force_new_block = false; |
| 1314 | } |
| 1315 | |
| 1316 | // Register instruction. |
| 1317 | dex_pc_to_incl_id.insert(std::make_pair(dex_pc, dex_pc_to_node_id.size() - 1)); |
| 1318 | |
| 1319 | // Print instruction. |
| 1320 | if (!first_in_block) { |
| 1321 | os << " | "; |
| 1322 | } else { |
| 1323 | first_in_block = false; |
| 1324 | } |
| 1325 | |
| 1326 | // Dump the instruction. Need to escape '"', '<', '>', '{' and '}'. |
| 1327 | os << "<" << "p" << dex_pc << ">"; |
| 1328 | os << " 0x" << std::hex << dex_pc << std::dec << ": "; |
| 1329 | std::string inst_str = inst->DumpString(dex_file); |
| 1330 | size_t cur_start = 0; // It's OK to start at zero, instruction dumps don't start with chars |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1331 | // we need to escape. |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1332 | while (cur_start != std::string::npos) { |
| 1333 | size_t next_escape = inst_str.find_first_of("\"{}<>", cur_start + 1); |
| 1334 | if (next_escape == std::string::npos) { |
| 1335 | os << inst_str.substr(cur_start, inst_str.size() - cur_start); |
| 1336 | break; |
| 1337 | } else { |
| 1338 | os << inst_str.substr(cur_start, next_escape - cur_start); |
| 1339 | // Escape all necessary characters. |
| 1340 | while (next_escape < inst_str.size()) { |
| 1341 | char c = inst_str.at(next_escape); |
| 1342 | if (c == '"' || c == '{' || c == '}' || c == '<' || c == '>') { |
| 1343 | os << '\\' << c; |
| 1344 | } else { |
| 1345 | break; |
| 1346 | } |
| 1347 | next_escape++; |
| 1348 | } |
| 1349 | if (next_escape >= inst_str.size()) { |
| 1350 | next_escape = std::string::npos; |
| 1351 | } |
| 1352 | cur_start = next_escape; |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | // Force a new block for some fall-throughs and some instructions that terminate the "local" |
| 1357 | // control flow. |
| 1358 | force_new_block = inst->IsSwitch() || inst->IsBasicBlockEnd(); |
| 1359 | } |
| 1360 | // Close last node. |
| 1361 | if (dex_pc_to_node_id.size() > 0) { |
| 1362 | os << "}\"];\n"; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | // Create edges between them. |
| 1367 | { |
| 1368 | std::ostringstream regular_edges; |
| 1369 | std::ostringstream taken_edges; |
| 1370 | std::ostringstream exception_edges; |
| 1371 | |
| 1372 | // Common set of exception edges. |
| 1373 | std::set<uint32_t> exception_targets; |
| 1374 | |
| 1375 | // These blocks (given by the first dex pc) need exception per dex-pc handling in a second |
| 1376 | // pass. In the first pass we try and see whether we can use a common set of edges. |
| 1377 | std::set<uint32_t> blocks_with_detailed_exceptions; |
| 1378 | |
| 1379 | { |
| 1380 | uint32_t last_node_id = std::numeric_limits<uint32_t>::max(); |
| 1381 | uint32_t old_dex_pc = 0; |
| 1382 | uint32_t block_start_dex_pc = std::numeric_limits<uint32_t>::max(); |
| 1383 | const Instruction* inst = Instruction::At(code_item->insns_); |
| 1384 | for (uint32_t dex_pc = 0; |
| 1385 | dex_pc < code_item->insns_size_in_code_units_; |
| 1386 | old_dex_pc = dex_pc, dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) { |
| 1387 | { |
| 1388 | auto it = dex_pc_to_node_id.find(dex_pc); |
| 1389 | if (it != dex_pc_to_node_id.end()) { |
| 1390 | if (!exception_targets.empty()) { |
| 1391 | // It seems the last block had common exception handlers. Add the exception edges now. |
| 1392 | uint32_t node_id = dex_pc_to_node_id.find(block_start_dex_pc)->second; |
| 1393 | for (uint32_t handler_pc : exception_targets) { |
| 1394 | auto node_id_it = dex_pc_to_incl_id.find(handler_pc); |
| 1395 | if (node_id_it != dex_pc_to_incl_id.end()) { |
| 1396 | exception_edges << " node" << node_id |
| 1397 | << " -> node" << node_id_it->second << ":p" << handler_pc |
| 1398 | << ";\n"; |
| 1399 | } |
| 1400 | } |
| 1401 | exception_targets.clear(); |
| 1402 | } |
| 1403 | |
| 1404 | block_start_dex_pc = dex_pc; |
| 1405 | |
| 1406 | // Seems to be a fall-through, connect to last_node_id. May be spurious edges for things |
| 1407 | // like switch data. |
| 1408 | uint32_t old_last = last_node_id; |
| 1409 | last_node_id = it->second; |
| 1410 | if (old_last != std::numeric_limits<uint32_t>::max()) { |
| 1411 | regular_edges << " node" << old_last << ":p" << old_dex_pc |
| 1412 | << " -> node" << last_node_id << ":p" << dex_pc |
| 1413 | << ";\n"; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | // Look at the exceptions of the first entry. |
| 1418 | CatchHandlerIterator catch_it(*code_item, dex_pc); |
| 1419 | for (; catch_it.HasNext(); catch_it.Next()) { |
| 1420 | exception_targets.insert(catch_it.GetHandlerAddress()); |
| 1421 | } |
| 1422 | } |
| 1423 | |
| 1424 | // Handle instruction. |
| 1425 | |
| 1426 | // Branch: something with at most two targets. |
| 1427 | if (inst->IsBranch()) { |
| 1428 | const int32_t offset = inst->GetTargetOffset(); |
| 1429 | const bool conditional = !inst->IsUnconditional(); |
| 1430 | |
| 1431 | auto target_it = dex_pc_to_node_id.find(dex_pc + offset); |
| 1432 | if (target_it != dex_pc_to_node_id.end()) { |
| 1433 | taken_edges << " node" << last_node_id << ":p" << dex_pc |
| 1434 | << " -> node" << target_it->second << ":p" << (dex_pc + offset) |
| 1435 | << ";\n"; |
| 1436 | } |
| 1437 | if (!conditional) { |
| 1438 | // No fall-through. |
| 1439 | last_node_id = std::numeric_limits<uint32_t>::max(); |
| 1440 | } |
| 1441 | } else if (inst->IsSwitch()) { |
| 1442 | // TODO: Iterate through all switch targets. |
| 1443 | const uint16_t* insns = code_item->insns_ + dex_pc; |
| 1444 | /* make sure the start of the switch is in range */ |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1445 | int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16); |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1446 | /* offset to switch table is a relative branch-style offset */ |
| 1447 | const uint16_t* switch_insns = insns + switch_offset; |
| 1448 | uint32_t switch_count = switch_insns[1]; |
| 1449 | int32_t targets_offset; |
| 1450 | if ((*insns & 0xff) == Instruction::PACKED_SWITCH) { |
| 1451 | /* 0=sig, 1=count, 2/3=firstKey */ |
| 1452 | targets_offset = 4; |
| 1453 | } else { |
| 1454 | /* 0=sig, 1=count, 2..count*2 = keys */ |
| 1455 | targets_offset = 2 + 2 * switch_count; |
| 1456 | } |
| 1457 | /* make sure the end of the switch is in range */ |
| 1458 | /* verify each switch target */ |
| 1459 | for (uint32_t targ = 0; targ < switch_count; targ++) { |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1460 | int32_t offset = |
| 1461 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) | |
| 1462 | static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16); |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1463 | int32_t abs_offset = dex_pc + offset; |
| 1464 | auto target_it = dex_pc_to_node_id.find(abs_offset); |
| 1465 | if (target_it != dex_pc_to_node_id.end()) { |
| 1466 | // TODO: value label. |
| 1467 | taken_edges << " node" << last_node_id << ":p" << dex_pc |
| 1468 | << " -> node" << target_it->second << ":p" << (abs_offset) |
| 1469 | << ";\n"; |
| 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | // Exception edges. If this is not the first instruction in the block |
| 1475 | if (block_start_dex_pc != dex_pc) { |
| 1476 | std::set<uint32_t> current_handler_pcs; |
| 1477 | CatchHandlerIterator catch_it(*code_item, dex_pc); |
| 1478 | for (; catch_it.HasNext(); catch_it.Next()) { |
| 1479 | current_handler_pcs.insert(catch_it.GetHandlerAddress()); |
| 1480 | } |
| 1481 | if (current_handler_pcs != exception_targets) { |
| 1482 | exception_targets.clear(); // Clear so we don't do something at the end. |
| 1483 | blocks_with_detailed_exceptions.insert(block_start_dex_pc); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | if (inst->IsReturn() || |
| 1488 | (inst->Opcode() == Instruction::THROW) || |
| 1489 | (inst->IsBranch() && inst->IsUnconditional())) { |
| 1490 | // No fall-through. |
| 1491 | last_node_id = std::numeric_limits<uint32_t>::max(); |
| 1492 | } |
| 1493 | } |
| 1494 | // Finish up the last block, if it had common exceptions. |
| 1495 | if (!exception_targets.empty()) { |
| 1496 | // It seems the last block had common exception handlers. Add the exception edges now. |
| 1497 | uint32_t node_id = dex_pc_to_node_id.find(block_start_dex_pc)->second; |
| 1498 | for (uint32_t handler_pc : exception_targets) { |
| 1499 | auto node_id_it = dex_pc_to_incl_id.find(handler_pc); |
| 1500 | if (node_id_it != dex_pc_to_incl_id.end()) { |
| 1501 | exception_edges << " node" << node_id |
| 1502 | << " -> node" << node_id_it->second << ":p" << handler_pc |
| 1503 | << ";\n"; |
| 1504 | } |
| 1505 | } |
| 1506 | exception_targets.clear(); |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | // Second pass for detailed exception blocks. |
| 1511 | // TODO |
| 1512 | // Exception edges. If this is not the first instruction in the block |
| 1513 | for (uint32_t dex_pc : blocks_with_detailed_exceptions) { |
| 1514 | const Instruction* inst = Instruction::At(&code_item->insns_[dex_pc]); |
| 1515 | uint32_t this_node_id = dex_pc_to_incl_id.find(dex_pc)->second; |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1516 | while (true) { |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1517 | CatchHandlerIterator catch_it(*code_item, dex_pc); |
| 1518 | if (catch_it.HasNext()) { |
| 1519 | std::set<uint32_t> handled_targets; |
| 1520 | for (; catch_it.HasNext(); catch_it.Next()) { |
| 1521 | uint32_t handler_pc = catch_it.GetHandlerAddress(); |
| 1522 | auto it = handled_targets.find(handler_pc); |
| 1523 | if (it == handled_targets.end()) { |
| 1524 | auto node_id_it = dex_pc_to_incl_id.find(handler_pc); |
| 1525 | if (node_id_it != dex_pc_to_incl_id.end()) { |
| 1526 | exception_edges << " node" << this_node_id << ":p" << dex_pc |
| 1527 | << " -> node" << node_id_it->second << ":p" << handler_pc |
| 1528 | << ";\n"; |
| 1529 | } |
| 1530 | |
| 1531 | // Mark as done. |
| 1532 | handled_targets.insert(handler_pc); |
| 1533 | } |
| 1534 | } |
| 1535 | } |
| 1536 | if (inst->IsBasicBlockEnd()) { |
| 1537 | break; |
| 1538 | } |
| 1539 | |
Andreas Gampe | 53de99c | 2015-08-17 13:43:55 -0700 | [diff] [blame] | 1540 | // Loop update. Have a break-out if the next instruction is a branch target and thus in |
| 1541 | // another block. |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 1542 | dex_pc += inst->SizeInCodeUnits(); |
| 1543 | if (dex_pc >= code_item->insns_size_in_code_units_) { |
| 1544 | break; |
| 1545 | } |
| 1546 | if (dex_pc_to_node_id.find(dex_pc) != dex_pc_to_node_id.end()) { |
| 1547 | break; |
| 1548 | } |
| 1549 | inst = inst->Next(); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | // Write out the sub-graphs to make edges styled. |
| 1554 | os << "\n"; |
| 1555 | os << " subgraph regular_edges {\n"; |
| 1556 | os << " edge [color=\"#000000\",weight=.3,len=3];\n\n"; |
| 1557 | os << " " << regular_edges.str() << "\n"; |
| 1558 | os << " }\n\n"; |
| 1559 | |
| 1560 | os << " subgraph taken_edges {\n"; |
| 1561 | os << " edge [color=\"#00FF00\",weight=.3,len=3];\n\n"; |
| 1562 | os << " " << taken_edges.str() << "\n"; |
| 1563 | os << " }\n\n"; |
| 1564 | |
| 1565 | os << " subgraph exception_edges {\n"; |
| 1566 | os << " edge [color=\"#FF0000\",weight=.3,len=3];\n\n"; |
| 1567 | os << " " << exception_edges.str() << "\n"; |
| 1568 | os << " }\n\n"; |
| 1569 | } |
| 1570 | |
| 1571 | os << "}\n"; |
| 1572 | } |
| 1573 | |
| 1574 | void DumpMethodCFG(ArtMethod* method, std::ostream& os) { |
| 1575 | const DexFile* dex_file = method->GetDexFile(); |
| 1576 | const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); |
| 1577 | |
| 1578 | DumpMethodCFGImpl(dex_file, method->GetDexMethodIndex(), code_item, os); |
| 1579 | } |
| 1580 | |
| 1581 | void DumpMethodCFG(const DexFile* dex_file, uint32_t dex_method_idx, std::ostream& os) { |
| 1582 | // This is painful, we need to find the code item. That means finding the class, and then |
| 1583 | // iterating the table. |
| 1584 | if (dex_method_idx >= dex_file->NumMethodIds()) { |
| 1585 | os << "Could not find method-idx."; |
| 1586 | return; |
| 1587 | } |
| 1588 | const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx); |
| 1589 | |
| 1590 | const DexFile::ClassDef* class_def = dex_file->FindClassDef(method_id.class_idx_); |
| 1591 | if (class_def == nullptr) { |
| 1592 | os << "Could not find class-def."; |
| 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | const uint8_t* class_data = dex_file->GetClassData(*class_def); |
| 1597 | if (class_data == nullptr) { |
| 1598 | os << "No class data."; |
| 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | ClassDataItemIterator it(*dex_file, class_data); |
| 1603 | // Skip fields |
| 1604 | while (it.HasNextStaticField() || it.HasNextInstanceField()) { |
| 1605 | it.Next(); |
| 1606 | } |
| 1607 | |
| 1608 | // Find method, and dump it. |
| 1609 | while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) { |
| 1610 | uint32_t method_idx = it.GetMemberIndex(); |
| 1611 | if (method_idx == dex_method_idx) { |
| 1612 | DumpMethodCFGImpl(dex_file, dex_method_idx, it.GetMethodCodeItem(), os); |
| 1613 | return; |
| 1614 | } |
| 1615 | it.Next(); |
| 1616 | } |
| 1617 | |
| 1618 | // Otherwise complain. |
| 1619 | os << "Something went wrong, didn't find the method in the class data."; |
| 1620 | } |
| 1621 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 1622 | static void ParseStringAfterChar(const std::string& s, |
| 1623 | char c, |
| 1624 | std::string* parsed_value, |
| 1625 | UsageFn Usage) { |
| 1626 | std::string::size_type colon = s.find(c); |
| 1627 | if (colon == std::string::npos) { |
| 1628 | Usage("Missing char %c in option %s\n", c, s.c_str()); |
| 1629 | } |
| 1630 | // Add one to remove the char we were trimming until. |
| 1631 | *parsed_value = s.substr(colon + 1); |
| 1632 | } |
| 1633 | |
| 1634 | void ParseDouble(const std::string& option, |
| 1635 | char after_char, |
| 1636 | double min, |
| 1637 | double max, |
| 1638 | double* parsed_value, |
| 1639 | UsageFn Usage) { |
| 1640 | std::string substring; |
| 1641 | ParseStringAfterChar(option, after_char, &substring, Usage); |
| 1642 | bool sane_val = true; |
| 1643 | double value; |
| 1644 | if ((false)) { |
| 1645 | // TODO: this doesn't seem to work on the emulator. b/15114595 |
| 1646 | std::stringstream iss(substring); |
| 1647 | iss >> value; |
| 1648 | // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range. |
| 1649 | sane_val = iss.eof() && (value >= min) && (value <= max); |
| 1650 | } else { |
| 1651 | char* end = nullptr; |
| 1652 | value = strtod(substring.c_str(), &end); |
| 1653 | sane_val = *end == '\0' && value >= min && value <= max; |
| 1654 | } |
| 1655 | if (!sane_val) { |
| 1656 | Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str()); |
| 1657 | } |
| 1658 | *parsed_value = value; |
| 1659 | } |
| 1660 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 1661 | int64_t GetFileSizeBytes(const std::string& filename) { |
| 1662 | struct stat stat_buf; |
| 1663 | int rc = stat(filename.c_str(), &stat_buf); |
| 1664 | return rc == 0 ? stat_buf.st_size : -1; |
| 1665 | } |
| 1666 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 1667 | void SleepForever() { |
| 1668 | while (true) { |
| 1669 | usleep(1000000); |
| 1670 | } |
| 1671 | } |
| 1672 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 1673 | } // namespace art |