xref: /plan9/sys/src/liboventi/debug.c (revision 368c31ab13393dea083228fdd1c3445076f83a4b)
1 #include <u.h>
2 #include <libc.h>
3 #include <oventi.h>
4 #include "session.h"
5 
6 void vtDumpSome(Packet*);
7 
8 void
vtDebug(VtSession * s,char * fmt,...)9 vtDebug(VtSession *s, char *fmt, ...)
10 {
11 	va_list arg;
12 
13 	if(!s->debug)
14 		return;
15 
16 	va_start(arg, fmt);
17 	vfprint(2, fmt, arg);
18 	va_end(arg);
19 }
20 
21 void
vtDebugMesg(VtSession * z,Packet * p,char * s)22 vtDebugMesg(VtSession *z, Packet *p, char *s)
23 {
24 	int op;
25 	int tid;
26 	int n;
27 	uchar buf[100], *b;
28 
29 
30 	if(!z->debug)
31 		return;
32 	n = packetSize(p);
33 	if(n < 2) {
34 		fprint(2, "runt packet%s", s);
35 		return;
36 	}
37 	b = packetPeek(p, buf, 0, 2);
38 	op = b[0];
39 	tid = b[1];
40 
41 	fprint(2, "%c%d[%d] %d", ((op&1)==0)?'R':'Q', op, tid, n);
42 	vtDumpSome(p);
43 	fprint(2, "%s", s);
44 }
45 
46 void
vtDumpSome(Packet * pkt)47 vtDumpSome(Packet *pkt)
48 {
49 	int printable;
50 	int i, n;
51 	char buf[200], *q, *eq;
52 	uchar data[32], *p;
53 
54 	n = packetSize(pkt);
55 	printable = 1;
56 	q = buf;
57 	eq = buf + sizeof(buf);
58 	q = seprint(q, eq, "(%d) '", n);
59 
60 	if(n > sizeof(data))
61 		n = sizeof(data);
62 	p = packetPeek(pkt, data, 0, n);
63 	for(i=0; i<n && printable; i++)
64 		if((p[i]<32 && p[i] !='\n' && p[i] !='\t') || p[i]>127)
65 				printable = 0;
66 	if(printable) {
67 		for(i=0; i<n; i++)
68 			q = seprint(q, eq, "%c", p[i]);
69 	} else {
70 		for(i=0; i<n; i++) {
71 			if(i>0 && i%4==0)
72 				q = seprint(q, eq, " ");
73 			q = seprint(q, eq, "%.2X", p[i]);
74 		}
75 	}
76 	seprint(q, eq, "'");
77 	fprint(2, "%s", buf);
78 }
79