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