enhanced debug information of lua opcodes

This commit is contained in:
Pawel Kolodziejski 2009-05-23 13:59:42 +00:00
parent 91676c68dc
commit 671e2be1c4
5 changed files with 151 additions and 456 deletions

View file

@ -37,7 +37,7 @@ enum enDebugLevels {
DEBUG_NORMAL, DEBUG_NORMAL,
DEBUG_WARN, DEBUG_WARN,
DEBUG_ERROR, DEBUG_ERROR,
DEBUG_FUNC, DEBUG_LUA,
DEBUG_BITMAPS, DEBUG_BITMAPS,
DEBUG_MODEL, DEBUG_MODEL,
DEBUG_STUB, DEBUG_STUB,

File diff suppressed because it is too large Load diff

View file

@ -110,7 +110,7 @@ void luaD_lineHook(int32 line) {
void luaD_callHook(StkId base, TProtoFunc *tf, int32 isreturn) { void luaD_callHook(StkId base, TProtoFunc *tf, int32 isreturn) {
struct C_Lua_Stack oldCLS = L->Cstack; struct C_Lua_Stack oldCLS = L->Cstack;
StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top-L->stack.stack; StkId old_top = L->Cstack.lua2C = L->Cstack.base = L->stack.top - L->stack.stack;
L->Cstack.num = 0; L->Cstack.num = 0;
if (isreturn) if (isreturn)
(*lua_callhook)(LUA_NOOBJECT, "(return)", 0); (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
@ -134,15 +134,17 @@ static StkId callC(lua_CFunction f, StkId base) {
struct C_Lua_Stack *CS = &L->Cstack; struct C_Lua_Stack *CS = &L->Cstack;
struct C_Lua_Stack oldCLS = *CS; struct C_Lua_Stack oldCLS = *CS;
StkId firstResult; StkId firstResult;
int32 numarg = (L->stack.top-L->stack.stack) - base; int32 numarg = (L->stack.top - L->stack.stack) - base;
CS->num = numarg; CS->num = numarg;
CS->lua2C = base; CS->lua2C = base;
CS->base = base + numarg; // == top - stack CS->base = base + numarg; // == top - stack
if (lua_callhook) if (lua_callhook) {
luaD_callHook(base, NULL, 0); TObject *f = L->stack.stack + base - 1;
(*lua_callhook)(Ref(f), "(C)", -1);
}
(*f)(); // do the actual call (*f)(); // do the actual call
if (lua_callhook) // func may have changed lua_callhook // if (lua_callhook) // func may have changed lua_callhook
luaD_callHook(base, NULL, 1); // (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
firstResult = CS->base; firstResult = CS->base;
*CS = oldCLS; *CS = oldCLS;
return firstResult; return firstResult;
@ -206,8 +208,10 @@ void luaD_precall(TObject *f, StkId base, int32 nResults) {
L->ci->pc = NULL; L->ci->pc = NULL;
} else { } else {
byte *pc = tfvalue(f)->code; byte *pc = tfvalue(f)->code;
if (lua_callhook) if (lua_callhook) {
luaD_callHook(base, tfvalue(f), 0); TObject *f = L->stack.stack + base - 1;
(*lua_callhook)(Ref(f), tfvalue(f)->fileName->str, tfvalue(f)->lineDefined);
}
luaD_checkstack((*pc++) + EXTRA_STACK); luaD_checkstack((*pc++) + EXTRA_STACK);
if (*pc < ZEROVARARG) { if (*pc < ZEROVARARG) {
luaD_adjusttop(base + *(pc++)); luaD_adjusttop(base + *(pc++));

View file

@ -4,6 +4,9 @@
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include "common/endian.h"
#include "engine/actor.h"
#include "engine/lua/lbuiltin.h" #include "engine/lua/lbuiltin.h"
#include "engine/lua/ldo.h" #include "engine/lua/ldo.h"
@ -18,6 +21,7 @@
#include "engine/lua/ltask.h" #include "engine/lua/ltask.h"
#include "engine/lua/ltm.h" #include "engine/lua/ltm.h"
#include "engine/lua/lualib.h" #include "engine/lua/lualib.h"
#include "engine/lua/luadebug.h"
LState *lua_state = NULL; LState *lua_state = NULL;
@ -67,6 +71,68 @@ void lua_resetglobals() {
luaX_init(); luaX_init();
} }
Actor *check_actor(int num);
Color *check_color(int num);
void callHook(lua_Function func, const char *filename, int line) {
const char *name, *type;
FILE *output = stdout;
int i;
type = lua_getobjname(func, &name);
if (func == LUA_NOOBJECT) {
fprintf(output, "%s\n", filename);
return;
}
switch (*type) {
case 'g':
fprintf(output, "function: %s(", name);
for (i = 1; ; i++) {
if (lua_getparam(i) == LUA_NOOBJECT)
break;
if (lua_isnil(lua_getparam(i)))
fprintf(output, "nil");
else if (lua_istable(lua_getparam(i)))
fprintf(output, "{...}");
else if (lua_isuserdata(lua_getparam(i))) {
if (lua_tag(lua_getparam(i)) == MKID_BE('ACTR')) {
Actor *a = check_actor(i);
fprintf(output, "<actor \"%s\">", a->name());
} else if (lua_tag(lua_getparam(i)) == MKID_BE('COLR')) {
Color *c = check_color(i);
fprintf(output, "<color #%02x%02x%02x>", c->red(), c->green(), c->blue());
} else
fprintf(output, "<userdata %p>", lua_getuserdata(lua_getparam(i)));
} else if (lua_isfunction(lua_getparam(i))) {
fprintf(output, "<function>");
} else if (lua_isnumber(lua_getparam(i)))
fprintf(output, "%g", lua_getnumber(lua_getparam(i)));
else if (lua_isstring(lua_getparam(i)))
fprintf(output, "\"%s\"", lua_getstring(lua_getparam(i)));
else
fprintf(output, "<unknown>");
if (lua_getparam(i + 1) != LUA_NOOBJECT)
fprintf(output, ", ");
}
fprintf(output, ")");
break;
case 't':
fprintf(output, "`%s' tag method", name);
break;
default:
{
if (line == 0)
fprintf(output, "{START SCRIPT: %s}", filename);
else if (line < 0) {
fprintf(output, "%s", filename);
} else
fprintf(output, "function (%s:%d)", filename, line);
}
}
fprintf(output, "\n");
}
void lua_open() { void lua_open() {
if (lua_state) if (lua_state)
return; return;
@ -75,6 +141,8 @@ void lua_open() {
luaT_init(); luaT_init();
luaB_predefine(); luaB_predefine();
luaL_addlibtolist(stdErrorRimFunc, (sizeof(stdErrorRimFunc) / sizeof(stdErrorRimFunc[0]))); luaL_addlibtolist(stdErrorRimFunc, (sizeof(stdErrorRimFunc) / sizeof(stdErrorRimFunc[0])));
if (gDebugLevel == DEBUG_LUA || gDebugLevel == DEBUG_ALL)
lua_callhook = callHook;
} }
void lua_close() { void lua_close() {

View file

@ -678,8 +678,8 @@ callfunc:
case RETCODE: case RETCODE:
{ {
StkId firstResult = (base + ((aux == RETCODE) ? *pc : 0)); StkId firstResult = (base + ((aux == RETCODE) ? *pc : 0));
if (lua_callhook) // if (lua_callhook)
luaD_callHook(base, NULL, 1); // (*lua_callhook)(LUA_NOOBJECT, "(END OF SCRIPT}", 0);
// If returning from the original stack frame, terminate // If returning from the original stack frame, terminate
if (L->ci == L->base_ci + ci_index) if (L->ci == L->base_ci + ci_index)
return firstResult; return firstResult;