]> git.tdb.fi Git - gldbg.git/blobdiff - source/commandinterpreter.cpp
Fix several problems reported by valgrind
[gldbg.git] / source / commandinterpreter.cpp
index c51d0a2b223a326ff297be9d37e6fb46df573eae..b8a10a99d03cb0f30a4048c4754f370d42f09f8b 100644 (file)
@@ -1,57 +1,62 @@
-/* $Id$
-
-This file is part of gldbg
-Copyright © 2009  Mikko Rasa, Mikkosoft Productions
-Distributed under the GPL
-*/
-
+#include <stdexcept>
+#include <cstdlib>
 #include <signal.h>
-#include <msp/core/except.h>
-#include <msp/io/file.h>
-#include <msp/io/print.h>
-#include <msp/strings/lexicalcast.h>
-#include <msp/strings/utils.h>
+#include <readline/readline.h>
+#include "breakpoint.h"
 #include "commandinterpreter.h"
+#include "enums.h"
+#include "functions.h"
 #include "gldbg.h"
 #include "tracer.h"
 
 using namespace std;
-using namespace Msp;
 
 CommandInterpreter::CommandInterpreter(GlDbg &d):
        gldbg(d)
 {
-       commands["help"] = Command(&CommandInterpreter::cmd_help,
-               "Provides help on commands",
-               "help\n"
-               "  Displays a list of commands\n\n"
-               "help COMMAND\n"
-               "  Gives detailed information on a command\n");
-       commands["exit"] = Command(&CommandInterpreter::cmd_exit,
-               "Ends the debugging session");
-
-       commands["run"] = Command(&CommandInterpreter::cmd_run,
-               "Starts the program");
-       commands["continue"] = Command(&CommandInterpreter::cmd_continue,
-               "Resumes program execution");
-       commands["kill"] = Command(&CommandInterpreter::cmd_kill,
-               "Terminates the program immediately");
-       commands["signal"] = Command(&CommandInterpreter::cmd_signal,
-               "Resumes execution with a signal",
-               "signal NUM\n"
-               "signal NAME\n"
-               "  Sends the signal identified by NUM or NAME to the program and resumes\n"
-               "  execution.  Currently recognized signal names are HUP, INT, TERM, SEGV\n"
-               "  and TERM.\n");
-
-       commands["trace"] = Command(&CommandInterpreter::cmd_trace,
-               "Traces GL function calls",
-               "trace >FILE\n"
-               "  Send trace output to FILE.  As a special case, - means stdout.\n\n"
-               "trace {off|on}\n"
-               "  Temporarily suspend or resume trace without closing the file.\n\n"
-               "trace end\n"
-               "  Terminate trace, closing the file.\n");
+       register_command("help", this, &CommandInterpreter::cmd_help)
+               .set_help("Provides help on commands",
+                       "help\n"
+                       "  Displays a list of commands\n\n"
+                       "help COMMAND\n"
+                       "  Gives detailed information on a command\n");
+       register_command("exit", this, &CommandInterpreter::cmd_exit)
+               .set_help("Ends the debugging session");
+       commands["quit"] = new CommandAlias(commands["exit"]);
+
+       register_command("run", this, &CommandInterpreter::cmd_run)
+               .set_help("Starts the program");
+       register_command("break", this, &CommandInterpreter::cmd_break)
+               .set_help("Sets a breakpoint",
+                       "break FUNC\n"
+                       "  Makes program execution stop at FUNC\n");
+       register_command("unbreak", this, &CommandInterpreter::cmd_unbreak)
+               .set_help("Removes a breakpoint",
+                       "unbreak FUNC\n"
+                       "  Makes program execution no longer stop at FUNC\n");
+       register_command("next", this, &CommandInterpreter::cmd_next)
+               .set_help("Resumes program execution until the next function call");
+       commands["step"] = new CommandAlias(commands["next"]);
+       register_command("finish", this, &CommandInterpreter::cmd_finish)
+               .set_help("Resumes program execution until the next function return");
+       register_command("continue", this, &CommandInterpreter::cmd_continue)
+               .set_help("Resumes program execution");
+       register_command("kill", this, &CommandInterpreter::cmd_kill)
+               .set_help("Terminates the program immediately");
+       register_command("signal", this, &CommandInterpreter::cmd_signal)
+               .set_help("Resumes execution with a signal",
+                       "signal NUM\n"
+                       "signal NAME\n"
+                       "  Sends the signal identified by NUM or NAME to the program and resumes\n"
+                       "  execution.  Currently recognized signal names are HUP, INT, TERM, SEGV\n"
+                       "  and TERM.\n");
+
+}
+
+CommandInterpreter::~CommandInterpreter()
+{
+       for(CommandMap::iterator i=commands.begin(); i!=commands.end(); ++i)
+               delete i->second;
 }
 
 void CommandInterpreter::execute(const string &cmd)
@@ -60,36 +65,42 @@ void CommandInterpreter::execute(const string &cmd)
        string name = cmd.substr(0, space);
        CommandMap::const_iterator i = commands.lower_bound(name);
        if(i==commands.end() || i->first.compare(0, name.size(), name))
-               throw KeyError("Unknown command", name);
+               throw runtime_error("Unknown command "+name);
        if(i->first!=name)
        {
                CommandMap::const_iterator j = i;
                if((++j)!=commands.end() && !j->first.compare(0, name.size(), name))
-                       throw KeyError("Ambiguous command", name);
+                       throw runtime_error("Ambiguous command "+name);
        }
 
        string args;
        if(space!=string::npos)
                args = cmd.substr(space+1);
 
-       (this->*(i->second.func))(args);
+       i->second->execute(args);
 }
 
 void CommandInterpreter::cmd_help(const string &args)
 {
        if(args.empty())
        {
-               for(map<string, Command>::const_iterator i=commands.begin(); i!=commands.end(); ++i)
-                       IO::print("%-10s : %s\n", i->first, i->second.description);
+               for(CommandMap::const_iterator i=commands.begin(); i!=commands.end(); ++i)
+                       if(!dynamic_cast<const CommandAlias *>(i->second))
+                               printf("%-10s : %s\n", i->first.c_str(), i->second->get_description().c_str());
        }
        else
        {
-               map<string, Command>::const_iterator i = commands.find(args);
+               CommandMap::const_iterator i = commands.find(args);
                if(i==commands.end())
-                       throw KeyError("Unknown command", args);
-               IO::print("%s : %s\n", i->first, i->second.description);
-               if(!i->second.help.empty())
-                       IO::print("\n%s", i->second.help);
+                       throw runtime_error("Unknown command "+args);
+
+               const Command *cmd = i->second;
+               while(const CommandAlias *alias = dynamic_cast<const CommandAlias *>(cmd))
+                       cmd = alias->get_target();
+
+               printf("%s : %s\n", i->first.c_str(), cmd->get_description().c_str());
+               if(!cmd->get_help().empty())
+                       printf("\n%s", cmd->get_help().c_str());
        }
 }
 
@@ -98,8 +109,39 @@ void CommandInterpreter::cmd_run(const string &)
        gldbg.launch();
 }
 
+void CommandInterpreter::cmd_break(const string &args)
+{
+       unsigned short func = get_function(args.c_str());
+       if(!func)
+               throw runtime_error("Unknown function");
+
+       gldbg.set_breakpoint(func, BREAK_CALL, 0);
+}
+
+void CommandInterpreter::cmd_unbreak(const string &args)
+{
+       unsigned short func = get_function(args.c_str());
+       if(!func)
+               throw runtime_error("Unknown function");
+
+       gldbg.clear_breakpoint(func, BREAK_CALL, 0);
+}
+
+void CommandInterpreter::cmd_next(const string &)
+{
+       gldbg.set_breakpoint(0, BREAK_CALL, 0);
+       gldbg.get_process().resume();
+}
+
+void CommandInterpreter::cmd_finish(const string &)
+{
+       gldbg.set_breakpoint(0, BREAK_RETURN, 0);
+       gldbg.get_process().resume();
+}
+
 void CommandInterpreter::cmd_continue(const string &)
 {
+       printf("Continuing.\n");
        gldbg.get_process().resume();
 }
 
@@ -116,10 +158,13 @@ void CommandInterpreter::cmd_signal(const string &args)
                sig = SIGSEGV;
        else if(args=="TERM" || args=="SIGTERM")
                sig = SIGTERM;
-       else if(isnumrc(args))
-               sig = lexical_cast<unsigned>(args);
        else
-               throw InvalidParameterValue("Invalid signal specification");
+       {
+               char *end;
+               sig = strtoul(args.c_str(), &end, 0);
+               if(end && *end)
+                       throw runtime_error("Invalid signal specification");
+       }
        gldbg.get_process().resume(sig);
 }
 
@@ -130,58 +175,40 @@ void CommandInterpreter::cmd_kill(const string &)
 
 void CommandInterpreter::cmd_exit(const string &)
 {
-       gldbg.quit();
-}
-
-void CommandInterpreter::cmd_trace(const string &args)
-{
-       Tracer &tracer = gldbg.get_tracer();
-       if(args[0]=='>')
+       if(gldbg.get_process().get_state()!=Process::INACTIVE)
        {
-               string fn = args.substr(1);
-               if(fn=="-")
+               printf("Program is still running.  Kill it?\n");
+               char *answer = readline("[y/n] ");
+               if(answer[0]=='y')
                {
-                       tracer.set_output(IO::cout);
-                       IO::print("Tracing to stdout\n");
+                       gldbg.get_process().kill();
+                       gldbg.quit(true);
                }
                else
-               {
-                       tracer.set_output(new IO::File(fn, IO::M_WRITE));
-                       IO::print("Tracing to %s\n", fn);
-               }
+                       printf("Not confirmed.\n");
        }
        else
-       {
-               if(args=="on")
-               {
-                       tracer.enable();
-                       IO::print("Tracing enabled\n");
-               }
-               else if(args=="off")
-               {
-                       tracer.disable();
-                       IO::print("Tracing disabled\n");
-               }
-               else if(args=="end")
-               {
-                       tracer.set_output(0);
-                       IO::print("Tracing terminated\n");
-               }
-       }
+               gldbg.quit(false);
 }
 
 
-CommandInterpreter::Command::Command():
-       func(0)
-{ }
+void CommandInterpreter::Command::set_help(const string &d)
+{
+       description = d;
+}
+
+void CommandInterpreter::Command::set_help(const string &d, const string &h)
+{
+       description = d;
+       help = h;
+}
 
-CommandInterpreter::Command::Command(Func f, const string &d):
-       func(f),
-       description(d)
-{ }
 
-CommandInterpreter::Command::Command(Func f, const string &d, const string &h):
-       func(f),
-       description(d),
-       help(h)
+CommandInterpreter::CommandAlias::CommandAlias(Command *t):
+       target(t)
 { }
+
+void CommandInterpreter::CommandAlias::execute(const string &a)
+{
+       target->execute(a);
+}