1 -- Variables ending with these names will not
2 -- have their values printed ('password' includes
3 -- 'new_password', etc.)
4 local censored_names = {
11 local function get_locals_table(level)
12 level = level + 1; -- Skip this function itself
14 for local_num = 1, math.huge do
15 local name, value = debug.getlocal(level, local_num);
16 if not name then break; end
17 table.insert(locals, { name = name, value = value });
22 local function get_upvalues_table(func)
25 for upvalue_num = 1, math.huge do
26 local name, value = debug.getupvalue(func, upvalue_num);
27 if not name then break; end
28 table.insert(upvalues, { name = name, value = value });
34 local function string_from_var_table(var_table, max_line_len, indent_str)
35 local var_string = {};
37 max_line_len = max_line_len or math.huge;
38 indent_str = "\n"..(indent_str or "");
39 for _, var in ipairs(var_table) do
40 local name, value = var.name, var.value;
41 if name:sub(1,1) ~= "(" then
42 if type(value) == "string" then
43 if censored_names[name:match("%a+$")] then
46 value = ("%q"):format(value);
49 value = tostring(value);
51 if #value > max_line_len then
52 value = value:sub(1, max_line_len-3).."…";
54 local str = ("%s = %s"):format(name, tostring(value));
55 col_pos = col_pos + #str;
56 if col_pos > max_line_len then
57 table.insert(var_string, indent_str);
60 table.insert(var_string, str);
63 if #var_string == 0 then
66 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
70 function get_traceback_table(thread, start_level)
72 for level = start_level, math.huge do
75 info = debug.getinfo(thread, level);
77 info = debug.getinfo(level);
79 if not info then break; end
81 levels[(level-start_level)+1] = {
84 locals = get_locals_table(level);
85 upvalues = get_upvalues_table(info.func);
91 function debug.traceback(...)
92 local ok, ret = pcall(debug._traceback, ...);
94 return "Error in error handling: "..ret;
99 function debug._traceback(thread, message, level)
100 if type(thread) ~= "thread" then
101 thread, message, level = coroutine.running(), thread, message;
103 if level and type(message) ~= "string" then
104 return nil, "invalid message";
105 elseif not level then
106 if type(message) == "number" then
107 level, message = message, nil;
113 message = message and (message.."\n") or "";
115 local levels = get_traceback_table(thread, level+2);
118 for nlevel, level in ipairs(levels) do
119 local info = level.info;
121 local func_type = info.namewhat.." ";
122 if func_type == " " then func_type = ""; end;
123 if info.short_src == "[C]" then
124 line = "[ C ] "..func_type.."C function "..(info.name and ("%q"):format(info.name) or "(unknown name)")
125 elseif info.what == "main" then
126 line = "[Lua] "..info.short_src.." line "..info.currentline;
128 local name = info.name or " ";
130 name = ("%q"):format(name);
132 if func_type == "global " or func_type == "local " then
133 func_type = func_type.."function ";
135 line = "[Lua] "..info.short_src.." line "..info.currentline.." in "..func_type..name.." defined on line "..info.linedefined;
138 table.insert(lines, "\t"..(nlevel==0 and ">" or " ").."("..nlevel..") "..line);
139 local npadding = (" "):rep(#tostring(nlevel));
140 local locals_str = string_from_var_table(level.locals, 65, "\t "..npadding);
142 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
144 local upvalues_str = string_from_var_table(level.upvalues, 65, "\t "..npadding);
146 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
149 return message.."stack traceback:\n"..table.concat(lines, "\n");