Home | History | Annotate | Download | only in server
      1 /*
      2  * Copyright (C) 2018 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 
     17 package com.android.server;
     18 
     19 import android.util.Log;
     20 import android.util.LogWriter;
     21 import android.util.Slog;
     22 
     23 import com.android.internal.annotations.VisibleForTesting;
     24 import com.android.server.Watchdog.HandlerChecker;
     25 
     26 import dalvik.system.AnnotatedStackTraceElement;
     27 import dalvik.system.VMStack;
     28 
     29 import java.io.PrintWriter;
     30 import java.util.List;
     31 
     32 /**
     33  * Class to give diagnostic messages for Watchdogs.
     34  */
     35 class WatchdogDiagnostics {
     36     private static String getBlockedOnString(Object blockedOn) {
     37         return String.format("- waiting to lock <0x%08x> (a %s)",
     38                 System.identityHashCode(blockedOn), blockedOn.getClass().getName());
     39     }
     40 
     41     private static String getLockedString(Object heldLock) {
     42         return String.format("- locked <0x%08x> (a %s)", System.identityHashCode(heldLock),
     43                 heldLock.getClass().getName());
     44     }
     45 
     46     /**
     47      * Print the annotated stack for the given thread. If the annotated stack cannot be retrieved,
     48      * returns false.
     49      */
     50     @VisibleForTesting
     51     public static boolean printAnnotatedStack(Thread thread, PrintWriter out) {
     52         AnnotatedStackTraceElement stack[] = VMStack.getAnnotatedThreadStackTrace(thread);
     53         if (stack == null) {
     54             return false;
     55         }
     56         out.println(thread.getName() + " annotated stack trace:");
     57         for (AnnotatedStackTraceElement element : stack) {
     58             out.println("    at " + element.getStackTraceElement());
     59             if (element.getBlockedOn() != null) {
     60                 out.println("    " + getBlockedOnString(element.getBlockedOn()));
     61             }
     62             if (element.getHeldLocks() != null) {
     63                 for (Object held : element.getHeldLocks()) {
     64                     out.println("    " + getLockedString(held));
     65                 }
     66             }
     67         }
     68         return true;
     69     }
     70 
     71     public static void diagnoseCheckers(final List<HandlerChecker> blockedCheckers) {
     72         PrintWriter out = new PrintWriter(new LogWriter(Log.WARN, Watchdog.TAG, Log.LOG_ID_SYSTEM),
     73                 true);
     74         for (int i=0; i<blockedCheckers.size(); i++) {
     75             Thread blockedThread = blockedCheckers.get(i).getThread();
     76             if (printAnnotatedStack(blockedThread, out)) {
     77                 continue;
     78             }
     79 
     80             // Fall back to "regular" stack trace, if necessary.
     81             Slog.w(Watchdog.TAG, blockedThread.getName() + " stack trace:");
     82             StackTraceElement[] stackTrace = blockedThread.getStackTrace();
     83             for (StackTraceElement element : stackTrace) {
     84                 Slog.w(Watchdog.TAG, "    at " + element);
     85             }
     86         }
     87     }
     88 }
     89