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