The branch main has been updated by sjg: URL: https://cgit.FreeBSD.org/src/commit/?id=e67aef419093b08984b8a2de535bc3e4ce13e087
commit e67aef419093b08984b8a2de535bc3e4ce13e087 Author: Simon J. Gerraty <s...@freebsd.org> AuthorDate: 2025-07-17 23:36:17 +0000 Commit: Simon J. Gerraty <s...@freebsd.org> CommitDate: 2025-07-17 23:36:17 +0000 Add DEBUG_PRINTF to stand.h stand/ is mostly debugged with printfs, in an ad hoc and sometimes fragile manner. For example BOOTP_DEBUG in bootp.c cannot be defined unless NETIF_DEBUG is defined in dev_net.c or build fails for lack of the symbol debug. The DEBUG_PRINTF implementation in stand.h addresses that and allows for more control over debug output. It is compatible with the usage in libsecureboot. Simply define _DEBUG_LEVEL to the desired level of debug or in the case of libsecureboot _DEBUG_LEVEL_VAR to the variable that will hold that value - default is _debug which is static so each translation unit can be controlled independently. The 1st arg to DEBUG_PRINTF is a level which must be greater than or equal to _DEBUG_LEVEL_VAR if the printf is to be called. See libsecureboot for more examples. Reviewed by: imp Sponsored by: Juniper Networks, Inc. Differential Revision: https://reviews.freebsd.org/D51269 --- lib/libsecureboot/h/libsecureboot.h | 1 + stand/common/dev_net.c | 57 ++++++++------------------- stand/libsa/bootp.c | 78 +++++++++---------------------------- stand/libsa/pkgfs.c | 33 +++++++++------- stand/libsa/stand.h | 13 +++++++ 5 files changed, 68 insertions(+), 114 deletions(-) diff --git a/lib/libsecureboot/h/libsecureboot.h b/lib/libsecureboot/h/libsecureboot.h index 017558536825..d32df9594332 100644 --- a/lib/libsecureboot/h/libsecureboot.h +++ b/lib/libsecureboot/h/libsecureboot.h @@ -29,6 +29,7 @@ #include <sys/param.h> #ifdef _STANDALONE +#define _DEBUG_LEVEL_VAR DebugVe #include <stand.h> #else #include <sys/types.h> diff --git a/stand/common/dev_net.c b/stand/common/dev_net.c index fc6b43ec7a40..964fa514cac5 100644 --- a/stand/common/dev_net.c +++ b/stand/common/dev_net.c @@ -66,10 +66,6 @@ #include "dev_net.h" #include "bootstrap.h" -#ifdef NETIF_DEBUG -int debug = 0; -#endif - static char *netdev_name; static int netdev_sock = -1; static int netdev_opens; @@ -143,11 +139,8 @@ net_open(struct open_file *f, ...) return (ENXIO); } netdev_name = strdup(devname); -#ifdef NETIF_DEBUG - if (debug) - printf("%s: netif_open() succeeded\n", - __func__); -#endif + DEBUG_PRINTF(1,("%s: netif_open() succeeded %#x\n", + __func__, rootip.s_addr)); } /* * If network params were not set by netif_open(), try to get @@ -200,10 +193,7 @@ net_close(struct open_file *f) { struct devdesc *dev; -#ifdef NETIF_DEBUG - if (debug) - printf("%s: opens=%d\n", __func__, netdev_opens); -#endif + DEBUG_PRINTF(1,("%s: opens=%d\n", __func__, netdev_opens)); dev = f->f_devdata; dev->d_opendata = NULL; @@ -216,10 +206,7 @@ net_cleanup(void) { if (netdev_sock >= 0) { -#ifdef NETIF_DEBUG - if (debug) - printf("%s: calling netif_close()\n", __func__); -#endif + DEBUG_PRINTF(1,("%s: calling netif_close()\n", __func__)); rootip.s_addr = 0; free(netdev_name); netif_close(netdev_sock); @@ -271,10 +258,7 @@ net_getparams(int sock) bootp(sock); if (myip.s_addr != 0) goto exit; -#ifdef NETIF_DEBUG - if (debug) - printf("%s: BOOTP failed, trying RARP/RPC...\n", __func__); -#endif + DEBUG_PRINTF(1,("%s: BOOTP failed, trying RARP/RPC...\n", __func__)); #endif /* @@ -292,10 +276,7 @@ net_getparams(int sock) printf("%s: bootparam/whoami RPC failed\n", __func__); return (EIO); } -#ifdef NETIF_DEBUG - if (debug) - printf("%s: client name: %s\n", __func__, hostname); -#endif + DEBUG_PRINTF(1,("%s: client name: %s\n", __func__, hostname)); /* * Ignore the gateway from whoami (unreliable). @@ -309,16 +290,12 @@ net_getparams(int sock) } if (smask) { netmask = smask; -#ifdef NETIF_DEBUG - if (debug) - printf("%s: subnet mask: %s\n", __func__, - intoa(netmask)); -#endif + DEBUG_PRINTF(1,("%s: subnet mask: %s\n", __func__, + intoa(netmask))); } -#ifdef NETIF_DEBUG - if (gateip.s_addr && debug) - printf("%s: net gateway: %s\n", __func__, inet_ntoa(gateip)); -#endif + if (gateip.s_addr) + DEBUG_PRINTF(1,("%s: net gateway: %s\n", __func__, + inet_ntoa(gateip))); /* Get the root server and pathname. */ if (bp_getfile(sock, "root", &rootip, rootpath)) { @@ -329,12 +306,10 @@ exit: if ((rootaddr = net_parse_rootpath()) != INADDR_NONE) rootip.s_addr = rootaddr; -#ifdef NETIF_DEBUG - if (debug) { - printf("%s: server addr: %s\n", __func__, inet_ntoa(rootip)); - printf("%s: server path: %s\n", __func__, rootpath); - } -#endif + DEBUG_PRINTF(1,("%s: proto: %d\n", __func__, netproto)); + DEBUG_PRINTF(1,("%s: server addr: %s\n", __func__, inet_ntoa(rootip))); + DEBUG_PRINTF(1,("%s: server port: %d\n", __func__, rootport)); + DEBUG_PRINTF(1,("%s: server path: %s\n", __func__, rootpath)); return (0); } @@ -410,6 +385,8 @@ net_parse_rootpath(void) (void)strsep(&ptr, ":"); if (ptr != NULL) { addr = inet_addr(rootpath); + DEBUG_PRINTF(1,("rootpath=%s addr=%#x\n", + rootpath, addr)); bcopy(ptr, rootpath, strlen(ptr) + 1); } } else { diff --git a/stand/libsa/bootp.c b/stand/libsa/bootp.c index d919bb59e843..ac37553c6d34 100644 --- a/stand/libsa/bootp.c +++ b/stand/libsa/bootp.c @@ -42,7 +42,6 @@ #include <string.h> -#define BOOTP_DEBUGxx #define SUPPORT_DHCP #define DHCP_ENV_NOVENDOR 1 /* do not parse vendor options */ @@ -130,10 +129,7 @@ bootp(int sock) } wbuf; struct bootp *rbootp; -#ifdef BOOTP_DEBUG - if (debug) - printf("bootp: socket=%d\n", sock); -#endif + DEBUG_PRINTF(1, ("bootp: socket=%d\n", sock)); if (!bot) bot = getsecs(); @@ -141,10 +137,7 @@ bootp(int sock) printf("bootp: bad socket. %d\n", sock); return; } -#ifdef BOOTP_DEBUG - if (debug) - printf("bootp: d=%lx\n", (long)d); -#endif + DEBUG_PRINTF(1, ("bootp: socktodesc=%lx\n", (long)d)); bp = &wbuf.wbootp; bzero(bp, sizeof(*bp)); @@ -225,31 +218,20 @@ bootp(int sock) netmask = htonl(IN_CLASSB_NET); else netmask = htonl(IN_CLASSC_NET); -#ifdef BOOTP_DEBUG - if (debug) - printf("'native netmask' is %s\n", intoa(netmask)); -#endif + DEBUG_PRINTF(1, ("'native netmask' is %s\n", intoa(netmask))); } -#ifdef BOOTP_DEBUG - if (debug) - printf("mask: %s\n", intoa(netmask)); -#endif + DEBUG_PRINTF(1,("rootip: %s\n", inet_ntoa(rootip))); + DEBUG_PRINTF(1,("mask: %s\n", intoa(netmask))); /* We need a gateway if root is on a different net */ if (!SAMENET(myip, rootip, netmask)) { -#ifdef BOOTP_DEBUG - if (debug) - printf("need gateway for root ip\n"); -#endif + DEBUG_PRINTF(1,("need gateway for root ip\n")); } /* Toss gateway if on a different net */ if (!SAMENET(myip, gateip, netmask)) { -#ifdef BOOTP_DEBUG - if (debug) - printf("gateway ip (%s) bad\n", inet_ntoa(gateip)); -#endif + DEBUG_PRINTF(1,("gateway ip (%s) bad\n", inet_ntoa(gateip))); gateip.s_addr = 0; } @@ -264,18 +246,11 @@ bootpsend(struct iodesc *d, void *pkt, size_t len) { struct bootp *bp; -#ifdef BOOTP_DEBUG - if (debug) - printf("bootpsend: d=%lx called.\n", (long)d); -#endif - + DEBUG_PRINTF(1,("bootpsend: d=%lx called.\n", (long)d)); bp = pkt; bp->bp_secs = htons((u_short)(getsecs() - bot)); -#ifdef BOOTP_DEBUG - if (debug) - printf("bootpsend: calling sendudp\n"); -#endif + DEBUG_PRINTF(1,("bootpsend: calling sendudp\n")); return (sendudp(d, pkt, len)); } @@ -288,34 +263,22 @@ bootprecv(struct iodesc *d, void **pkt, void **payload, time_t tleft, struct bootp *bp; void *ptr; -#ifdef BOOTP_DEBUG - if (debug) - printf("bootp_recvoffer: called\n"); -#endif + DEBUG_PRINTF(1,("bootp_recvoffer: called\n")); ptr = NULL; n = readudp(d, &ptr, (void **)&bp, tleft); if (n == -1 || n < sizeof(struct bootp) - BOOTP_VENDSIZE) goto bad; -#ifdef BOOTP_DEBUG - if (debug) - printf("bootprecv: checked. bp = %p, n = %zd\n", bp, n); -#endif + DEBUG_PRINTF(1,("bootprecv: checked. bp = %p, n = %zd\n", bp, n)); + if (bp->bp_xid != htonl(d->xid)) { -#ifdef BOOTP_DEBUG - if (debug) { - printf("bootprecv: expected xid 0x%lx, got 0x%x\n", - d->xid, ntohl(bp->bp_xid)); - } -#endif + DEBUG_PRINTF(1,("bootprecv: expected xid 0x%lx, got 0x%x\n", + d->xid, ntohl(bp->bp_xid))); goto bad; } -#ifdef BOOTP_DEBUG - if (debug) - printf("bootprecv: got one!\n"); -#endif + DEBUG_PRINTF(1,("bootprecv: got one!\n")); /* Suck out vendor info */ if (bcmp(vm_rfc1048, bp->bp_vend, sizeof(vm_rfc1048)) == 0) { @@ -359,10 +322,7 @@ vend_rfc1048(u_char *cp, u_int len) u_char tag; const char *val; -#ifdef BOOTP_DEBUG - if (debug) - printf("vend_rfc1048 bootp info. len=%d\n", len); -#endif + DEBUG_PRINTF(1,("vend_rfc1048 bootp info. len=%d\n", len)); ep = cp + len; /* Step over magic cookie */ @@ -443,10 +403,8 @@ vend_cmu(u_char *cp) { struct cmu_vend *vp; -#ifdef BOOTP_DEBUG - if (debug) - printf("vend_cmu bootp info.\n"); -#endif + DEBUG_PRINTF(1,("vend_cmu bootp info.\n")); + vp = (struct cmu_vend *)cp; if (vp->v_smask.s_addr != 0) { diff --git a/stand/libsa/pkgfs.c b/stand/libsa/pkgfs.c index 64ebdf033f14..32d488de5cfb 100644 --- a/stand/libsa/pkgfs.c +++ b/stand/libsa/pkgfs.c @@ -31,12 +31,6 @@ #include <string.h> #include <zlib.h> -#ifdef PKGFS_DEBUG -#define DBG(x) printf x -#else -#define DBG(x) -#endif - static int pkg_open(const char *, struct open_file *); static int pkg_close(struct open_file *); static int pkg_read(struct open_file *, void *, size_t, size_t *); @@ -172,6 +166,9 @@ pkgfs_init(const char *pkgname, struct fs_ops *proto) exclusive_file_system = NULL; + DEBUG_PRINTF(0, ("%s(%s: '%s') -> %d (error=%d)\n", __func__, + proto->fs_name, pkgname, fd, errno)); + if (fd == -1) return (errno); @@ -239,7 +236,7 @@ pkg_open_follow(const char *fn, struct open_file *f, int lnks) if (strcmp(fn, tf->tf_hdr.ut_name) == 0) { f->f_fsdata = tf; tf->tf_fp = 0; /* Reset the file pointer. */ - DBG(("%s: found %s type %c\n", __func__, + DEBUG_PRINTF(1, ("%s: found %s type %c\n", __func__, fn, tf->tf_hdr.ut_typeflag[0])); if (tf->tf_hdr.ut_typeflag[0] == '2') { /* we have a symlink @@ -275,6 +272,7 @@ pkg_close(struct open_file *f) /* * Free up the cache if we read all of the file. */ + DEBUG_PRINTF(1, ("%s(%s)\n", __func__, tf->tf_hdr.ut_name)); if (tf->tf_fp == tf->tf_size && tf->tf_cachesz > 0) { free(tf->tf_cache); tf->tf_cachesz = 0; @@ -297,6 +295,8 @@ pkg_read(struct open_file *f, void *buf, size_t size, size_t *res) return (EBADF); } + DEBUG_PRINTF(4, ("%s(%s,%zd)\n", __func__, tf->tf_hdr.ut_name, size)); + if (tf->tf_cachesz == 0) cache_data(tf, 1); @@ -334,6 +334,8 @@ pkg_read(struct open_file *f, void *buf, size_t size, size_t *res) tf->tf_fp = fp; if (res != NULL) *res = size; + DEBUG_PRINTF(4, ("%s(%s) res=%zd\n", __func__, tf->tf_hdr.ut_name, + (ssize_t)(tf->tf_size - tf->tf_fp))); return ((sz == -1) ? errno : 0); } @@ -377,7 +379,7 @@ pkg_seek(struct open_file *f, off_t ofs, int whence) return (tf->tf_fp); } } - DBG(("%s: negative file seek (%jd)\n", __func__, + DEBUG_PRINTF(3, ("%s: negative file seek (%jd)\n", __func__, (intmax_t)delta)); errno = ESPIPE; return (-1); @@ -511,26 +513,28 @@ cache_data(struct tarfile *tf, int force) size_t sz; if (tf == NULL) { - DBG(("%s: no file to cache data for?\n", __func__)); + DEBUG_PRINTF(5, ("%s: no file to cache data for?\n", + __func__)); errno = EINVAL; return (-1); } pkg = tf->tf_pkg; if (pkg == NULL) { - DBG(("%s: no package associated with file?\n", __func__)); + DEBUG_PRINTF(5, ("%s: no package associated with file?\n", + __func__)); errno = EINVAL; return (-1); } if (tf->tf_cachesz > 0) { - DBG(("%s: data already cached\n", __func__)); + DEBUG_PRINTF(5, ("%s: data already cached\n", __func__)); errno = EINVAL; return (-1); } if (tf->tf_ofs != pkg->pkg_ofs) { - DBG(("%s: caching after force read of file %s?\n", + DEBUG_PRINTF(5, ("%s: caching after force read of file %s?\n", __func__, tf->tf_hdr.ut_name)); errno = EINVAL; return (-1); @@ -548,7 +552,8 @@ cache_data(struct tarfile *tf, int force) tf->tf_cache = malloc(sz); if (tf->tf_cache == NULL) { - DBG(("%s: could not allocate %d bytes\n", __func__, (int)sz)); + DEBUG_PRINTF(5, ("%s: could not allocate %d bytes\n", + __func__, (int)sz)); errno = ENOMEM; return (-1); } @@ -732,7 +737,7 @@ new_package(int fd, struct package **pp) } /* - * Done parsing the ZIP header. Spkgt the inflation engine. + * Done parsing the ZIP header. Start the inflation engine. */ error = inflateInit2(&pkg->pkg_zs, -15); if (error != Z_OK) diff --git a/stand/libsa/stand.h b/stand/libsa/stand.h index e1188fb73a26..8b7d93074ef2 100644 --- a/stand/libsa/stand.h +++ b/stand/libsa/stand.h @@ -558,4 +558,17 @@ void tslog_getbuf(void ** buf, size_t * len); __END_DECLS +/* define _DEBUG_LEVEL n or _DEBUG_LEVEL_VAR before include */ +#ifndef DEBUG_PRINTF +# if defined(_DEBUG_LEVEL) || defined(_DEBUG_LEVEL_VAR) +# ifndef _DEBUG_LEVEL_VAR +# define _DEBUG_LEVEL_VAR _debug +static int _debug = _DEBUG_LEVEL; +# endif +# define DEBUG_PRINTF(n, args) if (_DEBUG_LEVEL_VAR >= n) printf args +# else +# define DEBUG_PRINTF(n, args) +# endif +#endif + #endif /* STAND_H */