xref: /freebsd/sys/gdb/gdb_cons.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Sam Leffler
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Support for redirecting console msgs to gdb.  We register
31  * a pseudo console to hook cnputc and send stuff to the gdb
32  * port.  The only trickiness here is buffering output so this
33  * isn't dog slow.
34  */
35 
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/cons.h>
40 #include <sys/kdb.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/reboot.h>
44 #include <sys/sysctl.h>
45 
46 #include <machine/gdb_machdep.h>
47 #include <machine/kdb.h>
48 
49 #include <gdb/gdb.h>
50 #include <gdb/gdb_int.h>
51 
52 struct gdbcons {
53 	int	npending;
54 	/* /2 for hex conversion, -6 for protocol glue */
55 	char	buf[GDB_BUFSZ/2 - 6];
56 	struct callout flush;
57 };
58 static struct gdbcons state = { -1 };
59 
60 static	int gdbcons_enable = 0;
61 SYSCTL_INT(_debug_gdb, OID_AUTO, cons, CTLFLAG_RWTUN, &gdbcons_enable, 0,
62 	"copy console messages to GDB");
63 /* Legacy sysctl alias */
64 SYSCTL_INT(_debug, OID_AUTO, gdbcons, CTLFLAG_RWTUN, &gdbcons_enable,
65 	0, "copy console messages to GDB");
66 
67 static void
68 gdb_cnprobe(struct consdev *cp)
69 {
70 	sprintf(cp->cn_name, "gdb");
71 	cp->cn_pri = CN_LOW;		/* XXX no way to say "write only" */
72 }
73 
74 static void
75 gdb_cninit(struct consdev *cp)
76 {
77 	struct gdbcons *c = &state;
78 
79 	/* setup tx buffer and callout */
80 	if (c->npending == -1) {
81 		c->npending = 0;
82 		callout_init(&c->flush, 1);
83 		cp->cn_arg = c;
84 	}
85 }
86 
87 static void
88 gdb_cnterm(struct consdev *cp)
89 {
90 }
91 
92 static void
93 gdb_cngrab(struct consdev *cp)
94 {
95 }
96 
97 static void
98 gdb_cnungrab(struct consdev *cp)
99 {
100 }
101 
102 static int
103 gdb_cngetc(struct consdev *cp)
104 {
105 	return -1;
106 }
107 
108 static void
109 gdb_tx_puthex(int c)
110 {
111 	const char *hex = "0123456789abcdef";
112 
113 	gdb_tx_char(hex[(c>>4)&0xf]);
114 	gdb_tx_char(hex[(c>>0)&0xf]);
115 }
116 
117 static void
118 gdb_cnflush(void *arg)
119 {
120 	struct gdbcons *gc = arg;
121 	int i;
122 
123 	gdb_tx_begin('O');
124 	for (i = 0; i < gc->npending; i++)
125 		gdb_tx_puthex(gc->buf[i]);
126 	gdb_tx_end();
127 	gc->npending = 0;
128 }
129 
130 /*
131  * This glop is to figure out when it's safe to use callouts
132  * to defer buffer flushing.  There's probably a better way
133  * and/or an earlier point in the boot process when it's ok.
134  */
135 static int calloutok = 0;
136 static void
137 oktousecallout(void *data __unused)
138 {
139 	calloutok = 1;
140 }
141 SYSINIT(gdbhack, SI_SUB_LAST, SI_ORDER_MIDDLE, oktousecallout, NULL);
142 
143 static void
144 gdb_cnputc(struct consdev *cp, int c)
145 {
146 	struct gdbcons *gc;
147 
148 	if (gdbcons_enable && gdb_cur != NULL && gdb_listening) {
149 		gc = cp->cn_arg;
150 		if (gc->npending != 0) {
151 			/*
152 			 * Cancel any pending callout and flush the
153 			 * buffer if there's no space for this byte.
154 			 */
155 			if (calloutok)
156 				callout_stop(&gc->flush);
157 			if (gc->npending == sizeof(gc->buf))
158 				gdb_cnflush(gc);
159 		}
160 		gc->buf[gc->npending++] = c;
161 		/*
162 		 * Flush on end of line; this is especially helpful
163 		 * during boot when we don't have callouts to flush
164 		 * the buffer.  Otherwise we defer flushing; a 1/4
165 		 * second is a guess.
166 		 */
167 		if (c == '\n')
168 			gdb_cnflush(gc);
169 		else if (calloutok)
170 			callout_reset(&gc->flush, hz/4, gdb_cnflush, gc);
171 	}
172 }
173 
174 CONSOLE_DRIVER(gdb);
175 
176 /*
177  * Our console device only gets attached if the system is booted
178  * with RB_MULTIPLE set so gdb_init also calls us to attach the
179  * console so we're setup regardless.
180  */
181 void
182 gdb_consinit(void)
183 {
184 	gdb_cnprobe(&gdb_consdev);
185 	gdb_cninit(&gdb_consdev);
186 	cnadd(&gdb_consdev);
187 }
188