blob: 84b1556e810b84d79064041e3493f3791ac9dcb8 [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 Hughesa0b8feb2011-08-20 09:50:55 -070087std::string PrettyMethod(const Method* m, bool with_signature) {
88 if (m == NULL) {
89 return "null";
90 }
91 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -070092 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -070093 result += '.';
94 result += m->GetName()->ToModifiedUtf8();
95 if (with_signature) {
96 // TODO: iterate over the signature's elements and pass them all to
97 // PrettyDescriptor? We'd need to pull out the return type specially, too.
98 result += m->GetSignature()->ToModifiedUtf8();
99 }
100 return result;
101}
102
Elliott Hughes11e45072011-08-16 17:40:46 -0700103std::string PrettyType(const Object* obj) {
104 if (obj == NULL) {
105 return "null";
106 }
107 if (obj->GetClass() == NULL) {
108 return "(raw)";
109 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700110 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700111 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700112 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700113 }
114 return result;
115}
116
Elliott Hughes79082e32011-08-25 12:07:32 -0700117std::string MangleForJni(const std::string& s) {
118 std::string result;
119 size_t char_count = CountModifiedUtf8Chars(s.c_str());
120 const char* cp = &s[0];
121 for (size_t i = 0; i < char_count; ++i) {
122 uint16_t ch = GetUtf16FromUtf8(&cp);
123 if (ch == '$' || ch > 127) {
124 StringAppendF(&result, "_0%04x", ch);
125 } else {
126 switch (ch) {
127 case '_':
128 result += "_1";
129 break;
130 case ';':
131 result += "_2";
132 break;
133 case '[':
134 result += "_3";
135 break;
136 case '/':
137 result += "_";
138 break;
139 default:
140 result.push_back(ch);
141 break;
142 }
143 }
144 }
145 return result;
146}
147
148std::string JniShortName(const Method* m) {
149 Class* declaring_class = m->GetDeclaringClass();
150
151 std::string class_name(declaring_class->GetDescriptor()->ToModifiedUtf8());
152 // Remove the leading 'L' and trailing ';'...
153 CHECK(class_name[0] == 'L') << class_name;
154 CHECK(class_name[class_name.size() - 1] == ';') << class_name;
155 class_name.erase(0, 1);
156 class_name.erase(class_name.size() - 1, 1);
157
158 std::string method_name(m->GetName()->ToModifiedUtf8());
159
160 std::string short_name;
161 short_name += "Java_";
162 short_name += MangleForJni(class_name);
163 short_name += "_";
164 short_name += MangleForJni(method_name);
165 return short_name;
166}
167
168std::string JniLongName(const Method* m) {
169 std::string long_name;
170 long_name += JniShortName(m);
171 long_name += "__";
172
173 std::string signature(m->GetSignature()->ToModifiedUtf8());
174 signature.erase(0, 1);
175 signature.erase(signature.begin() + signature.find(')'), signature.end());
176
177 long_name += MangleForJni(signature);
178
179 return long_name;
180}
181
Elliott Hughes11e45072011-08-16 17:40:46 -0700182} // namespace art