From 6fa8541aed68e72fd4f1cdeb13a46ec95547b275 Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Fri, 3 Jul 2009 14:22:50 +0000 Subject: [PATCH] - Pushed debugstate into debug.h - When an error occurs, manipulate the execution stack before error() opens the console inside getDebugger(), like FreeSCI did. Added another method for obtaining the SCI console for use by the engine itself. svn-id: r42062 --- engines/sci/console.cpp | 3 +-- engines/sci/console.h | 1 + engines/sci/debug.h | 15 +++++++++------ engines/sci/engine/grammar.cpp | 4 ++-- engines/sci/engine/kevent.cpp | 6 ++---- engines/sci/engine/kmisc.cpp | 2 -- engines/sci/engine/vm.cpp | 18 +++--------------- engines/sci/gfx/operations.cpp | 5 +++-- engines/sci/sci.cpp | 13 +++++++++++++ engines/sci/sci.h | 1 + engines/sci/tools.cpp | 2 +- engines/sci/vocabulary.cpp | 4 ++-- 12 files changed, 38 insertions(+), 36 deletions(-) diff --git a/engines/sci/console.cpp b/engines/sci/console.cpp index 002acbae863..bf919c3a25f 100644 --- a/engines/sci/console.cpp +++ b/engines/sci/console.cpp @@ -50,7 +50,6 @@ int g_debug_sleeptime_factor = 1; int g_debug_simulated_key = 0; bool g_debug_track_mouse_clicks = false; bool g_debug_weak_validations = true; -extern DebugState debugState; Console::Console(SciEngine *vm) : GUI::Debugger() { _vm = vm; @@ -1598,7 +1597,7 @@ bool Console::cmdGCObjects(int argc, const char **argv) { void _print_address(void * _, reg_t addr) { if (addr.segment) - ((SciEngine *)g_engine)->getDebugger()->DebugPrintf(" %04x:%04x\n", PRINT_REG(addr)); + ((SciEngine *)g_engine)->getSciDebugger()->DebugPrintf(" %04x:%04x\n", PRINT_REG(addr)); } bool Console::cmdGCShowReachable(int argc, const char **argv) { diff --git a/engines/sci/console.h b/engines/sci/console.h index c45202de16a..84a8d2f942e 100644 --- a/engines/sci/console.h +++ b/engines/sci/console.h @@ -154,6 +154,7 @@ private: private: SciEngine *_vm; + bool _mouseVisible; }; } // End of namespace Sci diff --git a/engines/sci/debug.h b/engines/sci/debug.h index 60bf938300c..fbd98e0d6f2 100644 --- a/engines/sci/debug.h +++ b/engines/sci/debug.h @@ -28,12 +28,6 @@ namespace Sci { -// Various global variables used for debugging are declared here -extern int g_debug_sleeptime_factor; -extern int g_debug_simulated_key; -extern bool g_debug_track_mouse_clicks; -extern bool g_debug_weak_validations; - enum DebugSeeking { kDebugSeekNothing = 0, kDebugSeekCallk = 1, // Step forward until callk is found @@ -50,6 +44,8 @@ struct DebugState { int runningStep; // Set to > 0 to allow multiple stepping int seekLevel; // Used for seekers that want to check their exec stack depth int seekSpecial; // Used for special seeks + int old_pc_offset; + StackPtr old_sp; reg_t *p_pc; StackPtr *p_sp; StackPtr *p_pp; @@ -61,6 +57,13 @@ struct DebugState { int *p_var_max; // May be NULL even in valid state! }; +// Various global variables used for debugging are declared here +extern int g_debug_sleeptime_factor; +extern int g_debug_simulated_key; +extern bool g_debug_track_mouse_clicks; +extern bool g_debug_weak_validations; +extern DebugState debugState; + } // End of namespace Sci #endif diff --git a/engines/sci/engine/grammar.cpp b/engines/sci/engine/grammar.cpp index 275a31a0d45..e75441432d8 100644 --- a/engines/sci/engine/grammar.cpp +++ b/engines/sci/engine/grammar.cpp @@ -351,7 +351,7 @@ parse_rule_list_t *Vocabulary::buildGNF(bool verbose) { int ntrules_nr; parse_rule_list_t *ntlist = NULL; parse_rule_list_t *tlist, *new_tlist; - GUI::Debugger *con = ((SciEngine *)g_engine)->getDebugger(); + Console *con = ((SciEngine *)g_engine)->getSciDebugger(); for (uint i = 1; i < _parserBranches.size(); i++) { // branch rule 0 is treated specially parse_rule_t *rule = _vbuild_rule(&_parserBranches[i]); @@ -477,7 +477,7 @@ static int _vbpt_write_subexpression(parse_tree_node_t *nodes, int *pos, parse_r } int Vocabulary::parseGNF(parse_tree_node_t *nodes, const ResultWordList &words, bool verbose) { - GUI::Debugger *con = ((SciEngine *)g_engine)->getDebugger(); + Console *con = ((SciEngine *)g_engine)->getSciDebugger(); // Get the start rules: parse_rule_list_t *work = _vocab_clone_rule_list_by_id(_parserRules, _parserBranches[0].data[1]); parse_rule_list_t *results = NULL; diff --git a/engines/sci/engine/kevent.cpp b/engines/sci/engine/kevent.cpp index 3b37506e6c2..e40368a5c01 100644 --- a/engines/sci/engine/kevent.cpp +++ b/engines/sci/engine/kevent.cpp @@ -33,8 +33,6 @@ namespace Sci { -extern DebugState debugState; - #define SCI_VARIABLE_GAME_SPEED 3 reg_t kGetEvent(EngineState *s, int funct_nr, int argc, reg_t *argv) { @@ -112,7 +110,7 @@ reg_t kGetEvent(EngineState *s, int funct_nr, int argc, reg_t *argv) { // track left buttton clicks, if requested if (e.type == SCI_EVT_MOUSE_PRESS && e.data == 1 && g_debug_track_mouse_clicks) { - ((SciEngine *)g_engine)->getDebugger()->DebugPrintf("Mouse clicked at %d, %d\n", + ((SciEngine *)g_engine)->getSciDebugger()->DebugPrintf("Mouse clicked at %d, %d\n", s->gfx_state->pointer_pos.x, s->gfx_state->pointer_pos.y); } @@ -143,7 +141,7 @@ reg_t kGetEvent(EngineState *s, int funct_nr, int argc, reg_t *argv) { debugState.stopOnEvent = false; // A SCI event occured, and we have been asked to stop, so open the debug console - GUI::Debugger *con = ((Sci::SciEngine*)g_engine)->getDebugger(); + Console *con = ((Sci::SciEngine*)g_engine)->getSciDebugger(); con->DebugPrintf("SCI event occured: "); switch (e.type) { case SCI_EVT_QUIT: diff --git a/engines/sci/engine/kmisc.cpp b/engines/sci/engine/kmisc.cpp index 99665bae7d0..98fba97f0e3 100644 --- a/engines/sci/engine/kmisc.cpp +++ b/engines/sci/engine/kmisc.cpp @@ -93,8 +93,6 @@ reg_t kFlushResources(EngineState *s, int funct_nr, int argc, reg_t *argv) { return s->r_acc; } -extern DebugState debugState; - reg_t kSetDebug(EngineState *s, int funct_nr, int argc, reg_t *argv) { sciprintf("Debug mode activated\n"); diff --git a/engines/sci/engine/vm.cpp b/engines/sci/engine/vm.cpp index 30b2f07edd6..44b119ab464 100644 --- a/engines/sci/engine/vm.cpp +++ b/engines/sci/engine/vm.cpp @@ -49,7 +49,6 @@ reg_t NULL_REG = {0, 0}; int script_abort_flag = 0; // Set to 1 to abort execution. Set to 2 to force a replay afterwards // FIXME: Avoid non-const global vars int script_step_counter = 0; // Counts the number of steps executed // FIXME: Avoid non-const global vars int script_gc_interval = GC_INTERVAL; // Number of steps in between gcs // FIXME: Avoid non-const global vars -extern DebugState debugState; static bool breakpointFlag = false; // FIXME: Avoid non-const global vars static reg_t _dummy_register; // FIXME: Avoid non-const global vars @@ -606,14 +605,12 @@ void run_vm(EngineState *s, int restoring) { while (1) { byte opcode; - int old_pc_offset; - StackPtr old_sp; byte opnumber; int var_type; // See description below int var_number; - old_pc_offset = xs->addr.pc.offset; - old_sp = xs->sp; + debugState.old_pc_offset = xs->addr.pc.offset; + debugState.old_sp = xs->sp; if (s->_executionStackPosChanged) { Script *scr; @@ -1424,16 +1421,7 @@ void run_vm(EngineState *s, int restoring) { opnumber); } //#endif - -#if 0 - if (script_error_flag) { - debugState.runningStep = 0; // Stop multiple execution - debugState.seeking = 0; // Stop special seeks - xs->addr.pc.offset = old_pc_offset; - xs->sp = old_sp; - } else -#endif - ++script_step_counter; + ++script_step_counter; } } diff --git a/engines/sci/gfx/operations.cpp b/engines/sci/gfx/operations.cpp index 7496c55868a..82b5e781176 100644 --- a/engines/sci/gfx/operations.cpp +++ b/engines/sci/gfx/operations.cpp @@ -1418,8 +1418,9 @@ static sci_event_t scummvm_get_event(GfxDriver *drv) { // Debug console if (ev.kbd.flags == Common::KBD_CTRL && ev.kbd.keycode == Common::KEYCODE_d) { // Open debug console - ((Sci::SciEngine*)g_engine)->getDebugger()->attach(); - ((Sci::SciEngine*)g_engine)->getDebugger()->onFrame(); + Console *con = ((Sci::SciEngine*)g_engine)->getSciDebugger(); + con->attach(); + con->onFrame(); // Clear keyboard event input.type = SCI_EVT_NONE; diff --git a/engines/sci/sci.cpp b/engines/sci/sci.cpp index f58d729bf64..aa9e2415f5c 100644 --- a/engines/sci/sci.cpp +++ b/engines/sci/sci.cpp @@ -29,6 +29,7 @@ #include "engines/advancedDetector.h" #include "sci/sci.h" +#include "sci/debug.h" #include "sci/console.h" #include "sci/engine/state.h" @@ -254,7 +255,19 @@ Common::Error SciEngine::run() { return Common::kNoError; } +// Invoked by error() when a severe error occurs GUI::Debugger *SciEngine::getDebugger() { + ExecStack *xs = &(_gamestate->_executionStack.back()); + debugState.runningStep = 0; // Stop multiple execution + debugState.seeking = kDebugSeekNothing; // Stop special seeks + xs->addr.pc.offset = debugState.old_pc_offset; + xs->sp = debugState.old_sp; + + return _console; +} + +// Used to obtain the engine's console in order to print messages to it +Console *SciEngine::getSciDebugger() { return _console; } diff --git a/engines/sci/sci.h b/engines/sci/sci.h index 91f491fe718..b68301546db 100644 --- a/engines/sci/sci.h +++ b/engines/sci/sci.h @@ -142,6 +142,7 @@ public: virtual Common::Error run(); void pauseEngineIntern(bool pause); virtual GUI::Debugger *getDebugger(); + Console *getSciDebugger(); const char* getGameID() const; int getResourceVersion() const; diff --git a/engines/sci/tools.cpp b/engines/sci/tools.cpp index 589ce298d7f..d0e11aca091 100644 --- a/engines/sci/tools.cpp +++ b/engines/sci/tools.cpp @@ -70,7 +70,7 @@ void sciprintf(const char *fmt, ...) { // Display the result suitably if (g_redirect_sciprintf_to_gui) - ((SciEngine *)g_engine)->getDebugger()->DebugPrintf("%s", buf); + ((SciEngine *)g_engine)->getSciDebugger()->DebugPrintf("%s", buf); printf("%s", buf); free(buf); diff --git a/engines/sci/vocabulary.cpp b/engines/sci/vocabulary.cpp index c877053060d..6616ac362d1 100644 --- a/engines/sci/vocabulary.cpp +++ b/engines/sci/vocabulary.cpp @@ -438,7 +438,7 @@ bool Vocabulary::tokenizeString(ResultWordList &retval, const char *sentence, ch void Vocabulary::printSuffixes() const { char word_buf[256], alt_buf[256]; - GUI::Debugger *con = ((SciEngine *)g_engine)->getDebugger(); + Console *con = ((SciEngine *)g_engine)->getSciDebugger(); int i = 0; for (SuffixList::const_iterator suf = _parserSuffixes.begin(); suf != _parserSuffixes.end(); ++suf) { @@ -453,7 +453,7 @@ void Vocabulary::printSuffixes() const { } void Vocabulary::printParserWords() const { - GUI::Debugger *con = ((SciEngine *)g_engine)->getDebugger(); + Console *con = ((SciEngine *)g_engine)->getSciDebugger(); int j = 0; for (WordMap::iterator i = _parserWords.begin(); i != _parserWords.end(); ++i) {