Remove all trailing whitespace
[prosody.git] / util / debug.lua
1 -- Variables ending with these names will not
2 -- have their values printed ('password' includes
3 -- 'new_password', etc.)
4 local censored_names = {
5         password = true;
6         passwd = true;
7         pass = true;
8         pwd = true;
9 };
10 local optimal_line_length = 65;
11
12 local termcolours = require "util.termcolours";
13 local getstring = termcolours.getstring;
14 local styles;
15 do
16         _ = termcolours.getstyle;
17         styles = {
18                 boundary_padding = _("bright");
19                 filename         = _("bright", "blue");
20                 level_num        = _("green");
21                 funcname         = _("yellow");
22                 location         = _("yellow");
23         };
24 end
25 module("debugx", package.seeall);
26
27 function get_locals_table(level)
28         level = level + 1; -- Skip this function itself
29         local locals = {};
30         for local_num = 1, math.huge do
31                 local name, value = debug.getlocal(level, local_num);
32                 if not name then break; end
33                 table.insert(locals, { name = name, value = value });
34         end
35         return locals;
36 end
37
38 function get_upvalues_table(func)
39         local upvalues = {};
40         if func then
41                 for upvalue_num = 1, math.huge do
42                         local name, value = debug.getupvalue(func, upvalue_num);
43                         if not name then break; end
44                         table.insert(upvalues, { name = name, value = value });
45                 end
46         end
47         return upvalues;
48 end
49
50 function string_from_var_table(var_table, max_line_len, indent_str)
51         local var_string = {};
52         local col_pos = 0;
53         max_line_len = max_line_len or math.huge;
54         indent_str = "\n"..(indent_str or "");
55         for _, var in ipairs(var_table) do
56                 local name, value = var.name, var.value;
57                 if name:sub(1,1) ~= "(" then
58                         if type(value) == "string" then
59                                 if censored_names[name:match("%a+$")] then
60                                         value = "<hidden>";
61                                 else
62                                         value = ("%q"):format(value);
63                                 end
64                         else
65                                 value = tostring(value);
66                         end
67                         if #value > max_line_len then
68                                 value = value:sub(1, max_line_len-3).."…";
69                         end
70                         local str = ("%s = %s"):format(name, tostring(value));
71                         col_pos = col_pos + #str;
72                         if col_pos > max_line_len then
73                                 table.insert(var_string, indent_str);
74                                 col_pos = 0;
75                         end
76                         table.insert(var_string, str);
77                 end
78         end
79         if #var_string == 0 then
80                 return nil;
81         else
82                 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
83         end
84 end
85
86 function get_traceback_table(thread, start_level)
87         local levels = {};
88         for level = start_level, math.huge do
89                 local info;
90                 if thread then
91                         info = debug.getinfo(thread, level+1);
92                 else
93                         info = debug.getinfo(level+1);
94                 end
95                 if not info then break; end
96
97                 levels[(level-start_level)+1] = {
98                         level = level;
99                         info = info;
100                         locals = get_locals_table(level+1);
101                         upvalues = get_upvalues_table(info.func);
102                 };
103         end
104         return levels;
105 end
106
107 function traceback(...)
108         local ok, ret = pcall(_traceback, ...);
109         if not ok then
110                 return "Error in error handling: "..ret;
111         end
112         return ret;
113 end
114
115 local function build_source_boundary_marker(last_source_desc)
116         local padding = string.rep("-", math.floor(((optimal_line_length - 6) - #last_source_desc)/2));
117         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 "));
118 end
119
120 function _traceback(thread, message, level)
121
122         -- Lua manual says: debug.traceback ([thread,] [message [, level]])
123         -- I fathom this to mean one of:
124         -- ()
125         -- (thread)
126         -- (message, level)
127         -- (thread, message, level)
128
129         if thread == nil then -- Defaults
130                 thread, message, level = coroutine.running(), message, level;
131         elseif type(thread) == "string" then
132                 thread, message, level = coroutine.running(), thread, message;
133         elseif type(thread) ~= "thread" then
134                 return nil; -- debug.traceback() does this
135         end
136
137         level = level or 1;
138
139         message = message and (message.."\n") or "";
140
141         -- +3 counts for this function, and the pcall() and wrapper above us
142         local levels = get_traceback_table(thread, level+3);
143
144         local last_source_desc;
145
146         local lines = {};
147         for nlevel, level in ipairs(levels) do
148                 local info = level.info;
149                 local line = "...";
150                 local func_type = info.namewhat.." ";
151                 local source_desc = (info.short_src == "[C]" and "C code") or info.short_src or "Unknown";
152                 if func_type == " " then func_type = ""; end;
153                 if info.short_src == "[C]" then
154                         line = "[ C ] "..func_type.."C function "..getstring(styles.location, (info.name and ("%q"):format(info.name) or "(unknown name)"));
155                 elseif info.what == "main" then
156                         line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline);
157                 else
158                         local name = info.name or " ";
159                         if name ~= " " then
160                                 name = ("%q"):format(name);
161                         end
162                         if func_type == "global " or func_type == "local " then
163                                 func_type = func_type.."function ";
164                         end
165                         line = "[Lua] "..getstring(styles.location, info.short_src.." line "..info.currentline).." in "..func_type..getstring(styles.funcname, name).." (defined on line "..info.linedefined..")";
166                 end
167                 if source_desc ~= last_source_desc then -- Venturing into a new source, add marker for previous
168                         last_source_desc = source_desc;
169                         table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
170                 end
171                 nlevel = nlevel-1;
172                 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
173                 local npadding = (" "):rep(#tostring(nlevel));
174                 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t            "..npadding);
175                 if locals_str then
176                         table.insert(lines, "\t    "..npadding.."Locals: "..locals_str);
177                 end
178                 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t            "..npadding);
179                 if upvalues_str then
180                         table.insert(lines, "\t    "..npadding.."Upvals: "..upvalues_str);
181                 end
182         end
183
184 --      table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
185
186         return message.."stack traceback:\n"..table.concat(lines, "\n");
187 end
188
189 function use()
190         debug.traceback = traceback;
191 end
192
193 return _M;