debug.c 1.2 KB

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