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