blob: d890d2927d5aa4376c99582dd7454e4b0ced7878 [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
Elliott Hughesdcc24742011-09-07 14:02:44 -070015#if defined(HAVE_PRCTL)
16#include <sys/prctl.h>
17#endif
18
Elliott Hughes11e45072011-08-16 17:40:46 -070019namespace art {
20
Elliott Hughesd92bec42011-09-02 17:04:36 -070021bool ReadFileToString(const std::string& file_name, std::string* result) {
22 UniquePtr<File> file(OS::OpenFile(file_name.c_str(), false));
23 if (file.get() == NULL) {
24 return false;
25 }
buzbeec143c552011-08-20 17:38:58 -070026
buzbeec143c552011-08-20 17:38:58 -070027 char buf[8 * KB];
28 while (true) {
29 int64_t n = file->Read(buf, sizeof(buf));
Elliott Hughesd92bec42011-09-02 17:04:36 -070030 if (n == -1) {
31 return false;
buzbeec143c552011-08-20 17:38:58 -070032 }
Elliott Hughesd92bec42011-09-02 17:04:36 -070033 if (n == 0) {
34 return true;
35 }
36 result->append(buf, n);
buzbeec143c552011-08-20 17:38:58 -070037 }
buzbeec143c552011-08-20 17:38:58 -070038}
39
Elliott Hughese27955c2011-08-26 15:21:24 -070040std::string GetIsoDate() {
41 time_t now = time(NULL);
42 struct tm tmbuf;
43 struct tm* ptm = localtime_r(&now, &tmbuf);
44 return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d",
45 ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday,
46 ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
47}
48
Elliott Hughes5174fe62011-08-23 15:12:35 -070049std::string PrettyDescriptor(const String* java_descriptor) {
50 std::string descriptor(java_descriptor->ToModifiedUtf8());
51
Elliott Hughes11e45072011-08-16 17:40:46 -070052 // Count the number of '['s to get the dimensionality.
Elliott Hughes5174fe62011-08-23 15:12:35 -070053 const char* c = descriptor.c_str();
Elliott Hughes11e45072011-08-16 17:40:46 -070054 size_t dim = 0;
55 while (*c == '[') {
56 dim++;
57 c++;
58 }
59
60 // Reference or primitive?
61 if (*c == 'L') {
62 // "[[La/b/C;" -> "a.b.C[][]".
63 c++; // Skip the 'L'.
64 } else {
65 // "[[B" -> "byte[][]".
66 // To make life easier, we make primitives look like unqualified
67 // reference types.
68 switch (*c) {
69 case 'B': c = "byte;"; break;
70 case 'C': c = "char;"; break;
71 case 'D': c = "double;"; break;
72 case 'F': c = "float;"; break;
73 case 'I': c = "int;"; break;
74 case 'J': c = "long;"; break;
75 case 'S': c = "short;"; break;
76 case 'Z': c = "boolean;"; break;
Elliott Hughes5174fe62011-08-23 15:12:35 -070077 default: return descriptor;
Elliott Hughes11e45072011-08-16 17:40:46 -070078 }
79 }
80
81 // At this point, 'c' is a string of the form "fully/qualified/Type;"
82 // or "primitive;". Rewrite the type with '.' instead of '/':
83 std::string result;
84 const char* p = c;
85 while (*p != ';') {
86 char ch = *p++;
87 if (ch == '/') {
88 ch = '.';
89 }
90 result.push_back(ch);
91 }
92 // ...and replace the semicolon with 'dim' "[]" pairs:
93 while (dim--) {
94 result += "[]";
95 }
96 return result;
97}
98
Elliott Hughesa2501992011-08-26 19:39:54 -070099std::string PrettyField(const Field* f) {
100 if (f == NULL) {
101 return "null";
102 }
103 std::string result(PrettyDescriptor(f->GetDeclaringClass()->GetDescriptor()));
104 result += '.';
105 result += f->GetName()->ToModifiedUtf8();
106 return result;
107}
108
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700109std::string PrettyMethod(const Method* m, bool with_signature) {
110 if (m == NULL) {
111 return "null";
112 }
113 Class* c = m->GetDeclaringClass();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700114 std::string result(PrettyDescriptor(c->GetDescriptor()));
Elliott Hughesa0b8feb2011-08-20 09:50:55 -0700115 result += '.';
116 result += m->GetName()->ToModifiedUtf8();
117 if (with_signature) {
118 // TODO: iterate over the signature's elements and pass them all to
119 // PrettyDescriptor? We'd need to pull out the return type specially, too.
120 result += m->GetSignature()->ToModifiedUtf8();
121 }
122 return result;
123}
124
Elliott Hughes11e45072011-08-16 17:40:46 -0700125std::string PrettyType(const Object* obj) {
126 if (obj == NULL) {
127 return "null";
128 }
129 if (obj->GetClass() == NULL) {
130 return "(raw)";
131 }
Elliott Hughes5174fe62011-08-23 15:12:35 -0700132 std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700133 if (obj->IsClass()) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700134 result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">";
Elliott Hughes11e45072011-08-16 17:40:46 -0700135 }
136 return result;
137}
138
Elliott Hughes79082e32011-08-25 12:07:32 -0700139std::string MangleForJni(const std::string& s) {
140 std::string result;
141 size_t char_count = CountModifiedUtf8Chars(s.c_str());
142 const char* cp = &s[0];
143 for (size_t i = 0; i < char_count; ++i) {
144 uint16_t ch = GetUtf16FromUtf8(&cp);
145 if (ch == '$' || ch > 127) {
146 StringAppendF(&result, "_0%04x", ch);
147 } else {
148 switch (ch) {
149 case '_':
150 result += "_1";
151 break;
152 case ';':
153 result += "_2";
154 break;
155 case '[':
156 result += "_3";
157 break;
158 case '/':
159 result += "_";
160 break;
161 default:
162 result.push_back(ch);
163 break;
164 }
165 }
166 }
167 return result;
168}
169
170std::string JniShortName(const Method* m) {
171 Class* declaring_class = m->GetDeclaringClass();
172
173 std::string class_name(declaring_class->GetDescriptor()->ToModifiedUtf8());
174 // Remove the leading 'L' and trailing ';'...
175 CHECK(class_name[0] == 'L') << class_name;
176 CHECK(class_name[class_name.size() - 1] == ';') << class_name;
177 class_name.erase(0, 1);
178 class_name.erase(class_name.size() - 1, 1);
179
180 std::string method_name(m->GetName()->ToModifiedUtf8());
181
182 std::string short_name;
183 short_name += "Java_";
184 short_name += MangleForJni(class_name);
185 short_name += "_";
186 short_name += MangleForJni(method_name);
187 return short_name;
188}
189
190std::string JniLongName(const Method* m) {
191 std::string long_name;
192 long_name += JniShortName(m);
193 long_name += "__";
194
195 std::string signature(m->GetSignature()->ToModifiedUtf8());
196 signature.erase(0, 1);
197 signature.erase(signature.begin() + signature.find(')'), signature.end());
198
199 long_name += MangleForJni(signature);
200
201 return long_name;
202}
203
Elliott Hughes34023802011-08-30 12:06:17 -0700204void Split(const std::string& s, char delim, std::vector<std::string>& result) {
205 const char* p = s.data();
206 const char* end = p + s.size();
207 while (p != end) {
208 if (*p == delim) {
209 ++p;
210 } else {
211 const char* start = p;
212 while (++p != end && *p != delim) {
213 // Skip to the next occurrence of the delimiter.
214 }
215 result.push_back(std::string(start, p - start));
216 }
217 }
218}
219
Elliott Hughesdcc24742011-09-07 14:02:44 -0700220void SetThreadName(const char *threadName) {
221 int hasAt = 0;
222 int hasDot = 0;
223 const char *s = threadName;
224 while (*s) {
225 if (*s == '.') {
226 hasDot = 1;
227 } else if (*s == '@') {
228 hasAt = 1;
229 }
230 s++;
231 }
232 int len = s - threadName;
233 if (len < 15 || hasAt || !hasDot) {
234 s = threadName;
235 } else {
236 s = threadName + len - 15;
237 }
238#if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP)
239 /* pthread_setname_np fails rather than truncating long strings */
240 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
241 strncpy(buf, s, sizeof(buf)-1);
242 buf[sizeof(buf)-1] = '\0';
243 errno = pthread_setname_np(pthread_self(), buf);
244 if (errno != 0) {
245 PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
246 }
247#elif defined(HAVE_PRCTL)
248 prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0);
249#else
250#error no implementation for SetThreadName
251#endif
252}
253
Elliott Hughes11e45072011-08-16 17:40:46 -0700254} // namespace art
Elliott Hughes42ee1422011-09-06 12:33:32 -0700255
256// Neither bionic nor glibc exposes gettid(2).
257#define __KERNEL__
258#include <linux/unistd.h>
259namespace art {
260#ifdef _syscall0
261_syscall0(pid_t, GetTid)
262#else
263pid_t GetTid() { return syscall(__NR_gettid); }
264#endif
265} // namespace art
266#undef __KERNEL__