/* $Id$
This file is part of gldbg
-Copyright © 2009-2010 Mikko Rasa, Mikkosoft Productions
+Copyright © 2009-2011 Mikko Rasa, Mikkosoft Productions
Distributed under the GPL
*/
#include <algorithm>
+#include <stdexcept>
#include <cstdlib>
+#include <climits>
#include <fcntl.h>
#include <signal.h>
+#include <unistd.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <readline/readline.h>
-#include <msp/core/except.h>
-#include <msp/fs/dir.h>
-#include <msp/io/print.h>
-#include <msp/strings/lexicalcast.h>
#include "functions.h"
#include "gldbg.h"
#include "gldecoder.h"
#include "packet.h"
+#include "strformat.h"
#include "tool.h"
using namespace std;
-using namespace Msp;
-Application::RegApp<GlDbg> GlDbg::reg;
+GlDbg *GlDbg::instance = 0;
GlDbg::GlDbg(int argc, char **argv):
cmd_interp(*this),
stop_reason(0),
current_break(0)
{
- FS::Path libdir = FS::get_sys_lib_dir(argv[0], "gldbg");
- process.setenv("LD_PRELOAD", (libdir/"glwrap.so").str().c_str());
+ instance = this;
+
+ char buf[PATH_MAX];
+ unsigned len = readlink("/proc/self/exe", buf, sizeof(buf));
+ string exe(buf, len);
+ string::size_type slash = exe.rfind('/');
+ process.setenv("LD_PRELOAD", (exe.substr(0, slash+1)+"glwrap.so"));
const list<Tool::Factory *> &factories = Tool::get_factories();
for(list<Tool::Factory *>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
int GlDbg::main()
{
- catch_signal(SIGINT);
- catch_signal(SIGCHLD);
- set_loop_mode(TICK_BUSY);
+ signal(SIGINT, &sighandler);
+ signal(SIGCHLD, &sighandler);
- IO::print("GLdbg 0.0\n");
- IO::print("Copyright © 2009-2010 Mikkosoft Productions\n");
- IO::print("Type \"help\" for a list of commands\n");
+ printf("GLdbg 0.0\n");
+ printf("Copyright © 2009-2010 Mikkosoft Productions\n");
+ printf("Type \"help\" for a list of commands\n");
- Application::main();
+ while(1)
+ tick();
return 0;
}
void GlDbg::launch()
{
if(process.get_state()!=Process::INACTIVE)
- throw InvalidState("Program is already running");
+ throw runtime_error("Program is already running");
int fds[2];
socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
int flags = fcntl(sock_fd, F_GETFD);
fcntl(sock_fd, F_SETFD, flags|FD_CLOEXEC);
- process.setenv("GLWRAP_FD", lexical_cast(fds[1]));
- process.setenv("GLWRAP_CTRL_FD", lexical_cast(fds[1]));
+ process.setenv("GLWRAP_FD", strformat("%d", fds[1]));
+ process.setenv("GLWRAP_CTRL_FD", strformat("%d", fds[1]));
process.launch();
close(fds[1]);
void GlDbg::quit(bool force)
{
if(!force && process.get_state()!=Process::INACTIVE)
- throw InvalidState("Program is still running");
+ throw runtime_error("Program is still running");
exit(0);
}
if(stop_reason)
{
if(stop_reason==0x100)
- IO::print("Target process exited normally\n");
+ printf("Target process exited normally\n");
else if((stop_reason>>8)==1)
- IO::print("Target process exited with status %d\n", stop_reason&0xFF);
+ printf("Target process exited with status %d\n", stop_reason&0xFF);
else if((stop_reason>>8)==2)
- IO::print("Target process terminated with signal %d\n", stop_reason&0xFF);
+ printf("Target process terminated with signal %d\n", stop_reason&0xFF);
else if((stop_reason>>8)==3)
- IO::print("Target process stopped by signal %d\n", stop_reason&0xFF);
+ printf("Target process stopped by signal %d\n", stop_reason&0xFF);
stop_reason = 0;
}
{
cmd_interp.execute(line);
}
- catch(const Exception &e)
+ catch(const exception &e)
{
- IO::print("%s\n", e.what());
+ printf("%s\n", e.what());
}
free(line);
}
}
if(announce)
- IO::print("Breakpoint: %s\n", get_function_name(func));
+ printf("Breakpoint: %s\n", get_function_name(func));
}
for(ToolList::iterator i=tools.begin(); i!=tools.end(); ++i)
void GlDbg::sighandler(int sig)
{
if(sig==SIGCHLD)
- got_sigchld = true;
+ instance->got_sigchld = true;
}