2023-10-31 20:12:02 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/serial/serial_gdbstub.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/serial/serial.h>
|
|
|
|
#include <nuttx/panic_notifier.h>
|
2024-07-17 10:39:53 +08:00
|
|
|
#include <nuttx/syslog/syslog.h>
|
2023-10-31 20:12:02 +08:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/gdbstub.h>
|
|
|
|
#include <nuttx/nuttx.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
2024-07-17 10:39:53 +08:00
|
|
|
* Private Types
|
2023-10-31 20:12:02 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct uart_gdbstub_s
|
|
|
|
{
|
|
|
|
FAR struct uart_dev_s *dev;
|
2024-07-17 10:39:53 +08:00
|
|
|
FAR struct uart_dev_s *console;
|
2023-10-31 20:12:02 +08:00
|
|
|
FAR struct gdb_state_s *state;
|
|
|
|
FAR const struct uart_ops_s *org_ops;
|
|
|
|
struct uart_ops_s ops;
|
|
|
|
struct notifier_block nb;
|
|
|
|
};
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-10-31 20:12:02 +08:00
|
|
|
static FAR struct uart_gdbstub_s *g_uart_gdbstub;
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uart_gdbstub_ctrlc(FAR struct uart_dev_s *dev,
|
|
|
|
FAR unsigned int *status);
|
|
|
|
|
2023-10-31 20:12:02 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
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);
|
2024-08-20 16:38:42 +08:00
|
|
|
uart_disablerxint(dev);
|
2024-07-17 10:39:53 +08:00
|
|
|
}
|
|
|
|
|
2023-10-31 20:12:02 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_gdbstub_panic_callback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is panic callback for gdbstub, If a crash occurs,
|
|
|
|
* you can debug it through gdb
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uart_gdbstub_panic_callback(FAR struct notifier_block *nb,
|
|
|
|
unsigned long action, FAR void *data)
|
|
|
|
{
|
|
|
|
FAR struct uart_gdbstub_s *uart_gdbstub =
|
|
|
|
container_of(nb, struct uart_gdbstub_s, nb);
|
2024-07-17 10:39:53 +08:00
|
|
|
#if CONFIG_SERIAL_GDBSTUB_PANIC_TIMEOUT != 0
|
|
|
|
unsigned int base;
|
|
|
|
unsigned int status;
|
|
|
|
char ch;
|
|
|
|
#endif
|
2023-10-31 20:12:02 +08:00
|
|
|
|
|
|
|
if (action != PANIC_KERNEL_FINAL)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
#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)
|
|
|
|
{
|
2024-08-20 16:38:42 +08:00
|
|
|
if (uart_gdbstub->org_ops->recvbuf)
|
|
|
|
{
|
|
|
|
if (uart_gdbstub->org_ops->rxavailable(uart_gdbstub->console))
|
|
|
|
{
|
|
|
|
uart_gdbstub->org_ops->recvbuf(uart_gdbstub->console,
|
|
|
|
&ch, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch = uart_gdbstub->org_ops->receive(uart_gdbstub->console,
|
|
|
|
&status);
|
|
|
|
}
|
2024-07-17 10:39:53 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-20 16:38:42 +08:00
|
|
|
if (uart_gdbstub->console->ops->recvbuf)
|
|
|
|
{
|
|
|
|
if (uart_gdbstub->console->ops->rxavailable(
|
|
|
|
uart_gdbstub->console))
|
|
|
|
{
|
|
|
|
uart_gdbstub->console->ops->recvbuf(uart_gdbstub->console,
|
|
|
|
&ch, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch = uart_gdbstub->console->ops->receive(uart_gdbstub->console,
|
|
|
|
&status);
|
|
|
|
}
|
2024-07-17 10:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-10-31 20:12:02 +08:00
|
|
|
_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");
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
syslog_flush();
|
2024-08-20 16:38:42 +08:00
|
|
|
gdb_process(uart_gdbstub->state, GDB_STOPREASON_NONE, NULL);
|
2023-10-31 20:12:02 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_gdbstub_ctrlc
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is uart receive callback in interruption.
|
|
|
|
* The function is to accept the initial connection of Ctrl c and gdb.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int uart_gdbstub_ctrlc(FAR struct uart_dev_s *dev,
|
|
|
|
FAR unsigned int *status)
|
|
|
|
{
|
|
|
|
uart_disablerxint(dev);
|
2024-06-24 11:39:58 +08:00
|
|
|
gdb_process(g_uart_gdbstub->state, GDB_STOPREASON_CTRLC, NULL);
|
2023-10-31 20:12:02 +08:00
|
|
|
uart_enablerxint(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_gdbstub_receive
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is gdbstub receive char function.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t uart_gdbstub_receive(FAR void *priv, FAR void *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
FAR struct uart_gdbstub_s *uart_gdbstub = priv;
|
|
|
|
FAR uart_dev_t *dev = uart_gdbstub->dev;
|
|
|
|
FAR char *ptr = buf;
|
|
|
|
unsigned int state;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
while (i < len)
|
|
|
|
{
|
|
|
|
if (uart_gdbstub->org_ops->rxavailable(dev))
|
|
|
|
{
|
2024-08-20 16:38:42 +08:00
|
|
|
if (uart_gdbstub->org_ops->recvbuf)
|
|
|
|
{
|
|
|
|
i += uart_gdbstub->org_ops->recvbuf(dev, ptr + i, len - i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr[i++] = uart_gdbstub->org_ops->receive(dev, &state);
|
|
|
|
}
|
2023-10-31 20:12:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_gdbstub_send
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This is gdbstub send char function.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t uart_gdbstub_send(FAR void *priv, FAR void *buf, size_t len)
|
|
|
|
{
|
|
|
|
FAR struct uart_gdbstub_s *uart_gdbstub = priv;
|
|
|
|
FAR uart_dev_t *dev = uart_gdbstub->dev;
|
|
|
|
size_t i = 0;
|
|
|
|
|
|
|
|
while (i < len)
|
|
|
|
{
|
|
|
|
if (uart_gdbstub->org_ops->txready(dev))
|
|
|
|
{
|
2024-08-20 16:38:42 +08:00
|
|
|
if (uart_gdbstub->org_ops->sendbuf)
|
|
|
|
{
|
|
|
|
i += uart_gdbstub->org_ops->sendbuf(dev, buf + i, len - i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
uart_gdbstub->org_ops->send(dev, ((FAR char *)buf)[i++]);
|
|
|
|
}
|
2023-10-31 20:12:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-20 16:38:42 +08:00
|
|
|
while (!uart_gdbstub->org_ops->txempty(dev));
|
|
|
|
|
2023-10-31 20:12:02 +08:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: uart_gdbstub_register
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Use the uart device to register gdbstub.
|
|
|
|
* gdbstub run with serial interrupt.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
int uart_gdbstub_register(FAR uart_dev_t *dev, FAR const char *path)
|
2023-10-31 20:12:02 +08:00
|
|
|
{
|
|
|
|
FAR struct uart_gdbstub_s *uart_gdbstub;
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
if (g_uart_gdbstub == NULL)
|
2023-10-31 20:12:02 +08:00
|
|
|
{
|
2024-07-17 10:39:53 +08:00
|
|
|
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;
|
2023-10-31 20:12:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uart_gdbstub->state = gdb_state_init(uart_gdbstub_send,
|
|
|
|
uart_gdbstub_receive,
|
|
|
|
NULL, uart_gdbstub);
|
|
|
|
if (uart_gdbstub->state == NULL)
|
|
|
|
{
|
|
|
|
kmm_free(uart_gdbstub);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
uart_gdbstub->dev = dev;
|
|
|
|
uart_gdbstub->nb.notifier_call = uart_gdbstub_panic_callback;
|
|
|
|
panic_notifier_chain_register(&uart_gdbstub->nb);
|
|
|
|
|
2024-07-17 10:39:53 +08:00
|
|
|
#ifdef CONFIG_SERIAL_GDBSTUB_AUTO_ATTACH
|
|
|
|
uart_gdbstub_attach(uart_gdbstub, true);
|
2023-10-31 20:12:02 +08:00
|
|
|
return 0;
|
2024-07-17 10:39:53 +08:00
|
|
|
#else
|
|
|
|
return 1;
|
|
|
|
#endif
|
2023-10-31 20:12:02 +08:00
|
|
|
}
|