blob: 79ddcb9bffba7e3c29c0c98d7d555079b8c3ae98 [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
David Sehr013fd802018-01-11 22:55:24 -080029#include "android-base/file.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080030#include "android-base/stringprintf.h"
Andreas Gampe9186ced2016-12-12 14:28:21 -080031#include "android-base/strings.h"
32
David Sehr9e734c72018-01-04 17:56:19 -080033#include "dex/dex_file-inl.h"
buzbeec143c552011-08-20 17:38:58 -070034#include "os.h"
Ian Rogersa6724902013-09-23 09:23:37 -070035#include "utf-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070036
Elliott Hughes4ae722a2012-03-13 11:08:51 -070037#if defined(__APPLE__)
David Sehrfa442002016-08-22 18:42:08 -070038#include <crt_externs.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include <sys/syscall.h>
40#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
Elliott Hughes4ae722a2012-03-13 11:08:51 -070041#endif
42
Elliott Hughes058a6de2012-05-24 19:13:02 -070043#if defined(__linux__)
Elliott Hughese1aee692012-01-17 16:40:10 -080044#include <linux/unistd.h>
Elliott Hughese1aee692012-01-17 16:40:10 -080045#endif
46
Elliott Hughes11e45072011-08-16 17:40:46 -070047namespace art {
48
David Sehr013fd802018-01-11 22:55:24 -080049using android::base::ReadFileToString;
Andreas Gampe46ee31b2016-12-14 10:11:49 -080050using android::base::StringAppendF;
51using android::base::StringPrintf;
52
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080053pid_t GetTid() {
Brian Carlstromf3a26412012-08-24 11:06:02 -070054#if defined(__APPLE__)
55 uint64_t owner;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070056 CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__); // Requires Mac OS 10.6
Brian Carlstromf3a26412012-08-24 11:06:02 -070057 return owner;
Elliott Hughes323aa862014-08-20 15:00:04 -070058#elif defined(__BIONIC__)
59 return gettid();
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080060#else
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080061 return syscall(__NR_gettid);
62#endif
63}
64
Elliott Hughes289be852012-06-12 13:57:20 -070065std::string GetThreadName(pid_t tid) {
66 std::string result;
David Sehr013fd802018-01-11 22:55:24 -080067 // TODO: make this less Linux-specific.
Elliott Hughes289be852012-06-12 13:57:20 -070068 if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070069 result.resize(result.size() - 1); // Lose the trailing '\n'.
Elliott Hughes289be852012-06-12 13:57:20 -070070 } else {
71 result = "<unknown>";
72 }
73 return result;
74}
75
Vladimir Markob8a55f82017-09-21 16:21:43 +010076void AppendPrettyDescriptor(const char* descriptor, std::string* result) {
Elliott Hughes11e45072011-08-16 17:40:46 -070077 // Count the number of '['s to get the dimensionality.
Ian Rogers1ff3c982014-08-12 02:30:58 -070078 const char* c = descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070079 size_t dim = 0;
80 while (*c == '[') {
81 dim++;
82 c++;
83 }
84
85 // Reference or primitive?
86 if (*c == 'L') {
87 // "[[La/b/C;" -> "a.b.C[][]".
Brian Carlstrom7934ac22013-07-26 10:54:15 -070088 c++; // Skip the 'L'.
Elliott Hughes11e45072011-08-16 17:40:46 -070089 } else {
90 // "[[B" -> "byte[][]".
91 // To make life easier, we make primitives look like unqualified
92 // reference types.
93 switch (*c) {
Vladimir Markob8a55f82017-09-21 16:21:43 +010094 case 'B': c = "byte;"; break;
95 case 'C': c = "char;"; break;
96 case 'D': c = "double;"; break;
97 case 'F': c = "float;"; break;
98 case 'I': c = "int;"; break;
99 case 'J': c = "long;"; break;
100 case 'S': c = "short;"; break;
101 case 'Z': c = "boolean;"; break;
102 case 'V': c = "void;"; break; // Used when decoding return types.
103 default: result->append(descriptor); return;
Elliott Hughes11e45072011-08-16 17:40:46 -0700104 }
105 }
106
107 // At this point, 'c' is a string of the form "fully/qualified/Type;"
108 // or "primitive;". Rewrite the type with '.' instead of '/':
Elliott Hughes11e45072011-08-16 17:40:46 -0700109 const char* p = c;
110 while (*p != ';') {
111 char ch = *p++;
112 if (ch == '/') {
113 ch = '.';
114 }
Vladimir Markob8a55f82017-09-21 16:21:43 +0100115 result->push_back(ch);
Elliott Hughes11e45072011-08-16 17:40:46 -0700116 }
117 // ...and replace the semicolon with 'dim' "[]" pairs:
Ian Rogers1ff3c982014-08-12 02:30:58 -0700118 for (size_t i = 0; i < dim; ++i) {
Vladimir Markob8a55f82017-09-21 16:21:43 +0100119 result->append("[]");
Elliott Hughes11e45072011-08-16 17:40:46 -0700120 }
Elliott Hughes11e45072011-08-16 17:40:46 -0700121}
122
Vladimir Markob8a55f82017-09-21 16:21:43 +0100123std::string PrettyDescriptor(const char* descriptor) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700124 std::string result;
Vladimir Markob8a55f82017-09-21 16:21:43 +0100125 AppendPrettyDescriptor(descriptor, &result);
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700126 return result;
127}
128
Andreas Gampec0d82292014-09-23 10:38:30 -0700129std::string PrettyJavaAccessFlags(uint32_t access_flags) {
130 std::string result;
131 if ((access_flags & kAccPublic) != 0) {
132 result += "public ";
133 }
134 if ((access_flags & kAccProtected) != 0) {
135 result += "protected ";
136 }
137 if ((access_flags & kAccPrivate) != 0) {
138 result += "private ";
139 }
140 if ((access_flags & kAccFinal) != 0) {
141 result += "final ";
142 }
143 if ((access_flags & kAccStatic) != 0) {
144 result += "static ";
145 }
David Brazdilca3c8c32016-09-06 14:04:48 +0100146 if ((access_flags & kAccAbstract) != 0) {
147 result += "abstract ";
148 }
149 if ((access_flags & kAccInterface) != 0) {
150 result += "interface ";
151 }
Andreas Gampec0d82292014-09-23 10:38:30 -0700152 if ((access_flags & kAccTransient) != 0) {
153 result += "transient ";
154 }
155 if ((access_flags & kAccVolatile) != 0) {
156 result += "volatile ";
157 }
158 if ((access_flags & kAccSynchronized) != 0) {
159 result += "synchronized ";
160 }
161 return result;
162}
163
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800164std::string PrettySize(int64_t byte_count) {
Elliott Hughesc967f782012-04-16 10:23:15 -0700165 // The byte thresholds at which we display amounts. A byte count is displayed
166 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
Ian Rogersef7d42f2014-01-06 12:55:46 -0800167 static const int64_t kUnitThresholds[] = {
Elliott Hughesc967f782012-04-16 10:23:15 -0700168 0, // B up to...
169 3*1024, // KB up to...
170 2*1024*1024, // MB up to...
171 1024*1024*1024 // GB from here.
172 };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800173 static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB };
Elliott Hughesc967f782012-04-16 10:23:15 -0700174 static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800175 const char* negative_str = "";
176 if (byte_count < 0) {
177 negative_str = "-";
178 byte_count = -byte_count;
179 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700180 int i = arraysize(kUnitThresholds);
181 while (--i > 0) {
182 if (byte_count >= kUnitThresholds[i]) {
183 break;
184 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800185 }
Brian Carlstrom474cc792014-03-07 14:18:15 -0800186 return StringPrintf("%s%" PRId64 "%s",
187 negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800188}
189
Andreas Gampe9186ced2016-12-12 14:28:21 -0800190static inline constexpr bool NeedsEscaping(uint16_t ch) {
191 return (ch < ' ' || ch > '~');
192}
193
Ian Rogers576ca0c2014-06-06 15:58:22 -0700194std::string PrintableChar(uint16_t ch) {
195 std::string result;
196 result += '\'';
197 if (NeedsEscaping(ch)) {
198 StringAppendF(&result, "\\u%04x", ch);
199 } else {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700200 result += static_cast<std::string::value_type>(ch);
Ian Rogers576ca0c2014-06-06 15:58:22 -0700201 }
202 result += '\'';
203 return result;
204}
205
Ian Rogers68b56852014-08-29 20:19:11 -0700206std::string PrintableString(const char* utf) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700207 std::string result;
208 result += '"';
Ian Rogers68b56852014-08-29 20:19:11 -0700209 const char* p = utf;
Elliott Hughes82914b62012-04-09 15:56:29 -0700210 size_t char_count = CountModifiedUtf8Chars(p);
211 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000212 uint32_t ch = GetUtf16FromUtf8(&p);
Elliott Hughes82914b62012-04-09 15:56:29 -0700213 if (ch == '\\') {
214 result += "\\\\";
215 } else if (ch == '\n') {
216 result += "\\n";
217 } else if (ch == '\r') {
218 result += "\\r";
219 } else if (ch == '\t') {
220 result += "\\t";
Elliott Hughes82914b62012-04-09 15:56:29 -0700221 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000222 const uint16_t leading = GetLeadingUtf16Char(ch);
223
224 if (NeedsEscaping(leading)) {
225 StringAppendF(&result, "\\u%04x", leading);
226 } else {
Andreas Gampef45d61c2017-06-07 10:29:33 -0700227 result += static_cast<std::string::value_type>(leading);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000228 }
229
230 const uint32_t trailing = GetTrailingUtf16Char(ch);
231 if (trailing != 0) {
232 // All high surrogates will need escaping.
233 StringAppendF(&result, "\\u%04x", trailing);
234 }
Elliott Hughes82914b62012-04-09 15:56:29 -0700235 }
236 }
237 result += '"';
238 return result;
239}
240
Alex Light888a59e2017-01-25 11:41:41 -0800241std::string GetJniShortName(const std::string& class_descriptor, const std::string& method) {
242 // Remove the leading 'L' and trailing ';'...
243 std::string class_name(class_descriptor);
244 CHECK_EQ(class_name[0], 'L') << class_name;
245 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
246 class_name.erase(0, 1);
247 class_name.erase(class_name.size() - 1, 1);
248
249 std::string short_name;
250 short_name += "Java_";
251 short_name += MangleForJni(class_name);
252 short_name += "_";
253 short_name += MangleForJni(method);
254 return short_name;
255}
256
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800257// 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 -0700258std::string MangleForJni(const std::string& s) {
259 std::string result;
260 size_t char_count = CountModifiedUtf8Chars(s.c_str());
261 const char* cp = &s[0];
262 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000263 uint32_t ch = GetUtf16FromUtf8(&cp);
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800264 if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
265 result.push_back(ch);
266 } else if (ch == '.' || ch == '/') {
267 result += "_";
268 } else if (ch == '_') {
269 result += "_1";
270 } else if (ch == ';') {
271 result += "_2";
272 } else if (ch == '[') {
273 result += "_3";
Elliott Hughes79082e32011-08-25 12:07:32 -0700274 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000275 const uint16_t leading = GetLeadingUtf16Char(ch);
276 const uint32_t trailing = GetTrailingUtf16Char(ch);
277
278 StringAppendF(&result, "_0%04x", leading);
279 if (trailing != 0) {
280 StringAppendF(&result, "_0%04x", trailing);
281 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700282 }
283 }
284 return result;
285}
286
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700287std::string DotToDescriptor(const char* class_name) {
288 std::string descriptor(class_name);
289 std::replace(descriptor.begin(), descriptor.end(), '.', '/');
290 if (descriptor.length() > 0 && descriptor[0] != '[') {
291 descriptor = "L" + descriptor + ";";
292 }
293 return descriptor;
294}
295
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800296std::string DescriptorToDot(const char* descriptor) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800297 size_t length = strlen(descriptor);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700298 if (length > 1) {
299 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
300 // Descriptors have the leading 'L' and trailing ';' stripped.
301 std::string result(descriptor + 1, length - 2);
302 std::replace(result.begin(), result.end(), '/', '.');
303 return result;
304 } else {
305 // For arrays the 'L' and ';' remain intact.
306 std::string result(descriptor);
307 std::replace(result.begin(), result.end(), '/', '.');
308 return result;
309 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800310 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700311 // Do nothing for non-class/array descriptors.
Elliott Hughes2435a572012-02-17 16:07:41 -0800312 return descriptor;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800313}
314
315std::string DescriptorToName(const char* descriptor) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800316 size_t length = strlen(descriptor);
Elliott Hughes2435a572012-02-17 16:07:41 -0800317 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
318 std::string result(descriptor + 1, length - 2);
319 return result;
320 }
321 return descriptor;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700322}
323
jeffhao10037c82012-01-23 15:06:23 -0800324// Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700325uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700326 0x00000000, // 00..1f low control characters; nothing valid
327 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
328 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
329 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z'
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700330};
331
jeffhao10037c82012-01-23 15:06:23 -0800332// Helper for IsValidPartOfMemberNameUtf8(); do not call directly.
333bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700334 /*
335 * It's a multibyte encoded character. Decode it and analyze. We
336 * accept anything that isn't (a) an improperly encoded low value,
337 * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
338 * control character, or (e) a high space, layout, or special
339 * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
340 * U+fff0..U+ffff). This is all specified in the dex format
341 * document.
342 */
343
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000344 const uint32_t pair = GetUtf16FromUtf8(pUtf8Ptr);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000345 const uint16_t leading = GetLeadingUtf16Char(pair);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000346
Narayan Kamath8508e372015-05-06 14:55:43 +0100347 // We have a surrogate pair resulting from a valid 4 byte UTF sequence.
348 // No further checks are necessary because 4 byte sequences span code
349 // points [U+10000, U+1FFFFF], which are valid codepoints in a dex
350 // identifier. Furthermore, GetUtf16FromUtf8 guarantees that each of
351 // the surrogate halves are valid and well formed in this instance.
352 if (GetTrailingUtf16Char(pair) != 0) {
353 return true;
354 }
355
356
357 // We've encountered a one, two or three byte UTF-8 sequence. The
358 // three byte UTF-8 sequence could be one half of a surrogate pair.
359 switch (leading >> 8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000360 case 0x00:
361 // It's only valid if it's above the ISO-8859-1 high space (0xa0).
362 return (leading > 0x00a0);
363 case 0xd8:
364 case 0xd9:
365 case 0xda:
366 case 0xdb:
Narayan Kamath8508e372015-05-06 14:55:43 +0100367 {
368 // We found a three byte sequence encoding one half of a surrogate.
369 // Look for the other half.
370 const uint32_t pair2 = GetUtf16FromUtf8(pUtf8Ptr);
371 const uint16_t trailing = GetLeadingUtf16Char(pair2);
372
373 return (GetTrailingUtf16Char(pair2) == 0) && (0xdc00 <= trailing && trailing <= 0xdfff);
374 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000375 case 0xdc:
376 case 0xdd:
377 case 0xde:
378 case 0xdf:
379 // It's a trailing surrogate, which is not valid at this point.
380 return false;
381 case 0x20:
382 case 0xff:
383 // It's in the range that has spaces, controls, and specials.
384 switch (leading & 0xfff8) {
Narayan Kamath8508e372015-05-06 14:55:43 +0100385 case 0x2000:
386 case 0x2008:
387 case 0x2028:
388 case 0xfff0:
389 case 0xfff8:
390 return false;
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000391 }
Narayan Kamath8508e372015-05-06 14:55:43 +0100392 return true;
393 default:
394 return true;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700395 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000396
Narayan Kamath8508e372015-05-06 14:55:43 +0100397 UNREACHABLE();
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700398}
399
400/* Return whether the pointed-at modified-UTF-8 encoded character is
401 * valid as part of a member name, updating the pointer to point past
402 * the consumed character. This will consume two encoded UTF-16 code
403 * points if the character is encoded as a surrogate pair. Also, if
404 * this function returns false, then the given pointer may only have
405 * been partially advanced.
406 */
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700407static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700408 uint8_t c = (uint8_t) **pUtf8Ptr;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700409 if (LIKELY(c <= 0x7f)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700410 // It's low-ascii, so check the table.
411 uint32_t wordIdx = c >> 5;
412 uint32_t bitIdx = c & 0x1f;
413 (*pUtf8Ptr)++;
414 return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
415 }
416
417 // It's a multibyte encoded character. Call a non-inline function
418 // for the heavy lifting.
jeffhao10037c82012-01-23 15:06:23 -0800419 return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr);
420}
421
422bool IsValidMemberName(const char* s) {
423 bool angle_name = false;
424
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700425 switch (*s) {
jeffhao10037c82012-01-23 15:06:23 -0800426 case '\0':
427 // The empty string is not a valid name.
428 return false;
429 case '<':
430 angle_name = true;
431 s++;
432 break;
433 }
434
435 while (true) {
436 switch (*s) {
437 case '\0':
438 return !angle_name;
439 case '>':
440 return angle_name && s[1] == '\0';
441 }
442
443 if (!IsValidPartOfMemberNameUtf8(&s)) {
444 return false;
445 }
446 }
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700447}
448
Elliott Hughes906e6852011-10-28 14:52:10 -0700449enum ClassNameType { kName, kDescriptor };
Ian Rogers7b078e82014-09-10 14:44:24 -0700450template<ClassNameType kType, char kSeparator>
451static bool IsValidClassName(const char* s) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700452 int arrayCount = 0;
453 while (*s == '[') {
454 arrayCount++;
455 s++;
456 }
457
458 if (arrayCount > 255) {
459 // Arrays may have no more than 255 dimensions.
460 return false;
461 }
462
Ian Rogers7b078e82014-09-10 14:44:24 -0700463 ClassNameType type = kType;
464 if (type != kDescriptor && arrayCount != 0) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700465 /*
466 * If we're looking at an array of some sort, then it doesn't
467 * matter if what is being asked for is a class name; the
468 * format looks the same as a type descriptor in that case, so
469 * treat it as such.
470 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700471 type = kDescriptor;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700472 }
473
Elliott Hughes906e6852011-10-28 14:52:10 -0700474 if (type == kDescriptor) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700475 /*
476 * We are looking for a descriptor. Either validate it as a
477 * single-character primitive type, or continue on to check the
478 * embedded class name (bracketed by "L" and ";").
479 */
480 switch (*(s++)) {
481 case 'B':
482 case 'C':
483 case 'D':
484 case 'F':
485 case 'I':
486 case 'J':
487 case 'S':
488 case 'Z':
489 // These are all single-character descriptors for primitive types.
490 return (*s == '\0');
491 case 'V':
492 // Non-array void is valid, but you can't have an array of void.
493 return (arrayCount == 0) && (*s == '\0');
494 case 'L':
495 // Class name: Break out and continue below.
496 break;
497 default:
498 // Oddball descriptor character.
499 return false;
500 }
501 }
502
503 /*
504 * We just consumed the 'L' that introduces a class name as part
505 * of a type descriptor, or we are looking for an unadorned class
506 * name.
507 */
508
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700509 bool sepOrFirst = true; // first character or just encountered a separator.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700510 for (;;) {
511 uint8_t c = (uint8_t) *s;
512 switch (c) {
513 case '\0':
514 /*
515 * Premature end for a type descriptor, but valid for
516 * a class name as long as we haven't encountered an
517 * empty component (including the degenerate case of
518 * the empty string "").
519 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700520 return (type == kName) && !sepOrFirst;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700521 case ';':
522 /*
523 * Invalid character for a class name, but the
524 * legitimate end of a type descriptor. In the latter
525 * case, make sure that this is the end of the string
526 * and that it doesn't end with an empty component
527 * (including the degenerate case of "L;").
528 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700529 return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0');
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700530 case '/':
531 case '.':
Ian Rogers7b078e82014-09-10 14:44:24 -0700532 if (c != kSeparator) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700533 // The wrong separator character.
534 return false;
535 }
536 if (sepOrFirst) {
537 // Separator at start or two separators in a row.
538 return false;
539 }
540 sepOrFirst = true;
541 s++;
542 break;
543 default:
jeffhao10037c82012-01-23 15:06:23 -0800544 if (!IsValidPartOfMemberNameUtf8(&s)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700545 return false;
546 }
547 sepOrFirst = false;
548 break;
549 }
550 }
551}
552
Elliott Hughes906e6852011-10-28 14:52:10 -0700553bool IsValidBinaryClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700554 return IsValidClassName<kName, '.'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700555}
556
557bool IsValidJniClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700558 return IsValidClassName<kName, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700559}
560
561bool IsValidDescriptor(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700562 return IsValidClassName<kDescriptor, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700563}
564
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700565void Split(const std::string& s, char separator, std::vector<std::string>* result) {
Elliott Hughes34023802011-08-30 12:06:17 -0700566 const char* p = s.data();
567 const char* end = p + s.size();
568 while (p != end) {
Elliott Hughes48436bb2012-02-07 15:23:28 -0800569 if (*p == separator) {
Elliott Hughes34023802011-08-30 12:06:17 -0700570 ++p;
571 } else {
572 const char* start = p;
Elliott Hughes48436bb2012-02-07 15:23:28 -0800573 while (++p != end && *p != separator) {
574 // Skip to the next occurrence of the separator.
Elliott Hughes34023802011-08-30 12:06:17 -0700575 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700576 result->push_back(std::string(start, p - start));
Elliott Hughes34023802011-08-30 12:06:17 -0700577 }
578 }
579}
580
Elliott Hughes22869a92012-03-27 14:08:24 -0700581void SetThreadName(const char* thread_name) {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700582 int hasAt = 0;
583 int hasDot = 0;
Elliott Hughes22869a92012-03-27 14:08:24 -0700584 const char* s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700585 while (*s) {
586 if (*s == '.') {
587 hasDot = 1;
588 } else if (*s == '@') {
589 hasAt = 1;
590 }
591 s++;
592 }
Elliott Hughes22869a92012-03-27 14:08:24 -0700593 int len = s - thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700594 if (len < 15 || hasAt || !hasDot) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700595 s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700596 } else {
Elliott Hughes22869a92012-03-27 14:08:24 -0700597 s = thread_name + len - 15;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700598 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800599#if defined(__linux__)
Elliott Hughes7c6a61e2012-03-12 18:01:41 -0700600 // pthread_setname_np fails rather than truncating long strings.
Elliott Hughes0a18df82015-01-09 15:16:16 -0800601 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700602 strncpy(buf, s, sizeof(buf)-1);
603 buf[sizeof(buf)-1] = '\0';
604 errno = pthread_setname_np(pthread_self(), buf);
605 if (errno != 0) {
606 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
607 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800608#else // __APPLE__
Elliott Hughes22869a92012-03-27 14:08:24 -0700609 pthread_setname_np(thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700610#endif
611}
612
Brian Carlstrom29212012013-09-12 22:18:30 -0700613void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
614 *utime = *stime = *task_cpu = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700615 std::string stats;
David Sehr013fd802018-01-11 22:55:24 -0800616 // TODO: make this less Linux-specific.
Elliott Hughes8a31b502012-04-30 19:36:11 -0700617 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700618 return;
619 }
620 // Skip the command, which may contain spaces.
621 stats = stats.substr(stats.find(')') + 2);
622 // Extract the three fields we care about.
623 std::vector<std::string> fields;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700624 Split(stats, ' ', &fields);
Brian Carlstrom29212012013-09-12 22:18:30 -0700625 *state = fields[0][0];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700626 *utime = strtoull(fields[11].c_str(), nullptr, 10);
627 *stime = strtoull(fields[12].c_str(), nullptr, 10);
628 *task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700629}
630
Mathieu Chartier76433272014-09-26 14:32:37 -0700631std::string PrettyDescriptor(Primitive::Type type) {
632 return PrettyDescriptor(Primitive::Descriptor(type));
633}
634
Nicolas Geoffrayabbb0f72015-10-29 18:55:58 +0000635static void ParseStringAfterChar(const std::string& s,
636 char c,
637 std::string* parsed_value,
638 UsageFn Usage) {
639 std::string::size_type colon = s.find(c);
640 if (colon == std::string::npos) {
641 Usage("Missing char %c in option %s\n", c, s.c_str());
642 }
643 // Add one to remove the char we were trimming until.
644 *parsed_value = s.substr(colon + 1);
645}
646
647void ParseDouble(const std::string& option,
648 char after_char,
649 double min,
650 double max,
651 double* parsed_value,
652 UsageFn Usage) {
653 std::string substring;
654 ParseStringAfterChar(option, after_char, &substring, Usage);
655 bool sane_val = true;
656 double value;
657 if ((false)) {
658 // TODO: this doesn't seem to work on the emulator. b/15114595
659 std::stringstream iss(substring);
660 iss >> value;
661 // Ensure that we have a value, there was no cruft after it and it satisfies a sensible range.
662 sane_val = iss.eof() && (value >= min) && (value <= max);
663 } else {
664 char* end = nullptr;
665 value = strtod(substring.c_str(), &end);
666 sane_val = *end == '\0' && value >= min && value <= max;
667 }
668 if (!sane_val) {
669 Usage("Invalid double value %s for option %s\n", substring.c_str(), option.c_str());
670 }
671 *parsed_value = value;
672}
673
Mathieu Chartier4d87df62016-01-07 15:14:19 -0800674void SleepForever() {
675 while (true) {
676 usleep(1000000);
677 }
678}
679
Elliott Hughes42ee1422011-09-06 12:33:32 -0700680} // namespace art