blob: f9575804254cf2b54e046e06bebc45a9756b0c97 [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
Elliott Hughes90a33692011-08-30 13:27:07 -07004#include "UniquePtr.h"
buzbeec143c552011-08-20 17:38:58 -07005#include "file.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07006#include "object.h"
buzbeec143c552011-08-20 17:38:58 -07007#include "os.h"
Elliott Hughes11e45072011-08-16 17:40:46 -07008#include "utils.h"
9
10namespace art {
11
buzbeec143c552011-08-20 17:38:58 -070012std::string ReadFileToString(const char* file_name) {
Elliott Hughes90a33692011-08-30 13:27:07 -070013 UniquePtr<File> file(OS::OpenFile(file_name, false));
14 CHECK(file.get() != NULL);
buzbeec143c552011-08-20 17:38:58 -070015
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 Hughese27955c2011-08-26 15:21:24 -070029std::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 Hughes5174fe62011-08-23 15:12:35 -070038std::string PrettyDescriptor(const String* java_descriptor) {
39 std::string descriptor(java_descriptor->ToModifiedUtf8());
40
Elliott Hughes11e45072011-08-16 17:40:46 -070041 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -070042 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -070043 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 Hughes5174fe62011-08-23 15:12:35 -070066 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070067 }
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 Hughesa2501992011-08-26 19:39:54 -070088std::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 Hughesa0b8feb2011-08-20 09:50:55 -070098std::string PrettyMethod(const Method* m, bool with_signature) {
99 if (m == NULL) {
100 return "null";
101 }
102 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700103 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700104 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 Hughes11e45072011-08-16 17:40:46 -0700114std::string PrettyType(const Object* obj) {
115 if (obj == NULL) {
116 return "null";
117 }
118 if (obj->GetClass() == NULL) {
119 return "(raw)";
120 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700121 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700122 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700123 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700124 }
125 return result;
126}
127
Elliott Hughes79082e32011-08-25 12:07:32 -0700128std::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
159std::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
179std::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 Hughes34023802011-08-30 12:06:17 -0700193void 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 Hughes11e45072011-08-16 17:40:46 -0700209} // namespace art