print.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * DUMA - Red-Zone memory allocator.
  3. * Copyright (C) 2006 Michael Eddington <meddington@gmail.com>
  4. * Copyright (C) 2002-2008 Hayati Ayguen <h_ayguen@web.de>, Procitec GmbH
  5. * Copyright (C) 1987-1999 Bruce Perens <bruce@perens.com>
  6. * License: GNU GPL (GNU General Public License, see COPYING-GPL)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. *
  23. * FILE CONTENTS:
  24. * internal implementation file
  25. * contains aborting, printing functions with minor system/platform dependencies
  26. */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <signal.h>
  30. #include <stdarg.h>
  31. #ifndef WIN32
  32. #include <unistd.h>
  33. #include <fcntl.h>
  34. #include <sys/mman.h>
  35. #include <sys/param.h>
  36. #else
  37. #define WIN32_LEAN_AND_MEAN 1
  38. #include <windows.h>
  39. #include <winbase.h>
  40. #include <fcntl.h>
  41. #include <io.h>
  42. #endif
  43. #if(defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__))
  44. /* use these for mingw */
  45. #include <unistd.h>
  46. #include <fcntl.h>
  47. #else
  48. /* already defined in cygwin headers */
  49. #ifndef LPVOID
  50. typedef void *LPVOID;
  51. #endif
  52. #if(defined(caddr_t) && defined(daddr_t))
  53. #ifndef __USE_MISC
  54. typedef LPVOID caddr_t;
  55. #endif // __USE_MISC
  56. #endif // (defined(caddr_t) && defined(daddr_t))
  57. #endif // (defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__))
  58. #ifdef _MSC_VER
  59. #include <crtdbg.h>
  60. #endif
  61. #include "duma.h"
  62. #include "noduma.h"
  63. #include "print.h"
  64. /*
  65. * NBBY is the number of bits per byte. Some systems define it in <sys/param.h>
  66. */
  67. #ifndef NBBY
  68. #define NBBY 8
  69. #endif
  70. /*
  71. * These routines do their printing without using stdio. Stdio can't
  72. * be used because it calls malloc(). Internal routines of a malloc()
  73. * debugger should not re-enter malloc(), so stdio is out.
  74. */
  75. /* define prototypes / forward declarations */
  76. static int sprintAddr(char* dest, DUMA_ADDR addr, DUMA_ADDR base);
  77. static int sprintLong(char* dest, long number, long base);
  78. static int DUMA_vsprintf(char* buffer, const char *pattern, va_list args);
  79. /*
  80. * NUMBER_BUFFER_SIZE is the longest character string that could be needed
  81. * to represent an unsigned integer, assuming we might print in base 2.
  82. * Any other representation (base 10 or 16) will need less characters.
  83. */
  84. #define NUMBER_BUFFER_SIZE (sizeof(DUMA_ADDR) * NBBY)
  85. #define STRING_BUFFER_SIZE 4096
  86. /* Function: sprintAddr
  87. *
  88. * internal function to print a DUMA_ADDR into a buffer
  89. */
  90. static int sprintAddr(char* dest, DUMA_ADDR value, DUMA_ADDR base)
  91. {
  92. char buffer[NUMBER_BUFFER_SIZE+1];
  93. char * s = &buffer[NUMBER_BUFFER_SIZE];
  94. int size;
  95. DUMA_ADDR digit;
  96. do
  97. {
  98. if ( --s == buffer )
  99. DUMA_Abort("Internal error printing number.");
  100. digit = value % base;
  101. *s = (char)( (digit < 10) ? ('0' + digit) : ('a' + digit -10) );
  102. } while ( (value /= base) > 0 );
  103. size = &buffer[NUMBER_BUFFER_SIZE] - s;
  104. buffer[NUMBER_BUFFER_SIZE] = '\0';
  105. strcpy(dest, s);
  106. return size;
  107. }
  108. /* Function: sprintLong
  109. *
  110. * internal function to print a int into a buffer
  111. */
  112. static int sprintLong(char* dest, long value, long base)
  113. {
  114. char buffer[NUMBER_BUFFER_SIZE+1];
  115. char * s = &buffer[NUMBER_BUFFER_SIZE];
  116. long size;
  117. long digit;
  118. do
  119. {
  120. if ( --s == buffer )
  121. DUMA_Abort("Internal error printing number.");
  122. digit = value % base;
  123. *s = (char)( (digit < 10) ? ('0' + digit) : ('a' + digit -10) );
  124. } while ( (value /= base) > 0 );
  125. size = &buffer[NUMBER_BUFFER_SIZE] - s;
  126. buffer[NUMBER_BUFFER_SIZE] = '\0';
  127. strcpy(dest, s);
  128. return size;
  129. }
  130. /* Function: DUMA_vsprintf
  131. *
  132. * internal function to print a formatted string into a buffer
  133. * int sprintf(char* buffer, const char *pattern, va_list args)
  134. * allowed format specifier are:
  135. *
  136. * %a = adress of type DUMA_ADDR
  137. * %x = adress of type DUMA_ADDR
  138. * %d = unsigned of type DUMA_SIZE
  139. * %i = int
  140. * %l = long
  141. * %s = string teminated with '\0'
  142. * %c = char
  143. */
  144. static int DUMA_vsprintf(char* buffer, const char *pattern, va_list args)
  145. {
  146. char c;
  147. static const char bad_pattern[] = "\nDUMA: Bad pattern specifier %%%c in DUMA_Print().\n";
  148. const char * s = pattern;
  149. int len = 0;
  150. DUMA_ADDR n;
  151. c = *s++;
  152. while ( c )
  153. {
  154. if ( c == '%' )
  155. {
  156. c = *s++;
  157. switch ( c )
  158. {
  159. case '%':
  160. buffer[len++] = c;
  161. break;
  162. case 'a': /* DUMA_ADDR */
  163. case 'x': /* DUMA_ADDR */
  164. /*
  165. * Print an address passed as a void pointer.
  166. * The type of DUMA_ADDR must be set so that
  167. * it is large enough to contain all of the
  168. * bits of a void pointer.
  169. */
  170. n = va_arg(args, DUMA_ADDR);
  171. len += sprintAddr(&buffer[len], n, 16);
  172. break;
  173. case 'd': /* DUMA_SIZE */
  174. n = va_arg(args, DUMA_SIZE);
  175. len += sprintAddr(&buffer[len], n, 10);
  176. break;
  177. case 'i': /* int */
  178. {
  179. long n = (long)va_arg(args, int);
  180. if ( n < 0 )
  181. {
  182. buffer[len++] = '-';
  183. n = -n;
  184. }
  185. len += sprintLong(&buffer[len], n, 10);
  186. break;
  187. }
  188. case 'l': /* long */
  189. {
  190. long n = va_arg(args, long);
  191. if ( n < 0 )
  192. {
  193. buffer[len++] = '-';
  194. n = -n;
  195. }
  196. len += sprintLong(&buffer[len], n, 10);
  197. break;
  198. }
  199. case 's': /* string */
  200. {
  201. const char * string;
  202. size_t length;
  203. string = va_arg(args, char *);
  204. if (string)
  205. {
  206. length = strlen(string);
  207. strcpy(&buffer[len], string);
  208. }
  209. else
  210. {
  211. length = 4; /* = strlen("NULL") */
  212. strcpy(&buffer[len], "NULL");
  213. }
  214. len += length;
  215. break;
  216. }
  217. case 'c': /* char */
  218. /* characters are passed as int ! */
  219. buffer[len++] = (char)va_arg(args, int);
  220. break;
  221. default:
  222. DUMA_Print(bad_pattern, c);
  223. }
  224. }
  225. else /* if ( c != '%' ) */
  226. buffer[len++] = c;
  227. c = *s++;
  228. } /* end while (c) */
  229. buffer[len] = '\0';
  230. return len;
  231. }
  232. /* Function: DUMA_Abort
  233. *
  234. * external abort function
  235. * on Visual C++ it additionally prints to Debug Output of the IDE
  236. * void DUMA_Abort(const char * pattern, ...)
  237. */
  238. void
  239. DUMA_Abort(const char * pattern, ...)
  240. {
  241. char buffer[STRING_BUFFER_SIZE];
  242. int lena, lenb;
  243. va_list args;
  244. va_start(args, pattern);
  245. strcpy(buffer, "\nDUMA Aborting: ");
  246. lena = strlen(buffer);
  247. lenb = DUMA_vsprintf(&buffer[lena], pattern, args);
  248. strcat(buffer, "\n");
  249. DUMA_Print("%s", buffer);
  250. va_end(args);
  251. #ifndef WIN32
  252. /*
  253. * I use kill(getpid(), SIGILL) instead of abort() because some
  254. * mis-guided implementations of abort() flush stdio, which can
  255. * cause malloc() or free() to be called.
  256. */
  257. kill(getpid(), SIGILL);
  258. #else
  259. /* Windows doesn't have a kill() */
  260. abort();
  261. #endif
  262. /* Just in case something handles SIGILL and returns, exit here. */
  263. _exit(-1);
  264. }
  265. /* Function: DUMA_Print
  266. *
  267. * external print function
  268. * on Visual C++ it additionally prints to Debug Output of the IDE
  269. * void DUMA_Print(const char * pattern, ...)
  270. */
  271. void
  272. DUMA_Print(const char * pattern, ...)
  273. {
  274. char buffer[STRING_BUFFER_SIZE];
  275. int len, fd;
  276. va_list args;
  277. va_start(args, pattern);
  278. len = DUMA_vsprintf(buffer, pattern, args);
  279. va_end(args);
  280. #ifdef WIN32
  281. if(DUMA_OUTPUT_DEBUG)
  282. OutputDebugString(buffer);
  283. #endif
  284. if(DUMA_OUTPUT_STDOUT)
  285. write(1, buffer, len);
  286. if(DUMA_OUTPUT_STDERR)
  287. write(2, buffer, len);
  288. if(DUMA_OUTPUT_FILE != NULL)
  289. {
  290. #if defined(WIN32) && !defined(__CYGWIN__)
  291. fd = _open(DUMA_OUTPUT_FILE, _O_APPEND|_O_CREAT|_O_WRONLY, 0600);
  292. #else
  293. fd = open(DUMA_OUTPUT_FILE, O_APPEND|O_CREAT|O_WRONLY, 0600);
  294. #endif
  295. if ( fd >= 0 )
  296. {
  297. write(fd, buffer, len);
  298. #if defined(WIN32) && !defined(__CYGWIN__)
  299. _close(fd);
  300. #else
  301. close(fd);
  302. #endif
  303. }
  304. }
  305. }
  306. /* Function: DUMA_Exit
  307. *
  308. * external exit function
  309. * on Visual C++ it additionally prints to Debug Output of the IDE
  310. * void DUMA_Exit(const char * pattern, ...)
  311. */
  312. void
  313. DUMA_Exit(const char * pattern, ...)
  314. {
  315. char buffer[STRING_BUFFER_SIZE];
  316. int lena, lenb;
  317. va_list args;
  318. va_start(args, pattern);
  319. strcpy(buffer, "\nDUMA Exiting: ");
  320. lena = strlen(buffer);
  321. lenb = DUMA_vsprintf(&buffer[lena], pattern, args);
  322. strcat(buffer, "\n");
  323. DUMA_Print("%s", buffer);
  324. #ifdef WIN32
  325. if(DUMA_OUTPUT_DEBUG)
  326. OutputDebugString(buffer);
  327. #endif
  328. va_end(args);
  329. /*
  330. * I use _exit() because the regular exit() flushes stdio,
  331. * which may cause malloc() or free() to be called.
  332. */
  333. _exit(-1);
  334. }
  335. /* Function: DUMA_sprintf */
  336. void
  337. DUMA_sprintf(char* buffer, const char * pattern, ...)
  338. {
  339. int len;
  340. va_list args;
  341. va_start(args, pattern);
  342. len = DUMA_vsprintf(buffer, pattern, args);
  343. va_end(args);
  344. if (len <= 0)
  345. buffer[0] = 0;
  346. }
  347. const char *
  348. DUMA_strerror(int duma_errno)
  349. {
  350. static char acStrError[STRING_BUFFER_SIZE];
  351. DUMA_sprintf(acStrError, "System Error Number 'errno' from Standard C Library is %i\n", duma_errno);
  352. return acStrError;
  353. }
  354. /* end */