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 name of the ANRing process.
49      * @param pid         The pid of the ANRing process.
50      * @param errorId     The unique id with which to tag the trace.
51      */
addProcessInfoAndErrorIdToTrace(String processName, int pid, UUID errorId)52     public void addProcessInfoAndErrorIdToTrace(String processName, int pid, UUID errorId) {
53         Trace.traceCounter(Trace.TRACE_TAG_ACTIVITY_MANAGER,
54                 COUNTER_PREFIX + processName + " " + pid + "#" + errorId.toString(),
55                 PLACEHOLDER_VALUE);
56     }
57 
58     /**
59      * Pushes a counter containing an ANR/Watchdog subject and a unique id so that the subject
60      * can be uniquely identified.
61      *
62      * @param subject The subject to include in the trace.
63      * @param errorId The unique id with which to tag the trace.
64      */
addSubjectToTrace(String subject, UUID errorId)65     public void addSubjectToTrace(String subject, UUID errorId) {
66         Trace.traceCounter(Trace.TRACE_TAG_ACTIVITY_MANAGER,
67                 String.format("Subject(for ErrorId %s):%s", errorId.toString(), subject),
68                 PLACEHOLDER_VALUE);
69     }
70 }
71