Use VSCode to debug cocos2d-x JSB programs

@zhangxm
Ok, here is the number of new issue: 16304.
Thanks for replying.

@Djong thanks

@zhangxm
I add this sentence
(JS_DefineFunction(_cx, rootedDebugObj, "require", ScriptingCore::executeScript, 2, JSPROP_ENUMERATE | JSPROP_PERMANENT);)
to the file of ScriptingCore.cpp in 3.10.
Then the console didnā€™t show the ReferenceError: require is not defined and the VSCode can debug.
But got some err in the ā€œScriptingCore.cpp::update(float dt)ā€,here is the screenshot:


Then i found at some point in time, the function static bool NS_ProcessNextEvent() also in dealing with the g_queue.
So, got error msg : ā€œthe g_queue is out of range.ā€
Here is the VSCode console screenshot:

I test set 3-4 breakpoints in one js file.Itā€™s no problem.
Then test one breakpoint in A file, the other one breakpoint in B file.The problem will be appeared.
Would you please have a look, Is there some places to improve?
Or not i will avoid to set too much breakpoints in diff files.

@Djong since the feature is added since v3.11, so i donā€™t test on v3.10 and lower version. May be i was wrong, can not just do the modification to work for v3.10 and lower version. I would suggest you using v3.11+.

@zhangxm
Nothing, itā€™s ok. Without too much breakpoints on entry, it seems running well.
I test on 3.11.1 and 3.12, the problem is also exist.
The method to avoid it is doesnā€™t set any breakpoints in VSCode before start debug.
About the 3.10,this is the place where i add the code:


OK,my problem is solved.Thank you for your help.

@Djong Could you show me the detail steps about how to reproduce the issue in v3.12? A test case is better. Thanks.

Ok.
1.Open the 3.12 js-test with vs2013, then run it.
2.Open the folder of js-test with VSCode, then set one breakpoint in ActionManagerTest.js and set one breakpoint in ActionsTest.js.
3.Start debug with VSCode.
4.With 3.12, the VS2013 canā€™t show error msg, but it will canā€™t touch and donā€™t respond any operation.I think it maybe get locked.
5.But with 3.11.1 or 3.10, can got error msg that i mentioned above.And can debugged where my screenshot show.
In my debugging, i found the function static bool NS_ProcessNextEvent() also handle with g_queue at the same time. It seems the lock() running is not good.
6.if donā€™t set any breakpoints before start debug with VSCode, there is nothing wrong. And during the debugging with VSCode add breakpoints also nothing wrong happened.
7.Itā€™s all info i got.

@zhangxm
Hello.
Iā€™m currently able to debug my cocos2d js project with Firefox JSB debugger - it works well (without taking into account its weak functionality and UX)
I tried VSCode - it looks very powerful and cute, but I failed to attach to my running project.
VSCode says

ar: attachRequest: address: localhost port: 5086 
ar: attachRequest: connected 
ar: _termiated: Not a valid project. 

What can be wrong? I did all required Environment setup.

Sorry, Iā€™ve read this thread again, and found the reason - I was opening wrong folder (src instead of root folder where main.js exists).
Now Iā€™ve got another problem. VSCode says

cocos request failed (reason: TypeError: Cannot read property 'length' of undefined)

right after debugger connected

PS
I tried on cocos 3.10 & 3.12
Iā€™m using Mac

When trying to attach the debugger, I am getting an error immediately after connecting the debugger.
I am using an example project from Cocos Creator. This is using the version of cocos2d-x that is included with Cocos Creator.

any help would be appreciated.

xcode output:

ar: attachRequest: address: 192.168.4.98 port: 5086
ar: attachRequest: connected
ar: _termiated: socket end
ar: _termiated: cocos firefox protocol close
ar: _termiated: socket error
ar: _termiated: cocos firefox protocol error: input stream error
ar: _termiated: cocos firefox protocol error: error happend in send request

Device callstack from Xcode

Thread 1Queue : com.apple.main-thread (serial) 
 #0	0x00000001008e29c4 in js::Debugger::wrapDebuggeeValue(JSContext*, JS::MutableHandle) ()
 #1	0x00000001008f1058 in DebuggerSource_getElement(JSContext*, unsigned int, JS::Value*) ()
 #2	0x000000010090b7f4 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #3	0x000000010090bc44 in js::Invoke(JSContext*, JS::Value const&, JS::Value const&, unsigned int, JS::Value const*, JS::MutableHandle) ()
 #4	0x000000010090c098 in js::InvokeGetterOrSetter(JSContext*, JSObject*, JS::Value, unsigned int, JS::Value*, JS::MutableHandle) ()
 #5	0x0000000100885040 in js::baseops::GetProperty(JSContext*, JS::Handle, JS::Handle, JS::Handle, JS::MutableHandle) ()
 #6	0x0000000100906234 in Interpret(JSContext*, js::RunState&) ()
 #7	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #8	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #9	0x00000001009076bc in Interpret(JSContext*, js::RunState&) ()
 #10	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #11	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #12	0x00000001009076bc in Interpret(JSContext*, js::RunState&) ()
 #13	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #14	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #15	0x00000001009076bc in Interpret(JSContext*, js::RunState&) ()
 #16	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #17	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #18	0x000000010085967c in js::CallOrConstructBoundFunction(JSContext*, unsigned int, JS::Value*) ()
 #19	0x000000010090b7f4 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #20	0x00000001009076bc in Interpret(JSContext*, js::RunState&) ()
 #21	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #22	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #23	0x000000010090bc44 in js::Invoke(JSContext*, JS::Value const&, JS::Value const&, unsigned int, JS::Value const*, JS::MutableHandle) ()
 #24	0x0000000100833a60 in JS_CallFunctionName(JSContext*, JS::Handle, char const*, JS::HandleValueArray const&, JS::MutableHandle) ()
 #25	0x000000010075ca8c in ScriptingCore::debugProcessInput(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&) at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/ScriptingCore.cpp:1699
 #26	0x000000010075d440 in NS_ProcessNextEvent() at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/ScriptingCore.cpp:1720
 #27	0x000000010075cb48 in JSBDebug_enterNestedEventLoop(JSContext*, unsigned int, JS::Value*) at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/ScriptingCore.cpp:1745
 #28	0x000000010090b7f4 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #29	0x00000001009076bc in Interpret(JSContext*, js::RunState&) ()
 #30	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #31	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #32	0x000000010085967c in js::CallOrConstructBoundFunction(JSContext*, unsigned int, JS::Value*) ()
 #33	0x000000010090b7f4 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #34	0x00000001009076bc in Interpret(JSContext*, js::RunState&) ()
 #35	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #36	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #37	0x000000010085967c in js::CallOrConstructBoundFunction(JSContext*, unsigned int, JS::Value*) ()
 #38	0x000000010090b7f4 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #39	0x00000001009076bc in Interpret(JSContext*, js::RunState&) ()
 #40	0x00000001009007f4 in js::RunScript(JSContext*, js::RunState&) ()
 #41	0x000000010090b900 in js::Invoke(JSContext*, JS::CallArgs, js::MaybeConstruct) ()
 #42	0x000000010090bc44 in js::Invoke(JSContext*, JS::Value const&, JS::Value const&, unsigned int, JS::Value const*, JS::MutableHandle) ()
 #43	0x0000000100833a60 in JS_CallFunctionName(JSContext*, JS::Handle, char const*, JS::HandleValueArray const&, JS::MutableHandle) ()
 #44	0x000000010075ca8c in ScriptingCore::debugProcessInput(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&) at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/ScriptingCore.cpp:1699
 #45	0x000000010075c850 in SimpleRunLoop::update(float) at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/ScriptingCore.cpp:1683
 #46	0x000000010076f440 in void cocos2d::Scheduler::scheduleUpdate(SimpleRunLoop*, int, bool)::'lambda'(float)::operator()(float) const at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/proj.ios_mac/../../../../cocos/base/CCScheduler.h:284
 #47	0x000000010076f410 in decltype(std::__1::forward(fp)(std::__1::forward(fp0))) std::__1::__invoke<void cocos2d::Scheduler::scheduleUpdate(SimpleRunLoop*, int, bool)::'lambda'(float)&, float>(SimpleRunLoop&&, float&&) [inlined] at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_base:416
 #48	0x000000010076f3f0 in void std::__1::__invoke_void_return_wrapper::__call<void cocos2d::Scheduler::scheduleUpdate(SimpleRunLoop*, int, bool)::'lambda'(float)&, float>(void cocos2d::Scheduler::scheduleUpdate(SimpleRunLoop*, int, bool)::'lambda'(float)&&&, float&&) at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_base:468
 #49	0x000000010076f21c in std::__1::__function::__func<void cocos2d::Scheduler::scheduleUpdate(SimpleRunLoop*, int, bool)::'lambda'(float), std::__1::allocator<void cocos2d::Scheduler::scheduleUpdate(SimpleRunLoop*, int, bool)::'lambda'(float)>, void (float)>::operator()(float&&) at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:1437
 #50	0x0000000100a9f0cc in std::__1::function::operator()(float) const at /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:1817
 #51	0x0000000100a9c570 in cocos2d::Scheduler::update(float) at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/base/CCScheduler.cpp:859
 #52	0x0000000100ae7560 in cocos2d::Director::drawScene() at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/base/CCDirector.cpp:272
 #53	0x0000000100aeb5c0 in cocos2d::DisplayLinkDirector::mainLoop() at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/base/CCDirector.cpp:1348
 #54	0x0000000100ea77a0 in -[CCDirectorCaller doCaller:] at /projects/examples/build/jsb-default/frameworks/cocos2d-x/cocos/platform/ios/CCDirectorCaller-ios.mm:138
 #55	0x0000000101e37768 in -[DYDisplayLinkInterposer forwardDisplayLinkCallback:] ()
 #56	0x000000018531c234 in CA::Display::DisplayLinkItem::dispatch() ()
 #57	0x000000018531c0e8 in CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) ()
 #58	0x0000000182a61e54 in IODispatchCalloutFromCFMessage ()
 #59	0x0000000182789030 in __CFMachPortPerform ()
 #60	0x00000001827a17d4 in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
 #61	0x00000001827a0f0c in __CFRunLoopDoSource1 ()
 #62	0x000000018279ec64 in __CFRunLoopRun ()
 #63	0x00000001826c8c50 in CFRunLoopRunSpecific ()
 #64	0x0000000183fb0088 in GSEventRunModal ()
 #65	0x00000001879aa088 in UIApplicationMain ()
 #66	0x000000010010675c in main at /projects/examples/build/jsb-default/frameworks/runtime-src/proj.ios_mac/ios/main.m:8
 #67	0x00000001822668b8 in start ()

@heximal could you please provide the JS source code?
@jeffrey_ankrum Could you please provide your project?

Hi @zhangxm
here is my cocos project template, I canā€™t attach the entire project, sorry.
but this code produces the same issue I mentioned (cocos request failed (reason: TypeError: Cannot read property ā€˜lengthā€™ of undefined))
Thanks for your help.

cocos_template.zip (450.0 KB)

I am using the examples project from a fresh install of Cocos Creator. I have my startup scene set to scheduler scene under scripting. The scene works fine until I attach the debugger.

@heximal i tested with your script, it worked ok. I can debug the JS codes. I used latest codes in v3.13 branch, but i think it is nothing about it. And i used Mac too.

@jeffrey_ankrum whatā€™s your cocos creator version?

@zhangxm
May it be the reason that Iā€™m using Cocos Studio 2?

cocos creator v1.2.0

@heximal i am not sure. But i think it is only related with engine codes. You can use without Cocos Studio to have a try.

@jeffrey_ankrum i will have a try.

@zhangxm
do you have any idea what may cause this issue? whom is it produced by? is it cocos2d engine or maybe it happens on VSCode plugin side. which way to dig into?)

@heximal i takes charge of this VSCode plugin. But i can not reproduce it by your example.

I think the reason I am having trouble is I am using the built in engine code for Cocos2dx, which Creator uses 3.9 lite.

Could this be the issue @zhangxm