8

I'm currently having trouble debugging some Android code which relies on a native library. One native call in particular seems prone to this "spin on suspend" error. It generally manifests like so:

threadid=2: spin on suspend #2 threadid=48 (pcf=3)

Thus far I haven't been able to determine exactly what's failing here, except that after about 10 of these messages, my application encounters a SIGSTKFLT and exits. Every time, the first thread is the GC, and the second thread is whatever thread is currently executing the native code. The portion of the stack printed along with this message always has a native method at the top of the stack.

What exactly is happening when Dalvik complains about this, and how can I begin to debug the cause so I can fix it?

EDIT: An interesting wrinkle -- after the native developer made some changes, I now also see the following error sometimes:

PopFrame missed the break
VM aborting
Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1)

It's also extremely odd to me that the thread dump shows my native method at the top of the stack, yet the thread state is RUNNABLE, not NATIVE -- how can that be possible?

1 Answer 1

10

The basic problem is that Dalvik is a safe-point suspend VM, and uses "stop the world" garbage collection. This means that, for the GC to operate, it has to wait for all threads to reach a point where it can be sure that they won't be altering the heap.

For some reason, one of your threads isn't responding to the GC thread's request to suspend. It's not actually executing in native code; if it were, the thread would be in NATIVE state, which is considered safe. (All access to the native heap is gated through JNI calls, and all JNI calls do a suspend check.)

For performance reasons, the JIT is capable of chaining blocks of compiled code together in a way that skips the suspend checks. If a thread takes too long to suspend, the suspending thread will "unchain" the blocks, and wait a little longer. Eventually it starts complaining, and eventually-eventually it gives up and aborts the VM.

Some devices use a vendor-modified version of Dalvik that gets this wrong, and aborts can happen on tight loops. I wouldn't expect to see a native method at the top of the stack in this case.

Your best bet for debugging is to attach gdb at the point it goes unhappy and try to figure out what the target thread is doing. It's possible that the native code trashed the VM state or return stack in some way, and so on its return from native code the thread gets jammed up.

Update after EDIT: The dvmPopFrame() function is used to pop a stack frame off the managed stack. When the VM calls your native method it inserts a "break" frame, so that when the stack is unrolled for exception handling the VM doesn't blow past the call site. (It's also used for managed-code method calls issued by the VM, e.g. for reflection or <clinit>.) The message PopFrame missed the break means that the break frame wasn't found.

Break frames have a null method pointer. When unrolling the stack, dvmPopFrame() continues as long as it sees a non-null method pointer (meaning it's not a break frame) and a non-null previous-frame pointer (meaning you haven't hit the top of the stack). If you hit the top of the stack, you've missed the break -- all Dalvik stacks start with a real method (sometimes a "fake" method if the thread was attached to the VM with JNI).

So my guess would be that the native code trashed the stack, nulling the previous-frame pointer. One technique for sorting this out would be to have the VM call a native method that calls the actual native method; the "middle man" allocates some stuff on the stack, sets it to known values, calls the actual method, then verifies that its stack allocations are unchanged before returning.

(It may be necessary to use the values to prevent the compiler from optimizing them away; if you use something like:

if (jniEnv == NULL) {
    printf("my stuff is ...", ...);
}

then it'll never actually run, since the JNIEnv* is never null... but the compiler doesn't know that.)

For a full description of the Dalvik stack layout, see dalvik/vm/interp/Stack.h.

It's normal for the thread to be in RUNNABLE when returning from native code. Your native method is still at the top because the code that pops it off failed and aborted the VM.

5
  • So, if my native code is trashing the VM state, why doesn't it fail consistently? Sometimes the call will return just fine (presumably because a GC didn't happen to occur at that point). Shouldn't I get a segfault or similar?
    – Thorn G
    Dec 27, 2013 at 19:21
  • Anything is possible. The first step is to figure out what the "stuck" thread is actually doing. You'll see a "sysTid" value in the thread dump -- that's the Linux thread ID, which you'll also see in gdb.
    – fadden
    Dec 27, 2013 at 21:56
  • I think I have a grip on how the stack got trashed -- I noticed that in the JNI code, the developer stashed the JNIEnv* pointer in a static field, and then reused it on another native method invocation (which happened on a different thread). I only discovered this because I had a thread "resurrected" when the second JNI method called back into Java.
    – Thorn G
    Dec 31, 2013 at 18:33
  • If you enable the CheckJNI feature, you will be alerted vigorously when this happens. See developer.android.com/training/articles/… .
    – fadden
    Dec 31, 2013 at 22:22
  • 2
    I just voted this up. I don't think I've ever seen such a comprehensive answer about a topic I should know about and I obviously don't. fadden has some pretty cool experience!
    – user289463
    Jul 18, 2014 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.