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