GDBLocalProcess.st
author Jan Vrany <jan.vrany@labware.com>
Fri, 08 Sep 2023 12:40:22 +0100
changeset 317 7f63737e0374
parent 293 d1422e1ee1bd
permissions -rw-r--r--
Fix `GDBMIDebugger` after rename of `GDBStXUnixProcess` to `GDBUnixProcess` ...in commit d1422e1ee.

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

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: 'jv:libgdbs' }"

"{ NameSpace: Smalltalk }"

GDBProcess subclass:#GDBLocalProcess
	instanceVariableNames:'command pid'
	classVariableNames:''
	poolDictionaries:''
	category:'GDB-Internal'
!

!GDBLocalProcess class methodsFor:'documentation'!

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

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

documentation
"
    GDBLocalProcess is a specialization of GDBProcess that spawns a
    local GDB process on a host machine. Spawning a running of GDB
    process is ully managed by an (sub)instance of `GDBLocalProcess`
    Due to platform differences, there are concrete variants for 
    different platforms.

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

    [instance variables:]

    [class variables:]

    [see also:]
        GDBStXUnixProcess
        GDBStXWindowsProcess
        GDBRemoteProcess

"
! !

!GDBLocalProcess class methodsFor:'instance creation'!

newWithCommand: command
    "Return a new local GDBLocalProcess suitable for this platform 
     using `command` to launch GDB.

     If `command` is nil, default configured command is used
     (See GDBProcess class >> gdbExecutable)
    "
    self == GDBLocalProcess ifTrue:[
        OperatingSystem isUNIXlike ifTrue:[
            ^ GDBUnixProcess basicNew initializeWithCommand: command
        ].
        ^ GDBSimpleProcess basicNew initializeWithCommand: command
    ].
    ^ self basicNew initializeWithCommand: command


    "
     GDBProcess new release."

    "Created: / 12-12-2018 / 22:19:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 25-07-2023 / 12:50:55 / Jan Vrany <jan.vrany@labware.com>"
! !

!GDBLocalProcess methodsFor:'accessing'!

command
    ^ command

    "Created: / 27-03-2019 / 09:46:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

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

    ^ pid

    "Created: / 19-10-2018 / 10:09:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 20-10-2018 / 06:53:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBLocalProcess methodsFor:'initialization & release'!

initialize
    "Initializes itself using default gdb command"

    self initializeWithCommand: nil

    "Created: / 12-12-2018 / 20:11:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 12-12-2018 / 22:17:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

initializeWithCommand: aString
    "Initializes itself using  given command (`aString`) to launch GDB. 
     If `aString` is nil, default configured command is used
     (See GDBProcess class >> gdbExecutable)"

    self subclassResponsibility

    "Created: / 12-12-2018 / 20:11:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (comment): / 27-03-2019 / 08:31:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

release
    (pid notNil and:[pid > 1]) ifTrue:[
        OperatingSystem sendSignal:(OperatingSystem sigTERM) to: pid.       
    ].

    "Created: / 20-10-2018 / 07:12:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBLocalProcess methodsFor:'private'!

command2argv: commandOrNil
    "Parse given `commandOrNil` string and return an array of tokens
     suitable for passing back to `exec()` family of functions.

     If `commandOrNil` is `nil`, then use default GDB command.
     On error, thrown GDBError.
    "

    | cmd argv |


    commandOrNil isNil ifTrue:[ 
        cmd := self class gdbCommand.
        cmd isEmptyOrNil ifTrue:[ 
            GDBError signal: 'GDB not found. Please set GDB command - `UserPreferences current gdbCommand:''...''`'.
            ^ nil.
        ].
    ] ifFalse:[ 
        cmd := commandOrNil.
    ].
    argv := self class gdbCommandParseAndValidate:cmd.

    "/ As a courtesy to the us who use GDB from build tree, try to detect
    "/ and automatically add --data-directory if no arguments are specified.
    "/ For example:
    "/ 
    "/   GDBLocalProcess basicNew command2argv: '/some/path/to/my/gdb/gdb'
    "/ 
    "/ returns
    "/ 
    "/   #('/some/path/to/my/gdb/gdb' '--data-directory' '/some/path/to/my/gdb/data-directory')
    "/ 

    argv size == 1 ifTrue:[ 
        | dir |

        dir := argv first asFilename directory.
        (dir baseName = 'gdb' and:[ (dir / 'data-directory') isDirectory ]) ifTrue:[ 
            argv := argv , (Array with: '--data-directory' with: (dir / 'data-directory') pathName)
        ].
    ].
    ^ argv

    "
    GDBLocalProcess basicNew command2argv: '/home/jv/Projects/gdb/users_jv_patches/gdb/gdb'
    GDBLocalProcess basicNew command2argv: '/home/jv/Projects/gdb/users_jv_patches/gdb/gdb -nx'
    GDBLocalProcess basicNew command2argv: '/usr/bin/gdb'
    GDBLocalProcess basicNew command2argv: 'gdb'   
    GDBLocalProcess basicNew command2argv: nil
    "

    "Created: / 17-12-2018 / 10:48:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified (format): / 20-06-2019 / 11:17:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

exited: status
    "Called when spawn GDB process terminates for whatever reason"
    pid := nil.

    "/ connection may be nil if GDBProcess instance is used
    "/ on it own, without GDBConnection (such as in GDBMIDebugger)
    connection notNil ifTrue:[
        connection released: status
    ].

    "Created: / 20-06-2014 / 21:35:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 19-06-2019 / 10:50:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBLocalProcess methodsFor:'testing'!

isLocal
    ^ true

    "Created: / 27-03-2019 / 09:23:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBLocalProcess class methodsFor:'documentation'!

version_HG

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