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