pkulchenko/ZeroBraneStudio

Fail to display stack and variable in watch

vinceee opened this issue · 9 comments

Hi, I'using debugger server to debug lua in my Unity game(x64 on windows). Break points work perfectly. But step into function can only work in opened files. Also the stack and some table type variables can't be displayed. I get the same error as "Error:[string "D:/Tools/ZeroBraneStudio/lualibs/mobdebug/mobdebug.lua"]:185: field or property __serialize does not exist".
error1
Some variables can be correctly viewed while some are not.
error1

I think I have solved this by problem by changes in the pic
error1
Don't know the exact reason, maybe the __serialize visit in metatable in my lua environment will trigger an exception? I'm a newbie in lua. Hope this issue can help other Unity user.

But even without this __serialize error, I still can't step into an unopened file. Is it possible to fix the step into issue?

I still can't step into an unopened file. Is it possible to fix the step into issue?

Yes, you can configure the IDE to do this; see this FAQ answer.

I'm not sure why a check for __serialize field would trigger an exception, but I think there is a better fix. Can you try running the following code in the Remote console when InShopItemPanel is defined: getmetatable(InShopItemPanel) and getmetatable(InShopItemPanel).__serialize?

Thank you for your reply. InShopItemPanel is an Object defined in C# and passed to Lua as userdata
I try to register the __serialize function in c# as
L.RegFunction("__serialize", ToLua.op_ToString);
then the watch works fine.
InShopItemPanel="ShopItem103010 (UnityEngine.GameObject)"
maybe there're some missing handles in the modified luajit level.
I also check ZeroBraneStudio\cfg\user-sample.lua and the value "editor.autoactivate" has been set to true. I can step into correctly in pure Lua project, but still can't step into in my unity project. But the step-into-missing issue is acceptable by opening all the related Lua files in advance :).

@vinceee, interesting; thank you for the details!

I also check ZeroBraneStudio\cfg\user-sample.lua and the value "editor.autoactivate" has been set to true. I can step into correctly in pure Lua project, but still can't step into in my unity project. But the step-into-missing issue is acceptable by opening all the related Lua files in advance :).

editor.autoactivate should still work, unless there is an issue with mapping the paths returned by Unity to the paths in the IDE. Can you print the value of debug.getinfo(1,"S").source from the file that doesn't get activated and show the content of the Output window when that happens? I'm mostly interested in Mapped remote request for ... and Debugger session started in ... messages (if present).

Can you remove the registration for __serialize function you added and try the following patch for the debugger to see if this fixes the issue?

diff --git a/lualibs/mobdebug/mobdebug.lua b/lualibs/mobdebug/mobdebug.lua
index de1f024..e374469 100644
--- a/lualibs/mobdebug/mobdebug.lua
+++ b/lualibs/mobdebug/mobdebug.lua
@@ -181,11 +181,15 @@ local function s(t, opts)
       sref[#sref+1] = spath..space..'='..space..seen[t]
       return tag..'nil'..comment('ref', level) end
     -- protect from those cases where __tostring may fail
-    if type(mt) == 'table' and pcall(function() return mt.__tostring and mt.__tostring(t) end)
-    and (mt.__serialize or mt.__tostring) then -- knows how to serialize itself
-      seen[t] = insref or spath
-      if mt.__serialize then t = mt.__serialize(t) else t = tostring(t) end
-      ttype = type(t) end -- new value falls through to be serialized
+    if type(mt) == 'table' then
+      local to, tr = pcall(function() return mt.__tostring(t) end)
+      local so, sr = pcall(function() return mt.__serialize(t) end)
+      if (to or so) then -- knows how to serialize itself
+        seen[t] = insref or spath
+        if so then t = sr else t = tostring(t) end
+        ttype = type(t)
+      end -- new value falls through to be serialized
+    end
     if ttype == "table" then
       if level >= maxl then return tag..'{}'..comment('max', level) end
       seen[t] = insref or spath

I integrated the code into mobdebug.lua, then the stack and watch in editor work normally, GREAT JOB!

I check the Output window and find some related info about the step into issue.
When my Unity called require("mobdebug").start(DebugServerIp) (line:17 in tolua.lua,without any file opened in Zerobrane)
Zerobrane instantly output:

"
Debugging suspended at 'c:/workspace/assets/luaframework/tolua/lua/tolua.lua:18' (couldn't activate the file).
Debugging session started in 'C:\Workspace\Assets\LuaFramework'.
"

When I manually opened the tolua.lua in Zerobrane and started Unity again, no "couldn't activate the file" output and the green cursor was set to the line 18 in tolua.lua correctly. Then I pressed f5, my Unity continued to work.

Then I added "print(debug.getinfo(1,"S").source)" in line 18 in tolua.lua, the output is in Unity console log:
"
16:56:0.485-1: [C:/Workspace/Assets/LuaFramework/ToLua/Lua/tolua.lua:18]:C:/Workspace/Assets/LuaFramework/ToLua/Lua/tolua.lua
UnityEngine.Debug:Log(Object)
LuaInterface.Debugger:Log(String)
LuaInterface.ToLua:Print(IntPtr) (at Assets/LuaFramework/ToLua/Core/ToLua.cs:182)
LuaInterface.LuaDLL:lua_pcall(IntPtr, Int32, Int32, Int32)
LuaInterface.LuaStatePtr:LuaPCall(Int32, Int32, Int32) (at Assets/LuaFramework/ToLua/Core/LuaStatePtr.cs:306)
LuaInterface.LuaState:LuaLoadBuffer(Byte[], String) (at Assets/LuaFramework/ToLua/Core/LuaState.cs:2026)
LuaInterface.LuaState:DoFile(String) (at Assets/LuaFramework/ToLua/Core/LuaState.cs:592)
LuaInterface.LuaState:OpenBaseLuaLibs() (at Assets/LuaFramework/ToLua/Core/LuaState.cs:201)
"
Hope this information is helpful.

I integrated the code into mobdebug.lua, then the stack and watch in editor work normally, GREAT JOB!

Thank you for checking; I'll integrate the changes into the serializer/debugger then.

Debugging suspended at 'c:/workspace/assets/luaframework/tolua/lua/tolua.lua:18' (couldn't activate the file).

Hm, this is strange; if editor.autoactivate is indeed set to true, then the file should be loaded. Can you execute the following in the Local console: wx.wxFileName([[c:/workspace/assets/luaframework/tolua/lua/tolua.lua]]):FileExists()? Does it return true?

I tried to reproduce in different ways, but the file is always opened in my case. Still trying to figure out what may be different in your case...

Hi pkulchenko, FileExists() returns true.
But I search all the files under ZerobraneStudio folder that contain string "autoactivate",
and set one to true in src\main.lua line 56
"
editor = {
autoactivate = true,
"
and I can step into correctly now! It seems that in my environment "editor.autoactivate" in "cfg\user-sample.lua" doesn't take effect.
Thank you for your effort, ZerobraneStudio works perfectly in my workspace now!

It seems that in my environment "editor.autoactivate" in "cfg\user-sample.lua" doesn't take effect.

Oh, that explains it: the changes (editor.autoactivate=true) need to be in cfg\user.lua; user-sample.lua is only to provide samples of commands.

@vinceee, these changes have been merge into master.