From patchwork Tue Feb 12 03:59:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kyeong Yoo X-Patchwork-Id: 31405 Received: (qmail 28078 invoked by alias); 12 Feb 2019 03:59:12 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 28070 invoked by uid 89); 12 Feb 2019 03:59:11 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gate2.alliedtelesis.co.nz DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=alliedtelesis.co.nz; s=mail181024; t=1549943948; bh=mIYu4mXDxgXzMeM0Q2IftPXhrcB/uE9M4N4Kw6HThXE=; h=Subject:From:To:Date; b=MfwKeOVtoOPlKfspnwypwLt8GbK/kbwLf6xdGkcajDDv4CfmkfgMiwDNvsKhAanml GwYwmlBiU/LScohdhraN9fgAsGlf5lS0v009LtB9Q03ol7I7AcfWbazBxCeWyeyDK2 rOPAASA0FIi3dt5Or6PirMthdLL2pMV0nSUo7N4VzVbj5yhvZGQ6oEA0suNm92THMP FAimKu4gFfT413a2GVntnsa5NzvY/hZ52aOqQqsYulpLOuXxE0ysjNiuy9vtGtmAhU GTDScglVOpynn6AQflhUeo3CxNqAU1XMElxL4DN6vmx92G1/Q49ZgwX3x9K9l/DAA1 qfKzI60WCeTsg== Subject: [PATCH v3 2/2] mtrace: record backtrace of memory allocation/deallocation From: Kyeong Yoo To: libc-alpha@sourceware.org Date: Tue, 12 Feb 2019 16:59:07 +1300 Message-ID: <154994393070.1879.1205862072081380968.stgit@kyeongy-dl.ws.atlnz.lc> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 x-atlnz-ls: pat New test is added for mtrace with backtrace output. It is simply to check when the feature is turned on, backtrace appears 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 diff --git a/malloc/Makefile b/malloc/Makefile index ab2eed09c6..d0111bb4ec 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -59,7 +59,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 \ @@ -161,6 +161,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 @@ -186,6 +187,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 + . */ + +#include +#include +#include + +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 +# . + +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