Tools__BreakpointService.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 12 Jun 2013 16:47:06 +0100
branchjv
changeset 13170 c9b815af5777
parent 12679 7dffb3cbf7c4
parent 12855 ab87c94ed5ac
child 13173 e9da2324940d
permissions -rw-r--r--
Merged 0ce340e972c4 and 2d878b37539e (branch default - CVS HEAD)

"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
	      All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
"{ Package: 'stx:libtool' }"

"{ NameSpace: Tools }"

CodeViewService subclass:#BreakpointService
	instanceVariableNames:'breakpoints currentMethod mode'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-CodeView'
!

!BreakpointService class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2010 by Jan Vrany, SWING Research Group. CTU in Prague
	      All Rights Reserved

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the 'Software'), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"
! !

!BreakpointService methodsFor:'accessing'!

breakpoints
    ^ breakpoints
! !

!BreakpointService methodsFor:'change & update'!

update: aspect with: param from: sender
    |method|

    method := codeView methodHolder value.
    method ~~ currentMethod ifTrue:[
	self updateBreakPointsFor:method.
    ].

    super update: aspect with: param from: sender

    "Created: / 06-07-2011 / 15:21:08 / cg"
!

updateBreakPointsFor:aMethod
    |methodsBreakPoints|

    methodsBreakPoints := OrderedCollection new.
    aMethod notNil ifTrue:[
        mode == #vm ifTrue:[
            (aMethod breakpointTable ? #()) do:[:each|
                each class == Breakpoint ifTrue:[
                    methodsBreakPoints add: each.
                ].
            ].
        ] ifFalse:[
            aMethod literalsDo:[:eachLiteral |
                eachLiteral class == Breakpoint ifTrue:[
                    methodsBreakPoints add:eachLiteral copy.
                ].
            ].
        ].
    ].
    breakpoints := methodsBreakPoints.
    currentMethod := aMethod.

    "Created: / 06-07-2011 / 15:24:09 / cg"
    "Modified: / 06-07-2011 / 17:32:54 / jv"
    "Modified: / 15-04-2013 / 12:33:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BreakpointService methodsFor:'event handling'!

buttonPress:button x:x y:y in:view
    |lineNr|

    codeView methodHolder value isNil ifTrue:[
	^ false
    ].
    view == gutterView ifTrue:[
	button == 1 ifTrue:[
	    lineNr := textView yVisibleToLineNr:y.
	    lineNr notNil ifTrue:[ self setOrToggleBreakpointAtLine:lineNr ].
	    ^ true.
	].
	button == 3 ifTrue:[
	    ^ true.
	]
    ].
    ^ false

    "Created: / 17-06-2011 / 13:05:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 28-06-2011 / 08:31:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 19-09-2011 / 14:41:00 / cg"
!

linesDeletedFrom: start to: end

    breakpoints isEmptyOrNil ifTrue:[^self].
    self moveBreakpointsAfterLine: start - 1 by: (end - start + 1) negated

    "Created: / 06-07-2011 / 17:16:27 / jv"
!

linesInsertedFrom: start to: end

    breakpoints isEmptyOrNil ifTrue:[^self].
    self moveBreakpointsAfterLine: start - 1 by: (end - start + 1)

    "Created: / 06-07-2011 / 17:16:36 / jv"
! !

!BreakpointService methodsFor:'initialization'!

initialize

    super initialize.
    breakpoints := OrderedCollection new.
    mode := (ConfigurableFeatures includesFeature:#VMBreakpointSupport)
                ifTrue:[#vm]
                ifFalse:[#compiled].

    "Created: / 17-06-2011 / 13:49:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 15-04-2013 / 12:28:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BreakpointService methodsFor:'private'!

breakpointAtLine:line
    |pos|

    pos := textView characterPositionOfLine:line col:1.
    ^ breakpoints ? #() detect:[:each | each position = pos ] ifNone:[ nil ]

    "Modified: / 17-06-2011 / 13:59:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 05-07-2011 / 21:33:23 / cg"
!

moveBreakpointsAfterLine:line by: delta
    |pos |

    breakpoints do:[:bpnt|
        bpnt line >= line ifTrue:[
            pos := textView characterPositionOfLine:bpnt line + delta col:1.
            bpnt position:pos line:(bpnt line + delta). 
        ]
    ].

    "/gutterView redrawLinesFrom: line.

    "Created: / 17-06-2011 / 13:45:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Created: / 06-07-2011 / 17:26:30 / jv"
    "Modified: / 02-08-2012 / 09:27:10 / cg"
!

recompile
    "recompile the current method for changed breakpoints"

    |oldMethod newMethod compiler class selector|

    oldMethod := codeView methodHolder value.
    (oldMethod notNil and:[oldMethod hasPrimitiveCode not]) ifTrue:[
        "/ be careful: if the text has been edited/modified, do not compile
        textView modified ifTrue:[
            self breakPoint: #cg.
            self breakPoint: #jv.
            ^self.
        ] ifFalse:[
            "/ prepare to get reachable bpts
            breakpoints do:[:bp | bp isReached:false].

            class := oldMethod mclass.
            class isNil ifTrue:[
                class := codeView classHolder value.
                class isNil ifTrue:[
                    self breakPoint:#jv.
                    Dialog warn:'oops - lost the methods''s class'.
                    ^ self.
                ]
            ].
            selector := oldMethod selector.

            Class withoutUpdatingChangesDo:[
                compiler := ByteCodeCompilerWithBreakpointSupport new.
                compiler breakpoints:breakpoints.
                compiler methodClass:MethodWithBreakpoints.
                newMethod := compiler
                            compile:oldMethod source
                            forClass:class
                            inCategory:oldMethod category
                            notifying:nil
                            install:false
                            skipIfSame:false
                            silent:true
                            foldConstants:true
                            ifFail:[ Transcript showCR:'BreakpointService: failed to recompile for breakpoint' ].
                newMethod originalMethod: oldMethod.

                selector isNil ifTrue:[
                    "/ May happen as the selector is not stored in the method but
                    "/ searches through method's mclass methodDictionary.
                    "/ Following should be save as breakpoint is not installed when
                    "/ the code is modified...
                    selector := compiler selector.
                ].

                (class primAddSelector: selector withMethod:newMethod) ifFalse:[
                    oldMethod mclass:class.
                    self breakPoint: #cg.
                    self breakPoint: #jv.
                    ^ self
                ].
                codeView methodHolder value:newMethod.
                oldMethod mclass isNil ifTrue:[
                    "/ although this is not strictly true, not doing this
                    "/ would confuse a lot of other tools (such as the browser)
                    oldMethod mclass:class.
                ].
                class changed:#methodTrap with:selector. "/ tell browsers
                MethodTrapChangeNotificationParameter notNil ifTrue:[    
                    Smalltalk changed:#methodTrap with:(MethodTrapChangeNotificationParameter changeClass:class changeSelector:selector).
                ].
            ].
"/ self halt.
            breakpoints := breakpoints
                            select:[:bp |
"/                                bp isReached ifFalse:[ 
"/                                    "/ Transcript show:'remove unreached:'; showCR:bp 
"/                                ].
                                bp isReached
                            ]
        ]
    ]

    "Created: / 05-07-2011 / 21:33:13 / cg"
    "Modified: / 18-07-2012 / 10:53:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 02-08-2012 / 09:35:41 / cg"
!

reinstall
    | vmbreakpoints i |

    vmbreakpoints := Array new: breakpoints size * 2.
    i := 1.
    breakpoints do:[:breakpoint|
        vmbreakpoints at: i     put: breakpoint line.
        vmbreakpoints at: i + 1 put: breakpoint.
        i := i + 2.
    ].
    currentMethod breakpointTable: vmbreakpoints

    "Created: / 15-04-2013 / 12:32:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

setOrToggleBreakpointAtLine:line
    |pos bpnt|

    textView reallyModified ifTrue:[
        "/ leads to ugly behavior (method no longer found), if we allow
        "/ this...
          Dialog warn:'Please accept first (cannot set breakpoint while text is modified)'.
        ^ self
    ].

    pos := textView characterPositionOfLine:line col:1.
    bpnt := self breakpointAtLine:line.
    bpnt isNil ifTrue:[
        (mode == #vm or:[codeView topView class ~~ DebugView]) ifTrue:[
            mode == #vm ifTrue:[
                bpnt := MessageTracer breakMethod: currentMethod atLine: line. 
                bpnt notNil ifTrue:[
                    bpnt position:pos.
                    breakpoints add: bpnt.
                ].
            ] ifFalse:[
                breakpoints add:((bpnt := Breakpoint new) position:pos line:line).
                self recompile
            ].
            Display shiftDown ifTrue:[
                "/ trace
                bpnt beTracepoint
            ].
        ].
    ] ifFalse:[
        Display shiftDown ifTrue:[
            bpnt toggleTracing
        ] ifFalse:[
            bpnt toggle.
        ]
    ].

    gutterView redrawLine:line.

    "Created: / 17-06-2011 / 13:45:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 02-08-2012 / 09:26:38 / cg"
    "Modified: / 16-04-2013 / 00:52:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BreakpointService methodsFor:'redrawing'!

drawLine:lineNo in:view atX:x y:y width:w height:h from:startCol to:endColOrNil with:fg and:bg
    "Called by both gutterView and textView (well, not yet) to
     allow services to draw custom things on text view.
     Ask JV what the args means if unsure (I'm lazy to document
     them, now it is just an experiment...)"

    |bpnt icon dx dy|

    codeView methodHolder value isNil ifTrue:[
	^ self
    ].
    view == gutterView ifTrue:[
	bpnt := self breakpointAtLine:lineNo.
	bpnt isNil ifTrue:[
	    ^ self
	].
	icon := bpnt icon.
	icon isNil ifTrue:[
	    ^ self
	].
	dx := ((w - icon width) / 2) rounded.
	dy := ((h - icon height) / 2) rounded.
	icon
	    displayOn:view
	    x:x + dx
	    y:y - h + dy + 4. "TODO: Magic constant"
    ].

    "Created: / 17-06-2011 / 13:52:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 05-07-2011 / 22:14:33 / cg"
! !

!BreakpointService methodsFor:'registering'!

registerIn: aCodeView
    "Installs myself in aCodeView"

    super registerIn: aCodeView.
    self updateBreakPointsFor: aCodeView methodHolder value.

    "Created: / 15-04-2013 / 14:03:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!BreakpointService class methodsFor:'documentation'!

version_CVS
    ^ '$Header: /cvs/stx/stx/libtool/Tools__BreakpointService.st,v 1.19 2013-06-05 17:18:17 cg Exp $'

!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '$Id: Tools__BreakpointService.st,v 1.19 2013-06-05 17:18:17 cg Exp $'
! !