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