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