Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 17 | #include "utils.h" |
| 18 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 19 | #include <pthread.h> |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 21 | #include <sys/syscall.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <unistd.h> |
| 24 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 25 | #include "UniquePtr.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 26 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 27 | #include "dex_file-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 28 | #include "mirror/art_field-inl.h" |
| 29 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 31 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
| 33 | #include "mirror/object_array-inl.h" |
| 34 | #include "mirror/string.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 35 | #include "object_utils.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 36 | #include "os.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 37 | #include "utf-inl.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 39 | #if !defined(HAVE_POSIX_CLOCKS) |
| 40 | #include <sys/time.h> |
| 41 | #endif |
| 42 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 43 | #if defined(HAVE_PRCTL) |
| 44 | #include <sys/prctl.h> |
| 45 | #endif |
| 46 | |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 47 | #if defined(__APPLE__) |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 48 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 49 | #include <sys/syscall.h> |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 50 | #endif |
| 51 | |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 52 | #include <backtrace/Backtrace.h> // For DumpNativeStack. |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 53 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 54 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 55 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 56 | #endif |
| 57 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 58 | namespace art { |
| 59 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 60 | pid_t GetTid() { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 61 | #if defined(__APPLE__) |
| 62 | uint64_t owner; |
| 63 | CHECK_PTHREAD_CALL(pthread_threadid_np, (NULL, &owner), __FUNCTION__); // Requires Mac OS 10.6 |
| 64 | return owner; |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 65 | #else |
| 66 | // Neither bionic nor glibc exposes gettid(2). |
| 67 | return syscall(__NR_gettid); |
| 68 | #endif |
| 69 | } |
| 70 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 71 | std::string GetThreadName(pid_t tid) { |
| 72 | std::string result; |
| 73 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 74 | result.resize(result.size() - 1); // Lose the trailing '\n'. |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 75 | } else { |
| 76 | result = "<unknown>"; |
| 77 | } |
| 78 | return result; |
| 79 | } |
| 80 | |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 81 | void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size) { |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 82 | #if defined(__APPLE__) |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 83 | *stack_size = pthread_get_stacksize_np(thread); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 84 | void* stack_addr = pthread_get_stackaddr_np(thread); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 85 | |
| 86 | // Check whether stack_addr is the base or end of the stack. |
| 87 | // (On Mac OS 10.7, it's the end.) |
| 88 | int stack_variable; |
| 89 | if (stack_addr > &stack_variable) { |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 90 | *stack_base = reinterpret_cast<byte*>(stack_addr) - *stack_size; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 91 | } else { |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 92 | *stack_base = stack_addr; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 93 | } |
| 94 | #else |
| 95 | pthread_attr_t attributes; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 96 | CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 97 | CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 98 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
| 99 | #endif |
| 100 | } |
| 101 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 102 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 103 | UniquePtr<File> file(new File); |
| 104 | if (!file->Open(file_name, O_RDONLY)) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 105 | return false; |
| 106 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 107 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 108 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 109 | while (true) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 110 | int64_t n = TEMP_FAILURE_RETRY(read(file->Fd(), &buf[0], buf.size())); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 111 | if (n == -1) { |
| 112 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 113 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 114 | if (n == 0) { |
| 115 | return true; |
| 116 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 117 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 118 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 121 | std::string GetIsoDate() { |
| 122 | time_t now = time(NULL); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 123 | tm tmbuf; |
| 124 | tm* ptm = localtime_r(&now, &tmbuf); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 125 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d", |
| 126 | ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, |
| 127 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 128 | } |
| 129 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 130 | uint64_t MilliTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 131 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 132 | timespec now; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 133 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 134 | return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_nsec / 1000000LL; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 135 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 136 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 137 | gettimeofday(&now, NULL); |
| 138 | return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_usec / 1000LL; |
| 139 | #endif |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 140 | } |
| 141 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 142 | uint64_t MicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 143 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 144 | timespec now; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 145 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 146 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_nsec / 1000LL; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 147 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 148 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 149 | gettimeofday(&now, NULL); |
TDYa127 | 5482503 | 2012-04-11 10:45:23 -0700 | [diff] [blame] | 150 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_usec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 151 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 154 | uint64_t NanoTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 155 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 156 | timespec now; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 157 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 158 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 159 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 160 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 161 | gettimeofday(&now, NULL); |
| 162 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL; |
| 163 | #endif |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 166 | uint64_t ThreadCpuNanoTime() { |
| 167 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 168 | timespec now; |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 169 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
| 170 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
| 171 | #else |
| 172 | UNIMPLEMENTED(WARNING); |
| 173 | return -1; |
| 174 | #endif |
| 175 | } |
| 176 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 177 | void NanoSleep(uint64_t ns) { |
| 178 | timespec tm; |
| 179 | tm.tv_sec = 0; |
| 180 | tm.tv_nsec = ns; |
| 181 | nanosleep(&tm, NULL); |
| 182 | } |
| 183 | |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 184 | void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) { |
| 185 | int64_t endSec; |
| 186 | |
| 187 | if (absolute) { |
| 188 | #if !defined(__APPLE__) |
| 189 | clock_gettime(clock, ts); |
| 190 | #else |
| 191 | UNUSED(clock); |
| 192 | timeval tv; |
| 193 | gettimeofday(&tv, NULL); |
| 194 | ts->tv_sec = tv.tv_sec; |
| 195 | ts->tv_nsec = tv.tv_usec * 1000; |
| 196 | #endif |
| 197 | } else { |
| 198 | ts->tv_sec = 0; |
| 199 | ts->tv_nsec = 0; |
| 200 | } |
| 201 | endSec = ts->tv_sec + ms / 1000; |
| 202 | if (UNLIKELY(endSec >= 0x7fffffff)) { |
| 203 | std::ostringstream ss; |
| 204 | LOG(INFO) << "Note: end time exceeds epoch: " << ss.str(); |
| 205 | endSec = 0x7ffffffe; |
| 206 | } |
| 207 | ts->tv_sec = endSec; |
| 208 | ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns; |
| 209 | |
| 210 | // Catch rollover. |
| 211 | if (ts->tv_nsec >= 1000000000L) { |
| 212 | ts->tv_sec++; |
| 213 | ts->tv_nsec -= 1000000000L; |
| 214 | } |
| 215 | } |
| 216 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 217 | std::string PrettyDescriptor(const mirror::String* java_descriptor) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 218 | if (java_descriptor == NULL) { |
| 219 | return "null"; |
| 220 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 221 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8()); |
| 222 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 223 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 224 | std::string PrettyDescriptor(const mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 225 | if (klass == NULL) { |
| 226 | return "null"; |
| 227 | } |
| 228 | return PrettyDescriptor(ClassHelper(klass).GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 231 | std::string PrettyDescriptor(const std::string& descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 232 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 233 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 234 | size_t dim = 0; |
| 235 | while (*c == '[') { |
| 236 | dim++; |
| 237 | c++; |
| 238 | } |
| 239 | |
| 240 | // Reference or primitive? |
| 241 | if (*c == 'L') { |
| 242 | // "[[La/b/C;" -> "a.b.C[][]". |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 243 | c++; // Skip the 'L'. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 244 | } else { |
| 245 | // "[[B" -> "byte[][]". |
| 246 | // To make life easier, we make primitives look like unqualified |
| 247 | // reference types. |
| 248 | switch (*c) { |
| 249 | case 'B': c = "byte;"; break; |
| 250 | case 'C': c = "char;"; break; |
| 251 | case 'D': c = "double;"; break; |
| 252 | case 'F': c = "float;"; break; |
| 253 | case 'I': c = "int;"; break; |
| 254 | case 'J': c = "long;"; break; |
| 255 | case 'S': c = "short;"; break; |
| 256 | case 'Z': c = "boolean;"; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 257 | case 'V': c = "void;"; break; // Used when decoding return types. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 258 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 263 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 264 | std::string result; |
| 265 | const char* p = c; |
| 266 | while (*p != ';') { |
| 267 | char ch = *p++; |
| 268 | if (ch == '/') { |
| 269 | ch = '.'; |
| 270 | } |
| 271 | result.push_back(ch); |
| 272 | } |
| 273 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 274 | while (dim--) { |
| 275 | result += "[]"; |
| 276 | } |
| 277 | return result; |
| 278 | } |
| 279 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 280 | std::string PrettyDescriptor(Primitive::Type type) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 281 | std::string descriptor_string(Primitive::Descriptor(type)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 282 | return PrettyDescriptor(descriptor_string); |
| 283 | } |
| 284 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 285 | std::string PrettyField(const mirror::ArtField* f, bool with_type) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 286 | if (f == NULL) { |
| 287 | return "null"; |
| 288 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 289 | FieldHelper fh(f); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 290 | std::string result; |
| 291 | if (with_type) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 292 | result += PrettyDescriptor(fh.GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 293 | result += ' '; |
| 294 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 295 | result += PrettyDescriptor(fh.GetDeclaringClassDescriptor()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 296 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 297 | result += fh.GetName(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 298 | return result; |
| 299 | } |
| 300 | |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 301 | std::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 302 | if (field_idx >= dex_file.NumFieldIds()) { |
| 303 | return StringPrintf("<<invalid-field-idx-%d>>", field_idx); |
| 304 | } |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 305 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 306 | std::string result; |
| 307 | if (with_type) { |
| 308 | result += dex_file.GetFieldTypeDescriptor(field_id); |
| 309 | result += ' '; |
| 310 | } |
| 311 | result += PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(field_id)); |
| 312 | result += '.'; |
| 313 | result += dex_file.GetFieldName(field_id); |
| 314 | return result; |
| 315 | } |
| 316 | |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 317 | std::string PrettyType(uint32_t type_idx, const DexFile& dex_file) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 318 | if (type_idx >= dex_file.NumTypeIds()) { |
| 319 | return StringPrintf("<<invalid-type-idx-%d>>", type_idx); |
| 320 | } |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 321 | const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx); |
Mathieu Chartier | 4c70d77 | 2012-09-10 14:08:32 -0700 | [diff] [blame] | 322 | return PrettyDescriptor(dex_file.GetTypeDescriptor(type_id)); |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 325 | std::string PrettyArguments(const char* signature) { |
| 326 | std::string result; |
| 327 | result += '('; |
| 328 | CHECK_EQ(*signature, '('); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 329 | ++signature; // Skip the '('. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 330 | while (*signature != ')') { |
| 331 | size_t argument_length = 0; |
| 332 | while (signature[argument_length] == '[') { |
| 333 | ++argument_length; |
| 334 | } |
| 335 | if (signature[argument_length] == 'L') { |
| 336 | argument_length = (strchr(signature, ';') - signature + 1); |
| 337 | } else { |
| 338 | ++argument_length; |
| 339 | } |
| 340 | std::string argument_descriptor(signature, argument_length); |
| 341 | result += PrettyDescriptor(argument_descriptor); |
| 342 | if (signature[argument_length] != ')') { |
| 343 | result += ", "; |
| 344 | } |
| 345 | signature += argument_length; |
| 346 | } |
| 347 | CHECK_EQ(*signature, ')'); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 348 | ++signature; // Skip the ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 349 | result += ')'; |
| 350 | return result; |
| 351 | } |
| 352 | |
| 353 | std::string PrettyReturnType(const char* signature) { |
| 354 | const char* return_type = strchr(signature, ')'); |
| 355 | CHECK(return_type != NULL); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 356 | ++return_type; // Skip ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 357 | return PrettyDescriptor(return_type); |
| 358 | } |
| 359 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 360 | std::string PrettyMethod(const mirror::ArtMethod* m, bool with_signature) { |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 361 | if (m == NULL) { |
| 362 | return "null"; |
| 363 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 364 | MethodHelper mh(m); |
| 365 | std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 366 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 367 | result += mh.GetName(); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 368 | if (with_signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 369 | const Signature signature = mh.GetSignature(); |
| 370 | std::string sig_as_string(signature.ToString()); |
| 371 | if (signature == Signature::NoSignature()) { |
| 372 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 373 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 374 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 375 | PrettyArguments(sig_as_string.c_str()); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 376 | } |
| 377 | return result; |
| 378 | } |
| 379 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 380 | std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 381 | if (method_idx >= dex_file.NumMethodIds()) { |
| 382 | return StringPrintf("<<invalid-method-idx-%d>>", method_idx); |
| 383 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 384 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 385 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 386 | result += '.'; |
| 387 | result += dex_file.GetMethodName(method_id); |
| 388 | if (with_signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 389 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 390 | std::string sig_as_string(signature.ToString()); |
| 391 | if (signature == Signature::NoSignature()) { |
| 392 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 393 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 394 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 395 | PrettyArguments(sig_as_string.c_str()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 396 | } |
| 397 | return result; |
| 398 | } |
| 399 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 400 | std::string PrettyTypeOf(const mirror::Object* obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 401 | if (obj == NULL) { |
| 402 | return "null"; |
| 403 | } |
| 404 | if (obj->GetClass() == NULL) { |
| 405 | return "(raw)"; |
| 406 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 407 | ClassHelper kh(obj->GetClass()); |
| 408 | std::string result(PrettyDescriptor(kh.GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 409 | if (obj->IsClass()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 410 | kh.ChangeClass(obj->AsClass()); |
| 411 | result += "<" + PrettyDescriptor(kh.GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 412 | } |
| 413 | return result; |
| 414 | } |
| 415 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 416 | std::string PrettyClass(const mirror::Class* c) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 417 | if (c == NULL) { |
| 418 | return "null"; |
| 419 | } |
| 420 | std::string result; |
| 421 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 422 | result += PrettyDescriptor(c); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 423 | result += ">"; |
| 424 | return result; |
| 425 | } |
| 426 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 427 | std::string PrettyClassAndClassLoader(const mirror::Class* c) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 428 | if (c == NULL) { |
| 429 | return "null"; |
| 430 | } |
| 431 | std::string result; |
| 432 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 433 | result += PrettyDescriptor(c); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 434 | result += ","; |
| 435 | result += PrettyTypeOf(c->GetClassLoader()); |
| 436 | // TODO: add an identifying hash value for the loader |
| 437 | result += ">"; |
| 438 | return result; |
| 439 | } |
| 440 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 441 | std::string PrettySize(size_t byte_count) { |
| 442 | // The byte thresholds at which we display amounts. A byte count is displayed |
| 443 | // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. |
| 444 | static const size_t kUnitThresholds[] = { |
| 445 | 0, // B up to... |
| 446 | 3*1024, // KB up to... |
| 447 | 2*1024*1024, // MB up to... |
| 448 | 1024*1024*1024 // GB from here. |
| 449 | }; |
| 450 | static const size_t kBytesPerUnit[] = { 1, KB, MB, GB }; |
| 451 | static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" }; |
| 452 | |
| 453 | int i = arraysize(kUnitThresholds); |
| 454 | while (--i > 0) { |
| 455 | if (byte_count >= kUnitThresholds[i]) { |
| 456 | break; |
| 457 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 458 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 459 | |
| 460 | return StringPrintf("%zd%s", byte_count / kBytesPerUnit[i], kUnitStrings[i]); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | std::string PrettyDuration(uint64_t nano_duration) { |
| 464 | if (nano_duration == 0) { |
| 465 | return "0"; |
| 466 | } else { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 467 | return FormatDuration(nano_duration, GetAppropriateTimeUnit(nano_duration)); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration) { |
| 472 | const uint64_t one_sec = 1000 * 1000 * 1000; |
| 473 | const uint64_t one_ms = 1000 * 1000; |
| 474 | const uint64_t one_us = 1000; |
| 475 | if (nano_duration >= one_sec) { |
| 476 | return kTimeUnitSecond; |
| 477 | } else if (nano_duration >= one_ms) { |
| 478 | return kTimeUnitMillisecond; |
| 479 | } else if (nano_duration >= one_us) { |
| 480 | return kTimeUnitMicrosecond; |
| 481 | } else { |
| 482 | return kTimeUnitNanosecond; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit) { |
| 487 | const uint64_t one_sec = 1000 * 1000 * 1000; |
| 488 | const uint64_t one_ms = 1000 * 1000; |
| 489 | const uint64_t one_us = 1000; |
| 490 | |
| 491 | switch (time_unit) { |
| 492 | case kTimeUnitSecond: |
| 493 | return one_sec; |
| 494 | case kTimeUnitMillisecond: |
| 495 | return one_ms; |
| 496 | case kTimeUnitMicrosecond: |
| 497 | return one_us; |
| 498 | case kTimeUnitNanosecond: |
| 499 | return 1; |
| 500 | } |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit) { |
| 505 | const char* unit = NULL; |
| 506 | uint64_t divisor = GetNsToTimeUnitDivisor(time_unit); |
| 507 | uint32_t zero_fill = 1; |
| 508 | switch (time_unit) { |
| 509 | case kTimeUnitSecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 510 | unit = "s"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 511 | zero_fill = 9; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 512 | break; |
| 513 | case kTimeUnitMillisecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 514 | unit = "ms"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 515 | zero_fill = 6; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 516 | break; |
| 517 | case kTimeUnitMicrosecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 518 | unit = "us"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 519 | zero_fill = 3; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 520 | break; |
| 521 | case kTimeUnitNanosecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 522 | unit = "ns"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 523 | zero_fill = 0; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 524 | break; |
| 525 | } |
| 526 | |
| 527 | uint64_t whole_part = nano_duration / divisor; |
| 528 | uint64_t fractional_part = nano_duration % divisor; |
| 529 | if (fractional_part == 0) { |
| 530 | return StringPrintf("%llu%s", whole_part, unit); |
| 531 | } else { |
| 532 | while ((fractional_part % 1000) == 0) { |
| 533 | zero_fill -= 3; |
| 534 | fractional_part /= 1000; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 535 | } |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 536 | if (zero_fill == 3) { |
| 537 | return StringPrintf("%llu.%03llu%s", whole_part, fractional_part, unit); |
| 538 | } else if (zero_fill == 6) { |
| 539 | return StringPrintf("%llu.%06llu%s", whole_part, fractional_part, unit); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 540 | } else { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 541 | return StringPrintf("%llu.%09llu%s", whole_part, fractional_part, unit); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 546 | std::string PrintableString(const std::string& utf) { |
| 547 | std::string result; |
| 548 | result += '"'; |
| 549 | const char* p = utf.c_str(); |
| 550 | size_t char_count = CountModifiedUtf8Chars(p); |
| 551 | for (size_t i = 0; i < char_count; ++i) { |
| 552 | uint16_t ch = GetUtf16FromUtf8(&p); |
| 553 | if (ch == '\\') { |
| 554 | result += "\\\\"; |
| 555 | } else if (ch == '\n') { |
| 556 | result += "\\n"; |
| 557 | } else if (ch == '\r') { |
| 558 | result += "\\r"; |
| 559 | } else if (ch == '\t') { |
| 560 | result += "\\t"; |
| 561 | } else if (NeedsEscaping(ch)) { |
| 562 | StringAppendF(&result, "\\u%04x", ch); |
| 563 | } else { |
| 564 | result += ch; |
| 565 | } |
| 566 | } |
| 567 | result += '"'; |
| 568 | return result; |
| 569 | } |
| 570 | |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 571 | // See http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp615 for the full rules. |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 572 | std::string MangleForJni(const std::string& s) { |
| 573 | std::string result; |
| 574 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 575 | const char* cp = &s[0]; |
| 576 | for (size_t i = 0; i < char_count; ++i) { |
| 577 | uint16_t ch = GetUtf16FromUtf8(&cp); |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 578 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) { |
| 579 | result.push_back(ch); |
| 580 | } else if (ch == '.' || ch == '/') { |
| 581 | result += "_"; |
| 582 | } else if (ch == '_') { |
| 583 | result += "_1"; |
| 584 | } else if (ch == ';') { |
| 585 | result += "_2"; |
| 586 | } else if (ch == '[') { |
| 587 | result += "_3"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 588 | } else { |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 589 | StringAppendF(&result, "_0%04x", ch); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | return result; |
| 593 | } |
| 594 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 595 | std::string DotToDescriptor(const char* class_name) { |
| 596 | std::string descriptor(class_name); |
| 597 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 598 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 599 | descriptor = "L" + descriptor + ";"; |
| 600 | } |
| 601 | return descriptor; |
| 602 | } |
| 603 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 604 | std::string DescriptorToDot(const char* descriptor) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 605 | size_t length = strlen(descriptor); |
| 606 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 607 | std::string result(descriptor + 1, length - 2); |
| 608 | std::replace(result.begin(), result.end(), '/', '.'); |
| 609 | return result; |
| 610 | } |
| 611 | return descriptor; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | std::string DescriptorToName(const char* descriptor) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 615 | size_t length = strlen(descriptor); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 616 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 617 | std::string result(descriptor + 1, length - 2); |
| 618 | return result; |
| 619 | } |
| 620 | return descriptor; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 623 | std::string JniShortName(const mirror::ArtMethod* m) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 624 | MethodHelper mh(m); |
| 625 | std::string class_name(mh.GetDeclaringClassDescriptor()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 626 | // Remove the leading 'L' and trailing ';'... |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 627 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 628 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 629 | class_name.erase(0, 1); |
| 630 | class_name.erase(class_name.size() - 1, 1); |
| 631 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 632 | std::string method_name(mh.GetName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 633 | |
| 634 | std::string short_name; |
| 635 | short_name += "Java_"; |
| 636 | short_name += MangleForJni(class_name); |
| 637 | short_name += "_"; |
| 638 | short_name += MangleForJni(method_name); |
| 639 | return short_name; |
| 640 | } |
| 641 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 642 | std::string JniLongName(const mirror::ArtMethod* m) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 643 | std::string long_name; |
| 644 | long_name += JniShortName(m); |
| 645 | long_name += "__"; |
| 646 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 647 | std::string signature(MethodHelper(m).GetSignature().ToString()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 648 | signature.erase(0, 1); |
| 649 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 650 | |
| 651 | long_name += MangleForJni(signature); |
| 652 | |
| 653 | return long_name; |
| 654 | } |
| 655 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 656 | // Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 657 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 658 | 0x00000000, // 00..1f low control characters; nothing valid |
| 659 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 660 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 661 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 664 | // Helper for IsValidPartOfMemberNameUtf8(); do not call directly. |
| 665 | bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 666 | /* |
| 667 | * It's a multibyte encoded character. Decode it and analyze. We |
| 668 | * accept anything that isn't (a) an improperly encoded low value, |
| 669 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 670 | * control character, or (e) a high space, layout, or special |
| 671 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 672 | * U+fff0..U+ffff). This is all specified in the dex format |
| 673 | * document. |
| 674 | */ |
| 675 | |
| 676 | uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 677 | |
| 678 | // Perform follow-up tests based on the high 8 bits. |
| 679 | switch (utf16 >> 8) { |
| 680 | case 0x00: |
| 681 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 682 | return (utf16 > 0x00a0); |
| 683 | case 0xd8: |
| 684 | case 0xd9: |
| 685 | case 0xda: |
| 686 | case 0xdb: |
| 687 | // It's a leading surrogate. Check to see that a trailing |
| 688 | // surrogate follows. |
| 689 | utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 690 | return (utf16 >= 0xdc00) && (utf16 <= 0xdfff); |
| 691 | case 0xdc: |
| 692 | case 0xdd: |
| 693 | case 0xde: |
| 694 | case 0xdf: |
| 695 | // It's a trailing surrogate, which is not valid at this point. |
| 696 | return false; |
| 697 | case 0x20: |
| 698 | case 0xff: |
| 699 | // It's in the range that has spaces, controls, and specials. |
| 700 | switch (utf16 & 0xfff8) { |
| 701 | case 0x2000: |
| 702 | case 0x2008: |
| 703 | case 0x2028: |
| 704 | case 0xfff0: |
| 705 | case 0xfff8: |
| 706 | return false; |
| 707 | } |
| 708 | break; |
| 709 | } |
| 710 | return true; |
| 711 | } |
| 712 | |
| 713 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 714 | * valid as part of a member name, updating the pointer to point past |
| 715 | * the consumed character. This will consume two encoded UTF-16 code |
| 716 | * points if the character is encoded as a surrogate pair. Also, if |
| 717 | * this function returns false, then the given pointer may only have |
| 718 | * been partially advanced. |
| 719 | */ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 720 | static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 721 | uint8_t c = (uint8_t) **pUtf8Ptr; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 722 | if (LIKELY(c <= 0x7f)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 723 | // It's low-ascii, so check the table. |
| 724 | uint32_t wordIdx = c >> 5; |
| 725 | uint32_t bitIdx = c & 0x1f; |
| 726 | (*pUtf8Ptr)++; |
| 727 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 728 | } |
| 729 | |
| 730 | // It's a multibyte encoded character. Call a non-inline function |
| 731 | // for the heavy lifting. |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 732 | return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr); |
| 733 | } |
| 734 | |
| 735 | bool IsValidMemberName(const char* s) { |
| 736 | bool angle_name = false; |
| 737 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 738 | switch (*s) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 739 | case '\0': |
| 740 | // The empty string is not a valid name. |
| 741 | return false; |
| 742 | case '<': |
| 743 | angle_name = true; |
| 744 | s++; |
| 745 | break; |
| 746 | } |
| 747 | |
| 748 | while (true) { |
| 749 | switch (*s) { |
| 750 | case '\0': |
| 751 | return !angle_name; |
| 752 | case '>': |
| 753 | return angle_name && s[1] == '\0'; |
| 754 | } |
| 755 | |
| 756 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
| 757 | return false; |
| 758 | } |
| 759 | } |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 762 | enum ClassNameType { kName, kDescriptor }; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 763 | static bool IsValidClassName(const char* s, ClassNameType type, char separator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 764 | int arrayCount = 0; |
| 765 | while (*s == '[') { |
| 766 | arrayCount++; |
| 767 | s++; |
| 768 | } |
| 769 | |
| 770 | if (arrayCount > 255) { |
| 771 | // Arrays may have no more than 255 dimensions. |
| 772 | return false; |
| 773 | } |
| 774 | |
| 775 | if (arrayCount != 0) { |
| 776 | /* |
| 777 | * If we're looking at an array of some sort, then it doesn't |
| 778 | * matter if what is being asked for is a class name; the |
| 779 | * format looks the same as a type descriptor in that case, so |
| 780 | * treat it as such. |
| 781 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 782 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 785 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 786 | /* |
| 787 | * We are looking for a descriptor. Either validate it as a |
| 788 | * single-character primitive type, or continue on to check the |
| 789 | * embedded class name (bracketed by "L" and ";"). |
| 790 | */ |
| 791 | switch (*(s++)) { |
| 792 | case 'B': |
| 793 | case 'C': |
| 794 | case 'D': |
| 795 | case 'F': |
| 796 | case 'I': |
| 797 | case 'J': |
| 798 | case 'S': |
| 799 | case 'Z': |
| 800 | // These are all single-character descriptors for primitive types. |
| 801 | return (*s == '\0'); |
| 802 | case 'V': |
| 803 | // Non-array void is valid, but you can't have an array of void. |
| 804 | return (arrayCount == 0) && (*s == '\0'); |
| 805 | case 'L': |
| 806 | // Class name: Break out and continue below. |
| 807 | break; |
| 808 | default: |
| 809 | // Oddball descriptor character. |
| 810 | return false; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* |
| 815 | * We just consumed the 'L' that introduces a class name as part |
| 816 | * of a type descriptor, or we are looking for an unadorned class |
| 817 | * name. |
| 818 | */ |
| 819 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 820 | bool sepOrFirst = true; // first character or just encountered a separator. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 821 | for (;;) { |
| 822 | uint8_t c = (uint8_t) *s; |
| 823 | switch (c) { |
| 824 | case '\0': |
| 825 | /* |
| 826 | * Premature end for a type descriptor, but valid for |
| 827 | * a class name as long as we haven't encountered an |
| 828 | * empty component (including the degenerate case of |
| 829 | * the empty string ""). |
| 830 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 831 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 832 | case ';': |
| 833 | /* |
| 834 | * Invalid character for a class name, but the |
| 835 | * legitimate end of a type descriptor. In the latter |
| 836 | * case, make sure that this is the end of the string |
| 837 | * and that it doesn't end with an empty component |
| 838 | * (including the degenerate case of "L;"). |
| 839 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 840 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 841 | case '/': |
| 842 | case '.': |
| 843 | if (c != separator) { |
| 844 | // The wrong separator character. |
| 845 | return false; |
| 846 | } |
| 847 | if (sepOrFirst) { |
| 848 | // Separator at start or two separators in a row. |
| 849 | return false; |
| 850 | } |
| 851 | sepOrFirst = true; |
| 852 | s++; |
| 853 | break; |
| 854 | default: |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 855 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 856 | return false; |
| 857 | } |
| 858 | sepOrFirst = false; |
| 859 | break; |
| 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 864 | bool IsValidBinaryClassName(const char* s) { |
| 865 | return IsValidClassName(s, kName, '.'); |
| 866 | } |
| 867 | |
| 868 | bool IsValidJniClassName(const char* s) { |
| 869 | return IsValidClassName(s, kName, '/'); |
| 870 | } |
| 871 | |
| 872 | bool IsValidDescriptor(const char* s) { |
| 873 | return IsValidClassName(s, kDescriptor, '/'); |
| 874 | } |
| 875 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 876 | void Split(const std::string& s, char separator, std::vector<std::string>& result) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 877 | const char* p = s.data(); |
| 878 | const char* end = p + s.size(); |
| 879 | while (p != end) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 880 | if (*p == separator) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 881 | ++p; |
| 882 | } else { |
| 883 | const char* start = p; |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 884 | while (++p != end && *p != separator) { |
| 885 | // Skip to the next occurrence of the separator. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 886 | } |
| 887 | result.push_back(std::string(start, p - start)); |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 892 | template <typename StringT> |
| 893 | std::string Join(std::vector<StringT>& strings, char separator) { |
| 894 | if (strings.empty()) { |
| 895 | return ""; |
| 896 | } |
| 897 | |
| 898 | std::string result(strings[0]); |
| 899 | for (size_t i = 1; i < strings.size(); ++i) { |
| 900 | result += separator; |
| 901 | result += strings[i]; |
| 902 | } |
| 903 | return result; |
| 904 | } |
| 905 | |
| 906 | // Explicit instantiations. |
| 907 | template std::string Join<std::string>(std::vector<std::string>& strings, char separator); |
| 908 | template std::string Join<const char*>(std::vector<const char*>& strings, char separator); |
| 909 | template std::string Join<char*>(std::vector<char*>& strings, char separator); |
| 910 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 911 | bool StartsWith(const std::string& s, const char* prefix) { |
| 912 | return s.compare(0, strlen(prefix), prefix) == 0; |
| 913 | } |
| 914 | |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 915 | bool EndsWith(const std::string& s, const char* suffix) { |
| 916 | size_t suffix_length = strlen(suffix); |
| 917 | size_t string_length = s.size(); |
| 918 | if (suffix_length > string_length) { |
| 919 | return false; |
| 920 | } |
| 921 | size_t offset = string_length - suffix_length; |
| 922 | return s.compare(offset, suffix_length, suffix) == 0; |
| 923 | } |
| 924 | |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 925 | void SetThreadName(const char* thread_name) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 926 | int hasAt = 0; |
| 927 | int hasDot = 0; |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 928 | const char* s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 929 | while (*s) { |
| 930 | if (*s == '.') { |
| 931 | hasDot = 1; |
| 932 | } else if (*s == '@') { |
| 933 | hasAt = 1; |
| 934 | } |
| 935 | s++; |
| 936 | } |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 937 | int len = s - thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 938 | if (len < 15 || hasAt || !hasDot) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 939 | s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 940 | } else { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 941 | s = thread_name + len - 15; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 942 | } |
| 943 | #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP) |
Elliott Hughes | 7c6a61e | 2012-03-12 18:01:41 -0700 | [diff] [blame] | 944 | // pthread_setname_np fails rather than truncating long strings. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 945 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 946 | strncpy(buf, s, sizeof(buf)-1); |
| 947 | buf[sizeof(buf)-1] = '\0'; |
| 948 | errno = pthread_setname_np(pthread_self(), buf); |
| 949 | if (errno != 0) { |
| 950 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 951 | } |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 952 | #elif defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 953 | pthread_setname_np(thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 954 | #elif defined(HAVE_PRCTL) |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 955 | prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0); // NOLINT (unsigned long) |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 956 | #else |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 957 | UNIMPLEMENTED(WARNING) << thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 958 | #endif |
| 959 | } |
| 960 | |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 961 | void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) { |
| 962 | *utime = *stime = *task_cpu = 0; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 963 | std::string stats; |
Elliott Hughes | 8a31b50 | 2012-04-30 19:36:11 -0700 | [diff] [blame] | 964 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 965 | return; |
| 966 | } |
| 967 | // Skip the command, which may contain spaces. |
| 968 | stats = stats.substr(stats.find(')') + 2); |
| 969 | // Extract the three fields we care about. |
| 970 | std::vector<std::string> fields; |
| 971 | Split(stats, ' ', fields); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 972 | *state = fields[0][0]; |
| 973 | *utime = strtoull(fields[11].c_str(), NULL, 10); |
| 974 | *stime = strtoull(fields[12].c_str(), NULL, 10); |
| 975 | *task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 978 | std::string GetSchedulerGroupName(pid_t tid) { |
| 979 | // /proc/<pid>/cgroup looks like this: |
| 980 | // 2:devices:/ |
| 981 | // 1:cpuacct,cpu:/ |
| 982 | // We want the third field from the line whose second field contains the "cpu" token. |
| 983 | std::string cgroup_file; |
| 984 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) { |
| 985 | return ""; |
| 986 | } |
| 987 | std::vector<std::string> cgroup_lines; |
| 988 | Split(cgroup_file, '\n', cgroup_lines); |
| 989 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 990 | std::vector<std::string> cgroup_fields; |
| 991 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 992 | std::vector<std::string> cgroups; |
| 993 | Split(cgroup_fields[1], ',', cgroups); |
| 994 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 995 | if (cgroups[i] == "cpu") { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 996 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | } |
| 1000 | return ""; |
| 1001 | } |
| 1002 | |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1003 | static const char* CleanMapName(const char* map_name) { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1004 | if (map_name == NULL) { |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1005 | return "???"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1006 | } |
| 1007 | // Turn "/usr/local/google/home/enh/clean-dalvik-dev/out/host/linux-x86/lib/libartd.so" |
| 1008 | // into "libartd.so". |
| 1009 | const char* last_slash = strrchr(map_name, '/'); |
| 1010 | if (last_slash != NULL) { |
| 1011 | map_name = last_slash + 1; |
| 1012 | } |
| 1013 | return map_name; |
| 1014 | } |
| 1015 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1016 | void DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) { |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1017 | UniquePtr<Backtrace> backtrace(Backtrace::Create(-1, tid)); |
| 1018 | if (!backtrace->Unwind(0)) { |
| 1019 | os << prefix << "(backtrace::Unwind failed for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1020 | return; |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1021 | } else if (backtrace->NumFrames() == 0) { |
Elliott Hughes | 225f5a1 | 2012-06-11 11:23:48 -0700 | [diff] [blame] | 1022 | os << prefix << "(no native stack frames for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1023 | return; |
| 1024 | } |
| 1025 | |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1026 | for (size_t i = 0; i < backtrace->NumFrames(); ++i) { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1027 | // We produce output like this: |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1028 | // ] #00 unwind_backtrace_thread+536 [0x55d75bb8] (libbacktrace.so) |
| 1029 | const backtrace_frame_data_t* frame = backtrace->GetFrame(i); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1030 | |
| 1031 | os << prefix; |
| 1032 | if (include_count) { |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1033 | os << StringPrintf("#%02zu ", i); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1034 | } |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1035 | if (frame->func_name) { |
| 1036 | os << frame->func_name; |
| 1037 | } else { |
| 1038 | os << "???"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1039 | } |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1040 | if (frame->func_offset != 0) { |
| 1041 | os << "+" << frame->func_offset; |
| 1042 | } |
| 1043 | os << StringPrintf(" [%p]", reinterpret_cast<void*>(frame->pc)); |
| 1044 | os << " (" << CleanMapName(frame->map_name) << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1045 | } |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1048 | #if defined(__APPLE__) |
| 1049 | |
| 1050 | // TODO: is there any way to get the kernel stack on Mac OS? |
| 1051 | void DumpKernelStack(std::ostream&, pid_t, const char*, bool) {} |
| 1052 | |
| 1053 | #else |
| 1054 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1055 | void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) { |
Elliott Hughes | 12a9502 | 2012-05-24 21:41:38 -0700 | [diff] [blame] | 1056 | if (tid == GetTid()) { |
| 1057 | // There's no point showing that we're reading our stack out of /proc! |
| 1058 | return; |
| 1059 | } |
| 1060 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1061 | std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid)); |
| 1062 | std::string kernel_stack; |
| 1063 | if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) { |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1064 | os << prefix << "(couldn't read " << kernel_stack_filename << ")\n"; |
jeffhao | c4c3ee2 | 2012-05-25 16:16:32 -0700 | [diff] [blame] | 1065 | return; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | std::vector<std::string> kernel_stack_frames; |
| 1069 | Split(kernel_stack, '\n', kernel_stack_frames); |
| 1070 | // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff", |
| 1071 | // which looking at the source appears to be the kernel's way of saying "that's all, folks!". |
| 1072 | kernel_stack_frames.pop_back(); |
| 1073 | for (size_t i = 0; i < kernel_stack_frames.size(); ++i) { |
| 1074 | // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110" into "futex_wait_queue_me+0xcd/0x110". |
| 1075 | const char* text = kernel_stack_frames[i].c_str(); |
| 1076 | const char* close_bracket = strchr(text, ']'); |
| 1077 | if (close_bracket != NULL) { |
| 1078 | text = close_bracket + 2; |
| 1079 | } |
| 1080 | os << prefix; |
| 1081 | if (include_count) { |
| 1082 | os << StringPrintf("#%02zd ", i); |
| 1083 | } |
| 1084 | os << text << "\n"; |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | #endif |
| 1089 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1090 | const char* GetAndroidRoot() { |
| 1091 | const char* android_root = getenv("ANDROID_ROOT"); |
| 1092 | if (android_root == NULL) { |
| 1093 | if (OS::DirectoryExists("/system")) { |
| 1094 | android_root = "/system"; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1095 | } else { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1096 | LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist"; |
| 1097 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1098 | } |
| 1099 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1100 | if (!OS::DirectoryExists(android_root)) { |
| 1101 | LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1102 | return ""; |
| 1103 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1104 | return android_root; |
| 1105 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1106 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1107 | const char* GetAndroidData() { |
| 1108 | const char* android_data = getenv("ANDROID_DATA"); |
| 1109 | if (android_data == NULL) { |
| 1110 | if (OS::DirectoryExists("/data")) { |
| 1111 | android_data = "/data"; |
| 1112 | } else { |
| 1113 | LOG(FATAL) << "ANDROID_DATA not set and /data does not exist"; |
| 1114 | return ""; |
| 1115 | } |
| 1116 | } |
| 1117 | if (!OS::DirectoryExists(android_data)) { |
| 1118 | LOG(FATAL) << "Failed to find ANDROID_DATA directory " << android_data; |
| 1119 | return ""; |
| 1120 | } |
| 1121 | return android_data; |
| 1122 | } |
| 1123 | |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1124 | std::string GetDalvikCacheOrDie(const char* android_data) { |
| 1125 | std::string dalvik_cache(StringPrintf("%s/dalvik-cache", android_data)); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1126 | |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1127 | if (!OS::DirectoryExists(dalvik_cache.c_str())) { |
| 1128 | if (StartsWith(dalvik_cache, "/tmp/")) { |
| 1129 | int result = mkdir(dalvik_cache.c_str(), 0700); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1130 | if (result != 0) { |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1131 | LOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1132 | return ""; |
| 1133 | } |
| 1134 | } else { |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1135 | LOG(FATAL) << "Failed to find dalvik-cache directory " << dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1136 | return ""; |
| 1137 | } |
| 1138 | } |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1139 | return dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1142 | std::string GetDalvikCacheFilenameOrDie(const char* location) { |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1143 | std::string dalvik_cache(GetDalvikCacheOrDie(GetAndroidData())); |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 1144 | if (location[0] != '/') { |
| 1145 | LOG(FATAL) << "Expected path in location to be absolute: "<< location; |
| 1146 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1147 | std::string cache_file(&location[1]); // skip leading slash |
Brian Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 1148 | if (!EndsWith(location, ".dex") && !EndsWith(location, ".art")) { |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 1149 | cache_file += "/"; |
| 1150 | cache_file += DexFile::kClassesDex; |
| 1151 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1152 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1153 | return dalvik_cache + "/" + cache_file; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1156 | bool IsZipMagic(uint32_t magic) { |
| 1157 | return (('P' == ((magic >> 0) & 0xff)) && |
| 1158 | ('K' == ((magic >> 8) & 0xff))); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1161 | bool IsDexMagic(uint32_t magic) { |
| 1162 | return DexFile::IsMagicValid(reinterpret_cast<const byte*>(&magic)); |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1165 | bool IsOatMagic(uint32_t magic) { |
| 1166 | return (memcmp(reinterpret_cast<const byte*>(magic), |
| 1167 | OatHeader::kOatMagic, |
| 1168 | sizeof(OatHeader::kOatMagic)) == 0); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 1171 | } // namespace art |