如何在嵌入式V8中调试javascript?

时间:2016-10-01 00:51:24

标签: javascript v8 embedded-v8

我试图学习如何在V8中使用Debug对象来调试嵌入式javascript c ++应用程序中的javascript。

我已拨打v8::Debug::SetDebugEventListener并设置回拨。然后我调用v8::Debug::GetDebugContext来获取调试上下文,然后运行类似:Debug.scripts()

的内容

如果我从C ++打印该调用的结果,我得到:

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

我真正想要做的是在脚本中为给定的行号设置一个断点,但是即使是最基本的东西也无法工作。

我得到了调试事件监听器的回调,但回调输入参数(非用户提供的)数据也没有任何意义。

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

谢谢。

1 个答案:

答案 0 :(得分:1)

(注意:这可能是也可能不是正确的做事方式,但在一些简单的情况下它对我有用)

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

然后打开结果:

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

这是一个非常简单的断点被传入的传入数据:

 /* 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:您可以在我的v8集成简化库v8toolkit中看到我为调试v8所取得的进展:https://github.com/xaxxon/v8toolkit/blob/master/src/debugger.cpp