9fd0f9dd9be67a5fd028d1eed019a9fb70a98180
[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
11 local function get_locals_table(level)
12         level = level + 1; -- Skip this function itself
13         local locals = {};
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 });
18         end
19         return locals;
20 end
21
22 local function get_upvalues_table(func)
23         local upvalues = {};
24         if func then
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 });
29                 end
30         end
31         return upvalues;
32 end
33
34 local function string_from_var_table(var_table, max_line_len, indent_str)
35         local var_string = {};
36         local col_pos = 0;
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
44                                         value = "<hidden>";
45                                 else
46                                         value = ("%q"):format(value);
47                                 end
48                         else
49                                 value = tostring(value);
50                         end
51                         if #value > max_line_len then
52                                 value = value:sub(1, max_line_len-3).."…";
53                         end
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);
58                                 col_pos = 0;
59                         end
60                         table.insert(var_string, str);
61                 end
62         end
63         if #var_string == 0 then
64                 return nil;
65         else
66                 return "{ "..table.concat(var_string, ", "):gsub(indent_str..", ", indent_str).." }";
67         end
68 end
69
70 function get_traceback_table(thread, start_level)
71         local levels = {};
72         for level = start_level, math.huge do
73                 local info;
74                 if thread then
75                         info = debug.getinfo(thread, level);
76                 else
77                         info = debug.getinfo(level);
78                 end
79                 if not info then break; end
80                 
81                 levels[(level-start_level)+1] = {
82                         level = level;
83                         info = info;
84                         locals = get_locals_table(level);
85                         upvalues = get_upvalues_table(info.func);
86                 };
87         end     
88         return levels;
89 end
90
91 function debug.traceback(...)
92         local ok, ret = pcall(debug._traceback, ...);
93         if not ok then
94                 return "Error in error handling: "..ret;
95         end
96         return ret;
97 end
98
99 function debug._traceback(thread, message, level)
100         if type(thread) ~= "thread" then
101                 thread, message, level = coroutine.running(), thread, message;
102         end
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;
108                 else
109                         level = 2;
110                 end
111         end
112         
113         message = message and (message.."\n") or "";
114         
115         local levels = get_traceback_table(thread, level+2);
116         
117         local lines = {};
118         for nlevel, level in ipairs(levels) do
119                 local info = level.info;
120                 local line = "...";
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;
127                 else
128                         local name = info.name or " ";
129                         if name ~= " " then
130                                 name = ("%q"):format(name);
131                         end
132                         if func_type == "global " or func_type == "local " then
133                                 func_type = func_type.."function ";
134                         end
135                         line = "[Lua] "..info.short_src.." line "..info.currentline.." in "..func_type..name.." defined on line "..info.linedefined;
136                 end
137                 nlevel = nlevel-1;
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);
141                 if locals_str then
142                         table.insert(lines, "\t    "..npadding.."Locals: "..locals_str);
143                 end
144                 local upvalues_str = string_from_var_table(level.upvalues, 65, "\t            "..npadding);
145                 if upvalues_str then
146                         table.insert(lines, "\t    "..npadding.."Upvals: "..upvalues_str);
147                 end
148         end
149         return message.."stack traceback:\n"..table.concat(lines, "\n");
150 end