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