28 #include <sys/ioctl.h>
34 #include <netinet/in.h>
37 #include <sys/socket.h>
42 #if HAVE_NETINET_TCP_H
43 #include <netinet/tcp.h>
46 #include <sys/ioctl.h>
57 #include <arpa/inet.h>
66 #include <sys/iomgr.h>
69 #ifndef HAVE_SOCKLEN_T
73 #ifndef IN_PROCESS_AGENT
76 # define INVALID_DESCRIPTOR INVALID_SOCKET
78 # define INVALID_DESCRIPTOR -1
121 # define read(fd, buf, len) recv (fd, (char *) buf, len, 0)
122 # define write(fd, buf, len) send (fd, (char *) buf, len, 0)
142 #if defined(F_SETFL) && defined (FASYNC)
143 int save_fcntl_flags;
145 save_fcntl_flags = fcntl (fd, F_GETFL, 0);
146 fcntl (fd, F_SETFL, save_fcntl_flags | FASYNC);
147 #if defined (F_SETOWN)
148 fcntl (fd, F_SETOWN, getpid ());
156 struct sockaddr_in sockaddr;
162 tmp =
sizeof (sockaddr);
163 remote_desc = accept (listen_desc, (
struct sockaddr *) &sockaddr, &tmp);
164 if (remote_desc == -1)
169 setsockopt (remote_desc, SOL_SOCKET, SO_KEEPALIVE,
170 (
char *) &tmp,
sizeof (tmp));
175 setsockopt (remote_desc, IPPROTO_TCP, TCP_NODELAY,
176 (
char *) &tmp,
sizeof (tmp));
179 signal (SIGPIPE, SIG_IGN);
188 closesocket (listen_desc);
197 fprintf (stderr,
"Remote debugging from host %s\n",
198 inet_ntoa (sockaddr.sin_addr));
224 static int winsock_initialized;
227 struct sockaddr_in sockaddr;
238 transport_is_reliable = 1;
242 port_str = strchr (name,
':');
243 if (port_str == NULL)
245 transport_is_reliable = 0;
249 port = strtoul (port_str + 1, &port_end, 10);
250 if (port_str[1] ==
'\0' || *port_end !=
'\0')
251 error (
"Bad port argument: %s", name);
254 if (!winsock_initialized)
258 WSAStartup (MAKEWORD (1, 0), &wsad);
259 winsock_initialized = 1;
263 listen_desc = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
264 if (listen_desc == -1)
269 setsockopt (listen_desc, SOL_SOCKET, SO_REUSEADDR, (
char *) &tmp,
272 sockaddr.sin_family = PF_INET;
273 sockaddr.sin_port = htons (port);
274 sockaddr.sin_addr.s_addr = INADDR_ANY;
276 if (bind (listen_desc, (
struct sockaddr *) &sockaddr,
sizeof (sockaddr))
277 || listen (listen_desc, 1))
280 transport_is_reliable = 1;
291 port_str = strchr (name,
':');
293 if (port_str == NULL)
294 error (
"Only <host>:<port> is supported on this platform.");
299 fprintf (stderr,
"Remote debugging using stdio\n");
303 remote_desc = fileno (stdin);
311 else if (port_str == NULL)
315 if (stat (name, &statbuf) == 0
317 remote_desc = open (name, O_RDWR);
329 struct termios termios;
330 tcgetattr (remote_desc, &termios);
335 termios.c_cflag &= ~(CSIZE | PARENB);
336 termios.c_cflag |= CLOCAL | CS8;
337 termios.c_cc[VMIN] = 1;
338 termios.c_cc[VTIME] = 0;
340 tcsetattr (remote_desc, TCSANOW, &termios);
346 struct termio termio;
347 ioctl (remote_desc, TCGETA, &termio);
352 termio.c_cflag &= ~(CSIZE | PARENB);
353 termio.c_cflag |= CLOCAL | CS8;
354 termio.c_cc[VMIN] = 1;
355 termio.c_cc[VTIME] = 0;
357 ioctl (remote_desc, TCSETA, &termio);
365 ioctl (remote_desc, TIOCGETP, &sg);
367 ioctl (remote_desc, TIOCSETP, &sg);
371 fprintf (stderr,
"Remote debugging using %s\n", name);
383 struct sockaddr_in sockaddr;
385 len =
sizeof (sockaddr);
386 if (getsockname (listen_desc,
387 (
struct sockaddr *) &sockaddr, &len) < 0
388 || len <
sizeof (sockaddr))
390 port = ntohs (sockaddr.sin_port);
392 fprintf (stderr,
"Listening on port %d\n", port);
406 closesocket (remote_desc);
418 #ifndef IN_PROCESS_AGENT
428 for (i = 0; i < len; i++)
432 addr = addr | (
fromhex (ch) & 0x0f);
443 while (*end !=
'\0' && *end !=
';')
455 #ifndef IN_PROCESS_AGENT
462 try_rle (
char *buf,
int remaining,
unsigned char *csum,
char **p)
474 for (n = 1; n < remaining; n++)
475 if (buf[n] != buf[0])
490 while (n + 29 ==
'$' || n + 29 ==
'#')
503 #ifndef IN_PROCESS_AGENT
516 buf += sprintf (buf,
"p-%x.", -pid);
518 buf += sprintf (buf,
"p%x.", pid);
522 buf += sprintf (buf,
"-%x", -tid);
524 buf += sprintf (buf,
"%x", tid);
562 error (
"invalid remote ptid: %s\n", p);
593 return write (fileno (stdout), buf, count);
595 return write (remote_desc, buf, count);
606 return read (fileno (stdin), buf, count);
608 return read (remote_desc, buf, count);
619 unsigned char csum = 0;
624 buf2 =
xmalloc (strlen (
"$") + cnt + strlen (
"#nn") + 1);
635 for (i = 0; i < cnt;)
636 i +=
try_rle (buf + i, cnt - i, &csum, &p);
639 *p++ =
tohex ((csum >> 4) & 0xf);
640 *p++ =
tohex (csum & 0xf);
655 if (noack_mode || is_notif)
661 fprintf (stderr,
"putpkt (\"%s\"); [notif]\n", buf2);
663 fprintf (stderr,
"putpkt (\"%s\"); [noack mode]\n", buf2);
671 fprintf (stderr,
"putpkt (\"%s\"); [looking for ack]\n", buf2);
685 fprintf (stderr,
"[received '%c' (0x%x)]\n", cc, cc);
731 struct timeval immediate = { 0, 0 };
737 FD_SET (remote_desc, &readset);
738 if (select (remote_desc + 1, &readset, 0, 0, &immediate) > 0)
747 fprintf (stderr,
"client connection closed\n");
752 fprintf (stderr,
"input_interrupt, count = %d c = %d ", cc, c);
754 fprintf (stderr,
"('%c')\n", c);
756 fprintf (stderr,
"('\\x%02x')\n", c & 0xff);
787 sigemptyset (&sigio_set);
788 sigaddset (&sigio_set, SIGIO);
789 sigprocmask (SIG_UNBLOCK, &sigio_set, NULL);
795 nto_comctrl (
int enable)
797 struct sigevent event;
801 event.sigev_notify = SIGEV_SIGNAL_THREAD;
802 event.sigev_signo = SIGIO;
803 event.sigev_code = 0;
804 event.sigev_value.sival_ptr = NULL;
805 event.sigev_priority = -1;
806 ionotify (remote_desc, _NOTIFY_ACTION_POLLARM, _NOTIFY_COND_INPUT,
810 ionotify (remote_desc, _NOTIFY_ACTION_POLL, _NOTIFY_COND_INPUT, NULL);
822 if (async_io_enabled)
828 async_io_enabled = 1;
838 if (!async_io_enabled)
842 signal (SIGIO, SIG_IGN);
844 async_io_enabled = 0;
855 async_io_enabled = 1;
866 static unsigned char readchar_buf[BUFSIZ];
867 static int readchar_bufcnt = 0;
877 if (readchar_bufcnt == 0)
879 readchar_bufcnt =
read_prim (readchar_buf,
sizeof (readchar_buf));
881 if (readchar_bufcnt <= 0)
883 if (readchar_bufcnt == 0)
884 fprintf (stderr,
"readchar: Got EOF\n");
895 ch = *readchar_bufp++;
923 if (readchar_bufcnt > 0)
937 if (readchar_bufcnt > 0 && readchar_callback ==
NOT_SCHEDULED)
948 unsigned char csum, c1, c2;
962 fprintf (stderr,
"[getpkt: discarding char '%c']\n", c);
986 if (csum == (c1 << 4) + c2)
992 "Bad checksum, sentsum=0x%x, csum=0x%x, "
993 "buf=%s [no-ack-mode, Bad medium?]\n",
994 (c1 << 4) + c2, csum, buf);
999 fprintf (stderr,
"Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
1000 (c1 << 4) + c2, csum, buf);
1009 fprintf (stderr,
"getpkt (\"%s\"); [sending ack] \n", buf);
1018 fprintf (stderr,
"[sent ack]\n");
1026 fprintf (stderr,
"getpkt (\"%s\"); [no ack sent] \n", buf);
1054 #ifndef IN_PROCESS_AGENT
1059 if ((regno >> 12) != 0)
1060 *buf++ =
tohex ((regno >> 12) & 0xf);
1061 if ((regno >> 8) != 0)
1062 *buf++ =
tohex ((regno >> 8) & 0xf);
1063 *buf++ =
tohex ((regno >> 4) & 0xf);
1064 *buf++ =
tohex (regno & 0xf);
1085 sprintf (own_buf,
"n%x",
id);
1100 sprintf (own_buf,
"x%x",
id);
1108 struct target_waitstatus *status)
1114 switch (status->kind)
1116 case TARGET_WAITKIND_STOPPED:
1117 case TARGET_WAITKIND_FORKED:
1118 case TARGET_WAITKIND_VFORKED:
1127 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1128 const char *
event = (status->kind == TARGET_WAITKIND_FORKED
1129 ?
"fork" :
"vfork");
1131 sprintf (buf,
"T%02x%s:", signal, event);
1132 buf += strlen (buf);
1133 buf =
write_ptid (buf, status->value.related_pid);
1137 sprintf (buf,
"T%02x", status->value.sig);
1139 buf += strlen (buf);
1155 strncpy (buf,
"watch:", 6);
1164 for (i =
sizeof (
void *) * 2; i > 0; i--)
1165 *buf++ =
tohex ((addr >> (i - 1) * 4) & 0xf);
1170 sprintf (buf,
"swbreak:;");
1171 buf += strlen (buf);
1175 sprintf (buf,
"hwbreak:;");
1176 buf += strlen (buf);
1208 sprintf (buf,
"thread:");
1209 buf += strlen (buf);
1212 buf += strlen (buf);
1218 sprintf (buf,
"core:");
1219 buf += strlen (buf);
1220 sprintf (buf,
"%x", core);
1222 buf += strlen (buf);
1229 strcpy (buf,
"library:;");
1230 buf += strlen (buf);
1237 case TARGET_WAITKIND_EXITED:
1239 sprintf (buf,
"W%x;process:%x",
1242 sprintf (buf,
"W%02x", status->value.integer);
1244 case TARGET_WAITKIND_SIGNALLED:
1246 sprintf (buf,
"X%x;process:%x",
1249 sprintf (buf,
"X%02x", status->value.sig);
1251 case TARGET_WAITKIND_VFORK_DONE:
1254 enum gdb_signal signal = GDB_SIGNAL_TRAP;
1256 sprintf (buf,
"T%02xvforkdone:;", signal);
1259 sprintf (buf,
"T%02x", GDB_SIGNAL_0);
1262 error (
"unhandled waitkind");
1272 *mem_addr_ptr = *len_ptr = 0;
1274 while ((ch = from[i++]) !=
',')
1276 *mem_addr_ptr = *mem_addr_ptr << 4;
1277 *mem_addr_ptr |=
fromhex (ch) & 0x0f;
1280 for (j = 0; j < 4; j++)
1282 if ((ch = from[i++]) == 0)
1284 *len_ptr = *len_ptr << 4;
1285 *len_ptr |=
fromhex (ch) & 0x0f;
1291 unsigned char **to_p)
1295 *mem_addr_ptr = *len_ptr = 0;
1297 while ((ch = from[i++]) !=
',')
1299 *mem_addr_ptr = *mem_addr_ptr << 4;
1300 *mem_addr_ptr |=
fromhex (ch) & 0x0f;
1303 while ((ch = from[i++]) !=
':')
1305 *len_ptr = *len_ptr << 4;
1306 *len_ptr |=
fromhex (ch) & 0x0f;
1312 hex2bin (&from[i++], *to_p, *len_ptr);
1317 unsigned int *len_ptr,
unsigned char **to_p)
1321 *mem_addr_ptr = *len_ptr = 0;
1323 while ((ch = from[i++]) !=
',')
1325 *mem_addr_ptr = *mem_addr_ptr << 4;
1326 *mem_addr_ptr |=
fromhex (ch) & 0x0f;
1329 while ((ch = from[i++]) !=
':')
1331 *len_ptr = *len_ptr << 4;
1332 *len_ptr |=
fromhex (ch) & 0x0f;
1339 *to_p, *len_ptr) != *len_ptr)
1349 unsigned int *len,
unsigned char *data)
1356 while ((ch = *buf++) !=
':')
1358 *offset = *offset << 4;
1359 *offset |=
fromhex (ch) & 0x0f;
1363 packet_len -= buf - b;
1375 gdb_byte *pattern,
unsigned int *pattern_lenp)
1377 const char *p = buf;
1381 packet_len -= p - buf;
1383 pattern, packet_len);
1403 for (sym = *symcache_p; sym; sym =
next)
1428 if (strcmp (name, sym->
name) == 0)
1440 strcpy (own_buf,
"qSymbol:");
1443 if (
putpkt (own_buf) < 0)
1456 while (own_buf[0] ==
'm')
1460 unsigned int mem_len;
1465 bin2hex (mem_buf, own_buf, mem_len);
1469 if (
putpkt (own_buf) < 0)
1478 warning (
"Malformed response to qSymbol, ignoring: %s\n", own_buf);
1482 p = own_buf + strlen (
"qSymbol:");
1484 while (*q && *q !=
':')
1488 if (p == q || *q ==
'\0')
1494 sym =
xmalloc (
sizeof (*sym));
1523 strcpy (own_buf,
"qRelocInsn:");
1524 sprintf (own_buf,
"qRelocInsn:%s;%s",
paddress (oldloc),
1526 if (
putpkt (own_buf) < 0)
1538 while (own_buf[0] ==
'm' || own_buf[0] ==
'M' || own_buf[0] ==
'X')
1541 unsigned char *
mem_buf = NULL;
1542 unsigned int mem_len;
1544 if (own_buf[0] ==
'm')
1549 bin2hex (mem_buf, own_buf, mem_len);
1553 else if (own_buf[0] ==
'X')
1556 &mem_len, &mem_buf) < 0
1571 if (
putpkt (own_buf) < 0)
1578 if (own_buf[0] ==
'E')
1580 warning (
"An error occurred while relocating an instruction: %s\n",
1587 warning (
"Malformed response to qRelocInsn, ignoring: %s\n",
1601 int len = strlen (msg);
1602 char *buf =
xmalloc (len * 2 + 2);
const struct target_desc * tdesc
static int putpkt_binary_1(char *buf, int cnt, int is_notif)
void delete_callback_event(int id)
int handle_serial_event(int err, gdb_client_data client_data)
struct thread_info * current_thread
void monitor_output(const char *msg)
static int remote_is_stdio
struct thread_info * find_thread_ptid(ptid_t ptid)
int remote_unescape_input(const gdb_byte *buffer, int len, gdb_byte *out_buf, int out_maxlen)
ULONGEST hex_or_minus_one(char *buf, char **obuf)
#define target_core_of_thread(ptid)
int(* stopped_by_watchpoint)(void)
static void unblock_async_io(void)
void clear_symbol_cache(struct sym_cache **symcache_p)
int ptid_equal(ptid_t ptid1, ptid_t ptid2)
int putpkt_binary(char *buf, int cnt)
const char ** expedite_regs
int decode_xfer_write(char *buf, int packet_len, CORE_ADDR *offset, unsigned int *len, unsigned char *data)
static void reschedule(void)
void prepare_resume_reply(char *buf, ptid_t ptid, struct target_waitstatus *status)
void warning(const char *fmt,...)
#define target_stopped_by_hw_breakpoint()
struct ui_file * gdb_stdlog
static void enable_async_notification(int fd)
int bin2hex(const gdb_byte *bin, char *hex, int count)
void enable_async_io(void)
void dead_thread_notify(int id)
static void reset_readchar(void)
void initialize_async_io(void)
char * paddress(CORE_ADDR addr)
static int write_prim(const void *buf, int count)
char * write_ptid(char *buf, ptid_t ptid)
struct target_ops * the_target
void perror(const char *)
void disable_async_io(void)
static int read_prim(void *buf, int count)
char * xstrdup(const char *s)
ptid_t ptid_build(int pid, long lwp, long tid)
ptid_t read_ptid(char *buf, char **obuf)
#define STDIO_CONNECTION_NAME
void decode_address(CORE_ADDR *addrp, const char *start, int len)
static int async_io_enabled
#define target_stopped_by_sw_breakpoint()
static int process_remaining(void *context)
static char * outreg(struct regcache *regcache, int regno, char *buf)
int remote_connection_is_stdio(void)
void delete_file_handler(gdb_fildes_t fd)
void collect_register_as_string(struct regcache *regcache, int n, char *buf)
const char * decode_address_to_semicolon(CORE_ADDR *addrp, const char *start)
void remote_open(char *name)
void(* request_interrupt)(void)
static int readchar(void)
int read_inferior_memory(CORE_ADDR memaddr, unsigned char *myaddr, int len)
static gdb_fildes_t remote_desc
static int readchar_callback
int register_size(const struct target_desc *tdesc, int n)
void decode_m_packet(char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
int look_up_one_symbol(const char *name, CORE_ADDR *addrp, int may_ask_gdb)
int disable_packet_Tthread
int decode_search_memory_packet(const char *buf, int packet_len, CORE_ADDR *start_addrp, CORE_ADDR *search_space_lenp, gdb_byte *pattern, unsigned int *pattern_lenp)
static int startswith(const char *string, const char *pattern)
#define INVALID_DESCRIPTOR
static unsigned char * mem_buf
struct process_info * current_process(void)
char * unpack_varlen_hex(char *buff, ULONGEST *result)
int putpkt_notif(char *buf)
int append_callback_event(callback_handler_func *proc, gdb_client_data data)
int write_inferior_memory(CORE_ADDR memaddr, const unsigned char *myaddr, int len)
int ptid_get_pid(ptid_t ptid)
void add_file_handler(gdb_fildes_t fd, handler_func *proc, gdb_client_data client_data)
static gdb_fildes_t listen_desc
struct sym_cache * symbol_cache
void decode_M_packet(char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, unsigned char **to_p)
static void free_sym_cache(struct sym_cache *sym)
int find_regno(const struct target_desc *tdesc, const char *name)
const struct target_desc * current_target_desc(void)
static unsigned char readchar_buf[BUFSIZ]
static unsigned char * readchar_bufp
#define target_async(enable)
static int try_rle(char *buf, int remaining, unsigned char *csum, char **p)
const char * target_pid_to_str(ptid_t ptid)
void remote_prepare(char *name)
void new_thread_notify(int id)
struct regcache * get_thread_regcache(struct thread_info *thread, int fetch)
void perror_with_name(const char *string)
unsigned long long ULONGEST
static int handle_accept_event(int err, gdb_client_data client_data)
long ptid_get_lwp(ptid_t ptid)
int hex2bin(const char *hex, gdb_byte *bin, int count)
void debug_printf(const char *fmt,...)
int transport_is_reliable
static void input_interrupt(int unused)
void check_remote_input_interrupt_request(void)
CORE_ADDR(* stopped_data_address)(void)
int decode_X_packet(char *from, int packet_len, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr, unsigned char **to_p)
void error(const char *fmt,...)
int relocate_instruction(CORE_ADDR *to, CORE_ADDR oldloc)
void write_enn(char *buf)