blob: 90dd842553b9cd6b428821839efbaae18cbae403 [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 Hughes42ee1422011-09-06 12:33:32 -07004#include "utils.h"
5
6#include <sys/syscall.h>
7#include <sys/types.h>
8#include <unistd.h>
9
Elliott Hughes90a33692011-08-30 13:27:07 -070010#include "UniquePtr.h"
buzbeec143c552011-08-20 17:38:58 -070011#include "file.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070012#include "object.h"
buzbeec143c552011-08-20 17:38:58 -070013#include "os.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070014
15namespace art {
16
Elliott Hughesd92bec42011-09-02 17:04:36 -070017bool ReadFileToString(const std::string& file_name, std::string* result) {
18 UniquePtr<File> file(OS::OpenFile(file_name.c_str(), false));
19 if (file.get() == NULL) {
20 return false;
21 }
buzbeec143c552011-08-20 17:38:58 -070022
buzbeec143c552011-08-20 17:38:58 -070023 char buf[8 * KB];
24 while (true) {
25 int64_t n = file->Read(buf, sizeof(buf));
Elliott Hughesd92bec42011-09-02 17:04:36 -070026 if (n == -1) {
27 return false;
buzbeec143c552011-08-20 17:38:58 -070028 }
Elliott Hughesd92bec42011-09-02 17:04:36 -070029 if (n == 0) {
30 return true;
31 }
32 result->append(buf, n);
buzbeec143c552011-08-20 17:38:58 -070033 }
buzbeec143c552011-08-20 17:38:58 -070034}
35
Elliott Hughese27955c2011-08-26 15:21:24 -070036std::string GetIsoDate() {
37 time_t now = time(NULL);
38 struct tm tmbuf;
39 struct tm* ptm = localtime_r(&now, &tmbuf);
40 return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d",
41 ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday,
42 ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
43}
44
Elliott Hughes5174fe62011-08-23 15:12:35 -070045std::string PrettyDescriptor(const String* java_descriptor) {
46 std::string descriptor(java_descriptor->ToModifiedUtf8());
47
Elliott Hughes11e45072011-08-16 17:40:46 -070048 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -070049 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -070050 size_t dim = 0;
51 while (*c == '[') {
52 dim++;
53 c++;
54 }
55
56 // Reference or primitive?
57 if (*c == 'L') {
58 // "[[La/b/C;" -> "a.b.C[][]".
59 c++; // Skip the 'L'.
60 } else {
61 // "[[B" -> "byte[][]".
62 // To make life easier, we make primitives look like unqualified
63 // reference types.
64 switch (*c) {
65 case 'B': c = "byte;"; break;
66 case 'C': c = "char;"; break;
67 case 'D': c = "double;"; break;
68 case 'F': c = "float;"; break;
69 case 'I': c = "int;"; break;
70 case 'J': c = "long;"; break;
71 case 'S': c = "short;"; break;
72 case 'Z': c = "boolean;"; break;
Elliott Hughes5174fe62011-08-23 15:12:35 -070073 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070074 }
75 }
76
77 // At this point, 'c' is a string of the form "fully/qualified/Type;"
78 // or "primitive;". Rewrite the type with '.' instead of '/':
79 std::string result;
80 const char* p = c;
81 while (*p != ';') {
82 char ch = *p++;
83 if (ch == '/') {
84 ch = '.';
85 }
86 result.push_back(ch);
87 }
88 // ...and replace the semicolon with 'dim' "[]" pairs:
89 while (dim--) {
90 result += "[]";
91 }
92 return result;
93}
94
Elliott Hughesa2501992011-08-26 19:39:54 -070095std::string PrettyField(const Field* f) {
96 if (f == NULL) {
97 return "null";
98 }
99 std::string result(PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor()));
100 result += '.';
101 result += f->GetName()->ToModifiedUtf8();
102 return result;
103}
104
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700105std::string PrettyMethod(const Method* m, bool with_signature) {
106 if (m == NULL) {
107 return "null";
108 }
109 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700110 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700111 result += '.';
112 result += m->GetName()->ToModifiedUtf8();
113 if (with_signature) {
114 // TODO: iterate over the signature's elements and pass them all to
115 // PrettyDescriptor? We'd need to pull out the return type specially, too.
116 result += m->GetSignature()->ToModifiedUtf8();
117 }
118 return result;
119}
120
Elliott Hughes11e45072011-08-16 17:40:46 -0700121std::string PrettyType(const Object* obj) {
122 if (obj == NULL) {
123 return "null";
124 }
125 if (obj->GetClass() == NULL) {
126 return "(raw)";
127 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700128 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700129 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700130 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700131 }
132 return result;
133}
134
Elliott Hughes79082e32011-08-25 12:07:32 -0700135std::string MangleForJni(const std::string& s) {
136 std::string result;
137 size_t char_count = CountModifiedUtf8Chars(s.c_str());
138 const char* cp = &s[0];
139 for (size_t i = 0; i < char_count; ++i) {
140 uint16_t ch = GetUtf16FromUtf8(&cp);
141 if (ch == '$' || ch > 127) {
142 StringAppendF(&result, "_0%04x", ch);
143 } else {
144 switch (ch) {
145 case '_':
146 result += "_1";
147 break;
148 case ';':
149 result += "_2";
150 break;
151 case '[':
152 result += "_3";
153 break;
154 case '/':
155 result += "_";
156 break;
157 default:
158 result.push_back(ch);
159 break;
160 }
161 }
162 }
163 return result;
164}
165
166std::string JniShortName(const Method* m) {
167 Class* declaring_class = m->GetDeclaringClass();
168
169 std::string class_name(declaring_class->GetDescriptor()->ToModifiedUtf8());
170 // Remove the leading 'L' and trailing ';'...
171 CHECK(class_name[0] == 'L') << class_name;
172 CHECK(class_name[class_name.size() - 1] == ';') << class_name;
173 class_name.erase(0, 1);
174 class_name.erase(class_name.size() - 1, 1);
175
176 std::string method_name(m->GetName()->ToModifiedUtf8());
177
178 std::string short_name;
179 short_name += "Java_";
180 short_name += MangleForJni(class_name);
181 short_name += "_";
182 short_name += MangleForJni(method_name);
183 return short_name;
184}
185
186std::string JniLongName(const Method* m) {
187 std::string long_name;
188 long_name += JniShortName(m);
189 long_name += "__";
190
191 std::string signature(m->GetSignature()->ToModifiedUtf8());
192 signature.erase(0, 1);
193 signature.erase(signature.begin() + signature.find(')'), signature.end());
194
195 long_name += MangleForJni(signature);
196
197 return long_name;
198}
199
Elliott Hughes34023802011-08-30 12:06:17 -0700200void Split(const std::string& s, char delim, std::vector<std::string>& result) {
201 const char* p = s.data();
202 const char* end = p + s.size();
203 while (p != end) {
204 if (*p == delim) {
205 ++p;
206 } else {
207 const char* start = p;
208 while (++p != end && *p != delim) {
209 // Skip to the next occurrence of the delimiter.
210 }
211 result.push_back(std::string(start, p - start));
212 }
213 }
214}
215
Elliott Hughes11e45072011-08-16 17:40:46 -0700216} // namespace art
Elliott Hughes42ee1422011-09-06 12:33:32 -0700217
218// Neither bionic nor glibc exposes gettid(2).
219#define __KERNEL__
220#include <linux/unistd.h>
221namespace art {
222#ifdef _syscall0
223_syscall0(pid_t, GetTid)
224#else
225pid_t GetTid() { return syscall(__NR_gettid); }
226#endif
227} // namespace art
228#undef __KERNEL__