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