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 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 28 | #include "android-base/stringprintf.h" |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 29 | #include "android-base/strings.h" |
| 30 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 31 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 32 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 33 | #include "dex_file-inl.h" |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 34 | #include "dex_instruction.h" |
Nicolas Geoffray | 524e7ea | 2015-10-16 17:13:34 +0100 | [diff] [blame] | 35 | #include "oat_quick_method_header.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 36 | #include "os.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 37 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 38 | #include "utf-inl.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 40 | #if defined(__APPLE__) |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 41 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 42 | #include <sys/syscall.h> |
David Sehr | fa44200 | 2016-08-22 18:42:08 -0700 | [diff] [blame] | 43 | #include <crt_externs.h> |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 46 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 47 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 48 | #endif |
| 49 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 50 | namespace art { |
| 51 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 52 | using android::base::StringAppendF; |
| 53 | using android::base::StringPrintf; |
| 54 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 55 | pid_t GetTid() { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 56 | #if defined(__APPLE__) |
| 57 | uint64_t owner; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 58 | 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] | 59 | return owner; |
Elliott Hughes | 323aa86 | 2014-08-20 15:00:04 -0700 | [diff] [blame] | 60 | #elif defined(__BIONIC__) |
| 61 | return gettid(); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 62 | #else |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 63 | return syscall(__NR_gettid); |
| 64 | #endif |
| 65 | } |
| 66 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 67 | std::string GetThreadName(pid_t tid) { |
| 68 | std::string result; |
| 69 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 70 | result.resize(result.size() - 1); // Lose the trailing '\n'. |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 71 | } else { |
| 72 | result = "<unknown>"; |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 77 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
Andreas Gampe | df87892 | 2015-08-13 16:44:54 -0700 | [diff] [blame] | 78 | File file(file_name, O_RDONLY, false); |
| 79 | if (!file.IsOpened()) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 82 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 83 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 84 | while (true) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 85 | 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] | 86 | if (n == -1) { |
| 87 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 88 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 89 | if (n == 0) { |
| 90 | return true; |
| 91 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 92 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 93 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 96 | bool PrintFileToLog(const std::string& file_name, LogSeverity level) { |
Andreas Gampe | df87892 | 2015-08-13 16:44:54 -0700 | [diff] [blame] | 97 | File file(file_name, O_RDONLY, false); |
| 98 | if (!file.IsOpened()) { |
Andreas Gampe | a6dfdae | 2015-02-24 15:50:19 -0800 | [diff] [blame] | 99 | return false; |
| 100 | } |
| 101 | |
| 102 | constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings. |
| 103 | char buf[kBufSize + 1]; // +1 for terminator. |
| 104 | size_t filled_to = 0; |
| 105 | while (true) { |
| 106 | DCHECK_LT(filled_to, kBufSize); |
| 107 | int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to)); |
| 108 | if (n <= 0) { |
| 109 | // Print the rest of the buffer, if it exists. |
| 110 | if (filled_to > 0) { |
| 111 | buf[filled_to] = 0; |
| 112 | LOG(level) << buf; |
| 113 | } |
| 114 | return n == 0; |
| 115 | } |
| 116 | // Scan for '\n'. |
| 117 | size_t i = filled_to; |
| 118 | bool found_newline = false; |
| 119 | for (; i < filled_to + n; ++i) { |
| 120 | if (buf[i] == '\n') { |
| 121 | // Found a line break, that's something to print now. |
| 122 | buf[i] = 0; |
| 123 | LOG(level) << buf; |
| 124 | // Copy the rest to the front. |
| 125 | if (i + 1 < filled_to + n) { |
| 126 | memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1); |
| 127 | filled_to = filled_to + n - i - 1; |
| 128 | } else { |
| 129 | filled_to = 0; |
| 130 | } |
| 131 | found_newline = true; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | if (found_newline) { |
| 136 | continue; |
| 137 | } else { |
| 138 | filled_to += n; |
| 139 | // Check if we must flush now. |
| 140 | if (filled_to == kBufSize) { |
| 141 | buf[kBufSize] = 0; |
| 142 | LOG(level) << buf; |
| 143 | filled_to = 0; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 149 | std::string PrettyDescriptor(const char* descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 150 | // Count the number of '['s to get the dimensionality. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 151 | const char* c = descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 152 | size_t dim = 0; |
| 153 | while (*c == '[') { |
| 154 | dim++; |
| 155 | c++; |
| 156 | } |
| 157 | |
| 158 | // Reference or primitive? |
| 159 | if (*c == 'L') { |
| 160 | // "[[La/b/C;" -> "a.b.C[][]". |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 161 | c++; // Skip the 'L'. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 162 | } else { |
| 163 | // "[[B" -> "byte[][]". |
| 164 | // To make life easier, we make primitives look like unqualified |
| 165 | // reference types. |
| 166 | switch (*c) { |
| 167 | case 'B': c = "byte;"; break; |
| 168 | case 'C': c = "char;"; break; |
| 169 | case 'D': c = "double;"; break; |
| 170 | case 'F': c = "float;"; break; |
| 171 | case 'I': c = "int;"; break; |
| 172 | case 'J': c = "long;"; break; |
| 173 | case 'S': c = "short;"; break; |
| 174 | case 'Z': c = "boolean;"; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 175 | case 'V': c = "void;"; break; // Used when decoding return types. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 176 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 181 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 182 | std::string result; |
| 183 | const char* p = c; |
| 184 | while (*p != ';') { |
| 185 | char ch = *p++; |
| 186 | if (ch == '/') { |
| 187 | ch = '.'; |
| 188 | } |
| 189 | result.push_back(ch); |
| 190 | } |
| 191 | // ...and replace the semicolon with 'dim' "[]" pairs: |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 192 | for (size_t i = 0; i < dim; ++i) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 193 | result += "[]"; |
| 194 | } |
| 195 | return result; |
| 196 | } |
| 197 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 198 | std::string PrettyArguments(const char* signature) { |
| 199 | std::string result; |
| 200 | result += '('; |
| 201 | CHECK_EQ(*signature, '('); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 202 | ++signature; // Skip the '('. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 203 | while (*signature != ')') { |
| 204 | size_t argument_length = 0; |
| 205 | while (signature[argument_length] == '[') { |
| 206 | ++argument_length; |
| 207 | } |
| 208 | if (signature[argument_length] == 'L') { |
| 209 | argument_length = (strchr(signature, ';') - signature + 1); |
| 210 | } else { |
| 211 | ++argument_length; |
| 212 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 213 | { |
| 214 | std::string argument_descriptor(signature, argument_length); |
| 215 | result += PrettyDescriptor(argument_descriptor.c_str()); |
| 216 | } |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 217 | if (signature[argument_length] != ')') { |
| 218 | result += ", "; |
| 219 | } |
| 220 | signature += argument_length; |
| 221 | } |
| 222 | CHECK_EQ(*signature, ')'); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 223 | ++signature; // Skip the ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 224 | result += ')'; |
| 225 | return result; |
| 226 | } |
| 227 | |
| 228 | std::string PrettyReturnType(const char* signature) { |
| 229 | const char* return_type = strchr(signature, ')'); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 230 | CHECK(return_type != nullptr); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 231 | ++return_type; // Skip ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 232 | return PrettyDescriptor(return_type); |
| 233 | } |
| 234 | |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 235 | std::string PrettyJavaAccessFlags(uint32_t access_flags) { |
| 236 | std::string result; |
| 237 | if ((access_flags & kAccPublic) != 0) { |
| 238 | result += "public "; |
| 239 | } |
| 240 | if ((access_flags & kAccProtected) != 0) { |
| 241 | result += "protected "; |
| 242 | } |
| 243 | if ((access_flags & kAccPrivate) != 0) { |
| 244 | result += "private "; |
| 245 | } |
| 246 | if ((access_flags & kAccFinal) != 0) { |
| 247 | result += "final "; |
| 248 | } |
| 249 | if ((access_flags & kAccStatic) != 0) { |
| 250 | result += "static "; |
| 251 | } |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 252 | if ((access_flags & kAccAbstract) != 0) { |
| 253 | result += "abstract "; |
| 254 | } |
| 255 | if ((access_flags & kAccInterface) != 0) { |
| 256 | result += "interface "; |
| 257 | } |
Andreas Gampe | c0d8229 | 2014-09-23 10:38:30 -0700 | [diff] [blame] | 258 | if ((access_flags & kAccTransient) != 0) { |
| 259 | result += "transient "; |
| 260 | } |
| 261 | if ((access_flags & kAccVolatile) != 0) { |
| 262 | result += "volatile "; |
| 263 | } |
| 264 | if ((access_flags & kAccSynchronized) != 0) { |
| 265 | result += "synchronized "; |
| 266 | } |
| 267 | return result; |
| 268 | } |
| 269 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 270 | std::string PrettySize(int64_t byte_count) { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 271 | // The byte thresholds at which we display amounts. A byte count is displayed |
| 272 | // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 273 | static const int64_t kUnitThresholds[] = { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 274 | 0, // B up to... |
| 275 | 3*1024, // KB up to... |
| 276 | 2*1024*1024, // MB up to... |
| 277 | 1024*1024*1024 // GB from here. |
| 278 | }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 279 | static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB }; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 280 | static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 281 | const char* negative_str = ""; |
| 282 | if (byte_count < 0) { |
| 283 | negative_str = "-"; |
| 284 | byte_count = -byte_count; |
| 285 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 286 | int i = arraysize(kUnitThresholds); |
| 287 | while (--i > 0) { |
| 288 | if (byte_count >= kUnitThresholds[i]) { |
| 289 | break; |
| 290 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 291 | } |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 292 | return StringPrintf("%s%" PRId64 "%s", |
| 293 | negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 296 | static inline constexpr bool NeedsEscaping(uint16_t ch) { |
| 297 | return (ch < ' ' || ch > '~'); |
| 298 | } |
| 299 | |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 300 | std::string PrintableChar(uint16_t ch) { |
| 301 | std::string result; |
| 302 | result += '\''; |
| 303 | if (NeedsEscaping(ch)) { |
| 304 | StringAppendF(&result, "\\u%04x", ch); |
| 305 | } else { |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame^] | 306 | result += static_cast<std::string::value_type>(ch); |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 307 | } |
| 308 | result += '\''; |
| 309 | return result; |
| 310 | } |
| 311 | |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 312 | std::string PrintableString(const char* utf) { |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 313 | std::string result; |
| 314 | result += '"'; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 315 | const char* p = utf; |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 316 | size_t char_count = CountModifiedUtf8Chars(p); |
| 317 | for (size_t i = 0; i < char_count; ++i) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 318 | uint32_t ch = GetUtf16FromUtf8(&p); |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 319 | if (ch == '\\') { |
| 320 | result += "\\\\"; |
| 321 | } else if (ch == '\n') { |
| 322 | result += "\\n"; |
| 323 | } else if (ch == '\r') { |
| 324 | result += "\\r"; |
| 325 | } else if (ch == '\t') { |
| 326 | result += "\\t"; |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 327 | } else { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 328 | const uint16_t leading = GetLeadingUtf16Char(ch); |
| 329 | |
| 330 | if (NeedsEscaping(leading)) { |
| 331 | StringAppendF(&result, "\\u%04x", leading); |
| 332 | } else { |
Andreas Gampe | f45d61c | 2017-06-07 10:29:33 -0700 | [diff] [blame^] | 333 | result += static_cast<std::string::value_type>(leading); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | const uint32_t trailing = GetTrailingUtf16Char(ch); |
| 337 | if (trailing != 0) { |
| 338 | // All high surrogates will need escaping. |
| 339 | StringAppendF(&result, "\\u%04x", trailing); |
| 340 | } |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | result += '"'; |
| 344 | return result; |
| 345 | } |
| 346 | |
Alex Light | 888a59e | 2017-01-25 11:41:41 -0800 | [diff] [blame] | 347 | std::string GetJniShortName(const std::string& class_descriptor, const std::string& method) { |
| 348 | // Remove the leading 'L' and trailing ';'... |
| 349 | std::string class_name(class_descriptor); |
| 350 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 351 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
| 352 | class_name.erase(0, 1); |
| 353 | class_name.erase(class_name.size() - 1, 1); |
| 354 | |
| 355 | std::string short_name; |
| 356 | short_name += "Java_"; |
| 357 | short_name += MangleForJni(class_name); |
| 358 | short_name += "_"; |
| 359 | short_name += MangleForJni(method); |
| 360 | return short_name; |
| 361 | } |
| 362 | |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 363 | // 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] | 364 | std::string MangleForJni(const std::string& s) { |
| 365 | std::string result; |
| 366 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 367 | const char* cp = &s[0]; |
| 368 | for (size_t i = 0; i < char_count; ++i) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 369 | uint32_t ch = GetUtf16FromUtf8(&cp); |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 370 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) { |
| 371 | result.push_back(ch); |
| 372 | } else if (ch == '.' || ch == '/') { |
| 373 | result += "_"; |
| 374 | } else if (ch == '_') { |
| 375 | result += "_1"; |
| 376 | } else if (ch == ';') { |
| 377 | result += "_2"; |
| 378 | } else if (ch == '[') { |
| 379 | result += "_3"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 380 | } else { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 381 | const uint16_t leading = GetLeadingUtf16Char(ch); |
| 382 | const uint32_t trailing = GetTrailingUtf16Char(ch); |
| 383 | |
| 384 | StringAppendF(&result, "_0%04x", leading); |
| 385 | if (trailing != 0) { |
| 386 | StringAppendF(&result, "_0%04x", trailing); |
| 387 | } |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | return result; |
| 391 | } |
| 392 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 393 | std::string DotToDescriptor(const char* class_name) { |
| 394 | std::string descriptor(class_name); |
| 395 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 396 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 397 | descriptor = "L" + descriptor + ";"; |
| 398 | } |
| 399 | return descriptor; |
| 400 | } |
| 401 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 402 | std::string DescriptorToDot(const char* descriptor) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 403 | size_t length = strlen(descriptor); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 404 | if (length > 1) { |
| 405 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 406 | // Descriptors have the leading 'L' and trailing ';' stripped. |
| 407 | std::string result(descriptor + 1, length - 2); |
| 408 | std::replace(result.begin(), result.end(), '/', '.'); |
| 409 | return result; |
| 410 | } else { |
| 411 | // For arrays the 'L' and ';' remain intact. |
| 412 | std::string result(descriptor); |
| 413 | std::replace(result.begin(), result.end(), '/', '.'); |
| 414 | return result; |
| 415 | } |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 416 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 417 | // Do nothing for non-class/array descriptors. |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 418 | return descriptor; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | std::string DescriptorToName(const char* descriptor) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 422 | size_t length = strlen(descriptor); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 423 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 424 | std::string result(descriptor + 1, length - 2); |
| 425 | return result; |
| 426 | } |
| 427 | return descriptor; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 428 | } |
| 429 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 430 | // Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 431 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 432 | 0x00000000, // 00..1f low control characters; nothing valid |
| 433 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 434 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 435 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 436 | }; |
| 437 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 438 | // Helper for IsValidPartOfMemberNameUtf8(); do not call directly. |
| 439 | bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 440 | /* |
| 441 | * It's a multibyte encoded character. Decode it and analyze. We |
| 442 | * accept anything that isn't (a) an improperly encoded low value, |
| 443 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 444 | * control character, or (e) a high space, layout, or special |
| 445 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 446 | * U+fff0..U+ffff). This is all specified in the dex format |
| 447 | * document. |
| 448 | */ |
| 449 | |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 450 | const uint32_t pair = GetUtf16FromUtf8(pUtf8Ptr); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 451 | const uint16_t leading = GetLeadingUtf16Char(pair); |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 452 | |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 453 | // We have a surrogate pair resulting from a valid 4 byte UTF sequence. |
| 454 | // No further checks are necessary because 4 byte sequences span code |
| 455 | // points [U+10000, U+1FFFFF], which are valid codepoints in a dex |
| 456 | // identifier. Furthermore, GetUtf16FromUtf8 guarantees that each of |
| 457 | // the surrogate halves are valid and well formed in this instance. |
| 458 | if (GetTrailingUtf16Char(pair) != 0) { |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | |
| 463 | // We've encountered a one, two or three byte UTF-8 sequence. The |
| 464 | // three byte UTF-8 sequence could be one half of a surrogate pair. |
| 465 | switch (leading >> 8) { |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 466 | case 0x00: |
| 467 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 468 | return (leading > 0x00a0); |
| 469 | case 0xd8: |
| 470 | case 0xd9: |
| 471 | case 0xda: |
| 472 | case 0xdb: |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 473 | { |
| 474 | // We found a three byte sequence encoding one half of a surrogate. |
| 475 | // Look for the other half. |
| 476 | const uint32_t pair2 = GetUtf16FromUtf8(pUtf8Ptr); |
| 477 | const uint16_t trailing = GetLeadingUtf16Char(pair2); |
| 478 | |
| 479 | return (GetTrailingUtf16Char(pair2) == 0) && (0xdc00 <= trailing && trailing <= 0xdfff); |
| 480 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 481 | case 0xdc: |
| 482 | case 0xdd: |
| 483 | case 0xde: |
| 484 | case 0xdf: |
| 485 | // It's a trailing surrogate, which is not valid at this point. |
| 486 | return false; |
| 487 | case 0x20: |
| 488 | case 0xff: |
| 489 | // It's in the range that has spaces, controls, and specials. |
| 490 | switch (leading & 0xfff8) { |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 491 | case 0x2000: |
| 492 | case 0x2008: |
| 493 | case 0x2028: |
| 494 | case 0xfff0: |
| 495 | case 0xfff8: |
| 496 | return false; |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 497 | } |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 498 | return true; |
| 499 | default: |
| 500 | return true; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 501 | } |
Narayan Kamath | a5afcfc | 2015-01-29 20:06:46 +0000 | [diff] [blame] | 502 | |
Narayan Kamath | 8508e37 | 2015-05-06 14:55:43 +0100 | [diff] [blame] | 503 | UNREACHABLE(); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 507 | * valid as part of a member name, updating the pointer to point past |
| 508 | * the consumed character. This will consume two encoded UTF-16 code |
| 509 | * points if the character is encoded as a surrogate pair. Also, if |
| 510 | * this function returns false, then the given pointer may only have |
| 511 | * been partially advanced. |
| 512 | */ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 513 | static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 514 | uint8_t c = (uint8_t) **pUtf8Ptr; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 515 | if (LIKELY(c <= 0x7f)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 516 | // It's low-ascii, so check the table. |
| 517 | uint32_t wordIdx = c >> 5; |
| 518 | uint32_t bitIdx = c & 0x1f; |
| 519 | (*pUtf8Ptr)++; |
| 520 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 521 | } |
| 522 | |
| 523 | // It's a multibyte encoded character. Call a non-inline function |
| 524 | // for the heavy lifting. |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 525 | return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr); |
| 526 | } |
| 527 | |
| 528 | bool IsValidMemberName(const char* s) { |
| 529 | bool angle_name = false; |
| 530 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 531 | switch (*s) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 532 | case '\0': |
| 533 | // The empty string is not a valid name. |
| 534 | return false; |
| 535 | case '<': |
| 536 | angle_name = true; |
| 537 | s++; |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | while (true) { |
| 542 | switch (*s) { |
| 543 | case '\0': |
| 544 | return !angle_name; |
| 545 | case '>': |
| 546 | return angle_name && s[1] == '\0'; |
| 547 | } |
| 548 | |
| 549 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
| 550 | return false; |
| 551 | } |
| 552 | } |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 555 | enum ClassNameType { kName, kDescriptor }; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 556 | template<ClassNameType kType, char kSeparator> |
| 557 | static bool IsValidClassName(const char* s) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 558 | int arrayCount = 0; |
| 559 | while (*s == '[') { |
| 560 | arrayCount++; |
| 561 | s++; |
| 562 | } |
| 563 | |
| 564 | if (arrayCount > 255) { |
| 565 | // Arrays may have no more than 255 dimensions. |
| 566 | return false; |
| 567 | } |
| 568 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 569 | ClassNameType type = kType; |
| 570 | if (type != kDescriptor && arrayCount != 0) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 571 | /* |
| 572 | * If we're looking at an array of some sort, then it doesn't |
| 573 | * matter if what is being asked for is a class name; the |
| 574 | * format looks the same as a type descriptor in that case, so |
| 575 | * treat it as such. |
| 576 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 577 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 580 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 581 | /* |
| 582 | * We are looking for a descriptor. Either validate it as a |
| 583 | * single-character primitive type, or continue on to check the |
| 584 | * embedded class name (bracketed by "L" and ";"). |
| 585 | */ |
| 586 | switch (*(s++)) { |
| 587 | case 'B': |
| 588 | case 'C': |
| 589 | case 'D': |
| 590 | case 'F': |
| 591 | case 'I': |
| 592 | case 'J': |
| 593 | case 'S': |
| 594 | case 'Z': |
| 595 | // These are all single-character descriptors for primitive types. |
| 596 | return (*s == '\0'); |
| 597 | case 'V': |
| 598 | // Non-array void is valid, but you can't have an array of void. |
| 599 | return (arrayCount == 0) && (*s == '\0'); |
| 600 | case 'L': |
| 601 | // Class name: Break out and continue below. |
| 602 | break; |
| 603 | default: |
| 604 | // Oddball descriptor character. |
| 605 | return false; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * We just consumed the 'L' that introduces a class name as part |
| 611 | * of a type descriptor, or we are looking for an unadorned class |
| 612 | * name. |
| 613 | */ |
| 614 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 615 | bool sepOrFirst = true; // first character or just encountered a separator. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 616 | for (;;) { |
| 617 | uint8_t c = (uint8_t) *s; |
| 618 | switch (c) { |
| 619 | case '\0': |
| 620 | /* |
| 621 | * Premature end for a type descriptor, but valid for |
| 622 | * a class name as long as we haven't encountered an |
| 623 | * empty component (including the degenerate case of |
| 624 | * the empty string ""). |
| 625 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 626 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 627 | case ';': |
| 628 | /* |
| 629 | * Invalid character for a class name, but the |
| 630 | * legitimate end of a type descriptor. In the latter |
| 631 | * case, make sure that this is the end of the string |
| 632 | * and that it doesn't end with an empty component |
| 633 | * (including the degenerate case of "L;"). |
| 634 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 635 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 636 | case '/': |
| 637 | case '.': |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 638 | if (c != kSeparator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 639 | // The wrong separator character. |
| 640 | return false; |
| 641 | } |
| 642 | if (sepOrFirst) { |
| 643 | // Separator at start or two separators in a row. |
| 644 | return false; |
| 645 | } |
| 646 | sepOrFirst = true; |
| 647 | s++; |
| 648 | break; |
| 649 | default: |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 650 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 651 | return false; |
| 652 | } |
| 653 | sepOrFirst = false; |
| 654 | break; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 659 | bool IsValidBinaryClassName(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 660 | return IsValidClassName<kName, '.'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | bool IsValidJniClassName(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 664 | return IsValidClassName<kName, '/'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | bool IsValidDescriptor(const char* s) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 668 | return IsValidClassName<kDescriptor, '/'>(s); |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 671 | 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] | 672 | const char* p = s.data(); |
| 673 | const char* end = p + s.size(); |
| 674 | while (p != end) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 675 | if (*p == separator) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 676 | ++p; |
| 677 | } else { |
| 678 | const char* start = p; |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 679 | while (++p != end && *p != separator) { |
| 680 | // Skip to the next occurrence of the separator. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 681 | } |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 682 | result->push_back(std::string(start, p - start)); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 687 | void SetThreadName(const char* thread_name) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 688 | int hasAt = 0; |
| 689 | int hasDot = 0; |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 690 | const char* s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 691 | while (*s) { |
| 692 | if (*s == '.') { |
| 693 | hasDot = 1; |
| 694 | } else if (*s == '@') { |
| 695 | hasAt = 1; |
| 696 | } |
| 697 | s++; |
| 698 | } |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 699 | int len = s - thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 700 | if (len < 15 || hasAt || !hasDot) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 701 | s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 702 | } else { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 703 | s = thread_name + len - 15; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 704 | } |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 705 | #if defined(__linux__) |
Elliott Hughes | 7c6a61e | 2012-03-12 18:01:41 -0700 | [diff] [blame] | 706 | // pthread_setname_np fails rather than truncating long strings. |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 707 | 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] | 708 | strncpy(buf, s, sizeof(buf)-1); |
| 709 | buf[sizeof(buf)-1] = '\0'; |
| 710 | errno = pthread_setname_np(pthread_self(), buf); |
| 711 | if (errno != 0) { |
| 712 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 713 | } |
Elliott Hughes | 0a18df8 | 2015-01-09 15:16:16 -0800 | [diff] [blame] | 714 | #else // __APPLE__ |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 715 | pthread_setname_np(thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 716 | #endif |
| 717 | } |
| 718 | |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 719 | void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) { |
| 720 | *utime = *stime = *task_cpu = 0; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 721 | std::string stats; |
Elliott Hughes | 8a31b50 | 2012-04-30 19:36:11 -0700 | [diff] [blame] | 722 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 723 | return; |
| 724 | } |
| 725 | // Skip the command, which may contain spaces. |
| 726 | stats = stats.substr(stats.find(')') + 2); |
| 727 | // Extract the three fields we care about. |
| 728 | std::vector<std::string> fields; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 729 | Split(stats, ' ', &fields); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 730 | *state = fields[0][0]; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 731 | *utime = strtoull(fields[11].c_str(), nullptr, 10); |
| 732 | *stime = strtoull(fields[12].c_str(), nullptr, 10); |
| 733 | *task_cpu = strtoull(fields[36].c_str(), nullptr, 10); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 736 | static const char* GetAndroidDirSafe(const char* env_var, |
| 737 | const char* default_dir, |
| 738 | std::string* error_msg) { |
| 739 | const char* android_dir = getenv(env_var); |
| 740 | if (android_dir == nullptr) { |
| 741 | if (OS::DirectoryExists(default_dir)) { |
| 742 | android_dir = default_dir; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 743 | } else { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 744 | *error_msg = StringPrintf("%s not set and %s does not exist", env_var, default_dir); |
| 745 | return nullptr; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 746 | } |
| 747 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 748 | if (!OS::DirectoryExists(android_dir)) { |
| 749 | *error_msg = StringPrintf("Failed to find %s directory %s", env_var, android_dir); |
| 750 | return nullptr; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 751 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 752 | return android_dir; |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 753 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 754 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 755 | const char* GetAndroidDir(const char* env_var, const char* default_dir) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 756 | std::string error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 757 | const char* dir = GetAndroidDirSafe(env_var, default_dir, &error_msg); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 758 | if (dir != nullptr) { |
| 759 | return dir; |
| 760 | } else { |
| 761 | LOG(FATAL) << error_msg; |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 762 | return nullptr; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 766 | const char* GetAndroidRoot() { |
| 767 | return GetAndroidDir("ANDROID_ROOT", "/system"); |
| 768 | } |
| 769 | |
| 770 | const char* GetAndroidRootSafe(std::string* error_msg) { |
| 771 | return GetAndroidDirSafe("ANDROID_ROOT", "/system", error_msg); |
| 772 | } |
| 773 | |
| 774 | const char* GetAndroidData() { |
| 775 | return GetAndroidDir("ANDROID_DATA", "/data"); |
| 776 | } |
| 777 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 778 | const char* GetAndroidDataSafe(std::string* error_msg) { |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 779 | return GetAndroidDirSafe("ANDROID_DATA", "/data", error_msg); |
| 780 | } |
| 781 | |
| 782 | std::string GetDefaultBootImageLocation(std::string* error_msg) { |
| 783 | const char* android_root = GetAndroidRootSafe(error_msg); |
| 784 | if (android_root == nullptr) { |
| 785 | return ""; |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 786 | } |
Calin Juravle | 36eb313 | 2017-01-13 16:32:38 -0800 | [diff] [blame] | 787 | return StringPrintf("%s/framework/boot.art", android_root); |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 790 | 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] | 791 | bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 792 | CHECK(subdir != nullptr); |
| 793 | std::string error_msg; |
| 794 | const char* android_data = GetAndroidDataSafe(&error_msg); |
| 795 | if (android_data == nullptr) { |
| 796 | *have_android_data = false; |
| 797 | *dalvik_cache_exists = false; |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 798 | *is_global_cache = false; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 799 | return; |
| 800 | } else { |
| 801 | *have_android_data = true; |
| 802 | } |
| 803 | const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data)); |
| 804 | *dalvik_cache = dalvik_cache_root + subdir; |
| 805 | *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str()); |
Andreas Gampe | 3c13a79 | 2014-09-18 20:56:04 -0700 | [diff] [blame] | 806 | *is_global_cache = strcmp(android_data, "/data") == 0; |
| 807 | if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 808 | // Don't create the system's /data/dalvik-cache/... because it needs special permissions. |
| 809 | *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) && |
| 810 | (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST)); |
| 811 | } |
| 812 | } |
| 813 | |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 814 | std::string GetDalvikCache(const char* subdir) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 815 | CHECK(subdir != nullptr); |
Brian Carlstrom | 41ccffd | 2014-05-06 10:37:30 -0700 | [diff] [blame] | 816 | const char* android_data = GetAndroidData(); |
| 817 | const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data)); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 818 | const std::string dalvik_cache = dalvik_cache_root + subdir; |
Andreas Gampe | 40da286 | 2015-02-27 12:49:04 -0800 | [diff] [blame] | 819 | if (!OS::DirectoryExists(dalvik_cache.c_str())) { |
Richard Uhler | 55b58b6 | 2016-08-12 09:05:13 -0700 | [diff] [blame] | 820 | // TODO: Check callers. Traditional behavior is to not abort. |
| 821 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 822 | } |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 823 | return dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 824 | } |
| 825 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 826 | bool GetDalvikCacheFilename(const char* location, const char* cache_location, |
| 827 | std::string* filename, std::string* error_msg) { |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 828 | if (location[0] != '/') { |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 829 | *error_msg = StringPrintf("Expected path in location to be absolute: %s", location); |
| 830 | return false; |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 831 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 832 | std::string cache_file(&location[1]); // skip leading slash |
Andreas Gampe | 9186ced | 2016-12-12 14:28:21 -0800 | [diff] [blame] | 833 | if (!android::base::EndsWith(location, ".dex") && |
| 834 | !android::base::EndsWith(location, ".art") && |
| 835 | !android::base::EndsWith(location, ".oat")) { |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 836 | cache_file += "/"; |
| 837 | cache_file += DexFile::kClassesDex; |
| 838 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 839 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 840 | *filename = StringPrintf("%s/%s", cache_location, cache_file.c_str()); |
| 841 | return true; |
| 842 | } |
| 843 | |
Calin Juravle | 367b9d8 | 2017-05-15 18:18:39 -0700 | [diff] [blame] | 844 | std::string GetVdexFilename(const std::string& oat_location) { |
| 845 | return ReplaceFileExtension(oat_location, "vdex"); |
| 846 | } |
| 847 | |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 848 | static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) { |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 849 | // in = /foo/bar/baz |
| 850 | // out = /foo/bar/<isa>/baz |
| 851 | size_t pos = filename->rfind('/'); |
| 852 | CHECK_NE(pos, std::string::npos) << *filename << " " << isa; |
| 853 | filename->insert(pos, "/", 1); |
| 854 | filename->insert(pos + 1, GetInstructionSetString(isa)); |
| 855 | } |
| 856 | |
| 857 | std::string GetSystemImageFilename(const char* location, const InstructionSet isa) { |
| 858 | // location = /system/framework/boot.art |
| 859 | // filename = /system/framework/<isa>/boot.art |
| 860 | std::string filename(location); |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 861 | InsertIsaDirectory(isa, &filename); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 862 | return filename; |
| 863 | } |
| 864 | |
Calin Juravle | 5e2b971 | 2015-12-18 14:10:00 +0200 | [diff] [blame] | 865 | bool FileExists(const std::string& filename) { |
| 866 | struct stat buffer; |
| 867 | return stat(filename.c_str(), &buffer) == 0; |
| 868 | } |
| 869 | |
Calin Juravle | b9c1b9b | 2016-03-17 17:07:52 +0000 | [diff] [blame] | 870 | bool FileExistsAndNotEmpty(const std::string& filename) { |
| 871 | struct stat buffer; |
| 872 | if (stat(filename.c_str(), &buffer) != 0) { |
| 873 | return false; |
| 874 | } |
| 875 | return buffer.st_size > 0; |
| 876 | } |
| 877 | |
David Brazdil | 7b49e6c | 2016-09-01 11:06:18 +0100 | [diff] [blame] | 878 | std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension) { |
| 879 | const size_t last_ext = filename.find_last_of('.'); |
| 880 | if (last_ext == std::string::npos) { |
| 881 | return filename + "." + new_extension; |
| 882 | } else { |
| 883 | return filename.substr(0, last_ext + 1) + new_extension; |
| 884 | } |
| 885 | } |
| 886 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 887 | std::string PrettyDescriptor(Primitive::Type type) { |
| 888 | return PrettyDescriptor(Primitive::Descriptor(type)); |
| 889 | } |
| 890 | |
Nicolas Geoffray | abbb0f7 | 2015-10-29 18:55:58 +0000 | [diff] [blame] | 891 | static void ParseStringAfterChar(const std::string& s, |
| 892 | char c, |
| 893 | std::string* parsed_value, |
| 894 | UsageFn Usage) { |
| 895 | std::string::size_type colon = s.find(c); |
| 896 | if (colon == std::string::npos) { |
| 897 | Usage("Missing char %c in option %s\n", c, s.c_str()); |
| 898 | } |
| 899 | // Add one to remove the char we were trimming until. |
| 900 | *parsed_value = s.substr(colon + 1); |
| 901 | } |
| 902 | |
| 903 | void ParseDouble(const std::string& option, |
| 904 | char after_char, |
| 905 | double min, |
| 906 | double max, |
| 907 | double* parsed_value, |
| 908 | UsageFn Usage) { |
| 909 | std::string substring; |
| 910 | ParseStringAfterChar(option, after_char, &substring, Usage); |
| 911 | bool sane_val = true; |
| 912 | double value; |
| 913 | if ((false)) { |
| 914 | // TODO: this doesn't seem to work on the emulator. b/15114595 |
| 915 | std::stringstream iss(substring); |
| 916 | iss >> value; |
| 917 | // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range. |
| 918 | sane_val = iss.eof() && (value >= min) && (value <= max); |
| 919 | } else { |
| 920 | char* end = nullptr; |
| 921 | value = strtod(substring.c_str(), &end); |
| 922 | sane_val = *end == '\0' && value >= min && value <= max; |
| 923 | } |
| 924 | if (!sane_val) { |
| 925 | Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str()); |
| 926 | } |
| 927 | *parsed_value = value; |
| 928 | } |
| 929 | |
Calin Juravle | 4d77b6a | 2015-12-01 18:38:09 +0000 | [diff] [blame] | 930 | int64_t GetFileSizeBytes(const std::string& filename) { |
| 931 | struct stat stat_buf; |
| 932 | int rc = stat(filename.c_str(), &stat_buf); |
| 933 | return rc == 0 ? stat_buf.st_size : -1; |
| 934 | } |
| 935 | |
Mathieu Chartier | 4d87df6 | 2016-01-07 15:14:19 -0800 | [diff] [blame] | 936 | void SleepForever() { |
| 937 | while (true) { |
| 938 | usleep(1000000); |
| 939 | } |
| 940 | } |
| 941 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 942 | } // namespace art |