| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.inputmethod; |
| 18 |
|
| 19 |
import static android.os.Build.IS_USER; |
| 20 |
|
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.os.RemoteException; |
| 23 |
import android.os.ServiceManager.ServiceNotFoundException; |
| 24 |
import android.util.Log; |
| 25 |
import android.util.proto.ProtoOutputStream; |
| 26 |
import android.view.inputmethod.InputMethodEditorTraceProto.InputMethodClientsTraceFileProto; |
| 27 |
import android.view.inputmethod.InputMethodEditorTraceProto.InputMethodManagerServiceTraceFileProto; |
| 28 |
import android.view.inputmethod.InputMethodEditorTraceProto.InputMethodServiceTraceFileProto; |
| 29 |
import android.view.inputmethod.InputMethodManager; |
| 30 |
|
| 31 |
import com.android.internal.annotations.GuardedBy; |
| 32 |
import com.android.internal.util.TraceBuffer; |
| 33 |
|
| 34 |
import java.io.File; |
| 35 |
import java.io.IOException; |
| 36 |
import java.io.PrintWriter; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class ImeTracingServerImpl extends ImeTracing { |
| 42 |
private static final String TRACE_DIRNAME = "/data/misc/wmtrace/"; |
| 43 |
private static final String TRACE_FILENAME_CLIENTS = "ime_trace_clients.winscope"; |
| 44 |
private static final String TRACE_FILENAME_IMS = "ime_trace_service.winscope"; |
| 45 |
private static final String TRACE_FILENAME_IMMS = "ime_trace_managerservice.winscope"; |
| 46 |
private static final int BUFFER_CAPACITY = 4096 * 1024; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private static final long MAGIC_NUMBER_CLIENTS_VALUE = |
| 52 |
((long) InputMethodClientsTraceFileProto.MAGIC_NUMBER_H << 32) |
| 53 |
| InputMethodClientsTraceFileProto.MAGIC_NUMBER_L; |
| 54 |
|
| 55 |
private static final long MAGIC_NUMBER_IMS_VALUE = |
| 56 |
((long) InputMethodServiceTraceFileProto.MAGIC_NUMBER_H << 32) |
| 57 |
| InputMethodServiceTraceFileProto.MAGIC_NUMBER_L; |
| 58 |
|
| 59 |
private static final long MAGIC_NUMBER_IMMS_VALUE = |
| 60 |
((long) InputMethodManagerServiceTraceFileProto.MAGIC_NUMBER_H << 32) |
| 61 |
| InputMethodManagerServiceTraceFileProto.MAGIC_NUMBER_L; |
| 62 |
|
| 63 |
private final TraceBuffer mBufferClients; |
| 64 |
private final File mTraceFileClients; |
| 65 |
private final TraceBuffer mBufferIms; |
| 66 |
private final File mTraceFileIms; |
| 67 |
private final TraceBuffer mBufferImms; |
| 68 |
private final File mTraceFileImms; |
| 69 |
|
| 70 |
private final Object mEnabledLock = new Object(); |
| 71 |
|
| 72 |
ImeTracingServerImpl() throws ServiceNotFoundException { |
| 73 |
mBufferClients = new TraceBuffer<>(BUFFER_CAPACITY); |
| 74 |
mTraceFileClients = new File(TRACE_DIRNAME + TRACE_FILENAME_CLIENTS); |
| 75 |
mBufferIms = new TraceBuffer<>(BUFFER_CAPACITY); |
| 76 |
mTraceFileIms = new File(TRACE_DIRNAME + TRACE_FILENAME_IMS); |
| 77 |
mBufferImms = new TraceBuffer<>(BUFFER_CAPACITY); |
| 78 |
mTraceFileImms = new File(TRACE_DIRNAME + TRACE_FILENAME_IMMS); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
@Override |
| 88 |
public void addToBuffer(ProtoOutputStream proto, int source) { |
| 89 |
if (isAvailable() && isEnabled()) { |
| 90 |
switch (source) { |
| 91 |
case IME_TRACING_FROM_CLIENT: |
| 92 |
mBufferClients.add(proto); |
| 93 |
return; |
| 94 |
case IME_TRACING_FROM_IMS: |
| 95 |
mBufferIms.add(proto); |
| 96 |
return; |
| 97 |
case IME_TRACING_FROM_IMMS: |
| 98 |
mBufferImms.add(proto); |
| 99 |
return; |
| 100 |
default: |
| 101 |
|
| 102 |
Log.w(TAG, "Request to add to buffer, but source not recognised."); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void triggerClientDump(String where, InputMethodManager immInstance, |
| 109 |
@Nullable byte[] icProto) { |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void triggerServiceDump(String where, ServiceDumper dumper, @Nullable byte[] icProto) { |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void triggerManagerServiceDump(String where) { |
| 120 |
if (!isEnabled() || !isAvailable()) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
synchronized (mDumpInProgressLock) { |
| 125 |
if (mDumpInProgress) { |
| 126 |
return; |
| 127 |
} |
| 128 |
mDumpInProgress = true; |
| 129 |
} |
| 130 |
|
| 131 |
try { |
| 132 |
sendToService(null, IME_TRACING_FROM_IMMS, where); |
| 133 |
} catch (RemoteException e) { |
| 134 |
Log.e(TAG, "Exception while sending ime-related manager service dump to server", e); |
| 135 |
} finally { |
| 136 |
mDumpInProgress = false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
private void writeTracesToFilesLocked() { |
| 141 |
try { |
| 142 |
ProtoOutputStream clientsProto = new ProtoOutputStream(); |
| 143 |
clientsProto.write(InputMethodClientsTraceFileProto.MAGIC_NUMBER, |
| 144 |
MAGIC_NUMBER_CLIENTS_VALUE); |
| 145 |
mBufferClients.writeTraceToFile(mTraceFileClients, clientsProto); |
| 146 |
|
| 147 |
ProtoOutputStream imsProto = new ProtoOutputStream(); |
| 148 |
imsProto.write(InputMethodServiceTraceFileProto.MAGIC_NUMBER, MAGIC_NUMBER_IMS_VALUE); |
| 149 |
mBufferIms.writeTraceToFile(mTraceFileIms, imsProto); |
| 150 |
|
| 151 |
ProtoOutputStream immsProto = new ProtoOutputStream(); |
| 152 |
immsProto.write(InputMethodManagerServiceTraceFileProto.MAGIC_NUMBER, |
| 153 |
MAGIC_NUMBER_IMMS_VALUE); |
| 154 |
mBufferImms.writeTraceToFile(mTraceFileImms, immsProto); |
| 155 |
|
| 156 |
resetBuffers(); |
| 157 |
} catch (IOException e) { |
| 158 |
Log.e(TAG, "Unable to write buffer to file", e); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
@GuardedBy("mEnabledLock") |
| 163 |
@Override |
| 164 |
public void startTrace(@Nullable PrintWriter pw) { |
| 165 |
if (IS_USER) { |
| 166 |
Log.w(TAG, "Warn: Tracing is not supported on user builds."); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
synchronized (mEnabledLock) { |
| 171 |
if (isAvailable() && isEnabled()) { |
| 172 |
Log.w(TAG, "Warn: Tracing is already started."); |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
logAndPrintln(pw, "Starting tracing in " + TRACE_DIRNAME + ": " + TRACE_FILENAME_CLIENTS |
| 177 |
+ ", " + TRACE_FILENAME_IMS + ", " + TRACE_FILENAME_IMMS); |
| 178 |
sEnabled = true; |
| 179 |
resetBuffers(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void stopTrace(@Nullable PrintWriter pw) { |
| 185 |
if (IS_USER) { |
| 186 |
Log.w(TAG, "Warn: Tracing is not supported on user builds."); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
synchronized (mEnabledLock) { |
| 191 |
if (!isAvailable() || !isEnabled()) { |
| 192 |
Log.w(TAG, "Warn: Tracing is not available or not started."); |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
logAndPrintln(pw, "Stopping tracing and writing traces in " + TRACE_DIRNAME + ": " |
| 197 |
+ TRACE_FILENAME_CLIENTS + ", " + TRACE_FILENAME_IMS + ", " |
| 198 |
+ TRACE_FILENAME_IMMS); |
| 199 |
sEnabled = false; |
| 200 |
writeTracesToFilesLocked(); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@Override |
| 208 |
public void saveForBugreport(@Nullable PrintWriter pw) { |
| 209 |
if (IS_USER) { |
| 210 |
return; |
| 211 |
} |
| 212 |
synchronized (mEnabledLock) { |
| 213 |
if (!isAvailable() || !isEnabled()) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
sEnabled = false; |
| 224 |
logAndPrintln(pw, "Writing traces in " + TRACE_DIRNAME + ": " |
| 225 |
+ TRACE_FILENAME_CLIENTS + ", " + TRACE_FILENAME_IMS + ", " |
| 226 |
+ TRACE_FILENAME_IMMS); |
| 227 |
writeTracesToFilesLocked(); |
| 228 |
sEnabled = true; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
private void resetBuffers() { |
| 233 |
mBufferClients.resetBuffer(); |
| 234 |
mBufferIms.resetBuffer(); |
| 235 |
mBufferImms.resetBuffer(); |
| 236 |
} |
| 237 |
} |