GDBDebugger.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Fri, 20 Jun 2014 21:11:40 +0100
changeset 21 83395ca8b257
parent 19 GDB.st@c48d33e27d34
child 23 a7eb888c81b5
permissions -rw-r--r--
Renamed GDB to GDBDebugger GDB is prefix of all classes, GDB was renamed to avoid confusion with namespace. Also, it better describes what it really is.

"{ Package: 'jv:libgdbs' }"

Object subclass:#GDBDebugger
	instanceVariableNames:'driver commandSequenceNumber inferiorStateSequenceNumber'
	classVariableNames:''
	poolDictionaries:'GDBCommandStatus'
	category:'GDB-Core'
!


!GDBDebugger class methodsFor:'instance creation'!

new
    "return an initialized instance"

    ^ self basicNew initialize.
! !

!GDBDebugger methodsFor:'accessing'!

announcer
    ^ driver eventAnnouncer.

    "Created: / 02-06-2014 / 23:06:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

inferiorStderr
    ^ driver inferiorPTY master

    "Created: / 09-06-2014 / 10:01:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 09-06-2014 / 18:26:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

inferiorStdin
    ^ driver inferiorPTY master

    "Created: / 09-06-2014 / 10:00:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 09-06-2014 / 18:27:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

inferiorStdout
    ^ driver inferiorPTY master

    "Created: / 09-06-2014 / 10:01:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 09-06-2014 / 18:27:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBDebugger methodsFor:'accessing-private'!

currentInferiorStateSequnceNumber
    ^ inferiorStateSequenceNumber

    "Created: / 19-06-2014 / 22:22:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

nextCommandSequnceNumber
    commandSequenceNumber := commandSequenceNumber + 1.
    commandSequenceNumber == SmallInteger maxVal ifTrue:[ 
        commandSequenceNumber := 0.
    ].
    ^ commandSequenceNumber

    "Created: / 02-06-2014 / 23:48:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

nextInferiorStateSequnceNumber
    inferiorStateSequenceNumber := inferiorStateSequenceNumber + 1.
    inferiorStateSequenceNumber == SmallInteger maxVal ifTrue:[
        inferiorStateSequenceNumber := 0.
    ].
    ^ inferiorStateSequenceNumber

    "Created: / 02-06-2014 / 23:48:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBDebugger methodsFor:'commands'!

send: aGDBCommand
    ^ self send: aGDBCommand wait: true.

    "Created: / 03-06-2014 / 00:10:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

send: aGDBCommand wait: aBoolean
    "Sends given command to GDB. If `aBoolean` is true, wait for
     command to finish. Otherwise, return immediately."

    | token blocker releaser |

    token := self nextCommandSequnceNumber.
    aGDBCommand token: token.
    aBoolean ifTrue:[ 
        releaser := [ :ev |
                    ev token == token ifTrue:[ 
                        self announcer unsubscribe: releaser.  
                        blocker signal.
                    ]].
        blocker := Semaphore new.
        self announcer when: GDBCommandResultEvent do: releaser.
        driver pushEvent: (GDBCommandEvent new command: aGDBCommand).
        blocker wait.
    ] ifFalse:[
        driver pushEvent: (GDBCommandEvent new command: aGDBCommand).
    ]

    "Created: / 02-06-2014 / 23:45:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 04-06-2014 / 09:32:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBDebugger methodsFor:'event handling'!

onCommandResult: aGDBCommandResultEvent
    aGDBCommandResultEvent result status == CommandStatusExit ifTrue:[ 
        driver pushEvent: GDBExitEvent new.
    ].

    "Created: / 02-06-2014 / 23:40:18 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 04-06-2014 / 09:30:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

onExecutionEvent: aGDBExecutionEvent
    self nextInferiorStateSequnceNumber.

    "Created: / 19-06-2014 / 22:21:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

onExit: aGDBExitEvent
    self release.

    "Created: / 03-06-2014 / 00:36:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 04-06-2014 / 09:28:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBDebugger methodsFor:'finalization'!

finalize
    self release.

    "Created: / 26-05-2014 / 21:23:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBDebugger methodsFor:'initialize & release'!

initialize
    self registerForFinalization.
    driver := GDBLauncher startGDB.

    commandSequenceNumber := 0.
    inferiorStateSequenceNumber := 0.

    driver eventAnnouncerInternal
        when: GDBCommandResultEvent     send: #onCommandResult: to: self;
        when: GDBExitEvent              send: #onExit: to: self;
        when: GDBExecutionEvent         send: #onExecutionEvent: to: self.
        

    driver eventPumpStart.
    driver eventDispatchStart.

"/    self send: (GDBMICommand inferiorTtySet: driver inferiorPTY name).
    self send: (GDBMI_inferior_tty_set arguments: { driver inferiorPTY name })

    "Created: / 26-05-2014 / 21:23:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 19-06-2014 / 22:22:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

release
    driver notNil ifTrue:[ 
        driver release.
        driver := nil.
    ].

    "Created: / 26-05-2014 / 21:24:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBDebugger class methodsFor:'documentation'!

version_HG

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