/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2006 Sam Leffler4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,20* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728/*29* Support for redirecting console msgs to gdb. We register30* a pseudo console to hook cnputc and send stuff to the gdb31* port. The only trickiness here is buffering output so this32* isn't dog slow.33*/3435#include <sys/param.h>36#include <sys/systm.h>37#include <sys/cons.h>38#include <sys/kdb.h>39#include <sys/kernel.h>40#include <sys/malloc.h>41#include <sys/reboot.h>42#include <sys/sysctl.h>4344#include <machine/gdb_machdep.h>45#include <machine/kdb.h>4647#include <gdb/gdb.h>48#include <gdb/gdb_int.h>4950struct gdbcons {51int npending;52/* /2 for hex conversion, -6 for protocol glue */53char buf[GDB_BUFSZ/2 - 6];54struct callout flush;55};56static struct gdbcons state = { -1 };5758static int gdbcons_enable = 0;59SYSCTL_INT(_debug_gdb, OID_AUTO, cons, CTLFLAG_RWTUN, &gdbcons_enable, 0,60"copy console messages to GDB");61/* Legacy sysctl alias */62SYSCTL_INT(_debug, OID_AUTO, gdbcons, CTLFLAG_RWTUN, &gdbcons_enable,630, "copy console messages to GDB");6465static void66gdb_cnprobe(struct consdev *cp)67{68sprintf(cp->cn_name, "gdb");69cp->cn_pri = CN_LOW; /* XXX no way to say "write only" */70}7172static void73gdb_cninit(struct consdev *cp)74{75struct gdbcons *c = &state;7677/* setup tx buffer and callout */78if (c->npending == -1) {79c->npending = 0;80callout_init(&c->flush, 1);81cp->cn_arg = c;82}83}8485static void86gdb_cnterm(struct consdev *cp)87{88}8990static void91gdb_cngrab(struct consdev *cp)92{93}9495static void96gdb_cnungrab(struct consdev *cp)97{98}99100static int101gdb_cngetc(struct consdev *cp)102{103return -1;104}105106static void107gdb_tx_puthex(int c)108{109const char *hex = "0123456789abcdef";110111gdb_tx_char(hex[(c>>4)&0xf]);112gdb_tx_char(hex[(c>>0)&0xf]);113}114115static void116gdb_cnflush(void *arg)117{118struct gdbcons *gc = arg;119int i;120121gdb_tx_begin('O');122for (i = 0; i < gc->npending; i++)123gdb_tx_puthex(gc->buf[i]);124gdb_tx_end();125gc->npending = 0;126}127128/*129* This glop is to figure out when it's safe to use callouts130* to defer buffer flushing. There's probably a better way131* and/or an earlier point in the boot process when it's ok.132*/133static int calloutok = 0;134static void135oktousecallout(void *data __unused)136{137calloutok = 1;138}139SYSINIT(gdbhack, SI_SUB_LAST, SI_ORDER_MIDDLE, oktousecallout, NULL);140141static void142gdb_cnputc(struct consdev *cp, int c)143{144struct gdbcons *gc;145146if (gdbcons_enable && gdb_cur != NULL && gdb_listening) {147gc = cp->cn_arg;148if (gc->npending != 0) {149/*150* Cancel any pending callout and flush the151* buffer if there's no space for this byte.152*/153if (calloutok)154callout_stop(&gc->flush);155if (gc->npending == sizeof(gc->buf))156gdb_cnflush(gc);157}158gc->buf[gc->npending++] = c;159/*160* Flush on end of line; this is especially helpful161* during boot when we don't have callouts to flush162* the buffer. Otherwise we defer flushing; a 1/4163* second is a guess.164*/165if (c == '\n')166gdb_cnflush(gc);167else if (calloutok)168callout_reset(&gc->flush, hz/4, gdb_cnflush, gc);169}170}171172CONSOLE_DRIVER(gdb);173174/*175* Our console device only gets attached if the system is booted176* with RB_MULTIPLE set so gdb_init also calls us to attach the177* console so we're setup regardless.178*/179void180gdb_consinit(void)181{182gdb_cnprobe(&gdb_consdev);183gdb_cninit(&gdb_consdev);184cnadd(&gdb_consdev);185}186187188