blob: bc51c2a254bef62d7dcac7f3b50534dbab35ca3e [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"
buzbeec143c552011-08-20 17:38:58 -070013#include "file.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070014#include "object.h"
buzbeec143c552011-08-20 17:38:58 -070015#include "os.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Elliott Hughesdcc24742011-09-07 14:02:44 -070017#if defined(HAVE_PRCTL)
18#include <sys/prctl.h>
19#endif
20
Elliott Hughes11e45072011-08-16 17:40:46 -070021namespace art {
22
Elliott Hughesd92bec42011-09-02 17:04:36 -070023bool 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 }
buzbeec143c552011-08-20 17:38:58 -070028
buzbeec143c552011-08-20 17:38:58 -070029 char buf[8 * KB];
30 while (true) {
31 int64_t n = file->Read(buf, sizeof(buf));
Elliott Hughesd92bec42011-09-02 17:04:36 -070032 if (n == -1) {
33 return false;
buzbeec143c552011-08-20 17:38:58 -070034 }
Elliott Hughesd92bec42011-09-02 17:04:36 -070035 if (n == 0) {
36 return true;
37 }
38 result->append(buf, n);
buzbeec143c552011-08-20 17:38:58 -070039 }
buzbeec143c552011-08-20 17:38:58 -070040}
41
Elliott Hughese27955c2011-08-26 15:21:24 -070042std::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 Hughes83df2ac2011-10-11 16:37:54 -070051uint64_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 Hughes5174fe62011-08-23 15:12:35 -070057std::string PrettyDescriptor(const String* java_descriptor) {
Brian Carlstrome24fa612011-09-29 00:53:55 -070058 if (java_descriptor == NULL) {
59 return "null";
60 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -070061 return PrettyDescriptor(java_descriptor->ToModifiedUtf8());
62}
Elliott Hughes5174fe62011-08-23 15:12:35 -070063
Elliott Hughes6c8867d2011-10-03 16:34:05 -070064std::string PrettyDescriptor(const std::string& descriptor) {
Elliott Hughes11e45072011-08-16 17:40:46 -070065 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -070066 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -070067 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 Hughes5174fe62011-08-23 15:12:35 -070090 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070091 }
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 Hughes54e7df12011-09-16 11:47:04 -0700112std::string PrettyField(const Field* f, bool with_type) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700113 if (f == NULL) {
114 return "null";
115 }
Elliott Hughes54e7df12011-09-16 11:47:04 -0700116 std::string result;
117 if (with_type) {
118 result += PrettyDescriptor(f->GetType()->GetDescriptor());
119 result += ' ';
120 }
121 result += PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor());
Elliott Hughesa2501992011-08-26 19:39:54 -0700122 result += '.';
123 result += f->GetName()->ToModifiedUtf8();
124 return result;
125}
126
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700127std::string PrettyMethod(const Method* m, bool with_signature) {
128 if (m == NULL) {
129 return "null";
130 }
131 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700132 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700133 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 Hughes54e7df12011-09-16 11:47:04 -0700143std::string PrettyTypeOf(const Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700144 if (obj == NULL) {
145 return "null";
146 }
147 if (obj->GetClass() == NULL) {
148 return "(raw)";
149 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700150 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700151 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700152 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700153 }
154 return result;
155}
156
Elliott Hughes54e7df12011-09-16 11:47:04 -0700157std::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 Hughes79082e32011-08-25 12:07:32 -0700168std::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 Carlstromf91c8c32011-09-21 17:30:34 -0700199std::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 Carlstromaded5f72011-10-07 17:15:04 -0700208std::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 Hughes79082e32011-08-25 12:07:32 -0700216std::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 Hughesf5a7a472011-10-07 14:31:02 -0700221 CHECK_EQ(class_name[0], 'L') << class_name;
222 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
Elliott Hughes79082e32011-08-25 12:07:32 -0700223 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
236std::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 Hughes64bf5a32011-09-20 14:43:12 -0700250namespace {
251
252// Helper for IsValidMemberNameUtf8(), a bit vector indicating valid low ascii.
253uint32_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.
261bool 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 */
316bool 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
333bool 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 Hughes34023802011-08-30 12:06:17 -0700436void 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 Hughesdcc24742011-09-07 14:02:44 -0700452void 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 Carlstroma9f19782011-10-13 00:14:47 -0700486std::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
Elliott Hughes11e45072011-08-16 17:40:46 -0700517} // namespace art
Elliott Hughes42ee1422011-09-06 12:33:32 -0700518
519// Neither bionic nor glibc exposes gettid(2).
520#define __KERNEL__
521#include <linux/unistd.h>
522namespace art {
523#ifdef _syscall0
524_syscall0(pid_t, GetTid)
525#else
526pid_t GetTid() { return syscall(__NR_gettid); }
527#endif
528} // namespace art
529#undef __KERNEL__