efficient debug-to-file output (/tmp racy, but hey)

This commit is contained in:
tg 2012-03-23 19:38:12 +00:00
parent 4f043002be
commit c5cc22a13f
3 changed files with 57 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/Makefile,v 1.92 2011/12/31 02:04:17 tg Exp $
# $MirOS: src/bin/mksh/Makefile,v 1.93 2012/03/23 19:38:11 tg Exp $
#-
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
# Thorsten Glaser <tg@mirbsd.org>
@ -58,6 +58,11 @@ SRCS+= printf.c
CPPFLAGS+= -DMKSH_PRINTF_BUILTIN
.endif
DEBUGFILE?= No
.if ${DEBUGFILE:L} == "yes"
CPPFLAGS+= -DDF=mksh_debugtofile
.endif
MANLINKS= [ false pwd sh sleep test true
BINLINKS= ${MANLINKS} echo domainname kill
.for _i in ${BINLINKS}

46
main.c
View File

@ -34,7 +34,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.206 2012/03/23 18:58:15 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.207 2012/03/23 19:38:12 tg Exp $");
extern char **environ;
@ -1242,7 +1242,13 @@ can_seek(int fd)
SHF_UNBUF : 0);
}
struct shf shf_iob[3];
#ifdef DF
int shl_dbg_fd;
#define NSHF_IOB 4
#else
#define NSHF_IOB 3
#endif
struct shf shf_iob[NSHF_IOB];
void
initio(void)
@ -1250,8 +1256,22 @@ initio(void)
/* force buffer allocation */
shf_fdopen(1, SHF_WR, shl_stdout);
shf_fdopen(2, SHF_WR, shl_out);
/* force buffer allocation */
shf_fdopen(2, SHF_WR, shl_spare);
#ifdef DF
if ((shl_dbg_fd = open("/tmp/mksh-dbg.txt",
O_WRONLY | O_APPEND | O_CREAT, 0600)) == -1)
errorf("cannot open debug output file");
if (shl_dbg_fd < FDBASE) {
int nfd;
nfd = fcntl(shl_dbg_fd, F_DUPFD, FDBASE);
close(shl_dbg_fd);
if ((shl_dbg_fd = nfd) == -1)
errorf("cannot dup debug output file");
}
shf_fdopen(shl_dbg_fd, SHF_WR, shl_dbg);
DF("=== open ===");
#endif
initio_done = true;
}
@ -1694,3 +1714,23 @@ x_sigwinch(int sig MKSH_A_UNUSED)
got_winch = 1;
}
#endif
#ifdef DF
void
DF(const char *fmt, ...)
{
va_list args;
struct timeval tv;
(void)flock(shl_dbg_fd, LOCK_EX);
gettimeofday(&tv, NULL);
shf_fprintf(shl_dbg, "[%d.%06d:%d] ", (int)tv.tv_sec, (int)tv.tv_usec,
(int)getpid());
va_start(args, fmt);
shf_vfprintf(shl_dbg, fmt, args);
va_end(args);
shf_putc('\n', shl_dbg);
shf_flush(shl_dbg);
(void)flock(shl_dbg_fd, LOCK_UN);
}
#endif

9
sh.h
View File

@ -152,7 +152,7 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.522 2012/03/03 21:30:57 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.523 2012/03/23 19:38:12 tg Exp $");
#endif
#define MKSH_VERSION "R40 2012/03/03"
@ -697,6 +697,9 @@ struct temp {
#define shl_spare (&shf_iob[0]) /* for c_read()/c_print() */
#define shl_stdout (&shf_iob[1])
#define shl_out (&shf_iob[2])
#ifdef DF
#define shl_dbg (&shf_iob[3]) /* for DF() */
#endif
EXTERN bool shl_stdout_ok;
/*
@ -1744,6 +1747,10 @@ struct tbl *ktenter(struct table *, const char *, uint32_t);
void ktwalk(struct tstate *, struct table *);
struct tbl *ktnext(struct tstate *);
struct tbl **ktsort(struct table *);
#ifdef DF
void DF(const char *, ...)
MKSH_A_FORMAT(__printf__, 1, 2);
#endif
/* misc.c */
void setctypes(const char *, int);
void initctypes(void);