@@ -812,6 +812,53 @@ where
812812 }
813813}
814814
815+ /// A helper struct which enables writing arbitrary text to the GDB client
816+ /// console (either via `write_raw`, or via the `core::fmt::Write`
817+ /// implementation + [`output!`](crate::output) macro).
818+ pub struct GdbConsoleWriter < ' gdb , ' a , T , C >
819+ where
820+ T : Target ,
821+ C : Connection ,
822+ {
823+ gdb : & ' gdb mut GdbStubStateMachineInner < ' a , state:: Running , T , C > ,
824+ use_rle : bool ,
825+ }
826+
827+ impl < ' gdb , ' a , T , C > GdbConsoleWriter < ' gdb , ' a , T , C >
828+ where
829+ T : Target ,
830+ C : Connection ,
831+ {
832+ /// Write raw data to the GDB client console.
833+ ///
834+ /// Unless you have a specific reason to use this method, you will likely be
835+ /// better served by using the `core::fmt::Write` implementation +
836+ /// [`output!`](crate::output) macros instead.
837+ ///
838+ /// The GDB RSP docs recommend that console output be ASCII, but in
839+ /// practice, GDB seems to be fine with receiving arbitrary binary data here
840+ /// (e.g: it's possible to write UTF-8 data just fine, and it renders
841+ /// correctly in GDB's console).
842+ pub fn write_raw ( & mut self , data : & [ u8 ] ) -> Result < ( ) , GdbStubError < T :: Error , C :: Error > > {
843+ let mut res = ResponseWriter :: new ( self . gdb . borrow_conn ( ) , self . use_rle ) ;
844+ res. write_str ( "O" ) . map_err ( InternalError :: from) ?;
845+ res. write_hex_buf ( data) . map_err ( InternalError :: from) ?;
846+ res. flush ( ) . map_err ( InternalError :: from) ?;
847+ Ok ( ( ) )
848+ }
849+ }
850+
851+ impl < ' gdb , ' a , T , C > core:: fmt:: Write for GdbConsoleWriter < ' gdb , ' a , T , C >
852+ where
853+ T : Target ,
854+ C : Connection ,
855+ {
856+ fn write_str ( & mut self , s : & str ) -> core:: fmt:: Result {
857+ self . write_raw ( s. as_bytes ( ) )
858+ . map_err ( |_| core:: fmt:: Error { } )
859+ }
860+ }
861+
815862/// Methods which can only be called from the
816863/// [`GdbStubStateMachine::Running`] state.
817864impl < ' a , T , C > GdbStubStateMachineInner < ' a , state:: Running , T , C >
@@ -831,6 +878,15 @@ where
831878 }
832879 }
833880
881+ /// Return a struct that can be used to write arbitrary text to the GDB
882+ /// client console.
883+ pub fn console_writer ( & mut self , target : & mut T ) -> GdbConsoleWriter < ' _ , ' a , T , C > {
884+ GdbConsoleWriter {
885+ gdb : self ,
886+ use_rle : target. use_rle ( ) ,
887+ }
888+ }
889+
834890 /// Pass a byte to the GDB stub.
835891 pub fn incoming_data (
836892 mut self ,
0 commit comments