root/common/trunk/src/debug.c @ 373

Revision 373, 5.4 KB (checked in by ucaccsp, 14 years ago)

Workaround for buffer overflow bug which caused crashes on winNT with service
pack 6.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * FILE:    debug.c
3 * PROGRAM: RAT
4 * AUTHORS: Isidor Kouvelas
5 *          Colin Perkins
6 *          Mark Handley
7 *          Orion Hodson
8 *          Jerry Isdale
9 *
10 * $Revision$
11 * $Date$
12 *
13 * Copyright (c) 1995-2000 University College London
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, is permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 *    must display the following acknowledgement:
26 *      This product includes software developed by the Computer Science
27 *      Department at University College London
28 * 4. Neither the name of the University nor of the Department may be used
29 *    to endorse or promote products derived from this software without
30 *    specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 */
43
44#include "config_unix.h"
45#include "config_win32.h"
46#include "debug.h"
47
48void _dprintf(const char *format, ...)
49{
50#ifdef DEBUG
51#ifdef WIN32
52        char msg[65535];
53        va_list ap;
54       
55        va_start(ap, format);
56        vsprintf(msg, format, ap);      /* FIXME - this should be vsnprintf() */
57        va_end(ap);
58        OutputDebugString(msg);
59#else 
60        va_list ap;
61 
62        va_start(ap, format);
63        vfprintf(stderr, format, ap);
64        va_end(ap);
65#endif /* WIN32 */
66#else
67        UNUSED (format);
68#endif /* DEBUG */
69}
70
71void debug_dump(void*lp, long len)
72{
73   char * p;
74   long i, j, start;
75   char Buff[80];
76   char stuffBuff[10];
77   char tmpBuf[10];
78 
79   _dprintf("Dump of %ld=%lx bytes\n",len, len);
80   start = 0L;
81   while (start < len)
82   {
83    /* start line with pointer position key */
84      p = (char*)lp + start;
85      sprintf(Buff,"%p: ",p);
86 
87    /* display each character as hex value */
88      for (i=start, j=0; j < 16; p++,i++, j++)
89      {
90         if (i < len)
91         {
92            sprintf(tmpBuf,"%X",((int)(*p) & 0xFF));
93 
94            if (strlen((char *)tmpBuf) < 2)
95            {
96               stuffBuff[0] = '0';
97               stuffBuff[1] = tmpBuf[0];
98               stuffBuff[2] = ' ';
99               stuffBuff[3] = '\0';
100            } else
101            {
102               stuffBuff[0] = tmpBuf[0];
103               stuffBuff[1] = tmpBuf[1];
104               stuffBuff[2] = ' ';
105               stuffBuff[3] = '\0';
106            }
107            strcat(Buff, stuffBuff);
108         } else
109            strcat(Buff," ");
110         if (j == 7) /* space between groups of 8 */
111            strcat(Buff," ");
112      }
113 
114    /* fill out incomplete lines */
115      for(;j<16;j++)
116      {
117         strcat(Buff,"   ");
118         if (j == 7)
119            strcat(Buff," ");
120      }
121      strcat(Buff,"  ");
122 
123    /* display each character as character value */
124      for (i=start,j=0,p=(char*)lp+start;
125         (i < len && j < 16); p++,i++, j++)
126      {
127         if ( ((*p) >= ' ') && ((*p) <= '~') )   /* test displayable */
128            sprintf(tmpBuf,"%c", *p);
129         else
130            sprintf(tmpBuf,"%c", '.');
131         strcat(Buff,tmpBuf);
132         if (j == 7)   /* space between groups of 8 */
133            strcat(Buff," ");
134      }
135      _dprintf("%s\n", Buff);
136      start = i;  /* next line starting byte */
137   }
138}
139
140void _debug_set_core_dir(const char *argv0)
141{
142#if defined(DEBUG) && !defined(WIN32)
143        struct stat s;
144        char coredir[64];
145        const char *appname;
146
147        appname = strrchr(argv0, '/');
148        if (appname == NULL) {
149                appname = argv0;
150        } else {
151                appname = appname + 1;
152        }
153
154        /* Should check length of appname, but this is debug code   */
155        /* and developers should know better than to have 64 char   */
156        /* app name.                                                */
157        sprintf(coredir, "core-%s", appname);
158
159        mkdir(coredir, S_IRWXU);
160        if (stat(coredir, &s) != 0) {
161                debug_msg("Could not stat %s\n", coredir);
162                return;
163        }
164        if (!S_ISDIR(s.st_mode)) {
165                debug_msg("Not a directory: %s\n", coredir);
166                return;
167        }
168        if (!(s.st_mode & S_IWUSR) || !(s.st_mode & S_IXUSR)) {
169                debug_msg("Cannot write in or change to %s\n", coredir);
170                return;
171        }
172        if (!chdir(coredir)) {
173                perror(coredir);
174        }
175#endif /* DEBUG */
176        UNUSED(argv0);
177}
Note: See TracBrowser for help on using the browser.