1 /* $Cambridge: exim/src/src/debug.c,v 1.7 2009/11/16 19:50:36 nm4 Exp $ */
3 /*************************************************
4 * Exim - an Internet mail transport agent *
5 *************************************************/
7 /* Copyright (c) University of Cambridge 1995 - 2009 */
8 /* See the file NOTICE for conditions of use and distribution. */
13 static uschar debug_buffer[2048];
14 static uschar *debug_ptr = debug_buffer;
15 static int debug_prefix_length = 0;
19 /*************************************************
21 *************************************************/
23 /* Recursive tree-printing subroutine. It uses a static vector of uschar to
24 hold the line-drawing characters that need to be printed on every line as it
25 moves down the page. This function is used only in debugging circumstances. The
26 output is done via debug_printf(). */
28 #define tree_printlinesize 132 /* line size for printing */
29 static uschar tree_printline[tree_printlinesize];
31 /* Internal recursive subroutine.
35 pos amount of indenting & vertical bars to pring
36 barswitch if TRUE print | at the pos value
42 tree_printsub(tree_node *p, int pos, int barswitch)
45 if (p->right != NULL) tree_printsub(p->right, pos+2, 1);
46 for (i = 0; i <= pos-1; i++) debug_printf("%c", tree_printline[i]);
47 debug_printf("-->%s [%d]\n", p->name, p->balance);
48 tree_printline[pos] = barswitch? '|' : ' ';
51 tree_printline[pos+2] = '|';
52 tree_printsub(p->left, pos+2, 0);
56 /* The external function, with just a tree node argument. */
59 debug_print_tree(tree_node *p)
62 for (i = 0; i < tree_printlinesize; i++) tree_printline[i] = ' ';
63 if (p == NULL) debug_printf("Empty Tree\n"); else tree_printsub(p, 0, 0);
64 debug_printf("---- End of tree ----\n");
69 /*************************************************
70 * Print an argv vector *
71 *************************************************/
73 /* Called when about to obey execv().
75 Argument: the argv vector
80 debug_print_argv(uschar **argv)
83 while (*argv != NULL) debug_printf(" %.256s", *argv++);
89 /*************************************************
90 * Expand and print debugging string *
91 *************************************************/
93 /* The string is expanded and written as debugging output. If
94 expansion fails, a message is written instead.
101 debug_print_string(uschar *debug_string)
103 if (debug_string == NULL) return;
106 uschar *s = expand_string(debug_string);
108 debug_printf("failed to expand debug_output \"%s\": %s\n", debug_string,
109 expand_string_message);
111 debug_printf("%s%s", s, (s[Ustrlen(s)-1] == '\n')? "" : "\n");
117 /*************************************************
118 * Print current uids and gids *
119 *************************************************/
122 Argument: an introductory string
127 debug_print_ids(uschar *s)
129 debug_printf("%s uid=%ld gid=%ld euid=%ld egid=%ld\n", s,
130 (long int)getuid(), (long int)getgid(), (long int)geteuid(),
131 (long int)getegid());
137 /*************************************************
138 * Print debugging message *
139 *************************************************/
141 /* There are two entries, one for use when being called directly from a
142 function with a variable argument list.
144 If debug_pid is nonzero, print the pid at the start of each line. This is for
145 tidier output when running parallel remote deliveries with debugging turned on.
146 Must do the whole thing with a single printf and flush, as otherwise output may
147 get interleaved. Since some calls to debug_printf() don't end with newline,
148 we save up the text until we do get the newline. */
151 debug_printf(const char *format, ...)
154 va_start(ap, format);
155 debug_vprintf(format, ap);
160 debug_vprintf(const char *format, va_list ap)
162 if (debug_file == NULL) return;
164 /* Various things can be inserted at the start of a line. Don't use the
165 tod_stamp() function for the timestamp, because that will overwrite the
166 timestamp buffer, which may contain something useful. (This was a bug fix: the
167 +memory debugging with +timestamp did cause a problem.) */
169 if (debug_ptr == debug_buffer)
173 time_t now = time(NULL);
174 struct tm *t = timestamps_utc? gmtime(&now) : localtime(&now);
175 (void) sprintf(CS debug_ptr, "%02d:%02d:%02d ", t->tm_hour, t->tm_min,
177 while(*debug_ptr != 0) debug_ptr++;
182 sprintf(CS debug_ptr, "%5d ", (int)getpid());
183 while(*debug_ptr != 0) debug_ptr++;
186 /* Set up prefix if outputting for host checking and not debugging */
188 if (host_checking && debug_selector == 0)
190 Ustrcpy(debug_ptr, ">>> ");
194 debug_prefix_length = debug_ptr - debug_buffer;
197 /* Use the checked formatting routine to ensure that the buffer
198 does not overflow. Ensure there's space for a newline at the end. */
200 if (!string_vformat(debug_ptr,
201 sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1, format, ap))
203 uschar *s = US"**** debug string too long - truncated ****\n";
204 uschar *p = debug_buffer + Ustrlen(debug_buffer);
205 int maxlen = sizeof(debug_buffer) - Ustrlen(s) - 3;
206 if (p > debug_buffer + maxlen) p = debug_buffer + maxlen;
207 if (p > debug_buffer && p[-1] != '\n') *p++ = '\n';
211 while(*debug_ptr != 0) debug_ptr++;
213 /* Output the line if it is complete. If we added any prefix data and there
214 are internal newlines, make sure the prefix is on the continuation lines,
215 as long as there is room in the buffer. We want to do just a single fprintf()
216 so as to avoid interleaving. */
218 if (debug_ptr[-1] == '\n')
220 if (debug_prefix_length > 0)
222 uschar *p = debug_buffer;
223 int left = sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1;
224 while ((p = Ustrchr(p, '\n') + 1) != debug_ptr &&
225 left >= debug_prefix_length)
227 int len = debug_ptr - p;
228 memmove(p + debug_prefix_length, p, len + 1);
229 memmove(p, debug_buffer, debug_prefix_length);
230 debug_ptr += debug_prefix_length;
231 left -= debug_prefix_length;
235 fprintf(debug_file, "%s", CS debug_buffer);
237 debug_ptr = debug_buffer;
238 debug_prefix_length = 0;