blob: 0d6aff956b593e728b96e2d424eaeea14c8f9bb0 [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) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800129 std::string descriptor_string(Primitive::Descriptor(type));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700130 return PrettyDescriptor(descriptor_string);
131}
132
Elliott Hughes54e7df12011-09-16 11:47:04 -0700133std::string PrettyField(const Field* f, bool with_type) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700134 if (f == NULL) {
135 return "null";
136 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800137 FieldHelper fh(f);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700138 std::string result;
139 if (with_type) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 result += PrettyDescriptor(fh.GetTypeDescriptor());
Elliott Hughes54e7df12011-09-16 11:47:04 -0700141 result += ' ';
142 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 result += PrettyDescriptor(fh.GetDeclaringClassDescriptor());
Elliott Hughesa2501992011-08-26 19:39:54 -0700144 result += '.';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800145 result += fh.GetName();
Elliott Hughesa2501992011-08-26 19:39:54 -0700146 return result;
147}
148
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700149std::string PrettyMethod(const Method* m, bool with_signature) {
150 if (m == NULL) {
151 return "null";
152 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153 MethodHelper mh(m);
154 std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700155 result += '.';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800156 result += mh.GetName();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700157 if (with_signature) {
158 // TODO: iterate over the signature's elements and pass them all to
159 // PrettyDescriptor? We'd need to pull out the return type specially, too.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800160 result += mh.GetSignature();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700161 }
162 return result;
163}
164
Ian Rogers0571d352011-11-03 19:51:38 -0700165std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) {
166 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
167 std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id)));
168 result += '.';
169 result += dex_file.GetMethodName(method_id);
170 if (with_signature) {
171 // TODO: iterate over the signature's elements and pass them all to
172 // PrettyDescriptor? We'd need to pull out the return type specially, too.
173 result += dex_file.GetMethodSignature(method_id);
174 }
175 return result;
176}
177
Elliott Hughes54e7df12011-09-16 11:47:04 -0700178std::string PrettyTypeOf(const Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700179 if (obj == NULL) {
180 return "null";
181 }
182 if (obj->GetClass() == NULL) {
183 return "(raw)";
184 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 ClassHelper kh(obj->GetClass());
186 std::string result(PrettyDescriptor(kh.GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700187 if (obj->IsClass()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800188 kh.ChangeClass(obj->AsClass());
189 result += "<" + PrettyDescriptor(kh.GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700190 }
191 return result;
192}
193
Elliott Hughes54e7df12011-09-16 11:47:04 -0700194std::string PrettyClass(const Class* c) {
195 if (c == NULL) {
196 return "null";
197 }
198 std::string result;
199 result += "java.lang.Class<";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800200 result += PrettyDescriptor(c);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700201 result += ">";
202 return result;
203}
204
Ian Rogersd81871c2011-10-03 13:57:23 -0700205std::string PrettyClassAndClassLoader(const Class* c) {
206 if (c == NULL) {
207 return "null";
208 }
209 std::string result;
210 result += "java.lang.Class<";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800211 result += PrettyDescriptor(c);
Ian Rogersd81871c2011-10-03 13:57:23 -0700212 result += ",";
213 result += PrettyTypeOf(c->GetClassLoader());
214 // TODO: add an identifying hash value for the loader
215 result += ">";
216 return result;
217}
218
Elliott Hughes79082e32011-08-25 12:07:32 -0700219std::string MangleForJni(const std::string& s) {
220 std::string result;
221 size_t char_count = CountModifiedUtf8Chars(s.c_str());
222 const char* cp = &s[0];
223 for (size_t i = 0; i < char_count; ++i) {
224 uint16_t ch = GetUtf16FromUtf8(&cp);
225 if (ch == '$' || ch > 127) {
226 StringAppendF(&result, "_0%04x", ch);
227 } else {
228 switch (ch) {
229 case '_':
230 result += "_1";
231 break;
232 case ';':
233 result += "_2";
234 break;
235 case '[':
236 result += "_3";
237 break;
238 case '/':
239 result += "_";
240 break;
241 default:
242 result.push_back(ch);
243 break;
244 }
245 }
246 }
247 return result;
248}
249
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700250std::string DotToDescriptor(const char* class_name) {
251 std::string descriptor(class_name);
252 std::replace(descriptor.begin(), descriptor.end(), '.', '/');
253 if (descriptor.length() > 0 && descriptor[0] != '[') {
254 descriptor = "L" + descriptor + ";";
255 }
256 return descriptor;
257}
258
Brian Carlstromaded5f72011-10-07 17:15:04 -0700259std::string DescriptorToDot(const std::string& descriptor) {
260 DCHECK_EQ(descriptor[0], 'L');
261 DCHECK_EQ(descriptor[descriptor.size()-1], ';');
Elliott Hughes95572412011-12-13 18:14:20 -0800262 std::string dot(descriptor.substr(1, descriptor.size() - 2));
Brian Carlstromaded5f72011-10-07 17:15:04 -0700263 std::replace(dot.begin(), dot.end(), '/', '.');
264 return dot;
265}
266
Elliott Hughes79082e32011-08-25 12:07:32 -0700267std::string JniShortName(const Method* m) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800268 MethodHelper mh(m);
269 std::string class_name(mh.GetDeclaringClassDescriptor());
Elliott Hughes79082e32011-08-25 12:07:32 -0700270 // Remove the leading 'L' and trailing ';'...
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700271 CHECK_EQ(class_name[0], 'L') << class_name;
272 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
Elliott Hughes79082e32011-08-25 12:07:32 -0700273 class_name.erase(0, 1);
274 class_name.erase(class_name.size() - 1, 1);
275
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 std::string method_name(mh.GetName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700277
278 std::string short_name;
279 short_name += "Java_";
280 short_name += MangleForJni(class_name);
281 short_name += "_";
282 short_name += MangleForJni(method_name);
283 return short_name;
284}
285
286std::string JniLongName(const Method* m) {
287 std::string long_name;
288 long_name += JniShortName(m);
289 long_name += "__";
290
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 std::string signature(MethodHelper(m).GetSignature());
Elliott Hughes79082e32011-08-25 12:07:32 -0700292 signature.erase(0, 1);
293 signature.erase(signature.begin() + signature.find(')'), signature.end());
294
295 long_name += MangleForJni(signature);
296
297 return long_name;
298}
299
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700300// Helper for IsValidMemberNameUtf8(), a bit vector indicating valid low ascii.
301uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = {
302 0x00000000, // 00..1f low control characters; nothing valid
303 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
304 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
305 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z'
306};
307
308// Helper for IsValidMemberNameUtf8(); do not call directly.
309bool IsValidMemberNameUtf8Slow(const char** pUtf8Ptr) {
310 /*
311 * It's a multibyte encoded character. Decode it and analyze. We
312 * accept anything that isn't (a) an improperly encoded low value,
313 * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
314 * control character, or (e) a high space, layout, or special
315 * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
316 * U+fff0..U+ffff). This is all specified in the dex format
317 * document.
318 */
319
320 uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr);
321
322 // Perform follow-up tests based on the high 8 bits.
323 switch (utf16 >> 8) {
324 case 0x00:
325 // It's only valid if it's above the ISO-8859-1 high space (0xa0).
326 return (utf16 > 0x00a0);
327 case 0xd8:
328 case 0xd9:
329 case 0xda:
330 case 0xdb:
331 // It's a leading surrogate. Check to see that a trailing
332 // surrogate follows.
333 utf16 = GetUtf16FromUtf8(pUtf8Ptr);
334 return (utf16 >= 0xdc00) && (utf16 <= 0xdfff);
335 case 0xdc:
336 case 0xdd:
337 case 0xde:
338 case 0xdf:
339 // It's a trailing surrogate, which is not valid at this point.
340 return false;
341 case 0x20:
342 case 0xff:
343 // It's in the range that has spaces, controls, and specials.
344 switch (utf16 & 0xfff8) {
345 case 0x2000:
346 case 0x2008:
347 case 0x2028:
348 case 0xfff0:
349 case 0xfff8:
350 return false;
351 }
352 break;
353 }
354 return true;
355}
356
357/* Return whether the pointed-at modified-UTF-8 encoded character is
358 * valid as part of a member name, updating the pointer to point past
359 * the consumed character. This will consume two encoded UTF-16 code
360 * points if the character is encoded as a surrogate pair. Also, if
361 * this function returns false, then the given pointer may only have
362 * been partially advanced.
363 */
364bool IsValidMemberNameUtf8(const char** pUtf8Ptr) {
365 uint8_t c = (uint8_t) **pUtf8Ptr;
366 if (c <= 0x7f) {
367 // It's low-ascii, so check the table.
368 uint32_t wordIdx = c >> 5;
369 uint32_t bitIdx = c & 0x1f;
370 (*pUtf8Ptr)++;
371 return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
372 }
373
374 // It's a multibyte encoded character. Call a non-inline function
375 // for the heavy lifting.
376 return IsValidMemberNameUtf8Slow(pUtf8Ptr);
377}
378
Elliott Hughes906e6852011-10-28 14:52:10 -0700379enum ClassNameType { kName, kDescriptor };
380bool IsValidClassName(const char* s, ClassNameType type, char separator) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700381 int arrayCount = 0;
382 while (*s == '[') {
383 arrayCount++;
384 s++;
385 }
386
387 if (arrayCount > 255) {
388 // Arrays may have no more than 255 dimensions.
389 return false;
390 }
391
392 if (arrayCount != 0) {
393 /*
394 * If we're looking at an array of some sort, then it doesn't
395 * matter if what is being asked for is a class name; the
396 * format looks the same as a type descriptor in that case, so
397 * treat it as such.
398 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700399 type = kDescriptor;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700400 }
401
Elliott Hughes906e6852011-10-28 14:52:10 -0700402 if (type == kDescriptor) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700403 /*
404 * We are looking for a descriptor. Either validate it as a
405 * single-character primitive type, or continue on to check the
406 * embedded class name (bracketed by "L" and ";").
407 */
408 switch (*(s++)) {
409 case 'B':
410 case 'C':
411 case 'D':
412 case 'F':
413 case 'I':
414 case 'J':
415 case 'S':
416 case 'Z':
417 // These are all single-character descriptors for primitive types.
418 return (*s == '\0');
419 case 'V':
420 // Non-array void is valid, but you can't have an array of void.
421 return (arrayCount == 0) && (*s == '\0');
422 case 'L':
423 // Class name: Break out and continue below.
424 break;
425 default:
426 // Oddball descriptor character.
427 return false;
428 }
429 }
430
431 /*
432 * We just consumed the 'L' that introduces a class name as part
433 * of a type descriptor, or we are looking for an unadorned class
434 * name.
435 */
436
437 bool sepOrFirst = true; // first character or just encountered a separator.
438 for (;;) {
439 uint8_t c = (uint8_t) *s;
440 switch (c) {
441 case '\0':
442 /*
443 * Premature end for a type descriptor, but valid for
444 * a class name as long as we haven't encountered an
445 * empty component (including the degenerate case of
446 * the empty string "").
447 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700448 return (type == kName) && !sepOrFirst;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700449 case ';':
450 /*
451 * Invalid character for a class name, but the
452 * legitimate end of a type descriptor. In the latter
453 * case, make sure that this is the end of the string
454 * and that it doesn't end with an empty component
455 * (including the degenerate case of "L;").
456 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700457 return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0');
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700458 case '/':
459 case '.':
460 if (c != separator) {
461 // The wrong separator character.
462 return false;
463 }
464 if (sepOrFirst) {
465 // Separator at start or two separators in a row.
466 return false;
467 }
468 sepOrFirst = true;
469 s++;
470 break;
471 default:
472 if (!IsValidMemberNameUtf8(&s)) {
473 return false;
474 }
475 sepOrFirst = false;
476 break;
477 }
478 }
479}
480
Elliott Hughes906e6852011-10-28 14:52:10 -0700481bool IsValidBinaryClassName(const char* s) {
482 return IsValidClassName(s, kName, '.');
483}
484
485bool IsValidJniClassName(const char* s) {
486 return IsValidClassName(s, kName, '/');
487}
488
489bool IsValidDescriptor(const char* s) {
490 return IsValidClassName(s, kDescriptor, '/');
491}
492
Elliott Hughes34023802011-08-30 12:06:17 -0700493void Split(const std::string& s, char delim, std::vector<std::string>& result) {
494 const char* p = s.data();
495 const char* end = p + s.size();
496 while (p != end) {
497 if (*p == delim) {
498 ++p;
499 } else {
500 const char* start = p;
501 while (++p != end && *p != delim) {
502 // Skip to the next occurrence of the delimiter.
503 }
504 result.push_back(std::string(start, p - start));
505 }
506 }
507}
508
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800509void SetThreadName(const char* threadName) {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700510 int hasAt = 0;
511 int hasDot = 0;
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800512 const char* s = threadName;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700513 while (*s) {
514 if (*s == '.') {
515 hasDot = 1;
516 } else if (*s == '@') {
517 hasAt = 1;
518 }
519 s++;
520 }
521 int len = s - threadName;
522 if (len < 15 || hasAt || !hasDot) {
523 s = threadName;
524 } else {
525 s = threadName + len - 15;
526 }
527#if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP)
528 /* pthread_setname_np fails rather than truncating long strings */
529 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
530 strncpy(buf, s, sizeof(buf)-1);
531 buf[sizeof(buf)-1] = '\0';
532 errno = pthread_setname_np(pthread_self(), buf);
533 if (errno != 0) {
534 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
535 }
536#elif defined(HAVE_PRCTL)
537 prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);
538#else
539#error no implementation for SetThreadName
540#endif
541}
542
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700543void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu) {
544 utime = stime = task_cpu = 0;
545 std::string stats;
546 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) {
547 return;
548 }
549 // Skip the command, which may contain spaces.
550 stats = stats.substr(stats.find(')') + 2);
551 // Extract the three fields we care about.
552 std::vector<std::string> fields;
553 Split(stats, ' ', fields);
554 utime = strtoull(fields[11].c_str(), NULL, 10);
555 stime = strtoull(fields[12].c_str(), NULL, 10);
556 task_cpu = strtoull(fields[36].c_str(), NULL, 10);
557}
558
Brian Carlstroma9f19782011-10-13 00:14:47 -0700559std::string GetArtCacheOrDie() {
560 const char* data_root = getenv("ANDROID_DATA");
561 if (data_root == NULL) {
562 if (OS::DirectoryExists("/data")) {
563 data_root = "/data";
564 } else {
565 data_root = "/tmp";
566 }
567 }
568 if (!OS::DirectoryExists(data_root)) {
569 LOG(FATAL) << "Failed to find ANDROID_DATA directory " << data_root;
570 return "";
571 }
572
Elliott Hughes95572412011-12-13 18:14:20 -0800573 std::string art_cache(StringPrintf("%s/art-cache", data_root));
Brian Carlstroma9f19782011-10-13 00:14:47 -0700574
575 if (!OS::DirectoryExists(art_cache.c_str())) {
576 if (StringPiece(art_cache).starts_with("/tmp/")) {
577 int result = mkdir(art_cache.c_str(), 0700);
578 if (result != 0) {
579 LOG(FATAL) << "Failed to create art-cache directory " << art_cache;
580 return "";
581 }
582 } else {
583 LOG(FATAL) << "Failed to find art-cache directory " << art_cache;
584 return "";
585 }
586 }
587 return art_cache;
588}
589
jeffhao262bf462011-10-20 18:36:32 -0700590std::string GetArtCacheFilenameOrDie(const std::string& location) {
Elliott Hughes95572412011-12-13 18:14:20 -0800591 std::string art_cache(GetArtCacheOrDie());
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700592 CHECK_EQ(location[0], '/');
593 std::string cache_file(location, 1); // skip leading slash
594 std::replace(cache_file.begin(), cache_file.end(), '/', '@');
595 return art_cache + "/" + cache_file;
596}
597
jeffhao262bf462011-10-20 18:36:32 -0700598bool IsValidZipFilename(const std::string& filename) {
599 if (filename.size() < 4) {
600 return false;
601 }
602 std::string suffix(filename.substr(filename.size() - 4));
603 return (suffix == ".zip" || suffix == ".jar" || suffix == ".apk");
604}
605
606bool IsValidDexFilename(const std::string& filename) {
607 if (filename.size() < 4) {
608 return false;
609 }
610 std::string suffix(filename.substr(filename.size() - 4));
611 return (suffix == ".dex");
612}
613
Elliott Hughes11e45072011-08-16 17:40:46 -0700614} // namespace art
Elliott Hughes42ee1422011-09-06 12:33:32 -0700615
616// Neither bionic nor glibc exposes gettid(2).
617#define __KERNEL__
618#include <linux/unistd.h>
619namespace art {
620#ifdef _syscall0
621_syscall0(pid_t, GetTid)
622#else
623pid_t GetTid() { return syscall(__NR_gettid); }
624#endif
625} // namespace art
626#undef __KERNEL__