Giter Club home page Giter Club logo

stacktraceplus's Introduction

StackTracePlus

Build Status Coverage Status

StackTracePlus provides enhanced stack traces for Lua 5.1, Lua 5.2, Lua 5.3, LuaJIT and OpenResty.

StackTracePlus can be used as a replacement for debug.traceback. It gives detailed information about locals, tries to guess function names when they're not available, etc, so, instead of

lua5.1.exe: D:\trunk_git\sources\stacktraceplus\test\test.lua:10: attempt to concatenate a nil value
stack traceback:
	D:\trunk_git\sources\stacktraceplus\test\test.lua:10: in function <D:\trunk_git\sources\stacktraceplus\test\test.lua:7>
	(tail call): ?
	D:\trunk_git\sources\stacktraceplus\test\test.lua:15: in main chunk
	[C]: ?

you'll get

lua5.1.exe: D:\trunk_git\sources\stacktraceplus\test\test.lua:10: attempt to concatenate a nil value
Stack Traceback
===============
(2)  C function 'function: 00A8F418'
(3) Lua function 'g' at file 'D:\trunk_git\sources\stacktraceplus\test\test.lua:10' (best guess)
	Local variables:
	 fun = table module
	 str = string: "hey"
	 tb = table: 027DCBE0  {dummy:1, blah:true, foo:bar}
	 (*temporary) = nil
	 (*temporary) = string: "text"
	 (*temporary) = string: "attempt to concatenate a nil value"
(4) tail call
(5) main chunk of file 'D:\trunk_git\sources\stacktraceplus\test\test.lua' at line 15
(6)  C function 'function: 002CA480'

Usage

StackTracePlus can be used as a replacement for debug.traceback, as an xpcall error handler or even from C code. Note that only the Lua 5.1 interpreter and OpenResty allows the traceback function to be replaced "on the fly". Interpreters for LuaJIT, Lua 5.2 and 5.3 always calls luaL_traceback internally so there is no easy way to override that.

local STP = require "StackTracePlus"

debug.traceback = STP.stacktrace
function test()
	local s = "this is a string"
	local n = 42
	local t = { foo = "bar" }
	local co = coroutine
	local cr = coroutine.create
	
	error("an error")
end
test()

That script will output (only with Lua 5.1):

lua5.1: example.lua:11: an error
Stack Traceback
===============
(2)  C function 'function: 006B5758'
(3) global C function 'error'
(4) Lua global 'test' at file 'example.lua:11'
        Local variables:
         s = string: "this is a string"
         n = number: 42
         t = table: 006E5220  {foo:bar}
         co = coroutine table
         cr = C function: 003C7080
(5) main chunk of file 'example.lua' at line 14
(6)  C function 'function: 00637B30'

StackTracePlus is aware of the usual Lua libraries, like coroutine, table, string, io, etc and functions like print, pcall, assert, and so on.

You can also make STP aware of your own tables and functions by calling add_known_function and add_known_table.

local STP = require "StackTracePlus"

debug.traceback = STP.stacktrace
local my_table = {
	f = function() end
}
function my_function()
end

function test(data, func)
	local s = "this is a string"
	
	error("an error")
end

STP.add_known_table(my_table, "A description for my_table")
STP.add_known_function(my_function, "A description for my_function")

test( my_table, my_function )

Will output:

lua5.1: ..\test\example2.lua:13: an error
Stack Traceback
===============
(2)  C function 'function: 0073AAA8'
(3) global C function 'error'
(4) Lua global 'test' at file '..\test\example2.lua:13'
        Local variables:
         data = A description for my_table
         func = Lua function 'A description for my_function' (defined at line 7 of chunk ..\test\example2.lua)
         s = string: "this is a string"
(5) main chunk of file '..\test\example2.lua' at line 19
(6)  C function 'function: 00317B30'

Installation

The easiest way to install is with LuaRocks.

  • luarocks install stacktraceplus

If you don't want to use LuaRocks, just copy StackTracePlus.lua to Lua's path.

License

StackTracePlus is available under the MIT license.

stacktraceplus's People

Contributors

alexdowad avatar eykamp avatar hishamhm avatar ignacio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

stacktraceplus's Issues

Allow user to set a limit on size of local variables which will be printed

@ignacio, I really like this library and am using it in production.

The other day there was an incident where a user was trying to upload a large file. This caused an error to be raised in the Lua code... and since StackTracePlus was in use, and the content of the file was in a local variable... it logged the entire contents of the file.

We would like to avoid log entries which are dozens or hundreds of MB long. I am wondering if StackTracePlus could be enhanced to allow setting a maximum string length which will be logged (perhaps also maximum table size/depth would also be good).

If a string is too long, I am thinking it could be printed something like:

"contents contents contents..." (truncated at XYZ bytes)

If you like this idea, I am happy to implement it.

STP.stacktrace not strictly compatible to debug.traceback

STP.stacktrace is not strictly compatible to debug.traceback: It returns two values, instead of one. This creates problems in conjunction with varargs functions and those created through the Lua C API (which will happily consume the additional argument, creating an unexpected stack layout).

Is this something you want to fix?

--- a/src/StackTracePlus.lua
+++ b/src/StackTracePlus.lua
@@ -391,7 +392,7 @@ Stack Traceback
                info = dumper.getinfo(level, "nSlf")
        end

-       return dumper:concat_lines(), original_error
+       return dumper:concat_lines()
 end

 --

Doesn't work with LuaJIT

Hi Ignacio,

maybe I'm doing something wrong but the library doesn't seem to work with LuaJIT for me. I used the simple script from README example:

local STP = require "StackTracePlus"

debug.traceback = STP.stacktrace
function test()
    local s = "this is a string"
    local n = 42
    local t = { foo = "bar" }
    local co = coroutine
    local cr = coroutine.create

    error("an error")
end
test()

When I run the file in LuaJIT I see the default error output:

$ luajit -v
LuaJIT 2.0.2 -- Copyright (C) 2005-2013 Mike Pall. http://luajit.org
$ luajit stptest.lua 
luajit: stptest.lua:11: an error
stack traceback:
    [C]: in function 'error'
    stptest.lua:11: in function 'test'
    stptest.lua:13: in main chunk
    [C]: at 0x00404ce0

However, running it with regular Lua 5.1, it see the pretty output:

$ lua -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
$ lua stptest.lua 
lua: stptest.lua:11: an error
Stack Traceback
===============
(2) global C function 'error'
(3) Lua global 'test' at file 'stptest.lua:11'
    Local variables:
     s = string: "this is a string"
     n = number: 42
     t = table: 0xfb5760  {foo:bar}
     co = coroutine module
     cr = C function: 0xfa42e0
(4) main chunk of file 'stptest.lua' at line 13
(5)  C function 'function: 0xfa41a0'

Dump locals of C functions

STP currently does not dump locals of C functions. When debugging those written using the Lua C API, it sometimes happens that the stacks looks different from what I expected. In these cases I would love to use STP to tell me about the stack.

The fix is simple:

--- a/src/StackTracePlus.lua
+++ b/src/StackTracePlus.lua
@@ -355,6 +355,7 @@ Stack Traceback
                        local function_name = m_user_known_functions[info.func] or m_known_functions[info.func] or info.name or tostring(info.func)
                        dumper:add_f("(%d) %s C function '%s'\r\n", level_to_show, info.namewhat, function_name)
                        --dumper:add_f("%s%s = C %s\r\n", prefix, name, (m_known_functions[value] and ("function: " .. m_known_functions[value]) or tostring(value)))
+                       dumper:DumpLocals(level)
                elseif info.what == "tail" then
                        --print("tail")
                        --for k,v in pairs(info) do print(k,v, type(v)) end--print(info.namewhat, info.name)

Error in error handling

I can't investigate it deeper right now, but there seems to be some edge case probably caused by weird metamethods or references.

I tagged the relevant commit in my host code here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.