Fri Dec 13 15:10:44 1996 Tom Yu * admin.texinfo (The User--Kerberos Interaction): The environment variable is KRB5CCNAME, not KRB5_CCNAME. (Getting DNS Information Correct): It's Domain Name System, not Distributed Name Service. Thu Dec 12 18:36:20 1996 Tom Yu * user-guide.texinfo: Bump edition to 1.0. Nuke gratuitous vfills. Change @value{PRODUCT} to explicit reference, to avoid makeinfo bug that introduces a nul character. * krb425.texinfo: Bump edition to 1.0. * build.texinfo (OS Incompatibilities): Refer to krb5-send-pr instead of mail to krb5-bugs. (Options to Configure): Change options to indicate meta-names, e.g. "--prefix=PREFIX"; also nuke leading slash on some descriptions of options. * admin.texinfo: Add more explicit linebreaks to prevent overruns; also break up a few long lines in example program output. Fri Dec 6 13:58:19 1996 Tom Yu * .cvsignore: Ignore by suffix rather than by file. * Makefile (admin-guide-info): (admin-guide-info): Update to reflect new info file name. * admin.texinfo: Change info file name to krb5-admin.info for consistency. (Administrating Kerberos Database Entries): "-c credentials_cache" -> "-c ccache" to avoid overrun. (Privileges): Null instance example is joeuser@REALM, not joeuser/null@REALM. * install.texinfo (Kerberos Realms): SAN_FRANCISCO.FUBAR.ORG -> HOUSTON.FUBAR.ORG to prevent margin overrun. (Create a kadmind Keytab): Add explicit linebreak to prevent margin overrun. (Overview of This Guide): (Installing Kerberos V5): Remove references to windows and mac client machines for now. * krb425.texinfo: Use krb425.info instead of Kerberos-V4-to-V5.info in the interest of shorter filenames. * user-guide.texinfo (Introduction): Eliminate "Kerberos V5 is based on Kerberos V5". (Ticket Management): Fix ref to "/rsh". (rsh): Add explicit linebreak to avoid margin overrun. * krb425.texinfo (Upgrading KDCs): Use ROOTDIR rather than INSTALLDIR to get the proper pathname. * send-pr.texinfo: Fix some misspellings, also remove references to things that are only really applicable for Cygnus. Wed Dec 4 23:47:28 1996 Tom Yu * krb425.texinfo (Upgrading Application Servers): v5srvtab -> krb5.keytab Mon Dec 2 13:00:26 1996 Barry Jaspan * build.texinfo (The DejaGnu Tests): info about .k5login for krb-root tests [krb5-doc/261] * install.texinfo (Add Administrators to the Kerberos Database): note relationship between acl file and admin principals [krb5-doc/251] (Edit the Configuration Files): mention the logging stanza here, too, and tell people to check it when they start the daemons [krb5-doc/253] * build.texinfo (The KADM5 Tests): add section for the kadm5 tests [krb5-doc/247] Fri Nov 29 19:47:38 1996 Tom Yu * build.texinfo (Unpacking the Sources): Mention that ./krb5-1.0 is the default directory that the tarballs will unpack into. Also, "/u1/krb5" -> "/u1/krb5-1.0". (osconf.h): Remove reference to DEFAULT_LNAME_FILENAME, as we're no longer using the aname database code. (Options to Configure): Add mention of --localstatedir (Shared Library Support): It's Solaris 2.4/SunOS 5.4, not Solaris 5.4. (Solaris 2.X): Shared libs work with gcc. * install.texinfo (Mapping Hostnames onto Kerberos Realms): Fix spacing error. * admin.texinfo: Fix up old references to "/krb5". * send-pr.texinfo: krb5-send-pr is in PREFIX/sbin, not PREFIX/bin. * admin.texinfo: "/lib/krb5kdc" -> "/var/krb5kdc" * krb425.texinfo (Upgrading Application Servers): Change flag "cygnus" to "CYGNUS". * install.texinfo (Please Read the Documentation): Change flag "cygnus" to "CYGNUS". * admin.texinfo (domain_realm): Change flag "cygnus" to "CYGNUS". * definitions.texinfo: Change /usr/@value{LCPRODUCT} to /usr/local to sync with default paths. * admin.texinfo (domain_realm): Conditionalize "COM" vs "EDU" in example. * install.texinfo (Create Host Keys for the Slave KDCs): Change -randpass to -randkey. [244] Thu Nov 28 18:54:02 1996 Tom Yu * krb425.texinfo: Change to use "@chapternewpage odd", also frob copyright page as per other docs. Also, remove footnote claiming that "Kerberos V5 is based on the MIT beta7 release". * admin.texinfo (capaths): Fix unquoted braces. (appdefaults): Fix unquoted braces. Wed Nov 27 18:27:18 1996 Jeffrey C. G. Bigler * Makefile: Added send-pr.texinfo to ADMIN_INCLUDES and INSTALL_INCLUDES. * admin.texinfo: Added chapter on config files. Changed bug reporting section to include send-pr file. * install.texinfo: Changed bug reporting section to include send-pr file. Added reference to Sysadmin Guide chapter on config files. * send-pr.texinfo: Fixed this up to match our version of krb5-send-pr. Tue Nov 26 18:34:11 1996 Tom Yu * install.texinfo: Fix a couple of references to "@value{COMPANY}" so they don't commit MIT to doing user support; also fixed some punctuation errors. Mon Nov 25 23:39:53 1996 Theodore Y. Ts'o * copyright.texinfo: Change lib/kadm to lib/kadm5, and add the kadmin/passwd to the list of directories containing OV code. Sun Nov 24 23:55:52 1996 Ezra Peisach (epeisach@mit.edu) * build.texinfo: Remove --with-kdb-db section as it does not exist. Add --with-tcl info. Tue Nov 19 13:42:00 1996 Barry Jaspan * install.texinfo, build.texinfo: misc suggestions from jhawk [krb5-doc/55] Fri Nov 15 17:52:39 1996 Jeff Bigler * Makefile (krb425-guide): added section to make krb425 guide. * krb425.texinfo: brought in this document from Cygnus. Fri Nov 15 00:06:53 1996 Tom Yu * user-guide.texinfo: Changes to put copyright page in its own node in the info file. * install.texinfo: Changes to put copyright page in its own node in the info file. * admin.texinfo: Changes to put copyright page in its own node in the info file, plus more krb5.keytab name updates. * install.texinfo (The Keytab File): (Extract Host Keytabs for the KDCs): (The Keytab File): (Some Advice about Secure Hosts): Update filename to krb5.keytab. (Upgrading the application servers): Add node to explain keytab rename. Wed Nov 13 15:17:22 1996 Barry Jaspan * install.texinfo (kdc.conf): remove profile relation from krb5.conf and kdc.conf [PR 124] Mon Nov 11 12:45:39 1996 Barry Jaspan * install.texinfo (kdc.conf): remove admin_database_name and lockfile fields from sample kdc.conf Sun Nov 10 21:20:05 1996 Sam Hartman * bug-report.texinfo: We have krb5-send-pr now. * install.texinfo (Edit the Configuration Files): kdc.conf lives in var/krb5kdc/kdc.conf (krb5.conf): No, we will not tell people to log to /dev/ttyp9; default to /var/log (Slave KDCs): Avoid over committing MIT to random things that other vendors might want to do. (Limit Access to the KDCs): If you are running klogind -c you want -5 not -k (Some Advice about Secure Hosts): We disable things in /etc/inetd.conf not /etc/services Tue Nov 5 16:14:06 1996 Barry Jaspan * install.texinfo (Upgrading existing Master and Slave KDCs to the current release): added section on upgrading to new db format with dump/load [krb5-doc/119] Mon Nov 4 16:50:51 1996 Barry Jaspan * admin.texinfo (Adding or Modifying Principals): update kadmin "default" policy semantics [krb5-doc/84] * install.texinfo: various minor comments from jhawk [krb5-doc/55] Fri Nov 1 19:05:15 1996 Tom Yu * .cvsignore: Ignore the .html files. * copyright.texinfo: Add call to "@pagealignmacro" to force a blank page after copyright spewage. [krb5-doc/54] * Makefile: Apply jik's changes for dependencies [krb5-doc/101] * install.texinfo (krb5.conf): Fixed extra closebrace. [krb5-doc/92] Fri Oct 18 13:42:49 1996 Barry Jaspan * install.texinfo (Create a kadmind Keytab): use kadmin.local instead of kadmin to perform pre-kadmind setup [krb5-admin/28] Tue Sep 10 20:47:41 1996 Theodore Y. Ts'o * user-guide.texinfo: The telnet man page only has 9 pages; the rcp man page only has 1 page. Mon Sep 9 19:35:37 1996 Theodore Ts'o * build.texinfo: Updated to reflect latest OS inconsistencies; removed shared library theory section to a new file, implementor.texinfo, which will have information that only developers will care about. Mon Sep 9 13:25:06 1996 Jeff Bigler * texinfo-suppl.tex: extracted @doubleleftarrow{} hack into separate file so we don't have to ship a modified version of texinfo.tex. * user-guide.texinfo (Reference): changed offset to -700 for PostScript man pages for full page printing. * man2ps: changed scale from 0.7 to 0.85 for full page printing. Piped csplit output to /dev/null, to make it a bit neater. Fri Sep 6 19:59:12 1996 Theodore Y. Ts'o * copyright.texinfo: Updated copyright dates and modify copyright text to have non-commital words about export licenses. * admin.texinfo, install.texinfo: Set edition to "b7-1". * user-guide.texinfo: Set edition to "b7-1". Fix the hoffset and voffsets for the included man page poscript files so that the images are lined up correctly (at least using MIT's dvips). * Makefile (user-guide): Make sure the kpasswd man page's postscript is built. Add new target "clean-temp-ps" which cleans up the temporary postscript files. * man2ps: Changes to make man2ps more portable. Remove bash specific constructs, and replace with calls to expr. Since the arguments to "troff" require groff, just try to run groff explicitly (someone can always edit the ROFF and ROFF_OPTIONS at the top of the script). Try to automatically detect whether csplit or gcsplit should be used, by grovelling through the user's path. * texinfo.tex: Check in version of texinfo.tex that has Jeff's magic doubleleft arrow hack. Fri Sep 6 12:59:43 1996 Jeff Bigler * install.texinfo: new name for cyg-install.texinfo * install-old.texi: new name for install.texi (so we know what's what.) * Makefile (install-guide): renamed cyg-install.texinfo to install.texinfo. * man2ps: shell script to create postscript include files for user's guide. * man2ps.M: man page for above shell script * build.texinfo: this is now included by cyg-install.texinfo (which should eventually replace install.texi.) * definitions.texinfo: added flags "MIT" and "CYGNUS". * cyg-install.texinfo: added @ifset and @ifclear sections to separate MIT-specific and Cygnus-specific sections. * admin.texinfo: added @ifset and @ifclear sections as with cyg-install.texinfo * Makefile: rewrote entire contents of file. Fri Sep 6 01:22:56 1996 Theodore Y. Ts'o * build.texinfo: Remove --enable options which are no longer valid. Updated reference to dejagnu. Thu Apr 18 14:38:42 1996 Theodore Y. Ts'o * install.texi (Building using lndir): Minor grammer fix. Sun Apr 21 14:01:29 1996 Sam Hartman * install.texi (Installing Kerberos Applications): Updated to fix jhawk's comments to this section. Sat Apr 20 03:27:27 1996 Sam Hartman * install.texi (Top): Remade master menu. (Installing Kerberos Applications): New section. Fri Apr 19 22:55:36 1996 Sam Hartman * install.texi (Shared Library Support): Update to better reflect reality on AIX. Tue Mar 19 10:34:20 1996 Ezra Peisach * Makefile (clean): Remove *.info-? now. * install.texi: Changes to allow emacs M-X texinfo-format-buffer to work. (The problem was @item used within @enumerate). Fri Mar 8 09:43:13 1996 Ezra Peisach * install.texi (The DejaGnu Tests): Describe the DejaGnu tests. Mon Oct 30 16:58:54 1995 Ezra Peisach * install.texi: Fix all path names, spelling check, convert tabs to spaces. Fri Oct 20 14:06:35 1995 Ezra Peisach * install.texi (Shared Library Support): Document shared library specifics. Tue Oct 17 19:23:50 1995 Ezra Peisach * .Sanitize (Things-to-keep): Add Makefile * Makefile: Simple file to build both dvi and info versions of install and allow for easy cleaning. * install.texi (Options to Configure): Updated to reflect current major configuration options. Wed Oct 11 16:20:58 1995 Ezra Peisach (epeisach@kangaroo.mit.edu) * install.texi: Remove config.h descriptions, describe krb5.conf. Tue Jul 11 13:07:00 1995 * install.texi: BSDI fixes. Fri May 5 13:10:38 1995 Theodore Y. Ts'o (tytso@dcl) * install.texi: Minor fixups for beta 5 release. Mainly points that the krb.conf and krb.realms section need to be updated and should be ignored in favor of the krb5.conf manual page. Fri Apr 28 16:59:41 EDT 1995 Paul Park (pjpark@mit.edu) Add new kadmin.protocol. Wed Feb 22 18:56:06 1995 John Gilmore (gnu at toad.com) * install.texi: Minor corrections. Add info about bare source trees (without configure scripts). n299'>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 530 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 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 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 897 898 899 900 901 902 903 904 905 906 907
# Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
# Copyright (C) 2000  Information-technology Promotion Agency, Japan
# Copyright (C) 2000-2003  NAKAMURA, Hiroshi  <nahi@ruby-lang.org>

require 'continuation'

if $SAFE > 0
  STDERR.print "-r debug.rb is not available in safe mode\n"
  exit 1
end

require 'tracer'
require 'pp'

class Tracer
  def Tracer.trace_func(*vars)
    Single.trace_func(*vars)
  end
end

SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__

class DEBUGGER__
MUTEX = Mutex.new

class Context
  DEBUG_LAST_CMD = []

  begin
    require 'readline'
    def readline(prompt, hist)
      Readline::readline(prompt, hist)
    end
  rescue LoadError
    def readline(prompt, hist)
      STDOUT.print prompt
      STDOUT.flush
      line = STDIN.gets
      exit unless line
      line.chomp!
      line
    end
    USE_READLINE = false
  end

  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

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

  def set_suspend
    @suspend_next = true
  end

  def clear_suspend
    @suspend_next = false
  end

  def suspend_all
    DEBUGGER__.suspend
  end

  def resume_all
    DEBUGGER__.resume
  end

  def check_suspend
    while MUTEX.synchronize {
	if @suspend_next
	  DEBUGGER__.waiting.push Thread.current
	  @suspend_next = false
	  true
	end
      }
    end
  end

  def trace?
    @trace
  end

  def set_trace(arg)
    @trace = arg
  end

  def stdout
    DEBUGGER__.stdout
  end

  def break_points
    DEBUGGER__.break_points
  end

  def display
    DEBUGGER__.display
  end

  def context(th)
    DEBUGGER__.context(th)
  end

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

  def set_last_thread(th)
    DEBUGGER__.set_last_thread(th)
  end

  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

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

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

  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

  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

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

  def debug_command(file, line, id, binding)
    MUTEX.lock
    unless defined?($debugger_restart) and $debugger_restart
      callcc{|c| $debugger_restart = c} 
    end
    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 )
            if $1 == 'on'
              set_trace_all true
            else
              set_trace_all false
            end
          elsif defined?( $1 )
            if $1 == 'on'
              set_trace true
            else
              set_trace false
            end
          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:%s\n", break_points.size, exp

	when /^\s*b(?:reak)?$/
	  if break_points.find{|b| b[1] == 0}
	    n = 1
	    stdout.print "Breakpoints:\n"
	    break_points.each do |b|
	      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 DEBUGGER__.debug_thread_info($', binding) == :cont
	    prompt = false
	  end

	when /^\s*pp\s+/
	  PP.pp(debug_eval($', binding), stdout)

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

	when /^\s*r(?:estart)?$/
          $debugger_restart.call

	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

  def debug_print_help
    stdout.print <<EOHELP
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] (<exception>|off)  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
EOHELP
  end

  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

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

  def frame_set_pos(file, line)
    if @frames[0]
      @frames[0][1] = file
      @frames[0][2] = line
    end
  end

  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

  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

  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
      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

  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

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

  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

  def excn_handle(file, line, id, binding)
    if $!.class <= SystemExit
      set_trace_func nil
      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

  def trace_func(event, file, line, id, binding, klass)
    Tracer.trace_func(event, file, line, id, binding, klass) if trace?
    context(Thread.current).check_suspend
    @file = file
    @line = line
    case event
    when 'line'
      frame_set_pos(file, line)
      if !@no_step or @frames.size == @no_step
	@stop_next -= 1
	@stop_next = -1 if @stop_next < 0
      elsif @frames.size < @no_step
	@stop_next = 0		# break here before leaving...
      else
	# nothing to do. skipped.
      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

    when 'call'
      @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

    when 'c-call'
      frame_set_pos(file, line)

    when 'class'
      @frames.unshift [binding, file, line, id]

    when 'return', 'end'
      if @frames.size == @finish_pos
	@stop_next = 1
	@finish_pos = 0
      end
      @frames.shift

    when 'raise' 
      excn_handle(file, line, id, binding)

    end
    @last_file = file
  end
end

trap("INT") { DEBUGGER__.interrupt }
@last_thread = Thread::main
@max_thread = 1
@thread_list = {Thread::main => 1}
@break_points = []
@display = []
@waiting = []
@stdout = STDOUT

class << DEBUGGER__
  def stdout
    @stdout
  end

  def stdout=(s)
    @stdout = s
  end

  def display
    @display
  end

  def break_points
    @break_points
  end

  def waiting
    @waiting
  end

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

  def set_last_thread(th)
    @last_thread = th
  end

  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

  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

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

  def interrupt
    context(@last_thread).stop_next
  end

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

  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

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

  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

  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
end

stdout.printf "Debug.rb\n"
stdout.printf "Emacs support available.\n\n"
set_trace_func proc { |event, file, line, id, binding, klass, *rest|
  DEBUGGER__.context.trace_func event, file, line, id, binding, klass
}
RubyVM::InstructionSequence.compile_option = {
  trace_instruction: true
}
end