Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: enh@google.com (Elliott Hughes) |
| 3 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 4 | #include "utils.h" |
| 5 | |
| 6 | #include <sys/syscall.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <unistd.h> |
| 9 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 10 | #include "UniquePtr.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 11 | #include "file.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 12 | #include "object.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 13 | #include "os.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 14 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 15 | #if defined(HAVE_PRCTL) |
| 16 | #include <sys/prctl.h> |
| 17 | #endif |
| 18 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 19 | namespace art { |
| 20 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 21 | bool 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 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 26 | |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 27 | char buf[8 * KB]; |
| 28 | while (true) { |
| 29 | int64_t n = file->Read(buf, sizeof(buf)); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 30 | if (n == -1) { |
| 31 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 32 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 33 | if (n == 0) { |
| 34 | return true; |
| 35 | } |
| 36 | result->append(buf, n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 37 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 40 | std::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 Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 49 | std::string PrettyDescriptor(const String* java_descriptor) { |
| 50 | std::string descriptor(java_descriptor->ToModifiedUtf8()); |
| 51 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 52 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 53 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 54 | 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 Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 77 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 78 | } |
| 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 Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 99 | std::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 Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 109 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 110 | if (m == NULL) { |
| 111 | return "null"; |
| 112 | } |
| 113 | Class* c = m->GetDeclaringClass(); |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 114 | std::string result(PrettyDescriptor(c->GetDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 115 | 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 125 | std::string PrettyType(const Object* obj) { |
| 126 | if (obj == NULL) { |
| 127 | return "null"; |
| 128 | } |
| 129 | if (obj->GetClass() == NULL) { |
| 130 | return "(raw)"; |
| 131 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 132 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 133 | if (obj->IsClass()) { |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 134 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 135 | } |
| 136 | return result; |
| 137 | } |
| 138 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 139 | std::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 | |
| 170 | std::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 | |
| 190 | std::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 Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 204 | void 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 Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame^] | 220 | void 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 254 | } // namespace art |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 255 | |
| 256 | // Neither bionic nor glibc exposes gettid(2). |
| 257 | #define __KERNEL__ |
| 258 | #include <linux/unistd.h> |
| 259 | namespace art { |
| 260 | #ifdef _syscall0 |
| 261 | _syscall0(pid_t, GetTid) |
| 262 | #else |
| 263 | pid_t GetTid() { return syscall(__NR_gettid); } |
| 264 | #endif |
| 265 | } // namespace art |
| 266 | #undef __KERNEL__ |