Home | History | Annotate | Line # | Download | only in linux
printk.h revision 1.1
      1  1.1  riastrad /*	$NetBSD: printk.h,v 1.1 2018/08/27 06:06:10 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #ifndef _LINUX_PRINTK_H_
     33  1.1  riastrad #define _LINUX_PRINTK_H_
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/param.h>
     36  1.1  riastrad #include <sys/systm.h>
     37  1.1  riastrad 
     38  1.1  riastrad #define	printk		printf
     39  1.1  riastrad #define	vprintk		vprintf
     40  1.1  riastrad #define	printk_once	printf
     41  1.1  riastrad #define	pr_err		printf	/* XXX */
     42  1.1  riastrad #define	pr_cont		printf	/* XXX */
     43  1.1  riastrad #define	pr_info		printf	/* XXX */
     44  1.1  riastrad #define	pr_info_once	printf	/* XXX */
     45  1.1  riastrad #define	pr_warn_once	printf	/* XXX */
     46  1.1  riastrad #define	KERN_DEBUG	"drm kern debug: "
     47  1.1  riastrad #define	KERN_INFO	"drm kern info: "
     48  1.1  riastrad #define	KERN_WARNING	"drm kern warning: "
     49  1.1  riastrad #define	KERN_ERR	"drm kern error: "
     50  1.1  riastrad #define	KERN_CRIT	"drm kern crit: "
     51  1.1  riastrad #define	KERN_CONT	""
     52  1.1  riastrad 
     53  1.1  riastrad #define	DUMP_PREFIX_NONE	0
     54  1.1  riastrad #define	DUMP_PREFIX_OFFSET	1
     55  1.1  riastrad #define	DUMP_PREFIX_ADDRESS	2
     56  1.1  riastrad 
     57  1.1  riastrad static inline void
     58  1.1  riastrad hex_dump_to_buffer(const void *buf, size_t buf_size, int bytes_per_line,
     59  1.1  riastrad     int bytes_per_group, char *output, size_t output_size, bool ascii __unused)
     60  1.1  riastrad {
     61  1.1  riastrad 	const uint8_t *bytes = buf;
     62  1.1  riastrad 	size_t i = 0, n;
     63  1.1  riastrad 
     64  1.1  riastrad 	KASSERT(output_size >= 1);
     65  1.1  riastrad 	KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
     66  1.1  riastrad 	KASSERT(powerof2(bytes_per_group));
     67  1.1  riastrad 	KASSERT(0 < bytes_per_group);
     68  1.1  riastrad 	KASSERT(bytes_per_group <= 8);
     69  1.1  riastrad 
     70  1.1  riastrad 	output[output_size - 1] = '\0';
     71  1.1  riastrad 	while (i < buf_size) {
     72  1.1  riastrad 		n = snprintf(output, output_size, "%02x", bytes[i++]);
     73  1.1  riastrad 		if (n >= output_size)
     74  1.1  riastrad 			break;
     75  1.1  riastrad 		output += n; output_size -= n;
     76  1.1  riastrad 		if ((i == buf_size) || (0 == (i % bytes_per_line)))
     77  1.1  riastrad 			n = snprintf(output, output_size, "\n");
     78  1.1  riastrad 		else if ((0 < i) && (0 == (i % bytes_per_group)))
     79  1.1  riastrad 			n = snprintf(output, output_size, " ");
     80  1.1  riastrad 		else
     81  1.1  riastrad 			n = 0;
     82  1.1  riastrad 		if (n >= output_size)
     83  1.1  riastrad 			break;
     84  1.1  riastrad 		output += n; output_size -= n;
     85  1.1  riastrad 	}
     86  1.1  riastrad }
     87  1.1  riastrad 
     88  1.1  riastrad static inline void
     89  1.1  riastrad print_hex_dump(const char *level, const char *prefix, int prefix_type,
     90  1.1  riastrad     int bytes_per_line, int bytes_per_group, const void *buf, size_t buf_size,
     91  1.1  riastrad     bool ascii)
     92  1.1  riastrad {
     93  1.1  riastrad 	const uint8_t *bytes = buf;
     94  1.1  riastrad 	/* Two digits and one space/newline per byte, plus a null.  */
     95  1.1  riastrad 	char line[32*3 + 1];
     96  1.1  riastrad 
     97  1.1  riastrad 	KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
     98  1.1  riastrad 	KASSERT(powerof2(bytes_per_group));
     99  1.1  riastrad 	KASSERT(0 < bytes_per_group);
    100  1.1  riastrad 	KASSERT(bytes_per_group <= 8);
    101  1.1  riastrad 
    102  1.1  riastrad 	while (0 < buf_size) {
    103  1.1  riastrad 		const size_t n = MIN(buf_size, 32);
    104  1.1  riastrad 
    105  1.1  riastrad 		switch (prefix_type) {
    106  1.1  riastrad 		case DUMP_PREFIX_OFFSET:
    107  1.1  riastrad 			printf("%08zu: ", (bytes - (const uint8_t *)buf));
    108  1.1  riastrad 			break;
    109  1.1  riastrad 
    110  1.1  riastrad 		case DUMP_PREFIX_ADDRESS:
    111  1.1  riastrad 			printf("%p: ", bytes);
    112  1.1  riastrad 			break;
    113  1.1  riastrad 
    114  1.1  riastrad 		case DUMP_PREFIX_NONE:
    115  1.1  riastrad 		default:
    116  1.1  riastrad 			break;
    117  1.1  riastrad 		}
    118  1.1  riastrad 
    119  1.1  riastrad 		hex_dump_to_buffer(bytes, n, bytes_per_line, bytes_per_group,
    120  1.1  riastrad 		    line, sizeof(line), ascii);
    121  1.1  riastrad 		KASSERT(0 < strnlen(line, sizeof(line)));
    122  1.1  riastrad 		KASSERT(line[strnlen(line, sizeof(line)) - 1] == '\n');
    123  1.1  riastrad 		printf("%s", line);
    124  1.1  riastrad 
    125  1.1  riastrad 		bytes += n;
    126  1.1  riastrad 		buf_size -= n;
    127  1.1  riastrad 	}
    128  1.1  riastrad }
    129  1.1  riastrad 
    130  1.1  riastrad #endif  /* _LINUX_PRINTK_H_ */
    131