Mesa (debug-refcnt-2): u_debug_symbol: add support for getting symbol names from glibc
Luca Barbieri
lb at kemper.freedesktop.org
Fri Aug 20 12:20:46 UTC 2010
Module: Mesa
Branch: debug-refcnt-2
Commit: 382b022619d9d2b21226f1f59c57c095e7e4f1ac
URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=382b022619d9d2b21226f1f59c57c095e7e4f1ac
Author: Luca Barbieri <luca at luca-barbieri.com>
Date: Wed Aug 18 00:39:49 2010 +0200
u_debug_symbol: add support for getting symbol names from glibc
---
src/gallium/auxiliary/util/u_debug_symbol.c | 23 +++++++++++++++++++++++
src/gallium/tools/addr2line.sh | 26 ++++++++++++++++++++++++++
2 files changed, 49 insertions(+), 0 deletions(-)
diff --git a/src/gallium/auxiliary/util/u_debug_symbol.c b/src/gallium/auxiliary/util/u_debug_symbol.c
index 1f71c1d..61f4e6f 100644
--- a/src/gallium/auxiliary/util/u_debug_symbol.c
+++ b/src/gallium/auxiliary/util/u_debug_symbol.c
@@ -143,6 +143,23 @@ debug_symbol_name_imagehlp(const void *addr, char* buf, unsigned size)
}
#endif
+#ifdef __GLIBC__
+#include <execinfo.h>
+
+/* This can only provide dynamic symbols, or binary offsets into a file.
+ *
+ * To fix this, post-process the output with tools/addr2line.sh
+ */
+static INLINE void
+debug_symbol_name_glibc(const void *addr, char* buf, unsigned size)
+{
+ char** syms = backtrace_symbols((void**)&addr, 1);
+ strncpy(buf, syms[0], size);
+ buf[size - 1] = 0;
+ free(syms);
+}
+#endif
+
void
debug_symbol_name(const void *addr, char* buf, unsigned size)
{
@@ -152,6 +169,12 @@ debug_symbol_name(const void *addr, char* buf, unsigned size)
return;
#endif
+#ifdef __GLIBC__
+ debug_symbol_name_glibc(addr, buf, size);
+ if(buf[0])
+ return;
+#endif
+
util_snprintf(buf, size, "%p", addr);
buf[size - 1] = 0;
}
diff --git a/src/gallium/tools/addr2line.sh b/src/gallium/tools/addr2line.sh
new file mode 100755
index 0000000..34dec14
--- /dev/null
+++ b/src/gallium/tools/addr2line.sh
@@ -0,0 +1,26 @@
+#!/bin/bash
+# This script processes symbols output by Gallium using glibc to human-readable function names
+
+lastbin=
+i=-1
+dir="$(mktemp -d)"
+input="$1"
+
+# Gather all unique addresses for each binary
+sed -nre 's|([^ ]*/[^ ]*)\(\+0x([^)]*).*|\1 \2|p' "$input"|sort|uniq|while read bin addr; do
+ if test "$lastbin" != "$bin"; then
+ ((++i))
+ lastbin="$bin"
+ echo "$bin" > "$dir/$i.addrs.bin"
+ fi
+ echo "$addr" >> "$dir/$i.addrs"
+done
+
+# Construct a sed script to convert hex address to human readable form, and apply it
+for i in "$dir"/*.addrs; do
+ bin="$(<"$i.bin")"
+ addr2line -p -e "$bin" -a -f < "$i"|sed -nre 's@^0x0*([^:]*): ([^?]*)$@s|'"$bin"'(+0x\1)|\2|g at gp'
+ rm -f "$i" "$i.bin"
+done|sed -f - "$input"
+
+rmdir "$dir"
More information about the mesa-commit
mailing list