]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/gdbserver/remote-utils.c
* infrun.c (print_stop_reason): Add missing uiout field
[thirdparty/binutils-gdb.git] / gdb / gdbserver / remote-utils.c
CommitLineData
c906108c 1/* Remote utility routines for the remote server for GDB.
b6ba6518
KB
2 Copyright 1986, 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "server.h"
23#include "terminal.h"
24#include <stdio.h>
25#include <string.h>
26#include <sys/ioctl.h>
27#include <sys/file.h>
28#include <netinet/in.h>
29#include <sys/socket.h>
30#include <netdb.h>
31#include <netinet/tcp.h>
32#include <sys/ioctl.h>
33#include <signal.h>
34#include <fcntl.h>
35
36int remote_debug = 0;
03863182 37struct ui_file *gdb_stdlog;
c906108c
SS
38
39static int remote_desc;
40
41/* Open a connection to a remote debugger.
42 NAME is the filename used for communication. */
43
44void
fba45db2 45remote_open (char *name)
c906108c
SS
46{
47 int save_fcntl_flags;
48
49 if (!strchr (name, ':'))
50 {
51 remote_desc = open (name, O_RDWR);
52 if (remote_desc < 0)
53 perror_with_name ("Could not open remote device");
54
55#ifdef HAVE_TERMIOS
56 {
57 struct termios termios;
c5aa993b 58 tcgetattr (remote_desc, &termios);
c906108c
SS
59
60 termios.c_iflag = 0;
61 termios.c_oflag = 0;
62 termios.c_lflag = 0;
c5aa993b 63 termios.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
64 termios.c_cflag |= CLOCAL | CS8;
65 termios.c_cc[VMIN] = 0;
66 termios.c_cc[VTIME] = 0;
67
c5aa993b 68 tcsetattr (remote_desc, TCSANOW, &termios);
c906108c
SS
69 }
70#endif
71
72#ifdef HAVE_TERMIO
73 {
74 struct termio termio;
75 ioctl (remote_desc, TCGETA, &termio);
76
77 termio.c_iflag = 0;
78 termio.c_oflag = 0;
79 termio.c_lflag = 0;
c5aa993b 80 termio.c_cflag &= ~(CSIZE | PARENB);
c906108c
SS
81 termio.c_cflag |= CLOCAL | CS8;
82 termio.c_cc[VMIN] = 0;
83 termio.c_cc[VTIME] = 0;
84
85 ioctl (remote_desc, TCSETA, &termio);
86 }
87#endif
88
89#ifdef HAVE_SGTTY
90 {
91 struct sgttyb sg;
92
93 ioctl (remote_desc, TIOCGETP, &sg);
94 sg.sg_flags = RAW;
95 ioctl (remote_desc, TIOCSETP, &sg);
96 }
97#endif
98
99
100 }
101 else
102 {
103 char *port_str;
104 int port;
105 struct sockaddr_in sockaddr;
106 int tmp;
107 struct protoent *protoent;
108 int tmp_desc;
109
110 port_str = strchr (name, ':');
111
112 port = atoi (port_str + 1);
113
114 tmp_desc = socket (PF_INET, SOCK_STREAM, 0);
115 if (tmp_desc < 0)
116 perror_with_name ("Can't open socket");
117
118 /* Allow rapid reuse of this port. */
119 tmp = 1;
c5aa993b
JM
120 setsockopt (tmp_desc, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp,
121 sizeof (tmp));
c906108c
SS
122
123 sockaddr.sin_family = PF_INET;
c5aa993b 124 sockaddr.sin_port = htons (port);
c906108c
SS
125 sockaddr.sin_addr.s_addr = INADDR_ANY;
126
c5aa993b 127 if (bind (tmp_desc, (struct sockaddr *) &sockaddr, sizeof (sockaddr))
c906108c
SS
128 || listen (tmp_desc, 1))
129 perror_with_name ("Can't bind address");
130
131 tmp = sizeof (sockaddr);
c5aa993b 132 remote_desc = accept (tmp_desc, (struct sockaddr *) &sockaddr, &tmp);
c906108c
SS
133 if (remote_desc == -1)
134 perror_with_name ("Accept failed");
135
136 protoent = getprotobyname ("tcp");
137 if (!protoent)
138 perror_with_name ("getprotobyname");
139
140 /* Enable TCP keep alive process. */
141 tmp = 1;
c5aa993b 142 setsockopt (tmp_desc, SOL_SOCKET, SO_KEEPALIVE, (char *) &tmp, sizeof (tmp));
c906108c
SS
143
144 /* Tell TCP not to delay small packets. This greatly speeds up
c5aa993b 145 interactive response. */
c906108c
SS
146 tmp = 1;
147 setsockopt (remote_desc, protoent->p_proto, TCP_NODELAY,
c5aa993b 148 (char *) &tmp, sizeof (tmp));
c906108c
SS
149
150 close (tmp_desc); /* No longer need this */
151
c5aa993b
JM
152 signal (SIGPIPE, SIG_IGN); /* If we don't do this, then gdbserver simply
153 exits when the remote side dies. */
c906108c
SS
154 }
155
156#if defined(F_SETFL) && defined (FASYNC)
157 save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
158 fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
159 disable_async_io ();
160#endif /* FASYNC */
161 fprintf (stderr, "Remote debugging using %s\n", name);
162}
163
164void
fba45db2 165remote_close (void)
c906108c
SS
166{
167 close (remote_desc);
168}
169
170/* Convert hex digit A to a number. */
171
172static int
fba45db2 173fromhex (int a)
c906108c
SS
174{
175 if (a >= '0' && a <= '9')
176 return a - '0';
177 else if (a >= 'a' && a <= 'f')
178 return a - 'a' + 10;
179 else
180 error ("Reply contains invalid hex digit");
181}
182
183/* Convert number NIB to a hex digit. */
184
185static int
fba45db2 186tohex (int nib)
c906108c
SS
187{
188 if (nib < 10)
189 return '0' + nib;
190 else
191 return 'a' + nib - 10;
192}
193
194/* Send a packet to the remote machine, with error checking.
195 The data of the packet is in BUF. Returns >= 0 on success, -1 otherwise. */
196
197int
fba45db2 198putpkt (char *buf)
c906108c
SS
199{
200 int i;
201 unsigned char csum = 0;
5c44784c 202 char buf2[PBUFSIZ];
c906108c
SS
203 char buf3[1];
204 int cnt = strlen (buf);
205 char *p;
206
207 /* Copy the packet into buffer BUF2, encapsulating it
208 and giving it a checksum. */
209
210 p = buf2;
211 *p++ = '$';
212
213 for (i = 0; i < cnt; i++)
214 {
215 csum += buf[i];
216 *p++ = buf[i];
217 }
218 *p++ = '#';
219 *p++ = tohex ((csum >> 4) & 0xf);
220 *p++ = tohex (csum & 0xf);
221
222 *p = '\0';
223
224 /* Send it over and over until we get a positive ack. */
225
226 do
227 {
228 int cc;
229
230 if (write (remote_desc, buf2, p - buf2) != p - buf2)
231 {
232 perror ("putpkt(write)");
233 return -1;
234 }
235
236 if (remote_debug)
237 printf ("putpkt (\"%s\"); [looking for ack]\n", buf2);
238 cc = read (remote_desc, buf3, 1);
239 if (remote_debug)
240 printf ("[received '%c' (0x%x)]\n", buf3[0], buf3[0]);
241 if (cc <= 0)
242 {
243 if (cc == 0)
244 fprintf (stderr, "putpkt(read): Got EOF\n");
245 else
246 perror ("putpkt(read)");
247
248 return -1;
249 }
250 }
251 while (buf3[0] != '+');
252
253 return 1; /* Success! */
254}
255
256/* Come here when we get an input interrupt from the remote side. This
257 interrupt should only be active while we are waiting for the child to do
258 something. About the only thing that should come through is a ^C, which
259 will cause us to send a SIGINT to the child. */
260
261static void
fba45db2 262input_interrupt (void)
c906108c
SS
263{
264 int cc;
265 char c;
266
267 cc = read (remote_desc, &c, 1);
268
269 if (cc != 1 || c != '\003')
270 {
c5aa993b 271 fprintf (stderr, "input_interrupt, cc = %d c = %d\n", cc, c);
c906108c
SS
272 return;
273 }
274
275 kill (inferior_pid, SIGINT);
276}
277
278void
fba45db2 279enable_async_io (void)
c906108c
SS
280{
281 signal (SIGIO, input_interrupt);
282}
283
284void
fba45db2 285disable_async_io (void)
c906108c
SS
286{
287 signal (SIGIO, SIG_IGN);
288}
289
290/* Returns next char from remote GDB. -1 if error. */
291
292static int
fba45db2 293readchar (void)
c906108c
SS
294{
295 static char buf[BUFSIZ];
296 static int bufcnt = 0;
297 static char *bufp;
298
299 if (bufcnt-- > 0)
300 return *bufp++ & 0x7f;
301
302 bufcnt = read (remote_desc, buf, sizeof (buf));
303
304 if (bufcnt <= 0)
305 {
306 if (bufcnt == 0)
307 fprintf (stderr, "readchar: Got EOF\n");
308 else
309 perror ("readchar");
310
311 return -1;
312 }
313
314 bufp = buf;
315 bufcnt--;
316 return *bufp++ & 0x7f;
317}
318
319/* Read a packet from the remote machine, with error checking,
320 and store it in BUF. Returns length of packet, or negative if error. */
321
322int
fba45db2 323getpkt (char *buf)
c906108c
SS
324{
325 char *bp;
326 unsigned char csum, c1, c2;
327 int c;
328
329 while (1)
330 {
331 csum = 0;
332
333 while (1)
334 {
335 c = readchar ();
336 if (c == '$')
337 break;
338 if (remote_debug)
339 printf ("[getpkt: discarding char '%c']\n", c);
340 if (c < 0)
341 return -1;
342 }
343
344 bp = buf;
345 while (1)
346 {
347 c = readchar ();
348 if (c < 0)
349 return -1;
350 if (c == '#')
351 break;
352 *bp++ = c;
353 csum += c;
354 }
355 *bp = 0;
356
357 c1 = fromhex (readchar ());
358 c2 = fromhex (readchar ());
c5aa993b 359
c906108c
SS
360 if (csum == (c1 << 4) + c2)
361 break;
362
363 fprintf (stderr, "Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
364 (c1 << 4) + c2, csum, buf);
365 write (remote_desc, "-", 1);
366 }
367
368 if (remote_debug)
369 printf ("getpkt (\"%s\"); [sending ack] \n", buf);
370
371 write (remote_desc, "+", 1);
372
373 if (remote_debug)
374 printf ("[sent ack]\n");
375 return bp - buf;
376}
377
378void
fba45db2 379write_ok (char *buf)
c906108c
SS
380{
381 buf[0] = 'O';
382 buf[1] = 'K';
383 buf[2] = '\0';
384}
385
386void
fba45db2 387write_enn (char *buf)
c906108c
SS
388{
389 buf[0] = 'E';
390 buf[1] = 'N';
391 buf[2] = 'N';
392 buf[3] = '\0';
393}
394
395void
fba45db2 396convert_int_to_ascii (char *from, char *to, int n)
c906108c
SS
397{
398 int nib;
399 char ch;
400 while (n--)
401 {
402 ch = *from++;
403 nib = ((ch & 0xf0) >> 4) & 0x0f;
404 *to++ = tohex (nib);
405 nib = ch & 0x0f;
406 *to++ = tohex (nib);
407 }
408 *to++ = 0;
409}
410
411
412void
fba45db2 413convert_ascii_to_int (char *from, char *to, int n)
c906108c
SS
414{
415 int nib1, nib2;
416 while (n--)
417 {
418 nib1 = fromhex (*from++);
419 nib2 = fromhex (*from++);
420 *to++ = (((nib1 & 0x0f) << 4) & 0xf0) | (nib2 & 0x0f);
421 }
422}
423
424static char *
fba45db2 425outreg (int regno, char *buf)
c906108c 426{
c906108c
SS
427 int regsize = REGISTER_RAW_SIZE (regno);
428
5c44784c
JM
429 if ((regno >> 12) != 0)
430 *buf++ = tohex ((regno >> 12) & 0xf);
431 if ((regno >> 8) != 0)
432 *buf++ = tohex ((regno >> 8) & 0xf);
433 *buf++ = tohex ((regno >> 4) & 0xf);
c906108c
SS
434 *buf++ = tohex (regno & 0xf);
435 *buf++ = ':';
436 convert_int_to_ascii (&registers[REGISTER_BYTE (regno)], buf, regsize);
437 buf += 2 * regsize;
438 *buf++ = ';';
439
440 return buf;
441}
442
443void
fba45db2 444prepare_resume_reply (char *buf, char status, unsigned char signo)
c906108c
SS
445{
446 int nib;
447
448 *buf++ = status;
449
450 /* FIXME! Should be converting this signal number (numbered
451 according to the signal numbering of the system we are running on)
452 to the signal numbers used by the gdb protocol (see enum target_signal
453 in gdb/target.h). */
454 nib = ((signo & 0xf0) >> 4);
455 *buf++ = tohex (nib);
456 nib = signo & 0x0f;
457 *buf++ = tohex (nib);
458
459 if (status == 'T')
460 {
5c44784c
JM
461#ifdef GDBSERVER_RESUME_REGS
462 static int gdbserver_resume_regs[] = GDBSERVER_RESUME_REGS ;
463 int i;
464 for (i = 0;
465 i < sizeof (gdbserver_resume_regs)
466 / sizeof (gdbserver_resume_regs[0]);
467 i++)
468 {
469 int regnum = gdbserver_resume_regs[i];
470 buf = outreg (regnum, buf);
471 }
472#else /* !defined(GDBSERVER_RESUME_REGS) */
c906108c
SS
473 buf = outreg (PC_REGNUM, buf);
474 buf = outreg (FP_REGNUM, buf);
475 buf = outreg (SP_REGNUM, buf);
03863182
AC
476 if (NPC_REGNUM >= 0)
477 buf = outreg (NPC_REGNUM, buf);
c906108c
SS
478#ifdef O7_REGNUM
479 buf = outreg (O7_REGNUM, buf);
480#endif
5c44784c 481#endif /* GDBSERVER_RESUME_REGS */
c906108c
SS
482
483 /* If the debugger hasn't used any thread features, don't burden it with
c5aa993b 484 threads. If we didn't check this, GDB 4.13 and older would choke. */
c906108c
SS
485 if (cont_thread != 0)
486 {
487 if (old_thread_from_wait != thread_from_wait)
488 {
489 sprintf (buf, "thread:%x;", thread_from_wait);
490 buf += strlen (buf);
491 old_thread_from_wait = thread_from_wait;
492 }
493 }
494 }
495 /* For W and X, we're done. */
496 *buf++ = 0;
497}
498
499void
fba45db2 500decode_m_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr)
c906108c
SS
501{
502 int i = 0, j = 0;
503 char ch;
504 *mem_addr_ptr = *len_ptr = 0;
505
506 while ((ch = from[i++]) != ',')
507 {
508 *mem_addr_ptr = *mem_addr_ptr << 4;
509 *mem_addr_ptr |= fromhex (ch) & 0x0f;
510 }
511
512 for (j = 0; j < 4; j++)
513 {
514 if ((ch = from[i++]) == 0)
515 break;
516 *len_ptr = *len_ptr << 4;
517 *len_ptr |= fromhex (ch) & 0x0f;
518 }
519}
520
521void
fba45db2
KB
522decode_M_packet (char *from, CORE_ADDR *mem_addr_ptr, unsigned int *len_ptr,
523 char *to)
c906108c
SS
524{
525 int i = 0;
526 char ch;
527 *mem_addr_ptr = *len_ptr = 0;
528
529 while ((ch = from[i++]) != ',')
530 {
531 *mem_addr_ptr = *mem_addr_ptr << 4;
532 *mem_addr_ptr |= fromhex (ch) & 0x0f;
533 }
534
535 while ((ch = from[i++]) != ':')
536 {
537 *len_ptr = *len_ptr << 4;
538 *len_ptr |= fromhex (ch) & 0x0f;
539 }
540
541 convert_ascii_to_int (&from[i++], to, *len_ptr);
542}