Summary
This article briefly introduces how to implement a Lua debugger. The goal of implementing the Lua debugger is simply to familiarize yourself with the Lua source code. The more powerful the Lua debugger is, the more familiar you are with the Lua source code.
Body
Previously I used Lua to write some applications. It seems that Lua is a very small language, and Lua source code is undoubtedly the first choice for language-related research. "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.
1. How is the Lua Virtual Machine suspended?
The Lua virtual machine is the same as a general 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 runs the hook before executing each Lua command. 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.
1: if ((L->hookmask & (LUA_MASKLINE | LUA_MASKCOUNT)) &&
2: (--L->hookcount == 0 || L->hookmask & LUA_MASKLINE)) {
3: traceexec (L, PC); // the corresponding hook function is executed internally.
4: If (L-> Status = lua_yield) {// does the hook function convert the status to paused?
5: L->savedpc = pc - 1;
6: return; // leave the luav_execute function here, causing the VM to pause execution.
7: }
8: base = L->base;
9: }
A method can be used to suspend the Lua virtual machine:
First, you can use the lua_sethook function to set the hook function. Generally, the Lua debugger supports single-step tracing. You can use lua_maskline hooks. Note that this hook function is triggered before executing a Lua command.
Then, modify the status of the Lua virtual machine in the hook function. You can use Lua's c Function API lua_yield. This function is used to set the status of the Lua Virtual Machine to lua_yield. This ensures that the system exits before executing the command.
2. How does the Lua Virtual Machine continue execution?
After learning how the Lua virtual machine is paused, you can easily see the following steps: first, set the status of the Lua Virtual Machine to 0 (normal), and then execute the function luav_execute. You can use Lua's c Function lua_resume to perform these two steps.
3. How to implement other functions of the Lua debugger?
Other functions, such as obtaining information from the Lua virtual machine, are easy to implement. Once the Lua virtual machine is suspended, you can query the lua_state information. The specific query depends on your familiarity with the Lua source code. It is all in lua_state and can be obtained directly.
4. How can I implement the Lua debugger?
The debugger may be a command line version or a debugger containing the interface. You can consider using the debugger as a library, and then the Library provides some interfaces to facilitate the connection with the foreground. Here are some of my encapsulated interfaces for your reference only:
1: ECode luad_init(const char * filename);
2: ECode luad_command_step(int * pErr);
3: ECode luad_command_go(int * pErr);
4: ECode luad_command_bk(int line);
5: ECode luad_command_bkinfo(int ** ppBklines, int * pNum);
6: int luad_currentline();
7: Boolean luad_is_script_ended();
This database is added with the command input control in the previous section, so it is easy to create a command line version Lua debugger. Similarly, it is easy to make an interface version. Below is the Lua debugger command line version I wrote.