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