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