1、【优化】函数调用栈获取功能,提升获取的准确性。

Signed-off-by: armink <armink.ztl@gmail.com>
pull/1/head
armink 9 years ago
parent 043c1f2eb0
commit 4591679ad0

@ -317,12 +317,20 @@ static void dump_main_stack(uint32_t stack_start_addr, size_t stack_size, uint32
* @return depth * @return depth
*/ */
size_t cm_backtrace_call_stack(uint32_t *buffer, size_t size, uint32_t sp) { size_t cm_backtrace_call_stack(uint32_t *buffer, size_t size, uint32_t sp) {
uint32_t stack_start_addr = main_stack_start_addr, stack_size = main_stack_size; uint32_t stack_start_addr = main_stack_start_addr, stack_size = main_stack_size, pc;
size_t depth = 0; size_t depth = 0;
bool regs_saved_lr_is_valid = false;
if (on_fault) { if (on_fault) {
/* first depth is PC */ /* first depth is PC */
buffer[depth++] = regs.saved.pc; buffer[depth++] = regs.saved.pc;
/* second depth is from LR, so need decrease a word to PC */
pc = regs.saved.lr - sizeof(size_t);
if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
&& (depth < size)) {
buffer[depth++] = pc;
regs_saved_lr_is_valid = true;
}
#ifdef CMB_USING_OS_PLATFORM #ifdef CMB_USING_OS_PLATFORM
/* program is running on thread before fault */ /* program is running on thread before fault */
@ -340,10 +348,15 @@ size_t cm_backtrace_call_stack(uint32_t *buffer, size_t size, uint32_t sp) {
/* copy called function address */ /* copy called function address */
for (; sp < stack_start_addr + stack_size; sp += sizeof(size_t)) { for (; sp < stack_start_addr + stack_size; sp += sizeof(size_t)) {
if ((*((uint32_t *) sp) >= code_start_addr) && (*((uint32_t *) sp) <= code_start_addr + code_size) /* the *sp value may be LR, so need decrease a word to PC */
&& (depth < CMB_CALL_STACK_MAX_DEPTH) && (depth < size)) { pc = *((uint32_t *) sp) - sizeof(size_t);
/* this get value maybe LR, so need decrease a word to PC */ if ((pc >= code_start_addr) && (pc <= code_start_addr + code_size) && (depth < CMB_CALL_STACK_MAX_DEPTH)
buffer[depth++] = *((uint32_t *) sp) - sizeof(size_t); && (depth < size)) {
/* the second depth function may be already saved, so need ignore repeat */
if ((depth == 2) && regs_saved_lr_is_valid && (pc == buffer[1])) {
continue;
}
buffer[depth++] = pc;
} }
} }

Loading…
Cancel
Save