command: Allow running Python on failure for state introspection

Signed-off-by: gatecat <gatecat@ds0.me>
This commit is contained in:
gatecat 2021-09-16 20:56:00 +01:00
parent 67bd349e8f
commit 1e4f706ace
2 changed files with 16 additions and 1 deletions

View File

@ -122,6 +122,8 @@ po::options_description CommandHandler::getGeneralOptions()
general.add_options()("pre-place", po::value<std::vector<std::string>>(), "python file to run before placement"); general.add_options()("pre-place", po::value<std::vector<std::string>>(), "python file to run before placement");
general.add_options()("pre-route", po::value<std::vector<std::string>>(), "python file to run before routing"); general.add_options()("pre-route", po::value<std::vector<std::string>>(), "python file to run before routing");
general.add_options()("post-route", po::value<std::vector<std::string>>(), "python file to run after routing"); general.add_options()("post-route", po::value<std::vector<std::string>>(), "python file to run after routing");
general.add_options()("on-failure", po::value<std::vector<std::string>>(),
"python file to run in event of crash for design introspection");
#endif #endif
general.add_options()("json", po::value<std::string>(), "JSON design file to ingest"); general.add_options()("json", po::value<std::string>(), "JSON design file to ingest");
@ -184,6 +186,15 @@ po::options_description CommandHandler::getGeneralOptions()
return general; return general;
} }
namespace {
static CommandHandler *global_command_handler = nullptr;
void script_terminate_handler()
{
if (global_command_handler != nullptr)
global_command_handler->run_script_hook("on-failure");
}
}; // namespace
void CommandHandler::setupContext(Context *ctx) void CommandHandler::setupContext(Context *ctx)
{ {
if (ctx->settings.find(ctx->id("seed")) != ctx->settings.end()) if (ctx->settings.find(ctx->id("seed")) != ctx->settings.end())
@ -321,6 +332,10 @@ void CommandHandler::setupContext(Context *ctx)
int CommandHandler::executeMain(std::unique_ptr<Context> ctx) int CommandHandler::executeMain(std::unique_ptr<Context> ctx)
{ {
if (vm.count("on-failure")) {
global_command_handler = this;
std::set_terminate(script_terminate_handler);
}
if (vm.count("test")) { if (vm.count("test")) {
ctx->archcheck(); ctx->archcheck();
return 0; return 0;

View File

@ -39,6 +39,7 @@ class CommandHandler
int exec(); int exec();
void load_json(Context *ctx, std::string filename); void load_json(Context *ctx, std::string filename);
void clear(); void clear();
void run_script_hook(const std::string &name);
protected: protected:
virtual void setupArchContext(Context *ctx) = 0; virtual void setupArchContext(Context *ctx) = 0;
@ -55,7 +56,6 @@ class CommandHandler
void setupContext(Context *ctx); void setupContext(Context *ctx);
int executeMain(std::unique_ptr<Context> ctx); int executeMain(std::unique_ptr<Context> ctx);
po::options_description getGeneralOptions(); po::options_description getGeneralOptions();
void run_script_hook(const std::string &name);
void printFooter(); void printFooter();
protected: protected: