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 <gabeblack@google.com> Maintainer: Gabe Black <gabeblack@google.com> Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
Reference in New Issue
Block a user