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