mirror of
https://github.com/apache/nuttx.git
synced 2026-06-08 01:42:58 +08:00
libc: add instrument api support
Add registration function instrumentation API, which can achieve instrumentation of entering and exiting functions through the compiler's functionality. We can use CONFIG_ARCH_INSTRUMENT_ALL to add instrumentation for all source, or add '-finstrument-functions' to CFLAGS for Part of the source. Notice: 1. use CONFIG_ARCH_INSTRUMENT_ALL must mark _start or entry noinstrument_function, becuase bss not set. 2. Make sure your callbacks are not instrumented recursively. use instrument_register to register entry function and exit function. They will be called by the instrumented function Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
@@ -79,7 +79,8 @@ list(
|
||||
lib_glob.c
|
||||
lib_execinfo.c
|
||||
lib_ftok.c
|
||||
lib_err.c)
|
||||
lib_err.c
|
||||
lib_instrument.c)
|
||||
|
||||
# Keyboard driver encoder/decoder
|
||||
|
||||
|
||||
@@ -40,7 +40,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_crc16ccitt.c lib_crc8.c
|
||||
CSRCS += lib_crc8ccitt.c lib_crc8table.c lib_glob.c lib_execinfo.c
|
||||
CSRCS += lib_ftok.c lib_err.c
|
||||
CSRCS += lib_ftok.c lib_err.c lib_instrument.c
|
||||
|
||||
# Keyboard driver encoder/decoder
|
||||
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/****************************************************************************
|
||||
* libs/libc/misc/lib_instrument.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/instrument.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* Avoid instrument bootstrap */
|
||||
|
||||
#define MAIGC_NUMBMER 0x5a5a5a5a
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/* Use static to avoid instrument bootstrap */
|
||||
|
||||
static volatile uint32_t g_magic;
|
||||
static sq_queue_t g_instrument_queue;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: __cyg_profile_func_enter
|
||||
****************************************************************************/
|
||||
|
||||
void noinstrument_function
|
||||
__cyg_profile_func_enter(FAR void *this_fn, FAR void *call_site)
|
||||
{
|
||||
FAR struct instrument_s *instrument;
|
||||
FAR sq_entry_t *entry;
|
||||
|
||||
if (g_magic != MAIGC_NUMBMER)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sq_for_every(&g_instrument_queue, entry)
|
||||
{
|
||||
instrument = (FAR struct instrument_s *)entry;
|
||||
if (instrument->enter)
|
||||
{
|
||||
instrument->enter(this_fn, call_site, instrument->arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: __cyg_profile_func_exit
|
||||
****************************************************************************/
|
||||
|
||||
void noinstrument_function
|
||||
__cyg_profile_func_exit(FAR void *this_fn, FAR void *call_site)
|
||||
{
|
||||
FAR struct instrument_s *instrument;
|
||||
FAR sq_entry_t *entry;
|
||||
|
||||
if (g_magic != MAIGC_NUMBMER)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sq_for_every(&g_instrument_queue, entry)
|
||||
{
|
||||
instrument = (FAR struct instrument_s *)entry;
|
||||
if (instrument->leave)
|
||||
{
|
||||
instrument->leave(this_fn, call_site, instrument->arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: instrument_register
|
||||
*
|
||||
* Description: register instrument, it will be called
|
||||
* when function enter or exit.
|
||||
*
|
||||
* Input Parameters:
|
||||
* entry - instrument entry structure.
|
||||
* Notice:
|
||||
* use CONFIG_ARCH_INSTRUMENT_ALL must mark _start or entry
|
||||
* noinstrument_function, becuase bss not set.
|
||||
* Make sure your callbacks are not instrumented recursively.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void noinstrument_function
|
||||
instrument_register(FAR struct instrument_s *entry)
|
||||
{
|
||||
if (entry != NULL)
|
||||
{
|
||||
sq_addlast((FAR sq_entry_t *)entry, &g_instrument_queue);
|
||||
g_magic = MAIGC_NUMBMER;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user