Skip to content

Commit

Permalink
Improve segfault handling with better error messages, and add segfaul…
Browse files Browse the repository at this point in the history
…t command (simulation only) for testing
  • Loading branch information
AgalmicVentures committed Dec 17, 2020
1 parent 777f5db commit 5b4ee9a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Werk/Application/ApplicationContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <unistd.h>

#include "Werk/Commands/CommandAlias.hpp"
#include "Werk/Commands/SegfaultCommand.hpp"
#include "Werk/Commands/WriteCommandLogAction.hpp"
#include "Werk/Console/ConsoleCommandReceiver.hpp"
#include "Werk/Console/IpcConsoleServer.hpp"
Expand Down Expand Up @@ -231,6 +232,12 @@ ApplicationContext::ApplicationContext(const std::string &configPath) :
_commandManager->add("quit", new ActionCommand(
quitAction,
"Quits the application cleanly."));

//Some commands are only available in simulation
if (_simulation) {
_commandManager->add("segfault", new SegfaultCommand(_log));
}

_log->logRaw(LogLevel::SUCCESS, "<CommandManager> Initialized.");

//Setup command aliases configured like so:
Expand Down Expand Up @@ -385,7 +392,7 @@ int ApplicationContext::run(Action *mainAction)
"Number of times the main thread can miss the watchdog");
//TODO: configurable watchdog action
Watchdog *watchdog = new Watchdog("Watchdog", &_backgroundThread.backgroundClock(),
new LogAction("WatchdogWarning", new StringLoggable("Main thread missed watchdog!", LogLevel::WARNING), _log),
new LogAction("WatchdogWarning", new StringLoggable("Main thread missed watchdog timer! It may be hung or dead.", LogLevel::ERROR), _log),
watchdogInterval, watchdogAllowedMisses);

if (0 != watchdogInterval) {
Expand Down
57 changes: 57 additions & 0 deletions src/Werk/Commands/SegfaultCommand.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

/*
* Copyright (c) 2015-2020 Agalmic Ventures LLC (www.agalmicventures.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

#pragma once

#include <signal.h>

#include "Werk/Logging/Log.hpp"

#include "Command.hpp"

namespace werk
{

/**
* Command to intentionally cause a segfault -- useful for debugging the handler.
*/
class SegfaultCommand : public Command
{
public:

SegfaultCommand(Log *log) :
Command("Segfaults the program (useful for testing)."),
_log(log) { }

bool execute(const std::vector<std::string> &arguments) override {
//Join all but the first argument since the command name needn't be echoed
_log->logRaw(LogLevel::WARNING, "Intentionally segfaulting...");
raise(SIGSEGV);
return true;
}

private:
Log *_log;
};

}
2 changes: 2 additions & 0 deletions src/Werk/OS/Signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static void handleBusError(int /*signal*/, siginfo_t *info, void * /*context*/)
<< "Cause: " << cause << "(" << info->si_code << ")" << std::endl;

//Sleep before aborting to allow logging to finish
std::cout << std::endl << "Sleeping 5 to allow other threads to finish (e.g. logging)..." << std::endl;
sleep(5);
std::abort();
}
Expand All @@ -67,6 +68,7 @@ static void handleSegfault(int /*signal*/, siginfo_t *info, void * /*context*/)
<< "Cause: " << cause << "(" << info->si_code << ")" << std::endl;

//Sleep before aborting to allow logging to finish
std::cout << std::endl << "Sleeping 5 to allow other threads to finish (e.g. logging)..." << std::endl;
sleep(5);
std::abort();
}
Expand Down

0 comments on commit 5b4ee9a

Please sign in to comment.