lua-users home
lua-l archive

Yielding from hook and lua_getinfo (Was: Personal Lua Versions)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


-----Original Message----- 
> From: "Roberto Ierusalimschy" <roberto@inf.puc-rio.br> 
> To: "Lua mailing list" <lua-l@lists.lua.org> 
> Date: 05-05-2014 13:13 
> Subject: Re: Personal Lua Versions (Was: Apology to Thomas Jericke) 
> 
> > - Some changes to lua_getinfo to return the correct line when called
> > on a yielded task (It was off by one).
> 
> This seems like a bug to me. Have you reported that?
> 
> -- Roberto
> 
> 
So I tried this again with a standard interpreter.
First I had to remember again in which case I had problems with lua_getinfo.
The changes I had to do are because I yield from a debug hook.
The reason I  do this is to debug a Lua task while the other tasks
are still able to run.
I changed the main function to be like this:
static void step_hook (lua_State *L, lua_Debug *ar) {
    lua_yieldk(L, 0, 0, 0);
}
static int start_step (lua_State *L) {
    lua_sethook (L, &step_hook, LUA_MASKLINE, 0);
    return lua_yieldk(L, 0, 0, 0);
}
int main (int argc, char **argv) {
  int status = LUA_YIELD;
  int result;
  lua_State *L;
  lua_State *Lmain = luaL_newstate();  /* create state */
  if (Lmain == NULL) {
    l_message(argv[0], "cannot create state: not enough memory");
    return EXIT_FAILURE;
  }
  /* add global to start stepping */
  lua_pushcfunction(Lmain, &start_step);
  lua_setglobal(Lmain, "start_step");
  L = lua_newthread(Lmain);
  luaL_loadfile(L, "test.lua");
  /* while not ok or error */
  while(status == LUA_YIELD) {
      int level = 0;
      lua_Debug ar;
      /* call resume */
      status = lua_resume(L, NULL, 0);
      printf("\nPrint stack:\n");
      while(lua_getstack (L, level, &ar)) {
          if(lua_getinfo (L, "l", &ar)) {
              printf("Current Line=%i\n", ar.currentline);
          }
          level++;
      }
  }
  result = lua_toboolean(L, -1);  /* get result */
  lua_close(L);
  return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
}
With test.lua being:
local a = 1
start_step()
a = a + 1
a = a + 1
Which gets me a segfault :-(
As I knew that I got this working once I looked into my changes and found out that
the problem is in line 88 of lvm.c
ci->func = L->top - 1;  /* protect stack below results */
The problem is, that L->top - 1 is not a valid pointer and then ci->funct is not valid
But ci->func must be valid for lua_getinfo to work.
By removing the line I removed the problem, no segfault anymore but I get the following output:
Print stack:
Current Line=-1
Current Line=2
Print stack:
Current Line=2
Print stack:
Current Line=3
As you can see, line 2 is printed twice, as soon as I yield from a hook I am 
of by one. The first print, which is a yield from a C function call is correct.
My current solution to this problem is not very nice, I have an additional field in
the callinfo called debugpc (next to the savedpc) which stores the pc which is correct
from the viewpoint of the debugger.
I haven't reported this back than because I thought that I am doing something that is not supported.
Of course if Lua 5.3 would support this out of the box, I would be more than happy :-)
--
Thomas
 

AltStyle によって変換されたページ (->オリジナル) /