1cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown/* 2cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * Copyright (C) 2012 The Android Open Source Project 3cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * 4cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * Licensed under the Apache License, Version 2.0 (the "License"); 5cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * you may not use this file except in compliance with the License. 6cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * You may obtain a copy of the License at 7cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * 8cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * http://www.apache.org/licenses/LICENSE-2.0 9cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * 10cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * Unless required by applicable law or agreed to in writing, software 11cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * distributed under the License is distributed on an "AS IS" BASIS, 12cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * See the License for the specific language governing permissions and 14cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * limitations under the License. 15cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown */ 16cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown 17cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brownpackage com.android.internal.util; 18cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown 19cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brownimport android.os.Handler; 20cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown 21cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brownimport java.io.PrintWriter; 22cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brownimport java.io.StringWriter; 23cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown 24cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown/** 25cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * Helper functions for dumping the state of system services. 26cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown */ 27cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brownpublic final class DumpUtils { 28cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown private DumpUtils() { 29cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown } 30cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown 31cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown /** 32cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * Helper for dumping state owned by a handler thread. 33cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * 34cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * Because the caller might be holding an important lock that the handler is 35cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * trying to acquire, we use a short timeout to avoid deadlocks. The process 36cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown * is inelegant but this function is only used for debugging purposes. 37cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown */ 38ae6688b09649447e57468b3e7935691bc09ec9b9Dianne Hackborn public static void dumpAsync(Handler handler, final Dump dump, PrintWriter pw, 39ae6688b09649447e57468b3e7935691bc09ec9b9Dianne Hackborn final String prefix, long timeout) { 40cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown final StringWriter sw = new StringWriter(); 41cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown if (handler.runWithScissors(new Runnable() { 42cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown @Override 43cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown public void run() { 448c84109b9fbbf473b225707a38261ff5f99d95fbDianne Hackborn PrintWriter lpw = new FastPrintWriter(sw); 45ae6688b09649447e57468b3e7935691bc09ec9b9Dianne Hackborn dump.dump(lpw, prefix); 46cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown lpw.close(); 47cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown } 48cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown }, timeout)) { 49cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown pw.print(sw.toString()); 50cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown } else { 51cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown pw.println("... timed out"); 52cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown } 53cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown } 54cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown 55cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown public interface Dump { 56ae6688b09649447e57468b3e7935691bc09ec9b9Dianne Hackborn void dump(PrintWriter pw, String prefix); 57cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown } 58cbad976b2a36a0895ca94510d5208a86f66cf596Jeff Brown} 59