Class: ZenDebugger::Context

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

Constant Summary collapse

DEBUG_LAST_CMD =
[]

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/zendebug.rb', line 84

def initialize
  if Thread.current == Thread.main
    @stop_next = 1
  else
    @stop_next = 0
  end
  @last_file = nil
  @file = nil
  @line = nil
  @no_step = nil
  @frames = []
  @finish_pos = 0
  @trace = false
  @catch = "StandardError"
  @suspend_next = false
end

Instance Method Details

#break_pointsObject



143
144
145
# File 'lib/zendebug.rb', line 143

def break_points
  ZenDebugger.break_points
end

#check_break_points(file, klass, pos, binding, id) ⇒ Object



644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
# File 'lib/zendebug.rb', line 644

def check_break_points(file, klass, pos, binding, id)
  return false if break_points.empty?
  n = 1
  for b in break_points
    if b[0]		# valid
      if b[1] == 0	# breakpoint
        if (b[2] == file and b[3] == pos) or
            (klass and b[2] == klass and b[3] == pos)
          stdout.printf "Breakpoint %d, %s at %s:%s\n", n, debug_funcname(id), file, pos
          return true
        end
      elsif b[1] == 1	# watchpoint
        if debug_silent_eval(b[2], binding)
          stdout.printf "Watchpoint %d, %s at %s:%s\n", n, debug_funcname(id), file, pos
          return true
        end
      end
    end
    n += 1
  end
  return false
end

#check_suspendObject



121
122
123
124
125
126
127
128
129
# File 'lib/zendebug.rb', line 121

def check_suspend
  return if Thread.critical
  while (Thread.critical = true; @suspend_next)
    ZenDebugger.waiting.push Thread.current
    @suspend_next = false
    Thread.stop
  end
  Thread.critical = false
end

#clear_suspendObject



109
110
111
# File 'lib/zendebug.rb', line 109

def clear_suspend
  @suspend_next = false
end

#context(th) ⇒ Object



151
152
153
# File 'lib/zendebug.rb', line 151

def context(th)
  ZenDebugger.context(th)
end

#debug_command(file, line, id, binding) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/zendebug.rb', line 257

def debug_command(file, line, id, binding)
  MUTEX.lock
  set_last_thread(Thread.current)
  frame_pos = 0
  binding_file = file
  binding_line = line
  previous_line = nil
  if ENV['EMACS']
    stdout.printf "\032\032%s:%d:\n", binding_file, binding_line
  else
    stdout.printf "%s:%d:%s", binding_file, binding_line,
	line_at(binding_file, binding_line)
  end
  @frames[0] = [binding, file, line, id]
  display_expressions(binding)
  prompt = true
  while prompt and input = readline("(rdb:%d) "%thnum(), true)
    catch(:debug_error) do
      if input == ""
        next unless DEBUG_LAST_CMD[0]
        input = DEBUG_LAST_CMD[0]
        stdout.print input, "\n"
      else
        DEBUG_LAST_CMD[0] = input
      end

      case input
      when /^\s*tr(?:ace)?(?:\s+(on|off))?(?:\s+(all))?$/
        if defined? $2 then
          set_trace_all $1 == 'on'
        elsif defined? $1 then
          set_trace $1 == 'on'
        end

        if trace?
          stdout.print "Trace on.\n"
        else
          stdout.print "Trace off.\n"
        end

      when /^\s*b(?:reak)?\s+(?:(.+):)?([^.:]+)$/
        pos = $2
        if $1
          klass = debug_silent_eval($1, binding)
          file = $1
        end
        if pos =~ /^\d+$/
          pname = pos
          pos = pos.to_i
        else
          pname = pos = pos.intern.id2name
        end
        break_points.push [true, 0, klass || file, pos]
        stdout.printf "Set breakpoint %d at %s:%s\n", break_points.size, klass || file, pname

      when /^\s*b(?:reak)?\s+(.+)[#.]([^.:]+)$/
        pos = $2.intern.id2name
        klass = debug_eval($1, binding)
        break_points.push [true, 0, klass, pos]
        stdout.printf "Set breakpoint %d at %s.%s\n", break_points.size, klass, pos

      when /^\s*wat(?:ch)?\s+(.+)$/
        exp = $1
        break_points.push [true, 1, exp]
        stdout.printf "Set watchpoint %d\n", break_points.size, exp

      when /^\s*b(?:reak)?$/
        if break_points.find{|b| b[1] == 0}
          n = 1
          stdout.print "Breakpoints:\n"
          for b in break_points
            if b[0] and b[1] == 0
              stdout.printf "  %d %s:%s\n", n, b[2], b[3] 
            end
            n += 1
          end
        end
        if break_points.find{|b| b[1] == 1}
          n = 1
          stdout.print "\n"
          stdout.print "Watchpoints:\n"
          for b in break_points
            if b[0] and b[1] == 1
              stdout.printf "  %d %s\n", n, b[2]
            end
            n += 1
          end
        end
        if break_points.size == 0
          stdout.print "No breakpoints\n"
        else
          stdout.print "\n"
        end

      when /^\s*del(?:ete)?(?:\s+(\d+))?$/
        pos = $1
        unless pos
          input = readline("Clear all breakpoints? (y/n) ", false)
          if input == "y"
            for b in break_points
              b[0] = false
            end
          end
        else
          pos = pos.to_i
          if break_points[pos-1]
            break_points[pos-1][0] = false
          else
            stdout.printf "Breakpoint %d is not defined\n", pos
          end
        end

      when /^\s*disp(?:lay)?\s+(.+)$/
        exp = $1
        display.push [true, exp]
        stdout.printf "%d: ", display.size
        display_expression(exp, binding)

      when /^\s*disp(?:lay)?$/
        display_expressions(binding)

      when /^\s*undisp(?:lay)?(?:\s+(\d+))?$/
        pos = $1
        unless pos
          input = readline("Clear all expressions? (y/n) ", false)
          if input == "y"
            for d in display
              d[0] = false
            end
          end
        else
          pos = pos.to_i
          if display[pos-1]
            display[pos-1][0] = false
          else
            stdout.printf "Display expression %d is not defined\n", pos
          end
        end

      when /^\s*c(?:ont)?$/
        prompt = false

      when /^\s*s(?:tep)?(?:\s+(\d+))?$/
        if $1
          lev = $1.to_i
        else
          lev = 1
        end
        @stop_next = lev
        prompt = false

      when /^\s*n(?:ext)?(?:\s+(\d+))?$/
        if $1
          lev = $1.to_i
        else
          lev = 1
        end
        @stop_next = lev
        @no_step = @frames.size - frame_pos
        prompt = false

      when /^\s*w(?:here)?$/, /^\s*f(?:rame)?$/
        display_frames(frame_pos)

      when /^\s*l(?:ist)?(?:\s+(.+))?$/
        if not $1
          b = previous_line ? previous_line + 10 : binding_line - 5
          e = b + 9
        elsif $1 == '-'
          b = previous_line ? previous_line - 10 : binding_line - 5
          e = b + 9
        else
          b, e = $1.split(/[-,]/)
          if e
            b = b.to_i
            e = e.to_i
          else
            b = b.to_i - 5
            e = b + 9
          end
        end
        previous_line = b
        display_list(b, e, binding_file, binding_line)

      when /^\s*up(?:\s+(\d+))?$/
        previous_line = nil
        if $1
          lev = $1.to_i
        else
          lev = 1
        end
        frame_pos += lev
        if frame_pos >= @frames.size
          frame_pos = @frames.size - 1
          stdout.print "At toplevel\n"
        end
        binding, binding_file, binding_line = @frames[frame_pos]
        stdout.print format_frame(frame_pos)

      when /^\s*down(?:\s+(\d+))?$/
        previous_line = nil
        if $1
          lev = $1.to_i
        else
          lev = 1
        end
        frame_pos -= lev
        if frame_pos < 0
          frame_pos = 0
          stdout.print "At stack bottom\n"
        end
        binding, binding_file, binding_line = @frames[frame_pos]
        stdout.print format_frame(frame_pos)

      when /^\s*fin(?:ish)?$/
        if frame_pos == @frames.size
          stdout.print "\"finish\" not meaningful in the outermost frame.\n"
        else
          @finish_pos = @frames.size - frame_pos
          frame_pos = 0
          prompt = false
        end

      when /^\s*cat(?:ch)?(?:\s+(.+))?$/
        if $1
          excn = $1
          if excn == 'off'
            @catch = nil
            stdout.print "Clear catchpoint.\n"
          else
            @catch = excn
            stdout.printf "Set catchpoint %s.\n", @catch
          end
        else
          if @catch
            stdout.printf "Catchpoint %s.\n", @catch
          else
            stdout.print "No catchpoint.\n"
          end
        end

      when /^\s*q(?:uit)?$/
        input = readline("Really quit? (y/n) ", false)
        if input == "y"
          exit!	# exit -> exit!: No graceful way to stop threads...
        end

      when /^\s*v(?:ar)?\s+/
        debug_variable_info($', binding)

      when /^\s*m(?:ethod)?\s+/
        debug_method_info($', binding)

      when /^\s*th(?:read)?\s+/
        if ZenDebugger.debug_thread_info($', binding) == :cont
          prompt = false
        end

      when /^\s*p\s+/
        stdout.printf "%s\n", debug_eval($', binding).inspect

      when /^\s*h(?:elp)?$/
        debug_print_help()

      else
        v = debug_eval(input, binding)
        stdout.printf "%s\n", v.inspect
      end
    end
  end
  MUTEX.unlock
  resume_all
end

#debug_eval(str, binding) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/zendebug.rb', line 163

def debug_eval(str, binding)
  begin
    val = eval(str, binding)
  rescue StandardError, ScriptError => e
    at = eval("caller(1)", binding)
    stdout.printf "%s:%s\n", at.shift, e.to_s.sub(/\(eval\):1:(in `.*?':)?/, '')
    for i in at
      stdout.printf "\tfrom %s\n", i
    end
    throw :debug_error
  end
end

#debug_funcname(id) ⇒ Object



636
637
638
639
640
641
642
# File 'lib/zendebug.rb', line 636

def debug_funcname(id)
  if id.nil?
    "toplevel"
  else
    id.id2name
  end
end

#debug_method_info(input, binding) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/zendebug.rb', line 213

def debug_method_info(input, binding)
  case input
  when /^i(:?nstance)?\s+/
    obj = debug_eval($', binding)

    len = 0
    for v in obj.methods.sort
      len += v.size + 1
      if len > 70
        len = v.size + 1
        stdout.print "\n"
      end
      stdout.print v, " "
    end
    stdout.print "\n"

  else
    obj = debug_eval(input, binding)
    unless obj.kind_of? Module
      stdout.print "Should be Class/Module: ", input, "\n"
    else
      len = 0
      for v in obj.instance_methods(false).sort
        len += v.size + 1
        if len > 70
          len = v.size + 1
          stdout.print "\n"
        end
        stdout.print v, " "
      end
      stdout.print "\n"
    end
  end
end

#debug_print_helpObject

def debug_command



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/zendebug.rb', line 531

def debug_print_help
  stdout.print '
Debugger help v.-0.002b
Commands
  b[reak] [file:|class:]<line|method>
  b[reak] [class.]<line|method>
                         set breakpoint to some position
  wat[ch] <expression>       set watchpoint to some expression
  cat[ch] <an Exception>     set catchpoint to an exception
  b[reak]                    list breakpoints
  cat[ch]                    show catchpoint
  del[ete][ nnn]             delete some or all breakpoints
  disp[lay] <expression>     add expression into display expression list
  undisp[lay][ nnn]          delete one particular or all display expressions
  c[ont]                     run until program ends or hit breakpoint
  s[tep][ nnn]               step (into methods) one line or till line nnn
  n[ext][ nnn]               go over one line or till line nnn
  w[here]                    display frames
  f[rame]                    alias for where
  l[ist][ (-|nn-mm)]         list program, - lists backwards
                         nn-mm lists given lines
  up[ nn]                    move to higher frame
  down[ nn]                  move to lower frame
  fin[ish]                   return to outer frame
  tr[ace] (on|off)           set trace mode of current thread
  tr[ace] (on|off) all       set trace mode of all threads
  q[uit]                     exit from debugger
  v[ar] g[lobal]             show global variables
  v[ar] l[ocal]              show local variables
  v[ar] i[nstance] <object>  show instance variables of object
  v[ar] c[onst] <object>     show constants of object
  m[ethod] i[nstance] <obj>  show methods of object
  m[ethod] <class|module>    show instance methods of class or module
  th[read] l[ist]            list all threads
  th[read] c[ur[rent]]       show current thread
  th[read] [sw[itch]] <nnn>  switch thread context to nnn
  th[read] stop <nnn>        stop thread nnn
  th[read] resume <nnn>      resume thread nnn
  p expression               evaluate expression and print its value
  h[elp]                     print this help
  <everything else>          evaluate
'
end

#debug_silent_eval(str, binding) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/zendebug.rb', line 176

def debug_silent_eval(str, binding)
  begin
    eval(str, binding)
  rescue StandardError, ScriptError
    nil
  end
end

#debug_variable_info(input, binding) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/zendebug.rb', line 191

def debug_variable_info(input, binding)
  case input
  when /^\s*g(?:lobal)?\s*$/
    var_list(global_variables, binding)

  when /^\s*l(?:ocal)?\s*$/
    var_list(eval("local_variables", binding), binding)

  when /^\s*i(?:nstance)?\s+/
    obj = debug_eval($', binding)
    var_list(obj.instance_variables, obj.instance_eval{binding()})

  when /^\s*c(?:onst(?:ant)?)?\s+/
    obj = debug_eval($', binding)
    unless obj.kind_of? Module
      stdout.print "Should be Class/Module: ", $', "\n"
    else
      var_list(obj.constants, obj.module_eval{binding()})
    end
  end
end

#displayObject



147
148
149
# File 'lib/zendebug.rb', line 147

def display
  ZenDebugger.display
end

#display_expression(exp, binding) ⇒ Object



586
587
588
# File 'lib/zendebug.rb', line 586

def display_expression(exp, binding)
  stdout.printf "%s = %s\n", exp, debug_silent_eval(exp, binding).to_s
end

#display_expressions(binding) ⇒ Object



575
576
577
578
579
580
581
582
583
584
# File 'lib/zendebug.rb', line 575

def display_expressions(binding)
  n = 1
  for d in display
    if d[0]
      stdout.printf "%d: ", n
      display_expression(d[1], binding)
    end
    n += 1
  end
end

#display_frames(pos) ⇒ Object



590
591
592
593
594
595
596
597
598
599
# File 'lib/zendebug.rb', line 590

def display_frames(pos)
  0.upto(@frames.size - 1) do |n|
    if n == pos
      stdout.print "--> "
    else
      stdout.print "    "
    end
    stdout.print format_frame(n)
  end
end

#display_list(b, e, file, line) ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/zendebug.rb', line 607

def display_list(b, e, file, line)
  stdout.printf "[%d, %d] in %s\n", b, e, file
  if lines = SCRIPT_LINES__[file] and lines != true
    n = 0
    b.upto(e) do |n|
      if n > 0 && lines[n-1]
        if n == line
          stdout.printf "=> %d  %s\n", n, lines[n-1].chomp
        else
          stdout.printf "   %d  %s\n", n, lines[n-1].chomp
        end
      end
    end
  else
    stdout.printf "No sourcefile available for %s\n", file
  end
end

#excn_handle(file, line, id, binding) ⇒ Object



667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
# File 'lib/zendebug.rb', line 667

def excn_handle(file, line, id, binding)
  if $!.class <= SystemExit
    ZenDebugger.stop_debugging
    exit
  end

  if @catch and ($!.class.ancestors.find { |e| e.to_s == @catch })
    stdout.printf "%s:%d: `%s' (%s)\n", file, line, $!, $!.class
    fs = @frames.size
    tb = caller(0)[-fs..-1]
    if tb
      for i in tb
        stdout.printf "\tfrom %s\n", i
      end
    end
    suspend_all
    debug_command(file, line, id, binding)
  end
end

#format_frame(pos) ⇒ Object



601
602
603
604
605
# File 'lib/zendebug.rb', line 601

def format_frame(pos)
  bind, file, line, id = @frames[pos]
  sprintf "#%d %s:%s%s\n", pos + 1, file, line,
  (id ? ":in `#{id.id2name}'" : "")
end

#line_at(file, line) ⇒ Object



625
626
627
628
629
630
631
632
633
634
# File 'lib/zendebug.rb', line 625

def line_at(file, line)
  lines = SCRIPT_LINES__[file]
  if lines
    return "\n" if lines == true
    line = lines[line-1]
    return "\n" unless line
    return line
  end
  return "\n"
end

#resume_allObject



117
118
119
# File 'lib/zendebug.rb', line 117

def resume_all
  ZenDebugger.resume
end

#set_last_thread(th) ⇒ Object



159
160
161
# File 'lib/zendebug.rb', line 159

def set_last_thread(th)
  ZenDebugger.set_last_thread(th)
end

#set_suspendObject



105
106
107
# File 'lib/zendebug.rb', line 105

def set_suspend
  @suspend_next = true
end

#set_trace(arg) ⇒ Object



135
136
137
# File 'lib/zendebug.rb', line 135

def set_trace(arg)
  @trace = arg
end

#set_trace_all(arg) ⇒ Object



155
156
157
# File 'lib/zendebug.rb', line 155

def set_trace_all(arg)
  ZenDebugger.set_trace(arg)
end

#stdoutObject



139
140
141
# File 'lib/zendebug.rb', line 139

def stdout
  ZenDebugger.stdout
end

#stop_next(n = 1) ⇒ Object



101
102
103
# File 'lib/zendebug.rb', line 101

def stop_next(n=1)
  @stop_next = n
end

#suspend_allObject



113
114
115
# File 'lib/zendebug.rb', line 113

def suspend_all
  ZenDebugger.suspend
end

#thnumObject



248
249
250
251
252
253
254
255
# File 'lib/zendebug.rb', line 248

def thnum
  num = ZenDebugger.instance_eval{@thread_list[Thread.current]}
  unless num
    ZenDebugger.make_thread_list
    num = ZenDebugger.instance_eval{@thread_list[Thread.current]}
  end
  num
end

#trace?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/zendebug.rb', line 131

def trace?
  @trace
end

#trace_func_c_call(file, line, id, binding, klass) ⇒ Object



714
715
716
# File 'lib/zendebug.rb', line 714

def trace_func_c_call(file, line, id, binding, klass)
  frame_set_pos(file, line)
end

#trace_func_call(file, line, id, binding, klass) ⇒ Object



706
707
708
709
710
711
712
# File 'lib/zendebug.rb', line 706

def trace_func_call(file, line, id, binding, klass)
  @frames.unshift [binding, file, line, id]
  if check_break_points(file, klass, id.id2name, binding, id)
    suspend_all
    debug_command(file, line, id, binding)
  end
end

#trace_func_class(file, line, id, binding, klass) ⇒ Object



718
719
720
# File 'lib/zendebug.rb', line 718

def trace_func_class(file, line, id, binding, klass)
  @frames.unshift [binding, file, line, id]
end

#trace_func_end(file, line, id, binding, klass) ⇒ Object



730
731
732
# File 'lib/zendebug.rb', line 730

def trace_func_end(file, line, id, binding, klass)
  @frames.shift
end

#trace_func_line(file, line, id, binding, klass) ⇒ Object

Beginning of broken up trace methods:



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/zendebug.rb', line 690

def trace_func_line(file, line, id, binding, klass)
  frame_set_pos(file, line)
  frame_size = @frames.size
  if !@no_step or frame_size == @no_step
    @stop_next -= 1
    @stop_next = -1 if @stop_next < 0
  elsif frame_size < @no_step
    @stop_next = 0		# break here before leaving...
  end
  if @stop_next == 0 or check_break_points(file, nil, line, binding, id)
    @no_step = nil
    suspend_all
    debug_command(file, line, id, binding)
  end
end

#trace_func_raise(file, line, id, binding, klass) ⇒ Object



734
735
736
# File 'lib/zendebug.rb', line 734

def trace_func_raise(file, line, id, binding, klass)
  excn_handle(file, line, id, binding)
end

#trace_func_return(file, line, id, binding, klass) ⇒ Object



722
723
724
725
726
727
728
# File 'lib/zendebug.rb', line 722

def trace_func_return(file, line, id, binding, klass)
  if @frames.size == @finish_pos
    @stop_next = 1
    @finish_pos = 0
  end
  @frames.shift
end

#var_list(ary, binding) ⇒ Object



184
185
186
187
188
189
# File 'lib/zendebug.rb', line 184

def var_list(ary, binding)
  ary.sort!
  for v in ary
    stdout.printf "  %s => %s\n", v, eval(v, binding).inspect
  end
end