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

Luca Clementi luca.clementi at gmail.com
Sat Oct 5 04:08:37 UTC 2013


For each stack frame 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.:
 > /bin/ls(_init+0x19be) [0x40398e]
 > /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7f50cbc3676d]
 > /bin/ls(_init+0x25fd) [0x4045cd]
---
 output.c |   34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/output.c b/output.c
index da8c419..f89b287 100644
--- a/output.c
+++ b/output.c
@@ -548,6 +548,7 @@ free_stringp_cb(const char **stringp, void *data)
 	free((char *)*stringp);
 }
 
+
 void
 output_right(enum tof type, struct process *proc, struct library_symbol *libsym)
 {
@@ -670,16 +671,41 @@ again:
 	    && proc->unwind_priv != NULL
 	    && proc->unwind_as != NULL) {
 		unw_cursor_t cursor;
-		unw_word_t ip, sp;
+		arch_addr_t ip;
+		unw_word_t function_offset, sp;
+		struct library *lib = NULL;
 		int unwind_depth = options.bt_depth;
 		char fn_name[100];
+		const char *lib_name;
+		size_t distance;
 
 		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_IP, (unw_word_t *) &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);
+
+			/* we are looking for the for the library with the
+			 * closest base address to the current ip */
+			lib_name = NULL;
+			distance = (size_t) -1;
+			lib = proc->libraries;
+			while (lib != NULL) {
+				if ((ip > lib->base) &&
+					    ((size_t)(ip - lib->base)
+					    < distance)) {
+					distance = ip - lib->base;
+					lib_name = lib->pathname;
+				}
+				lib = lib->next;
+			}
+
+			if (!lib_name)
+				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