Question regarding the Lua Debug interfaces.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Question regarding the Lua Debug interfaces.

Paul Winwood
Hi!

I have a question concerning the Lua debug interfaces (lua_getstack,
lua_getinfo) etc implemented in Lua 4.0
I apologize for the length of this question but I wanted to make myself clear.

I have implemented Lua in my Active Scripting engine using the Lua 4.0. To
implement Active Script Debugging I am using a line hook and when the line hook
is called on a line that has a breakpoint set , the debugging mechanism is
called. The following code is an extract from the line hook:

[[
void LineHookFunction(lua_State *pluaState, lua_Debug *pluaDebug) {
    if (*pluaDebug->event == 'l') {
        ...
    lua_Debug luaDebug;
    int maxIndex = 0;
    while (lua_getstack(pluaState, maxIndex, &luaDebug)) {
            if (lua_getinfo (pluaState, "lnuS", &luaDebug)) {
                printf("Line hook: c:%u, n:%s, w:%s, u:%u, d:%u, w:%s, s:%s
m:%s\r\n",
                        luaDebug.currentline,
                        luaDebug.name,
                        luaDebug.namewhat,
                        luaDebug.nups,
                        luaDebug.linedefined,
                        luaDebug.what,
                        luaDebug.source,
                        luaDebug.short_src);                
            }           
    maxIndex++;
        }
    }
}

The script function that I run is as follows:

function function2()
    print(" in function2")
    function3() 
end

function function3()
    print(" in function3")  --> breakpoint set here
end

function function1()
    print("in function1")
    function2()
end

function Main()
    local a, b, c
    function1()
end

I load this code into Lua using lua_dobuffer() and then call getglobal and
lua_rawcall as follows:

{
    int nOldTop = lua_gettop(m_luaState);
    lua_getglobal(m_luaState, "Main");
    if (lua_isfunction(m_luaState, -1))
        lua_rawcall(m_luaState, 0, 0);
    lua_settop(m_luaState, nOldTop);
}

Now to my question. I set a breakpoint on the print statement in the function 3
and invoke 'Main'. 
Then when the breakpoint is hit, I get the following output from my line hook

Line hook: c:6, n:function3, w:global, u:0, d:5, w:Lua, s:(0:0) m:string "(0:0)"
Line hook: c:2, n:function2, w:global, u:0, d:0, w:main, s:(0:0) m:string
"(0:0)"
Line hook: c:11, n:function1, w:global, u:0, d:9, w:Lua, s:(0:0) m:string
"(0:0)"

You will observe the following about this output.

-  The function Main does not have an accessible 'activation record'. 
   Does this also mean I am unable to inspect the locals of this function from
my debug code?

- The function 'function2' has a 'what' of 'main' whereas 'function1' and
'function3' have a what of 'Lua'.
   Does the first lexed function in a lua script block get a what of 'main' and
all others are 'Lua'?

>From my debugging routines, could you provide assistance as to how I access the
activation record of function 
'Main' and also access its locals?. Also could you indicate what the meaning is
of the 'what' in the luaDebug structure.

Any assistance that you give would be appreciated.

Thanks,
Paul.


Reply | Threaded
Open this post in threaded view
|

Re: Question regarding the Lua Debug interfaces.

Luiz Henrique de Figueiredo
>Now to my question. I set a breakpoint on the print statement in the function 3
>and invoke 'Main'. 
>Then when the breakpoint is hit, I get the following output from my line hook
>
>Line hook: c:6, n:function3, w:global, u:0, d:5, w:Lua, s:(0:0) m:string "(0:0)"
>Line hook: c:2, n:function2, w:global, u:0, d:0, w:main, s:(0:0) m:string
>"(0:0)"
>Line hook: c:11, n:function1, w:global, u:0, d:9, w:Lua, s:(0:0) m:string
>"(0:0)"

I don't know what is wrong, but when the same thing is run from Lua it works:

I've changed function3 to:

function function3()
    print(" in function3")  --> breakpoint set here
    trace()
end

where

function trace()
 for i=1,1000 do 
  local a=getinfo(i)
  if a==nil then break end
  write(format("Line hook: c:%u, n:%s, w:%s, u:%u, d:%u, w:%s, s:%s m:%s\n",
  a.currentline, a.name or "nil", a.namewhat or "nil",
  a.nups, a.linedefined, a.what, a.source, a.short_src))
 end
end

Then I get

in function1
 in function2
 in function3
Line hook: c:3, n:trace, w:global, u:0, d:1, w:Lua, s:@j m:file `j'
Line hook: c:8, n:function3, w:global, u:0, d:6, w:Lua, s:@i m:file `i'
Line hook: c:3, n:function2, w:global, u:0, d:1, w:Lua, s:@i m:file `i'
Line hook: c:13, n:function1, w:global, u:0, d:11, w:Lua, s:@i m:file `i'
Line hook: c:18, n:Main, w:global, u:0, d:16, w:Lua, s:@i m:file `i'
Line hook: c:21, n:nil, w:, u:0, d:0, w:main, s:@i m:file `i'

which seems right.
--lhf