X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/9bccf70c0258c7cac2dcb80011b2a964d884c552..c910b4d9d2451126ae3917b931cd4390c11e1d52:/osfmk/kern/printf.c?ds=sidebyside diff --git a/osfmk/kern/printf.c b/osfmk/kern/printf.c index 472ab86c2..f8376b419 100644 --- a/osfmk/kern/printf.c +++ b/osfmk/kern/printf.c @@ -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@ */ /* * @OSF_COPYRIGHT@ @@ -150,9 +156,11 @@ * output bases such as x, X, u, U, o, and O also work. */ +#include +#include +#include #include #include -#include #include #include #include @@ -164,6 +172,7 @@ #ifdef MACH_BSD #include #endif +#include #ifdef __ppc__ #include @@ -175,15 +184,20 @@ #define MAXBUF (sizeof(long long int) * 8) /* enough for binary */ static char digs[] = "0123456789abcdef"; + +#if CONFIG_NO_PRINTF_STRINGS +#undef printf(x, ...) +#endif + static int printnum( - register unsigned long long int u, /* number to print */ - register int base, + unsigned long long int u, /* number to print */ + int base, void (*putc)(int, void *), void *arg) { char buf[MAXBUF]; /* build number here */ - register char * p = &buf[MAXBUF-1]; + char * p = &buf[MAXBUF-1]; int nprinted = 0; do { @@ -203,7 +217,7 @@ boolean_t _doprnt_truncates = FALSE; int __doprnt( - register const char *fmt, + const char *fmt, va_list *argp, /* character output routine */ void (*putc)(int, void *arg), @@ -220,7 +234,7 @@ __doprnt( int sign_char; boolean_t altfmt, truncate; int base; - register char c; + char c; int capitals; int long_long; int nprinted = 0; @@ -392,15 +406,15 @@ __doprnt( case 's': { - register char *p; - register char *p2; + register const char *p; + register const char *p2; if (prec == -1) prec = 0x7fffffff; /* MAXINT */ p = va_arg(*argp, char *); - if (p == (char *)0) + if (p == NULL) p = ""; if (length > 0 && !ladjust) { @@ -421,12 +435,13 @@ __doprnt( n = 0; - while (*p != '\0') { - if (++n > prec || (length > 0 && n > length)) - break; - - (*putc)(*p++, arg); - nprinted++; + while ((n < prec) && (!(length > 0 && n >= length))) { + if (*p == '\0') { + break; + } + (*putc)(*p++, arg); + nprinted++; + n++; } if (n < length && ladjust) { @@ -543,7 +558,7 @@ __doprnt( char buf[MAXBUF]; /* build number here */ register char * p = &buf[MAXBUF-1]; static char digits[] = "0123456789abcdef0123456789ABCDEF"; - char *prefix = 0; + const char *prefix = NULL; if (truncate) u = (long long)((int)(u)); @@ -564,7 +579,7 @@ __doprnt( if (sign_char) length--; if (prefix) - length -= strlen((const char *) prefix); + length -= strlen(prefix); if (padc == ' ' && !ladjust) { /* blank padding goes before prefix */ @@ -643,7 +658,10 @@ boolean_t new_printf_cpu_number = FALSE; decl_simple_lock_data(,printf_lock) -decl_mutex_data(,sprintf_lock) +decl_simple_lock_data(,bsd_log_spinlock) +extern void bsd_log_init(void); +void bsd_log_lock(void); +void bsd_log_unlock(void); void printf_init(void) @@ -651,8 +669,21 @@ printf_init(void) /* * Lock is only really needed after the first thread is created. */ - simple_lock_init(&printf_lock, ETAP_MISC_PRINTF); - mutex_init(&sprintf_lock, ETAP_MISC_PRINTF); + simple_lock_init(&printf_lock, 0); + simple_lock_init(&bsd_log_spinlock, 0); + bsd_log_init(); +} + +void +bsd_log_lock(void) +{ + simple_lock(&bsd_log_spinlock); +} + +void +bsd_log_unlock(void) +{ + simple_unlock(&bsd_log_spinlock); } /* derived from boot_gets */ @@ -704,45 +735,75 @@ safe_gets( } } +extern int disableConsoleOutput; + void conslog_putc( char c) { - extern unsigned int debug_mode, disableDebugOuput, disableConsoleOutput; - - if ((debug_mode && !disableDebugOuput) || !disableConsoleOutput) + if ((debug_mode && !disable_debug_output) || !disableConsoleOutput) cnputc(c); #ifdef MACH_BSD - log_putc(c); + if (debug_mode == 0) + log_putc(c); #endif } +#if MACH_KDB +extern void db_putchar(char c); +#endif + void -printf(const char *fmt, ...) +dbugprintf(__unused const char *fmt, ...) { + +#if MACH_KDB va_list listp; - disable_preemption(); va_start(listp, fmt); - _doprnt(fmt, &listp, conslog_putc, 16); + _doprnt(fmt, &listp, db_putchar, 16); va_end(listp); - enable_preemption(); +#endif + return; } -void -consdebug_putc( - char c) +int +printf(const char *fmt, ...) { - extern unsigned int debug_mode, disableDebugOuput, disableConsoleOutput; + va_list listp; + + if (fmt) { + disable_preemption(); + va_start(listp, fmt); + _doprnt(fmt, &listp, conslog_putc, 16); + va_end(listp); + enable_preemption(); + } + return 0; +} - if ((debug_mode && !disableDebugOuput) || !disableConsoleOutput) +void +consdebug_putc(char c) +{ + if ((debug_mode && !disable_debug_output) || !disableConsoleOutput) cnputc(c); debug_putc(c); + + if (!console_is_serial()) + if (!disable_serial_output) + PE_kputc(c); } + void +consdebug_log(char c) +{ + debug_putc(c); +} + +int kdb_printf(const char *fmt, ...) { va_list listp; @@ -750,28 +811,48 @@ kdb_printf(const char *fmt, ...) va_start(listp, fmt); _doprnt(fmt, &listp, consdebug_putc, 16); va_end(listp); + return 0; } -static char *copybyte_str; +int +kdb_log(const char *fmt, ...) +{ + va_list listp; + + va_start(listp, fmt); + _doprnt(fmt, &listp, consdebug_log, 16); + va_end(listp); + return 0; +} static void -copybyte( - char byte) +copybyte(int c, void *arg) { - *copybyte_str++ = byte; - *copybyte_str = '\0'; + /* + * arg is a pointer (outside pointer) to the pointer + * (inside pointer) which points to the character. + * We pass a double pointer, so that we can increment + * the inside pointer. + */ + char** p = arg; /* cast outside pointer */ + **p = c; /* store character */ + (*p)++; /* increment inside pointer */ } +/* + * Deprecation Warning: + * sprintf() is being deprecated. Please use snprintf() instead. + */ int sprintf(char *buf, const char *fmt, ...) { va_list listp; + char *copybyte_str; va_start(listp, fmt); - mutex_lock(&sprintf_lock); copybyte_str = buf; - _doprnt(fmt, &listp, copybyte, 16); - mutex_unlock(&sprintf_lock); + __doprnt(fmt, &listp, copybyte, ©byte_str, 16); va_end(listp); + *copybyte_str = '\0'; return strlen(buf); }