2022-01-06 13:44:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* mm/mm_heap/mm_memdump.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/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/mm/mm.h>
|
|
|
|
|
|
|
|
#include "mm_heap/mm.h"
|
|
|
|
|
2022-01-20 10:17:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if UINTPTR_MAX <= UINT32_MAX
|
|
|
|
# define MM_PTR_FMT_WIDTH 11
|
|
|
|
#elif UINTPTR_MAX <= UINT64_MAX
|
|
|
|
# define MM_PTR_FMT_WIDTH 19
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-06 13:44:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void memdump_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
|
|
|
|
{
|
2023-05-05 10:52:30 +02:00
|
|
|
FAR const struct mm_memdump_s *dump = arg;
|
2022-12-15 12:55:16 +01:00
|
|
|
size_t nodesize = SIZEOF_MM_NODE(node);
|
2022-01-06 13:44:17 +01:00
|
|
|
|
2022-12-15 12:55:16 +01:00
|
|
|
if ((node->size & MM_ALLOC_BIT) != 0)
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
2022-12-15 12:55:16 +01:00
|
|
|
DEBUGASSERT(nodesize >= SIZEOF_MM_ALLOCNODE);
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE < 0
|
2023-05-05 10:52:30 +02:00
|
|
|
if (dump->pid == MM_BACKTRACE_ALLOC_PID)
|
2022-01-20 10:17:29 +01:00
|
|
|
#else
|
2023-05-05 10:52:30 +02:00
|
|
|
if ((dump->pid == MM_BACKTRACE_ALLOC_PID || node->pid == dump->pid) &&
|
|
|
|
node->seqno >= dump->seqmin && node->seqno <= dump->seqmax)
|
2022-01-20 10:17:29 +01:00
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE < 0
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12zu%*p\n",
|
2022-12-15 12:55:16 +01:00
|
|
|
nodesize, MM_PTR_FMT_WIDTH,
|
2022-01-20 10:17:29 +01:00
|
|
|
((FAR char *)node + SIZEOF_MM_ALLOCNODE));
|
|
|
|
#else
|
2022-07-04 09:41:06 +02:00
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
2022-01-20 10:17:29 +01:00
|
|
|
int i;
|
2022-02-09 08:27:22 +01:00
|
|
|
FAR const char *format = " %0*p";
|
2022-07-04 09:41:06 +02:00
|
|
|
# endif
|
|
|
|
char buf[CONFIG_MM_BACKTRACE * MM_PTR_FMT_WIDTH + 1];
|
2022-01-20 10:17:29 +01:00
|
|
|
|
2022-02-09 08:27:22 +01:00
|
|
|
buf[0] = '\0';
|
2022-07-04 09:41:06 +02:00
|
|
|
# if CONFIG_MM_BACKTRACE > 0
|
|
|
|
for (i = 0; i < CONFIG_MM_BACKTRACE && node->backtrace[i]; i++)
|
2022-01-20 10:17:29 +01:00
|
|
|
{
|
2023-03-05 15:34:33 +01:00
|
|
|
snprintf(buf + i * MM_PTR_FMT_WIDTH,
|
|
|
|
sizeof(buf) - i * MM_PTR_FMT_WIDTH,
|
|
|
|
format, MM_PTR_FMT_WIDTH - 1, node->backtrace[i]);
|
2022-01-20 10:17:29 +01:00
|
|
|
}
|
2022-07-04 09:41:06 +02:00
|
|
|
# endif
|
2022-01-20 10:17:29 +01:00
|
|
|
|
2023-05-05 10:52:30 +02:00
|
|
|
syslog(LOG_INFO, "%6d%12zu%12lu%*p%s\n",
|
|
|
|
(int)node->pid, nodesize, node->seqno,
|
|
|
|
MM_PTR_FMT_WIDTH,
|
2022-01-20 10:17:29 +01:00
|
|
|
((FAR char *)node + SIZEOF_MM_ALLOCNODE), buf);
|
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FAR struct mm_freenode_s *fnode = (FAR void *)node;
|
|
|
|
|
2022-12-30 09:30:55 +01:00
|
|
|
DEBUGASSERT(nodesize >= MM_MIN_CHUNK);
|
2022-01-06 13:44:17 +01:00
|
|
|
DEBUGASSERT(fnode->blink->flink == fnode);
|
2022-12-15 12:55:16 +01:00
|
|
|
DEBUGASSERT(SIZEOF_MM_NODE(fnode->blink) <= nodesize);
|
2022-01-06 13:44:17 +01:00
|
|
|
DEBUGASSERT(fnode->flink == NULL ||
|
|
|
|
fnode->flink->blink == fnode);
|
|
|
|
DEBUGASSERT(fnode->flink == NULL ||
|
2022-12-15 12:55:16 +01:00
|
|
|
SIZEOF_MM_NODE(fnode->flink) == 0 ||
|
|
|
|
SIZEOF_MM_NODE(fnode->flink) >= nodesize);
|
2022-01-06 13:44:17 +01:00
|
|
|
|
2023-05-05 10:52:30 +02:00
|
|
|
if (dump->pid <= MM_BACKTRACE_FREE_PID)
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12zu%*p\n",
|
2022-12-15 12:55:16 +01:00
|
|
|
nodesize, MM_PTR_FMT_WIDTH,
|
2022-01-20 10:17:29 +01:00
|
|
|
((FAR char *)node + SIZEOF_MM_ALLOCNODE));
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: mm_memdump
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* mm_memdump returns a memory info about specified pid of task/thread.
|
2022-01-20 10:17:29 +01:00
|
|
|
* if pid equals -1, this function will dump all allocated node and output
|
|
|
|
* backtrace for every allocated node for this heap, if pid equals -2, this
|
|
|
|
* function will dump all free node for this heap, and if pid is greater
|
|
|
|
* than or equal to 0, will dump pid allocated node and output backtrace.
|
2022-01-06 13:44:17 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2023-05-05 10:52:30 +02:00
|
|
|
void mm_memdump(FAR struct mm_heap_s *heap,
|
|
|
|
FAR const struct mm_memdump_s *dump)
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
2022-11-20 08:00:44 +01:00
|
|
|
struct mallinfo_task info;
|
2022-01-06 13:44:17 +01:00
|
|
|
|
2023-05-05 10:52:30 +02:00
|
|
|
if (dump->pid >= -1)
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Dump all used memory node info:\n");
|
2022-07-04 09:41:06 +02:00
|
|
|
#if CONFIG_MM_BACKTRACE < 0
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12s%*s\n", "Size", MM_PTR_FMT_WIDTH, "Address");
|
|
|
|
#else
|
2023-05-05 10:52:30 +02:00
|
|
|
syslog(LOG_INFO, "%6s%12s%12s%*s %s\n", "PID", "Size", "Sequence",
|
|
|
|
MM_PTR_FMT_WIDTH, "Address", "Backtrace");
|
2022-01-20 10:17:29 +01:00
|
|
|
#endif
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
2022-01-20 10:17:29 +01:00
|
|
|
else
|
2022-01-06 13:44:17 +01:00
|
|
|
{
|
|
|
|
syslog(LOG_INFO, "Dump all free memory node info:\n");
|
2022-01-20 10:17:29 +01:00
|
|
|
syslog(LOG_INFO, "%12s%*s\n", "Size", MM_PTR_FMT_WIDTH, "Address");
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 16:27:44 +01:00
|
|
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
2023-05-05 10:52:30 +02:00
|
|
|
mempool_multiple_memdump(heap->mm_mpool, dump);
|
2022-11-06 16:27:44 +01:00
|
|
|
#endif
|
2023-05-05 10:52:30 +02:00
|
|
|
mm_foreach(heap, memdump_handler, (FAR void *)dump);
|
|
|
|
|
|
|
|
info = mm_mallinfo_task(heap, dump);
|
2022-01-06 13:44:17 +01:00
|
|
|
|
|
|
|
syslog(LOG_INFO, "%12s%12s\n", "Total Blks", "Total Size");
|
2022-11-20 08:00:44 +01:00
|
|
|
syslog(LOG_INFO, "%12d%12d\n", info.aordblks, info.uordblks);
|
2022-01-06 13:44:17 +01:00
|
|
|
}
|