GDBProcess.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 08 Jul 2019 12:34:18 +0100
changeset 200 e9250da35d87
parent 195 17a6f1d1cb22
child 259 651864c2aa29
permissions -rw-r--r--
API: add method for importing Python support code This can be used by VDB, VDB plugins or any other user of libgdbs to load Python support code.

"
jv:libgdbs - GNU Debugger Interface Library
Copyright (C) 2015-now Jan Vrany

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"
"{ Package: 'jv:libgdbs' }"

"{ NameSpace: Smalltalk }"

Object subclass:#GDBProcess
	instanceVariableNames:'connection debuggerInput debuggerOutput'
	classVariableNames:''
	poolDictionaries:''
	category:'GDB-Private'
!

!GDBProcess class methodsFor:'documentation'!

copyright
"
jv:libgdbs - GNU Debugger Interface Library
Copyright (C) 2015-now Jan Vrany

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"
!

documentation
"
    GDBProcess is represents a running GDB and provides access
    to MI and CLI channels (see #debuggerIput / #debuggerOutput).

    [author:]
        Jan Vrany <jan.vrany@fit.cvut.cz>

    [instance variables:]

    [class variables:]

    [see also:]
        GDBStXUnixProcess
        GDBStXWindowsProcess

"
! !

!GDBProcess class methodsFor:'instance creation'!

new
    self == GDBProcess ifTrue:[
        ^ GDBLocalProcess newWithCommand: nil  
    ] ifFalse:[
        ^ super new.
    ].

    "
     GDBProcess new release."

    "Modified (comment): / 11-01-2018 / 23:02:11 / jv"
    "Modified: / 12-12-2018 / 22:22:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBProcess class methodsFor:'accessing'!

gdbCommand
    | exe |

    exe := UserPreferences current gdbCommand.
    exe isNil ifTrue:[ 
        exe := self gdbCommandDefault
    ].
    ^ exe

    "
    GDBProcess  gdbCommand
    UserPreferences current gdbCommand: nil.
    UserPreferences current gdbCommand: '/home/jv/Projects/gdb/users_jv_vdb/gdb/gdb'.

      

    "

    "Created: / 13-12-2018 / 11:29:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 17-06-2019 / 14:21:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

gdbCommand: aString
    UserPreferences current gdbCommand: aString

    "Created: / 13-12-2018 / 11:29:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

gdbCommandDefault
    "Return default path to `gdb` command or nil if no
     suitable `gdb` executable is found"

    | exe |

    "/ On Windows, look for MSYS2 installation and
    "/ try to pick GDB matching current arch (i686
    "/ or x86_64) since on Windows, there's no GDB
    "/ supporting both targets :-(
    OperatingSystem isMSWINDOWSlike ifTrue:[ 
        ExternalAddress pointerSize == 8 ifTrue:[
            exe := 'C:\msys64\mingw64\bin\gdb.exe'.
            exe asFilename exists ifTrue:[ ^ exe ].
            exe := 'C:\msys64\usr\bin\gdb.exe'.
            exe asFilename exists ifTrue:[ ^ exe ].
        ] ifFalse:[
            exe := 'C:\msys64\mingw32\bin\gdb.exe'.
            exe asFilename exists ifTrue:[ ^ exe ].
        ]
    ].

    "/ Search for `gdb` along the PATH...
    exe := OperatingSystem pathOfCommand: 'gdb'.
    exe notNil ifTrue:[ ^ exe ].   

    "/ Nothing found :-(
    ^ nil

    "
    GDBProcess  gdbCommand
    UserPreferences current gdbCommand: nil.
    UserPreferences current gdbCommand: '/home/jv/Projects/gdb/users_jv_vdb/gdb/gdb'.

      

    "

    "Created: / 17-06-2019 / 14:20:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

gdbCommandParseAndValidate:command 
    "Given a `command`, checks whether it is a valid gdb command.
     Returns absolute path to gdb binary and global arguments (if any)
     or raise an GDBError if `command` is not valid hg command."
    
    | tokens  executable  executableAsFilename  arguments  version |

    command isEmptyOrNil ifTrue:[
        GDBError 
            raiseErrorString:('''gdb'' executable not configured and not found in standard places!!').
        ^ nil
    ].
    command asFilename exists ifTrue:[
        executable := command.
        arguments := #().
    ] ifFalse:[
        tokens := GDBShellCommandParser parse:command.
        executable := tokens first.
        arguments := tokens copyFrom:2.
    ].
    executableAsFilename := executable asFilename.
    executableAsFilename isAbsolute ifFalse:[
        executableAsFilename := executableAsFilename asAbsoluteFilename.
        executableAsFilename exists ifTrue:[
            executable := executableAsFilename pathName.
        ] ifFalse:[
            "/ Also try to find specified command along PATH, maybe somebody
            "/ just typed 'gdb' in...
            executable := (OperatingSystem pathOfCommand:executable).
            executable isNil ifTrue:[
                GDBError 
                    raiseErrorString:('''gdb'' executable (%1) not found!!' bindWith:command).
                ^ nil
            ] ifFalse:[
                executableAsFilename := executable asFilename.
            ].
        ]
    ].
    executableAsFilename exists ifFalse:[
        GDBError 
            raiseErrorString:('Specified ''gdb'' executable (%1) does not exists!!' 
                    bindWith:executable).
        ^ nil
    ].
    executableAsFilename isDirectory ifTrue:[
        GDBError 
            raiseErrorString:('Specified ''gdb'' executable (%1) is actually a directory!!' 
                    bindWith:executable).
        ^ nil
    ].
    executableAsFilename isExecutable ifFalse:[
        GDBError 
            raiseErrorString:('Specified ''gdb'' executable (%1) is cannot be executed!!' 
                    bindWith:executable).
        ^ nil
    ].
     "
     [
     version := self gdbVersionOf: executable arguments: arguments.
     ] on: Error do:[:ex |
     HGInvalidExecutableError newException
     parameter: ex;
     messageText: 'Failed to check version: ', ex description;
     raise.
     ^ nil
     ].
     (self gdbVersionIsSupported: version) ifFalse:[
     HGInvalidVersionError newException
     parameter: version;
     messageText: ('Unsuported Mercurial version (%1)' bindWith: ((version collect:[:e|e printString]) asStringWith:$.));
     raise.
     ^ nil
     ].
    "
    ^ (Array with:executable) , arguments

    "
     GDBProcess  gdbCommand
     GDBProcess gdbCommandParseAndValidate: 'gdb'
     GDBProcess gdbCommandParseAndValidate: 'gdb -q'
"

    "Created: / 18-06-2019 / 10:26:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 20-06-2019 / 11:06:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

gdbExecutable
    <resource: #obsolete>

    self obsoleteFeatureWarning: 'Renamed to #gdbCommand'.
    ^ self gdbCommand

    "Created: / 01-03-2015 / 08:07:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 07-10-2018 / 07:59:57 / jv"
    "Modified: / 13-12-2018 / 11:33:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

gdbExecutable: aString
     <resource: #obsolete>

    self obsoleteFeatureWarning: 'Renamed to #gdbCommand'.   
    self gdbCommand: aString

    "Created: / 01-03-2015 / 08:07:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-12-2018 / 11:33:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBProcess class methodsFor:'queries'!

isAbstract
    "Return if this class is an abstract class.
     True is returned here for myself only; false for subclasses.
     Abstract subclasses must redefine again."

    ^ self == GDBProcess.
! !

!GDBProcess methodsFor:'accessing'!

connection:aGDBConnection
    connection := aGDBConnection.
!

consoleInput
    "Return a write stream on debugger's CLI channel. CLI commands
     should be written here. If this process does not have a CLI
     channel opened, `nil` is returned. In this case, CLI must be
     emulated - if required. See `-interpreter-exec` MI command.

     CLI channel is generally available only on Smalltalk/X and only
     on UNIX platform where this stream points to TTY."

    ^ nil

    "Created: / 02-06-2017 / 23:35:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 15-12-2017 / 23:58:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 17-10-2018 / 22:20:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

consoleOutput
    "Return a read stream on debugger's CLI channel. CLI output
     should be read from. If this process does not have a CLI
     channel opened, `nil` is returned. In this case, CLI must be
     emulated - if required.

     CLI channel is generally available only on Smalltalk/X and only
     on UNIX platform where this stream points to TTY."

    ^ nil

    "Created: / 02-06-2017 / 23:35:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 15-12-2017 / 23:58:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 17-10-2018 / 22:21:05 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

debuggerInput
    "Return a write stream on debugger's MI channel. (MI) commands
     should be written there."

    ^ debuggerInput

    "Modified: / 15-12-2017 / 23:58:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 17-10-2018 / 22:14:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

debuggerOutput
    "Return read stream on debuugger's MI channel. Command responses
     and events should be read from here"

    ^ debuggerOutput

    "Modified: / 15-12-2017 / 23:58:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 17-10-2018 / 22:15:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

id
    "Return a string identification of this GDBProcess. 
     Used for debugging purposes only."

    ^ self subclassResponsibility

    "Created: / 19-10-2018 / 10:08:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

nativeTargetFeatures    
    "Return default native target features. This is used when
     used GDB does not support =target-connected and =target-disconnected
     events."

    "/ Be conservative and  report no features at all...
    ^ #()

    "Created: / 09-04-2018 / 15:40:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 17-10-2018 / 22:12:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBProcess methodsFor:'queries'!

canUsePTY
    "Return true if PTYs can be used with this process, false otherwise"

    ^ false

    "Created: / 16-01-2019 / 23:02:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBProcess class methodsFor:'documentation'!

version_HG

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