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