# luatrace **Repository Path**: seafuture/luatrace ## Basic Information - **Project Name**: luatrace - **Description**: https://github.com/geoffleyland/luatrace - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2026-01-20 - **Last Updated**: 2026-01-20 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # luatrace - tracing, profiling and coverage for Lua ## 1. What? luatrace is a Lua module that collects information about what your code is doing and how long it takes, and can analyse that information to generate profile and coverage reports. luatrace adds a layer on top of Lua's debug hooks to make it easier to collect information for profiling and coverage analysis. luatrace traces of every line executed, not just calls. luatrace can trace through coroutine resumes and yields, and through xpcalls, pcalls and errors. On some platforms it uses high resolution timers to collect times of the order of nanoseconds. To use it, install luatrace with `sudo make install`, run your code with `lua -luatrace ` and then analyse it with `luatrace.profile`. The profiler will display a list of the top 20 functions by time, and write a copy of all the source traced annotated with times for each line. Alternatively, you can `local luatrace = require("luatrace")` and surround the code you wish to trace with `luatrace.tron()` and `luatrace.troff()`. If you wish to use the profiler directly rather than on a trace file you can use `lua -luatrace.profile ` or `local luatrace = require("luatrace.profile")`. You can pass settings to `luatrace.tron`. Probably the only interesting one is the name of the file to write the trace to, for example, you might have Lua code that calls `luatrace.tron{trace_file_name="mytrace.txt"}`. Later, you can run the profiler on this trace with the command-line `luatrace.profile mytrace.txt`. luatrace runs under "plain" Lua and LuaJIT with the -joff option (LuaJIT doesn't call hooks in compiled code, and luatrace loses track of where it's up to) luatrace is brought to you by [Incremental](http://www.incremental.co.nz/) () and is available under the [MIT Licence](http://www.opensource.org/licenses/mit-license.php). ## 2. How? luatrace is separated into two parts - the trace collector, and the backends that record and process the traces. The trace collector uses Lua's debug hooks and adds timing information and a little bit of processing to make the traces easier to use. Timing is provided in one of three ways: + Lua - with a debug hook calling `os.clock` + LuaJIT - with a debug hook calling `ffi.C.clock` - `os.clock` is not yet implemented as a fast function + Lua and LuaJIT - if the c_hook has been built then that's used instead of the Lua or LuaJIT hook. It's always better to use the C hook. By default the C hook uses the C library's `clock` and should call it closer to actual code execution, so the traces should be more accurate. On some plaforms the C hook uses a high-resolution timer: + On mach plaforms (ie OS X), the c_hook uses the `mach_absolute_time` + On Linux, it uses `clock_gettime` + On Windows, it uses `QueryPerformanceCounter` However, although the timing might be collected at nanosecond resolution, there are many reasons why profiles are not accurate to within a nanosecond! The collector outputs traces by calling a recorder's `record` function with a range of arguments: + `("S", , )` - the trace has started somewhere in a function defined on line + `(">", , )` - there's been a call to a function defined on line + `("T", , )` - there's been a tailcall to a function defined on line (LuaJIT only) + `("<")` - return from a function + `("R", )` - Resume the thread thread_id + `("Y")` - Yield + `("P")` - pcall - the current line is protected for the duration of the following call + `("E")` - Error - unwind the stack looking for a "P" + `(,