blob: 26fe6052cb71950d62e68ba6826cb803329b02b3 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Elliott Hughes42ee1422011-09-06 12:33:32 -070017#include "utils.h"
18
Elliott Hughes06e3ad42012-02-07 14:51:57 -080019#include <dynamic_annotations.h>
Elliott Hughes92b3b562011-09-08 16:32:26 -070020#include <pthread.h>
Brian Carlstroma9f19782011-10-13 00:14:47 -070021#include <sys/stat.h>
Elliott Hughes42ee1422011-09-06 12:33:32 -070022#include <sys/syscall.h>
23#include <sys/types.h>
24#include <unistd.h>
25
Elliott Hughes90a33692011-08-30 13:27:07 -070026#include "UniquePtr.h"
Ian Rogersd81871c2011-10-03 13:57:23 -070027#include "class_loader.h"
buzbeec143c552011-08-20 17:38:58 -070028#include "file.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070029#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
buzbeec143c552011-08-20 17:38:58 -070031#include "os.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070032
Elliott Hughesad6c9c32012-01-19 17:39:12 -080033#if !defined(HAVE_POSIX_CLOCKS)
34#include <sys/time.h>
35#endif
36
Elliott Hughesdcc24742011-09-07 14:02:44 -070037#if defined(HAVE_PRCTL)
38#include <sys/prctl.h>
39#endif
40
Elliott Hughes4ae722a2012-03-13 11:08:51 -070041#if defined(__APPLE__)
42#include "AvailabilityMacros.h"
43#endif
44
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080045#if defined(__linux__)
Elliott Hughese1aee692012-01-17 16:40:10 -080046#include <linux/unistd.h>
Elliott Hughese1aee692012-01-17 16:40:10 -080047#endif
48
Elliott Hughes11e45072011-08-16 17:40:46 -070049namespace art {
50
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080051pid_t GetTid() {
52#if defined(__APPLE__)
53 // Mac OS doesn't have gettid(2).
54 return getpid();
55#else
56 // Neither bionic nor glibc exposes gettid(2).
57 return syscall(__NR_gettid);
58#endif
59}
60
Elliott Hughesd92bec42011-09-02 17:04:36 -070061bool ReadFileToString(const std::string& file_name, std::string* result) {
62 UniquePtr<File> file(OS::OpenFile(file_name.c_str(), false));
63 if (file.get() == NULL) {
64 return false;
65 }
buzbeec143c552011-08-20 17:38:58 -070066
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070067 std::vector<char> buf(8 * KB);
buzbeec143c552011-08-20 17:38:58 -070068 while (true) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070069 int64_t n = file->Read(&buf[0], buf.size());
Elliott Hughesd92bec42011-09-02 17:04:36 -070070 if (n == -1) {
71 return false;
buzbeec143c552011-08-20 17:38:58 -070072 }
Elliott Hughesd92bec42011-09-02 17:04:36 -070073 if (n == 0) {
74 return true;
75 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070076 result->append(&buf[0], n);
buzbeec143c552011-08-20 17:38:58 -070077 }
buzbeec143c552011-08-20 17:38:58 -070078}
79
Elliott Hughese27955c2011-08-26 15:21:24 -070080std::string GetIsoDate() {
81 time_t now = time(NULL);
82 struct tm tmbuf;
83 struct tm* ptm = localtime_r(&now, &tmbuf);
84 return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d",
85 ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday,
86 ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
87}
88
Elliott Hughes7162ad92011-10-27 14:08:42 -070089uint64_t MilliTime() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -080090#if defined(HAVE_POSIX_CLOCKS)
Elliott Hughes7162ad92011-10-27 14:08:42 -070091 struct timespec now;
92 clock_gettime(CLOCK_MONOTONIC, &now);
93 return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_nsec / 1000000LL;
Elliott Hughesad6c9c32012-01-19 17:39:12 -080094#else
95 struct timeval now;
96 gettimeofday(&now, NULL);
97 return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_usec / 1000LL;
98#endif
Elliott Hughes7162ad92011-10-27 14:08:42 -070099}
100
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800101uint64_t MicroTime() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800102#if defined(HAVE_POSIX_CLOCKS)
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800103 struct timespec now;
104 clock_gettime(CLOCK_MONOTONIC, &now);
105 return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_nsec / 1000LL;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800106#else
107 struct timeval now;
108 gettimeofday(&now, NULL);
109 return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_usec * 1000LL;
110#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800111}
112
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700113uint64_t NanoTime() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800114#if defined(HAVE_POSIX_CLOCKS)
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700115 struct timespec now;
116 clock_gettime(CLOCK_MONOTONIC, &now);
117 return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800118#else
119 struct timeval now;
120 gettimeofday(&now, NULL);
121 return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL;
122#endif
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700123}
124
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800125uint64_t ThreadCpuMicroTime() {
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800126#if defined(HAVE_POSIX_CLOCKS)
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800127 struct timespec now;
128 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
129 return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_nsec / 1000LL;
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800130#else
131 UNIMPLEMENTED(WARNING);
132 return -1;
133#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800134}
135
Elliott Hughes0512f022012-03-15 22:10:52 -0700136uint64_t ThreadCpuNanoTime() {
137#if defined(HAVE_POSIX_CLOCKS)
138 struct timespec now;
139 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now);
140 return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec;
141#else
142 UNIMPLEMENTED(WARNING);
143 return -1;
144#endif
145}
146
Elliott Hughes5174fe62011-08-23 15:12:35 -0700147std::string PrettyDescriptor(const String* java_descriptor) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700148 if (java_descriptor == NULL) {
149 return "null";
150 }
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700151 return PrettyDescriptor(java_descriptor->ToModifiedUtf8());
152}
Elliott Hughes5174fe62011-08-23 15:12:35 -0700153
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800154std::string PrettyDescriptor(const Class* klass) {
155 if (klass == NULL) {
156 return "null";
157 }
158 return PrettyDescriptor(ClassHelper(klass).GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159}
160
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700161std::string PrettyDescriptor(const std::string& descriptor) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700162 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700163 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -0700164 size_t dim = 0;
165 while (*c == '[') {
166 dim++;
167 c++;
168 }
169
170 // Reference or primitive?
171 if (*c == 'L') {
172 // "[[La/b/C;" -> "a.b.C[][]".
173 c++; // Skip the 'L'.
174 } else {
175 // "[[B" -> "byte[][]".
176 // To make life easier, we make primitives look like unqualified
177 // reference types.
178 switch (*c) {
179 case 'B': c = "byte;"; break;
180 case 'C': c = "char;"; break;
181 case 'D': c = "double;"; break;
182 case 'F': c = "float;"; break;
183 case 'I': c = "int;"; break;
184 case 'J': c = "long;"; break;
185 case 'S': c = "short;"; break;
186 case 'Z': c = "boolean;"; break;
Elliott Hughes5174fe62011-08-23 15:12:35 -0700187 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -0700188 }
189 }
190
191 // At this point, 'c' is a string of the form "fully/qualified/Type;"
192 // or "primitive;". Rewrite the type with '.' instead of '/':
193 std::string result;
194 const char* p = c;
195 while (*p != ';') {
196 char ch = *p++;
197 if (ch == '/') {
198 ch = '.';
199 }
200 result.push_back(ch);
201 }
202 // ...and replace the semicolon with 'dim' "[]" pairs:
203 while (dim--) {
204 result += "[]";
205 }
206 return result;
207}
208
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700209std::string PrettyDescriptor(Primitive::Type type) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800210 std::string descriptor_string(Primitive::Descriptor(type));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700211 return PrettyDescriptor(descriptor_string);
212}
213
Elliott Hughes54e7df12011-09-16 11:47:04 -0700214std::string PrettyField(const Field* f, bool with_type) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700215 if (f == NULL) {
216 return "null";
217 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800218 FieldHelper fh(f);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700219 std::string result;
220 if (with_type) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800221 result += PrettyDescriptor(fh.GetTypeDescriptor());
Elliott Hughes54e7df12011-09-16 11:47:04 -0700222 result += ' ';
223 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800224 result += PrettyDescriptor(fh.GetDeclaringClassDescriptor());
Elliott Hughesa2501992011-08-26 19:39:54 -0700225 result += '.';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800226 result += fh.GetName();
Elliott Hughesa2501992011-08-26 19:39:54 -0700227 return result;
228}
229
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700230std::string PrettyMethod(const Method* m, bool with_signature) {
231 if (m == NULL) {
232 return "null";
233 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234 MethodHelper mh(m);
235 std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700236 result += '.';
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800237 result += mh.GetName();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700238 if (with_signature) {
239 // TODO: iterate over the signature's elements and pass them all to
240 // PrettyDescriptor? We'd need to pull out the return type specially, too.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800241 result += mh.GetSignature();
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700242 }
243 return result;
244}
245
Ian Rogers0571d352011-11-03 19:51:38 -0700246std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) {
247 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
248 std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id)));
249 result += '.';
250 result += dex_file.GetMethodName(method_id);
251 if (with_signature) {
252 // TODO: iterate over the signature's elements and pass them all to
253 // PrettyDescriptor? We'd need to pull out the return type specially, too.
254 result += dex_file.GetMethodSignature(method_id);
255 }
256 return result;
257}
258
Elliott Hughes54e7df12011-09-16 11:47:04 -0700259std::string PrettyTypeOf(const Object* obj) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700260 if (obj == NULL) {
261 return "null";
262 }
263 if (obj->GetClass() == NULL) {
264 return "(raw)";
265 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 ClassHelper kh(obj->GetClass());
267 std::string result(PrettyDescriptor(kh.GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700268 if (obj->IsClass()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269 kh.ChangeClass(obj->AsClass());
270 result += "<" + PrettyDescriptor(kh.GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700271 }
272 return result;
273}
274
Elliott Hughes54e7df12011-09-16 11:47:04 -0700275std::string PrettyClass(const Class* c) {
276 if (c == NULL) {
277 return "null";
278 }
279 std::string result;
280 result += "java.lang.Class<";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800281 result += PrettyDescriptor(c);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700282 result += ">";
283 return result;
284}
285
Ian Rogersd81871c2011-10-03 13:57:23 -0700286std::string PrettyClassAndClassLoader(const Class* c) {
287 if (c == NULL) {
288 return "null";
289 }
290 std::string result;
291 result += "java.lang.Class<";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 result += PrettyDescriptor(c);
Ian Rogersd81871c2011-10-03 13:57:23 -0700293 result += ",";
294 result += PrettyTypeOf(c->GetClassLoader());
295 // TODO: add an identifying hash value for the loader
296 result += ">";
297 return result;
298}
299
Ian Rogers3bb17a62012-01-27 23:56:44 -0800300std::string PrettySize(size_t size_in_bytes) {
301 if ((size_in_bytes / GB) * GB == size_in_bytes) {
302 return StringPrintf("%zdGB", size_in_bytes / GB);
303 } else if ((size_in_bytes / MB) * MB == size_in_bytes) {
304 return StringPrintf("%zdMB", size_in_bytes / MB);
305 } else if ((size_in_bytes / KB) * KB == size_in_bytes) {
306 return StringPrintf("%zdKiB", size_in_bytes / KB);
307 } else {
308 return StringPrintf("%zdB", size_in_bytes);
309 }
310}
311
312std::string PrettyDuration(uint64_t nano_duration) {
313 if (nano_duration == 0) {
314 return "0";
315 } else {
316 const uint64_t one_sec = 1000 * 1000 * 1000;
317 const uint64_t one_ms = 1000 * 1000;
318 const uint64_t one_us = 1000;
319 const char* unit;
320 uint64_t divisor;
321 uint32_t zero_fill;
322 if (nano_duration >= one_sec) {
323 unit = "s";
324 divisor = one_sec;
325 zero_fill = 9;
326 } else if(nano_duration >= one_ms) {
327 unit = "ms";
328 divisor = one_ms;
329 zero_fill = 6;
330 } else if(nano_duration >= one_us) {
331 unit = "us";
332 divisor = one_us;
333 zero_fill = 3;
334 } else {
335 unit = "ns";
336 divisor = 1;
337 zero_fill = 0;
338 }
339 uint64_t whole_part = nano_duration / divisor;
340 uint64_t fractional_part = nano_duration % divisor;
341 if (fractional_part == 0) {
342 return StringPrintf("%llu%s", whole_part, unit);
343 } else {
344 while ((fractional_part % 1000) == 0) {
345 zero_fill -= 3;
346 fractional_part /= 1000;
347 }
348 if (zero_fill == 3) {
349 return StringPrintf("%llu.%03llu%s", whole_part, fractional_part, unit);
350 } else if (zero_fill == 6) {
351 return StringPrintf("%llu.%06llu%s", whole_part, fractional_part, unit);
352 } else {
353 return StringPrintf("%llu.%09llu%s", whole_part, fractional_part, unit);
354 }
355 }
356 }
357}
358
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800359// See http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp615 for the full rules.
Elliott Hughes79082e32011-08-25 12:07:32 -0700360std::string MangleForJni(const std::string& s) {
361 std::string result;
362 size_t char_count = CountModifiedUtf8Chars(s.c_str());
363 const char* cp = &s[0];
364 for (size_t i = 0; i < char_count; ++i) {
365 uint16_t ch = GetUtf16FromUtf8(&cp);
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800366 if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
367 result.push_back(ch);
368 } else if (ch == '.' || ch == '/') {
369 result += "_";
370 } else if (ch == '_') {
371 result += "_1";
372 } else if (ch == ';') {
373 result += "_2";
374 } else if (ch == '[') {
375 result += "_3";
Elliott Hughes79082e32011-08-25 12:07:32 -0700376 } else {
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800377 StringAppendF(&result, "_0%04x", ch);
Elliott Hughes79082e32011-08-25 12:07:32 -0700378 }
379 }
380 return result;
381}
382
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700383std::string DotToDescriptor(const char* class_name) {
384 std::string descriptor(class_name);
385 std::replace(descriptor.begin(), descriptor.end(), '.', '/');
386 if (descriptor.length() > 0 && descriptor[0] != '[') {
387 descriptor = "L" + descriptor + ";";
388 }
389 return descriptor;
390}
391
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800392std::string DescriptorToDot(const char* descriptor) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800393 size_t length = strlen(descriptor);
394 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
395 std::string result(descriptor + 1, length - 2);
396 std::replace(result.begin(), result.end(), '/', '.');
397 return result;
398 }
399 return descriptor;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800400}
401
402std::string DescriptorToName(const char* descriptor) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800403 size_t length = strlen(descriptor);
Elliott Hughes2435a572012-02-17 16:07:41 -0800404 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
405 std::string result(descriptor + 1, length - 2);
406 return result;
407 }
408 return descriptor;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700409}
410
Elliott Hughes79082e32011-08-25 12:07:32 -0700411std::string JniShortName(const Method* m) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 MethodHelper mh(m);
413 std::string class_name(mh.GetDeclaringClassDescriptor());
Elliott Hughes79082e32011-08-25 12:07:32 -0700414 // Remove the leading 'L' and trailing ';'...
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700415 CHECK_EQ(class_name[0], 'L') << class_name;
416 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
Elliott Hughes79082e32011-08-25 12:07:32 -0700417 class_name.erase(0, 1);
418 class_name.erase(class_name.size() - 1, 1);
419
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800420 std::string method_name(mh.GetName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700421
422 std::string short_name;
423 short_name += "Java_";
424 short_name += MangleForJni(class_name);
425 short_name += "_";
426 short_name += MangleForJni(method_name);
427 return short_name;
428}
429
430std::string JniLongName(const Method* m) {
431 std::string long_name;
432 long_name += JniShortName(m);
433 long_name += "__";
434
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800435 std::string signature(MethodHelper(m).GetSignature());
Elliott Hughes79082e32011-08-25 12:07:32 -0700436 signature.erase(0, 1);
437 signature.erase(signature.begin() + signature.find(')'), signature.end());
438
439 long_name += MangleForJni(signature);
440
441 return long_name;
442}
443
jeffhao10037c82012-01-23 15:06:23 -0800444// Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700445uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = {
446 0x00000000, // 00..1f low control characters; nothing valid
447 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
448 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
449 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z'
450};
451
jeffhao10037c82012-01-23 15:06:23 -0800452// Helper for IsValidPartOfMemberNameUtf8(); do not call directly.
453bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700454 /*
455 * It's a multibyte encoded character. Decode it and analyze. We
456 * accept anything that isn't (a) an improperly encoded low value,
457 * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
458 * control character, or (e) a high space, layout, or special
459 * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
460 * U+fff0..U+ffff). This is all specified in the dex format
461 * document.
462 */
463
464 uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr);
465
466 // Perform follow-up tests based on the high 8 bits.
467 switch (utf16 >> 8) {
468 case 0x00:
469 // It's only valid if it's above the ISO-8859-1 high space (0xa0).
470 return (utf16 > 0x00a0);
471 case 0xd8:
472 case 0xd9:
473 case 0xda:
474 case 0xdb:
475 // It's a leading surrogate. Check to see that a trailing
476 // surrogate follows.
477 utf16 = GetUtf16FromUtf8(pUtf8Ptr);
478 return (utf16 >= 0xdc00) && (utf16 <= 0xdfff);
479 case 0xdc:
480 case 0xdd:
481 case 0xde:
482 case 0xdf:
483 // It's a trailing surrogate, which is not valid at this point.
484 return false;
485 case 0x20:
486 case 0xff:
487 // It's in the range that has spaces, controls, and specials.
488 switch (utf16 & 0xfff8) {
489 case 0x2000:
490 case 0x2008:
491 case 0x2028:
492 case 0xfff0:
493 case 0xfff8:
494 return false;
495 }
496 break;
497 }
498 return true;
499}
500
501/* Return whether the pointed-at modified-UTF-8 encoded character is
502 * valid as part of a member name, updating the pointer to point past
503 * the consumed character. This will consume two encoded UTF-16 code
504 * points if the character is encoded as a surrogate pair. Also, if
505 * this function returns false, then the given pointer may only have
506 * been partially advanced.
507 */
jeffhao10037c82012-01-23 15:06:23 -0800508bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700509 uint8_t c = (uint8_t) **pUtf8Ptr;
510 if (c <= 0x7f) {
511 // It's low-ascii, so check the table.
512 uint32_t wordIdx = c >> 5;
513 uint32_t bitIdx = c & 0x1f;
514 (*pUtf8Ptr)++;
515 return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
516 }
517
518 // It's a multibyte encoded character. Call a non-inline function
519 // for the heavy lifting.
jeffhao10037c82012-01-23 15:06:23 -0800520 return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr);
521}
522
523bool IsValidMemberName(const char* s) {
524 bool angle_name = false;
525
526 switch(*s) {
527 case '\0':
528 // The empty string is not a valid name.
529 return false;
530 case '<':
531 angle_name = true;
532 s++;
533 break;
534 }
535
536 while (true) {
537 switch (*s) {
538 case '\0':
539 return !angle_name;
540 case '>':
541 return angle_name && s[1] == '\0';
542 }
543
544 if (!IsValidPartOfMemberNameUtf8(&s)) {
545 return false;
546 }
547 }
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700548}
549
Elliott Hughes906e6852011-10-28 14:52:10 -0700550enum ClassNameType { kName, kDescriptor };
551bool IsValidClassName(const char* s, ClassNameType type, char separator) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700552 int arrayCount = 0;
553 while (*s == '[') {
554 arrayCount++;
555 s++;
556 }
557
558 if (arrayCount > 255) {
559 // Arrays may have no more than 255 dimensions.
560 return false;
561 }
562
563 if (arrayCount != 0) {
564 /*
565 * If we're looking at an array of some sort, then it doesn't
566 * matter if what is being asked for is a class name; the
567 * format looks the same as a type descriptor in that case, so
568 * treat it as such.
569 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700570 type = kDescriptor;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700571 }
572
Elliott Hughes906e6852011-10-28 14:52:10 -0700573 if (type == kDescriptor) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700574 /*
575 * We are looking for a descriptor. Either validate it as a
576 * single-character primitive type, or continue on to check the
577 * embedded class name (bracketed by "L" and ";").
578 */
579 switch (*(s++)) {
580 case 'B':
581 case 'C':
582 case 'D':
583 case 'F':
584 case 'I':
585 case 'J':
586 case 'S':
587 case 'Z':
588 // These are all single-character descriptors for primitive types.
589 return (*s == '\0');
590 case 'V':
591 // Non-array void is valid, but you can't have an array of void.
592 return (arrayCount == 0) && (*s == '\0');
593 case 'L':
594 // Class name: Break out and continue below.
595 break;
596 default:
597 // Oddball descriptor character.
598 return false;
599 }
600 }
601
602 /*
603 * We just consumed the 'L' that introduces a class name as part
604 * of a type descriptor, or we are looking for an unadorned class
605 * name.
606 */
607
608 bool sepOrFirst = true; // first character or just encountered a separator.
609 for (;;) {
610 uint8_t c = (uint8_t) *s;
611 switch (c) {
612 case '\0':
613 /*
614 * Premature end for a type descriptor, but valid for
615 * a class name as long as we haven't encountered an
616 * empty component (including the degenerate case of
617 * the empty string "").
618 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700619 return (type == kName) && !sepOrFirst;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700620 case ';':
621 /*
622 * Invalid character for a class name, but the
623 * legitimate end of a type descriptor. In the latter
624 * case, make sure that this is the end of the string
625 * and that it doesn't end with an empty component
626 * (including the degenerate case of "L;").
627 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700628 return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0');
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700629 case '/':
630 case '.':
631 if (c != separator) {
632 // The wrong separator character.
633 return false;
634 }
635 if (sepOrFirst) {
636 // Separator at start or two separators in a row.
637 return false;
638 }
639 sepOrFirst = true;
640 s++;
641 break;
642 default:
jeffhao10037c82012-01-23 15:06:23 -0800643 if (!IsValidPartOfMemberNameUtf8(&s)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700644 return false;
645 }
646 sepOrFirst = false;
647 break;
648 }
649 }
650}
651
Elliott Hughes906e6852011-10-28 14:52:10 -0700652bool IsValidBinaryClassName(const char* s) {
653 return IsValidClassName(s, kName, '.');
654}
655
656bool IsValidJniClassName(const char* s) {
657 return IsValidClassName(s, kName, '/');
658}
659
660bool IsValidDescriptor(const char* s) {
661 return IsValidClassName(s, kDescriptor, '/');
662}
663
Elliott Hughes48436bb2012-02-07 15:23:28 -0800664void Split(const std::string& s, char separator, std::vector<std::string>& result) {
Elliott Hughes34023802011-08-30 12:06:17 -0700665 const char* p = s.data();
666 const char* end = p + s.size();
667 while (p != end) {
Elliott Hughes48436bb2012-02-07 15:23:28 -0800668 if (*p == separator) {
Elliott Hughes34023802011-08-30 12:06:17 -0700669 ++p;
670 } else {
671 const char* start = p;
Elliott Hughes48436bb2012-02-07 15:23:28 -0800672 while (++p != end && *p != separator) {
673 // Skip to the next occurrence of the separator.
Elliott Hughes34023802011-08-30 12:06:17 -0700674 }
675 result.push_back(std::string(start, p - start));
676 }
677 }
678}
679
Elliott Hughes48436bb2012-02-07 15:23:28 -0800680template <typename StringT>
681std::string Join(std::vector<StringT>& strings, char separator) {
682 if (strings.empty()) {
683 return "";
684 }
685
686 std::string result(strings[0]);
687 for (size_t i = 1; i < strings.size(); ++i) {
688 result += separator;
689 result += strings[i];
690 }
691 return result;
692}
693
694// Explicit instantiations.
695template std::string Join<std::string>(std::vector<std::string>& strings, char separator);
696template std::string Join<const char*>(std::vector<const char*>& strings, char separator);
697template std::string Join<char*>(std::vector<char*>& strings, char separator);
698
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800699bool StartsWith(const std::string& s, const char* prefix) {
700 return s.compare(0, strlen(prefix), prefix) == 0;
701}
702
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800703void SetThreadName(const char* threadName) {
Elliott Hughes06e3ad42012-02-07 14:51:57 -0800704 ANNOTATE_THREAD_NAME(threadName); // For tsan.
705
Elliott Hughesdcc24742011-09-07 14:02:44 -0700706 int hasAt = 0;
707 int hasDot = 0;
Elliott Hughesc1f143d2011-12-01 17:31:10 -0800708 const char* s = threadName;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700709 while (*s) {
710 if (*s == '.') {
711 hasDot = 1;
712 } else if (*s == '@') {
713 hasAt = 1;
714 }
715 s++;
716 }
717 int len = s - threadName;
718 if (len < 15 || hasAt || !hasDot) {
719 s = threadName;
720 } else {
721 s = threadName + len - 15;
722 }
723#if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP)
Elliott Hughes7c6a61e2012-03-12 18:01:41 -0700724 // pthread_setname_np fails rather than truncating long strings.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700725 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
726 strncpy(buf, s, sizeof(buf)-1);
727 buf[sizeof(buf)-1] = '\0';
728 errno = pthread_setname_np(pthread_self(), buf);
729 if (errno != 0) {
730 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
731 }
Elliott Hughes4ae722a2012-03-13 11:08:51 -0700732#elif defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
Elliott Hughes7c6a61e2012-03-12 18:01:41 -0700733 pthread_setname_np(threadName);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700734#elif defined(HAVE_PRCTL)
735 prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);
736#else
Elliott Hughesad6c9c32012-01-19 17:39:12 -0800737 UNIMPLEMENTED(WARNING) << threadName;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700738#endif
739}
740
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700741void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu) {
742 utime = stime = task_cpu = 0;
743 std::string stats;
744 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) {
745 return;
746 }
747 // Skip the command, which may contain spaces.
748 stats = stats.substr(stats.find(')') + 2);
749 // Extract the three fields we care about.
750 std::vector<std::string> fields;
751 Split(stats, ' ', fields);
752 utime = strtoull(fields[11].c_str(), NULL, 10);
753 stime = strtoull(fields[12].c_str(), NULL, 10);
754 task_cpu = strtoull(fields[36].c_str(), NULL, 10);
755}
756
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800757const char* GetAndroidRoot() {
758 const char* android_root = getenv("ANDROID_ROOT");
759 if (android_root == NULL) {
760 if (OS::DirectoryExists("/system")) {
761 android_root = "/system";
Brian Carlstroma9f19782011-10-13 00:14:47 -0700762 } else {
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800763 LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist";
764 return "";
Brian Carlstroma9f19782011-10-13 00:14:47 -0700765 }
766 }
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800767 if (!OS::DirectoryExists(android_root)) {
768 LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700769 return "";
770 }
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800771 return android_root;
772}
Brian Carlstroma9f19782011-10-13 00:14:47 -0700773
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800774const char* GetAndroidData() {
775 const char* android_data = getenv("ANDROID_DATA");
776 if (android_data == NULL) {
777 if (OS::DirectoryExists("/data")) {
778 android_data = "/data";
779 } else {
780 LOG(FATAL) << "ANDROID_DATA not set and /data does not exist";
781 return "";
782 }
783 }
784 if (!OS::DirectoryExists(android_data)) {
785 LOG(FATAL) << "Failed to find ANDROID_DATA directory " << android_data;
786 return "";
787 }
788 return android_data;
789}
790
791std::string GetArtCacheOrDie() {
792 std::string art_cache(StringPrintf("%s/art-cache", GetAndroidData()));
Brian Carlstroma9f19782011-10-13 00:14:47 -0700793
794 if (!OS::DirectoryExists(art_cache.c_str())) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800795 if (StartsWith(art_cache, "/tmp/")) {
Brian Carlstroma9f19782011-10-13 00:14:47 -0700796 int result = mkdir(art_cache.c_str(), 0700);
797 if (result != 0) {
798 LOG(FATAL) << "Failed to create art-cache directory " << art_cache;
799 return "";
800 }
801 } else {
802 LOG(FATAL) << "Failed to find art-cache directory " << art_cache;
803 return "";
804 }
805 }
806 return art_cache;
807}
808
jeffhao262bf462011-10-20 18:36:32 -0700809std::string GetArtCacheFilenameOrDie(const std::string& location) {
Elliott Hughes95572412011-12-13 18:14:20 -0800810 std::string art_cache(GetArtCacheOrDie());
Elliott Hughesc308a5d2012-02-16 17:12:06 -0800811 CHECK_EQ(location[0], '/') << location;
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700812 std::string cache_file(location, 1); // skip leading slash
813 std::replace(cache_file.begin(), cache_file.end(), '/', '@');
814 return art_cache + "/" + cache_file;
815}
816
jeffhao262bf462011-10-20 18:36:32 -0700817bool IsValidZipFilename(const std::string& filename) {
818 if (filename.size() < 4) {
819 return false;
820 }
821 std::string suffix(filename.substr(filename.size() - 4));
822 return (suffix == ".zip" || suffix == ".jar" || suffix == ".apk");
823}
824
825bool IsValidDexFilename(const std::string& filename) {
826 if (filename.size() < 4) {
827 return false;
828 }
829 std::string suffix(filename.substr(filename.size() - 4));
830 return (suffix == ".dex");
831}
832
Elliott Hughes42ee1422011-09-06 12:33:32 -0700833} // namespace art