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