debug.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <oventi.h>
  12. #include "session.h"
  13. void vtDumpSome(Packet*);
  14. void
  15. vtDebug(VtSession *s, char *fmt, ...)
  16. {
  17. va_list arg;
  18. if(!s->debug)
  19. return;
  20. va_start(arg, fmt);
  21. vfprint(2, fmt, arg);
  22. va_end(arg);
  23. }
  24. void
  25. vtDebugMesg(VtSession *z, Packet *p, char *s)
  26. {
  27. int op;
  28. int tid;
  29. int n;
  30. uint8_t buf[100], *b;
  31. if(!z->debug)
  32. return;
  33. n = packetSize(p);
  34. if(n < 2) {
  35. fprint(2, "runt packet%s", s);
  36. return;
  37. }
  38. b = packetPeek(p, buf, 0, 2);
  39. op = b[0];
  40. tid = b[1];
  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. void
  46. vtDumpSome(Packet *pkt)
  47. {
  48. int printable;
  49. int i, n;
  50. char buf[200], *q, *eq;
  51. uint8_t data[32], *p;
  52. n = packetSize(pkt);
  53. printable = 1;
  54. q = buf;
  55. eq = buf + sizeof(buf);
  56. q = seprint(q, eq, "(%d) '", n);
  57. if(n > sizeof(data))
  58. n = sizeof(data);
  59. p = packetPeek(pkt, data, 0, n);
  60. for(i=0; i<n && printable; i++)
  61. if((p[i]<32 && p[i] !='\n' && p[i] !='\t') || p[i]>127)
  62. printable = 0;
  63. if(printable) {
  64. for(i=0; i<n; i++)
  65. q = seprint(q, eq, "%c", p[i]);
  66. } else {
  67. for(i=0; i<n; i++) {
  68. if(i>0 && i%4==0)
  69. q = seprint(q, eq, " ");
  70. q = seprint(q, eq, "%.2X", p[i]);
  71. }
  72. }
  73. seprint(q, eq, "'");
  74. fprint(2, "%s", buf);
  75. }