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