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