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