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