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