print_stack_frame function in unwind.c searches a mmap entry from mmap
caches. The found entry is used for unwinding. However, a function
searching a mmap entry may be useful for other purpose than unwinding.

This change re-factors the function; code for searching an entry is
now defiend as a stand-alone function named mmap_cache_search.

* defs.h (mmap_cache_search): New function derived from
print_stack_frame.
* mmap_cached.c (mmap_cache_search): The implementation of the
new function.
* unwind.c (print_stack_frame): Use the new function.

Signed-off-by: Masatake YAMATO <yam...@redhat.com>
---
 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 df3b78cb..a653c852 100644
--- a/defs.h
+++ b/defs.h
@@ -732,6 +732,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 void
 printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
diff --git a/mmap_cache.c b/mmap_cache.c
index 27fa548f..01ff3938 100644
--- a/mmap_cache.c
+++ b/mmap_cache.c
@@ -181,3 +181,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 95f7f2fa..c527d9a2 100644
--- a/unwind.c
+++ b/unwind.c
@@ -127,53 +127,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.14.3


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Strace-devel mailing list
Strace-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/strace-devel

Reply via email to