[v2,2/2] mtrace: record backtrace of memory allocation/deallocation

Message ID 154268921068.10712.396300436830603098.stgit@kyeongy-dl.ws.atlnz.lc
State Superseded
Headers show
Series
  • [v2,1/2] mtrace: record backtrace of memory allocation/deallocation
Related show

Commit Message

Kyeong Yoo Nov. 20, 2018, 4:47 a.m.
New test is added for mtrace with backtrace output.
It is simply to check when the feature is turned on, backtrace appeares
in the mtrace output.
---
 malloc/Makefile                |    8 +++++-
 malloc/tst-mtrace-backtrace.c  |   52 ++++++++++++++++++++++++++++++++++++++
 malloc/tst-mtrace-backtrace.sh |   55 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+), 1 deletion(-)
 create mode 100644 malloc/tst-mtrace-backtrace.c
 create mode 100644 malloc/tst-mtrace-backtrace.sh

Comments

Joseph Myers Nov. 20, 2018, 4:44 p.m. | #1
On Tue, 20 Nov 2018, Kyeong Yoo wrote:

> New test is added for mtrace with backtrace output.

> It is simply to check when the feature is turned on, backtrace appeares

> in the mtrace output.


I don't see anything here to add a configure test for the appropriate 
cross-compilation addr2line program and to pass the right option to 
mtrace - does the test succeed anyway in a cross-compilation environment 
even with mtrace using the native addr2line?

-- 
Joseph S. Myers
joseph@codesourcery.com
Kyeong Yoo Nov. 20, 2018, 10:22 p.m. | #2
Hi Joseph,
Thanks for reviewing my patch.

On 21/11/18 05:44, Joseph Myers wrote:
> On Tue, 20 Nov 2018, Kyeong Yoo wrote:

> 

>> New test is added for mtrace with backtrace output.

>> It is simply to check when the feature is turned on, backtrace appeares

>> in the mtrace output.

> 

> I don't see anything here to add a configure test for the appropriate

> cross-compilation addr2line program and to pass the right option to

> mtrace - does the test succeed anyway in a cross-compilation environment

> even with mtrace using the native addr2line?

The test I'm adding is for backtrace output from mtrace().
The case of cross-compilation is not there. If you have any suggestion 
of how to test cross-compilation case, please let me know.
For cross-compilation, native addr2line may mostly print the address 
with no meaningful source line (mostly ??:0).

Patch

diff --git a/malloc/Makefile b/malloc/Makefile
index 7d54bad866..b5972575fa 100644
--- a/malloc/Makefile
+++ b/malloc/Makefile
@@ -58,7 +58,7 @@  tests-static += tst-malloc-usable-static-tunables
 endif
 
 tests += $(tests-static)
-test-srcs = tst-mtrace
+test-srcs = tst-mtrace tst-mtrace-backtrace
 
 routines = malloc morecore mcheck mtrace obstack reallocarray \
   scratch_buffer_grow scratch_buffer_grow_preserve \
@@ -160,6 +160,7 @@  ifeq ($(run-built-tests),yes)
 ifeq (yes,$(build-shared))
 ifneq ($(PERL),no)
 tests-special += $(objpfx)tst-mtrace.out
+tests-special += $(objpfx)tst-mtrace-backtrace.out
 tests-special += $(objpfx)tst-dynarray-mem.out
 tests-special += $(objpfx)tst-dynarray-fail-mem.out
 endif
@@ -185,6 +186,11 @@  $(objpfx)tst-mtrace.out: tst-mtrace.sh $(objpfx)tst-mtrace
 	$(SHELL) $< $(common-objpfx) '$(test-program-prefix-before-env)' \
 		 '$(run-program-env)' '$(test-program-prefix-after-env)' > $@; \
 	$(evaluate-test)
+
+$(objpfx)tst-mtrace-backtrace.out: tst-mtrace-backtrace.sh $(objpfx)tst-mtrace-backtrace
+	$(SHELL) $< $(common-objpfx) '$(test-program-prefix-before-env)' \
+		 '$(run-program-env)' '$(test-program-prefix-after-env)' > $@; \
+	$(evaluate-test)
 endif
 endif
 endif
diff --git a/malloc/tst-mtrace-backtrace.c b/malloc/tst-mtrace-backtrace.c
new file mode 100644
index 0000000000..7b872a2e83
--- /dev/null
+++ b/malloc/tst-mtrace-backtrace.c
@@ -0,0 +1,52 @@ 
+/* Test program for mtrace.
+   Copyright (C) 2000-2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <mcheck.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static char *
+my_alloc (int n)
+{
+  char *p;
+
+  /* deliberate memory leak */
+  p = malloc (n + 1);
+
+  return p;
+}
+
+static int
+do_test (void)
+{
+  char *p;
+
+  /* Enable memory usage tracing.  */
+  mtrace ();
+
+  p = my_alloc (3);
+  printf ("p = %p\n", p);
+
+  muntrace ();
+
+  /* That's it.  */
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/malloc/tst-mtrace-backtrace.sh b/malloc/tst-mtrace-backtrace.sh
new file mode 100644
index 0000000000..1ae62a1f6a
--- /dev/null
+++ b/malloc/tst-mtrace-backtrace.sh
@@ -0,0 +1,55 @@ 
+#!/bin/sh
+# Testing the mtrace output with backtrace
+#
+# Copyright (C) 2018 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+
+# The GNU C Library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+
+# The GNU C Library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+
+# You should have received a copy of the GNU Lesser General Public
+# License along with the GNU C Library; if not, see
+# <http://www.gnu.org/licenses/>.
+
+set -e
+
+common_objpfx=$1; shift
+test_program_prefix_before_env=$1; shift
+run_program_env=$1; shift
+test_program_prefix_after_env=$1; shift
+
+app=${common_objpfx}malloc/tst-mtrace-backtrace
+app_leak=$app.leak
+app_out=$app.out
+mtrace=${common_objpfx}malloc/mtrace
+
+status=0
+trap "rm -f $app_leak; exit 1" 1 2 15
+
+# Run mtrace with backtrace
+${test_program_prefix_before_env} \
+${run_program_env} \
+MALLOC_TRACE=$app_leak \
+MALLOC_TRACE_LEVEL=3 \
+${test_program_prefix_after_env} \
+  $app || status=1
+
+# Check memory leak (added deliberately) and backtrace (marked with '#')
+if test $status -eq 0 && test -f $mtrace; then
+  if ! $mtrace $app $app_leak > $app_out; then
+    grep -q "#" $app_out || status=2
+  else
+    status=3
+  fi
+fi
+
+rm -f $app_leak
+
+exit $status