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