moduleByebug# Mix-in module to assist in command parsing.moduleFrameFunctionsdefadjust_frame(frame_pos,absolute,context=@state.context)@state.frame_pos=0ifcontext!=@state.contextifabsoluteifframe_pos<0abs_frame_pos=context.stack_size+frame_poselseabs_frame_pos=frame_posendelseabs_frame_pos=@state.frame_pos+frame_posendifabs_frame_pos>=context.stack_sizethenerrmsg"Adjusting would put us beyond the oldest (initial) frame.\n"returnelsifabs_frame_pos<0thenerrmsg"Adjusting would put us beyond the newest (innermost) frame.\n"returnendif@state.frame_pos!=abs_frame_posthen@state.previous_line=nil@state.frame_pos=abs_frame_posend@state.file=context.frame_file(@state.frame_pos)@state.line=context.frame_line(@state.frame_pos)print_frame(@state.frame_pos,true)enddefget_frame_call(prefix,pos,context)id=context.frame_method(pos)klass=context.frame_class(pos)call_str=""ifidargs=context.frame_args(pos)locals=context.frame_locals(pos)ifCommand.settings[:callstyle]!=:short&&klassifCommand.settings[:callstyle]==:trackedarg_info=context.frame_args_info(pos)endcall_str<<"#{klass}."endcall_str<<id.id2nameifargs.any?call_str<<"("args.each_with_indexdo|name,i|caseCommand.settings[:callstyle]when:shortcall_str+="%s, "%[name]when:lastklass=locals[name].classifklass.inspect.size>20+3klass=klass.inspect[0..20]+"..."endcall_str+="%s#%s, "%[name,klass]when:trackedifarg_info&&arg_info.size>icall_str+="#{name}: #{arg_info[i].inspect}, "elsecall_str+="%s, "%nameendendifcall_str.size>self.class.settings[:width]-prefix.size# Strip off trailing ', ' if any but add stuff for later trunccall_str[-2..-1]=",...XX"breakendendcall_str[-2..-1]=")"# Strip off trailing ', ' if anyendendreturncall_strenddefprint_backtrace(0...@state.context.stack_size).eachdo|idx|ifidx==@state.frame_posprint"--> "elseprint" "endprint_frame(idx)endenddefprint_frame(pos,adjust=false,context=@state.context)file=context.frame_file(pos)line=context.frame_line(pos)klass=context.frame_class(pos)unlessCommand.settings[:full_path]path_components=file.split(/[\\\/]/)ifpath_components.size>3path_components[0...-3]='...'file=path_components.join(File::ALT_SEPARATOR||File::SEPARATOR)endendframe_num="##{pos} "call_str=get_frame_call(frame_num,pos,context)file_line="at #{CommandProcessor.canonic_file(file)}:#{line}\n"printframe_numunlesscall_str.empty?print"#{call_str} "ifcall_str.size+frame_num.size+file_line.size>self.class.settings[:width]print"\n "endendprintfile_line#if ENV['EMACS'] && adjust# fmt = (Byebug.annotate.to_i > 1 ?# "\032\032source %s:%d\n" : "\032\032%s:%d\n")# print fmt % [CommandProcessor.canonic_file(file), line]#endend### Check if call stack is truncated. This can happen if Byebug.start is not# called low enough in the call stack. An array of additional callstack# lines from caller is returned if definitely truncated, false if not, and# nil if we don't know.## We determine truncation based on a passed in sentinal set via caller which# can be nil.## First we see if we can find our position in caller. If so, then we compare# context position to that in caller using sentinal as a place to start# ignoring additional caller entries. sentinal is set by byebug, but if it's# nil then additional entries are presumably ones that we haven't recorded# in contextdeftruncated_callstack?(context,sentinal=nil,cs=caller)recorded_size=context.stack_sizeto_find_fl="#{context.frame_file(0)}:#{context.frame_line(0)}"top_discard=falsecs.each_with_indexdo|fl,i|fl.gsub!(/in `.*'$/,'')fl.gsub!(/:$/,'')iffl==to_find_fltop_discard=ibreakendendiftop_discardcs=cs[top_discard..-1]returnfalseunlesscsreturncsunlesssentinalifcs.size>recorded_size+2&&cs[recorded_size+2]!=sentinal# caller seems to truncate recursive calls and we don't.# See if we can find sentinal in the first 0..recorded_size+1 entriesreturnfalseifcs[0..recorded_size+1].any?{|f|f==sentinal}returncsendreturnfalseendreturnnilendend# Implements byebug "where" or "backtrace" command.classWhereCommand<Commanddefregexp/^\s*(?:w(?:here)?|bt|backtrace)$/enddefexecuteprint_backtraceiftruncated_callstack?(@state.context,Byebug.start_sentinal)print"Warning: saved frames may be incomplete; compare with caller(0).\n"endendclass<<selfdefhelp_command%w|where backtrace|enddefhelp(cmd)s=ifcmd=='where'%{
w[here]\tdisplay stack frames
}else%{
bt|backtrace\t\talias for where - display stack frames
}ends+=%{
Print the entire stack frame. Each frame is numbered, the most
recent frame is 0. frame number can be referred to in the "frame"
command; "up" and "down" add or subtract respectively to frame
numbers shown. The position of the current frame is marked with
-->.
}endendendclassUpCommand<Command# :nodoc:defregexp/^\s* u(?:p)? (?:\s+(.*))?$/xenddefexecutepos=get_int(@match[1],"Up")returnunlessposadjust_frame(pos,false)endclass<<selfdefhelp_command'up'enddefhelp(cmd)%{
up[count]\tmove to higher frame
}endendendclassDownCommand<Command# :nodoc:defregexp/^\s* down (?:\s+(.*))? .*$/xenddefexecutepos=get_int(@match[1],"Down")returnunlessposadjust_frame(-pos,false)endclass<<selfdefhelp_command'down'enddefhelp(cmd)%{
down[count]\tmove to lower frame
}endendendclassFrameCommand<Command# :nodoc:defregexp/ ^\s*
f(?:rame)?
(?: \s+ (\S+))? \s*
(?: thread \s+ (.*))? \s*
$/xenddefexecuteifnot@match[1]pos=0elsepos=get_int(@match[1],"Frame")returnunlessposend#if @match[2]# context = parse_thread_num('frame', @match[2])# unless context# errmsg "Thread #{@match[2]} doesn't exist.\n"# return# end#elsecontext=@state.context#endadjust_frame(pos,true,context)endclass<<selfdefhelp_command'frame'enddefhelp(cmd)%{
f[rame] [frame-number [thread thread-number]]
Move the current frame to the specified frame number, or the
0 if no frame-number has been given.
A negative number indicates position from the other end. So
'frame -1' moves to the oldest frame, and 'frame 0' moves to
the newest frame.
Without an argument, the command prints the current stack
frame. Since the current position is redisplayed, it may trigger a
resyncronization if there is a front end also watching over
things.
If a thread number is given then we set the context for evaluating
expressions to that frame of that thread.
}endendendend