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)
43 if (p->right) tree_printsub(p->right, pos+2, 1);
44 for (i = 0; i <= pos-1; i++) debug_printf("%c", tree_printline[i]);
45 debug_printf("-->%s [%d]\n", p->name, p->balance);
46 tree_printline[pos] = barswitch? '|' : ' ';
49 tree_printline[pos+2] = '|';
50 tree_printsub(p->left, pos+2, 0);
54 /* The external function, with just a tree node argument. */
57 debug_print_tree(tree_node *p)
60 for (i = 0; i < tree_printlinesize; i++) tree_printline[i] = ' ';
61 if (!p) debug_printf("Empty Tree\n"); else tree_printsub(p, 0, 0);
62 debug_printf("---- End of tree ----\n");
67 /*************************************************
68 * Print an argv vector *
69 *************************************************/
71 /* Called when about to obey execv().
73 Argument: the argv vector
78 debug_print_argv(const uschar ** argv)
81 while (*argv) debug_printf(" %.256s", *argv++);
87 /*************************************************
88 * Expand and print debugging string *
89 *************************************************/
91 /* The string is expanded and written as debugging output. If
92 expansion fails, a message is written instead.
99 debug_print_string(uschar *debug_string)
101 if (!debug_string) return;
104 uschar *s = expand_string(debug_string);
106 debug_printf("failed to expand debug_output \"%s\": %s\n", debug_string,
107 expand_string_message);
109 debug_printf("%s%s", s, (s[Ustrlen(s)-1] == '\n')? "" : "\n");
115 /*************************************************
116 * Print current uids and gids *
117 *************************************************/
120 Argument: an introductory string
125 debug_print_ids(uschar *s)
127 debug_printf("%s uid=%ld gid=%ld euid=%ld egid=%ld\n", s,
128 (long int)getuid(), (long int)getgid(), (long int)geteuid(),
129 (long int)getegid());
135 /*************************************************
136 * Print debugging message *
137 *************************************************/
139 /* There are two entries, one for use when being called directly from a
140 function with a variable argument list, one for prepending an indent.
142 If debug_pid is nonzero, print the pid at the start of each line. This is for
143 tidier output when running parallel remote deliveries with debugging turned on.
144 Must do the whole thing with a single printf and flush, as otherwise output may
145 get interleaved. Since some calls to debug_printf() don't end with newline,
146 we save up the text until we do get the newline.
147 Take care to not disturb errno. */
150 /* Debug printf indented by ACL nest depth */
152 debug_printf_indent(const char * format, ...)
155 va_start(ap, format);
156 debug_vprintf(acl_level + expand_level, format, ap);
161 debug_printf(const char *format, ...)
164 va_start(ap, format);
165 debug_vprintf(0, format, ap);
170 debug_vprintf(int indent, const char *format, va_list ap)
172 int save_errno = errno;
174 if (!debug_file) return;
176 /* Various things can be inserted at the start of a line. Don't use the
177 tod_stamp() function for the timestamp, because that will overwrite the
178 timestamp buffer, which may contain something useful. (This was a bug fix: the
179 +memory debugging with +timestamp did cause a problem.) */
181 if (debug_ptr == debug_buffer)
189 gettimeofday(&now, NULL);
191 t = f.timestamps_utc ? gmtime(&tmp) : localtime(&tmp);
192 debug_ptr += sprintf(CS debug_ptr,
193 LOGGING(millisec) ? "%02d:%02d:%02d.%03d " : "%02d:%02d:%02d ",
194 t->tm_hour, t->tm_min, t->tm_sec, (int)(now.tv_usec/1000));
198 debug_ptr += sprintf(CS debug_ptr, "%5d ", (int)getpid());
200 /* Set up prefix if outputting for host checking and not debugging */
202 if (host_checking && debug_selector == 0)
204 Ustrcpy(debug_ptr, ">>> ");
208 debug_prefix_length = debug_ptr - debug_buffer;
214 for (i = indent >> 2; i > 0; i--)
217 Ustrcpy(debug_ptr, " !");
218 debug_ptr += 4; /* 3 spaces + shriek */
219 debug_prefix_length += 4;
223 Ustrcpy(debug_ptr, " " UTF8_VERT_2DASH);
224 debug_ptr += 6; /* 3 spaces + 3 UTF-8 octets */
225 debug_prefix_length += 6;
228 Ustrncpy(debug_ptr, " ", indent &= 3);
230 debug_prefix_length += indent;
233 /* Use the checked formatting routine to ensure that the buffer
234 does not overflow. Ensure there's space for a newline at the end. */
237 gstring gs = { .size = (int)sizeof(debug_buffer) - 1,
238 .ptr = debug_ptr - debug_buffer,
240 if (!string_vformat(&gs, FALSE, format, ap))
242 uschar * s = US"**** debug string too long - truncated ****\n";
243 uschar * p = gs.s + gs.ptr;
244 int maxlen = gs.size - Ustrlen(s) - 2;
245 if (p > gs.s + maxlen) p = gs.s + maxlen;
246 if (p > gs.s && p[-1] != '\n') *p++ = '\n';
248 while(*debug_ptr) debug_ptr++;
252 string_from_gstring(&gs);
253 debug_ptr = gs.s + gs.ptr;
257 /* Output the line if it is complete. If we added any prefix data and there
258 are internal newlines, make sure the prefix is on the continuation lines,
259 as long as there is room in the buffer. We want to do just a single fprintf()
260 so as to avoid interleaving. */
262 if (debug_ptr[-1] == '\n')
264 if (debug_prefix_length > 0)
266 uschar *p = debug_buffer;
267 int left = sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1;
268 while ((p = Ustrchr(p, '\n') + 1) != debug_ptr &&
269 left >= debug_prefix_length)
271 int len = debug_ptr - p;
272 memmove(p + debug_prefix_length, p, len + 1);
273 memmove(p, debug_buffer, debug_prefix_length);
274 debug_ptr += debug_prefix_length;
275 left -= debug_prefix_length;
279 fprintf(debug_file, "%s", CS debug_buffer);
281 debug_ptr = debug_buffer;
282 debug_prefix_length = 0;