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

On Mon, Jun 19, 2023 at 10:27 AM Patric Lemo <jiangzhan1...@gmail.com>
wrote:

>
> 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
>
> --
> --
> v8-dev mailing list
> v8-dev@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+unsubscr...@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
> <https://groups.google.com/d/msgid/v8-dev/0e06f5e3-b37b-4bcc-a293-65d152b0938fn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>


-- 

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.

-- 
-- 
v8-dev mailing list
v8-dev@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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/CAELSTveAc5EhxCBbKvixwHmTy1G9tfwnKPDRW%3D45S%3DPA8qPGYA%40mail.gmail.com.

Reply via email to