blob: 3be3fa6b01fec59100eb56ef6a9f03982cecd0b7 [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
Elliott Hughesd92bec42011-09-02 17:04:36 -070012bool 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 }
buzbeec143c552011-08-20 17:38:58 -070017
buzbeec143c552011-08-20 17:38:58 -070018 char buf[8 * KB];
19 while (true) {
20 int64_t n = file->Read(buf, sizeof(buf));
Elliott Hughesd92bec42011-09-02 17:04:36 -070021 if (n == -1) {
22 return false;
buzbeec143c552011-08-20 17:38:58 -070023 }
Elliott Hughesd92bec42011-09-02 17:04:36 -070024 if (n == 0) {
25 return true;
26 }
27 result->append(buf, n);
buzbeec143c552011-08-20 17:38:58 -070028 }
buzbeec143c552011-08-20 17:38:58 -070029}
30
Elliott Hughese27955c2011-08-26 15:21:24 -070031std::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 Hughes5174fe62011-08-23 15:12:35 -070040std::string PrettyDescriptor(const String* java_descriptor) {
41 std::string descriptor(java_descriptor->ToModifiedUtf8());
42
Elliott Hughes11e45072011-08-16 17:40:46 -070043 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -070044 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -070045 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 Hughes5174fe62011-08-23 15:12:35 -070068 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070069 }
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 Hughesa2501992011-08-26 19:39:54 -070090std::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 Hughesa0b8feb2011-08-20 09:50:55 -0700100std::string PrettyMethod(const Method* m, bool with_signature) {
101 if (m == NULL) {
102 return "null";
103 }
104 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700105 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700106 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 Hughes11e45072011-08-16 17:40:46 -0700116std::string PrettyType(const Object* obj) {
117 if (obj == NULL) {
118 return "null";
119 }
120 if (obj->GetClass() == NULL) {
121 return "(raw)";
122 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700123 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700124 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700125 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700126 }
127 return result;
128}
129
Elliott Hughes79082e32011-08-25 12:07:32 -0700130std::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
161std::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
181std::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 Hughes34023802011-08-30 12:06:17 -0700195void 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 Hughes11e45072011-08-16 17:40:46 -0700211} // namespace art