printk.h revision 1.7 1 1.7 riastrad /* $NetBSD: printk.h,v 1.7 2021/12/19 00:48:02 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.2 riastrad #define pr_notice printf /* XXX */
47 1.3 riastrad #define pr_debug printf /* XXX */
48 1.6 skrll #define KERN_EMERG "kern emerg: "
49 1.6 skrll #define KERN_ALERT "kern alert: "
50 1.5 skrll #define KERN_CRIT "kern crit: "
51 1.5 skrll #define KERN_ERR "kern error: "
52 1.5 skrll #define KERN_WARNING "kern warning: "
53 1.6 skrll #define KERN_NOTICE "kern notice: "
54 1.5 skrll #define KERN_INFO "kern info: "
55 1.4 skrll #define KERN_DEBUG "kern debug: "
56 1.1 riastrad #define KERN_CONT ""
57 1.1 riastrad
58 1.7 riastrad struct va_format {
59 1.7 riastrad const char *fmt;
60 1.7 riastrad va_list *va;
61 1.7 riastrad };
62 1.7 riastrad
63 1.1 riastrad #define DUMP_PREFIX_NONE 0
64 1.1 riastrad #define DUMP_PREFIX_OFFSET 1
65 1.1 riastrad #define DUMP_PREFIX_ADDRESS 2
66 1.1 riastrad
67 1.1 riastrad static inline void
68 1.1 riastrad hex_dump_to_buffer(const void *buf, size_t buf_size, int bytes_per_line,
69 1.1 riastrad int bytes_per_group, char *output, size_t output_size, bool ascii __unused)
70 1.1 riastrad {
71 1.1 riastrad const uint8_t *bytes = buf;
72 1.1 riastrad size_t i = 0, n;
73 1.1 riastrad
74 1.1 riastrad KASSERT(output_size >= 1);
75 1.1 riastrad KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
76 1.1 riastrad KASSERT(powerof2(bytes_per_group));
77 1.1 riastrad KASSERT(0 < bytes_per_group);
78 1.1 riastrad KASSERT(bytes_per_group <= 8);
79 1.1 riastrad
80 1.1 riastrad output[output_size - 1] = '\0';
81 1.1 riastrad while (i < buf_size) {
82 1.1 riastrad n = snprintf(output, output_size, "%02x", bytes[i++]);
83 1.1 riastrad if (n >= output_size)
84 1.1 riastrad break;
85 1.1 riastrad output += n; output_size -= n;
86 1.1 riastrad if ((i == buf_size) || (0 == (i % bytes_per_line)))
87 1.1 riastrad n = snprintf(output, output_size, "\n");
88 1.1 riastrad else if ((0 < i) && (0 == (i % bytes_per_group)))
89 1.1 riastrad n = snprintf(output, output_size, " ");
90 1.1 riastrad else
91 1.1 riastrad n = 0;
92 1.1 riastrad if (n >= output_size)
93 1.1 riastrad break;
94 1.1 riastrad output += n; output_size -= n;
95 1.1 riastrad }
96 1.1 riastrad }
97 1.1 riastrad
98 1.1 riastrad static inline void
99 1.1 riastrad print_hex_dump(const char *level, const char *prefix, int prefix_type,
100 1.1 riastrad int bytes_per_line, int bytes_per_group, const void *buf, size_t buf_size,
101 1.1 riastrad bool ascii)
102 1.1 riastrad {
103 1.1 riastrad const uint8_t *bytes = buf;
104 1.1 riastrad /* Two digits and one space/newline per byte, plus a null. */
105 1.1 riastrad char line[32*3 + 1];
106 1.1 riastrad
107 1.1 riastrad KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
108 1.1 riastrad KASSERT(powerof2(bytes_per_group));
109 1.1 riastrad KASSERT(0 < bytes_per_group);
110 1.1 riastrad KASSERT(bytes_per_group <= 8);
111 1.1 riastrad
112 1.1 riastrad while (0 < buf_size) {
113 1.1 riastrad const size_t n = MIN(buf_size, 32);
114 1.1 riastrad
115 1.1 riastrad switch (prefix_type) {
116 1.1 riastrad case DUMP_PREFIX_OFFSET:
117 1.1 riastrad printf("%08zu: ", (bytes - (const uint8_t *)buf));
118 1.1 riastrad break;
119 1.1 riastrad
120 1.1 riastrad case DUMP_PREFIX_ADDRESS:
121 1.1 riastrad printf("%p: ", bytes);
122 1.1 riastrad break;
123 1.1 riastrad
124 1.1 riastrad case DUMP_PREFIX_NONE:
125 1.1 riastrad default:
126 1.1 riastrad break;
127 1.1 riastrad }
128 1.1 riastrad
129 1.1 riastrad hex_dump_to_buffer(bytes, n, bytes_per_line, bytes_per_group,
130 1.1 riastrad line, sizeof(line), ascii);
131 1.1 riastrad KASSERT(0 < strnlen(line, sizeof(line)));
132 1.1 riastrad KASSERT(line[strnlen(line, sizeof(line)) - 1] == '\n');
133 1.1 riastrad printf("%s", line);
134 1.1 riastrad
135 1.1 riastrad bytes += n;
136 1.1 riastrad buf_size -= n;
137 1.1 riastrad }
138 1.1 riastrad }
139 1.1 riastrad
140 1.1 riastrad #endif /* _LINUX_PRINTK_H_ */
141