lua-users home
lua-l archive

Hacking Lua for debug information

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


Hello,
I am trying to create a debug function which shows what is currently on the stack, since it is a debug function and wont end up in a release build, i decided to dive in to the guts of Lua.
See here, my testing program for the debug function simple_dump:
	#include <stdio.h>
	#include <stdlib.h>
	
	#include <lua.h>
	#include <lualib.h>
	#include <lauxlib.h>
	
	#ifdef LUA_GUTS
	#include <lapi.h>
	#endif
	
	int simple_dump(lua_State* L)
	{
		int top = lua_gettop(L);
	
		printf("%d stack items.\n", top);
	
		for (int i = 1; i <= top; i++)
		{
	#ifdef LUA_GUTS
			TValue* p = L->ci->func + i;
			int type = ttypenv(p);
	#else
			const void* p = lua_topointer(L, i);
			int type = lua_type(L, i);
	#endif
	
			printf("pointer: %p", p);
	
			switch(type)
			{
			case LUA_TSTRING:
				printf("\tstr value: %s", lua_tostring(L, i));
				break;
			case LUA_TNUMBER:
				printf("\tnr value: %f", lua_tonumber(L, i));
				break;
	#ifdef LUA_GUTS
			default:
				printf("\tgc pointer: %p", p->value_.gc);
				break;
	#endif
			}
	
			printf("\n");
		}
	
		return 0;
	}
	
	int main(int argc, char* argv[])
	{
		puts("Lua 5.2 debug test.");
	
		lua_State* L = luaL_newstate();
	
		lua_pushcfunction(L, simple_dump);
	
		lua_newtable(L);
		lua_newtable(L);
		lua_pushvalue(L, -1);
		lua_pushstring(L, "Foo");
		lua_pushnumber(L, 5.2);
		lua_pushnumber(L, -3.223);
		lua_pushvalue(L, -3);
		lua_pushnumber(L, 10.8233);
		lua_pushstring(L, "Bar");
		lua_newtable(L);
		lua_newtable(L);
	
		lua_call(L, lua_gettop(L) - 1, 0);
	
		lua_close(L);
	}
On 64 bit Linux, with GCC 4.6.3, and compiled as:
gcc main.c -g -DLUA_GUTS -std=c99 -o main -I/opt/lua52/src /opt/lua52/src/liblua.a -lm
The ouput:
	Lua 5.2 debug test.
	11 stack items.
	pointer: 0x23f41a0	gc pointer: 0x23f3fe0
	pointer: 0x23f41b0	gc pointer: 0x23f4030
	pointer: 0x23f41c0	gc pointer: 0x23f4030
	pointer: 0x23f41d0	str value: Foo
	pointer: 0x23f41e0	nr value: 5.200000
	pointer: 0x23f41f0	nr value: -3.223000
	pointer: 0x23f4200	str value: Foo
	pointer: 0x23f4210	nr value: 10.823300
	pointer: 0x23f4220	str value: Bar
	pointer: 0x23f4230	gc pointer: 0x23f40e0
	pointer: 0x23f4240	gc pointer: 0x23f4130
Looks okay so far.
But since the software that is going use this debugging function is cross platform, it should also work on Windows.
On 64 bit Windows 7, with GCC 4.6.2 (MinGW 32 bit), and compiled as:
gcc -g -std=c99 -DLUA_GUTS -ID:\software\lua-5.2.0\src main.c -o main.exe D:\software\lua-5.2.0\src\liblua.a
The output (run from GDB):
	Starting program: d:\code\c\luatest\main.exe
	[New Thread 1052.0x1004]
	Lua 5.2 debug test.
	11 stack items.
	pointer: 003e4230 gc pointer: 003e4140
	pointer: 003e4240 gc pointer: 003e37b0
	pointer: 003e4250 gc pointer: 39581062
	pointer: 003e4260 gc pointer: 93dd97f6
	pointer: 003e4270 gc pointer: 003e41a8
	pointer: 003e4280 gc pointer: baadf00d
	pointer: 003e4290 gc pointer: baadf00d
	pointer: 003e42a0 gc pointer: baadf00d
	pointer: 003e42b0 gc pointer: baadf00d
	pointer: 003e42c0 gc pointer: baadf00d
	pointer: 003e42d0 gc pointer: baadf00d
	[Inferior 1 (process 1052) exited normally]
Wait, what? how can this be? The first item and the last item are both a table (different instances), but why is the last table's gc pointer NULL? And why is the type not found for the string and number values? What is going on here? Lua seems to work just fine otherwise. There is no reason for me to believe Lua is corrupted somehow other then the above output. Also without LUA_GUTS defined, i get the right output according to Lua documentation on both platforms. So, am i doing something which i simply should not be doing, debugging or otherwise, or is there truly something screwy going on?
- Florian

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