9 * These routines do their printing without using stdio. Stdio can't
10 * be used because it calls malloc(). Internal routines of a malloc()
11 * debugger should not re-enter malloc(), so stdio is out.
15 * NUMBER_BUFFER_SIZE is the longest character string that could be needed
16 * to represent an unsigned integer, assuming we might print in base 2.
18 #define NUMBER_BUFFER_SIZE (sizeof(ef_number) * NBBY)
20 static void printNumber(ef_number number, ef_number base)
22 char buffer[NUMBER_BUFFER_SIZE];
23 char *s = &buffer[NUMBER_BUFFER_SIZE];
30 EF_Abort("Internal error printing number.");
32 digit = number % base;
37 *s = 'a' + digit - 10;
39 } while ((number /= base) > 0);
41 size = &buffer[NUMBER_BUFFER_SIZE] - s;
47 static void vprint(const char *pattern, va_list args)
49 static const char bad_pattern[] =
50 "\nBad pattern specifier %%%c in EF_Print().\n";
51 const char *s = pattern;
54 while ((c = *s++) != '\0') {
59 (void)write(2, &c, 1);
63 * Print an address passed as a void pointer.
64 * The type of ef_number must be set so that
65 * it is large enough to contain all of the
66 * bits of a void pointer.
68 printNumber((ef_number)va_arg(args, void *), 0x10);
74 string = va_arg(args, char *);
75 length = strlen(string);
77 (void)write(2, string, length);
80 int n = va_arg(args, int);
90 printNumber(va_arg(args, u_int), 0x10);
92 case 'c': { /*Cast used, since char gets promoted to int in ... */
93 char c = (char)va_arg(args, int);
95 (void)write(2, &c, 1);
98 EF_Print(bad_pattern, c);
102 (void)write(2, &c, 1);
106 void EF_Abort(const char *pattern, ...)
110 va_start(args, pattern);
112 EF_Print("\nElectricFence Aborting: ");
113 vprint(pattern, args);
119 * I use kill(getpid(), SIGILL) instead of abort() because some
120 * mis-guided implementations of abort() flush stdio, which can
121 * cause malloc() or free() to be called.
123 kill(getpid(), SIGILL);
124 /* Just in case something handles SIGILL and returns, exit here. */
128 void EF_Exit(const char *pattern, ...)
132 va_start(args, pattern);
134 EF_Print("\nElectricFence Exiting: ");
135 vprint(pattern, args);
141 * I use _exit() because the regular exit() flushes stdio,
142 * which may cause malloc() or free() to be called.
147 void EF_Print(const char *pattern, ...)
151 va_start(args, pattern);
152 vprint(pattern, args);