Git Product home page Git Product logo

jcrash's Introduction

JCrash

Android自定义异常处理模块(Based on KSCrash 2017-11-14)  

JCrash

Handles errors that can only be caught at the mach level, such as stack overflow.
Tracks the REAL cause of an uncaught C++ exception.
Handles a crash in the crash handler itself (or in the user crash handler callback).
Extracts information about objects referenced by an exception (such as "unrecognized selector sent to instance 0xa26d9a0")
Its pluggable server reporting architecture makes it easy to adapt to any API service.
Dumps the stack contents.
Diagnoses crash causes (Crash Doctor).
Supports including extra data that the programmer supplies (before and during a crash).

JCrash handles the following kinds of crashes:

Fatal signals
C++ exceptions
Java exceptions
Main thread deadlock (experimental)
Custom crashes (e.g. from Java languages)

With zombie tracking enabled, JCrash will also detect a lost NSException and print its contents. Certain kinds of memory corruption or stack corruption crashes can cause the exception to deallocate early, further twarting efforts to debug your app, so this feature can be quite handy at times.

Trade off: Zombie tracking at the cost of adding very slight overhead to object deallocation, and having some memory reserved.

Deadlock Detection (KSCrashMonitorTypeMainThreadDeadlock in KSCrashMonitorType.h)

WARNING WARNING WARNING WARNING WARNING WARNING WARNING

This feature is UNSTABLE! It can false-positive and crash your app!

If your main thread deadlocks, your user interface will become unresponsive, and the user will have to manually shut down the app (for which there will be no crash report). With deadlock detection enabled, a watchdog timer is set up. If anything holds the main thread for longer than the watchdog timer duration, JCrash will shut down the app and give you a stack trace showing what the main thread was doing at the time.

This is wonderful, but you must be careful:

App initialization generally occurs on the main thread. If your initialization code takes longer than the watchdog timer, your app will be forcibly shut down during start up! If you enable this feature, you MUST ensure that NONE of your normally running code holds the main thread for longer than the watchdog value! At the same time,you'll want to set the timer to a low enough value that the user doesn't become impatient and shut down the app manually before the watchdog triggers!

Trade off: Deadlock detection, but you must be a lot more careful about what runs on the main thread!

Memory Introspection (introspectMemory in KSCrash.h)

When an app crashes, there are usually objects and strings in memory that are being referenced by the stack, registers, or even exception messages. When enabled, JCrash will introspect these memory regions and store their contents in the crash report.

You can also specify a list of classes that should not be introspected by setting the doNotIntrospectClasses property in JCrash.

Custom crash handling code (onCrash in KSCrash.h)

If you want to do some extra processing after a crash occurs (perhaps to add more contextual data to the report), you can do so.

However, you must ensure that you only use async-safe code! There are many cases where you can get away with doing so anyway, but there are certain classes of crashes where handler code that disregards this warning will cause the crash handler to crash! Note that if this happens, JCrash will detect it and write a full report anyway, though your custom handler code may not fully run.

Trade off: Custom crash handling code, but you must be careful what you put in it!

JCrash log redirection

This takes whatever JCrash would have printed to the console, and writes it to a file instead. I mostly use this for debugging JCrash itself, but it could be useful for other purposes, so I've exposed an API for it.

方法调用:

   //本地日志处理
   KSCrashInstallationLocal installation = KSCrashInstallationLocal.INSTANCE;
    //KSCrashInstallation installation = new KSCrashInstallationEmail(this, "[email protected]");
    installation.install(this);
    //获得异常的详情和详细信息  
    installation.setIDealWithCrash(new IDealWithCrash(){  //属于耗时操作
            @Override
            public void dealWithJavaCrash(Throwable summary, String detail) {
                Log.e(TAG, "dealWithCrash summary----------" + summary.toString());
                Log.e(TAG, "dealWithCrash detail----------" + detail);
            }
    });  
    
    
    
    //当前版本的异常信息会被记录到本地数据库中。    
    int taskId = 1;  
    int appId = 1000;  
    String taskVersion = "1.0.0";  
    String channel = "Lily";    
    try {  //在处理native异常时可能会跑IOException  
      //日志本地处理,存储到DB中
      KSCrashInstallationLocal.INSTANCE.install(MainActivity.this, taskId, appId, taskVersion, channel);
    } catch (IOException e) {    
        e.printStackTrace();    
    } catch(SQLException e){    
        e.printStackTrace();    
    }   

jcrash's People

Contributors

xuningjack avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.