2023-11-09 05:02:37 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/misc/coredump.c
|
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2023-11-09 05:02:37 +01:00
|
|
|
* 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/binfmt/binfmt.h>
|
2024-01-30 09:08:04 +01:00
|
|
|
#include <nuttx/coredump.h>
|
2023-11-09 05:02:37 +01:00
|
|
|
#include <syslog.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_COMPRESSION
|
|
|
|
static struct lib_lzfoutstream_s g_lzfstream;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_SYSLOG
|
|
|
|
static struct lib_syslogstream_s g_syslogstream;
|
|
|
|
static struct lib_hexdumpstream_s g_hexstream;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_BLKDEV
|
|
|
|
static struct lib_blkoutstream_s g_blockstream;
|
|
|
|
static unsigned char *g_blockinfo;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static struct memory_region_s *g_regions;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: coredump_dump_syslog
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Put coredump to block device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_SYSLOG
|
|
|
|
static void coredump_dump_syslog(pid_t pid)
|
|
|
|
{
|
|
|
|
FAR void *stream;
|
|
|
|
int logmask;
|
|
|
|
|
|
|
|
logmask = setlogmask(LOG_ALERT);
|
|
|
|
|
|
|
|
_alert("Start coredump:\n");
|
|
|
|
|
|
|
|
/* Initialize hex output stream */
|
|
|
|
|
|
|
|
lib_syslogstream(&g_syslogstream, LOG_EMERG);
|
|
|
|
stream = &g_syslogstream;
|
|
|
|
lib_hexdumpstream(&g_hexstream, stream);
|
|
|
|
stream = &g_hexstream;
|
|
|
|
# ifdef CONFIG_BOARD_COREDUMP_COMPRESSION
|
|
|
|
|
|
|
|
/* Initialize LZF compression stream */
|
|
|
|
|
|
|
|
lib_lzfoutstream(&g_lzfstream, stream);
|
|
|
|
stream = &g_lzfstream;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/* Do core dump */
|
|
|
|
|
|
|
|
core_dump(g_regions, stream, pid);
|
|
|
|
|
|
|
|
# ifdef CONFIG_BOARD_COREDUMP_COMPRESSION
|
|
|
|
_alert("Finish coredump (Compression Enabled).\n");
|
|
|
|
# else
|
|
|
|
_alert("Finish coredump.\n");
|
|
|
|
# endif
|
|
|
|
|
|
|
|
setlogmask(logmask);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: coredump_dump_blkdev
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Save coredump to block device.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_BLKDEV
|
|
|
|
static void coredump_dump_blkdev(pid_t pid)
|
|
|
|
{
|
|
|
|
FAR void *stream = &g_blockstream;
|
|
|
|
FAR struct coredump_info_s *info;
|
2024-08-09 08:23:52 +02:00
|
|
|
blkcnt_t nsectors;
|
2023-11-09 05:02:37 +01:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (g_blockstream.inode == NULL)
|
|
|
|
{
|
2024-02-02 04:20:52 +01:00
|
|
|
_alert("Coredump device not found\n");
|
2023-11-09 05:02:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-09 08:23:52 +02:00
|
|
|
nsectors = (sizeof(struct coredump_info_s) +
|
|
|
|
g_blockstream.geo.geo_sectorsize - 1) /
|
|
|
|
g_blockstream.geo.geo_sectorsize;
|
|
|
|
|
2023-11-09 05:02:37 +01:00
|
|
|
ret = g_blockstream.inode->u.i_bops->read(g_blockstream.inode,
|
2024-08-09 08:23:52 +02:00
|
|
|
g_blockinfo, g_blockstream.geo.geo_nsectors - nsectors, nsectors);
|
2023-11-09 05:02:37 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-02-02 04:20:52 +01:00
|
|
|
_alert("Coredump information read fail\n");
|
2023-11-09 05:02:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = (FAR struct coredump_info_s *)g_blockinfo;
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_COMPRESSION
|
|
|
|
lib_lzfoutstream(&g_lzfstream,
|
|
|
|
(FAR struct lib_outstream_s *)&g_blockstream);
|
|
|
|
stream = &g_lzfstream;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ret = core_dump(g_regions, stream, pid);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-02-02 04:20:52 +01:00
|
|
|
_alert("Coredump fail\n");
|
2023-11-09 05:02:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
info->magic = COREDUMP_MAGIC;
|
|
|
|
info->size = g_blockstream.common.nput;
|
2024-08-09 08:23:52 +02:00
|
|
|
info->time = time(NULL);
|
2023-11-09 05:02:37 +01:00
|
|
|
uname(&info->name);
|
2024-02-02 04:20:52 +01:00
|
|
|
ret = g_blockstream.inode->u.i_bops->write(g_blockstream.inode,
|
2024-08-09 08:23:52 +02:00
|
|
|
(FAR void *)info, g_blockstream.geo.geo_nsectors - nsectors, nsectors);
|
2024-02-02 04:20:52 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
_alert("Coredump information write fail\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_alert("Finish coredump, write %d bytes to %s\n",
|
|
|
|
info->size, CONFIG_BOARD_COREDUMP_BLKDEV_PATH);
|
2023-11-09 05:02:37 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-01-30 09:08:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: coredump_set_memory_region
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set do coredump memory region.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int coredump_set_memory_region(FAR struct memory_region_s *region)
|
|
|
|
{
|
|
|
|
/* Not free g_regions, because allow call this fun when crash */
|
|
|
|
|
|
|
|
g_regions = region;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-09 05:02:37 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: coredump_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the coredump facility. Called once and only from
|
|
|
|
* nx_start_application.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int coredump_initialize(void)
|
|
|
|
{
|
2024-08-09 08:23:52 +02:00
|
|
|
blkcnt_t nsectors;
|
2023-11-09 05:02:37 +01:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (CONFIG_BOARD_MEMORY_RANGE[0] != '\0')
|
|
|
|
{
|
2024-01-30 09:08:04 +01:00
|
|
|
coredump_set_memory_region(
|
|
|
|
alloc_memory_region(CONFIG_BOARD_MEMORY_RANGE));
|
2023-11-09 05:02:37 +01:00
|
|
|
if (g_regions == NULL)
|
|
|
|
{
|
2024-02-02 04:20:52 +01:00
|
|
|
_alert("Coredump memory region alloc fail\n");
|
2023-11-09 05:02:37 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_BLKDEV
|
|
|
|
ret = lib_blkoutstream_open(&g_blockstream,
|
|
|
|
CONFIG_BOARD_COREDUMP_BLKDEV_PATH);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2024-02-02 04:20:52 +01:00
|
|
|
_alert("%s Coredump device not found\n",
|
2023-11-09 05:02:37 +01:00
|
|
|
CONFIG_BOARD_COREDUMP_BLKDEV_PATH);
|
|
|
|
free_memory_region(g_regions);
|
|
|
|
g_regions = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-08-09 08:23:52 +02:00
|
|
|
nsectors = (sizeof(struct coredump_info_s) +
|
|
|
|
g_blockstream.geo.geo_sectorsize - 1) /
|
|
|
|
g_blockstream.geo.geo_sectorsize;
|
|
|
|
|
|
|
|
g_blockinfo = kmm_malloc(g_blockstream.geo.geo_sectorsize * nsectors);
|
2023-11-09 05:02:37 +01:00
|
|
|
if (g_blockinfo == NULL)
|
|
|
|
{
|
2024-02-02 04:20:52 +01:00
|
|
|
_alert("Coredump device memory alloc fail\n");
|
2023-11-09 05:02:37 +01:00
|
|
|
free_memory_region(g_regions);
|
|
|
|
g_regions = NULL;
|
|
|
|
lib_blkoutstream_close(&g_blockstream);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2024-08-09 08:23:52 +02:00
|
|
|
UNUSED(nsectors);
|
2023-11-09 05:02:37 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: coredump_dump
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Do coredump of the task specified by pid.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* pid - The task/thread ID of the thread to dump
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void coredump_dump(pid_t pid)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_SYSLOG
|
|
|
|
coredump_dump_syslog(pid);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_BOARD_COREDUMP_BLKDEV
|
|
|
|
coredump_dump_blkdev(pid);
|
|
|
|
#endif
|
|
|
|
}
|