00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SIM_GDB_H
00028 #define SIM_GDB_H
00029
00030 #include "config.h"
00031
00032 #if defined(HAVE_SYS_MINGW) || defined(_MSC_VER)
00033 # include <winsock2.h>
00034 # undef GetCurrentTime // defined by winbase.h, clashes with SystemClock::GetCurrentTime()
00035 # include <sys/types.h>
00036 #else
00037 # include <sys/socket.h>
00038 # include <sys/types.h>
00039 # include <netinet/in.h>
00040 # include <netinet/tcp.h>
00041 # include <arpa/inet.h>
00042 #endif
00043
00044 #include <vector>
00045 #include "avrdevice.h"
00046 #include "types.h"
00047 #include "simulationmember.h"
00048
00049 #define MAX_BUF 400
00050
00051
00052
00053 #define GDB_SIGHUP 1 // Hangup (POSIX).
00054 #define GDB_SIGINT 2 // Interrupt (ANSI).
00055 #define GDB_SIGILL 4 // Illegal instruction (ANSI).
00056 #define GDB_SIGTRAP 5 // Trace trap (POSIX).
00057
00059 class GdbServerSocket {
00060 public:
00061
00062 virtual void Close(void)=0;
00063 virtual int ReadByte(void)=0;
00064 virtual void Write(const void* buf, size_t count)=0;
00065 virtual void SetBlockingMode(int mode)=0;
00066 virtual bool Connect(void)=0;
00067 virtual void CloseConnection(void)=0;
00068 };
00069
00070 #if defined(HAVE_SYS_MINGW) || defined(_MSC_VER)
00071
00073 class GdbServerSocketMingW: public GdbServerSocket {
00074
00075 private:
00076 static void Start();
00077 static void End();
00078 static int socketCount;
00079 SOCKET _socket;
00080 SOCKET _conn;
00081
00082 public:
00083 GdbServerSocketMingW(int port);
00084 ~GdbServerSocketMingW();
00085 virtual void Close(void);
00086 virtual int ReadByte(void);
00087 virtual void Write(const void* buf, size_t count);
00088 virtual void SetBlockingMode(int mode);
00089 virtual bool Connect(void);
00090 virtual void CloseConnection(void);
00091 };
00092
00093 #else
00094
00096 class GdbServerSocketUnix: public GdbServerSocket {
00097 private:
00098 int sock;
00099 int conn;
00100 struct sockaddr_in address[1];
00101
00102 public:
00103 GdbServerSocketUnix(int port);
00104 ~GdbServerSocketUnix();
00105 virtual void Close(void);
00106 virtual int ReadByte(void);
00107 virtual void Write(const void* buf, size_t count);
00108 virtual void SetBlockingMode(int mode);
00109 virtual bool Connect(void);
00110 virtual void CloseConnection(void);
00111 };
00112
00113 #endif
00114
00116 class GdbServer: public SimulationMember {
00117
00118 protected:
00119 static std::vector<GdbServer*> allGdbServers;
00120 AvrDevice *core;
00121 GdbServerSocket *server;
00122 bool connState;
00123
00126 time_t oldTime;
00127
00128 int global_debug_on;
00129 int waitForGdbConnection;
00130 bool exitOnKillRequest;
00131 int runMode;
00132 bool lastCoreStepFinished;
00133
00134
00135
00136 char *last_reply;
00137 char buf[MAX_BUF];
00138 int m_gdb_thread_id;
00139
00140
00141 word avr_core_flash_read(int addr) ;
00142 void avr_core_flash_write(int addr, word val) ;
00143 void avr_core_flash_write_hi8( int addr, byte val) ;
00144 void avr_core_flash_write_lo8( int addr, byte val) ;
00145 void avr_core_remove_breakpoint(dword pc) ;
00146 void avr_core_insert_breakpoint(dword pc) ;
00147 int signal_has_occurred(int signo);
00148 void signal_watch_start(int signo);
00149 void signal_watch_stop(int signo);
00150 int avr_core_step() ;
00151 int hex2nib(char hex);
00152 const char* gdb_last_reply(const char *reply);
00153 void gdb_send_ack();
00154 void gdb_send_reply(const char *reply);
00155 void gdb_send_hex_reply(const char *reply, const char *reply_to_encode);
00156 void gdb_read_registers();
00157 void gdb_write_registers(const char *pkt);
00158 int gdb_extract_hex_num(const char **pkt, char stop);
00159 void gdb_read_register(const char *pkt);
00160 void gdb_write_register(const char *pkt);
00161 int gdb_get_addr_len(const char *pkt, char a_end, char l_end, unsigned int *addr, int *len);
00162 void gdb_read_memory(const char *pkt);
00163 void gdb_write_memory(const char *pkt);
00164 void gdb_break_point(const char *pkt);
00165 void gdb_select_thread(const char *pkt);
00166 void gdb_is_thread_alive(const char *pkt);
00167 void gdb_get_thread_list(const char *pkt);
00168 int gdb_get_signal(const char *pkt);
00169 int gdb_parse_packet(const char *pkt);
00170 int gdb_receive_and_process_packet(int blocking);
00171 void gdb_main_loop();
00172 void gdb_interact(int port, int debug_on);
00173 void IdleStep();
00174
00175 public:
00176 int Step(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns=0) ;
00177 int InternalStep(bool &trueHwStep, SystemClockOffset *timeToNextStepIn_ns=0) ;
00178 void TryConnectGdb();
00179 void SendPosition(int signal);
00180 int SleepStep();
00181 GdbServer( AvrDevice*, int port, int debugOn, int WaitForGdbConnection=true);
00182 virtual ~GdbServer();
00183 void Run();
00184 };
00185
00186 #endif