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