Detailed description of Lua debugger code implementation

Source: Internet
Author: User

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.

 
 
  1. Main. cpp
  2.  
  3. # Include <iostream>
  4. # Include "luaDebug. h"
  5.  
  6. Using namespace std;
  7.  
  8. Int main () startLuaDebug ();
  9. DebugFile ("add. lua ");
  10. ParamData in [1];
  11. ParamData out;
  12. In [0]. tt = PNUM; in [0]. value. p = "HELLO :";
  13. Out. tt = PNUM;
  14.  
  15. DebugFunction ("Hello", in, 1, 1, & out );
  16.  
  17. StopLuaDebug ();
  18.  
  19. Printf ("% s \ n", out. value. p );
  20. System ("pause ");
  21. Return 0;
  22. }
  23.  
  24. LuaDebug. h
  25.  
  26. # Ifndef LUA_DEBUG_H
  27. # Define LUA_DEBUG_H
  28.  
  29. Enum tt nil, // null
  30. BNUM, // boolean
  31. CNUM, // char
  32. INUM, // int
  33. LNUM, // long
  34. FNUM, // float | double
  35. PNUM, // char *
  36. VNUM // void *
  37. };
  38.  
  39. Typedef union ParamValue bool B;
  40. Char c;
  41. Int I;
  42. Long l;
  43. Float f;
  44. Char * p;
  45. Void * v;
  46. } ParamValue;
  47.  
  48. Typedef struct ParamData int tt;
  49. ParamValue value;
  50. } ParamData;
  51.  
  52. Int startLuaDebug ();
  53. Void stopLuaDebug ();
  54. Int DebugFile (char * filename );
  55. Void DebugFunction (char * funName,
  56. ParamData param [],
  57. Int len,
  58. Bool bret,
  59. ParamData * pRet
  60. );
  61.  
  62. # Endif
  63.  
  64. LuaDebug. cpp
  65.  
  66. # Include <stdio. h>
  67. # Include <iostream>
  68. # Include <stdlib. h>
  69. # Include "lua. hpp"
  70. # Include "luaDebug. h"
  71.  
  72. Lua_State * L;
  73.  
  74. /*
  75. * Enable lua Virtual Machine
  76. * Ret 1 => open vm error!
  77. * 0 => open vm success! Int startLuaDebug () L = lua_open ();
  78. If (L = NULL) return 1;
  79. LuaL_openlibs (L );
  80. Return 0;
  81. }
  82.  
  83. /*
  84. * Shut down the lua Virtual Machine void stopLuaDebug () lua_close (L );
  85. }
  86.  
  87. /*
  88. * FunName function name
  89. * Param [] parameter Array
  90. * Len parameter length
  91. * Whether bret has returned results
  92. * Void DebugFunction (char * funName,
  93. ParamData param [],
  94. Int len,
  95. Bool bret,
  96. ParamData * pRet {
  97. If (NULL = L | funName = NULL) return;
  98. Lua_getglobal (L, funName );
  99. For (int I = 0; I <len; I ++ ){
  100. Switch (param [I]. tt ){
  101. Case BNUM:
  102. Lua_pushboolean (L, param [I]. value. B );
  103. Break;
  104. Case CNUM:
  105. Lua_pushinteger (L, (int) param [I]. value. c );
  106. Break;
  107. Case INUM:
  108. Lua_pushinteger (L, param [I]. value. I );
  109. Break;
  110. Case LNUM:
  111. Lua_pushinteger (L, param [I]. value. l );
  112. Break;
  113. Case FNUM:
  114. Lua_pushnumber (L, param [I]. value. f );
  115. Break;
  116. Case PNUM: lua_pushstring (L, param [I]. value. p );
  117. Break; case VNUM: lua_pushlightuserdata (L, param [I]. value. v );
  118. Break ;}
  119. } Lua_call (L, len, (int) bret );
  120. If (bret ){
  121. If (pRet! = NULL ){
  122. // For convenience of expansion and application, [lua_type (L, lua_gettop (L)] is not used here, And the type is specified by the Parameter
  123. Switch (pRet-> tt ){
  124. Case BNUM: pRet-> value. B = lua_toboolean (L,-1); break;
  125. Case CNUM: pRet-> value. c = (char) lua_tointeger (L,-1); break;
  126. Case INUM: pRet-> value. I = lua_tointeger (L,-1); break;
  127. Case LNUM: pRet-> value. l = lua_tointeger (L,-1); break;
  128. Case FNUM: pRet-> value. f = lua_tonumber (L,-1); break;
  129. Case PNUM: char * pRetTemp = (char *) malloc (strlen (lua_tostring (L,-1) + 1 );
  130. Strcpy (pRetTemp, lua_tostring (L,-1 ));
  131. PRet-> value. p = pRetTemp;
  132. Break; case VNUM: break; // here it is reserved for extension when necessary. }
  133.  
  134. Lua_pop (L, 1 );}
  135.  
  136. /*
  137. * Filename file name
  138. * Ret 1 => debug error!
  139. * 0 => debug success! Int DebugFile (char * filename) if (filename = NULL) return 1;
  140. If (NULL = L) return 1;
  141. Return luaL_dofile (L, filename );
  142. }
  143.  
  144. Add. lua
  145.  
  146. Function Hello ()
  147. Local c = a .. "yegui! ";
  148. Return c;
  149. End
  150.  
  151. Local I = 3
  152. Local j = 4
  153. Local k = I + j
  154. 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.