Class: DEBUGGER__

Inherits:
Object
  • Object
show all
Defined in:
lib/debug.rb

Defined Under Namespace

Classes: Context

Constant Summary collapse

MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.break_pointsObject



744
745
746
# File 'lib/debug.rb', line 744

def break_points
  @break_points
end

.context(thread = Thread.current) ⇒ Object



794
795
796
797
798
799
800
# File 'lib/debug.rb', line 794

def context(thread=Thread.current)
  c = thread[:__debugger_data__]
  unless c
    thread[:__debugger_data__] = c = Context.new
  end
  c
end

.debug_thread_info(input, binding) ⇒ Object



850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
# File 'lib/debug.rb', line 850

def debug_thread_info(input, binding)
  case input
  when /^l(?:ist)?/
    make_thread_list
    thread_list_all

  when /^c(?:ur(?:rent)?)?$/
    make_thread_list
    thread_list(@thread_list[Thread.current])

  when /^(?:sw(?:itch)?\s+)?(\d+)/
    make_thread_list
    th = get_thread($1.to_i)
    if th == Thread.current
      @stdout.print "It's the current thread.\n"
    else
      thread_list(@thread_list[th])
      context(th).stop_next
      th.run
      return :cont
    end

  when /^stop\s+(\d+)/
    make_thread_list
    th = get_thread($1.to_i)
    if th == Thread.current
      @stdout.print "It's the current thread.\n"
    elsif th.stop?
      @stdout.print "Already stopped.\n"
    else
      thread_list(@thread_list[th])
      context(th).suspend
    end

  when /^resume\s+(\d+)/
    make_thread_list
    th = get_thread($1.to_i)
    if th == Thread.current
      @stdout.print "It's the current thread.\n"
    elsif !th.stop?
      @stdout.print "Already running."
    else
      thread_list(@thread_list[th])
      th.run
    end
  end
end

.displayObject



740
741
742
# File 'lib/debug.rb', line 740

def display
  @display
end

.get_thread(num) ⇒ Object



806
807
808
809
810
811
812
813
# File 'lib/debug.rb', line 806

def get_thread(num)
  th = @thread_list.key(num)
  unless th
    @stdout.print "No thread ##{num}\n"
    throw :debug_error
  end
  th
end

.interruptObject



802
803
804
# File 'lib/debug.rb', line 802

def interrupt
  context(@last_thread).stop_next
end

.make_thread_listObject



837
838
839
840
841
842
843
844
845
846
847
848
# File 'lib/debug.rb', line 837

def make_thread_list
  hash = {}
  for th in Thread::list
    if @thread_list.key? th
      hash[th] = @thread_list[th]
    else
      @max_thread += 1
      hash[th] = @max_thread
    end
  end
  @thread_list = hash
end

.resumeObject



778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'lib/debug.rb', line 778

def resume
  MUTEX.synchronize do
    make_thread_list
    @thread_list.each do |th,|
      next if th == Thread.current
      context(th).clear_suspend
    end
    waiting.each do |th|
      th.run
    end
    waiting.clear
  end
  # Schedule other threads to restart as soon as possible.
  Thread.pass
end

.set_last_thread(th) ⇒ Object



762
763
764
# File 'lib/debug.rb', line 762

def set_last_thread(th)
  @last_thread = th
end

.set_trace(arg) ⇒ Object



752
753
754
755
756
757
758
759
760
# File 'lib/debug.rb', line 752

def set_trace( arg )
  MUTEX.synchronize do
    make_thread_list
    for th, in @thread_list
      context(th).set_trace arg
    end
  end
  arg
end

.stdoutObject



732
733
734
# File 'lib/debug.rb', line 732

def stdout
  @stdout
end

.stdout=(s) ⇒ Object



736
737
738
# File 'lib/debug.rb', line 736

def stdout=(s)
  @stdout = s
end

.suspendObject



766
767
768
769
770
771
772
773
774
775
776
# File 'lib/debug.rb', line 766

def suspend
  MUTEX.synchronize do
    make_thread_list
    for th, in @thread_list
      next if th == Thread.current
      context(th).set_suspend
    end
  end
  # Schedule other threads to suspend as soon as possible.
  Thread.pass
end

.thread_list(num) ⇒ Object



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# File 'lib/debug.rb', line 815

def thread_list(num)
  th = get_thread(num)
  if th == Thread.current
    @stdout.print "+"
  else
    @stdout.print " "
  end
  @stdout.printf "%d ", num
  @stdout.print th.inspect, "\t"
  file = context(th).instance_eval{@file}
  if file
    @stdout.print file,":",context(th).instance_eval{@line}
  end
  @stdout.print "\n"
end

.thread_list_allObject



831
832
833
834
835
# File 'lib/debug.rb', line 831

def thread_list_all
  for th in @thread_list.values.sort
    thread_list(th)
  end
end

.waitingObject



748
749
750
# File 'lib/debug.rb', line 748

def waiting
  @waiting
end