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 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 4 | #include "file.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 5 | #include "object.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 6 | #include "os.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 7 | #include "utils.h" |
| 8 | |
| 9 | namespace art { |
| 10 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 11 | std::string ReadFileToString(const char* file_name) { |
| 12 | scoped_ptr<File> file(OS::OpenFile(file_name, false)); |
| 13 | CHECK(file != NULL); |
| 14 | |
| 15 | std::string contents; |
| 16 | char buf[8 * KB]; |
| 17 | while (true) { |
| 18 | int64_t n = file->Read(buf, sizeof(buf)); |
| 19 | CHECK_NE(-1, n); |
| 20 | if (n == 0) { |
| 21 | break; |
| 22 | } |
| 23 | contents.append(buf, n); |
| 24 | } |
| 25 | return contents; |
| 26 | } |
| 27 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame^] | 28 | std::string GetIsoDate() { |
| 29 | time_t now = time(NULL); |
| 30 | struct tm tmbuf; |
| 31 | struct tm* ptm = localtime_r(&now, &tmbuf); |
| 32 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d", |
| 33 | ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, |
| 34 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 35 | } |
| 36 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 37 | std::string PrettyDescriptor(const String* java_descriptor) { |
| 38 | std::string descriptor(java_descriptor->ToModifiedUtf8()); |
| 39 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 40 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 41 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 42 | size_t dim = 0; |
| 43 | while (*c == '[') { |
| 44 | dim++; |
| 45 | c++; |
| 46 | } |
| 47 | |
| 48 | // Reference or primitive? |
| 49 | if (*c == 'L') { |
| 50 | // "[[La/b/C;" -> "a.b.C[][]". |
| 51 | c++; // Skip the 'L'. |
| 52 | } else { |
| 53 | // "[[B" -> "byte[][]". |
| 54 | // To make life easier, we make primitives look like unqualified |
| 55 | // reference types. |
| 56 | switch (*c) { |
| 57 | case 'B': c = "byte;"; break; |
| 58 | case 'C': c = "char;"; break; |
| 59 | case 'D': c = "double;"; break; |
| 60 | case 'F': c = "float;"; break; |
| 61 | case 'I': c = "int;"; break; |
| 62 | case 'J': c = "long;"; break; |
| 63 | case 'S': c = "short;"; break; |
| 64 | case 'Z': c = "boolean;"; break; |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 65 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
| 69 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 70 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 71 | std::string result; |
| 72 | const char* p = c; |
| 73 | while (*p != ';') { |
| 74 | char ch = *p++; |
| 75 | if (ch == '/') { |
| 76 | ch = '.'; |
| 77 | } |
| 78 | result.push_back(ch); |
| 79 | } |
| 80 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 81 | while (dim--) { |
| 82 | result += "[]"; |
| 83 | } |
| 84 | return result; |
| 85 | } |
| 86 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 87 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 88 | if (m == NULL) { |
| 89 | return "null"; |
| 90 | } |
| 91 | Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 92 | std::string result(PrettyDescriptor(c->GetDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 93 | result += '.'; |
| 94 | result += m->GetName()->ToModifiedUtf8(); |
| 95 | if (with_signature) { |
| 96 | // TODO: iterate over the signature's elements and pass them all to |
| 97 | // PrettyDescriptor? We'd need to pull out the return type specially, too. |
| 98 | result += m->GetSignature()->ToModifiedUtf8(); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 103 | std::string PrettyType(const Object* obj) { |
| 104 | if (obj == NULL) { |
| 105 | return "null"; |
| 106 | } |
| 107 | if (obj->GetClass() == NULL) { |
| 108 | return "(raw)"; |
| 109 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 110 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 111 | if (obj->IsClass()) { |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 112 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 113 | } |
| 114 | return result; |
| 115 | } |
| 116 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 117 | std::string MangleForJni(const std::string& s) { |
| 118 | std::string result; |
| 119 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 120 | const char* cp = &s[0]; |
| 121 | for (size_t i = 0; i < char_count; ++i) { |
| 122 | uint16_t ch = GetUtf16FromUtf8(&cp); |
| 123 | if (ch == '$' || ch > 127) { |
| 124 | StringAppendF(&result, "_0%04x", ch); |
| 125 | } else { |
| 126 | switch (ch) { |
| 127 | case '_': |
| 128 | result += "_1"; |
| 129 | break; |
| 130 | case ';': |
| 131 | result += "_2"; |
| 132 | break; |
| 133 | case '[': |
| 134 | result += "_3"; |
| 135 | break; |
| 136 | case '/': |
| 137 | result += "_"; |
| 138 | break; |
| 139 | default: |
| 140 | result.push_back(ch); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | return result; |
| 146 | } |
| 147 | |
| 148 | std::string JniShortName(const Method* m) { |
| 149 | Class* declaring_class = m->GetDeclaringClass(); |
| 150 | |
| 151 | std::string class_name(declaring_class->GetDescriptor()->ToModifiedUtf8()); |
| 152 | // Remove the leading 'L' and trailing ';'... |
| 153 | CHECK(class_name[0] == 'L') << class_name; |
| 154 | CHECK(class_name[class_name.size() - 1] == ';') << class_name; |
| 155 | class_name.erase(0, 1); |
| 156 | class_name.erase(class_name.size() - 1, 1); |
| 157 | |
| 158 | std::string method_name(m->GetName()->ToModifiedUtf8()); |
| 159 | |
| 160 | std::string short_name; |
| 161 | short_name += "Java_"; |
| 162 | short_name += MangleForJni(class_name); |
| 163 | short_name += "_"; |
| 164 | short_name += MangleForJni(method_name); |
| 165 | return short_name; |
| 166 | } |
| 167 | |
| 168 | std::string JniLongName(const Method* m) { |
| 169 | std::string long_name; |
| 170 | long_name += JniShortName(m); |
| 171 | long_name += "__"; |
| 172 | |
| 173 | std::string signature(m->GetSignature()->ToModifiedUtf8()); |
| 174 | signature.erase(0, 1); |
| 175 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 176 | |
| 177 | long_name += MangleForJni(signature); |
| 178 | |
| 179 | return long_name; |
| 180 | } |
| 181 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 182 | } // namespace art |