MiniInspector.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 17 Jun 2015 06:22:00 +0100
branchjv
changeset 18487 8735bd9eee2f
parent 18120 e3a375d5f6a8
child 19410 f9d7cb8bd74c
permissions -rw-r--r--
Use inlined FNV1a hash for String ...and do not use __symbolHash(). Although currently the VM also uses FNV1a hash for Symbols, the __symbolHash() does not handle properly character with codepoint 0 (because '\0' is used as a string terminator). This causes problems with Unicode16/32Strigs whose version of FNV1a hash is using object size from header to determine string's end. Added Symbol>>hash that actually *uses* the __symbolHash() to make sure it's hash is the the same as used bu the VM. Symbols with zeroes are rare and there's no Unicode16/32Symbol. This commit fixes issue #65.

"
 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 commandArg'
	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
    |anInspector|

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

!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.
	    '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
!

getCommand:prompt
    |cmd c num arg|

    prompt errorPrint.

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

    c := Character fromUser.
    [c notNil and:[c isEndOfLineCharacter not and:[c isSeparator ]]] whileTrue:[ c := Character fromUser ].
    arg := ''.
    [c notNil and:[c isEndOfLineCharacter]] whileFalse:[
	arg := arg copyWith:c.
	c := Character fromUser
    ].
    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).
	    '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).
	    '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 := 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: /cvs/stx/stx/libbasic/MiniInspector.st,v 1.32 2014-06-25 16:30:11 cg Exp $'
! !