The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | */ |
| 16 | |
| 17 | package android.os; |
| 18 | |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 19 | import com.android.internal.util.FastPrintWriter; |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 20 | import com.android.internal.util.TypedProperties; |
| 21 | |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 24 | import java.io.FileDescriptor; |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 25 | import java.io.FileNotFoundException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import java.io.FileOutputStream; |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 27 | import java.io.FileReader; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import java.io.IOException; |
| 29 | import java.io.OutputStreamWriter; |
| 30 | import java.io.PrintWriter; |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 31 | import java.io.Reader; |
| 32 | import java.lang.reflect.Field; |
| 33 | import java.lang.reflect.Modifier; |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 34 | import java.lang.annotation.Target; |
| 35 | import java.lang.annotation.ElementType; |
| 36 | import java.lang.annotation.Retention; |
| 37 | import java.lang.annotation.RetentionPolicy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
| 39 | import org.apache.harmony.dalvik.ddmc.Chunk; |
| 40 | import org.apache.harmony.dalvik.ddmc.ChunkHandler; |
| 41 | import org.apache.harmony.dalvik.ddmc.DdmServer; |
| 42 | |
Dan Bornstein | 9f31554 | 2010-11-19 18:12:55 -0800 | [diff] [blame] | 43 | import dalvik.bytecode.OpcodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | import dalvik.bytecode.Opcodes; |
| 45 | import dalvik.system.VMDebug; |
| 46 | |
| 47 | |
| 48 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 49 | * Provides various debugging methods for Android applications, including |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | * tracing and allocation counts. |
| 51 | * <p><strong>Logging Trace Files</strong></p> |
| 52 | * <p>Debug can create log files that give details about an application, such as |
| 53 | * a call stack and start/stop times for any running methods. See <a |
| 54 | href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for |
| 55 | * information about reading trace files. To start logging trace files, call one |
| 56 | * of the startMethodTracing() methods. To stop tracing, call |
| 57 | * {@link #stopMethodTracing()}. |
| 58 | */ |
| 59 | public final class Debug |
| 60 | { |
Dan Egnor | 3eda979 | 2010-03-05 13:28:36 -0800 | [diff] [blame] | 61 | private static final String TAG = "Debug"; |
| 62 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | /** |
| 64 | * Flags for startMethodTracing(). These can be ORed together. |
| 65 | * |
| 66 | * TRACE_COUNT_ALLOCS adds the results from startAllocCounting to the |
| 67 | * trace key file. |
| 68 | */ |
| 69 | public static final int TRACE_COUNT_ALLOCS = VMDebug.TRACE_COUNT_ALLOCS; |
| 70 | |
| 71 | /** |
| 72 | * Flags for printLoadedClasses(). Default behavior is to only show |
| 73 | * the class name. |
| 74 | */ |
| 75 | public static final int SHOW_FULL_DETAIL = 1; |
| 76 | public static final int SHOW_CLASSLOADER = (1 << 1); |
| 77 | public static final int SHOW_INITIALIZED = (1 << 2); |
| 78 | |
| 79 | // set/cleared by waitForDebugger() |
| 80 | private static volatile boolean mWaiting = false; |
| 81 | |
| 82 | private Debug() {} |
| 83 | |
| 84 | /* |
| 85 | * How long to wait for the debugger to finish sending requests. I've |
| 86 | * seen this hit 800msec on the device while waiting for a response |
| 87 | * to travel over USB and get processed, so we take that and add |
| 88 | * half a second. |
| 89 | */ |
| 90 | private static final int MIN_DEBUGGER_IDLE = 1300; // msec |
| 91 | |
| 92 | /* how long to sleep when polling for activity */ |
| 93 | private static final int SPIN_DELAY = 200; // msec |
| 94 | |
| 95 | /** |
| 96 | * Default trace file path and file |
| 97 | */ |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 98 | private static final String DEFAULT_TRACE_PATH_PREFIX = |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 99 | Environment.getLegacyExternalStorageDirectory().getPath() + "/"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | private static final String DEFAULT_TRACE_BODY = "dmtrace"; |
| 101 | private static final String DEFAULT_TRACE_EXTENSION = ".trace"; |
| 102 | private static final String DEFAULT_TRACE_FILE_PATH = |
| 103 | DEFAULT_TRACE_PATH_PREFIX + DEFAULT_TRACE_BODY |
| 104 | + DEFAULT_TRACE_EXTENSION; |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * This class is used to retrieved various statistics about the memory mappings for this |
| 109 | * process. The returns info broken down by dalvik, native, and other. All results are in kB. |
| 110 | */ |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 111 | public static class MemoryInfo implements Parcelable { |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 112 | /** The proportional set size for dalvik heap. (Doesn't include other Dalvik overhead.) */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | public int dalvikPss; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 114 | /** The proportional set size that is swappable for dalvik heap. */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 115 | /** @hide We may want to expose this, eventually. */ |
| 116 | public int dalvikSwappablePss; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 117 | /** The private dirty pages used by dalvik heap. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | public int dalvikPrivateDirty; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 119 | /** The shared dirty pages used by dalvik heap. */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | public int dalvikSharedDirty; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 121 | /** The private clean pages used by dalvik heap. */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 122 | /** @hide We may want to expose this, eventually. */ |
| 123 | public int dalvikPrivateClean; |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 124 | /** The shared clean pages used by dalvik heap. */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 125 | /** @hide We may want to expose this, eventually. */ |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 126 | public int dalvikSharedClean; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | |
| 128 | /** The proportional set size for the native heap. */ |
| 129 | public int nativePss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 130 | /** The proportional set size that is swappable for the native heap. */ |
| 131 | /** @hide We may want to expose this, eventually. */ |
| 132 | public int nativeSwappablePss; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | /** The private dirty pages used by the native heap. */ |
| 134 | public int nativePrivateDirty; |
| 135 | /** The shared dirty pages used by the native heap. */ |
| 136 | public int nativeSharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 137 | /** The private clean pages used by the native heap. */ |
| 138 | /** @hide We may want to expose this, eventually. */ |
| 139 | public int nativePrivateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 140 | /** The shared clean pages used by the native heap. */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 141 | /** @hide We may want to expose this, eventually. */ |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 142 | public int nativeSharedClean; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | |
| 144 | /** The proportional set size for everything else. */ |
| 145 | public int otherPss; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 146 | /** The proportional set size that is swappable for everything else. */ |
| 147 | /** @hide We may want to expose this, eventually. */ |
| 148 | public int otherSwappablePss; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | /** The private dirty pages used by everything else. */ |
| 150 | public int otherPrivateDirty; |
| 151 | /** The shared dirty pages used by everything else. */ |
| 152 | public int otherSharedDirty; |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 153 | /** The private clean pages used by everything else. */ |
| 154 | /** @hide We may want to expose this, eventually. */ |
| 155 | public int otherPrivateClean; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 156 | /** The shared clean pages used by everything else. */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 157 | /** @hide We may want to expose this, eventually. */ |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 158 | public int otherSharedClean; |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 159 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 160 | /** @hide */ |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 161 | public static final int NUM_OTHER_STATS = 13; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 162 | |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 163 | /** @hide */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 164 | public static final int NUM_DVK_STATS = 5; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 165 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 166 | /** @hide */ |
| 167 | public static final int NUM_CATEGORIES = 6; |
| 168 | |
| 169 | /** @hide */ |
| 170 | public static final int offsetPss = 0; |
| 171 | /** @hide */ |
| 172 | public static final int offsetSwappablePss = 1; |
| 173 | /** @hide */ |
| 174 | public static final int offsetPrivateDirty = 2; |
| 175 | /** @hide */ |
| 176 | public static final int offsetSharedDirty = 3; |
| 177 | /** @hide */ |
| 178 | public static final int offsetPrivateClean = 4; |
| 179 | /** @hide */ |
| 180 | public static final int offsetSharedClean = 5; |
| 181 | |
| 182 | |
| 183 | private int[] otherStats = new int[(NUM_OTHER_STATS+NUM_DVK_STATS)*NUM_CATEGORIES]; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 184 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 185 | public MemoryInfo() { |
| 186 | } |
| 187 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 188 | /** |
| 189 | * Return total PSS memory usage in kB. |
| 190 | */ |
| 191 | public int getTotalPss() { |
| 192 | return dalvikPss + nativePss + otherPss; |
| 193 | } |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 194 | |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 195 | /** |
| 196 | * @hide Return total PSS memory usage in kB. |
| 197 | */ |
| 198 | public int getTotalUss() { |
| 199 | return dalvikPrivateClean + dalvikPrivateDirty |
| 200 | + nativePrivateClean + nativePrivateDirty |
| 201 | + otherPrivateClean + otherPrivateDirty; |
| 202 | } |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * Return total PSS memory usage in kB. |
| 206 | */ |
| 207 | public int getTotalSwappablePss() { |
| 208 | return dalvikSwappablePss + nativeSwappablePss + otherSwappablePss; |
| 209 | } |
| 210 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 211 | /** |
| 212 | * Return total private dirty memory usage in kB. |
| 213 | */ |
| 214 | public int getTotalPrivateDirty() { |
| 215 | return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty; |
| 216 | } |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 217 | |
Dianne Hackborn | 4f21c4c | 2009-09-17 10:24:05 -0700 | [diff] [blame] | 218 | /** |
| 219 | * Return total shared dirty memory usage in kB. |
| 220 | */ |
| 221 | public int getTotalSharedDirty() { |
| 222 | return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty; |
| 223 | } |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 224 | |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 225 | /** |
| 226 | * Return total shared clean memory usage in kB. |
| 227 | */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 228 | public int getTotalPrivateClean() { |
| 229 | return dalvikPrivateClean + nativePrivateClean + otherPrivateClean; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Return total shared clean memory usage in kB. |
| 234 | */ |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 235 | public int getTotalSharedClean() { |
| 236 | return dalvikSharedClean + nativeSharedClean + otherSharedClean; |
| 237 | } |
| 238 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 239 | /* @hide */ |
| 240 | public int getOtherPss(int which) { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 241 | return otherStats[which*NUM_CATEGORIES + offsetPss]; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 244 | |
| 245 | /* @hide */ |
| 246 | public int getOtherSwappablePss(int which) { |
| 247 | return otherStats[which*NUM_CATEGORIES + offsetSwappablePss]; |
| 248 | } |
| 249 | |
| 250 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 251 | /* @hide */ |
| 252 | public int getOtherPrivateDirty(int which) { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 253 | return otherStats[which*NUM_CATEGORIES + offsetPrivateDirty]; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* @hide */ |
| 257 | public int getOtherSharedDirty(int which) { |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 258 | return otherStats[which*NUM_CATEGORIES + offsetSharedDirty]; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 261 | /* @hide */ |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 262 | public int getOtherPrivateClean(int which) { |
| 263 | return otherStats[which*NUM_CATEGORIES + offsetPrivateClean]; |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 264 | } |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 265 | |
| 266 | |
| 267 | /* @hide */ |
| 268 | public int getOtherSharedClean(int which) { |
| 269 | return otherStats[which*NUM_CATEGORIES + offsetSharedClean]; |
| 270 | } |
| 271 | |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 272 | /* @hide */ |
| 273 | public static String getOtherLabel(int which) { |
| 274 | switch (which) { |
Dianne Hackborn | 64770d1 | 2013-05-23 17:51:19 -0700 | [diff] [blame] | 275 | case 0: return "Dalvik Other"; |
| 276 | case 1: return "Stack"; |
| 277 | case 2: return "Cursor"; |
| 278 | case 3: return "Ashmem"; |
| 279 | case 4: return "Other dev"; |
| 280 | case 5: return ".so mmap"; |
| 281 | case 6: return ".jar mmap"; |
| 282 | case 7: return ".apk mmap"; |
| 283 | case 8: return ".ttf mmap"; |
| 284 | case 9: return ".dex mmap"; |
| 285 | case 10: return "code mmap"; |
| 286 | case 11: return "image mmap"; |
| 287 | case 12: return "Other mmap"; |
| 288 | case 13: return ".Heap"; |
| 289 | case 14: return ".LOS"; |
| 290 | case 15: return ".LinearAlloc"; |
| 291 | case 16: return ".GC"; |
| 292 | case 17: return ".JITCache"; |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 293 | default: return "????"; |
| 294 | } |
| 295 | } |
| 296 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 297 | public int describeContents() { |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | public void writeToParcel(Parcel dest, int flags) { |
| 302 | dest.writeInt(dalvikPss); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 303 | dest.writeInt(dalvikSwappablePss); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 304 | dest.writeInt(dalvikPrivateDirty); |
| 305 | dest.writeInt(dalvikSharedDirty); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 306 | dest.writeInt(dalvikPrivateClean); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 307 | dest.writeInt(dalvikSharedClean); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 308 | dest.writeInt(nativePss); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 309 | dest.writeInt(nativeSwappablePss); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 310 | dest.writeInt(nativePrivateDirty); |
| 311 | dest.writeInt(nativeSharedDirty); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 312 | dest.writeInt(nativePrivateClean); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 313 | dest.writeInt(nativeSharedClean); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 314 | dest.writeInt(otherPss); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 315 | dest.writeInt(otherSwappablePss); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 316 | dest.writeInt(otherPrivateDirty); |
| 317 | dest.writeInt(otherSharedDirty); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 318 | dest.writeInt(otherPrivateClean); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 319 | dest.writeInt(otherSharedClean); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 320 | dest.writeIntArray(otherStats); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | public void readFromParcel(Parcel source) { |
| 324 | dalvikPss = source.readInt(); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 325 | dalvikSwappablePss = source.readInt(); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 326 | dalvikPrivateDirty = source.readInt(); |
| 327 | dalvikSharedDirty = source.readInt(); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 328 | dalvikPrivateClean = source.readInt(); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 329 | dalvikSharedClean = source.readInt(); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 330 | nativePss = source.readInt(); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 331 | nativeSwappablePss = source.readInt(); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 332 | nativePrivateDirty = source.readInt(); |
| 333 | nativeSharedDirty = source.readInt(); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 334 | nativePrivateClean = source.readInt(); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 335 | nativeSharedClean = source.readInt(); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 336 | otherPss = source.readInt(); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 337 | otherSwappablePss = source.readInt(); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 338 | otherPrivateDirty = source.readInt(); |
| 339 | otherSharedDirty = source.readInt(); |
Anwar Ghuloum | 3c61506 | 2013-05-13 14:18:02 -0700 | [diff] [blame] | 340 | otherPrivateClean = source.readInt(); |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 341 | otherSharedClean = source.readInt(); |
Dianne Hackborn | 0e3328f | 2011-07-17 13:31:17 -0700 | [diff] [blame] | 342 | otherStats = source.createIntArray(); |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 343 | } |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 344 | |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 345 | public static final Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() { |
| 346 | public MemoryInfo createFromParcel(Parcel source) { |
| 347 | return new MemoryInfo(source); |
| 348 | } |
| 349 | public MemoryInfo[] newArray(int size) { |
| 350 | return new MemoryInfo[size]; |
| 351 | } |
| 352 | }; |
| 353 | |
| 354 | private MemoryInfo(Parcel source) { |
| 355 | readFromParcel(source); |
| 356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
| 360 | /** |
| 361 | * Wait until a debugger attaches. As soon as the debugger attaches, |
| 362 | * this returns, so you will need to place a breakpoint after the |
| 363 | * waitForDebugger() call if you want to start tracing immediately. |
| 364 | */ |
| 365 | public static void waitForDebugger() { |
| 366 | if (!VMDebug.isDebuggingEnabled()) { |
| 367 | //System.out.println("debugging not enabled, not waiting"); |
| 368 | return; |
| 369 | } |
| 370 | if (isDebuggerConnected()) |
| 371 | return; |
| 372 | |
| 373 | // if DDMS is listening, inform them of our plight |
| 374 | System.out.println("Sending WAIT chunk"); |
| 375 | byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger" |
| 376 | Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1); |
| 377 | DdmServer.sendChunk(waitChunk); |
| 378 | |
| 379 | mWaiting = true; |
| 380 | while (!isDebuggerConnected()) { |
| 381 | try { Thread.sleep(SPIN_DELAY); } |
| 382 | catch (InterruptedException ie) {} |
| 383 | } |
| 384 | mWaiting = false; |
| 385 | |
| 386 | System.out.println("Debugger has connected"); |
| 387 | |
| 388 | /* |
| 389 | * There is no "ready to go" signal from the debugger, and we're |
| 390 | * not allowed to suspend ourselves -- the debugger expects us to |
| 391 | * be running happily, and gets confused if we aren't. We need to |
| 392 | * allow the debugger a chance to set breakpoints before we start |
| 393 | * running again. |
| 394 | * |
| 395 | * Sit and spin until the debugger has been idle for a short while. |
| 396 | */ |
| 397 | while (true) { |
| 398 | long delta = VMDebug.lastDebuggerActivity(); |
| 399 | if (delta < 0) { |
| 400 | System.out.println("debugger detached?"); |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | if (delta < MIN_DEBUGGER_IDLE) { |
| 405 | System.out.println("waiting for debugger to settle..."); |
| 406 | try { Thread.sleep(SPIN_DELAY); } |
| 407 | catch (InterruptedException ie) {} |
| 408 | } else { |
| 409 | System.out.println("debugger has settled (" + delta + ")"); |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Returns "true" if one or more threads is waiting for a debugger |
| 417 | * to attach. |
| 418 | */ |
| 419 | public static boolean waitingForDebugger() { |
| 420 | return mWaiting; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Determine if a debugger is currently attached. |
| 425 | */ |
| 426 | public static boolean isDebuggerConnected() { |
| 427 | return VMDebug.isDebuggerConnected(); |
| 428 | } |
| 429 | |
| 430 | /** |
Andy McFadden | e577232 | 2010-01-22 07:23:31 -0800 | [diff] [blame] | 431 | * Returns an array of strings that identify VM features. This is |
| 432 | * used by DDMS to determine what sorts of operations the VM can |
| 433 | * perform. |
| 434 | * |
| 435 | * @hide |
| 436 | */ |
| 437 | public static String[] getVmFeatureList() { |
| 438 | return VMDebug.getVmFeatureList(); |
| 439 | } |
| 440 | |
| 441 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | * Change the JDWP port. |
| 443 | * |
| 444 | * @deprecated no longer needed or useful |
| 445 | */ |
| 446 | @Deprecated |
| 447 | public static void changeDebugPort(int port) {} |
| 448 | |
| 449 | /** |
| 450 | * This is the pathname to the sysfs file that enables and disables |
| 451 | * tracing on the qemu emulator. |
| 452 | */ |
| 453 | private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state"; |
| 454 | |
| 455 | /** |
| 456 | * Enable qemu tracing. For this to work requires running everything inside |
| 457 | * the qemu emulator; otherwise, this method will have no effect. The trace |
| 458 | * file is specified on the command line when the emulator is started. For |
| 459 | * example, the following command line <br /> |
| 460 | * <code>emulator -trace foo</code><br /> |
| 461 | * will start running the emulator and create a trace file named "foo". This |
| 462 | * method simply enables writing the trace records to the trace file. |
| 463 | * |
| 464 | * <p> |
| 465 | * The main differences between this and {@link #startMethodTracing()} are |
| 466 | * that tracing in the qemu emulator traces every cpu instruction of every |
| 467 | * process, including kernel code, so we have more complete information, |
| 468 | * including all context switches. We can also get more detailed information |
| 469 | * such as cache misses. The sequence of calls is determined by |
| 470 | * post-processing the instruction trace. The qemu tracing is also done |
| 471 | * without modifying the application or perturbing the timing of calls |
| 472 | * because no instrumentation is added to the application being traced. |
| 473 | * </p> |
| 474 | * |
| 475 | * <p> |
| 476 | * One limitation of using this method compared to using |
| 477 | * {@link #startMethodTracing()} on the real device is that the emulator |
| 478 | * does not model all of the real hardware effects such as memory and |
| 479 | * bus contention. The emulator also has a simple cache model and cannot |
| 480 | * capture all the complexities of a real cache. |
| 481 | * </p> |
| 482 | */ |
| 483 | public static void startNativeTracing() { |
| 484 | // Open the sysfs file for writing and write "1" to it. |
| 485 | PrintWriter outStream = null; |
| 486 | try { |
| 487 | FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 488 | outStream = new FastPrintWriter(fos); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | outStream.println("1"); |
| 490 | } catch (Exception e) { |
| 491 | } finally { |
| 492 | if (outStream != null) |
| 493 | outStream.close(); |
| 494 | } |
| 495 | |
| 496 | VMDebug.startEmulatorTracing(); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing. |
| 501 | * |
| 502 | * <p>Tracing can be started and stopped as many times as desired. When |
| 503 | * the qemu emulator itself is stopped then the buffered trace records |
| 504 | * are flushed and written to the trace file. In fact, it is not necessary |
| 505 | * to call this method at all; simply killing qemu is sufficient. But |
| 506 | * starting and stopping a trace is useful for examining a specific |
| 507 | * region of code.</p> |
| 508 | */ |
| 509 | public static void stopNativeTracing() { |
| 510 | VMDebug.stopEmulatorTracing(); |
| 511 | |
| 512 | // Open the sysfs file for writing and write "0" to it. |
| 513 | PrintWriter outStream = null; |
| 514 | try { |
| 515 | FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 516 | outStream = new FastPrintWriter(fos); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 517 | outStream.println("0"); |
| 518 | } catch (Exception e) { |
| 519 | // We could print an error message here but we probably want |
| 520 | // to quietly ignore errors if we are not running in the emulator. |
| 521 | } finally { |
| 522 | if (outStream != null) |
| 523 | outStream.close(); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Enable "emulator traces", in which information about the current |
| 529 | * method is made available to the "emulator -trace" feature. There |
| 530 | * is no corresponding "disable" call -- this is intended for use by |
| 531 | * the framework when tracing should be turned on and left that way, so |
| 532 | * that traces captured with F9/F10 will include the necessary data. |
| 533 | * |
| 534 | * This puts the VM into "profile" mode, which has performance |
| 535 | * consequences. |
| 536 | * |
| 537 | * To temporarily enable tracing, use {@link #startNativeTracing()}. |
| 538 | */ |
| 539 | public static void enableEmulatorTraceOutput() { |
| 540 | VMDebug.startEmulatorTracing(); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Start method tracing with default log name and buffer size. See <a |
| 545 | href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for |
| 546 | * information about reading these files. Call stopMethodTracing() to stop |
| 547 | * tracing. |
| 548 | */ |
| 549 | public static void startMethodTracing() { |
| 550 | VMDebug.startMethodTracing(DEFAULT_TRACE_FILE_PATH, 0, 0); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Start method tracing, specifying the trace log file name. The trace |
| 555 | * file will be put under "/sdcard" unless an absolute path is given. |
| 556 | * See <a |
| 557 | href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for |
| 558 | * information about reading trace files. |
| 559 | * |
| 560 | * @param traceName Name for the trace log file to create. |
| 561 | * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". |
| 562 | * If the files already exist, they will be truncated. |
| 563 | * If the trace file given does not end in ".trace", it will be appended for you. |
| 564 | */ |
| 565 | public static void startMethodTracing(String traceName) { |
| 566 | startMethodTracing(traceName, 0, 0); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Start method tracing, specifying the trace log file name and the |
| 571 | * buffer size. The trace files will be put under "/sdcard" unless an |
| 572 | * absolute path is given. See <a |
| 573 | href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for |
| 574 | * information about reading trace files. |
| 575 | * @param traceName Name for the trace log file to create. |
| 576 | * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". |
| 577 | * If the files already exist, they will be truncated. |
| 578 | * If the trace file given does not end in ".trace", it will be appended for you. |
| 579 | * |
| 580 | * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB. |
| 581 | */ |
| 582 | public static void startMethodTracing(String traceName, int bufferSize) { |
| 583 | startMethodTracing(traceName, bufferSize, 0); |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Start method tracing, specifying the trace log file name and the |
| 588 | * buffer size. The trace files will be put under "/sdcard" unless an |
| 589 | * absolute path is given. See <a |
| 590 | href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for |
| 591 | * information about reading trace files. |
| 592 | * |
| 593 | * <p> |
| 594 | * When method tracing is enabled, the VM will run more slowly than |
| 595 | * usual, so the timings from the trace files should only be considered |
| 596 | * in relative terms (e.g. was run #1 faster than run #2). The times |
| 597 | * for native methods will not change, so don't try to use this to |
| 598 | * compare the performance of interpreted and native implementations of the |
| 599 | * same method. As an alternative, consider using "native" tracing |
| 600 | * in the emulator via {@link #startNativeTracing()}. |
| 601 | * </p> |
| 602 | * |
| 603 | * @param traceName Name for the trace log file to create. |
| 604 | * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". |
| 605 | * If the files already exist, they will be truncated. |
| 606 | * If the trace file given does not end in ".trace", it will be appended for you. |
| 607 | * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB. |
| 608 | */ |
| 609 | public static void startMethodTracing(String traceName, int bufferSize, |
| 610 | int flags) { |
| 611 | |
| 612 | String pathName = traceName; |
| 613 | if (pathName.charAt(0) != '/') |
| 614 | pathName = DEFAULT_TRACE_PATH_PREFIX + pathName; |
| 615 | if (!pathName.endsWith(DEFAULT_TRACE_EXTENSION)) |
| 616 | pathName = pathName + DEFAULT_TRACE_EXTENSION; |
| 617 | |
| 618 | VMDebug.startMethodTracing(pathName, bufferSize, flags); |
| 619 | } |
| 620 | |
| 621 | /** |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 622 | * Like startMethodTracing(String, int, int), but taking an already-opened |
| 623 | * FileDescriptor in which the trace is written. The file name is also |
| 624 | * supplied simply for logging. Makes a dup of the file descriptor. |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 625 | * |
Dianne Hackborn | 9c8dd55 | 2009-06-23 19:22:52 -0700 | [diff] [blame] | 626 | * Not exposed in the SDK unless we are really comfortable with supporting |
| 627 | * this and find it would be useful. |
| 628 | * @hide |
| 629 | */ |
| 630 | public static void startMethodTracing(String traceName, FileDescriptor fd, |
| 631 | int bufferSize, int flags) { |
| 632 | VMDebug.startMethodTracing(traceName, fd, bufferSize, flags); |
| 633 | } |
| 634 | |
| 635 | /** |
Andy McFadden | 72a20db0c | 2010-01-22 12:20:41 -0800 | [diff] [blame] | 636 | * Starts method tracing without a backing file. When stopMethodTracing |
| 637 | * is called, the result is sent directly to DDMS. (If DDMS is not |
| 638 | * attached when tracing ends, the profiling data will be discarded.) |
| 639 | * |
| 640 | * @hide |
| 641 | */ |
| 642 | public static void startMethodTracingDdms(int bufferSize, int flags) { |
| 643 | VMDebug.startMethodTracingDdms(bufferSize, flags); |
| 644 | } |
| 645 | |
| 646 | /** |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 647 | * Determine whether method tracing is currently active. |
| 648 | * @hide |
| 649 | */ |
| 650 | public static boolean isMethodTracingActive() { |
| 651 | return VMDebug.isMethodTracingActive(); |
| 652 | } |
| 653 | |
| 654 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | * Stop method tracing. |
| 656 | */ |
| 657 | public static void stopMethodTracing() { |
| 658 | VMDebug.stopMethodTracing(); |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Get an indication of thread CPU usage. The value returned |
| 663 | * indicates the amount of time that the current thread has spent |
| 664 | * executing code or waiting for certain types of I/O. |
| 665 | * |
| 666 | * The time is expressed in nanoseconds, and is only meaningful |
| 667 | * when compared to the result from an earlier call. Note that |
| 668 | * nanosecond resolution does not imply nanosecond accuracy. |
| 669 | * |
| 670 | * On system which don't support this operation, the call returns -1. |
| 671 | */ |
| 672 | public static long threadCpuTimeNanos() { |
| 673 | return VMDebug.threadCpuTimeNanos(); |
| 674 | } |
| 675 | |
| 676 | /** |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 677 | * Start counting the number and aggregate size of memory allocations. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | * |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 679 | * <p>The {@link #startAllocCounting() start} method resets the counts and enables counting. |
| 680 | * The {@link #stopAllocCounting() stop} method disables the counting so that the analysis |
| 681 | * code doesn't cause additional allocations. The various <code>get</code> methods return |
| 682 | * the specified value. And the various <code>reset</code> methods reset the specified |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 683 | * count.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | * |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 685 | * <p>Counts are kept for the system as a whole (global) and for each thread. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | * The per-thread counts for threads other than the current thread |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 687 | * are not cleared by the "reset" or "start" calls.</p> |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 688 | * |
| 689 | * @deprecated Accurate counting is a burden on the runtime and may be removed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 690 | */ |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 691 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | public static void startAllocCounting() { |
| 693 | VMDebug.startAllocCounting(); |
| 694 | } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 695 | |
| 696 | /** |
| 697 | * Stop counting the number and aggregate size of memory allocations. |
| 698 | * |
| 699 | * @see #startAllocCounting() |
| 700 | */ |
Ian Rogers | c2a3adb | 2013-04-19 11:31:48 -0700 | [diff] [blame] | 701 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 702 | public static void stopAllocCounting() { |
| 703 | VMDebug.stopAllocCounting(); |
| 704 | } |
| 705 | |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 706 | /** |
| 707 | * Returns the global count of objects allocated by the runtime between a |
| 708 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 709 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | public static int getGlobalAllocCount() { |
| 711 | return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); |
| 712 | } |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 713 | |
| 714 | /** |
| 715 | * Clears the global count of objects allocated. |
| 716 | * @see #getGlobalAllocCount() |
| 717 | */ |
| 718 | public static void resetGlobalAllocCount() { |
| 719 | VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Returns the global size, in bytes, of objects allocated by the runtime between a |
| 724 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 725 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | public static int getGlobalAllocSize() { |
| 727 | return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); |
| 728 | } |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 729 | |
| 730 | /** |
| 731 | * Clears the global size of objects allocated. |
| 732 | * @see #getGlobalAllocCountSize() |
| 733 | */ |
| 734 | public static void resetGlobalAllocSize() { |
| 735 | VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Returns the global count of objects freed by the runtime between a |
| 740 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 741 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 742 | public static int getGlobalFreedCount() { |
| 743 | return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); |
| 744 | } |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 745 | |
| 746 | /** |
| 747 | * Clears the global count of objects freed. |
| 748 | * @see #getGlobalFreedCount() |
| 749 | */ |
| 750 | public static void resetGlobalFreedCount() { |
| 751 | VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); |
| 752 | } |
| 753 | |
| 754 | /** |
| 755 | * Returns the global size, in bytes, of objects freed by the runtime between a |
| 756 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 757 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | public static int getGlobalFreedSize() { |
| 759 | return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); |
| 760 | } |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 761 | |
| 762 | /** |
| 763 | * Clears the global size of objects freed. |
| 764 | * @see #getGlobalFreedSize() |
| 765 | */ |
| 766 | public static void resetGlobalFreedSize() { |
| 767 | VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Returns the number of non-concurrent GC invocations between a |
| 772 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 773 | */ |
| 774 | public static int getGlobalGcInvocationCount() { |
| 775 | return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * Clears the count of non-concurrent GC invocations. |
| 780 | * @see #getGlobalGcInvocationCount() |
| 781 | */ |
| 782 | public static void resetGlobalGcInvocationCount() { |
| 783 | VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * Returns the number of classes successfully initialized (ie those that executed without |
| 788 | * throwing an exception) between a {@link #startAllocCounting() start} and |
| 789 | * {@link #stopAllocCounting() stop}. |
| 790 | */ |
Andy McFadden | c4e1bf7 | 2010-02-22 17:07:36 -0800 | [diff] [blame] | 791 | public static int getGlobalClassInitCount() { |
Andy McFadden | c4e1bf7 | 2010-02-22 17:07:36 -0800 | [diff] [blame] | 792 | return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); |
| 793 | } |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 794 | |
| 795 | /** |
| 796 | * Clears the count of classes initialized. |
| 797 | * @see #getGlobalClassInitCount() |
| 798 | */ |
| 799 | public static void resetGlobalClassInitCount() { |
| 800 | VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Returns the time spent successfully initializing classes between a |
| 805 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 806 | */ |
Andy McFadden | c4e1bf7 | 2010-02-22 17:07:36 -0800 | [diff] [blame] | 807 | public static int getGlobalClassInitTime() { |
| 808 | /* cumulative elapsed time for class initialization, in usec */ |
| 809 | return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); |
| 810 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 811 | |
| 812 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 813 | * Clears the count of time spent initializing classes. |
| 814 | * @see #getGlobalClassInitTime() |
| 815 | */ |
| 816 | public static void resetGlobalClassInitTime() { |
| 817 | VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); |
| 818 | } |
| 819 | |
| 820 | /** |
Carl Shapiro | 7e94284 | 2011-01-12 17:17:45 -0800 | [diff] [blame] | 821 | * This method exists for compatibility and always returns 0. |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 822 | * @deprecated This method is now obsolete. |
| 823 | */ |
| 824 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 825 | public static int getGlobalExternalAllocCount() { |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 826 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 827 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 828 | |
| 829 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 830 | * This method exists for compatibility and has no effect. |
| 831 | * @deprecated This method is now obsolete. |
| 832 | */ |
| 833 | @Deprecated |
| 834 | public static void resetGlobalExternalAllocSize() {} |
| 835 | |
| 836 | /** |
| 837 | * This method exists for compatibility and has no effect. |
| 838 | * @deprecated This method is now obsolete. |
| 839 | */ |
| 840 | @Deprecated |
| 841 | public static void resetGlobalExternalAllocCount() {} |
| 842 | |
| 843 | /** |
Carl Shapiro | 7e94284 | 2011-01-12 17:17:45 -0800 | [diff] [blame] | 844 | * This method exists for compatibility and always returns 0. |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 845 | * @deprecated This method is now obsolete. |
| 846 | */ |
| 847 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | public static int getGlobalExternalAllocSize() { |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 849 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 851 | |
| 852 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 853 | * This method exists for compatibility and always returns 0. |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 854 | * @deprecated This method is now obsolete. |
| 855 | */ |
| 856 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | public static int getGlobalExternalFreedCount() { |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 858 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 860 | |
| 861 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 862 | * This method exists for compatibility and has no effect. |
| 863 | * @deprecated This method is now obsolete. |
| 864 | */ |
| 865 | @Deprecated |
| 866 | public static void resetGlobalExternalFreedCount() {} |
| 867 | |
| 868 | /** |
| 869 | * This method exists for compatibility and has no effect. |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 870 | * @deprecated This method is now obsolete. |
| 871 | */ |
| 872 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | public static int getGlobalExternalFreedSize() { |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 874 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 875 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 876 | |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 877 | /** |
| 878 | * This method exists for compatibility and has no effect. |
| 879 | * @deprecated This method is now obsolete. |
| 880 | */ |
| 881 | @Deprecated |
| 882 | public static void resetGlobalExternalFreedSize() {} |
| 883 | |
| 884 | /** |
| 885 | * Returns the thread-local count of objects allocated by the runtime between a |
| 886 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 887 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 888 | public static int getThreadAllocCount() { |
| 889 | return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); |
| 890 | } |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 891 | |
| 892 | /** |
| 893 | * Clears the thread-local count of objects allocated. |
| 894 | * @see #getThreadAllocCount() |
| 895 | */ |
| 896 | public static void resetThreadAllocCount() { |
| 897 | VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * Returns the thread-local size of objects allocated by the runtime between a |
| 902 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 903 | * @return The allocated size in bytes. |
| 904 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | public static int getThreadAllocSize() { |
| 906 | return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); |
| 907 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 908 | |
| 909 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 910 | * Clears the thread-local count of objects allocated. |
| 911 | * @see #getThreadAllocSize() |
| 912 | */ |
| 913 | public static void resetThreadAllocSize() { |
| 914 | VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * This method exists for compatibility and has no effect. |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 919 | * @deprecated This method is now obsolete. |
| 920 | */ |
| 921 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | public static int getThreadExternalAllocCount() { |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 923 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 925 | |
| 926 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 927 | * This method exists for compatibility and has no effect. |
| 928 | * @deprecated This method is now obsolete. |
| 929 | */ |
| 930 | @Deprecated |
| 931 | public static void resetThreadExternalAllocCount() {} |
| 932 | |
| 933 | /** |
| 934 | * This method exists for compatibility and has no effect. |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 935 | * @deprecated This method is now obsolete. |
| 936 | */ |
| 937 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | public static int getThreadExternalAllocSize() { |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 939 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | } |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 941 | |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 942 | /** |
Carl Shapiro | 7e94284 | 2011-01-12 17:17:45 -0800 | [diff] [blame] | 943 | * This method exists for compatibility and has no effect. |
Carl Shapiro | b596198 | 2010-12-22 15:54:53 -0800 | [diff] [blame] | 944 | * @deprecated This method is now obsolete. |
| 945 | */ |
| 946 | @Deprecated |
| 947 | public static void resetThreadExternalAllocSize() {} |
| 948 | |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 949 | /** |
| 950 | * Returns the number of thread-local non-concurrent GC invocations between a |
| 951 | * {@link #startAllocCounting() start} and {@link #stopAllocCounting() stop}. |
| 952 | */ |
| 953 | public static int getThreadGcInvocationCount() { |
| 954 | return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * Clears the thread-local count of non-concurrent GC invocations. |
| 959 | * @see #getThreadGcInvocationCount() |
| 960 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | public static void resetThreadGcInvocationCount() { |
| 962 | VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); |
| 963 | } |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 964 | |
| 965 | /** |
| 966 | * Clears all the global and thread-local memory allocation counters. |
| 967 | * @see #startAllocCounting() |
| 968 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | public static void resetAllCounts() { |
| 970 | VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS); |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Returns the size of the native heap. |
| 975 | * @return The size of the native heap in bytes. |
| 976 | */ |
| 977 | public static native long getNativeHeapSize(); |
| 978 | |
| 979 | /** |
| 980 | * Returns the amount of allocated memory in the native heap. |
| 981 | * @return The allocated size in bytes. |
| 982 | */ |
| 983 | public static native long getNativeHeapAllocatedSize(); |
| 984 | |
| 985 | /** |
| 986 | * Returns the amount of free memory in the native heap. |
| 987 | * @return The freed size in bytes. |
| 988 | */ |
| 989 | public static native long getNativeHeapFreeSize(); |
| 990 | |
| 991 | /** |
| 992 | * Retrieves information about this processes memory usages. This information is broken down by |
| 993 | * how much is in use by dalivk, the native heap, and everything else. |
| 994 | */ |
| 995 | public static native void getMemoryInfo(MemoryInfo memoryInfo); |
| 996 | |
| 997 | /** |
Dianne Hackborn | 3025ef3 | 2009-08-31 21:31:47 -0700 | [diff] [blame] | 998 | * Note: currently only works when the requested pid has the same UID |
| 999 | * as the caller. |
| 1000 | * @hide |
| 1001 | */ |
| 1002 | public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo); |
| 1003 | |
| 1004 | /** |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 1005 | * Retrieves the PSS memory used by the process as given by the |
| 1006 | * smaps. |
| 1007 | */ |
| 1008 | public static native long getPss(); |
| 1009 | |
| 1010 | /** |
| 1011 | * Retrieves the PSS memory used by the process as given by the |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 1012 | * smaps. Optionally supply a long array of 1 entry to also |
| 1013 | * receive the uss of the process. @hide |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 1014 | */ |
Dianne Hackborn | c823051 | 2013-07-13 21:32:12 -0700 | [diff] [blame] | 1015 | public static native long getPss(int pid, long[] outUss); |
Dianne Hackborn | b437e09 | 2011-08-05 17:50:29 -0700 | [diff] [blame] | 1016 | |
| 1017 | /** |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1018 | * Establish an object allocation limit in the current thread. |
Carl Shapiro | 7e94284 | 2011-01-12 17:17:45 -0800 | [diff] [blame] | 1019 | * This feature was never enabled in release builds. The |
| 1020 | * allocation limits feature was removed in Honeycomb. This |
| 1021 | * method exists for compatibility and always returns -1 and has |
| 1022 | * no effect. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | * |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1024 | * @deprecated This method is now obsolete. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | */ |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1026 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | public static int setAllocationLimit(int limit) { |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1028 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /** |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1032 | * Establish a global object allocation limit. This feature was |
Carl Shapiro | 7e94284 | 2011-01-12 17:17:45 -0800 | [diff] [blame] | 1033 | * never enabled in release builds. The allocation limits feature |
| 1034 | * was removed in Honeycomb. This method exists for compatibility |
| 1035 | * and always returns -1 and has no effect. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | * |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1037 | * @deprecated This method is now obsolete. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | */ |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1039 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1040 | public static int setGlobalAllocationLimit(int limit) { |
Carl Shapiro | 1107383 | 2011-01-12 16:28:57 -0800 | [diff] [blame] | 1041 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * Dump a list of all currently loaded class to the log file. |
| 1046 | * |
| 1047 | * @param flags See constants above. |
| 1048 | */ |
| 1049 | public static void printLoadedClasses(int flags) { |
| 1050 | VMDebug.printLoadedClasses(flags); |
| 1051 | } |
| 1052 | |
| 1053 | /** |
| 1054 | * Get the number of loaded classes. |
| 1055 | * @return the number of loaded classes. |
| 1056 | */ |
| 1057 | public static int getLoadedClassCount() { |
| 1058 | return VMDebug.getLoadedClassCount(); |
| 1059 | } |
| 1060 | |
| 1061 | /** |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 1062 | * Dump "hprof" data to the specified file. This may cause a GC. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1063 | * |
| 1064 | * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof"). |
| 1065 | * @throws UnsupportedOperationException if the VM was built without |
| 1066 | * HPROF support. |
| 1067 | * @throws IOException if an error occurs while opening or writing files. |
| 1068 | */ |
| 1069 | public static void dumpHprofData(String fileName) throws IOException { |
| 1070 | VMDebug.dumpHprofData(fileName); |
| 1071 | } |
| 1072 | |
| 1073 | /** |
Andy McFadden | 824c510 | 2010-07-09 16:26:57 -0700 | [diff] [blame] | 1074 | * Like dumpHprofData(String), but takes an already-opened |
| 1075 | * FileDescriptor to which the trace is written. The file name is also |
| 1076 | * supplied simply for logging. Makes a dup of the file descriptor. |
| 1077 | * |
| 1078 | * Primarily for use by the "am" shell command. |
| 1079 | * |
| 1080 | * @hide |
| 1081 | */ |
| 1082 | public static void dumpHprofData(String fileName, FileDescriptor fd) |
| 1083 | throws IOException { |
| 1084 | VMDebug.dumpHprofData(fileName, fd); |
| 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * Collect "hprof" and send it to DDMS. This may cause a GC. |
Andy McFadden | 07a9661 | 2010-01-28 16:54:37 -0800 | [diff] [blame] | 1089 | * |
| 1090 | * @throws UnsupportedOperationException if the VM was built without |
| 1091 | * HPROF support. |
Andy McFadden | 07a9661 | 2010-01-28 16:54:37 -0800 | [diff] [blame] | 1092 | * @hide |
| 1093 | */ |
| 1094 | public static void dumpHprofDataDdms() { |
| 1095 | VMDebug.dumpHprofDataDdms(); |
| 1096 | } |
| 1097 | |
| 1098 | /** |
Andy McFadden | 06a6b55 | 2010-07-13 16:28:09 -0700 | [diff] [blame] | 1099 | * Writes native heap data to the specified file descriptor. |
| 1100 | * |
| 1101 | * @hide |
| 1102 | */ |
| 1103 | public static native void dumpNativeHeap(FileDescriptor fd); |
| 1104 | |
| 1105 | /** |
Brian Carlstrom | c21550a | 2010-10-05 21:34:06 -0700 | [diff] [blame] | 1106 | * Returns a count of the extant instances of a class. |
| 1107 | * |
| 1108 | * @hide |
| 1109 | */ |
| 1110 | public static long countInstancesOfClass(Class cls) { |
Brian Carlstrom | 7495cfa | 2010-11-30 18:06:00 -0800 | [diff] [blame] | 1111 | return VMDebug.countInstancesOfClass(cls, true); |
Brian Carlstrom | c21550a | 2010-10-05 21:34:06 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | * Returns the number of sent transactions from this process. |
| 1116 | * @return The number of sent transactions or -1 if it could not read t. |
| 1117 | */ |
| 1118 | public static native int getBinderSentTransactions(); |
| 1119 | |
| 1120 | /** |
| 1121 | * Returns the number of received transactions from the binder driver. |
| 1122 | * @return The number of received transactions or -1 if it could not read the stats. |
| 1123 | */ |
| 1124 | public static native int getBinderReceivedTransactions(); |
| 1125 | |
| 1126 | /** |
| 1127 | * Returns the number of active local Binder objects that exist in the |
| 1128 | * current process. |
| 1129 | */ |
| 1130 | public static final native int getBinderLocalObjectCount(); |
| 1131 | |
| 1132 | /** |
| 1133 | * Returns the number of references to remote proxy Binder objects that |
| 1134 | * exist in the current process. |
| 1135 | */ |
| 1136 | public static final native int getBinderProxyObjectCount(); |
| 1137 | |
| 1138 | /** |
| 1139 | * Returns the number of death notification links to Binder objects that |
| 1140 | * exist in the current process. |
| 1141 | */ |
| 1142 | public static final native int getBinderDeathObjectCount(); |
| 1143 | |
| 1144 | /** |
Andy McFadden | 599c918 | 2009-04-08 00:35:56 -0700 | [diff] [blame] | 1145 | * Primes the register map cache. |
| 1146 | * |
| 1147 | * Only works for classes in the bootstrap class loader. Does not |
| 1148 | * cause classes to be loaded if they're not already present. |
| 1149 | * |
| 1150 | * The classAndMethodDesc argument is a concatentation of the VM-internal |
| 1151 | * class descriptor, method name, and method descriptor. Examples: |
| 1152 | * Landroid/os/Looper;.loop:()V |
| 1153 | * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V |
| 1154 | * |
| 1155 | * @param classAndMethodDesc the method to prepare |
| 1156 | * |
| 1157 | * @hide |
| 1158 | */ |
| 1159 | public static final boolean cacheRegisterMap(String classAndMethodDesc) { |
| 1160 | return VMDebug.cacheRegisterMap(classAndMethodDesc); |
| 1161 | } |
| 1162 | |
| 1163 | /** |
Andy McFadden | bfd6d48 | 2009-10-22 17:25:57 -0700 | [diff] [blame] | 1164 | * Dumps the contents of VM reference tables (e.g. JNI locals and |
| 1165 | * globals) to the log file. |
| 1166 | * |
| 1167 | * @hide |
| 1168 | */ |
| 1169 | public static final void dumpReferenceTables() { |
| 1170 | VMDebug.dumpReferenceTables(); |
| 1171 | } |
| 1172 | |
| 1173 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | * API for gathering and querying instruction counts. |
| 1175 | * |
| 1176 | * Example usage: |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1177 | * <pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | * Debug.InstructionCount icount = new Debug.InstructionCount(); |
| 1179 | * icount.resetAndStart(); |
| 1180 | * [... do lots of stuff ...] |
| 1181 | * if (icount.collect()) { |
| 1182 | * System.out.println("Total instructions executed: " |
| 1183 | * + icount.globalTotal()); |
| 1184 | * System.out.println("Method invocations: " |
| 1185 | * + icount.globalMethodInvocations()); |
| 1186 | * } |
Chet Haase | 2970c49 | 2010-11-09 13:58:04 -0800 | [diff] [blame] | 1187 | * </pre> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1188 | */ |
| 1189 | public static class InstructionCount { |
Dan Bornstein | b96f589 | 2010-12-02 17:19:53 -0800 | [diff] [blame] | 1190 | private static final int NUM_INSTR = |
| 1191 | OpcodeInfo.MAXIMUM_PACKED_VALUE + 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | |
| 1193 | private int[] mCounts; |
| 1194 | |
| 1195 | public InstructionCount() { |
| 1196 | mCounts = new int[NUM_INSTR]; |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * Reset counters and ensure counts are running. Counts may |
| 1201 | * have already been running. |
| 1202 | * |
| 1203 | * @return true if counting was started |
| 1204 | */ |
| 1205 | public boolean resetAndStart() { |
| 1206 | try { |
| 1207 | VMDebug.startInstructionCounting(); |
| 1208 | VMDebug.resetInstructionCount(); |
| 1209 | } catch (UnsupportedOperationException uoe) { |
| 1210 | return false; |
| 1211 | } |
| 1212 | return true; |
| 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * Collect instruction counts. May or may not stop the |
| 1217 | * counting process. |
| 1218 | */ |
| 1219 | public boolean collect() { |
| 1220 | try { |
| 1221 | VMDebug.stopInstructionCounting(); |
| 1222 | VMDebug.getInstructionCount(mCounts); |
| 1223 | } catch (UnsupportedOperationException uoe) { |
| 1224 | return false; |
| 1225 | } |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Return the total number of instructions executed globally (i.e. in |
| 1231 | * all threads). |
| 1232 | */ |
| 1233 | public int globalTotal() { |
| 1234 | int count = 0; |
Dan Bornstein | 1d99b06 | 2010-11-30 12:26:52 -0800 | [diff] [blame] | 1235 | |
| 1236 | for (int i = 0; i < NUM_INSTR; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | count += mCounts[i]; |
Dan Bornstein | 1d99b06 | 2010-11-30 12:26:52 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1240 | return count; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * Return the total number of method-invocation instructions |
| 1245 | * executed globally. |
| 1246 | */ |
| 1247 | public int globalMethodInvocations() { |
| 1248 | int count = 0; |
| 1249 | |
Dan Bornstein | 1d99b06 | 2010-11-30 12:26:52 -0800 | [diff] [blame] | 1250 | for (int i = 0; i < NUM_INSTR; i++) { |
| 1251 | if (OpcodeInfo.isInvoke(i)) { |
| 1252 | count += mCounts[i]; |
| 1253 | } |
| 1254 | } |
| 1255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | return count; |
| 1257 | } |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1260 | /** |
| 1261 | * A Map of typed debug properties. |
| 1262 | */ |
| 1263 | private static final TypedProperties debugProperties; |
| 1264 | |
| 1265 | /* |
| 1266 | * Load the debug properties from the standard files into debugProperties. |
| 1267 | */ |
| 1268 | static { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 1269 | if (false) { |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1270 | final String TAG = "DebugProperties"; |
| 1271 | final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" }; |
| 1272 | final TypedProperties tp = new TypedProperties(); |
| 1273 | |
| 1274 | // Read the properties from each of the files, if present. |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1275 | for (String file : files) { |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1276 | Reader r; |
| 1277 | try { |
| 1278 | r = new FileReader(file); |
| 1279 | } catch (FileNotFoundException ex) { |
| 1280 | // It's ok if a file is missing. |
| 1281 | continue; |
| 1282 | } |
| 1283 | |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1284 | try { |
| 1285 | tp.load(r); |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1286 | } catch (Exception ex) { |
| 1287 | throw new RuntimeException("Problem loading " + file, ex); |
| 1288 | } finally { |
| 1289 | try { |
| 1290 | r.close(); |
| 1291 | } catch (IOException ex) { |
| 1292 | // Ignore this error. |
| 1293 | } |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | debugProperties = tp.isEmpty() ? null : tp; |
| 1298 | } else { |
| 1299 | debugProperties = null; |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | /** |
| 1305 | * Returns true if the type of the field matches the specified class. |
| 1306 | * Handles the case where the class is, e.g., java.lang.Boolean, but |
| 1307 | * the field is of the primitive "boolean" type. Also handles all of |
| 1308 | * the java.lang.Number subclasses. |
| 1309 | */ |
| 1310 | private static boolean fieldTypeMatches(Field field, Class<?> cl) { |
| 1311 | Class<?> fieldClass = field.getType(); |
| 1312 | if (fieldClass == cl) { |
| 1313 | return true; |
| 1314 | } |
| 1315 | Field primitiveTypeField; |
| 1316 | try { |
| 1317 | /* All of the classes we care about (Boolean, Integer, etc.) |
| 1318 | * have a Class field called "TYPE" that points to the corresponding |
| 1319 | * primitive class. |
| 1320 | */ |
| 1321 | primitiveTypeField = cl.getField("TYPE"); |
| 1322 | } catch (NoSuchFieldException ex) { |
| 1323 | return false; |
| 1324 | } |
| 1325 | try { |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1326 | return fieldClass == (Class<?>) primitiveTypeField.get(null); |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1327 | } catch (IllegalAccessException ex) { |
| 1328 | return false; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | |
| 1333 | /** |
| 1334 | * Looks up the property that corresponds to the field, and sets the field's value |
| 1335 | * if the types match. |
| 1336 | */ |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1337 | private static void modifyFieldIfSet(final Field field, final TypedProperties properties, |
| 1338 | final String propertyName) { |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1339 | if (field.getType() == java.lang.String.class) { |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1340 | int stringInfo = properties.getStringInfo(propertyName); |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1341 | switch (stringInfo) { |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1342 | case TypedProperties.STRING_SET: |
| 1343 | // Handle as usual below. |
| 1344 | break; |
| 1345 | case TypedProperties.STRING_NULL: |
| 1346 | try { |
| 1347 | field.set(null, null); // null object for static fields; null string |
| 1348 | } catch (IllegalAccessException ex) { |
| 1349 | throw new IllegalArgumentException( |
| 1350 | "Cannot set field for " + propertyName, ex); |
| 1351 | } |
| 1352 | return; |
| 1353 | case TypedProperties.STRING_NOT_SET: |
| 1354 | return; |
| 1355 | case TypedProperties.STRING_TYPE_MISMATCH: |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1356 | throw new IllegalArgumentException( |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1357 | "Type of " + propertyName + " " + |
| 1358 | " does not match field type (" + field.getType() + ")"); |
| 1359 | default: |
| 1360 | throw new IllegalStateException( |
| 1361 | "Unexpected getStringInfo(" + propertyName + ") return value " + |
| 1362 | stringInfo); |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1363 | } |
| 1364 | } |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1365 | Object value = properties.get(propertyName); |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1366 | if (value != null) { |
| 1367 | if (!fieldTypeMatches(field, value.getClass())) { |
| 1368 | throw new IllegalArgumentException( |
| 1369 | "Type of " + propertyName + " (" + value.getClass() + ") " + |
| 1370 | " does not match field type (" + field.getType() + ")"); |
| 1371 | } |
| 1372 | try { |
| 1373 | field.set(null, value); // null object for static fields |
| 1374 | } catch (IllegalAccessException ex) { |
| 1375 | throw new IllegalArgumentException( |
| 1376 | "Cannot set field for " + propertyName, ex); |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | |
| 1382 | /** |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1383 | * Equivalent to <code>setFieldsOn(cl, false)</code>. |
| 1384 | * |
| 1385 | * @see #setFieldsOn(Class, boolean) |
Romain Guy | d4103d0 | 2009-05-14 12:24:21 -0700 | [diff] [blame] | 1386 | * |
| 1387 | * @hide |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1388 | */ |
| 1389 | public static void setFieldsOn(Class<?> cl) { |
| 1390 | setFieldsOn(cl, false); |
| 1391 | } |
| 1392 | |
| 1393 | /** |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1394 | * Reflectively sets static fields of a class based on internal debugging |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 1395 | * properties. This method is a no-op if false is |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1396 | * false. |
| 1397 | * <p> |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 1398 | * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1399 | * always be false in release builds. This API is typically only useful |
| 1400 | * for platform developers. |
| 1401 | * </p> |
| 1402 | * Class setup: define a class whose only fields are non-final, static |
| 1403 | * primitive types (except for "char") or Strings. In a static block |
| 1404 | * after the field definitions/initializations, pass the class to |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1405 | * this method, Debug.setFieldsOn(). Example: |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1406 | * <pre> |
| 1407 | * package com.example; |
| 1408 | * |
| 1409 | * import android.os.Debug; |
| 1410 | * |
| 1411 | * public class MyDebugVars { |
| 1412 | * public static String s = "a string"; |
| 1413 | * public static String s2 = "second string"; |
| 1414 | * public static String ns = null; |
| 1415 | * public static boolean b = false; |
| 1416 | * public static int i = 5; |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1417 | * @Debug.DebugProperty |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1418 | * public static float f = 0.1f; |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1419 | * @@Debug.DebugProperty |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1420 | * public static double d = 0.5d; |
| 1421 | * |
| 1422 | * // This MUST appear AFTER all fields are defined and initialized! |
| 1423 | * static { |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1424 | * // Sets all the fields |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1425 | * Debug.setFieldsOn(MyDebugVars.class); |
Christian Mehlmauer | 798e2d3 | 2010-06-17 18:24:07 +0200 | [diff] [blame] | 1426 | * |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1427 | * // Sets only the fields annotated with @Debug.DebugProperty |
| 1428 | * // Debug.setFieldsOn(MyDebugVars.class, true); |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1429 | * } |
| 1430 | * } |
| 1431 | * </pre> |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1432 | * setFieldsOn() may override the value of any field in the class based |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1433 | * on internal properties that are fixed at boot time. |
| 1434 | * <p> |
| 1435 | * These properties are only set during platform debugging, and are not |
| 1436 | * meant to be used as a general-purpose properties store. |
| 1437 | * |
| 1438 | * {@hide} |
| 1439 | * |
| 1440 | * @param cl The class to (possibly) modify |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1441 | * @param partial If false, sets all static fields, otherwise, only set |
| 1442 | * fields with the {@link android.os.Debug.DebugProperty} |
| 1443 | * annotation |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1444 | * @throws IllegalArgumentException if any fields are final or non-static, |
| 1445 | * or if the type of the field does not match the type of |
| 1446 | * the internal debugging property value. |
| 1447 | */ |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1448 | public static void setFieldsOn(Class<?> cl, boolean partial) { |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 1449 | if (false) { |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1450 | if (debugProperties != null) { |
| 1451 | /* Only look for fields declared directly by the class, |
| 1452 | * so we don't mysteriously change static fields in superclasses. |
| 1453 | */ |
| 1454 | for (Field field : cl.getDeclaredFields()) { |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1455 | if (!partial || field.getAnnotation(DebugProperty.class) != null) { |
| 1456 | final String propertyName = cl.getName() + "." + field.getName(); |
| 1457 | boolean isStatic = Modifier.isStatic(field.getModifiers()); |
| 1458 | boolean isFinal = Modifier.isFinal(field.getModifiers()); |
| 1459 | |
| 1460 | if (!isStatic || isFinal) { |
| 1461 | throw new IllegalArgumentException(propertyName + |
| 1462 | " must be static and non-final"); |
| 1463 | } |
| 1464 | modifyFieldIfSet(field, debugProperties, propertyName); |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1465 | } |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1466 | } |
| 1467 | } |
| 1468 | } else { |
Dan Egnor | 3eda979 | 2010-03-05 13:28:36 -0800 | [diff] [blame] | 1469 | Log.wtf(TAG, |
Dave Bort | e9bfd9b | 2009-05-04 14:35:23 -0700 | [diff] [blame] | 1470 | "setFieldsOn(" + (cl == null ? "null" : cl.getName()) + |
Dave Bort | 1ce5bd3 | 2009-04-22 17:36:56 -0700 | [diff] [blame] | 1471 | ") called in non-DEBUG build"); |
| 1472 | } |
| 1473 | } |
Romain Guy | c4b11a7 | 2009-05-13 15:46:37 -0700 | [diff] [blame] | 1474 | |
| 1475 | /** |
| 1476 | * Annotation to put on fields you want to set with |
| 1477 | * {@link Debug#setFieldsOn(Class, boolean)}. |
| 1478 | * |
| 1479 | * @hide |
| 1480 | */ |
| 1481 | @Target({ ElementType.FIELD }) |
| 1482 | @Retention(RetentionPolicy.RUNTIME) |
| 1483 | public @interface DebugProperty { |
| 1484 | } |
Dan Egnor | 3eda979 | 2010-03-05 13:28:36 -0800 | [diff] [blame] | 1485 | |
| 1486 | /** |
| 1487 | * Get a debugging dump of a system service by name. |
| 1488 | * |
| 1489 | * <p>Most services require the caller to hold android.permission.DUMP. |
| 1490 | * |
| 1491 | * @param name of the service to dump |
| 1492 | * @param fd to write dump output to (usually an output log file) |
| 1493 | * @param args to pass to the service's dump method, may be null |
| 1494 | * @return true if the service was dumped successfully, false if |
| 1495 | * the service could not be found or had an error while dumping |
| 1496 | */ |
| 1497 | public static boolean dumpService(String name, FileDescriptor fd, String[] args) { |
| 1498 | IBinder service = ServiceManager.getService(name); |
| 1499 | if (service == null) { |
| 1500 | Log.e(TAG, "Can't find service to dump: " + name); |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
| 1504 | try { |
| 1505 | service.dump(fd, args); |
| 1506 | return true; |
| 1507 | } catch (RemoteException e) { |
| 1508 | Log.e(TAG, "Can't dump service: " + name, e); |
| 1509 | return false; |
| 1510 | } |
| 1511 | } |
Craig Mautner | a51a956 | 2012-04-17 17:05:26 -0700 | [diff] [blame] | 1512 | |
| 1513 | /** |
Dianne Hackborn | f72467a | 2012-06-08 17:23:59 -0700 | [diff] [blame] | 1514 | * Have the stack traces of the given native process dumped to the |
| 1515 | * specified file. Will be appended to the file. |
| 1516 | * @hide |
| 1517 | */ |
| 1518 | public static native void dumpNativeBacktraceToFile(int pid, String file); |
| 1519 | |
| 1520 | /** |
Craig Mautner | a51a956 | 2012-04-17 17:05:26 -0700 | [diff] [blame] | 1521 | * Return a String describing the calling method and location at a particular stack depth. |
Anwar Ghuloum | 3a8ce1b | 2013-04-26 16:18:28 -0700 | [diff] [blame] | 1522 | * @param callStack the Thread stack |
Craig Mautner | a51a956 | 2012-04-17 17:05:26 -0700 | [diff] [blame] | 1523 | * @param depth the depth of stack to return information for. |
| 1524 | * @return the String describing the caller at that depth. |
| 1525 | */ |
| 1526 | private static String getCaller(StackTraceElement callStack[], int depth) { |
| 1527 | // callStack[4] is the caller of the method that called getCallers() |
| 1528 | if (4 + depth >= callStack.length) { |
| 1529 | return "<bottom of call stack>"; |
| 1530 | } |
| 1531 | StackTraceElement caller = callStack[4 + depth]; |
| 1532 | return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber(); |
| 1533 | } |
| 1534 | |
| 1535 | /** |
| 1536 | * Return a string consisting of methods and locations at multiple call stack levels. |
| 1537 | * @param depth the number of levels to return, starting with the immediate caller. |
| 1538 | * @return a string describing the call stack. |
| 1539 | * {@hide} |
| 1540 | */ |
| 1541 | public static String getCallers(final int depth) { |
| 1542 | final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); |
| 1543 | StringBuffer sb = new StringBuffer(); |
| 1544 | for (int i = 0; i < depth; i++) { |
| 1545 | sb.append(getCaller(callStack, i)).append(" "); |
| 1546 | } |
| 1547 | return sb.toString(); |
| 1548 | } |
| 1549 | |
| 1550 | /** |
Dianne Hackborn | ef03a7f | 2012-10-29 18:46:52 -0700 | [diff] [blame] | 1551 | * Like {@link #getCallers(int)}, but each location is append to the string |
| 1552 | * as a new line with <var>linePrefix</var> in front of it. |
| 1553 | * @param depth the number of levels to return, starting with the immediate caller. |
| 1554 | * @param linePrefix prefix to put in front of each location. |
| 1555 | * @return a string describing the call stack. |
| 1556 | * {@hide} |
| 1557 | */ |
| 1558 | public static String getCallers(final int depth, String linePrefix) { |
| 1559 | final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); |
| 1560 | StringBuffer sb = new StringBuffer(); |
| 1561 | for (int i = 0; i < depth; i++) { |
| 1562 | sb.append(linePrefix).append(getCaller(callStack, i)).append("\n"); |
| 1563 | } |
| 1564 | return sb.toString(); |
| 1565 | } |
| 1566 | |
| 1567 | /** |
Ian Rogers | fe067a4 | 2013-02-22 19:59:23 -0800 | [diff] [blame] | 1568 | * @return a String describing the immediate caller of the calling method. |
Craig Mautner | a51a956 | 2012-04-17 17:05:26 -0700 | [diff] [blame] | 1569 | * {@hide} |
| 1570 | */ |
| 1571 | public static String getCaller() { |
| 1572 | return getCaller(Thread.currentThread().getStackTrace(), 0); |
| 1573 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1574 | } |