Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: enh@google.com (Elliott Hughes) |
| 3 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 4 | #include "utils.h" |
| 5 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 6 | #include <pthread.h> |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 7 | #include <sys/stat.h> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 8 | #include <sys/syscall.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <unistd.h> |
| 11 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 12 | #include "UniquePtr.h" |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 13 | #include "class_loader.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 14 | #include "file.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 15 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 16 | #include "object_utils.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 17 | #include "os.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 18 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 19 | #if !defined(HAVE_POSIX_CLOCKS) |
| 20 | #include <sys/time.h> |
| 21 | #endif |
| 22 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 23 | #if defined(HAVE_PRCTL) |
| 24 | #include <sys/prctl.h> |
| 25 | #endif |
| 26 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame^] | 27 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 28 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 29 | #endif |
| 30 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 31 | namespace art { |
| 32 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame^] | 33 | pid_t GetTid() { |
| 34 | #if defined(__APPLE__) |
| 35 | // Mac OS doesn't have gettid(2). |
| 36 | return getpid(); |
| 37 | #else |
| 38 | // Neither bionic nor glibc exposes gettid(2). |
| 39 | return syscall(__NR_gettid); |
| 40 | #endif |
| 41 | } |
| 42 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 43 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
| 44 | UniquePtr<File> file(OS::OpenFile(file_name.c_str(), false)); |
| 45 | if (file.get() == NULL) { |
| 46 | return false; |
| 47 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 48 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 49 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 50 | while (true) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 51 | int64_t n = file->Read(&buf[0], buf.size()); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 52 | if (n == -1) { |
| 53 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 54 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 55 | if (n == 0) { |
| 56 | return true; |
| 57 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 58 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 59 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 62 | std::string GetIsoDate() { |
| 63 | time_t now = time(NULL); |
| 64 | struct tm tmbuf; |
| 65 | struct tm* ptm = localtime_r(&now, &tmbuf); |
| 66 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d", |
| 67 | ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, |
| 68 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 69 | } |
| 70 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 71 | uint64_t MilliTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 72 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 73 | struct timespec now; |
| 74 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 75 | 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] | 76 | #else |
| 77 | struct timeval now; |
| 78 | gettimeofday(&now, NULL); |
| 79 | return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_usec / 1000LL; |
| 80 | #endif |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 81 | } |
| 82 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 83 | uint64_t MicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 84 | #if defined(HAVE_POSIX_CLOCKS) |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 85 | struct timespec now; |
| 86 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 87 | 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] | 88 | #else |
| 89 | struct timeval now; |
| 90 | gettimeofday(&now, NULL); |
| 91 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_usec * 1000LL; |
| 92 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 95 | uint64_t NanoTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 96 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 97 | struct timespec now; |
| 98 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 99 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 100 | #else |
| 101 | struct timeval now; |
| 102 | gettimeofday(&now, NULL); |
| 103 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL; |
| 104 | #endif |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 105 | } |
| 106 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 107 | uint64_t ThreadCpuMicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 108 | #if defined(HAVE_POSIX_CLOCKS) |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 109 | struct timespec now; |
| 110 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
| 111 | 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] | 112 | #else |
| 113 | UNIMPLEMENTED(WARNING); |
| 114 | return -1; |
| 115 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 118 | std::string PrettyDescriptor(const String* java_descriptor) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 119 | if (java_descriptor == NULL) { |
| 120 | return "null"; |
| 121 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 122 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8()); |
| 123 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 124 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 125 | std::string PrettyDescriptor(const Class* klass) { |
| 126 | if (klass == NULL) { |
| 127 | return "null"; |
| 128 | } |
| 129 | return PrettyDescriptor(ClassHelper(klass).GetDescriptor()); |
| 130 | |
| 131 | } |
| 132 | |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 133 | std::string PrettyDescriptor(const std::string& descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 134 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 135 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 136 | size_t dim = 0; |
| 137 | while (*c == '[') { |
| 138 | dim++; |
| 139 | c++; |
| 140 | } |
| 141 | |
| 142 | // Reference or primitive? |
| 143 | if (*c == 'L') { |
| 144 | // "[[La/b/C;" -> "a.b.C[][]". |
| 145 | c++; // Skip the 'L'. |
| 146 | } else { |
| 147 | // "[[B" -> "byte[][]". |
| 148 | // To make life easier, we make primitives look like unqualified |
| 149 | // reference types. |
| 150 | switch (*c) { |
| 151 | case 'B': c = "byte;"; break; |
| 152 | case 'C': c = "char;"; break; |
| 153 | case 'D': c = "double;"; break; |
| 154 | case 'F': c = "float;"; break; |
| 155 | case 'I': c = "int;"; break; |
| 156 | case 'J': c = "long;"; break; |
| 157 | case 'S': c = "short;"; break; |
| 158 | case 'Z': c = "boolean;"; break; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 159 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 164 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 165 | std::string result; |
| 166 | const char* p = c; |
| 167 | while (*p != ';') { |
| 168 | char ch = *p++; |
| 169 | if (ch == '/') { |
| 170 | ch = '.'; |
| 171 | } |
| 172 | result.push_back(ch); |
| 173 | } |
| 174 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 175 | while (dim--) { |
| 176 | result += "[]"; |
| 177 | } |
| 178 | return result; |
| 179 | } |
| 180 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 181 | std::string PrettyDescriptor(Primitive::Type type) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 182 | std::string descriptor_string(Primitive::Descriptor(type)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 183 | return PrettyDescriptor(descriptor_string); |
| 184 | } |
| 185 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 186 | std::string PrettyField(const Field* f, bool with_type) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 187 | if (f == NULL) { |
| 188 | return "null"; |
| 189 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 190 | FieldHelper fh(f); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 191 | std::string result; |
| 192 | if (with_type) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 193 | result += PrettyDescriptor(fh.GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 194 | result += ' '; |
| 195 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 196 | result += PrettyDescriptor(fh.GetDeclaringClassDescriptor()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 197 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 198 | result += fh.GetName(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 199 | return result; |
| 200 | } |
| 201 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 202 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 203 | if (m == NULL) { |
| 204 | return "null"; |
| 205 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 206 | MethodHelper mh(m); |
| 207 | std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 208 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 209 | result += mh.GetName(); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 210 | if (with_signature) { |
| 211 | // TODO: iterate over the signature's elements and pass them all to |
| 212 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 213 | result += mh.GetSignature(); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 214 | } |
| 215 | return result; |
| 216 | } |
| 217 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 218 | std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) { |
| 219 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 220 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 221 | result += '.'; |
| 222 | result += dex_file.GetMethodName(method_id); |
| 223 | if (with_signature) { |
| 224 | // TODO: iterate over the signature's elements and pass them all to |
| 225 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
| 226 | result += dex_file.GetMethodSignature(method_id); |
| 227 | } |
| 228 | return result; |
| 229 | } |
| 230 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 231 | std::string PrettyTypeOf(const Object* obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 232 | if (obj == NULL) { |
| 233 | return "null"; |
| 234 | } |
| 235 | if (obj->GetClass() == NULL) { |
| 236 | return "(raw)"; |
| 237 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 238 | ClassHelper kh(obj->GetClass()); |
| 239 | std::string result(PrettyDescriptor(kh.GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 240 | if (obj->IsClass()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 241 | kh.ChangeClass(obj->AsClass()); |
| 242 | result += "<" + PrettyDescriptor(kh.GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 243 | } |
| 244 | return result; |
| 245 | } |
| 246 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 247 | std::string PrettyClass(const Class* c) { |
| 248 | if (c == NULL) { |
| 249 | return "null"; |
| 250 | } |
| 251 | std::string result; |
| 252 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 253 | result += PrettyDescriptor(c); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 254 | result += ">"; |
| 255 | return result; |
| 256 | } |
| 257 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 258 | std::string PrettyClassAndClassLoader(const Class* c) { |
| 259 | if (c == NULL) { |
| 260 | return "null"; |
| 261 | } |
| 262 | std::string result; |
| 263 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 264 | result += PrettyDescriptor(c); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 265 | result += ","; |
| 266 | result += PrettyTypeOf(c->GetClassLoader()); |
| 267 | // TODO: add an identifying hash value for the loader |
| 268 | result += ">"; |
| 269 | return result; |
| 270 | } |
| 271 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 272 | std::string MangleForJni(const std::string& s) { |
| 273 | std::string result; |
| 274 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 275 | const char* cp = &s[0]; |
| 276 | for (size_t i = 0; i < char_count; ++i) { |
| 277 | uint16_t ch = GetUtf16FromUtf8(&cp); |
| 278 | if (ch == '$' || ch > 127) { |
| 279 | StringAppendF(&result, "_0%04x", ch); |
| 280 | } else { |
| 281 | switch (ch) { |
| 282 | case '_': |
| 283 | result += "_1"; |
| 284 | break; |
| 285 | case ';': |
| 286 | result += "_2"; |
| 287 | break; |
| 288 | case '[': |
| 289 | result += "_3"; |
| 290 | break; |
| 291 | case '/': |
| 292 | result += "_"; |
| 293 | break; |
| 294 | default: |
| 295 | result.push_back(ch); |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | return result; |
| 301 | } |
| 302 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 303 | std::string DotToDescriptor(const char* class_name) { |
| 304 | std::string descriptor(class_name); |
| 305 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 306 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 307 | descriptor = "L" + descriptor + ";"; |
| 308 | } |
| 309 | return descriptor; |
| 310 | } |
| 311 | |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 312 | std::string DescriptorToDot(const StringPiece& descriptor) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 313 | DCHECK_EQ(descriptor[0], 'L'); |
| 314 | DCHECK_EQ(descriptor[descriptor.size()-1], ';'); |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 315 | std::string dot(descriptor.substr(1, descriptor.size() - 2).ToString()); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 316 | std::replace(dot.begin(), dot.end(), '/', '.'); |
| 317 | return dot; |
| 318 | } |
| 319 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 320 | std::string JniShortName(const Method* m) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 321 | MethodHelper mh(m); |
| 322 | std::string class_name(mh.GetDeclaringClassDescriptor()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 323 | // Remove the leading 'L' and trailing ';'... |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 324 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 325 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 326 | class_name.erase(0, 1); |
| 327 | class_name.erase(class_name.size() - 1, 1); |
| 328 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 329 | std::string method_name(mh.GetName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 330 | |
| 331 | std::string short_name; |
| 332 | short_name += "Java_"; |
| 333 | short_name += MangleForJni(class_name); |
| 334 | short_name += "_"; |
| 335 | short_name += MangleForJni(method_name); |
| 336 | return short_name; |
| 337 | } |
| 338 | |
| 339 | std::string JniLongName(const Method* m) { |
| 340 | std::string long_name; |
| 341 | long_name += JniShortName(m); |
| 342 | long_name += "__"; |
| 343 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 344 | std::string signature(MethodHelper(m).GetSignature()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 345 | signature.erase(0, 1); |
| 346 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 347 | |
| 348 | long_name += MangleForJni(signature); |
| 349 | |
| 350 | return long_name; |
| 351 | } |
| 352 | |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 353 | // Helper for IsValidMemberNameUtf8(), a bit vector indicating valid low ascii. |
| 354 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
| 355 | 0x00000000, // 00..1f low control characters; nothing valid |
| 356 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 357 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 358 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
| 359 | }; |
| 360 | |
| 361 | // Helper for IsValidMemberNameUtf8(); do not call directly. |
| 362 | bool IsValidMemberNameUtf8Slow(const char** pUtf8Ptr) { |
| 363 | /* |
| 364 | * It's a multibyte encoded character. Decode it and analyze. We |
| 365 | * accept anything that isn't (a) an improperly encoded low value, |
| 366 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 367 | * control character, or (e) a high space, layout, or special |
| 368 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 369 | * U+fff0..U+ffff). This is all specified in the dex format |
| 370 | * document. |
| 371 | */ |
| 372 | |
| 373 | uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 374 | |
| 375 | // Perform follow-up tests based on the high 8 bits. |
| 376 | switch (utf16 >> 8) { |
| 377 | case 0x00: |
| 378 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 379 | return (utf16 > 0x00a0); |
| 380 | case 0xd8: |
| 381 | case 0xd9: |
| 382 | case 0xda: |
| 383 | case 0xdb: |
| 384 | // It's a leading surrogate. Check to see that a trailing |
| 385 | // surrogate follows. |
| 386 | utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 387 | return (utf16 >= 0xdc00) && (utf16 <= 0xdfff); |
| 388 | case 0xdc: |
| 389 | case 0xdd: |
| 390 | case 0xde: |
| 391 | case 0xdf: |
| 392 | // It's a trailing surrogate, which is not valid at this point. |
| 393 | return false; |
| 394 | case 0x20: |
| 395 | case 0xff: |
| 396 | // It's in the range that has spaces, controls, and specials. |
| 397 | switch (utf16 & 0xfff8) { |
| 398 | case 0x2000: |
| 399 | case 0x2008: |
| 400 | case 0x2028: |
| 401 | case 0xfff0: |
| 402 | case 0xfff8: |
| 403 | return false; |
| 404 | } |
| 405 | break; |
| 406 | } |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 411 | * valid as part of a member name, updating the pointer to point past |
| 412 | * the consumed character. This will consume two encoded UTF-16 code |
| 413 | * points if the character is encoded as a surrogate pair. Also, if |
| 414 | * this function returns false, then the given pointer may only have |
| 415 | * been partially advanced. |
| 416 | */ |
| 417 | bool IsValidMemberNameUtf8(const char** pUtf8Ptr) { |
| 418 | uint8_t c = (uint8_t) **pUtf8Ptr; |
| 419 | if (c <= 0x7f) { |
| 420 | // It's low-ascii, so check the table. |
| 421 | uint32_t wordIdx = c >> 5; |
| 422 | uint32_t bitIdx = c & 0x1f; |
| 423 | (*pUtf8Ptr)++; |
| 424 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 425 | } |
| 426 | |
| 427 | // It's a multibyte encoded character. Call a non-inline function |
| 428 | // for the heavy lifting. |
| 429 | return IsValidMemberNameUtf8Slow(pUtf8Ptr); |
| 430 | } |
| 431 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 432 | enum ClassNameType { kName, kDescriptor }; |
| 433 | bool IsValidClassName(const char* s, ClassNameType type, char separator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 434 | int arrayCount = 0; |
| 435 | while (*s == '[') { |
| 436 | arrayCount++; |
| 437 | s++; |
| 438 | } |
| 439 | |
| 440 | if (arrayCount > 255) { |
| 441 | // Arrays may have no more than 255 dimensions. |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | if (arrayCount != 0) { |
| 446 | /* |
| 447 | * If we're looking at an array of some sort, then it doesn't |
| 448 | * matter if what is being asked for is a class name; the |
| 449 | * format looks the same as a type descriptor in that case, so |
| 450 | * treat it as such. |
| 451 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 452 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 455 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 456 | /* |
| 457 | * We are looking for a descriptor. Either validate it as a |
| 458 | * single-character primitive type, or continue on to check the |
| 459 | * embedded class name (bracketed by "L" and ";"). |
| 460 | */ |
| 461 | switch (*(s++)) { |
| 462 | case 'B': |
| 463 | case 'C': |
| 464 | case 'D': |
| 465 | case 'F': |
| 466 | case 'I': |
| 467 | case 'J': |
| 468 | case 'S': |
| 469 | case 'Z': |
| 470 | // These are all single-character descriptors for primitive types. |
| 471 | return (*s == '\0'); |
| 472 | case 'V': |
| 473 | // Non-array void is valid, but you can't have an array of void. |
| 474 | return (arrayCount == 0) && (*s == '\0'); |
| 475 | case 'L': |
| 476 | // Class name: Break out and continue below. |
| 477 | break; |
| 478 | default: |
| 479 | // Oddball descriptor character. |
| 480 | return false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * We just consumed the 'L' that introduces a class name as part |
| 486 | * of a type descriptor, or we are looking for an unadorned class |
| 487 | * name. |
| 488 | */ |
| 489 | |
| 490 | bool sepOrFirst = true; // first character or just encountered a separator. |
| 491 | for (;;) { |
| 492 | uint8_t c = (uint8_t) *s; |
| 493 | switch (c) { |
| 494 | case '\0': |
| 495 | /* |
| 496 | * Premature end for a type descriptor, but valid for |
| 497 | * a class name as long as we haven't encountered an |
| 498 | * empty component (including the degenerate case of |
| 499 | * the empty string ""). |
| 500 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 501 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 502 | case ';': |
| 503 | /* |
| 504 | * Invalid character for a class name, but the |
| 505 | * legitimate end of a type descriptor. In the latter |
| 506 | * case, make sure that this is the end of the string |
| 507 | * and that it doesn't end with an empty component |
| 508 | * (including the degenerate case of "L;"). |
| 509 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 510 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 511 | case '/': |
| 512 | case '.': |
| 513 | if (c != separator) { |
| 514 | // The wrong separator character. |
| 515 | return false; |
| 516 | } |
| 517 | if (sepOrFirst) { |
| 518 | // Separator at start or two separators in a row. |
| 519 | return false; |
| 520 | } |
| 521 | sepOrFirst = true; |
| 522 | s++; |
| 523 | break; |
| 524 | default: |
| 525 | if (!IsValidMemberNameUtf8(&s)) { |
| 526 | return false; |
| 527 | } |
| 528 | sepOrFirst = false; |
| 529 | break; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 534 | bool IsValidBinaryClassName(const char* s) { |
| 535 | return IsValidClassName(s, kName, '.'); |
| 536 | } |
| 537 | |
| 538 | bool IsValidJniClassName(const char* s) { |
| 539 | return IsValidClassName(s, kName, '/'); |
| 540 | } |
| 541 | |
| 542 | bool IsValidDescriptor(const char* s) { |
| 543 | return IsValidClassName(s, kDescriptor, '/'); |
| 544 | } |
| 545 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 546 | void Split(const std::string& s, char delim, std::vector<std::string>& result) { |
| 547 | const char* p = s.data(); |
| 548 | const char* end = p + s.size(); |
| 549 | while (p != end) { |
| 550 | if (*p == delim) { |
| 551 | ++p; |
| 552 | } else { |
| 553 | const char* start = p; |
| 554 | while (++p != end && *p != delim) { |
| 555 | // Skip to the next occurrence of the delimiter. |
| 556 | } |
| 557 | result.push_back(std::string(start, p - start)); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 562 | void SetThreadName(const char* threadName) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 563 | int hasAt = 0; |
| 564 | int hasDot = 0; |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 565 | const char* s = threadName; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 566 | while (*s) { |
| 567 | if (*s == '.') { |
| 568 | hasDot = 1; |
| 569 | } else if (*s == '@') { |
| 570 | hasAt = 1; |
| 571 | } |
| 572 | s++; |
| 573 | } |
| 574 | int len = s - threadName; |
| 575 | if (len < 15 || hasAt || !hasDot) { |
| 576 | s = threadName; |
| 577 | } else { |
| 578 | s = threadName + len - 15; |
| 579 | } |
| 580 | #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP) |
| 581 | /* pthread_setname_np fails rather than truncating long strings */ |
| 582 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 583 | strncpy(buf, s, sizeof(buf)-1); |
| 584 | buf[sizeof(buf)-1] = '\0'; |
| 585 | errno = pthread_setname_np(pthread_self(), buf); |
| 586 | if (errno != 0) { |
| 587 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 588 | } |
| 589 | #elif defined(HAVE_PRCTL) |
| 590 | prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0); |
| 591 | #else |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 592 | UNIMPLEMENTED(WARNING) << threadName; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 593 | #endif |
| 594 | } |
| 595 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 596 | void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu) { |
| 597 | utime = stime = task_cpu = 0; |
| 598 | std::string stats; |
| 599 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", GetTid()).c_str(), &stats)) { |
| 600 | return; |
| 601 | } |
| 602 | // Skip the command, which may contain spaces. |
| 603 | stats = stats.substr(stats.find(')') + 2); |
| 604 | // Extract the three fields we care about. |
| 605 | std::vector<std::string> fields; |
| 606 | Split(stats, ' ', fields); |
| 607 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 608 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 609 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 610 | } |
| 611 | |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 612 | std::string GetArtCacheOrDie() { |
| 613 | const char* data_root = getenv("ANDROID_DATA"); |
| 614 | if (data_root == NULL) { |
| 615 | if (OS::DirectoryExists("/data")) { |
| 616 | data_root = "/data"; |
| 617 | } else { |
| 618 | data_root = "/tmp"; |
| 619 | } |
| 620 | } |
| 621 | if (!OS::DirectoryExists(data_root)) { |
| 622 | LOG(FATAL) << "Failed to find ANDROID_DATA directory " << data_root; |
| 623 | return ""; |
| 624 | } |
| 625 | |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 626 | std::string art_cache(StringPrintf("%s/art-cache", data_root)); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 627 | |
| 628 | if (!OS::DirectoryExists(art_cache.c_str())) { |
| 629 | if (StringPiece(art_cache).starts_with("/tmp/")) { |
| 630 | int result = mkdir(art_cache.c_str(), 0700); |
| 631 | if (result != 0) { |
| 632 | LOG(FATAL) << "Failed to create art-cache directory " << art_cache; |
| 633 | return ""; |
| 634 | } |
| 635 | } else { |
| 636 | LOG(FATAL) << "Failed to find art-cache directory " << art_cache; |
| 637 | return ""; |
| 638 | } |
| 639 | } |
| 640 | return art_cache; |
| 641 | } |
| 642 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 643 | std::string GetArtCacheFilenameOrDie(const std::string& location) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 644 | std::string art_cache(GetArtCacheOrDie()); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 645 | CHECK_EQ(location[0], '/'); |
| 646 | std::string cache_file(location, 1); // skip leading slash |
| 647 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
| 648 | return art_cache + "/" + cache_file; |
| 649 | } |
| 650 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 651 | bool IsValidZipFilename(const std::string& filename) { |
| 652 | if (filename.size() < 4) { |
| 653 | return false; |
| 654 | } |
| 655 | std::string suffix(filename.substr(filename.size() - 4)); |
| 656 | return (suffix == ".zip" || suffix == ".jar" || suffix == ".apk"); |
| 657 | } |
| 658 | |
| 659 | bool IsValidDexFilename(const std::string& filename) { |
| 660 | if (filename.size() < 4) { |
| 661 | return false; |
| 662 | } |
| 663 | std::string suffix(filename.substr(filename.size() - 4)); |
| 664 | return (suffix == ".dex"); |
| 665 | } |
| 666 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 667 | } // namespace art |