// ---------------------------------------------------------------------------
// FILE NAME : LuaDebugger.cpp
// ---------------------------------------------------------------------------
// DESCRIPTION :
//
// Simple debugging routines
//
// ---------------------------------------------------------------------------
// VERSION : 1.00
// DATE : 1-Sep-2005
// AUTHOR : Richard Shephard
// ---------------------------------------------------------------------------
// LIBRARY INCLUDE FILES
#include "stdafx.h"
#include "luadebugger.h"
#include "string.h"
// ---------------------------------------------------------------------------
bool CLuaDebugger::bAbortScript = false;
// typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
void CLuaDebugger::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);
}
}
}
void CLuaDebugger::LuaHookRet (lua_State *lua)
{
}
void CLuaDebugger::LuaHookLine (lua_State *lua)
{
// +- Modified by Auralius based : http://www.codegurus.be/codegurus/Programming/luahooks_en.htm
// Check the global flag to know if we should abort
if(bAbortScript)
{
// Ok, let's abort the script
lua_pushstring(lua, "HookRoutine: Abort requested!");
lua_error(lua);
}
}
void CLuaDebugger::LuaHookCount (lua_State *lua)
{
LuaHookLine (lua);
}
void CLuaDebugger::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);
}
bAbortScript = false;
}
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);
}
// +- Modified by Auralius Jan 10, 2009
void CLuaDebugger::ErrorRun (int iErrorCode)
{
/*switch (iErrorCode)
{
case LUA_ERRRUN:
strcpy(errorMsg, "LUA_ERRRUN\n");
break;
case LUA_ERRMEM:
strcpy(errorMsg, "LUA_ERRMEM\n");
break;
case LUA_ERRERR:
strcpy(errorMsg, "LUA_ERRERR\n");
break;
}*/
// Get the error string that appears on top of stack when a function
// fails to run
sprintf(errorMsg, "Error code: %i\r\nLine:%s\n", iErrorCode, lua_tostring ((lua_State *) m_vm, -1));
}
// ++ Added by Auralius Jan 10, 2009
void CLuaDebugger::GetErrorMsg(char *msg)
{
strcpy(msg, errorMsg);
}