MiniInspector.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 31 Oct 2011 22:19:21 +0000
branchjv
changeset 17892 d86c8bd5ece3
parent 17869 9610c6c94e71
child 17910 8d796ca8bd1d
permissions -rw-r--r--
Merged with CVS

"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

Object subclass:#MiniInspector
	instanceVariableNames:'inspectedObject'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Debugging-Support'
!

!MiniInspector class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    a primitive (non graphical) inspector for use on systems without
    graphics or when the real inspector dies.

    [author:]
        Claus Gittinger
"
! !

!MiniInspector class methodsFor:'instance creation'!

openOn:anObject
    |anInspector|
    anInspector := (self new) initializeFor:anObject.
    anInspector enter
! !

!MiniInspector methodsFor:'private'!

commandLoop
    |cmd valid|

    'MiniInspector on ' print.  inspectedObject displayString printCR.
    '' printCR.

    [true] whileTrue:[
        valid := false.
        cmd := self getCommand:'inspector> '.
        cmd isNil ifTrue:[   "/ EOF -> quit
            cmd := $q
        ].
	cmd isNumber ifTrue:[
	    valid := true.
	    self inspectInstvar:cmd of:inspectedObject
	].
        (cmd == $i) ifTrue:[
            valid := true.
            self printInstVarsOf:inspectedObject
        ].
        (cmd == $p) ifTrue:[
            valid := true.
            inspectedObject displayString printCR
        ].
        (cmd == $c) ifTrue:[
            valid := true.
            inspectedObject class displayString printCR
        ].
        (cmd == $C) ifTrue:[
            valid := true.
            MiniInspector openOn:inspectedObject class.
	    'back in previous inspector; inspecting ' print.  inspectedObject displayString printCR.
        ].
	(cmd == $d) ifTrue:[
	    valid := true.
	    ObjectMemory dumpObject:inspectedObject
	].
	(cmd == $*) ifTrue:[
	    valid := true.
	    inspectedObject becomeNil.
	    ^ cmd. 
	].
	(cmd == $I) ifTrue:[
	    valid := true.
            self interpreterLoopWith:inspectedObject
	].

        (cmd == $q) ifTrue:[
	    ^ cmd. 
	].

        valid ifFalse: [
            'valid commands:
 p     .... print inspected object
 c     .... print inspected objects class
 i     .... print instvars
 d     .... VM-dump inspected object

 I     .... interpreter

 C     .... inspect class
 <Num> .... inspect instvar num (1..)

 *     .... becomeNil and quit (dangerous)
 q     .... quit
'       errorPrintCR
        ]
    ].

    "Modified: 24.7.1997 / 10:00:24 / cg"
!

enter
    AbortSignal handle:[:ex |
        '** Abort Signal cought - back in previous debugLevel' printCR.
        ex restart
    ] do:[
        Error handle:[:ex |
            |yesNo|

            'Error while executing command: ' print.
            ex description printCR.
            yesNo := self getCommand:'- (i)gnore / (p)roceed / (d)ebug ? '.
            yesNo == $d ifTrue:[
                ex reject
            ].
            yesNo == $p ifTrue:[
                ex proceed
            ].
            ex restart
        ] do:[
            self commandLoop.
        ].
    ].                                                                                                                  
    ^ nil
!

getCommand:prompt
    |cmd c num|

    prompt print.

    cmd := Character fromUser.
    c := cmd.
    (c notNil and:[c isDigit]) ifTrue:[
        num := 0.
        [c notNil and:[c isDigit]] whileTrue:[
            num := (num * 10) + c digitValue.
            c := Character fromUser.
        ].
        ^ num "/ numeric
    ].

    [c notNil and:[c isEndOfLineCharacter]] whileFalse:[
        c := Character fromUser
    ].
    ^ cmd
!

initializeFor:anObject
    inspectedObject := anObject.
    ^self
!

inspectInstvar:which of:anObject
    |numInsts idx|

    numInsts := anObject class instSize.

    which > numInsts ifTrue:[
	idx := which - numInsts.
        idx > anObject basicSize ifTrue:[
            'invalid indexed instvar index: ' print. idx printCR
        ] ifFalse:[
	    'Inspecting indexed instVar ' print. idx print. '...' printCR.
	    MiniInspector openOn:(anObject basicAt:idx).
	    'back in previous inspector; inspecting ' print.  inspectedObject displayString printCR.
        ]
    ] ifFalse: [
	which < 0 ifTrue:[
	    'invalid instVar # (must be >= 1)' printCR
	] ifFalse:[
	    'Inspecting instVar ' print. which print. '...' printCR.
	    MiniInspector openOn:(anObject instVarAt:which).
	    'back in previous inspector; inspecting ' print.  inspectedObject displayString printCR.
	].
    ]

    "Modified: 20.5.1996 / 10:27:40 / cg"
!

interpreterLoopWith:anObject
    |line done rslt|
 
    'read-eval-print loop; exit with empty line' printCR.
    '' printCR.
 
    done := false.
    [done] whileFalse:[
        '> ' print.
 
        line := Stdin nextLine.
        (line size == 0) ifTrue:[
            done := true
        ] ifFalse:[
            rslt := Compiler
                evaluate:line
                in:nil
                receiver:anObject
                notifying:nil
                ifFail:[].
            rslt printCR.
        ]
    ]
!

printInstVarsOf:anObject
    |n "{ Class: SmallInteger }" names |

    n := anObject class instSize.
    names := anObject class allInstVarNames.

    'number of instvars: ' print. n printCR.
    1 to:n do:[:i |
        (i printStringLeftPaddedTo:2) print. 
	' {' print. (names at:i) print. '}' print.
	': ' print.
        ((anObject instVarAt:i) displayString contractAtEndTo:80) printCR
    ].

    n := anObject basicSize.
    n > 0 ifTrue:[
        'number of indexed instvars: ' print. n printCR.
        n > 10 ifTrue:[n := 10].
        1 to:n do:[:i |
            ' [' print. i print. ']: ' print.
            ((anObject basicAt:i) displayString contractAtEndTo:80) printCR
	]
    ].

    "Modified: 20.5.1996 / 10:27:45 / cg"
! !

!MiniInspector class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic/MiniInspector.st,v 1.26 2007/01/12 20:58:11 cg Exp $'
!

version_SVN
    ^ '$Id: MiniInspector.st 10729 2011-10-31 22:19:21Z vranyj1 $'
! !