|
| 1 | +package backtraceio.library; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Build; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import androidx.annotation.RequiresApi; |
| 8 | + |
| 9 | +import com.facebook.react.bridge.WritableArray; |
| 10 | +import com.facebook.react.bridge.WritableMap; |
| 11 | +import com.facebook.react.bridge.WritableNativeArray; |
| 12 | +import com.facebook.react.bridge.WritableNativeMap; |
| 13 | + |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import backtraceio.library.anr.ActivityManagerExitInfoProvider; |
| 21 | +import backtraceio.library.anr.AppExitInfoDetailsExtractor; |
| 22 | +import backtraceio.library.anr.ExitInfo; |
| 23 | +import backtraceio.library.anr.ExitInfoStackTraceParser; |
| 24 | +import backtraceio.library.anr.ProcessExitInfoProvider; |
| 25 | +import backtraceio.library.anr.StackFrameMapper; |
| 26 | + |
| 27 | +class AnrExitInfoReader { |
| 28 | + private final static transient String LOG_TAG = AnrExitInfoReader.class.getSimpleName(); |
| 29 | + |
| 30 | + private static final int ALL_EXIT_RECORDS = 0; |
| 31 | + |
| 32 | + private final Context context; |
| 33 | + private final ProcessExitInfoProvider exitInfoProvider; |
| 34 | + |
| 35 | + AnrExitInfoReader(Context context) { |
| 36 | + this(context, new ActivityManagerExitInfoProvider(context)); |
| 37 | + } |
| 38 | + |
| 39 | + AnrExitInfoReader(Context context, ProcessExitInfoProvider exitInfoProvider) { |
| 40 | + this.context = context; |
| 41 | + this.exitInfoProvider = exitInfoProvider; |
| 42 | + } |
| 43 | + |
| 44 | + WritableArray read(long sinceEpochMillis) { |
| 45 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { |
| 46 | + return new WritableNativeArray(); |
| 47 | + } |
| 48 | + return readAnrRecords(sinceEpochMillis); |
| 49 | + } |
| 50 | + |
| 51 | + @RequiresApi(Build.VERSION_CODES.R) |
| 52 | + private WritableArray readAnrRecords(long sinceEpochMillis) { |
| 53 | + WritableArray records = new WritableNativeArray(); |
| 54 | + |
| 55 | + List<ExitInfo> exitInfos = this.exitInfoProvider.getHistoricalExitInfo( |
| 56 | + this.context.getPackageName(), 0, ALL_EXIT_RECORDS); |
| 57 | + List<Integer> supportedReasons = this.exitInfoProvider.getSupportedTypesOfExitInfo(); |
| 58 | + |
| 59 | + // oldest first, so a caller advancing a last-seen timestamp cannot skip a record |
| 60 | + List<ExitInfo> ordered = new ArrayList<>(exitInfos); |
| 61 | + Collections.reverse(ordered); |
| 62 | + |
| 63 | + for (ExitInfo exitInfo : ordered) { |
| 64 | + if (!supportedReasons.contains(exitInfo.getReason())) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + if (exitInfo.getTimestamp() <= sinceEpochMillis) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + records.pushMap(describe(exitInfo)); |
| 71 | + } |
| 72 | + |
| 73 | + return records; |
| 74 | + } |
| 75 | + |
| 76 | + @RequiresApi(Build.VERSION_CODES.R) |
| 77 | + private WritableMap describe(ExitInfo exitInfo) { |
| 78 | + String stackTrace = AppExitInfoDetailsExtractor.getStackTraceInfo(exitInfo); |
| 79 | + |
| 80 | + WritableMap record = new WritableNativeMap(); |
| 81 | + // putInt would overflow epoch millis |
| 82 | + record.putDouble("timestamp", (double) exitInfo.getTimestamp()); |
| 83 | + record.putString("message", AppExitInfoDetailsExtractor.getANRMessage(exitInfo)); |
| 84 | + record.putMap("attributes", toWritableMap(AppExitInfoDetailsExtractor.getANRAttributes(exitInfo))); |
| 85 | + record.putString("stackTrace", stackTrace); |
| 86 | + |
| 87 | + Map<String, Object> parsed = parseStackTrace(stackTrace); |
| 88 | + |
| 89 | + StackTraceElement[] frames = ExitInfoStackTraceParser.parseMainThreadStackTrace(parsed); |
| 90 | + if (frames.length > 0) { |
| 91 | + record.putArray("mainThreadFrames", StackFrameMapper.toWritableFrames(frames)); |
| 92 | + } |
| 93 | + |
| 94 | + WritableArray threads = toWritableThreads(ExitInfoStackTraceParser.parseOtherThreadStackTraces(parsed)); |
| 95 | + if (threads.size() > 0) { |
| 96 | + record.putArray("threads", threads); |
| 97 | + } |
| 98 | + return record; |
| 99 | + } |
| 100 | + |
| 101 | + private Map<String, Object> parseStackTrace(String stackTrace) { |
| 102 | + if (stackTrace == null || stackTrace.isEmpty()) { |
| 103 | + return new HashMap<>(); |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + return ExitInfoStackTraceParser.parseANRStackTrace(stackTrace); |
| 108 | + } catch (Exception e) { |
| 109 | + Log.e(LOG_TAG, "Could not parse the ANR stack trace", e); |
| 110 | + return new HashMap<>(); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private WritableArray toWritableThreads(List<ExitInfoStackTraceParser.ThreadStackTrace> threads) { |
| 115 | + WritableArray result = new WritableNativeArray(); |
| 116 | + for (ExitInfoStackTraceParser.ThreadStackTrace thread : threads) { |
| 117 | + WritableMap map = new WritableNativeMap(); |
| 118 | + map.putString("name", thread.getName()); |
| 119 | + map.putArray("frames", StackFrameMapper.toWritableFrames(thread.getFrames())); |
| 120 | + result.pushMap(map); |
| 121 | + } |
| 122 | + return result; |
| 123 | + } |
| 124 | + |
| 125 | + private WritableMap toWritableMap(Map<String, Object> values) { |
| 126 | + WritableMap map = new WritableNativeMap(); |
| 127 | + for (Map.Entry<String, Object> entry : values.entrySet()) { |
| 128 | + String key = entry.getKey(); |
| 129 | + Object value = entry.getValue(); |
| 130 | + |
| 131 | + if (value instanceof String) { |
| 132 | + map.putString(key, (String) value); |
| 133 | + } else if (value instanceof Integer) { |
| 134 | + map.putInt(key, (Integer) value); |
| 135 | + } else if (value instanceof Long || value instanceof Double || value instanceof Float) { |
| 136 | + map.putDouble(key, ((Number) value).doubleValue()); |
| 137 | + } else if (value instanceof Boolean) { |
| 138 | + map.putBoolean(key, (Boolean) value); |
| 139 | + } else if (value == null) { |
| 140 | + map.putNull(key); |
| 141 | + } else { |
| 142 | + map.putString(key, value.toString()); |
| 143 | + } |
| 144 | + } |
| 145 | + return map; |
| 146 | + } |
| 147 | +} |
0 commit comments