MiniInspector.st
author Claus Gittinger <cg@exept.de>
Tue, 27 Sep 2016 15:25:06 +0200
changeset 20461 cfd271409c54
parent 19390 c9149f3022e1
child 19410 f9d7cb8bd74c
child 20721 79c02fb533fb
permissions -rw-r--r--
class: UnixOperatingSystem changed: #pathOfCommand: build order due to changed inheritance of ExecutionError etc.

"
 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' }"

"{ NameSpace: Smalltalk }"

Object subclass:#MiniInspector
	instanceVariableNames:'inspectedObject commandArg inputStream'
	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 (i.e. the UI is locked).
    Sometimes useful as a last chance to fix a broken UI / event handling.
    Needs a console.

    MiniInspector openOn: Display

    [author:]
	Claus Gittinger
"
! !

!MiniInspector class methodsFor:'instance creation'!

openOn:anObject
    ^ self openOn:anObject input:nil
!

openOn:anObject input:inputStreamOrNil
    |anInspector|

    anInspector := (self new) initializeFor:anObject.
    anInspector inputStream:inputStreamOrNil.
    anInspector enter.
    ^ anInspector
! !

!MiniInspector methodsFor:'accessing'!

inputStream:something
    inputStream := something.
! !

!MiniInspector methodsFor:'private'!

commandLoop
    |cmd valid|

    'MiniInspector on ' errorPrint.  inspectedObject displayString errorPrintCR.
    '' errorPrintCR.

    [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 errorPrintCR
        ].
        (cmd == $c) ifTrue:[
            valid := true.
            inspectedObject class displayString errorPrintCR
        ].
        (cmd == $C) ifTrue:[
            valid := true.
            MiniInspector openOn:inspectedObject class input:inputStream.
            'back in previous inspector; inspecting ' errorPrint.  inspectedObject displayString errorPrintCR.
        ].
        (cmd == $d) ifTrue:[
            valid := true.
            ObjectMemory dumpObject:inspectedObject
        ].
        (cmd == $E) ifTrue:[
            valid := true.
            Parser evaluate:commandArg receiver:inspectedObject.
        ].
        (cmd == $e) ifTrue:[
            valid := true.
            (Parser evaluate:commandArg receiver:inspectedObject) errorPrintCR.
        ].
        (cmd == $e) 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
 e expr   evaluate expression & print result ("E" to not print)

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

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

    "Modified: / 03-02-2014 / 10:19:46 / cg"
!

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

	    'Error while executing command: ' errorPrint.
	    ex description errorPrintCR.
	    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
!

getCharacter
    inputStream isNil ifTrue:[
        "/ globally blocking
        ^ Character fromUser
    ].
    ^ inputStream next
!

getCommand:prompt
    |cmd c num arg|

    prompt errorPrint.

    c := cmd := self getCharacter.
    c isNil ifTrue:[
        ^ nil.
    ].
    c isDigit ifTrue:[
        num := 0.
        [
            num := (num * 10) + c digitValue.
            c := self getCharacter.
        ] doWhile:[c notNil and:[c isDigit]].
        ^ num "/ numeric
    ].

    c := self getCharacter.
    [c notNil and:[c isEndOfLineCharacter not and:[c isSeparator ]]] whileTrue:[ c := self getCharacter ].
    arg := ''.
    [c notNil and:[c isEndOfLineCharacter]] whileFalse:[
        arg := arg copyWith:c.
        c := self getCharacter
    ].
    commandArg := arg.
    ^ cmd

    "Modified: / 03-02-2014 / 10:16:49 / cg"
!

initializeFor:anObject
    inspectedObject := anObject.
    ^self
!

inspect:anObject
    inspectedObject := anObject.
!

inspectInstvar:which of:anObject
    |numInsts idx|

    numInsts := anObject class instSize.

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

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

interpreterLoopWith:anObject
    |line done rslt|

    'read-eval-print loop; exit with empty line' errorPrintCR.
    '' errorPrintCR.

    done := false.
    [done] whileFalse:[
        '> ' errorPrint.

        line := Processor activeProcess stdin nextLine.
        (line size == 0) ifTrue:[
            done := true
        ] ifFalse:[
            rslt := Compiler
                evaluate:line
                in:nil
                receiver:anObject
                notifying:nil
                ifFail:[].
            rslt errorPrintCR.
        ]
    ]
!

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

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

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

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

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

!MiniInspector class methodsFor:'documentation'!

version
    ^ '$Header$'
!

version_CVS
    ^ '$Header$'
! !