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