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