Send report after Android JNI signal/exception - android

I am trying to get firebase working with my Android app but it is mainly C++ code.
A lot of chances are that if there is any crash it will be some kind of bad access in the C++ part.
Firebase works well with uncaught java exceptions however I cannot get it to work with JNI signals/exceptions.
As far as I know it is not yet compatible with JNI but I thought a workaround would be something similar to this:
Somewehre in the C++ add a signal handler for signals we would like to handle that will send it back to the Java side and try to send a report ( with part of the stacktrace if possible ).
#include <cisgnal>
namespace
{
void SignalHandler( int sig )
{
// Code to call a static method in my Activity
}
}
CrashReporter::CrashReporter()
{
::signal( SIGABRT, & ::SignalHandler )
}
// In java
public static void SendReportOnCrash()
{
FirebaseCrash.report( new Exception( "OOPS" ) );
}
Unfortunately, fake reports are never sent, however I do get callback in Java.
I tried to launch a process separated activity in which I would call FirebaseCrash.report() but there is no non-static way to it therefore it always crash since FirebaseApp/Crash are not instantiated in secondary activity.
I come here to ask if someone would have a hint on how to do that.
My last try but least wanted test would be to write the stack trace to a file, and upon a new start, test if this file exists, if so use FirebaseCrash.log then send a fake report...

You are not guaranteed to be able to do any Java processing after the JVM calls abort() for a fatal error. Per the Java documentation:
SIGABRT
The HotSpot VM does not handle this signal. Instead it calls the abort
function after fatal error handling. If an application uses this
signal then it should terminate the process to preserve the expected
semantics.
Yes, that is for the Oracle implementation. It quite likely applies to all other implementations also.
Because at the point it calls abort(), the JVM expects to be killed.

Related

Report all Android crashes to own API

I am in a situation where I cannot use third party Crash reporting platform (Crashalytics ...etc)
So, I started to use self developed Rest API to receive crash reports from mobile app.
However, I can only send handled crashes so far by using try-catch
How can I handle all crashes thrown by my mobile app even those which are not in my try-catch clauses
Just like Crashlytics which can catch all crashes and sends them to Firebase without placing try-catch
Have a look at Thread.UncaughtExceptionHandler
from the docs :
When a thread is about to terminate due to an uncaught exception the
Java Virtual Machine will query the thread for its
UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler()
and will invoke the handler's uncaughtException method, passing the
thread and the exception as arguments. If a thread has not had its
UncaughtExceptionHandler explicitly set, then its ThreadGroup object
acts as its UncaughtExceptionHandler. If the ThreadGroup object has no
special requirements for dealing with the exception, it can forward
the invocation to the default uncaught exception handler.
using this, you can get all threads and all exceptions, which you can then report to any API you'd like
you can find examples of how it was implemented
here and here
There is a library which you can use called ACRA , its an open source library you can check its code , or you can directly use their library and configure it to hit your api
#AcraHttpSender(uri = "http://yourserver.com/yourscript",
basicAuthLogin = "yourlogin", // optional
basicAuthPassword = "y0uRpa$$w0rd", // optional
httpMethod = HttpSender.Method.POST)
public class MyApplication extends Application {

Stacktrace for libc C function call on Android/Linux

Usually stacktraces (backtraces) are generated from point where special stacktrace generation function is called (like _Unwind_Backtrace here: Android NDK: getting the backtrace). Is it possible to generate stacktrace which also includes libc functions? For example, I would like to inspect stacktrace for "open" libc call when it's called from my program (without modifying libc library). I know that in Java it's possible to invoke some method which throws exception and get stacktrace in exception handler from point where exception was thrown. I would like to do something similar in C but I don't think it's possible as there even are no exceptions in C.
For example, I would like to inspect stacktrace for "open" libc call when it's called from my program (without modifying libc library).
There are several use cases to consider:
Direct calls from your program to open
Indirect calls (e.g. you call getgroups, which opens /etc/nsswitch.conf)
Calls to open that are not triggered by your program at all (e.g. dynamic loader opening libc.so
Your question suggests that you are only interested in case 1, which is trivial to solve: compile the code from which you want to intercept open with -Dopen=open_with_stacktrace, and link with a separate file:
// Compile without -Dopen=...
#include <stdlib.>
ssize_t open_with_stacktrace(const char *fname, int flags, int mode)
{
// Do whatever to record the stack trace.
return open(fname, flags, mode);
}
For case 2, you may use a function interposer via LD_PRELOAD.
For case 3, you'll have to use external tracer, such as GDB.
What I actually want is to implement security feature ...
You should have started with that. In general, you can't implement any meaningful security in the program itself, you have to use an external monitor.
You are trying to detect whether someone interposed open. But that someone could just as easily interpose your "checker" function, and make it always return "everything is fine" result.
You should consider using one of the sandboxing techniques instead.

IdHTTP in Android service in Delphi RX

I try to write an Android service, which will use IdHTTP component to send messages to the server.
I just put IdHTTP1 on TAndroidServiceDM and try to Run app on Phone (Android 4.2.2)
I get error:
"Project LocSensDemo.apk raised exception class Exception with message 'Activity not found, maybe you are in a service'"
in FMX.Platform.Android line 1792 "Activity := TAndroidHelper.Activity;"
and then
"Project LocSensDemo.apk raised exception class Segmentation fault (11)".
Service crashes.
How to solve this problem?
The Activity not found exception occurs when the TAndroidHelper.Activity property is accessed inside a service project. That property is not usable in a service (see RSP-13381). Anything that uses TAndroidHelper.Activity in a service will crash at runtime. Line 1792 in the FMX.Platform.Android unit is inside of FMX.Platform.Android.TWindowManager.RetrieveContentRect(). What are you accessing in a service that is using TWindowManager? Services don't have UIs, you should not be touching anything UI-related to begin with.
If your service code needs access to an Android Context object, such as to call getContentResolver(), you can use the TAndroidHelper.Context property, or your service's own JavaService property (Android's Service class derives from Context).
As for the Segmentation fault exception, you haven't provided any details about that error, or shown what code is causing it, so there is no way for anyone here to diagnose it for you.

AttachCurrentThread crash in multi threads JNI after moving to Android L release

I have implemented a service which is invoked by system server during the boot-up.
My service has JNI implementation which creates another thread.
The reason for having another thread is to have the capability of canceling operation while monitoring the call back.
This mechanism worked well in Android Kitkat release but it crashes in Android L.
Between two threads, I stored the JavaVM* that I get from GetJavaVM(env) to a static global variable. Of course, this shared data is protected by pthread mutex.
I have tried the following so far, but all of them below still crashed :
1) Used JavaVM* I get from JNI onLoad() function by store it to global
2) In the new thread, as there is only one JavaVM running on Android, get the vm from calling android::AndroidRuntime::getJavaVM();
3) Stored the vm information in the main thread after calling NewGlobalRef(). And saved that reference to the shared data. The new thread used the reference from NewGlobalRef().
Does anyone know what is significantly changed in JNI environment on Android L release?
UPDATE :
Debugged further and the solution I mentioned 1) or 2) should have worked.
The actual issue was due to the garbage collection running more frequently. So the HAL pointer I kept was not valid any more...
These links were helpful!!!
https://developer.android.com/guide/practices/verifying-apps-art.html
http://developer.android.com/training/articles/perf-jni.html
Thanks for all the comments!
What has changed with L release is the move to ART that is less flexible than Dalvik regarding errors.
It's perfectly fine to share JavaVM* across threads, you should keep it this way.
However, what are you doing later with this JavaVM* ?
JNIEnv* has to be retrieved and used from the same thread and must not be used across threads. To use JNIEnv*, a thread must have been attached to the VM (using AttachCurrentThread).
Threads also have to be detached using DetachCurrentThread before they exit.

Xamarin android - Integration with crittercism for unhandled exceptions not working

I've created a bindings project, hooked everything up in my onCreate etc. Everything works except for unhandled exceptions. Let me elaborate on "Everything works" - I can see via crittercism's live stats page that there is indeed an app load, I can also send up "ManagedExceptions" using the "LogHandledException" interface.
I have implemented the ICritterCallback interface and the "CrashedOnLastLoad" boolean is always false. This is wierd cos I can see the app crashes.
I have used these 3 ways to try and get my logs sent to Crittercism. (All 3 crash the app)
Java exception
throw new Java.Lang.IllegalArgumentException("This is a test for critter");
Background exception
.Click += delegate { ThreadPool.QueueUserWorkItem(o => { throw new Exception("Crashed Background thread."); } ); };
Simple .net exception
throw new Exception("Crashed UI thread.");
None of the above are registering as crashes on the next load... weird right?
Maybe the .net runtime is swallowing all the unhandled exceptions then calling exit gracefully on dalvic's runtime... is this possible..?
As a hack for now im implementing the exception handlers for android as per this blog post then calling Crittercism.LogHandledException(Throwable.FromException(e.Exception)); from inside both the events.
It works, but im using Crittercism's handled exceptions for unhandled exceptions.. So when I want to send up real "HandledExceptions" they will be lost in the mess.
So is there any way to send an unhandled exception to Crittercism??
Or is there a way to simulate a crash on android from .net that will send it to Crittercism??
Any help would be much appreciated!
Cheers,
Sam
Co-founder of Crittercism here. We just released an official plugin for Xamarin which should automatically log javascript exceptions (and any other crashes) as unhandled exceptions so you won't run into this problem anymore. You can download the latest version from the Xamarin asset store here:
http://components.xamarin.com/view/crittercism

Categories

Resources