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