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