remote_gdb.hh revision 8700
15124Sgblack@eecs.umich.edu/* 25124Sgblack@eecs.umich.edu * Copyright (c) 2002-2005 The Regents of The University of Michigan 35124Sgblack@eecs.umich.edu * All rights reserved. 45124Sgblack@eecs.umich.edu * 57087Snate@binkert.org * Redistribution and use in source and binary forms, with or without 67087Snate@binkert.org * modification, are permitted provided that the following conditions are 77087Snate@binkert.org * met: redistributions of source code must retain the above copyright 87087Snate@binkert.org * notice, this list of conditions and the following disclaimer; 97087Snate@binkert.org * redistributions in binary form must reproduce the above copyright 107087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the 117087Snate@binkert.org * documentation and/or other materials provided with the distribution; 127087Snate@binkert.org * neither the name of the copyright holders nor the names of its 135124Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 147087Snate@binkert.org * this software without specific prior written permission. 157087Snate@binkert.org * 167087Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 177087Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 187087Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 197087Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 207087Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 217087Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 225124Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237087Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 245124Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 255124Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 265124Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 275124Sgblack@eecs.umich.edu * 285124Sgblack@eecs.umich.edu * Authors: Nathan Binkert 295124Sgblack@eecs.umich.edu */ 305124Sgblack@eecs.umich.edu 315124Sgblack@eecs.umich.edu#ifndef __REMOTE_GDB_HH__ 325124Sgblack@eecs.umich.edu#define __REMOTE_GDB_HH__ 335124Sgblack@eecs.umich.edu 345124Sgblack@eecs.umich.edu#include <sys/signal.h> 355124Sgblack@eecs.umich.edu 365124Sgblack@eecs.umich.edu#include <map> 375124Sgblack@eecs.umich.edu 385124Sgblack@eecs.umich.edu#include "arch/types.hh" 395124Sgblack@eecs.umich.edu#include "base/pollevent.hh" 405184Sgblack@eecs.umich.edu#include "base/socket.hh" 415124Sgblack@eecs.umich.edu#include "cpu/pc_event.hh" 425124Sgblack@eecs.umich.edu 435124Sgblack@eecs.umich.educlass System; 445124Sgblack@eecs.umich.educlass ThreadContext; 455124Sgblack@eecs.umich.educlass PhysicalMemory; 465124Sgblack@eecs.umich.edu 475184Sgblack@eecs.umich.educlass GDBListener; 485184Sgblack@eecs.umich.edu 495184Sgblack@eecs.umich.eduenum GDBCommands 505184Sgblack@eecs.umich.edu{ 515184Sgblack@eecs.umich.edu GDBSignal = '?', // last signal 525124Sgblack@eecs.umich.edu GDBSetBaud = 'b', // set baud (depracated) 535124Sgblack@eecs.umich.edu GDBSetBreak = 'B', // set breakpoint (depracated) 545124Sgblack@eecs.umich.edu GDBCont = 'c', // resume 555124Sgblack@eecs.umich.edu GDBAsyncCont = 'C', // continue with signal 565124Sgblack@eecs.umich.edu GDBDebug = 'd', // toggle debug flags (deprecated) 575124Sgblack@eecs.umich.edu GDBDetach = 'D', // detach remote gdb 585124Sgblack@eecs.umich.edu GDBRegR = 'g', // read general registers 595124Sgblack@eecs.umich.edu GDBRegW = 'G', // write general registers 605124Sgblack@eecs.umich.edu GDBSetThread = 'H', // set thread 615124Sgblack@eecs.umich.edu GDBCycleStep = 'i', // step a single cycle 625124Sgblack@eecs.umich.edu GDBSigCycleStep = 'I', // signal then cycle step 63 GDBKill = 'k', // kill program 64 GDBMemR = 'm', // read memory 65 GDBMemW = 'M', // write memory 66 GDBReadReg = 'p', // read register 67 GDBSetReg = 'P', // write register 68 GDBQueryVar = 'q', // query variable 69 GDBSetVar = 'Q', // set variable 70 GDBReset = 'r', // reset system. (Deprecated) 71 GDBStep = 's', // step 72 GDBAsyncStep = 'S', // signal and step 73 GDBThreadAlive = 'T', // find out if the thread is alive 74 GDBTargetExit = 'W', // target exited 75 GDBBinaryDload = 'X', // write memory 76 GDBClrHwBkpt = 'z', // remove breakpoint or watchpoint 77 GDBSetHwBkpt = 'Z' // insert breakpoint or watchpoint 78}; 79 80const char GDBStart = '$'; 81const char GDBEnd = '#'; 82const char GDBGoodP = '+'; 83const char GDBBadP = '-'; 84 85const int GDBPacketBufLen = 1024; 86 87class BaseRemoteGDB 88{ 89 private: 90 friend void debugger(); 91 friend class GDBListener; 92 93 //Helper functions 94 protected: 95 int digit2i(char); 96 char i2digit(int); 97 Addr hex2i(const char **); 98 //Address formats, break types, and gdb commands may change 99 //between architectures, so they're defined as virtual 100 //functions. 101 virtual void mem2hex(void *, const void *, int); 102 virtual const char * hex2mem(void *, const char *, int); 103 virtual const char * break_type(char c); 104 virtual const char * gdb_command(char cmd); 105 106 protected: 107 class Event : public PollEvent 108 { 109 protected: 110 BaseRemoteGDB *gdb; 111 112 public: 113 Event(BaseRemoteGDB *g, int fd, int e); 114 void process(int revent); 115 }; 116 117 friend class Event; 118 Event *event; 119 GDBListener *listener; 120 int number; 121 122 protected: 123 //The socket commands come in through 124 int fd; 125 126 protected: 127#ifdef notyet 128 label_t recover; 129#endif 130 bool active; 131 bool attached; 132 133 System *system; 134 PhysicalMemory *pmem; 135 ThreadContext *context; 136 137 protected: 138 class GdbRegCache 139 { 140 public: 141 GdbRegCache(size_t newSize) : regs(new uint64_t[newSize]), size(newSize) 142 {} 143 ~GdbRegCache() 144 { 145 delete [] regs; 146 } 147 148 uint64_t * regs; 149 size_t size; 150 size_t bytes() { return size * sizeof(uint64_t); } 151 }; 152 153 GdbRegCache gdbregs; 154 155 protected: 156 uint8_t getbyte(); 157 void putbyte(uint8_t b); 158 159 int recv(char *data, int len); 160 void send(const char *data); 161 162 protected: 163 // Machine memory 164 virtual bool read(Addr addr, size_t size, char *data); 165 virtual bool write(Addr addr, size_t size, const char *data); 166 167 template <class T> T read(Addr addr); 168 template <class T> void write(Addr addr, T data); 169 170 public: 171 BaseRemoteGDB(System *system, ThreadContext *context, size_t cacheSize); 172 virtual ~BaseRemoteGDB(); 173 174 void replaceThreadContext(ThreadContext *tc) { context = tc; } 175 176 void attach(int fd); 177 void detach(); 178 bool isattached(); 179 180 virtual bool acc(Addr addr, size_t len) = 0; 181 bool trap(int type); 182 virtual bool breakpoint() 183 { 184 return trap(SIGTRAP); 185 } 186 187 protected: 188 virtual void getregs() = 0; 189 virtual void setregs() = 0; 190 191 virtual void clearSingleStep() = 0; 192 virtual void setSingleStep() = 0; 193 194 PCEventQueue *getPcEventQueue(); 195 196 protected: 197 class HardBreakpoint : public PCEvent 198 { 199 private: 200 BaseRemoteGDB *gdb; 201 202 public: 203 int refcount; 204 205 public: 206 HardBreakpoint(BaseRemoteGDB *_gdb, Addr addr); 207 std::string name() { return gdb->name() + ".hwbkpt"; } 208 209 virtual void process(ThreadContext *tc); 210 }; 211 friend class HardBreakpoint; 212 213 typedef std::map<Addr, HardBreakpoint *> break_map_t; 214 typedef break_map_t::iterator break_iter_t; 215 break_map_t hardBreakMap; 216 217 bool insertSoftBreak(Addr addr, size_t len); 218 bool removeSoftBreak(Addr addr, size_t len); 219 virtual bool insertHardBreak(Addr addr, size_t len); 220 bool removeHardBreak(Addr addr, size_t len); 221 222 protected: 223 void clearTempBreakpoint(Addr &bkpt); 224 void setTempBreakpoint(Addr bkpt); 225 226 public: 227 std::string name(); 228}; 229 230template <class T> 231inline T 232BaseRemoteGDB::read(Addr addr) 233{ 234 T temp; 235 read(addr, sizeof(T), (char *)&temp); 236 return temp; 237} 238 239template <class T> 240inline void 241BaseRemoteGDB::write(Addr addr, T data) 242{ write(addr, sizeof(T), (const char *)&data); } 243 244class GDBListener 245{ 246 protected: 247 class Event : public PollEvent 248 { 249 protected: 250 GDBListener *listener; 251 252 public: 253 Event(GDBListener *l, int fd, int e); 254 void process(int revent); 255 }; 256 257 friend class Event; 258 Event *event; 259 260 protected: 261 ListenSocket listener; 262 BaseRemoteGDB *gdb; 263 int port; 264 265 public: 266 GDBListener(BaseRemoteGDB *g, int p); 267 ~GDBListener(); 268 269 void accept(); 270 void listen(); 271 std::string name(); 272}; 273 274#endif /* __REMOTE_GDB_H__ */ 275