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