@@ -291,6 +291,8 @@ func init() {
291291 registerCmdFn ("mediaview" , MediaViewCommand )
292292
293293 registerCmdFn ("csvview" , CSVViewCommand )
294+
295+ registerCmdFn ("_debug:ri" , DebugRemoteInstanceCommand )
294296}
295297
296298func getValidCommands () []string {
@@ -3783,6 +3785,91 @@ func SessionCommand(ctx context.Context, pk *scpacket.FeCommandPacketType) (scbu
37833785 return update , nil
37843786}
37853787
3788+ type statePtrInfoType struct {
3789+ IsDiff bool
3790+ BaseHash string
3791+ DiffHash string
3792+ StateSize int
3793+ }
3794+
3795+ func getStatePtrInfo (ctx context.Context , statePtr * packet.ShellStatePtr ) (statePtrInfoType , error ) {
3796+ rtn := statePtrInfoType {}
3797+ if statePtr == nil {
3798+ return rtn , fmt .Errorf ("stateptr is nil" )
3799+ }
3800+ if len (statePtr .DiffHashArr ) > 1 {
3801+ return rtn , fmt .Errorf ("stateptr has more than 1 diffhash" )
3802+ }
3803+ if len (statePtr .DiffHashArr ) == 1 {
3804+ rtn .IsDiff = true
3805+ rtn .BaseHash = statePtr .BaseHash
3806+ rtn .DiffHash = statePtr .DiffHashArr [0 ]
3807+ stateDiff , err := sstore .GetStateDiff (ctx , rtn .DiffHash )
3808+ if err != nil {
3809+ return rtn , fmt .Errorf ("cannot get state diff: %w" , err )
3810+ }
3811+ _ , encodedDiff := stateDiff .EncodeAndHash ()
3812+ rtn .StateSize = len (encodedDiff )
3813+ } else {
3814+ rtn .BaseHash = statePtr .BaseHash
3815+ state , err := sstore .GetStateBase (ctx , rtn .BaseHash )
3816+ if err != nil {
3817+ return rtn , fmt .Errorf ("cannot get state base: %w" , err )
3818+ }
3819+ _ , encodedState := state .EncodeAndHash ()
3820+ rtn .StateSize = len (encodedState )
3821+ }
3822+ return rtn , nil
3823+ }
3824+
3825+ func DebugRemoteInstanceCommand (ctx context.Context , pk * scpacket.FeCommandPacketType ) (scbus.UpdatePacket , error ) {
3826+ ids , err := resolveUiIds (ctx , pk , R_Session | R_Screen )
3827+ if err != nil {
3828+ return nil , err
3829+ }
3830+ slines , err := sstore .GetScreenLinesById (ctx , ids .ScreenId )
3831+ if err != nil {
3832+ return nil , err
3833+ }
3834+ lines := slines .Lines
3835+ if len (lines ) > 100 {
3836+ lines = lines [:100 ]
3837+ }
3838+ cmdMap := make (map [string ]* sstore.CmdType )
3839+ for _ , cmd := range slines .Cmds {
3840+ cmdMap [cmd .LineId ] = cmd
3841+ }
3842+ cmds := make ([]* sstore.CmdType , 0 , len (lines ))
3843+ for _ , line := range lines {
3844+ cmds = append (cmds , cmdMap [line .LineId ])
3845+ }
3846+ var outputLines []string
3847+ for idx , cmd := range cmds {
3848+ if cmd == nil || cmd .RtnStatePtr .IsEmpty () {
3849+ continue
3850+ }
3851+ line := lines [idx ]
3852+ info , err := getStatePtrInfo (ctx , & cmd .RtnStatePtr )
3853+ if err != nil {
3854+ outputLines = append (outputLines , fmt .Sprintf ("line %5d | err %v" , line .LineNum , err ))
3855+ continue
3856+ }
3857+ outputStr := ""
3858+ if info .IsDiff {
3859+ outputStr = fmt .Sprintf ("line %5d | diff %8s-%8s | size %8d" , line .LineNum , info .BaseHash [0 :8 ], info .DiffHash [0 :8 ], info .StateSize )
3860+ } else {
3861+ outputStr = fmt .Sprintf ("line %5d | base %8s %8s | size %8d" , line .LineNum , info .BaseHash [0 :8 ], "" , info .StateSize )
3862+ }
3863+ outputLines = append (outputLines , outputStr )
3864+ }
3865+ update := scbus .MakeUpdatePacket ()
3866+ update .AddUpdate (sstore.InfoMsgType {
3867+ InfoTitle : "remote instance" ,
3868+ InfoLines : outputLines ,
3869+ })
3870+ return update , nil
3871+ }
3872+
37863873func RemoteResetCommand (ctx context.Context , pk * scpacket.FeCommandPacketType ) (rtnUpdate scbus.UpdatePacket , rtnErr error ) {
37873874 ids , err := resolveUiIds (ctx , pk , R_Session | R_Screen | R_Remote )
37883875 if err != nil {
0 commit comments