Add traces to systrace when binder tracing is enabled

If binder tracing is enabled, we also output the class/method name
to systrace. This is the first step toward a system in which we could
show the full stack trace in systrace.

Test:
adb shell am trace-ipc start
python systrace.py
adb shell am trace-ipc stop

Change-Id: I6d0354c9560c4518c84fb3fdb66219b460e69f67
This commit is contained in:
Jorim Jaggi
2016-11-01 16:17:22 -07:00
parent 46fff5e905
commit 25ee0bc6d1
2 changed files with 25 additions and 4 deletions

View File

@@ -563,7 +563,11 @@ public class Binder implements IBinder {
boolean res;
// Log any exceptions as warnings, don't silently suppress them.
// If the call was FLAG_ONEWAY then these exceptions disappear into the ether.
final boolean tracingEnabled = Binder.isTracingEnabled();
try {
if (tracingEnabled) {
Trace.traceBegin(Trace.TRACE_TAG_ALWAYS, getClass().getName() + ":" + code);
}
res = onTransact(code, data, reply, flags);
} catch (RemoteException|RuntimeException e) {
if (LOG_RUNTIME_EXCEPTION) {
@@ -587,6 +591,10 @@ public class Binder implements IBinder {
reply.setDataPosition(0);
reply.writeException(re);
res = true;
} finally {
if (tracingEnabled) {
Trace.traceEnd(Trace.TRACE_TAG_ALWAYS);
}
}
checkParcel(this, code, reply, "Unreasonably large binder reply buffer");
reply.recycle();
@@ -613,8 +621,21 @@ final class BinderProxy implements IBinder {
public boolean transact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {
Binder.checkParcel(this, code, data, "Unreasonably large binder buffer");
if (Binder.isTracingEnabled()) { Binder.getTransactionTracker().addTrace(); }
return transactNative(code, data, reply, flags);
final boolean tracingEnabled = Binder.isTracingEnabled();
if (tracingEnabled) {
final Throwable tr = new Throwable();
Binder.getTransactionTracker().addTrace(tr);
StackTraceElement stackTraceElement = tr.getStackTrace()[1];
Trace.traceBegin(Trace.TRACE_TAG_ALWAYS,
stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName());
}
try {
return transactNative(code, data, reply, flags);
} finally {
if (tracingEnabled) {
Trace.traceEnd(Trace.TRACE_TAG_ALWAYS);
}
}
}
public native String getInterfaceDescriptor() throws RemoteException;

View File

@@ -43,8 +43,8 @@ public class TransactionTracker {
resetTraces();
}
public void addTrace() {
String trace = Log.getStackTraceString(new Throwable());
public void addTrace(Throwable tr) {
String trace = Log.getStackTraceString(tr);
synchronized (this) {
if (mTraces.containsKey(trace)) {
mTraces.put(trace, mTraces.get(trace) + 1);