From: John L. <jr...@us...> - 2007-11-28 22:55:06
|
Update of /cvsroot/wxlua/wxLua/modules/wxluadebug/src In directory sc8-pr-cvs9.sourceforge.net:/tmp/cvs-serv28670/wxLua/modules/wxluadebug/src Modified Files: wxldebug.cpp Log Message: Use wxSIGKILL to kill debuggee process since wxSIGTERM doesn't work in MSW Made wxLuaCheckStack class work a little nicer for easier debugging in C++ Made wxluaT_insert(L, idx) not pop the value that it refs since it may not be the one at the top of the stack. Use wxLuaDebugData::GetTypeValue in wxLuaDebugTarget and other code cleanup in wxLuaDebugTarget Index: wxldebug.cpp =================================================================== RCS file: /cvsroot/wxlua/wxLua/modules/wxluadebug/src/wxldebug.cpp,v retrieving revision 1.48 retrieving revision 1.49 diff -C2 -d -r1.48 -r1.49 *** wxldebug.cpp 28 Nov 2007 00:20:50 -0000 1.48 --- wxldebug.cpp 28 Nov 2007 22:55:02 -0000 1.49 *************** *** 151,158 **** lua_State* L = wxlState.GetLuaState(); lua_Debug luaDebug = INIT_LUA_DEBUG; ! int nIndex = 0; int count = 0; ! while (lua_getstack(L, nIndex, &luaDebug) != 0) { if (lua_getinfo(L, "Sln", &luaDebug)) --- 151,158 ---- lua_State* L = wxlState.GetLuaState(); lua_Debug luaDebug = INIT_LUA_DEBUG; ! int stack_frame = 0; int count = 0; ! while (lua_getstack(L, stack_frame, &luaDebug) != 0) { if (lua_getinfo(L, "Sln", &luaDebug)) *************** *** 162,166 **** // skip stack frames that do not have line number, always add first int currentLine = luaDebug.currentline; ! if ((count == 0) || (currentLine != -1)) { wxString name; --- 162,166 ---- // skip stack frames that do not have line number, always add first int currentLine = luaDebug.currentline; ! if (1) //(count == 0) || (currentLine != -1)) { wxString name; *************** *** 175,184 **** name = wxString::Format(_("line %u"), currentLine); ! Add(new wxLuaDebugItem(name, WXLUAARG_None, wxT(""), WXLUAARG_None, source, LUA_NOREF, nIndex, 0)); count++; } } ! ++nIndex; } --- 175,184 ---- name = wxString::Format(_("line %u"), currentLine); ! Add(new wxLuaDebugItem(name, WXLUAARG_None, wxT(""), WXLUAARG_None, source, LUA_NOREF, stack_frame, 0)); count++; } } ! ++stack_frame; } *************** *** 186,190 **** } ! int wxLuaDebugData::EnumerateStackEntry(const wxLuaState& wxlState_, int stackRef, wxArrayInt& references) { wxCHECK_MSG(wxlState_.Ok(), 0, wxT("Invalid wxLuaState")); --- 186,190 ---- } ! int wxLuaDebugData::EnumerateStackEntry(const wxLuaState& wxlState_, int stack_frame, wxArrayInt& references) { wxCHECK_MSG(wxlState_.Ok(), 0, wxT("Invalid wxLuaState")); *************** *** 196,236 **** int count = 0; ! if (lua_getstack(L, stackRef, &luaDebug) != 0) { ! int idx = 1; ! wxString name(lua2wx(lua_getlocal(L, &luaDebug, idx))); while (!name.IsEmpty()) { //wxPrintf(wxString(lua_Debug_to_wxString(luaDebug) + wxT(" lua_getlocal :") + name + wxT("\n")).c_str()); ! int wxl_type = WXLUAARG_None; wxString value; wxString source(lua2wx(luaDebug.source)); - int flag_type = 0; ! GetTypeValue(wxlState, -1, &wxl_type, value); ! int nRef = LUA_NOREF; ! if (lua_istable(L, -1)) ! { ! nRef = wxluaT_isrefed(L, -1); // don't duplicate refs ! if (nRef == LUA_NOREF) ! { ! flag_type |= WXLUA_DEBUGITEM_LUAREFED; ! lua_pushvalue(L, -1); ! nRef = wxlState.wxluaT_Insert(-1); ! references.Add(nRef); ! } ! else ! lua_pop(L, 1); ! } ! else ! lua_pop(L, 1); // remove variable value ! Add(new wxLuaDebugItem(name, WXLUAARG_None, value, wxl_type, source, nRef, 0, flag_type)); count++; ! name = lua2wx(lua_getlocal(L, &luaDebug, ++idx)); } } --- 196,229 ---- int count = 0; ! if (lua_getstack(L, stack_frame, &luaDebug) != 0) { ! int stack_idx = 1; ! wxString name(lua2wx(lua_getlocal(L, &luaDebug, stack_idx))); while (!name.IsEmpty()) { //wxPrintf(wxString(lua_Debug_to_wxString(luaDebug) + wxT(" lua_getlocal :") + name + wxT("\n")).c_str()); ! int wxl_valuetype = WXLUAARG_None; wxString value; wxString source(lua2wx(luaDebug.source)); ! GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! // FIXME! local tables get the right values for GetTypeValue(...) ! // but when you run wxluaT_insert() to store them they disappear ! // so that the next time wxluaT_insert() is run it reuses the same ! // index as your local table. ! // When using the stack dialog the next wxluaT_insert() is the ! // global table which is very confusing. ! int flag_type = 0; ! int nRef = LUA_NOREF; //RefTable(L, -1, &flag_type, references); ! ! lua_pop(L, 1); // remove variable value ! Add(new wxLuaDebugItem(name, WXLUAARG_None, value, wxl_valuetype, source, nRef, 0, flag_type)); count++; ! name = lua2wx(lua_getlocal(L, &luaDebug, ++stack_idx)); } } *************** *** 250,254 **** int wxl_keytype = WXLUAARG_None; int wxl_valuetype = WXLUAARG_None; - int flag_type = 0; wxString value; wxString name; --- 243,246 ---- *************** *** 258,264 **** wxlState.GetGlobals(); GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! int nRef = wxluaT_insert(L, -1); ! Add(new wxLuaDebugItem(wxT("Globals"), WXLUAARG_None, value, WXLUAARG_Table, wxT(""), nRef, 0, WXLUA_DEBUGITEM_LUAREFED)); ! references.Add(nRef); } else if (tableRef == LUA_REGISTRYINDEX) --- 250,259 ---- wxlState.GetGlobals(); GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! ! int flag_type = 0; ! int nRef = RefTable(L, -1, &flag_type, references); ! lua_pop(L, 1); // pop globals table ! ! Add(new wxLuaDebugItem(wxT("Globals"), WXLUAARG_None, value, WXLUAARG_Table, wxT(""), nRef, 0, flag_type)); } else if (tableRef == LUA_REGISTRYINDEX) *************** *** 266,272 **** wxlState.lua_PushValue(LUA_REGISTRYINDEX); GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! int nRef = wxluaT_insert(L, -1); ! Add(new wxLuaDebugItem(wxT("Lua Registry"), WXLUAARG_None, value, WXLUAARG_Table, wxT(""), nRef, 0, WXLUA_DEBUGITEM_LUAREFED)); ! references.Add(nRef); } else --- 261,270 ---- wxlState.lua_PushValue(LUA_REGISTRYINDEX); GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! ! int flag_type = 0; ! int nRef = RefTable(L, -1, &flag_type, references); ! lua_pop(L, 1); // pop registry table ! ! Add(new wxLuaDebugItem(wxT("Registry"), WXLUAARG_None, value, WXLUAARG_Table, wxT(""), nRef, 0, WXLUA_DEBUGITEM_LUAREFED)); } else *************** *** 275,283 **** if (wxlState.wxluaT_Get(tableRef)) { ! int nTop = lua_gettop(L); // start iterating lua_pushnil(L); ! while (lua_next(L, nTop) != 0) { // get the index, just want the name=value, type is dummy here --- 273,293 ---- if (wxlState.wxluaT_Get(tableRef)) { ! if (lua_getmetatable(L, -1)) // if no metatable then nothing is pushed ! { ! // get the type and value ! GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! ! int flag_type = 0; ! int nRef = RefTable(L, -1, &flag_type, references); ! ! Add(new wxLuaDebugItem(wxT(" __metatable"), WXLUAARG_Table, value, wxl_valuetype, wxT(""), nRef, nIndex, flag_type)); ! count++; ! ! lua_pop(L, 1); // pop metatable ! } // start iterating lua_pushnil(L); ! while (lua_next(L, -2) != 0) { // get the index, just want the name=value, type is dummy here *************** *** 286,309 **** GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! //wxPrintf(wxT("wxLuaDebugData::EnumerateTable %d type='%s' key='%s' val='%s'\n"), count, type.c_str(), name.c_str(), value.c_str()); ! ! int nRef = LUA_NOREF; ! if (lua_istable(L, -1)) ! { ! nRef = wxluaT_isrefed(L, -1); // don't duplicate refs ! ! if (nRef == LUA_NOREF) ! { ! flag_type |= WXLUA_DEBUGITEM_LUAREFED; ! nRef = wxluaT_insert(L, -1); // pops value ! references.Add(nRef); ! } ! else ! lua_pop(L, 1); ! } ! else ! lua_pop(L, 1); ! Add(new wxLuaDebugItem(name, wxl_keytype, value, wxl_valuetype, wxT(""), nRef, nIndex)); count++; } --- 296,305 ---- GetTypeValue(wxlState, -1, &wxl_valuetype, value); ! int flag_type = 0; ! int nRef = RefTable(L, -1, &flag_type, references); ! ! lua_pop(L, 1); ! Add(new wxLuaDebugItem(name, wxl_keytype, value, wxl_valuetype, wxT(""), nRef, nIndex, flag_type)); count++; } *************** *** 316,319 **** --- 312,336 ---- } + + int wxLuaDebugData::RefTable(lua_State* L, int stack_idx, int* flag_type, wxArrayInt& references) + { + wxCHECK_MSG(L, LUA_NOREF, wxT("Invalid lua_State")); + + int nRef = LUA_NOREF; + if (lua_istable(L, stack_idx)) + { + nRef = wxluaT_isrefed(L, stack_idx); // don't duplicate refs + + if (nRef == LUA_NOREF) + { + if (flag_type) *flag_type |= WXLUA_DEBUGITEM_LUAREFED; + nRef = wxluaT_insert(L, -1); + references.Add(nRef); + } + } + + return nRef; + } + int wxLuaDebugData::GetTypeValue(const wxLuaState& wxlState, int stack_idx, int* wxl_type_, wxString& value) { *************** *** 470,541 **** // ---------------------------------------------------------------------------- ! wxLuaCheckStack::wxLuaCheckStack(lua_State *L, const wxString &msg) { m_luaState = L; m_msg = msg; m_top = lua_gettop(m_luaState); } wxLuaCheckStack::~wxLuaCheckStack() { ! TestStack(m_msg); } ! void wxLuaCheckStack::TestStack(const wxString &msg) { ! wxString message(!msg.IsEmpty() ? msg : m_msg); ! wxString s = wxString::Format(wxT("Stack state in '%s': starting top %d ending top %d\n"), ! message.c_str(), m_top, lua_gettop(m_luaState)); OutputMsg(s); } ! void wxLuaCheckStack::DumpGlobals() { ! wxCHECK_RET(m_luaState, wxT("Invalid lua_State")); lua_State* L = m_luaState; wxSortedArrayString tableArray; lua_pushvalue(L, LUA_GLOBALSINDEX); ! DumpTable(lua_gettop(L), wxT("Globals"), tableArray, 0); lua_pop(L, 1); } ! void wxLuaCheckStack::DumpTable(const wxString &name) { ! wxCHECK_RET(m_luaState, wxT("Invalid lua_State")); - lua_State* L = m_luaState; wxSortedArrayString tableArray; ! lua_pushstring(L, wx2lua(name)); ! lua_gettable(L, LUA_GLOBALSINDEX); ! DumpTable(lua_gettop(L), name, tableArray, 0); ! lua_pop(L, 1); } ! void wxLuaCheckStack::DumpTable(int index, const wxString& tablename, wxSortedArrayString& tableArray, int indent) { ! wxCHECK_RET(m_luaState, wxT("Invalid lua_State")); lua_State* L = m_luaState; wxLuaState wxlState(L); wxString indentStr; ! if (indent > 5) ! return; ! for (int i = 0; i < indent; i++) ! indentStr += wxT(" "); ! wxString title = wxString::Format(wxT("%sTable: %s"), indentStr.c_str(), tablename.c_str()); OutputMsg(title); ! if (!indentStr.IsEmpty()) ! indentStr += wxT(">"); lua_pushnil(L); ! while(lua_next(L, index) != 0) { int keyType = 0, valueType = 0; --- 487,641 ---- // ---------------------------------------------------------------------------- ! wxLuaCheckStack::wxLuaCheckStack(lua_State *L, const wxString &msg, bool print_to_console) { m_luaState = L; m_msg = msg; m_top = lua_gettop(m_luaState); + m_print_to_console = print_to_console; } wxLuaCheckStack::~wxLuaCheckStack() { ! if (m_print_to_console) ! TestStack(wxT("~wxLuaCheckStack")); } ! wxString wxLuaCheckStack::TestStack(const wxString &msg) { ! wxString s; ! s.Printf(wxT("wxLuaCheckStack::TestStack(L=%p) '%s':'%s': starting top %d ending top %d\n"), ! m_luaState, m_msg.c_str(), msg.c_str(), m_top, lua_gettop(m_luaState)); OutputMsg(s); + + return s; } ! wxString wxLuaCheckStack::DumpStack(const wxString& msg) { ! wxCHECK_MSG(m_luaState, wxEmptyString, wxT("Invalid lua_State")); ! ! lua_State* L = m_luaState; ! int i, count = lua_gettop(L); ! wxString str; ! wxString retStr; ! ! str.Printf(wxT("wxLuaCheckStack::DumpStack(L=%p), '%s':'%s', items %d, starting top %d\n"), L, m_msg.c_str(), msg.c_str(), count, m_top); ! retStr += str; ! OutputMsg(str); ! ! wxLuaState wxlState(L); ! ! for (i = 1; i <= count; i++) ! { ! int wxl_type = 0; ! wxString value; ! int l_type = wxLuaDebugData::GetTypeValue(wxlState, i, &wxl_type, value); ! ! str.Printf(wxT(" idx %d: l_type = %d, wxl_type = %d : '%s'='%s'\n"), ! i, l_type, wxl_type, wxlua_getwxluatypename(wxl_type).c_str(), value.c_str()); ! retStr += str; ! OutputMsg(str); ! } ! ! return retStr; ! } ! ! wxString wxLuaCheckStack::DumpGlobals(const wxString& msg) ! { ! wxCHECK_MSG(m_luaState, wxEmptyString, wxT("Invalid lua_State")); ! ! wxSortedArrayString tableArray; ! ! return DumpTable(LUA_GLOBALSINDEX, wxT("Globals"), msg, tableArray, 0); ! } ! ! wxString wxLuaCheckStack::DumpTable(const wxString &tablename, const wxString& msg) ! { ! wxCHECK_MSG(m_luaState, wxEmptyString, wxT("Invalid lua_State")); lua_State* L = m_luaState; wxSortedArrayString tableArray; + wxString s; + // Allow iteration through table1.table2.table3... + wxString tname(tablename); lua_pushvalue(L, LUA_GLOBALSINDEX); ! ! do { ! lua_pushstring(L, wx2lua(tname.BeforeFirst(wxT('.')))); ! lua_rawget(L, -2); ! ! if (lua_isnil(L, -1) || !lua_istable(L, -1)) ! { ! lua_pop(L, 2); // remove table and value ! ! s.Printf(wxT("wxLuaCheckStack::DumpTable(L=%p) Table: '%s' cannot be found!\n"), L, tablename.c_str()); ! OutputMsg(s); ! return s; ! } ! ! lua_remove(L, -2); // remove previous table ! tname = tname.AfterFirst(wxT('.')); ! } while (tname.Len() > 0); ! ! s = DumpTable(lua_gettop(L), tablename, msg, tableArray, 0); lua_pop(L, 1); + + return s; } ! wxString wxLuaCheckStack::DumpTable(int stack_idx, const wxString& msg) { ! wxCHECK_MSG(m_luaState, wxEmptyString, wxT("Invalid lua_State")); wxSortedArrayString tableArray; ! return DumpTable(stack_idx, wxString::Format(wxT("StackIdx=%d"), stack_idx), msg, tableArray, 0); } ! wxString wxLuaCheckStack::DumpTable(int stack_idx, const wxString& tablename, const wxString& msg, wxSortedArrayString& tableArray, int indent) { ! wxCHECK_MSG(m_luaState, wxEmptyString, wxT("Invalid lua_State")); lua_State* L = m_luaState; wxLuaState wxlState(L); wxString indentStr; + wxString s; ! // We only do tables, return error message ! if (!lua_istable(L, stack_idx)) ! { ! s.Printf(wxT("wxLuaCheckStack::DumpTable(L=%p) stack idx %d is not a table.\n"), L, stack_idx); ! OutputMsg(s); ! return s; ! } ! if (indent == 0) ! { ! // First time through print header ! s.Printf(wxT("wxLuaCheckStack::DumpTable(L=%p) Table: '%s'\n"), L, tablename.c_str()); ! OutputMsg(s); ! } ! else if (indent > 10) ! { ! // Don't let things get out of hand... ! s.Printf(wxT("wxLuaCheckStack::DumpTable(L=%p) Table depth > 10! Truncating: '%s'\n"), L, tablename.c_str()); ! OutputMsg(s); ! return s; ! } ! else ! { ! indentStr = wxString(wxT(' '), indent*2) + wxT(">"); ! } ! wxString title = wxString::Format(wxT("%sTable Level %d : name '%s'\n"), indentStr.c_str(), indent, tablename.c_str()); ! s += title; OutputMsg(title); ! lua_pushvalue(L, stack_idx); // push the table to read the top of the stack lua_pushnil(L); ! while (lua_next(L, -2) != 0) { int keyType = 0, valueType = 0; *************** *** 545,550 **** wxLuaDebugData::GetTypeValue(wxlState, -1, &valueType, value); ! wxString info = wxString::Format(wxT("%s%-32s\t%d\t%-20s\t%d"), ! indentStr.c_str(), key.c_str(), keyType, value.c_str(), valueType); OutputMsg(info); --- 645,651 ---- wxLuaDebugData::GetTypeValue(wxlState, -1, &valueType, value); ! wxString info = wxString::Format(wxT("%s%-32s\t%-16s\t%-20s\t%-16s\n"), ! indentStr.c_str(), key.c_str(), wxlua_getwxluatypename(keyType).c_str(), value.c_str(), wxlua_getwxluatypename(valueType).c_str()); ! s += info; OutputMsg(info); *************** *** 553,561 **** if (valueType == WXLUAARG_Table) { - wxString tname = wxString::Format(wxT("%s.%s"), tablename.c_str(), key.c_str()); tableArray.Add(value); ! int tableIndex = lua_gettop(L); ! // lua_pushvalue(L, lua_gettop(L)); ! DumpTable(tableIndex, tname, tableArray, indent+1); } else --- 654,659 ---- if (valueType == WXLUAARG_Table) { tableArray.Add(value); ! s += DumpTable(lua_gettop(L), tablename + wxT(".") + key, msg, tableArray, indent+1); } else *************** *** 565,608 **** } ! //#if defined(__WXMSW__) ! // OutputDebugString(outputIndex + outputValue); ! //#elif defined(__WXGTK__) ! // fprintf(stderr, wx2lua(outputIndex + outputValue)); ! //#endif ! lua_pop(L, 1); } - lua_pop(L, 1); - - //wxLog::FlushActive(); - } - - void wxLuaCheckStack::DumpStack() - { - wxCHECK_RET(m_luaState, wxT("Invalid lua_State")); - - lua_State* L = m_luaState; - int i, count = lua_gettop(L); - OutputMsg(wxString::Format(wxT("wxLuaCheckStack::DumpStack(%ld) items %d\n"), (long)L, count)); - - wxLuaState wxlState(L); ! for (i = 1; i <= count; i++) ! { ! //int type = lua_type(L, i); ! //wxPrintf(wxT("Stack item %d of %d : type %d '%s'\n"), i, argCount, type, lua2wx(lua_typename(L, type)).c_str()); ! int wxl_type = 0; ! wxString value; ! int l_type = wxLuaDebugData::GetTypeValue(wxlState, i, &wxl_type, value); ! OutputMsg(wxString::Format(wxT("item %d: lua_type %d wxlua_type %d = '%s'\n"), i, l_type, wxl_type, value.c_str())); ! } } void wxLuaCheckStack::OutputMsg(const wxString& msg) const { #if defined(__WXMSW__) ! OutputDebugString(msg.c_str()); #else //if defined(__WXGTK__) || defined(__WXMAC__) ! wxPrintf(wxT("%s\n"), msg.c_str()); #endif } --- 663,684 ---- } ! lua_pop(L, 1); // pop value } ! lua_pop(L, 1); // pop pushed table ! return s; } void wxLuaCheckStack::OutputMsg(const wxString& msg) const { + if (m_print_to_console) + { #if defined(__WXMSW__) ! //OutputDebugString(msg.c_str()); ! wxPrintf(wxT("%s"), msg.c_str()); fflush(stdout); #else //if defined(__WXGTK__) || defined(__WXMAC__) ! wxPrintf(wxT("%s"), msg.c_str()); #endif + } } |