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