Usage of the Lua built-in Debugger

Source: Internet
Author: User

Usage of the Lua built-in Debugger

This article mainly introduces Lua's built-in debugger usage, which is very useful in programming debug operations. For more information, see

Lua provides a debugging library that provides all the basic functions to create its own debugger. Even so, there is no built-in Lua debugger. Lua creates many open-source debuggers for developers.

The functions available in the debugging Lua library are listed in the following table and their usage.

We often use the complete list of Lua debugging functions in the above list. We use the above functions and provide a more convenient debugging library. Using these functions is rather complicated than creating your own debugger. In any case, we will see examples of using simple debugging functions.

The Code is as follows:

Function myfunction ()

Print (debug. traceback ("Stack trace "))

Print (debug. getinfo (1 ))

Print ("Stack trace end ")

Return 10

End

Myfunction ()

Print (debug. getinfo (1 ))

When we run the above program, we will get stack trace information, as shown in.

The Code is as follows:

Stack trace

Stack traceback:

Test2.lua: 2: in function 'myfunction'

Test2.lua: 8: in main chunk

[C]:?

Table: 0054C6C8

Stack trace end

In the preceding example, stack tracing is printed by using the debug. trace function available in the debug library. Debug. getinfo to get the current table of the function.

Another example

We often need to know the local variables of a function for debugging. Therefore, you can use setupvalue to set and use getupvalue to obtain these local variables ,. A simple example is as follows.

The Code is as follows:

Function newCounter ()

Local n = 0

Local k = 0

Return function ()

K = n

N = n + 1

Return n

End

End

Counter = newCounter ()

Print (counter ())

Print (counter ())

Local I = 1

Repeat

Name, val = debug. getupvalue (counter, I)

If name then

Print ("index", I, name, "=", val)

If (name = "n") then

Debug. setupvalue (counter, 2, 10)

End

I = I + 1

End -- if

Until not name

Print (counter ())

When we run the above program, we will get the following output.

The Code is as follows:

1

2

Index 1 k = 1

Index 2 n = 2

11

In this example, each counter is updated when it is called. You can use the getupvalue function to obtain the current state of a local variable. Then, set a new value for the local variable. Here, n is set to 2 before the operation is called. Use the setupvalue function to update to 10. When the counter function is called, it returns 11 instead of 3.

Debugging type

Command Line Debugging

Graphical debugging

Command Line Debugging

Command Line Debugging is a debugging type that uses command lines and print reports for debugging. Many Command Line debuggers available for Lua are listed below.

RemDebug: RemDebug is a remote debugger in Lua5.0 and 5.1. It can remotely control the execution of another Lua program and set the breakpoint and check the current state of the program. RemDebug can also debug the CGILua script.

Clidebugger: A simple command line interface debugging program of lua5.1 is written in pure Lua. It does not rely on any other library than standard Lua5.1. It is inspired by RemDebug, but does not have its remote device.

Ctrace: a tool that tracks Lua API calls.

XdbLua: A simple Lua command line debugger on Windows.

LuaInterface-Debugger: This project is the extended LuaInterface Debugger. It proposes to build the Lua debugging interface to a higher level. Interaction with the debugger is completed by calling events and methods.

Rldb: this is through the socket remote LUA debugger, applicable to Windows and Linux. It gives you more features than any existing one.

ModDebug: This allows you to remotely control the execution of other Lua programs, set breakpoints, and check the current state of the programs.

Graphical debugging

Graphical debugging provides visual debugging for various states such as variable values, stack trace information, and other related information with IDE. There is a visual representation that the breakpoint is executed step by step with the help of the next step control, step by step, skip and other buttons in IDE.

Number of graphical Lua debuggers, which includes the following content.

SciTE: by default, Windows IDE Lua provides a variety of debugging tools, such as breakpoint, step by step, step by step, skip, and view variables.

Decoda: This is a remote debugging tool that supports graphical debuggers.

ZeroBrane Studio: Lua IDE integrates the remote debugger, stack view, table view, remote console, and static analyzer. Engineering with LuaJIT, Love2d, Moai, and other Lua engines. It is open-source in Windows, OSX, and Linux.

Akdebugger: Eclipse plug-in-debugger and editor of Lua.

Luaedit: remote debugging, local debugging, syntax highlighting, Automatic completion suggestion list, parameter proposition engine, breakpoint management (including breakpoint Air Conditioning System and hit count), function list, global variables and local variables list, view and solve problems-oriented management

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.