From 10a0e290ac07524dc129cc2b227b0f9e95df0f8e Mon Sep 17 00:00:00 2001 From: Ben Beltran Date: Mon, 16 Apr 2012 09:39:52 -0500 Subject: first commit --- LuaDebugger.cpp | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 LuaDebugger.cpp (limited to 'LuaDebugger.cpp') diff --git a/LuaDebugger.cpp b/LuaDebugger.cpp new file mode 100644 index 0000000..18f3909 --- /dev/null +++ b/LuaDebugger.cpp @@ -0,0 +1,122 @@ +// --------------------------------------------------------------------------- +// FILE NAME : LuaDebugger.cpp +// --------------------------------------------------------------------------- +// DESCRIPTION : +// +// Simple debugging routines +// +// --------------------------------------------------------------------------- +// VERSION : 1.00 +// DATE : 1-Sep-2005 +// AUTHOR : Richard Shephard +// --------------------------------------------------------------------------- +// LIBRARY INCLUDE FILES +#include "luadebugger.h" +// --------------------------------------------------------------------------- + +// typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); +static void LuaHookCall (lua_State *lua) +{ + + printf ("---- Call Stack ----\n"); +// printf ("[Level] [Function] [# args] [@line] [src]\n"); + + lua_Debug ar; + + // Look at call stack + for (int iLevel = 0; lua_getstack (lua, iLevel, &ar) != 0; ++iLevel) + { + if (lua_getinfo (lua, "Snlu", &ar) != 0) + { + printf ("%d %s %s %d @%d %s\n", iLevel, ar.namewhat, ar.name, ar.nups, ar.linedefined, ar.short_src); + } + } +} + +static void LuaHookRet (lua_State *lua) +{ + +} + +static void LuaHookLine (lua_State *lua) +{ + lua_Debug ar; + lua_getstack (lua, 0, &ar); + + + if (lua_getinfo (lua, "Sl", &ar) != 0) + { + printf ("exe %s on line %d\n", ar.short_src, ar.currentline); + } +} + +static void LuaHookCount (lua_State *lua) +{ + LuaHookLine (lua); +} + +static void LuaHook (lua_State *lua, lua_Debug *ar) +{ + // Handover to the correct hook + switch (ar->event) + { + case LUA_HOOKCALL: + LuaHookCall (lua); + break; + case LUA_HOOKRET: + case LUA_HOOKTAILRET: + LuaHookRet (lua); + break; + case LUA_HOOKLINE: + LuaHookLine (lua); + break; + case LUA_HOOKCOUNT: + LuaHookCount (lua); + break; + } +} + +CLuaDebugger::CLuaDebugger (CLuaVirtualMachine& vm) : m_iCountMask (10), m_vm (vm) +{ + // Clear all current hooks + if (vm.Ok ()) + { + vm.AttachDebugger (this); + lua_sethook ((lua_State *) vm, LuaHook, 0, m_iCountMask); + } +} + +CLuaDebugger::~CLuaDebugger (void) +{ + // Clear all current hooks + if (m_vm.Ok ()) + { + lua_sethook ((lua_State *) m_vm, LuaHook, 0, m_iCountMask); + } +} + +void CLuaDebugger::SetHooksFlag (int iMask) +{ + // Set hooks + lua_sethook ((lua_State *) m_vm, LuaHook, iMask, m_iCountMask); +} + +void CLuaDebugger::ErrorRun (int iErrorCode) +{ + switch (iErrorCode) + { + case LUA_ERRRUN: + printf ("LUA_ERRRUN\n"); + break; + case LUA_ERRMEM: + printf ("LUA_ERRMEM\n"); + break; + case LUA_ERRERR: + printf ("LUA_ERRERR\n"); + break; + } + + // Get the error string that appears on top of stack when a function + // fails to run + printf ("Error: %s\n", lua_tostring ((lua_State *) m_vm, -1)); +} \ No newline at end of file -- cgit