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