This article introduces how to implementLua Debugger, ImplementationLua DebuggerOnly hope to familiarize yourself with thisLuaSource code. TheLua DebuggerThe more powerful the function, the moreLuaThe more you understand the source code.
Previously UsedLuaAfter writing some applications, I feel that Lua is a very small language, and Lua source code is undoubtedly the first choice for studying language-related. "Lua is small and dirty "! To study the Lua source code, we plan to write a simple Lua debugger and find that there are still some gains, as shown below.
As a debugger, it should support some of the simplest and most commonly used functions, such as single-step tracking, output debugging information, and breakpoint setting. If you want to explore how to implement the Lua debugger, find the answers to these questions. The Development Environment used in this article is: win7, lua 5.1.4 source code.
How is the Lua Virtual Machine paused?
LuaA virtual machine is the same as a common CPU. It consists of a data storage zone and a logical control zone. The data storage area corresponds to the CPU registers and states. In Lua, it is actually lua_State. The logical control area corresponds to the specific implementation of each CPU command. The source code of the Lua Virtual Machine logical control area is located in lvm. c. The luaV_execute function is used to execute the Lua command.
To facilitate debugging, The luaV_execute function checks whether there is a debugging hook before executing each Lua command.): If yes, execute the hook. Then, judge whether the status of the Lua virtual machine is paused. If so, return without executing the current Lua command. If no debugging hook exists, the Lua command is executed normally.
- If (L-> hookmask & (LUA_MASKLINE | LUA_MASKCOUNT ))&&
- (-- L-> hookcount = 0 | L-> hookmask & LUA_MASKLINE )){
- Traceexec (L, pc); // the corresponding hook function is executed internally.
- If (L-> status = LUA_YIELD) {// does the hook function convert the status to paused?
- L-> savedpc = pc-1;
- Return; // leave the luaV_execute function here, causing the VM to pause execution.
- }
- Base = L-> base;
- }
Summary: AboutLua DebuggerThe implementation of the case is complete. I hope this article will help you!