Accessing V8 Stack Trace in Signal Handler

68 views
Skip to first unread message

Patric Lemo

unread,
Jun 19, 2023, 4:27:18 AM6/19/23
to v8-dev

Hello all: 

I am working on a project where I need to print the JS stack trace when a JS process becomes unresponsive. My approach involves setting a periodic alarm using the `alarm()` function, and in the timeout function, I attempt to print the JS stack trace.

I'm experiencing an issue where `v8::StackTrace::CurrentStackTrace` returns a frame count of 0 when invoked within the signal handler. I understand there could be some complications using the V8 API within a signal handler due to its asynchronous signal-unsafe nature.

Here is a simplified version of my current approach:

// Assume isolate and context are correctly setup
void signalHandler(int signal) {
    // Try to get stack trace and print
    v8::HandleScope handleScope(isolate);
    v8::Local<v8::StackTrace> stackTrace = v8::StackTrace::CurrentStackTrace(
        isolate,
        10,
        v8::StackTrace::kDetailed
    );
    int frameCount = stackTrace->GetFrameCount();
    printf("Frame Count: %d\n", frameCount);
    // Iterate and print each frame here...
}

int main() {
    // Setup V8, isolate and context here...
    signal(SIGALRM, signalHandler);
    alarm(10);  // Set alarm
    // Rest of the program...
}



Is my approach flawed? Is there a safer or more effective method to achieve what I'm trying to do?

Thanks

Andreas Haas

unread,
Jun 19, 2023, 6:10:52 AM6/19/23
to v8-...@googlegroups.com
Hi Partric,

I am not an expert with stack traces, so I don't know if there is a better method, but I think your approach is flawed. v8::StackTrace::CurrentStackTrace() can only work when code execution made a valid transition from JavaScript to C++, in other words, when there is an Exit frame on the stack. With a signal you transition from JavaScript code directly to the signal handling code, without an Exit frame. However, without the Exit frame V8 cannot find the beginning of the JavaScript stack, and can therefore not generate a stack trace.

You could try to request an interrupt with https://source.chromium.org/chromium/chromium/src/+/refs/heads/main:v8/include/v8-isolate.h;l=1115;drc=8d3b19d9432fa06963a1efbd8b85d5d310c3a05a, and then create the stack trace from within the callback. Maybe that works better.

Cheers, Andreas

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/0e06f5e3-b37b-4bcc-a293-65d152b0938fn%40googlegroups.com.


--

Andreas Haas

Software Engineer

ah...@google.com


Google Germany GmbH

Erika-Mann-Straße 33

80636 München


Geschäftsführer: Paul Manicle, Liana Sebastian

Registergericht und -nummer: Hamburg, HRB 86891

Sitz der Gesellschaft: Hamburg


Diese E-Mail ist vertraulich. Falls sie diese fälschlicherweise erhalten haben sollten, leiten Sie diese bitte nicht an jemand anderes weiter, löschen Sie alle Kopien und Anhänge davon und lassen Sie mich bitte wissen, dass die E-Mail an die falsche Person gesendet wurde.

    

This e-mail is confidential. If you received this communication by mistake, please don't forward it to anyone else, please erase all copies and attachments, and please let me know that it has gone to the wrong person.


Simon Zünd

unread,
Jun 19, 2023, 6:57:26 AM6/19/23
to v8-...@googlegroups.com
Hi Patric,

one more thing to note is that signal handlers might have a separate stack, depending on whether one was set up with "sigaltstack".

As Andreas mentioned, your best bet is to request an interrupt from the signal handler (or a separate thread) and capture the stack trace in the callback.

Patric Lemo

unread,
Jun 19, 2023, 9:43:04 PM6/19/23
to v8-dev
Hey Andreas, Simon,

Just wanted to drop a quick note to say thanks. Your advice hit the mark and the interrupt request method worked like a charm. Really appreciate it!

Take care,
Patric Lemo
Reply all
Reply to author
Forward
0 new messages