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