AboutLua DebuggerCode implementation is the content to be introduced in this article, mainly to understandLUA DebuggerFor more information, see this article.
1. http://www.unknownworlds.com/decoda. this tool can be noted toLuaDebug the script.
2. There are two ways to debug lua
Start the Host Program from Decoda
1) Settings in the project menu
2) enter the host program you want to run in commond. Click OK
3) use it to open the lua script to set the breakpoint. Select Start Debugging in Decoda.
The following is a simple example.
- Main. cpp
-
- # Include <iostream>
- # Include "luaDebug. h"
-
- Using namespace std;
-
- Int main () startLuaDebug ();
- DebugFile ("add. lua ");
- ParamData in [1];
- ParamData out;
- In [0]. tt = PNUM; in [0]. value. p = "HELLO :";
- Out. tt = PNUM;
-
- DebugFunction ("Hello", in, 1, 1, & out );
-
- StopLuaDebug ();
-
- Printf ("% s \ n", out. value. p );
- System ("pause ");
- Return 0;
- }
-
- LuaDebug. h
-
- # Ifndef LUA_DEBUG_H
- # Define LUA_DEBUG_H
-
- Enum tt nil, // null
- BNUM, // boolean
- CNUM, // char
- INUM, // int
- LNUM, // long
- FNUM, // float | double
- PNUM, // char *
- VNUM // void *
- };
-
- Typedef union ParamValue bool B;
- Char c;
- Int I;
- Long l;
- Float f;
- Char * p;
- Void * v;
- } ParamValue;
-
- Typedef struct ParamData int tt;
- ParamValue value;
- } ParamData;
-
- Int startLuaDebug ();
- Void stopLuaDebug ();
- Int DebugFile (char * filename );
- Void DebugFunction (char * funName,
- ParamData param [],
- Int len,
- Bool bret,
- ParamData * pRet
- );
-
- # Endif
-
- LuaDebug. cpp
-
- # Include <stdio. h>
- # Include <iostream>
- # Include <stdlib. h>
- # Include "lua. hpp"
- # Include "luaDebug. h"
-
- Lua_State * L;
-
- /*
- * Enable lua Virtual Machine
- * Ret 1 => open vm error!
- * 0 => open vm success! Int startLuaDebug () L = lua_open ();
- If (L = NULL) return 1;
- LuaL_openlibs (L );
- Return 0;
- }
-
- /*
- * Shut down the lua Virtual Machine void stopLuaDebug () lua_close (L );
- }
-
- /*
- * FunName function name
- * Param [] parameter Array
- * Len parameter length
- * Whether bret has returned results
- * Void DebugFunction (char * funName,
- ParamData param [],
- Int len,
- Bool bret,
- ParamData * pRet {
- If (NULL = L | funName = NULL) return;
- Lua_getglobal (L, funName );
- For (int I = 0; I <len; I ++ ){
- Switch (param [I]. tt ){
- Case BNUM:
- Lua_pushboolean (L, param [I]. value. B );
- Break;
- Case CNUM:
- Lua_pushinteger (L, (int) param [I]. value. c );
- Break;
- Case INUM:
- Lua_pushinteger (L, param [I]. value. I );
- Break;
- Case LNUM:
- Lua_pushinteger (L, param [I]. value. l );
- Break;
- Case FNUM:
- Lua_pushnumber (L, param [I]. value. f );
- Break;
- Case PNUM: lua_pushstring (L, param [I]. value. p );
- Break; case VNUM: lua_pushlightuserdata (L, param [I]. value. v );
- Break ;}
- } Lua_call (L, len, (int) bret );
- If (bret ){
- If (pRet! = NULL ){
- // For convenience of expansion and application, [lua_type (L, lua_gettop (L)] is not used here, And the type is specified by the Parameter
- Switch (pRet-> tt ){
- Case BNUM: pRet-> value. B = lua_toboolean (L,-1); break;
- Case CNUM: pRet-> value. c = (char) lua_tointeger (L,-1); break;
- Case INUM: pRet-> value. I = lua_tointeger (L,-1); break;
- Case LNUM: pRet-> value. l = lua_tointeger (L,-1); break;
- Case FNUM: pRet-> value. f = lua_tonumber (L,-1); break;
- Case PNUM: char * pRetTemp = (char *) malloc (strlen (lua_tostring (L,-1) + 1 );
- Strcpy (pRetTemp, lua_tostring (L,-1 ));
- PRet-> value. p = pRetTemp;
- Break; case VNUM: break; // here it is reserved for extension when necessary. }
-
- Lua_pop (L, 1 );}
-
- /*
- * Filename file name
- * Ret 1 => debug error!
- * 0 => debug success! Int DebugFile (char * filename) if (filename = NULL) return 1;
- If (NULL = L) return 1;
- Return luaL_dofile (L, filename );
- }
-
- Add. lua
-
- Function Hello ()
- Local c = a .. "yegui! ";
- Return c;
- End
-
- Local I = 3
- Local j = 4
- Local k = I + j
- Print (k );
Debugging process diagram
Debugging method for Decoda injection into the host Program
1. Put getch () in the Host Program and other pause operations seem to be unable to set breakpoints, otherwise Decoda will be abnormal. Why does this happen?) run the Host Program.
2. Select the decoda debug menu. The Processes option in.
3. Select the Host Program Attach.
4. OK
Summary: DetailsLua DebuggerThe content of code implementation has been introduced. I hope this article will help you!