]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/kern/subr_prf.c
xnu-4570.20.62.tar.gz
[apple/xnu.git] / bsd / kern / subr_prf.c
index 68867e8ea990231001fc4ba28d9960bfebb44771..840fd1e7576f6fd2406e88abd06de162256021a3 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
 /*-
 
 #include <sys/param.h>
 #include <sys/systm.h>
-#include <sys/buf.h>
 #include <sys/conf.h>
 #include <sys/reboot.h>
 #include <sys/msgbuf.h>
-#include <sys/proc.h>
+#include <sys/proc_internal.h>
 #include <sys/ioctl.h>
 #include <sys/tty.h>
-#include <sys/file.h>
+#include <sys/file_internal.h>
 #include <sys/tprintf.h>
 #include <sys/syslog.h>
 #include <stdarg.h>
 #include <sys/subr_prf.h>
 
 #include <kern/cpu_number.h>   /* for cpu_number() */
-#include <machine/spl.h>
 #include <libkern/libkern.h>
+#include <os/log_private.h>
+
+/* for vaddlog(): the following are implemented in osfmk/kern/printf.c  */
+extern void bsd_log_lock(void);
+extern void bsd_log_unlock(void);
+
+/* Keep this around only because it's exported */
+void _printf(int, struct tty *, const char *, ...);
 
 struct snprintf_arg {
        char *str;
@@ -104,39 +116,29 @@ struct snprintf_arg {
 
 /*
  * In case console is off,
- * panicstr contains argument to last
+ * debugger_panic_str contains argument to last
  * call to panic.
  */
-extern const char      *panicstr;
+extern const char      *debugger_panic_str;
 
-extern cnputc();                       /* standard console putc */
-int    (*v_putc)() = cnputc;           /* routine to putc on virtual console */
+extern void cnputc(char);              /* standard console putc */
+void   (*v_putc)(char) = cnputc;       /* routine to putc on virtual console */
 
 extern struct tty cons;                /* standard console tty */
 extern struct  tty *constty;           /* pointer to console "window" tty */
 extern int  __doprnt(const char *fmt,
-                                        va_list    *argp,
-                                        void       (*putc)(int, void *arg),
+                                        va_list    argp,
+                                        void       (*)(int, void *),
                                         void       *arg,
-                                        int        radix);
+                                        int        radix,
+                                        int        is_log);
 
 /*
  *     Record cpu that panic'd and lock around panic data
  */
 
-static void puts(const char *s, int flags, struct tty *ttyp);
-static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size);
-
-/* MP printf stuff */
-decl_simple_lock_data(,printf_lock)
-#if    NCPUS > 1
-boolean_t new_printf_cpu_number;  /* do we need to output who we are */
-#endif
-
-extern void logwakeup();
-extern void halt_cpu();
-extern boot();
-
+extern void logwakeup(struct msgbuf *);
+extern void halt_cpu(void);
 
 static void
 snprintf_func(int ch, void *arg);
@@ -156,72 +158,100 @@ static void putchar(int c, void *arg);
 void
 uprintf(const char *fmt, ...)
 {
-       register struct proc *p = current_proc();
+       struct proc *p = current_proc();
        struct putchar_args pca;
        va_list ap;
+       struct session *sessp;
        
-       pca.flags = TOTTY;
-       pca.tty   = (struct tty *)p->p_session->s_ttyp;
+       sessp = proc_session(p);
 
-       if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
+       if (p->p_flag & P_CONTROLT && sessp != SESSION_NULL && sessp->s_ttyvp) {
+               pca.flags = TOTTY;
+               pca.tty   = SESSION_TP(sessp);
+               if (pca.tty != NULL)
+                       tty_lock(pca.tty);
                va_start(ap, fmt);
-               __doprnt(fmt, &ap, putchar, &pca, 10);
+               __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
                va_end(ap);
+               if (pca.tty != NULL)
+               tty_unlock(pca.tty);
        }
+       if (sessp != SESSION_NULL)
+               session_rele(sessp);
 }
 
 tpr_t
-tprintf_open(p)
-       register struct proc *p;
+tprintf_open(struct proc *p)
 {
-       if (p->p_flag & P_CONTROLT && p->p_session->s_ttyvp) {
-               SESSHOLD(p->p_session);
-               return ((tpr_t) p->p_session);
+       struct session * sessp;
+
+       sessp = proc_session(p);
+
+       if (p->p_flag & P_CONTROLT && sessp->s_ttyvp) {
+               return ((tpr_t)sessp);
        }
+       if (sessp != SESSION_NULL)
+               session_rele(sessp);
+
        return ((tpr_t) NULL);
 }
 
 void
-tprintf_close(sess)
-       tpr_t sess;
+tprintf_close(tpr_t sessp)
 {
-       if (sess)
-               SESSRELE((struct session *) sess);
+       if (sessp)
+               session_rele((struct session *) sessp);
 }
 
 /*
  * tprintf prints on the controlling terminal associated
  * with the given session.
+ *
+ * NOTE:       No one else should call this function!!!
  */
 void
 tprintf(tpr_t tpr, const char *fmt, ...)
 {
-       register struct session *sess = (struct session *)tpr;
-       struct tty *tp = NULL;
-       int flags = TOLOG;
+       struct session *sess = (struct session *)tpr;
+       struct tty *tp;
        va_list ap;
        struct putchar_args pca;
 
-       logpri(LOG_INFO);
-       if (sess && sess->s_ttyvp && ttycheckoutq(sess->s_ttyp, 0)) {
-               flags |= TOTTY;
-               tp = sess->s_ttyp;
+       if (sess && (tp = SESSION_TP(sess)) != TTY_NULL) {
+               /* ttycheckoutq(), tputchar() require a locked tp */
+               tty_lock(tp);
+               if(ttycheckoutq(tp, 0)) {
+                       pca.flags = TOTTY;
+                       /* going to the tty; leave locked */
+                       pca.tty = tp;
+                       va_start(ap, fmt);
+                       __doprnt(fmt, ap, putchar, &pca, 10, FALSE);
+                       va_end(ap);
+               }
+               tty_unlock(tp);
        }
-       if (tp != NULL) {
-               pca.flags = TOTTY;
-               pca.tty   = tp;
-               
-               va_start(ap, fmt);
-               __doprnt(fmt, &ap, putchar, &pca, 10);
-               va_end(ap);
-       }
-       logwakeup();
+
+       pca.flags = TOLOG;
+       pca.tty   = TTY_NULL;
+       va_start(ap, fmt);
+       __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
+       va_end(ap);
+
+       logwakeup(msgbufp);
+
+       va_start(ap, fmt);
+       os_log_with_args(OS_LOG_DEFAULT, OS_LOG_TYPE_DEFAULT, fmt, ap, __builtin_return_address(0));
+       va_end(ap);
 }
 
 /*
  * Ttyprintf displays a message on a tty; it should be used only by
  * the tty driver, or anything that knows the underlying tty will not
  * be revoke(2)'d away.  Other callers should use tprintf.
+ *
+ * Locks:      It is assumed that the tty_lock() is held over the call
+ *             to this function.  Ensuring this is the responsibility
+ *             of the caller.
  */
 void
 ttyprintf(struct tty *tp, const char *fmt, ...)
@@ -234,146 +264,79 @@ ttyprintf(struct tty *tp, const char *fmt, ...)
                pca.tty   = tp;
                
                va_start(ap, fmt);
-               __doprnt(fmt, &ap, putchar, &pca, 10);
+               __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
                va_end(ap);
        }
 }
 
-extern int log_open;
-
-
 void
-logpri(level)
-       int level;
+logtime(time_t secs)
 {
-       struct putchar_args pca;
-       pca.flags = TOLOG;
-       pca.tty   = NULL;
-       
-       putchar('<', &pca);
-       printn((u_long)level, 10, TOLOG, (struct tty *)0, 0, 0);
-       putchar('>', &pca);
+       printf("Time %ld Message ", secs);
 }
 
-void
-addlog(const char *fmt, ...)
+static void
+putchar_asl(int c, void *arg)
 {
-       register s = splhigh();
-       va_list ap;
-       struct putchar_args pca;
-
-       pca.flags = TOLOG;
-       pca.tty   = NULL;
+       struct putchar_args *pca = arg;
 
-       va_start(ap, fmt);
-       __doprnt(fmt, &ap, putchar, &pca, 10);
-       
-       splx(s);
-       if (!log_open) {
-               pca.flags = TOCONS;
-               __doprnt(fmt, &ap, putchar, &pca, 10);
-       }
-       va_end(ap);
-       logwakeup();
+       if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177)
+               log_putc_locked(aslbufp, c);
+       putchar(c, arg);
 }
-void _printf(int flags, struct tty *ttyp, const char *format, ...)
+
+/*
+ * Vestigial support for kern_asl_msg() via /dev/klog
+ */
+int
+vaddlog(const char *fmt, va_list ap)
 {
-       va_list ap;
-       struct putchar_args pca;
+       struct putchar_args pca = {
+               .flags = TOLOGLOCKED,
+               .tty = NULL,
+       };
 
-       pca.flags = flags;
-       pca.tty   = ttyp;
-       
-       va_start(ap, format);
-       __doprnt(format, &ap, putchar, &pca, 10);
-       va_end(ap);
+       bsd_log_lock();
+       __doprnt(fmt, ap, putchar_asl, &pca, 10, TRUE);
+       bsd_log_unlock();
+       logwakeup(NULL);
+
+       return (0);
 }
 
-int prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
+void
+_printf(int flags, struct tty *ttyp, const char *format, ...)
 {
+       va_list ap;
        struct putchar_args pca;
 
        pca.flags = flags;
        pca.tty   = ttyp;
 
-#if    NCPUS > 1
-    int cpun = cpu_number();
-
-    if(ttyp == 0) {
-           simple_lock(&printf_lock);
-       } else
-               TTY_LOCK(ttyp);
-
-       if (cpun != master_cpu)
-           new_printf_cpu_number = TRUE;
+       if (ttyp != NULL) {
+               tty_lock(ttyp);
+       
+               va_start(ap, format);
+               __doprnt(format, ap, putchar, &pca, 10, TRUE);
+               va_end(ap);
 
-       if (new_printf_cpu_number) {
-               putchar('{', flags, ttyp);
-               printn((u_long)cpun, 10, flags, ttyp, 0, 0);
-               putchar('}', flags, ttyp);
+               tty_unlock(ttyp);
        }
-#endif /* NCPUS > 1 */
-         
-       __doprnt(fmt, &ap, putchar, &pca, 10);
-
-#if    NCPUS > 1
-       if(ttyp == 0) {
-               simple_unlock(&printf_lock);
-       } else
-               TTY_UNLOCK(ttyp);
-#endif
-       return 0;
 }
 
-static void puts(const char *s, int flags, struct tty *ttyp)
+int
+prf(const char *fmt, va_list ap, int flags, struct tty *ttyp)
 {
-       register char c;
        struct putchar_args pca;
 
        pca.flags = flags;
        pca.tty   = ttyp;
 
-       while ((c = *s++))
-               putchar(c, &pca);
-}
-
-/*
- * Printn prints a number n in base b.
- * We don't use recursion to avoid deep kernel stacks.
- */
-static void printn(u_long n, int b, int flags, struct tty *ttyp, int zf, int fld_size)
-{
-       char prbuf[11];
-       register char *cp;
-       struct putchar_args pca;
+       __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
 
-       pca.flags = flags;
-       pca.tty   = ttyp;
-
-       if (b == 10 && (int)n < 0) {
-               putchar('-', &pca);
-               n = (unsigned)(-(int)n);
-       }
-       cp = prbuf;
-       do {
-               *cp++ = "0123456789abcdef"[n%b];
-               n /= b;
-       } while (n);
-       if (fld_size) {
-               for (fld_size -= cp - prbuf; fld_size > 0; fld_size--)
-                       if (zf)
-                               putchar('0', &pca);
-                       else
-                               putchar(' ', &pca);
-       }
-       do
-               putchar(*--cp, &pca);
-       while (cp > prbuf);
+       return 0;
 }
 
-
-
 /*
  * Warn that a system table is full.
  */
@@ -386,16 +349,18 @@ void tablefull(const char *tab)
  * Print a character on console or users terminal.
  * If destination is console then the last MSGBUFS characters
  * are saved in msgbuf for inspection later.
+ *
+ * Locks:      If TOTTY is set, we assume that the tty lock is held
+ *             over the call to this function.
  */
 /*ARGSUSED*/
 void
 putchar(int c, void *arg)
 {
        struct putchar_args *pca = arg;
-       register struct msgbuf *mbp;
        char **sp = (char**) pca->tty;
 
-       if (panicstr)
+       if (debugger_panic_str)
                constty = 0;
        if ((pca->flags & TOCONS) && pca->tty == NULL && constty) {
                pca->tty = constty;
@@ -406,6 +371,8 @@ putchar(int c, void *arg)
                constty = 0;
        if ((pca->flags & TOLOG) && c != '\0' && c != '\r' && c != 0177)
                log_putc(c);
+       if ((pca->flags & TOLOGLOCKED) && c != '\0' && c != '\r' && c != 0177)
+               log_putc_locked(msgbufp, c);
        if ((pca->flags & TOCONS) && constty == 0 && c != '\0')
                (*v_putc)(c);
        if (pca->flags & TOSTR) {
@@ -414,10 +381,24 @@ putchar(int c, void *arg)
        }
 }
 
+int
+vprintf_log_locked(const char *fmt, va_list ap)
+{
+       struct putchar_args pca;
 
+       pca.flags = TOLOGLOCKED;
+       pca.tty   = NULL;
+       __doprnt(fmt, ap, putchar, &pca, 10, TRUE);
+       return 0;
+}
+
+#if !CONFIG_EMBEDDED
 
 /*
  * Scaled down version of vsprintf(3).
+ *
+ * Deprecation Warning:
+ *     vsprintf() is being deprecated. Please use vsnprintf() instead. 
  */
 int
 vsprintf(char *buf, const char *cfmt, va_list ap)
@@ -428,12 +409,13 @@ vsprintf(char *buf, const char *cfmt, va_list ap)
        info.str = buf;
        info.remain = 999999;
 
-       retval = __doprnt(cfmt, &ap, snprintf_func, &info, 10);
+       retval = __doprnt(cfmt, ap, snprintf_func, &info, 10, FALSE);
        if (info.remain >= 1) {
                *info.str++ = '\0';
        }
        return 0;
 }
+#endif /* !CONFIG_EMBEDDED */
 
 /*
  * Scaled down version of snprintf(3).
@@ -461,7 +443,7 @@ vsnprintf(char *str, size_t size, const char *format, va_list ap)
 
        info.str = str;
        info.remain = size;
-       retval = __doprnt(format, &ap, snprintf_func, &info, 10);
+       retval = __doprnt(format, ap, snprintf_func, &info, 10, FALSE);
        if (info.remain >= 1)
                *info.str++ = '\0';
        return retval;
@@ -481,7 +463,7 @@ snprintf_func(int ch, void *arg)
 int
 kvprintf(char const *fmt, void (*func)(int, void*), void *arg, int radix, va_list ap)
 {
-       __doprnt(fmt, &ap, func, arg, radix);
+       __doprnt(fmt, ap, func, arg, radix, TRUE);
        return 0;
 }