Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 17 | #include "utils.h" |
| 18 | |
Elliott Hughes | 06e3ad4 | 2012-02-07 14:51:57 -0800 | [diff] [blame] | 19 | #include <dynamic_annotations.h> |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 20 | #include <pthread.h> |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 21 | #include <sys/stat.h> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 22 | #include <sys/syscall.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 26 | #include "UniquePtr.h" |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 27 | #include "class_loader.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 28 | #include "file.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 29 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "object_utils.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 31 | #include "os.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 33 | #if !defined(HAVE_POSIX_CLOCKS) |
| 34 | #include <sys/time.h> |
| 35 | #endif |
| 36 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 37 | #if defined(HAVE_PRCTL) |
| 38 | #include <sys/prctl.h> |
| 39 | #endif |
| 40 | |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 41 | #if defined(__APPLE__) |
| 42 | #include "AvailabilityMacros.h" |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame^] | 43 | #include <sys/syscall.h> |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 46 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 47 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 48 | #endif |
| 49 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 50 | namespace art { |
| 51 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 52 | pid_t GetTid() { |
| 53 | #if defined(__APPLE__) |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame^] | 54 | // gettid(2) returns -1 on Darwin, but thread_selfid(2) works, and seems to be what their |
| 55 | // pthreads implementation uses. |
| 56 | return syscall(SYS_thread_selfid); |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 57 | #else |
| 58 | // Neither bionic nor glibc exposes gettid(2). |
| 59 | return syscall(__NR_gettid); |
| 60 | #endif |
| 61 | } |
| 62 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 63 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
| 64 | UniquePtr<File> file(OS::OpenFile(file_name.c_str(), false)); |
| 65 | if (file.get() == NULL) { |
| 66 | return false; |
| 67 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 68 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 69 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 70 | while (true) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 71 | int64_t n = file->Read(&buf[0], buf.size()); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 72 | if (n == -1) { |
| 73 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 74 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 75 | if (n == 0) { |
| 76 | return true; |
| 77 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 78 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 79 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 82 | std::string GetIsoDate() { |
| 83 | time_t now = time(NULL); |
| 84 | struct tm tmbuf; |
| 85 | struct tm* ptm = localtime_r(&now, &tmbuf); |
| 86 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d", |
| 87 | ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, |
| 88 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 89 | } |
| 90 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 91 | uint64_t MilliTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 92 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 93 | struct timespec now; |
| 94 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 95 | return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_nsec / 1000000LL; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 96 | #else |
| 97 | struct timeval now; |
| 98 | gettimeofday(&now, NULL); |
| 99 | return static_cast<uint64_t>(now.tv_sec) * 1000LL + now.tv_usec / 1000LL; |
| 100 | #endif |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 103 | uint64_t MicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 104 | #if defined(HAVE_POSIX_CLOCKS) |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 105 | struct timespec now; |
| 106 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 107 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_nsec / 1000LL; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 108 | #else |
| 109 | struct timeval now; |
| 110 | gettimeofday(&now, NULL); |
| 111 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_usec * 1000LL; |
| 112 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 115 | uint64_t NanoTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 116 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 117 | struct timespec now; |
| 118 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 119 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 120 | #else |
| 121 | struct timeval now; |
| 122 | gettimeofday(&now, NULL); |
| 123 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_usec * 1000LL; |
| 124 | #endif |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 125 | } |
| 126 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 127 | uint64_t ThreadCpuMicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 128 | #if defined(HAVE_POSIX_CLOCKS) |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 129 | struct timespec now; |
| 130 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
| 131 | return static_cast<uint64_t>(now.tv_sec) * 1000000LL + now.tv_nsec / 1000LL; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 132 | #else |
| 133 | UNIMPLEMENTED(WARNING); |
| 134 | return -1; |
| 135 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 138 | uint64_t ThreadCpuNanoTime() { |
| 139 | #if defined(HAVE_POSIX_CLOCKS) |
| 140 | struct timespec now; |
| 141 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
| 142 | return static_cast<uint64_t>(now.tv_sec) * 1000000000LL + now.tv_nsec; |
| 143 | #else |
| 144 | UNIMPLEMENTED(WARNING); |
| 145 | return -1; |
| 146 | #endif |
| 147 | } |
| 148 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 149 | std::string PrettyDescriptor(const String* java_descriptor) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 150 | if (java_descriptor == NULL) { |
| 151 | return "null"; |
| 152 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 153 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8()); |
| 154 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 155 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 156 | std::string PrettyDescriptor(const Class* klass) { |
| 157 | if (klass == NULL) { |
| 158 | return "null"; |
| 159 | } |
| 160 | return PrettyDescriptor(ClassHelper(klass).GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 163 | std::string PrettyDescriptor(const std::string& descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 164 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 165 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 166 | size_t dim = 0; |
| 167 | while (*c == '[') { |
| 168 | dim++; |
| 169 | c++; |
| 170 | } |
| 171 | |
| 172 | // Reference or primitive? |
| 173 | if (*c == 'L') { |
| 174 | // "[[La/b/C;" -> "a.b.C[][]". |
| 175 | c++; // Skip the 'L'. |
| 176 | } else { |
| 177 | // "[[B" -> "byte[][]". |
| 178 | // To make life easier, we make primitives look like unqualified |
| 179 | // reference types. |
| 180 | switch (*c) { |
| 181 | case 'B': c = "byte;"; break; |
| 182 | case 'C': c = "char;"; break; |
| 183 | case 'D': c = "double;"; break; |
| 184 | case 'F': c = "float;"; break; |
| 185 | case 'I': c = "int;"; break; |
| 186 | case 'J': c = "long;"; break; |
| 187 | case 'S': c = "short;"; break; |
| 188 | case 'Z': c = "boolean;"; break; |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 189 | case 'V': c = "void;"; break; // Used when decoding return types. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 190 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 195 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 196 | std::string result; |
| 197 | const char* p = c; |
| 198 | while (*p != ';') { |
| 199 | char ch = *p++; |
| 200 | if (ch == '/') { |
| 201 | ch = '.'; |
| 202 | } |
| 203 | result.push_back(ch); |
| 204 | } |
| 205 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 206 | while (dim--) { |
| 207 | result += "[]"; |
| 208 | } |
| 209 | return result; |
| 210 | } |
| 211 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 212 | std::string PrettyDescriptor(Primitive::Type type) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 213 | std::string descriptor_string(Primitive::Descriptor(type)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 214 | return PrettyDescriptor(descriptor_string); |
| 215 | } |
| 216 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 217 | std::string PrettyField(const Field* f, bool with_type) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 218 | if (f == NULL) { |
| 219 | return "null"; |
| 220 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 221 | FieldHelper fh(f); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 222 | std::string result; |
| 223 | if (with_type) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 224 | result += PrettyDescriptor(fh.GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 225 | result += ' '; |
| 226 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 227 | result += PrettyDescriptor(fh.GetDeclaringClassDescriptor()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 228 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 229 | result += fh.GetName(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 230 | return result; |
| 231 | } |
| 232 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 233 | std::string PrettyArguments(const char* signature) { |
| 234 | std::string result; |
| 235 | result += '('; |
| 236 | CHECK_EQ(*signature, '('); |
| 237 | ++signature; // Skip the '('. |
| 238 | while (*signature != ')') { |
| 239 | size_t argument_length = 0; |
| 240 | while (signature[argument_length] == '[') { |
| 241 | ++argument_length; |
| 242 | } |
| 243 | if (signature[argument_length] == 'L') { |
| 244 | argument_length = (strchr(signature, ';') - signature + 1); |
| 245 | } else { |
| 246 | ++argument_length; |
| 247 | } |
| 248 | std::string argument_descriptor(signature, argument_length); |
| 249 | result += PrettyDescriptor(argument_descriptor); |
| 250 | if (signature[argument_length] != ')') { |
| 251 | result += ", "; |
| 252 | } |
| 253 | signature += argument_length; |
| 254 | } |
| 255 | CHECK_EQ(*signature, ')'); |
| 256 | ++signature; // Skip the ')'. |
| 257 | result += ')'; |
| 258 | return result; |
| 259 | } |
| 260 | |
| 261 | std::string PrettyReturnType(const char* signature) { |
| 262 | const char* return_type = strchr(signature, ')'); |
| 263 | CHECK(return_type != NULL); |
| 264 | ++return_type; // Skip ')'. |
| 265 | return PrettyDescriptor(return_type); |
| 266 | } |
| 267 | |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 268 | std::string PrettyMethod(const Method* m, bool with_signature) { |
| 269 | if (m == NULL) { |
| 270 | return "null"; |
| 271 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 272 | MethodHelper mh(m); |
| 273 | std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 274 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 275 | result += mh.GetName(); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 276 | if (with_signature) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 277 | std::string signature(mh.GetSignature()); |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 278 | if (signature == "<no signature>") { |
| 279 | return result + signature; |
| 280 | } |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 281 | result = PrettyReturnType(signature.c_str()) + " " + result + PrettyArguments(signature.c_str()); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 282 | } |
| 283 | return result; |
| 284 | } |
| 285 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 286 | std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) { |
| 287 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 288 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 289 | result += '.'; |
| 290 | result += dex_file.GetMethodName(method_id); |
| 291 | if (with_signature) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 292 | std::string signature(dex_file.GetMethodSignature(method_id)); |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 293 | if (signature == "<no signature>") { |
| 294 | return result + signature; |
| 295 | } |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 296 | result = PrettyReturnType(signature.c_str()) + " " + result + PrettyArguments(signature.c_str()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 297 | } |
| 298 | return result; |
| 299 | } |
| 300 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 301 | std::string PrettyTypeOf(const Object* obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 302 | if (obj == NULL) { |
| 303 | return "null"; |
| 304 | } |
| 305 | if (obj->GetClass() == NULL) { |
| 306 | return "(raw)"; |
| 307 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 308 | ClassHelper kh(obj->GetClass()); |
| 309 | std::string result(PrettyDescriptor(kh.GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 310 | if (obj->IsClass()) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 311 | kh.ChangeClass(obj->AsClass()); |
| 312 | result += "<" + PrettyDescriptor(kh.GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 313 | } |
| 314 | return result; |
| 315 | } |
| 316 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 317 | std::string PrettyClass(const Class* c) { |
| 318 | if (c == NULL) { |
| 319 | return "null"; |
| 320 | } |
| 321 | std::string result; |
| 322 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 323 | result += PrettyDescriptor(c); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 324 | result += ">"; |
| 325 | return result; |
| 326 | } |
| 327 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 328 | std::string PrettyClassAndClassLoader(const Class* c) { |
| 329 | if (c == NULL) { |
| 330 | return "null"; |
| 331 | } |
| 332 | std::string result; |
| 333 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 334 | result += PrettyDescriptor(c); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 335 | result += ","; |
| 336 | result += PrettyTypeOf(c->GetClassLoader()); |
| 337 | // TODO: add an identifying hash value for the loader |
| 338 | result += ">"; |
| 339 | return result; |
| 340 | } |
| 341 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 342 | std::string PrettySize(size_t size_in_bytes) { |
| 343 | if ((size_in_bytes / GB) * GB == size_in_bytes) { |
| 344 | return StringPrintf("%zdGB", size_in_bytes / GB); |
| 345 | } else if ((size_in_bytes / MB) * MB == size_in_bytes) { |
| 346 | return StringPrintf("%zdMB", size_in_bytes / MB); |
| 347 | } else if ((size_in_bytes / KB) * KB == size_in_bytes) { |
| 348 | return StringPrintf("%zdKiB", size_in_bytes / KB); |
| 349 | } else { |
| 350 | return StringPrintf("%zdB", size_in_bytes); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | std::string PrettyDuration(uint64_t nano_duration) { |
| 355 | if (nano_duration == 0) { |
| 356 | return "0"; |
| 357 | } else { |
| 358 | const uint64_t one_sec = 1000 * 1000 * 1000; |
| 359 | const uint64_t one_ms = 1000 * 1000; |
| 360 | const uint64_t one_us = 1000; |
| 361 | const char* unit; |
| 362 | uint64_t divisor; |
| 363 | uint32_t zero_fill; |
| 364 | if (nano_duration >= one_sec) { |
| 365 | unit = "s"; |
| 366 | divisor = one_sec; |
| 367 | zero_fill = 9; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 368 | } else if (nano_duration >= one_ms) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 369 | unit = "ms"; |
| 370 | divisor = one_ms; |
| 371 | zero_fill = 6; |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 372 | } else if (nano_duration >= one_us) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 373 | unit = "us"; |
| 374 | divisor = one_us; |
| 375 | zero_fill = 3; |
| 376 | } else { |
| 377 | unit = "ns"; |
| 378 | divisor = 1; |
| 379 | zero_fill = 0; |
| 380 | } |
| 381 | uint64_t whole_part = nano_duration / divisor; |
| 382 | uint64_t fractional_part = nano_duration % divisor; |
| 383 | if (fractional_part == 0) { |
| 384 | return StringPrintf("%llu%s", whole_part, unit); |
| 385 | } else { |
| 386 | while ((fractional_part % 1000) == 0) { |
| 387 | zero_fill -= 3; |
| 388 | fractional_part /= 1000; |
| 389 | } |
| 390 | if (zero_fill == 3) { |
| 391 | return StringPrintf("%llu.%03llu%s", whole_part, fractional_part, unit); |
| 392 | } else if (zero_fill == 6) { |
| 393 | return StringPrintf("%llu.%06llu%s", whole_part, fractional_part, unit); |
| 394 | } else { |
| 395 | return StringPrintf("%llu.%09llu%s", whole_part, fractional_part, unit); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 401 | // See http://java.sun.com/j2se/1.5.0/docs/guide/jni/spec/design.html#wp615 for the full rules. |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 402 | std::string MangleForJni(const std::string& s) { |
| 403 | std::string result; |
| 404 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 405 | const char* cp = &s[0]; |
| 406 | for (size_t i = 0; i < char_count; ++i) { |
| 407 | uint16_t ch = GetUtf16FromUtf8(&cp); |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 408 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) { |
| 409 | result.push_back(ch); |
| 410 | } else if (ch == '.' || ch == '/') { |
| 411 | result += "_"; |
| 412 | } else if (ch == '_') { |
| 413 | result += "_1"; |
| 414 | } else if (ch == ';') { |
| 415 | result += "_2"; |
| 416 | } else if (ch == '[') { |
| 417 | result += "_3"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 418 | } else { |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 419 | StringAppendF(&result, "_0%04x", ch); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
| 422 | return result; |
| 423 | } |
| 424 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 425 | std::string DotToDescriptor(const char* class_name) { |
| 426 | std::string descriptor(class_name); |
| 427 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 428 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 429 | descriptor = "L" + descriptor + ";"; |
| 430 | } |
| 431 | return descriptor; |
| 432 | } |
| 433 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 434 | std::string DescriptorToDot(const char* descriptor) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 435 | size_t length = strlen(descriptor); |
| 436 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 437 | std::string result(descriptor + 1, length - 2); |
| 438 | std::replace(result.begin(), result.end(), '/', '.'); |
| 439 | return result; |
| 440 | } |
| 441 | return descriptor; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | std::string DescriptorToName(const char* descriptor) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 445 | size_t length = strlen(descriptor); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 446 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 447 | std::string result(descriptor + 1, length - 2); |
| 448 | return result; |
| 449 | } |
| 450 | return descriptor; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 453 | std::string JniShortName(const Method* m) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 454 | MethodHelper mh(m); |
| 455 | std::string class_name(mh.GetDeclaringClassDescriptor()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 456 | // Remove the leading 'L' and trailing ';'... |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 457 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 458 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 459 | class_name.erase(0, 1); |
| 460 | class_name.erase(class_name.size() - 1, 1); |
| 461 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 462 | std::string method_name(mh.GetName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 463 | |
| 464 | std::string short_name; |
| 465 | short_name += "Java_"; |
| 466 | short_name += MangleForJni(class_name); |
| 467 | short_name += "_"; |
| 468 | short_name += MangleForJni(method_name); |
| 469 | return short_name; |
| 470 | } |
| 471 | |
| 472 | std::string JniLongName(const Method* m) { |
| 473 | std::string long_name; |
| 474 | long_name += JniShortName(m); |
| 475 | long_name += "__"; |
| 476 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 477 | std::string signature(MethodHelper(m).GetSignature()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 478 | signature.erase(0, 1); |
| 479 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 480 | |
| 481 | long_name += MangleForJni(signature); |
| 482 | |
| 483 | return long_name; |
| 484 | } |
| 485 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 486 | // Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 487 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
| 488 | 0x00000000, // 00..1f low control characters; nothing valid |
| 489 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 490 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 491 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
| 492 | }; |
| 493 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 494 | // Helper for IsValidPartOfMemberNameUtf8(); do not call directly. |
| 495 | bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 496 | /* |
| 497 | * It's a multibyte encoded character. Decode it and analyze. We |
| 498 | * accept anything that isn't (a) an improperly encoded low value, |
| 499 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 500 | * control character, or (e) a high space, layout, or special |
| 501 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 502 | * U+fff0..U+ffff). This is all specified in the dex format |
| 503 | * document. |
| 504 | */ |
| 505 | |
| 506 | uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 507 | |
| 508 | // Perform follow-up tests based on the high 8 bits. |
| 509 | switch (utf16 >> 8) { |
| 510 | case 0x00: |
| 511 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 512 | return (utf16 > 0x00a0); |
| 513 | case 0xd8: |
| 514 | case 0xd9: |
| 515 | case 0xda: |
| 516 | case 0xdb: |
| 517 | // It's a leading surrogate. Check to see that a trailing |
| 518 | // surrogate follows. |
| 519 | utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 520 | return (utf16 >= 0xdc00) && (utf16 <= 0xdfff); |
| 521 | case 0xdc: |
| 522 | case 0xdd: |
| 523 | case 0xde: |
| 524 | case 0xdf: |
| 525 | // It's a trailing surrogate, which is not valid at this point. |
| 526 | return false; |
| 527 | case 0x20: |
| 528 | case 0xff: |
| 529 | // It's in the range that has spaces, controls, and specials. |
| 530 | switch (utf16 & 0xfff8) { |
| 531 | case 0x2000: |
| 532 | case 0x2008: |
| 533 | case 0x2028: |
| 534 | case 0xfff0: |
| 535 | case 0xfff8: |
| 536 | return false; |
| 537 | } |
| 538 | break; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 544 | * valid as part of a member name, updating the pointer to point past |
| 545 | * the consumed character. This will consume two encoded UTF-16 code |
| 546 | * points if the character is encoded as a surrogate pair. Also, if |
| 547 | * this function returns false, then the given pointer may only have |
| 548 | * been partially advanced. |
| 549 | */ |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 550 | bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 551 | uint8_t c = (uint8_t) **pUtf8Ptr; |
| 552 | if (c <= 0x7f) { |
| 553 | // It's low-ascii, so check the table. |
| 554 | uint32_t wordIdx = c >> 5; |
| 555 | uint32_t bitIdx = c & 0x1f; |
| 556 | (*pUtf8Ptr)++; |
| 557 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 558 | } |
| 559 | |
| 560 | // It's a multibyte encoded character. Call a non-inline function |
| 561 | // for the heavy lifting. |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 562 | return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr); |
| 563 | } |
| 564 | |
| 565 | bool IsValidMemberName(const char* s) { |
| 566 | bool angle_name = false; |
| 567 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 568 | switch (*s) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 569 | case '\0': |
| 570 | // The empty string is not a valid name. |
| 571 | return false; |
| 572 | case '<': |
| 573 | angle_name = true; |
| 574 | s++; |
| 575 | break; |
| 576 | } |
| 577 | |
| 578 | while (true) { |
| 579 | switch (*s) { |
| 580 | case '\0': |
| 581 | return !angle_name; |
| 582 | case '>': |
| 583 | return angle_name && s[1] == '\0'; |
| 584 | } |
| 585 | |
| 586 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
| 587 | return false; |
| 588 | } |
| 589 | } |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 592 | enum ClassNameType { kName, kDescriptor }; |
| 593 | bool IsValidClassName(const char* s, ClassNameType type, char separator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 594 | int arrayCount = 0; |
| 595 | while (*s == '[') { |
| 596 | arrayCount++; |
| 597 | s++; |
| 598 | } |
| 599 | |
| 600 | if (arrayCount > 255) { |
| 601 | // Arrays may have no more than 255 dimensions. |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | if (arrayCount != 0) { |
| 606 | /* |
| 607 | * If we're looking at an array of some sort, then it doesn't |
| 608 | * matter if what is being asked for is a class name; the |
| 609 | * format looks the same as a type descriptor in that case, so |
| 610 | * treat it as such. |
| 611 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 612 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 613 | } |
| 614 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 615 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 616 | /* |
| 617 | * We are looking for a descriptor. Either validate it as a |
| 618 | * single-character primitive type, or continue on to check the |
| 619 | * embedded class name (bracketed by "L" and ";"). |
| 620 | */ |
| 621 | switch (*(s++)) { |
| 622 | case 'B': |
| 623 | case 'C': |
| 624 | case 'D': |
| 625 | case 'F': |
| 626 | case 'I': |
| 627 | case 'J': |
| 628 | case 'S': |
| 629 | case 'Z': |
| 630 | // These are all single-character descriptors for primitive types. |
| 631 | return (*s == '\0'); |
| 632 | case 'V': |
| 633 | // Non-array void is valid, but you can't have an array of void. |
| 634 | return (arrayCount == 0) && (*s == '\0'); |
| 635 | case 'L': |
| 636 | // Class name: Break out and continue below. |
| 637 | break; |
| 638 | default: |
| 639 | // Oddball descriptor character. |
| 640 | return false; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * We just consumed the 'L' that introduces a class name as part |
| 646 | * of a type descriptor, or we are looking for an unadorned class |
| 647 | * name. |
| 648 | */ |
| 649 | |
| 650 | bool sepOrFirst = true; // first character or just encountered a separator. |
| 651 | for (;;) { |
| 652 | uint8_t c = (uint8_t) *s; |
| 653 | switch (c) { |
| 654 | case '\0': |
| 655 | /* |
| 656 | * Premature end for a type descriptor, but valid for |
| 657 | * a class name as long as we haven't encountered an |
| 658 | * empty component (including the degenerate case of |
| 659 | * the empty string ""). |
| 660 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 661 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 662 | case ';': |
| 663 | /* |
| 664 | * Invalid character for a class name, but the |
| 665 | * legitimate end of a type descriptor. In the latter |
| 666 | * case, make sure that this is the end of the string |
| 667 | * and that it doesn't end with an empty component |
| 668 | * (including the degenerate case of "L;"). |
| 669 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 670 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 671 | case '/': |
| 672 | case '.': |
| 673 | if (c != separator) { |
| 674 | // The wrong separator character. |
| 675 | return false; |
| 676 | } |
| 677 | if (sepOrFirst) { |
| 678 | // Separator at start or two separators in a row. |
| 679 | return false; |
| 680 | } |
| 681 | sepOrFirst = true; |
| 682 | s++; |
| 683 | break; |
| 684 | default: |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 685 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 686 | return false; |
| 687 | } |
| 688 | sepOrFirst = false; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 694 | bool IsValidBinaryClassName(const char* s) { |
| 695 | return IsValidClassName(s, kName, '.'); |
| 696 | } |
| 697 | |
| 698 | bool IsValidJniClassName(const char* s) { |
| 699 | return IsValidClassName(s, kName, '/'); |
| 700 | } |
| 701 | |
| 702 | bool IsValidDescriptor(const char* s) { |
| 703 | return IsValidClassName(s, kDescriptor, '/'); |
| 704 | } |
| 705 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 706 | void Split(const std::string& s, char separator, std::vector<std::string>& result) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 707 | const char* p = s.data(); |
| 708 | const char* end = p + s.size(); |
| 709 | while (p != end) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 710 | if (*p == separator) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 711 | ++p; |
| 712 | } else { |
| 713 | const char* start = p; |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 714 | while (++p != end && *p != separator) { |
| 715 | // Skip to the next occurrence of the separator. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 716 | } |
| 717 | result.push_back(std::string(start, p - start)); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 722 | template <typename StringT> |
| 723 | std::string Join(std::vector<StringT>& strings, char separator) { |
| 724 | if (strings.empty()) { |
| 725 | return ""; |
| 726 | } |
| 727 | |
| 728 | std::string result(strings[0]); |
| 729 | for (size_t i = 1; i < strings.size(); ++i) { |
| 730 | result += separator; |
| 731 | result += strings[i]; |
| 732 | } |
| 733 | return result; |
| 734 | } |
| 735 | |
| 736 | // Explicit instantiations. |
| 737 | template std::string Join<std::string>(std::vector<std::string>& strings, char separator); |
| 738 | template std::string Join<const char*>(std::vector<const char*>& strings, char separator); |
| 739 | template std::string Join<char*>(std::vector<char*>& strings, char separator); |
| 740 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 741 | bool StartsWith(const std::string& s, const char* prefix) { |
| 742 | return s.compare(0, strlen(prefix), prefix) == 0; |
| 743 | } |
| 744 | |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 745 | bool EndsWith(const std::string& s, const char* suffix) { |
| 746 | size_t suffix_length = strlen(suffix); |
| 747 | size_t string_length = s.size(); |
| 748 | if (suffix_length > string_length) { |
| 749 | return false; |
| 750 | } |
| 751 | size_t offset = string_length - suffix_length; |
| 752 | return s.compare(offset, suffix_length, suffix) == 0; |
| 753 | } |
| 754 | |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 755 | void SetThreadName(const char* thread_name) { |
| 756 | ANNOTATE_THREAD_NAME(thread_name); // For tsan. |
Elliott Hughes | 06e3ad4 | 2012-02-07 14:51:57 -0800 | [diff] [blame] | 757 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 758 | int hasAt = 0; |
| 759 | int hasDot = 0; |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 760 | const char* s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 761 | while (*s) { |
| 762 | if (*s == '.') { |
| 763 | hasDot = 1; |
| 764 | } else if (*s == '@') { |
| 765 | hasAt = 1; |
| 766 | } |
| 767 | s++; |
| 768 | } |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 769 | int len = s - thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 770 | if (len < 15 || hasAt || !hasDot) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 771 | s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 772 | } else { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 773 | s = thread_name + len - 15; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 774 | } |
| 775 | #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP) |
Elliott Hughes | 7c6a61e | 2012-03-12 18:01:41 -0700 | [diff] [blame] | 776 | // pthread_setname_np fails rather than truncating long strings. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 777 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 778 | strncpy(buf, s, sizeof(buf)-1); |
| 779 | buf[sizeof(buf)-1] = '\0'; |
| 780 | errno = pthread_setname_np(pthread_self(), buf); |
| 781 | if (errno != 0) { |
| 782 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 783 | } |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 784 | #elif defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 785 | pthread_setname_np(thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 786 | #elif defined(HAVE_PRCTL) |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 787 | prctl(PR_SET_NAME, (unsigned long) s, 0, 0, 0); // NOLINT (unsigned long) |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 788 | #else |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 789 | UNIMPLEMENTED(WARNING) << thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 790 | #endif |
| 791 | } |
| 792 | |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 793 | void GetTaskStats(pid_t tid, int& utime, int& stime, int& task_cpu) { |
| 794 | utime = stime = task_cpu = 0; |
| 795 | std::string stats; |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 796 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid).c_str(), &stats)) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | // Skip the command, which may contain spaces. |
| 800 | stats = stats.substr(stats.find(')') + 2); |
| 801 | // Extract the three fields we care about. |
| 802 | std::vector<std::string> fields; |
| 803 | Split(stats, ' ', fields); |
| 804 | utime = strtoull(fields[11].c_str(), NULL, 10); |
| 805 | stime = strtoull(fields[12].c_str(), NULL, 10); |
| 806 | task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
| 807 | } |
| 808 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 809 | std::string GetSchedulerGroupName(pid_t tid) { |
| 810 | // /proc/<pid>/cgroup looks like this: |
| 811 | // 2:devices:/ |
| 812 | // 1:cpuacct,cpu:/ |
| 813 | // We want the third field from the line whose second field contains the "cpu" token. |
| 814 | std::string cgroup_file; |
| 815 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) { |
| 816 | return ""; |
| 817 | } |
| 818 | std::vector<std::string> cgroup_lines; |
| 819 | Split(cgroup_file, '\n', cgroup_lines); |
| 820 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 821 | std::vector<std::string> cgroup_fields; |
| 822 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 823 | std::vector<std::string> cgroups; |
| 824 | Split(cgroup_fields[1], ',', cgroups); |
| 825 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 826 | if (cgroups[i] == "cpu") { |
| 827 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | return ""; |
| 832 | } |
| 833 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 834 | const char* GetAndroidRoot() { |
| 835 | const char* android_root = getenv("ANDROID_ROOT"); |
| 836 | if (android_root == NULL) { |
| 837 | if (OS::DirectoryExists("/system")) { |
| 838 | android_root = "/system"; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 839 | } else { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 840 | LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist"; |
| 841 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 842 | } |
| 843 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 844 | if (!OS::DirectoryExists(android_root)) { |
| 845 | LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 846 | return ""; |
| 847 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 848 | return android_root; |
| 849 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 850 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 851 | const char* GetAndroidData() { |
| 852 | const char* android_data = getenv("ANDROID_DATA"); |
| 853 | if (android_data == NULL) { |
| 854 | if (OS::DirectoryExists("/data")) { |
| 855 | android_data = "/data"; |
| 856 | } else { |
| 857 | LOG(FATAL) << "ANDROID_DATA not set and /data does not exist"; |
| 858 | return ""; |
| 859 | } |
| 860 | } |
| 861 | if (!OS::DirectoryExists(android_data)) { |
| 862 | LOG(FATAL) << "Failed to find ANDROID_DATA directory " << android_data; |
| 863 | return ""; |
| 864 | } |
| 865 | return android_data; |
| 866 | } |
| 867 | |
| 868 | std::string GetArtCacheOrDie() { |
| 869 | std::string art_cache(StringPrintf("%s/art-cache", GetAndroidData())); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 870 | |
| 871 | if (!OS::DirectoryExists(art_cache.c_str())) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 872 | if (StartsWith(art_cache, "/tmp/")) { |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 873 | int result = mkdir(art_cache.c_str(), 0700); |
| 874 | if (result != 0) { |
| 875 | LOG(FATAL) << "Failed to create art-cache directory " << art_cache; |
| 876 | return ""; |
| 877 | } |
| 878 | } else { |
| 879 | LOG(FATAL) << "Failed to find art-cache directory " << art_cache; |
| 880 | return ""; |
| 881 | } |
| 882 | } |
| 883 | return art_cache; |
| 884 | } |
| 885 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 886 | std::string GetArtCacheFilenameOrDie(const std::string& location) { |
Elliott Hughes | 9557241 | 2011-12-13 18:14:20 -0800 | [diff] [blame] | 887 | std::string art_cache(GetArtCacheOrDie()); |
Elliott Hughes | c308a5d | 2012-02-16 17:12:06 -0800 | [diff] [blame] | 888 | CHECK_EQ(location[0], '/') << location; |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 889 | std::string cache_file(location, 1); // skip leading slash |
| 890 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
| 891 | return art_cache + "/" + cache_file; |
| 892 | } |
| 893 | |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 894 | bool IsValidZipFilename(const std::string& filename) { |
| 895 | if (filename.size() < 4) { |
| 896 | return false; |
| 897 | } |
| 898 | std::string suffix(filename.substr(filename.size() - 4)); |
| 899 | return (suffix == ".zip" || suffix == ".jar" || suffix == ".apk"); |
| 900 | } |
| 901 | |
| 902 | bool IsValidDexFilename(const std::string& filename) { |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 903 | return EndsWith(filename, ".dex"); |
| 904 | } |
| 905 | |
| 906 | bool IsValidOatFilename(const std::string& filename) { |
| 907 | return EndsWith(filename, ".oat"); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 910 | } // namespace art |