Emit and handle (custom) `-register-changed` notification default tip
authorJan Vrany <jan.vrany@labware.com>
Thu, 07 Dec 2023 12:33:31 +0000
changeset 322 1b26d0a9560c
parent 321 3b841438d14b
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.
GDBDebugger.st
GDBRegisterChangedEvent.st
GDBRegisterWithValue.st
python/libgdbs.py
--- a/GDBDebugger.st	Thu Dec 07 15:41:12 2023 +0000
+++ b/GDBDebugger.st	Thu Dec 07 12:33:31 2023 +0000
@@ -1194,6 +1194,18 @@
     "Created: / 18-11-2021 / 23:10:21 / Jan Vrany <jan.vrany@labware.com>"
 !
 
+onRegisterChangedEvent: aGDBRegisterChangedEvent
+    "Some register has changed. We treat it here as if complete state of
+     the inferior has changed since one may change SP / PC / FP which then
+     affects thread state.
+
+     We could in theory only invalidate specific thread, but won't buy us
+     much."
+    self nextInferiorStateSequnceNumber.
+
+    "Created: / 04-09-2023 / 16:26:44 / Jan Vrany <jan.vrany@labware.com>"
+!
+
 onRunningEvent: aGDBRunningEvent
     | threads threadId |
 
@@ -1463,11 +1475,13 @@
 
         when: GDBCmdParamChangedEvent       send: #onCmdParamChangedEvent:    to: self;
 
-        when: GDBTargetConnectedEvent       send: #onTargetConnectedEvent:    to: self.
+        when: GDBTargetConnectedEvent       send: #onTargetConnectedEvent:    to: self;
+
+        when: GDBRegisterChangedEvent       send: #onRegisterChangedEvent:    to: self.
 
     "Created: / 20-06-2014 / 22:07:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 22-01-2019 / 13:36:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 18-11-2021 / 23:11:08 / Jan Vrany <jan.vrany@labware.com>"
+    "Modified: / 04-09-2023 / 16:25:40 / Jan Vrany <jan.vrany@labware.com>"
 !
 
 unsubscribe
--- a/GDBRegisterChangedEvent.st	Thu Dec 07 15:41:12 2023 +0000
+++ b/GDBRegisterChangedEvent.st	Thu Dec 07 12:33:31 2023 +0000
@@ -24,8 +24,8 @@
 
 "{ NameSpace: Smalltalk }"
 
-GDBInternalEvent subclass:#GDBRegisterChangedEvent
-	instanceVariableNames:'register'
+GDBNotificationEvent subclass:#GDBRegisterChangedEvent
+	instanceVariableNames:'frame regnum'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'GDB-Events'
@@ -58,15 +58,34 @@
 "
 ! !
 
-!GDBRegisterChangedEvent methodsFor:'accessing'!
+!GDBRegisterChangedEvent class methodsFor:'accessing - GDB value descriptors'!
 
-register
-    ^ register
+description
+    ^ (super description)
+        define:#frame as:Integer;
+        define:#regnum as:Integer;
+        yourself
+
+    "Created: / 18-11-2021 / 23:04:07 / Jan Vrany <jan.vrany@labware.com>"
+    "Modified: / 18-09-2023 / 17:41:00 / Jan Vrany <jan.vrany@labware.com>"
 ! !
 
 !GDBRegisterChangedEvent methodsFor:'initialization'!
 
-setRegister: aGDBRegisterWithValue
-    register := aGDBRegisterWithValue
+setFrame: aGDBFrameOrInteger
+    self assert: frame isNil.
+
+    frame := aGDBFrameOrInteger
+
+    "Created: / 06-12-2023 / 11:11:24 / Jan Vrany <jan.vrany@labware.com>"
+!
+
+setRegnum: anInteger
+    self assert: regnum isNil.
+    self assert: anInteger isInteger.
+
+    regnum := anInteger
+
+    "Created: / 06-12-2023 / 11:12:35 / Jan Vrany <jan.vrany@labware.com>"
 ! !
 
--- a/GDBRegisterWithValue.st	Thu Dec 07 15:41:12 2023 +0000
+++ b/GDBRegisterWithValue.st	Thu Dec 07 12:33:31 2023 +0000
@@ -73,6 +73,10 @@
 
 !GDBRegisterWithValue methodsFor:'accessing'!
 
+frame
+    ^ frame
+!
+
 name
     ^  register name
 
@@ -147,12 +151,12 @@
             result isDone ifTrue: [
                 frame invalidate: self.
                 changed value: true.
-                debugger push: (GDBRegisterChangedEvent new setRegister: self)
             ].
         ].
 
     "Created: / 10-09-2021 / 15:31:35 / Jan Vrany <jan.vrany@labware.com>"
     "Modified: / 18-11-2021 / 12:13:34 / Jan Vrany <jan.vrany@labware.com>"
+    "Modified: / 06-12-2023 / 11:17:46 / Jan Vrany <jan.vrany@labware.com>"
 !
 
 valueString
@@ -179,7 +183,6 @@
         ].
         (previous notNil and: [ previous ~= valueString ]) ifTrue: [
             changed value: true.
-            debugger push: (GDBRegisterChangedEvent new setRegister: self)
         ].
     ] ifFalse: [
         changed value.
@@ -188,6 +191,7 @@
 
     "Created: / 10-09-2021 / 14:14:14 / Jan Vrany <jan.vrany@labware.com>"
     "Modified: / 18-11-2021 / 18:03:18 / Jan Vrany <jan.vrany@labware.com>"
+    "Modified: / 06-12-2023 / 11:17:20 / Jan Vrany <jan.vrany@labware.com>"
 ! !
 
 !GDBRegisterWithValue methodsFor:'displaying'!
--- a/python/libgdbs.py	Thu Dec 07 15:41:12 2023 +0000
+++ b/python/libgdbs.py	Thu Dec 07 12:33:31 2023 +0000
@@ -23,4 +23,19 @@
 #
 import gdb
 
+#
+# Register couple of events not (yet) supported by the
+# GDB/MI.
+#
+if hasattr(gdb, 'notify_mi'):
+    def __on_register_change(event):
+        # Sadly, there's no way to get from gdb.Frame to gdb.Thread (and to gdb.Inferior)
+        gdb.notify_mi('-register-changed', { 'frame' : event.frame.level() , 'regnum' : event.regnum })
+
+    gdb.events.register_changed.connect(__on_register_change)
+else:
+    gdb.write("This GDB has no support for custom GDB/MI notifications\n" +
+              "This means LibGDBs cannot track register changes made \n"  +
+              "from GDB CLI. You may want to upgrade your GDB.")
+
 loaded = True