詳解關(guān)于Lua調(diào)試器代碼實(shí)現(xiàn)
關(guān)于Lua調(diào)試器代碼實(shí)現(xiàn)是本文要介紹的內(nèi)容,主要是來了解LUA調(diào)試器的使用,不多說,具體內(nèi)容來看本文詳解。
1、http://www.unknownworlds.com/decoda,這個工具可以注入到宿主程序內(nèi)對lua腳本進(jìn)行調(diào)試。
2、有2種方式對lua進(jìn)行調(diào)試
從Decoda啟動宿主程序
(1)project菜單中的Settings
(2)在commond中填入你要運(yùn)行的宿主程序。點(diǎn)擊ok
(3)用它打開lua腳本設(shè)置斷點(diǎn)。Decoda 中選擇Start Debugging
下面是簡單的例子。
- 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;
- /*
- * 開啟lua虛擬機(jī)
- * ret 1 => open vm error!
- * 0 => open vm success! int startLuaDebug() L = lua_open();
- if(L == NULL) return 1;
- luaL_openlibs(L);
- return 0;
- }
- /*
- * 關(guān)閉lua虛擬機(jī) void stopLuaDebug() lua_close(L);
- }
- /*
- * funName 函數(shù)名稱
- * param[] 參數(shù)數(shù)組
- * len 參數(shù)的長度
- * bret 是否有返回結(jié)果
- * pRet 返回的結(jié)果 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) {
- // 為了便于擴(kuò)展和應(yīng)用這里不采用[ lua_type(L,lua_gettop(L)) ]而由參數(shù)指定類型
- 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; //這里留給具體要用時再去擴(kuò)展。 }
- lua_pop(L,1); }
- /*
- * filename 文件名
- * 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(a)
- local c = a .. "yegui!";
- return c;
- end
- local i = 3
- local j = 4
- local k = i + j
- print(k);
調(diào)試過程圖
Decoda注入宿主程序的調(diào)試方法
1、在宿主程序中放入getch()等暫停操作(貌似不能用設(shè)置斷點(diǎn)的方式,否則Decoda將會異常。為什么會這樣有待進(jìn)一步學(xué)習(xí)),運(yùn)行宿主程序
2、選擇decoda debug菜單。中的Processes選項(xiàng)。
3、選擇宿主程序Attach。
4、ok
小結(jié):詳解關(guān)于Lua調(diào)試器代碼實(shí)現(xiàn)的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對你有所幫助!