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