1 -- Variables ending with these names will not
2 -- have their values printed ('password' includes
3 -- 'new_password', etc.)
4 local censored_names = {
10 local optimal_line_length = 65;
12 local termcolours = require "util.termcolours";
13 local getstring = termcolours.getstring;
16 _ = termcolours.getstyle;
18 boundary_padding = _("bright");
19 filename = _("bright", "blue");
20 level_num = _("green");
21 funcname = _("yellow");
22 location = _("yellow");
25 module("debugx", package.seeall);
27 function get_locals_table(thread, level)
29 for local_num = 1, math.huge do
32 name, value = debug.getlocal(thread, level, local_num);
34 name, value = debug.getlocal(level+1, local_num);
36 if not name then break; end
37 table.insert(locals, { name = name, value = value });
42 function get_upvalues_table(func)
45 for upvalue_num = 1, math.huge do
46 local name, value = debug.getupvalue(func, upvalue_num);
47 if not name then break; end
48 table.insert(upvalues, { name = name, value = value });
54 function string_from_var_table(var_table, max_line_len, indent_str)
55 local var_string = {};
57 max_line_len = max_line_len or math.huge;
58 indent_str = "\n"..(indent_str or "");
59 for _, var in ipairs(var_table) do
60 local name, value = var.name, var.value;
61 if name:sub(1,1) ~= "(" then
62 if type(value) == "string" then
63 if censored_names[name:match("%a+$")] then
66 value = ("%q"):format(value);
69 value = tostring(value);
71 if #value > max_line_len then
72 value = value:sub(1, max_line_len-3).."…";
74 local str = ("%s = %s"):format(name, tostring(value));
75 col_pos = col_pos + #str;
76 if col_pos > max_line_len then
77 table.insert(var_string, indent_str);
80 table.insert(var_string, str);
83 if #var_string == 0 then
86 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
90 function get_traceback_table(thread, start_level)
92 for level = start_level, math.huge do
95 info = debug.getinfo(thread, level);
97 info = debug.getinfo(level+1);
99 if not info then break; end
101 levels[(level-start_level)+1] = {
104 locals = get_locals_table(thread, level+(thread and 0 or 1));
105 upvalues = get_upvalues_table(info.func);
111 function traceback(...)
112 local ok, ret = pcall(_traceback, ...);
114 return "Error in error handling: "..ret;
119 local function build_source_boundary_marker(last_source_desc)
120 local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
121 return getstring(styles.boundary_padding, "v"..padding).." "..getstring(styles.filename, last_source_desc).." "..getstring(styles.boundary_padding, padding..(#last_source_desc%2==0 and "-v" or "v "));
124 function _traceback(thread, message, level)
126 -- Lua manual says: debug.traceback ([thread,] [message [, level]])
127 -- I fathom this to mean one of:
131 -- (thread, message, level)
133 if thread == nil then -- Defaults
134 thread, message, level = coroutine.running(), message, level;
135 elseif type(thread) == "string" then
136 thread, message, level = coroutine.running(), thread, message;
137 elseif type(thread) ~= "thread" then
138 return nil; -- debug.traceback() does this
143 message = message and (message.."\n") or "";
145 -- +3 counts for this function, and the pcall() and wrapper above us, the +1... I don't know.
146 local levels = get_traceback_table(thread, level+(thread == nil and 4 or 0));
148 local last_source_desc;
151 for nlevel, level in ipairs(levels) do
152 local info = level.info;
154 local func_type = info.namewhat.." ";
155 local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
156 if func_type == " " then func_type = ""; end;
157 if info.short_src == "[C]" then
158 line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
159 elseif info.what == "main" then
160 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
162 local name = info.name or " ";
164 name = ("%q"):format(name);
166 if func_type == "global " or func_type == "local " then
167 func_type = func_type.."function ";
169 line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
171 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
172 last_source_desc = source_desc;
173 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
176 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
177 local npadding = (" "):rep(#tostring(nlevel));
179 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
181 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
184 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
186 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
190 -- table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
192 return message.."stack traceback:\n"..table.concat(lines, "\n");
196 debug.traceback = traceback;