lua-users home
lua-l archive

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


It was thus said that the Great M. Gerhardy once stated:
> Hi.
> 
> I was looking for a way to do stackdumps from within C/C++ - I couldn't
> find anything that also prints tables.
> 
> That's why I did this small helper header file:
> https://github.com/mgerhardy/lua_stackdump
> 
> Feedback is more than welcome. Pull requests, too ;). I'm quite new to lua,
> so please let me know if you can find anything wrong. There are also a few
> TODO comments included - if someone could help me to solve them... please do

  Hmm ... and to think I just got by with:

	void dumpstack_lua(lua_State *L)
	{
	  int max = max = lua_gettop(L);
	  int i;

	  /*----------------------------------------------------------
	  ; however you want to handle the output---I normally go for
	  ; syslog, but for this example, I'm using printf()
	  ;---------------------------------------------------------*/
          
	  printf("----LUA STACK DUMP----\n");
	  
	  for (i = 1 ; i <= max ; i++)
	  {
	    const char *name;
	    const char *type;
	    int         ri;
	    
	    lua_getglobal(L,"tostring");
	    lua_pushvalue(L,i);
	    lua_call(L,1,1);
	    
	    name = lua_tostring(L,-1);
	    type = luaL_typename(L,i);
	    ri   = i - max - 1;

	    printf("Stack: %d %d - %s %s",i,ri,type,name);
	    lua_pop(L,1);
	  }
	}
	
  I could probably do more with functions, table and userdata, but for my
own use, this has been good enough.  Also, you could take advantage of Lua
(like I did by calling tostring()) and provide a Lua function to dump
tables, function names, etc.  

  -spc