Comparison

util/debug.lua @ 5785:b2d6c5f94aa5

util.debug: Fixes to make coroutine tracebacks work properly
author Matthew Wild <mwild1@gmail.com>
date Sat, 10 Aug 2013 20:30:40 +0100
parent 5776:bd0ff8ae98a8
child 5786:d50005796a26
comparison
equal deleted inserted replaced
5784:02217725454b 5785:b2d6c5f94aa5
86 function get_traceback_table(thread, start_level) 86 function get_traceback_table(thread, start_level)
87 local levels = {}; 87 local levels = {};
88 for level = start_level, math.huge do 88 for level = start_level, math.huge do
89 local info; 89 local info;
90 if thread then 90 if thread then
91 info = debug.getinfo(thread, level+1); 91 info = debug.getinfo(thread, level);
92 else 92 else
93 info = debug.getinfo(level+1); 93 info = debug.getinfo(level+1);
94 end 94 end
95 if not info then break; end 95 if not info then break; end
96 96
97 levels[(level-start_level)+1] = { 97 levels[(level-start_level)+1] = {
98 level = level; 98 level = level;
99 info = info; 99 info = info;
100 locals = get_locals_table(level+1); 100 locals = not thread and get_locals_table(level+1);
101 upvalues = get_upvalues_table(info.func); 101 upvalues = get_upvalues_table(info.func);
102 }; 102 };
103 end 103 end
104 return levels; 104 return levels;
105 end 105 end
132 thread, message, level = coroutine.running(), thread, message; 132 thread, message, level = coroutine.running(), thread, message;
133 elseif type(thread) ~= "thread" then 133 elseif type(thread) ~= "thread" then
134 return nil; -- debug.traceback() does this 134 return nil; -- debug.traceback() does this
135 end 135 end
136 136
137 level = level or 1; 137 level = level or 0;
138 138
139 message = message and (message.."\n") or ""; 139 message = message and (message.."\n") or "";
140 140
141 -- +3 counts for this function, and the pcall() and wrapper above us 141 -- +3 counts for this function, and the pcall() and wrapper above us, the +1... I don't know.
142 local levels = get_traceback_table(thread, level+3); 142 local levels = get_traceback_table(thread, level+(thread == nil and 4 or 0));
143 143
144 local last_source_desc; 144 local last_source_desc;
145 145
146 local lines = {}; 146 local lines = {};
147 for nlevel, level in ipairs(levels) do 147 for nlevel, level in ipairs(levels) do
169 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc)); 169 table.insert(lines, "\t "..build_source_boundary_marker(last_source_desc));
170 end 170 end
171 nlevel = nlevel-1; 171 nlevel = nlevel-1;
172 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line); 172 table.insert(lines, "\t"..(nlevel==0 and ">" or " ")..getstring(styles.level_num, "("..nlevel..") ")..line);
173 local npadding = (" "):rep(#tostring(nlevel)); 173 local npadding = (" "):rep(#tostring(nlevel));
174 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding); 174 if level.locals then
175 if locals_str then 175 local locals_str = string_from_var_table(level.locals, optimal_line_length, "\t "..npadding);
176 table.insert(lines, "\t "..npadding.."Locals: "..locals_str); 176 if locals_str then
177 table.insert(lines, "\t "..npadding.."Locals: "..locals_str);
178 end
177 end 179 end
178 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding); 180 local upvalues_str = string_from_var_table(level.upvalues, optimal_line_length, "\t "..npadding);
179 if upvalues_str then 181 if upvalues_str then
180 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str); 182 table.insert(lines, "\t "..npadding.."Upvals: "..upvalues_str);
181 end 183 end