Koneki/LDT/Developer Area/User Guides/User Guide 0.8
The LDT project provides plug-ins that implement a Lua IDE supporting the development of Lua scripts and applications. It adds a dedicated Lua perspective to the Eclipse Workbench, which, together with features such as syntax highlighting, scope-aware code completion, code folding, etc. allows to structure Lua application better (project-based organization), and boosts the productivity of Lua developers.
Create a project
For your code to be manipulated correctly by Lua Development Tools, it must be contained into an Eclipse project having a Lua flavour. In order to do this, you have to create a Lua Project, it will add Lua nature to your code.
In "File > New > Project...", select the right project type: Lua Project.
Enter a valid name, choose your Execution Environment, press "Finish", and voila!
Note: You might want to configure an Execution Environment.
Edit a Lua source file
Now that your project is created, you are ready to start working on your Lua scripts. In every project, there is a default source folder, named src. You can edit the main.lua file in it.
Source folders are imported, as they are the one processed to compute tooling information. That is to say if you write a module outside of a source folder, it will not be part of the accessible scope when you will try to reference it from another module or script ; thus, its content will not be available in e.g. code completion.
Before diving into the concepts of LDT, if you need more information about the concepts of Lua itself, you are highly encouraged to refer to the Lua manual (http://www.lua.org/manual/5.1/).
The Debugger of Lua Development Tools is based on the DBGp protocol, and the IDE implements a DBGp server.
To connect to this server, and begin remote/attach debugging, you need to use a DBGp Lua client.
The DBGp Lua client is composed of two Lua files and can be downloaded here.
It runs on Unix-like OS and Windows (XP and later). It is written in Lua 5.1 and depends on lua-socket.
You can get Lua on http://www.lua.org/download.html and install lua-socket thanks to luarocks, or via your official OS repositories.
To use it, you must have these two files in your Lua path.
To begin the connection, you must execute this Lua code :
> local initconnection = require("debugger") > initconnection(host, port, idekey)
which can be shortened like this:
> require("debugger")(host, port, idekey)
- host: the host name or the IP address of the DBGp server (thus, of your IDE).
- if host is nil, the DBGP_IDEHOST environment variable is used.
- if the environment variable is nil, the default value '127.0.0.1' is used.
- port: the port of the DBGp server (must be configured in the IDE).
- if port is nil, the DBGP_IDEPORT environment variable is used.
- if the environment variable is nil, the default value '10000' is used.
- idekey: a string which is used as session key (must be configured in the IDE).
- if IDEKEY is nil, the DBGP_IDEKEY environment variable is used.
- if the environment variable is nil, the default value 'luaidekey' is used.
So, to debug any Lua script, you can do:
lua -e "require('debugger')("idehost","ideport");" MyApp.lua
or even just go with:
lua -e "require('debugger')();" MyApp.lua
if you want to rely on default values for the debug host and port (which should be 127.0.0.1:10000 if you didn't tweak any DBGP_* environment variable).
The DBGp Server is integrated in LDT.
In order to accept incoming debug sessions, you must create a new Lua Attach to Application launch configuration, then launch it.
Go in Run/Debug Configurations....
- Project : Set the LDT project in your workspace which includes the Lua source file(s) of the application you want to debug.
- IdeKey : Default value is luaidekey, if you need to debug more than one application at the same time, you should change it to associate a launch configuration with only one application to debug.
- Source Mapping : Define a common way for DBGp Server (IDE) and DBGp Client (running application) to identify source file. There are several strategy, each more or less adapted to a specific used case. To better understand it, see the advanced documentation on it.
Now you can start your debug session by clicking Debug. IDE will wait for an incoming connection from the debugger client, on the port you can see in the debug view. By default the port used is 10000, but if it is taken, another one may be used.
If needed, you can change the server port, in Window > Preferences > Dynamic Languages > Debug.
In order to use propoperly remote debugging, you must care about paths. In our case, there are several kind of them to consider:
- Project path
- Runtime path
Project path is the one set using the Build Path > Configure Build Path ... UI. From there you can specify dependency to other project from current workspace. As a result, will enjoy completion and navigation between your project and the ones configured in Build Path.
Once your program is ready, you might try Remote debugging. You must ensure beforehand that all the projects you depend on are in Lua path. The configuration of this path is not detailed here since it is highly dependent on how you are managing paths in your own environment.
An Execution Environment, which may also be referred to as EE, is file containing information about the environment where a Lua application could run. An environment is a set of libraries available to the application out of the box, as in Lua 5.1 or Corona SDK.
The idea is to provide a file capable to describe elements of this environment. All provided information allows LDT to offer better code completion, code navigation and documentation layout in LDT.
Manage Execution Environments
To find preferences related to EE, go to Window > Preferences > Lua > Execution Environments and you will have following interface.
From there, is it possible to add an EE to current workspace by pressing Add, then select an Execution Environment.
The EE is now available.
Link an existing project to an Execution Environment
Once you have manage to install Execution Environments in your workspace, it is time to use them in a project. Right click a project and select Build Path >> Add Libraries ....
You will have to choose between several libraries types, choose Lua Execution Environment.
You can now choose among installed EEs and press OK.
It is now linked to selected project.
Debugging a Lua program
You can set breakpoints at a particular file/line, you can do it with the regular double-click on margin:
You can optionally indicate conditions to stop execution only under specific circumstances:
- Enable or disable breakpoint globally
- Condition on hit count (stop only after the 3rd hit, every 4 hits, etc.)
- Conditional breakpoint: you can put any expression, it will be evaluated using the local scope every time the breakpoint is hit, and stop only when the expression evaluates to true.
Once a breakpoint has been hit, and the execution has actually stopped, you can use the Step Into, Step Over and Step Return commands.
When a breakpoint is reached, you can see any variable visible from any stack frame (local, upvalue and global variables). You can also change values to another.
Some special values can also be displayed such as metatables or function environments (if it is different from global environment). You can also change these values.
Interactive console and expressions
In addition to variable view, you have two other useful tools to evaluate some code snippets: expressions view and interactive console.
The DBGp Server (IDE) and the DBGp client (running application) need to communicate about source files.
E.g. When you set a breakpoint, the IDE need to say to the running application on which file the breakpoint must be added.
E.g. When the running application stops on a breakpoint of a file, the IDE must retrieve the file and open it.
The problem is that the file executed in your Lua VM could be physically different than the source file in your IDE (in your workspace). For example, in the case where your code is executed on a different host or just if your executed code is duplicated in another folder.
To resolve this problem, LDT proposes to you different strategies, each with advantages and drawbacks :
- Local Resolution
This way to resolve the source mapping is the more simple and the more reliable. Both client and server works with absolute path. The IDE will search only in its buildpath a file which have the given absolute path. This means that executed file should be in your workspace (in your buildpath more exactly). So you can not use it to debug code on a remote host.
- Module Resolution
Both IDE and application have their own way to retrieve a module from its name. So we could use the module name has file ID instead of path.
With this mode, you could do remote debugging without settin a list of path mapping.
The limitation is that you should use the standard lua_path way to load module.
Another problem is that we could not really retrieve the module name at client side, because the Lua debug api uses source path. The module name is retrieved from the path (from debug.getinfo) and the lua_path (package.path). so with ambiguous lua_path you could have insolvable conflict.
package.path = "/foo/bar/?.lua, /foo/?.lua" and
debug.getinfo="@/foo/bar/baz.lua" the possible module name is bar.baz or baz
There are no way to know the real module name, in this case the debugger will use the shorter one, but the ideal is to avoid ambiguous lua_path with this mode.
- Replace path Resolution
This mode is a fallback. If the two previous one don't fit your needs, you could try it.
In this mode, client talks with absolute path and server (IDE) uses relative path (relative to the buildpath).
A path must be set in launchconfiguration to move from one world to another.
e.g: You set path with "/foo/bar/".
when file path is sent from client (/foo/bar/baz/qux.lua), path will be removed (baz/qux.lua) and a file with this relative path will be searched in your buildpath.
when file path is sent from server (baz/qux.lua), path will be add (/foo/bar/baz/qux.lua) and the absolute path will be sent to the client
The problem is that you could set only one path.
The path comparison is case sensitive (even on windows).
On windows, you should prefix your path with "/" (e.g. /C:/foo/bar/)
In all case, if file is not found in the workspace, the source code is sent via the DBGp protocol
The dynamic code is not supported, it means that any code that is loaded with load, loadstring will not be supported. The debugger will step over it just like a C function.
The debugger is officially supported for Lua VM 5.1. Any other implementation (Lua 5.2, LuaJIT, ...) is not guaranteed to work at this time.