Debug.java revision 1a4b5a4f02e7d8ff8ff645377d97e6062d36aeaa
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, and another array to also 1075 * retrieve the separate memtrack size. @hide 1076 */ 1077 public static native long getPss(int pid, long[] outUss, long[] outMemtrack); 1078 1079 /** @hide */ 1080 public static final int MEMINFO_TOTAL = 0; 1081 /** @hide */ 1082 public static final int MEMINFO_FREE = 1; 1083 /** @hide */ 1084 public static final int MEMINFO_BUFFERS = 2; 1085 /** @hide */ 1086 public static final int MEMINFO_CACHED = 3; 1087 /** @hide */ 1088 public static final int MEMINFO_SHMEM = 4; 1089 /** @hide */ 1090 public static final int MEMINFO_SLAB = 5; 1091 /** @hide */ 1092 public static final int MEMINFO_SWAP_TOTAL = 6; 1093 /** @hide */ 1094 public static final int MEMINFO_SWAP_FREE = 7; 1095 /** @hide */ 1096 public static final int MEMINFO_ZRAM_TOTAL = 8; 1097 /** @hide */ 1098 public static final int MEMINFO_MAPPED = 9; 1099 /** @hide */ 1100 public static final int MEMINFO_VM_ALLOC_USED = 10; 1101 /** @hide */ 1102 public static final int MEMINFO_PAGE_TABLES = 11; 1103 /** @hide */ 1104 public static final int MEMINFO_KERNEL_STACK = 12; 1105 /** @hide */ 1106 public static final int MEMINFO_COUNT = 13; 1107 1108 /** 1109 * Retrieves /proc/meminfo. outSizes is filled with fields 1110 * as defined by MEMINFO_* offsets. 1111 * @hide 1112 */ 1113 public static native void getMemInfo(long[] outSizes); 1114 1115 /** 1116 * Establish an object allocation limit in the current thread. 1117 * This feature was never enabled in release builds. The 1118 * allocation limits feature was removed in Honeycomb. This 1119 * method exists for compatibility and always returns -1 and has 1120 * no effect. 1121 * 1122 * @deprecated This method is now obsolete. 1123 */ 1124 @Deprecated 1125 public static int setAllocationLimit(int limit) { 1126 return -1; 1127 } 1128 1129 /** 1130 * Establish a global object allocation limit. This feature was 1131 * never enabled in release builds. The allocation limits feature 1132 * was removed in Honeycomb. This method exists for compatibility 1133 * and always returns -1 and has no effect. 1134 * 1135 * @deprecated This method is now obsolete. 1136 */ 1137 @Deprecated 1138 public static int setGlobalAllocationLimit(int limit) { 1139 return -1; 1140 } 1141 1142 /** 1143 * Dump a list of all currently loaded class to the log file. 1144 * 1145 * @param flags See constants above. 1146 */ 1147 public static void printLoadedClasses(int flags) { 1148 VMDebug.printLoadedClasses(flags); 1149 } 1150 1151 /** 1152 * Get the number of loaded classes. 1153 * @return the number of loaded classes. 1154 */ 1155 public static int getLoadedClassCount() { 1156 return VMDebug.getLoadedClassCount(); 1157 } 1158 1159 /** 1160 * Dump "hprof" data to the specified file. This may cause a GC. 1161 * 1162 * @param fileName Full pathname of output file (e.g. "/sdcard/dump.hprof"). 1163 * @throws UnsupportedOperationException if the VM was built without 1164 * HPROF support. 1165 * @throws IOException if an error occurs while opening or writing files. 1166 */ 1167 public static void dumpHprofData(String fileName) throws IOException { 1168 VMDebug.dumpHprofData(fileName); 1169 } 1170 1171 /** 1172 * Like dumpHprofData(String), but takes an already-opened 1173 * FileDescriptor to which the trace is written. The file name is also 1174 * supplied simply for logging. Makes a dup of the file descriptor. 1175 * 1176 * Primarily for use by the "am" shell command. 1177 * 1178 * @hide 1179 */ 1180 public static void dumpHprofData(String fileName, FileDescriptor fd) 1181 throws IOException { 1182 VMDebug.dumpHprofData(fileName, fd); 1183 } 1184 1185 /** 1186 * Collect "hprof" and send it to DDMS. This may cause a GC. 1187 * 1188 * @throws UnsupportedOperationException if the VM was built without 1189 * HPROF support. 1190 * @hide 1191 */ 1192 public static void dumpHprofDataDdms() { 1193 VMDebug.dumpHprofDataDdms(); 1194 } 1195 1196 /** 1197 * Writes native heap data to the specified file descriptor. 1198 * 1199 * @hide 1200 */ 1201 public static native void dumpNativeHeap(FileDescriptor fd); 1202 1203 /** 1204 * Returns a count of the extant instances of a class. 1205 * 1206 * @hide 1207 */ 1208 public static long countInstancesOfClass(Class cls) { 1209 return VMDebug.countInstancesOfClass(cls, true); 1210 } 1211 1212 /** 1213 * Returns the number of sent transactions from this process. 1214 * @return The number of sent transactions or -1 if it could not read t. 1215 */ 1216 public static native int getBinderSentTransactions(); 1217 1218 /** 1219 * Returns the number of received transactions from the binder driver. 1220 * @return The number of received transactions or -1 if it could not read the stats. 1221 */ 1222 public static native int getBinderReceivedTransactions(); 1223 1224 /** 1225 * Returns the number of active local Binder objects that exist in the 1226 * current process. 1227 */ 1228 public static final native int getBinderLocalObjectCount(); 1229 1230 /** 1231 * Returns the number of references to remote proxy Binder objects that 1232 * exist in the current process. 1233 */ 1234 public static final native int getBinderProxyObjectCount(); 1235 1236 /** 1237 * Returns the number of death notification links to Binder objects that 1238 * exist in the current process. 1239 */ 1240 public static final native int getBinderDeathObjectCount(); 1241 1242 /** 1243 * Primes the register map cache. 1244 * 1245 * Only works for classes in the bootstrap class loader. Does not 1246 * cause classes to be loaded if they're not already present. 1247 * 1248 * The classAndMethodDesc argument is a concatentation of the VM-internal 1249 * class descriptor, method name, and method descriptor. Examples: 1250 * Landroid/os/Looper;.loop:()V 1251 * Landroid/app/ActivityThread;.main:([Ljava/lang/String;)V 1252 * 1253 * @param classAndMethodDesc the method to prepare 1254 * 1255 * @hide 1256 */ 1257 public static final boolean cacheRegisterMap(String classAndMethodDesc) { 1258 return VMDebug.cacheRegisterMap(classAndMethodDesc); 1259 } 1260 1261 /** 1262 * Dumps the contents of VM reference tables (e.g. JNI locals and 1263 * globals) to the log file. 1264 * 1265 * @hide 1266 */ 1267 public static final void dumpReferenceTables() { 1268 VMDebug.dumpReferenceTables(); 1269 } 1270 1271 /** 1272 * API for gathering and querying instruction counts. 1273 * 1274 * Example usage: 1275 * <pre> 1276 * Debug.InstructionCount icount = new Debug.InstructionCount(); 1277 * icount.resetAndStart(); 1278 * [... do lots of stuff ...] 1279 * if (icount.collect()) { 1280 * System.out.println("Total instructions executed: " 1281 * + icount.globalTotal()); 1282 * System.out.println("Method invocations: " 1283 * + icount.globalMethodInvocations()); 1284 * } 1285 * </pre> 1286 */ 1287 public static class InstructionCount { 1288 private static final int NUM_INSTR = 1289 OpcodeInfo.MAXIMUM_PACKED_VALUE + 1; 1290 1291 private int[] mCounts; 1292 1293 public InstructionCount() { 1294 mCounts = new int[NUM_INSTR]; 1295 } 1296 1297 /** 1298 * Reset counters and ensure counts are running. Counts may 1299 * have already been running. 1300 * 1301 * @return true if counting was started 1302 */ 1303 public boolean resetAndStart() { 1304 try { 1305 VMDebug.startInstructionCounting(); 1306 VMDebug.resetInstructionCount(); 1307 } catch (UnsupportedOperationException uoe) { 1308 return false; 1309 } 1310 return true; 1311 } 1312 1313 /** 1314 * Collect instruction counts. May or may not stop the 1315 * counting process. 1316 */ 1317 public boolean collect() { 1318 try { 1319 VMDebug.stopInstructionCounting(); 1320 VMDebug.getInstructionCount(mCounts); 1321 } catch (UnsupportedOperationException uoe) { 1322 return false; 1323 } 1324 return true; 1325 } 1326 1327 /** 1328 * Return the total number of instructions executed globally (i.e. in 1329 * all threads). 1330 */ 1331 public int globalTotal() { 1332 int count = 0; 1333 1334 for (int i = 0; i < NUM_INSTR; i++) { 1335 count += mCounts[i]; 1336 } 1337 1338 return count; 1339 } 1340 1341 /** 1342 * Return the total number of method-invocation instructions 1343 * executed globally. 1344 */ 1345 public int globalMethodInvocations() { 1346 int count = 0; 1347 1348 for (int i = 0; i < NUM_INSTR; i++) { 1349 if (OpcodeInfo.isInvoke(i)) { 1350 count += mCounts[i]; 1351 } 1352 } 1353 1354 return count; 1355 } 1356 } 1357 1358 /** 1359 * A Map of typed debug properties. 1360 */ 1361 private static final TypedProperties debugProperties; 1362 1363 /* 1364 * Load the debug properties from the standard files into debugProperties. 1365 */ 1366 static { 1367 if (false) { 1368 final String TAG = "DebugProperties"; 1369 final String[] files = { "/system/debug.prop", "/debug.prop", "/data/debug.prop" }; 1370 final TypedProperties tp = new TypedProperties(); 1371 1372 // Read the properties from each of the files, if present. 1373 for (String file : files) { 1374 Reader r; 1375 try { 1376 r = new FileReader(file); 1377 } catch (FileNotFoundException ex) { 1378 // It's ok if a file is missing. 1379 continue; 1380 } 1381 1382 try { 1383 tp.load(r); 1384 } catch (Exception ex) { 1385 throw new RuntimeException("Problem loading " + file, ex); 1386 } finally { 1387 try { 1388 r.close(); 1389 } catch (IOException ex) { 1390 // Ignore this error. 1391 } 1392 } 1393 } 1394 1395 debugProperties = tp.isEmpty() ? null : tp; 1396 } else { 1397 debugProperties = null; 1398 } 1399 } 1400 1401 1402 /** 1403 * Returns true if the type of the field matches the specified class. 1404 * Handles the case where the class is, e.g., java.lang.Boolean, but 1405 * the field is of the primitive "boolean" type. Also handles all of 1406 * the java.lang.Number subclasses. 1407 */ 1408 private static boolean fieldTypeMatches(Field field, Class<?> cl) { 1409 Class<?> fieldClass = field.getType(); 1410 if (fieldClass == cl) { 1411 return true; 1412 } 1413 Field primitiveTypeField; 1414 try { 1415 /* All of the classes we care about (Boolean, Integer, etc.) 1416 * have a Class field called "TYPE" that points to the corresponding 1417 * primitive class. 1418 */ 1419 primitiveTypeField = cl.getField("TYPE"); 1420 } catch (NoSuchFieldException ex) { 1421 return false; 1422 } 1423 try { 1424 return fieldClass == (Class<?>) primitiveTypeField.get(null); 1425 } catch (IllegalAccessException ex) { 1426 return false; 1427 } 1428 } 1429 1430 1431 /** 1432 * Looks up the property that corresponds to the field, and sets the field's value 1433 * if the types match. 1434 */ 1435 private static void modifyFieldIfSet(final Field field, final TypedProperties properties, 1436 final String propertyName) { 1437 if (field.getType() == java.lang.String.class) { 1438 int stringInfo = properties.getStringInfo(propertyName); 1439 switch (stringInfo) { 1440 case TypedProperties.STRING_SET: 1441 // Handle as usual below. 1442 break; 1443 case TypedProperties.STRING_NULL: 1444 try { 1445 field.set(null, null); // null object for static fields; null string 1446 } catch (IllegalAccessException ex) { 1447 throw new IllegalArgumentException( 1448 "Cannot set field for " + propertyName, ex); 1449 } 1450 return; 1451 case TypedProperties.STRING_NOT_SET: 1452 return; 1453 case TypedProperties.STRING_TYPE_MISMATCH: 1454 throw new IllegalArgumentException( 1455 "Type of " + propertyName + " " + 1456 " does not match field type (" + field.getType() + ")"); 1457 default: 1458 throw new IllegalStateException( 1459 "Unexpected getStringInfo(" + propertyName + ") return value " + 1460 stringInfo); 1461 } 1462 } 1463 Object value = properties.get(propertyName); 1464 if (value != null) { 1465 if (!fieldTypeMatches(field, value.getClass())) { 1466 throw new IllegalArgumentException( 1467 "Type of " + propertyName + " (" + value.getClass() + ") " + 1468 " does not match field type (" + field.getType() + ")"); 1469 } 1470 try { 1471 field.set(null, value); // null object for static fields 1472 } catch (IllegalAccessException ex) { 1473 throw new IllegalArgumentException( 1474 "Cannot set field for " + propertyName, ex); 1475 } 1476 } 1477 } 1478 1479 1480 /** 1481 * Equivalent to <code>setFieldsOn(cl, false)</code>. 1482 * 1483 * @see #setFieldsOn(Class, boolean) 1484 * 1485 * @hide 1486 */ 1487 public static void setFieldsOn(Class<?> cl) { 1488 setFieldsOn(cl, false); 1489 } 1490 1491 /** 1492 * Reflectively sets static fields of a class based on internal debugging 1493 * properties. This method is a no-op if false is 1494 * false. 1495 * <p> 1496 * <strong>NOTE TO APPLICATION DEVELOPERS</strong>: false will 1497 * always be false in release builds. This API is typically only useful 1498 * for platform developers. 1499 * </p> 1500 * Class setup: define a class whose only fields are non-final, static 1501 * primitive types (except for "char") or Strings. In a static block 1502 * after the field definitions/initializations, pass the class to 1503 * this method, Debug.setFieldsOn(). Example: 1504 * <pre> 1505 * package com.example; 1506 * 1507 * import android.os.Debug; 1508 * 1509 * public class MyDebugVars { 1510 * public static String s = "a string"; 1511 * public static String s2 = "second string"; 1512 * public static String ns = null; 1513 * public static boolean b = false; 1514 * public static int i = 5; 1515 * @Debug.DebugProperty 1516 * public static float f = 0.1f; 1517 * @@Debug.DebugProperty 1518 * public static double d = 0.5d; 1519 * 1520 * // This MUST appear AFTER all fields are defined and initialized! 1521 * static { 1522 * // Sets all the fields 1523 * Debug.setFieldsOn(MyDebugVars.class); 1524 * 1525 * // Sets only the fields annotated with @Debug.DebugProperty 1526 * // Debug.setFieldsOn(MyDebugVars.class, true); 1527 * } 1528 * } 1529 * </pre> 1530 * setFieldsOn() may override the value of any field in the class based 1531 * on internal properties that are fixed at boot time. 1532 * <p> 1533 * These properties are only set during platform debugging, and are not 1534 * meant to be used as a general-purpose properties store. 1535 * 1536 * {@hide} 1537 * 1538 * @param cl The class to (possibly) modify 1539 * @param partial If false, sets all static fields, otherwise, only set 1540 * fields with the {@link android.os.Debug.DebugProperty} 1541 * annotation 1542 * @throws IllegalArgumentException if any fields are final or non-static, 1543 * or if the type of the field does not match the type of 1544 * the internal debugging property value. 1545 */ 1546 public static void setFieldsOn(Class<?> cl, boolean partial) { 1547 if (false) { 1548 if (debugProperties != null) { 1549 /* Only look for fields declared directly by the class, 1550 * so we don't mysteriously change static fields in superclasses. 1551 */ 1552 for (Field field : cl.getDeclaredFields()) { 1553 if (!partial || field.getAnnotation(DebugProperty.class) != null) { 1554 final String propertyName = cl.getName() + "." + field.getName(); 1555 boolean isStatic = Modifier.isStatic(field.getModifiers()); 1556 boolean isFinal = Modifier.isFinal(field.getModifiers()); 1557 1558 if (!isStatic || isFinal) { 1559 throw new IllegalArgumentException(propertyName + 1560 " must be static and non-final"); 1561 } 1562 modifyFieldIfSet(field, debugProperties, propertyName); 1563 } 1564 } 1565 } 1566 } else { 1567 Log.wtf(TAG, 1568 "setFieldsOn(" + (cl == null ? "null" : cl.getName()) + 1569 ") called in non-DEBUG build"); 1570 } 1571 } 1572 1573 /** 1574 * Annotation to put on fields you want to set with 1575 * {@link Debug#setFieldsOn(Class, boolean)}. 1576 * 1577 * @hide 1578 */ 1579 @Target({ ElementType.FIELD }) 1580 @Retention(RetentionPolicy.RUNTIME) 1581 public @interface DebugProperty { 1582 } 1583 1584 /** 1585 * Get a debugging dump of a system service by name. 1586 * 1587 * <p>Most services require the caller to hold android.permission.DUMP. 1588 * 1589 * @param name of the service to dump 1590 * @param fd to write dump output to (usually an output log file) 1591 * @param args to pass to the service's dump method, may be null 1592 * @return true if the service was dumped successfully, false if 1593 * the service could not be found or had an error while dumping 1594 */ 1595 public static boolean dumpService(String name, FileDescriptor fd, String[] args) { 1596 IBinder service = ServiceManager.getService(name); 1597 if (service == null) { 1598 Log.e(TAG, "Can't find service to dump: " + name); 1599 return false; 1600 } 1601 1602 try { 1603 service.dump(fd, args); 1604 return true; 1605 } catch (RemoteException e) { 1606 Log.e(TAG, "Can't dump service: " + name, e); 1607 return false; 1608 } 1609 } 1610 1611 /** 1612 * Have the stack traces of the given native process dumped to the 1613 * specified file. Will be appended to the file. 1614 * @hide 1615 */ 1616 public static native void dumpNativeBacktraceToFile(int pid, String file); 1617 1618 /** 1619 * Return a String describing the calling method and location at a particular stack depth. 1620 * @param callStack the Thread stack 1621 * @param depth the depth of stack to return information for. 1622 * @return the String describing the caller at that depth. 1623 */ 1624 private static String getCaller(StackTraceElement callStack[], int depth) { 1625 // callStack[4] is the caller of the method that called getCallers() 1626 if (4 + depth >= callStack.length) { 1627 return "<bottom of call stack>"; 1628 } 1629 StackTraceElement caller = callStack[4 + depth]; 1630 return caller.getClassName() + "." + caller.getMethodName() + ":" + caller.getLineNumber(); 1631 } 1632 1633 /** 1634 * Return a string consisting of methods and locations at multiple call stack levels. 1635 * @param depth the number of levels to return, starting with the immediate caller. 1636 * @return a string describing the call stack. 1637 * {@hide} 1638 */ 1639 public static String getCallers(final int depth) { 1640 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); 1641 StringBuffer sb = new StringBuffer(); 1642 for (int i = 0; i < depth; i++) { 1643 sb.append(getCaller(callStack, i)).append(" "); 1644 } 1645 return sb.toString(); 1646 } 1647 1648 /** 1649 * Return a string consisting of methods and locations at multiple call stack levels. 1650 * @param depth the number of levels to return, starting with the immediate caller. 1651 * @return a string describing the call stack. 1652 * {@hide} 1653 */ 1654 public static String getCallers(final int start, int depth) { 1655 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); 1656 StringBuffer sb = new StringBuffer(); 1657 depth += start; 1658 for (int i = start; i < depth; i++) { 1659 sb.append(getCaller(callStack, i)).append(" "); 1660 } 1661 return sb.toString(); 1662 } 1663 1664 /** 1665 * Like {@link #getCallers(int)}, but each location is append to the string 1666 * as a new line with <var>linePrefix</var> in front of it. 1667 * @param depth the number of levels to return, starting with the immediate caller. 1668 * @param linePrefix prefix to put in front of each location. 1669 * @return a string describing the call stack. 1670 * {@hide} 1671 */ 1672 public static String getCallers(final int depth, String linePrefix) { 1673 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); 1674 StringBuffer sb = new StringBuffer(); 1675 for (int i = 0; i < depth; i++) { 1676 sb.append(linePrefix).append(getCaller(callStack, i)).append("\n"); 1677 } 1678 return sb.toString(); 1679 } 1680 1681 /** 1682 * @return a String describing the immediate caller of the calling method. 1683 * {@hide} 1684 */ 1685 public static String getCaller() { 1686 return getCaller(Thread.currentThread().getStackTrace(), 0); 1687 } 1688} 1689