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