From 8a4fcdee4dce240c4a2f8369f0aba8733234245b Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Tue, 7 Apr 2020 00:40:27 -0700 Subject: [PATCH] util: Make m5 commands return a bool instead of calling usage. By delegating actually calling usage() elsewhere, we can remove a dependency from the commands themselves, and also make testing easier since we won't exit() every time we call a command with bad arguments. Change-Id: I6b8e2cb77ce0456b16673f10349362cc53218bba Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/27565 Reviewed-by: Gabe Black Maintainer: Gabe Black Tested-by: kokoro --- util/m5/src/command.cc | 3 +-- util/m5/src/command.hh | 2 +- util/m5/src/command.test.cc | 6 ++++-- util/m5/src/command/addsymbol.cc | 7 ++++--- util/m5/src/command/checkpoint.cc | 6 +++--- util/m5/src/command/dumpresetstats.cc | 7 ++++--- util/m5/src/command/dumpstats.cc | 7 ++++--- util/m5/src/command/exit.cc | 7 ++++--- util/m5/src/command/fail.cc | 7 ++++--- util/m5/src/command/initparam.cc | 8 +++++--- util/m5/src/command/loadsymbol.cc | 4 ++-- util/m5/src/command/readfile.cc | 7 ++++--- util/m5/src/command/resetstats.cc | 7 ++++--- util/m5/src/command/sum.cc | 7 ++++--- util/m5/src/command/writefile.cc | 5 +++-- 15 files changed, 51 insertions(+), 39 deletions(-) diff --git a/util/m5/src/command.cc b/util/m5/src/command.cc index d7dc03028..2527dc6ae 100644 --- a/util/m5/src/command.cc +++ b/util/m5/src/command.cc @@ -52,6 +52,5 @@ Command::run(const DispatchTable &dt, Args &args) if (num_args < cmd.minArgs || num_args > cmd.maxArgs) return false; - cmd.func(dt, args); - return true; + return cmd.func(dt, args); } diff --git a/util/m5/src/command.hh b/util/m5/src/command.hh index 8348d1116..7321d3973 100644 --- a/util/m5/src/command.hh +++ b/util/m5/src/command.hh @@ -46,7 +46,7 @@ class Command // The maximum number of arguments the command can handle. const int maxArgs; - using FuncType = void (*)(const DispatchTable &dt, Args &args); + using FuncType = bool (*)(const DispatchTable &dt, Args &args); // A function which processes command line arguments and passes them to // the underlying function through the dispatch table. FuncType func; diff --git a/util/m5/src/command.test.cc b/util/m5/src/command.test.cc index 342a42cd4..b29d648fd 100644 --- a/util/m5/src/command.test.cc +++ b/util/m5/src/command.test.cc @@ -40,18 +40,20 @@ class DispatchTable {}; DispatchTable dt; -void +bool do_test1(const DispatchTable &dt, Args &args) { ran_test1 = true; + return true; } bool ran_test2 = false; -void +bool do_test2(const DispatchTable &dt, Args &args) { ran_test2 = true; + return true; } TEST(CommandTest, OneCommandNoArgs) diff --git a/util/m5/src/command/addsymbol.cc b/util/m5/src/command/addsymbol.cc index dfd20d7c9..0ebd6b29a 100644 --- a/util/m5/src/command/addsymbol.cc +++ b/util/m5/src/command/addsymbol.cc @@ -29,20 +29,21 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_add_symbol(const DispatchTable &dt, Args &args) { uint64_t addr; if (!args.pop(addr)) - usage(); + return false; const std::string &symbol = args.pop(); (*dt.m5_add_symbol)(addr, symbol.c_str()); + + return true; } Command add_symbol = { diff --git a/util/m5/src/command/checkpoint.cc b/util/m5/src/command/checkpoint.cc index bd234a7ce..b16307cee 100644 --- a/util/m5/src/command/checkpoint.cc +++ b/util/m5/src/command/checkpoint.cc @@ -29,19 +29,19 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_checkpoint(const DispatchTable &dt, Args &args) { uint64_t ns_delay, ns_period; if (!args.pop(ns_delay, 0) || !args.pop(ns_period, 0)) - usage(); + return false; (*dt.m5_checkpoint)(ns_delay, ns_period); + return true; } Command checkpoint = { diff --git a/util/m5/src/command/dumpresetstats.cc b/util/m5/src/command/dumpresetstats.cc index e3a7cc60e..b5337afae 100644 --- a/util/m5/src/command/dumpresetstats.cc +++ b/util/m5/src/command/dumpresetstats.cc @@ -29,19 +29,20 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_dump_reset_stats(const DispatchTable &dt, Args &args) { uint64_t ns_delay, ns_period; if (!args.pop(ns_delay, 0) || !args.pop(ns_period, 0)) - usage(); + return false; (*dt.m5_dump_reset_stats)(ns_delay, ns_period); + + return true; } Command dump_reset_stats = { diff --git a/util/m5/src/command/dumpstats.cc b/util/m5/src/command/dumpstats.cc index eef3b383c..106f822a1 100644 --- a/util/m5/src/command/dumpstats.cc +++ b/util/m5/src/command/dumpstats.cc @@ -29,19 +29,20 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_dump_stats(const DispatchTable &dt, Args &args) { uint64_t ns_delay, ns_period; if (!args.pop(ns_delay, 0) || !args.pop(ns_period, 0)) - usage(); + return false; (*dt.m5_dump_stats)(ns_delay, ns_period); + + return true; } Command dump_stats = { diff --git a/util/m5/src/command/exit.cc b/util/m5/src/command/exit.cc index ea322ca02..4f8815c27 100644 --- a/util/m5/src/command/exit.cc +++ b/util/m5/src/command/exit.cc @@ -29,19 +29,20 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_exit(const DispatchTable &dt, Args &args) { uint64_t ns_delay; if (!args.pop(ns_delay, 0)) - usage(); + return false; (*dt.m5_exit)(ns_delay); + + return true; } Command exit_cmd = { diff --git a/util/m5/src/command/fail.cc b/util/m5/src/command/fail.cc index db21979ca..c34571c5c 100644 --- a/util/m5/src/command/fail.cc +++ b/util/m5/src/command/fail.cc @@ -29,19 +29,20 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_fail(const DispatchTable &dt, Args &args) { uint64_t ns_delay, code; if (!args.pop(code) || !args.pop(ns_delay, 0)) - usage(); + return false; (*dt.m5_fail)(ns_delay, code); + + return true; } Command fail_cmd = { diff --git a/util/m5/src/command/initparam.cc b/util/m5/src/command/initparam.cc index 3fe000c10..eb1fdff46 100644 --- a/util/m5/src/command/initparam.cc +++ b/util/m5/src/command/initparam.cc @@ -31,19 +31,21 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_initparam(const DispatchTable &dt, Args &args) { uint64_t key_str[2]; if (!args.pop(key_str, 2)) - usage(); + return false; + uint64_t val = (*dt.m5_init_param)(key_str[0], key_str[1]); std::cout << val; + + return true; } Command init_param = { diff --git a/util/m5/src/command/loadsymbol.cc b/util/m5/src/command/loadsymbol.cc index c55ae9905..794e53b47 100644 --- a/util/m5/src/command/loadsymbol.cc +++ b/util/m5/src/command/loadsymbol.cc @@ -29,15 +29,15 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_loadsymbol(const DispatchTable &dt, Args &args) { (*dt.m5_load_symbol)(); + return true; } Command load_symbol = { diff --git a/util/m5/src/command/readfile.cc b/util/m5/src/command/readfile.cc index 9295dfd95..92c39e25f 100644 --- a/util/m5/src/command/readfile.cc +++ b/util/m5/src/command/readfile.cc @@ -32,7 +32,6 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { @@ -62,13 +61,15 @@ read_file(const DispatchTable &dt, std::ostream &os) return offset; } -void +bool do_read_file(const DispatchTable &dt, Args &args) { if (args.size() > 0) - usage(); + return false; read_file(dt, std::cout); + + return true; } Command read_file_cmd = { diff --git a/util/m5/src/command/resetstats.cc b/util/m5/src/command/resetstats.cc index 2408fb334..cf51bfb5a 100644 --- a/util/m5/src/command/resetstats.cc +++ b/util/m5/src/command/resetstats.cc @@ -29,19 +29,20 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { -void +bool do_reset_stats(const DispatchTable &dt, Args &args) { uint64_t ns_delay, ns_period; if (!args.pop(ns_delay, 0) || !args.pop(ns_period, 0)) - usage(); + return false; (*dt.m5_reset_stats)(ns_delay, ns_period); + + return true; } Command reset_stats = { diff --git a/util/m5/src/command/sum.cc b/util/m5/src/command/sum.cc index c7d44b397..8b1b3d276 100644 --- a/util/m5/src/command/sum.cc +++ b/util/m5/src/command/sum.cc @@ -31,22 +31,23 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { // For testing purposes. -void +bool do_sum(const DispatchTable &dt, Args &args) { uint64_t a, b, c, d, e, f; if (!args.pop(a) || !args.pop(b) || !args.pop(c, 0) || !args.pop(d, 0) || !args.pop(e, 0) || !args.pop(f, 0)) - usage(); + return false; unsigned sum = (*dt.m5_sum)(a, b, c, d, e, f); std::cout << "Sum is " << sum << "." << std::endl; + + return true; } Command sum = { diff --git a/util/m5/src/command/writefile.cc b/util/m5/src/command/writefile.cc index 801b7af2a..bef193296 100644 --- a/util/m5/src/command/writefile.cc +++ b/util/m5/src/command/writefile.cc @@ -33,7 +33,6 @@ #include "args.hh" #include "command.hh" #include "dispatch_table.hh" -#include "usage.hh" namespace { @@ -75,13 +74,15 @@ write_file(const DispatchTable &dt, const std::string &filename, std::cerr << "Wrote " << offset << " bytes." << std::endl; } -void +bool do_write_file(const DispatchTable &dt, Args &args) { const std::string &filename = args.pop(); const std::string &host_filename = args.pop(filename); write_file(dt, filename, host_filename); + + return true; } Command write_file_cmd = { -- 2.30.2