1 /*************************************************
2 * Exim - an Internet mail transport agent *
3 *************************************************/
5 /* Copyright (c) University of Cambridge 1995 - 2018 */
6 /* See the file NOTICE for conditions of use and distribution. */
11 static uschar debug_buffer[2048];
12 static uschar *debug_ptr = debug_buffer;
13 static int debug_prefix_length = 0;
17 /*************************************************
19 *************************************************/
21 /* Recursive tree-printing subroutine. It uses a static vector of uschar to
22 hold the line-drawing characters that need to be printed on every line as it
23 moves down the page. This function is used only in debugging circumstances. The
24 output is done via debug_printf(). */
26 #define tree_printlinesize 132 /* line size for printing */
27 static uschar tree_printline[tree_printlinesize];
29 /* Internal recursive subroutine.
33 pos amount of indenting & vertical bars to print
34 barswitch if TRUE print | at the pos value
40 tree_printsub(tree_node *p, int pos, int barswitch)
42 if (p->right) tree_printsub(p->right, pos+2, 1);
43 for (int i = 0; i <= pos-1; i++) debug_printf("%c", tree_printline[i]);
44 debug_printf("-->%s [%d]\n", p->name, p->balance);
45 tree_printline[pos] = barswitch? '|' : ' ';
48 tree_printline[pos+2] = '|';
49 tree_printsub(p->left, pos+2, 0);
53 /* The external function, with just a tree node argument. */
56 debug_print_tree(tree_node *p)
58 for (int i = 0; i < tree_printlinesize; i++) tree_printline[i] = ' ';
59 if (!p) debug_printf("Empty Tree\n"); else tree_printsub(p, 0, 0);
60 debug_printf("---- End of tree ----\n");
65 /*************************************************
66 * Print an argv vector *
67 *************************************************/
69 /* Called when about to obey execv().
71 Argument: the argv vector
76 debug_print_argv(const uschar ** argv)
79 while (*argv) debug_printf(" %.256s", *argv++);
85 /*************************************************
86 * Expand and print debugging string *
87 *************************************************/
89 /* The string is expanded and written as debugging output. If
90 expansion fails, a message is written instead.
97 debug_print_string(uschar *debug_string)
99 if (!debug_string) return;
102 uschar *s = expand_string(debug_string);
104 debug_printf("failed to expand debug_output \"%s\": %s\n", debug_string,
105 expand_string_message);
107 debug_printf("%s%s", s, (s[Ustrlen(s)-1] == '\n')? "" : "\n");
113 /*************************************************
114 * Print current uids and gids *
115 *************************************************/
118 Argument: an introductory string
123 debug_print_ids(uschar *s)
125 debug_printf("%s uid=%ld gid=%ld euid=%ld egid=%ld\n", s,
126 (long int)getuid(), (long int)getgid(), (long int)geteuid(),
127 (long int)getegid());
133 /*************************************************
134 * Print debugging message *
135 *************************************************/
137 /* There are two entries, one for use when being called directly from a
138 function with a variable argument list, one for prepending an indent.
140 If debug_pid is nonzero, print the pid at the start of each line. This is for
141 tidier output when running parallel remote deliveries with debugging turned on.
142 Must do the whole thing with a single printf and flush, as otherwise output may
143 get interleaved. Since some calls to debug_printf() don't end with newline,
144 we save up the text until we do get the newline.
145 Take care to not disturb errno. */
148 /* Debug printf indented by ACL nest depth */
150 debug_printf_indent(const char * format, ...)
153 va_start(ap, format);
154 debug_vprintf(acl_level + expand_level, format, ap);
159 debug_printf(const char *format, ...)
162 va_start(ap, format);
163 debug_vprintf(0, format, ap);
168 debug_vprintf(int indent, const char *format, va_list ap)
170 int save_errno = errno;
172 if (!debug_file) return;
174 /* Various things can be inserted at the start of a line. Don't use the
175 tod_stamp() function for the timestamp, because that will overwrite the
176 timestamp buffer, which may contain something useful. (This was a bug fix: the
177 +memory debugging with +timestamp did cause a problem.) */
179 if (debug_ptr == debug_buffer)
187 gettimeofday(&now, NULL);
189 t = f.timestamps_utc ? gmtime(&tmp) : localtime(&tmp);
190 debug_ptr += sprintf(CS debug_ptr,
191 LOGGING(millisec) ? "%02d:%02d:%02d.%03d " : "%02d:%02d:%02d ",
192 t->tm_hour, t->tm_min, t->tm_sec, (int)(now.tv_usec/1000));
196 debug_ptr += sprintf(CS debug_ptr, "%5d ", (int)getpid());
198 /* Set up prefix if outputting for host checking and not debugging */
200 if (host_checking && debug_selector == 0)
202 Ustrcpy(debug_ptr, ">>> ");
206 debug_prefix_length = debug_ptr - debug_buffer;
211 for (int i = indent >> 2; i > 0; i--)
214 Ustrcpy(debug_ptr, " !");
215 debug_ptr += 4; /* 3 spaces + shriek */
216 debug_prefix_length += 4;
220 Ustrcpy(debug_ptr, " " UTF8_VERT_2DASH);
221 debug_ptr += 6; /* 3 spaces + 3 UTF-8 octets */
222 debug_prefix_length += 6;
225 Ustrncpy(debug_ptr, " ", indent &= 3);
227 debug_prefix_length += indent;
230 /* Use the checked formatting routine to ensure that the buffer
231 does not overflow. Ensure there's space for a newline at the end. */
234 gstring gs = { .size = (int)sizeof(debug_buffer) - 1,
235 .ptr = debug_ptr - debug_buffer,
237 if (!string_vformat(&gs, FALSE, format, ap))
239 uschar * s = US"**** debug string too long - truncated ****\n";
240 uschar * p = gs.s + gs.ptr;
241 int maxlen = gs.size - Ustrlen(s) - 2;
242 if (p > gs.s + maxlen) p = gs.s + maxlen;
243 if (p > gs.s && p[-1] != '\n') *p++ = '\n';
245 while(*debug_ptr) debug_ptr++;
249 string_from_gstring(&gs);
250 debug_ptr = gs.s + gs.ptr;
254 /* Output the line if it is complete. If we added any prefix data and there
255 are internal newlines, make sure the prefix is on the continuation lines,
256 as long as there is room in the buffer. We want to do just a single fprintf()
257 so as to avoid interleaving. */
259 if (debug_ptr[-1] == '\n')
261 if (debug_prefix_length > 0)
263 uschar *p = debug_buffer;
264 int left = sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1;
265 while ((p = Ustrchr(p, '\n') + 1) != debug_ptr &&
266 left >= debug_prefix_length)
268 int len = debug_ptr - p;
269 memmove(p + debug_prefix_length, p, len + 1);
270 memmove(p, debug_buffer, debug_prefix_length);
271 debug_ptr += debug_prefix_length;
272 left -= debug_prefix_length;
276 fprintf(debug_file, "%s", CS debug_buffer);
278 debug_ptr = debug_buffer;
279 debug_prefix_length = 0;