Debug.java revision 1107383e029e30bb18c77ca887db2af067a79154
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.TypedProperties; 20 21import android.util.Config; 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.OutputStreamWriter; 30import java.io.PrintWriter; 31import java.io.Reader; 32import java.lang.reflect.Field; 33import java.lang.reflect.Modifier; 34import java.lang.annotation.Target; 35import java.lang.annotation.ElementType; 36import java.lang.annotation.Retention; 37import java.lang.annotation.RetentionPolicy; 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.bytecode.Opcodes; 45import dalvik.system.VMDebug; 46 47 48/** 49 * Provides various debugging functions for Android applications, including 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 54href="{@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 */ 59public final class Debug 60{ 61 private static final String TAG = "Debug"; 62 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 */ 98 private static final String DEFAULT_TRACE_PATH_PREFIX = 99 Environment.getExternalStorageDirectory().getPath() + "/"; 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 */ 111 public static class MemoryInfo implements Parcelable { 112 /** The proportional set size for dalvik. */ 113 public int dalvikPss; 114 /** The private dirty pages used by dalvik. */ 115 public int dalvikPrivateDirty; 116 /** The shared dirty pages used by dalvik. */ 117 public int dalvikSharedDirty; 118 119 /** The proportional set size for the native heap. */ 120 public int nativePss; 121 /** The private dirty pages used by the native heap. */ 122 public int nativePrivateDirty; 123 /** The shared dirty pages used by the native heap. */ 124 public int nativeSharedDirty; 125 126 /** The proportional set size for everything else. */ 127 public int otherPss; 128 /** The private dirty pages used by everything else. */ 129 public int otherPrivateDirty; 130 /** The shared dirty pages used by everything else. */ 131 public int otherSharedDirty; 132 133 public MemoryInfo() { 134 } 135 136 /** 137 * Return total PSS memory usage in kB. 138 */ 139 public int getTotalPss() { 140 return dalvikPss + nativePss + otherPss; 141 } 142 143 /** 144 * Return total private dirty memory usage in kB. 145 */ 146 public int getTotalPrivateDirty() { 147 return dalvikPrivateDirty + nativePrivateDirty + otherPrivateDirty; 148 } 149 150 /** 151 * Return total shared dirty memory usage in kB. 152 */ 153 public int getTotalSharedDirty() { 154 return dalvikSharedDirty + nativeSharedDirty + otherSharedDirty; 155 } 156 157 public int describeContents() { 158 return 0; 159 } 160 161 public void writeToParcel(Parcel dest, int flags) { 162 dest.writeInt(dalvikPss); 163 dest.writeInt(dalvikPrivateDirty); 164 dest.writeInt(dalvikSharedDirty); 165 dest.writeInt(nativePss); 166 dest.writeInt(nativePrivateDirty); 167 dest.writeInt(nativeSharedDirty); 168 dest.writeInt(otherPss); 169 dest.writeInt(otherPrivateDirty); 170 dest.writeInt(otherSharedDirty); 171 } 172 173 public void readFromParcel(Parcel source) { 174 dalvikPss = source.readInt(); 175 dalvikPrivateDirty = source.readInt(); 176 dalvikSharedDirty = source.readInt(); 177 nativePss = source.readInt(); 178 nativePrivateDirty = source.readInt(); 179 nativeSharedDirty = source.readInt(); 180 otherPss = source.readInt(); 181 otherPrivateDirty = source.readInt(); 182 otherSharedDirty = source.readInt(); 183 } 184 185 public static final Creator<MemoryInfo> CREATOR = new Creator<MemoryInfo>() { 186 public MemoryInfo createFromParcel(Parcel source) { 187 return new MemoryInfo(source); 188 } 189 public MemoryInfo[] newArray(int size) { 190 return new MemoryInfo[size]; 191 } 192 }; 193 194 private MemoryInfo(Parcel source) { 195 readFromParcel(source); 196 } 197 } 198 199 200 /** 201 * Wait until a debugger attaches. As soon as the debugger attaches, 202 * this returns, so you will need to place a breakpoint after the 203 * waitForDebugger() call if you want to start tracing immediately. 204 */ 205 public static void waitForDebugger() { 206 if (!VMDebug.isDebuggingEnabled()) { 207 //System.out.println("debugging not enabled, not waiting"); 208 return; 209 } 210 if (isDebuggerConnected()) 211 return; 212 213 // if DDMS is listening, inform them of our plight 214 System.out.println("Sending WAIT chunk"); 215 byte[] data = new byte[] { 0 }; // 0 == "waiting for debugger" 216 Chunk waitChunk = new Chunk(ChunkHandler.type("WAIT"), data, 0, 1); 217 DdmServer.sendChunk(waitChunk); 218 219 mWaiting = true; 220 while (!isDebuggerConnected()) { 221 try { Thread.sleep(SPIN_DELAY); } 222 catch (InterruptedException ie) {} 223 } 224 mWaiting = false; 225 226 System.out.println("Debugger has connected"); 227 228 /* 229 * There is no "ready to go" signal from the debugger, and we're 230 * not allowed to suspend ourselves -- the debugger expects us to 231 * be running happily, and gets confused if we aren't. We need to 232 * allow the debugger a chance to set breakpoints before we start 233 * running again. 234 * 235 * Sit and spin until the debugger has been idle for a short while. 236 */ 237 while (true) { 238 long delta = VMDebug.lastDebuggerActivity(); 239 if (delta < 0) { 240 System.out.println("debugger detached?"); 241 break; 242 } 243 244 if (delta < MIN_DEBUGGER_IDLE) { 245 System.out.println("waiting for debugger to settle..."); 246 try { Thread.sleep(SPIN_DELAY); } 247 catch (InterruptedException ie) {} 248 } else { 249 System.out.println("debugger has settled (" + delta + ")"); 250 break; 251 } 252 } 253 } 254 255 /** 256 * Returns "true" if one or more threads is waiting for a debugger 257 * to attach. 258 */ 259 public static boolean waitingForDebugger() { 260 return mWaiting; 261 } 262 263 /** 264 * Determine if a debugger is currently attached. 265 */ 266 public static boolean isDebuggerConnected() { 267 return VMDebug.isDebuggerConnected(); 268 } 269 270 /** 271 * Returns an array of strings that identify VM features. This is 272 * used by DDMS to determine what sorts of operations the VM can 273 * perform. 274 * 275 * @hide 276 */ 277 public static String[] getVmFeatureList() { 278 return VMDebug.getVmFeatureList(); 279 } 280 281 /** 282 * Change the JDWP port. 283 * 284 * @deprecated no longer needed or useful 285 */ 286 @Deprecated 287 public static void changeDebugPort(int port) {} 288 289 /** 290 * This is the pathname to the sysfs file that enables and disables 291 * tracing on the qemu emulator. 292 */ 293 private static final String SYSFS_QEMU_TRACE_STATE = "/sys/qemu_trace/state"; 294 295 /** 296 * Enable qemu tracing. For this to work requires running everything inside 297 * the qemu emulator; otherwise, this method will have no effect. The trace 298 * file is specified on the command line when the emulator is started. For 299 * example, the following command line <br /> 300 * <code>emulator -trace foo</code><br /> 301 * will start running the emulator and create a trace file named "foo". This 302 * method simply enables writing the trace records to the trace file. 303 * 304 * <p> 305 * The main differences between this and {@link #startMethodTracing()} are 306 * that tracing in the qemu emulator traces every cpu instruction of every 307 * process, including kernel code, so we have more complete information, 308 * including all context switches. We can also get more detailed information 309 * such as cache misses. The sequence of calls is determined by 310 * post-processing the instruction trace. The qemu tracing is also done 311 * without modifying the application or perturbing the timing of calls 312 * because no instrumentation is added to the application being traced. 313 * </p> 314 * 315 * <p> 316 * One limitation of using this method compared to using 317 * {@link #startMethodTracing()} on the real device is that the emulator 318 * does not model all of the real hardware effects such as memory and 319 * bus contention. The emulator also has a simple cache model and cannot 320 * capture all the complexities of a real cache. 321 * </p> 322 */ 323 public static void startNativeTracing() { 324 // Open the sysfs file for writing and write "1" to it. 325 PrintWriter outStream = null; 326 try { 327 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); 328 outStream = new PrintWriter(new OutputStreamWriter(fos)); 329 outStream.println("1"); 330 } catch (Exception e) { 331 } finally { 332 if (outStream != null) 333 outStream.close(); 334 } 335 336 VMDebug.startEmulatorTracing(); 337 } 338 339 /** 340 * Stop qemu tracing. See {@link #startNativeTracing()} to start tracing. 341 * 342 * <p>Tracing can be started and stopped as many times as desired. When 343 * the qemu emulator itself is stopped then the buffered trace records 344 * are flushed and written to the trace file. In fact, it is not necessary 345 * to call this method at all; simply killing qemu is sufficient. But 346 * starting and stopping a trace is useful for examining a specific 347 * region of code.</p> 348 */ 349 public static void stopNativeTracing() { 350 VMDebug.stopEmulatorTracing(); 351 352 // Open the sysfs file for writing and write "0" to it. 353 PrintWriter outStream = null; 354 try { 355 FileOutputStream fos = new FileOutputStream(SYSFS_QEMU_TRACE_STATE); 356 outStream = new PrintWriter(new OutputStreamWriter(fos)); 357 outStream.println("0"); 358 } catch (Exception e) { 359 // We could print an error message here but we probably want 360 // to quietly ignore errors if we are not running in the emulator. 361 } finally { 362 if (outStream != null) 363 outStream.close(); 364 } 365 } 366 367 /** 368 * Enable "emulator traces", in which information about the current 369 * method is made available to the "emulator -trace" feature. There 370 * is no corresponding "disable" call -- this is intended for use by 371 * the framework when tracing should be turned on and left that way, so 372 * that traces captured with F9/F10 will include the necessary data. 373 * 374 * This puts the VM into "profile" mode, which has performance 375 * consequences. 376 * 377 * To temporarily enable tracing, use {@link #startNativeTracing()}. 378 */ 379 public static void enableEmulatorTraceOutput() { 380 VMDebug.startEmulatorTracing(); 381 } 382 383 /** 384 * Start method tracing with default log name and buffer size. See <a 385href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 386 * information about reading these files. Call stopMethodTracing() to stop 387 * tracing. 388 */ 389 public static void startMethodTracing() { 390 VMDebug.startMethodTracing(DEFAULT_TRACE_FILE_PATH, 0, 0); 391 } 392 393 /** 394 * Start method tracing, specifying the trace log file name. The trace 395 * file will be put under "/sdcard" unless an absolute path is given. 396 * See <a 397 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 398 * information about reading trace files. 399 * 400 * @param traceName Name for the trace log file to create. 401 * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". 402 * If the files already exist, they will be truncated. 403 * If the trace file given does not end in ".trace", it will be appended for you. 404 */ 405 public static void startMethodTracing(String traceName) { 406 startMethodTracing(traceName, 0, 0); 407 } 408 409 /** 410 * Start method tracing, specifying the trace log file name and the 411 * buffer size. The trace files will be put under "/sdcard" unless an 412 * absolute path is given. See <a 413 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 414 * information about reading trace files. 415 * @param traceName Name for the trace log file to create. 416 * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". 417 * If the files already exist, they will be truncated. 418 * If the trace file given does not end in ".trace", it will be appended for you. 419 * 420 * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB. 421 */ 422 public static void startMethodTracing(String traceName, int bufferSize) { 423 startMethodTracing(traceName, bufferSize, 0); 424 } 425 426 /** 427 * Start method tracing, specifying the trace log file name and the 428 * buffer size. The trace files will be put under "/sdcard" unless an 429 * absolute path is given. See <a 430 href="{@docRoot}guide/developing/tools/traceview.html">Traceview: A Graphical Log Viewer</a> for 431 * information about reading trace files. 432 * 433 * <p> 434 * When method tracing is enabled, the VM will run more slowly than 435 * usual, so the timings from the trace files should only be considered 436 * in relative terms (e.g. was run #1 faster than run #2). The times 437 * for native methods will not change, so don't try to use this to 438 * compare the performance of interpreted and native implementations of the 439 * same method. As an alternative, consider using "native" tracing 440 * in the emulator via {@link #startNativeTracing()}. 441 * </p> 442 * 443 * @param traceName Name for the trace log file to create. 444 * If no name argument is given, this value defaults to "/sdcard/dmtrace.trace". 445 * If the files already exist, they will be truncated. 446 * If the trace file given does not end in ".trace", it will be appended for you. 447 * @param bufferSize The maximum amount of trace data we gather. If not given, it defaults to 8MB. 448 */ 449 public static void startMethodTracing(String traceName, int bufferSize, 450 int flags) { 451 452 String pathName = traceName; 453 if (pathName.charAt(0) != '/') 454 pathName = DEFAULT_TRACE_PATH_PREFIX + pathName; 455 if (!pathName.endsWith(DEFAULT_TRACE_EXTENSION)) 456 pathName = pathName + DEFAULT_TRACE_EXTENSION; 457 458 VMDebug.startMethodTracing(pathName, bufferSize, flags); 459 } 460 461 /** 462 * Like startMethodTracing(String, int, int), but taking an already-opened 463 * FileDescriptor in which the trace is written. The file name is also 464 * supplied simply for logging. Makes a dup of the file descriptor. 465 * 466 * Not exposed in the SDK unless we are really comfortable with supporting 467 * this and find it would be useful. 468 * @hide 469 */ 470 public static void startMethodTracing(String traceName, FileDescriptor fd, 471 int bufferSize, int flags) { 472 VMDebug.startMethodTracing(traceName, fd, bufferSize, flags); 473 } 474 475 /** 476 * Starts method tracing without a backing file. When stopMethodTracing 477 * is called, the result is sent directly to DDMS. (If DDMS is not 478 * attached when tracing ends, the profiling data will be discarded.) 479 * 480 * @hide 481 */ 482 public static void startMethodTracingDdms(int bufferSize, int flags) { 483 VMDebug.startMethodTracingDdms(bufferSize, flags); 484 } 485 486 /** 487 * Determine whether method tracing is currently active. 488 * @hide 489 */ 490 public static boolean isMethodTracingActive() { 491 return VMDebug.isMethodTracingActive(); 492 } 493 494 /** 495 * Stop method tracing. 496 */ 497 public static void stopMethodTracing() { 498 VMDebug.stopMethodTracing(); 499 } 500 501 /** 502 * Get an indication of thread CPU usage. The value returned 503 * indicates the amount of time that the current thread has spent 504 * executing code or waiting for certain types of I/O. 505 * 506 * The time is expressed in nanoseconds, and is only meaningful 507 * when compared to the result from an earlier call. Note that 508 * nanosecond resolution does not imply nanosecond accuracy. 509 * 510 * On system which don't support this operation, the call returns -1. 511 */ 512 public static long threadCpuTimeNanos() { 513 return VMDebug.threadCpuTimeNanos(); 514 } 515 516 /** 517 * Start counting the number and aggregate size of memory allocations. 518 * 519 * <p>The {@link #startAllocCounting() start} function resets the counts and enables counting. 520 * The {@link #stopAllocCounting() stop} function disables the counting so that the analysis 521 * code doesn't cause additional allocations. The various <code>get</code> functions return 522 * the specified value. And the various <code>reset</code> functions reset the specified 523 * count.</p> 524 * 525 * <p>Counts are kept for the system as a whole and for each thread. 526 * The per-thread counts for threads other than the current thread 527 * are not cleared by the "reset" or "start" calls.</p> 528 */ 529 public static void startAllocCounting() { 530 VMDebug.startAllocCounting(); 531 } 532 533 /** 534 * Stop counting the number and aggregate size of memory allocations. 535 * 536 * @see #startAllocCounting() 537 */ 538 public static void stopAllocCounting() { 539 VMDebug.stopAllocCounting(); 540 } 541 542 public static int getGlobalAllocCount() { 543 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 544 } 545 public static int getGlobalAllocSize() { 546 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 547 } 548 public static int getGlobalFreedCount() { 549 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 550 } 551 public static int getGlobalFreedSize() { 552 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 553 } 554 public static int getGlobalClassInitCount() { 555 /* number of classes that have been successfully initialized */ 556 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 557 } 558 public static int getGlobalClassInitTime() { 559 /* cumulative elapsed time for class initialization, in usec */ 560 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 561 } 562 563 /** 564 * Returns the global count of external allocation requests. Now 565 * that external allocation tracking no longer exists this method 566 * always returns 0. 567 * 568 * @deprecated This method is now obsolete. 569 */ 570 @Deprecated 571 public static int getGlobalExternalAllocCount() { 572 return 0; 573 } 574 575 /** 576 * Returns the global count of bytes externally allocated. Now 577 * that external allocation tracking no longer exists this method 578 * always returns 0. 579 * 580 * @deprecated This method is now obsolete. 581 */ 582 @Deprecated 583 public static int getGlobalExternalAllocSize() { 584 return 0; 585 } 586 587 /** 588 * Returns the global count of freed external allocation requests. 589 * Now that external allocation tracking no longer exists this 590 * method always returns 0. 591 * 592 * @deprecated This method is now obsolete. 593 */ 594 @Deprecated 595 public static int getGlobalExternalFreedCount() { 596 return 0; 597 } 598 599 /** 600 * Returns the global count of freed bytes from external 601 * allocation requests. Now that external allocation tracking no 602 * longer exists this method always returns 0. 603 * 604 * @deprecated This method is now obsolete. 605 */ 606 @Deprecated 607 public static int getGlobalExternalFreedSize() { 608 return 0; 609 } 610 611 public static int getGlobalGcInvocationCount() { 612 return VMDebug.getAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 613 } 614 public static int getThreadAllocCount() { 615 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 616 } 617 public static int getThreadAllocSize() { 618 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 619 } 620 621 /** 622 * Returns the count of external allocation requests made by the 623 * current thread. Now that external allocation tracking no 624 * longer exists this method always returns 0. 625 * 626 * @deprecated This method is now obsolete. 627 */ 628 @Deprecated 629 public static int getThreadExternalAllocCount() { 630 return 0; 631 } 632 633 /** 634 * Returns the global count of bytes externally allocated. Now 635 * that external allocation tracking no longer exists this method 636 * always returns 0. 637 * 638 * @deprecated This method is now obsolete. 639 */ 640 @Deprecated 641 public static int getThreadExternalAllocSize() { 642 return 0; 643 } 644 645 public static int getThreadGcInvocationCount() { 646 return VMDebug.getAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 647 } 648 649 public static void resetGlobalAllocCount() { 650 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_OBJECTS); 651 } 652 public static void resetGlobalAllocSize() { 653 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_ALLOCATED_BYTES); 654 } 655 public static void resetGlobalFreedCount() { 656 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_OBJECTS); 657 } 658 public static void resetGlobalFreedSize() { 659 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_FREED_BYTES); 660 } 661 public static void resetGlobalClassInitCount() { 662 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_COUNT); 663 } 664 public static void resetGlobalClassInitTime() { 665 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_CLASS_INIT_TIME); 666 } 667 668 /** 669 * Resets the global count of external allocation requests. Now 670 * that external allocation tracking has been removed this method 671 * has no effect. 672 * 673 * @deprecated This method is now obsolete. 674 */ 675 @Deprecated 676 public static void resetGlobalExternalAllocCount() {} 677 678 /** 679 * Resets the global count of bytes externally allocated. Now 680 * that external allocation tracking has been removed this method 681 * has no effect. 682 * 683 * @deprecated This method is now obsolete. 684 */ 685 @Deprecated 686 public static void resetGlobalExternalAllocSize() {} 687 688 /** 689 * Resets the global count of freed external allocations. Now 690 * that external allocation tracking has been removed this method 691 * has no effect. 692 * 693 * @deprecated This method is now obsolete. 694 */ 695 @Deprecated 696 public static void resetGlobalExternalFreedCount() {} 697 698 /** 699 * Resets the global count counter of freed bytes from external 700 * allocations. Now that external allocation tracking has been 701 * removed this method has no effect. 702 * 703 * @deprecated This method is now obsolete. 704 */ 705 @Deprecated 706 public static void resetGlobalExternalFreedSize() {} 707 708 public static void resetGlobalGcInvocationCount() { 709 VMDebug.resetAllocCount(VMDebug.KIND_GLOBAL_GC_INVOCATIONS); 710 } 711 public static void resetThreadAllocCount() { 712 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_OBJECTS); 713 } 714 public static void resetThreadAllocSize() { 715 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_ALLOCATED_BYTES); 716 } 717 718 /** 719 * Resets the count of external allocation requests made by the 720 * current thread. Now that external allocation tracking has been 721 * removed this method has no effect. 722 * 723 * @deprecated This method is now obsolete. 724 */ 725 @Deprecated 726 public static void resetThreadExternalAllocCount() {} 727 728 /** 729 * Resets the count of bytes externally allocated by the current 730 * thread. Now that external allocation tracking has been removed 731 * this method has no effect. 732 * 733 * @deprecated This method is now obsolete. 734 */ 735 @Deprecated 736 public static void resetThreadExternalAllocSize() {} 737 738 public static void resetThreadGcInvocationCount() { 739 VMDebug.resetAllocCount(VMDebug.KIND_THREAD_GC_INVOCATIONS); 740 } 741 public static void resetAllCounts() { 742 VMDebug.resetAllocCount(VMDebug.KIND_ALL_COUNTS); 743 } 744 745 /** 746 * Returns the size of the native heap. 747 * @return The size of the native heap in bytes. 748 */ 749 public static native long getNativeHeapSize(); 750 751 /** 752 * Returns the amount of allocated memory in the native heap. 753 * @return The allocated size in bytes. 754 */ 755 public static native long getNativeHeapAllocatedSize(); 756 757 /** 758 * Returns the amount of free memory in the native heap. 759 * @return The freed size in bytes. 760 */ 761 public static native long getNativeHeapFreeSize(); 762 763 /** 764 * Retrieves information about this processes memory usages. This information is broken down by 765 * how much is in use by dalivk, the native heap, and everything else. 766 */ 767 public static native void getMemoryInfo(MemoryInfo memoryInfo); 768 769 /** 770 * Note: currently only works when the requested pid has the same UID 771 * as the caller. 772 * @hide 773 */ 774 public static native void getMemoryInfo(int pid, MemoryInfo memoryInfo); 775 776 /** 777 * Establish an object allocation limit in the current thread. 778 * This feature was never enabled in release builds. Now that 779 * allocation limits have been removed this method has no effect. 780 * 781 * @deprecated This method is now obsolete. 782 */ 783 @Deprecated 784 public static int setAllocationLimit(int limit) { 785 return -1; 786 } 787 788 /** 789 * Establish a global object allocation limit. This feature was 790 * never enabled in release builds. Now that allocation limits 791 * have been removed this method has no effect. 792 * 793 * @deprecated This method is now obsolete. 794 */ 795 @Deprecated 796 public static int setGlobalAllocationLimit(int limit) { 797 return -1; 798 } 799 800 /** 801 * Dump a list of all currently loaded class to the log file. 802 * 803 * @param flags See constants above. 804 */ 805 public static void printLoadedClasses(int flags) { 806 VMDebug.printLoadedClasses(flags); 807 } 808 809 /** 810 * Get the number of loaded classes. 811 * @return the number of loaded classes. 812 */ 813 public static int getLoadedClassCount() { 814 return VMDebug.getLoadedClassCount(); 815 } 816 817 /** 818 * Dump "hprof" data to the specified file. This may cause a GC. 819 * 820 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof"). 821 * @throws UnsupportedOperationException if the VM was built without 822 * HPROF support. 823 * @throws IOException if an error occurs while opening or writing files. 824 */ 825 public static void dumpHprofData(String fileName) throws IOException { 826 VMDebug.dumpHprofData(fileName); 827 } 828 829 /** 830 * Like dumpHprofData(String), but takes an already-opened 831 * FileDescriptor to which the trace is written. The file name is also 832 * supplied simply for logging. Makes a dup of the file descriptor. 833 * 834 * Primarily for use by the "am" shell command. 835 * 836 * @hide 837 */ 838 public static void dumpHprofData(String fileName, FileDescriptor fd) 839 throws IOException { 840 VMDebug.dumpHprofData(fileName, fd); 841 } 842 843 /** 844 * Collect "hprof" and send it to DDMS. This may cause a GC. 845 * 846 * @throws UnsupportedOperationException if the VM was built without 847 * HPROF support. 848 * @hide 849 */ 850 public static void dumpHprofDataDdms() { 851 VMDebug.dumpHprofDataDdms(); 852 } 853 854 /** 855 * Writes native heap data to the specified file descriptor. 856 * 857 * @hide 858 */ 859 public static native void dumpNativeHeap(FileDescriptor fd); 860 861 /** 862 * Returns a count of the extant instances of a class. 863 * 864 * @hide 865 */ 866 public static long countInstancesOfClass(Class cls) { 867 return VMDebug.countInstancesOfClass(cls, true); 868 } 869 870 /** 871 * Returns the number of sent transactions from this process. 872 * @return The number of sent transactions or -1 if it could not read t. 873 */ 874 public static native int getBinderSentTransactions(); 875 876 /** 877 * Returns the number of received transactions from the binder driver. 878 * @return The number of received transactions or -1 if it could not read the stats. 879 */ 880 public static native int getBinderReceivedTransactions(); 881 882 /** 883 * Returns the number of active local Binder objects that exist in the 884 * current process. 885 */ 886 public static final native int getBinderLocalObjectCount(); 887 888 /** 889 * Returns the number of references to remote proxy Binder objects that 890 * exist in the current process. 891 */ 892 public static final native int getBinderProxyObjectCount(); 893 894 /** 895 * Returns the number of death notification links to Binder objects that 896 * exist in the current process. 897 */ 898 public static final native int getBinderDeathObjectCount(); 899 900 /** 901 * Primes the register map cache. 902 * 903 * Only works for classes in the bootstrap class loader. Does not 904 * cause classes to be loaded if they're not already present. 905 * 906 * The classAndMethodDesc argument is a concatentation of the VM-internal 907 * class descriptor, method name, and method descriptor. Examples: 908 * Landroid/os/Looper;.loop:()V 909 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V 910 * 911 * @param classAndMethodDesc the method to prepare 912 * 913 * @hide 914 */ 915 public static final boolean cacheRegisterMap(String classAndMethodDesc) { 916 return VMDebug.cacheRegisterMap(classAndMethodDesc); 917 } 918 919 /** 920 * Dumps the contents of VM reference tables (e.g. JNI locals and 921 * globals) to the log file. 922 * 923 * @hide 924 */ 925 public static final void dumpReferenceTables() { 926 VMDebug.dumpReferenceTables(); 927 } 928 929 /** 930 * API for gathering and querying instruction counts. 931 * 932 * Example usage: 933 * <pre> 934 * Debug.InstructionCount icount = new Debug.InstructionCount(); 935 * icount.resetAndStart(); 936 * [... do lots of stuff ...] 937 * if (icount.collect()) { 938 * System.out.println("Total instructions executed: " 939 * + icount.globalTotal()); 940 * System.out.println("Method invocations: " 941 * + icount.globalMethodInvocations()); 942 * } 943 * </pre> 944 */ 945 public static class InstructionCount { 946 private static final int NUM_INSTR = 947 OpcodeInfo.MAXIMUM_PACKED_VALUE + 1; 948 949 private int[] mCounts; 950 951 public InstructionCount() { 952 mCounts = new int[NUM_INSTR]; 953 } 954 955 /** 956 * Reset counters and ensure counts are running. Counts may 957 * have already been running. 958 * 959 * @return true if counting was started 960 */ 961 public boolean resetAndStart() { 962 try { 963 VMDebug.startInstructionCounting(); 964 VMDebug.resetInstructionCount(); 965 } catch (UnsupportedOperationException uoe) { 966 return false; 967 } 968 return true; 969 } 970 971 /** 972 * Collect instruction counts. May or may not stop the 973 * counting process. 974 */ 975 public boolean collect() { 976 try { 977 VMDebug.stopInstructionCounting(); 978 VMDebug.getInstructionCount(mCounts); 979 } catch (UnsupportedOperationException uoe) { 980 return false; 981 } 982 return true; 983 } 984 985 /** 986 * Return the total number of instructions executed globally (i.e. in 987 * all threads). 988 */ 989 public int globalTotal() { 990 int count = 0; 991 992 for (int i = 0; i < NUM_INSTR; i++) { 993 count += mCounts[i]; 994 } 995 996 return count; 997 } 998 999 /** 1000 * Return the total number of method-invocation instructions 1001 * executed globally. 1002 */ 1003 public int globalMethodInvocations() { 1004 int count = 0; 1005 1006 for (int i = 0; i < NUM_INSTR; i++) { 1007 if (OpcodeInfo.isInvoke(i)) { 1008 count += mCounts[i]; 1009 } 1010 } 1011 1012 return count; 1013 } 1014 } 1015 1016 /** 1017 * A Map of typed debug properties. 1018 */ 1019 private static final TypedProperties debugProperties; 1020 1021 /* 1022 * Load the debug properties from the standard files into debugProperties. 1023 */ 1024 static { 1025 if (Config.DEBUG) { 1026 final String TAG = "DebugProperties"; 1027 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" }; 1028 final TypedProperties tp = new TypedProperties(); 1029 1030 // Read the properties from each of the files, if present. 1031 for (String file : files) { 1032 Reader r; 1033 try { 1034 r = new FileReader(file); 1035 } catch (FileNotFoundException ex) { 1036 // It's ok if a file is missing. 1037 continue; 1038 } 1039 1040 try { 1041 tp.load(r); 1042 } catch (Exception ex) { 1043 throw new RuntimeException("Problem loading " + file, ex); 1044 } finally { 1045 try { 1046 r.close(); 1047 } catch (IOException ex) { 1048 // Ignore this error. 1049 } 1050 } 1051 } 1052 1053 debugProperties = tp.isEmpty() ? null : tp; 1054 } else { 1055 debugProperties = null; 1056 } 1057 } 1058 1059 1060 /** 1061 * Returns true if the type of the field matches the specified class. 1062 * Handles the case where the class is, e.g., java.lang.Boolean, but 1063 * the field is of the primitive "boolean" type. Also handles all of 1064 * the java.lang.Number subclasses. 1065 */ 1066 private static boolean fieldTypeMatches(Field field, Class<?> cl) { 1067 Class<?> fieldClass = field.getType(); 1068 if (fieldClass == cl) { 1069 return true; 1070 } 1071 Field primitiveTypeField; 1072 try { 1073 /* All of the classes we care about (Boolean, Integer, etc.) 1074 * have a Class field called "TYPE" that points to the corresponding 1075 * primitive class. 1076 */ 1077 primitiveTypeField = cl.getField("TYPE"); 1078 } catch (NoSuchFieldException ex) { 1079 return false; 1080 } 1081 try { 1082 return fieldClass == (Class<?>) primitiveTypeField.get(null); 1083 } catch (IllegalAccessException ex) { 1084 return false; 1085 } 1086 } 1087 1088 1089 /** 1090 * Looks up the property that corresponds to the field, and sets the field's value 1091 * if the types match. 1092 */ 1093 private static void modifyFieldIfSet(final Field field, final TypedProperties properties, 1094 final String propertyName) { 1095 if (field.getType() == java.lang.String.class) { 1096 int stringInfo = properties.getStringInfo(propertyName); 1097 switch (stringInfo) { 1098 case TypedProperties.STRING_SET: 1099 // Handle as usual below. 1100 break; 1101 case TypedProperties.STRING_NULL: 1102 try { 1103 field.set(null, null); // null object for static fields; null string 1104 } catch (IllegalAccessException ex) { 1105 throw new IllegalArgumentException( 1106 "Cannot set field for " + propertyName, ex); 1107 } 1108 return; 1109 case TypedProperties.STRING_NOT_SET: 1110 return; 1111 case TypedProperties.STRING_TYPE_MISMATCH: 1112 throw new IllegalArgumentException( 1113 "Type of " + propertyName + " " + 1114 " does not match field type (" + field.getType() + ")"); 1115 default: 1116 throw new IllegalStateException( 1117 "Unexpected getStringInfo(" + propertyName + ") return value " + 1118 stringInfo); 1119 } 1120 } 1121 Object value = properties.get(propertyName); 1122 if (value != null) { 1123 if (!fieldTypeMatches(field, value.getClass())) { 1124 throw new IllegalArgumentException( 1125 "Type of " + propertyName + " (" + value.getClass() + ") " + 1126 " does not match field type (" + field.getType() + ")"); 1127 } 1128 try { 1129 field.set(null, value); // null object for static fields 1130 } catch (IllegalAccessException ex) { 1131 throw new IllegalArgumentException( 1132 "Cannot set field for " + propertyName, ex); 1133 } 1134 } 1135 } 1136 1137 1138 /** 1139 * Equivalent to <code>setFieldsOn(cl, false)</code>. 1140 * 1141 * @see #setFieldsOn(Class, boolean) 1142 * 1143 * @hide 1144 */ 1145 public static void setFieldsOn(Class<?> cl) { 1146 setFieldsOn(cl, false); 1147 } 1148 1149 /** 1150 * Reflectively sets static fields of a class based on internal debugging 1151 * properties. This method is a no-op if android.util.Config.DEBUG is 1152 * false. 1153 * <p> 1154 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: Config.DEBUG will 1155 * always be false in release builds. This API is typically only useful 1156 * for platform developers. 1157 * </p> 1158 * Class setup: define a class whose only fields are non-final, static 1159 * primitive types (except for "char") or Strings. In a static block 1160 * after the field definitions/initializations, pass the class to 1161 * this method, Debug.setFieldsOn(). Example: 1162 * <pre> 1163 * package com.example; 1164 * 1165 * import android.os.Debug; 1166 * 1167 * public class MyDebugVars { 1168 * public static String s = "a string"; 1169 * public static String s2 = "second string"; 1170 * public static String ns = null; 1171 * public static boolean b = false; 1172 * public static int i = 5; 1173 * @Debug.DebugProperty 1174 * public static float f = 0.1f; 1175 * @@Debug.DebugProperty 1176 * public static double d = 0.5d; 1177 * 1178 * // This MUST appear AFTER all fields are defined and initialized! 1179 * static { 1180 * // Sets all the fields 1181 * Debug.setFieldsOn(MyDebugVars.class); 1182 * 1183 * // Sets only the fields annotated with @Debug.DebugProperty 1184 * // Debug.setFieldsOn(MyDebugVars.class, true); 1185 * } 1186 * } 1187 * </pre> 1188 * setFieldsOn() may override the value of any field in the class based 1189 * on internal properties that are fixed at boot time. 1190 * <p> 1191 * These properties are only set during platform debugging, and are not 1192 * meant to be used as a general-purpose properties store. 1193 * 1194 * {@hide} 1195 * 1196 * @param cl The class to (possibly) modify 1197 * @param partial If false, sets all static fields, otherwise, only set 1198 * fields with the {@link android.os.Debug.DebugProperty} 1199 * annotation 1200 * @throws IllegalArgumentException if any fields are final or non-static, 1201 * or if the type of the field does not match the type of 1202 * the internal debugging property value. 1203 */ 1204 public static void setFieldsOn(Class<?> cl, boolean partial) { 1205 if (Config.DEBUG) { 1206 if (debugProperties != null) { 1207 /* Only look for fields declared directly by the class, 1208 * so we don't mysteriously change static fields in superclasses. 1209 */ 1210 for (Field field : cl.getDeclaredFields()) { 1211 if (!partial || field.getAnnotation(DebugProperty.class) != null) { 1212 final String propertyName = cl.getName() + "." + field.getName(); 1213 boolean isStatic = Modifier.isStatic(field.getModifiers()); 1214 boolean isFinal = Modifier.isFinal(field.getModifiers()); 1215 1216 if (!isStatic || isFinal) { 1217 throw new IllegalArgumentException(propertyName + 1218 " must be static and non-final"); 1219 } 1220 modifyFieldIfSet(field, debugProperties, propertyName); 1221 } 1222 } 1223 } 1224 } else { 1225 Log.wtf(TAG, 1226 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) + 1227 ") called in non-DEBUG build"); 1228 } 1229 } 1230 1231 /** 1232 * Annotation to put on fields you want to set with 1233 * {@link Debug#setFieldsOn(Class, boolean)}. 1234 * 1235 * @hide 1236 */ 1237 @Target({ ElementType.FIELD }) 1238 @Retention(RetentionPolicy.RUNTIME) 1239 public @interface DebugProperty { 1240 } 1241 1242 /** 1243 * Get a debugging dump of a system service by name. 1244 * 1245 * <p>Most services require the caller to hold android.permission.DUMP. 1246 * 1247 * @param name of the service to dump 1248 * @param fd to write dump output to (usually an output log file) 1249 * @param args to pass to the service's dump method, may be null 1250 * @return true if the service was dumped successfully, false if 1251 * the service could not be found or had an error while dumping 1252 */ 1253 public static boolean dumpService(String name, FileDescriptor fd, String[] args) { 1254 IBinder service = ServiceManager.getService(name); 1255 if (service == null) { 1256 Log.e(TAG, "Can't find service to dump: " + name); 1257 return false; 1258 } 1259 1260 try { 1261 service.dump(fd, args); 1262 return true; 1263 } catch (RemoteException e) { 1264 Log.e(TAG, "Can't dump service: " + name, e); 1265 return false; 1266 } 1267 } 1268} 1269