| 1 | /* Remote debugging interface for boot monitors, for GDB. |
| 2 | Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Support. Written by Rob Savoye for Cygnus. |
| 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 20 | |
| 21 | /* This file was derived from various remote-* modules. It is a collection |
| 22 | of generic support functions so GDB can talk directly to a ROM based |
| 23 | monitor. This saves use from having to hack an exception based handler |
| 24 | into existance, and makes for quick porting. |
| 25 | |
| 26 | This module talks to a debug monitor called 'MONITOR', which |
| 27 | We communicate with MONITOR via either a direct serial line, or a TCP |
| 28 | (or possibly TELNET) stream to a terminal multiplexor, |
| 29 | which in turn talks to the target board. */ |
| 30 | |
| 31 | #include "defs.h" |
| 32 | #include "gdbcore.h" |
| 33 | #include "target.h" |
| 34 | #include "wait.h" |
| 35 | #ifdef ANSI_PROTOTYPES |
| 36 | #include <stdarg.h> |
| 37 | #else |
| 38 | #include <varargs.h> |
| 39 | #endif |
| 40 | #include <signal.h> |
| 41 | #include <string.h> |
| 42 | #include <sys/types.h> |
| 43 | #include "command.h" |
| 44 | #include "serial.h" |
| 45 | #include "monitor.h" |
| 46 | #include "gdbcmd.h" |
| 47 | #include "inferior.h" |
| 48 | #include "regex.h" |
| 49 | #include "dcache.h" |
| 50 | |
| 51 | static int readchar PARAMS ((int timeout)); |
| 52 | |
| 53 | static void monitor_command PARAMS ((char *args, int fromtty)); |
| 54 | static void monitor_load_srec PARAMS ((char *args)); |
| 55 | |
| 56 | static int monitor_make_srec PARAMS ((char *buffer, int type, |
| 57 | CORE_ADDR memaddr, |
| 58 | unsigned char *myaddr, int len)); |
| 59 | |
| 60 | static void monitor_fetch_register PARAMS ((int regno)); |
| 61 | static void monitor_store_register PARAMS ((int regno)); |
| 62 | |
| 63 | static void monitor_close PARAMS ((int quitting)); |
| 64 | static void monitor_detach PARAMS ((char *args, int from_tty)); |
| 65 | static void monitor_resume PARAMS ((int pid, int step, enum target_signal sig)); |
| 66 | static void monitor_interrupt PARAMS ((int signo)); |
| 67 | static void monitor_interrupt_twice PARAMS ((int signo)); |
| 68 | static void monitor_interrupt_query PARAMS ((void)); |
| 69 | static void monitor_wait_cleanup PARAMS ((int old_timeout)); |
| 70 | |
| 71 | static int monitor_wait PARAMS ((int pid, struct target_waitstatus *status)); |
| 72 | static void monitor_fetch_registers PARAMS ((int regno)); |
| 73 | static void monitor_store_registers PARAMS ((int regno)); |
| 74 | static void monitor_prepare_to_store PARAMS ((void)); |
| 75 | static int monitor_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, int write, struct target_ops *target)); |
| 76 | static void monitor_files_info PARAMS ((struct target_ops *ops)); |
| 77 | static int monitor_insert_breakpoint PARAMS ((CORE_ADDR addr, char *shadow)); |
| 78 | static int monitor_remove_breakpoint PARAMS ((CORE_ADDR addr, char *shadow)); |
| 79 | static void monitor_kill PARAMS ((void)); |
| 80 | static void monitor_load PARAMS ((char *file, int from_tty)); |
| 81 | static void monitor_mourn_inferior PARAMS ((void)); |
| 82 | static void monitor_stop PARAMS ((void)); |
| 83 | |
| 84 | static int monitor_read_memory PARAMS ((CORE_ADDR addr, char *myaddr,int len)); |
| 85 | static int monitor_write_memory PARAMS ((CORE_ADDR addr, char *myaddr,int len)); |
| 86 | |
| 87 | static int from_hex PARAMS ((int a)); |
| 88 | static unsigned long get_hex_word PARAMS ((void)); |
| 89 | |
| 90 | static struct monitor_ops *current_monitor; |
| 91 | |
| 92 | static int hashmark; /* flag set by "set hash" */ |
| 93 | |
| 94 | static int timeout = 30; |
| 95 | |
| 96 | static int in_monitor_wait = 0; /* Non-zero means we are in monitor_wait() */ |
| 97 | |
| 98 | static void (*ofunc)(); /* Old SIGINT signal handler */ |
| 99 | |
| 100 | /* Descriptor for I/O to remote machine. Initialize it to NULL so |
| 101 | that monitor_open knows that we don't have a file open when the |
| 102 | program starts. */ |
| 103 | |
| 104 | static serial_t monitor_desc = NULL; |
| 105 | |
| 106 | /* Pointer to regexp pattern matching data */ |
| 107 | |
| 108 | static struct re_pattern_buffer register_pattern; |
| 109 | |
| 110 | /* Element 0 points to start of register name, and element 1 points to the |
| 111 | start of the register value. */ |
| 112 | |
| 113 | static struct re_registers register_strings; |
| 114 | |
| 115 | static char fastmap[256]; |
| 116 | |
| 117 | static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when |
| 118 | monitor_wait wakes up. */ |
| 119 | |
| 120 | static DCACHE *remote_dcache; |
| 121 | |
| 122 | /* monitor_printf_noecho -- Send data to monitor, but don't expect an echo. |
| 123 | Works just like printf. */ |
| 124 | |
| 125 | void |
| 126 | #ifdef ANSI_PROTOTYPES |
| 127 | monitor_printf_noecho (char *pattern, ...) |
| 128 | #else |
| 129 | monitor_printf_noecho (va_alist) |
| 130 | va_dcl |
| 131 | #endif |
| 132 | { |
| 133 | va_list args; |
| 134 | char sndbuf[2000]; |
| 135 | int len; |
| 136 | |
| 137 | #if ANSI_PROTOTYPES |
| 138 | va_start (args, pattern); |
| 139 | #else |
| 140 | char *pattern; |
| 141 | va_start (args); |
| 142 | pattern = va_arg (args, char *); |
| 143 | #endif |
| 144 | |
| 145 | vsprintf (sndbuf, pattern, args); |
| 146 | |
| 147 | if (remote_debug > 0) |
| 148 | fputs_unfiltered (sndbuf, gdb_stderr); |
| 149 | |
| 150 | len = strlen (sndbuf); |
| 151 | |
| 152 | if (len + 1 > sizeof sndbuf) |
| 153 | abort (); |
| 154 | |
| 155 | if (SERIAL_WRITE(monitor_desc, sndbuf, len)) |
| 156 | fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno)); |
| 157 | } |
| 158 | |
| 159 | /* monitor_printf -- Send data to monitor and check the echo. Works just like |
| 160 | printf. */ |
| 161 | |
| 162 | void |
| 163 | #ifdef ANSI_PROTOTYPES |
| 164 | monitor_printf (char *pattern, ...) |
| 165 | #else |
| 166 | monitor_printf (va_alist) |
| 167 | va_dcl |
| 168 | #endif |
| 169 | { |
| 170 | va_list args; |
| 171 | char sndbuf[2000]; |
| 172 | int len; |
| 173 | int i, c; |
| 174 | |
| 175 | #ifdef ANSI_PROTOTYPES |
| 176 | va_start (args, pattern); |
| 177 | #else |
| 178 | char *pattern; |
| 179 | va_start (args); |
| 180 | pattern = va_arg (args, char *); |
| 181 | #endif |
| 182 | |
| 183 | vsprintf (sndbuf, pattern, args); |
| 184 | |
| 185 | if (remote_debug > 0) |
| 186 | fputs_unfiltered (sndbuf, gdb_stderr); |
| 187 | |
| 188 | len = strlen (sndbuf); |
| 189 | |
| 190 | if (len + 1 > sizeof sndbuf) |
| 191 | abort (); |
| 192 | |
| 193 | if (SERIAL_WRITE(monitor_desc, sndbuf, len)) |
| 194 | fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno)); |
| 195 | |
| 196 | for (i = 0; i < len; i++) |
| 197 | { |
| 198 | trycr: |
| 199 | c = readchar (timeout); |
| 200 | |
| 201 | if (c != sndbuf[i]) |
| 202 | { |
| 203 | /* Don't fail if we sent a ^C, they're never echoed */ |
| 204 | if (sndbuf[i] == '\003') |
| 205 | continue; |
| 206 | #if 0 |
| 207 | if (sndbuf[i] == '\r' |
| 208 | && c == '\n') |
| 209 | goto trycr; |
| 210 | #endif |
| 211 | warning ("monitor_printf: Bad echo. Sent: \"%s\", Got: \"%.*s%c\".", |
| 212 | sndbuf, i, sndbuf, c); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | /* Read a character from the remote system, doing all the fancy |
| 218 | timeout stuff. */ |
| 219 | |
| 220 | static int |
| 221 | readchar (timeout) |
| 222 | int timeout; |
| 223 | { |
| 224 | int c; |
| 225 | |
| 226 | c = SERIAL_READCHAR (monitor_desc, timeout); |
| 227 | |
| 228 | if (remote_debug > 0) |
| 229 | fputc_unfiltered (c, gdb_stderr); |
| 230 | |
| 231 | if (c >= 0) |
| 232 | return c & 0x7f; |
| 233 | |
| 234 | if (c == SERIAL_TIMEOUT) |
| 235 | #ifdef MAINTENANCE_CMDS |
| 236 | if (in_monitor_wait) /* Watchdog went off */ |
| 237 | { |
| 238 | target_mourn_inferior (); |
| 239 | error ("Watchdog has expired. Target detached.\n"); |
| 240 | } |
| 241 | else |
| 242 | #endif |
| 243 | error ("Timeout reading from remote system."); |
| 244 | |
| 245 | perror_with_name ("remote-monitor"); |
| 246 | } |
| 247 | |
| 248 | /* Scan input from the remote system, until STRING is found. If BUF is non- |
| 249 | zero, then collect input until we have collected either STRING or BUFLEN-1 |
| 250 | chars. In either case we terminate BUF with a 0. If input overflows BUF |
| 251 | because STRING can't be found, return -1, else return number of chars in BUF |
| 252 | (minus the terminating NUL). Note that in the non-overflow case, STRING |
| 253 | will be at the end of BUF. */ |
| 254 | |
| 255 | int |
| 256 | monitor_expect (string, buf, buflen) |
| 257 | char *string; |
| 258 | char *buf; |
| 259 | int buflen; |
| 260 | { |
| 261 | char *p = string; |
| 262 | int obuflen = buflen; |
| 263 | int c; |
| 264 | |
| 265 | immediate_quit = 1; |
| 266 | while (1) |
| 267 | { |
| 268 | if (buf) |
| 269 | { |
| 270 | if (buflen < 2) |
| 271 | { |
| 272 | *buf = '\000'; |
| 273 | immediate_quit = 0; |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | c = readchar (timeout); |
| 278 | *buf++ = c; |
| 279 | buflen--; |
| 280 | } |
| 281 | else |
| 282 | c = readchar (timeout); |
| 283 | |
| 284 | if (c == *p++) |
| 285 | { |
| 286 | if (*p == '\0') |
| 287 | { |
| 288 | immediate_quit = 0; |
| 289 | |
| 290 | if (buf) |
| 291 | { |
| 292 | *buf++ = '\000'; |
| 293 | return obuflen - buflen; |
| 294 | } |
| 295 | else |
| 296 | return 0; |
| 297 | } |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | p = string; |
| 302 | if (c == *p) |
| 303 | p++; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* Keep discarding input until we see the MONITOR prompt. |
| 309 | |
| 310 | The convention for dealing with the prompt is that you |
| 311 | o give your command |
| 312 | o *then* wait for the prompt. |
| 313 | |
| 314 | Thus the last thing that a procedure does with the serial line |
| 315 | will be an monitor_expect_prompt(). Exception: monitor_resume does not |
| 316 | wait for the prompt, because the terminal is being handed over |
| 317 | to the inferior. However, the next thing which happens after that |
| 318 | is a monitor_wait which does wait for the prompt. |
| 319 | Note that this includes abnormal exit, e.g. error(). This is |
| 320 | necessary to prevent getting into states from which we can't |
| 321 | recover. */ |
| 322 | |
| 323 | int |
| 324 | monitor_expect_prompt (buf, buflen) |
| 325 | char *buf; |
| 326 | int buflen; |
| 327 | { |
| 328 | return monitor_expect (PROMPT, buf, buflen); |
| 329 | } |
| 330 | |
| 331 | /* Get N 32-bit words from remote, each preceded by a space, and put |
| 332 | them in registers starting at REGNO. */ |
| 333 | |
| 334 | static unsigned long |
| 335 | get_hex_word () |
| 336 | { |
| 337 | unsigned long val; |
| 338 | int i; |
| 339 | int ch; |
| 340 | |
| 341 | do |
| 342 | ch = readchar (timeout); |
| 343 | while (isspace(ch)); |
| 344 | |
| 345 | val = from_hex (ch); |
| 346 | |
| 347 | for (i = 7; i >= 1; i--) |
| 348 | { |
| 349 | ch = readchar (timeout); |
| 350 | if (!isxdigit (ch)) |
| 351 | break; |
| 352 | val = (val << 4) | from_hex (ch); |
| 353 | } |
| 354 | |
| 355 | return val; |
| 356 | } |
| 357 | |
| 358 | /* Open a connection to a remote debugger. NAME is the filename used |
| 359 | for communication. */ |
| 360 | |
| 361 | static char *dev_name; |
| 362 | static struct target_ops *targ_ops; |
| 363 | |
| 364 | void |
| 365 | monitor_open (args, mon_ops, from_tty) |
| 366 | char *args; |
| 367 | struct monitor_ops *mon_ops; |
| 368 | int from_tty; |
| 369 | { |
| 370 | char *name; |
| 371 | int i; |
| 372 | char **p; |
| 373 | |
| 374 | if (mon_ops->magic != MONITOR_OPS_MAGIC) |
| 375 | error ("Magic number of monitor_ops struct wrong."); |
| 376 | |
| 377 | targ_ops = mon_ops->target; |
| 378 | name = targ_ops->to_shortname; |
| 379 | |
| 380 | if (!args) |
| 381 | error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\ |
| 382 | `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name); |
| 383 | |
| 384 | target_preopen (from_tty); |
| 385 | |
| 386 | /* Setup pattern for register dump */ |
| 387 | |
| 388 | if (mon_ops->register_pattern) |
| 389 | { |
| 390 | int tmp; |
| 391 | char *val; |
| 392 | |
| 393 | register_pattern.fastmap = fastmap; |
| 394 | tmp = re_set_syntax (RE_SYNTAX_EMACS); |
| 395 | val = re_compile_pattern (mon_ops->register_pattern, |
| 396 | strlen (mon_ops->register_pattern), |
| 397 | ®ister_pattern); |
| 398 | re_set_syntax (tmp); |
| 399 | if (val) |
| 400 | error ("Can't compiler register pattern string: %s!", val); |
| 401 | re_compile_fastmap (®ister_pattern); |
| 402 | } |
| 403 | |
| 404 | unpush_target (targ_ops); |
| 405 | |
| 406 | if (dev_name) |
| 407 | free (dev_name); |
| 408 | dev_name = strsave (args); |
| 409 | |
| 410 | monitor_desc = SERIAL_OPEN (dev_name); |
| 411 | |
| 412 | if (!monitor_desc) |
| 413 | perror_with_name (dev_name); |
| 414 | |
| 415 | if (baud_rate != -1) |
| 416 | { |
| 417 | if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) |
| 418 | { |
| 419 | SERIAL_CLOSE (monitor_desc); |
| 420 | perror_with_name (dev_name); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | SERIAL_RAW (monitor_desc); |
| 425 | |
| 426 | SERIAL_FLUSH_INPUT (monitor_desc); |
| 427 | |
| 428 | /* some systems only work with 2 stop bits */ |
| 429 | |
| 430 | SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits); |
| 431 | |
| 432 | current_monitor = mon_ops; |
| 433 | |
| 434 | /* See if we can wake up the monitor. First, try sending a stop sequence, |
| 435 | then send the init strings. Last, remove all breakpoints. */ |
| 436 | |
| 437 | if (current_monitor->stop) |
| 438 | { |
| 439 | monitor_stop (); |
| 440 | monitor_expect_prompt (NULL, 0); |
| 441 | } |
| 442 | |
| 443 | /* wake up the monitor and see if it's alive */ |
| 444 | for (p = mon_ops->init; *p != NULL; p++) |
| 445 | { |
| 446 | monitor_printf (*p); |
| 447 | monitor_expect_prompt (NULL, 0); |
| 448 | } |
| 449 | |
| 450 | SERIAL_FLUSH_INPUT (monitor_desc); |
| 451 | |
| 452 | /* Remove all breakpoints */ |
| 453 | |
| 454 | if (mon_ops->clr_all_break) |
| 455 | { |
| 456 | monitor_printf (mon_ops->clr_all_break); |
| 457 | monitor_expect_prompt (NULL, 0); |
| 458 | } |
| 459 | |
| 460 | if (from_tty) |
| 461 | printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name); |
| 462 | |
| 463 | push_target (targ_ops); |
| 464 | |
| 465 | inferior_pid = 42000; /* Make run command think we are busy... */ |
| 466 | |
| 467 | /* Give monitor_wait something to read */ |
| 468 | |
| 469 | monitor_printf (current_monitor->line_term); |
| 470 | |
| 471 | remote_dcache = dcache_init (monitor_read_memory, monitor_write_memory); |
| 472 | |
| 473 | start_remote (); |
| 474 | } |
| 475 | |
| 476 | /* Close out all files and local state before this target loses |
| 477 | control. */ |
| 478 | |
| 479 | static void |
| 480 | monitor_close (quitting) |
| 481 | int quitting; |
| 482 | { |
| 483 | if (monitor_desc) |
| 484 | SERIAL_CLOSE (monitor_desc); |
| 485 | monitor_desc = NULL; |
| 486 | } |
| 487 | |
| 488 | /* Terminate the open connection to the remote debugger. Use this |
| 489 | when you want to detach and do something else with your gdb. */ |
| 490 | |
| 491 | static void |
| 492 | monitor_detach (args, from_tty) |
| 493 | char *args; |
| 494 | int from_tty; |
| 495 | { |
| 496 | pop_target (); /* calls monitor_close to do the real work */ |
| 497 | if (from_tty) |
| 498 | printf_unfiltered ("Ending remote %s debugging\n", target_shortname); |
| 499 | } |
| 500 | |
| 501 | /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */ |
| 502 | |
| 503 | char * |
| 504 | monitor_supply_register (regno, valstr) |
| 505 | int regno; |
| 506 | char *valstr; |
| 507 | { |
| 508 | unsigned LONGEST val; |
| 509 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; |
| 510 | char *p; |
| 511 | |
| 512 | val = strtoul (valstr, &p, 16); |
| 513 | |
| 514 | if (val == 0 && valstr == p) |
| 515 | error ("monitor_supply_register (%d): bad value from monitor: %s.", |
| 516 | regno, valstr); |
| 517 | |
| 518 | /* supply register stores in target byte order, so swap here */ |
| 519 | |
| 520 | store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val); |
| 521 | |
| 522 | supply_register (regno, regbuf); |
| 523 | |
| 524 | return p; |
| 525 | } |
| 526 | |
| 527 | /* Tell the remote machine to resume. */ |
| 528 | |
| 529 | static void |
| 530 | monitor_resume (pid, step, sig) |
| 531 | int pid, step; |
| 532 | enum target_signal sig; |
| 533 | { |
| 534 | dcache_flush (remote_dcache); |
| 535 | if (step) |
| 536 | monitor_printf (STEP_CMD); |
| 537 | else |
| 538 | { |
| 539 | monitor_printf (CONT_CMD); |
| 540 | if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT) |
| 541 | dump_reg_flag = 1; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* Parse the output of a register dump command. A monitor specific regexp is |
| 546 | used to extract individual register descriptions of the form REG=VAL. Each |
| 547 | description is split up into a name and a value string which are passed down |
| 548 | to monitor specific code. */ |
| 549 | |
| 550 | static char * |
| 551 | parse_register_dump (buf, len) |
| 552 | char *buf; |
| 553 | int len; |
| 554 | { |
| 555 | while (1) |
| 556 | { |
| 557 | int regnamelen, vallen; |
| 558 | char *regname, *val; |
| 559 | |
| 560 | if (re_search (®ister_pattern, buf, len, 0, len, |
| 561 | ®ister_strings) == -1) |
| 562 | break; |
| 563 | |
| 564 | regnamelen = register_strings.end[1] - register_strings.start[1]; |
| 565 | regname = buf + register_strings.start[1]; |
| 566 | vallen = register_strings.end[2] - register_strings.start[2]; |
| 567 | val = buf + register_strings.start[2]; |
| 568 | |
| 569 | current_monitor->supply_register (regname, regnamelen, val, vallen); |
| 570 | |
| 571 | buf += register_strings.end[0]; |
| 572 | len -= register_strings.end[0]; |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /* Send ^C to target to halt it. Target will respond, and send us a |
| 577 | packet. */ |
| 578 | |
| 579 | static void |
| 580 | monitor_interrupt (signo) |
| 581 | int signo; |
| 582 | { |
| 583 | /* If this doesn't work, try more severe steps. */ |
| 584 | signal (signo, monitor_interrupt_twice); |
| 585 | |
| 586 | if (remote_debug) |
| 587 | printf_unfiltered ("monitor_interrupt called\n"); |
| 588 | |
| 589 | target_stop (); |
| 590 | } |
| 591 | |
| 592 | /* The user typed ^C twice. */ |
| 593 | |
| 594 | static void |
| 595 | monitor_interrupt_twice (signo) |
| 596 | int signo; |
| 597 | { |
| 598 | signal (signo, ofunc); |
| 599 | |
| 600 | monitor_interrupt_query (); |
| 601 | |
| 602 | signal (signo, monitor_interrupt); |
| 603 | } |
| 604 | |
| 605 | /* Ask the user what to do when an interrupt is received. */ |
| 606 | |
| 607 | static void |
| 608 | monitor_interrupt_query () |
| 609 | { |
| 610 | target_terminal_ours (); |
| 611 | |
| 612 | if (query ("Interrupted while waiting for the program.\n\ |
| 613 | Give up (and stop debugging it)? ")) |
| 614 | { |
| 615 | target_mourn_inferior (); |
| 616 | return_to_top_level (RETURN_QUIT); |
| 617 | } |
| 618 | |
| 619 | target_terminal_inferior (); |
| 620 | } |
| 621 | |
| 622 | static void |
| 623 | monitor_wait_cleanup (old_timeout) |
| 624 | int old_timeout; |
| 625 | { |
| 626 | timeout = old_timeout; |
| 627 | signal (SIGINT, ofunc); |
| 628 | in_monitor_wait = 0; |
| 629 | } |
| 630 | |
| 631 | /* Wait until the remote machine stops, then return, storing status in |
| 632 | status just as `wait' would. */ |
| 633 | |
| 634 | static int |
| 635 | monitor_wait (pid, status) |
| 636 | int pid; |
| 637 | struct target_waitstatus *status; |
| 638 | { |
| 639 | int old_timeout = timeout; |
| 640 | char buf[1024]; |
| 641 | int resp_len; |
| 642 | struct cleanup *old_chain; |
| 643 | |
| 644 | status->kind = TARGET_WAITKIND_EXITED; |
| 645 | status->value.integer = 0; |
| 646 | |
| 647 | old_chain = make_cleanup (monitor_wait_cleanup, old_timeout); |
| 648 | |
| 649 | #ifdef MAINTENANCE_CMDS |
| 650 | in_monitor_wait = 1; |
| 651 | timeout = watchdog > 0 ? watchdog : -1; |
| 652 | #else |
| 653 | timeout = -1; /* Don't time out -- user program is running. */ |
| 654 | #endif |
| 655 | |
| 656 | ofunc = (void (*)()) signal (SIGINT, monitor_interrupt); |
| 657 | |
| 658 | do |
| 659 | { |
| 660 | resp_len = monitor_expect_prompt (buf, sizeof (buf)); |
| 661 | |
| 662 | if (resp_len <= 0) |
| 663 | fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf); |
| 664 | } |
| 665 | while (resp_len < 0); |
| 666 | |
| 667 | signal (SIGINT, ofunc); |
| 668 | |
| 669 | timeout = old_timeout; |
| 670 | |
| 671 | if (dump_reg_flag && current_monitor->dump_registers) |
| 672 | { |
| 673 | dump_reg_flag = 0; |
| 674 | |
| 675 | monitor_printf (current_monitor->dump_registers); |
| 676 | resp_len = monitor_expect_prompt (buf, sizeof (buf)); |
| 677 | } |
| 678 | |
| 679 | if (current_monitor->register_pattern) |
| 680 | parse_register_dump (buf, resp_len); |
| 681 | |
| 682 | status->kind = TARGET_WAITKIND_STOPPED; |
| 683 | status->value.sig = TARGET_SIGNAL_TRAP; |
| 684 | |
| 685 | discard_cleanups (old_chain); |
| 686 | |
| 687 | in_monitor_wait = 0; |
| 688 | |
| 689 | return inferior_pid; |
| 690 | } |
| 691 | |
| 692 | /* Fetch register REGNO, or all registers if REGNO is -1. Returns |
| 693 | errno value. */ |
| 694 | |
| 695 | static void |
| 696 | monitor_fetch_register (regno) |
| 697 | int regno; |
| 698 | { |
| 699 | char *name; |
| 700 | static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0}; |
| 701 | char regbuf[MAX_REGISTER_RAW_SIZE * 2 + 1]; |
| 702 | int i; |
| 703 | |
| 704 | name = REGNAMES (regno); |
| 705 | |
| 706 | if (!name) |
| 707 | { |
| 708 | supply_register (regno, zerobuf); |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | /* send the register examine command */ |
| 713 | |
| 714 | monitor_printf (current_monitor->getreg.cmd, name); |
| 715 | |
| 716 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for |
| 717 | the register value. Otherwise, we just start searching from the start of |
| 718 | the buf. */ |
| 719 | |
| 720 | if (current_monitor->getreg.resp_delim) |
| 721 | monitor_expect (current_monitor->getreg.resp_delim, NULL, 0); |
| 722 | |
| 723 | /* Now, read the appropriate number of hex digits for this register, skipping |
| 724 | spaces. */ |
| 725 | |
| 726 | for (i = 0; i < REGISTER_RAW_SIZE (regno) * 2; i++) |
| 727 | { |
| 728 | int c; |
| 729 | |
| 730 | while (1) |
| 731 | { |
| 732 | c = readchar (timeout); |
| 733 | if (isxdigit (c)) |
| 734 | break; |
| 735 | if (c == ' ') |
| 736 | continue; |
| 737 | |
| 738 | error ("monitor_fetch_register (%d): bad response from monitor: %.*s%c.", |
| 739 | regno, i, regbuf, c); |
| 740 | } |
| 741 | |
| 742 | regbuf[i] = c; |
| 743 | } |
| 744 | |
| 745 | regbuf[i] = '\000'; /* terminate the number */ |
| 746 | |
| 747 | /* If TERM is present, we wait for that to show up. Also, (if TERM is |
| 748 | present), we will send TERM_CMD if that is present. In any case, we collect |
| 749 | all of the output into buf, and then wait for the normal prompt. */ |
| 750 | |
| 751 | if (current_monitor->getreg.term) |
| 752 | { |
| 753 | monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */ |
| 754 | |
| 755 | if (current_monitor->getreg.term_cmd) |
| 756 | { |
| 757 | monitor_printf (current_monitor->getreg.term_cmd); |
| 758 | monitor_expect_prompt (NULL, 0); |
| 759 | } |
| 760 | } |
| 761 | else |
| 762 | monitor_expect_prompt (NULL, 0); /* get response */ |
| 763 | |
| 764 | monitor_supply_register (regno, regbuf); |
| 765 | } |
| 766 | |
| 767 | /* Read the remote registers into the block regs. */ |
| 768 | |
| 769 | static void monitor_dump_regs () |
| 770 | { |
| 771 | if (current_monitor->dump_registers) |
| 772 | { |
| 773 | char buf[200]; |
| 774 | int resp_len; |
| 775 | monitor_printf (current_monitor->dump_registers); |
| 776 | resp_len = monitor_expect_prompt (buf, sizeof (buf)); |
| 777 | parse_register_dump (buf, resp_len); |
| 778 | } |
| 779 | else |
| 780 | abort(); /* Need some way to read registers */ |
| 781 | } |
| 782 | |
| 783 | static void |
| 784 | monitor_fetch_registers (regno) |
| 785 | int regno; |
| 786 | { |
| 787 | if (current_monitor->getreg.cmd) |
| 788 | { |
| 789 | if (regno >= 0) |
| 790 | { |
| 791 | monitor_fetch_register (regno); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | for (regno = 0; regno < NUM_REGS; regno++) |
| 796 | monitor_fetch_register (regno); |
| 797 | } |
| 798 | else { |
| 799 | monitor_dump_regs (); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | /* Store register REGNO, or all if REGNO == 0. Return errno value. */ |
| 804 | |
| 805 | static void |
| 806 | monitor_store_register (regno) |
| 807 | int regno; |
| 808 | { |
| 809 | char *name; |
| 810 | unsigned LONGEST val; |
| 811 | |
| 812 | name = REGNAMES (regno); |
| 813 | if (!name) |
| 814 | return; |
| 815 | |
| 816 | val = read_register (regno); |
| 817 | |
| 818 | /* send the register deposit command */ |
| 819 | |
| 820 | monitor_printf (current_monitor->setreg.cmd, name, val); |
| 821 | |
| 822 | /* It's possible that there are actually some monitors out there that will |
| 823 | prompt you when you set a register. In that case, you may need to add some |
| 824 | code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get |
| 825 | an idea of what's needed...) */ |
| 826 | |
| 827 | monitor_expect_prompt (NULL, 0); |
| 828 | } |
| 829 | |
| 830 | /* Store the remote registers. */ |
| 831 | |
| 832 | static void |
| 833 | monitor_store_registers (regno) |
| 834 | int regno; |
| 835 | { |
| 836 | if (regno >= 0) |
| 837 | { |
| 838 | monitor_store_register (regno); |
| 839 | return; |
| 840 | } |
| 841 | |
| 842 | for (regno = 0; regno < NUM_REGS; regno++) |
| 843 | monitor_store_register (regno); |
| 844 | } |
| 845 | |
| 846 | /* Get ready to modify the registers array. On machines which store |
| 847 | individual registers, this doesn't need to do anything. On machines |
| 848 | which store all the registers in one fell swoop, this makes sure |
| 849 | that registers contains all the registers from the program being |
| 850 | debugged. */ |
| 851 | |
| 852 | static void |
| 853 | monitor_prepare_to_store () |
| 854 | { |
| 855 | /* Do nothing, since we can store individual regs */ |
| 856 | } |
| 857 | |
| 858 | static void |
| 859 | monitor_files_info (ops) |
| 860 | struct target_ops *ops; |
| 861 | { |
| 862 | printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate); |
| 863 | } |
| 864 | |
| 865 | static int |
| 866 | monitor_write_memory (memaddr, myaddr, len) |
| 867 | CORE_ADDR memaddr; |
| 868 | unsigned char *myaddr; |
| 869 | int len; |
| 870 | { |
| 871 | unsigned LONGEST val; |
| 872 | char *cmd; |
| 873 | int i; |
| 874 | |
| 875 | /* Use memory fill command for leading 0 bytes. */ |
| 876 | |
| 877 | if (current_monitor->fill) |
| 878 | { |
| 879 | for (i = 0; i < len; i++) |
| 880 | if (myaddr[i] != 0) |
| 881 | break; |
| 882 | |
| 883 | if (i > 4) /* More than 4 zeros is worth doing */ |
| 884 | { |
| 885 | if (current_monitor->flags & MO_FILL_USES_ADDR) |
| 886 | monitor_printf (current_monitor->fill, memaddr, memaddr + i, 0); |
| 887 | else |
| 888 | monitor_printf (current_monitor->fill, memaddr, i, 0); |
| 889 | |
| 890 | monitor_expect_prompt (NULL, 0); |
| 891 | |
| 892 | return i; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll) |
| 897 | { |
| 898 | len = 8; |
| 899 | cmd = current_monitor->setmem.cmdll; |
| 900 | } |
| 901 | else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl) |
| 902 | { |
| 903 | len = 4; |
| 904 | cmd = current_monitor->setmem.cmdl; |
| 905 | } |
| 906 | else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw) |
| 907 | { |
| 908 | len = 2; |
| 909 | cmd = current_monitor->setmem.cmdw; |
| 910 | } |
| 911 | else |
| 912 | { |
| 913 | len = 1; |
| 914 | cmd = current_monitor->setmem.cmdb; |
| 915 | } |
| 916 | |
| 917 | val = extract_unsigned_integer (myaddr, len); |
| 918 | |
| 919 | monitor_printf (cmd, memaddr, val); |
| 920 | |
| 921 | monitor_expect_prompt (NULL, 0); |
| 922 | |
| 923 | return len; |
| 924 | } |
| 925 | |
| 926 | /* This is an alternate form of monitor_read_memory which is used for monitors |
| 927 | which can only read a single byte/word/etc. at a time. */ |
| 928 | |
| 929 | static int |
| 930 | monitor_read_memory_single (memaddr, myaddr, len) |
| 931 | CORE_ADDR memaddr; |
| 932 | unsigned char *myaddr; |
| 933 | int len; |
| 934 | { |
| 935 | unsigned LONGEST val; |
| 936 | char membuf[sizeof(LONGEST) * 2 + 1]; |
| 937 | char *p; |
| 938 | char *cmd; |
| 939 | int i; |
| 940 | |
| 941 | if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll) |
| 942 | { |
| 943 | len = 8; |
| 944 | cmd = current_monitor->getmem.cmdll; |
| 945 | } |
| 946 | else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl) |
| 947 | { |
| 948 | len = 4; |
| 949 | cmd = current_monitor->getmem.cmdl; |
| 950 | } |
| 951 | else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw) |
| 952 | { |
| 953 | len = 2; |
| 954 | cmd = current_monitor->getmem.cmdw; |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | len = 1; |
| 959 | cmd = current_monitor->getmem.cmdb; |
| 960 | } |
| 961 | |
| 962 | /* Send the examine command. */ |
| 963 | |
| 964 | monitor_printf (cmd, memaddr); |
| 965 | |
| 966 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for |
| 967 | the register value. Otherwise, we just start searching from the start of |
| 968 | the buf. */ |
| 969 | |
| 970 | if (current_monitor->getmem.resp_delim) |
| 971 | monitor_expect (current_monitor->getmem.resp_delim, NULL, 0); |
| 972 | |
| 973 | /* Now, read the appropriate number of hex digits for this loc, skipping |
| 974 | spaces. */ |
| 975 | |
| 976 | for (i = 0; i < len * 2; i++) |
| 977 | { |
| 978 | int c; |
| 979 | |
| 980 | while (1) |
| 981 | { |
| 982 | c = readchar (timeout); |
| 983 | if (isxdigit (c)) |
| 984 | break; |
| 985 | if (c == ' ') |
| 986 | continue; |
| 987 | |
| 988 | error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.", |
| 989 | memaddr, i, membuf, c); |
| 990 | } |
| 991 | |
| 992 | membuf[i] = c; |
| 993 | } |
| 994 | |
| 995 | membuf[i] = '\000'; /* terminate the number */ |
| 996 | |
| 997 | /* If TERM is present, we wait for that to show up. Also, (if TERM is |
| 998 | present), we will send TERM_CMD if that is present. In any case, we collect |
| 999 | all of the output into buf, and then wait for the normal prompt. */ |
| 1000 | |
| 1001 | if (current_monitor->getmem.term) |
| 1002 | { |
| 1003 | monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */ |
| 1004 | |
| 1005 | if (current_monitor->getmem.term_cmd) |
| 1006 | { |
| 1007 | monitor_printf (current_monitor->getmem.term_cmd); |
| 1008 | monitor_expect_prompt (NULL, 0); |
| 1009 | } |
| 1010 | } |
| 1011 | else |
| 1012 | monitor_expect_prompt (NULL, 0); /* get response */ |
| 1013 | |
| 1014 | p = membuf; |
| 1015 | val = strtoul (membuf, &p, 16); |
| 1016 | |
| 1017 | if (val == 0 && membuf == p) |
| 1018 | error ("monitor_read_memory_single (0x%x): bad value from monitor: %s.", |
| 1019 | memaddr, membuf); |
| 1020 | |
| 1021 | /* supply register stores in target byte order, so swap here */ |
| 1022 | |
| 1023 | store_unsigned_integer (myaddr, len, val); |
| 1024 | |
| 1025 | return len; |
| 1026 | } |
| 1027 | |
| 1028 | /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory |
| 1029 | at MEMADDR. Returns length moved. Currently, we only do one byte at a |
| 1030 | time. */ |
| 1031 | |
| 1032 | static int |
| 1033 | monitor_read_memory (memaddr, myaddr, len) |
| 1034 | CORE_ADDR memaddr; |
| 1035 | char *myaddr; |
| 1036 | int len; |
| 1037 | { |
| 1038 | unsigned LONGEST val; |
| 1039 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; |
| 1040 | char buf[512]; |
| 1041 | char *p, *p1; |
| 1042 | char *name; |
| 1043 | int resp_len; |
| 1044 | int i; |
| 1045 | |
| 1046 | if (current_monitor->flags & MO_GETMEM_READ_SINGLE) |
| 1047 | return monitor_read_memory_single (memaddr, myaddr, len); |
| 1048 | |
| 1049 | len = min (len, 16); |
| 1050 | |
| 1051 | /* See if xfer would cross a 16 byte boundary. If so, clip it. */ |
| 1052 | if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0) |
| 1053 | len = ((memaddr + len) & ~0xf) - memaddr; |
| 1054 | |
| 1055 | /* send the memory examine command */ |
| 1056 | |
| 1057 | if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE) |
| 1058 | monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len - 1); |
| 1059 | else |
| 1060 | monitor_printf (current_monitor->getmem.cmdb, memaddr, len); |
| 1061 | |
| 1062 | /* If TERM is present, we wait for that to show up. Also, (if TERM is |
| 1063 | present), we will send TERM_CMD if that is present. In any case, we collect |
| 1064 | all of the output into buf, and then wait for the normal prompt. */ |
| 1065 | |
| 1066 | if (current_monitor->getmem.term) |
| 1067 | { |
| 1068 | resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */ |
| 1069 | |
| 1070 | if (resp_len <= 0) |
| 1071 | error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.", |
| 1072 | memaddr, resp_len, buf); |
| 1073 | |
| 1074 | if (current_monitor->getmem.term_cmd) |
| 1075 | { |
| 1076 | SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd, |
| 1077 | strlen (current_monitor->getmem.term_cmd)); |
| 1078 | monitor_expect_prompt (NULL, 0); |
| 1079 | } |
| 1080 | } |
| 1081 | else |
| 1082 | resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */ |
| 1083 | |
| 1084 | p = buf; |
| 1085 | |
| 1086 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for |
| 1087 | the values. Otherwise, we just start searching from the start of the buf. |
| 1088 | */ |
| 1089 | |
| 1090 | if (current_monitor->getmem.resp_delim) |
| 1091 | { |
| 1092 | p = strstr (p, current_monitor->getmem.resp_delim); |
| 1093 | if (!p) |
| 1094 | error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.", |
| 1095 | memaddr, resp_len, buf); |
| 1096 | p += strlen (current_monitor->getmem.resp_delim); |
| 1097 | } |
| 1098 | |
| 1099 | for (i = len; i > 0; i--) |
| 1100 | { |
| 1101 | /* Skip non-hex chars, but bomb on end of string and newlines */ |
| 1102 | |
| 1103 | while (1) |
| 1104 | { |
| 1105 | if (isxdigit (*p)) |
| 1106 | break; |
| 1107 | if (*p == '\000' || *p == '\n' || *p == '\r') |
| 1108 | error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr, resp_len, buf); |
| 1109 | p++; |
| 1110 | } |
| 1111 | |
| 1112 | val = strtoul (p, &p1, 16); |
| 1113 | |
| 1114 | if (val == 0 && p == p1) |
| 1115 | error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr, |
| 1116 | resp_len, buf); |
| 1117 | |
| 1118 | *myaddr++ = val; |
| 1119 | |
| 1120 | if (i == 1) |
| 1121 | break; |
| 1122 | |
| 1123 | p = p1; |
| 1124 | } |
| 1125 | |
| 1126 | return len; |
| 1127 | } |
| 1128 | |
| 1129 | static int |
| 1130 | monitor_xfer_memory (memaddr, myaddr, len, write, target) |
| 1131 | CORE_ADDR memaddr; |
| 1132 | char *myaddr; |
| 1133 | int len; |
| 1134 | int write; |
| 1135 | struct target_ops *target; /* ignored */ |
| 1136 | { |
| 1137 | return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, write); |
| 1138 | } |
| 1139 | |
| 1140 | static void |
| 1141 | monitor_kill () |
| 1142 | { |
| 1143 | return; /* ignore attempts to kill target system */ |
| 1144 | } |
| 1145 | |
| 1146 | /* All we actually do is set the PC to the start address of exec_bfd, and start |
| 1147 | the program at that point. */ |
| 1148 | |
| 1149 | static void |
| 1150 | monitor_create_inferior (exec_file, args, env) |
| 1151 | char *exec_file; |
| 1152 | char *args; |
| 1153 | char **env; |
| 1154 | { |
| 1155 | if (args && (*args != '\000')) |
| 1156 | error ("Args are not supported by the monitor."); |
| 1157 | |
| 1158 | clear_proceed_status (); |
| 1159 | proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0); |
| 1160 | } |
| 1161 | |
| 1162 | /* Clean up when a program exits. |
| 1163 | The program actually lives on in the remote processor's RAM, and may be |
| 1164 | run again without a download. Don't leave it full of breakpoint |
| 1165 | instructions. */ |
| 1166 | |
| 1167 | static void |
| 1168 | monitor_mourn_inferior () |
| 1169 | { |
| 1170 | unpush_target (targ_ops); |
| 1171 | generic_mourn_inferior (); /* Do all the proper things now */ |
| 1172 | } |
| 1173 | |
| 1174 | #define NUM_MONITOR_BREAKPOINTS 8 |
| 1175 | |
| 1176 | static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0}; |
| 1177 | |
| 1178 | /* Tell the monitor to add a breakpoint. */ |
| 1179 | |
| 1180 | static int |
| 1181 | monitor_insert_breakpoint (addr, shadow) |
| 1182 | CORE_ADDR addr; |
| 1183 | char *shadow; |
| 1184 | { |
| 1185 | int i; |
| 1186 | static unsigned char break_insn[] = BREAKPOINT; |
| 1187 | |
| 1188 | for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++) |
| 1189 | { |
| 1190 | if (breakaddr[i] == 0) |
| 1191 | { |
| 1192 | breakaddr[i] = addr; |
| 1193 | monitor_read_memory (addr, shadow, sizeof (break_insn)); |
| 1194 | monitor_printf (SET_BREAK_CMD, addr); |
| 1195 | monitor_expect_prompt (NULL, 0); |
| 1196 | return 0; |
| 1197 | } |
| 1198 | } |
| 1199 | |
| 1200 | error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS); |
| 1201 | } |
| 1202 | |
| 1203 | /* Tell the monitor to remove a breakpoint. */ |
| 1204 | |
| 1205 | static int |
| 1206 | monitor_remove_breakpoint (addr, shadow) |
| 1207 | CORE_ADDR addr; |
| 1208 | char *shadow; |
| 1209 | { |
| 1210 | int i; |
| 1211 | |
| 1212 | for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++) |
| 1213 | { |
| 1214 | if (breakaddr[i] == addr) |
| 1215 | { |
| 1216 | breakaddr[i] = 0; |
| 1217 | /* some monitors remove breakpoints based on the address */ |
| 1218 | if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR) |
| 1219 | monitor_printf (CLR_BREAK_CMD, addr); |
| 1220 | else |
| 1221 | monitor_printf (CLR_BREAK_CMD, i); |
| 1222 | monitor_expect_prompt (NULL, 0); |
| 1223 | return 0; |
| 1224 | } |
| 1225 | } |
| 1226 | fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr); |
| 1227 | return 1; |
| 1228 | } |
| 1229 | |
| 1230 | /* monitor_load -- download a file. */ |
| 1231 | |
| 1232 | static void |
| 1233 | monitor_load (file, from_tty) |
| 1234 | char *file; |
| 1235 | int from_tty; |
| 1236 | { |
| 1237 | dcache_flush (remote_dcache); |
| 1238 | |
| 1239 | if (current_monitor->load_routine) |
| 1240 | current_monitor->load_routine (monitor_desc, file, hashmark); |
| 1241 | else |
| 1242 | monitor_load_srec (file); |
| 1243 | |
| 1244 | /* Finally, make the PC point at the start address */ |
| 1245 | |
| 1246 | if (exec_bfd) |
| 1247 | write_pc (bfd_get_start_address (exec_bfd)); |
| 1248 | |
| 1249 | inferior_pid = 0; /* No process now */ |
| 1250 | |
| 1251 | /* This is necessary because many things were based on the PC at the time that |
| 1252 | we attached to the monitor, which is no longer valid now that we have loaded |
| 1253 | new code (and just changed the PC). Another way to do this might be to call |
| 1254 | normal_stop, except that the stack may not be valid, and things would get |
| 1255 | horribly confused... */ |
| 1256 | |
| 1257 | clear_symtab_users (); |
| 1258 | } |
| 1259 | |
| 1260 | static void |
| 1261 | monitor_stop () |
| 1262 | { |
| 1263 | if (current_monitor->stop) |
| 1264 | monitor_printf_noecho (current_monitor->stop); |
| 1265 | } |
| 1266 | |
| 1267 | /* Put a command string, in args, out to MONITOR. Output from MONITOR |
| 1268 | is placed on the users terminal until the prompt is seen. FIXME: We |
| 1269 | read the characters ourseleves here cause of a nasty echo. */ |
| 1270 | |
| 1271 | static void |
| 1272 | monitor_command (args, from_tty) |
| 1273 | char *args; |
| 1274 | int from_tty; |
| 1275 | { |
| 1276 | char *p; |
| 1277 | int resp_len; |
| 1278 | char buf[1000]; |
| 1279 | |
| 1280 | if (monitor_desc == NULL) |
| 1281 | error ("monitor target not open."); |
| 1282 | |
| 1283 | p = PROMPT; |
| 1284 | |
| 1285 | /* Send the command. Note that if no args were supplied, then we're |
| 1286 | just sending the monitor a newline, which is sometimes useful. */ |
| 1287 | |
| 1288 | monitor_printf ("%s\r", (args ? args : "")); |
| 1289 | |
| 1290 | resp_len = monitor_expect_prompt (buf, sizeof buf); |
| 1291 | |
| 1292 | fputs_unfiltered (buf, gdb_stdout); /* Output the response */ |
| 1293 | } |
| 1294 | |
| 1295 | /* Download a binary file by converting it to S records. */ |
| 1296 | |
| 1297 | static void |
| 1298 | monitor_load_srec (args) |
| 1299 | char *args; |
| 1300 | { |
| 1301 | bfd *abfd; |
| 1302 | asection *s; |
| 1303 | char *buffer, srec[1024]; |
| 1304 | int i; |
| 1305 | int srec_frame = 32; |
| 1306 | int reclen; |
| 1307 | |
| 1308 | buffer = alloca (srec_frame * 2 + 256); |
| 1309 | |
| 1310 | abfd = bfd_openr (args, 0); |
| 1311 | if (!abfd) |
| 1312 | { |
| 1313 | printf_filtered ("Unable to open file %s\n", args); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
| 1317 | if (bfd_check_format (abfd, bfd_object) == 0) |
| 1318 | { |
| 1319 | printf_filtered ("File is not an object file\n"); |
| 1320 | return; |
| 1321 | } |
| 1322 | |
| 1323 | monitor_printf (LOAD_CMD); /* tell the monitor to load */ |
| 1324 | if (current_monitor->loadresp) |
| 1325 | monitor_expect (current_monitor->loadresp, NULL, 0); |
| 1326 | |
| 1327 | for (s = abfd->sections; s; s = s->next) |
| 1328 | { |
| 1329 | if (s->flags & SEC_LOAD) |
| 1330 | { |
| 1331 | int numbytes; |
| 1332 | |
| 1333 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, |
| 1334 | s->vma + s->_raw_size); |
| 1335 | gdb_flush (gdb_stdout); |
| 1336 | |
| 1337 | for (i = 0; i < s->_raw_size; i += numbytes) |
| 1338 | { |
| 1339 | numbytes = min (srec_frame, s->_raw_size - i); |
| 1340 | |
| 1341 | bfd_get_section_contents (abfd, s, buffer, i, numbytes); |
| 1342 | |
| 1343 | reclen = monitor_make_srec (srec, 'd', s->vma + i, buffer, numbytes); |
| 1344 | |
| 1345 | monitor_printf_noecho ("%.*s\r", reclen, srec); |
| 1346 | |
| 1347 | if (hashmark) |
| 1348 | { |
| 1349 | putchar_unfiltered ('#'); |
| 1350 | gdb_flush (gdb_stdout); |
| 1351 | } |
| 1352 | } /* Per-packet (or S-record) loop */ |
| 1353 | |
| 1354 | putchar_unfiltered ('\n'); |
| 1355 | } /* Loadable sections */ |
| 1356 | } |
| 1357 | if (hashmark) |
| 1358 | putchar_unfiltered ('\n'); |
| 1359 | |
| 1360 | /* Write a type 7 terminator record. no data for a type 7, and there |
| 1361 | is no data, so len is 0. */ |
| 1362 | |
| 1363 | reclen = monitor_make_srec (srec, 't', abfd->start_address, NULL, 0); |
| 1364 | |
| 1365 | monitor_printf_noecho ("%.*s\r", reclen, srec); |
| 1366 | |
| 1367 | monitor_printf_noecho ("\r\r"); /* Some monitors need these to wake up */ |
| 1368 | |
| 1369 | monitor_expect_prompt (NULL, 0); |
| 1370 | |
| 1371 | SERIAL_FLUSH_INPUT (monitor_desc); |
| 1372 | } |
| 1373 | |
| 1374 | /* |
| 1375 | * monitor_make_srec -- make an srecord. This writes each line, one at a |
| 1376 | * time, each with it's own header and trailer line. |
| 1377 | * An srecord looks like this: |
| 1378 | * |
| 1379 | * byte count-+ address |
| 1380 | * start ---+ | | data +- checksum |
| 1381 | * | | | | |
| 1382 | * S01000006F6B692D746573742E73726563E4 |
| 1383 | * S315000448600000000000000000FC00005900000000E9 |
| 1384 | * S31A0004000023C1400037DE00F023604000377B009020825000348D |
| 1385 | * S30B0004485A0000000000004E |
| 1386 | * S70500040000F6 |
| 1387 | * |
| 1388 | * S<type><length><address><data><checksum> |
| 1389 | * |
| 1390 | * Where |
| 1391 | * - length |
| 1392 | * is the number of bytes following upto the checksum. Note that |
| 1393 | * this is not the number of chars following, since it takes two |
| 1394 | * chars to represent a byte. |
| 1395 | * - type |
| 1396 | * is one of: |
| 1397 | * 0) header record |
| 1398 | * 1) two byte address data record |
| 1399 | * 2) three byte address data record |
| 1400 | * 3) four byte address data record |
| 1401 | * 7) four byte address termination record |
| 1402 | * 8) three byte address termination record |
| 1403 | * 9) two byte address termination record |
| 1404 | * |
| 1405 | * - address |
| 1406 | * is the start address of the data following, or in the case of |
| 1407 | * a termination record, the start address of the image |
| 1408 | * - data |
| 1409 | * is the data. |
| 1410 | * - checksum |
| 1411 | * is the sum of all the raw byte data in the record, from the length |
| 1412 | * upwards, modulo 256 and subtracted from 255. |
| 1413 | * |
| 1414 | * This routine returns the length of the S-record. |
| 1415 | * |
| 1416 | */ |
| 1417 | |
| 1418 | static int |
| 1419 | monitor_make_srec (buffer, type, memaddr, myaddr, len) |
| 1420 | char *buffer; |
| 1421 | int type; |
| 1422 | CORE_ADDR memaddr; |
| 1423 | unsigned char *myaddr; |
| 1424 | int len; |
| 1425 | { |
| 1426 | unsigned char checksum; |
| 1427 | int i; |
| 1428 | char *buf; |
| 1429 | static char hextab[16] = "0123456789ABCDEF"; |
| 1430 | static char data_code_table[] = { 0,0,1,2,3}; |
| 1431 | static char term_code_table[] = { 0,0,9,8,7}; |
| 1432 | int addr_size; /* Number of bytes in the record */ |
| 1433 | int type_code; |
| 1434 | buf = buffer; |
| 1435 | |
| 1436 | checksum = 0; |
| 1437 | |
| 1438 | addr_size = 2; |
| 1439 | if (memaddr >= 0xffffff) |
| 1440 | addr_size = 4; |
| 1441 | else if (memaddr >= 0xffffff) |
| 1442 | addr_size = 3; |
| 1443 | else |
| 1444 | addr_size = 2; |
| 1445 | |
| 1446 | switch (type) |
| 1447 | { |
| 1448 | case 't': |
| 1449 | type_code = term_code_table[addr_size]; |
| 1450 | break; |
| 1451 | case 'd': |
| 1452 | type_code = data_code_table[addr_size]; |
| 1453 | break; |
| 1454 | default: |
| 1455 | abort(); |
| 1456 | } |
| 1457 | /* Create the header for the srec. addr_size is the number of bytes in the address, |
| 1458 | and 1 is the number of bytes in the count. */ |
| 1459 | |
| 1460 | switch (addr_size) |
| 1461 | { |
| 1462 | case 4: |
| 1463 | sprintf (buf, "S%d%02X%08X", type_code, len + addr_size + 1, memaddr); |
| 1464 | buf += 12; |
| 1465 | break; |
| 1466 | case 3: |
| 1467 | sprintf (buf, "S%d%02X%06X", type_code, len + addr_size + 1, memaddr); |
| 1468 | buf += 10; |
| 1469 | break; |
| 1470 | case 2: |
| 1471 | sprintf (buf, "S%d%02X%04X", type_code, len + addr_size + 1, memaddr); |
| 1472 | buf += 8; |
| 1473 | break; |
| 1474 | } |
| 1475 | |
| 1476 | /* Note that the checksum is calculated on the raw data, not the hexified |
| 1477 | data. It includes the length, address and the data portions of the |
| 1478 | packet. */ |
| 1479 | |
| 1480 | checksum += (len + addr_size + 1 /* Packet length */ |
| 1481 | + (memaddr & 0xff) /* Address... */ |
| 1482 | + ((memaddr >> 8) & 0xff) |
| 1483 | + ((memaddr >> 16) & 0xff) |
| 1484 | + ((memaddr >> 24) & 0xff)); |
| 1485 | |
| 1486 | /* build the srecord */ |
| 1487 | for (i = 0; i < len; i++) |
| 1488 | { |
| 1489 | *buf++ = hextab [myaddr[i] >> 4]; |
| 1490 | *buf++ = hextab [myaddr[i] & 0xf]; |
| 1491 | checksum += myaddr[i]; |
| 1492 | } |
| 1493 | |
| 1494 | checksum = ~checksum; |
| 1495 | |
| 1496 | *buf++ = hextab[checksum >> 4]; |
| 1497 | *buf++ = hextab[checksum & 0xf]; |
| 1498 | |
| 1499 | return buf - buffer; |
| 1500 | } |
| 1501 | |
| 1502 | /* Convert hex digit A to a number. */ |
| 1503 | |
| 1504 | static int |
| 1505 | from_hex (a) |
| 1506 | int a; |
| 1507 | { |
| 1508 | if (a >= '0' && a <= '9') |
| 1509 | return a - '0'; |
| 1510 | if (a >= 'a' && a <= 'f') |
| 1511 | return a - 'a' + 10; |
| 1512 | if (a >= 'A' && a <= 'F') |
| 1513 | return a - 'A' + 10; |
| 1514 | |
| 1515 | error ("Reply contains invalid hex digit 0x%x", a); |
| 1516 | } |
| 1517 | |
| 1518 | static struct target_ops monitor_ops = |
| 1519 | { |
| 1520 | NULL, /* to_shortname */ |
| 1521 | NULL, /* to_longname */ |
| 1522 | NULL, /* to_doc */ |
| 1523 | NULL, /* to_open */ |
| 1524 | monitor_close, /* to_close */ |
| 1525 | NULL, /* to_attach */ |
| 1526 | monitor_detach, /* to_detach */ |
| 1527 | monitor_resume, /* to_resume */ |
| 1528 | monitor_wait, /* to_wait */ |
| 1529 | monitor_fetch_registers, /* to_fetch_registers */ |
| 1530 | monitor_store_registers, /* to_store_registers */ |
| 1531 | monitor_prepare_to_store, /* to_prepare_to_store */ |
| 1532 | monitor_xfer_memory, /* to_xfer_memory */ |
| 1533 | monitor_files_info, /* to_files_info */ |
| 1534 | monitor_insert_breakpoint, /* to_insert_breakpoint */ |
| 1535 | monitor_remove_breakpoint, /* to_remove_breakpoint */ |
| 1536 | 0, /* to_terminal_init */ |
| 1537 | 0, /* to_terminal_inferior */ |
| 1538 | 0, /* to_terminal_ours_for_output */ |
| 1539 | 0, /* to_terminal_ours */ |
| 1540 | 0, /* to_terminal_info */ |
| 1541 | monitor_kill, /* to_kill */ |
| 1542 | monitor_load, /* to_load */ |
| 1543 | 0, /* to_lookup_symbol */ |
| 1544 | monitor_create_inferior, /* to_create_inferior */ |
| 1545 | monitor_mourn_inferior, /* to_mourn_inferior */ |
| 1546 | 0, /* to_can_run */ |
| 1547 | 0, /* to_notice_signals */ |
| 1548 | monitor_stop, /* to_stop */ |
| 1549 | process_stratum, /* to_stratum */ |
| 1550 | 0, /* to_next */ |
| 1551 | 1, /* to_has_all_memory */ |
| 1552 | 1, /* to_has_memory */ |
| 1553 | 1, /* to_has_stack */ |
| 1554 | 1, /* to_has_registers */ |
| 1555 | 1, /* to_has_execution */ |
| 1556 | 0, /* sections */ |
| 1557 | 0, /* sections_end */ |
| 1558 | OPS_MAGIC /* to_magic */ |
| 1559 | }; |
| 1560 | |
| 1561 | /* Init the target_ops structure pointed at by OPS */ |
| 1562 | |
| 1563 | void |
| 1564 | init_monitor_ops (ops) |
| 1565 | struct target_ops *ops; |
| 1566 | { |
| 1567 | memcpy (ops, &monitor_ops, sizeof monitor_ops); |
| 1568 | } |
| 1569 | |
| 1570 | /* Define additional commands that are usually only used by monitors. */ |
| 1571 | |
| 1572 | void |
| 1573 | _initialize_remote_monitors () |
| 1574 | { |
| 1575 | add_show_from_set (add_set_cmd ("hash", no_class, var_boolean, |
| 1576 | (char *)&hashmark, |
| 1577 | "Set display of activity while downloading a file.\n\ |
| 1578 | When enabled, a hashmark \'#\' is displayed.", |
| 1579 | &setlist), |
| 1580 | &showlist); |
| 1581 | |
| 1582 | add_com ("monitor", class_obscure, monitor_command, |
| 1583 | "Send a command to the debug monitor."); |
| 1584 | } |