| 1 | /* Generic support for remote debugging interfaces. |
| 2 | |
| 3 | Copyright 1993, 1994, 1998 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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, Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | /* This file actually contains two distinct logical "packages". They |
| 22 | are packaged together in this one file because they are typically |
| 23 | used together. |
| 24 | |
| 25 | The first package is an addition to the serial package. The |
| 26 | addition provides reading and writing with debugging output and |
| 27 | timeouts based on user settable variables. These routines are |
| 28 | intended to support serial port based remote backends. These |
| 29 | functions are prefixed with sr_. |
| 30 | |
| 31 | The second package is a collection of more or less generic |
| 32 | functions for use by remote backends. They support user settable |
| 33 | variables for debugging, retries, and the like. |
| 34 | |
| 35 | Todo: |
| 36 | |
| 37 | * a pass through mode a la kermit or telnet. |
| 38 | * autobaud. |
| 39 | * ask remote to change his baud rate. |
| 40 | */ |
| 41 | |
| 42 | #include <ctype.h> |
| 43 | |
| 44 | #include "defs.h" |
| 45 | #include "gdb_string.h" |
| 46 | #include "gdbcmd.h" |
| 47 | #include "target.h" |
| 48 | #include "serial.h" |
| 49 | #include "gdbcore.h" /* for exec_bfd */ |
| 50 | #include "inferior.h" /* for generic_mourn_inferior */ |
| 51 | #include "remote-utils.h" |
| 52 | |
| 53 | |
| 54 | void _initialize_sr_support PARAMS ((void)); |
| 55 | |
| 56 | struct _sr_settings sr_settings = { |
| 57 | 4, /* timeout: |
| 58 | remote-hms.c had 2 |
| 59 | remote-bug.c had "with a timeout of 2, we time out waiting for |
| 60 | the prompt after an s-record dump." |
| 61 | |
| 62 | remote.c had (2): This was 5 seconds, which is a long time to |
| 63 | sit and wait. Unless this is going though some terminal server |
| 64 | or multiplexer or other form of hairy serial connection, I |
| 65 | would think 2 seconds would be plenty. |
| 66 | */ |
| 67 | |
| 68 | 10, /* retries */ |
| 69 | NULL, /* device */ |
| 70 | NULL, /* descriptor */ |
| 71 | }; |
| 72 | |
| 73 | struct gr_settings *gr_settings = NULL; |
| 74 | |
| 75 | static void usage PARAMS ((char *, char *)); |
| 76 | static void sr_com PARAMS ((char *, int)); |
| 77 | |
| 78 | static void |
| 79 | usage(proto, junk) |
| 80 | char *proto; |
| 81 | char *junk; |
| 82 | { |
| 83 | if (junk != NULL) |
| 84 | fprintf_unfiltered(gdb_stderr, "Unrecognized arguments: `%s'.\n", junk); |
| 85 | |
| 86 | error ("Usage: target %s [DEVICE [SPEED [DEBUG]]]\n\ |
| 87 | where DEVICE is the name of a device or HOST:PORT", proto, proto); |
| 88 | |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | #define CHECKDONE(p, q) \ |
| 93 | { \ |
| 94 | if (q == p) \ |
| 95 | { \ |
| 96 | if (*p == '\0') \ |
| 97 | return; \ |
| 98 | else \ |
| 99 | usage(proto, p); \ |
| 100 | } \ |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | sr_scan_args(proto, args) |
| 105 | char *proto; |
| 106 | char *args; |
| 107 | { |
| 108 | int n; |
| 109 | char *p, *q; |
| 110 | |
| 111 | /* if no args, then nothing to do. */ |
| 112 | if (args == NULL || *args == '\0') |
| 113 | return; |
| 114 | |
| 115 | /* scan off white space. */ |
| 116 | for (p = args; isspace(*p); ++p) ;; |
| 117 | |
| 118 | /* find end of device name. */ |
| 119 | for (q = p; *q != '\0' && !isspace(*q); ++q) ;; |
| 120 | |
| 121 | /* check for missing or empty device name. */ |
| 122 | CHECKDONE(p, q); |
| 123 | sr_set_device(savestring(p, q - p)); |
| 124 | |
| 125 | /* look for baud rate. */ |
| 126 | n = strtol(q, &p, 10); |
| 127 | |
| 128 | /* check for missing or empty baud rate. */ |
| 129 | CHECKDONE(p, q); |
| 130 | baud_rate = n; |
| 131 | |
| 132 | /* look for debug value. */ |
| 133 | n = strtol(p, &q, 10); |
| 134 | |
| 135 | /* check for missing or empty debug value. */ |
| 136 | CHECKDONE(p, q); |
| 137 | sr_set_debug(n); |
| 138 | |
| 139 | /* scan off remaining white space. */ |
| 140 | for (p = q; isspace(*p); ++p) ;; |
| 141 | |
| 142 | /* if not end of string, then there's unrecognized junk. */ |
| 143 | if (*p != '\0') |
| 144 | usage(proto, p); |
| 145 | |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | gr_generic_checkin() |
| 151 | { |
| 152 | sr_write_cr(""); |
| 153 | gr_expect_prompt(); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | gr_open(args, from_tty, gr) |
| 158 | char *args; |
| 159 | int from_tty; |
| 160 | struct gr_settings *gr; |
| 161 | { |
| 162 | target_preopen(from_tty); |
| 163 | sr_scan_args(gr->ops->to_shortname, args); |
| 164 | unpush_target(gr->ops); |
| 165 | |
| 166 | gr_settings = gr; |
| 167 | |
| 168 | gr_set_dcache(dcache_init(gr->readfunc, gr->writefunc)); |
| 169 | |
| 170 | if (sr_get_desc() != NULL) |
| 171 | gr_close (0); |
| 172 | |
| 173 | /* If no args are specified, then we use the device specified by a |
| 174 | previous command or "set remotedevice". But if there is no |
| 175 | device, better stop now, not dump core. */ |
| 176 | |
| 177 | if (sr_get_device () == NULL) |
| 178 | usage (gr->ops->to_shortname, NULL); |
| 179 | |
| 180 | sr_set_desc(SERIAL_OPEN (sr_get_device())); |
| 181 | if (!sr_get_desc()) |
| 182 | perror_with_name((char *) sr_get_device()); |
| 183 | |
| 184 | if (baud_rate != -1) |
| 185 | { |
| 186 | if (SERIAL_SETBAUDRATE(sr_get_desc(), baud_rate) != 0) |
| 187 | { |
| 188 | SERIAL_CLOSE(sr_get_desc()); |
| 189 | perror_with_name(sr_get_device()); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | SERIAL_RAW (sr_get_desc()); |
| 194 | |
| 195 | /* If there is something sitting in the buffer we might take it as a |
| 196 | response to a command, which would be bad. */ |
| 197 | SERIAL_FLUSH_INPUT (sr_get_desc ()); |
| 198 | |
| 199 | /* default retries */ |
| 200 | if (sr_get_retries() == 0) |
| 201 | sr_set_retries(1); |
| 202 | |
| 203 | /* default clear breakpoint function */ |
| 204 | if (gr_settings->clear_all_breakpoints == NULL) |
| 205 | gr_settings->clear_all_breakpoints = remove_breakpoints; |
| 206 | |
| 207 | if (from_tty) |
| 208 | { |
| 209 | printf_filtered ("Remote debugging using `%s'", sr_get_device ()); |
| 210 | if (baud_rate != -1) |
| 211 | printf_filtered (" at baud rate of %d", |
| 212 | baud_rate); |
| 213 | printf_filtered ("\n"); |
| 214 | } |
| 215 | |
| 216 | push_target(gr->ops); |
| 217 | gr_checkin(); |
| 218 | gr_clear_all_breakpoints (); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | /* Read a character from the remote system masking it down to 7 bits |
| 223 | and doing all the fancy timeout stuff. */ |
| 224 | |
| 225 | int |
| 226 | sr_readchar () |
| 227 | { |
| 228 | int buf; |
| 229 | |
| 230 | buf = SERIAL_READCHAR (sr_get_desc(), sr_get_timeout()); |
| 231 | |
| 232 | if (buf == SERIAL_TIMEOUT) |
| 233 | error ("Timeout reading from remote system."); |
| 234 | |
| 235 | if (sr_get_debug() > 0) |
| 236 | printf_unfiltered ("%c", buf); |
| 237 | |
| 238 | return buf & 0x7f; |
| 239 | } |
| 240 | |
| 241 | int |
| 242 | sr_pollchar() |
| 243 | { |
| 244 | int buf; |
| 245 | |
| 246 | buf = SERIAL_READCHAR (sr_get_desc(), 0); |
| 247 | if (buf == SERIAL_TIMEOUT) |
| 248 | buf = 0; |
| 249 | if (sr_get_debug() > 0) |
| 250 | { |
| 251 | if (buf) |
| 252 | printf_unfiltered ("%c", buf); |
| 253 | else |
| 254 | printf_unfiltered ("<empty character poll>"); |
| 255 | } |
| 256 | |
| 257 | return buf & 0x7f; |
| 258 | } |
| 259 | |
| 260 | /* Keep discarding input from the remote system, until STRING is found. |
| 261 | Let the user break out immediately. */ |
| 262 | void |
| 263 | sr_expect (string) |
| 264 | char *string; |
| 265 | { |
| 266 | char *p = string; |
| 267 | |
| 268 | immediate_quit = 1; |
| 269 | while (1) |
| 270 | { |
| 271 | if (sr_readchar () == *p) |
| 272 | { |
| 273 | p++; |
| 274 | if (*p == '\0') |
| 275 | { |
| 276 | immediate_quit = 0; |
| 277 | return; |
| 278 | } |
| 279 | } |
| 280 | else |
| 281 | p = string; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | sr_write (a, l) |
| 287 | char *a; |
| 288 | int l; |
| 289 | { |
| 290 | int i; |
| 291 | |
| 292 | if (SERIAL_WRITE (sr_get_desc(), a, l) != 0) |
| 293 | perror_with_name ("sr_write: Error writing to remote"); |
| 294 | |
| 295 | if (sr_get_debug() > 0) |
| 296 | for (i = 0; i < l; i++) |
| 297 | printf_unfiltered ("%c", a[i]); |
| 298 | |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | void |
| 303 | sr_write_cr (s) |
| 304 | char *s; |
| 305 | { |
| 306 | sr_write (s, strlen (s)); |
| 307 | sr_write ("\r", 1); |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | int |
| 312 | sr_timed_read (buf, n) |
| 313 | char *buf; |
| 314 | int n; |
| 315 | { |
| 316 | int i; |
| 317 | char c; |
| 318 | |
| 319 | i = 0; |
| 320 | while (i < n) |
| 321 | { |
| 322 | c = sr_readchar (); |
| 323 | |
| 324 | if (c == 0) |
| 325 | return i; |
| 326 | buf[i] = c; |
| 327 | i++; |
| 328 | |
| 329 | } |
| 330 | return i; |
| 331 | } |
| 332 | |
| 333 | /* Get a hex digit from the remote system & return its value. If |
| 334 | ignore_space is nonzero, ignore spaces (not newline, tab, etc). */ |
| 335 | |
| 336 | int |
| 337 | sr_get_hex_digit (ignore_space) |
| 338 | int ignore_space; |
| 339 | { |
| 340 | int ch; |
| 341 | |
| 342 | while (1) |
| 343 | { |
| 344 | ch = sr_readchar (); |
| 345 | if (ch >= '0' && ch <= '9') |
| 346 | return ch - '0'; |
| 347 | else if (ch >= 'A' && ch <= 'F') |
| 348 | return ch - 'A' + 10; |
| 349 | else if (ch >= 'a' && ch <= 'f') |
| 350 | return ch - 'a' + 10; |
| 351 | else if (ch != ' ' || !ignore_space) |
| 352 | { |
| 353 | gr_expect_prompt (); |
| 354 | error ("Invalid hex digit from remote system."); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* Get a byte from the remote and put it in *BYT. Accept any number |
| 360 | leading spaces. */ |
| 361 | void |
| 362 | sr_get_hex_byte (byt) |
| 363 | char *byt; |
| 364 | { |
| 365 | int val; |
| 366 | |
| 367 | val = sr_get_hex_digit (1) << 4; |
| 368 | val |= sr_get_hex_digit (0); |
| 369 | *byt = val; |
| 370 | } |
| 371 | |
| 372 | /* Read a 32-bit hex word from the remote, preceded by a space */ |
| 373 | long |
| 374 | sr_get_hex_word () |
| 375 | { |
| 376 | long val; |
| 377 | int j; |
| 378 | |
| 379 | val = 0; |
| 380 | for (j = 0; j < 8; j++) |
| 381 | val = (val << 4) + sr_get_hex_digit (j == 0); |
| 382 | return val; |
| 383 | } |
| 384 | |
| 385 | /* Put a command string, in args, out to the remote. The remote is assumed to |
| 386 | be in raw mode, all writing/reading done through desc. |
| 387 | Ouput from the remote is placed on the users terminal until the |
| 388 | prompt from the remote is seen. |
| 389 | FIXME: Can't handle commands that take input. */ |
| 390 | |
| 391 | static void |
| 392 | sr_com (args, fromtty) |
| 393 | char *args; |
| 394 | int fromtty; |
| 395 | { |
| 396 | sr_check_open (); |
| 397 | |
| 398 | if (!args) |
| 399 | return; |
| 400 | |
| 401 | /* Clear all input so only command relative output is displayed */ |
| 402 | |
| 403 | sr_write_cr (args); |
| 404 | sr_write ("\030", 1); |
| 405 | registers_changed (); |
| 406 | gr_expect_prompt (); |
| 407 | } |
| 408 | |
| 409 | void |
| 410 | gr_close(quitting) |
| 411 | int quitting; |
| 412 | { |
| 413 | gr_clear_all_breakpoints(); |
| 414 | |
| 415 | if (sr_is_open()) |
| 416 | { |
| 417 | SERIAL_CLOSE (sr_get_desc()); |
| 418 | sr_set_desc(NULL); |
| 419 | } |
| 420 | |
| 421 | return; |
| 422 | } |
| 423 | |
| 424 | /* gr_detach() |
| 425 | takes a program previously attached to and detaches it. |
| 426 | We better not have left any breakpoints |
| 427 | in the program or it'll die when it hits one. |
| 428 | Close the open connection to the remote debugger. |
| 429 | Use this when you want to detach and do something else |
| 430 | with your gdb. */ |
| 431 | |
| 432 | void |
| 433 | gr_detach(args, from_tty) |
| 434 | char *args; |
| 435 | int from_tty; |
| 436 | { |
| 437 | if (args) |
| 438 | error ("Argument given to \"detach\" when remotely debugging."); |
| 439 | |
| 440 | if (sr_is_open()) |
| 441 | gr_clear_all_breakpoints (); |
| 442 | |
| 443 | pop_target (); |
| 444 | if (from_tty) |
| 445 | puts_filtered ("Ending remote debugging.\n"); |
| 446 | |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | void |
| 451 | gr_files_info (ops) |
| 452 | struct target_ops *ops; |
| 453 | { |
| 454 | #ifdef __GO32__ |
| 455 | printf_filtered ("\tAttached to DOS asynctsr\n"); |
| 456 | #else |
| 457 | printf_filtered ("\tAttached to %s", sr_get_device()); |
| 458 | if (baud_rate != -1) |
| 459 | printf_filtered ("at %d baud", baud_rate); |
| 460 | printf_filtered ("\n"); |
| 461 | #endif |
| 462 | |
| 463 | if (exec_bfd) |
| 464 | { |
| 465 | printf_filtered ("\tand running program %s\n", |
| 466 | bfd_get_filename (exec_bfd)); |
| 467 | } |
| 468 | printf_filtered ("\tusing the %s protocol.\n", ops->to_shortname); |
| 469 | } |
| 470 | |
| 471 | void |
| 472 | gr_mourn () |
| 473 | { |
| 474 | gr_clear_all_breakpoints (); |
| 475 | unpush_target (gr_get_ops()); |
| 476 | generic_mourn_inferior (); |
| 477 | } |
| 478 | |
| 479 | void |
| 480 | gr_kill () |
| 481 | { |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | /* This is called not only when we first attach, but also when the |
| 486 | user types "run" after having attached. */ |
| 487 | void |
| 488 | gr_create_inferior (execfile, args, env) |
| 489 | char *execfile; |
| 490 | char *args; |
| 491 | char **env; |
| 492 | { |
| 493 | int entry_pt; |
| 494 | |
| 495 | if (args && *args) |
| 496 | error ("Can't pass arguments to remote process."); |
| 497 | |
| 498 | if (execfile == 0 || exec_bfd == 0) |
| 499 | error ("No executable file specified"); |
| 500 | |
| 501 | entry_pt = (int) bfd_get_start_address (exec_bfd); |
| 502 | sr_check_open (); |
| 503 | |
| 504 | gr_kill (); |
| 505 | gr_clear_all_breakpoints (); |
| 506 | |
| 507 | init_wait_for_inferior (); |
| 508 | gr_checkin(); |
| 509 | |
| 510 | insert_breakpoints (); /* Needed to get correct instruction in cache */ |
| 511 | proceed (entry_pt, -1, 0); |
| 512 | } |
| 513 | |
| 514 | /* Given a null terminated list of strings LIST, read the input until we find one of |
| 515 | them. Return the index of the string found or -1 on error. '?' means match |
| 516 | any single character. Note that with the algorithm we use, the initial |
| 517 | character of the string cannot recur in the string, or we will not find some |
| 518 | cases of the string in the input. If PASSTHROUGH is non-zero, then |
| 519 | pass non-matching data on. */ |
| 520 | |
| 521 | int |
| 522 | gr_multi_scan (list, passthrough) |
| 523 | char *list[]; |
| 524 | int passthrough; |
| 525 | { |
| 526 | char *swallowed = NULL; /* holding area */ |
| 527 | char *swallowed_p = swallowed; /* Current position in swallowed. */ |
| 528 | int ch; |
| 529 | int ch_handled; |
| 530 | int i; |
| 531 | int string_count; |
| 532 | int max_length; |
| 533 | char **plist; |
| 534 | |
| 535 | /* Look through the strings. Count them. Find the largest one so we can |
| 536 | allocate a holding area. */ |
| 537 | |
| 538 | for (max_length = string_count = i = 0; |
| 539 | list[i] != NULL; |
| 540 | ++i, ++string_count) |
| 541 | { |
| 542 | int length = strlen(list[i]); |
| 543 | |
| 544 | if (length > max_length) |
| 545 | max_length = length; |
| 546 | } |
| 547 | |
| 548 | /* if we have no strings, then something is wrong. */ |
| 549 | if (string_count == 0) |
| 550 | return(-1); |
| 551 | |
| 552 | /* otherwise, we will need a holding area big enough to hold almost two |
| 553 | copies of our largest string. */ |
| 554 | swallowed_p = swallowed = alloca(max_length << 1); |
| 555 | |
| 556 | /* and a list of pointers to current scan points. */ |
| 557 | plist = (char **) alloca (string_count * sizeof(*plist)); |
| 558 | |
| 559 | /* and initialize */ |
| 560 | for (i = 0; i < string_count; ++i) |
| 561 | plist[i] = list[i]; |
| 562 | |
| 563 | for (ch = sr_readchar(); /* loop forever */ ; ch = sr_readchar()) |
| 564 | { |
| 565 | QUIT; /* Let user quit and leave process running */ |
| 566 | ch_handled = 0; |
| 567 | |
| 568 | for (i = 0; i < string_count; ++i) |
| 569 | { |
| 570 | if (ch == *plist[i] || *plist[i] == '?') |
| 571 | { |
| 572 | ++plist[i]; |
| 573 | if (*plist[i] == '\0') |
| 574 | return(i); |
| 575 | |
| 576 | if (!ch_handled) |
| 577 | *swallowed_p++ = ch; |
| 578 | |
| 579 | ch_handled = 1; |
| 580 | } |
| 581 | else |
| 582 | plist[i] = list[i]; |
| 583 | } |
| 584 | |
| 585 | if (!ch_handled) |
| 586 | { |
| 587 | char *p; |
| 588 | |
| 589 | /* Print out any characters which have been swallowed. */ |
| 590 | if (passthrough) |
| 591 | { |
| 592 | for (p = swallowed; p < swallowed_p; ++p) |
| 593 | fputc_unfiltered (*p, gdb_stdout); |
| 594 | |
| 595 | fputc_unfiltered (ch, gdb_stdout); |
| 596 | } |
| 597 | |
| 598 | swallowed_p = swallowed; |
| 599 | } |
| 600 | } |
| 601 | #if 0 |
| 602 | /* Never reached. */ |
| 603 | return(-1); |
| 604 | #endif |
| 605 | } |
| 606 | |
| 607 | /* Get ready to modify the registers array. On machines which store |
| 608 | individual registers, this doesn't need to do anything. On machines |
| 609 | which store all the registers in one fell swoop, this makes sure |
| 610 | that registers contains all the registers from the program being |
| 611 | debugged. */ |
| 612 | |
| 613 | void |
| 614 | gr_prepare_to_store () |
| 615 | { |
| 616 | /* Do nothing, since we assume we can store individual regs */ |
| 617 | } |
| 618 | |
| 619 | /* Read a word from remote address ADDR and return it. |
| 620 | * This goes through the data cache. |
| 621 | */ |
| 622 | int |
| 623 | gr_fetch_word (addr) |
| 624 | CORE_ADDR addr; |
| 625 | { |
| 626 | return dcache_fetch (gr_get_dcache(), addr); |
| 627 | } |
| 628 | |
| 629 | /* Write a word WORD into remote address ADDR. |
| 630 | This goes through the data cache. */ |
| 631 | |
| 632 | void |
| 633 | gr_store_word (addr, word) |
| 634 | CORE_ADDR addr; |
| 635 | int word; |
| 636 | { |
| 637 | dcache_poke (gr_get_dcache(), addr, word); |
| 638 | } |
| 639 | |
| 640 | void |
| 641 | _initialize_sr_support () |
| 642 | { |
| 643 | /* FIXME-now: if target is open... */ |
| 644 | add_show_from_set (add_set_cmd ("remotedevice", no_class, |
| 645 | var_filename, (char *)&sr_settings.device, |
| 646 | "Set device for remote serial I/O.\n\ |
| 647 | This device is used as the serial port when debugging using remote\n\ |
| 648 | targets.", &setlist), |
| 649 | &showlist); |
| 650 | |
| 651 | add_com ("remote <command>", class_obscure, sr_com, |
| 652 | "Send a command to the remote monitor."); |
| 653 | |
| 654 | } |