Skip to content

Commit 1c1ef69

Browse files
CV-Bowenluckyyaojin
authored andcommitted
drivers/rptun: add rptun trace feature for debug
Signed-off-by: Bowen Wang <[email protected]>
1 parent 59a849a commit 1c1ef69

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

drivers/rptun/rptun.c

+150
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <nuttx/config.h>
2828

29+
#include <execinfo.h>
2930
#include <inttypes.h>
3031
#include <stdio.h>
3132
#include <stdbool.h>
@@ -56,6 +57,12 @@
5657
* Private Types
5758
****************************************************************************/
5859

60+
struct rptun_trace_s
61+
{
62+
FAR void *hdr;
63+
FAR void *backtrace[16];
64+
};
65+
5966
struct rptun_priv_s
6067
{
6168
struct rpmsg_s rpmsg;
@@ -64,6 +71,7 @@ struct rptun_priv_s
6471
struct remoteproc rproc;
6572
struct rpmsg_virtio_shm_pool pool[2];
6673
sem_t semtx;
74+
FAR struct rptun_trace_s *traces;
6775
sem_t semrx;
6876
pid_t tid;
6977
uint16_t headrx;
@@ -128,6 +136,20 @@ static void rptun_panic(FAR struct rpmsg_s *rpmsg);
128136
static void rptun_dump(FAR struct rpmsg_s *rpmsg);
129137
static FAR const char *rptun_get_local_cpuname(FAR struct rpmsg_s *rpmsg);
130138
static FAR const char *rptun_get_cpuname(FAR struct rpmsg_s *rpmsg);
139+
#ifdef CONFIG_OPENAMP_RPMSG_TRACE
140+
static void rptun_trace_init(FAR struct rptun_priv_s *priv);
141+
static void rptun_trace_uninit(FAR struct rptun_priv_s *priv);
142+
static void rptun_trace_get_tx_buffer(FAR struct rpmsg_device *rdev,
143+
FAR void *hdr);
144+
static void rptun_trace_release_tx_buffer(FAR struct rpmsg_device *rdev,
145+
FAR void *hdr);
146+
static void rptun_dump_trace(FAR struct rpmsg_virtio_device *rvdev);
147+
148+
#else
149+
# define rptun_trace_init(priv)
150+
# define rptun_trace_uninit(priv)
151+
# define rptun_dump_trace(rvdev);
152+
#endif
131153

132154
/****************************************************************************
133155
* Private Data
@@ -722,6 +744,8 @@ static void rptun_dump(FAR struct rpmsg_s *rpmsg)
722744
metal_mutex_acquire(&rdev->lock);
723745
}
724746

747+
rptun_dump_trace(rvdev);
748+
725749
metal_log(METAL_LOG_EMERGENCY,
726750
"Dump rpmsg info between cpu (master: %s)%s <==> %s:\n",
727751
rpmsg_virtio_get_role(rvdev) == RPMSG_HOST ? "yes" : "no",
@@ -918,6 +942,7 @@ static int rptun_dev_start(FAR struct remoteproc *rproc)
918942
return ret;
919943
}
920944

945+
rptun_trace_init(priv);
921946
priv->rvdev.rdev.ns_unbind_cb = rpmsg_ns_unbind;
922947
priv->rvdev.notify_wait_cb = rptun_notify_wait;
923948

@@ -981,6 +1006,7 @@ static int rptun_dev_stop(FAR struct remoteproc *rproc, bool stop_ns)
9811006
/* Remote proc stop and shutdown */
9821007

9831008
remoteproc_shutdown(rproc);
1009+
rptun_trace_uninit(priv);
9841010

9851011
return OK;
9861012
}
@@ -1113,6 +1139,130 @@ static metal_phys_addr_t rptun_da_to_pa(FAR struct rptun_dev_s *dev,
11131139
return da;
11141140
}
11151141

1142+
#ifdef CONFIG_OPENAMP_RPMSG_TRACE
1143+
static void *rptun_get_priv_by_rdev(FAR struct rpmsg_device *rdev)
1144+
{
1145+
FAR struct rpmsg_virtio_device *rvdev;
1146+
FAR struct virtio_device *vdev;
1147+
FAR struct remoteproc_virtio *rpvdev;
1148+
FAR struct remoteproc *rproc;
1149+
1150+
if (!rdev)
1151+
{
1152+
return NULL;
1153+
}
1154+
1155+
rvdev = metal_container_of(rdev, struct rpmsg_virtio_device, rdev);
1156+
vdev = rvdev->vdev;
1157+
if (!vdev)
1158+
{
1159+
return NULL;
1160+
}
1161+
1162+
rpvdev = metal_container_of(vdev, struct remoteproc_virtio, vdev);
1163+
rproc = rpvdev->priv;
1164+
if (!rproc)
1165+
{
1166+
return NULL;
1167+
}
1168+
1169+
return rproc->priv;
1170+
}
1171+
1172+
static void rptun_trace_init(FAR struct rptun_priv_s *priv)
1173+
{
1174+
int txnum = priv->rvdev.svq->vq_nentries;
1175+
1176+
priv->traces = kmm_zalloc(sizeof(*priv->traces) * txnum);
1177+
DEBUGASSERT(priv->traces != NULL);
1178+
1179+
priv->rvdev.rdev.trace.get_tx_buffer = rptun_trace_get_tx_buffer;
1180+
priv->rvdev.rdev.trace.release_tx_buffer = rptun_trace_release_tx_buffer;
1181+
}
1182+
1183+
static void rptun_trace_uninit(FAR struct rptun_priv_s *priv)
1184+
{
1185+
priv->rvdev.rdev.trace.get_tx_buffer = NULL;
1186+
priv->rvdev.rdev.trace.release_tx_buffer = NULL;
1187+
1188+
kmm_free(priv->traces);
1189+
}
1190+
1191+
static void rptun_trace_get_tx_buffer(FAR struct rpmsg_device *rdev,
1192+
FAR void *hdr)
1193+
{
1194+
FAR struct rptun_priv_s *priv = rptun_get_priv_by_rdev(rdev);
1195+
int txnum = priv->rvdev.svq->vq_nentries;
1196+
int i;
1197+
1198+
for (i = 0; i < txnum; i++)
1199+
{
1200+
if (priv->traces[i].hdr == NULL)
1201+
{
1202+
break;
1203+
}
1204+
}
1205+
1206+
if (i >= txnum)
1207+
{
1208+
metal_log(METAL_LOG_EMERGENCY, "Not find empty buffer i=%d num=%d\n",
1209+
i, txnum);
1210+
PANIC();
1211+
}
1212+
1213+
priv->traces[i].hdr = hdr;
1214+
backtrace(priv->traces[i].backtrace, 16);
1215+
}
1216+
1217+
static void rptun_trace_release_tx_buffer(FAR struct rpmsg_device *rdev,
1218+
FAR void *hdr)
1219+
{
1220+
FAR struct rptun_priv_s *priv = rptun_get_priv_by_rdev(rdev);
1221+
int txnum = priv->rvdev.svq->vq_nentries;
1222+
int i;
1223+
int j;
1224+
1225+
for (i = 0; i < txnum; i++)
1226+
{
1227+
if (priv->traces[i].hdr == hdr)
1228+
{
1229+
break;
1230+
}
1231+
}
1232+
1233+
if (i >= txnum)
1234+
{
1235+
metal_log(METAL_LOG_EMERGENCY, "Not find tx buffer i=%d num=%d\n",
1236+
i, txnum);
1237+
PANIC();
1238+
}
1239+
1240+
priv->traces[i].hdr = NULL;
1241+
1242+
for (j = 0; j < 16; j++)
1243+
{
1244+
priv->traces[i].backtrace[j] = NULL;
1245+
}
1246+
}
1247+
1248+
static void rptun_dump_trace(FAR struct rpmsg_virtio_device *rvdev)
1249+
{
1250+
FAR struct rptun_priv_s *priv = rptun_get_priv_by_rdev(&rvdev->rdev);
1251+
char backtrace[256];
1252+
int txnum = rvdev->svq->vq_nentries;
1253+
int i;
1254+
1255+
metal_log(METAL_LOG_EMERGENCY, "Dump not released tx buffer:\n");
1256+
for (i = 0; i < txnum; i++)
1257+
{
1258+
backtrace_format(backtrace, sizeof(backtrace),
1259+
priv->traces[i].backtrace, 16);
1260+
metal_log(METAL_LOG_EMERGENCY, "hdr: %p backtrace: %s\n",
1261+
priv->traces[i].hdr, backtrace);
1262+
}
1263+
}
1264+
#endif
1265+
11161266
/****************************************************************************
11171267
* Public Functions
11181268
****************************************************************************/

openamp/Kconfig

+4
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ config OPENAMP_VIRTIO_DRIVER_SUPPORT
3030
bool "Enable VIRTIO_DRIVER_SUPPORT"
3131
default y
3232

33+
config OPENAMP_RPMSG_TRACE
34+
bool "Enable OpenAMP Rpmsg Trace"
35+
default n
36+
3337
endif # OPENAMP

openamp/open-amp.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,7 @@ target_include_directories(
113113
open_amp-static PRIVATE $<TARGET_PROPERTY:metal-static,INCLUDE_DIRECTORIES>)
114114

115115
nuttx_add_external_library(open_amp-static MODE KERNEL)
116+
117+
if(CONFIG_OPENAMP_RPMSG_TRACE)
118+
target_compile_options(openamp PRIVATE -DRPMSG_TRACE)
119+
endif()

openamp/open-amp.defs

+4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ else
4444
CFLAGS += -DVIRTIO_DRIVER_SUPPORT=0
4545
endif
4646

47+
ifeq ($(CONFIG_OPENAMP_RPMSG_TRACE),y)
48+
CFLAGS += -DRPMSG_TRACE
49+
endif
50+
4751
CSRCS += open-amp/lib/remoteproc/elf_loader.c
4852
CSRCS += open-amp/lib/remoteproc/remoteproc.c
4953
CSRCS += open-amp/lib/remoteproc/remoteproc_virtio.c

0 commit comments

Comments
 (0)