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 | 06e3ad4 | 2012-02-07 14:51:57 -0800 | [diff] [blame] | 19 | #include <dynamic_annotations.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 | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "mirror/abstract_method-inl.h" |
| 29 | #include "mirror/class.h" |
| 30 | #include "mirror/class_loader.h" |
| 31 | #include "mirror/field.h" |
| 32 | #include "mirror/field-inl.h" |
| 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 | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | #include "utf.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__) |
Elliott Hughes | b08e8a3 | 2012-04-02 10:51:41 -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 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 53 | #include <corkscrew/backtrace.h> // For DumpNativeStack. |
| 54 | #include <corkscrew/demangle.h> // For DumpNativeStack. |
| 55 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 56 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 57 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 58 | #endif |
| 59 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 60 | namespace art { |
| 61 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 62 | pid_t GetTid() { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 63 | #if defined(__APPLE__) |
| 64 | uint64_t owner; |
| 65 | CHECK_PTHREAD_CALL(pthread_threadid_np, (NULL, &owner), __FUNCTION__); // Requires Mac OS 10.6 |
| 66 | return owner; |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 67 | #else |
| 68 | // Neither bionic nor glibc exposes gettid(2). |
| 69 | return syscall(__NR_gettid); |
| 70 | #endif |
| 71 | } |
| 72 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 73 | std::string GetThreadName(pid_t tid) { |
| 74 | std::string result; |
| 75 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) { |
| 76 | result.resize(result.size() - 1); // Lose the trailing '\n'. |
| 77 | } else { |
| 78 | result = "<unknown>"; |
| 79 | } |
| 80 | return result; |
| 81 | } |
| 82 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 83 | void GetThreadStack(pthread_t thread, void*& stack_base, size_t& stack_size) { |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 84 | #if defined(__APPLE__) |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 85 | stack_size = pthread_get_stacksize_np(thread); |
| 86 | void* stack_addr = pthread_get_stackaddr_np(thread); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 87 | |
| 88 | // Check whether stack_addr is the base or end of the stack. |
| 89 | // (On Mac OS 10.7, it's the end.) |
| 90 | int stack_variable; |
| 91 | if (stack_addr > &stack_variable) { |
| 92 | stack_base = reinterpret_cast<byte*>(stack_addr) - stack_size; |
| 93 | } else { |
| 94 | stack_base = stack_addr; |
| 95 | } |
| 96 | #else |
| 97 | pthread_attr_t attributes; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 98 | CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 99 | CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, &stack_base, &stack_size), __FUNCTION__); |
| 100 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
| 101 | #endif |
| 102 | } |
| 103 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 104 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 105 | UniquePtr<File> file(new File); |
| 106 | if (!file->Open(file_name, O_RDONLY)) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 109 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 110 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 111 | while (true) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 112 | 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] | 113 | if (n == -1) { |
| 114 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 115 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 116 | if (n == 0) { |
| 117 | return true; |
| 118 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 119 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 120 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 123 | std::string GetIsoDate() { |
| 124 | time_t now = time(NULL); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 125 | tm tmbuf; |
| 126 | tm* ptm = localtime_r(&now, &tmbuf); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 127 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d", |
| 128 | ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, |
| 129 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 130 | } |
| 131 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 132 | uint64_t MilliTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 133 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 134 | timespec now; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 135 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 136 | 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] | 137 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 138 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 139 | gettimeofday(&now, NULL); |
| 140 | return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_usec / 1000LL; |
| 141 | #endif |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 142 | } |
| 143 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 144 | uint64_t MicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 145 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 146 | timespec now; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 147 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 148 | 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] | 149 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 150 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 151 | gettimeofday(&now, NULL); |
TDYa127 | 5482503 | 2012-04-11 10:45:23 -0700 | [diff] [blame] | 152 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_usec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 153 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 156 | uint64_t NanoTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 157 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 158 | timespec now; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 159 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 160 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 161 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 162 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 163 | gettimeofday(&now, NULL); |
| 164 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL; |
| 165 | #endif |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 166 | } |
| 167 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 168 | uint64_t ThreadCpuMicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 169 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 170 | timespec now; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 171 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
| 172 | 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] | 173 | #else |
| 174 | UNIMPLEMENTED(WARNING); |
| 175 | return -1; |
| 176 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 179 | uint64_t ThreadCpuNanoTime() { |
| 180 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 181 | timespec now; |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 182 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
| 183 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
| 184 | #else |
| 185 | UNIMPLEMENTED(WARNING); |
| 186 | return -1; |
| 187 | #endif |
| 188 | } |
| 189 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 190 | void NanoSleep(uint64_t ns) { |
| 191 | timespec tm; |
| 192 | tm.tv_sec = 0; |
| 193 | tm.tv_nsec = ns; |
| 194 | nanosleep(&tm, NULL); |
| 195 | } |
| 196 | |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 197 | void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) { |
| 198 | int64_t endSec; |
| 199 | |
| 200 | if (absolute) { |
| 201 | #if !defined(__APPLE__) |
| 202 | clock_gettime(clock, ts); |
| 203 | #else |
| 204 | UNUSED(clock); |
| 205 | timeval tv; |
| 206 | gettimeofday(&tv, NULL); |
| 207 | ts->tv_sec = tv.tv_sec; |
| 208 | ts->tv_nsec = tv.tv_usec * 1000; |
| 209 | #endif |
| 210 | } else { |
| 211 | ts->tv_sec = 0; |
| 212 | ts->tv_nsec = 0; |
| 213 | } |
| 214 | endSec = ts->tv_sec + ms / 1000; |
| 215 | if (UNLIKELY(endSec >= 0x7fffffff)) { |
| 216 | std::ostringstream ss; |
| 217 | LOG(INFO) << "Note: end time exceeds epoch: " << ss.str(); |
| 218 | endSec = 0x7ffffffe; |
| 219 | } |
| 220 | ts->tv_sec = endSec; |
| 221 | ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns; |
| 222 | |
| 223 | // Catch rollover. |
| 224 | if (ts->tv_nsec >= 1000000000L) { |
| 225 | ts->tv_sec++; |
| 226 | ts->tv_nsec -= 1000000000L; |
| 227 | } |
| 228 | } |
| 229 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 230 | std::string PrettyDescriptor(const mirror::String* java_descriptor) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 231 | if (java_descriptor == NULL) { |
| 232 | return "null"; |
| 233 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 234 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8()); |
| 235 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 236 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 237 | std::string PrettyDescriptor(const mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 238 | if (klass == NULL) { |
| 239 | return "null"; |
| 240 | } |
| 241 | return PrettyDescriptor(ClassHelper(klass).GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 244 | std::string PrettyDescriptor(const std::string& descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 245 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 246 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 247 | size_t dim = 0; |
| 248 | while (*c == '[') { |
| 249 | dim++; |
| 250 | c++; |
| 251 | } |
| 252 | |
| 253 | // Reference or primitive? |
| 254 | if (*c == 'L') { |
| 255 | // "[[La/b/C;" -> "a.b.C[][]". |
| 256 | c++; // Skip the 'L'. |
| 257 | } else { |
| 258 | // "[[B" -> "byte[][]". |
| 259 | // To make life easier, we make primitives look like unqualified |
| 260 | // reference types. |
| 261 | switch (*c) { |
| 262 | case 'B': c = "byte;"; break; |
| 263 | case 'C': c = "char;"; break; |
| 264 | case 'D': c = "double;"; break; |
| 265 | case 'F': c = "float;"; break; |
| 266 | case 'I': c = "int;"; break; |
| 267 | case 'J': c = "long;"; break; |
| 268 | case 'S': c = "short;"; break; |
| 269 | case 'Z': c = "boolean;"; break; |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 270 | case 'V': c = "void;"; break; // Used when decoding return types. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 271 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 276 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 277 | std::string result; |
| 278 | const char* p = c; |
| 279 | while (*p != ';') { |
| 280 | char ch = *p++; |
| 281 | if (ch == '/') { |
| 282 | ch = '.'; |
| 283 | } |
| 284 | result.push_back(ch); |
| 285 | } |
| 286 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 287 | while (dim--) { |
| 288 | result += "[]"; |
| 289 | } |
| 290 | return result; |
| 291 | } |
| 292 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 293 | std::string PrettyDescriptor(Primitive::Type type) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 294 | std::string descriptor_string(Primitive::Descriptor(type)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 295 | return PrettyDescriptor(descriptor_string); |
| 296 | } |
| 297 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 298 | std::string PrettyField(const mirror::Field* f, bool with_type) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 299 | if (f == NULL) { |
| 300 | return "null"; |
| 301 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 302 | FieldHelper fh(f); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 303 | std::string result; |
| 304 | if (with_type) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 305 | result += PrettyDescriptor(fh.GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 306 | result += ' '; |
| 307 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 308 | result += PrettyDescriptor(fh.GetDeclaringClassDescriptor()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 309 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 310 | result += fh.GetName(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 311 | return result; |
| 312 | } |
| 313 | |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 314 | std::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type) { |
| 315 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 316 | std::string result; |
| 317 | if (with_type) { |
| 318 | result += dex_file.GetFieldTypeDescriptor(field_id); |
| 319 | result += ' '; |
| 320 | } |
| 321 | result += PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(field_id)); |
| 322 | result += '.'; |
| 323 | result += dex_file.GetFieldName(field_id); |
| 324 | return result; |
| 325 | } |
| 326 | |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 327 | std::string PrettyType(uint32_t type_idx, const DexFile& dex_file) { |
| 328 | const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx); |
Mathieu Chartier | 4c70d77 | 2012-09-10 14:08:32 -0700 | [diff] [blame] | 329 | return PrettyDescriptor(dex_file.GetTypeDescriptor(type_id)); |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 332 | std::string PrettyArguments(const char* signature) { |
| 333 | std::string result; |
| 334 | result += '('; |
| 335 | CHECK_EQ(*signature, '('); |
| 336 | ++signature; // Skip the '('. |
| 337 | while (*signature != ')') { |
| 338 | size_t argument_length = 0; |
| 339 | while (signature[argument_length] == '[') { |
| 340 | ++argument_length; |
| 341 | } |
| 342 | if (signature[argument_length] == 'L') { |
| 343 | argument_length = (strchr(signature, ';') - signature + 1); |
| 344 | } else { |
| 345 | ++argument_length; |
| 346 | } |
| 347 | std::string argument_descriptor(signature, argument_length); |
| 348 | result += PrettyDescriptor(argument_descriptor); |
| 349 | if (signature[argument_length] != ')') { |
| 350 | result += ", "; |
| 351 | } |
| 352 | signature += argument_length; |
| 353 | } |
| 354 | CHECK_EQ(*signature, ')'); |
| 355 | ++signature; // Skip the ')'. |
| 356 | result += ')'; |
| 357 | return result; |
| 358 | } |
| 359 | |
| 360 | std::string PrettyReturnType(const char* signature) { |
| 361 | const char* return_type = strchr(signature, ')'); |
| 362 | CHECK(return_type != NULL); |
| 363 | ++return_type; // Skip ')'. |
| 364 | return PrettyDescriptor(return_type); |
| 365 | } |
| 366 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 367 | std::string PrettyMethod(const mirror::AbstractMethod* m, bool with_signature) { |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 368 | if (m == NULL) { |
| 369 | return "null"; |
| 370 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 371 | MethodHelper mh(m); |
| 372 | std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 373 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 374 | result += mh.GetName(); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 375 | if (with_signature) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 376 | std::string signature(mh.GetSignature()); |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 377 | if (signature == "<no signature>") { |
| 378 | return result + signature; |
| 379 | } |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 380 | result = PrettyReturnType(signature.c_str()) + " " + result + PrettyArguments(signature.c_str()); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 381 | } |
| 382 | return result; |
| 383 | } |
| 384 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 385 | std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) { |
| 386 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 387 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 388 | result += '.'; |
| 389 | result += dex_file.GetMethodName(method_id); |
| 390 | if (with_signature) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 391 | std::string signature(dex_file.GetMethodSignature(method_id)); |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 392 | if (signature == "<no signature>") { |
| 393 | return result + signature; |
| 394 | } |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 395 | result = PrettyReturnType(signature.c_str()) + " " + result + PrettyArguments(signature.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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 623 | std::string JniShortName(const mirror::AbstractMethod* 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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 642 | std::string JniLongName(const mirror::AbstractMethod* 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 | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 647 | std::string signature(MethodHelper(m).GetSignature()); |
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] = { |
| 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' |
| 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 | */ |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 720 | bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 721 | uint8_t c = (uint8_t) **pUtf8Ptr; |
| 722 | if (c <= 0x7f) { |
| 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 }; |
| 763 | 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 | |
| 820 | bool sepOrFirst = true; // first character or just encountered a separator. |
| 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) { |
| 926 | ANNOTATE_THREAD_NAME(thread_name); // For tsan. |
Elliott Hughes | 06e3ad4 | 2012-02-07 14:51:57 -0800 | [diff] [blame] | 927 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 928 | int hasAt = 0; |
| 929 | int hasDot = 0; |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 930 | const char* s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 931 | while (*s) { |
| 932 | if (*s == '.') { |
| 933 | hasDot = 1; |
| 934 | } else if (*s == '@') { |
| 935 | hasAt = 1; |
| 936 | } |
| 937 | s++; |
| 938 | } |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 939 | int len = s - thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 940 | if (len < 15 || hasAt || !hasDot) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 941 | s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 942 | } else { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 943 | s = thread_name + len - 15; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 944 | } |
| 945 | #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP) |
Elliott Hughes | 7c6a61e | 2012-03-12 18:01:41 -0700 | [diff] [blame] | 946 | // pthread_setname_np fails rather than truncating long strings. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 947 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 948 | strncpy(buf, s, sizeof(buf)-1); |
| 949 | buf[sizeof(buf)-1] = '\0'; |
| 950 | errno = pthread_setname_np(pthread_self(), buf); |
| 951 | if (errno != 0) { |
| 952 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 953 | } |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 954 | #elif defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 955 | pthread_setname_np(thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 956 | #elif defined(HAVE_PRCTL) |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 957 | 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] | 958 | #else |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 959 | UNIMPLEMENTED(WARNING) << thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 960 | #endif |
| 961 | } |
| 962 | |
Elliott Hughes | ba0b9c5 | 2012-09-20 11:25:12 -0700 | [diff] [blame] | 963 | void GetTaskStats(pid_t tid, char& state, int& utime, int& stime, int& task_cpu) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 964 | utime = stime = task_cpu = 0; |
| 965 | std::string stats; |
Elliott Hughes | 8a31b50 | 2012-04-30 19:36:11 -0700 | [diff] [blame] | 966 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 967 | return; |
| 968 | } |
| 969 | // Skip the command, which may contain spaces. |
| 970 | stats = stats.substr(stats.find(')') + 2); |
| 971 | // Extract the three fields we care about. |
| 972 | std::vector<std::string> fields; |
| 973 | Split(stats, ' ', fields); |
Elliott Hughes | ba0b9c5 | 2012-09-20 11:25:12 -0700 | [diff] [blame] | 974 | state = fields[0][0]; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 975 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 976 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 977 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 978 | } |
| 979 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 980 | std::string GetSchedulerGroupName(pid_t tid) { |
| 981 | // /proc/<pid>/cgroup looks like this: |
| 982 | // 2:devices:/ |
| 983 | // 1:cpuacct,cpu:/ |
| 984 | // We want the third field from the line whose second field contains the "cpu" token. |
| 985 | std::string cgroup_file; |
| 986 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) { |
| 987 | return ""; |
| 988 | } |
| 989 | std::vector<std::string> cgroup_lines; |
| 990 | Split(cgroup_file, '\n', cgroup_lines); |
| 991 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 992 | std::vector<std::string> cgroup_fields; |
| 993 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 994 | std::vector<std::string> cgroups; |
| 995 | Split(cgroup_fields[1], ',', cgroups); |
| 996 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 997 | if (cgroups[i] == "cpu") { |
| 998 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | return ""; |
| 1003 | } |
| 1004 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1005 | static const char* CleanMapName(const backtrace_symbol_t* symbol) { |
| 1006 | const char* map_name = symbol->map_name; |
| 1007 | if (map_name == NULL) { |
| 1008 | map_name = "???"; |
| 1009 | } |
| 1010 | // Turn "/usr/local/google/home/enh/clean-dalvik-dev/out/host/linux-x86/lib/libartd.so" |
| 1011 | // into "libartd.so". |
| 1012 | const char* last_slash = strrchr(map_name, '/'); |
| 1013 | if (last_slash != NULL) { |
| 1014 | map_name = last_slash + 1; |
| 1015 | } |
| 1016 | return map_name; |
| 1017 | } |
| 1018 | |
| 1019 | static void FindSymbolInElf(const backtrace_frame_t* frame, const backtrace_symbol_t* symbol, |
| 1020 | std::string& symbol_name, uint32_t& pc_offset) { |
| 1021 | symbol_table_t* symbol_table = NULL; |
| 1022 | if (symbol->map_name != NULL) { |
| 1023 | symbol_table = load_symbol_table(symbol->map_name); |
| 1024 | } |
| 1025 | const symbol_t* elf_symbol = NULL; |
Elliott Hughes | 95aff77 | 2012-06-12 17:44:15 -0700 | [diff] [blame] | 1026 | bool was_relative = true; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1027 | if (symbol_table != NULL) { |
| 1028 | elf_symbol = find_symbol(symbol_table, symbol->relative_pc); |
| 1029 | if (elf_symbol == NULL) { |
| 1030 | elf_symbol = find_symbol(symbol_table, frame->absolute_pc); |
Elliott Hughes | 95aff77 | 2012-06-12 17:44:15 -0700 | [diff] [blame] | 1031 | was_relative = false; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1032 | } |
| 1033 | } |
| 1034 | if (elf_symbol != NULL) { |
| 1035 | const char* demangled_symbol_name = demangle_symbol_name(elf_symbol->name); |
| 1036 | if (demangled_symbol_name != NULL) { |
| 1037 | symbol_name = demangled_symbol_name; |
| 1038 | } else { |
| 1039 | symbol_name = elf_symbol->name; |
| 1040 | } |
Elliott Hughes | 95aff77 | 2012-06-12 17:44:15 -0700 | [diff] [blame] | 1041 | |
| 1042 | // TODO: is it a libcorkscrew bug that we have to do this? |
| 1043 | pc_offset = (was_relative ? symbol->relative_pc : frame->absolute_pc) - elf_symbol->start; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1044 | } else { |
| 1045 | symbol_name = "???"; |
| 1046 | } |
| 1047 | free_symbol_table(symbol_table); |
| 1048 | } |
| 1049 | |
| 1050 | void DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) { |
Elliott Hughes | 02fb9f7 | 2012-06-13 22:22:33 -0700 | [diff] [blame] | 1051 | // Ensure libcorkscrew doesn't use a stale cache of /proc/self/maps. |
| 1052 | flush_my_map_info_list(); |
| 1053 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1054 | const size_t MAX_DEPTH = 32; |
| 1055 | UniquePtr<backtrace_frame_t[]> frames(new backtrace_frame_t[MAX_DEPTH]); |
Elliott Hughes | 5db7ea0 | 2012-06-14 13:33:49 -0700 | [diff] [blame] | 1056 | size_t ignore_count = 2; // Don't include unwind_backtrace_thread or DumpNativeStack. |
| 1057 | ssize_t frame_count = unwind_backtrace_thread(tid, frames.get(), ignore_count, MAX_DEPTH); |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1058 | if (frame_count == -1) { |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1059 | os << prefix << "(unwind_backtrace_thread failed for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1060 | return; |
| 1061 | } else if (frame_count == 0) { |
Elliott Hughes | 225f5a1 | 2012-06-11 11:23:48 -0700 | [diff] [blame] | 1062 | os << prefix << "(no native stack frames for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1063 | return; |
| 1064 | } |
| 1065 | |
| 1066 | UniquePtr<backtrace_symbol_t[]> backtrace_symbols(new backtrace_symbol_t[frame_count]); |
| 1067 | get_backtrace_symbols(frames.get(), frame_count, backtrace_symbols.get()); |
| 1068 | |
| 1069 | for (size_t i = 0; i < static_cast<size_t>(frame_count); ++i) { |
| 1070 | const backtrace_frame_t* frame = &frames[i]; |
| 1071 | const backtrace_symbol_t* symbol = &backtrace_symbols[i]; |
| 1072 | |
| 1073 | // We produce output like this: |
| 1074 | // ] #00 unwind_backtrace_thread+536 [0x55d75bb8] (libcorkscrew.so) |
| 1075 | |
| 1076 | std::string symbol_name; |
| 1077 | uint32_t pc_offset = 0; |
| 1078 | if (symbol->demangled_name != NULL) { |
| 1079 | symbol_name = symbol->demangled_name; |
| 1080 | pc_offset = symbol->relative_pc - symbol->relative_symbol_addr; |
| 1081 | } else if (symbol->symbol_name != NULL) { |
| 1082 | symbol_name = symbol->symbol_name; |
| 1083 | pc_offset = symbol->relative_pc - symbol->relative_symbol_addr; |
| 1084 | } else { |
| 1085 | // dladdr(3) didn't find a symbol; maybe it's static? Look in the ELF file... |
| 1086 | FindSymbolInElf(frame, symbol, symbol_name, pc_offset); |
| 1087 | } |
| 1088 | |
| 1089 | os << prefix; |
| 1090 | if (include_count) { |
| 1091 | os << StringPrintf("#%02zd ", i); |
| 1092 | } |
| 1093 | os << symbol_name; |
| 1094 | if (pc_offset != 0) { |
| 1095 | os << "+" << pc_offset; |
| 1096 | } |
| 1097 | os << StringPrintf(" [%p] (%s)\n", |
| 1098 | reinterpret_cast<void*>(frame->absolute_pc), CleanMapName(symbol)); |
| 1099 | } |
| 1100 | |
| 1101 | free_backtrace_symbols(backtrace_symbols.get(), frame_count); |
| 1102 | } |
| 1103 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1104 | #if defined(__APPLE__) |
| 1105 | |
| 1106 | // TODO: is there any way to get the kernel stack on Mac OS? |
| 1107 | void DumpKernelStack(std::ostream&, pid_t, const char*, bool) {} |
| 1108 | |
| 1109 | #else |
| 1110 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1111 | 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] | 1112 | if (tid == GetTid()) { |
| 1113 | // There's no point showing that we're reading our stack out of /proc! |
| 1114 | return; |
| 1115 | } |
| 1116 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1117 | std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid)); |
| 1118 | std::string kernel_stack; |
| 1119 | if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) { |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1120 | os << prefix << "(couldn't read " << kernel_stack_filename << ")\n"; |
jeffhao | c4c3ee2 | 2012-05-25 16:16:32 -0700 | [diff] [blame] | 1121 | return; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | std::vector<std::string> kernel_stack_frames; |
| 1125 | Split(kernel_stack, '\n', kernel_stack_frames); |
| 1126 | // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff", |
| 1127 | // which looking at the source appears to be the kernel's way of saying "that's all, folks!". |
| 1128 | kernel_stack_frames.pop_back(); |
| 1129 | for (size_t i = 0; i < kernel_stack_frames.size(); ++i) { |
| 1130 | // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110" into "futex_wait_queue_me+0xcd/0x110". |
| 1131 | const char* text = kernel_stack_frames[i].c_str(); |
| 1132 | const char* close_bracket = strchr(text, ']'); |
| 1133 | if (close_bracket != NULL) { |
| 1134 | text = close_bracket + 2; |
| 1135 | } |
| 1136 | os << prefix; |
| 1137 | if (include_count) { |
| 1138 | os << StringPrintf("#%02zd ", i); |
| 1139 | } |
| 1140 | os << text << "\n"; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | #endif |
| 1145 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1146 | const char* GetAndroidRoot() { |
| 1147 | const char* android_root = getenv("ANDROID_ROOT"); |
| 1148 | if (android_root == NULL) { |
| 1149 | if (OS::DirectoryExists("/system")) { |
| 1150 | android_root = "/system"; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1151 | } else { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1152 | LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist"; |
| 1153 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1154 | } |
| 1155 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1156 | if (!OS::DirectoryExists(android_root)) { |
| 1157 | LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1158 | return ""; |
| 1159 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1160 | return android_root; |
| 1161 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1162 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1163 | const char* GetAndroidData() { |
| 1164 | const char* android_data = getenv("ANDROID_DATA"); |
| 1165 | if (android_data == NULL) { |
| 1166 | if (OS::DirectoryExists("/data")) { |
| 1167 | android_data = "/data"; |
| 1168 | } else { |
| 1169 | LOG(FATAL) << "ANDROID_DATA not set and /data does not exist"; |
| 1170 | return ""; |
| 1171 | } |
| 1172 | } |
| 1173 | if (!OS::DirectoryExists(android_data)) { |
| 1174 | LOG(FATAL) << "Failed to find ANDROID_DATA directory " << android_data; |
| 1175 | return ""; |
| 1176 | } |
| 1177 | return android_data; |
| 1178 | } |
| 1179 | |
Shih-wei Liao | 795e330 | 2012-04-21 00:20:57 -0700 | [diff] [blame] | 1180 | std::string GetArtCacheOrDie(const char* android_data) { |
| 1181 | std::string art_cache(StringPrintf("%s/art-cache", android_data)); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1182 | |
| 1183 | if (!OS::DirectoryExists(art_cache.c_str())) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 1184 | if (StartsWith(art_cache, "/tmp/")) { |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1185 | int result = mkdir(art_cache.c_str(), 0700); |
| 1186 | if (result != 0) { |
| 1187 | LOG(FATAL) << "Failed to create art-cache directory " << art_cache; |
| 1188 | return ""; |
| 1189 | } |
| 1190 | } else { |
| 1191 | LOG(FATAL) << "Failed to find art-cache directory " << art_cache; |
| 1192 | return ""; |
| 1193 | } |
| 1194 | } |
| 1195 | return art_cache; |
| 1196 | } |
| 1197 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1198 | std::string GetArtCacheFilenameOrDie(const std::string& location) { |
Shih-wei Liao | 795e330 | 2012-04-21 00:20:57 -0700 | [diff] [blame] | 1199 | std::string art_cache(GetArtCacheOrDie(GetAndroidData())); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 1200 | CHECK_EQ(location[0], '/') << location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1201 | std::string cache_file(location, 1); // skip leading slash |
| 1202 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
| 1203 | return art_cache + "/" + cache_file; |
| 1204 | } |
| 1205 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1206 | bool IsValidZipFilename(const std::string& filename) { |
| 1207 | if (filename.size() < 4) { |
| 1208 | return false; |
| 1209 | } |
| 1210 | std::string suffix(filename.substr(filename.size() - 4)); |
| 1211 | return (suffix == ".zip" || suffix == ".jar" || suffix == ".apk"); |
| 1212 | } |
| 1213 | |
| 1214 | bool IsValidDexFilename(const std::string& filename) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 1215 | return EndsWith(filename, ".dex"); |
| 1216 | } |
| 1217 | |
| 1218 | bool IsValidOatFilename(const std::string& filename) { |
| 1219 | return EndsWith(filename, ".oat"); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 1222 | } // namespace art |