[PATCH] stack: Add --quiet to not resolve addresses, add --raw to not demangle.

Mark Wielaard mjw at redhat.com
Mon Dec 23 20:35:41 UTC 2013


Resolving addresses to function symbol names can be expensive. Use -q
to only print addresses (use together with --build-id to process later).
Demangle names by default, but add the -r option to not demangle and
show the raw names.

Signed-off-by: Mark Wielaard <mjw at redhat.com>
---
 src/ChangeLog   |   12 ++++++++++++
 src/Makefile.am |    3 ++-
 src/stack.c     |   45 +++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 0f1b971..28a6f9e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,17 @@
 2013-12-23  Mark Wielaard  <mjw at redhat.com>
 
+	* Makefile.am (stack_LDADD): Add demanglelib.
+	* stack.c (show_quiet): New static boolean, default false.
+	(show_raw): Likewise.
+	(demangle_buffer_len): New static size_t.
+	(demangle_buffer): New static char *.
+	(print_frames): Don't resolve pc name if show_quiet. Demangle name
+	unless show_raw.
+	(parse_opt): Handle '-q' and '-r'.
+	(main): Add 'q' and 'r' to options. Free demangle_buffer.
+
+2013-12-23  Mark Wielaard  <mjw at redhat.com>
+
 	* stack.c (main): Print pid of process or core.
 
 2013-12-23  Mark Wielaard  <mjw at redhat.com>
diff --git a/src/Makefile.am b/src/Makefile.am
index 954a14b..9a78348 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -115,7 +115,8 @@ ranlib_LDADD = libar.a $(libelf) $(libeu) $(libmudflap)
 strings_LDADD = $(libelf) $(libeu) $(libmudflap)
 ar_LDADD = libar.a $(libelf) $(libeu) $(libmudflap)
 unstrip_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(libmudflap) -ldl
-stack_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(libmudflap) -ldl
+stack_LDADD = $(libebl) $(libelf) $(libdw) $(libeu) $(libmudflap) -ldl \
+	      $(demanglelib)
 
 ldlex.o: ldscript.c
 ldlex_no_Werror = yes
diff --git a/src/stack.c b/src/stack.c
index dfb0272..3568298 100644
--- a/src/stack.c
+++ b/src/stack.c
@@ -45,6 +45,10 @@ static bool show_module = false;
 static bool show_build_id = false;
 static bool show_source = false;
 static bool show_one_tid = false;
+static bool show_quiet = false;
+#ifdef USE_DEMANGLE
+static bool show_raw = false;
+#endif
 
 static unsigned maxframes = 64;
 
@@ -81,6 +85,11 @@ static const Dwfl_Callbacks core_callbacks =
     .debuginfo_path = &debuginfo_path,
   };
 
+#ifdef USE_DEMANGLE
+static size_t demangle_buffer_len = 0;
+static char *demangle_buffer = NULL;
+#endif
+
 static int
 frame_callback (Dwfl_Frame *state, void *arg)
 {
@@ -112,7 +121,7 @@ print_frames (struct frames *frames)
       /* Get PC->SYMNAME.  */
       Dwfl_Module *mod = dwfl_addrmodule (dwfl, pc_adjusted);
       const char *symname = NULL;
-      if (mod)
+      if (mod && ! show_quiet)
 	symname = dwfl_module_addrname (mod, pc_adjusted);
 
       // Try to find the address wide if possible.
@@ -138,7 +147,19 @@ print_frames (struct frames *frames)
 	printf ("%4s", ! isactivation ? "- 1" : "");
 
       if (symname != NULL)
-	printf (" %s", symname);
+	{
+#ifdef USE_DEMANGLE
+	  if (! show_raw)
+	    {
+	      int status = -1;
+	      char *dsymname = __cxa_demangle (symname, demangle_buffer,
+					       &demangle_buffer_len, &status);
+	      if (status == 0)
+		symname = demangle_buffer = dsymname;
+	    }
+#endif
+	  printf (" %s", symname);
+	}
 
       const char* fname;
       Dwarf_Addr start;
@@ -267,6 +288,16 @@ parse_opt (int key, char *arg __attribute__ ((unused)),
       show_build_id = true;
       break;
 
+    case 'q':
+      show_quiet = true;
+      break;
+
+#ifdef USE_DEMANGLE
+    case 'r':
+      show_raw = true;
+      break;
+#endif
+
     case '1':
       show_one_tid = true;
       break;
@@ -352,6 +383,12 @@ main (int argc, char **argv)
 	N_("Additionally show source file information"), 0 },
       { "verbose", 'v', NULL, 0,
 	N_("Show all additional information (activation, module and source)"), 0 },
+      { "quiet", 'q', NULL, 0,
+	N_("Do not resolve address to function symbol name"), 0 },
+#ifdef USE_DEMANGLE
+      { "raw", 'r', NULL, 0,
+	N_("Show raw function symbol names, do not try to demangle names"), 0 },
+#endif
       { "build-id",  'b', NULL, 0,
 	N_("Show module build-id, load address and pc offset"), 0 },
       { NULL, '1', NULL, 0,
@@ -413,5 +450,9 @@ main (int argc, char **argv)
   if (core_fd != -1)
     close (core_fd);
 
+#ifdef USE_DEMANGLE
+  free (demangle_buffer);
+#endif
+
   return 0;
 }
-- 
1.7.1



More information about the elfutils-devel mailing list