| 1 | /* Remote debugging interface for Array Tech RAID controller.. |
| 2 | Copyright 90, 91, 92, 93, 94, 1995, 1998 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Support. Written by Rob Savoye for Cygnus. |
| 4 | |
| 5 | This module talks to a debug monitor called 'MONITOR', which |
| 6 | We communicate with MONITOR via either a direct serial line, or a TCP |
| 7 | (or possibly TELNET) stream to a terminal multiplexor, |
| 8 | which in turn talks to the target board. |
| 9 | |
| 10 | This file is part of GDB. |
| 11 | |
| 12 | This program is free software; you can redistribute it and/or modify |
| 13 | it under the terms of the GNU General Public License as published by |
| 14 | the Free Software Foundation; either version 2 of the License, or |
| 15 | (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License |
| 23 | along with this program; if not, write to the Free Software |
| 24 | Foundation, Inc., 59 Temple Place - Suite 330, |
| 25 | Boston, MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #include "defs.h" |
| 29 | #include "gdbcore.h" |
| 30 | #include "target.h" |
| 31 | #include "gdb_wait.h" |
| 32 | #include <ctype.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/types.h> |
| 35 | #include "gdb_string.h" |
| 36 | #include "command.h" |
| 37 | #include "serial.h" |
| 38 | #include "monitor.h" |
| 39 | #include "remote-utils.h" |
| 40 | #include "inferior.h" |
| 41 | #include "version.h" |
| 42 | |
| 43 | extern int baud_rate; |
| 44 | |
| 45 | #define ARRAY_PROMPT ">> " |
| 46 | |
| 47 | #define SWAP_TARGET_AND_HOST(buffer,len) \ |
| 48 | do \ |
| 49 | { \ |
| 50 | if (TARGET_BYTE_ORDER != HOST_BYTE_ORDER) \ |
| 51 | { \ |
| 52 | char tmp; \ |
| 53 | char *p = (char *)(buffer); \ |
| 54 | char *q = ((char *)(buffer)) + len - 1; \ |
| 55 | for (; p < q; p++, q--) \ |
| 56 | { \ |
| 57 | tmp = *q; \ |
| 58 | *q = *p; \ |
| 59 | *p = tmp; \ |
| 60 | } \ |
| 61 | } \ |
| 62 | } \ |
| 63 | while (0) |
| 64 | |
| 65 | static void debuglogs (int, char *, ...); |
| 66 | static void array_open (); |
| 67 | static void array_close (); |
| 68 | static void array_detach (); |
| 69 | static void array_attach (); |
| 70 | static void array_resume (); |
| 71 | static void array_fetch_register (); |
| 72 | static void array_store_register (); |
| 73 | static void array_fetch_registers (); |
| 74 | static void array_store_registers (); |
| 75 | static void array_prepare_to_store (); |
| 76 | static void array_files_info (); |
| 77 | static void array_kill (); |
| 78 | static void array_create_inferior (); |
| 79 | static void array_mourn_inferior (); |
| 80 | static void make_gdb_packet (); |
| 81 | static int array_xfer_memory (); |
| 82 | static int array_wait (); |
| 83 | static int array_insert_breakpoint (); |
| 84 | static int array_remove_breakpoint (); |
| 85 | static int tohex (); |
| 86 | static int to_hex (); |
| 87 | static int from_hex (); |
| 88 | static int array_send_packet (); |
| 89 | static int array_get_packet (); |
| 90 | static unsigned long ascii2hexword (); |
| 91 | static void hexword2ascii (); |
| 92 | |
| 93 | #define LOG_FILE "monitor.log" |
| 94 | #if defined (LOG_FILE) |
| 95 | FILE *log_file; |
| 96 | #endif |
| 97 | |
| 98 | static int timeout = 30; |
| 99 | /* Having this larger than 400 causes us to be incompatible with m68k-stub.c |
| 100 | and i386-stub.c. Normally, no one would notice because it only matters |
| 101 | for writing large chunks of memory (e.g. in downloads). Also, this needs |
| 102 | to be more than 400 if required to hold the registers (see below, where |
| 103 | we round it up based on REGISTER_BYTES). */ |
| 104 | #define PBUFSIZ 400 |
| 105 | |
| 106 | /* |
| 107 | * Descriptor for I/O to remote machine. Initialize it to NULL so that |
| 108 | * array_open knows that we don't have a file open when the program starts. |
| 109 | */ |
| 110 | serial_t array_desc = NULL; |
| 111 | |
| 112 | /* |
| 113 | * this array of registers need to match the indexes used by GDB. The |
| 114 | * whole reason this exists is cause the various ROM monitors use |
| 115 | * different strings than GDB does, and doesn't support all the |
| 116 | * registers either. So, typing "info reg sp" becomes a "r30". |
| 117 | */ |
| 118 | extern char *tmp_mips_processor_type; |
| 119 | extern int mips_set_processor_type (); |
| 120 | |
| 121 | static struct target_ops array_ops; |
| 122 | |
| 123 | static void |
| 124 | init_array_ops (void) |
| 125 | { |
| 126 | array_ops.to_shortname = "array"; |
| 127 | array_ops.to_longname = |
| 128 | "Debug using the standard GDB remote protocol for the Array Tech target.", |
| 129 | array_ops.to_doc = |
| 130 | "Debug using the standard GDB remote protocol for the Array Tech target.\n\ |
| 131 | Specify the serial device it is connected to (e.g. /dev/ttya)."; |
| 132 | array_ops.to_open = array_open; |
| 133 | array_ops.to_close = array_close; |
| 134 | array_ops.to_attach = NULL; |
| 135 | array_ops.to_post_attach = NULL; |
| 136 | array_ops.to_require_attach = NULL; |
| 137 | array_ops.to_detach = array_detach; |
| 138 | array_ops.to_require_detach = NULL; |
| 139 | array_ops.to_resume = array_resume; |
| 140 | array_ops.to_wait = array_wait; |
| 141 | array_ops.to_post_wait = NULL; |
| 142 | array_ops.to_fetch_registers = array_fetch_registers; |
| 143 | array_ops.to_store_registers = array_store_registers; |
| 144 | array_ops.to_prepare_to_store = array_prepare_to_store; |
| 145 | array_ops.to_xfer_memory = array_xfer_memory; |
| 146 | array_ops.to_files_info = array_files_info; |
| 147 | array_ops.to_insert_breakpoint = array_insert_breakpoint; |
| 148 | array_ops.to_remove_breakpoint = array_remove_breakpoint; |
| 149 | array_ops.to_terminal_init = 0; |
| 150 | array_ops.to_terminal_inferior = 0; |
| 151 | array_ops.to_terminal_ours_for_output = 0; |
| 152 | array_ops.to_terminal_ours = 0; |
| 153 | array_ops.to_terminal_info = 0; |
| 154 | array_ops.to_kill = array_kill; |
| 155 | array_ops.to_load = 0; |
| 156 | array_ops.to_lookup_symbol = 0; |
| 157 | array_ops.to_create_inferior = array_create_inferior; |
| 158 | array_ops.to_post_startup_inferior = NULL; |
| 159 | array_ops.to_acknowledge_created_inferior = NULL; |
| 160 | array_ops.to_clone_and_follow_inferior = NULL; |
| 161 | array_ops.to_post_follow_inferior_by_clone = NULL; |
| 162 | array_ops.to_insert_fork_catchpoint = NULL; |
| 163 | array_ops.to_remove_fork_catchpoint = NULL; |
| 164 | array_ops.to_insert_vfork_catchpoint = NULL; |
| 165 | array_ops.to_remove_vfork_catchpoint = NULL; |
| 166 | array_ops.to_has_forked = NULL; |
| 167 | array_ops.to_has_vforked = NULL; |
| 168 | array_ops.to_can_follow_vfork_prior_to_exec = NULL; |
| 169 | array_ops.to_post_follow_vfork = NULL; |
| 170 | array_ops.to_insert_exec_catchpoint = NULL; |
| 171 | array_ops.to_remove_exec_catchpoint = NULL; |
| 172 | array_ops.to_has_execd = NULL; |
| 173 | array_ops.to_reported_exec_events_per_exec_call = NULL; |
| 174 | array_ops.to_has_exited = NULL; |
| 175 | array_ops.to_mourn_inferior = array_mourn_inferior; |
| 176 | array_ops.to_can_run = 0; |
| 177 | array_ops.to_notice_signals = 0; |
| 178 | array_ops.to_thread_alive = 0; |
| 179 | array_ops.to_stop = 0; |
| 180 | array_ops.to_pid_to_exec_file = NULL; |
| 181 | array_ops.to_core_file_to_sym_file = NULL; |
| 182 | array_ops.to_stratum = process_stratum; |
| 183 | array_ops.DONT_USE = 0; |
| 184 | array_ops.to_has_all_memory = 1; |
| 185 | array_ops.to_has_memory = 1; |
| 186 | array_ops.to_has_stack = 1; |
| 187 | array_ops.to_has_registers = 1; |
| 188 | array_ops.to_has_execution = 1; |
| 189 | array_ops.to_sections = 0; |
| 190 | array_ops.to_sections_end = 0; |
| 191 | array_ops.to_magic = OPS_MAGIC; |
| 192 | }; |
| 193 | |
| 194 | /* |
| 195 | * printf_monitor -- send data to monitor. Works just like printf. |
| 196 | */ |
| 197 | static void |
| 198 | printf_monitor (char *pattern,...) |
| 199 | { |
| 200 | va_list args; |
| 201 | char buf[PBUFSIZ]; |
| 202 | int i; |
| 203 | |
| 204 | va_start (args, pattern); |
| 205 | |
| 206 | vsprintf (buf, pattern, args); |
| 207 | |
| 208 | debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf); |
| 209 | |
| 210 | if (strlen (buf) > PBUFSIZ) |
| 211 | error ("printf_monitor(): string too long"); |
| 212 | if (SERIAL_WRITE (array_desc, buf, strlen (buf))) |
| 213 | fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno)); |
| 214 | } |
| 215 | /* |
| 216 | * write_monitor -- send raw data to monitor. |
| 217 | */ |
| 218 | static void |
| 219 | write_monitor (char data[], int len) |
| 220 | { |
| 221 | if (SERIAL_WRITE (array_desc, data, len)) |
| 222 | fprintf (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno)); |
| 223 | |
| 224 | *(data + len + 1) = '\0'; |
| 225 | debuglogs (1, "write_monitor(), Sending: \"%s\".", data); |
| 226 | |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * debuglogs -- deal with debugging info to multiple sources. This takes |
| 231 | * two real args, the first one is the level to be compared against |
| 232 | * the sr_get_debug() value, the second arg is a printf buffer and args |
| 233 | * to be formatted and printed. A CR is added after each string is printed. |
| 234 | */ |
| 235 | static void |
| 236 | debuglogs (int level, char *pattern,...) |
| 237 | { |
| 238 | va_list args; |
| 239 | char *p; |
| 240 | unsigned char buf[PBUFSIZ]; |
| 241 | char newbuf[PBUFSIZ]; |
| 242 | int i; |
| 243 | |
| 244 | va_start (args, pattern); |
| 245 | |
| 246 | if ((level < 0) || (level > 100)) |
| 247 | { |
| 248 | error ("Bad argument passed to debuglogs(), needs debug level"); |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | vsprintf (buf, pattern, args); /* format the string */ |
| 253 | |
| 254 | /* convert some characters so it'll look right in the log */ |
| 255 | p = newbuf; |
| 256 | for (i = 0; buf[i] != '\0'; i++) |
| 257 | { |
| 258 | if (i > PBUFSIZ) |
| 259 | error ("Debug message too long"); |
| 260 | switch (buf[i]) |
| 261 | { |
| 262 | case '\n': /* newlines */ |
| 263 | *p++ = '\\'; |
| 264 | *p++ = 'n'; |
| 265 | continue; |
| 266 | case '\r': /* carriage returns */ |
| 267 | *p++ = '\\'; |
| 268 | *p++ = 'r'; |
| 269 | continue; |
| 270 | case '\033': /* escape */ |
| 271 | *p++ = '\\'; |
| 272 | *p++ = 'e'; |
| 273 | continue; |
| 274 | case '\t': /* tab */ |
| 275 | *p++ = '\\'; |
| 276 | *p++ = 't'; |
| 277 | continue; |
| 278 | case '\b': /* backspace */ |
| 279 | *p++ = '\\'; |
| 280 | *p++ = 'b'; |
| 281 | continue; |
| 282 | default: /* no change */ |
| 283 | *p++ = buf[i]; |
| 284 | } |
| 285 | |
| 286 | if (buf[i] < 26) |
| 287 | { /* modify control characters */ |
| 288 | *p++ = '^'; |
| 289 | *p++ = buf[i] + 'A'; |
| 290 | continue; |
| 291 | } |
| 292 | if (buf[i] >= 128) |
| 293 | { /* modify control characters */ |
| 294 | *p++ = '!'; |
| 295 | *p++ = buf[i] + 'A'; |
| 296 | continue; |
| 297 | } |
| 298 | } |
| 299 | *p = '\0'; /* terminate the string */ |
| 300 | |
| 301 | if (sr_get_debug () > level) |
| 302 | printf_unfiltered ("%s\n", newbuf); |
| 303 | |
| 304 | #ifdef LOG_FILE /* write to the monitor log */ |
| 305 | if (log_file != 0x0) |
| 306 | { |
| 307 | fputs (newbuf, log_file); |
| 308 | fputc ('\n', log_file); |
| 309 | fflush (log_file); |
| 310 | } |
| 311 | #endif |
| 312 | } |
| 313 | |
| 314 | /* readchar -- read a character from the remote system, doing all the fancy |
| 315 | * timeout stuff. |
| 316 | */ |
| 317 | static int |
| 318 | readchar (int timeout) |
| 319 | { |
| 320 | int c; |
| 321 | |
| 322 | c = SERIAL_READCHAR (array_desc, abs (timeout)); |
| 323 | |
| 324 | if (sr_get_debug () > 5) |
| 325 | { |
| 326 | putchar (c & 0x7f); |
| 327 | debuglogs (5, "readchar: timeout = %d\n", timeout); |
| 328 | } |
| 329 | |
| 330 | #ifdef LOG_FILE |
| 331 | if (isascii (c)) |
| 332 | putc (c & 0x7f, log_file); |
| 333 | #endif |
| 334 | |
| 335 | if (c >= 0) |
| 336 | return c & 0x7f; |
| 337 | |
| 338 | if (c == SERIAL_TIMEOUT) |
| 339 | { |
| 340 | if (timeout <= 0) |
| 341 | return c; /* Polls shouldn't generate timeout errors */ |
| 342 | error ("Timeout reading from remote system."); |
| 343 | #ifdef LOG_FILE |
| 344 | fputs ("ERROR: Timeout reading from remote system", log_file); |
| 345 | #endif |
| 346 | } |
| 347 | perror_with_name ("readchar"); |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * expect -- scan input from the remote system, until STRING is found. |
| 352 | * If DISCARD is non-zero, then discard non-matching input, else print |
| 353 | * it out. Let the user break out immediately. |
| 354 | */ |
| 355 | static void |
| 356 | expect (char *string, int discard) |
| 357 | { |
| 358 | char *p = string; |
| 359 | int c; |
| 360 | |
| 361 | |
| 362 | debuglogs (1, "Expecting \"%s\".", string); |
| 363 | |
| 364 | immediate_quit++; |
| 365 | while (1) |
| 366 | { |
| 367 | c = readchar (timeout); |
| 368 | if (!isascii (c)) |
| 369 | continue; |
| 370 | if (c == *p++) |
| 371 | { |
| 372 | if (*p == '\0') |
| 373 | { |
| 374 | immediate_quit--; |
| 375 | debuglogs (4, "Matched"); |
| 376 | return; |
| 377 | } |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | if (!discard) |
| 382 | { |
| 383 | fputc_unfiltered (c, gdb_stdout); |
| 384 | } |
| 385 | p = string; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* Keep discarding input until we see the MONITOR array_cmds->prompt. |
| 391 | |
| 392 | The convention for dealing with the expect_prompt is that you |
| 393 | o give your command |
| 394 | o *then* wait for the expect_prompt. |
| 395 | |
| 396 | Thus the last thing that a procedure does with the serial line |
| 397 | will be an expect_prompt(). Exception: array_resume does not |
| 398 | wait for the expect_prompt, because the terminal is being handed over |
| 399 | to the inferior. However, the next thing which happens after that |
| 400 | is a array_wait which does wait for the expect_prompt. |
| 401 | Note that this includes abnormal exit, e.g. error(). This is |
| 402 | necessary to prevent getting into states from which we can't |
| 403 | recover. */ |
| 404 | static void |
| 405 | expect_prompt (int discard) |
| 406 | { |
| 407 | expect (ARRAY_PROMPT, discard); |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise |
| 412 | */ |
| 413 | static int |
| 414 | junk (char ch) |
| 415 | { |
| 416 | switch (ch) |
| 417 | { |
| 418 | case '\0': |
| 419 | case ' ': |
| 420 | case '-': |
| 421 | case '\t': |
| 422 | case '\r': |
| 423 | case '\n': |
| 424 | if (sr_get_debug () > 5) |
| 425 | debuglogs (5, "Ignoring \'%c\'.", ch); |
| 426 | return 1; |
| 427 | default: |
| 428 | if (sr_get_debug () > 5) |
| 429 | debuglogs (5, "Accepting \'%c\'.", ch); |
| 430 | return 0; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * get_hex_digit -- Get a hex digit from the remote system & return its value. |
| 436 | * If ignore is nonzero, ignore spaces, newline & tabs. |
| 437 | */ |
| 438 | static int |
| 439 | get_hex_digit (int ignore) |
| 440 | { |
| 441 | static int ch; |
| 442 | while (1) |
| 443 | { |
| 444 | ch = readchar (timeout); |
| 445 | if (junk (ch)) |
| 446 | continue; |
| 447 | if (sr_get_debug () > 4) |
| 448 | { |
| 449 | debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | #ifdef LOG_FILE /* write to the monitor log */ |
| 454 | if (log_file != 0x0) |
| 455 | { |
| 456 | fputs ("get_hex_digit() got a 0x", log_file); |
| 457 | fputc (ch, log_file); |
| 458 | fputc ('\n', log_file); |
| 459 | fflush (log_file); |
| 460 | } |
| 461 | #endif |
| 462 | } |
| 463 | |
| 464 | if (ch >= '0' && ch <= '9') |
| 465 | return ch - '0'; |
| 466 | else if (ch >= 'A' && ch <= 'F') |
| 467 | return ch - 'A' + 10; |
| 468 | else if (ch >= 'a' && ch <= 'f') |
| 469 | return ch - 'a' + 10; |
| 470 | else if (ch == ' ' && ignore) |
| 471 | ; |
| 472 | else |
| 473 | { |
| 474 | expect_prompt (1); |
| 475 | debuglogs (4, "Invalid hex digit from remote system. (0x%x)", ch); |
| 476 | error ("Invalid hex digit from remote system. (0x%x)", ch); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /* get_hex_byte -- Get a byte from monitor and put it in *BYT. |
| 482 | * Accept any number leading spaces. |
| 483 | */ |
| 484 | static void |
| 485 | get_hex_byte (char *byt) |
| 486 | { |
| 487 | int val; |
| 488 | |
| 489 | val = get_hex_digit (1) << 4; |
| 490 | debuglogs (4, "get_hex_byte() -- Read first nibble 0x%x", val); |
| 491 | |
| 492 | val |= get_hex_digit (0); |
| 493 | debuglogs (4, "get_hex_byte() -- Read second nibble 0x%x", val); |
| 494 | *byt = val; |
| 495 | |
| 496 | debuglogs (4, "get_hex_byte() -- Read a 0x%x", val); |
| 497 | } |
| 498 | |
| 499 | /* |
| 500 | * get_hex_word -- Get N 32-bit words from remote, each preceded by a space, |
| 501 | * and put them in registers starting at REGNO. |
| 502 | */ |
| 503 | static int |
| 504 | get_hex_word (void) |
| 505 | { |
| 506 | long val, newval; |
| 507 | int i; |
| 508 | |
| 509 | val = 0; |
| 510 | |
| 511 | #if 0 |
| 512 | if (HOST_BYTE_ORDER == BIG_ENDIAN) |
| 513 | { |
| 514 | #endif |
| 515 | for (i = 0; i < 8; i++) |
| 516 | val = (val << 4) + get_hex_digit (i == 0); |
| 517 | #if 0 |
| 518 | } |
| 519 | else |
| 520 | { |
| 521 | for (i = 7; i >= 0; i--) |
| 522 | val = (val << 4) + get_hex_digit (i == 0); |
| 523 | } |
| 524 | #endif |
| 525 | |
| 526 | debuglogs (4, "get_hex_word() got a 0x%x for a %s host.", val, (HOST_BYTE_ORDER == BIG_ENDIAN) ? "big endian" : "little endian"); |
| 527 | |
| 528 | return val; |
| 529 | } |
| 530 | |
| 531 | /* This is called not only when we first attach, but also when the |
| 532 | user types "run" after having attached. */ |
| 533 | static void |
| 534 | array_create_inferior (char *execfile, char *args, char **env) |
| 535 | { |
| 536 | int entry_pt; |
| 537 | |
| 538 | if (args && *args) |
| 539 | error ("Can't pass arguments to remote MONITOR process"); |
| 540 | |
| 541 | if (execfile == 0 || exec_bfd == 0) |
| 542 | error ("No executable file specified"); |
| 543 | |
| 544 | entry_pt = (int) bfd_get_start_address (exec_bfd); |
| 545 | |
| 546 | /* The "process" (board) is already stopped awaiting our commands, and |
| 547 | the program is already downloaded. We just set its PC and go. */ |
| 548 | |
| 549 | clear_proceed_status (); |
| 550 | |
| 551 | /* Tell wait_for_inferior that we've started a new process. */ |
| 552 | init_wait_for_inferior (); |
| 553 | |
| 554 | /* Set up the "saved terminal modes" of the inferior |
| 555 | based on what modes we are starting it with. */ |
| 556 | target_terminal_init (); |
| 557 | |
| 558 | /* Install inferior's terminal modes. */ |
| 559 | target_terminal_inferior (); |
| 560 | |
| 561 | /* insert_step_breakpoint (); FIXME, do we need this? */ |
| 562 | |
| 563 | /* Let 'er rip... */ |
| 564 | proceed ((CORE_ADDR) entry_pt, TARGET_SIGNAL_DEFAULT, 0); |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * array_open -- open a connection to a remote debugger. |
| 569 | * NAME is the filename used for communication. |
| 570 | */ |
| 571 | static int baudrate = 9600; |
| 572 | static char dev_name[100]; |
| 573 | |
| 574 | static void |
| 575 | array_open (char *args, char *name, int from_tty) |
| 576 | { |
| 577 | char packet[PBUFSIZ]; |
| 578 | |
| 579 | if (args == NULL) |
| 580 | error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\ |
| 581 | `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name); |
| 582 | |
| 583 | /* if (is_open) */ |
| 584 | array_close (0); |
| 585 | |
| 586 | target_preopen (from_tty); |
| 587 | unpush_target (&array_ops); |
| 588 | |
| 589 | tmp_mips_processor_type = "lsi33k"; /* change the default from r3051 */ |
| 590 | mips_set_processor_type_command ("lsi33k", 0); |
| 591 | |
| 592 | strcpy (dev_name, args); |
| 593 | array_desc = SERIAL_OPEN (dev_name); |
| 594 | |
| 595 | if (array_desc == NULL) |
| 596 | perror_with_name (dev_name); |
| 597 | |
| 598 | if (baud_rate != -1) |
| 599 | { |
| 600 | if (SERIAL_SETBAUDRATE (array_desc, baud_rate)) |
| 601 | { |
| 602 | SERIAL_CLOSE (array_desc); |
| 603 | perror_with_name (name); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | SERIAL_RAW (array_desc); |
| 608 | |
| 609 | #if defined (LOG_FILE) |
| 610 | log_file = fopen (LOG_FILE, "w"); |
| 611 | if (log_file == NULL) |
| 612 | perror_with_name (LOG_FILE); |
| 613 | fprintf (log_file, "GDB %s (%s", version, host_name); |
| 614 | fprintf (log_file, " --target %s)\n", array_ops.to_shortname); |
| 615 | fprintf (log_file, "Remote target %s connected to %s\n\n", array_ops.to_shortname, dev_name); |
| 616 | #endif |
| 617 | |
| 618 | /* see if the target is alive. For a ROM monitor, we can just try to force the |
| 619 | expect_prompt to print a few times. For the GDB remote protocol, the application |
| 620 | being debugged is sitting at a breakpoint and waiting for GDB to initialize |
| 621 | the connection. We force it to give us an empty packet to see if it's alive. |
| 622 | */ |
| 623 | debuglogs (3, "Trying to ACK the target's debug stub"); |
| 624 | /* unless your are on the new hardware, the old board won't initialize |
| 625 | because the '@' doesn't flush output like it does on the new ROMS. |
| 626 | */ |
| 627 | printf_monitor ("@"); /* ask for the last signal */ |
| 628 | expect_prompt (1); /* See if we get a expect_prompt */ |
| 629 | #ifdef TEST_ARRAY /* skip packet for testing */ |
| 630 | make_gdb_packet (packet, "?"); /* ask for a bogus packet */ |
| 631 | if (array_send_packet (packet) == 0) |
| 632 | error ("Couldn't transmit packet\n"); |
| 633 | printf_monitor ("@\n"); /* force it to flush stdout */ |
| 634 | expect_prompt (1); /* See if we get a expect_prompt */ |
| 635 | #endif |
| 636 | push_target (&array_ops); |
| 637 | if (from_tty) |
| 638 | printf ("Remote target %s connected to %s\n", array_ops.to_shortname, dev_name); |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * array_close -- Close out all files and local state before this |
| 643 | * target loses control. |
| 644 | */ |
| 645 | |
| 646 | static void |
| 647 | array_close (int quitting) |
| 648 | { |
| 649 | SERIAL_CLOSE (array_desc); |
| 650 | array_desc = NULL; |
| 651 | |
| 652 | debuglogs (1, "array_close (quitting=%d)", quitting); |
| 653 | |
| 654 | #if defined (LOG_FILE) |
| 655 | if (log_file) |
| 656 | { |
| 657 | if (ferror (log_file)) |
| 658 | printf_filtered ("Error writing log file.\n"); |
| 659 | if (fclose (log_file) != 0) |
| 660 | printf_filtered ("Error closing log file.\n"); |
| 661 | } |
| 662 | #endif |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * array_detach -- terminate the open connection to the remote |
| 667 | * debugger. Use this when you want to detach and do something |
| 668 | * else with your gdb. |
| 669 | */ |
| 670 | static void |
| 671 | array_detach (int from_tty) |
| 672 | { |
| 673 | |
| 674 | debuglogs (1, "array_detach ()"); |
| 675 | |
| 676 | pop_target (); /* calls array_close to do the real work */ |
| 677 | if (from_tty) |
| 678 | printf ("Ending remote %s debugging\n", target_shortname); |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * array_attach -- attach GDB to the target. |
| 683 | */ |
| 684 | static void |
| 685 | array_attach (char *args, int from_tty) |
| 686 | { |
| 687 | if (from_tty) |
| 688 | printf ("Starting remote %s debugging\n", target_shortname); |
| 689 | |
| 690 | debuglogs (1, "array_attach (args=%s)", args); |
| 691 | |
| 692 | printf_monitor ("go %x\n"); |
| 693 | /* swallow the echo. */ |
| 694 | expect ("go %x\n", 1); |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * array_resume -- Tell the remote machine to resume. |
| 699 | */ |
| 700 | static void |
| 701 | array_resume (int pid, int step, enum target_signal sig) |
| 702 | { |
| 703 | debuglogs (1, "array_resume (step=%d, sig=%d)", step, sig); |
| 704 | |
| 705 | if (step) |
| 706 | { |
| 707 | printf_monitor ("s\n"); |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | printf_monitor ("go\n"); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | #define TMPBUFSIZ 5 |
| 716 | |
| 717 | /* |
| 718 | * array_wait -- Wait until the remote machine stops, then return, |
| 719 | * storing status in status just as `wait' would. |
| 720 | */ |
| 721 | static int |
| 722 | array_wait (int pid, struct target_waitstatus *status) |
| 723 | { |
| 724 | int old_timeout = timeout; |
| 725 | int result, i; |
| 726 | char c; |
| 727 | serial_t tty_desc; |
| 728 | serial_ttystate ttystate; |
| 729 | |
| 730 | debuglogs (1, "array_wait (), printing extraneous text."); |
| 731 | |
| 732 | status->kind = TARGET_WAITKIND_EXITED; |
| 733 | status->value.integer = 0; |
| 734 | |
| 735 | timeout = 0; /* Don't time out -- user program is running. */ |
| 736 | |
| 737 | #if !defined(__GO32__) && !defined(__MSDOS__) && !defined(_WIN32) |
| 738 | tty_desc = SERIAL_FDOPEN (0); |
| 739 | ttystate = SERIAL_GET_TTY_STATE (tty_desc); |
| 740 | SERIAL_RAW (tty_desc); |
| 741 | |
| 742 | i = 0; |
| 743 | /* poll on the serial port and the keyboard. */ |
| 744 | while (1) |
| 745 | { |
| 746 | c = readchar (timeout); |
| 747 | if (c > 0) |
| 748 | { |
| 749 | if (c == *(ARRAY_PROMPT + i)) |
| 750 | { |
| 751 | if (++i >= strlen (ARRAY_PROMPT)) |
| 752 | { /* matched the prompt */ |
| 753 | debuglogs (4, "array_wait(), got the expect_prompt."); |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | else |
| 758 | { /* not the prompt */ |
| 759 | i = 0; |
| 760 | } |
| 761 | fputc_unfiltered (c, gdb_stdout); |
| 762 | gdb_flush (gdb_stdout); |
| 763 | } |
| 764 | c = SERIAL_READCHAR (tty_desc, timeout); |
| 765 | if (c > 0) |
| 766 | { |
| 767 | SERIAL_WRITE (array_desc, &c, 1); |
| 768 | /* do this so it looks like there's keyboard echo */ |
| 769 | if (c == 3) /* exit on Control-C */ |
| 770 | break; |
| 771 | #if 0 |
| 772 | fputc_unfiltered (c, gdb_stdout); |
| 773 | gdb_flush (gdb_stdout); |
| 774 | #endif |
| 775 | } |
| 776 | } |
| 777 | SERIAL_SET_TTY_STATE (tty_desc, ttystate); |
| 778 | #else |
| 779 | expect_prompt (1); |
| 780 | debuglogs (4, "array_wait(), got the expect_prompt."); |
| 781 | #endif |
| 782 | |
| 783 | status->kind = TARGET_WAITKIND_STOPPED; |
| 784 | status->value.sig = TARGET_SIGNAL_TRAP; |
| 785 | |
| 786 | timeout = old_timeout; |
| 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * array_fetch_registers -- read the remote registers into the |
| 793 | * block regs. |
| 794 | */ |
| 795 | static void |
| 796 | array_fetch_registers (int ignored) |
| 797 | { |
| 798 | int regno, i; |
| 799 | char *p; |
| 800 | unsigned char packet[PBUFSIZ]; |
| 801 | char regs[REGISTER_BYTES]; |
| 802 | |
| 803 | debuglogs (1, "array_fetch_registers (ignored=%d)\n", ignored); |
| 804 | |
| 805 | memset (packet, 0, PBUFSIZ); |
| 806 | /* Unimplemented registers read as all bits zero. */ |
| 807 | memset (regs, 0, REGISTER_BYTES); |
| 808 | make_gdb_packet (packet, "g"); |
| 809 | if (array_send_packet (packet) == 0) |
| 810 | error ("Couldn't transmit packet\n"); |
| 811 | if (array_get_packet (packet) == 0) |
| 812 | error ("Couldn't receive packet\n"); |
| 813 | /* FIXME: read bytes from packet */ |
| 814 | debuglogs (4, "array_fetch_registers: Got a \"%s\" back\n", packet); |
| 815 | for (regno = 0; regno <= PC_REGNUM + 4; regno++) |
| 816 | { |
| 817 | /* supply register stores in target byte order, so swap here */ |
| 818 | /* FIXME: convert from ASCII hex to raw bytes */ |
| 819 | i = ascii2hexword (packet + (regno * 8)); |
| 820 | debuglogs (5, "Adding register %d = %x\n", regno, i); |
| 821 | SWAP_TARGET_AND_HOST (&i, 4); |
| 822 | supply_register (regno, (char *) &i); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * This is unused by targets like this one that use a |
| 828 | * protocol based on GDB's remote protocol. |
| 829 | */ |
| 830 | static void |
| 831 | array_fetch_register (int ignored) |
| 832 | { |
| 833 | array_fetch_registers (0 /* ignored */); |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | * Get all the registers from the targets. They come back in a large array. |
| 838 | */ |
| 839 | static void |
| 840 | array_store_registers (int ignored) |
| 841 | { |
| 842 | int regno; |
| 843 | unsigned long i; |
| 844 | char packet[PBUFSIZ]; |
| 845 | char buf[PBUFSIZ]; |
| 846 | char num[9]; |
| 847 | |
| 848 | debuglogs (1, "array_store_registers()"); |
| 849 | |
| 850 | memset (packet, 0, PBUFSIZ); |
| 851 | memset (buf, 0, PBUFSIZ); |
| 852 | buf[0] = 'G'; |
| 853 | |
| 854 | /* Unimplemented registers read as all bits zero. */ |
| 855 | /* FIXME: read bytes from packet */ |
| 856 | for (regno = 0; regno < 41; regno++) |
| 857 | { /* FIXME */ |
| 858 | /* supply register stores in target byte order, so swap here */ |
| 859 | /* FIXME: convert from ASCII hex to raw bytes */ |
| 860 | i = (unsigned long) read_register (regno); |
| 861 | hexword2ascii (num, i); |
| 862 | strcpy (buf + (regno * 8) + 1, num); |
| 863 | } |
| 864 | *(buf + (regno * 8) + 2) = 0; |
| 865 | make_gdb_packet (packet, buf); |
| 866 | if (array_send_packet (packet) == 0) |
| 867 | error ("Couldn't transmit packet\n"); |
| 868 | if (array_get_packet (packet) == 0) |
| 869 | error ("Couldn't receive packet\n"); |
| 870 | |
| 871 | registers_changed (); |
| 872 | } |
| 873 | |
| 874 | /* |
| 875 | * This is unused by targets like this one that use a |
| 876 | * protocol based on GDB's remote protocol. |
| 877 | */ |
| 878 | static void |
| 879 | array_store_register (int ignored) |
| 880 | { |
| 881 | array_store_registers (0 /* ignored */); |
| 882 | } |
| 883 | |
| 884 | /* Get ready to modify the registers array. On machines which store |
| 885 | individual registers, this doesn't need to do anything. On machines |
| 886 | which store all the registers in one fell swoop, this makes sure |
| 887 | that registers contains all the registers from the program being |
| 888 | debugged. */ |
| 889 | |
| 890 | static void |
| 891 | array_prepare_to_store (void) |
| 892 | { |
| 893 | /* Do nothing, since we can store individual regs */ |
| 894 | } |
| 895 | |
| 896 | static void |
| 897 | array_files_info (void) |
| 898 | { |
| 899 | printf ("\tAttached to %s at %d baud.\n", |
| 900 | dev_name, baudrate); |
| 901 | } |
| 902 | |
| 903 | /* |
| 904 | * array_write_inferior_memory -- Copy LEN bytes of data from debugger |
| 905 | * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved. |
| 906 | */ |
| 907 | static int |
| 908 | array_write_inferior_memory (CORE_ADDR memaddr, unsigned char *myaddr, int len) |
| 909 | { |
| 910 | unsigned long i; |
| 911 | int j; |
| 912 | char packet[PBUFSIZ]; |
| 913 | char buf[PBUFSIZ]; |
| 914 | char num[9]; |
| 915 | char *p; |
| 916 | |
| 917 | debuglogs (1, "array_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len); |
| 918 | memset (buf, '\0', PBUFSIZ); /* this also sets the string terminator */ |
| 919 | p = buf; |
| 920 | |
| 921 | *p++ = 'M'; /* The command to write memory */ |
| 922 | hexword2ascii (num, memaddr); /* convert the address */ |
| 923 | strcpy (p, num); /* copy the address */ |
| 924 | p += 8; |
| 925 | *p++ = ','; /* add comma delimeter */ |
| 926 | hexword2ascii (num, len); /* Get the length as a 4 digit number */ |
| 927 | *p++ = num[4]; |
| 928 | *p++ = num[5]; |
| 929 | *p++ = num[6]; |
| 930 | *p++ = num[7]; |
| 931 | *p++ = ':'; /* add the colon delimeter */ |
| 932 | for (j = 0; j < len; j++) |
| 933 | { /* copy the data in after converting it */ |
| 934 | *p++ = tohex ((myaddr[j] >> 4) & 0xf); |
| 935 | *p++ = tohex (myaddr[j] & 0xf); |
| 936 | } |
| 937 | |
| 938 | make_gdb_packet (packet, buf); |
| 939 | if (array_send_packet (packet) == 0) |
| 940 | error ("Couldn't transmit packet\n"); |
| 941 | if (array_get_packet (packet) == 0) |
| 942 | error ("Couldn't receive packet\n"); |
| 943 | |
| 944 | return len; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * array_read_inferior_memory -- read LEN bytes from inferior memory |
| 949 | * at MEMADDR. Put the result at debugger address MYADDR. Returns |
| 950 | * length moved. |
| 951 | */ |
| 952 | static int |
| 953 | array_read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) |
| 954 | { |
| 955 | int j; |
| 956 | char buf[20]; |
| 957 | char packet[PBUFSIZ]; |
| 958 | int count; /* Number of bytes read so far. */ |
| 959 | unsigned long startaddr; /* Starting address of this pass. */ |
| 960 | int len_this_pass; /* Number of bytes to read in this pass. */ |
| 961 | |
| 962 | debuglogs (1, "array_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len); |
| 963 | |
| 964 | /* Note that this code works correctly if startaddr is just less |
| 965 | than UINT_MAX (well, really CORE_ADDR_MAX if there was such a |
| 966 | thing). That is, something like |
| 967 | array_read_bytes (CORE_ADDR_MAX - 4, foo, 4) |
| 968 | works--it never adds len To memaddr and gets 0. */ |
| 969 | /* However, something like |
| 970 | array_read_bytes (CORE_ADDR_MAX - 3, foo, 4) |
| 971 | doesn't need to work. Detect it and give up if there's an attempt |
| 972 | to do that. */ |
| 973 | if (((memaddr - 1) + len) < memaddr) |
| 974 | { |
| 975 | errno = EIO; |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | for (count = 0, startaddr = memaddr; count < len; startaddr += len_this_pass) |
| 980 | { |
| 981 | /* Try to align to 16 byte boundry (why?) */ |
| 982 | len_this_pass = 16; |
| 983 | if ((startaddr % 16) != 0) |
| 984 | { |
| 985 | len_this_pass -= startaddr % 16; |
| 986 | } |
| 987 | /* Only transfer bytes we need */ |
| 988 | if (len_this_pass > (len - count)) |
| 989 | { |
| 990 | len_this_pass = (len - count); |
| 991 | } |
| 992 | /* Fetch the bytes */ |
| 993 | debuglogs (3, "read %d bytes from inferior address %x", len_this_pass, |
| 994 | startaddr); |
| 995 | sprintf (buf, "m%08lx,%04x", startaddr, len_this_pass); |
| 996 | make_gdb_packet (packet, buf); |
| 997 | if (array_send_packet (packet) == 0) |
| 998 | { |
| 999 | error ("Couldn't transmit packet\n"); |
| 1000 | } |
| 1001 | if (array_get_packet (packet) == 0) |
| 1002 | { |
| 1003 | error ("Couldn't receive packet\n"); |
| 1004 | } |
| 1005 | if (*packet == 0) |
| 1006 | { |
| 1007 | error ("Got no data in the GDB packet\n"); |
| 1008 | } |
| 1009 | /* Pick packet apart and xfer bytes to myaddr */ |
| 1010 | debuglogs (4, "array_read_inferior_memory: Got a \"%s\" back\n", packet); |
| 1011 | for (j = 0; j < len_this_pass; j++) |
| 1012 | { |
| 1013 | /* extract the byte values */ |
| 1014 | myaddr[count++] = from_hex (*(packet + (j * 2))) * 16 + from_hex (*(packet + (j * 2) + 1)); |
| 1015 | debuglogs (5, "myaddr[%d] set to %x\n", count - 1, myaddr[count - 1]); |
| 1016 | } |
| 1017 | } |
| 1018 | return (count); |
| 1019 | } |
| 1020 | |
| 1021 | /* Transfer LEN bytes between GDB address MYADDR and target address |
| 1022 | MEMADDR. If WRITE is non-zero, transfer them to the target, |
| 1023 | otherwise transfer them from the target. TARGET is unused. |
| 1024 | |
| 1025 | Returns the number of bytes transferred. */ |
| 1026 | |
| 1027 | static int |
| 1028 | array_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, |
| 1029 | struct target_ops *target) |
| 1030 | { |
| 1031 | if (write) |
| 1032 | return array_write_inferior_memory (memaddr, myaddr, len); |
| 1033 | else |
| 1034 | return array_read_inferior_memory (memaddr, myaddr, len); |
| 1035 | } |
| 1036 | |
| 1037 | static void |
| 1038 | array_kill (char *args, int from_tty) |
| 1039 | { |
| 1040 | return; /* ignore attempts to kill target system */ |
| 1041 | } |
| 1042 | |
| 1043 | /* Clean up when a program exits. |
| 1044 | The program actually lives on in the remote processor's RAM, and may be |
| 1045 | run again without a download. Don't leave it full of breakpoint |
| 1046 | instructions. */ |
| 1047 | |
| 1048 | static void |
| 1049 | array_mourn_inferior (void) |
| 1050 | { |
| 1051 | remove_breakpoints (); |
| 1052 | generic_mourn_inferior (); /* Do all the proper things now */ |
| 1053 | } |
| 1054 | |
| 1055 | #define MAX_ARRAY_BREAKPOINTS 16 |
| 1056 | |
| 1057 | static CORE_ADDR breakaddr[MAX_ARRAY_BREAKPOINTS] = |
| 1058 | {0}; |
| 1059 | |
| 1060 | /* |
| 1061 | * array_insert_breakpoint -- add a breakpoint |
| 1062 | */ |
| 1063 | static int |
| 1064 | array_insert_breakpoint (CORE_ADDR addr, char *shadow) |
| 1065 | { |
| 1066 | int i; |
| 1067 | int bp_size = 0; |
| 1068 | CORE_ADDR bp_addr = addr; |
| 1069 | |
| 1070 | debuglogs (1, "array_insert_breakpoint() addr = 0x%x", addr); |
| 1071 | BREAKPOINT_FROM_PC (&bp_addr, &bp_size); |
| 1072 | |
| 1073 | for (i = 0; i <= MAX_ARRAY_BREAKPOINTS; i++) |
| 1074 | { |
| 1075 | if (breakaddr[i] == 0) |
| 1076 | { |
| 1077 | breakaddr[i] = addr; |
| 1078 | if (sr_get_debug () > 4) |
| 1079 | printf ("Breakpoint at %s\n", paddr_nz (addr)); |
| 1080 | array_read_inferior_memory (bp_addr, shadow, bp_size); |
| 1081 | printf_monitor ("b 0x%x\n", addr); |
| 1082 | expect_prompt (1); |
| 1083 | return 0; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | fprintf (stderr, "Too many breakpoints (> 16) for monitor\n"); |
| 1088 | return 1; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * _remove_breakpoint -- Tell the monitor to remove a breakpoint |
| 1093 | */ |
| 1094 | static int |
| 1095 | array_remove_breakpoint (CORE_ADDR addr, char *shadow) |
| 1096 | { |
| 1097 | int i; |
| 1098 | |
| 1099 | debuglogs (1, "array_remove_breakpoint() addr = 0x%x", addr); |
| 1100 | |
| 1101 | for (i = 0; i < MAX_ARRAY_BREAKPOINTS; i++) |
| 1102 | { |
| 1103 | if (breakaddr[i] == addr) |
| 1104 | { |
| 1105 | breakaddr[i] = 0; |
| 1106 | /* some monitors remove breakpoints based on the address */ |
| 1107 | printf_monitor ("bd %x\n", i); |
| 1108 | expect_prompt (1); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | } |
| 1112 | fprintf (stderr, "Can't find breakpoint associated with 0x%s\n", |
| 1113 | paddr_nz (addr)); |
| 1114 | return 1; |
| 1115 | } |
| 1116 | |
| 1117 | static void |
| 1118 | array_stop (void) |
| 1119 | { |
| 1120 | debuglogs (1, "array_stop()"); |
| 1121 | printf_monitor ("\003"); |
| 1122 | expect_prompt (1); |
| 1123 | } |
| 1124 | |
| 1125 | /* |
| 1126 | * array_command -- put a command string, in args, out to MONITOR. |
| 1127 | * Output from MONITOR is placed on the users terminal until the |
| 1128 | * expect_prompt is seen. FIXME |
| 1129 | */ |
| 1130 | static void |
| 1131 | monitor_command (char *args, int fromtty) |
| 1132 | { |
| 1133 | debuglogs (1, "monitor_command (args=%s)", args); |
| 1134 | |
| 1135 | if (array_desc == NULL) |
| 1136 | error ("monitor target not open."); |
| 1137 | |
| 1138 | if (!args) |
| 1139 | error ("Missing command."); |
| 1140 | |
| 1141 | printf_monitor ("%s\n", args); |
| 1142 | expect_prompt (0); |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * make_gdb_packet -- make a GDB packet. The data is always ASCII. |
| 1147 | * A debug packet whose contents are <data> |
| 1148 | * is encapsulated for transmission in the form: |
| 1149 | * |
| 1150 | * $ <data> # CSUM1 CSUM2 |
| 1151 | * |
| 1152 | * <data> must be ASCII alphanumeric and cannot include characters |
| 1153 | * '$' or '#'. If <data> starts with two characters followed by |
| 1154 | * ':', then the existing stubs interpret this as a sequence number. |
| 1155 | * |
| 1156 | * CSUM1 and CSUM2 are ascii hex representation of an 8-bit |
| 1157 | * checksum of <data>, the most significant nibble is sent first. |
| 1158 | * the hex digits 0-9,a-f are used. |
| 1159 | * |
| 1160 | */ |
| 1161 | static void |
| 1162 | make_gdb_packet (char *buf, char *data) |
| 1163 | { |
| 1164 | int i; |
| 1165 | unsigned char csum = 0; |
| 1166 | int cnt; |
| 1167 | char *p; |
| 1168 | |
| 1169 | debuglogs (3, "make_gdb_packet(%s)\n", data); |
| 1170 | cnt = strlen (data); |
| 1171 | if (cnt > PBUFSIZ) |
| 1172 | error ("make_gdb_packet(): to much data\n"); |
| 1173 | |
| 1174 | /* start with the packet header */ |
| 1175 | p = buf; |
| 1176 | *p++ = '$'; |
| 1177 | |
| 1178 | /* calculate the checksum */ |
| 1179 | for (i = 0; i < cnt; i++) |
| 1180 | { |
| 1181 | csum += data[i]; |
| 1182 | *p++ = data[i]; |
| 1183 | } |
| 1184 | |
| 1185 | /* terminate the data with a '#' */ |
| 1186 | *p++ = '#'; |
| 1187 | |
| 1188 | /* add the checksum as two ascii digits */ |
| 1189 | *p++ = tohex ((csum >> 4) & 0xf); |
| 1190 | *p++ = tohex (csum & 0xf); |
| 1191 | *p = 0x0; /* Null terminator on string */ |
| 1192 | } |
| 1193 | |
| 1194 | /* |
| 1195 | * array_send_packet -- send a GDB packet to the target with error handling. We |
| 1196 | * get a '+' (ACK) back if the packet is received and the checksum |
| 1197 | * matches. Otherwise a '-' (NAK) is returned. It returns a 1 for a |
| 1198 | * successful transmition, or a 0 for a failure. |
| 1199 | */ |
| 1200 | static int |
| 1201 | array_send_packet (char *packet) |
| 1202 | { |
| 1203 | int c, retries, i; |
| 1204 | char junk[PBUFSIZ]; |
| 1205 | |
| 1206 | retries = 0; |
| 1207 | |
| 1208 | #if 0 |
| 1209 | /* scan the packet to make sure it only contains valid characters. |
| 1210 | this may sound silly, but sometimes a garbled packet will hang |
| 1211 | the target board. We scan the whole thing, then print the error |
| 1212 | message. |
| 1213 | */ |
| 1214 | for (i = 0; i < strlen (packet); i++) |
| 1215 | { |
| 1216 | debuglogs (5, "array_send_packet(): Scanning \'%c\'\n", packet[i]); |
| 1217 | /* legit hex numbers or command */ |
| 1218 | if ((isxdigit (packet[i])) || (isalpha (packet[i]))) |
| 1219 | continue; |
| 1220 | switch (packet[i]) |
| 1221 | { |
| 1222 | case '+': /* ACK */ |
| 1223 | case '-': /* NAK */ |
| 1224 | case '#': /* end of packet */ |
| 1225 | case '$': /* start of packet */ |
| 1226 | continue; |
| 1227 | default: /* bogus character */ |
| 1228 | retries++; |
| 1229 | debuglogs (4, "array_send_packet(): Found a non-ascii digit \'%c\' in the packet.\n", packet[i]); |
| 1230 | } |
| 1231 | } |
| 1232 | #endif |
| 1233 | |
| 1234 | if (retries > 0) |
| 1235 | error ("Can't send packet, found %d non-ascii characters", retries); |
| 1236 | |
| 1237 | /* ok, try to send the packet */ |
| 1238 | retries = 0; |
| 1239 | while (retries++ <= 10) |
| 1240 | { |
| 1241 | printf_monitor ("%s", packet); |
| 1242 | |
| 1243 | /* read until either a timeout occurs (-2) or '+' is read */ |
| 1244 | while (retries <= 10) |
| 1245 | { |
| 1246 | c = readchar (-timeout); |
| 1247 | debuglogs (3, "Reading a GDB protocol packet... Got a '%c'\n", c); |
| 1248 | switch (c) |
| 1249 | { |
| 1250 | case '+': |
| 1251 | debuglogs (3, "Got Ack\n"); |
| 1252 | return 1; |
| 1253 | case SERIAL_TIMEOUT: |
| 1254 | debuglogs (3, "Timed out reading serial port\n"); |
| 1255 | printf_monitor ("@"); /* resync with the monitor */ |
| 1256 | expect_prompt (1); /* See if we get a expect_prompt */ |
| 1257 | break; /* Retransmit buffer */ |
| 1258 | case '-': |
| 1259 | debuglogs (3, "Got NAK\n"); |
| 1260 | printf_monitor ("@"); /* resync with the monitor */ |
| 1261 | expect_prompt (1); /* See if we get a expect_prompt */ |
| 1262 | break; |
| 1263 | case '$': |
| 1264 | /* it's probably an old response, or the echo of our command. |
| 1265 | * just gobble up the packet and ignore it. |
| 1266 | */ |
| 1267 | debuglogs (3, "Got a junk packet\n"); |
| 1268 | i = 0; |
| 1269 | do |
| 1270 | { |
| 1271 | c = readchar (timeout); |
| 1272 | junk[i++] = c; |
| 1273 | } |
| 1274 | while (c != '#'); |
| 1275 | c = readchar (timeout); |
| 1276 | junk[i++] = c; |
| 1277 | c = readchar (timeout); |
| 1278 | junk[i++] = c; |
| 1279 | junk[i++] = '\0'; |
| 1280 | debuglogs (3, "Reading a junk packet, got a \"%s\"\n", junk); |
| 1281 | continue; /* Now, go look for next packet */ |
| 1282 | default: |
| 1283 | continue; |
| 1284 | } |
| 1285 | retries++; |
| 1286 | debuglogs (3, "Retransmitting packet \"%s\"\n", packet); |
| 1287 | break; /* Here to retransmit */ |
| 1288 | } |
| 1289 | } /* outer while */ |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * array_get_packet -- get a GDB packet from the target. Basically we read till we |
| 1295 | * see a '#', then check the checksum. It returns a 1 if it's gotten a |
| 1296 | * packet, or a 0 it the packet wasn't transmitted correctly. |
| 1297 | */ |
| 1298 | static int |
| 1299 | array_get_packet (char *packet) |
| 1300 | { |
| 1301 | int c; |
| 1302 | int retries; |
| 1303 | unsigned char csum; |
| 1304 | unsigned char pktcsum; |
| 1305 | char *bp; |
| 1306 | |
| 1307 | csum = 0; |
| 1308 | bp = packet; |
| 1309 | |
| 1310 | memset (packet, 1, PBUFSIZ); |
| 1311 | retries = 0; |
| 1312 | while (retries <= 10) |
| 1313 | { |
| 1314 | do |
| 1315 | { |
| 1316 | c = readchar (timeout); |
| 1317 | if (c == SERIAL_TIMEOUT) |
| 1318 | { |
| 1319 | debuglogs (3, "array_get_packet: got time out from serial port.\n"); |
| 1320 | } |
| 1321 | debuglogs (3, "Waiting for a '$', got a %c\n", c); |
| 1322 | } |
| 1323 | while (c != '$'); |
| 1324 | |
| 1325 | retries = 0; |
| 1326 | while (retries <= 10) |
| 1327 | { |
| 1328 | c = readchar (timeout); |
| 1329 | debuglogs (3, "array_get_packet: got a '%c'\n", c); |
| 1330 | switch (c) |
| 1331 | { |
| 1332 | case SERIAL_TIMEOUT: |
| 1333 | debuglogs (3, "Timeout in mid-packet, retrying\n"); |
| 1334 | return 0; |
| 1335 | case '$': |
| 1336 | debuglogs (3, "Saw new packet start in middle of old one\n"); |
| 1337 | return 0; /* Start a new packet, count retries */ |
| 1338 | case '#': |
| 1339 | *bp = '\0'; |
| 1340 | pktcsum = from_hex (readchar (timeout)) << 4; |
| 1341 | pktcsum |= from_hex (readchar (timeout)); |
| 1342 | if (csum == 0) |
| 1343 | debuglogs (3, "\nGDB packet checksum zero, must be a bogus packet\n"); |
| 1344 | if (csum == pktcsum) |
| 1345 | { |
| 1346 | debuglogs (3, "\nGDB packet checksum correct, packet data is \"%s\",\n", packet); |
| 1347 | printf_monitor ("@"); |
| 1348 | expect_prompt (1); |
| 1349 | return 1; |
| 1350 | } |
| 1351 | debuglogs (3, "Bad checksum, sentsum=0x%x, csum=0x%x\n", pktcsum, csum); |
| 1352 | return 0; |
| 1353 | case '*': /* Run length encoding */ |
| 1354 | debuglogs (5, "Run length encoding in packet\n"); |
| 1355 | csum += c; |
| 1356 | c = readchar (timeout); |
| 1357 | csum += c; |
| 1358 | c = c - ' ' + 3; /* Compute repeat count */ |
| 1359 | |
| 1360 | if (c > 0 && c < 255 && bp + c - 1 < packet + PBUFSIZ - 1) |
| 1361 | { |
| 1362 | memset (bp, *(bp - 1), c); |
| 1363 | bp += c; |
| 1364 | continue; |
| 1365 | } |
| 1366 | *bp = '\0'; |
| 1367 | printf_filtered ("Repeat count %d too large for buffer.\n", c); |
| 1368 | return 0; |
| 1369 | |
| 1370 | default: |
| 1371 | if ((!isxdigit (c)) && (!ispunct (c))) |
| 1372 | debuglogs (4, "Got a non-ascii digit \'%c\'.\\n", c); |
| 1373 | if (bp < packet + PBUFSIZ - 1) |
| 1374 | { |
| 1375 | *bp++ = c; |
| 1376 | csum += c; |
| 1377 | continue; |
| 1378 | } |
| 1379 | |
| 1380 | *bp = '\0'; |
| 1381 | puts_filtered ("Remote packet too long.\n"); |
| 1382 | return 0; |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | return 0; /* exceeded retries */ |
| 1387 | } |
| 1388 | |
| 1389 | /* |
| 1390 | * ascii2hexword -- convert an ascii number represented by 8 digits to a hex value. |
| 1391 | */ |
| 1392 | static unsigned long |
| 1393 | ascii2hexword (unsigned char *mem) |
| 1394 | { |
| 1395 | unsigned long val; |
| 1396 | int i; |
| 1397 | char buf[9]; |
| 1398 | |
| 1399 | val = 0; |
| 1400 | for (i = 0; i < 8; i++) |
| 1401 | { |
| 1402 | val <<= 4; |
| 1403 | if (mem[i] >= 'A' && mem[i] <= 'F') |
| 1404 | val = val + mem[i] - 'A' + 10; |
| 1405 | if (mem[i] >= 'a' && mem[i] <= 'f') |
| 1406 | val = val + mem[i] - 'a' + 10; |
| 1407 | if (mem[i] >= '0' && mem[i] <= '9') |
| 1408 | val = val + mem[i] - '0'; |
| 1409 | buf[i] = mem[i]; |
| 1410 | } |
| 1411 | buf[8] = '\0'; |
| 1412 | debuglogs (4, "ascii2hexword() got a 0x%x from %s(%x).\n", val, buf, mem); |
| 1413 | return val; |
| 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * ascii2hexword -- convert a hex value to an ascii number represented by 8 |
| 1418 | * digits. |
| 1419 | */ |
| 1420 | static void |
| 1421 | hexword2ascii (unsigned char *mem, unsigned long num) |
| 1422 | { |
| 1423 | int i; |
| 1424 | unsigned char ch; |
| 1425 | |
| 1426 | debuglogs (4, "hexword2ascii() converting %x ", num); |
| 1427 | for (i = 7; i >= 0; i--) |
| 1428 | { |
| 1429 | mem[i] = tohex ((num >> 4) & 0xf); |
| 1430 | mem[i] = tohex (num & 0xf); |
| 1431 | num = num >> 4; |
| 1432 | } |
| 1433 | mem[8] = '\0'; |
| 1434 | debuglogs (4, "\tto a %s", mem); |
| 1435 | } |
| 1436 | |
| 1437 | /* Convert hex digit A to a number. */ |
| 1438 | static int |
| 1439 | from_hex (int a) |
| 1440 | { |
| 1441 | if (a == 0) |
| 1442 | return 0; |
| 1443 | |
| 1444 | debuglogs (4, "from_hex got a 0x%x(%c)\n", a, a); |
| 1445 | if (a >= '0' && a <= '9') |
| 1446 | return a - '0'; |
| 1447 | if (a >= 'a' && a <= 'f') |
| 1448 | return a - 'a' + 10; |
| 1449 | if (a >= 'A' && a <= 'F') |
| 1450 | return a - 'A' + 10; |
| 1451 | else |
| 1452 | { |
| 1453 | error ("Reply contains invalid hex digit 0x%x", a); |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | /* Convert number NIB to a hex digit. */ |
| 1458 | static int |
| 1459 | tohex (int nib) |
| 1460 | { |
| 1461 | if (nib < 10) |
| 1462 | return '0' + nib; |
| 1463 | else |
| 1464 | return 'a' + nib - 10; |
| 1465 | } |
| 1466 | |
| 1467 | /* |
| 1468 | * _initialize_remote_monitors -- setup a few addtitional commands that |
| 1469 | * are usually only used by monitors. |
| 1470 | */ |
| 1471 | void |
| 1472 | _initialize_remote_monitors (void) |
| 1473 | { |
| 1474 | /* generic monitor command */ |
| 1475 | add_com ("monitor", class_obscure, monitor_command, |
| 1476 | "Send a command to the debug monitor."); |
| 1477 | |
| 1478 | } |
| 1479 | |
| 1480 | /* |
| 1481 | * _initialize_array -- do any special init stuff for the target. |
| 1482 | */ |
| 1483 | void |
| 1484 | _initialize_array (void) |
| 1485 | { |
| 1486 | init_array_ops (); |
| 1487 | add_target (&array_ops); |
| 1488 | } |