VDBStartup.st
author Jan Vrany <jan.vrany@labware.com>
Tue, 26 Jul 2022 15:01:33 +0100
changeset 265 f2470f0dd9cd
parent 264 23960fcb9dac
permissions -rw-r--r--
Do not show address for (pseudo) instructions with no code While such instructions do not appear in GDB-produced disassembly, they may appear in some manually-generated instruction lists. One example of such (pseudo) instruction is label.

"
jv:libgdbs - GNU Debugger Interface Library
Copyright (C) 2015-now Jan Vrany
Copyright (C) 2020 LabWare
Copyright (C) 2022 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:vdb' }"

"{ NameSpace: Smalltalk }"

StandaloneStartup subclass:#VDBStartup
	instanceVariableNames:''
	classVariableNames:'Standalone'
	poolDictionaries:''
	category:'VDB-UI'
!

!VDBStartup class methodsFor:'documentation'!

copyright
"
jv:libgdbs - GNU Debugger Interface Library
Copyright (C) 2015-now Jan Vrany
Copyright (C) 2020 LabWare
Copyright (C) 2022 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
"
    This is the entrypoint to start VDB.

    Examples: 

    *  start VDB with no command line options/parameters

        VDBStartup main: { }.   

    * start VDB with --no-pty command line option:

        VDBStartup main: { '--no-pty' }.  

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

    [instance variables:]

    [class variables:]
        Standalone

    [see also:]

"
! !

!VDBStartup class methodsFor:'constants & defaults'!

applicationRegistryPath
    "the key under which this application stores its process ID in the registry
     as a collection of path-components.
     i.e. if #('foo' 'bar' 'baz') is returned here, the current applications ID will be stored
     in HKEY_CURRENT_USER\Software\foo\bar\baz\CurrentID.
     (would also be used as a relative path for a temporary lock file under unix).
     Used to detect if another instance of this application is already running."

    ^ #('vdb')

    "Modified: / 21-09-2014 / 01:29:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

applicationUUID
    "answer an application-specific unique uuid.
     This is used as the name of some exclusive OS-resource, which is used to find out,
     if another instance of this application is already running.
     Under win32, a mutex is used; under unix, an exclusive file in the tempDir could be used.
     If redefined, please return a real UUID (i.e. UUID fromString:'.....') and not a string or
     similar possibly conflicting identifier.
     You can paste a fresh worldwide unique id via the editor's more-misc-paste UUID menuFunction."

    ^ UUID fromString:'57b09330-4126-11e4-a80f-606720e43e2c'

    "Modified: / 21-09-2014 / 01:29:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!VDBStartup class methodsFor:'defaults'!

allowDebugOption
    "enable/disable the --debug startup option.
     Can be redefined in subclasses to enable it"

    ^ true

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

!VDBStartup class methodsFor:'private'!

loadPreferenceFile: file

    "Created: / 07-06-2017 / 09:49:58 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!VDBStartup class methodsFor:'queries'!

applicationName
    ^ 'vdb'

    "Created: / 06-06-2017 / 22:50:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

isStandalone
    Standalone isNil ifTrue: [^ Smalltalk isStandAloneApp ].
    ^ Standalone

    "Created: / 17-11-2020 / 13:08:20 / Jan Vrany <jan.vrany@labware.com>"
! !

!VDBStartup class methodsFor:'startup'!

main:argv
    "Application entry point. `argv` is the array of command arguments (as Array of Strings)"

    | optparser positional settingsFile settingsSuppressed cmd pySysDir1 noPTY replay
      programExecutable programArgs programPid attach
      debugger debuggerApp |

    "/ This is awkward. We HAVE TO load prefetences when VDB is compiled standalone
    "/ application or run from a command line (`--load jv:vdb` --run VDBStarup`.
    "/ We MUST NOT load preferences when `VDBStarup main` is run from running IDE.
    "/ for example while developing.
    "/
    "/ The trick is that there's no good and easy way to check this, so we have to
    "/ check whether this method is called from `Smalltalk >> start`. Bleh.
    Standalone := Smalltalk isStandAloneApp
                    or:[ thisContext sender method == (Smalltalk class compiledMethodAt: #start) ].
    settingsSuppressed := Standalone not.

    noPTY := false.
    replay := false.
    attach := false.
    cmd := GDBProcess gdbCommand.

    "/ Parse options...
    optparser := CmdLineParser new
                    ignoreUnknownOptions: true;
                    on: #('--help') do:[
                        self usage
                    ];
                    on: #('--preferences') do:[:filename |
                        | file |

                        file := filename asFilename.
                        file isReadable ifFalse:[
                            self error: 'preference file does not exists or not readable: ' , filename.
                        ].
                        file isRegularFile ifFalse:[
                            self error: 'preference file is not a regular file: ' , filename.
                        ].
                        settingsFile := file.
                    ];
                    on: #('--no-preferences') do:[
                        settingsSuppressed := true
                    ];
                    on: #('--replay') do:[
                        replay := true
                    ];
                    on: #('-d' '--gdb') do:[ :str |
                        cmd := str
                    ];
                    on: #('--no-pty') do:[
                        noPTY := true
                    ];
                    yourself.
    [
        positional := optparser parse:argv.
    ] on: CmdLineOptionError do:[:ex |
        self error: ex description.
    ].


    "/ Now validate and process options
    settingsSuppressed ifFalse:[
        | settings |

        settingsFile notNil ifTrue:[
            settingsFile exists ifFalse:[
                self error: 'preference file does not exist: ', settingsFile pathName
            ].
            settingsFile isDirectory ifTrue:[
                self error: 'preference file is not a regular file: ', settingsFile pathName
            ].
            settingsFile isReadable ifFalse:[
                self error: 'preference file is not a readable (check permissions): ', settingsFile pathName
            ].
            settings := UserPreferences loadSettingsFrom: settingsFile.
        ] ifFalse:[
            settings := UserPreferences loadSettings.
        ].
        UserPreferences setCurrent: settings.
    ].

    replay ifTrue:[ 
        OperatingSystem isLinuxLike ifFalse:[ 
            self error: 'replay not supported on this platform'.
        ].
        RR available ifFalse:[ 
            self error: 'cannot replay because rr not available'
        ].
    ].

    "/ Parse positional arguments - there are two forms:
    "/
    "/   vdb [OPTIONS] [PROGRAM [ARGS]]
    "/   vdb [OPTIONS] [PID]
    "/
    "/ [OPTIONS] have already been processed, the rest is in `positional`
    "/ variable

    positional notEmpty ifTrue:[
        programExecutable := positional first.
        programExecutable asFilename exists ifFalse:[
            "Try to find the executable in PATH..."

            | path |

            path := OperatingSystem pathOfCommand: programExecutable.
            path notNil ifTrue:[
                programExecutable := path.
            ].
        ].
        programPid := Integer fromString: positional first onError: [ nil ].
        programArgs := positional copyFrom: 2.

        replay ifTrue:[ 
            programArgs notEmptyOrNil ifTrue:[ 
                self error: 'cannot specify program args when replaying'.
            ].
        ] ifFalse:[
            "/ If * programExecutable does not exists
            "/    * AND programPid is not nil (i.e., first positional argument can be converted to an integer)
            "/    * AND programArguments are empty
            "/ then interpret positional argument as PID and attach to it.
            "/ Otherwise, interpret positional arguments
            (programExecutable asFilename exists not and: [ programPid notNil and: [ programArgs isEmpty ]]) ifTrue:[
                attach := true.
            ] ifFalse:[
                programExecutable asFilename exists ifFalse:[
                    self error: 'cannot find program executable: ', programExecutable.
                ].
            ].
        ].
    ].

    Debugger := DebugView ? MiniDebugger.
    Inspector := InspectorView ? MiniInspector.

    pySysDir1 := (Smalltalk getPackageDirectoryForPackage: #jv:vdb) / 'python'.
    self assert: pySysDir1 isDirectory.        

    cmd := cmd , (' -iex ''python import sys; sys.path.append("%1"); import vdb''' 
                    bindWith: (GDBMIPrinter cescaped: pySysDir1 pathName)).

    noPTY ifTrue:[ 
        debugger := GDBDebugger newWithProcess: (GDBStXSimpleProcess newWithCommand: cmd).
    ] ifFalse:[ 
        debugger := GDBDebugger newWithProcess: (GDBLocalProcess newWithCommand: cmd).
    ].


    attach ifTrue:[
        debugger attach: programPid
    ] ifFalse:[
        programExecutable notNil ifTrue:[
            debugger executable: programExecutable arguments: programArgs.
        ].
    ].
    Smalltalk openDisplay.
    debuggerApp := VDBDebuggerApplication new.
    debuggerApp debugger: debugger.
    debuggerApp open.
    replay ifTrue:[ 
        debuggerApp doAttachToRR
    ]. 

    "
        VDBStartup main: #()
        VDBStartup main: #('ls')
        VDBStartup main: #('/bin/ls' '/tmp')
    "

    "Modified: / 13-12-2018 / 15:32:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 19-10-2020 / 11:52:33 / Jan Vrany <jan.vrany@labware.com>"
!

usage
    Transcript nextPutAll:'usage: '; nextPutAll: self applicationName; nextPutAll: ' [OPTIONS] [PROGRAM [ARGS]] '; cr.
    Transcript nextPutAll:'       '; nextPutAll: self applicationName; nextPutAll: ' [OPTIONS] [PID]'; cr.
                                                                          "|"
    Transcript nextPutLine:'
options:
 --replay ..................... replay last rr record
 --no-pty ..................... do not use PTY-based GDB console even if current
                                platform supports it. 
 -d | --gdb CMD ............... use CMD to launch GDB (instead of platform
                                default).    
 --preference FILE ............ read user settings from FILE
 --no-preferences ............. do not read user settings at all
 --help ....................... output this message
'.
    Standalone ifTrue:[
        Smalltalk exit: 0.
    ].

    "Modified: / 13-12-2018 / 15:29:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!VDBStartup class methodsFor:'documentation'!

version_HG

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