Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve debugger threadsafety and consistency #10987

Merged
merged 7 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 18 additions & 24 deletions Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@
#include "thread/threadutil.h"
#include "profiler/profiler.h"

#include "Common/GraphicsContext.h"
#include "Core/Core.h"
#include "Core/Config.h"
#include "Core/Host.h"
#include "Core/MemMap.h"
#include "Core/SaveState.h"
#include "Core/System.h"
#include "Core/Debugger/Breakpoints.h"
#include "Core/MIPS/MIPS.h"
#include "Common/GraphicsContext.h"

#ifdef _WIN32
#include "Common/CommonWindows.h"
#include "Windows/InputDevice.h"
#endif

#include "Host.h"

#include "Core/Debugger/Breakpoints.h"

// Time until we stop considering the core active without user input.
// Should this be configurable? 2 hours currently.
Expand All @@ -54,7 +53,8 @@ static std::mutex m_hStepMutex;
static std::condition_variable m_InactiveCond;
static std::mutex m_hInactiveMutex;
static bool singleStepPending = false;
static std::set<Core_ShutdownFunc> shutdownFuncs;
static int steppingCounter = 0;
static std::set<CoreLifecycleFunc> shutdownFuncs;
static bool windowHidden = false;
static double lastActivity = 0.0;
static double lastKeepAwake = 0.0;
Expand All @@ -75,30 +75,19 @@ void Core_NotifyActivity() {
lastActivity = time_now_d();
}

void Core_ListenShutdown(Core_ShutdownFunc func) {
void Core_ListenLifecycle(CoreLifecycleFunc func) {
shutdownFuncs.insert(func);
}

void Core_NotifyShutdown() {
void Core_NotifyLifecycle(CoreLifecycle stage) {
for (auto it = shutdownFuncs.begin(); it != shutdownFuncs.end(); ++it) {
(*it)();
(*it)(stage);
}
}

void Core_ErrorPause() {
Core_UpdateState(CORE_ERROR);
}

void Core_Halt(const char *msg) {
Core_EnableStepping(true);
ERROR_LOG(CPU, "CPU HALTED : %s",msg);
_dbg_update_();
}

void Core_Stop() {
Core_UpdateState(CORE_POWERDOWN);
Core_NotifyShutdown();
m_StepCond.notify_one();
m_StepCond.notify_all();
}

bool Core_IsStepping() {
Expand Down Expand Up @@ -239,11 +228,11 @@ void Core_RunLoop(GraphicsContext *ctx) {

void Core_DoSingleStep() {
singleStepPending = true;
m_StepCond.notify_one();
m_StepCond.notify_all();
}

void Core_UpdateSingleStep() {
m_StepCond.notify_one();
m_StepCond.notify_all();
}

void Core_SingleStep() {
Expand All @@ -253,7 +242,7 @@ void Core_SingleStep() {
static inline void CoreStateProcessed() {
if (coreStatePending) {
coreStatePending = false;
m_InactiveCond.notify_one();
m_InactiveCond.notify_all();
}
}

Expand Down Expand Up @@ -334,10 +323,15 @@ void Core_EnableStepping(bool step) {
sleep_ms(1);
host->SetDebugMode(true);
Core_UpdateState(CORE_STEPPING);
steppingCounter++;
} else {
host->SetDebugMode(false);
coreState = CORE_RUNNING;
coreStatePending = false;
m_StepCond.notify_one();
m_StepCond.notify_all();
}
}

int Core_GetSteppingCounter() {
return steppingCounter;
}
23 changes: 15 additions & 8 deletions Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,28 @@ void UpdateRunLoop();

void Core_Run(GraphicsContext *ctx);
void Core_Stop();
void Core_ErrorPause();
// For platforms that don't call Core_Run
void Core_SetGraphicsContext(GraphicsContext *ctx);

void Core_RunRenderThreadFrame();

// called from gui
void Core_EnableStepping(bool step);
void Core_DoSingleStep();
void Core_UpdateSingleStep();

typedef void (* Core_ShutdownFunc)();
void Core_ListenShutdown(Core_ShutdownFunc func);
void Core_NotifyShutdown();
void Core_Halt(const char *msg);
// Changes every time we enter stepping.
int Core_GetSteppingCounter();

enum class CoreLifecycle {
STARTING,
// Note: includes failure cases. Guaranteed call after STARTING.
START_COMPLETE,
STOPPING,
// Guaranteed call after STOPPING.
STOPPED,
};

typedef void (* CoreLifecycleFunc)(CoreLifecycle stage);
void Core_ListenLifecycle(CoreLifecycleFunc func);
void Core_NotifyLifecycle(CoreLifecycle stage);

bool Core_IsStepping();

Expand Down
2 changes: 1 addition & 1 deletion Core/CoreTiming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ int RegisterEvent(const char *name, TimedCallback callback)
void AntiCrashCallback(u64 userdata, int cyclesLate)
{
ERROR_LOG(SAVESTATE, "Savestate broken: an unregistered event was called.");
Core_Halt("invalid timing events");
Core_EnableStepping(true);
}

void RestoreRegisterEvent(int event_type, const char *name, TimedCallback callback)
Expand Down
Loading