Files
neinterm/System/9P/Debug.HC
2025-10-07 10:51:07 -04:00

133 lines
2.9 KiB
HolyC

// debugging drawterm
#define _TVERSION 100
#define _TAUTH 102
#define _TATTACH 104
#define _TERROR 106
#define _TFLUSH 108
#define _TWALK 110
#define _TOPEN 112
#define _TCREATE 114
#define _TREAD 116
#define _TWRITE 118
#define _TCLUNK 120
#define _TREMOVE 122
#define _TSTAT 124
#define _TWSTAT 126
U0 @9p_debug_printf(U8* fmt, ...)
{
U8* buf;
if (argc) {
buf = StrPrintJoin(NULL, fmt, argc, argv);
} else {
buf = StrNew(fmt);
}
U8* s = buf;
while (*s) {
OutU8(0xe9, *s++);
}
Free(buf);
}
U8* @9p_debug_get_code_str(I64 code)
{
switch (code) {
case _TVERSION:
return "Tversion";
case _TAUTH:
return "Tauth";
case _TATTACH:
return "Tattach";
case _TERROR:
return "Terror";
case _TFLUSH:
return "Tflush";
case _TWALK:
return "Twalk";
case _TOPEN:
return "Topen";
case _TCREATE:
return "Tcreate";
case _TREAD:
return "Tread";
case _TWRITE:
return "Twrite";
case _TCLUNK:
return "Tclunk";
case _TREMOVE:
return "Tremove";
case _TSTAT:
return "Tstat";
case _TWSTAT:
return "Twstat";
case 1 + _TVERSION:
return "Rversion";
case 1 + _TAUTH:
return "Rauth";
case 1 + _TATTACH:
return "Rattach";
case 1 + _TERROR:
return "Rerror";
case 1 + _TFLUSH:
return "Rflush";
case 1 + _TWALK:
return "Rwalk";
case 1 + _TOPEN:
return "Ropen";
case 1 + _TCREATE:
return "Rcreate";
case 1 + _TREAD:
return "Rread";
case 1 + _TWRITE:
return "Rwrite";
case 1 + _TCLUNK:
return "Rclunk";
case 1 + _TREMOVE:
return "Rremove";
case 1 + _TSTAT:
return "Rstat";
case 1 + _TWSTAT:
return "Rwstat";
default:
return "(unimplemented)";
}
}
U0 @9p_debug_dump(U8* dat, U64 ndat, I64 mode)
{
I64 i, j, step;
U8 buf[17];
buf[16] = 0;
@9p_debug_printf("dump: ptr=%p, length=%d, code: %s, mode=%s\n", dat, ndat, @9p_debug_get_code_str(dat[4]), @t(mode == 1, "tx", "rx"));
for (i = 0; i < 32; i++)
@9p_debug_printf("=");
@9p_debug_printf("\n");
i = 0;
while (i < ndat) {
@9p_debug_printf("%08x: ", i);
step = @t(ndat - i > 16, 16, ndat - i);
for (j = 0; j < 16; j++) {
if (j > step - 1) {
@9p_debug_printf(" ");
buf[j] = 0;
} else {
@9p_debug_printf("%02x ", dat[i + j]);
switch (dat[i + j]) {
case ' ' ... 'z':
buf[j] = dat[i + j];
break;
default:
buf[j] = '.';
break;
}
}
}
@9p_debug_printf("%s\n", buf);
i += step;
}
@9p_debug_printf("\n");
}