1872b485imp/*-
2273d7fdpfg * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3273d7fdpfg *
46e0dccamarcel * Copyright (c) 2004 Marcel Moolenaar
56e0dccamarcel * All rights reserved.
66e0dccamarcel *
76e0dccamarcel * Redistribution and use in source and binary forms, with or without
86e0dccamarcel * modification, are permitted provided that the following conditions
96e0dccamarcel * are met:
106e0dccamarcel *
116e0dccamarcel * 1. Redistributions of source code must retain the above copyright
126e0dccamarcel *    notice, this list of conditions and the following disclaimer.
136e0dccamarcel * 2. Redistributions in binary form must reproduce the above copyright
146e0dccamarcel *    notice, this list of conditions and the following disclaimer in the
156e0dccamarcel *    documentation and/or other materials provided with the distribution.
166e0dccamarcel *
176e0dccamarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186e0dccamarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196e0dccamarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206e0dccamarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216e0dccamarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226e0dccamarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236e0dccamarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246e0dccamarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256e0dccamarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266e0dccamarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276e0dccamarcel *
286e0dccamarcel * $FreeBSD$
296e0dccamarcel */
306e0dccamarcel
316e0dccamarcel#ifndef _GDB_GDB_INT_H_
326e0dccamarcel#define	_GDB_GDB_INT_H_
336e0dccamarcel
3445bf92ccem#include "opt_ddb.h"
3545bf92ccem
361430355cem#include <sys/sysctl.h>
371430355cem
3845bf92ccem#ifdef DDB
3945bf92ccem#include <ddb/ddb.h>
4045bf92ccem#endif
4145bf92ccem
422eac876cem#ifndef EOF
432eac876cem#define EOF	(-1)
442eac876cem#endif
452eac876cem
461430355cemSYSCTL_DECL(_debug_gdb);
471430355cem
486e0dccamarcelextern struct gdb_dbgport *gdb_cur;
496e0dccamarcel
502b0d21fsamextern int gdb_listening;
512b0d21fsamvoid gdb_consinit(void);
522b0d21fsam
536e0dccamarcelextern char *gdb_rxp;
546e0dccamarcelextern size_t gdb_rxsz;
556e0dccamarcelextern char *gdb_txp;
566e0dccamarcel
57b6995dbcemextern bool gdb_ackmode;
58b6995dbcem
5945bf92ccem#ifdef DDB
6045bf92ccem/* If set, return to DDB when controlling GDB detaches. */
6145bf92ccemextern bool gdb_return_to_ddb;
6245bf92ccem#endif
6345bf92ccem
646e0dccamarcelint gdb_rx_begin(void);
656e0dccamarcelint gdb_rx_equal(const char *);
666e0dccamarcelint gdb_rx_mem(unsigned char *, size_t);
676e0dccamarcelint gdb_rx_varhex(uintmax_t *);
686e0dccamarcel
696e0dccamarcelstatic __inline int
706e0dccamarcelgdb_rx_char(void)
716e0dccamarcel{
726e0dccamarcel	int c;
736e0dccamarcel
746e0dccamarcel	if (gdb_rxsz > 0) {
756e0dccamarcel		c = *gdb_rxp++;
766e0dccamarcel		gdb_rxsz--;
776e0dccamarcel	} else
782eac876cem		c = EOF;
796e0dccamarcel	return (c);
806e0dccamarcel}
816e0dccamarcel
826e0dccamarcelvoid gdb_tx_begin(char);
836e0dccamarcelint gdb_tx_end(void);
846e0dccamarcelint gdb_tx_mem(const unsigned char *, size_t);
856e0dccamarcelvoid gdb_tx_reg(int);
86ddc3aefcembool gdb_txbuf_has_capacity(size_t);
87b46fbbabennoint gdb_rx_bindata(unsigned char *data, size_t datalen, size_t *amt);
88b46fbbabennoint gdb_search_mem(const unsigned char *addr, size_t size,
89b46fbbabenno    const unsigned char *pat, size_t patlen, const unsigned char **found);
906e0dccamarcel
916e0dccamarcelstatic __inline void
926e0dccamarcelgdb_tx_char(char c)
936e0dccamarcel{
946e0dccamarcel	*gdb_txp++ = c;
956e0dccamarcel}
966e0dccamarcel
976e0dccamarcelstatic __inline int
986e0dccamarcelgdb_tx_empty(void)
996e0dccamarcel{
1006e0dccamarcel	gdb_tx_begin('\0');
1016e0dccamarcel	return (gdb_tx_end());
1026e0dccamarcel}
1036e0dccamarcel
1046e0dccamarcelstatic __inline void
1056e0dccamarcelgdb_tx_hex(uintmax_t n, int sz)
1066e0dccamarcel{
1076e0dccamarcel	gdb_txp += sprintf(gdb_txp, "%0*jx", sz, n);
1086e0dccamarcel}
1096e0dccamarcel
1106e0dccamarcelstatic __inline int
1116e0dccamarcelgdb_tx_err(int err)
1126e0dccamarcel{
1136e0dccamarcel	gdb_tx_begin('E');
1146e0dccamarcel	gdb_tx_hex(err, 2);
1156e0dccamarcel	return (gdb_tx_end());
1166e0dccamarcel}
1176e0dccamarcel
1186e0dccamarcelstatic __inline int
1196e0dccamarcelgdb_tx_ok(void)
1206e0dccamarcel{
1216e0dccamarcel	gdb_tx_begin('O');
1226e0dccamarcel	gdb_tx_char('K');
1236e0dccamarcel	return (gdb_tx_end());
1246e0dccamarcel}
1256e0dccamarcel
1266e0dccamarcelstatic __inline void
1276e0dccamarcelgdb_tx_str(const char *s)
1286e0dccamarcel{
1296e0dccamarcel	while (*s)
1306e0dccamarcel		*gdb_txp++ = *s++;
1316e0dccamarcel}
1326e0dccamarcel
1336e0dccamarcelstatic __inline void
1346e0dccamarcelgdb_tx_varhex(uintmax_t n)
1356e0dccamarcel{
1366e0dccamarcel	gdb_txp += sprintf(gdb_txp, "%jx", n);
1376e0dccamarcel}
1386e0dccamarcel
139b6995dbcemstatic __inline void
140b6995dbcemgdb_nack(void)
141b6995dbcem{
142b6995dbcem	if (gdb_ackmode)
143b6995dbcem		gdb_cur->gdb_putc('-');
144b6995dbcem}
145b6995dbcem
146b6995dbcemstatic __inline void
147b6995dbcemgdb_ack(void)
148b6995dbcem{
149b6995dbcem	if (gdb_ackmode)
150b6995dbcem		gdb_cur->gdb_putc('+');
151b6995dbcem}
152b6995dbcem
1536e0dccamarcel#endif /* !_GDB_GDB_INT_H_ */
154