1 /* 2 * Copyright 2021 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.am; 18 19 import android.os.Trace; 20 21 import java.util.UUID; 22 23 /** 24 * Adds a unique id to a trace. 25 * 26 * @hide 27 */ 28 public class TraceErrorLogger { 29 private static final String COUNTER_PREFIX = "ErrorId:"; 30 private static final int PLACEHOLDER_VALUE = 1; 31 isAddErrorIdEnabled()32 public boolean isAddErrorIdEnabled() { 33 return true; 34 } 35 36 /** 37 * Generates a unique id with which to tag a trace. 38 */ generateErrorId()39 public UUID generateErrorId() { 40 return UUID.randomUUID(); 41 } 42 43 /** 44 * Pushes a counter containing a unique id and a label {@link #COUNTER_PREFIX} so that traces 45 * can be uniquely identified. We also add the same id to the dropbox entry of the error, so 46 * that we can join the trace and the error server-side. 47 * 48 * @param processName The process name to include in the error id. 49 * @param errorId The unique id with which to tag the trace. 50 */ addErrorIdToTrace(String processName, UUID errorId)51 public void addErrorIdToTrace(String processName, UUID errorId) { 52 Trace.traceCounter(Trace.TRACE_TAG_ACTIVITY_MANAGER, 53 COUNTER_PREFIX + processName + "#" + errorId.toString(), 54 PLACEHOLDER_VALUE); 55 } 56 57 /** 58 * Pushes a counter containing an ANR/Watchdog subject and a unique id so that the subject 59 * can be uniquely identified. 60 * 61 * @param subject The subject to include in the trace. 62 * @param errorId The unique id with which to tag the trace. 63 */ addSubjectToTrace(String subject, UUID errorId)64 public void addSubjectToTrace(String subject, UUID errorId) { 65 Trace.traceCounter(Trace.TRACE_TAG_ACTIVITY_MANAGER, 66 String.format("Subject(for ErrorId %s):%s", errorId.toString(), subject), 67 PLACEHOLDER_VALUE); 68 } 69 } 70