1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "utils.h"
18
19#include <pthread.h>
20#include <sys/stat.h>
21#include <sys/syscall.h>
22#include <sys/types.h>
23#include <unistd.h>
24
25#include "UniquePtr.h"
26#include "base/unix_file/fd_file.h"
27#include "dex_file-inl.h"
28#include "mirror/art_field-inl.h"
29#include "mirror/art_method-inl.h"
30#include "mirror/class-inl.h"
31#include "mirror/class_loader.h"
32#include "mirror/object-inl.h"
33#include "mirror/object_array-inl.h"
34#include "mirror/string.h"
35#include "object_utils.h"
36#include "os.h"
37#include "utf.h"
38
39#if !defined(HAVE_POSIX_CLOCKS)
40#include <sys/time.h>
41#endif
42
43#if defined(HAVE_PRCTL)
44#include <sys/prctl.h>
45#endif
46
47#if defined(__APPLE__)
48#include "AvailabilityMacros.h"  // For MAC_OS_X_VERSION_MAX_ALLOWED
49#include <sys/syscall.h>
50#endif
51
52#include <corkscrew/backtrace.h>  // For DumpNativeStack.
53#include <corkscrew/demangle.h>  // For DumpNativeStack.
54
55#if defined(__linux__)
56#include <linux/unistd.h>
57#endif
58
59namespace art {
60
61pid_t GetTid() {
62#if defined(__APPLE__)
63  uint64_t owner;
64  CHECK_PTHREAD_CALL(pthread_threadid_np, (NULL, &owner), __FUNCTION__);  // Requires Mac OS 10.6
65  return owner;
66#else
67  // Neither bionic nor glibc exposes gettid(2).
68  return syscall(__NR_gettid);
69#endif
70}
71
72std::string GetThreadName(pid_t tid) {
73  std::string result;
74  if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
75    result.resize(result.size() - 1);  // Lose the trailing '\n'.
76  } else {
77    result = "<unknown>";
78  }
79  return result;
80}
81
82void GetThreadStack(pthread_t thread, void*& stack_base, size_t& stack_size) {
83#if defined(__APPLE__)
84  stack_size = pthread_get_stacksize_np(thread);
85  void* stack_addr = pthread_get_stackaddr_np(thread);
86
87  // Check whether stack_addr is the base or end of the stack.
88  // (On Mac OS 10.7, it's the end.)
89  int stack_variable;
90  if (stack_addr > &stack_variable) {
91    stack_base = reinterpret_cast<byte*>(stack_addr) - stack_size;
92  } else {
93    stack_base = stack_addr;
94  }
95#else
96  pthread_attr_t attributes;
97  CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__);
98  CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &stack_base, &stack_size), __FUNCTION__);
99  CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
100#endif
101}
102
103bool ReadFileToString(const std::string& file_name, std::string* result) {
104  UniquePtr<File> file(new File);
105  if (!file->Open(file_name, O_RDONLY)) {
106    return false;
107  }
108
109  std::vector<char> buf(8 * KB);
110  while (true) {
111    int64_t n = TEMP_FAILURE_RETRY(read(file->Fd(), &buf[0], buf.size()));
112    if (n == -1) {
113      return false;
114    }
115    if (n == 0) {
116      return true;
117    }
118    result->append(&buf[0], n);
119  }
120}
121
122std::string GetIsoDate() {
123  time_t now = time(NULL);
124  tm tmbuf;
125  tm* ptm = localtime_r(&now, &tmbuf);
126  return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d",
127      ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday,
128      ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
129}
130
131uint64_t MilliTime() {
132#if defined(HAVE_POSIX_CLOCKS)
133  timespec now;
134  clock_gettime(CLOCK_MONOTONIC, &now);
135  return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_nsec / 1000000LL;
136#else
137  timeval now;
138  gettimeofday(&now, NULL);
139  return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_usec / 1000LL;
140#endif
141}
142
143uint64_t MicroTime() {
144#if defined(HAVE_POSIX_CLOCKS)
145  timespec now;
146  clock_gettime(CLOCK_MONOTONIC, &now);
147  return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_nsec / 1000LL;
148#else
149  timeval now;
150  gettimeofday(&now, NULL);
151  return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_usec;
152#endif
153}
154
155uint64_t NanoTime() {
156#if defined(HAVE_POSIX_CLOCKS)
157  timespec now;
158  clock_gettime(CLOCK_MONOTONIC, &now);
159  return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec;
160#else
161  timeval now;
162  gettimeofday(&now, NULL);
163  return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL;
164#endif
165}
166
167uint64_t ThreadCpuNanoTime() {
168#if defined(HAVE_POSIX_CLOCKS)
169  timespec now;
170  clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
171  return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec;
172#else
173  UNIMPLEMENTED(WARNING);
174  return -1;
175#endif
176}
177
178void NanoSleep(uint64_t ns) {
179  timespec tm;
180  tm.tv_sec = 0;
181  tm.tv_nsec = ns;
182  nanosleep(&tm, NULL);
183}
184
185void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) {
186  int64_t endSec;
187
188  if (absolute) {
189#if !defined(__APPLE__)
190    clock_gettime(clock, ts);
191#else
192    UNUSED(clock);
193    timeval tv;
194    gettimeofday(&tv, NULL);
195    ts->tv_sec = tv.tv_sec;
196    ts->tv_nsec = tv.tv_usec * 1000;
197#endif
198  } else {
199    ts->tv_sec = 0;
200    ts->tv_nsec = 0;
201  }
202  endSec = ts->tv_sec + ms / 1000;
203  if (UNLIKELY(endSec >= 0x7fffffff)) {
204    std::ostringstream ss;
205    LOG(INFO) << "Note: end time exceeds epoch: " << ss.str();
206    endSec = 0x7ffffffe;
207  }
208  ts->tv_sec = endSec;
209  ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns;
210
211  // Catch rollover.
212  if (ts->tv_nsec >= 1000000000L) {
213    ts->tv_sec++;
214    ts->tv_nsec -= 1000000000L;
215  }
216}
217
218std::string PrettyDescriptor(const mirror::String* java_descriptor) {
219  if (java_descriptor == NULL) {
220    return "null";
221  }
222  return PrettyDescriptor(java_descriptor->ToModifiedUtf8());
223}
224
225std::string PrettyDescriptor(const mirror::Class* klass) {
226  if (klass == NULL) {
227    return "null";
228  }
229  return PrettyDescriptor(ClassHelper(klass).GetDescriptor());
230}
231
232std::string PrettyDescriptor(const std::string& descriptor) {
233  // Count the number of '['s to get the dimensionality.
234  const char* c = descriptor.c_str();
235  size_t dim = 0;
236  while (*c == '[') {
237    dim++;
238    c++;
239  }
240
241  // Reference or primitive?
242  if (*c == 'L') {
243    // "[[La/b/C;" -> "a.b.C[][]".
244    c++;  // Skip the 'L'.
245  } else {
246    // "[[B" -> "byte[][]".
247    // To make life easier, we make primitives look like unqualified
248    // reference types.
249    switch (*c) {
250    case 'B': c = "byte;"; break;
251    case 'C': c = "char;"; break;
252    case 'D': c = "double;"; break;
253    case 'F': c = "float;"; break;
254    case 'I': c = "int;"; break;
255    case 'J': c = "long;"; break;
256    case 'S': c = "short;"; break;
257    case 'Z': c = "boolean;"; break;
258    case 'V': c = "void;"; break;  // Used when decoding return types.
259    default: return descriptor;
260    }
261  }
262
263  // At this point, 'c' is a string of the form "fully/qualified/Type;"
264  // or "primitive;". Rewrite the type with '.' instead of '/':
265  std::string result;
266  const char* p = c;
267  while (*p != ';') {
268    char ch = *p++;
269    if (ch == '/') {
270      ch = '.';
271    }
272    result.push_back(ch);
273  }
274  // ...and replace the semicolon with 'dim' "[]" pairs:
275  while (dim--) {
276    result += "[]";
277  }
278  return result;
279}
280
281std::string PrettyDescriptor(Primitive::Type type) {
282  std::string descriptor_string(Primitive::Descriptor(type));
283  return PrettyDescriptor(descriptor_string);
284}
285
286std::string PrettyField(const mirror::ArtField* f, bool with_type) {
287  if (f == NULL) {
288    return "null";
289  }
290  FieldHelper fh(f);
291  std::string result;
292  if (with_type) {
293    result += PrettyDescriptor(fh.GetTypeDescriptor());
294    result += ' ';
295  }
296  result += PrettyDescriptor(fh.GetDeclaringClassDescriptor());
297  result += '.';
298  result += fh.GetName();
299  return result;
300}
301
302std::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type) {
303  if (field_idx >= dex_file.NumFieldIds()) {
304    return StringPrintf("<<invalid-field-idx-%d>>", field_idx);
305  }
306  const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
307  std::string result;
308  if (with_type) {
309    result += dex_file.GetFieldTypeDescriptor(field_id);
310    result += ' ';
311  }
312  result += PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(field_id));
313  result += '.';
314  result += dex_file.GetFieldName(field_id);
315  return result;
316}
317
318std::string PrettyType(uint32_t type_idx, const DexFile& dex_file) {
319  if (type_idx >= dex_file.NumTypeIds()) {
320    return StringPrintf("<<invalid-type-idx-%d>>", type_idx);
321  }
322  const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx);
323  return PrettyDescriptor(dex_file.GetTypeDescriptor(type_id));
324}
325
326std::string PrettyArguments(const char* signature) {
327  std::string result;
328  result += '(';
329  CHECK_EQ(*signature, '(');
330  ++signature;  // Skip the '('.
331  while (*signature != ')') {
332    size_t argument_length = 0;
333    while (signature[argument_length] == '[') {
334      ++argument_length;
335    }
336    if (signature[argument_length] == 'L') {
337      argument_length = (strchr(signature, ';') - signature + 1);
338    } else {
339      ++argument_length;
340    }
341    std::string argument_descriptor(signature, argument_length);
342    result += PrettyDescriptor(argument_descriptor);
343    if (signature[argument_length] != ')') {
344      result += ", ";
345    }
346    signature += argument_length;
347  }
348  CHECK_EQ(*signature, ')');
349  ++signature;  // Skip the ')'.
350  result += ')';
351  return result;
352}
353
354std::string PrettyReturnType(const char* signature) {
355  const char* return_type = strchr(signature, ')');
356  CHECK(return_type != NULL);
357  ++return_type;  // Skip ')'.
358  return PrettyDescriptor(return_type);
359}
360
361std::string PrettyMethod(const mirror::ArtMethod* m, bool with_signature) {
362  if (m == NULL) {
363    return "null";
364  }
365  MethodHelper mh(m);
366  std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor()));
367  result += '.';
368  result += mh.GetName();
369  if (with_signature) {
370    std::string signature(mh.GetSignature());
371    if (signature == "<no signature>") {
372      return result + signature;
373    }
374    result = PrettyReturnType(signature.c_str()) + " " + result + PrettyArguments(signature.c_str());
375  }
376  return result;
377}
378
379std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) {
380  if (method_idx >= dex_file.NumMethodIds()) {
381    return StringPrintf("<<invalid-method-idx-%d>>", method_idx);
382  }
383  const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
384  std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id)));
385  result += '.';
386  result += dex_file.GetMethodName(method_id);
387  if (with_signature) {
388    std::string signature(dex_file.GetMethodSignature(method_id));
389    if (signature == "<no signature>") {
390      return result + signature;
391    }
392    result = PrettyReturnType(signature.c_str()) + " " + result + PrettyArguments(signature.c_str());
393  }
394  return result;
395}
396
397std::string PrettyTypeOf(const mirror::Object* obj) {
398  if (obj == NULL) {
399    return "null";
400  }
401  if (obj->GetClass() == NULL) {
402    return "(raw)";
403  }
404  ClassHelper kh(obj->GetClass());
405  std::string result(PrettyDescriptor(kh.GetDescriptor()));
406  if (obj->IsClass()) {
407    kh.ChangeClass(obj->AsClass());
408    result += "<" + PrettyDescriptor(kh.GetDescriptor()) + ">";
409  }
410  return result;
411}
412
413std::string PrettyClass(const mirror::Class* c) {
414  if (c == NULL) {
415    return "null";
416  }
417  std::string result;
418  result += "java.lang.Class<";
419  result += PrettyDescriptor(c);
420  result += ">";
421  return result;
422}
423
424std::string PrettyClassAndClassLoader(const mirror::Class* c) {
425  if (c == NULL) {
426    return "null";
427  }
428  std::string result;
429  result += "java.lang.Class<";
430  result += PrettyDescriptor(c);
431  result += ",";
432  result += PrettyTypeOf(c->GetClassLoader());
433  // TODO: add an identifying hash value for the loader
434  result += ">";
435  return result;
436}
437
438std::string PrettySize(size_t byte_count) {
439  // The byte thresholds at which we display amounts.  A byte count is displayed
440  // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
441  static const size_t kUnitThresholds[] = {
442    0,              // B up to...
443    3*1024,         // KB up to...
444    2*1024*1024,    // MB up to...
445    1024*1024*1024  // GB from here.
446  };
447  static const size_t kBytesPerUnit[] = { 1, KB, MB, GB };
448  static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
449
450  int i = arraysize(kUnitThresholds);
451  while (--i > 0) {
452    if (byte_count >= kUnitThresholds[i]) {
453      break;
454    }
455  }
456
457  return StringPrintf("%zd%s", byte_count / kBytesPerUnit[i], kUnitStrings[i]);
458}
459
460std::string PrettyDuration(uint64_t nano_duration) {
461  if (nano_duration == 0) {
462    return "0";
463  } else {
464    return FormatDuration(nano_duration, GetAppropriateTimeUnit(nano_duration));
465  }
466}
467
468TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration) {
469  const uint64_t one_sec = 1000 * 1000 * 1000;
470  const uint64_t one_ms  = 1000 * 1000;
471  const uint64_t one_us  = 1000;
472  if (nano_duration >= one_sec) {
473    return kTimeUnitSecond;
474  } else if (nano_duration >= one_ms) {
475    return kTimeUnitMillisecond;
476  } else if (nano_duration >= one_us) {
477    return kTimeUnitMicrosecond;
478  } else {
479    return kTimeUnitNanosecond;
480  }
481}
482
483uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit) {
484  const uint64_t one_sec = 1000 * 1000 * 1000;
485  const uint64_t one_ms  = 1000 * 1000;
486  const uint64_t one_us  = 1000;
487
488  switch (time_unit) {
489    case kTimeUnitSecond:
490      return one_sec;
491    case kTimeUnitMillisecond:
492      return one_ms;
493    case kTimeUnitMicrosecond:
494      return one_us;
495    case kTimeUnitNanosecond:
496      return 1;
497  }
498  return 0;
499}
500
501std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit) {
502  const char* unit = NULL;
503  uint64_t divisor = GetNsToTimeUnitDivisor(time_unit);
504  uint32_t zero_fill = 1;
505  switch (time_unit) {
506    case kTimeUnitSecond:
507      unit = "s";
508      zero_fill = 9;
509      break;
510    case kTimeUnitMillisecond:
511      unit = "ms";
512      zero_fill = 6;
513      break;
514    case kTimeUnitMicrosecond:
515      unit = "us";
516      zero_fill = 3;
517      break;
518    case kTimeUnitNanosecond:
519      unit = "ns";
520      zero_fill = 0;
521      break;
522  }
523
524  uint64_t whole_part = nano_duration / divisor;
525  uint64_t fractional_part = nano_duration % divisor;
526  if (fractional_part == 0) {
527    return StringPrintf("%llu%s", whole_part, unit);
528  } else {
529    while ((fractional_part % 1000) == 0) {
530      zero_fill -= 3;
531      fractional_part /= 1000;
532    }
533    if (zero_fill == 3) {
534      return StringPrintf("%llu.%03llu%s", whole_part, fractional_part, unit);
535    } else if (zero_fill == 6) {
536      return StringPrintf("%llu.%06llu%s", whole_part, fractional_part, unit);
537    } else {
538      return StringPrintf("%llu.%09llu%s", whole_part, fractional_part, unit);
539    }
540  }
541}
542
543std::string PrintableString(const std::string& utf) {
544  std::string result;
545  result += '"';
546  const char* p = utf.c_str();
547  size_t char_count = CountModifiedUtf8Chars(p);
548  for (size_t i = 0; i < char_count; ++i) {
549    uint16_t ch = GetUtf16FromUtf8(&p);
550    if (ch == '\\') {
551      result += "\\\\";
552    } else if (ch == '\n') {
553      result += "\\n";
554    } else if (ch == '\r') {
555      result += "\\r";
556    } else if (ch == '\t') {
557      result += "\\t";
558    } else if (NeedsEscaping(ch)) {
559      StringAppendF(&result, "\\u%04x", ch);
560    } else {
561      result += ch;
562    }
563  }
564  result += '"';
565  return result;
566}
567
568// See http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp615 for the full rules.
569std::string MangleForJni(const std::string& s) {
570  std::string result;
571  size_t char_count = CountModifiedUtf8Chars(s.c_str());
572  const char* cp = &s[0];
573  for (size_t i = 0; i < char_count; ++i) {
574    uint16_t ch = GetUtf16FromUtf8(&cp);
575    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
576      result.push_back(ch);
577    } else if (ch == '.' || ch == '/') {
578      result += "_";
579    } else if (ch == '_') {
580      result += "_1";
581    } else if (ch == ';') {
582      result += "_2";
583    } else if (ch == '[') {
584      result += "_3";
585    } else {
586      StringAppendF(&result, "_0%04x", ch);
587    }
588  }
589  return result;
590}
591
592std::string DotToDescriptor(const char* class_name) {
593  std::string descriptor(class_name);
594  std::replace(descriptor.begin(), descriptor.end(), '.', '/');
595  if (descriptor.length() > 0 && descriptor[0] != '[') {
596    descriptor = "L" + descriptor + ";";
597  }
598  return descriptor;
599}
600
601std::string DescriptorToDot(const char* descriptor) {
602  size_t length = strlen(descriptor);
603  if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
604    std::string result(descriptor + 1, length - 2);
605    std::replace(result.begin(), result.end(), '/', '.');
606    return result;
607  }
608  return descriptor;
609}
610
611std::string DescriptorToName(const char* descriptor) {
612  size_t length = strlen(descriptor);
613  if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
614    std::string result(descriptor + 1, length - 2);
615    return result;
616  }
617  return descriptor;
618}
619
620std::string JniShortName(const mirror::ArtMethod* m) {
621  MethodHelper mh(m);
622  std::string class_name(mh.GetDeclaringClassDescriptor());
623  // Remove the leading 'L' and trailing ';'...
624  CHECK_EQ(class_name[0], 'L') << class_name;
625  CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
626  class_name.erase(0, 1);
627  class_name.erase(class_name.size() - 1, 1);
628
629  std::string method_name(mh.GetName());
630
631  std::string short_name;
632  short_name += "Java_";
633  short_name += MangleForJni(class_name);
634  short_name += "_";
635  short_name += MangleForJni(method_name);
636  return short_name;
637}
638
639std::string JniLongName(const mirror::ArtMethod* m) {
640  std::string long_name;
641  long_name += JniShortName(m);
642  long_name += "__";
643
644  std::string signature(MethodHelper(m).GetSignature());
645  signature.erase(0, 1);
646  signature.erase(signature.begin() + signature.find(')'), signature.end());
647
648  long_name += MangleForJni(signature);
649
650  return long_name;
651}
652
653// Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii.
654uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = {
655  0x00000000,  // 00..1f low control characters; nothing valid
656  0x03ff2010,  // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
657  0x87fffffe,  // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
658  0x07fffffe   // 60..7f lowercase etc.; valid: 'a'..'z'
659};
660
661// Helper for IsValidPartOfMemberNameUtf8(); do not call directly.
662bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) {
663  /*
664   * It's a multibyte encoded character. Decode it and analyze. We
665   * accept anything that isn't (a) an improperly encoded low value,
666   * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
667   * control character, or (e) a high space, layout, or special
668   * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
669   * U+fff0..U+ffff). This is all specified in the dex format
670   * document.
671   */
672
673  uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr);
674
675  // Perform follow-up tests based on the high 8 bits.
676  switch (utf16 >> 8) {
677  case 0x00:
678    // It's only valid if it's above the ISO-8859-1 high space (0xa0).
679    return (utf16 > 0x00a0);
680  case 0xd8:
681  case 0xd9:
682  case 0xda:
683  case 0xdb:
684    // It's a leading surrogate. Check to see that a trailing
685    // surrogate follows.
686    utf16 = GetUtf16FromUtf8(pUtf8Ptr);
687    return (utf16 >= 0xdc00) && (utf16 <= 0xdfff);
688  case 0xdc:
689  case 0xdd:
690  case 0xde:
691  case 0xdf:
692    // It's a trailing surrogate, which is not valid at this point.
693    return false;
694  case 0x20:
695  case 0xff:
696    // It's in the range that has spaces, controls, and specials.
697    switch (utf16 & 0xfff8) {
698    case 0x2000:
699    case 0x2008:
700    case 0x2028:
701    case 0xfff0:
702    case 0xfff8:
703      return false;
704    }
705    break;
706  }
707  return true;
708}
709
710/* Return whether the pointed-at modified-UTF-8 encoded character is
711 * valid as part of a member name, updating the pointer to point past
712 * the consumed character. This will consume two encoded UTF-16 code
713 * points if the character is encoded as a surrogate pair. Also, if
714 * this function returns false, then the given pointer may only have
715 * been partially advanced.
716 */
717bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) {
718  uint8_t c = (uint8_t) **pUtf8Ptr;
719  if (c <= 0x7f) {
720    // It's low-ascii, so check the table.
721    uint32_t wordIdx = c >> 5;
722    uint32_t bitIdx = c & 0x1f;
723    (*pUtf8Ptr)++;
724    return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
725  }
726
727  // It's a multibyte encoded character. Call a non-inline function
728  // for the heavy lifting.
729  return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr);
730}
731
732bool IsValidMemberName(const char* s) {
733  bool angle_name = false;
734
735  switch (*s) {
736    case '\0':
737      // The empty string is not a valid name.
738      return false;
739    case '<':
740      angle_name = true;
741      s++;
742      break;
743  }
744
745  while (true) {
746    switch (*s) {
747      case '\0':
748        return !angle_name;
749      case '>':
750        return angle_name && s[1] == '\0';
751    }
752
753    if (!IsValidPartOfMemberNameUtf8(&s)) {
754      return false;
755    }
756  }
757}
758
759enum ClassNameType { kName, kDescriptor };
760bool IsValidClassName(const char* s, ClassNameType type, char separator) {
761  int arrayCount = 0;
762  while (*s == '[') {
763    arrayCount++;
764    s++;
765  }
766
767  if (arrayCount > 255) {
768    // Arrays may have no more than 255 dimensions.
769    return false;
770  }
771
772  if (arrayCount != 0) {
773    /*
774     * If we're looking at an array of some sort, then it doesn't
775     * matter if what is being asked for is a class name; the
776     * format looks the same as a type descriptor in that case, so
777     * treat it as such.
778     */
779    type = kDescriptor;
780  }
781
782  if (type == kDescriptor) {
783    /*
784     * We are looking for a descriptor. Either validate it as a
785     * single-character primitive type, or continue on to check the
786     * embedded class name (bracketed by "L" and ";").
787     */
788    switch (*(s++)) {
789    case 'B':
790    case 'C':
791    case 'D':
792    case 'F':
793    case 'I':
794    case 'J':
795    case 'S':
796    case 'Z':
797      // These are all single-character descriptors for primitive types.
798      return (*s == '\0');
799    case 'V':
800      // Non-array void is valid, but you can't have an array of void.
801      return (arrayCount == 0) && (*s == '\0');
802    case 'L':
803      // Class name: Break out and continue below.
804      break;
805    default:
806      // Oddball descriptor character.
807      return false;
808    }
809  }
810
811  /*
812   * We just consumed the 'L' that introduces a class name as part
813   * of a type descriptor, or we are looking for an unadorned class
814   * name.
815   */
816
817  bool sepOrFirst = true;  // first character or just encountered a separator.
818  for (;;) {
819    uint8_t c = (uint8_t) *s;
820    switch (c) {
821    case '\0':
822      /*
823       * Premature end for a type descriptor, but valid for
824       * a class name as long as we haven't encountered an
825       * empty component (including the degenerate case of
826       * the empty string "").
827       */
828      return (type == kName) && !sepOrFirst;
829    case ';':
830      /*
831       * Invalid character for a class name, but the
832       * legitimate end of a type descriptor. In the latter
833       * case, make sure that this is the end of the string
834       * and that it doesn't end with an empty component
835       * (including the degenerate case of "L;").
836       */
837      return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0');
838    case '/':
839    case '.':
840      if (c != separator) {
841        // The wrong separator character.
842        return false;
843      }
844      if (sepOrFirst) {
845        // Separator at start or two separators in a row.
846        return false;
847      }
848      sepOrFirst = true;
849      s++;
850      break;
851    default:
852      if (!IsValidPartOfMemberNameUtf8(&s)) {
853        return false;
854      }
855      sepOrFirst = false;
856      break;
857    }
858  }
859}
860
861bool IsValidBinaryClassName(const char* s) {
862  return IsValidClassName(s, kName, '.');
863}
864
865bool IsValidJniClassName(const char* s) {
866  return IsValidClassName(s, kName, '/');
867}
868
869bool IsValidDescriptor(const char* s) {
870  return IsValidClassName(s, kDescriptor, '/');
871}
872
873void Split(const std::string& s, char separator, std::vector<std::string>& result) {
874  const char* p = s.data();
875  const char* end = p + s.size();
876  while (p != end) {
877    if (*p == separator) {
878      ++p;
879    } else {
880      const char* start = p;
881      while (++p != end && *p != separator) {
882        // Skip to the next occurrence of the separator.
883      }
884      result.push_back(std::string(start, p - start));
885    }
886  }
887}
888
889template <typename StringT>
890std::string Join(std::vector<StringT>& strings, char separator) {
891  if (strings.empty()) {
892    return "";
893  }
894
895  std::string result(strings[0]);
896  for (size_t i = 1; i < strings.size(); ++i) {
897    result += separator;
898    result += strings[i];
899  }
900  return result;
901}
902
903// Explicit instantiations.
904template std::string Join<std::string>(std::vector<std::string>& strings, char separator);
905template std::string Join<const char*>(std::vector<const char*>& strings, char separator);
906template std::string Join<char*>(std::vector<char*>& strings, char separator);
907
908bool StartsWith(const std::string& s, const char* prefix) {
909  return s.compare(0, strlen(prefix), prefix) == 0;
910}
911
912bool EndsWith(const std::string& s, const char* suffix) {
913  size_t suffix_length = strlen(suffix);
914  size_t string_length = s.size();
915  if (suffix_length > string_length) {
916    return false;
917  }
918  size_t offset = string_length - suffix_length;
919  return s.compare(offset, suffix_length, suffix) == 0;
920}
921
922void SetThreadName(const char* thread_name) {
923  int hasAt = 0;
924  int hasDot = 0;
925  const char* s = thread_name;
926  while (*s) {
927    if (*s == '.') {
928      hasDot = 1;
929    } else if (*s == '@') {
930      hasAt = 1;
931    }
932    s++;
933  }
934  int len = s - thread_name;
935  if (len < 15 || hasAt || !hasDot) {
936    s = thread_name;
937  } else {
938    s = thread_name + len - 15;
939  }
940#if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP)
941  // pthread_setname_np fails rather than truncating long strings.
942  char buf[16];       // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
943  strncpy(buf, s, sizeof(buf)-1);
944  buf[sizeof(buf)-1] = '\0';
945  errno = pthread_setname_np(pthread_self(), buf);
946  if (errno != 0) {
947    PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
948  }
949#elif defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
950  pthread_setname_np(thread_name);
951#elif defined(HAVE_PRCTL)
952  prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);  // NOLINT (unsigned long)
953#else
954  UNIMPLEMENTED(WARNING) << thread_name;
955#endif
956}
957
958void GetTaskStats(pid_t tid, char& state, int& utime, int& stime, int& task_cpu) {
959  utime = stime = task_cpu = 0;
960  std::string stats;
961  if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
962    return;
963  }
964  // Skip the command, which may contain spaces.
965  stats = stats.substr(stats.find(')') + 2);
966  // Extract the three fields we care about.
967  std::vector<std::string> fields;
968  Split(stats, ' ', fields);
969  state = fields[0][0];
970  utime = strtoull(fields[11].c_str(), NULL, 10);
971  stime = strtoull(fields[12].c_str(), NULL, 10);
972  task_cpu = strtoull(fields[36].c_str(), NULL, 10);
973}
974
975std::string GetSchedulerGroupName(pid_t tid) {
976  // /proc/<pid>/cgroup looks like this:
977  // 2:devices:/
978  // 1:cpuacct,cpu:/
979  // We want the third field from the line whose second field contains the "cpu" token.
980  std::string cgroup_file;
981  if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) {
982    return "";
983  }
984  std::vector<std::string> cgroup_lines;
985  Split(cgroup_file, '\n', cgroup_lines);
986  for (size_t i = 0; i < cgroup_lines.size(); ++i) {
987    std::vector<std::string> cgroup_fields;
988    Split(cgroup_lines[i], ':', cgroup_fields);
989    std::vector<std::string> cgroups;
990    Split(cgroup_fields[1], ',', cgroups);
991    for (size_t i = 0; i < cgroups.size(); ++i) {
992      if (cgroups[i] == "cpu") {
993        return cgroup_fields[2].substr(1);  // Skip the leading slash.
994      }
995    }
996  }
997  return "";
998}
999
1000static const char* CleanMapName(const backtrace_symbol_t* symbol) {
1001  const char* map_name = symbol->map_name;
1002  if (map_name == NULL) {
1003    map_name = "???";
1004  }
1005  // Turn "/usr/local/google/home/enh/clean-dalvik-dev/out/host/linux-x86/lib/libartd.so"
1006  // into "libartd.so".
1007  const char* last_slash = strrchr(map_name, '/');
1008  if (last_slash != NULL) {
1009    map_name = last_slash + 1;
1010  }
1011  return map_name;
1012}
1013
1014static void FindSymbolInElf(const backtrace_frame_t* frame, const backtrace_symbol_t* symbol,
1015                            std::string& symbol_name, uint32_t& pc_offset) {
1016  symbol_table_t* symbol_table = NULL;
1017  if (symbol->map_name != NULL) {
1018    symbol_table = load_symbol_table(symbol->map_name);
1019  }
1020  const symbol_t* elf_symbol = NULL;
1021  bool was_relative = true;
1022  if (symbol_table != NULL) {
1023    elf_symbol = find_symbol(symbol_table, symbol->relative_pc);
1024    if (elf_symbol == NULL) {
1025      elf_symbol = find_symbol(symbol_table, frame->absolute_pc);
1026      was_relative = false;
1027    }
1028  }
1029  if (elf_symbol != NULL) {
1030    const char* demangled_symbol_name = demangle_symbol_name(elf_symbol->name);
1031    if (demangled_symbol_name != NULL) {
1032      symbol_name = demangled_symbol_name;
1033    } else {
1034      symbol_name = elf_symbol->name;
1035    }
1036
1037    // TODO: is it a libcorkscrew bug that we have to do this?
1038    pc_offset = (was_relative ? symbol->relative_pc : frame->absolute_pc) - elf_symbol->start;
1039  } else {
1040    symbol_name = "???";
1041  }
1042  free_symbol_table(symbol_table);
1043}
1044
1045void DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
1046  // Ensure libcorkscrew doesn't use a stale cache of /proc/self/maps.
1047  flush_my_map_info_list();
1048
1049  const size_t MAX_DEPTH = 32;
1050  UniquePtr<backtrace_frame_t[]> frames(new backtrace_frame_t[MAX_DEPTH]);
1051  size_t ignore_count = 2;  // Don't include unwind_backtrace_thread or DumpNativeStack.
1052  ssize_t frame_count = unwind_backtrace_thread(tid, frames.get(), ignore_count, MAX_DEPTH);
1053  if (frame_count == -1) {
1054    os << prefix << "(unwind_backtrace_thread failed for thread " << tid << ")\n";
1055    return;
1056  } else if (frame_count == 0) {
1057    os << prefix << "(no native stack frames for thread " << tid << ")\n";
1058    return;
1059  }
1060
1061  UniquePtr<backtrace_symbol_t[]> backtrace_symbols(new backtrace_symbol_t[frame_count]);
1062  get_backtrace_symbols(frames.get(), frame_count, backtrace_symbols.get());
1063
1064  for (size_t i = 0; i < static_cast<size_t>(frame_count); ++i) {
1065    const backtrace_frame_t* frame = &frames[i];
1066    const backtrace_symbol_t* symbol = &backtrace_symbols[i];
1067
1068    // We produce output like this:
1069    // ]    #00 unwind_backtrace_thread+536 [0x55d75bb8] (libcorkscrew.so)
1070
1071    std::string symbol_name;
1072    uint32_t pc_offset = 0;
1073    if (symbol->demangled_name != NULL) {
1074      symbol_name = symbol->demangled_name;
1075      pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
1076    } else if (symbol->symbol_name != NULL) {
1077      symbol_name = symbol->symbol_name;
1078      pc_offset = symbol->relative_pc - symbol->relative_symbol_addr;
1079    } else {
1080      // dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file...
1081      FindSymbolInElf(frame, symbol, symbol_name, pc_offset);
1082    }
1083
1084    os << prefix;
1085    if (include_count) {
1086      os << StringPrintf("#%02zd ", i);
1087    }
1088    os << symbol_name;
1089    if (pc_offset != 0) {
1090      os << "+" << pc_offset;
1091    }
1092    os << StringPrintf(" [%p] (%s)\n",
1093                       reinterpret_cast<void*>(frame->absolute_pc), CleanMapName(symbol));
1094  }
1095
1096  free_backtrace_symbols(backtrace_symbols.get(), frame_count);
1097}
1098
1099#if defined(__APPLE__)
1100
1101// TODO: is there any way to get the kernel stack on Mac OS?
1102void DumpKernelStack(std::ostream&, pid_t, const char*, bool) {}
1103
1104#else
1105
1106void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
1107  if (tid == GetTid()) {
1108    // There's no point showing that we're reading our stack out of /proc!
1109    return;
1110  }
1111
1112  std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid));
1113  std::string kernel_stack;
1114  if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) {
1115    os << prefix << "(couldn't read " << kernel_stack_filename << ")\n";
1116    return;
1117  }
1118
1119  std::vector<std::string> kernel_stack_frames;
1120  Split(kernel_stack, '\n', kernel_stack_frames);
1121  // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff",
1122  // which looking at the source appears to be the kernel's way of saying "that's all, folks!".
1123  kernel_stack_frames.pop_back();
1124  for (size_t i = 0; i < kernel_stack_frames.size(); ++i) {
1125    // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110" into "futex_wait_queue_me+0xcd/0x110".
1126    const char* text = kernel_stack_frames[i].c_str();
1127    const char* close_bracket = strchr(text, ']');
1128    if (close_bracket != NULL) {
1129      text = close_bracket + 2;
1130    }
1131    os << prefix;
1132    if (include_count) {
1133      os << StringPrintf("#%02zd ", i);
1134    }
1135    os << text << "\n";
1136  }
1137}
1138
1139#endif
1140
1141const char* GetAndroidRoot() {
1142  const char* android_root = getenv("ANDROID_ROOT");
1143  if (android_root == NULL) {
1144    if (OS::DirectoryExists("/system")) {
1145      android_root = "/system";
1146    } else {
1147      LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist";
1148      return "";
1149    }
1150  }
1151  if (!OS::DirectoryExists(android_root)) {
1152    LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root;
1153    return "";
1154  }
1155  return android_root;
1156}
1157
1158const char* GetAndroidData() {
1159  const char* android_data = getenv("ANDROID_DATA");
1160  if (android_data == NULL) {
1161    if (OS::DirectoryExists("/data")) {
1162      android_data = "/data";
1163    } else {
1164      LOG(FATAL) << "ANDROID_DATA not set and /data does not exist";
1165      return "";
1166    }
1167  }
1168  if (!OS::DirectoryExists(android_data)) {
1169    LOG(FATAL) << "Failed to find ANDROID_DATA directory " << android_data;
1170    return "";
1171  }
1172  return android_data;
1173}
1174
1175std::string GetDalvikCacheOrDie(const char* android_data) {
1176  std::string dalvik_cache(StringPrintf("%s/dalvik-cache", android_data));
1177
1178  if (!OS::DirectoryExists(dalvik_cache.c_str())) {
1179    if (StartsWith(dalvik_cache, "/tmp/")) {
1180      int result = mkdir(dalvik_cache.c_str(), 0700);
1181      if (result != 0) {
1182        LOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache;
1183        return "";
1184      }
1185    } else {
1186      LOG(FATAL) << "Failed to find dalvik-cache directory " << dalvik_cache;
1187      return "";
1188    }
1189  }
1190  return dalvik_cache;
1191}
1192
1193std::string GetDalvikCacheFilenameOrDie(const std::string& location) {
1194  std::string dalvik_cache(GetDalvikCacheOrDie(GetAndroidData()));
1195  if (location[0] != '/') {
1196    LOG(FATAL) << "Expected path in location to be absolute: "<< location;
1197  }
1198  std::string cache_file(location, 1);  // skip leading slash
1199  if (!EndsWith(location, ".dex") || !EndsWith(location, ".art")) {
1200    cache_file += "/";
1201    cache_file += DexFile::kClassesDex;
1202  }
1203  std::replace(cache_file.begin(), cache_file.end(), '/', '@');
1204  return dalvik_cache + "/" + cache_file;
1205}
1206
1207bool IsZipMagic(uint32_t magic) {
1208  return (('P' == ((magic >> 0) & 0xff)) &&
1209          ('K' == ((magic >> 8) & 0xff)));
1210}
1211
1212bool IsDexMagic(uint32_t magic) {
1213  return DexFile::IsMagicValid(reinterpret_cast<const byte*>(&magic));
1214}
1215
1216bool IsOatMagic(uint32_t magic) {
1217  return (memcmp(reinterpret_cast<const byte*>(magic),
1218                 OatHeader::kOatMagic,
1219                 sizeof(OatHeader::kOatMagic)) == 0);
1220}
1221
1222}  // namespace art
1223