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