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,false)enddefget_frame_call(prefix,pos,context)id=context.frame_method(pos)return"<main>"unlessidklass=context.frame_class(pos)ifCommand.settings[:callstyle]!=:short&&klasscall_str="#{klass}.#{id.id2name}"elsecall_str="#{id.id2name}"endargs=context.frame_args(pos)locals=context.frame_locals(pos)ifargs.any?call_str+="("args.each_with_indexdo|name,i|caseCommand.settings[:callstyle]when:shortcall_str+="#{name}, "when:lastklass=locals[name].classifklass.inspect.size>20+3klass=klass.inspect[0..20]+"..."endcall_str+="#{name}##{klass}, "when:trackedarg_info=context.frame_args_info(pos)ifarg_info&&arg_info.size>icall_str+="#{name}: #{arg_info[i].inspect}, "elsecall_str+="#{name}, "endendifcall_str.size>Command.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 anyendreturncall_strenddefprint_backtrace(0...@state.context.stack_size).eachdo|idx|print_frame(idx)endenddefprint_frame(pos,mark_current=true,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)endendifmark_currentframe_str=(pos==@state.frame_pos)?"--> ":" "elseframe_str=""endframe_str+=sprintf"#%-2d ",posframe_str+=get_frame_call(frame_str,pos,context)file_line="at #{CommandProcessor.canonic_file(file)}:#{line}"ifframe_str.size+file_line.size+1>Command.settings[:width]frame_str+="\n#{file_line}\n"elseframe_str+=" #{file_line}\n"endprintframe_strend### 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)"endendclass<<selfdefnames%w(where backtrace)enddefdescription%{
w[here]|bt|backtrace\tdisplay stack frames
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<Commanddefregexp/^\s* u(?:p)? (?:\s+(.*))?$/xenddefexecutepos=get_int(@match[1],"Up")returnunlessposadjust_frame(pos,false)endclass<<selfdefnames%w(up)enddefdescription%{
up[ count]\tmove to higher frame
}endendendclassDownCommand<Commanddefregexp/^\s* down (?:\s+(.*))? .*$/xenddefexecutepos=get_int(@match[1],"Down")returnunlessposadjust_frame(-pos,false)endclass<<selfdefnames%w(down)enddefdescription%{
down[ count]\tmove to lower frame
}endendendclassFrameCommand<Commanddefregexp/ ^\s*
f(?:rame)?
(?: \s+ (\S+))? \s*
$/xenddefexecuteifnot@match[1]pos=0elsepos=get_int(@match[1],"Frame")returnunlessposendadjust_frame(pos,true)endclass<<selfdefnames%w(frame)enddefhelp(cmd)%{
f[rame][ frame-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.
}endendendend