GDBLocalProcess.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 20 Jun 2019 11:24:54 +0100
changeset 195 17a6f1d1cb22
parent 194 312d96017653
child 259 651864c2aa29
permissions -rw-r--r--
Autodetect GDB data directory 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 in GDB command (`GDBProcess gdbCommand`).

"
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 }"

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

!GDBLocalProcess 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
"
    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:[
        Smalltalk isSmalltalkX ifTrue:[
            OperatingSystem isUNIXlike ifTrue:[
                ^ GDBStXUnixProcess basicNew initializeWithCommand: command
            ].
            ^ GDBStXSimpleProcess basicNew initializeWithCommand: command
        ].
    ].
    ^ self basicNew initializeWithCommand: command


    "
     GDBProcess new release."

    "Created: / 12-12-2018 / 22:19:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

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