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