package com.apm.insight.nativecrash;

import com.apm.insight.l.j;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* JADX INFO: loaded from: classes.dex */
public final class c {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static final Pattern f5399i = Pattern.compile("^pid:\\s(.*),\\stid:\\s(.*),\\sname:\\s(.*)\\s+>>>\\s(.*)\\s<<<$");

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    private static final Pattern f5400j = Pattern.compile("^signal\\s(.*),\\scode\\s(.*),\\sfault\\saddr\\s(.*)$");

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    private static final Pattern f5401k = Pattern.compile("^Abort message: (.*)$");

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    private static final Pattern f5402l = Pattern.compile("^Crash message: (.*)$");

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    private static final Pattern f5403m = Pattern.compile("^    \\/(\\w*)\\/.*\\/(.*\\.so)\\s\\(BuildId: ([a-f0-9]*)\\)$");

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private String f5404a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private String f5405b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private String f5406c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private String f5407d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private String f5408e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private String f5409f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private String f5410g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private Map<String, String> f5411h = new HashMap();

    public c(File file) {
        c(j.b(file));
    }

    public final String a() {
        return this.f5410g;
    }

    public final Map<String, String> b() {
        return this.f5411h;
    }

    public final String c() {
        StringBuilder sb = new StringBuilder();
        String str = this.f5408e;
        if (str != null) {
            sb.append(str);
        }
        String str2 = this.f5409f;
        if (str2 != null) {
            sb.append(str2);
        }
        String str3 = this.f5410g;
        if (str3 != null) {
            sb.append(str3);
        }
        return sb.toString();
    }

    public final void a(File file) {
        File fileB = j.b(file);
        if (fileB.exists()) {
            fileB.renameTo(new File(fileB.getAbsoluteFile() + ".old"));
        }
        NativeImpl.a(file);
        c(j.b(file));
    }

    public final void b(File file) {
        c(j.b(file));
    }

    /* JADX WARN: Code restructure failed: missing block: B:71:0x018a, code lost:
    
        r12 = r3.readLine();
     */
    /* JADX WARN: Code restructure failed: missing block: B:72:0x018e, code lost:
    
        if (r12 == null) goto L118;
     */
    /* JADX WARN: Code restructure failed: missing block: B:74:0x0196, code lost:
    
        if (r12.contains("BuildId:") == false) goto L119;
     */
    /* JADX WARN: Code restructure failed: missing block: B:75:0x0198, code lost:
    
        r12 = com.apm.insight.nativecrash.c.f5403m.matcher(r12);
     */
    /* JADX WARN: Code restructure failed: missing block: B:76:0x01a2, code lost:
    
        if (r12.find() == false) goto L123;
     */
    /* JADX WARN: Code restructure failed: missing block: B:77:0x01a4, code lost:
    
        r0 = r12.group(1);
        r1 = r12.group(2);
        r12 = r12.group(3);
     */
    /* JADX WARN: Code restructure failed: missing block: B:78:0x01b6, code lost:
    
        if (r0.equals("data") == false) goto L124;
     */
    /* JADX WARN: Code restructure failed: missing block: B:79:0x01b8, code lost:
    
        r11.f5411h.put(r1, r12);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private void c(File file) {
        if (!file.exists() || file.length() == 0) {
            return;
        }
        BufferedReader bufferedReader = null;
        try {
            BufferedReader bufferedReader2 = new BufferedReader(new FileReader(file));
            int i10 = 0;
            while (true) {
                try {
                    String line = bufferedReader2.readLine();
                    if (line == null || i10 >= 64) {
                        break;
                    }
                    if (this.f5404a == null && line.startsWith("pid: ")) {
                        Matcher matcher = f5399i.matcher(line);
                        if (matcher.find() && matcher.groupCount() == 4) {
                            this.f5404a = matcher.group(1);
                            this.f5405b = matcher.group(2);
                            this.f5407d = matcher.group(3);
                            this.f5406c = matcher.group(4);
                        }
                    } else if (this.f5408e == null && line.startsWith("signal ")) {
                        Matcher matcher2 = f5400j.matcher(line);
                        if (matcher2.find() && matcher2.groupCount() == 3) {
                            String strReplace = matcher2.group(1).replace(" ", "");
                            String strReplace2 = matcher2.group(2).replace(" ", "");
                            int iIndexOf = strReplace2.indexOf("frompid");
                            if (iIndexOf > 0) {
                                strReplace2 = strReplace2.substring(0, iIndexOf) + ")";
                            }
                            this.f5408e = "Signal " + strReplace + ", Code " + strReplace2 + "\n";
                        }
                    } else if (this.f5409f == null && line.startsWith("Abort ")) {
                        Matcher matcher3 = f5401k.matcher(line);
                        if (matcher3.find() && matcher3.groupCount() == 1) {
                            this.f5409f = "abort message: " + matcher3.group(1) + "\n";
                        }
                    } else if (this.f5409f == null && line.startsWith("Crash ")) {
                        Matcher matcher4 = f5402l.matcher(line);
                        if (matcher4.find() && matcher4.groupCount() == 1) {
                            this.f5409f = "crash message: " + matcher4.group(1) + "\n";
                        }
                    } else if (this.f5410g == null && line.startsWith("backtrace:")) {
                        StringBuilder sb = new StringBuilder();
                        while (true) {
                            String line2 = bufferedReader2.readLine();
                            if (line2 == null || !line2.startsWith("    #")) {
                                break;
                            }
                            sb.append(line2.substring(4));
                            sb.append('\n');
                        }
                        i10++;
                        this.f5410g = sb.toString();
                    } else if (this.f5411h.isEmpty() && line.startsWith("build id:")) {
                        break;
                    }
                    i10++;
                } catch (Throwable th) {
                    th = th;
                    bufferedReader = bufferedReader2;
                    try {
                        com.apm.insight.c.a();
                        com.apm.insight.runtime.j.a(th, "NPTH_CATCH");
                        return;
                    } finally {
                        com.apm.insight.a.a((Closeable) bufferedReader);
                    }
                }
            }
            com.apm.insight.a.a((Closeable) bufferedReader2);
        } catch (Throwable th2) {
            th = th2;
        }
    }
}
