From db9c0f865c8540d2a158aa99e6010150f89bef26 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 28 Apr 2001 09:24:19 +0000 Subject: [PATCH] Dave Hinton's debugging patch. git-svn-id: svn://svn.tartarus.org/sgt/putty@1079 cda61777-01e9-0310-a592-d414129be87e --- misc.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ misc.h | 38 ++++++++++++++++++++++++++++++++++++++ putty.h | 8 +------- puttymem.h | 13 +++++++++++++ 4 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 misc.h diff --git a/misc.c b/misc.c index 50b32485..8968fc22 100644 --- 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 index 00000000..37b73115 --- /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 856aeb07..8acc9a64 100644 --- 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 diff --git a/puttymem.h b/puttymem.h index ba4fd0ed..4396592f 100644 --- a/puttymem.h +++ b/puttymem.h @@ -5,6 +5,10 @@ #ifndef PUTTY_PUTTYMEM_H #define PUTTY_PUTTYMEM_H +#include /* for size_t */ +#include /* 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 -- 2.11.0