libc: Add backtrace_symbols[_fd] functions

specified here:
https://linux.die.net/man/3/backtrace_symbols

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2021-12-24 23:00:37 +08:00 committed by archer
parent f302e8fd40
commit 20bbdd0997
3 changed files with 94 additions and 1 deletions

View File

@ -59,6 +59,9 @@ extern "C"
#define EXTERN extern
#endif
FAR char **backtrace_symbols(FAR void *const *buffer, int size);
void backtrace_symbols_fd(FAR void *const *buffer, int size, int fd);
#undef EXTERN
#if defined(__cplusplus)
}

View File

@ -36,7 +36,7 @@ endif
CSRCS += lib_dumpbuffer.c lib_dumpvbuffer.c lib_fnmatch.c lib_debug.c
CSRCS += lib_crc64.c lib_crc32.c lib_crc16.c lib_crc8.c lib_crc8ccitt.c
CSRCS += lib_crc8table.c lib_glob.c
CSRCS += lib_crc8table.c lib_glob.c lib_execinfo.c
# Keyboard driver encoder/decoder

View File

@ -0,0 +1,90 @@
/****************************************************************************
* libs/libc/misc/lib_execinfo.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 <execinfo.h>
#include <stdio.h>
#include "libc.h"
/****************************************************************************
* Private Functions
****************************************************************************/
static FAR char **backtrace_malloc(FAR void *const *buffer, int size)
{
size_t length = 0;
if (size <= 0)
{
return NULL;
}
while (size-- > 0)
{
int ret = sprintf(NULL, "%pS", *buffer++);
if (ret < 0)
{
return NULL;
}
length += sizeof(FAR char *) + ret + 1;
}
return lib_malloc(length);
}
/****************************************************************************
* Public Functions
****************************************************************************/
FAR char **backtrace_symbols(FAR void *const *buffer, int size)
{
FAR char **syms;
FAR char *buf;
int i;
syms = backtrace_malloc(buffer, size);
if (syms != NULL)
{
buf = syms[size];
for (i = 0; i < size; i++)
{
syms[i] = buf;
buf += sprintf(buf, "%pS", buffer[i]);
buf += 1;
}
}
return syms;
}
void backtrace_symbols_fd(FAR void *const *buffer, int size, int fd)
{
int i;
for (i = 0; i < size; i++)
{
dprintf(fd, "%pS\n", buffer[i]);
}
}