Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug javascript in embedded V8?

I'm trying to learn how to use the Debug object in V8 to debug javascript in an embedded-javascript c++ application.

I've called v8::Debug::SetDebugEventListener and set a callback. I then call v8::Debug::GetDebugContext to get a debug context, and then run something like: Debug.scripts()

If I print the results of that call from C++, I get:

 [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]

What I actually want to do is set a breakpoint for a given line number in a script, but can't get even the most basic things working.

I get callbacks to my debug event listener, but the callback input parameter (non-user-provided) data doesn't make any sense to me, either.

{script_: {context_: {data_: undefined, handle_: 0, type_: "context"}, handle_: 1, script_: {}, type_: "script"}, type_: 4}
{break_id: 2, selected_frame: 0}

Thank you.

like image 890
xaxxon Avatar asked Aug 27 '26 10:08

xaxxon


1 Answers

(Note: this may or may not be the right way to do things, but it's working for me in some simple cases)

v8::DebugEvent debug_event_type = event_details.GetEvent();

then switch on the results:

if (debug_event_type == v8::DebugEvent::Break) {

Here's the passed in data for a pretty trivial breakpoint being hit:

 /* GetEventData() when a breakpoint is hit returns:
     * {
     *      break_points_hit_: [{active_: true, actual_location: {column: 4, line: 13, script_id: 55}, condition_: null,
     *      script_break_point_: {
     *          active_: true,
     *          break_points_: [],
     *          column_: undefined,
     *          condition_: undefined,
     *          groupId_: undefined,
     *          line_: 13,
     *          number_: 1, <== breakpoint number - v8-assigned
     *          position_alignment_: 0,
     *          script_id_: 55, <== script id passed in from v8::ScriptOrigin when compiled
     *          type_: 0
 *          },
 *          source_position_: 175}], frame_: {break_id_: 8, details_: {break_id_: 8, details_: [392424216, {}, function a(){
    println("Beginning of a()");
    let some_var = 5;
    some_var += 5;
    b(some_var);
    println("End of a()");
}, {sourceColumnStart_: [undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 4, undefined, undefined, undefined, undefined, undefined, undefined, undefined]}, 0, 1, 175, false, false, 0, "some_var", 5]}, index_: 0, type_: "frame"}}

shameless self promotion: You can see the progress I've made towards debugging v8 in my v8 integration simplification library v8toolkit here: https://github.com/xaxxon/v8toolkit/blob/master/src/debugger.cpp

like image 79
xaxxon Avatar answered Aug 29 '26 23:08

xaxxon