[Ltrace-devel] [PATCH v2] add library path to stack trace output when using -w

Luca Clementi luca.clementi at gmail.com
Tue Oct 1 05:40:10 UTC 2013


For each stack frame this patch prints the library path containing the
code pointed by the IP.
The output format is similar to the return value of backtrace_symbols
function found in glibc, e.g.:
 > ./a.out() [0x40063d]
 > ./a.out() [0x4006bb]
 > ./a.out() [0x4006c6]
 > /lib64/libc.so.6(__libc_start_main+0xed) [0x7fa2f8a5976d]
 > ./a.out() [0x400569]
---
 output.c |   26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/output.c b/output.c
index da8c419..26e83c0 100644
--- a/output.c
+++ b/output.c
@@ -548,6 +548,16 @@ free_stringp_cb(const char **stringp, void *data)
 	free((char *)*stringp);
 }
 
+
+#ifdef HAVE_LIBUNWIND
+static enum callback_status
+find_lib_ip(struct process *proc, struct library *lib, void *ip)
+{
+	arch_addr_t *my_ip =  ip;
+	return CBS_STOP_IF((*my_ip >= lib->base) && (*my_ip < lib->dyn_addr));
+}
+#endif
+
 void
 output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
 {
@@ -671,15 +681,27 @@ again:
 	    && proc->unwind_as != NULL) {
 		unw_cursor_t cursor;
 		unw_word_t ip, sp;
+		unw_word_t function_offset;
+		struct library *lib = NULL;
 		int unwind_depth = options.bt_depth;
 		char fn_name[100];
+		const char *lib_name;
 
 		unw_init_remote(&cursor, proc->unwind_as, proc->unwind_priv);
 		while (unwind_depth) {
 			unw_get_reg(&cursor, UNW_REG_IP, &ip);
 			unw_get_reg(&cursor, UNW_REG_SP, &sp);
-			unw_get_proc_name(&cursor, fn_name, 100, NULL);
-			fprintf(options.output, "\t\t\t%s (ip = 0x%lx)\n", fn_name, (long) ip);
+			unw_get_proc_name(&cursor, fn_name, sizeof(fn_name),
+					&function_offset);
+
+			lib = proc_each_library(proc, NULL, find_lib_ip, &ip);
+			if (lib)
+				lib_name = lib->pathname;
+			else
+				lib_name = "unmapped_area";
+			fprintf(options.output, " > %s(%s+0x%lx) [0x%lx]\n",
+					lib_name, fn_name, function_offset, (long) ip);
+
 			if (unw_step(&cursor) <= 0)
 				break;
 			unwind_depth--;
-- 
1.7.9.5




More information about the Ltrace-devel mailing list