[PATCH resend 3/8] mmap_cache: Move code for searching an mmap cache from unwind

Masatake YAMATO yamato at redhat.com
Fri Feb 16 19:37:10 UTC 2018


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 <yamato at 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 eb3aee4d..1081a962 100644
--- a/defs.h
+++ b/defs.h
@@ -749,6 +749,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.14.3





More information about the Strace-devel mailing list