GDBMIPrinter.st
author Jan Vrany <jan.vrany@labware.com>
Thu, 07 Dec 2023 12:33:31 +0000
changeset 322 1b26d0a9560c
parent 272 cdd1c9ad00de
permissions -rw-r--r--
Emit and handle (custom) `-register-changed` notification This commit adds new (custom) asynchronous notification about register value being changed. Standard GDB does not notify MI clients about register value being changed when debugging (for example, by CLI command `set $rax = 1` or via Python's `Value.assign()`). This caused libgdb's register value cache being out of sync. In the past, this was partially worked around by manually emiting the notification on `GDBRegisterWithValue` APIs, but this did not (and could not) handle the case register was changed from GDB command line. To solve this problem, this commit installs a custom Python event handler that emits new GDB/MI notification - `-register-changed` - whenever a register changes after debugee is stopped. This has been enabled by upstream GDB commit 4825fd "gdb/python: implement support for sending custom MI async notifications" On libgdbs side, complete inferior state is invalidated. In theory, one could carefully invalidate only the changed `GDBRegisterWithValue` but in certain cases this could also change the backtrace (for example, if one updates stack pointer) or position in code. So it seems safer to just invalidate everything.

"{ Encoding: utf8 }"

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

Object subclass:#GDBMIPrinter
	instanceVariableNames:'stream'
	classVariableNames:''
	poolDictionaries:''
	category:'GDB-Internal'
!

!GDBMIPrinter class methodsFor:'documentation'!

copyright
"
jv:libgdbs - GNU Debugger Interface Library
Copyright (C) 2015-now Jan Vrany
Copyright (C) 2020 LabWare
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
"
    A writer that writes GDB commands to an MI channel usin
    GDB MI input syntax [1].




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

    [instance variables:]

    [class variables:]

    [see also:]
        [1]: https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-Input-Syntax.html#GDB_002fMI-Input-Syntax

"
! !

!GDBMIPrinter class methodsFor:'instance creation'!

on: aStream
    ^ self new setStream: aStream.

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

!GDBMIPrinter class methodsFor:'utilities'!

cescaped: string
    "Return `string` with C-style escaping"

    ^ String streamContents:[:s|(self on: s) printCEscapedString: string ]

    "Created: / 08-07-2019 / 11:21:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBMIPrinter methodsFor:'initialization'!

setStream: aStream
    stream := aStream

    "Created: / 11-07-2017 / 21:22:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBMIPrinter methodsFor:'printing'!

printBytes: aByteArray
    stream nextPut: $".
    aByteArray do:[:byte | byte printOn:stream paddedWith:$0 to:2 base:16].
    stream nextPut: $".

    "Created: / 15-06-2020 / 13:05:36 / Jan Vrany <jan.vrany@labware.com>"
!

printCEscapedCharacter: aCharacter
    stream nextPut: $\.
    aCharacter == $" ifTrue:[ stream nextPut: $". ^ self ].
    aCharacter == $\ ifTrue:[ stream nextPut: $\. ^ self ].
    aCharacter == Character backspace ifTrue:[ stream nextPut: $b. ^ self ].
    aCharacter == Character tab ifTrue:[ stream nextPut: $t. ^ self ].
    aCharacter == Character linefeed ifTrue:[ stream nextPut: $n. ^ self ].
    aCharacter == Character return ifTrue:[ stream nextPut: $r. ^ self ].   
    aCharacter == Character newPage ifTrue:[ stream nextPut: $f. ^ self ]. 
    aCharacter codePoint <= 16rFF ifTrue:[ 
        stream nextPut: $x.
        aCharacter codePoint printOn: stream base: 16.
        ^ self.
    ].
    GDBError signal: 'Unrepresentable character: \U', (aCharacter codePoint printStringRadix: 16)

    "Created: / 11-07-2017 / 22:12:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 21-10-2018 / 08:06:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

printCEscapedString: aString
    "
    seven-bit-iso-c-string-content
    "
    | start stop |
    start := 1.
    [ start <= aString size ] whileTrue:[ 
        | char |
        stop := start.
        [ 
          stop > aString size ifTrue:[ 
            stream nextPutAll:aString startingAt: start to: stop - 1.
            ^ self.
          ].
          char := aString at: stop. 
          stop := stop + 1.
          char == Character space or:[(self needCEscaping: char) not] ] whileTrue.
        stream nextPutAll:aString startingAt: start to: stop - 2.
        self printCEscapedCharacter: char.
        start := stop.
    ].

    "Created: / 13-03-2019 / 12:12:42 / jv"
!

printCString: aString
    "
    c-string → ''' seven-bit-iso-c-string-content '''
    "
    stream nextPut: $".
    self printCEscapedString: aString.
    stream nextPut: $".

    "Created: / 11-07-2017 / 22:05:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-03-2019 / 12:13:10 / jv"
!

printCommand: aGDBCommand
    "
    command → cli-command | mi-command
    "
    aGDBCommand isMICommand ifTrue:[ 
        self printCommandMI: aGDBCommand
    ] ifFalse:[ 
        self printCommandCLI: aGDBCommand.
    ].

    "Created: / 11-07-2017 / 21:29:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

printCommandCLI: aGDBCLICommand
    "
    cli-command → [ token ] cli-command nl, where cli-command is any existing GDB CLI command.
    "
    "/ Although MI allows "bare" CLI commands to be issued as well,
    "/ there's a problem with C-escaping - some commands need C-escaping,
    "/ some other do not. 
    "/
    "/ Try to address this by using -interpreter-exec CLI command to
    "/ send it to GDB.
    self printCommandMI: 
        (GDBMI_interpreter_exec new
            token: aGDBCLICommand token;
            arguments: (Array with: 'console' with: aGDBCLICommand value))

    "/ Original implementation, left for reference:
"/ aGDBCLICommand token notNil ifTrue:[ 
"/        aGDBCLICommand token printOn: stream.
"/    ].
"/    self printCEscapedString: aGDBCLICommand value.
"/    aGDBCLICommand runOnBackground ifTrue:[ 
"/        stream space; nextPut:$&  
"/    ].

    "Created: / 11-07-2017 / 21:33:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 13-03-2019 / 12:15:36 / jv"
    "Modified (format): / 08-07-2019 / 10:56:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

printCommandMI: aGDBMICommand
    "
    mi-command → [ token ] '-' operation ( ' ' option )* [ ' --' ] ( ' ' parameter )* nl
    "

    | maybeOption |

    aGDBMICommand token notNil ifTrue:[ 
        aGDBMICommand token printOn: stream.
    ].
    stream nextPut:$-; nextPutAll: aGDBMICommand operation.

    "/ Now, this is tricky. We have no distinction between options :-(
    maybeOption := true.
    aGDBMICommand arguments notEmptyOrNil ifTrue:[
        aGDBMICommand arguments do:[:each | 
            stream space.
            each = '--' ifTrue:[ 
                maybeOption := false.
                stream nextPutAll: '--'.
            ] ifFalse:[
                (maybeOption and:[each isString and:[each first = $-]]) ifTrue:[
                    self printOption: each
                ] ifFalse:[ 
                    self printParameter: each.
                ].
            ].
        ].
    ].

    "Created: / 11-07-2017 / 21:36:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 22-06-2018 / 12:00:43 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 15-06-2020 / 13:08:29 / Jan Vrany <jan.vrany@labware.com>"
!

printInteger: anInteger
    anInteger printOn: stream

    "Created: / 15-06-2020 / 13:11:12 / Jan Vrany <jan.vrany@labware.com>"
!

printNl
    "
    nl → CR | CR-LF 

    "
    stream nextPut: Character return.

    "Created: / 11-07-2017 / 21:33:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

printNonBlankSequence: aString
    "
    non-blank-sequence → anything, provided it doesn’t contain special characters such as '-', nl, ''' and of course ' '
    "    
    stream nextPutAll: aString

    "Created: / 11-07-2017 / 21:54:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

printOption: aString
    "
    option → '-' parameter [ ' ' parameter ]
    "
    | i |

    i := 1.
    [ i <= aString size and:[ (aString at: i) == $- ] ] whileTrue:[
        i := i + 1.
    ].
    i to: aString size do:[:i |
        | c |

        c := aString at: i.
        (self needCEscaping: c) ifTrue:[ 
            self printParameter: aString.
            ^ self
        ].
    ].
    stream nextPutAll: aString.

    "Created: / 11-07-2017 / 21:51:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 11-07-2017 / 23:21:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

printParameter: anObject
    "
    parameter → non-blank-sequence | c-string
    "    
    anObject isByteArray ifTrue:[ 
        self printBytes: anObject  
    ] ifFalse:[ anObject isInteger ifTrue:[ 
        self printInteger: anObject.
    ] ifFalse:[
        | value |

        value := anObject asString.
        (self isNonBlankSequence: value) ifTrue:[
            self printNonBlankSequence: value
        ] ifFalse:[ 
            self printCString: value
        ].
    ]]

    "Created: / 11-07-2017 / 21:52:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 15-06-2020 / 13:10:49 / Jan Vrany <jan.vrany@labware.com>"
! !

!GDBMIPrinter methodsFor:'testing'!

isNonBlankSequence: aString
    ^ aString allSatisfy:[:each | (self needCEscaping: each) not ].

    "Created: / 11-07-2017 / 21:53:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 11-07-2017 / 23:26:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

needCEscaping: aCharacter
    ^ aCharacter == $\ 
        or:[ aCharacter == $" 
        or:[ (aCharacter codePoint between: 33 and: 16r7F) not ]]

    "Created: / 11-07-2017 / 22:42:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!GDBMIPrinter class methodsFor:'documentation'!

version_HG

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