blob: db3f2fecb8b0c0a045339cb3ebd3779f9dd4149a [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>
Ian Rogers700a4022014-05-19 16:49:03 -070026#include <memory>
Elliott Hughes42ee1422011-09-06 12:33:32 -070027
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "art_method-inl.h"
Brian Carlstrom6449c622014-02-10 23:48:36 -080030#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080031#include "base/unix_file/fd_file.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "dex_file-inl.h"
Andreas Gampe5073fed2015-08-10 11:40:25 -070033#include "dex_instruction.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070034#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/object-inl.h"
37#include "mirror/object_array-inl.h"
38#include "mirror/string.h"
buzbeec143c552011-08-20 17:38:58 -070039#include "os.h"
Kenny Root067d20f2014-03-05 14:57:21 -080040#include "scoped_thread_state_change.h"
Ian Rogersa6724902013-09-23 09:23:37 -070041#include "utf-inl.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070042
Elliott Hughes4ae722a2012-03-13 11:08:51 -070043#if defined(__APPLE__)
Brian Carlstrom7934ac22013-07-26 10:54:15 -070044#include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED
Elliott Hughesf1498432012-03-28 19:34:27 -070045#include <sys/syscall.h>
Elliott Hughes4ae722a2012-03-13 11:08:51 -070046#endif
47
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -070048#include <backtrace/Backtrace.h> // For DumpNativeStack.
Elliott Hughes46e251b2012-05-22 15:10:45 -070049
Elliott Hughes058a6de2012-05-24 19:13:02 -070050#if defined(__linux__)
Elliott Hughese1aee692012-01-17 16:40:10 -080051#include <linux/unistd.h>
Elliott Hughese1aee692012-01-17 16:40:10 -080052#endif
53
Elliott Hughes11e45072011-08-16 17:40:46 -070054namespace art {
55
Andreas Gampe8e1cb912015-01-08 20:11:09 -080056#if defined(__linux__)
57static constexpr bool kUseAddr2line = !kIsTargetBuild;
58#endif
59
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080060pid_t GetTid() {
Brian Carlstromf3a26412012-08-24 11:06:02 -070061#if defined(__APPLE__)
62 uint64_t owner;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070063 CHECK_PTHREAD_CALL(pthread_threadid_np, (nullptr, &owner), __FUNCTION__); // Requires Mac OS 10.6
Brian Carlstromf3a26412012-08-24 11:06:02 -070064 return owner;
Elliott Hughes323aa862014-08-20 15:00:04 -070065#elif defined(__BIONIC__)
66 return gettid();
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080067#else
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080068 return syscall(__NR_gettid);
69#endif
70}
71
Elliott Hughes289be852012-06-12 13:57:20 -070072std::string GetThreadName(pid_t tid) {
73 std::string result;
74 if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070075 result.resize(result.size() - 1); // Lose the trailing '\n'.
Elliott Hughes289be852012-06-12 13:57:20 -070076 } else {
77 result = "<unknown>";
78 }
79 return result;
80}
81
Elliott Hughes6d3fc562014-08-27 11:47:01 -070082void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size, size_t* guard_size) {
Elliott Hughese1884192012-04-23 12:38:15 -070083#if defined(__APPLE__)
Brian Carlstrom29212012013-09-12 22:18:30 -070084 *stack_size = pthread_get_stacksize_np(thread);
Ian Rogers120f1c72012-09-28 17:17:10 -070085 void* stack_addr = pthread_get_stackaddr_np(thread);
Elliott Hughese1884192012-04-23 12:38:15 -070086
87 // Check whether stack_addr is the base or end of the stack.
88 // (On Mac OS 10.7, it's the end.)
89 int stack_variable;
90 if (stack_addr > &stack_variable) {
Ian Rogers13735952014-10-08 12:43:28 -070091 *stack_base = reinterpret_cast<uint8_t*>(stack_addr) - *stack_size;
Elliott Hughese1884192012-04-23 12:38:15 -070092 } else {
Brian Carlstrom29212012013-09-12 22:18:30 -070093 *stack_base = stack_addr;
Elliott Hughese1884192012-04-23 12:38:15 -070094 }
Elliott Hughes6d3fc562014-08-27 11:47:01 -070095
96 // This is wrong, but there doesn't seem to be a way to get the actual value on the Mac.
97 pthread_attr_t attributes;
98 CHECK_PTHREAD_CALL(pthread_attr_init, (&attributes), __FUNCTION__);
99 CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__);
100 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
Elliott Hughese1884192012-04-23 12:38:15 -0700101#else
102 pthread_attr_t attributes;
Ian Rogers120f1c72012-09-28 17:17:10 -0700103 CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__);
Brian Carlstrom29212012013-09-12 22:18:30 -0700104 CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__);
Elliott Hughes6d3fc562014-08-27 11:47:01 -0700105 CHECK_PTHREAD_CALL(pthread_attr_getguardsize, (&attributes, guard_size), __FUNCTION__);
Elliott Hughese1884192012-04-23 12:38:15 -0700106 CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__);
Elliott Hughes839cc302014-08-28 10:24:44 -0700107
108#if defined(__GLIBC__)
109 // If we're the main thread, check whether we were run with an unlimited stack. In that case,
110 // glibc will have reported a 2GB stack for our 32-bit process, and our stack overflow detection
111 // will be broken because we'll die long before we get close to 2GB.
112 bool is_main_thread = (::art::GetTid() == getpid());
113 if (is_main_thread) {
114 rlimit stack_limit;
115 if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
116 PLOG(FATAL) << "getrlimit(RLIMIT_STACK) failed";
117 }
118 if (stack_limit.rlim_cur == RLIM_INFINITY) {
119 size_t old_stack_size = *stack_size;
120
121 // Use the kernel default limit as our size, and adjust the base to match.
122 *stack_size = 8 * MB;
123 *stack_base = reinterpret_cast<uint8_t*>(*stack_base) + (old_stack_size - *stack_size);
124
125 VLOG(threads) << "Limiting unlimited stack (reported as " << PrettySize(old_stack_size) << ")"
126 << " to " << PrettySize(*stack_size)
127 << " with base " << *stack_base;
128 }
129 }
130#endif
131
Elliott Hughese1884192012-04-23 12:38:15 -0700132#endif
133}
134
Elliott Hughesd92bec42011-09-02 17:04:36 -0700135bool ReadFileToString(const std::string& file_name, std::string* result) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800136 File file;
137 if (!file.Open(file_name, O_RDONLY)) {
Elliott Hughesd92bec42011-09-02 17:04:36 -0700138 return false;
139 }
buzbeec143c552011-08-20 17:38:58 -0700140
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700141 std::vector<char> buf(8 * KB);
buzbeec143c552011-08-20 17:38:58 -0700142 while (true) {
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800143 int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[0], buf.size()));
Elliott Hughesd92bec42011-09-02 17:04:36 -0700144 if (n == -1) {
145 return false;
buzbeec143c552011-08-20 17:38:58 -0700146 }
Elliott Hughesd92bec42011-09-02 17:04:36 -0700147 if (n == 0) {
148 return true;
149 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700150 result->append(&buf[0], n);
buzbeec143c552011-08-20 17:38:58 -0700151 }
buzbeec143c552011-08-20 17:38:58 -0700152}
153
Andreas Gampea6dfdae2015-02-24 15:50:19 -0800154bool PrintFileToLog(const std::string& file_name, LogSeverity level) {
155 File file;
156 if (!file.Open(file_name, O_RDONLY)) {
157 return false;
158 }
159
160 constexpr size_t kBufSize = 256; // Small buffer. Avoid stack overflow and stack size warnings.
161 char buf[kBufSize + 1]; // +1 for terminator.
162 size_t filled_to = 0;
163 while (true) {
164 DCHECK_LT(filled_to, kBufSize);
165 int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to));
166 if (n <= 0) {
167 // Print the rest of the buffer, if it exists.
168 if (filled_to > 0) {
169 buf[filled_to] = 0;
170 LOG(level) << buf;
171 }
172 return n == 0;
173 }
174 // Scan for '\n'.
175 size_t i = filled_to;
176 bool found_newline = false;
177 for (; i < filled_to + n; ++i) {
178 if (buf[i] == '\n') {
179 // Found a line break, that's something to print now.
180 buf[i] = 0;
181 LOG(level) << buf;
182 // Copy the rest to the front.
183 if (i + 1 < filled_to + n) {
184 memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1);
185 filled_to = filled_to + n - i - 1;
186 } else {
187 filled_to = 0;
188 }
189 found_newline = true;
190 break;
191 }
192 }
193 if (found_newline) {
194 continue;
195 } else {
196 filled_to += n;
197 // Check if we must flush now.
198 if (filled_to == kBufSize) {
199 buf[kBufSize] = 0;
200 LOG(level) << buf;
201 filled_to = 0;
202 }
203 }
204 }
205}
206
Ian Rogersef7d42f2014-01-06 12:55:46 -0800207std::string PrettyDescriptor(mirror::String* java_descriptor) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700208 if (java_descriptor == nullptr) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700209 return "null";
210 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 return PrettyDescriptor(java_descriptor->ToModifiedUtf8().c_str());
Elliott Hughes6c8867d2011-10-03 16:34:05 -0700212}
Elliott Hughes5174fe62011-08-23 15:12:35 -0700213
Ian Rogersef7d42f2014-01-06 12:55:46 -0800214std::string PrettyDescriptor(mirror::Class* klass) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700215 if (klass == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 return "null";
217 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700218 std::string temp;
219 return PrettyDescriptor(klass->GetDescriptor(&temp));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800220}
221
Ian Rogers1ff3c982014-08-12 02:30:58 -0700222std::string PrettyDescriptor(const char* descriptor) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700223 // Count the number of '['s to get the dimensionality.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700224 const char* c = descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -0700225 size_t dim = 0;
226 while (*c == '[') {
227 dim++;
228 c++;
229 }
230
231 // Reference or primitive?
232 if (*c == 'L') {
233 // "[[La/b/C;" -> "a.b.C[][]".
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700234 c++; // Skip the 'L'.
Elliott Hughes11e45072011-08-16 17:40:46 -0700235 } else {
236 // "[[B" -> "byte[][]".
237 // To make life easier, we make primitives look like unqualified
238 // reference types.
239 switch (*c) {
240 case 'B': c = "byte;"; break;
241 case 'C': c = "char;"; break;
242 case 'D': c = "double;"; break;
243 case 'F': c = "float;"; break;
244 case 'I': c = "int;"; break;
245 case 'J': c = "long;"; break;
246 case 'S': c = "short;"; break;
247 case 'Z': c = "boolean;"; break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700248 case 'V': c = "void;"; break; // Used when decoding return types.
Elliott Hughes5174fe62011-08-23 15:12:35 -0700249 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -0700250 }
251 }
252
253 // At this point, 'c' is a string of the form "fully/qualified/Type;"
254 // or "primitive;". Rewrite the type with '.' instead of '/':
255 std::string result;
256 const char* p = c;
257 while (*p != ';') {
258 char ch = *p++;
259 if (ch == '/') {
260 ch = '.';
261 }
262 result.push_back(ch);
263 }
264 // ...and replace the semicolon with 'dim' "[]" pairs:
Ian Rogers1ff3c982014-08-12 02:30:58 -0700265 for (size_t i = 0; i < dim; ++i) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700266 result += "[]";
267 }
268 return result;
269}
270
Mathieu Chartierc7853442015-03-27 14:35:38 -0700271std::string PrettyField(ArtField* f, bool with_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700272 if (f == nullptr) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700273 return "null";
274 }
Elliott Hughes54e7df12011-09-16 11:47:04 -0700275 std::string result;
276 if (with_type) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700277 result += PrettyDescriptor(f->GetTypeDescriptor());
Elliott Hughes54e7df12011-09-16 11:47:04 -0700278 result += ' ';
279 }
Ian Rogers08f1f502014-12-02 15:04:37 -0800280 std::string temp;
281 result += PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor(&temp));
Elliott Hughesa2501992011-08-26 19:39:54 -0700282 result += '.';
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700283 result += f->GetName();
Elliott Hughesa2501992011-08-26 19:39:54 -0700284 return result;
285}
286
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700287std::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type) {
Elliott Hughes60641a72013-02-27 14:36:16 -0800288 if (field_idx >= dex_file.NumFieldIds()) {
289 return StringPrintf("<<invalid-field-idx-%d>>", field_idx);
290 }
Brian Carlstrom6f29d0e2012-05-11 15:50:29 -0700291 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx);
292 std::string result;
293 if (with_type) {
294 result += dex_file.GetFieldTypeDescriptor(field_id);
295 result += ' ';
296 }
297 result += PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(field_id));
298 result += '.';
299 result += dex_file.GetFieldName(field_id);
300 return result;
301}
302
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700303std::string PrettyType(uint32_t type_idx, const DexFile& dex_file) {
Elliott Hughes60641a72013-02-27 14:36:16 -0800304 if (type_idx >= dex_file.NumTypeIds()) {
305 return StringPrintf("<<invalid-type-idx-%d>>", type_idx);
306 }
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700307 const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx);
Mathieu Chartier4c70d772012-09-10 14:08:32 -0700308 return PrettyDescriptor(dex_file.GetTypeDescriptor(type_id));
Mathieu Chartier18c24b62012-09-10 08:54:25 -0700309}
310
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700311std::string PrettyArguments(const char* signature) {
312 std::string result;
313 result += '(';
314 CHECK_EQ(*signature, '(');
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700315 ++signature; // Skip the '('.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700316 while (*signature != ')') {
317 size_t argument_length = 0;
318 while (signature[argument_length] == '[') {
319 ++argument_length;
320 }
321 if (signature[argument_length] == 'L') {
322 argument_length = (strchr(signature, ';') - signature + 1);
323 } else {
324 ++argument_length;
325 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700326 {
327 std::string argument_descriptor(signature, argument_length);
328 result += PrettyDescriptor(argument_descriptor.c_str());
329 }
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700330 if (signature[argument_length] != ')') {
331 result += ", ";
332 }
333 signature += argument_length;
334 }
335 CHECK_EQ(*signature, ')');
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700336 ++signature; // Skip the ')'.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700337 result += ')';
338 return result;
339}
340
341std::string PrettyReturnType(const char* signature) {
342 const char* return_type = strchr(signature, ')');
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700343 CHECK(return_type != nullptr);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700344 ++return_type; // Skip ')'.
Elliott Hughes9058f2b2012-03-22 18:06:48 -0700345 return PrettyDescriptor(return_type);
346}
347
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348std::string PrettyMethod(ArtMethod* m, bool with_signature) {
Ian Rogers16ce0922014-01-10 14:59:36 -0800349 if (m == nullptr) {
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700350 return "null";
351 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 if (!m->IsRuntimeMethod()) {
353 m = m->GetInterfaceMethodIfProxy(Runtime::Current()->GetClassLinker()->GetImagePointerSize());
354 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700355 std::string result(PrettyDescriptor(m->GetDeclaringClassDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700356 result += '.';
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700357 result += m->GetName();
Ian Rogers16ce0922014-01-10 14:59:36 -0800358 if (UNLIKELY(m->IsFastNative())) {
359 result += "!";
360 }
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700361 if (with_signature) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700362 const Signature signature = m->GetSignature();
Ian Rogersd91d6d62013-09-25 20:26:14 -0700363 std::string sig_as_string(signature.ToString());
364 if (signature == Signature::NoSignature()) {
365 return result + sig_as_string;
Elliott Hughesf8c11932012-03-23 19:53:59 -0700366 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700367 result = PrettyReturnType(sig_as_string.c_str()) + " " + result +
368 PrettyArguments(sig_as_string.c_str());
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700369 }
370 return result;
371}
372
Ian Rogers0571d352011-11-03 19:51:38 -0700373std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) {
Elliott Hughes60641a72013-02-27 14:36:16 -0800374 if (method_idx >= dex_file.NumMethodIds()) {
375 return StringPrintf("<<invalid-method-idx-%d>>", method_idx);
376 }
Ian Rogers0571d352011-11-03 19:51:38 -0700377 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx);
378 std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id)));
379 result += '.';
380 result += dex_file.GetMethodName(method_id);
381 if (with_signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700382 const Signature signature = dex_file.GetMethodSignature(method_id);
383 std::string sig_as_string(signature.ToString());
384 if (signature == Signature::NoSignature()) {
385 return result + sig_as_string;
Elliott Hughesf8c11932012-03-23 19:53:59 -0700386 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700387 result = PrettyReturnType(sig_as_string.c_str()) + " " + result +
388 PrettyArguments(sig_as_string.c_str());
Ian Rogers0571d352011-11-03 19:51:38 -0700389 }
390 return result;
391}
392
Ian Rogersef7d42f2014-01-06 12:55:46 -0800393std::string PrettyTypeOf(mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700394 if (obj == nullptr) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700395 return "null";
396 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700397 if (obj->GetClass() == nullptr) {
Elliott Hughes11e45072011-08-16 17:40:46 -0700398 return "(raw)";
399 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700400 std::string temp;
401 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor(&temp)));
Elliott Hughes11e45072011-08-16 17:40:46 -0700402 if (obj->IsClass()) {
Ian Rogers1ff3c982014-08-12 02:30:58 -0700403 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor(&temp)) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700404 }
405 return result;
406}
407
Ian Rogersef7d42f2014-01-06 12:55:46 -0800408std::string PrettyClass(mirror::Class* c) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700409 if (c == nullptr) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700410 return "null";
411 }
412 std::string result;
413 result += "java.lang.Class<";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800414 result += PrettyDescriptor(c);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700415 result += ">";
416 return result;
417}
418
Ian Rogersef7d42f2014-01-06 12:55:46 -0800419std::string PrettyClassAndClassLoader(mirror::Class* c) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700420 if (c == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 return "null";
422 }
423 std::string result;
424 result += "java.lang.Class<";
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800425 result += PrettyDescriptor(c);
Ian Rogersd81871c2011-10-03 13:57:23 -0700426 result += ",";
427 result += PrettyTypeOf(c->GetClassLoader());
428 // TODO: add an identifying hash value for the loader
429 result += ">";
430 return result;
431}
432
Andreas Gampec0d82292014-09-23 10:38:30 -0700433std::string PrettyJavaAccessFlags(uint32_t access_flags) {
434 std::string result;
435 if ((access_flags & kAccPublic) != 0) {
436 result += "public ";
437 }
438 if ((access_flags & kAccProtected) != 0) {
439 result += "protected ";
440 }
441 if ((access_flags & kAccPrivate) != 0) {
442 result += "private ";
443 }
444 if ((access_flags & kAccFinal) != 0) {
445 result += "final ";
446 }
447 if ((access_flags & kAccStatic) != 0) {
448 result += "static ";
449 }
450 if ((access_flags & kAccTransient) != 0) {
451 result += "transient ";
452 }
453 if ((access_flags & kAccVolatile) != 0) {
454 result += "volatile ";
455 }
456 if ((access_flags & kAccSynchronized) != 0) {
457 result += "synchronized ";
458 }
459 return result;
460}
461
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800462std::string PrettySize(int64_t byte_count) {
Elliott Hughesc967f782012-04-16 10:23:15 -0700463 // The byte thresholds at which we display amounts. A byte count is displayed
464 // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1].
Ian Rogersef7d42f2014-01-06 12:55:46 -0800465 static const int64_t kUnitThresholds[] = {
Elliott Hughesc967f782012-04-16 10:23:15 -0700466 0, // B up to...
467 3*1024, // KB up to...
468 2*1024*1024, // MB up to...
469 1024*1024*1024 // GB from here.
470 };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800471 static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB };
Elliott Hughesc967f782012-04-16 10:23:15 -0700472 static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" };
Mathieu Chartiere6da9af2013-12-16 11:54:42 -0800473 const char* negative_str = "";
474 if (byte_count < 0) {
475 negative_str = "-";
476 byte_count = -byte_count;
477 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700478 int i = arraysize(kUnitThresholds);
479 while (--i > 0) {
480 if (byte_count >= kUnitThresholds[i]) {
481 break;
482 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800483 }
Brian Carlstrom474cc792014-03-07 14:18:15 -0800484 return StringPrintf("%s%" PRId64 "%s",
485 negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800486}
487
Ian Rogers576ca0c2014-06-06 15:58:22 -0700488std::string PrintableChar(uint16_t ch) {
489 std::string result;
490 result += '\'';
491 if (NeedsEscaping(ch)) {
492 StringAppendF(&result, "\\u%04x", ch);
493 } else {
494 result += ch;
495 }
496 result += '\'';
497 return result;
498}
499
Ian Rogers68b56852014-08-29 20:19:11 -0700500std::string PrintableString(const char* utf) {
Elliott Hughes82914b62012-04-09 15:56:29 -0700501 std::string result;
502 result += '"';
Ian Rogers68b56852014-08-29 20:19:11 -0700503 const char* p = utf;
Elliott Hughes82914b62012-04-09 15:56:29 -0700504 size_t char_count = CountModifiedUtf8Chars(p);
505 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000506 uint32_t ch = GetUtf16FromUtf8(&p);
Elliott Hughes82914b62012-04-09 15:56:29 -0700507 if (ch == '\\') {
508 result += "\\\\";
509 } else if (ch == '\n') {
510 result += "\\n";
511 } else if (ch == '\r') {
512 result += "\\r";
513 } else if (ch == '\t') {
514 result += "\\t";
Elliott Hughes82914b62012-04-09 15:56:29 -0700515 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000516 const uint16_t leading = GetLeadingUtf16Char(ch);
517
518 if (NeedsEscaping(leading)) {
519 StringAppendF(&result, "\\u%04x", leading);
520 } else {
521 result += leading;
522 }
523
524 const uint32_t trailing = GetTrailingUtf16Char(ch);
525 if (trailing != 0) {
526 // All high surrogates will need escaping.
527 StringAppendF(&result, "\\u%04x", trailing);
528 }
Elliott Hughes82914b62012-04-09 15:56:29 -0700529 }
530 }
531 result += '"';
532 return result;
533}
534
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800535// 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 -0700536std::string MangleForJni(const std::string& s) {
537 std::string result;
538 size_t char_count = CountModifiedUtf8Chars(s.c_str());
539 const char* cp = &s[0];
540 for (size_t i = 0; i < char_count; ++i) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000541 uint32_t ch = GetUtf16FromUtf8(&cp);
Elliott Hughesd8c00d02012-01-30 14:08:31 -0800542 if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) {
543 result.push_back(ch);
544 } else if (ch == '.' || ch == '/') {
545 result += "_";
546 } else if (ch == '_') {
547 result += "_1";
548 } else if (ch == ';') {
549 result += "_2";
550 } else if (ch == '[') {
551 result += "_3";
Elliott Hughes79082e32011-08-25 12:07:32 -0700552 } else {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000553 const uint16_t leading = GetLeadingUtf16Char(ch);
554 const uint32_t trailing = GetTrailingUtf16Char(ch);
555
556 StringAppendF(&result, "_0%04x", leading);
557 if (trailing != 0) {
558 StringAppendF(&result, "_0%04x", trailing);
559 }
Elliott Hughes79082e32011-08-25 12:07:32 -0700560 }
561 }
562 return result;
563}
564
Brian Carlstromf91c8c32011-09-21 17:30:34 -0700565std::string DotToDescriptor(const char* class_name) {
566 std::string descriptor(class_name);
567 std::replace(descriptor.begin(), descriptor.end(), '.', '/');
568 if (descriptor.length() > 0 && descriptor[0] != '[') {
569 descriptor = "L" + descriptor + ";";
570 }
571 return descriptor;
572}
573
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800574std::string DescriptorToDot(const char* descriptor) {
Elliott Hughes2435a572012-02-17 16:07:41 -0800575 size_t length = strlen(descriptor);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700576 if (length > 1) {
577 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
578 // Descriptors have the leading 'L' and trailing ';' stripped.
579 std::string result(descriptor + 1, length - 2);
580 std::replace(result.begin(), result.end(), '/', '.');
581 return result;
582 } else {
583 // For arrays the 'L' and ';' remain intact.
584 std::string result(descriptor);
585 std::replace(result.begin(), result.end(), '/', '.');
586 return result;
587 }
Elliott Hughes2435a572012-02-17 16:07:41 -0800588 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700589 // Do nothing for non-class/array descriptors.
Elliott Hughes2435a572012-02-17 16:07:41 -0800590 return descriptor;
Elliott Hughes91bf6cd2012-02-14 17:27:48 -0800591}
592
593std::string DescriptorToName(const char* descriptor) {
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800594 size_t length = strlen(descriptor);
Elliott Hughes2435a572012-02-17 16:07:41 -0800595 if (descriptor[0] == 'L' && descriptor[length - 1] == ';') {
596 std::string result(descriptor + 1, length - 2);
597 return result;
598 }
599 return descriptor;
Brian Carlstromaded5f72011-10-07 17:15:04 -0700600}
601
Mathieu Chartiere401d142015-04-22 13:56:20 -0700602std::string JniShortName(ArtMethod* m) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700603 std::string class_name(m->GetDeclaringClassDescriptor());
Elliott Hughes79082e32011-08-25 12:07:32 -0700604 // Remove the leading 'L' and trailing ';'...
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700605 CHECK_EQ(class_name[0], 'L') << class_name;
606 CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name;
Elliott Hughes79082e32011-08-25 12:07:32 -0700607 class_name.erase(0, 1);
608 class_name.erase(class_name.size() - 1, 1);
609
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700610 std::string method_name(m->GetName());
Elliott Hughes79082e32011-08-25 12:07:32 -0700611
612 std::string short_name;
613 short_name += "Java_";
614 short_name += MangleForJni(class_name);
615 short_name += "_";
616 short_name += MangleForJni(method_name);
617 return short_name;
618}
619
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620std::string JniLongName(ArtMethod* m) {
Elliott Hughes79082e32011-08-25 12:07:32 -0700621 std::string long_name;
622 long_name += JniShortName(m);
623 long_name += "__";
624
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700625 std::string signature(m->GetSignature().ToString());
Elliott Hughes79082e32011-08-25 12:07:32 -0700626 signature.erase(0, 1);
627 signature.erase(signature.begin() + signature.find(')'), signature.end());
628
629 long_name += MangleForJni(signature);
630
631 return long_name;
632}
633
jeffhao10037c82012-01-23 15:06:23 -0800634// Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700635uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700636 0x00000000, // 00..1f low control characters; nothing valid
637 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-'
638 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_'
639 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z'
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700640};
641
jeffhao10037c82012-01-23 15:06:23 -0800642// Helper for IsValidPartOfMemberNameUtf8(); do not call directly.
643bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700644 /*
645 * It's a multibyte encoded character. Decode it and analyze. We
646 * accept anything that isn't (a) an improperly encoded low value,
647 * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high
648 * control character, or (e) a high space, layout, or special
649 * character (U+00a0, U+2000..U+200f, U+2028..U+202f,
650 * U+fff0..U+ffff). This is all specified in the dex format
651 * document.
652 */
653
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000654 const uint32_t pair = GetUtf16FromUtf8(pUtf8Ptr);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000655 const uint16_t leading = GetLeadingUtf16Char(pair);
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000656
Narayan Kamath8508e372015-05-06 14:55:43 +0100657 // We have a surrogate pair resulting from a valid 4 byte UTF sequence.
658 // No further checks are necessary because 4 byte sequences span code
659 // points [U+10000, U+1FFFFF], which are valid codepoints in a dex
660 // identifier. Furthermore, GetUtf16FromUtf8 guarantees that each of
661 // the surrogate halves are valid and well formed in this instance.
662 if (GetTrailingUtf16Char(pair) != 0) {
663 return true;
664 }
665
666
667 // We've encountered a one, two or three byte UTF-8 sequence. The
668 // three byte UTF-8 sequence could be one half of a surrogate pair.
669 switch (leading >> 8) {
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000670 case 0x00:
671 // It's only valid if it's above the ISO-8859-1 high space (0xa0).
672 return (leading > 0x00a0);
673 case 0xd8:
674 case 0xd9:
675 case 0xda:
676 case 0xdb:
Narayan Kamath8508e372015-05-06 14:55:43 +0100677 {
678 // We found a three byte sequence encoding one half of a surrogate.
679 // Look for the other half.
680 const uint32_t pair2 = GetUtf16FromUtf8(pUtf8Ptr);
681 const uint16_t trailing = GetLeadingUtf16Char(pair2);
682
683 return (GetTrailingUtf16Char(pair2) == 0) && (0xdc00 <= trailing && trailing <= 0xdfff);
684 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000685 case 0xdc:
686 case 0xdd:
687 case 0xde:
688 case 0xdf:
689 // It's a trailing surrogate, which is not valid at this point.
690 return false;
691 case 0x20:
692 case 0xff:
693 // It's in the range that has spaces, controls, and specials.
694 switch (leading & 0xfff8) {
Narayan Kamath8508e372015-05-06 14:55:43 +0100695 case 0x2000:
696 case 0x2008:
697 case 0x2028:
698 case 0xfff0:
699 case 0xfff8:
700 return false;
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000701 }
Narayan Kamath8508e372015-05-06 14:55:43 +0100702 return true;
703 default:
704 return true;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700705 }
Narayan Kamatha5afcfc2015-01-29 20:06:46 +0000706
Narayan Kamath8508e372015-05-06 14:55:43 +0100707 UNREACHABLE();
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700708}
709
710/* Return whether the pointed-at modified-UTF-8 encoded character is
711 * valid as part of a member name, updating the pointer to point past
712 * the consumed character. This will consume two encoded UTF-16 code
713 * points if the character is encoded as a surrogate pair. Also, if
714 * this function returns false, then the given pointer may only have
715 * been partially advanced.
716 */
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700717static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700718 uint8_t c = (uint8_t) **pUtf8Ptr;
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700719 if (LIKELY(c <= 0x7f)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700720 // It's low-ascii, so check the table.
721 uint32_t wordIdx = c >> 5;
722 uint32_t bitIdx = c & 0x1f;
723 (*pUtf8Ptr)++;
724 return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0;
725 }
726
727 // It's a multibyte encoded character. Call a non-inline function
728 // for the heavy lifting.
jeffhao10037c82012-01-23 15:06:23 -0800729 return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr);
730}
731
732bool IsValidMemberName(const char* s) {
733 bool angle_name = false;
734
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700735 switch (*s) {
jeffhao10037c82012-01-23 15:06:23 -0800736 case '\0':
737 // The empty string is not a valid name.
738 return false;
739 case '<':
740 angle_name = true;
741 s++;
742 break;
743 }
744
745 while (true) {
746 switch (*s) {
747 case '\0':
748 return !angle_name;
749 case '>':
750 return angle_name && s[1] == '\0';
751 }
752
753 if (!IsValidPartOfMemberNameUtf8(&s)) {
754 return false;
755 }
756 }
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700757}
758
Elliott Hughes906e6852011-10-28 14:52:10 -0700759enum ClassNameType { kName, kDescriptor };
Ian Rogers7b078e82014-09-10 14:44:24 -0700760template<ClassNameType kType, char kSeparator>
761static bool IsValidClassName(const char* s) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700762 int arrayCount = 0;
763 while (*s == '[') {
764 arrayCount++;
765 s++;
766 }
767
768 if (arrayCount > 255) {
769 // Arrays may have no more than 255 dimensions.
770 return false;
771 }
772
Ian Rogers7b078e82014-09-10 14:44:24 -0700773 ClassNameType type = kType;
774 if (type != kDescriptor && arrayCount != 0) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700775 /*
776 * If we're looking at an array of some sort, then it doesn't
777 * matter if what is being asked for is a class name; the
778 * format looks the same as a type descriptor in that case, so
779 * treat it as such.
780 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700781 type = kDescriptor;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700782 }
783
Elliott Hughes906e6852011-10-28 14:52:10 -0700784 if (type == kDescriptor) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700785 /*
786 * We are looking for a descriptor. Either validate it as a
787 * single-character primitive type, or continue on to check the
788 * embedded class name (bracketed by "L" and ";").
789 */
790 switch (*(s++)) {
791 case 'B':
792 case 'C':
793 case 'D':
794 case 'F':
795 case 'I':
796 case 'J':
797 case 'S':
798 case 'Z':
799 // These are all single-character descriptors for primitive types.
800 return (*s == '\0');
801 case 'V':
802 // Non-array void is valid, but you can't have an array of void.
803 return (arrayCount == 0) && (*s == '\0');
804 case 'L':
805 // Class name: Break out and continue below.
806 break;
807 default:
808 // Oddball descriptor character.
809 return false;
810 }
811 }
812
813 /*
814 * We just consumed the 'L' that introduces a class name as part
815 * of a type descriptor, or we are looking for an unadorned class
816 * name.
817 */
818
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700819 bool sepOrFirst = true; // first character or just encountered a separator.
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700820 for (;;) {
821 uint8_t c = (uint8_t) *s;
822 switch (c) {
823 case '\0':
824 /*
825 * Premature end for a type descriptor, but valid for
826 * a class name as long as we haven't encountered an
827 * empty component (including the degenerate case of
828 * the empty string "").
829 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700830 return (type == kName) && !sepOrFirst;
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700831 case ';':
832 /*
833 * Invalid character for a class name, but the
834 * legitimate end of a type descriptor. In the latter
835 * case, make sure that this is the end of the string
836 * and that it doesn't end with an empty component
837 * (including the degenerate case of "L;").
838 */
Elliott Hughes906e6852011-10-28 14:52:10 -0700839 return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0');
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700840 case '/':
841 case '.':
Ian Rogers7b078e82014-09-10 14:44:24 -0700842 if (c != kSeparator) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700843 // The wrong separator character.
844 return false;
845 }
846 if (sepOrFirst) {
847 // Separator at start or two separators in a row.
848 return false;
849 }
850 sepOrFirst = true;
851 s++;
852 break;
853 default:
jeffhao10037c82012-01-23 15:06:23 -0800854 if (!IsValidPartOfMemberNameUtf8(&s)) {
Elliott Hughes64bf5a32011-09-20 14:43:12 -0700855 return false;
856 }
857 sepOrFirst = false;
858 break;
859 }
860 }
861}
862
Elliott Hughes906e6852011-10-28 14:52:10 -0700863bool IsValidBinaryClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700864 return IsValidClassName<kName, '.'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700865}
866
867bool IsValidJniClassName(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700868 return IsValidClassName<kName, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700869}
870
871bool IsValidDescriptor(const char* s) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700872 return IsValidClassName<kDescriptor, '/'>(s);
Elliott Hughes906e6852011-10-28 14:52:10 -0700873}
874
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700875void Split(const std::string& s, char separator, std::vector<std::string>* result) {
Elliott Hughes34023802011-08-30 12:06:17 -0700876 const char* p = s.data();
877 const char* end = p + s.size();
878 while (p != end) {
Elliott Hughes48436bb2012-02-07 15:23:28 -0800879 if (*p == separator) {
Elliott Hughes34023802011-08-30 12:06:17 -0700880 ++p;
881 } else {
882 const char* start = p;
Elliott Hughes48436bb2012-02-07 15:23:28 -0800883 while (++p != end && *p != separator) {
884 // Skip to the next occurrence of the separator.
Elliott Hughes34023802011-08-30 12:06:17 -0700885 }
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700886 result->push_back(std::string(start, p - start));
Elliott Hughes34023802011-08-30 12:06:17 -0700887 }
888 }
889}
890
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700891std::string Trim(const std::string& s) {
Dave Allison70202782013-10-22 17:52:19 -0700892 std::string result;
893 unsigned int start_index = 0;
894 unsigned int end_index = s.size() - 1;
895
896 // Skip initial whitespace.
897 while (start_index < s.size()) {
898 if (!isspace(s[start_index])) {
899 break;
900 }
901 start_index++;
902 }
903
904 // Skip terminating whitespace.
905 while (end_index >= start_index) {
906 if (!isspace(s[end_index])) {
907 break;
908 }
909 end_index--;
910 }
911
912 // All spaces, no beef.
913 if (end_index < start_index) {
914 return "";
915 }
916 // Start_index is the first non-space, end_index is the last one.
917 return s.substr(start_index, end_index - start_index + 1);
918}
919
Elliott Hughes48436bb2012-02-07 15:23:28 -0800920template <typename StringT>
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700921std::string Join(const std::vector<StringT>& strings, char separator) {
Elliott Hughes48436bb2012-02-07 15:23:28 -0800922 if (strings.empty()) {
923 return "";
924 }
925
926 std::string result(strings[0]);
927 for (size_t i = 1; i < strings.size(); ++i) {
928 result += separator;
929 result += strings[i];
930 }
931 return result;
932}
933
934// Explicit instantiations.
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700935template std::string Join<std::string>(const std::vector<std::string>& strings, char separator);
936template std::string Join<const char*>(const std::vector<const char*>& strings, char separator);
Elliott Hughes48436bb2012-02-07 15:23:28 -0800937
Elliott Hughesf1a5adc2012-02-10 18:09:35 -0800938bool StartsWith(const std::string& s, const char* prefix) {
939 return s.compare(0, strlen(prefix), prefix) == 0;
940}
941
Brian Carlstrom7a967b32012-03-28 15:23:10 -0700942bool EndsWith(const std::string& s, const char* suffix) {
943 size_t suffix_length = strlen(suffix);
944 size_t string_length = s.size();
945 if (suffix_length > string_length) {
946 return false;
947 }
948 size_t offset = string_length - suffix_length;
949 return s.compare(offset, suffix_length, suffix) == 0;
950}
951
Elliott Hughes22869a92012-03-27 14:08:24 -0700952void SetThreadName(const char* thread_name) {
Elliott Hughesdcc24742011-09-07 14:02:44 -0700953 int hasAt = 0;
954 int hasDot = 0;
Elliott Hughes22869a92012-03-27 14:08:24 -0700955 const char* s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700956 while (*s) {
957 if (*s == '.') {
958 hasDot = 1;
959 } else if (*s == '@') {
960 hasAt = 1;
961 }
962 s++;
963 }
Elliott Hughes22869a92012-03-27 14:08:24 -0700964 int len = s - thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700965 if (len < 15 || hasAt || !hasDot) {
Elliott Hughes22869a92012-03-27 14:08:24 -0700966 s = thread_name;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700967 } else {
Elliott Hughes22869a92012-03-27 14:08:24 -0700968 s = thread_name + len - 15;
Elliott Hughesdcc24742011-09-07 14:02:44 -0700969 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800970#if defined(__linux__)
Elliott Hughes7c6a61e2012-03-12 18:01:41 -0700971 // pthread_setname_np fails rather than truncating long strings.
Elliott Hughes0a18df82015-01-09 15:16:16 -0800972 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
Elliott Hughesdcc24742011-09-07 14:02:44 -0700973 strncpy(buf, s, sizeof(buf)-1);
974 buf[sizeof(buf)-1] = '\0';
975 errno = pthread_setname_np(pthread_self(), buf);
976 if (errno != 0) {
977 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
978 }
Elliott Hughes0a18df82015-01-09 15:16:16 -0800979#else // __APPLE__
Elliott Hughes22869a92012-03-27 14:08:24 -0700980 pthread_setname_np(thread_name);
Elliott Hughesdcc24742011-09-07 14:02:44 -0700981#endif
982}
983
Brian Carlstrom29212012013-09-12 22:18:30 -0700984void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
985 *utime = *stime = *task_cpu = 0;
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700986 std::string stats;
Elliott Hughes8a31b502012-04-30 19:36:11 -0700987 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) {
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700988 return;
989 }
990 // Skip the command, which may contain spaces.
991 stats = stats.substr(stats.find(')') + 2);
992 // Extract the three fields we care about.
993 std::vector<std::string> fields;
Ian Rogers6f3dbba2014-10-14 17:41:57 -0700994 Split(stats, ' ', &fields);
Brian Carlstrom29212012013-09-12 22:18:30 -0700995 *state = fields[0][0];
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700996 *utime = strtoull(fields[11].c_str(), nullptr, 10);
997 *stime = strtoull(fields[12].c_str(), nullptr, 10);
998 *task_cpu = strtoull(fields[36].c_str(), nullptr, 10);
Elliott Hughesbfe487b2011-10-26 15:48:55 -0700999}
1000
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001001std::string GetSchedulerGroupName(pid_t tid) {
1002 // /proc/<pid>/cgroup looks like this:
1003 // 2:devices:/
1004 // 1:cpuacct,cpu:/
1005 // We want the third field from the line whose second field contains the "cpu" token.
1006 std::string cgroup_file;
1007 if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) {
1008 return "";
1009 }
1010 std::vector<std::string> cgroup_lines;
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001011 Split(cgroup_file, '\n', &cgroup_lines);
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001012 for (size_t i = 0; i < cgroup_lines.size(); ++i) {
1013 std::vector<std::string> cgroup_fields;
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001014 Split(cgroup_lines[i], ':', &cgroup_fields);
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001015 std::vector<std::string> cgroups;
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001016 Split(cgroup_fields[1], ',', &cgroups);
Andreas Gampe277ccbd2014-11-03 21:36:10 -08001017 for (size_t j = 0; j < cgroups.size(); ++j) {
1018 if (cgroups[j] == "cpu") {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001019 return cgroup_fields[2].substr(1); // Skip the leading slash.
Elliott Hughes1bac54f2012-03-16 12:48:31 -07001020 }
1021 }
1022 }
1023 return "";
1024}
1025
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001026#if defined(__linux__)
Andreas Gampe00bd2da2015-01-09 15:05:46 -08001027
1028ALWAYS_INLINE
1029static inline void WritePrefix(std::ostream* os, const char* prefix, bool odd) {
1030 if (prefix != nullptr) {
1031 *os << prefix;
1032 }
1033 *os << " ";
1034 if (!odd) {
1035 *os << " ";
1036 }
1037}
1038
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001039static bool RunCommand(std::string cmd, std::ostream* os, const char* prefix) {
1040 FILE* stream = popen(cmd.c_str(), "r");
1041 if (stream) {
1042 if (os != nullptr) {
1043 bool odd_line = true; // We indent them differently.
Andreas Gampe00bd2da2015-01-09 15:05:46 -08001044 bool wrote_prefix = false; // Have we already written a prefix?
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001045 constexpr size_t kMaxBuffer = 128; // Relatively small buffer. Should be OK as we're on an
1046 // alt stack, but just to be sure...
1047 char buffer[kMaxBuffer];
1048 while (!feof(stream)) {
1049 if (fgets(buffer, kMaxBuffer, stream) != nullptr) {
1050 // Split on newlines.
1051 char* tmp = buffer;
1052 for (;;) {
1053 char* new_line = strchr(tmp, '\n');
1054 if (new_line == nullptr) {
1055 // Print the rest.
1056 if (*tmp != 0) {
Andreas Gampe00bd2da2015-01-09 15:05:46 -08001057 if (!wrote_prefix) {
1058 WritePrefix(os, prefix, odd_line);
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001059 }
Andreas Gampe00bd2da2015-01-09 15:05:46 -08001060 wrote_prefix = true;
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001061 *os << tmp;
1062 }
1063 break;
1064 }
Andreas Gampe00bd2da2015-01-09 15:05:46 -08001065 if (!wrote_prefix) {
1066 WritePrefix(os, prefix, odd_line);
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001067 }
1068 char saved = *(new_line + 1);
1069 *(new_line + 1) = 0;
1070 *os << tmp;
1071 *(new_line + 1) = saved;
1072 tmp = new_line + 1;
1073 odd_line = !odd_line;
Andreas Gampe00bd2da2015-01-09 15:05:46 -08001074 wrote_prefix = false;
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001075 }
1076 }
1077 }
1078 }
1079 pclose(stream);
1080 return true;
1081 } else {
1082 return false;
1083 }
1084}
1085
1086static void Addr2line(const std::string& map_src, uintptr_t offset, std::ostream& os,
1087 const char* prefix) {
1088 std::string cmdline(StringPrintf("addr2line --functions --inlines --demangle -e %s %zx",
1089 map_src.c_str(), offset));
1090 RunCommand(cmdline.c_str(), &os, prefix);
1091}
1092#endif
1093
Christopher Ferrisa2cee182014-04-16 19:13:59 -07001094void DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001095 ArtMethod* current_method, void* ucontext_ptr) {
Ian Rogers83597d02014-11-20 10:29:00 -08001096#if __linux__
Andreas Gamped7576322014-10-24 22:13:45 -07001097 // b/18119146
Evgenii Stepanov1e133742015-05-20 12:30:59 -07001098 if (RUNNING_ON_MEMORY_TOOL != 0) {
Andreas Gamped7576322014-10-24 22:13:45 -07001099 return;
1100 }
1101
Ian Rogers700a4022014-05-19 16:49:03 -07001102 std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid));
Andreas Gampe628a61a2015-01-07 22:08:35 -08001103 if (!backtrace->Unwind(0, reinterpret_cast<ucontext*>(ucontext_ptr))) {
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -07001104 os << prefix << "(backtrace::Unwind failed for thread " << tid << ")\n";
Elliott Hughes46e251b2012-05-22 15:10:45 -07001105 return;
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -07001106 } else if (backtrace->NumFrames() == 0) {
Elliott Hughes225f5a12012-06-11 11:23:48 -07001107 os << prefix << "(no native stack frames for thread " << tid << ")\n";
Elliott Hughes46e251b2012-05-22 15:10:45 -07001108 return;
1109 }
1110
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001111 // Check whether we have and should use addr2line.
1112 bool use_addr2line;
1113 if (kUseAddr2line) {
1114 // Try to run it to see whether we have it. Push an argument so that it doesn't assume a.out
1115 // and print to stderr.
Andreas Gampe941c5512015-01-15 10:38:19 -08001116 use_addr2line = (gAborting > 0) && RunCommand("addr2line -h", nullptr, nullptr);
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001117 } else {
1118 use_addr2line = false;
1119 }
1120
Christopher Ferris943af7d2014-01-16 12:41:46 -08001121 for (Backtrace::const_iterator it = backtrace->begin();
1122 it != backtrace->end(); ++it) {
Elliott Hughes46e251b2012-05-22 15:10:45 -07001123 // We produce output like this:
Christopher Ferrisa2cee182014-04-16 19:13:59 -07001124 // ] #00 pc 000075bb8 /system/lib/libc.so (unwind_backtrace_thread+536)
1125 // In order for parsing tools to continue to function, the stack dump
1126 // format must at least adhere to this format:
1127 // #XX pc <RELATIVE_ADDR> <FULL_PATH_TO_SHARED_LIBRARY> ...
1128 // The parsers require a single space before and after pc, and two spaces
1129 // after the <RELATIVE_ADDR>. There can be any prefix data before the
1130 // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix.
1131 os << prefix << StringPrintf("#%02zu pc ", it->num);
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001132 bool try_addr2line = false;
Christopher Ferrisa1c96652015-02-06 13:18:58 -08001133 if (!BacktraceMap::IsValid(it->map)) {
Andreas Gampedd671252015-07-23 14:37:18 -07001134 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIxPTR " ???"
1135 : "%08" PRIxPTR " ???",
1136 it->pc);
Christopher Ferris7b5f0cf2013-11-01 15:18:45 -07001137 } else {
Andreas Gampedd671252015-07-23 14:37:18 -07001138 os << StringPrintf(Is64BitInstructionSet(kRuntimeISA) ? "%016" PRIxPTR " "
1139 : "%08" PRIxPTR " ",
1140 BacktraceMap::GetRelativePc(it->map, it->pc));
Christopher Ferrisa1c96652015-02-06 13:18:58 -08001141 os << it->map.name;
Andreas Gampe3ef69b42015-01-26 10:38:34 -08001142 os << " (";
Christopher Ferrisa2cee182014-04-16 19:13:59 -07001143 if (!it->func_name.empty()) {
1144 os << it->func_name;
1145 if (it->func_offset != 0) {
1146 os << "+" << it->func_offset;
1147 }
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001148 try_addr2line = true;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001149 } else if (
1150 current_method != nullptr && Locks::mutator_lock_->IsSharedHeld(Thread::Current()) &&
1151 current_method->PcIsWithinQuickCode(it->pc)) {
Brian Carlstrom474cc792014-03-07 14:18:15 -08001152 const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode();
1153 os << JniLongName(current_method) << "+"
1154 << (it->pc - reinterpret_cast<uintptr_t>(start_of_code));
Kenny Root067d20f2014-03-05 14:57:21 -08001155 } else {
1156 os << "???";
1157 }
Christopher Ferrisa2cee182014-04-16 19:13:59 -07001158 os << ")";
Elliott Hughes46e251b2012-05-22 15:10:45 -07001159 }
Christopher Ferrisa2cee182014-04-16 19:13:59 -07001160 os << "\n";
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001161 if (try_addr2line && use_addr2line) {
Christopher Ferrisa1c96652015-02-06 13:18:58 -08001162 Addr2line(it->map.name, it->pc - it->map.start, os, prefix);
Andreas Gampe8e1cb912015-01-08 20:11:09 -08001163 }
Elliott Hughes46e251b2012-05-22 15:10:45 -07001164 }
Nicolas Geoffraye6ac4fd2014-11-04 13:03:29 +00001165#else
Andreas Gampe9387c722015-01-08 11:32:22 -08001166 UNUSED(os, tid, prefix, current_method, ucontext_ptr);
Ian Rogersc5f17732014-06-05 20:48:42 -07001167#endif
Elliott Hughes46e251b2012-05-22 15:10:45 -07001168}
1169
Elliott Hughes058a6de2012-05-24 19:13:02 -07001170#if defined(__APPLE__)
1171
1172// TODO: is there any way to get the kernel stack on Mac OS?
1173void DumpKernelStack(std::ostream&, pid_t, const char*, bool) {}
1174
1175#else
1176
Elliott Hughes46e251b2012-05-22 15:10:45 -07001177void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
Elliott Hughes12a95022012-05-24 21:41:38 -07001178 if (tid == GetTid()) {
1179 // There's no point showing that we're reading our stack out of /proc!
1180 return;
1181 }
1182
Elliott Hughes46e251b2012-05-22 15:10:45 -07001183 std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid));
1184 std::string kernel_stack;
1185 if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) {
Elliott Hughes058a6de2012-05-24 19:13:02 -07001186 os << prefix << "(couldn't read " << kernel_stack_filename << ")\n";
jeffhaoc4c3ee22012-05-25 16:16:32 -07001187 return;
Elliott Hughes46e251b2012-05-22 15:10:45 -07001188 }
1189
1190 std::vector<std::string> kernel_stack_frames;
Ian Rogers6f3dbba2014-10-14 17:41:57 -07001191 Split(kernel_stack, '\n', &kernel_stack_frames);
Elliott Hughes46e251b2012-05-22 15:10:45 -07001192 // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff",
1193 // which looking at the source appears to be the kernel's way of saying "that's all, folks!".
1194 kernel_stack_frames.pop_back();
1195 for (size_t i = 0; i < kernel_stack_frames.size(); ++i) {
Brian Carlstrom474cc792014-03-07 14:18:15 -08001196 // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110"
1197 // into "futex_wait_queue_me+0xcd/0x110".
Elliott Hughes46e251b2012-05-22 15:10:45 -07001198 const char* text = kernel_stack_frames[i].c_str();
1199 const char* close_bracket = strchr(text, ']');
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001200 if (close_bracket != nullptr) {
Elliott Hughes46e251b2012-05-22 15:10:45 -07001201 text = close_bracket + 2;
1202 }
1203 os << prefix;
1204 if (include_count) {
1205 os << StringPrintf("#%02zd ", i);
1206 }
1207 os << text << "\n";
1208 }
1209}
1210
1211#endif
1212
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001213const char* GetAndroidRoot() {
1214 const char* android_root = getenv("ANDROID_ROOT");
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001215 if (android_root == nullptr) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001216 if (OS::DirectoryExists("/system")) {
1217 android_root = "/system";
Brian Carlstroma9f19782011-10-13 00:14:47 -07001218 } else {
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001219 LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist";
1220 return "";
Brian Carlstroma9f19782011-10-13 00:14:47 -07001221 }
1222 }
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001223 if (!OS::DirectoryExists(android_root)) {
1224 LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root;
Brian Carlstroma9f19782011-10-13 00:14:47 -07001225 return "";
1226 }
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001227 return android_root;
1228}
Brian Carlstroma9f19782011-10-13 00:14:47 -07001229
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001230const char* GetAndroidData() {
Alex Lighta59dd802014-07-02 16:28:08 -07001231 std::string error_msg;
1232 const char* dir = GetAndroidDataSafe(&error_msg);
1233 if (dir != nullptr) {
1234 return dir;
1235 } else {
1236 LOG(FATAL) << error_msg;
1237 return "";
1238 }
1239}
1240
1241const char* GetAndroidDataSafe(std::string* error_msg) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001242 const char* android_data = getenv("ANDROID_DATA");
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001243 if (android_data == nullptr) {
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001244 if (OS::DirectoryExists("/data")) {
1245 android_data = "/data";
1246 } else {
Alex Lighta59dd802014-07-02 16:28:08 -07001247 *error_msg = "ANDROID_DATA not set and /data does not exist";
1248 return nullptr;
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001249 }
1250 }
1251 if (!OS::DirectoryExists(android_data)) {
Alex Lighta59dd802014-07-02 16:28:08 -07001252 *error_msg = StringPrintf("Failed to find ANDROID_DATA directory %s", android_data);
1253 return nullptr;
Brian Carlstroma56fcd62012-02-04 21:23:01 -08001254 }
1255 return android_data;
1256}
1257
Alex Lighta59dd802014-07-02 16:28:08 -07001258void GetDalvikCache(const char* subdir, const bool create_if_absent, std::string* dalvik_cache,
Andreas Gampe3c13a792014-09-18 20:56:04 -07001259 bool* have_android_data, bool* dalvik_cache_exists, bool* is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -07001260 CHECK(subdir != nullptr);
1261 std::string error_msg;
1262 const char* android_data = GetAndroidDataSafe(&error_msg);
1263 if (android_data == nullptr) {
1264 *have_android_data = false;
1265 *dalvik_cache_exists = false;
Andreas Gampe3c13a792014-09-18 20:56:04 -07001266 *is_global_cache = false;
Alex Lighta59dd802014-07-02 16:28:08 -07001267 return;
1268 } else {
1269 *have_android_data = true;
1270 }
1271 const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
1272 *dalvik_cache = dalvik_cache_root + subdir;
1273 *dalvik_cache_exists = OS::DirectoryExists(dalvik_cache->c_str());
Andreas Gampe3c13a792014-09-18 20:56:04 -07001274 *is_global_cache = strcmp(android_data, "/data") == 0;
1275 if (create_if_absent && !*dalvik_cache_exists && !*is_global_cache) {
Alex Lighta59dd802014-07-02 16:28:08 -07001276 // Don't create the system's /data/dalvik-cache/... because it needs special permissions.
1277 *dalvik_cache_exists = ((mkdir(dalvik_cache_root.c_str(), 0700) == 0 || errno == EEXIST) &&
1278 (mkdir(dalvik_cache->c_str(), 0700) == 0 || errno == EEXIST));
1279 }
1280}
1281
Andreas Gampe40da2862015-02-27 12:49:04 -08001282static std::string GetDalvikCacheImpl(const char* subdir,
1283 const bool create_if_absent,
1284 const bool abort_on_error) {
Narayan Kamath11d9f062014-04-23 20:24:57 +01001285 CHECK(subdir != nullptr);
Brian Carlstrom41ccffd2014-05-06 10:37:30 -07001286 const char* android_data = GetAndroidData();
1287 const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data));
Narayan Kamath11d9f062014-04-23 20:24:57 +01001288 const std::string dalvik_cache = dalvik_cache_root + subdir;
Andreas Gampe40da2862015-02-27 12:49:04 -08001289 if (!OS::DirectoryExists(dalvik_cache.c_str())) {
1290 if (!create_if_absent) {
1291 // TODO: Check callers. Traditional behavior is to not to abort, even when abort_on_error.
1292 return "";
1293 }
1294
Brian Carlstrom41ccffd2014-05-06 10:37:30 -07001295 // Don't create the system's /data/dalvik-cache/... because it needs special permissions.
Andreas Gampe40da2862015-02-27 12:49:04 -08001296 if (strcmp(android_data, "/data") == 0) {
1297 if (abort_on_error) {
1298 LOG(FATAL) << "Failed to find dalvik-cache directory " << dalvik_cache
1299 << ", cannot create /data dalvik-cache.";
1300 UNREACHABLE();
Narayan Kamath11d9f062014-04-23 20:24:57 +01001301 }
Andreas Gampe40da2862015-02-27 12:49:04 -08001302 return "";
1303 }
1304
1305 int result = mkdir(dalvik_cache_root.c_str(), 0700);
1306 if (result != 0 && errno != EEXIST) {
1307 if (abort_on_error) {
1308 PLOG(FATAL) << "Failed to create dalvik-cache root directory " << dalvik_cache_root;
1309 UNREACHABLE();
1310 }
1311 return "";
1312 }
1313
1314 result = mkdir(dalvik_cache.c_str(), 0700);
1315 if (result != 0) {
1316 if (abort_on_error) {
Narayan Kamath11d9f062014-04-23 20:24:57 +01001317 PLOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache;
Andreas Gampe40da2862015-02-27 12:49:04 -08001318 UNREACHABLE();
Brian Carlstroma9f19782011-10-13 00:14:47 -07001319 }
Brian Carlstroma9f19782011-10-13 00:14:47 -07001320 return "";
1321 }
1322 }
Brian Carlstrom7675e162013-06-10 16:18:04 -07001323 return dalvik_cache;
Brian Carlstroma9f19782011-10-13 00:14:47 -07001324}
1325
Andreas Gampe40da2862015-02-27 12:49:04 -08001326std::string GetDalvikCache(const char* subdir, const bool create_if_absent) {
1327 return GetDalvikCacheImpl(subdir, create_if_absent, false);
1328}
1329
1330std::string GetDalvikCacheOrDie(const char* subdir, const bool create_if_absent) {
1331 return GetDalvikCacheImpl(subdir, create_if_absent, true);
1332}
1333
Alex Lighta59dd802014-07-02 16:28:08 -07001334bool GetDalvikCacheFilename(const char* location, const char* cache_location,
1335 std::string* filename, std::string* error_msg) {
Ian Rogerse6060102013-05-16 12:01:04 -07001336 if (location[0] != '/') {
Alex Lighta59dd802014-07-02 16:28:08 -07001337 *error_msg = StringPrintf("Expected path in location to be absolute: %s", location);
1338 return false;
Ian Rogerse6060102013-05-16 12:01:04 -07001339 }
Ian Rogers8d31bbd2013-10-13 10:44:14 -07001340 std::string cache_file(&location[1]); // skip leading slash
Alex Light6e183f22014-07-18 14:57:04 -07001341 if (!EndsWith(location, ".dex") && !EndsWith(location, ".art") && !EndsWith(location, ".oat")) {
Brian Carlstrom30e2ea42013-06-19 23:25:37 -07001342 cache_file += "/";
1343 cache_file += DexFile::kClassesDex;
1344 }
Brian Carlstromb7bbba42011-10-13 14:58:47 -07001345 std::replace(cache_file.begin(), cache_file.end(), '/', '@');
Alex Lighta59dd802014-07-02 16:28:08 -07001346 *filename = StringPrintf("%s/%s", cache_location, cache_file.c_str());
1347 return true;
1348}
1349
1350std::string GetDalvikCacheFilenameOrDie(const char* location, const char* cache_location) {
1351 std::string ret;
1352 std::string error_msg;
1353 if (!GetDalvikCacheFilename(location, cache_location, &ret, &error_msg)) {
1354 LOG(FATAL) << error_msg;
1355 }
1356 return ret;
Brian Carlstromb7bbba42011-10-13 14:58:47 -07001357}
1358
Brian Carlstrom2afe4942014-05-19 10:25:33 -07001359static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) {
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -07001360 // in = /foo/bar/baz
1361 // out = /foo/bar/<isa>/baz
1362 size_t pos = filename->rfind('/');
1363 CHECK_NE(pos, std::string::npos) << *filename << " " << isa;
1364 filename->insert(pos, "/", 1);
1365 filename->insert(pos + 1, GetInstructionSetString(isa));
1366}
1367
1368std::string GetSystemImageFilename(const char* location, const InstructionSet isa) {
1369 // location = /system/framework/boot.art
1370 // filename = /system/framework/<isa>/boot.art
1371 std::string filename(location);
Brian Carlstrom2afe4942014-05-19 10:25:33 -07001372 InsertIsaDirectory(isa, &filename);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -07001373 return filename;
1374}
1375
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -07001376bool IsZipMagic(uint32_t magic) {
1377 return (('P' == ((magic >> 0) & 0xff)) &&
1378 ('K' == ((magic >> 8) & 0xff)));
jeffhao262bf462011-10-20 18:36:32 -07001379}
1380
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -07001381bool IsDexMagic(uint32_t magic) {
Ian Rogers13735952014-10-08 12:43:28 -07001382 return DexFile::IsMagicValid(reinterpret_cast<const uint8_t*>(&magic));
Brian Carlstrom7a967b32012-03-28 15:23:10 -07001383}
1384
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -07001385bool IsOatMagic(uint32_t magic) {
Ian Rogers13735952014-10-08 12:43:28 -07001386 return (memcmp(reinterpret_cast<const uint8_t*>(magic),
Brian Carlstrom7c3d13a2013-09-04 17:15:11 -07001387 OatHeader::kOatMagic,
1388 sizeof(OatHeader::kOatMagic)) == 0);
jeffhao262bf462011-10-20 18:36:32 -07001389}
1390
Brian Carlstrom6449c622014-02-10 23:48:36 -08001391bool Exec(std::vector<std::string>& arg_vector, std::string* error_msg) {
1392 const std::string command_line(Join(arg_vector, ' '));
1393
1394 CHECK_GE(arg_vector.size(), 1U) << command_line;
1395
1396 // Convert the args to char pointers.
1397 const char* program = arg_vector[0].c_str();
1398 std::vector<char*> args;
Brian Carlstrom35d8b8e2014-02-25 10:51:11 -08001399 for (size_t i = 0; i < arg_vector.size(); ++i) {
1400 const std::string& arg = arg_vector[i];
1401 char* arg_str = const_cast<char*>(arg.c_str());
1402 CHECK(arg_str != nullptr) << i;
1403 args.push_back(arg_str);
Brian Carlstrom6449c622014-02-10 23:48:36 -08001404 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001405 args.push_back(nullptr);
Brian Carlstrom6449c622014-02-10 23:48:36 -08001406
1407 // fork and exec
1408 pid_t pid = fork();
1409 if (pid == 0) {
1410 // no allocation allowed between fork and exec
1411
1412 // change process groups, so we don't get reaped by ProcessManager
1413 setpgid(0, 0);
1414
1415 execv(program, &args[0]);
1416
Brian Carlstrom13db9aa2014-02-27 12:44:32 -08001417 PLOG(ERROR) << "Failed to execv(" << command_line << ")";
1418 exit(1);
Brian Carlstrom6449c622014-02-10 23:48:36 -08001419 } else {
1420 if (pid == -1) {
1421 *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s",
1422 command_line.c_str(), strerror(errno));
1423 return false;
1424 }
1425
1426 // wait for subprocess to finish
1427 int status;
1428 pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0));
1429 if (got_pid != pid) {
1430 *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: "
1431 "wanted %d, got %d: %s",
1432 command_line.c_str(), pid, got_pid, strerror(errno));
1433 return false;
1434 }
1435 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1436 *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status",
1437 command_line.c_str());
1438 return false;
1439 }
1440 }
1441 return true;
1442}
1443
Tong Shen547cdfd2014-08-05 01:54:19 -07001444void EncodeUnsignedLeb128(uint32_t data, std::vector<uint8_t>* dst) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001445 Leb128Encoder(dst).PushBackUnsigned(data);
Tong Shen547cdfd2014-08-05 01:54:19 -07001446}
1447
1448void EncodeSignedLeb128(int32_t data, std::vector<uint8_t>* dst) {
Yevgeny Roubane3ea8382014-08-08 16:29:38 +07001449 Leb128Encoder(dst).PushBackSigned(data);
Tong Shen547cdfd2014-08-05 01:54:19 -07001450}
1451
Mathieu Chartier76433272014-09-26 14:32:37 -07001452std::string PrettyDescriptor(Primitive::Type type) {
1453 return PrettyDescriptor(Primitive::Descriptor(type));
1454}
1455
Andreas Gampe5073fed2015-08-10 11:40:25 -07001456static void DumpMethodCFGImpl(const DexFile* dex_file,
1457 uint32_t dex_method_idx,
1458 const DexFile::CodeItem* code_item,
1459 std::ostream& os) {
1460 os << "digraph {\n";
1461 os << " # /* " << PrettyMethod(dex_method_idx, *dex_file, true) << " */\n";
1462
1463 std::set<uint32_t> dex_pc_is_branch_target;
1464 {
1465 // Go and populate.
1466 const Instruction* inst = Instruction::At(code_item->insns_);
1467 for (uint32_t dex_pc = 0;
1468 dex_pc < code_item->insns_size_in_code_units_;
1469 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
1470 if (inst->IsBranch()) {
1471 dex_pc_is_branch_target.insert(dex_pc + inst->GetTargetOffset());
1472 } else if (inst->IsSwitch()) {
1473 const uint16_t* insns = code_item->insns_ + dex_pc;
1474 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1475 const uint16_t* switch_insns = insns + switch_offset;
1476 uint32_t switch_count = switch_insns[1];
1477 int32_t targets_offset;
1478 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1479 /* 0=sig, 1=count, 2/3=firstKey */
1480 targets_offset = 4;
1481 } else {
1482 /* 0=sig, 1=count, 2..count*2 = keys */
1483 targets_offset = 2 + 2 * switch_count;
1484 }
1485 for (uint32_t targ = 0; targ < switch_count; targ++) {
1486 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1487 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1488 dex_pc_is_branch_target.insert(dex_pc + offset);
1489 }
1490 }
1491 }
1492 }
1493
1494 // Create nodes for "basic blocks."
1495 std::map<uint32_t, uint32_t> dex_pc_to_node_id; // This only has entries for block starts.
1496 std::map<uint32_t, uint32_t> dex_pc_to_incl_id; // This has entries for all dex pcs.
1497
1498 {
1499 const Instruction* inst = Instruction::At(code_item->insns_);
1500 bool first_in_block = true;
1501 bool force_new_block = false;
1502 for (uint32_t dex_pc = 0; dex_pc < code_item->insns_size_in_code_units_;
1503 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
1504 if (dex_pc == 0 ||
1505 (dex_pc_is_branch_target.find(dex_pc) != dex_pc_is_branch_target.end()) ||
1506 force_new_block) {
1507 uint32_t id = dex_pc_to_node_id.size();
1508 if (id > 0) {
1509 // End last node.
1510 os << "}\"];\n";
1511 }
1512 // Start next node.
1513 os << " node" << id << " [shape=record,label=\"{";
1514 dex_pc_to_node_id.insert(std::make_pair(dex_pc, id));
1515 first_in_block = true;
1516 force_new_block = false;
1517 }
1518
1519 // Register instruction.
1520 dex_pc_to_incl_id.insert(std::make_pair(dex_pc, dex_pc_to_node_id.size() - 1));
1521
1522 // Print instruction.
1523 if (!first_in_block) {
1524 os << " | ";
1525 } else {
1526 first_in_block = false;
1527 }
1528
1529 // Dump the instruction. Need to escape '"', '<', '>', '{' and '}'.
1530 os << "<" << "p" << dex_pc << ">";
1531 os << " 0x" << std::hex << dex_pc << std::dec << ": ";
1532 std::string inst_str = inst->DumpString(dex_file);
1533 size_t cur_start = 0; // It's OK to start at zero, instruction dumps don't start with chars
1534 // we need to escape.
1535 while (cur_start != std::string::npos) {
1536 size_t next_escape = inst_str.find_first_of("\"{}<>", cur_start + 1);
1537 if (next_escape == std::string::npos) {
1538 os << inst_str.substr(cur_start, inst_str.size() - cur_start);
1539 break;
1540 } else {
1541 os << inst_str.substr(cur_start, next_escape - cur_start);
1542 // Escape all necessary characters.
1543 while (next_escape < inst_str.size()) {
1544 char c = inst_str.at(next_escape);
1545 if (c == '"' || c == '{' || c == '}' || c == '<' || c == '>') {
1546 os << '\\' << c;
1547 } else {
1548 break;
1549 }
1550 next_escape++;
1551 }
1552 if (next_escape >= inst_str.size()) {
1553 next_escape = std::string::npos;
1554 }
1555 cur_start = next_escape;
1556 }
1557 }
1558
1559 // Force a new block for some fall-throughs and some instructions that terminate the "local"
1560 // control flow.
1561 force_new_block = inst->IsSwitch() || inst->IsBasicBlockEnd();
1562 }
1563 // Close last node.
1564 if (dex_pc_to_node_id.size() > 0) {
1565 os << "}\"];\n";
1566 }
1567 }
1568
1569 // Create edges between them.
1570 {
1571 std::ostringstream regular_edges;
1572 std::ostringstream taken_edges;
1573 std::ostringstream exception_edges;
1574
1575 // Common set of exception edges.
1576 std::set<uint32_t> exception_targets;
1577
1578 // These blocks (given by the first dex pc) need exception per dex-pc handling in a second
1579 // pass. In the first pass we try and see whether we can use a common set of edges.
1580 std::set<uint32_t> blocks_with_detailed_exceptions;
1581
1582 {
1583 uint32_t last_node_id = std::numeric_limits<uint32_t>::max();
1584 uint32_t old_dex_pc = 0;
1585 uint32_t block_start_dex_pc = std::numeric_limits<uint32_t>::max();
1586 const Instruction* inst = Instruction::At(code_item->insns_);
1587 for (uint32_t dex_pc = 0;
1588 dex_pc < code_item->insns_size_in_code_units_;
1589 old_dex_pc = dex_pc, dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
1590 {
1591 auto it = dex_pc_to_node_id.find(dex_pc);
1592 if (it != dex_pc_to_node_id.end()) {
1593 if (!exception_targets.empty()) {
1594 // It seems the last block had common exception handlers. Add the exception edges now.
1595 uint32_t node_id = dex_pc_to_node_id.find(block_start_dex_pc)->second;
1596 for (uint32_t handler_pc : exception_targets) {
1597 auto node_id_it = dex_pc_to_incl_id.find(handler_pc);
1598 if (node_id_it != dex_pc_to_incl_id.end()) {
1599 exception_edges << " node" << node_id
1600 << " -> node" << node_id_it->second << ":p" << handler_pc
1601 << ";\n";
1602 }
1603 }
1604 exception_targets.clear();
1605 }
1606
1607 block_start_dex_pc = dex_pc;
1608
1609 // Seems to be a fall-through, connect to last_node_id. May be spurious edges for things
1610 // like switch data.
1611 uint32_t old_last = last_node_id;
1612 last_node_id = it->second;
1613 if (old_last != std::numeric_limits<uint32_t>::max()) {
1614 regular_edges << " node" << old_last << ":p" << old_dex_pc
1615 << " -> node" << last_node_id << ":p" << dex_pc
1616 << ";\n";
1617 }
1618 }
1619
1620 // Look at the exceptions of the first entry.
1621 CatchHandlerIterator catch_it(*code_item, dex_pc);
1622 for (; catch_it.HasNext(); catch_it.Next()) {
1623 exception_targets.insert(catch_it.GetHandlerAddress());
1624 }
1625 }
1626
1627 // Handle instruction.
1628
1629 // Branch: something with at most two targets.
1630 if (inst->IsBranch()) {
1631 const int32_t offset = inst->GetTargetOffset();
1632 const bool conditional = !inst->IsUnconditional();
1633
1634 auto target_it = dex_pc_to_node_id.find(dex_pc + offset);
1635 if (target_it != dex_pc_to_node_id.end()) {
1636 taken_edges << " node" << last_node_id << ":p" << dex_pc
1637 << " -> node" << target_it->second << ":p" << (dex_pc + offset)
1638 << ";\n";
1639 }
1640 if (!conditional) {
1641 // No fall-through.
1642 last_node_id = std::numeric_limits<uint32_t>::max();
1643 }
1644 } else if (inst->IsSwitch()) {
1645 // TODO: Iterate through all switch targets.
1646 const uint16_t* insns = code_item->insns_ + dex_pc;
1647 /* make sure the start of the switch is in range */
1648 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
1649 /* offset to switch table is a relative branch-style offset */
1650 const uint16_t* switch_insns = insns + switch_offset;
1651 uint32_t switch_count = switch_insns[1];
1652 int32_t targets_offset;
1653 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1654 /* 0=sig, 1=count, 2/3=firstKey */
1655 targets_offset = 4;
1656 } else {
1657 /* 0=sig, 1=count, 2..count*2 = keys */
1658 targets_offset = 2 + 2 * switch_count;
1659 }
1660 /* make sure the end of the switch is in range */
1661 /* verify each switch target */
1662 for (uint32_t targ = 0; targ < switch_count; targ++) {
1663 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1664 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1665 int32_t abs_offset = dex_pc + offset;
1666 auto target_it = dex_pc_to_node_id.find(abs_offset);
1667 if (target_it != dex_pc_to_node_id.end()) {
1668 // TODO: value label.
1669 taken_edges << " node" << last_node_id << ":p" << dex_pc
1670 << " -> node" << target_it->second << ":p" << (abs_offset)
1671 << ";\n";
1672 }
1673 }
1674 }
1675
1676 // Exception edges. If this is not the first instruction in the block
1677 if (block_start_dex_pc != dex_pc) {
1678 std::set<uint32_t> current_handler_pcs;
1679 CatchHandlerIterator catch_it(*code_item, dex_pc);
1680 for (; catch_it.HasNext(); catch_it.Next()) {
1681 current_handler_pcs.insert(catch_it.GetHandlerAddress());
1682 }
1683 if (current_handler_pcs != exception_targets) {
1684 exception_targets.clear(); // Clear so we don't do something at the end.
1685 blocks_with_detailed_exceptions.insert(block_start_dex_pc);
1686 }
1687 }
1688
1689 if (inst->IsReturn() ||
1690 (inst->Opcode() == Instruction::THROW) ||
1691 (inst->IsBranch() && inst->IsUnconditional())) {
1692 // No fall-through.
1693 last_node_id = std::numeric_limits<uint32_t>::max();
1694 }
1695 }
1696 // Finish up the last block, if it had common exceptions.
1697 if (!exception_targets.empty()) {
1698 // It seems the last block had common exception handlers. Add the exception edges now.
1699 uint32_t node_id = dex_pc_to_node_id.find(block_start_dex_pc)->second;
1700 for (uint32_t handler_pc : exception_targets) {
1701 auto node_id_it = dex_pc_to_incl_id.find(handler_pc);
1702 if (node_id_it != dex_pc_to_incl_id.end()) {
1703 exception_edges << " node" << node_id
1704 << " -> node" << node_id_it->second << ":p" << handler_pc
1705 << ";\n";
1706 }
1707 }
1708 exception_targets.clear();
1709 }
1710 }
1711
1712 // Second pass for detailed exception blocks.
1713 // TODO
1714 // Exception edges. If this is not the first instruction in the block
1715 for (uint32_t dex_pc : blocks_with_detailed_exceptions) {
1716 const Instruction* inst = Instruction::At(&code_item->insns_[dex_pc]);
1717 uint32_t this_node_id = dex_pc_to_incl_id.find(dex_pc)->second;
1718 for (;;) {
1719 CatchHandlerIterator catch_it(*code_item, dex_pc);
1720 if (catch_it.HasNext()) {
1721 std::set<uint32_t> handled_targets;
1722 for (; catch_it.HasNext(); catch_it.Next()) {
1723 uint32_t handler_pc = catch_it.GetHandlerAddress();
1724 auto it = handled_targets.find(handler_pc);
1725 if (it == handled_targets.end()) {
1726 auto node_id_it = dex_pc_to_incl_id.find(handler_pc);
1727 if (node_id_it != dex_pc_to_incl_id.end()) {
1728 exception_edges << " node" << this_node_id << ":p" << dex_pc
1729 << " -> node" << node_id_it->second << ":p" << handler_pc
1730 << ";\n";
1731 }
1732
1733 // Mark as done.
1734 handled_targets.insert(handler_pc);
1735 }
1736 }
1737 }
1738 if (inst->IsBasicBlockEnd()) {
1739 break;
1740 }
1741
1742 // Loop update. In the body to have a late break-out if the next instruction is a branch
1743 // target and thus in another block.
1744 dex_pc += inst->SizeInCodeUnits();
1745 if (dex_pc >= code_item->insns_size_in_code_units_) {
1746 break;
1747 }
1748 if (dex_pc_to_node_id.find(dex_pc) != dex_pc_to_node_id.end()) {
1749 break;
1750 }
1751 inst = inst->Next();
1752 }
1753 }
1754
1755 // Write out the sub-graphs to make edges styled.
1756 os << "\n";
1757 os << " subgraph regular_edges {\n";
1758 os << " edge [color=\"#000000\",weight=.3,len=3];\n\n";
1759 os << " " << regular_edges.str() << "\n";
1760 os << " }\n\n";
1761
1762 os << " subgraph taken_edges {\n";
1763 os << " edge [color=\"#00FF00\",weight=.3,len=3];\n\n";
1764 os << " " << taken_edges.str() << "\n";
1765 os << " }\n\n";
1766
1767 os << " subgraph exception_edges {\n";
1768 os << " edge [color=\"#FF0000\",weight=.3,len=3];\n\n";
1769 os << " " << exception_edges.str() << "\n";
1770 os << " }\n\n";
1771 }
1772
1773 os << "}\n";
1774}
1775
1776void DumpMethodCFG(ArtMethod* method, std::ostream& os) {
1777 const DexFile* dex_file = method->GetDexFile();
1778 const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
1779
1780 DumpMethodCFGImpl(dex_file, method->GetDexMethodIndex(), code_item, os);
1781}
1782
1783void DumpMethodCFG(const DexFile* dex_file, uint32_t dex_method_idx, std::ostream& os) {
1784 // This is painful, we need to find the code item. That means finding the class, and then
1785 // iterating the table.
1786 if (dex_method_idx >= dex_file->NumMethodIds()) {
1787 os << "Could not find method-idx.";
1788 return;
1789 }
1790 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
1791
1792 const DexFile::ClassDef* class_def = dex_file->FindClassDef(method_id.class_idx_);
1793 if (class_def == nullptr) {
1794 os << "Could not find class-def.";
1795 return;
1796 }
1797
1798 const uint8_t* class_data = dex_file->GetClassData(*class_def);
1799 if (class_data == nullptr) {
1800 os << "No class data.";
1801 return;
1802 }
1803
1804 ClassDataItemIterator it(*dex_file, class_data);
1805 // Skip fields
1806 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
1807 it.Next();
1808 }
1809
1810 // Find method, and dump it.
1811 while (it.HasNextDirectMethod() || it.HasNextVirtualMethod()) {
1812 uint32_t method_idx = it.GetMemberIndex();
1813 if (method_idx == dex_method_idx) {
1814 DumpMethodCFGImpl(dex_file, dex_method_idx, it.GetMethodCodeItem(), os);
1815 return;
1816 }
1817 it.Next();
1818 }
1819
1820 // Otherwise complain.
1821 os << "Something went wrong, didn't find the method in the class data.";
1822}
1823
Elliott Hughes42ee1422011-09-06 12:33:32 -07001824} // namespace art