From c715cfc25436867729a2b70c6162e3cf4c15546f Mon Sep 17 00:00:00 2001 From: Szymon Gizler <151655464+sgizler@users.noreply.github.com> Date: Mon, 29 Jan 2024 13:50:05 +0100 Subject: [PATCH] Add --valgrind switch (#4828) --- bin/verilator | 15 +++++++++++++++ docs/guide/environment.rst | 5 +++++ docs/guide/exe_verilator.rst | 5 +++++ docs/internals.rst | 3 +++ src/V3Options.cpp | 1 + test_regress/driver.pl | 2 ++ test_regress/t/t_flag_help_valgrind.pl | 18 ++++++++++++++++++ 7 files changed, 49 insertions(+) create mode 100755 test_regress/t/t_flag_help_valgrind.pl diff --git a/bin/verilator b/bin/verilator index 0c2cbe780..83759f9ab 100755 --- a/bin/verilator +++ b/bin/verilator @@ -32,6 +32,7 @@ my $opt_rr; my $opt_gdbbt; my $opt_quiet_exit; my $opt_unlimited_stack = 1; +my $opt_valgrind; # No arguments can't do anything useful. Give help if ($#ARGV < 0) { @@ -59,6 +60,7 @@ if (! GetOptions( "quiet-exit!" => \$opt_quiet_exit, "rr!" => \$opt_rr, "unlimited-stack!" => \$opt_unlimited_stack, + "valgrind!" => \$opt_valgrind, # Additional parameters "<>" => sub {}, # Ignored )) { @@ -109,6 +111,18 @@ if ($opt_gdb) { . " -ex \"run " . join(' ', @quoted_sw)."\"" . " -ex 'set width 0'" . " -ex 'bt' -ex 'quit'"); +} elsif ($opt_valgrind) { + # Run under valgrind + my $valgrind_bin = ($ENV{VERILATOR_VALGRIND} || "valgrind --error-exitcode=1 --max-stackframe=2815880" + # Magic number sugested by valgrind, may need to be increased in future + # if you get warnings. See: https://valgrind.org/docs/manual/manual-core.html#opt.max-stackframe + ); + + run (ulimit_stack_unlimited() + . aslr_off() + . $valgrind_bin + . " " . verilator_bin() + . " " . join(' ', @quoted_sw)); } elsif ($Debug) { # Debug run(ulimit_stack_unlimited() @@ -453,6 +467,7 @@ detailed descriptions of these arguments. --unused-regexp Tune UNUSED lint signals -V Verbose version and config -v Verilog library + --valgrind Run Verilator under valgrind --verilate-jobs Job threads for Verilation stage --no-verilate Skip Verilation and just compile previously Verilated code +verilog1995ext+ Synonym for +1364-1995ext+ diff --git a/docs/guide/environment.rst b/docs/guide/environment.rst index 3afb8bf2f..8629c741e 100644 --- a/docs/guide/environment.rst +++ b/docs/guide/environment.rst @@ -118,6 +118,11 @@ associated programs. See :ref:`Installation` for more details. +.. option:: VERILATOR_VALGRIND + + If set, the command to run when using the :vlopt:`--valgrind` option, such as + "valgrind --tool=callgrind". If not specified, it will use "valgrind". + Make Variables ============== diff --git a/docs/guide/exe_verilator.rst b/docs/guide/exe_verilator.rst index d38560352..e4b3c914d 100644 --- a/docs/guide/exe_verilator.rst +++ b/docs/guide/exe_verilator.rst @@ -1516,6 +1516,11 @@ Summary: used to resolve instances in the top-level module, otherwise, they are ignored. Note "-v" is relatively standard across Verilog tools. +.. option:: --valgrind + + Run Verilator under `Valgrind `_. The command may be + changed with :option:`VERILATOR_VALGRIND`. + .. option:: --no-verilate When using :vlopt:`--build`, disable the generation of C++/SystemC code, and diff --git a/docs/internals.rst b/docs/internals.rst index b864badd4..de53d6b97 100644 --- a/docs/internals.rst +++ b/docs/internals.rst @@ -1937,6 +1937,9 @@ driver.pl Non-Scenario Arguments --trace Set the simulator specific flags to request waveform tracing. +--valgrind + Same as ``verilator --valgrind``: Run Verilator under `Valgrind `_. + --verbose Compile and run the test in verbose mode. This means ``TEST_VERBOSE`` will be defined for the test (Verilog and any C++/SystemC wrapper). diff --git a/src/V3Options.cpp b/src/V3Options.cpp index 0d589364c..25cf3ef65 100644 --- a/src/V3Options.cpp +++ b/src/V3Options.cpp @@ -1539,6 +1539,7 @@ void V3Options::parseOptsList(FileLine* fl, const string& optdir, int argc, DECL_OPTION("-v", CbVal, [this, &optdir](const char* valp) { V3Options::addLibraryFile(parseFileArg(optdir, valp)); }); + DECL_OPTION("-valgrind", CbCall, []() {}); // Processed only in bin/verilator shell DECL_OPTION("-verilate-jobs", CbVal, [this, fl](const char* valp) { int val = std::atoi(valp); if (val < 0) { diff --git a/test_regress/driver.pl b/test_regress/driver.pl index dbbd02f69..62735dfe7 100755 --- a/test_regress/driver.pl +++ b/test_regress/driver.pl @@ -1765,6 +1765,7 @@ sub _run { $wholefile =~ s/^- [^\n]+\n//mig; $wholefile =~ s/^- [a-z.0-9]+:\d+:[^\n]+\n//mig; $wholefile =~ s/^dot [^\n]+\n//mig; + $wholefile =~ s/^==[0-9]+== [^\n]+\n//mig; # valgrind # Compare my $quoted = quotemeta($param{expect}); @@ -2277,6 +2278,7 @@ sub files_identical { && !/^libgcov.*/ && !/--- \/tmp\// # t_difftree.pl && !/\+\+\+ \/tmp\// # t_difftree.pl + && !/^==[0-9]+== ?[^\n]*\n/ # valgrind } @l1; @l1 = map { while (s/(Internal Error: [^\n]+\.(cpp|h)):[0-9]+/$1:#/g) {} diff --git a/test_regress/t/t_flag_help_valgrind.pl b/test_regress/t/t_flag_help_valgrind.pl new file mode 100755 index 000000000..c254a5f23 --- /dev/null +++ b/test_regress/t/t_flag_help_valgrind.pl @@ -0,0 +1,18 @@ +#!/usr/bin/env perl +if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; } +# DESCRIPTION: Verilator: Verilog Test driver/expect definition +# +# Copyright 2024 by Wilson Snyder. This program is free software; you +# can redistribute it and/or modify it under the terms of either the GNU +# Lesser General Public License Version 3 or the Perl Artistic License +# Version 2.0. +# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0 + +run(fails => 0, + cmd => ["../bin/verilator", "--help", "--valgrind"], + tee => 0, + verilator_run => 1, + ); + +ok(1); +1;