cocos v8引擎调试js

creator打包的windows程序运行后直接js报错了,使用creator编辑器的模拟器是正常的,打算调试一下。

都是使用的v8执行的js,electron有个调试选项inspect-brk,可以让程序在js的第一行断下,理论上creator打包的windows程序也可以。

  • build\jsb-default\frameworks\cocos2d-x\cocos\scripting\js-bindings\jswrapper\v8\ScriptEngine.cpp

设置调试参数_isWaitForConnect

c++ 复制代码
bool AppDelegate::applicationDidFinishLaunching()
{
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
    //开启调试,使用的是localhost:6086,   ↓ 这个参数用来控制是否断下,当为true的时候,程序会挂起,直到调试器附加到应用程序
    jsb_enable_debugger("0.0.0.0", 6086, true);
#endif
}

bool jsb_enable_debugger(const std::string& debuggerServerAddr, uint32_t port, bool isWaitForConnect)
{
    auto se = se::ScriptEngine::getInstance();
    se->enableDebugger(debuggerServerAddr.c_str(), port, isWaitForConnect);
}
void ScriptEngine::enableDebugger(const std::string& serverAddr, uint32_t port, bool isWait)
{
    _debuggerServerAddr = serverAddr;
    _debuggerServerPort = port;
    _isWaitForConnect = isWait; // 这个参数
}
    

使用之前的调试配置

c++ 复制代码
bool ScriptEngine::start()
{
    if (isDebuggerEnabled())
    {
#if SE_ENABLE_INSPECTOR
        // V8 inspector stuff, most code are taken from NodeJS.
        _isolateData = node::CreateIsolateData(_isolate, uv_default_loop());
        _env = node::CreateEnvironment(_isolateData, _context.Get(_isolate), 0, nullptr, 0, nullptr);

        node::DebugOptions options;
        // 调试配置,其实就是在设置 break_first_line_ 
        options.set_wait_for_connect(_isWaitForConnect);// the program will be hung up until debug attach if _isWaitForConnect = true
        options.set_inspector_enabled(true);
        options.set_port((int)_debuggerServerPort);
        options.set_host_name(_debuggerServerAddr.c_str());
        bool ok = _env->inspector_agent()->Start(_platform, "", options);
        assert(ok);
#endif
}

打开chrome://inspect/#devices , 配置6086端口,稍等一会就会看到inpsect

调试器启动后,并没有断在第一行,直接运行起来了。

c++ 复制代码
void set_wait_for_connect(bool wait) { break_first_line_ = wait; }
bool wait_for_connect() const { return break_first_line_; }
DebugOptions::DebugOptions() :
                               inspector_enabled_(false),
                               deprecated_debug_(false),
                               break_first_line_(false),
                               host_name_("127.0.0.1"), port_(-1) { }
bool DebugOptions::ParseOption(const char* argv0, const std::string& option) {
  bool has_argument = false;
  std::string option_name;
  std::string argument;

  auto pos = option.find("=");
  if (pos == std::string::npos) {
    option_name = option;
  } else {
    option_name = option.substr(0, pos);
    argument = option.substr(pos + 1);

    if (argument.length() > 0)
      has_argument = true;
    else
      argument.clear();
  }

  // Note that --debug-port and --debug-brk in conjunction with --inspect
  // work but are undocumented.
  // --debug is no longer valid.
  // Ref: https://github.com/nodejs/node/issues/12630
  // Ref: https://github.com/nodejs/node/pull/12949
  if (option_name == "--inspect") {
    inspector_enabled_ = true;
  } else if (option_name == "--debug") {
    deprecated_debug_ = true;
  } else if (option_name == "--inspect-brk") {
    inspector_enabled_ = true;
    break_first_line_ = true;
  } else if (option_name == "--debug-brk") {
    break_first_line_ = true;
    deprecated_debug_ = true;
  } else if (option_name == "--debug-port" ||
             option_name == "--inspect-port") {
    if (!has_argument) {
      SE_LOGE("%s: %s requires an argument\n",
              argv0, option.c_str());
      exit(9);
    }
  } else {
    return false;
  }

#if !HAVE_INSPECTOR
  if (inspector_enabled_) {
    SE_LOGE("Inspector support is not available with this Node.js build\n");
  }
  inspector_enabled_ = false;
  return false;
#endif

  // argument can be specified for *any* option to specify host:port
  if (has_argument) {
    std::pair<std::string, int> host_port = split_host_port(argument);
    if (!host_port.first.empty()) {
      host_name_ = host_port.first;
    }
    if (host_port.second >= 0) {
      port_ = host_port.second;
    }
  }

  return true;
}

v8 demo

c++ 复制代码
bool ScriptEngine::init(){
    v8::Isolate::CreateParams create_params;
    create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
    _isolate = v8::Isolate::New(create_params);
    v8::HandleScope hs(_isolate);
    _isolate->Enter();
}

问题

可能无法找到,和chrome版本有关系

console.log(process.versions.v8);

creator的native在启动的时候会打印v8版本

D/jswrapper (485): Initializing V8, version: 8.0.426.16

npm install -g v8-inspector

yarn global add v8-inspector

v8-inspector --debug-port=7878 --debug-host=192.168.1.29

调试细节

编辑器可以设置等待调试,很明显只有debug版本才有这个功能

这里使用的是不一定debug版本,因为你可以配置release,cocos2d_debug是1同样能达到相同的效果

如何找到config.json

来源

生成事件里面

bash 复制代码
if exist "$(ProjectDir)..\..\..\..\..\simulator\win32\config.json" 
copy 
    "$(ProjectDir)..\..\..\..\..\simulator\win32\config.json" 
    "$(OutDir)config.json" 

从这里获取到最原始的配置,目标目录就是exe的工作目录,所以能够找到

creator模拟器使用的是Creator\2.4.10\resources\cocos2d-x\simulator\win32

当我把编译好的模拟器放到这个目录后,发现无法调试了,

通过生成事件将编译的exe、dll拷贝过去发现可以了,不知道为啥

相关推荐
木亦Sam12 分钟前
响应式网页设计中媒体查询的进阶运用
前端·响应式设计
diemeng111916 分钟前
2024系统编程语言风云变幻:Rust持续领跑,Zig与Ada异军突起
开发语言·前端·后端·rust
烂蜻蜓18 分钟前
Uniapp 中布局魔法:display 属性
前端·javascript·css·vue.js·uni-app·html
java1234_小锋1 小时前
一周学会Flask3 Python Web开发-redirect重定向
前端·python·flask·flask3
琑951 小时前
nextjs项目搭建——头部导航
开发语言·前端·javascript
light多学一点1 小时前
视频的分片上传
前端
Gazer_S2 小时前
【Windows系统node_modules删除失败(EPERM)问题解析与应对方案】
前端·javascript·windows
bigyoung2 小时前
基于 React 的列表实现方案,包含创建和编辑状态,使用 Modal 弹框和表单的最佳实践
前端
乌木前端2 小时前
包管理工具lock文件的作用
前端·javascript
司玄2 小时前
3dtiles平移旋转原理及可视化工具实现
前端