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