From 2bb35360c268717a5437d4d69a58dca3c929a9cd Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Sat, 17 Feb 2018 04:37:10 +0900 Subject: [PATCH] mmap_cache: move code for searching a mmap cache from unwind print_stack_frame function in unwind.c searches a mmap entry in mmap cache. The found entry is then used for unwinding. However, a function searching for a mmap entry may be useful for other purposes than unwinding. This change re-factors the function; code for searching an entry is now defined as a stand-alone function named mmap_cache_search. * defs.h (mmap_cache_search): New function prototype. print_stack_frame. * mmap_cached.c (mmap_cache_search): New function derived from print_stack_frame. * unwind.c (print_stack_frame): Use it. Signed-off-by: Masatake YAMATO --- defs.h | 1 + mmap_cache.c | 23 +++++++++++++++++++++++ unwind.c | 53 ++++++++++++++++++++-------------------------------- 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/defs.h b/defs.h index 403b20aa..97bdcd80 100644 --- a/defs.h +++ b/defs.h @@ -750,6 +750,7 @@ enum mmap_cache_rebuild_result { extern void mmap_cache_invalidate(struct tcb *tcp); extern void mmap_cache_delete(struct tcb *tcp, const char *caller); extern enum mmap_cache_rebuild_result mmap_cache_rebuild_if_invalid(struct tcb *tcp, const char *caller); +extern struct mmap_cache_t *mmap_cache_search(struct tcb *tcp, unsigned long ip); static inline int printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len) diff --git a/mmap_cache.c b/mmap_cache.c index eadfe283..0c6e2b7e 100644 --- a/mmap_cache.c +++ b/mmap_cache.c @@ -183,3 +183,26 @@ mmap_cache_invalidate(struct tcb *tcp) mmap_cache_generation, tcp, tcp->mmap_cache); } + +struct mmap_cache_t * +mmap_cache_search(struct tcb *tcp, unsigned long ip) +{ + int lower = 0; + int upper = (int) tcp->mmap_cache_size - 1; + + while (lower <= upper) { + struct mmap_cache_t *cur_mmap_cache; + int mid = (upper + lower) / 2; + + cur_mmap_cache = &tcp->mmap_cache[mid]; + + if (ip >= cur_mmap_cache->start_addr && + ip < cur_mmap_cache->end_addr) + return cur_mmap_cache; + else if (ip < cur_mmap_cache->start_addr) + upper = mid - 1; + else + lower = mid + 1; + } + return NULL; +} diff --git a/unwind.c b/unwind.c index 77fb2378..56a7b9ce 100644 --- a/unwind.c +++ b/unwind.c @@ -131,53 +131,40 @@ print_stack_frame(struct tcb *tcp, size_t *symbol_name_size) { unw_word_t ip; - int lower = 0; - int upper = (int) tcp->mmap_cache_size - 1; + struct mmap_cache_t *cur_mmap_cache; if (unw_get_reg(cursor, UNW_REG_IP, &ip) < 0) { perror_msg("Can't walk the stack of process %d", tcp->pid); return -1; } - while (lower <= upper) { - struct mmap_cache_t *cur_mmap_cache; - int mid = (upper + lower) / 2; - - cur_mmap_cache = &tcp->mmap_cache[mid]; - - if (ip >= cur_mmap_cache->start_addr && - ip < cur_mmap_cache->end_addr) { - unsigned long true_offset; - unw_word_t function_offset; - - get_symbol_name(cursor, symbol_name, symbol_name_size, - &function_offset); - true_offset = ip - cur_mmap_cache->start_addr + - cur_mmap_cache->mmap_offset; + cur_mmap_cache = mmap_cache_search(tcp, ip); + if (cur_mmap_cache) { + unsigned long true_offset; + unw_word_t function_offset; + get_symbol_name(cursor, symbol_name, symbol_name_size, + &function_offset); + true_offset = ip - cur_mmap_cache->start_addr + + cur_mmap_cache->mmap_offset; #ifdef USE_DEMANGLE - char *demangled_name = - cplus_demangle(*symbol_name, - DMGL_AUTO | DMGL_PARAMS); + char *demangled_name = + cplus_demangle(*symbol_name, + DMGL_AUTO | DMGL_PARAMS); #endif - - call_action(data, - cur_mmap_cache->binary_filename, + call_action(data, + cur_mmap_cache->binary_filename, #ifdef USE_DEMANGLE - demangled_name ? demangled_name : + demangled_name ? demangled_name : #endif - *symbol_name, - function_offset, - true_offset); + *symbol_name, + function_offset, + true_offset); #ifdef USE_DEMANGLE - free(demangled_name); + free(demangled_name); #endif - return 0; - } else if (ip < cur_mmap_cache->start_addr) - upper = mid - 1; - else - lower = mid + 1; + return 0; } /* -- 2.40.0