blob: ffa9d458124bfde630649954e83d8fb09e0ec64a [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
Christopher Ferris943af7d2014-01-16 12:41:46 -080019#include <inttypes.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>
Brian Carlstrom4cf5e572014-02-25 11:47:48 -080024#include <sys/wait.h>
Elliott Hughes42ee1422011-09-06 12:33:32 -070025#include <unistd.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026
Ian Rogers700a4022014-05-19 16:49:03 -070027#include <memory>
Elliott Hughes42ee1422011-09-06 12:33:32 -070028
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080030#include "android-base/strings.h"
31
Brian Carlstrom6449c622014-02-10 23:48:36 -080032#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080033#include "base/unix_file/fd_file.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "dex_file-inl.h"
Andreas Gampe5073fed2015-08-10 11:40:25 -070035#include "dex_instruction.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010036#include "oat_quick_method_header.h"
buzbeec143c552011-08-20 17:38:58 -070037#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Ian Rogersa6724902013-09-23 09:23:37 -070039#include "utf-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070040
Elliott Hughes4ae722a2012-03-13 11:08:51 -070041#if defined(__APPLE__)
David Sehrfa442002016-08-22 18:42:08 -070042#include <crt_externs.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include <sys/syscall.h>
44#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
Elliott Hughes4ae722a2012-03-13 11:08:51 -070045#endif
46
Elliott Hughes058a6de2012-05-24 19:13:02 -070047#if defined(__linux__)
Elliott Hughese1aee692012-01-17 16:40:10 -080048#include <linux/unistd.h>
Elliott Hughese1aee692012-01-17 16:40:10 -080049#endif
50
Elliott Hughes11e45072011-08-16 17:40:46 -070051namespace art {
52
Andreas Gampe46ee31b2016-12-14 10:11:49 -080053using android::base::StringAppendF;
54using android::base::StringPrintf;
55
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080056pid_t GetTid() {
Brian Carlstromf3a26412012-08-24 11:06:02 -070057#if defined(__APPLE__)
58 uint64_t owner;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070059 CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__); // Requires Mac OS 10.6
Brian Carlstromf3a26412012-08-24 11:06:02 -070060 return owner;
Elliott Hughes323aa862014-08-20 15:00:04 -070061#elif defined(__BIONIC__)
62 return gettid();
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080063#else
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080064 return syscall(__NR_gettid);
65#endif
66}
67
Elliott Hughes289be852012-06-12 13:57:20 -070068std::string GetThreadName(pid_t tid) {
69 std::string result;
70 if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070071 result.resize(result.size() - 1); // Lose the trailing '\n'.
Elliott Hughes289be852012-06-12 13:57:20 -070072 } else {
73 result = "<unknown>";
74 }
75 return result;
76}
77
Elliott Hughesd92bec42011-09-02 17:04:36 -070078bool ReadFileToString(const std::string& file_name, std::string* result) {
Andreas Gampedf878922015-08-13 16:44:54 -070079 File file(file_name, O_RDONLY, false);
80 if (!file.IsOpened()) {
Elliott Hughesd92bec42011-09-02 17:04:36 -070081 return false;
82 }
buzbeec143c552011-08-20 17:38:58 -070083
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070084 std::vector<char> buf(8 * KB);
buzbeec143c552011-08-20 17:38:58 -070085 while (true) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -080086 int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[0], buf.size()));
Elliott Hughesd92bec42011-09-02 17:04:36 -070087 if (n == -1) {
88 return false;
buzbeec143c552011-08-20 17:38:58 -070089 }
Elliott Hughesd92bec42011-09-02 17:04:36 -070090 if (n == 0) {
91 return true;
92 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070093 result->append(&buf[0], n);
buzbeec143c552011-08-20 17:38:58 -070094 }
buzbeec143c552011-08-20 17:38:58 -070095}
96
Andreas Gampea6dfdae2015-02-24 15:50:19 -080097bool PrintFileToLog(const std::string& file_name, LogSeverity level) {
Andreas Gampedf878922015-08-13 16:44:54 -070098 File file(file_name, O_RDONLY, false);
99 if (!file.IsOpened()) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800100 return false;
101 }
102
103 constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings.
104 char buf[kBufSize + 1]; // +1 for terminator.
105 size_t filled_to = 0;
106 while (true) {
107 DCHECK_LT(filled_to, kBufSize);
108 int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to));
109 if (n <= 0) {
110 // Print the rest of the buffer, if it exists.
111 if (filled_to > 0) {
112 buf[filled_to] = 0;
113 LOG(level) << buf;
114 }
115 return n == 0;
116 }
117 // Scan for '\n'.
118 size_t i = filled_to;
119 bool found_newline = false;
120 for (; i < filled_to + n; ++i) {
121 if (buf[i] == '\n') {
122 // Found a line break, that's something to print now.
123 buf[i] = 0;
124 LOG(level) << buf;
125 // Copy the rest to the front.
126 if (i + 1 < filled_to + n) {
127 memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1);
128 filled_to = filled_to + n - i - 1;
129 } else {
130 filled_to = 0;
131 }
132 found_newline = true;
133 break;
134 }
135 }
136 if (found_newline) {
137 continue;
138 } else {
139 filled_to += n;
140 // Check if we must flush now.
141 if (filled_to == kBufSize) {
142 buf[kBufSize] = 0;
143 LOG(level) << buf;
144 filled_to = 0;
145 }
146 }
147 }
148}
149
Ian Rogers1ff3c982014-08-12 02:30:58 -0700150std::string PrettyDescriptor(const char* descriptor) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700151 // Count the number of '['s to get the dimensionality.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700152 const char* c = descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -0700153 size_t dim = 0;
154 while (*c == '[') {
155 dim++;
156 c++;
157 }
158
159 // Reference or primitive?
160 if (*c == 'L') {
161 // "[[La/b/C;" -> "a.b.C[][]".
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700162 c++; // Skip the 'L'.
Elliott Hughes11e45072011-08-16 17:40:46 -0700163 } else {
164 // "[[B" -> "byte[][]".
165 // To make life easier, we make primitives look like unqualified
166 // reference types.
167 switch (*c) {
168 case 'B': c = "byte;"; break;
169 case 'C': c = "char;"; break;
170 case 'D': c = "double;"; break;
171 case 'F': c = "float;"; break;
172 case 'I': c = "int;"; break;
173 case 'J': c = "long;"; break;
174 case 'S': c = "short;"; break;
175 case 'Z': c = "boolean;"; break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700176 case 'V': c = "void;"; break; // Used when decoding return types.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700177 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -0700178 }
179 }
180
181 // At this point, 'c' is a string of the form "fully/qualified/Type;"
182 // or "primitive;". Rewrite the type with '.' instead of '/':
183 std::string result;
184 const char* p = c;
185 while (*p != ';') {
186 char ch = *p++;
187 if (ch == '/') {
188 ch = '.';
189 }
190 result.push_back(ch);
191 }
192 // ...and replace the semicolon with 'dim' "[]" pairs:
Ian Rogers1ff3c982014-08-12 02:30:58 -0700193 for (size_t i = 0; i < dim; ++i) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700194 result += "[]";
195 }
196 return result;
197}
198
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700199std::string PrettyArguments(const char* signature) {
200 std::string result;
201 result += '(';
202 CHECK_EQ(*signature, '(');
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700203 ++signature; // Skip the '('.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700204 while (*signature != ')') {
205 size_t argument_length = 0;
206 while (signature[argument_length] == '[') {
207 ++argument_length;
208 }
209 if (signature[argument_length] == 'L') {
210 argument_length = (strchr(signature, ';') - signature + 1);
211 } else {
212 ++argument_length;
213 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 {
215 std::string argument_descriptor(signature, argument_length);
216 result += PrettyDescriptor(argument_descriptor.c_str());
217 }
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700218 if (signature[argument_length] != ')') {
219 result += ", ";
220 }
221 signature += argument_length;
222 }
223 CHECK_EQ(*signature, ')');
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700224 ++signature; // Skip the ')'.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700225 result += ')';
226 return result;
227}
228
229std::string PrettyReturnType(const char* signature) {
230 const char* return_type = strchr(signature, ')');
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700231 CHECK(return_type != nullptr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700232 ++return_type; // Skip ')'.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700233 return PrettyDescriptor(return_type);
234}
235
Andreas Gampec0d82292014-09-23 10:38:30 -0700236std::string PrettyJavaAccessFlags(uint32_t access_flags) {
237 std::string result;
238 if ((access_flags & kAccPublic) != 0) {
239 result += "public ";
240 }
241 if ((access_flags & kAccProtected) != 0) {
242 result += "protected ";
243 }
244 if ((access_flags & kAccPrivate) != 0) {
245 result += "private ";
246 }
247 if ((access_flags & kAccFinal) != 0) {
248 result += "final ";
249 }
250 if ((access_flags & kAccStatic) != 0) {
251 result += "static ";
252 }
David Brazdilca3c8c32016-09-06 14:04:48 +0100253 if ((access_flags & kAccAbstract) != 0) {
254 result += "abstract ";
255 }
256 if ((access_flags & kAccInterface) != 0) {
257 result += "interface ";
258 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700259 if ((access_flags & kAccTransient) != 0) {
260 result += "transient ";
261 }
262 if ((access_flags & kAccVolatile) != 0) {
263 result += "volatile ";
264 }
265 if ((access_flags & kAccSynchronized) != 0) {
266 result += "synchronized ";
267 }
268 return result;
269}
270
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800271std::string PrettySize(int64_t byte_count) {
Elliott Hughesc967f782012-04-16 10:23:15 -0700272 // The byte thresholds at which we display amounts. A byte count is displayed
273 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274 static const int64_t kUnitThresholds[] = {
Elliott Hughesc967f782012-04-16 10:23:15 -0700275 0, // B up to...
276 3*1024, // KB up to...
277 2*1024*1024, // MB up to...
278 1024*1024*1024 // GB from here.
279 };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800280 static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB };
Elliott Hughesc967f782012-04-16 10:23:15 -0700281 static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800282 const char* negative_str = "";
283 if (byte_count < 0) {
284 negative_str = "-";
285 byte_count = -byte_count;
286 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700287 int i = arraysize(kUnitThresholds);
288 while (--i > 0) {
289 if (byte_count >= kUnitThresholds[i]) {
290 break;
291 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800292 }
Brian Carlstrom474cc792014-03-07 14:18:15 -0800293 return StringPrintf("%s%" PRId64 "%s",
294 negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800295}
296
Andreas Gampe9186ced2016-12-12 14:28:21 -0800297static inline constexpr bool NeedsEscaping(uint16_t ch) {
298 return (ch < ' ' || ch > '~');
299}
300
Ian Rogers576ca0c2014-06-06 15:58:22 -0700301std::string PrintableChar(uint16_t ch) {
302 std::string result;
303 result += '\'';
304 if (NeedsEscaping(ch)) {
305 StringAppendF(&result, "\\u%04x", ch);
306 } else {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700307 result += static_cast<std::string::value_type>(ch);
Ian Rogers576ca0c2014-06-06 15:58:22 -0700308 }
309 result += '\'';
310 return result;
311}
312
Ian Rogers68b56852014-08-29 20:19:11 -0700313std::string PrintableString(const char* utf) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700314 std::string result;
315 result += '"';
Ian Rogers68b56852014-08-29 20:19:11 -0700316 const char* p = utf;
Elliott Hughes82914b62012-04-09 15:56:29 -0700317 size_t char_count = CountModifiedUtf8Chars(p);
318 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000319 uint32_t ch = GetUtf16FromUtf8(&p);
Elliott Hughes82914b62012-04-09 15:56:29 -0700320 if (ch == '\\') {
321 result += "\\\\";
322 } else if (ch == '\n') {
323 result += "\\n";
324 } else if (ch == '\r') {
325 result += "\\r";
326 } else if (ch == '\t') {
327 result += "\\t";
Elliott Hughes82914b62012-04-09 15:56:29 -0700328 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000329 const uint16_t leading = GetLeadingUtf16Char(ch);
330
331 if (NeedsEscaping(leading)) {
332 StringAppendF(&result, "\\u%04x", leading);
333 } else {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700334 result += static_cast<std::string::value_type>(leading);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000335 }
336
337 const uint32_t trailing = GetTrailingUtf16Char(ch);
338 if (trailing != 0) {
339 // All high surrogates will need escaping.
340 StringAppendF(&result, "\\u%04x", trailing);
341 }
Elliott Hughes82914b62012-04-09 15:56:29 -0700342 }
343 }
344 result += '"';
345 return result;
346}
347
Alex Light888a59e2017-01-25 11:41:41 -0800348std::string GetJniShortName(const std::string& class_descriptor, const std::string& method) {
349 // Remove the leading 'L' and trailing ';'...
350 std::string class_name(class_descriptor);
351 CHECK_EQ(class_name[0], 'L') << class_name;
352 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
353 class_name.erase(0, 1);
354 class_name.erase(class_name.size() - 1, 1);
355
356 std::string short_name;
357 short_name += "Java_";
358 short_name += MangleForJni(class_name);
359 short_name += "_";
360 short_name += MangleForJni(method);
361 return short_name;
362}
363
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800364// 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 -0700365std::string MangleForJni(const std::string& s) {
366 std::string result;
367 size_t char_count = CountModifiedUtf8Chars(s.c_str());
368 const char* cp = &s[0];
369 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000370 uint32_t ch = GetUtf16FromUtf8(&cp);
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800371 if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
372 result.push_back(ch);
373 } else if (ch == '.' || ch == '/') {
374 result += "_";
375 } else if (ch == '_') {
376 result += "_1";
377 } else if (ch == ';') {
378 result += "_2";
379 } else if (ch == '[') {
380 result += "_3";
Elliott Hughes79082e32011-08-25 12:07:32 -0700381 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000382 const uint16_t leading = GetLeadingUtf16Char(ch);
383 const uint32_t trailing = GetTrailingUtf16Char(ch);
384
385 StringAppendF(&result, "_0%04x", leading);
386 if (trailing != 0) {
387 StringAppendF(&result, "_0%04x", trailing);
388 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700389 }
390 }
391 return result;
392}
393
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700394std::string DotToDescriptor(const char* class_name) {
395 std::string descriptor(class_name);
396 std::replace(descriptor.begin(), descriptor.end(), '.', '/');
397 if (descriptor.length() > 0 && descriptor[0] != '[') {
398 descriptor = "L" + descriptor + ";";
399 }
400 return descriptor;
401}
402
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800403std::string DescriptorToDot(const char* descriptor) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800404 size_t length = strlen(descriptor);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700405 if (length > 1) {
406 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
407 // Descriptors have the leading 'L' and trailing ';' stripped.
408 std::string result(descriptor + 1, length - 2);
409 std::replace(result.begin(), result.end(), '/', '.');
410 return result;
411 } else {
412 // For arrays the 'L' and ';' remain intact.
413 std::string result(descriptor);
414 std::replace(result.begin(), result.end(), '/', '.');
415 return result;
416 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800417 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700418 // Do nothing for non-class/array descriptors.
Elliott Hughes2435a572012-02-17 16:07:41 -0800419 return descriptor;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800420}
421
422std::string DescriptorToName(const char* descriptor) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800423 size_t length = strlen(descriptor);
Elliott Hughes2435a572012-02-17 16:07:41 -0800424 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
425 std::string result(descriptor + 1, length - 2);
426 return result;
427 }
428 return descriptor;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700429}
430
jeffhao10037c82012-01-23 15:06:23 -0800431// Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700432uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700433 0x00000000, // 00..1f low control characters; nothing valid
434 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
435 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
436 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z'
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700437};
438
jeffhao10037c82012-01-23 15:06:23 -0800439// Helper for IsValidPartOfMemberNameUtf8(); do not call directly.
440bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700441 /*
442 * It's a multibyte encoded character. Decode it and analyze. We
443 * accept anything that isn't (a) an improperly encoded low value,
444 * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
445 * control character, or (e) a high space, layout, or special
446 * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
447 * U+fff0..U+ffff). This is all specified in the dex format
448 * document.
449 */
450
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000451 const uint32_t pair = GetUtf16FromUtf8(pUtf8Ptr);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000452 const uint16_t leading = GetLeadingUtf16Char(pair);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000453
Narayan Kamath8508e372015-05-06 14:55:43 +0100454 // We have a surrogate pair resulting from a valid 4 byte UTF sequence.
455 // No further checks are necessary because 4 byte sequences span code
456 // points [U+10000, U+1FFFFF], which are valid codepoints in a dex
457 // identifier. Furthermore, GetUtf16FromUtf8 guarantees that each of
458 // the surrogate halves are valid and well formed in this instance.
459 if (GetTrailingUtf16Char(pair) != 0) {
460 return true;
461 }
462
463
464 // We've encountered a one, two or three byte UTF-8 sequence. The
465 // three byte UTF-8 sequence could be one half of a surrogate pair.
466 switch (leading >> 8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000467 case 0x00:
468 // It's only valid if it's above the ISO-8859-1 high space (0xa0).
469 return (leading > 0x00a0);
470 case 0xd8:
471 case 0xd9:
472 case 0xda:
473 case 0xdb:
Narayan Kamath8508e372015-05-06 14:55:43 +0100474 {
475 // We found a three byte sequence encoding one half of a surrogate.
476 // Look for the other half.
477 const uint32_t pair2 = GetUtf16FromUtf8(pUtf8Ptr);
478 const uint16_t trailing = GetLeadingUtf16Char(pair2);
479
480 return (GetTrailingUtf16Char(pair2) == 0) && (0xdc00 <= trailing && trailing <= 0xdfff);
481 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000482 case 0xdc:
483 case 0xdd:
484 case 0xde:
485 case 0xdf:
486 // It's a trailing surrogate, which is not valid at this point.
487 return false;
488 case 0x20:
489 case 0xff:
490 // It's in the range that has spaces, controls, and specials.
491 switch (leading & 0xfff8) {
Narayan Kamath8508e372015-05-06 14:55:43 +0100492 case 0x2000:
493 case 0x2008:
494 case 0x2028:
495 case 0xfff0:
496 case 0xfff8:
497 return false;
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000498 }
Narayan Kamath8508e372015-05-06 14:55:43 +0100499 return true;
500 default:
501 return true;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700502 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000503
Narayan Kamath8508e372015-05-06 14:55:43 +0100504 UNREACHABLE();
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700505}
506
507/* Return whether the pointed-at modified-UTF-8 encoded character is
508 * valid as part of a member name, updating the pointer to point past
509 * the consumed character. This will consume two encoded UTF-16 code
510 * points if the character is encoded as a surrogate pair. Also, if
511 * this function returns false, then the given pointer may only have
512 * been partially advanced.
513 */
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700514static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700515 uint8_t c = (uint8_t) **pUtf8Ptr;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700516 if (LIKELY(c <= 0x7f)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700517 // It's low-ascii, so check the table.
518 uint32_t wordIdx = c >> 5;
519 uint32_t bitIdx = c & 0x1f;
520 (*pUtf8Ptr)++;
521 return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
522 }
523
524 // It's a multibyte encoded character. Call a non-inline function
525 // for the heavy lifting.
jeffhao10037c82012-01-23 15:06:23 -0800526 return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr);
527}
528
529bool IsValidMemberName(const char* s) {
530 bool angle_name = false;
531
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700532 switch (*s) {
jeffhao10037c82012-01-23 15:06:23 -0800533 case '\0':
534 // The empty string is not a valid name.
535 return false;
536 case '<':
537 angle_name = true;
538 s++;
539 break;
540 }
541
542 while (true) {
543 switch (*s) {
544 case '\0':
545 return !angle_name;
546 case '>':
547 return angle_name && s[1] == '\0';
548 }
549
550 if (!IsValidPartOfMemberNameUtf8(&s)) {
551 return false;
552 }
553 }
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700554}
555
Elliott Hughes906e6852011-10-28 14:52:10 -0700556enum ClassNameType { kName, kDescriptor };
Ian Rogers7b078e82014-09-10 14:44:24 -0700557template<ClassNameType kType, char kSeparator>
558static bool IsValidClassName(const char* s) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700559 int arrayCount = 0;
560 while (*s == '[') {
561 arrayCount++;
562 s++;
563 }
564
565 if (arrayCount > 255) {
566 // Arrays may have no more than 255 dimensions.
567 return false;
568 }
569
Ian Rogers7b078e82014-09-10 14:44:24 -0700570 ClassNameType type = kType;
571 if (type != kDescriptor && arrayCount != 0) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700572 /*
573 * If we're looking at an array of some sort, then it doesn't
574 * matter if what is being asked for is a class name; the
575 * format looks the same as a type descriptor in that case, so
576 * treat it as such.
577 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700578 type = kDescriptor;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700579 }
580
Elliott Hughes906e6852011-10-28 14:52:10 -0700581 if (type == kDescriptor) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700582 /*
583 * We are looking for a descriptor. Either validate it as a
584 * single-character primitive type, or continue on to check the
585 * embedded class name (bracketed by "L" and ";").
586 */
587 switch (*(s++)) {
588 case 'B':
589 case 'C':
590 case 'D':
591 case 'F':
592 case 'I':
593 case 'J':
594 case 'S':
595 case 'Z':
596 // These are all single-character descriptors for primitive types.
597 return (*s == '\0');
598 case 'V':
599 // Non-array void is valid, but you can't have an array of void.
600 return (arrayCount == 0) && (*s == '\0');
601 case 'L':
602 // Class name: Break out and continue below.
603 break;
604 default:
605 // Oddball descriptor character.
606 return false;
607 }
608 }
609
610 /*
611 * We just consumed the 'L' that introduces a class name as part
612 * of a type descriptor, or we are looking for an unadorned class
613 * name.
614 */
615
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700616 bool sepOrFirst = true; // first character or just encountered a separator.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700617 for (;;) {
618 uint8_t c = (uint8_t) *s;
619 switch (c) {
620 case '\0':
621 /*
622 * Premature end for a type descriptor, but valid for
623 * a class name as long as we haven't encountered an
624 * empty component (including the degenerate case of
625 * the empty string "").
626 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700627 return (type == kName) && !sepOrFirst;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700628 case ';':
629 /*
630 * Invalid character for a class name, but the
631 * legitimate end of a type descriptor. In the latter
632 * case, make sure that this is the end of the string
633 * and that it doesn't end with an empty component
634 * (including the degenerate case of "L;").
635 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700636 return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0');
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700637 case '/':
638 case '.':
Ian Rogers7b078e82014-09-10 14:44:24 -0700639 if (c != kSeparator) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700640 // The wrong separator character.
641 return false;
642 }
643 if (sepOrFirst) {
644 // Separator at start or two separators in a row.
645 return false;
646 }
647 sepOrFirst = true;
648 s++;
649 break;
650 default:
jeffhao10037c82012-01-23 15:06:23 -0800651 if (!IsValidPartOfMemberNameUtf8(&s)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700652 return false;
653 }
654 sepOrFirst = false;
655 break;
656 }
657 }
658}
659
Elliott Hughes906e6852011-10-28 14:52:10 -0700660bool IsValidBinaryClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700661 return IsValidClassName<kName, '.'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700662}
663
664bool IsValidJniClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700665 return IsValidClassName<kName, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700666}
667
668bool IsValidDescriptor(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700669 return IsValidClassName<kDescriptor, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700670}
671
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700672void Split(const std::string& s, char separator, std::vector<std::string>* result) {
Elliott Hughes34023802011-08-30 12:06:17 -0700673 const char* p = s.data();
674 const char* end = p + s.size();
675 while (p != end) {
Elliott Hughes48436bb2012-02-07 15:23:28 -0800676 if (*p == separator) {
Elliott Hughes34023802011-08-30 12:06:17 -0700677 ++p;
678 } else {
679 const char* start = p;
Elliott Hughes48436bb2012-02-07 15:23:28 -0800680 while (++p != end && *p != separator) {
681 // Skip to the next occurrence of the separator.
Elliott Hughes34023802011-08-30 12:06:17 -0700682 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700683 result->push_back(std::string(start, p - start));
Elliott Hughes34023802011-08-30 12:06:17 -0700684 }
685 }
686}
687
Elliott Hughes22869a92012-03-27 14:08:24 -0700688void SetThreadName(const char* thread_name) {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700689 int hasAt = 0;
690 int hasDot = 0;
Elliott Hughes22869a92012-03-27 14:08:24 -0700691 const char* s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700692 while (*s) {
693 if (*s == '.') {
694 hasDot = 1;
695 } else if (*s == '@') {
696 hasAt = 1;
697 }
698 s++;
699 }
Elliott Hughes22869a92012-03-27 14:08:24 -0700700 int len = s - thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700701 if (len < 15 || hasAt || !hasDot) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700702 s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700703 } else {
Elliott Hughes22869a92012-03-27 14:08:24 -0700704 s = thread_name + len - 15;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700705 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800706#if defined(__linux__)
Elliott Hughes7c6a61e2012-03-12 18:01:41 -0700707 // pthread_setname_np fails rather than truncating long strings.
Elliott Hughes0a18df82015-01-09 15:16:16 -0800708 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700709 strncpy(buf, s, sizeof(buf)-1);
710 buf[sizeof(buf)-1] = '\0';
711 errno = pthread_setname_np(pthread_self(), buf);
712 if (errno != 0) {
713 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
714 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800715#else // __APPLE__
Elliott Hughes22869a92012-03-27 14:08:24 -0700716 pthread_setname_np(thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700717#endif
718}
719
Brian Carlstrom29212012013-09-12 22:18:30 -0700720void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
721 *utime = *stime = *task_cpu = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700722 std::string stats;
Elliott Hughes8a31b502012-04-30 19:36:11 -0700723 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700724 return;
725 }
726 // Skip the command, which may contain spaces.
727 stats = stats.substr(stats.find(')') + 2);
728 // Extract the three fields we care about.
729 std::vector<std::string> fields;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700730 Split(stats, ' ', &fields);
Brian Carlstrom29212012013-09-12 22:18:30 -0700731 *state = fields[0][0];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700732 *utime = strtoull(fields[11].c_str(), nullptr, 10);
733 *stime = strtoull(fields[12].c_str(), nullptr, 10);
734 *task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700735}
736
Calin Juravle36eb3132017-01-13 16:32:38 -0800737static const char* GetAndroidDirSafe(const char* env_var,
738 const char* default_dir,
739 std::string* error_msg) {
740 const char* android_dir = getenv(env_var);
741 if (android_dir == nullptr) {
742 if (OS::DirectoryExists(default_dir)) {
743 android_dir = default_dir;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700744 } else {
Calin Juravle36eb3132017-01-13 16:32:38 -0800745 *error_msg = StringPrintf("%s not set and %s does not exist", env_var, default_dir);
746 return nullptr;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700747 }
748 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800749 if (!OS::DirectoryExists(android_dir)) {
750 *error_msg = StringPrintf("Failed to find %s directory %s", env_var, android_dir);
751 return nullptr;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700752 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800753 return android_dir;
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800754}
Brian Carlstroma9f19782011-10-13 00:14:47 -0700755
Calin Juravle36eb3132017-01-13 16:32:38 -0800756const char* GetAndroidDir(const char* env_var, const char* default_dir) {
Alex Lighta59dd802014-07-02 16:28:08 -0700757 std::string error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800758 const char* dir = GetAndroidDirSafe(env_var, default_dir, &error_msg);
Alex Lighta59dd802014-07-02 16:28:08 -0700759 if (dir != nullptr) {
760 return dir;
761 } else {
762 LOG(FATAL) << error_msg;
Calin Juravle36eb3132017-01-13 16:32:38 -0800763 return nullptr;
Alex Lighta59dd802014-07-02 16:28:08 -0700764 }
765}
766
Calin Juravle36eb3132017-01-13 16:32:38 -0800767const char* GetAndroidRoot() {
768 return GetAndroidDir("ANDROID_ROOT", "/system");
769}
770
771const char* GetAndroidRootSafe(std::string* error_msg) {
772 return GetAndroidDirSafe("ANDROID_ROOT", "/system", error_msg);
773}
774
775const char* GetAndroidData() {
776 return GetAndroidDir("ANDROID_DATA", "/data");
777}
778
Alex Lighta59dd802014-07-02 16:28:08 -0700779const char* GetAndroidDataSafe(std::string* error_msg) {
Calin Juravle36eb3132017-01-13 16:32:38 -0800780 return GetAndroidDirSafe("ANDROID_DATA", "/data", error_msg);
781}
782
783std::string GetDefaultBootImageLocation(std::string* error_msg) {
784 const char* android_root = GetAndroidRootSafe(error_msg);
785 if (android_root == nullptr) {
786 return "";
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800787 }
Calin Juravle36eb3132017-01-13 16:32:38 -0800788 return StringPrintf("%s/framework/boot.art", android_root);
Brian Carlstroma56fcd62012-02-04 21:23:01 -0800789}
790
Alex Lighta59dd802014-07-02 16:28:08 -0700791void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string* dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -0700792 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700793 CHECK(subdir != nullptr);
794 std::string error_msg;
795 const char* android_data = GetAndroidDataSafe(&error_msg);
796 if (android_data == nullptr) {
797 *have_android_data = false;
798 *dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -0700799 *is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -0700800 return;
801 } else {
802 *have_android_data = true;
803 }
804 const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
805 *dalvik_cache = dalvik_cache_root + subdir;
806 *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str());
Andreas Gampe3c13a792014-09-18 20:56:04 -0700807 *is_global_cache = strcmp(android_data, "/data") == 0;
808 if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -0700809 // Don't create the system's /data/dalvik-cache/... because it needs special permissions.
810 *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) &&
811 (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST));
812 }
813}
814
Richard Uhler55b58b62016-08-12 09:05:13 -0700815std::string GetDalvikCache(const char* subdir) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100816 CHECK(subdir != nullptr);
Brian Carlstrom41ccffd2014-05-06 10:37:30 -0700817 const char* android_data = GetAndroidData();
818 const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
Narayan Kamath11d9f062014-04-23 20:24:57 +0100819 const std::string dalvik_cache = dalvik_cache_root + subdir;
Andreas Gampe40da2862015-02-27 12:49:04 -0800820 if (!OS::DirectoryExists(dalvik_cache.c_str())) {
Richard Uhler55b58b62016-08-12 09:05:13 -0700821 // TODO: Check callers. Traditional behavior is to not abort.
822 return "";
Brian Carlstroma9f19782011-10-13 00:14:47 -0700823 }
Brian Carlstrom7675e162013-06-10 16:18:04 -0700824 return dalvik_cache;
Brian Carlstroma9f19782011-10-13 00:14:47 -0700825}
826
Alex Lighta59dd802014-07-02 16:28:08 -0700827bool GetDalvikCacheFilename(const char* location, const char* cache_location,
828 std::string* filename, std::string* error_msg) {
Ian Rogerse6060102013-05-16 12:01:04 -0700829 if (location[0] != '/') {
Alex Lighta59dd802014-07-02 16:28:08 -0700830 *error_msg = StringPrintf("Expected path in location to be absolute: %s", location);
831 return false;
Ian Rogerse6060102013-05-16 12:01:04 -0700832 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700833 std::string cache_file(&location[1]); // skip leading slash
Andreas Gampe9186ced2016-12-12 14:28:21 -0800834 if (!android::base::EndsWith(location, ".dex") &&
835 !android::base::EndsWith(location, ".art") &&
836 !android::base::EndsWith(location, ".oat")) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -0700837 cache_file += "/";
838 cache_file += DexFile::kClassesDex;
839 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -0700840 std::replace(cache_file.begin(), cache_file.end(), '/', '@');
Alex Lighta59dd802014-07-02 16:28:08 -0700841 *filename = StringPrintf("%s/%s", cache_location, cache_file.c_str());
842 return true;
843}
844
Calin Juravle367b9d82017-05-15 18:18:39 -0700845std::string GetVdexFilename(const std::string& oat_location) {
846 return ReplaceFileExtension(oat_location, "vdex");
847}
848
Brian Carlstrom2afe4942014-05-19 10:25:33 -0700849static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) {
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700850 // in = /foo/bar/baz
851 // out = /foo/bar/<isa>/baz
852 size_t pos = filename->rfind('/');
853 CHECK_NE(pos, std::string::npos) << *filename << " " << isa;
854 filename->insert(pos, "/", 1);
855 filename->insert(pos + 1, GetInstructionSetString(isa));
856}
857
858std::string GetSystemImageFilename(const char* location, const InstructionSet isa) {
859 // location = /system/framework/boot.art
860 // filename = /system/framework/<isa>/boot.art
861 std::string filename(location);
Brian Carlstrom2afe4942014-05-19 10:25:33 -0700862 InsertIsaDirectory(isa, &filename);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -0700863 return filename;
864}
865
Calin Juravle5e2b9712015-12-18 14:10:00 +0200866bool FileExists(const std::string& filename) {
867 struct stat buffer;
868 return stat(filename.c_str(), &buffer) == 0;
869}
870
Calin Juravleb9c1b9b2016-03-17 17:07:52 +0000871bool FileExistsAndNotEmpty(const std::string& filename) {
872 struct stat buffer;
873 if (stat(filename.c_str(), &buffer) != 0) {
874 return false;
875 }
876 return buffer.st_size > 0;
877}
878
David Brazdil7b49e6c2016-09-01 11:06:18 +0100879std::string ReplaceFileExtension(const std::string& filename, const std::string& new_extension) {
880 const size_t last_ext = filename.find_last_of('.');
881 if (last_ext == std::string::npos) {
882 return filename + "." + new_extension;
883 } else {
884 return filename.substr(0, last_ext + 1) + new_extension;
885 }
886}
887
Mathieu Chartier76433272014-09-26 14:32:37 -0700888std::string PrettyDescriptor(Primitive::Type type) {
889 return PrettyDescriptor(Primitive::Descriptor(type));
890}
891
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000892static void ParseStringAfterChar(const std::string& s,
893 char c,
894 std::string* parsed_value,
895 UsageFn Usage) {
896 std::string::size_type colon = s.find(c);
897 if (colon == std::string::npos) {
898 Usage("Missing char %c in option %s\n", c, s.c_str());
899 }
900 // Add one to remove the char we were trimming until.
901 *parsed_value = s.substr(colon + 1);
902}
903
904void ParseDouble(const std::string& option,
905 char after_char,
906 double min,
907 double max,
908 double* parsed_value,
909 UsageFn Usage) {
910 std::string substring;
911 ParseStringAfterChar(option, after_char, &substring, Usage);
912 bool sane_val = true;
913 double value;
914 if ((false)) {
915 // TODO: this doesn't seem to work on the emulator. b/15114595
916 std::stringstream iss(substring);
917 iss >> value;
918 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
919 sane_val = iss.eof() && (value >= min) && (value <= max);
920 } else {
921 char* end = nullptr;
922 value = strtod(substring.c_str(), &end);
923 sane_val = *end == '\0' && value >= min && value <= max;
924 }
925 if (!sane_val) {
926 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
927 }
928 *parsed_value = value;
929}
930
Calin Juravle4d77b6a2015-12-01 18:38:09 +0000931int64_t GetFileSizeBytes(const std::string& filename) {
932 struct stat stat_buf;
933 int rc = stat(filename.c_str(), &stat_buf);
934 return rc == 0 ? stat_buf.st_size : -1;
935}
936
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800937void SleepForever() {
938 while (true) {
939 usleep(1000000);
940 }
941}
942
Elliott Hughes42ee1422011-09-06 12:33:32 -0700943} // namespace art