Decoda tutorial Lua debugger powerful Lua debugging tool

Source: Internet
Author: User

Http://www.unknownworlds.com/decoda

 

Today, colleagues in the project team raised the need for the entire Lua debugging tool. Due to the increasing number of Lua scripts used in recent projects, the chance of script problems is also increasing ~~ . Debugging is not very convenient, so it is necessary to complete one. So we first found a runner to meet the needs of the project. Next, I will post the software help written by a friend on the Internet. Thank you!

Decoda tutorial 1: Start the Host Program from decoda to debug Lua
I have been paying attention to it since the release of decoda. This tool can be injected into the host Program to debug the Lua script. It can also set the breakpoint to observe the variable value, which is very powerful.

The following describes how to use it.

First, find a program that uses Lua. Here I use Wireshark as an example. This is a very powerful network packet capture tool, and Wireshark uses Lua internally. If Wireshark is not available, Google it and download and install it. This is an open-source software.

There are two methods for debugging the host program using Lua: one is to start the Host Program from decoda, and the other is to start the Host Program first and then inject it with decoda.

This article describes how to start a Host Program from decoda

And click OK.

At this time, the wireshark program starts to run. Wait a moment and a init. Lua file will appear in the window on the left. This is the Lua file used by Wireshark. (Decoda can detect all the Lua files used by the program and display them in the left-side window)

Now you can. when a breakpoint is set in Lua, the breakpoint is displayed in disable_lua = true; do return end; this breakpoint can be seen by smart people. Only breakpoint can be set here, because the following statements are not executed unless you comment this sentence. Select Stop debugging in the Debug menu to exit debugging, and then select start debugging to restart debugging. At this time, the program will be directed to the line where you set the breakpoint.

The whole process is like this. You can enter the variables to be monitored in watch. These operations are the same as those in Visual Studio.

Decoda tutorial 1: Start the Host Program from decoda to debug Lua
I have been paying attention to it since the release of decoda. This tool can be injected into the host Program to debug the Lua script. It can also set the breakpoint to observe the variable value, which is very powerful.

The following describes how to use it.

First, find a program that uses Lua. Here I use Wireshark as an example. This is a very powerful network packet capture tool, and Wireshark uses Lua internally. If Wireshark is not available, Google it and download and install it. This is an open-source software.

There are two methods for debugging the host program using Lua: one is to start the Host Program from decoda, and the other is to start the Host Program first and then inject it with decoda.

This article describes how to start a Host Program from decoda

And click OK.

At this time, the wireshark program starts to run. Wait a moment and a init. Lua file will appear in the window on the left. This is the Lua file used by Wireshark. (Decoda can detect all the Lua files used by the program and display them in the left-side window)

Now you can. when a breakpoint is set in Lua, the breakpoint is displayed in disable_lua = true; do return end; this breakpoint can be seen by smart people. Only breakpoint can be set here, because the following statements are not executed unless you comment this sentence. Select Stop debugging in the Debug menu to exit debugging, and then select start debugging to restart debugging. At this time, the program will be directed to the line where you set the breakpoint.

The whole process is like this. You can enter the variables to be monitored in watch. These operations are the same as those in Visual Studio.

 

Decoda tutorial 2: debugging method for decoda injection into the host Program

The previous article introduced how to start the Host Program from decoda to debug Lua. The only drawback of this method is that you cannot debug the Host Program in Visual Studio. We may often encounter situations where both the host Program and script are intended to be debugged. The last tutorial method is definitely not enough. This article introduces the debugging method of decoda injection for started Host programs, so that we can debug the Host Program in Visual Studio and then use decoda to inject the Host Program to debug the Lua script. (The Host Program cannot be injected when it is debugged. It must be injected when the host Program is executed. Otherwise, both Visual Studio and decoda will die)

The wireshark software we used last demonstrated that this software was initiated by calling an init. Lua for some initialization operations. Now we need a program that does not execute the Lua script immediately after running, but manually executes the Lua script, so that I can start the program first, then inject the debug DLL file of decoda to the Host Program to debug the Lua script.

It is too troublesome to find a ready-made program. I wrote a super simple program with the following code: Main. cpp, script: Add. Lua,

File: Main. cpp

Extern "C"
{
# Include "Lua. H"
# Include "lualib. H"
# Include "lauxlib. H"
}

# Include <stdio. h>
# Include <conio. h>
Using namespace STD;

Int main ()
{
Lua_state * l = lua_open (); // initialize Lua
Lual_openlibs (l); // load all the Lua standard Libraries

Printf ("press any key to run Lua file./N ");
Getch ();

Lual_dofile (L, "add. Lua"); // execute Add. Lua and add the Add function to the lua_state stack.

Lua_getglobal (L, "add"); // use the function name to extract the function address and press it into the stack.

Lua_pushnumber (L, 10); // press the first parameter into the stack.

Lua_pushnumber (L, 10); // press the second parameter into the stack.

Lua_call (l, 2, 1); // call the Add function

Int sum = (INT) lua_tonumber (L,-1); // retrieves the function execution result from the lua_state stack.

Lua_pop (L, 1); // bring the result to the stack.

Printf ("the sum is % d/N", sum );
Getch ();

Return 1;
}

File: Add. Lua

Function add (x, y)
Return x + y
End

It doesn't matter if you are not a programmer and won't compile it. I have uploaded the compiled executable file and Lua script to SkyDrive. You can download the required files. The link is here: decodatutorial2_demo.

After the job is finished, execute the decompressed test.exe file. The program stops here. Do not press any key in this case! Because we haven't injected decoda into this program yet, what we need to do is wait for decoda injection and then execute the Add. Lua script for debugging.

Run decoda now and select debug-> processes.

The dialog box shows all the processes of the previous system. select our test.exe process and press the attach button.

Well, if RP (character type) is normal, you should see the following output in the lower left corner, indicating that the injection is successful.

Now we know the ADD. lua script, so we can directly find and open this script file in decoda. If we do not know, we can execute the program first, after the program is executed, all Lua files executed by the program will be listed in the window on the left of decoda. I have a breakpoint in all three lines of code because the Host Program will be disconnected once during lual_dofile and the start code of the function will be broken directly when the Add function is called, we can observe the order in which the yellow arrow appears in the code line.

 

Now we can press any keyto let our test.exe execute the Add. Lua script!

After you press any key, you will find that the decoda breakpoint is in the end line. We perform this step in one step, and it is in the function add (x, y) line, the end line is displayed in one step. The next one-step execution will go to the return x + y line, when the host program calls the Add function

Lua_call (l, 2, 1); // call the Add function

In this case, you can view the values of the X and Y variables in the Lua script, find the watch window, double-click the blank space in the name column, enter the variable name X, press enter, OK, you can see the value and type of your X variable.

 

Press f5 to restore test.exe. Okay. The whole process is over.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cyblueboy83/archive/2010/01/01/5116140.aspx

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.