From patchwork Wed Apr 27 20:34:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 11919 Received: (qmail 7848 invoked by alias); 27 Apr 2016 20:35:30 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 7305 invoked by uid 89); 27 Apr 2016 20:35:25 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gproxy7-pub.mail.unifiedlayer.com Received: from gproxy7-pub.mail.unifiedlayer.com (HELO gproxy7-pub.mail.unifiedlayer.com) (70.40.196.235) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with SMTP; Wed, 27 Apr 2016 20:35:08 +0000 Received: (qmail 10200 invoked by uid 0); 27 Apr 2016 20:35:05 -0000 Received: from unknown (HELO CMOut01) (10.0.90.82) by gproxy7.mail.unifiedlayer.com with SMTP; 27 Apr 2016 20:35:05 -0000 Received: from box522.bluehost.com ([74.220.219.122]) by CMOut01 with id nYas1s00d2f2jeq01YavNX; Wed, 27 Apr 2016 14:35:03 -0600 X-Authority-Analysis: v=2.1 cv=G/WPTbU5 c=1 sm=1 tr=0 a=GsOEXm/OWkKvwdLVJsfwcA==:117 a=GsOEXm/OWkKvwdLVJsfwcA==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=PnD2wP_eR3oA:10 a=_v2sUkyEFrwA:10 a=kziv93cY1bsA:10 a=zstS-IiYAAAA:8 a=0FD05c-RAAAA:8 a=mDV3o1hIAAAA:8 a=QyXUC8HyAAAA:8 a=LYxUrnBFnhYN9VJaNs8A:9 a=OUvoGx9YCqYtQQ_w:21 a=cx65mqEJFZ4rtQtH:21 Received: from [71.215.116.141] (port=33494 helo=bapiya.Home) by box522.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.86_2) (envelope-from ) id 1avWAe-0004OF-TJ; Wed, 27 Apr 2016 14:34:53 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 3/8] Add self-test framework to gdb Date: Wed, 27 Apr 2016 14:34:34 -0600 Message-Id: <1461789279-15996-4-git-send-email-tom@tromey.com> In-Reply-To: <1461789279-15996-1-git-send-email-tom@tromey.com> References: <1461789279-15996-1-git-send-email-tom@tromey.com> X-Identified-User: {36111:box522.bluehost.com:elynrobi:tromey.com} {sentby:smtp auth 71.215.116.141 authed with tom+tromey.com} I wanted to unit test the Rust lexer, so I added a simple unit testing command to gdb. The intent is that self tests will only be compiled into gdb in development mode. In release mode they simply won't exist. So, this exposes $development to C code as GDB_SELF_TEST. In development mode, test functions are registered with the self test module. A test function is just a function that does some checks, and aborts on failure. I chose this rather than something fancier because I think any such failure will require debugging anyhow. Then this adds a new "maint selftest" command which invokes the test functions, and a new dejagnu test case that invokes it. 2016-04-27 Tom Tromey * NEWS: Add "maint selftest" entry. * selftest.h: New file. * selftest.c: New file. * maint.c: Include selftest.h. (maintenance_selftest): New function. (_initialize_maint_cmds): Add "maint selftest" command. * configure.ac (GDB_SELF_TEST): Maybe define. * config.in, configure: Rebuild. * Makefile.in (SFILES): Add selftest.c. (COMMON_OBS): Add selftest.o. 2016-04-26 Tom Tromey * gdb.texinfo (Maintenance Commands): Document "maint selftest". 2016-04-26 Tom Tromey * gdb.gdb/unittest.exp: New file. --- gdb/ChangeLog | 13 ++++++++ gdb/Makefile.in | 6 ++-- gdb/NEWS | 3 ++ gdb/config.in | 3 ++ gdb/configure | 6 ++++ gdb/configure.ac | 5 +++ gdb/doc/ChangeLog | 4 +++ gdb/doc/gdb.texinfo | 6 ++++ gdb/maint.c | 18 +++++++++++ gdb/selftest.c | 66 ++++++++++++++++++++++++++++++++++++++ gdb/selftest.h | 35 ++++++++++++++++++++ gdb/testsuite/ChangeLog | 4 +++ gdb/testsuite/gdb.gdb/unittest.exp | 17 ++++++++++ 13 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 gdb/selftest.c create mode 100644 gdb/selftest.h create mode 100644 gdb/testsuite/gdb.gdb/unittest.exp diff --git a/gdb/ChangeLog b/gdb/ChangeLog index ea02ec9..d03a044 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,16 @@ +2016-04-27 Tom Tromey + + * NEWS: Add "maint selftest" entry. + * selftest.h: New file. + * selftest.c: New file. + * maint.c: Include selftest.h. + (maintenance_selftest): New function. + (_initialize_maint_cmds): Add "maint selftest" command. + * configure.ac (GDB_SELF_TEST): Maybe define. + * config.in, configure: Rebuild. + * Makefile.in (SFILES): Add selftest.c. + (COMMON_OBS): Add selftest.o. + 2016-04-26 Tom Tromey * expprint.c: Include f-lang.h. diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 400d2b0..c99bca6 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -869,8 +869,10 @@ SFILES = ada-exp.y ada-lang.c ada-typeprint.c ada-valprint.c ada-tasks.c \ proc-service.list progspace.c \ prologue-value.c psymtab.c \ regcache.c reggroups.c remote.c remote-fileio.c remote-notif.c reverse.c \ - sentinel-frame.c \ + selftest.c sentinel-frame.c \ serial.c ser-base.c ser-unix.c ser-event.c skip.c \ + selftest.c sentinel-frame.c \ + serial.c ser-base.c ser-unix.c skip.c \ solib.c solib-target.c source.c \ stabsread.c stack.c probe.c stap-probe.c std-regs.c \ symfile.c symfile-debug.c symfile-mem.c symmisc.c symtab.c \ @@ -1060,7 +1062,7 @@ COMMON_OBS = $(DEPFILES) $(CONFIG_OBS) $(YYOBJ) \ go-lang.o go-valprint.o go-typeprint.o \ jv-lang.o jv-valprint.o jv-typeprint.o jv-varobj.o \ m2-lang.o opencl-lang.o p-lang.o p-typeprint.o p-valprint.o \ - sentinel-frame.o \ + selftest.o sentinel-frame.o \ complaints.o typeprint.o \ ada-typeprint.o c-typeprint.o f-typeprint.o m2-typeprint.o \ ada-valprint.o c-valprint.o cp-valprint.o d-valprint.o f-valprint.o \ diff --git a/gdb/NEWS b/gdb/NEWS index 7bf1e1a..77dfc0c 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -40,6 +40,9 @@ skip -rfunction regular-expression maint info line-table REGEXP Display the contents of GDB's internal line table data struture. +maint selftest + Run any GDB unit tests that were compiled in. + * Support for tracepoints and fast tracepoints on s390-linux and s390x-linux was added in GDBserver, including JIT compiling fast tracepoint's conditional expression bytecode into native code. diff --git a/gdb/config.in b/gdb/config.in index dc9da0a..905caf0 100644 --- a/gdb/config.in +++ b/gdb/config.in @@ -65,6 +65,9 @@ /* Define to the default OS ABI for this configuration. */ #undef GDB_OSABI_DEFAULT +/* Define if self-testing features should be enabled */ +#undef GDB_SELF_TEST + /* Define to 1 if you have `alloca', as a function or macro. */ #undef HAVE_ALLOCA diff --git a/gdb/configure b/gdb/configure index 3cf95e7..9caf23f 100755 --- a/gdb/configure +++ b/gdb/configure @@ -16482,6 +16482,12 @@ ac_config_links="$ac_config_links $ac_config_links_1" $as_echo "#define GDB_DEFAULT_HOST_CHARSET \"UTF-8\"" >>confdefs.h +if $development; then + +$as_echo "#define GDB_SELF_TEST 1" >>confdefs.h + +fi + gdb_ac_transform=`echo "$program_transform_name" | sed -e 's/\\$\\$/\\$/g'` GDB_TRANSFORM_NAME=`echo gdb | sed -e "$gdb_ac_transform"` diff --git a/gdb/configure.ac b/gdb/configure.ac index 70452d3..b6a4aac 100644 --- a/gdb/configure.ac +++ b/gdb/configure.ac @@ -2330,6 +2330,11 @@ dnl At the moment, we just assume it's UTF-8. AC_DEFINE(GDB_DEFAULT_HOST_CHARSET, "UTF-8", [Define to be a string naming the default host character set.]) +if $development; then + AC_DEFINE(GDB_SELF_TEST, 1, + [Define if self-testing features should be enabled]) +fi + GDB_AC_TRANSFORM([gdb], [GDB_TRANSFORM_NAME]) GDB_AC_TRANSFORM([gcore], [GCORE_TRANSFORM_NAME]) AC_CONFIG_FILES([gcore], [chmod +x gcore]) diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index 59956ef..748a8e2 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,7 @@ +2016-04-26 Tom Tromey + + * gdb.texinfo (Maintenance Commands): Document "maint selftest". + 2016-04-13 Antoine Tremblay * agentexpr.texi (byte): Fix zero_ext description. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 7abd55e..f42c5f0 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -34450,6 +34450,12 @@ that symbol is described. The type chain produced by this command is a recursive definition of the data type as stored in @value{GDBN}'s data structures, including its flags and contained types. +@kindex maint selftest +@cindex self tests +Run any self tests that were compiled in to @value{GDBN}. If all the +self tests pass, this will print a message. If a self test fails, +@value{GDBN} will crash. + @kindex maint set dwarf always-disassemble @kindex maint show dwarf always-disassemble @item maint set dwarf always-disassemble diff --git a/gdb/maint.c b/gdb/maint.c index 358809d..60dc1ac 100644 --- a/gdb/maint.c +++ b/gdb/maint.c @@ -41,6 +41,7 @@ #include "top.h" #include "timeval-utils.h" #include "maint.h" +#include "selftest.h" #include "cli/cli-decode.h" #include "cli/cli-utils.h" @@ -982,6 +983,16 @@ show_per_command_cmd (char *args, int from_tty) cmd_show_list (per_command_showlist, from_tty, ""); } + +/* The "maintenance selftest" command. */ + +static void +maintenance_selftest (char *args, int from_tty) +{ + run_self_tests (); +} + + void _initialize_maint_cmds (void) { @@ -1154,6 +1165,13 @@ testsuite can check the command deprecator. You probably shouldn't use this,\n\ If you decide you want to use it: maintenance undeprecate 'commandname'"), &maintenancelist); + add_cmd ("selftest", class_maintenance, maintenance_selftest, _("\ +Run gdb's unit tests.\n\ +Usage: maintenance selftest\n\ +This will run any unit tests that were built in to gdb.\n\ +gdb will abort if any test fails."), + &maintenancelist); + add_setshow_zinteger_cmd ("watchdog", class_maintenance, &watchdog, _("\ Set watchdog timer."), _("\ Show watchdog timer."), _("\ diff --git a/gdb/selftest.c b/gdb/selftest.c new file mode 100644 index 0000000..fecc527 --- /dev/null +++ b/gdb/selftest.c @@ -0,0 +1,66 @@ +/* GDB self-testing. + Copyright (C) 2016 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include "defs.h" +#include "selftest.h" +#include "vec.h" + +typedef self_test_function *self_test_function_ptr; + +DEF_VEC_P (self_test_function_ptr); + +/* All the tests that have been registered. */ + +static VEC (self_test_function_ptr) *tests; + +/* See selftest.h. */ + +void +register_self_test (self_test_function *function) +{ + VEC_safe_push (self_test_function_ptr, tests, function); +} + +/* See selftest.h. */ + +void +run_self_tests (void) +{ + int i; + self_test_function_ptr func; + + for (i = 0; VEC_iterate (self_test_function_ptr, tests, i, func); ++i) + { + QUIT; + + TRY + { + (*func) (); + } + CATCH (ex, RETURN_MASK_ERROR) + { + internal_error (__FILE__, __LINE__, + _("self test threw exception: %s"), + ex.message); + } + END_CATCH + } + + printf_filtered (_("Ran %u unit tests\n"), + VEC_length (self_test_function_ptr, tests)); +} diff --git a/gdb/selftest.h b/gdb/selftest.h new file mode 100644 index 0000000..b191e8f --- /dev/null +++ b/gdb/selftest.h @@ -0,0 +1,35 @@ +/* GDB self-testing. + Copyright (C) 2016 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef SELFTEST_H +#define SELFTEST_H + +/* A test is just a function that does some checks and asserts if + something has gone wrong. */ + +typedef void self_test_function (void); + +/* Register a new self-test. */ + +extern void register_self_test (self_test_function *function); + +/* Run all the self tests. This will crash gdb if a test fails. */ + +extern void run_self_tests (void); + +#endif /* SELFTEST_H */ diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 92eb9ff..c3b6104 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2016-04-26 Tom Tromey + + * gdb.gdb/unittest.exp: New file. + 2016-04-26 Bernhard Heckel * vla-type.exp: Print structure from toplevel. diff --git a/gdb/testsuite/gdb.gdb/unittest.exp b/gdb/testsuite/gdb.gdb/unittest.exp new file mode 100644 index 0000000..8239349 --- /dev/null +++ b/gdb/testsuite/gdb.gdb/unittest.exp @@ -0,0 +1,17 @@ +# Copyright 2016 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +gdb_start +gdb_test "maintenance selftest" "Ran $decimal unit tests"