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