Skip to content

Commit 07cccef

Browse files
committed
pretty backtrace
Show more pretty backtrace like: ``` (rdbg) bt =>#0 block{|i=0, j=nil|} (3 levels) in foo at ~/src/rb/target.rb:12 #1 [C] Integer#times at ~/src/rb/target.rb:11 #2 rescue in rescue in block (2 levels) in foo at ~/src/rb/target.rb:11 #3 rescue in block (2 levels) in foo at ~/src/rb/target.rb:8 #4 block (2 levels) in foo at ~/src/rb/target.rb:5 #5 [C] Integer#times at ~/src/rb/target.rb:4 #6 block{|e=2|} in foo at ~/src/rb/target.rb:4 #7 [C] Range#each at ~/src/rb/target.rb:3 #8 [C] Enumerable#map at ~/src/rb/target.rb:3 #9 Object#foo(a=3) at ~/src/rb/target.rb:3 #10 <main> at ~/src/rb/target.rb:20 ```
1 parent bbb6e36 commit 07cccef

File tree

1 file changed

+43
-20
lines changed

1 file changed

+43
-20
lines changed

lib/debug/thread_client.rb

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ def show_src(frame_index: @current_frame_index,
197197
frame.show_line = end_line
198198
end
199199

200-
if start_line != end_line
201-
puts "[#{start_line+1}, #{end_line}] in #{path}" unless update_line
200+
if start_line != end_line && max_lines
201+
puts "[#{start_line+1}, #{end_line}] in #{pretty_path(path)}" if !update_line && max_lines != 1
202202
puts lines[start_line ... end_line]
203203
end
204204
end
@@ -287,9 +287,15 @@ def parameters_info b, vars
287287
}.compact.join(', ')
288288
end
289289

290+
def get_singleton_class obj
291+
obj.singleton_class # TODO: don't use it
292+
rescue TypeError
293+
nil
294+
end
295+
290296
def klass_sig frame
291297
klass = frame.class
292-
if klass == frame.self.singleton_class
298+
if klass == get_singleton_class(frame.self)
293299
"#{frame.self}."
294300
else
295301
"#{frame.class}#"
@@ -307,40 +313,57 @@ def short_inspect obj
307313
end
308314
end
309315

316+
HOME = ENV['HOME'] ? (ENV['HOME'] + '/') : nil
317+
318+
def pretty_path path
319+
case
320+
when HOME && path.start_with?(HOME)
321+
path.sub(HOME, '~/')
322+
else
323+
path
324+
end
325+
end
326+
327+
def pretty_location loc
328+
" at #{pretty_path(loc.path)}:#{loc.lineno}"
329+
end
330+
310331
def frame_str i
311-
buff = ''.dup
312332
frame = @target_frames[i]
313333
b = frame.binding
314334

315-
buff << (@current_frame_index == i ? '--> ' : ' ')
316-
if b
317-
buff << "##{i}\t#{frame.location}"
318-
else
319-
buff << "##{i}\t[C] #{frame.location}"
320-
end
335+
cur_str = (@current_frame_index == i ? '=>' : ' ')
321336

322337
if b && (iseq = frame.iseq)
323338
if iseq.type == :block
324339
if (argc = iseq.argc) > 0
325340
args = parameters_info b, iseq.locals[0...argc]
326-
buff << " {|#{args}|}"
341+
args_str = "{|#{args}|}"
327342
end
343+
344+
label_prefix = frame.location.label.sub('block'){ "block#{args_str}" }
345+
ci_str = label_prefix
346+
elsif (callee = b.eval('__callee__', __FILE__, __LINE__)) && (argc = iseq.argc) > 0
347+
args = parameters_info b, iseq.locals[0...argc]
348+
ksig = klass_sig frame
349+
ci_str = "#{ksig}#{callee}(#{args})"
328350
else
329-
if (callee = b.eval('__callee__', __FILE__, __LINE__)) && (argc = iseq.argc) > 0
330-
args = parameters_info b, iseq.locals[0...argc]
331-
ksig = klass_sig frame
332-
buff << " #{ksig}#{callee}(#{args})"
333-
end
351+
ci_str = frame.location.label
334352
end
335353

354+
loc_str = "#{pretty_location(frame.location)}"
355+
336356
if frame.has_return_value
337-
buff << " #=> #{short_inspect(frame.return_value)}"
357+
return_str = "\n #=> #{short_inspect(frame.return_value)}"
338358
end
339359
else
340-
# p frame.self
360+
ksig = klass_sig frame
361+
callee = frame.location.base_label
362+
ci_str = "[C] #{ksig}#{callee}"
363+
loc_str = "#{pretty_location(frame.location)}"
341364
end
342365

343-
buff
366+
"#{cur_str}##{i}\t#{ci_str}#{loc_str}#{return_str}"
344367
end
345368

346369
def show_frames max = (@target_frames || []).size
@@ -351,7 +374,7 @@ def show_frames max = (@target_frames || []).size
351374
break if i >= size
352375
puts frame_str(i)
353376
}
354-
puts " # and #{size - max} frames (use `bt' command for all frames)" if max < size
377+
puts " # and #{size - max} frames (use `bt' command for all frames)" if max < size
355378
end
356379
end
357380

0 commit comments

Comments
 (0)