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