Analysis of Lua debugger case implementation

Source: Internet
Author: User

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.

 
 
  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. }

Summary: AboutLua DebuggerThe implementation of the case is complete. I hope this article will help you!

Related Article

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.