Dave Hinton's debugging patch.
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 28 Apr 2001 09:24:19 +0000 (09:24 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Sat, 28 Apr 2001 09:24:19 +0000 (09:24 +0000)
git-svn-id: svn://svn.tartarus.org/sgt/putty@1079 cda61777-01e9-0310-a592-d414129be87e

misc.c
misc.h [new file with mode: 0644]
putty.h
puttymem.h

diff --git a/misc.c b/misc.c
index 50b3248..8968fc2 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -303,10 +303,8 @@ void safefree(void *ptr) {
 static FILE *debug_fp = NULL;
 static int debug_got_console = 0;
 
-void dprintf(char *fmt, ...) {
-    char buf[2048];
+static void dputs (char *buf) {
     DWORD dw;
-    va_list ap;
 
     if (!debug_got_console) {
        AllocConsole();
@@ -316,11 +314,50 @@ void dprintf(char *fmt, ...) {
        debug_fp = fopen("debug.log", "w");
     }
 
-    va_start(ap, fmt);
-    vsprintf(buf, fmt, ap);
     WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, NULL);
     fputs(buf, debug_fp);
     fflush(debug_fp);
+}
+
+
+void dprintf(char *fmt, ...) {
+    char buf[2048];
+    va_list ap;
+
+    va_start(ap, fmt);
+    vsprintf(buf, fmt, ap);
+    dputs (buf);
     va_end(ap);
 }
-#endif
+
+
+void debug_memdump (void *buf, int len, int L) {
+    int i;
+    unsigned char *p = buf;
+    if (L) {
+       int delta;
+        dprintf ("\t%d (0x%x) bytes:\n", len, len);
+       delta = 15 & (int) p;
+       p -= delta;
+       len += delta;
+    }
+    for (; 0 < len; p += 16, len -= 16) {
+       dputs ("\t");
+       if (L) dprintf ("%p:  ", p);
+       for (i = 0; i < 16 && i < len; ++i) {
+           if (&p[i] < (unsigned char *) buf) {
+               dputs ("   "); /* 3 spaces */
+           } else {
+               dprintf (
+                   "%c%02.2x",
+                   &p[i] != (unsigned char *) buf && i % 4 ? '.' : ' ',
+                   p[i]
+               );
+           }
+       }
+       dputs ("\n");
+    }
+}
+
+#endif /* def DEBUG */
+
diff --git a/misc.h b/misc.h
new file mode 100644 (file)
index 0000000..37b7311
--- /dev/null
+++ b/misc.h
@@ -0,0 +1,38 @@
+#ifndef PUTTY_MISC_H
+#define PUTTY_MISC_H
+
+#include "puttymem.h"
+
+
+/*
+ * Debugging functions.
+ *
+ * Output goes to debug.log
+ *
+ * debug(()) (note the double brackets) is like printf().
+ *
+ * dmemdump() and dmemdumpl() both do memory dumps.  The difference
+ * is that dmemdumpl() is more suited for when where the memory is is
+ * important (say because you'll be recording pointer values later
+ * on).  dmemdump() is more concise.
+ */
+
+#ifdef DEBUG
+void dprintf(char *fmt, ...);
+void debug_memdump (void *buf, int len, int L);
+#define debug(x) (dprintf x)
+#define dmemdump(buf,len) debug_memdump (buf, len, 0);
+#define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
+#else
+#define debug(x)
+#define dmemdump(buf,len)
+#define dmemdumpl(buf,len)
+#endif
+
+
+#ifndef lenof
+#define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
+#endif
+
+
+#endif
diff --git a/putty.h b/putty.h
index 856aeb0..8acc9a6 100644 (file)
--- a/putty.h
+++ b/putty.h
@@ -425,7 +425,7 @@ void random_get_savedata(void **data, int *len);
  * Exports from misc.c.
  */
 
-#include "puttymem.h"
+#include "misc.h"
 
 /*
  * Exports from version.c.
@@ -459,11 +459,5 @@ void crypto_wrapup();
 void agent_query(void *in, int inlen, void **out, int *outlen);
 int agent_exists(void);
 
-#ifdef DEBUG
-void dprintf(char *fmt, ...);
-#define debug(x) (dprintf x)
-#else
-#define debug(x)
-#endif
 
 #endif
index ba4fd0e..4396592 100644 (file)
@@ -5,6 +5,10 @@
 #ifndef PUTTY_PUTTYMEM_H
 #define PUTTY_PUTTYMEM_H
 
+#include <stddef.h> /* for size_t */
+#include <string.h> /* for memcpy() */
+
+
 /* #define MALLOC_LOG  do this if you suspect putty of leaking memory */
 #ifdef MALLOC_LOG
 #define smalloc(z) (mlog(__FILE__,__LINE__), safemalloc(z))
@@ -21,4 +25,13 @@ void *safemalloc(size_t);
 void *saferealloc(void *, size_t);
 void safefree(void *);
 
+
+/* smalloc a thing */
+#define smalloca(type) ((type *) smalloc (sizeof (type)))
+/* smalloc a copy of a thing */
+#define smallocc(ptr) memcpy (smalloc (sizeof (*ptr)), ptr, sizeof (*ptr))
+/* smalloc n things */
+#define smallocn(n,type) ((type *) smalloc ((n) * sizeof (type)))
+
+
 #endif