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