diff options
author | Eric Faurot <eric@cvs.openbsd.org> | 2012-09-09 09:42:07 +0000 |
---|---|---|
committer | Eric Faurot <eric@cvs.openbsd.org> | 2012-09-09 09:42:07 +0000 |
commit | 0d3ecf9a0af1218786796ea9803a89878a701907 (patch) | |
tree | 9657844b12222e505791f42e71d5607f6d4fc2aa /lib/libc/asr | |
parent | f831aa5327541026a22caeb47744830d30e97121 (diff) |
use proper macros for debug traces.
Diffstat (limited to 'lib/libc/asr')
-rw-r--r-- | lib/libc/asr/asr.c | 129 | ||||
-rw-r--r-- | lib/libc/asr/asr_debug.c | 18 | ||||
-rw-r--r-- | lib/libc/asr/asr_private.h | 17 | ||||
-rw-r--r-- | lib/libc/asr/res_search_async.c | 13 | ||||
-rw-r--r-- | lib/libc/asr/res_send_async.c | 90 |
5 files changed, 89 insertions, 178 deletions
diff --git a/lib/libc/asr/asr.c b/lib/libc/asr/asr.c index f6e8ffc6899..e328e224f82 100644 --- a/lib/libc/asr/asr.c +++ b/lib/libc/asr/asr.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asr.c,v 1.9 2012/09/07 13:49:43 eric Exp $ */ +/* $OpenBSD: asr.c,v 1.10 2012/09/09 09:42:06 eric Exp $ */ /* * Copyright (c) 2010-2012 Eric Faurot <eric@openbsd.org> * @@ -68,13 +68,14 @@ async_resolver(const char *conf) static int init = 0; struct asr *asr; -#ifdef DEBUG if (init == 0) { +#ifdef DEBUG if (getenv("ASR_DEBUG")) - asr_debug = 1; + asr_debug = stderr; +#endif init = 1; } -#endif + if ((asr = calloc(1, sizeof(*asr))) == NULL) goto fail; @@ -161,24 +162,16 @@ async_run(struct async *as, struct async_res *ar) { int r, saved_errno = errno; -#ifdef DEBUG - asr_printf("asr: async_run(%p, %p) %s ctx=[%p]\n", - as, ar, asr_querystr(as->as_type), as->as_ctx); -#endif + DPRINT("asr: async_run(%p, %p) %s ctx=[%p]\n", as, ar, + asr_querystr(as->as_type), as->as_ctx); r = as->as_run(as, ar); -#ifdef DEBUG - if (asr_debug) { - asr_printf("asr: async_run(%p, %p) -> %s", as, ar, - asr_transitionstr(r)); - if (r == ASYNC_COND) - asr_printf(" fd=%i timeout=%i\n", - ar->ar_fd, ar->ar_timeout); - else - asr_printf("\n"); - fflush(stderr); - } -#endif + DPRINT("asr: async_run(%p, %p) -> %s", as, ar, asr_transitionstr(r)); + if (r == ASYNC_COND) + DPRINT(" fd=%i timeout=%i\n", ar->ar_fd, ar->ar_timeout); + else + DPRINT("\n"); + if (r == ASYNC_DONE) async_free(as); @@ -223,10 +216,9 @@ struct async * async_new(struct asr_ctx *ac, int type) { struct async *as; -#ifdef DEBUG - asr_printf("asr: async_new(ctx=%p) type=%i refcount=%i\n", - ac, type, ac->ac_refcount); -#endif + + DPRINT("asr: async_new(ctx=%p) type=%i refcount=%i\n", ac, type, + ac->ac_refcount); if ((as = calloc(1, sizeof(*as))) == NULL) return (NULL); @@ -245,9 +237,7 @@ async_new(struct asr_ctx *ac, int type) void async_free(struct async *as) { -#ifdef DEBUG - asr_printf("asr: async_free(%p)\n", as); -#endif + DPRINT("asr: async_free(%p)\n", as); switch(as->as_type) { case ASR_SEND: if (as->as_fd != -1) @@ -326,15 +316,10 @@ asr_use_resolver(struct asr *asr) struct asr **priv; if (asr == NULL) { - /* Use the thread-local resolver. */ -#ifdef DEBUG - asr_printf("using thread-local resolver\n"); -#endif + DPRINT("using thread-local resolver\n"); priv = _THREAD_PRIVATE(_asr, asr, &_asr); if (*priv == NULL) { -#ifdef DEBUG - asr_printf("setting up thread-local resolver\n"); -#endif + DPRINT("setting up thread-local resolver\n"); *priv = async_resolver(NULL); } asr = *priv; @@ -348,10 +333,7 @@ asr_use_resolver(struct asr *asr) static void asr_ctx_ref(struct asr_ctx *ac) { -#ifdef DEBUG - asr_printf("asr: asr_ctx_ref(ctx=%p) refcount=%i\n", - ac, ac->ac_refcount); -#endif + DPRINT("asr: asr_ctx_ref(ctx=%p) refcount=%i\n", ac, ac->ac_refcount); ac->ac_refcount += 1; } @@ -362,10 +344,7 @@ asr_ctx_ref(struct asr_ctx *ac) void asr_ctx_unref(struct asr_ctx *ac) { -#ifdef DEBUG - asr_printf("asr: asr_ctx_unref(ctx=%p) refcount=%i\n", - ac, ac->ac_refcount); -#endif + DPRINT("asr: asr_ctx_unref(ctx=%p) refcount=%i\n", ac, ac->ac_refcount); if (--ac->ac_refcount) return; @@ -407,20 +386,14 @@ asr_check_reload(struct asr *asr) return; asr->a_rtime = tp.tv_sec; -#ifdef DEBUG - asr_printf("asr: checking for update of \"%s\"\n", asr->a_path); -#endif - + DPRINT("asr: checking for update of \"%s\"\n", asr->a_path); if (stat(asr->a_path, &st) == -1 || asr->a_mtime == st.st_mtime || (ac = asr_ctx_create()) == NULL) return; asr->a_mtime = st.st_mtime; -#ifdef DEBUG - asr_printf("asr: reloading config file\n"); -#endif - + DPRINT("asr: reloading config file\n"); if (asr_ctx_from_file(ac, asr->a_path) == -1) { asr_ctx_free(ac); return; @@ -705,9 +678,7 @@ asr_ctx_from_file(struct asr_ctx *ac, const char *path) r = fread(buf, 1, sizeof buf - 1, cf); if (feof(cf) == 0) { -#ifdef DEBUG - asr_printf("asr: config file too long: \"%s\"\n", path); -#endif + DPRINT("asr: config file too long: \"%s\"\n", path); r = -1; } fclose(cf); @@ -896,17 +867,14 @@ int asr_iter_db(struct async *as) { if (as->as_db_idx >= as->as_ctx->ac_dbcount) { -#ifdef DEBUG - asr_printf("asr_iter_db: done\n"); -#endif + DPRINT("asr_iter_db: done\n"); return (-1); } as->as_db_idx += 1; as->as_ns_idx = 0; -#ifdef DEBUG - asr_printf("asr_iter_db: %i\n", as->as_db_idx); -#endif + DPRINT("asr_iter_db: %i\n", as->as_db_idx); + return (0); } @@ -928,9 +896,7 @@ asr_iter_ns(struct async *as) break; as->as_ns_idx = 0; as->as_ns_cycles++; -#ifdef DEBUG - asr_printf("asr: asr_iter_ns(): cycle %i\n", as->as_ns_cycles); -#endif + DPRINT("asr: asr_iter_ns(): cycle %i\n", as->as_ns_cycles); } return (0); @@ -969,9 +935,7 @@ asr_iter_domain(struct async *as, const char *name, char * buf, size_t len) * don't try anything else. */ if (strlen(name) && name[strlen(name) - 1] == '.') { -#ifdef DEBUG - asr_printf("asr: asr_iter_domain(\"%s\") fqdn\n", name); -#endif + DPRINT("asr: asr_iter_domain(\"%s\") fqdn\n", name); as->as_dom_flags |= ASYNC_DOM_FQDN; as->as_dom_step = DOM_DONE; return (asr_domcat(name, NULL, buf, len)); @@ -984,10 +948,8 @@ asr_iter_domain(struct async *as, const char *name, char * buf, size_t len) if ((as->as_ctx->ac_options & RES_NOALIASES) == 0 && asr_ndots(name) == 0 && (alias = asr_hostalias(name, buf, len)) != NULL) { -#ifdef DEBUG - asr_printf("asr: asr_iter_domain(\"%s\") is alias " - "\"%s\"\n", name, alias); -#endif + DPRINT("asr: asr_iter_domain(\"%s\") is alias \"%s\"\n", + name, alias); as->as_dom_flags |= ASYNC_DOM_HOSTALIAS; as->as_dom_step = DOM_DONE; return (asr_domcat(alias, NULL, buf, len)); @@ -1004,10 +966,7 @@ asr_iter_domain(struct async *as, const char *name, char * buf, size_t len) * in resolv.conf(5). */ if ((asr_ndots(name)) >= as->as_ctx->ac_ndots) { -#ifdef DEBUG - asr_printf("asr: asr_iter_domain(\"%s\") ndots\n", - name); -#endif + DPRINT("asr: asr_iter_domain(\"%s\") ndots\n", name); as->as_dom_flags |= ASYNC_DOM_NDOTS; strlcpy(buf, name, len); return (0); @@ -1017,11 +976,8 @@ asr_iter_domain(struct async *as, const char *name, char * buf, size_t len) case DOM_DOMAIN: if (as->as_dom_idx < as->as_ctx->ac_domcount) { -#ifdef DEBUG - asr_printf("asr: asr_iter_domain(\"%s\") " - "domain \"%s\"\n", name, - as->as_ctx->ac_dom[as->as_dom_idx]); -#endif + DPRINT("asr: asr_iter_domain(\"%s\") domain \"%s\"\n", + name, as->as_ctx->ac_dom[as->as_dom_idx]); as->as_dom_flags |= ASYNC_DOM_DOMAIN; return (asr_domcat(name, as->as_ctx->ac_dom[as->as_dom_idx++], buf, len)); @@ -1036,10 +992,7 @@ asr_iter_domain(struct async *as, const char *name, char * buf, size_t len) * do it now. */ if (!(as->as_dom_flags & ASYNC_DOM_NDOTS)) { -#ifdef DEBUG - asr_printf("asr: asr_iter_domain(\"%s\") as is\n", - name); -#endif + DPRINT("asr: asr_iter_domain(\"%s\") as is\n", name); as->as_dom_flags |= ASYNC_DOM_ASIS; strlcpy(buf, name, len); return (0); @@ -1048,9 +1001,7 @@ asr_iter_domain(struct async *as, const char *name, char * buf, size_t len) case DOM_DONE: default: -#ifdef DEBUG - asr_printf("asr: asr_iter_domain(\"%s\") done\n", name); -#endif + DPRINT("asr: asr_iter_domain(\"%s\") done\n", name); return (-1); } } @@ -1072,9 +1023,7 @@ asr_hostalias(const char *name, char *abuf, size_t abufsz) if (file == NULL || issetugid() != 0 || (fp = fopen(file, "r")) == NULL) return (NULL); -#ifdef DEBUG - asr_printf("asr: looking up aliases in \"%s\"\n", file); -#endif + DPRINT("asr: looking up aliases in \"%s\"\n", file); while ((buf = fgetln(fp, &len)) != NULL) { if (buf[len - 1] == '\n') @@ -1085,9 +1034,7 @@ asr_hostalias(const char *name, char *abuf, size_t abufsz) if (!strcasecmp(tokens[0], name)) { if (strlcpy(abuf, tokens[1], abufsz) > abufsz) continue; -#ifdef DEBUG - asr_printf("asr: found alias \"%s\"\n", abuf); -#endif + DPRINT("asr: found alias \"%s\"\n", abuf); fclose(fp); return (abuf); } diff --git a/lib/libc/asr/asr_debug.c b/lib/libc/asr/asr_debug.c index 0085b9ac3b5..a961101994c 100644 --- a/lib/libc/asr/asr_debug.c +++ b/lib/libc/asr/asr_debug.c @@ -1,4 +1,4 @@ -/* $OpenBSD: asr_debug.c,v 1.6 2012/09/05 21:49:12 eric Exp $ */ +/* $OpenBSD: asr_debug.c,v 1.7 2012/09/09 09:42:06 eric Exp $ */ /* * Copyright (c) 2010-2012 Eric Faurot <eric@openbsd.org> * @@ -29,7 +29,7 @@ #include "asr.h" #include "asr_private.h" -static void asr_vdebug(const char *, va_list); +static void asr_printf(const char *, ...); static char *print_dname(const char *, char *, size_t); static char *print_host(const struct sockaddr *, char *, size_t); @@ -306,7 +306,7 @@ struct kv { int code; const char *name; }; static const char* kvlookup(struct kv *, int); -int asr_debug = 0; +FILE * asr_debug = NULL; void asr_dump(struct asr *a) @@ -542,19 +542,15 @@ asr_dump_packet(FILE *f, const void *data, size_t len, int noid) } static void -asr_vdebug(const char *fmt, va_list ap) -{ - if (asr_debug) - vfprintf(stderr, fmt, ap); -} - -void asr_printf(const char *fmt, ...) { va_list ap; + if (asr_debug == NULL) + return; + va_start(ap, fmt); - asr_vdebug(fmt, ap); + vfprintf(asr_debug, fmt, ap); va_end(ap); } diff --git a/lib/libc/asr/asr_private.h b/lib/libc/asr/asr_private.h index 7562ca5b190..1342d19e841 100644 --- a/lib/libc/asr/asr_private.h +++ b/lib/libc/asr/asr_private.h @@ -1,4 +1,4 @@ -/* $OpenBSD: asr_private.h,v 1.6 2012/09/06 13:57:51 eric Exp $ */ +/* $OpenBSD: asr_private.h,v 1.7 2012/09/09 09:42:06 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -329,7 +329,16 @@ struct async *gethostbyaddr_async_ctx(const void *, socklen_t, int, #ifdef DEBUG -extern int asr_debug; +extern FILE * asr_debug; + +#define DPRINT(...) do { if(asr_debug) { \ + fprintf(asr_debug, __VA_ARGS__); \ + } } while (0) +#define DPRINT_PACKET(n, p, s) do { if(asr_debug) { \ + fprintf(asr_debug, "----- %s -----\n", n); \ + asr_dump_packet(asr_debug, (p), (s), 0); \ + fprintf(asr_debug, "--------------\n"); \ + } } while (0) /* asr_debug.h */ const char *asr_querystr(int); @@ -337,12 +346,14 @@ const char *asr_transitionstr(int); void asr_dump(struct asr *); void asr_dump_async(struct async *); void asr_dump_packet(FILE *, const void *, size_t, int); -void asr_printf(const char *fmt, ...); void async_set_state(struct async *, int); char *asr_print_addr(const struct sockaddr *, char *, size_t); #else /* DEBUG */ +#define DPRINT(...) +#define DPRINT_PACKET(...) + #define async_set_state(a, s) do { (a)->as_state = (s); } while (0) #endif /* DEBUG */ diff --git a/lib/libc/asr/res_search_async.c b/lib/libc/asr/res_search_async.c index 99e315313e8..5ec59e48ecd 100644 --- a/lib/libc/asr/res_search_async.c +++ b/lib/libc/asr/res_search_async.c @@ -1,4 +1,4 @@ -/* $OpenBSD: res_search_async.c,v 1.1 2012/04/14 09:24:18 eric Exp $ */ +/* $OpenBSD: res_search_async.c,v 1.2 2012/09/09 09:42:06 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -48,9 +48,9 @@ res_search_async(const char *name, int class, int type, unsigned char *ans, { struct asr_ctx *ac; struct async *as; -#ifdef DEBUG - asr_printf("asr: res_search_async(\"%s\", %i, %i)\n", name, class, type); -#endif + + DPRINT("asr: res_search_async(\"%s\", %i, %i)\n", name, class, type); + ac = asr_use_resolver(asr); as = res_search_async_ctx(name, class, type, ans, anslen, ac); asr_ctx_unref(ac); @@ -64,10 +64,7 @@ res_search_async_ctx(const char *name, int class, int type, unsigned char *ans, { struct async *as; -#ifdef DEBUG - asr_printf("asr: res_search_async_ctx(\"%s\", %i, %i)\n", - name, class, type); -#endif + DPRINT("asr: res_search_async_ctx(\"%s\", %i, %i)\n", name, class, type); if ((as = async_new(ac, ASR_SEARCH)) == NULL) goto err; /* errno set */ diff --git a/lib/libc/asr/res_send_async.c b/lib/libc/asr/res_send_async.c index cab78a3dc3d..20cc6ff695d 100644 --- a/lib/libc/asr/res_send_async.c +++ b/lib/libc/asr/res_send_async.c @@ -1,4 +1,4 @@ -/* $OpenBSD: res_send_async.c,v 1.2 2012/07/07 20:41:52 eric Exp $ */ +/* $OpenBSD: res_send_async.c,v 1.3 2012/09/09 09:42:06 eric Exp $ */ /* * Copyright (c) 2012 Eric Faurot <eric@openbsd.org> * @@ -58,12 +58,8 @@ res_send_async(const unsigned char *buf, int buflen, unsigned char *ans, struct header h; struct query q; -#ifdef DEBUG - if (asr_debug) { - asr_printf("asr: res_send_async()\n"); - asr_dump_packet(stderr, buf, buflen, 0); - } -#endif + DPRINT_PACKET("asr: res_send_async()", buf, buflen); + ac = asr_use_resolver(asr); if ((as = async_new(ac, ASR_SEND)) == NULL) { asr_ctx_unref(ac); @@ -122,9 +118,9 @@ res_query_async(const char *name, int class, int type, unsigned char *ans, { struct asr_ctx *ac; struct async *as; -#ifdef DEBUG - asr_printf("asr: res_query_async(\"%s\", %i, %i)\n", name, class, type); -#endif + + DPRINT("asr: res_query_async(\"%s\", %i, %i)\n", name, class, type); + ac = asr_use_resolver(asr); as = res_query_async_ctx(name, class, type, ans, anslen, ac); asr_ctx_unref(ac); @@ -138,10 +134,8 @@ res_query_async_ctx(const char *name, int class, int type, unsigned char *ans, { struct async *as; -#ifdef DEBUG - asr_printf("asr: res_query_async_ctx(\"%s\", %i, %i)\n", name, class, - type); -#endif + DPRINT("asr: res_query_async_ctx(\"%s\", %i, %i)\n", name, class, type); + if ((as = async_new(a_ctx, ASR_SEND)) == NULL) return (NULL); /* errno set */ as->as_run = res_send_async_run; @@ -378,25 +372,19 @@ setup_query(struct async *as, const char *name, const char *dom, if (as->as.dns.flags & ASYNC_EXTOBUF) { errno = EINVAL; -#ifdef DEBUG - asr_printf("attempting to write in user packet"); -#endif + DPRINT("attempting to write in user packet"); return (-1); } if (asr_make_fqdn(name, dom, fqdn, sizeof(fqdn)) > sizeof(fqdn)) { errno = EINVAL; -#ifdef DEBUG - asr_printf("asr_make_fqdn: name too long\n"); -#endif + DPRINT("asr_make_fqdn: name too long\n"); return (-1); } if (dname_from_fqdn(fqdn, dname, sizeof(dname)) == -1) { errno = EINVAL; -#ifdef DEBUG - asr_printf("dname_from_fqdn: invalid\n"); -#endif + DPRINT("dname_from_fqdn: invalid\n"); return (-1); } @@ -418,9 +406,7 @@ setup_query(struct async *as, const char *name, const char *dom, pack_header(&p, &h); pack_query(&p, type, class, dname); if (p.err) { -#ifdef DEBUG - asr_printf("error packing query"); -#endif + DPRINT("error packing query"); errno = EINVAL; return (-1); } @@ -433,20 +419,12 @@ setup_query(struct async *as, const char *name, const char *dom, free(as->as.dns.dname); as->as.dns.dname = strdup(dname); if (as->as.dns.dname == NULL) { -#ifdef DEBUG - asr_printf("strdup"); -#endif + DPRINT("strdup"); return (-1); /* errno set */ } as->as.dns.obuflen = p.offset; -#ifdef DEBUG - if (asr_debug) { - asr_printf("------- asr_setup_query(): packet -------\n"); - asr_dump_packet(stderr, as->as.dns.obuf, as->as.dns.obuflen, 0); - asr_printf("-----------------------------------------\n"); - } -#endif + DPRINT_PACKET("asr_setup_query", as->as.dns.obuf, as->as.dns.obuflen); return (0); } @@ -463,11 +441,11 @@ udp_send(struct async *as) int save_errno; #ifdef DEBUG char buf[256]; - - if (asr_debug) - asr_printf("asr: [%p] connecting to %s UDP\n", as, - asr_print_addr(AS_NS_SA(as), buf, sizeof buf)); #endif + + DPRINT("asr: [%p] connecting to %s UDP\n", as, + asr_print_addr(AS_NS_SA(as), buf, sizeof buf)); + as->as_fd = sockaddr_connect(AS_NS_SA(as), SOCK_DGRAM); if (as->as_fd == -1) return (-1); /* errno set */ @@ -518,13 +496,7 @@ udp_recv(struct async *as) as->as.dns.ibuflen = n; -#ifdef DEBUG - if (asr_debug) { - asr_printf("------- asr_udp_recv() packet -------\n"); - asr_dump_packet(stderr, as->as.dns.ibuf, as->as.dns.ibuflen, 0); - asr_printf("-------------------------------------\n"); - } -#endif + DPRINT_PACKET("asr_udp_recv()", as->as.dns.ibuf, as->as.dns.ibuflen); if (validate_packet(as) == -1) return (-1); /* errno set */ @@ -552,11 +524,8 @@ tcp_write(struct async *as) /* First try to connect if not already */ if (as->as_fd == -1) { -#ifdef DEBUG - if (asr_debug) - asr_printf("asr: [%p] connecting to %s TCP\n", as, - asr_print_addr(AS_NS_SA(as), buf, sizeof buf)); -#endif + DPRINT("asr: [%p] connecting to %s TCP\n", as, + asr_print_addr(AS_NS_SA(as), buf, sizeof buf)); as->as_fd = sockaddr_connect(AS_NS_SA(as), SOCK_STREAM); if (as->as_fd == -1) return (-1); /* errno set */ @@ -675,13 +644,8 @@ tcp_read(struct async *as) if (as->as.dns.ibuflen != as->as.dns.datalen) return (1); -#ifdef DEBUG - if (asr_debug) { - asr_printf("------- asr_tcp_read() packet -------\n"); - asr_dump_packet(stderr, as->as.dns.ibuf, as->as.dns.ibuflen, 0); - asr_printf("-------------------------------------\n"); - } -#endif + DPRINT_PACKET("asr_tcp_read()", as->as.dns.ibuf, as->as.dns.ibuflen); + if (validate_packet(as) == -1) goto close; /* errno set */ @@ -751,9 +715,7 @@ validate_packet(struct async *as) goto inval; if (h.id != as->as.dns.reqid) { -#ifdef DEBUG - asr_printf("incorrect reqid\n"); -#endif + DPRINT("incorrect reqid\n"); goto inval; } if (h.qdcount != 1) @@ -778,10 +740,8 @@ validate_packet(struct async *as) if (q.q_type != as->as.dns.type || q.q_class != as->as.dns.class || strcasecmp(q.q_dname, as->as.dns.dname)) { -#ifdef DEBUG - asr_printf("incorrect type/class/dname '%s' != '%s'\n", + DPRINT("incorrect type/class/dname '%s' != '%s'\n", q.q_dname, as->as.dns.dname); -#endif goto inval; } |