1. http://www.unknownworlds.com/decoda. this tool can be injected to the host.ProgramTo debug the Lua script.
2. There are two ways to debug Lua
Start the Host Program from decoda
A. settings in the project menu
B. Enter the host program you want to run in commond. Click OK
C. 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
* Result returned by pret
*/
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 () and other pause operations in the host Program (it seems that you cannot set the breakpoint; 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