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 | |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 19 | #include <inttypes.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> |
Brian Carlstrom | 4cf5e57 | 2014-02-25 11:47:48 -0800 | [diff] [blame] | 24 | #include <sys/wait.h> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 25 | #include <unistd.h> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 26 | #include <memory> |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 27 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 28 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 29 | #include "base/unix_file/fd_file.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 30 | #include "dex_file-inl.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 31 | #include "mirror/art_field-inl.h" |
| 32 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 33 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 34 | #include "mirror/class_loader.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | #include "mirror/object-inl.h" |
| 36 | #include "mirror/object_array-inl.h" |
| 37 | #include "mirror/string.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 38 | #include "object_utils.h" |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 39 | #include "os.h" |
Kenny Root | 067d20f | 2014-03-05 14:57:21 -0800 | [diff] [blame] | 40 | #include "scoped_thread_state_change.h" |
Ian Rogers | a672490 | 2013-09-23 09:23:37 -0700 | [diff] [blame] | 41 | #include "utf-inl.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 43 | #if !defined(HAVE_POSIX_CLOCKS) |
| 44 | #include <sys/time.h> |
| 45 | #endif |
| 46 | |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 47 | #if defined(HAVE_PRCTL) |
| 48 | #include <sys/prctl.h> |
| 49 | #endif |
| 50 | |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 51 | #if defined(__APPLE__) |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 52 | #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 53 | #include <sys/syscall.h> |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 54 | #endif |
| 55 | |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 56 | #include <backtrace/Backtrace.h> // For DumpNativeStack. |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 58 | #if defined(__linux__) |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 59 | #include <linux/unistd.h> |
Elliott Hughes | e1aee69 | 2012-01-17 16:40:10 -0800 | [diff] [blame] | 60 | #endif |
| 61 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 62 | namespace art { |
| 63 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 64 | pid_t GetTid() { |
Brian Carlstrom | f3a2641 | 2012-08-24 11:06:02 -0700 | [diff] [blame] | 65 | #if defined(__APPLE__) |
| 66 | uint64_t owner; |
| 67 | CHECK_PTHREAD_CALL(pthread_threadid_np, (NULL, &owner), __FUNCTION__); // Requires Mac OS 10.6 |
| 68 | return owner; |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 69 | #else |
| 70 | // Neither bionic nor glibc exposes gettid(2). |
| 71 | return syscall(__NR_gettid); |
| 72 | #endif |
| 73 | } |
| 74 | |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 75 | std::string GetThreadName(pid_t tid) { |
| 76 | std::string result; |
| 77 | if (ReadFileToString(StringPrintf("/proc/self/task/%d/comm", tid), &result)) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 78 | result.resize(result.size() - 1); // Lose the trailing '\n'. |
Elliott Hughes | 289be85 | 2012-06-12 13:57:20 -0700 | [diff] [blame] | 79 | } else { |
| 80 | result = "<unknown>"; |
| 81 | } |
| 82 | return result; |
| 83 | } |
| 84 | |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 85 | void GetThreadStack(pthread_t thread, void** stack_base, size_t* stack_size) { |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 86 | #if defined(__APPLE__) |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 87 | *stack_size = pthread_get_stacksize_np(thread); |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 88 | void* stack_addr = pthread_get_stackaddr_np(thread); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 89 | |
| 90 | // Check whether stack_addr is the base or end of the stack. |
| 91 | // (On Mac OS 10.7, it's the end.) |
| 92 | int stack_variable; |
| 93 | if (stack_addr > &stack_variable) { |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 94 | *stack_base = reinterpret_cast<byte*>(stack_addr) - *stack_size; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 95 | } else { |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 96 | *stack_base = stack_addr; |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 97 | } |
| 98 | #else |
| 99 | pthread_attr_t attributes; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 100 | CHECK_PTHREAD_CALL(pthread_getattr_np, (thread, &attributes), __FUNCTION__); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 101 | CHECK_PTHREAD_CALL(pthread_attr_getstack, (&attributes, stack_base, stack_size), __FUNCTION__); |
Elliott Hughes | e188419 | 2012-04-23 12:38:15 -0700 | [diff] [blame] | 102 | CHECK_PTHREAD_CALL(pthread_attr_destroy, (&attributes), __FUNCTION__); |
| 103 | #endif |
| 104 | } |
| 105 | |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 106 | bool ReadFileToString(const std::string& file_name, std::string* result) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 107 | std::unique_ptr<File> file(new File); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 108 | if (!file->Open(file_name, O_RDONLY)) { |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 109 | return false; |
| 110 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 111 | |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 112 | std::vector<char> buf(8 * KB); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 113 | while (true) { |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 114 | int64_t n = TEMP_FAILURE_RETRY(read(file->Fd(), &buf[0], buf.size())); |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 115 | if (n == -1) { |
| 116 | return false; |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 117 | } |
Elliott Hughes | d92bec4 | 2011-09-02 17:04:36 -0700 | [diff] [blame] | 118 | if (n == 0) { |
| 119 | return true; |
| 120 | } |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 121 | result->append(&buf[0], n); |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 122 | } |
buzbee | c143c55 | 2011-08-20 17:38:58 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 125 | std::string GetIsoDate() { |
| 126 | time_t now = time(NULL); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 127 | tm tmbuf; |
| 128 | tm* ptm = localtime_r(&now, &tmbuf); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 129 | return StringPrintf("%04d-%02d-%02d %02d:%02d:%02d", |
| 130 | ptm->tm_year + 1900, ptm->tm_mon+1, ptm->tm_mday, |
| 131 | ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
| 132 | } |
| 133 | |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 134 | uint64_t MilliTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 135 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 136 | timespec now; |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 137 | clock_gettime(CLOCK_MONOTONIC, &now); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 138 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_nsec / UINT64_C(1000000); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 139 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 140 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 141 | gettimeofday(&now, NULL); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 142 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000) + now.tv_usec / UINT64_C(1000); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 143 | #endif |
Elliott Hughes | 7162ad9 | 2011-10-27 14:08:42 -0700 | [diff] [blame] | 144 | } |
| 145 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 146 | uint64_t MicroTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 147 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 148 | timespec now; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 149 | clock_gettime(CLOCK_MONOTONIC, &now); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 150 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_nsec / UINT64_C(1000); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 151 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 152 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 153 | gettimeofday(&now, NULL); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 154 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000) + now.tv_usec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 155 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 158 | uint64_t NanoTime() { |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 159 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 160 | timespec now; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 161 | clock_gettime(CLOCK_MONOTONIC, &now); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 162 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 163 | #else |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 164 | timeval now; |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 165 | gettimeofday(&now, NULL); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 166 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_usec * UINT64_C(1000); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 167 | #endif |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 170 | uint64_t ThreadCpuNanoTime() { |
| 171 | #if defined(HAVE_POSIX_CLOCKS) |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 172 | timespec now; |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 173 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &now); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 174 | return static_cast<uint64_t>(now.tv_sec) * UINT64_C(1000000000) + now.tv_nsec; |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 175 | #else |
| 176 | UNIMPLEMENTED(WARNING); |
| 177 | return -1; |
| 178 | #endif |
| 179 | } |
| 180 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 181 | void NanoSleep(uint64_t ns) { |
| 182 | timespec tm; |
| 183 | tm.tv_sec = 0; |
| 184 | tm.tv_nsec = ns; |
| 185 | nanosleep(&tm, NULL); |
| 186 | } |
| 187 | |
Brian Carlstrom | bcc2926 | 2012-11-02 11:36:03 -0700 | [diff] [blame] | 188 | void InitTimeSpec(bool absolute, int clock, int64_t ms, int32_t ns, timespec* ts) { |
| 189 | int64_t endSec; |
| 190 | |
| 191 | if (absolute) { |
| 192 | #if !defined(__APPLE__) |
| 193 | clock_gettime(clock, ts); |
| 194 | #else |
| 195 | UNUSED(clock); |
| 196 | timeval tv; |
| 197 | gettimeofday(&tv, NULL); |
| 198 | ts->tv_sec = tv.tv_sec; |
| 199 | ts->tv_nsec = tv.tv_usec * 1000; |
| 200 | #endif |
| 201 | } else { |
| 202 | ts->tv_sec = 0; |
| 203 | ts->tv_nsec = 0; |
| 204 | } |
| 205 | endSec = ts->tv_sec + ms / 1000; |
| 206 | if (UNLIKELY(endSec >= 0x7fffffff)) { |
| 207 | std::ostringstream ss; |
| 208 | LOG(INFO) << "Note: end time exceeds epoch: " << ss.str(); |
| 209 | endSec = 0x7ffffffe; |
| 210 | } |
| 211 | ts->tv_sec = endSec; |
| 212 | ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns; |
| 213 | |
| 214 | // Catch rollover. |
| 215 | if (ts->tv_nsec >= 1000000000L) { |
| 216 | ts->tv_sec++; |
| 217 | ts->tv_nsec -= 1000000000L; |
| 218 | } |
| 219 | } |
| 220 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 221 | std::string PrettyDescriptor(mirror::String* java_descriptor) { |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 222 | if (java_descriptor == NULL) { |
| 223 | return "null"; |
| 224 | } |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 225 | return PrettyDescriptor(java_descriptor->ToModifiedUtf8()); |
| 226 | } |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 227 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 228 | std::string PrettyDescriptor(mirror::Class* klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 229 | if (klass == NULL) { |
| 230 | return "null"; |
| 231 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 232 | return PrettyDescriptor(klass->GetDescriptor()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 235 | std::string PrettyDescriptor(const std::string& descriptor) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 236 | // Count the number of '['s to get the dimensionality. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 237 | const char* c = descriptor.c_str(); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 238 | size_t dim = 0; |
| 239 | while (*c == '[') { |
| 240 | dim++; |
| 241 | c++; |
| 242 | } |
| 243 | |
| 244 | // Reference or primitive? |
| 245 | if (*c == 'L') { |
| 246 | // "[[La/b/C;" -> "a.b.C[][]". |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 247 | c++; // Skip the 'L'. |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 248 | } else { |
| 249 | // "[[B" -> "byte[][]". |
| 250 | // To make life easier, we make primitives look like unqualified |
| 251 | // reference types. |
| 252 | switch (*c) { |
| 253 | case 'B': c = "byte;"; break; |
| 254 | case 'C': c = "char;"; break; |
| 255 | case 'D': c = "double;"; break; |
| 256 | case 'F': c = "float;"; break; |
| 257 | case 'I': c = "int;"; break; |
| 258 | case 'J': c = "long;"; break; |
| 259 | case 'S': c = "short;"; break; |
| 260 | case 'Z': c = "boolean;"; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 261 | case 'V': c = "void;"; break; // Used when decoding return types. |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 262 | default: return descriptor; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
| 266 | // At this point, 'c' is a string of the form "fully/qualified/Type;" |
| 267 | // or "primitive;". Rewrite the type with '.' instead of '/': |
| 268 | std::string result; |
| 269 | const char* p = c; |
| 270 | while (*p != ';') { |
| 271 | char ch = *p++; |
| 272 | if (ch == '/') { |
| 273 | ch = '.'; |
| 274 | } |
| 275 | result.push_back(ch); |
| 276 | } |
| 277 | // ...and replace the semicolon with 'dim' "[]" pairs: |
| 278 | while (dim--) { |
| 279 | result += "[]"; |
| 280 | } |
| 281 | return result; |
| 282 | } |
| 283 | |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 284 | std::string PrettyDescriptor(Primitive::Type type) { |
Elliott Hughes | 91250e0 | 2011-12-13 22:30:35 -0800 | [diff] [blame] | 285 | std::string descriptor_string(Primitive::Descriptor(type)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 286 | return PrettyDescriptor(descriptor_string); |
| 287 | } |
| 288 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 289 | std::string PrettyField(mirror::ArtField* f, bool with_type) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 290 | if (f == NULL) { |
| 291 | return "null"; |
| 292 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 293 | FieldHelper fh(f); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 294 | std::string result; |
| 295 | if (with_type) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 296 | result += PrettyDescriptor(fh.GetTypeDescriptor()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 297 | result += ' '; |
| 298 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 299 | result += PrettyDescriptor(fh.GetDeclaringClassDescriptor()); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 300 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 301 | result += fh.GetName(); |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 302 | return result; |
| 303 | } |
| 304 | |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 305 | std::string PrettyField(uint32_t field_idx, const DexFile& dex_file, bool with_type) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 306 | if (field_idx >= dex_file.NumFieldIds()) { |
| 307 | return StringPrintf("<<invalid-field-idx-%d>>", field_idx); |
| 308 | } |
Brian Carlstrom | 6f29d0e | 2012-05-11 15:50:29 -0700 | [diff] [blame] | 309 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 310 | std::string result; |
| 311 | if (with_type) { |
| 312 | result += dex_file.GetFieldTypeDescriptor(field_id); |
| 313 | result += ' '; |
| 314 | } |
| 315 | result += PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(field_id)); |
| 316 | result += '.'; |
| 317 | result += dex_file.GetFieldName(field_id); |
| 318 | return result; |
| 319 | } |
| 320 | |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 321 | std::string PrettyType(uint32_t type_idx, const DexFile& dex_file) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 322 | if (type_idx >= dex_file.NumTypeIds()) { |
| 323 | return StringPrintf("<<invalid-type-idx-%d>>", type_idx); |
| 324 | } |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 325 | const DexFile::TypeId& type_id = dex_file.GetTypeId(type_idx); |
Mathieu Chartier | 4c70d77 | 2012-09-10 14:08:32 -0700 | [diff] [blame] | 326 | return PrettyDescriptor(dex_file.GetTypeDescriptor(type_id)); |
Mathieu Chartier | 18c24b6 | 2012-09-10 08:54:25 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 329 | std::string PrettyArguments(const char* signature) { |
| 330 | std::string result; |
| 331 | result += '('; |
| 332 | CHECK_EQ(*signature, '('); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 333 | ++signature; // Skip the '('. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 334 | while (*signature != ')') { |
| 335 | size_t argument_length = 0; |
| 336 | while (signature[argument_length] == '[') { |
| 337 | ++argument_length; |
| 338 | } |
| 339 | if (signature[argument_length] == 'L') { |
| 340 | argument_length = (strchr(signature, ';') - signature + 1); |
| 341 | } else { |
| 342 | ++argument_length; |
| 343 | } |
| 344 | std::string argument_descriptor(signature, argument_length); |
| 345 | result += PrettyDescriptor(argument_descriptor); |
| 346 | if (signature[argument_length] != ')') { |
| 347 | result += ", "; |
| 348 | } |
| 349 | signature += argument_length; |
| 350 | } |
| 351 | CHECK_EQ(*signature, ')'); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 352 | ++signature; // Skip the ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 353 | result += ')'; |
| 354 | return result; |
| 355 | } |
| 356 | |
| 357 | std::string PrettyReturnType(const char* signature) { |
| 358 | const char* return_type = strchr(signature, ')'); |
| 359 | CHECK(return_type != NULL); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 360 | ++return_type; // Skip ')'. |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 361 | return PrettyDescriptor(return_type); |
| 362 | } |
| 363 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 364 | std::string PrettyMethod(mirror::ArtMethod* m, bool with_signature) { |
Ian Rogers | 16ce092 | 2014-01-10 14:59:36 -0800 | [diff] [blame] | 365 | if (m == nullptr) { |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 366 | return "null"; |
| 367 | } |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 368 | MethodHelper mh(m); |
| 369 | std::string result(PrettyDescriptor(mh.GetDeclaringClassDescriptor())); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 370 | result += '.'; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 371 | result += mh.GetName(); |
Ian Rogers | 16ce092 | 2014-01-10 14:59:36 -0800 | [diff] [blame] | 372 | if (UNLIKELY(m->IsFastNative())) { |
| 373 | result += "!"; |
| 374 | } |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 375 | if (with_signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 376 | const Signature signature = mh.GetSignature(); |
| 377 | std::string sig_as_string(signature.ToString()); |
| 378 | if (signature == Signature::NoSignature()) { |
| 379 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 380 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 381 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 382 | PrettyArguments(sig_as_string.c_str()); |
Elliott Hughes | a0b8feb | 2011-08-20 09:50:55 -0700 | [diff] [blame] | 383 | } |
| 384 | return result; |
| 385 | } |
| 386 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 387 | std::string PrettyMethod(uint32_t method_idx, const DexFile& dex_file, bool with_signature) { |
Elliott Hughes | 60641a7 | 2013-02-27 14:36:16 -0800 | [diff] [blame] | 388 | if (method_idx >= dex_file.NumMethodIds()) { |
| 389 | return StringPrintf("<<invalid-method-idx-%d>>", method_idx); |
| 390 | } |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 391 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 392 | std::string result(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(method_id))); |
| 393 | result += '.'; |
| 394 | result += dex_file.GetMethodName(method_id); |
| 395 | if (with_signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 396 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 397 | std::string sig_as_string(signature.ToString()); |
| 398 | if (signature == Signature::NoSignature()) { |
| 399 | return result + sig_as_string; |
Elliott Hughes | f8c1193 | 2012-03-23 19:53:59 -0700 | [diff] [blame] | 400 | } |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 401 | result = PrettyReturnType(sig_as_string.c_str()) + " " + result + |
| 402 | PrettyArguments(sig_as_string.c_str()); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 403 | } |
| 404 | return result; |
| 405 | } |
| 406 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 407 | std::string PrettyTypeOf(mirror::Object* obj) { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 408 | if (obj == NULL) { |
| 409 | return "null"; |
| 410 | } |
| 411 | if (obj->GetClass() == NULL) { |
| 412 | return "(raw)"; |
| 413 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 414 | std::string result(PrettyDescriptor(obj->GetClass()->GetDescriptor())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 415 | if (obj->IsClass()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 416 | result += "<" + PrettyDescriptor(obj->AsClass()->GetDescriptor()) + ">"; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 417 | } |
| 418 | return result; |
| 419 | } |
| 420 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 421 | std::string PrettyClass(mirror::Class* c) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 422 | if (c == NULL) { |
| 423 | return "null"; |
| 424 | } |
| 425 | std::string result; |
| 426 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 427 | result += PrettyDescriptor(c); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 428 | result += ">"; |
| 429 | return result; |
| 430 | } |
| 431 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 432 | std::string PrettyClassAndClassLoader(mirror::Class* c) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 433 | if (c == NULL) { |
| 434 | return "null"; |
| 435 | } |
| 436 | std::string result; |
| 437 | result += "java.lang.Class<"; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 438 | result += PrettyDescriptor(c); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 439 | result += ","; |
| 440 | result += PrettyTypeOf(c->GetClassLoader()); |
| 441 | // TODO: add an identifying hash value for the loader |
| 442 | result += ">"; |
| 443 | return result; |
| 444 | } |
| 445 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 446 | std::string PrettySize(int64_t byte_count) { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 447 | // The byte thresholds at which we display amounts. A byte count is displayed |
| 448 | // in unit U when kUnitThresholds[U] <= bytes < kUnitThresholds[U+1]. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 449 | static const int64_t kUnitThresholds[] = { |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 450 | 0, // B up to... |
| 451 | 3*1024, // KB up to... |
| 452 | 2*1024*1024, // MB up to... |
| 453 | 1024*1024*1024 // GB from here. |
| 454 | }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 455 | static const int64_t kBytesPerUnit[] = { 1, KB, MB, GB }; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 456 | static const char* const kUnitStrings[] = { "B", "KB", "MB", "GB" }; |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 457 | const char* negative_str = ""; |
| 458 | if (byte_count < 0) { |
| 459 | negative_str = "-"; |
| 460 | byte_count = -byte_count; |
| 461 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 462 | int i = arraysize(kUnitThresholds); |
| 463 | while (--i > 0) { |
| 464 | if (byte_count >= kUnitThresholds[i]) { |
| 465 | break; |
| 466 | } |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 467 | } |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 468 | return StringPrintf("%s%" PRId64 "%s", |
| 469 | negative_str, byte_count / kBytesPerUnit[i], kUnitStrings[i]); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | std::string PrettyDuration(uint64_t nano_duration) { |
| 473 | if (nano_duration == 0) { |
| 474 | return "0"; |
| 475 | } else { |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 476 | return FormatDuration(nano_duration, GetAppropriateTimeUnit(nano_duration)); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | TimeUnit GetAppropriateTimeUnit(uint64_t nano_duration) { |
| 481 | const uint64_t one_sec = 1000 * 1000 * 1000; |
| 482 | const uint64_t one_ms = 1000 * 1000; |
| 483 | const uint64_t one_us = 1000; |
| 484 | if (nano_duration >= one_sec) { |
| 485 | return kTimeUnitSecond; |
| 486 | } else if (nano_duration >= one_ms) { |
| 487 | return kTimeUnitMillisecond; |
| 488 | } else if (nano_duration >= one_us) { |
| 489 | return kTimeUnitMicrosecond; |
| 490 | } else { |
| 491 | return kTimeUnitNanosecond; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | uint64_t GetNsToTimeUnitDivisor(TimeUnit time_unit) { |
| 496 | const uint64_t one_sec = 1000 * 1000 * 1000; |
| 497 | const uint64_t one_ms = 1000 * 1000; |
| 498 | const uint64_t one_us = 1000; |
| 499 | |
| 500 | switch (time_unit) { |
| 501 | case kTimeUnitSecond: |
| 502 | return one_sec; |
| 503 | case kTimeUnitMillisecond: |
| 504 | return one_ms; |
| 505 | case kTimeUnitMicrosecond: |
| 506 | return one_us; |
| 507 | case kTimeUnitNanosecond: |
| 508 | return 1; |
| 509 | } |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | std::string FormatDuration(uint64_t nano_duration, TimeUnit time_unit) { |
| 514 | const char* unit = NULL; |
| 515 | uint64_t divisor = GetNsToTimeUnitDivisor(time_unit); |
| 516 | uint32_t zero_fill = 1; |
| 517 | switch (time_unit) { |
| 518 | case kTimeUnitSecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 519 | unit = "s"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 520 | zero_fill = 9; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 521 | break; |
| 522 | case kTimeUnitMillisecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 523 | unit = "ms"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 524 | zero_fill = 6; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 525 | break; |
| 526 | case kTimeUnitMicrosecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 527 | unit = "us"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 528 | zero_fill = 3; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 529 | break; |
| 530 | case kTimeUnitNanosecond: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 531 | unit = "ns"; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 532 | zero_fill = 0; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 533 | break; |
| 534 | } |
| 535 | |
| 536 | uint64_t whole_part = nano_duration / divisor; |
| 537 | uint64_t fractional_part = nano_duration % divisor; |
| 538 | if (fractional_part == 0) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 539 | return StringPrintf("%" PRIu64 "%s", whole_part, unit); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 540 | } else { |
| 541 | while ((fractional_part % 1000) == 0) { |
| 542 | zero_fill -= 3; |
| 543 | fractional_part /= 1000; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 544 | } |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 545 | if (zero_fill == 3) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 546 | return StringPrintf("%" PRIu64 ".%03" PRIu64 "%s", whole_part, fractional_part, unit); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 547 | } else if (zero_fill == 6) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 548 | return StringPrintf("%" PRIu64 ".%06" PRIu64 "%s", whole_part, fractional_part, unit); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 549 | } else { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 550 | return StringPrintf("%" PRIu64 ".%09" PRIu64 "%s", whole_part, fractional_part, unit); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Elliott Hughes | 82914b6 | 2012-04-09 15:56:29 -0700 | [diff] [blame] | 555 | std::string PrintableString(const std::string& utf) { |
| 556 | std::string result; |
| 557 | result += '"'; |
| 558 | const char* p = utf.c_str(); |
| 559 | size_t char_count = CountModifiedUtf8Chars(p); |
| 560 | for (size_t i = 0; i < char_count; ++i) { |
| 561 | uint16_t ch = GetUtf16FromUtf8(&p); |
| 562 | if (ch == '\\') { |
| 563 | result += "\\\\"; |
| 564 | } else if (ch == '\n') { |
| 565 | result += "\\n"; |
| 566 | } else if (ch == '\r') { |
| 567 | result += "\\r"; |
| 568 | } else if (ch == '\t') { |
| 569 | result += "\\t"; |
| 570 | } else if (NeedsEscaping(ch)) { |
| 571 | StringAppendF(&result, "\\u%04x", ch); |
| 572 | } else { |
| 573 | result += ch; |
| 574 | } |
| 575 | } |
| 576 | result += '"'; |
| 577 | return result; |
| 578 | } |
| 579 | |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 580 | // 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] | 581 | std::string MangleForJni(const std::string& s) { |
| 582 | std::string result; |
| 583 | size_t char_count = CountModifiedUtf8Chars(s.c_str()); |
| 584 | const char* cp = &s[0]; |
| 585 | for (size_t i = 0; i < char_count; ++i) { |
| 586 | uint16_t ch = GetUtf16FromUtf8(&cp); |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 587 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')) { |
| 588 | result.push_back(ch); |
| 589 | } else if (ch == '.' || ch == '/') { |
| 590 | result += "_"; |
| 591 | } else if (ch == '_') { |
| 592 | result += "_1"; |
| 593 | } else if (ch == ';') { |
| 594 | result += "_2"; |
| 595 | } else if (ch == '[') { |
| 596 | result += "_3"; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 597 | } else { |
Elliott Hughes | d8c00d0 | 2012-01-30 14:08:31 -0800 | [diff] [blame] | 598 | StringAppendF(&result, "_0%04x", ch); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | return result; |
| 602 | } |
| 603 | |
Brian Carlstrom | f91c8c3 | 2011-09-21 17:30:34 -0700 | [diff] [blame] | 604 | std::string DotToDescriptor(const char* class_name) { |
| 605 | std::string descriptor(class_name); |
| 606 | std::replace(descriptor.begin(), descriptor.end(), '.', '/'); |
| 607 | if (descriptor.length() > 0 && descriptor[0] != '[') { |
| 608 | descriptor = "L" + descriptor + ";"; |
| 609 | } |
| 610 | return descriptor; |
| 611 | } |
| 612 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 613 | std::string DescriptorToDot(const char* descriptor) { |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 614 | size_t length = strlen(descriptor); |
| 615 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 616 | std::string result(descriptor + 1, length - 2); |
| 617 | std::replace(result.begin(), result.end(), '/', '.'); |
| 618 | return result; |
| 619 | } |
| 620 | return descriptor; |
Elliott Hughes | 91bf6cd | 2012-02-14 17:27:48 -0800 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | std::string DescriptorToName(const char* descriptor) { |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 624 | size_t length = strlen(descriptor); |
Elliott Hughes | 2435a57 | 2012-02-17 16:07:41 -0800 | [diff] [blame] | 625 | if (descriptor[0] == 'L' && descriptor[length - 1] == ';') { |
| 626 | std::string result(descriptor + 1, length - 2); |
| 627 | return result; |
| 628 | } |
| 629 | return descriptor; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 632 | std::string JniShortName(mirror::ArtMethod* m) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 633 | MethodHelper mh(m); |
| 634 | std::string class_name(mh.GetDeclaringClassDescriptor()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 635 | // Remove the leading 'L' and trailing ';'... |
Elliott Hughes | f5a7a47 | 2011-10-07 14:31:02 -0700 | [diff] [blame] | 636 | CHECK_EQ(class_name[0], 'L') << class_name; |
| 637 | CHECK_EQ(class_name[class_name.size() - 1], ';') << class_name; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 638 | class_name.erase(0, 1); |
| 639 | class_name.erase(class_name.size() - 1, 1); |
| 640 | |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 641 | std::string method_name(mh.GetName()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 642 | |
| 643 | std::string short_name; |
| 644 | short_name += "Java_"; |
| 645 | short_name += MangleForJni(class_name); |
| 646 | short_name += "_"; |
| 647 | short_name += MangleForJni(method_name); |
| 648 | return short_name; |
| 649 | } |
| 650 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 651 | std::string JniLongName(mirror::ArtMethod* m) { |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 652 | std::string long_name; |
| 653 | long_name += JniShortName(m); |
| 654 | long_name += "__"; |
| 655 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 656 | std::string signature(MethodHelper(m).GetSignature().ToString()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 657 | signature.erase(0, 1); |
| 658 | signature.erase(signature.begin() + signature.find(')'), signature.end()); |
| 659 | |
| 660 | long_name += MangleForJni(signature); |
| 661 | |
| 662 | return long_name; |
| 663 | } |
| 664 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 665 | // Helper for IsValidPartOfMemberNameUtf8(), a bit vector indicating valid low ascii. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 666 | uint32_t DEX_MEMBER_VALID_LOW_ASCII[4] = { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 667 | 0x00000000, // 00..1f low control characters; nothing valid |
| 668 | 0x03ff2010, // 20..3f digits and symbols; valid: '0'..'9', '$', '-' |
| 669 | 0x87fffffe, // 40..5f uppercase etc.; valid: 'A'..'Z', '_' |
| 670 | 0x07fffffe // 60..7f lowercase etc.; valid: 'a'..'z' |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 671 | }; |
| 672 | |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 673 | // Helper for IsValidPartOfMemberNameUtf8(); do not call directly. |
| 674 | bool IsValidPartOfMemberNameUtf8Slow(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 675 | /* |
| 676 | * It's a multibyte encoded character. Decode it and analyze. We |
| 677 | * accept anything that isn't (a) an improperly encoded low value, |
| 678 | * (b) an improper surrogate pair, (c) an encoded '\0', (d) a high |
| 679 | * control character, or (e) a high space, layout, or special |
| 680 | * character (U+00a0, U+2000..U+200f, U+2028..U+202f, |
| 681 | * U+fff0..U+ffff). This is all specified in the dex format |
| 682 | * document. |
| 683 | */ |
| 684 | |
| 685 | uint16_t utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 686 | |
| 687 | // Perform follow-up tests based on the high 8 bits. |
| 688 | switch (utf16 >> 8) { |
| 689 | case 0x00: |
| 690 | // It's only valid if it's above the ISO-8859-1 high space (0xa0). |
| 691 | return (utf16 > 0x00a0); |
| 692 | case 0xd8: |
| 693 | case 0xd9: |
| 694 | case 0xda: |
| 695 | case 0xdb: |
| 696 | // It's a leading surrogate. Check to see that a trailing |
| 697 | // surrogate follows. |
| 698 | utf16 = GetUtf16FromUtf8(pUtf8Ptr); |
| 699 | return (utf16 >= 0xdc00) && (utf16 <= 0xdfff); |
| 700 | case 0xdc: |
| 701 | case 0xdd: |
| 702 | case 0xde: |
| 703 | case 0xdf: |
| 704 | // It's a trailing surrogate, which is not valid at this point. |
| 705 | return false; |
| 706 | case 0x20: |
| 707 | case 0xff: |
| 708 | // It's in the range that has spaces, controls, and specials. |
| 709 | switch (utf16 & 0xfff8) { |
| 710 | case 0x2000: |
| 711 | case 0x2008: |
| 712 | case 0x2028: |
| 713 | case 0xfff0: |
| 714 | case 0xfff8: |
| 715 | return false; |
| 716 | } |
| 717 | break; |
| 718 | } |
| 719 | return true; |
| 720 | } |
| 721 | |
| 722 | /* Return whether the pointed-at modified-UTF-8 encoded character is |
| 723 | * valid as part of a member name, updating the pointer to point past |
| 724 | * the consumed character. This will consume two encoded UTF-16 code |
| 725 | * points if the character is encoded as a surrogate pair. Also, if |
| 726 | * this function returns false, then the given pointer may only have |
| 727 | * been partially advanced. |
| 728 | */ |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 729 | static bool IsValidPartOfMemberNameUtf8(const char** pUtf8Ptr) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 730 | uint8_t c = (uint8_t) **pUtf8Ptr; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 731 | if (LIKELY(c <= 0x7f)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 732 | // It's low-ascii, so check the table. |
| 733 | uint32_t wordIdx = c >> 5; |
| 734 | uint32_t bitIdx = c & 0x1f; |
| 735 | (*pUtf8Ptr)++; |
| 736 | return (DEX_MEMBER_VALID_LOW_ASCII[wordIdx] & (1 << bitIdx)) != 0; |
| 737 | } |
| 738 | |
| 739 | // It's a multibyte encoded character. Call a non-inline function |
| 740 | // for the heavy lifting. |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 741 | return IsValidPartOfMemberNameUtf8Slow(pUtf8Ptr); |
| 742 | } |
| 743 | |
| 744 | bool IsValidMemberName(const char* s) { |
| 745 | bool angle_name = false; |
| 746 | |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 747 | switch (*s) { |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 748 | case '\0': |
| 749 | // The empty string is not a valid name. |
| 750 | return false; |
| 751 | case '<': |
| 752 | angle_name = true; |
| 753 | s++; |
| 754 | break; |
| 755 | } |
| 756 | |
| 757 | while (true) { |
| 758 | switch (*s) { |
| 759 | case '\0': |
| 760 | return !angle_name; |
| 761 | case '>': |
| 762 | return angle_name && s[1] == '\0'; |
| 763 | } |
| 764 | |
| 765 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
| 766 | return false; |
| 767 | } |
| 768 | } |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 771 | enum ClassNameType { kName, kDescriptor }; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 772 | static bool IsValidClassName(const char* s, ClassNameType type, char separator) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 773 | int arrayCount = 0; |
| 774 | while (*s == '[') { |
| 775 | arrayCount++; |
| 776 | s++; |
| 777 | } |
| 778 | |
| 779 | if (arrayCount > 255) { |
| 780 | // Arrays may have no more than 255 dimensions. |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | if (arrayCount != 0) { |
| 785 | /* |
| 786 | * If we're looking at an array of some sort, then it doesn't |
| 787 | * matter if what is being asked for is a class name; the |
| 788 | * format looks the same as a type descriptor in that case, so |
| 789 | * treat it as such. |
| 790 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 791 | type = kDescriptor; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 794 | if (type == kDescriptor) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 795 | /* |
| 796 | * We are looking for a descriptor. Either validate it as a |
| 797 | * single-character primitive type, or continue on to check the |
| 798 | * embedded class name (bracketed by "L" and ";"). |
| 799 | */ |
| 800 | switch (*(s++)) { |
| 801 | case 'B': |
| 802 | case 'C': |
| 803 | case 'D': |
| 804 | case 'F': |
| 805 | case 'I': |
| 806 | case 'J': |
| 807 | case 'S': |
| 808 | case 'Z': |
| 809 | // These are all single-character descriptors for primitive types. |
| 810 | return (*s == '\0'); |
| 811 | case 'V': |
| 812 | // Non-array void is valid, but you can't have an array of void. |
| 813 | return (arrayCount == 0) && (*s == '\0'); |
| 814 | case 'L': |
| 815 | // Class name: Break out and continue below. |
| 816 | break; |
| 817 | default: |
| 818 | // Oddball descriptor character. |
| 819 | return false; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * We just consumed the 'L' that introduces a class name as part |
| 825 | * of a type descriptor, or we are looking for an unadorned class |
| 826 | * name. |
| 827 | */ |
| 828 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 829 | bool sepOrFirst = true; // first character or just encountered a separator. |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 830 | for (;;) { |
| 831 | uint8_t c = (uint8_t) *s; |
| 832 | switch (c) { |
| 833 | case '\0': |
| 834 | /* |
| 835 | * Premature end for a type descriptor, but valid for |
| 836 | * a class name as long as we haven't encountered an |
| 837 | * empty component (including the degenerate case of |
| 838 | * the empty string ""). |
| 839 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 840 | return (type == kName) && !sepOrFirst; |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 841 | case ';': |
| 842 | /* |
| 843 | * Invalid character for a class name, but the |
| 844 | * legitimate end of a type descriptor. In the latter |
| 845 | * case, make sure that this is the end of the string |
| 846 | * and that it doesn't end with an empty component |
| 847 | * (including the degenerate case of "L;"). |
| 848 | */ |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 849 | return (type == kDescriptor) && !sepOrFirst && (s[1] == '\0'); |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 850 | case '/': |
| 851 | case '.': |
| 852 | if (c != separator) { |
| 853 | // The wrong separator character. |
| 854 | return false; |
| 855 | } |
| 856 | if (sepOrFirst) { |
| 857 | // Separator at start or two separators in a row. |
| 858 | return false; |
| 859 | } |
| 860 | sepOrFirst = true; |
| 861 | s++; |
| 862 | break; |
| 863 | default: |
jeffhao | 10037c8 | 2012-01-23 15:06:23 -0800 | [diff] [blame] | 864 | if (!IsValidPartOfMemberNameUtf8(&s)) { |
Elliott Hughes | 64bf5a3 | 2011-09-20 14:43:12 -0700 | [diff] [blame] | 865 | return false; |
| 866 | } |
| 867 | sepOrFirst = false; |
| 868 | break; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Elliott Hughes | 906e685 | 2011-10-28 14:52:10 -0700 | [diff] [blame] | 873 | bool IsValidBinaryClassName(const char* s) { |
| 874 | return IsValidClassName(s, kName, '.'); |
| 875 | } |
| 876 | |
| 877 | bool IsValidJniClassName(const char* s) { |
| 878 | return IsValidClassName(s, kName, '/'); |
| 879 | } |
| 880 | |
| 881 | bool IsValidDescriptor(const char* s) { |
| 882 | return IsValidClassName(s, kDescriptor, '/'); |
| 883 | } |
| 884 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 885 | 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] | 886 | const char* p = s.data(); |
| 887 | const char* end = p + s.size(); |
| 888 | while (p != end) { |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 889 | if (*p == separator) { |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 890 | ++p; |
| 891 | } else { |
| 892 | const char* start = p; |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 893 | while (++p != end && *p != separator) { |
| 894 | // Skip to the next occurrence of the separator. |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 895 | } |
| 896 | result.push_back(std::string(start, p - start)); |
| 897 | } |
| 898 | } |
| 899 | } |
| 900 | |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame] | 901 | std::string Trim(std::string s) { |
| 902 | std::string result; |
| 903 | unsigned int start_index = 0; |
| 904 | unsigned int end_index = s.size() - 1; |
| 905 | |
| 906 | // Skip initial whitespace. |
| 907 | while (start_index < s.size()) { |
| 908 | if (!isspace(s[start_index])) { |
| 909 | break; |
| 910 | } |
| 911 | start_index++; |
| 912 | } |
| 913 | |
| 914 | // Skip terminating whitespace. |
| 915 | while (end_index >= start_index) { |
| 916 | if (!isspace(s[end_index])) { |
| 917 | break; |
| 918 | } |
| 919 | end_index--; |
| 920 | } |
| 921 | |
| 922 | // All spaces, no beef. |
| 923 | if (end_index < start_index) { |
| 924 | return ""; |
| 925 | } |
| 926 | // Start_index is the first non-space, end_index is the last one. |
| 927 | return s.substr(start_index, end_index - start_index + 1); |
| 928 | } |
| 929 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 930 | template <typename StringT> |
| 931 | std::string Join(std::vector<StringT>& strings, char separator) { |
| 932 | if (strings.empty()) { |
| 933 | return ""; |
| 934 | } |
| 935 | |
| 936 | std::string result(strings[0]); |
| 937 | for (size_t i = 1; i < strings.size(); ++i) { |
| 938 | result += separator; |
| 939 | result += strings[i]; |
| 940 | } |
| 941 | return result; |
| 942 | } |
| 943 | |
| 944 | // Explicit instantiations. |
| 945 | template std::string Join<std::string>(std::vector<std::string>& strings, char separator); |
| 946 | template std::string Join<const char*>(std::vector<const char*>& strings, char separator); |
| 947 | template std::string Join<char*>(std::vector<char*>& strings, char separator); |
| 948 | |
Elliott Hughes | f1a5adc | 2012-02-10 18:09:35 -0800 | [diff] [blame] | 949 | bool StartsWith(const std::string& s, const char* prefix) { |
| 950 | return s.compare(0, strlen(prefix), prefix) == 0; |
| 951 | } |
| 952 | |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 953 | bool EndsWith(const std::string& s, const char* suffix) { |
| 954 | size_t suffix_length = strlen(suffix); |
| 955 | size_t string_length = s.size(); |
| 956 | if (suffix_length > string_length) { |
| 957 | return false; |
| 958 | } |
| 959 | size_t offset = string_length - suffix_length; |
| 960 | return s.compare(offset, suffix_length, suffix) == 0; |
| 961 | } |
| 962 | |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 963 | void SetThreadName(const char* thread_name) { |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 964 | int hasAt = 0; |
| 965 | int hasDot = 0; |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 966 | const char* s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 967 | while (*s) { |
| 968 | if (*s == '.') { |
| 969 | hasDot = 1; |
| 970 | } else if (*s == '@') { |
| 971 | hasAt = 1; |
| 972 | } |
| 973 | s++; |
| 974 | } |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 975 | int len = s - thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 976 | if (len < 15 || hasAt || !hasDot) { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 977 | s = thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 978 | } else { |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 979 | s = thread_name + len - 15; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 980 | } |
| 981 | #if defined(HAVE_ANDROID_PTHREAD_SETNAME_NP) |
Elliott Hughes | 7c6a61e | 2012-03-12 18:01:41 -0700 | [diff] [blame] | 982 | // pthread_setname_np fails rather than truncating long strings. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 983 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 984 | strncpy(buf, s, sizeof(buf)-1); |
| 985 | buf[sizeof(buf)-1] = '\0'; |
| 986 | errno = pthread_setname_np(pthread_self(), buf); |
| 987 | if (errno != 0) { |
| 988 | PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'"; |
| 989 | } |
Elliott Hughes | 4ae722a | 2012-03-13 11:08:51 -0700 | [diff] [blame] | 990 | #elif defined(__APPLE__) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 991 | pthread_setname_np(thread_name); |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 992 | #elif defined(HAVE_PRCTL) |
Elliott Hughes | 398f64b | 2012-03-26 18:05:48 -0700 | [diff] [blame] | 993 | 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] | 994 | #else |
Elliott Hughes | 22869a9 | 2012-03-27 14:08:24 -0700 | [diff] [blame] | 995 | UNIMPLEMENTED(WARNING) << thread_name; |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 996 | #endif |
| 997 | } |
| 998 | |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 999 | void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) { |
| 1000 | *utime = *stime = *task_cpu = 0; |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1001 | std::string stats; |
Elliott Hughes | 8a31b50 | 2012-04-30 19:36:11 -0700 | [diff] [blame] | 1002 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/stat", tid), &stats)) { |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1003 | return; |
| 1004 | } |
| 1005 | // Skip the command, which may contain spaces. |
| 1006 | stats = stats.substr(stats.find(')') + 2); |
| 1007 | // Extract the three fields we care about. |
| 1008 | std::vector<std::string> fields; |
| 1009 | Split(stats, ' ', fields); |
Brian Carlstrom | 2921201 | 2013-09-12 22:18:30 -0700 | [diff] [blame] | 1010 | *state = fields[0][0]; |
| 1011 | *utime = strtoull(fields[11].c_str(), NULL, 10); |
| 1012 | *stime = strtoull(fields[12].c_str(), NULL, 10); |
| 1013 | *task_cpu = strtoull(fields[36].c_str(), NULL, 10); |
Elliott Hughes | bfe487b | 2011-10-26 15:48:55 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1016 | std::string GetSchedulerGroupName(pid_t tid) { |
| 1017 | // /proc/<pid>/cgroup looks like this: |
| 1018 | // 2:devices:/ |
| 1019 | // 1:cpuacct,cpu:/ |
| 1020 | // We want the third field from the line whose second field contains the "cpu" token. |
| 1021 | std::string cgroup_file; |
| 1022 | if (!ReadFileToString(StringPrintf("/proc/self/task/%d/cgroup", tid), &cgroup_file)) { |
| 1023 | return ""; |
| 1024 | } |
| 1025 | std::vector<std::string> cgroup_lines; |
| 1026 | Split(cgroup_file, '\n', cgroup_lines); |
| 1027 | for (size_t i = 0; i < cgroup_lines.size(); ++i) { |
| 1028 | std::vector<std::string> cgroup_fields; |
| 1029 | Split(cgroup_lines[i], ':', cgroup_fields); |
| 1030 | std::vector<std::string> cgroups; |
| 1031 | Split(cgroup_fields[1], ',', cgroups); |
| 1032 | for (size_t i = 0; i < cgroups.size(); ++i) { |
| 1033 | if (cgroups[i] == "cpu") { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1034 | return cgroup_fields[2].substr(1); // Skip the leading slash. |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | return ""; |
| 1039 | } |
| 1040 | |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1041 | void DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix, |
Kenny Root | 067d20f | 2014-03-05 14:57:21 -0800 | [diff] [blame] | 1042 | mirror::ArtMethod* current_method) { |
| 1043 | // We may be called from contexts where current_method is not null, so we must assert this. |
| 1044 | if (current_method != nullptr) { |
| 1045 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 1046 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1047 | std::unique_ptr<Backtrace> backtrace(Backtrace::Create(BACKTRACE_CURRENT_PROCESS, tid)); |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1048 | if (!backtrace->Unwind(0)) { |
| 1049 | os << prefix << "(backtrace::Unwind failed for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1050 | return; |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1051 | } else if (backtrace->NumFrames() == 0) { |
Elliott Hughes | 225f5a1 | 2012-06-11 11:23:48 -0700 | [diff] [blame] | 1052 | os << prefix << "(no native stack frames for thread " << tid << ")\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1053 | return; |
| 1054 | } |
| 1055 | |
Christopher Ferris | 943af7d | 2014-01-16 12:41:46 -0800 | [diff] [blame] | 1056 | for (Backtrace::const_iterator it = backtrace->begin(); |
| 1057 | it != backtrace->end(); ++it) { |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1058 | // We produce output like this: |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1059 | // ] #00 pc 000075bb8 /system/lib/libc.so (unwind_backtrace_thread+536) |
| 1060 | // In order for parsing tools to continue to function, the stack dump |
| 1061 | // format must at least adhere to this format: |
| 1062 | // #XX pc <RELATIVE_ADDR> <FULL_PATH_TO_SHARED_LIBRARY> ... |
| 1063 | // The parsers require a single space before and after pc, and two spaces |
| 1064 | // after the <RELATIVE_ADDR>. There can be any prefix data before the |
| 1065 | // #XX. <RELATIVE_ADDR> has to be a hex number but with no 0x prefix. |
| 1066 | os << prefix << StringPrintf("#%02zu pc ", it->num); |
| 1067 | if (!it->map) { |
| 1068 | os << StringPrintf("%08" PRIxPTR " ???", it->pc); |
Christopher Ferris | 7b5f0cf | 2013-11-01 15:18:45 -0700 | [diff] [blame] | 1069 | } else { |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1070 | os << StringPrintf("%08" PRIxPTR " ", it->pc - it->map->start) |
| 1071 | << it->map->name << " ("; |
| 1072 | if (!it->func_name.empty()) { |
| 1073 | os << it->func_name; |
| 1074 | if (it->func_offset != 0) { |
| 1075 | os << "+" << it->func_offset; |
| 1076 | } |
| 1077 | } else if (current_method != nullptr && current_method->IsWithinQuickCode(it->pc)) { |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 1078 | const void* start_of_code = current_method->GetEntryPointFromQuickCompiledCode(); |
| 1079 | os << JniLongName(current_method) << "+" |
| 1080 | << (it->pc - reinterpret_cast<uintptr_t>(start_of_code)); |
Kenny Root | 067d20f | 2014-03-05 14:57:21 -0800 | [diff] [blame] | 1081 | } else { |
| 1082 | os << "???"; |
| 1083 | } |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1084 | os << ")"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1085 | } |
Christopher Ferris | a2cee18 | 2014-04-16 19:13:59 -0700 | [diff] [blame] | 1086 | os << "\n"; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1087 | } |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1090 | #if defined(__APPLE__) |
| 1091 | |
| 1092 | // TODO: is there any way to get the kernel stack on Mac OS? |
| 1093 | void DumpKernelStack(std::ostream&, pid_t, const char*, bool) {} |
| 1094 | |
| 1095 | #else |
| 1096 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1097 | void DumpKernelStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) { |
Elliott Hughes | 12a9502 | 2012-05-24 21:41:38 -0700 | [diff] [blame] | 1098 | if (tid == GetTid()) { |
| 1099 | // There's no point showing that we're reading our stack out of /proc! |
| 1100 | return; |
| 1101 | } |
| 1102 | |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1103 | std::string kernel_stack_filename(StringPrintf("/proc/self/task/%d/stack", tid)); |
| 1104 | std::string kernel_stack; |
| 1105 | if (!ReadFileToString(kernel_stack_filename, &kernel_stack)) { |
Elliott Hughes | 058a6de | 2012-05-24 19:13:02 -0700 | [diff] [blame] | 1106 | os << prefix << "(couldn't read " << kernel_stack_filename << ")\n"; |
jeffhao | c4c3ee2 | 2012-05-25 16:16:32 -0700 | [diff] [blame] | 1107 | return; |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | std::vector<std::string> kernel_stack_frames; |
| 1111 | Split(kernel_stack, '\n', kernel_stack_frames); |
| 1112 | // We skip the last stack frame because it's always equivalent to "[<ffffffff>] 0xffffffff", |
| 1113 | // which looking at the source appears to be the kernel's way of saying "that's all, folks!". |
| 1114 | kernel_stack_frames.pop_back(); |
| 1115 | for (size_t i = 0; i < kernel_stack_frames.size(); ++i) { |
Brian Carlstrom | 474cc79 | 2014-03-07 14:18:15 -0800 | [diff] [blame] | 1116 | // Turn "[<ffffffff8109156d>] futex_wait_queue_me+0xcd/0x110" |
| 1117 | // into "futex_wait_queue_me+0xcd/0x110". |
Elliott Hughes | 46e251b | 2012-05-22 15:10:45 -0700 | [diff] [blame] | 1118 | const char* text = kernel_stack_frames[i].c_str(); |
| 1119 | const char* close_bracket = strchr(text, ']'); |
| 1120 | if (close_bracket != NULL) { |
| 1121 | text = close_bracket + 2; |
| 1122 | } |
| 1123 | os << prefix; |
| 1124 | if (include_count) { |
| 1125 | os << StringPrintf("#%02zd ", i); |
| 1126 | } |
| 1127 | os << text << "\n"; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | #endif |
| 1132 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1133 | const char* GetAndroidRoot() { |
| 1134 | const char* android_root = getenv("ANDROID_ROOT"); |
| 1135 | if (android_root == NULL) { |
| 1136 | if (OS::DirectoryExists("/system")) { |
| 1137 | android_root = "/system"; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1138 | } else { |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1139 | LOG(FATAL) << "ANDROID_ROOT not set and /system does not exist"; |
| 1140 | return ""; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1143 | if (!OS::DirectoryExists(android_root)) { |
| 1144 | LOG(FATAL) << "Failed to find ANDROID_ROOT directory " << android_root; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1145 | return ""; |
| 1146 | } |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1147 | return android_root; |
| 1148 | } |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1149 | |
Brian Carlstrom | a56fcd6 | 2012-02-04 21:23:01 -0800 | [diff] [blame] | 1150 | const char* GetAndroidData() { |
| 1151 | const char* android_data = getenv("ANDROID_DATA"); |
| 1152 | if (android_data == NULL) { |
| 1153 | if (OS::DirectoryExists("/data")) { |
| 1154 | android_data = "/data"; |
| 1155 | } else { |
| 1156 | LOG(FATAL) << "ANDROID_DATA not set and /data does not exist"; |
| 1157 | return ""; |
| 1158 | } |
| 1159 | } |
| 1160 | if (!OS::DirectoryExists(android_data)) { |
| 1161 | LOG(FATAL) << "Failed to find ANDROID_DATA directory " << android_data; |
| 1162 | return ""; |
| 1163 | } |
| 1164 | return android_data; |
| 1165 | } |
| 1166 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1167 | std::string GetDalvikCacheOrDie(const char* subdir, const bool create_if_absent) { |
| 1168 | CHECK(subdir != nullptr); |
Brian Carlstrom | 41ccffd | 2014-05-06 10:37:30 -0700 | [diff] [blame] | 1169 | const char* android_data = GetAndroidData(); |
| 1170 | const std::string dalvik_cache_root(StringPrintf("%s/dalvik-cache/", android_data)); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1171 | const std::string dalvik_cache = dalvik_cache_root + subdir; |
| 1172 | if (create_if_absent && !OS::DirectoryExists(dalvik_cache.c_str())) { |
Brian Carlstrom | 41ccffd | 2014-05-06 10:37:30 -0700 | [diff] [blame] | 1173 | // Don't create the system's /data/dalvik-cache/... because it needs special permissions. |
| 1174 | if (strcmp(android_data, "/data") != 0) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1175 | int result = mkdir(dalvik_cache_root.c_str(), 0700); |
Narayan Kamath | ef204fa | 2014-04-30 17:25:23 +0100 | [diff] [blame] | 1176 | if (result != 0 && errno != EEXIST) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1177 | PLOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache_root; |
| 1178 | return ""; |
| 1179 | } |
| 1180 | result = mkdir(dalvik_cache.c_str(), 0700); |
| 1181 | if (result != 0) { |
| 1182 | PLOG(FATAL) << "Failed to create dalvik-cache directory " << dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1183 | return ""; |
| 1184 | } |
| 1185 | } else { |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1186 | LOG(FATAL) << "Failed to find dalvik-cache directory " << dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1187 | return ""; |
| 1188 | } |
| 1189 | } |
Brian Carlstrom | 7675e16 | 2013-06-10 16:18:04 -0700 | [diff] [blame] | 1190 | return dalvik_cache; |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1193 | std::string GetDalvikCacheFilenameOrDie(const char* location, const char* cache_location) { |
Ian Rogers | e606010 | 2013-05-16 12:01:04 -0700 | [diff] [blame] | 1194 | if (location[0] != '/') { |
| 1195 | LOG(FATAL) << "Expected path in location to be absolute: "<< location; |
| 1196 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1197 | std::string cache_file(&location[1]); // skip leading slash |
Brian Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 1198 | if (!EndsWith(location, ".dex") && !EndsWith(location, ".art")) { |
Brian Carlstrom | 30e2ea4 | 2013-06-19 23:25:37 -0700 | [diff] [blame] | 1199 | cache_file += "/"; |
| 1200 | cache_file += DexFile::kClassesDex; |
| 1201 | } |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1202 | std::replace(cache_file.begin(), cache_file.end(), '/', '@'); |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 1203 | return StringPrintf("%s/%s", cache_location, cache_file.c_str()); |
Brian Carlstrom | b7bbba4 | 2011-10-13 14:58:47 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 1206 | static void InsertIsaDirectory(const InstructionSet isa, std::string* filename) { |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 1207 | // in = /foo/bar/baz |
| 1208 | // out = /foo/bar/<isa>/baz |
| 1209 | size_t pos = filename->rfind('/'); |
| 1210 | CHECK_NE(pos, std::string::npos) << *filename << " " << isa; |
| 1211 | filename->insert(pos, "/", 1); |
| 1212 | filename->insert(pos + 1, GetInstructionSetString(isa)); |
| 1213 | } |
| 1214 | |
| 1215 | std::string GetSystemImageFilename(const char* location, const InstructionSet isa) { |
| 1216 | // location = /system/framework/boot.art |
| 1217 | // filename = /system/framework/<isa>/boot.art |
| 1218 | std::string filename(location); |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 1219 | InsertIsaDirectory(isa, &filename); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 1220 | return filename; |
| 1221 | } |
| 1222 | |
| 1223 | std::string DexFilenameToOdexFilename(const std::string& location, const InstructionSet isa) { |
| 1224 | // location = /foo/bar/baz.jar |
| 1225 | // odex_location = /foo/bar/<isa>/baz.odex |
| 1226 | CHECK_GE(location.size(), 4U) << location; // must be at least .123 |
| 1227 | std::string odex_location(location); |
Brian Carlstrom | 2afe494 | 2014-05-19 10:25:33 -0700 | [diff] [blame] | 1228 | InsertIsaDirectory(isa, &odex_location); |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 1229 | size_t dot_index = odex_location.size() - 3 - 1; // 3=dex or zip or apk |
| 1230 | CHECK_EQ('.', odex_location[dot_index]) << location; |
| 1231 | odex_location.resize(dot_index + 1); |
| 1232 | CHECK_EQ('.', odex_location[odex_location.size()-1]) << location << " " << odex_location; |
| 1233 | odex_location += "odex"; |
| 1234 | return odex_location; |
| 1235 | } |
| 1236 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1237 | bool IsZipMagic(uint32_t magic) { |
| 1238 | return (('P' == ((magic >> 0) & 0xff)) && |
| 1239 | ('K' == ((magic >> 8) & 0xff))); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1242 | bool IsDexMagic(uint32_t magic) { |
| 1243 | return DexFile::IsMagicValid(reinterpret_cast<const byte*>(&magic)); |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Brian Carlstrom | 7c3d13a | 2013-09-04 17:15:11 -0700 | [diff] [blame] | 1246 | bool IsOatMagic(uint32_t magic) { |
| 1247 | return (memcmp(reinterpret_cast<const byte*>(magic), |
| 1248 | OatHeader::kOatMagic, |
| 1249 | sizeof(OatHeader::kOatMagic)) == 0); |
jeffhao | 262bf46 | 2011-10-20 18:36:32 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1252 | bool Exec(std::vector<std::string>& arg_vector, std::string* error_msg) { |
| 1253 | const std::string command_line(Join(arg_vector, ' ')); |
| 1254 | |
| 1255 | CHECK_GE(arg_vector.size(), 1U) << command_line; |
| 1256 | |
| 1257 | // Convert the args to char pointers. |
| 1258 | const char* program = arg_vector[0].c_str(); |
| 1259 | std::vector<char*> args; |
Brian Carlstrom | 35d8b8e | 2014-02-25 10:51:11 -0800 | [diff] [blame] | 1260 | for (size_t i = 0; i < arg_vector.size(); ++i) { |
| 1261 | const std::string& arg = arg_vector[i]; |
| 1262 | char* arg_str = const_cast<char*>(arg.c_str()); |
| 1263 | CHECK(arg_str != nullptr) << i; |
| 1264 | args.push_back(arg_str); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1265 | } |
| 1266 | args.push_back(NULL); |
| 1267 | |
| 1268 | // fork and exec |
| 1269 | pid_t pid = fork(); |
| 1270 | if (pid == 0) { |
| 1271 | // no allocation allowed between fork and exec |
| 1272 | |
| 1273 | // change process groups, so we don't get reaped by ProcessManager |
| 1274 | setpgid(0, 0); |
| 1275 | |
| 1276 | execv(program, &args[0]); |
| 1277 | |
Brian Carlstrom | 13db9aa | 2014-02-27 12:44:32 -0800 | [diff] [blame] | 1278 | PLOG(ERROR) << "Failed to execv(" << command_line << ")"; |
| 1279 | exit(1); |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 1280 | } else { |
| 1281 | if (pid == -1) { |
| 1282 | *error_msg = StringPrintf("Failed to execv(%s) because fork failed: %s", |
| 1283 | command_line.c_str(), strerror(errno)); |
| 1284 | return false; |
| 1285 | } |
| 1286 | |
| 1287 | // wait for subprocess to finish |
| 1288 | int status; |
| 1289 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 1290 | if (got_pid != pid) { |
| 1291 | *error_msg = StringPrintf("Failed after fork for execv(%s) because waitpid failed: " |
| 1292 | "wanted %d, got %d: %s", |
| 1293 | command_line.c_str(), pid, got_pid, strerror(errno)); |
| 1294 | return false; |
| 1295 | } |
| 1296 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1297 | *error_msg = StringPrintf("Failed execv(%s) because non-0 exit status", |
| 1298 | command_line.c_str()); |
| 1299 | return false; |
| 1300 | } |
| 1301 | } |
| 1302 | return true; |
| 1303 | } |
| 1304 | |
Elliott Hughes | 42ee142 | 2011-09-06 12:33:32 -0700 | [diff] [blame] | 1305 | } // namespace art |