1
0
Fork 0
forked from nuttx/nuttx-update

gdbstub:support only use gdbstub when panic

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2024-07-17 10:39:53 +08:00 committed by Xiang Xiao
parent 5817f6153d
commit e007ace00d
4 changed files with 139 additions and 20 deletions

View file

@ -53,6 +53,23 @@ config SERIAL_GDBSTUB_PATH
---help---
The path to the serial device that will be used for GDB stub.
config SERIAL_GDBSTUB_AUTO_ATTACH
bool "GDBSTUB Serial Auto attach"
depends on SERIAL_GDBSTUB
default n
---help---
Attach the serial port to gdbstub by default.
Will cover the GDBSTUB_SERIAL_PATH, that port will only
be used for GDB stub.
config SERIAL_GDBSTUB_PANIC_TIMEOUT
int "GDBSTUB Serial panic timeout"
depends on SERIAL_GDBSTUB
default 10
---help---
Delay the panic timeout for gdbstub to connect.
0 will disable the timeout.
config UART_HOSTFS
bool "Use hostfs read/write on UART_HOSTFS_DEVPATH as uart console"
depends on FS_HOSTFS

View file

@ -1928,9 +1928,11 @@ int uart_register(FAR const char *path, FAR uart_dev_t *dev)
/* Register the serial driver */
#ifdef CONFIG_SERIAL_GDBSTUB
if (strcmp(path, CONFIG_SERIAL_GDBSTUB_PATH) == 0)
if (uart_gdbstub_register(dev, path) == 0)
{
return uart_gdbstub_register(dev);
/* No need register the device if it is used by gdbstub */
return 0;
}
#endif

View file

@ -24,6 +24,7 @@
#include <nuttx/serial/serial.h>
#include <nuttx/panic_notifier.h>
#include <nuttx/syslog/syslog.h>
#include <nuttx/kmalloc.h>
#include <nuttx/gdbstub.h>
#include <nuttx/nuttx.h>
@ -32,24 +33,54 @@
#include <debug.h>
/****************************************************************************
* Private Data
* Private Types
****************************************************************************/
struct uart_gdbstub_s
{
FAR struct uart_dev_s *dev;
FAR struct uart_dev_s *console;
FAR struct gdb_state_s *state;
FAR const struct uart_ops_s *org_ops;
struct uart_ops_s ops;
struct notifier_block nb;
};
/****************************************************************************
* Private Data
****************************************************************************/
static FAR struct uart_gdbstub_s *g_uart_gdbstub;
/****************************************************************************
* Private Functions prototypes
****************************************************************************/
static int uart_gdbstub_ctrlc(FAR struct uart_dev_s *dev,
FAR unsigned int *status);
/****************************************************************************
* Private Functions
****************************************************************************/
static void uart_gdbstub_attach(FAR struct uart_gdbstub_s *uart_gdbstub,
bool replace)
{
FAR uart_dev_t *dev = uart_gdbstub->dev;
if (replace && uart_gdbstub->org_ops == NULL)
{
memcpy(&uart_gdbstub->ops, dev->ops, sizeof(struct uart_ops_s));
uart_gdbstub->org_ops = dev->ops;
uart_gdbstub->ops.receive = uart_gdbstub_ctrlc;
dev->ops = &uart_gdbstub->ops;
}
uart_setup(dev);
uart_attach(dev);
uart_enablerxint(dev);
}
/****************************************************************************
* Name: uart_gdbstub_panic_callback
*
@ -64,19 +95,74 @@ static int uart_gdbstub_panic_callback(FAR struct notifier_block *nb,
{
FAR struct uart_gdbstub_s *uart_gdbstub =
container_of(nb, struct uart_gdbstub_s, nb);
#if CONFIG_SERIAL_GDBSTUB_PANIC_TIMEOUT != 0
unsigned int base;
unsigned int status;
char ch;
#endif
if (action != PANIC_KERNEL_FINAL)
{
return 0;
}
#if CONFIG_SERIAL_GDBSTUB_PANIC_TIMEOUT == 0
gdb_console_message(uart_gdbstub->state,
"Enter panic gdbstub mode!\n");
#else
_alert("Press Y/y key in %d seconds to enter gdb debug mode\n",
CONFIG_SERIAL_GDBSTUB_PANIC_TIMEOUT);
syslog_flush();
if (uart_gdbstub->console == NULL)
{
#ifndef CONFIG_SERIAL_GDBSTUB_AUTO_ATTACH
uart_gdbstub_attach(uart_gdbstub, false);
#endif
uart_gdbstub->console = uart_gdbstub->dev;
}
base = clock_systime_ticks();
while (true)
{
if (uart_gdbstub->console == uart_gdbstub->dev &&
uart_gdbstub->org_ops != NULL)
{
ch = uart_gdbstub->org_ops->receive(uart_gdbstub->console,
&status);
}
else
{
ch = uart_gdbstub->console->ops->receive(uart_gdbstub->console,
&status);
}
if (ch == 'Y' || ch == 'y')
{
break;
}
if ((clock_systime_ticks()) - base >=
SEC2TICK(CONFIG_SERIAL_GDBSTUB_PANIC_TIMEOUT))
{
_alert("%d seconds passed, exit now\n",
CONFIG_SERIAL_GDBSTUB_PANIC_TIMEOUT);
return 0;
}
}
#endif
#ifndef CONFIG_SERIAL_GDBSTUB_AUTO_ATTACH
uart_gdbstub_attach(uart_gdbstub, true);
#endif
_alert("Enter panic gdbstub mode, plase use gdb connect to debug\n");
_alert("Please use gdb of the corresponding architecture to "
"connect to nuttx");
_alert("such as: arm-none-eabi-gdb nuttx -ex \"set "
"target-charset ASCII\" -ex \"target remote /dev/ttyUSB0\"\n");
gdb_console_message(uart_gdbstub->state, "Enter panic gdbstub mode!\n");
syslog_flush();
gdb_process(uart_gdbstub->state, GDB_STOPREASON_CTRLC, NULL);
return 0;
}
@ -120,7 +206,7 @@ static ssize_t uart_gdbstub_receive(FAR void *priv, FAR void *buf,
{
if (uart_gdbstub->org_ops->rxavailable(dev))
{
ptr[i++] = g_uart_gdbstub->org_ops->receive(dev, &state);
ptr[i++] = uart_gdbstub->org_ops->receive(dev, &state);
}
}
@ -165,14 +251,33 @@ static ssize_t uart_gdbstub_send(FAR void *priv, FAR void *buf, size_t len)
*
****************************************************************************/
int uart_gdbstub_register(FAR uart_dev_t *dev)
int uart_gdbstub_register(FAR uart_dev_t *dev, FAR const char *path)
{
FAR struct uart_gdbstub_s *uart_gdbstub;
uart_gdbstub = kmm_malloc(sizeof(struct uart_gdbstub_s));
if (uart_gdbstub == NULL)
if (g_uart_gdbstub == NULL)
{
return -ENOMEM;
uart_gdbstub = kmm_zalloc(sizeof(struct uart_gdbstub_s));
if (uart_gdbstub == NULL)
{
return -ENOMEM;
}
g_uart_gdbstub = uart_gdbstub;
}
else
{
uart_gdbstub = g_uart_gdbstub;
}
if (dev->isconsole && uart_gdbstub->console == NULL)
{
uart_gdbstub->console = dev;
}
if (strcmp(path, CONFIG_SERIAL_GDBSTUB_PATH) != 0)
{
return -EINVAL;
}
uart_gdbstub->state = gdb_state_init(uart_gdbstub_send,
@ -184,19 +289,14 @@ int uart_gdbstub_register(FAR uart_dev_t *dev)
return -ENOMEM;
}
g_uart_gdbstub = uart_gdbstub;
memcpy(&uart_gdbstub->ops, dev->ops, sizeof(struct uart_ops_s));
uart_gdbstub->dev = dev;
uart_gdbstub->org_ops = dev->ops;
uart_gdbstub->ops.receive = uart_gdbstub_ctrlc;
dev->ops = &uart_gdbstub->ops;
uart_setup(dev);
uart_attach(dev);
uart_enablerxint(dev);
uart_gdbstub->nb.notifier_call = uart_gdbstub_panic_callback;
panic_notifier_chain_register(&uart_gdbstub->nb);
#ifdef CONFIG_SERIAL_GDBSTUB_AUTO_ATTACH
uart_gdbstub_attach(uart_gdbstub, true);
return 0;
#else
return 1;
#endif
}

View file

@ -561,7 +561,7 @@ int uart_check_special(FAR uart_dev_t *dev, FAR const char *buf,
****************************************************************************/
#ifdef CONFIG_SERIAL_GDBSTUB
int uart_gdbstub_register(FAR uart_dev_t *dev);
int uart_gdbstub_register(FAR uart_dev_t *dev, FAR const char *path);
#endif
#undef EXTERN