blob: b908c4d5814a56abd3e22984618cdbebda347e03 [file] [log] [blame]
Elliott Hughes11e45072011-08-16 17:40:46 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: enh@google.com (Elliott Hughes)
3
buzbeec143c552011-08-20 17:38:58 -07004#include "file.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07005#include "object.h"
buzbeec143c552011-08-20 17:38:58 -07006#include "os.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07007#include "utils.h"
8
9namespace art {
10
buzbeec143c552011-08-20 17:38:58 -070011std::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 Hughese27955c2011-08-26 15:21:24 -070028std::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 Hughes5174fe62011-08-23 15:12:35 -070037std::string PrettyDescriptor(const String* java_descriptor) {
38 std::string descriptor(java_descriptor->ToModifiedUtf8());
39
Elliott Hughes11e45072011-08-16 17:40:46 -070040 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -070041 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -070042 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 Hughes5174fe62011-08-23 15:12:35 -070065 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070066 }
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 Hughesa2501992011-08-26 19:39:54 -070087std::string PrettyField(const Field* f) {
88 if (f == NULL) {
89 return "null";
90 }
91 std::string result(PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor()));
92 result += '.';
93 result += f->GetName()->ToModifiedUtf8();
94 return result;
95}
96
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070097std::string PrettyMethod(const Method* m, bool with_signature) {
98 if (m == NULL) {
99 return "null";
100 }
101 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700102 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700103 result += '.';
104 result += m->GetName()->ToModifiedUtf8();
105 if (with_signature) {
106 // TODO: iterate over the signature's elements and pass them all to
107 // PrettyDescriptor? We'd need to pull out the return type specially, too.
108 result += m->GetSignature()->ToModifiedUtf8();
109 }
110 return result;
111}
112
Elliott Hughes11e45072011-08-16 17:40:46 -0700113std::string PrettyType(const Object* obj) {
114 if (obj == NULL) {
115 return "null";
116 }
117 if (obj->GetClass() == NULL) {
118 return "(raw)";
119 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700120 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700121 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700122 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700123 }
124 return result;
125}
126
Elliott Hughes79082e32011-08-25 12:07:32 -0700127std::string MangleForJni(const std::string& s) {
128 std::string result;
129 size_t char_count = CountModifiedUtf8Chars(s.c_str());
130 const char* cp = &s[0];
131 for (size_t i = 0; i < char_count; ++i) {
132 uint16_t ch = GetUtf16FromUtf8(&cp);
133 if (ch == '$' || ch > 127) {
134 StringAppendF(&result, "_0%04x", ch);
135 } else {
136 switch (ch) {
137 case '_':
138 result += "_1";
139 break;
140 case ';':
141 result += "_2";
142 break;
143 case '[':
144 result += "_3";
145 break;
146 case '/':
147 result += "_";
148 break;
149 default:
150 result.push_back(ch);
151 break;
152 }
153 }
154 }
155 return result;
156}
157
158std::string JniShortName(const Method* m) {
159 Class* declaring_class = m->GetDeclaringClass();
160
161 std::string class_name(declaring_class->GetDescriptor()->ToModifiedUtf8());
162 // Remove the leading 'L' and trailing ';'...
163 CHECK(class_name[0] == 'L') << class_name;
164 CHECK(class_name[class_name.size() - 1] == ';') << class_name;
165 class_name.erase(0, 1);
166 class_name.erase(class_name.size() - 1, 1);
167
168 std::string method_name(m->GetName()->ToModifiedUtf8());
169
170 std::string short_name;
171 short_name += "Java_";
172 short_name += MangleForJni(class_name);
173 short_name += "_";
174 short_name += MangleForJni(method_name);
175 return short_name;
176}
177
178std::string JniLongName(const Method* m) {
179 std::string long_name;
180 long_name += JniShortName(m);
181 long_name += "__";
182
183 std::string signature(m->GetSignature()->ToModifiedUtf8());
184 signature.erase(0, 1);
185 signature.erase(signature.begin() + signature.find(')'), signature.end());
186
187 long_name += MangleForJni(signature);
188
189 return long_name;
190}
191
Elliott Hughes34023802011-08-30 12:06:17 -0700192void Split(const std::string& s, char delim, std::vector<std::string>& result) {
193 const char* p = s.data();
194 const char* end = p + s.size();
195 while (p != end) {
196 if (*p == delim) {
197 ++p;
198 } else {
199 const char* start = p;
200 while (++p != end && *p != delim) {
201 // Skip to the next occurrence of the delimiter.
202 }
203 result.push_back(std::string(start, p - start));
204 }
205 }
206}
207
Elliott Hughes11e45072011-08-16 17:40:46 -0700208} // namespace art