Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: enh@google.com (Elliott Hughes) |
| 3 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 4 | #include "utils.h" |
| 5 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 6 | #include <pthread.h> |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 7 | #include <sys/stat.h> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 8 | #include <sys/syscall.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 12 | #include "UniquePtr.h" |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 13 | #include "class_loader.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 14 | #include "file.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 15 | #include "object.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 16 | #include "os.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 17 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 18 | #if defined(HAVE_PRCTL) |
| 19 | #include <sys/prctl.h> |
| 20 | #endif |
| 21 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 22 | namespace art { |
| 23 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 24 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
| 25 | UniquePtr<File> file(OS::OpenFile(file_name.c_str(), false)); |
| 26 | if (file.get() == NULL) { |
| 27 | return false; |
| 28 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 29 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 30 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 31 | while (true) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 32 | int64_t n = file->Read(&buf[0], buf.size()); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 33 | if (n == -1) { |
| 34 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 35 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 36 | if (n == 0) { |
| 37 | return true; |
| 38 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 39 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 40 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 43 | std::string GetIsoDate() { |
| 44 | time_t now = time(NULL); |
| 45 | struct tm tmbuf; |
| 46 | struct tm* ptm = localtime_r(&now, &tmbuf); |
| 47 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d", |
| 48 | ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, |
| 49 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 50 | } |
| 51 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 52 | uint64_t MilliTime() { |
| 53 | struct timespec now; |
| 54 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 55 | return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_nsec / 1000000LL; |
| 56 | } |
| 57 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 58 | uint64_t NanoTime() { |
| 59 | struct timespec now; |
| 60 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 61 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
| 62 | } |
| 63 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 64 | std::string PrettyDescriptor(const String* java_descriptor) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 65 | if (java_descriptor == NULL) { |
| 66 | return "null"; |
| 67 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 68 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8()); |
| 69 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 70 | |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 71 | std::string PrettyDescriptor(const std::string& descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 72 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 73 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 74 | size_t dim = 0; |
| 75 | while (*c == '[') { |
| 76 | dim++; |
| 77 | c++; |
| 78 | } |
| 79 | |
| 80 | // Reference or primitive? |
| 81 | if (*c == 'L') { |
| 82 | // "[[La/b/C;" -> "a.b.C[][]". |
| 83 | c++; // Skip the 'L'. |
| 84 | } else { |
| 85 | // "[[B" -> "byte[][]". |
| 86 | // To make life easier, we make primitives look like unqualified |
| 87 | // reference types. |
| 88 | switch (*c) { |
| 89 | case 'B': c = "byte;"; break; |
| 90 | case 'C': c = "char;"; break; |
| 91 | case 'D': c = "double;"; break; |
| 92 | case 'F': c = "float;"; break; |
| 93 | case 'I': c = "int;"; break; |
| 94 | case 'J': c = "long;"; break; |
| 95 | case 'S': c = "short;"; break; |
| 96 | case 'Z': c = "boolean;"; break; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 97 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 102 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 103 | std::string result; |
| 104 | const char* p = c; |
| 105 | while (*p != ';') { |
| 106 | char ch = *p++; |
| 107 | if (ch == '/') { |
| 108 | ch = '.'; |
| 109 | } |
| 110 | result.push_back(ch); |
| 111 | } |
| 112 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 113 | while (dim--) { |
| 114 | result += "[]"; |
| 115 | } |
| 116 | return result; |
| 117 | } |
| 118 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 119 | std::string PrettyDescriptor(Primitive::Type type) { |
| 120 | char descriptor_char = Primitive::DescriptorChar(type); |
| 121 | std::string descriptor_string(1, descriptor_char); |
| 122 | return PrettyDescriptor(descriptor_string); |
| 123 | } |
| 124 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 125 | std::string PrettyField(const Field* f, bool with_type) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 126 | if (f == NULL) { |
| 127 | return "null"; |
| 128 | } |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 129 | std::string result; |
| 130 | if (with_type) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 131 | result += PrettyDescriptor(f->GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 132 | result += ' '; |
| 133 | } |
| 134 | result += PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 135 | result += '.'; |
| 136 | result += f->GetName()->ToModifiedUtf8(); |
| 137 | return result; |
| 138 | } |
| 139 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 140 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 141 | if (m == NULL) { |
| 142 | return "null"; |
| 143 | } |
| 144 | Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 145 | std::string result(PrettyDescriptor(c->GetDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 146 | result += '.'; |
| 147 | result += m->GetName()->ToModifiedUtf8(); |
| 148 | if (with_signature) { |
| 149 | // TODO: iterate over the signature's elements and pass them all to |
| 150 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
| 151 | result += m->GetSignature()->ToModifiedUtf8(); |
| 152 | } |
| 153 | return result; |
| 154 | } |
| 155 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 156 | std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) { |
| 157 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 158 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 159 | result += '.'; |
| 160 | result += dex_file.GetMethodName(method_id); |
| 161 | if (with_signature) { |
| 162 | // TODO: iterate over the signature's elements and pass them all to |
| 163 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
| 164 | result += dex_file.GetMethodSignature(method_id); |
| 165 | } |
| 166 | return result; |
| 167 | } |
| 168 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 169 | std::string PrettyTypeOf(const Object* obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 170 | if (obj == NULL) { |
| 171 | return "null"; |
| 172 | } |
| 173 | if (obj->GetClass() == NULL) { |
| 174 | return "(raw)"; |
| 175 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 176 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 177 | if (obj->IsClass()) { |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 178 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 179 | } |
| 180 | return result; |
| 181 | } |
| 182 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 183 | std::string PrettyClass(const Class* c) { |
| 184 | if (c == NULL) { |
| 185 | return "null"; |
| 186 | } |
| 187 | std::string result; |
| 188 | result += "java.lang.Class<"; |
| 189 | result += PrettyDescriptor(c->GetDescriptor()); |
| 190 | result += ">"; |
| 191 | return result; |
| 192 | } |
| 193 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 194 | std::string PrettyClassAndClassLoader(const Class* c) { |
| 195 | if (c == NULL) { |
| 196 | return "null"; |
| 197 | } |
| 198 | std::string result; |
| 199 | result += "java.lang.Class<"; |
| 200 | result += PrettyDescriptor(c->GetDescriptor()); |
| 201 | result += ","; |
| 202 | result += PrettyTypeOf(c->GetClassLoader()); |
| 203 | // TODO: add an identifying hash value for the loader |
| 204 | result += ">"; |
| 205 | return result; |
| 206 | } |
| 207 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 208 | std::string MangleForJni(const std::string& s) { |
| 209 | std::string result; |
| 210 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 211 | const char* cp = &s[0]; |
| 212 | for (size_t i = 0; i < char_count; ++i) { |
| 213 | uint16_t ch = GetUtf16FromUtf8(&cp); |
| 214 | if (ch == '$' || ch > 127) { |
| 215 | StringAppendF(&result, "_0%04x", ch); |
| 216 | } else { |
| 217 | switch (ch) { |
| 218 | case '_': |
| 219 | result += "_1"; |
| 220 | break; |
| 221 | case ';': |
| 222 | result += "_2"; |
| 223 | break; |
| 224 | case '[': |
| 225 | result += "_3"; |
| 226 | break; |
| 227 | case '/': |
| 228 | result += "_"; |
| 229 | break; |
| 230 | default: |
| 231 | result.push_back(ch); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | return result; |
| 237 | } |
| 238 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 239 | std::string DotToDescriptor(const char* class_name) { |
| 240 | std::string descriptor(class_name); |
| 241 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 242 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 243 | descriptor = "L" + descriptor + ";"; |
| 244 | } |
| 245 | return descriptor; |
| 246 | } |
| 247 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 248 | std::string DescriptorToDot(const std::string& descriptor) { |
| 249 | DCHECK_EQ(descriptor[0], 'L'); |
| 250 | DCHECK_EQ(descriptor[descriptor.size()-1], ';'); |
| 251 | std::string dot = descriptor.substr(1, descriptor.size()-2); |
| 252 | std::replace(dot.begin(), dot.end(), '/', '.'); |
| 253 | return dot; |
| 254 | } |
| 255 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 256 | std::string JniShortName(const Method* m) { |
| 257 | Class* declaring_class = m->GetDeclaringClass(); |
| 258 | |
| 259 | std::string class_name(declaring_class->GetDescriptor()->ToModifiedUtf8()); |
| 260 | // Remove the leading 'L' and trailing ';'... |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 261 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 262 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 263 | class_name.erase(0, 1); |
| 264 | class_name.erase(class_name.size() - 1, 1); |
| 265 | |
| 266 | std::string method_name(m->GetName()->ToModifiedUtf8()); |
| 267 | |
| 268 | std::string short_name; |
| 269 | short_name += "Java_"; |
| 270 | short_name += MangleForJni(class_name); |
| 271 | short_name += "_"; |
| 272 | short_name += MangleForJni(method_name); |
| 273 | return short_name; |
| 274 | } |
| 275 | |
| 276 | std::string JniLongName(const Method* m) { |
| 277 | std::string long_name; |
| 278 | long_name += JniShortName(m); |
| 279 | long_name += "__"; |
| 280 | |
| 281 | std::string signature(m->GetSignature()->ToModifiedUtf8()); |
| 282 | signature.erase(0, 1); |
| 283 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 284 | |
| 285 | long_name += MangleForJni(signature); |
| 286 | |
| 287 | return long_name; |
| 288 | } |
| 289 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 290 | // Helper for IsValidMemberNameUtf8(), a bit vector indicating valid low ascii. |
| 291 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
| 292 | 0x00000000, // 00..1f low control characters; nothing valid |
| 293 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 294 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 295 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
| 296 | }; |
| 297 | |
| 298 | // Helper for IsValidMemberNameUtf8(); do not call directly. |
| 299 | bool IsValidMemberNameUtf8Slow(const char** pUtf8Ptr) { |
| 300 | /* |
| 301 | * It's a multibyte encoded character. Decode it and analyze. We |
| 302 | * accept anything that isn't (a) an improperly encoded low value, |
| 303 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 304 | * control character, or (e) a high space, layout, or special |
| 305 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 306 | * U+fff0..U+ffff). This is all specified in the dex format |
| 307 | * document. |
| 308 | */ |
| 309 | |
| 310 | uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 311 | |
| 312 | // Perform follow-up tests based on the high 8 bits. |
| 313 | switch (utf16 >> 8) { |
| 314 | case 0x00: |
| 315 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 316 | return (utf16 > 0x00a0); |
| 317 | case 0xd8: |
| 318 | case 0xd9: |
| 319 | case 0xda: |
| 320 | case 0xdb: |
| 321 | // It's a leading surrogate. Check to see that a trailing |
| 322 | // surrogate follows. |
| 323 | utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 324 | return (utf16 >= 0xdc00) && (utf16 <= 0xdfff); |
| 325 | case 0xdc: |
| 326 | case 0xdd: |
| 327 | case 0xde: |
| 328 | case 0xdf: |
| 329 | // It's a trailing surrogate, which is not valid at this point. |
| 330 | return false; |
| 331 | case 0x20: |
| 332 | case 0xff: |
| 333 | // It's in the range that has spaces, controls, and specials. |
| 334 | switch (utf16 & 0xfff8) { |
| 335 | case 0x2000: |
| 336 | case 0x2008: |
| 337 | case 0x2028: |
| 338 | case 0xfff0: |
| 339 | case 0xfff8: |
| 340 | return false; |
| 341 | } |
| 342 | break; |
| 343 | } |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 348 | * valid as part of a member name, updating the pointer to point past |
| 349 | * the consumed character. This will consume two encoded UTF-16 code |
| 350 | * points if the character is encoded as a surrogate pair. Also, if |
| 351 | * this function returns false, then the given pointer may only have |
| 352 | * been partially advanced. |
| 353 | */ |
| 354 | bool IsValidMemberNameUtf8(const char** pUtf8Ptr) { |
| 355 | uint8_t c = (uint8_t) **pUtf8Ptr; |
| 356 | if (c <= 0x7f) { |
| 357 | // It's low-ascii, so check the table. |
| 358 | uint32_t wordIdx = c >> 5; |
| 359 | uint32_t bitIdx = c & 0x1f; |
| 360 | (*pUtf8Ptr)++; |
| 361 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 362 | } |
| 363 | |
| 364 | // It's a multibyte encoded character. Call a non-inline function |
| 365 | // for the heavy lifting. |
| 366 | return IsValidMemberNameUtf8Slow(pUtf8Ptr); |
| 367 | } |
| 368 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 369 | enum ClassNameType { kName, kDescriptor }; |
| 370 | bool IsValidClassName(const char* s, ClassNameType type, char separator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 371 | int arrayCount = 0; |
| 372 | while (*s == '[') { |
| 373 | arrayCount++; |
| 374 | s++; |
| 375 | } |
| 376 | |
| 377 | if (arrayCount > 255) { |
| 378 | // Arrays may have no more than 255 dimensions. |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | if (arrayCount != 0) { |
| 383 | /* |
| 384 | * If we're looking at an array of some sort, then it doesn't |
| 385 | * matter if what is being asked for is a class name; the |
| 386 | * format looks the same as a type descriptor in that case, so |
| 387 | * treat it as such. |
| 388 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 389 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 392 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 393 | /* |
| 394 | * We are looking for a descriptor. Either validate it as a |
| 395 | * single-character primitive type, or continue on to check the |
| 396 | * embedded class name (bracketed by "L" and ";"). |
| 397 | */ |
| 398 | switch (*(s++)) { |
| 399 | case 'B': |
| 400 | case 'C': |
| 401 | case 'D': |
| 402 | case 'F': |
| 403 | case 'I': |
| 404 | case 'J': |
| 405 | case 'S': |
| 406 | case 'Z': |
| 407 | // These are all single-character descriptors for primitive types. |
| 408 | return (*s == '\0'); |
| 409 | case 'V': |
| 410 | // Non-array void is valid, but you can't have an array of void. |
| 411 | return (arrayCount == 0) && (*s == '\0'); |
| 412 | case 'L': |
| 413 | // Class name: Break out and continue below. |
| 414 | break; |
| 415 | default: |
| 416 | // Oddball descriptor character. |
| 417 | return false; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * We just consumed the 'L' that introduces a class name as part |
| 423 | * of a type descriptor, or we are looking for an unadorned class |
| 424 | * name. |
| 425 | */ |
| 426 | |
| 427 | bool sepOrFirst = true; // first character or just encountered a separator. |
| 428 | for (;;) { |
| 429 | uint8_t c = (uint8_t) *s; |
| 430 | switch (c) { |
| 431 | case '\0': |
| 432 | /* |
| 433 | * Premature end for a type descriptor, but valid for |
| 434 | * a class name as long as we haven't encountered an |
| 435 | * empty component (including the degenerate case of |
| 436 | * the empty string ""). |
| 437 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 438 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 439 | case ';': |
| 440 | /* |
| 441 | * Invalid character for a class name, but the |
| 442 | * legitimate end of a type descriptor. In the latter |
| 443 | * case, make sure that this is the end of the string |
| 444 | * and that it doesn't end with an empty component |
| 445 | * (including the degenerate case of "L;"). |
| 446 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 447 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 448 | case '/': |
| 449 | case '.': |
| 450 | if (c != separator) { |
| 451 | // The wrong separator character. |
| 452 | return false; |
| 453 | } |
| 454 | if (sepOrFirst) { |
| 455 | // Separator at start or two separators in a row. |
| 456 | return false; |
| 457 | } |
| 458 | sepOrFirst = true; |
| 459 | s++; |
| 460 | break; |
| 461 | default: |
| 462 | if (!IsValidMemberNameUtf8(&s)) { |
| 463 | return false; |
| 464 | } |
| 465 | sepOrFirst = false; |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 471 | bool IsValidBinaryClassName(const char* s) { |
| 472 | return IsValidClassName(s, kName, '.'); |
| 473 | } |
| 474 | |
| 475 | bool IsValidJniClassName(const char* s) { |
| 476 | return IsValidClassName(s, kName, '/'); |
| 477 | } |
| 478 | |
| 479 | bool IsValidDescriptor(const char* s) { |
| 480 | return IsValidClassName(s, kDescriptor, '/'); |
| 481 | } |
| 482 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 483 | void Split(const std::string& s, char delim, std::vector<std::string>& result) { |
| 484 | const char* p = s.data(); |
| 485 | const char* end = p + s.size(); |
| 486 | while (p != end) { |
| 487 | if (*p == delim) { |
| 488 | ++p; |
| 489 | } else { |
| 490 | const char* start = p; |
| 491 | while (++p != end && *p != delim) { |
| 492 | // Skip to the next occurrence of the delimiter. |
| 493 | } |
| 494 | result.push_back(std::string(start, p - start)); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 499 | void SetThreadName(const char* threadName) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 500 | int hasAt = 0; |
| 501 | int hasDot = 0; |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 502 | const char* s = threadName; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 503 | while (*s) { |
| 504 | if (*s == '.') { |
| 505 | hasDot = 1; |
| 506 | } else if (*s == '@') { |
| 507 | hasAt = 1; |
| 508 | } |
| 509 | s++; |
| 510 | } |
| 511 | int len = s - threadName; |
| 512 | if (len < 15 || hasAt || !hasDot) { |
| 513 | s = threadName; |
| 514 | } else { |
| 515 | s = threadName + len - 15; |
| 516 | } |
| 517 | #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP) |
| 518 | /* pthread_setname_np fails rather than truncating long strings */ |
| 519 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 520 | strncpy(buf, s, sizeof(buf)-1); |
| 521 | buf[sizeof(buf)-1] = '\0'; |
| 522 | errno = pthread_setname_np(pthread_self(), buf); |
| 523 | if (errno != 0) { |
| 524 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 525 | } |
| 526 | #elif defined(HAVE_PRCTL) |
| 527 | prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0); |
| 528 | #else |
| 529 | #error no implementation for SetThreadName |
| 530 | #endif |
| 531 | } |
| 532 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 533 | void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu) { |
| 534 | utime = stime = task_cpu = 0; |
| 535 | std::string stats; |
| 536 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 537 | return; |
| 538 | } |
| 539 | // Skip the command, which may contain spaces. |
| 540 | stats = stats.substr(stats.find(')') + 2); |
| 541 | // Extract the three fields we care about. |
| 542 | std::vector<std::string> fields; |
| 543 | Split(stats, ' ', fields); |
| 544 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 545 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 546 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 547 | } |
| 548 | |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 549 | std::string GetArtCacheOrDie() { |
| 550 | const char* data_root = getenv("ANDROID_DATA"); |
| 551 | if (data_root == NULL) { |
| 552 | if (OS::DirectoryExists("/data")) { |
| 553 | data_root = "/data"; |
| 554 | } else { |
| 555 | data_root = "/tmp"; |
| 556 | } |
| 557 | } |
| 558 | if (!OS::DirectoryExists(data_root)) { |
| 559 | LOG(FATAL) << "Failed to find ANDROID_DATA directory " << data_root; |
| 560 | return ""; |
| 561 | } |
| 562 | |
| 563 | std::string art_cache = StringPrintf("%s/art-cache", data_root); |
| 564 | |
| 565 | if (!OS::DirectoryExists(art_cache.c_str())) { |
| 566 | if (StringPiece(art_cache).starts_with("/tmp/")) { |
| 567 | int result = mkdir(art_cache.c_str(), 0700); |
| 568 | if (result != 0) { |
| 569 | LOG(FATAL) << "Failed to create art-cache directory " << art_cache; |
| 570 | return ""; |
| 571 | } |
| 572 | } else { |
| 573 | LOG(FATAL) << "Failed to find art-cache directory " << art_cache; |
| 574 | return ""; |
| 575 | } |
| 576 | } |
| 577 | return art_cache; |
| 578 | } |
| 579 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 580 | std::string GetArtCacheFilenameOrDie(const std::string& location) { |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 581 | std::string art_cache = GetArtCacheOrDie(); |
| 582 | CHECK_EQ(location[0], '/'); |
| 583 | std::string cache_file(location, 1); // skip leading slash |
| 584 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
| 585 | return art_cache + "/" + cache_file; |
| 586 | } |
| 587 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 588 | bool IsValidZipFilename(const std::string& filename) { |
| 589 | if (filename.size() < 4) { |
| 590 | return false; |
| 591 | } |
| 592 | std::string suffix(filename.substr(filename.size() - 4)); |
| 593 | return (suffix == ".zip" || suffix == ".jar" || suffix == ".apk"); |
| 594 | } |
| 595 | |
| 596 | bool IsValidDexFilename(const std::string& filename) { |
| 597 | if (filename.size() < 4) { |
| 598 | return false; |
| 599 | } |
| 600 | std::string suffix(filename.substr(filename.size() - 4)); |
| 601 | return (suffix == ".dex"); |
| 602 | } |
| 603 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 604 | } // namespace art |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 605 | |
| 606 | // Neither bionic nor glibc exposes gettid(2). |
| 607 | #define __KERNEL__ |
| 608 | #include <linux/unistd.h> |
| 609 | namespace art { |
| 610 | #ifdef _syscall0 |
| 611 | _syscall0(pid_t, GetTid) |
| 612 | #else |
| 613 | pid_t GetTid() { return syscall(__NR_gettid); } |
| 614 | #endif |
| 615 | } // namespace art |
| 616 | #undef __KERNEL__ |