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 const uschar * rc_names[] = { /* Mostly for debug output */
22 [FAIL_FORCED] = US"FAIL_FORCED",
23 [DECLINE] = US"DECLINE",
25 [DISCARD] = US"DISCARD",
27 [REROUTED] = US"REROUTED",
30 [UNEXPECTED] = US"UNEXPECTED",
31 [CANCELLED] = US"CANCELLED",
32 [FAIL_SEND] = US"FAIL_SEND",
33 [FAIL_DROP] = US"FAIL_DROP",
36 const uschar * dns_rc_names[] = {
37 [DNS_SUCCEED] = US"DNS_SUCCEED",
38 [DNS_NOMATCH] = US"DNS_NOMATCH",
39 [DNS_NODATA] = US"DNS_NODATA",
40 [DNS_AGAIN] = US"DNS_AGAIN",
41 [DNS_FAIL] = US"DNS_FAIL",
45 /*************************************************
47 *************************************************/
49 /* Recursive tree-printing subroutine. It uses a static vector of uschar to
50 hold the line-drawing characters that need to be printed on every line as it
51 moves down the page. This function is used only in debugging circumstances. The
52 output is done via debug_printf(). */
54 #define TREE_PRINTLINESIZE 132 /* line size for printing */
55 static uschar tree_printline[TREE_PRINTLINESIZE];
57 /* Internal recursive subroutine.
61 pos amount of indenting & vertical bars to print
62 barswitch if TRUE print | at the pos value
68 tree_printsub(tree_node * p, int pos, int barswitch)
70 if (p->right) tree_printsub(p->right, pos+2, 1);
71 for (int i = 0; i <= pos-1; i++) debug_printf_indent(" %c", tree_printline[i]);
72 debug_printf_indent(" -->%s [%d]\n", p->name, p->balance);
73 tree_printline[pos] = barswitch ? '|' : ' ';
76 tree_printline[pos+2] = '|';
77 tree_printsub(p->left, pos+2, 0);
81 /* The external function, with just a tree node argument. */
84 debug_print_tree(const char * title, tree_node * p)
86 debug_printf_indent("%s:\n", title);
87 for (int i = 0; i < TREE_PRINTLINESIZE; i++) tree_printline[i] = ' ';
88 if (!p) debug_printf_indent(" Empty Tree\n"); else tree_printsub(p, 0, 0);
89 debug_printf_indent("---- End of tree ----\n");
94 /*************************************************
95 * Print an argv vector *
96 *************************************************/
98 /* Called when about to obey execv().
100 Argument: the argv vector
105 debug_print_argv(const uschar ** argv)
107 debug_printf("exec");
108 while (*argv) debug_printf(" %.256s", *argv++);
114 /*************************************************
115 * Expand and print debugging string *
116 *************************************************/
118 /* The string is expanded and written as debugging output. If
119 expansion fails, a message is written instead.
126 debug_print_string(uschar *debug_string)
128 if (!debug_string) return;
131 uschar *s = expand_string(debug_string);
133 debug_printf("failed to expand debug_output \"%s\": %s\n", debug_string,
134 expand_string_message);
136 debug_printf("%s%s", s, (s[Ustrlen(s)-1] == '\n')? "" : "\n");
142 /*************************************************
143 * Print current uids and gids *
144 *************************************************/
147 Argument: an introductory string
152 debug_print_ids(uschar *s)
154 debug_printf("%s uid=%ld gid=%ld euid=%ld egid=%ld\n", s,
155 (long int)getuid(), (long int)getgid(), (long int)geteuid(),
156 (long int)getegid());
159 /************************************************/
161 /* Give a string for a return-code */
166 return rc < 0 || rc >= nelem(rc_names) ? US"?" : rc_names[rc];
173 /*************************************************
174 * Print debugging message *
175 *************************************************/
177 /* There are two entries, one for use when being called directly from a
178 function with a variable argument list, one for prepending an indent.
180 If debug_pid is nonzero, print the pid at the start of each line. This is for
181 tidier output when running parallel remote deliveries with debugging turned on.
182 Must do the whole thing with a single printf and flush, as otherwise output may
183 get interleaved. Since some calls to debug_printf() don't end with newline,
184 we save up the text until we do get the newline.
185 Take care to not disturb errno. */
188 /* Debug printf indented by ACL nest depth */
190 debug_printf_indent(const char * format, ...)
193 va_start(ap, format);
194 debug_vprintf(acl_level + expand_level, format, ap);
199 debug_printf(const char *format, ...)
202 va_start(ap, format);
203 debug_vprintf(0, format, ap);
208 debug_vprintf(int indent, const char *format, va_list ap)
210 int save_errno = errno;
212 if (!debug_file) return;
214 /* Various things can be inserted at the start of a line. Don't use the
215 tod_stamp() function for the timestamp, because that will overwrite the
216 timestamp buffer, which may contain something useful. (This was a bug fix: the
217 +memory debugging with +timestamp did cause a problem.) */
219 if (debug_ptr == debug_buffer)
227 gettimeofday(&now, NULL);
229 t = f.timestamps_utc ? gmtime(&tmp) : localtime(&tmp);
230 debug_ptr += sprintf(CS debug_ptr,
231 LOGGING(millisec) ? "%02d:%02d:%02d.%03d " : "%02d:%02d:%02d ",
232 t->tm_hour, t->tm_min, t->tm_sec, (int)(now.tv_usec/1000));
236 debug_ptr += sprintf(CS debug_ptr, "%5d ", (int)getpid());
238 /* Set up prefix if outputting for host checking and not debugging */
240 if (host_checking && debug_selector == 0)
242 Ustrcpy(debug_ptr, US">>> ");
246 debug_prefix_length = debug_ptr - debug_buffer;
251 for (int i = indent >> 2; i > 0; i--)
254 Ustrcpy(debug_ptr, US" !");
255 debug_ptr += 4; /* 3 spaces + shriek */
256 debug_prefix_length += 4;
260 Ustrcpy(debug_ptr, US" " UTF8_VERT_2DASH);
261 debug_ptr += 6; /* 3 spaces + 3 UTF-8 octets */
262 debug_prefix_length += 6;
265 Ustrncpy(debug_ptr, US" ", indent &= 3);
267 debug_prefix_length += indent;
270 /* Use the lengthchecked formatting routine to ensure that the buffer
271 does not overflow. Ensure there's space for a newline at the end.
272 However, use taint-unchecked routines for writing into the buffer
273 so that we can write tainted info into the static debug_buffer -
274 we trust that we will never expand the results. */
277 gstring gs = { .size = (int)sizeof(debug_buffer) - 1,
278 .ptr = debug_ptr - debug_buffer,
280 if (!string_vformat(&gs, SVFMT_TAINT_NOCHK, format, ap))
282 uschar * s = US"**** debug string too long - truncated ****\n";
283 uschar * p = gs.s + gs.ptr;
284 int maxlen = gs.size - Ustrlen(s) - 2;
285 if (p > gs.s + maxlen) p = gs.s + maxlen;
286 if (p > gs.s && p[-1] != '\n') *p++ = '\n';
288 while(*debug_ptr) debug_ptr++;
292 string_from_gstring(&gs);
293 debug_ptr = gs.s + gs.ptr;
297 /* Output the line if it is complete. If we added any prefix data and there
298 are internal newlines, make sure the prefix is on the continuation lines,
299 as long as there is room in the buffer. We want to do just a single fprintf()
300 so as to avoid interleaving. */
302 if (debug_ptr[-1] == '\n')
304 if (debug_prefix_length > 0)
306 uschar *p = debug_buffer;
307 int left = sizeof(debug_buffer) - (debug_ptr - debug_buffer) - 1;
308 while ((p = Ustrchr(p, '\n') + 1) != debug_ptr &&
309 left >= debug_prefix_length)
311 int len = debug_ptr - p;
312 memmove(p + debug_prefix_length, p, len + 1);
313 memmove(p, debug_buffer, debug_prefix_length);
314 debug_ptr += debug_prefix_length;
315 left -= debug_prefix_length;
319 fprintf(debug_file, "%s", CS debug_buffer);
321 debug_ptr = debug_buffer;
322 debug_prefix_length = 0;
329 /* Output the details of a socket */
332 debug_print_socket(int fd)
335 if (fstat(fd, &s) == 0 && (s.st_mode & S_IFMT) == S_IFSOCK)
339 socklen_t vlen = sizeof(val);
340 struct sockaddr_storage a;
341 socklen_t alen = sizeof(a);
342 struct sockaddr_in * sinp = (struct sockaddr_in *)&a;
343 struct sockaddr_in6 * sin6p = (struct sockaddr_in6 *)&a;
344 struct sockaddr_un * sunp = (struct sockaddr_un *)&a;
346 if (getsockname(fd, (struct sockaddr*)&a, &alen) == 0)
350 g = string_cat(g, US"domain AF_INET");
351 g = string_fmt_append(g, " lcl [%s]:%u",
352 inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port));
353 alen = sizeof(*sinp);
354 if (getpeername(fd, sinp, &alen) == 0)
355 g = string_fmt_append(g, " rmt [%s]:%u",
356 inet_ntoa(sinp->sin_addr), ntohs(sinp->sin_port));
361 g = string_cat(g, US"domain AF_INET6");
362 g = string_fmt_append(g, " lcl [%s]:%u",
363 inet_ntop(AF_INET6, &sin6p->sin6_addr, CS buf, sizeof(buf)),
364 ntohs(sin6p->sin6_port));
365 alen = sizeof(*sin6p);
366 if (getpeername(fd, sin6p, &alen) == 0)
367 g = string_fmt_append(g, " rmt [%s]:%u",
368 inet_ntop(AF_INET6, &sin6p->sin6_addr, CS buf, sizeof(buf)),
369 ntohs(sin6p->sin6_port));
373 g = string_cat(g, US"domain AF_UNIX");
374 if (alen > sizeof(sa_family_t)) /* not unix(7) "unnamed socket" */
375 g = string_fmt_append(g, " lcl %s%s",
376 sunp->sun_path[0] ? US"" : US"@",
377 sunp->sun_path[0] ? sunp->sun_path : sunp->sun_path+1);
378 alen = sizeof(*sunp);
379 if (getpeername(fd, sunp, &alen) == 0)
380 g = string_fmt_append(g, " rmt %s%s",
381 sunp->sun_path[0] ? US"" : US"@",
382 sunp->sun_path[0] ? sunp->sun_path : sunp->sun_path+1);
385 g = string_fmt_append(g, "domain %u", sinp->sin_family);
388 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &val, &vlen) == 0)
391 case SOCK_STREAM: g = string_cat(g, US" type SOCK_STREAM"); break;
392 case SOCK_DGRAM: g = string_cat(g, US" type SOCK_DGRAM"); break;
393 default: g = string_fmt_append(g, " type %d", val); break;
396 if (getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &val, &vlen) == 0)
398 struct protoent * p = getprotobynumber(val);
400 ? string_fmt_append(g, " proto %s", p->p_name)
401 : string_fmt_append(g, " proto %d", val);
404 debug_printf_indent(" socket: %s\n", string_from_gstring(g));
407 debug_printf_indent(" fd st_mode 0%o\n", s.st_mode);