debug.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Debugging routines used by the debug() macros, at least if you
  3. * compiled with -DDEBUG (aka the PUTTY_DEBUG cmake option) so that
  4. * those macros don't optimise down to nothing.
  5. */
  6. #include "defs.h"
  7. #include "misc.h"
  8. #include "utils/utils.h"
  9. void debug_printf(const char *fmt, ...)
  10. {
  11. char *buf;
  12. va_list ap;
  13. va_start(ap, fmt);
  14. buf = dupvprintf(fmt, ap);
  15. dputs(buf);
  16. sfree(buf);
  17. va_end(ap);
  18. }
  19. void debug_memdump(const void *buf, int len, bool L)
  20. {
  21. int i;
  22. const unsigned char *p = buf;
  23. char foo[17];
  24. if (L) {
  25. int delta;
  26. debug_printf("\t%d (0x%x) bytes:\n", len, len);
  27. delta = 15 & (uintptr_t)p;
  28. p -= delta;
  29. len += delta;
  30. }
  31. for (; 0 < len; p += 16, len -= 16) {
  32. dputs(" ");
  33. if (L)
  34. debug_printf("%p: ", p);
  35. strcpy(foo, "................"); /* sixteen dots */
  36. for (i = 0; i < 16 && i < len; ++i) {
  37. if (&p[i] < (unsigned char *) buf) {
  38. dputs(" "); /* 3 spaces */
  39. foo[i] = ' ';
  40. } else {
  41. debug_printf("%c%2.2x",
  42. &p[i] != (unsigned char *) buf
  43. && i % 4 ? '.' : ' ', p[i]
  44. );
  45. if (p[i] >= ' ' && p[i] <= '~')
  46. foo[i] = (char) p[i];
  47. }
  48. }
  49. foo[i] = '\0';
  50. debug_printf("%*s%s\n", (16 - i) * 3 + 2, "", foo);
  51. }
  52. }