Commit | Line | Data |
---|---|---|
5905161c RP |
1 | /* Remote debugging interface for Motorola's MVME187BUG monitor, an embedded |
2 | monitor for the m88k. | |
3 | ||
4 | Copyright 1992, 1993 Free Software Foundation, Inc. | |
5 | Contributed by Cygnus Support. Written by K. Richard Pixley. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
6c9638b4 | 21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
5905161c RP |
22 | |
23 | #include "defs.h" | |
24 | #include "inferior.h" | |
25 | #include "wait.h" | |
be58e87e | 26 | |
2b576293 | 27 | #include "gdb_string.h" |
5905161c RP |
28 | #include <ctype.h> |
29 | #include <fcntl.h> | |
30 | #include <signal.h> | |
31 | #include <setjmp.h> | |
32 | #include <errno.h> | |
be58e87e | 33 | |
5905161c | 34 | #include "terminal.h" |
5905161c | 35 | #include "gdbcore.h" |
be58e87e RP |
36 | #include "gdbcmd.h" |
37 | ||
c6f494e8 | 38 | #include "remote-utils.h" |
755892d6 | 39 | |
c719b714 | 40 | |
be58e87e | 41 | extern int sleep(); |
5905161c RP |
42 | |
43 | /* External data declarations */ | |
44 | extern int stop_soon_quietly; /* for wait_for_inferior */ | |
45 | ||
46 | /* Forward data declarations */ | |
ae87844d | 47 | extern struct target_ops bug_ops; /* Forward declaration */ |
5905161c RP |
48 | |
49 | /* Forward function declarations */ | |
755892d6 | 50 | static int bug_clear_breakpoints PARAMS((void)); |
be58e87e | 51 | |
c6f494e8 | 52 | static int bug_read_memory PARAMS((CORE_ADDR memaddr, |
755892d6 RP |
53 | unsigned char *myaddr, |
54 | int len)); | |
be58e87e | 55 | |
c6f494e8 | 56 | static int bug_write_memory PARAMS((CORE_ADDR memaddr, |
755892d6 RP |
57 | unsigned char *myaddr, |
58 | int len)); | |
be58e87e | 59 | |
be58e87e RP |
60 | /* This variable is somewhat arbitrary. It's here so that it can be |
61 | set from within a running gdb. */ | |
62 | ||
63 | static int srec_max_retries = 3; | |
64 | ||
65 | /* Each S-record download to the target consists of an S0 header | |
66 | record, some number of S3 data records, and one S7 termination | |
67 | record. I call this download a "frame". Srec_frame says how many | |
68 | bytes will be represented in each frame. */ | |
69 | ||
e3a7e9d5 JK |
70 | #define SREC_SIZE 160 |
71 | static int srec_frame = SREC_SIZE; | |
be58e87e RP |
72 | |
73 | /* This variable determines how many bytes will be represented in each | |
74 | S3 s-record. */ | |
75 | ||
76 | static int srec_bytes = 40; | |
77 | ||
78 | /* At one point it appeared to me as though the bug monitor could not | |
79 | really be expected to receive two sequential characters at 9600 | |
80 | baud reliably. Echo-pacing is an attempt to force data across the | |
81 | line even in this condition. Specifically, in echo-pace mode, each | |
82 | character is sent one at a time and we look for the echo before | |
83 | sending the next. This is excruciatingly slow. */ | |
5905161c | 84 | |
be58e87e RP |
85 | static int srec_echo_pace = 0; |
86 | ||
87 | /* How long to wait after an srec for a possible error message. | |
88 | Similar to the above, I tried sleeping after sending each S3 record | |
89 | in hopes that I might actually see error messages from the bug | |
90 | monitor. This might actually work if we were to use sleep | |
91 | intervals smaller than 1 second. */ | |
92 | ||
93 | static int srec_sleep = 0; | |
94 | ||
95 | /* Every srec_noise records, flub the checksum. This is a debugging | |
96 | feature. Set the variable to something other than 1 in order to | |
97 | inject *deliberate* checksum errors. One might do this if one | |
98 | wanted to test error handling and recovery. */ | |
99 | ||
100 | static int srec_noise = 0; | |
5905161c | 101 | |
5905161c RP |
102 | /* Called when SIGALRM signal sent due to alarm() timeout. */ |
103 | ||
104 | /* Number of SIGTRAPs we need to simulate. That is, the next | |
105 | NEED_ARTIFICIAL_TRAP calls to bug_wait should just return | |
106 | SIGTRAP without actually waiting for anything. */ | |
107 | ||
108 | static int need_artificial_trap = 0; | |
109 | ||
5905161c RP |
110 | /* |
111 | * Download a file specified in 'args', to the bug. | |
112 | */ | |
be58e87e | 113 | |
5905161c RP |
114 | static void |
115 | bug_load (args, fromtty) | |
116 | char *args; | |
117 | int fromtty; | |
118 | { | |
119 | bfd *abfd; | |
120 | asection *s; | |
5905161c RP |
121 | char buffer[1024]; |
122 | ||
c6f494e8 | 123 | sr_check_open (); |
5905161c | 124 | |
c6f494e8 | 125 | dcache_flush (gr_get_dcache()); |
5905161c RP |
126 | inferior_pid = 0; |
127 | abfd = bfd_openr (args, 0); | |
128 | if (!abfd) | |
129 | { | |
130 | printf_filtered ("Unable to open file %s\n", args); | |
131 | return; | |
132 | } | |
133 | ||
134 | if (bfd_check_format (abfd, bfd_object) == 0) | |
135 | { | |
136 | printf_filtered ("File is not an object file\n"); | |
137 | return; | |
138 | } | |
139 | ||
140 | s = abfd->sections; | |
141 | while (s != (asection *) NULL) | |
142 | { | |
e3a7e9d5 | 143 | srec_frame = SREC_SIZE; |
5905161c RP |
144 | if (s->flags & SEC_LOAD) |
145 | { | |
146 | int i; | |
147 | ||
be58e87e | 148 | char *buffer = xmalloc (srec_frame); |
5905161c RP |
149 | |
150 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size); | |
be58e87e RP |
151 | fflush (stdout); |
152 | for (i = 0; i < s->_raw_size; i += srec_frame) | |
5905161c | 153 | { |
be58e87e RP |
154 | if (srec_frame > s->_raw_size - i) |
155 | srec_frame = s->_raw_size - i; | |
5905161c | 156 | |
be58e87e | 157 | bfd_get_section_contents (abfd, s, buffer, i, srec_frame); |
c6f494e8 | 158 | bug_write_memory (s->vma + i, buffer, srec_frame); |
5905161c RP |
159 | printf_filtered ("*"); |
160 | fflush (stdout); | |
161 | } | |
162 | printf_filtered ("\n"); | |
163 | free (buffer); | |
164 | } | |
165 | s = s->next; | |
166 | } | |
be58e87e | 167 | sprintf (buffer, "rs ip %lx", (unsigned long) abfd->start_address); |
c6f494e8 RP |
168 | sr_write_cr (buffer); |
169 | gr_expect_prompt (); | |
5905161c RP |
170 | } |
171 | ||
c6f494e8 | 172 | #if 0 |
5905161c RP |
173 | static char * |
174 | get_word (p) | |
175 | char **p; | |
176 | { | |
177 | char *s = *p; | |
178 | char *word; | |
179 | char *copy; | |
180 | size_t len; | |
181 | ||
182 | while (isspace (*s)) | |
183 | s++; | |
184 | ||
185 | word = s; | |
186 | ||
187 | len = 0; | |
188 | ||
189 | while (*s && !isspace (*s)) | |
190 | { | |
191 | s++; | |
192 | len++; | |
193 | ||
194 | } | |
195 | copy = xmalloc (len + 1); | |
196 | memcpy (copy, word, len); | |
197 | copy[len] = 0; | |
198 | *p = s; | |
199 | return copy; | |
200 | } | |
c6f494e8 | 201 | #endif |
5905161c | 202 | |
c6f494e8 RP |
203 | static struct gr_settings bug_settings = { |
204 | NULL, /* dcache */ | |
205 | "Bug>", /* prompt */ | |
206 | &bug_ops, /* ops */ | |
207 | bug_clear_breakpoints, /* clear_all_breakpoints */ | |
208 | bug_read_memory, /* readfunc */ | |
209 | bug_write_memory, /* writefunc */ | |
210 | gr_generic_checkin, /* checkin */ | |
211 | }; | |
5905161c | 212 | |
c6f494e8 RP |
213 | static char *cpu_check_strings[] = { |
214 | "=", | |
215 | "Invalid Register", | |
216 | }; | |
5905161c RP |
217 | |
218 | static void | |
c6f494e8 | 219 | bug_open (args, from_tty) |
5905161c RP |
220 | char *args; |
221 | int from_tty; | |
222 | { | |
c6f494e8 RP |
223 | if (args == NULL) |
224 | args = ""; | |
5905161c | 225 | |
c6f494e8 RP |
226 | gr_open(args, from_tty, &bug_settings); |
227 | /* decide *now* whether we are on an 88100 or an 88110 */ | |
228 | sr_write_cr("rs cr06"); | |
229 | sr_expect("rs cr06"); | |
5905161c | 230 | |
817ac7f8 | 231 | switch (gr_multi_scan(cpu_check_strings, 0)) |
c6f494e8 RP |
232 | { |
233 | case 0: /* this is an m88100 */ | |
234 | target_is_m88110 = 0; | |
235 | break; | |
236 | case 1: /* this is an m88110 */ | |
237 | target_is_m88110 = 1; | |
238 | break; | |
239 | default: | |
240 | abort(); | |
241 | } | |
5905161c RP |
242 | } |
243 | ||
244 | /* Tell the remote machine to resume. */ | |
245 | ||
246 | void | |
be58e87e | 247 | bug_resume (pid, step, sig) |
67ac9759 JK |
248 | int pid, step; |
249 | enum target_signal sig; | |
5905161c | 250 | { |
c6f494e8 | 251 | dcache_flush (gr_get_dcache()); |
5905161c RP |
252 | |
253 | if (step) | |
254 | { | |
c6f494e8 | 255 | sr_write_cr("t"); |
5905161c RP |
256 | |
257 | /* Force the next bug_wait to return a trap. Not doing anything | |
258 | about I/O from the target means that the user has to type | |
259 | "continue" to see any. FIXME, this should be fixed. */ | |
260 | need_artificial_trap = 1; | |
261 | } | |
262 | else | |
c6f494e8 | 263 | sr_write_cr ("g"); |
5905161c RP |
264 | |
265 | return; | |
266 | } | |
267 | ||
be58e87e RP |
268 | /* Wait until the remote machine stops, then return, |
269 | storing status in STATUS just as `wait' would. */ | |
270 | ||
16f6ab6b RP |
271 | static char *wait_strings[] = { |
272 | "At Breakpoint", | |
273 | "Exception: Data Access Fault (Local Bus Timeout)", | |
22ba28a5 | 274 | "\r8??\?-Bug>", /* The '\?' avoids creating a trigraph */ |
c6f494e8 | 275 | "\r197-Bug>", |
16f6ab6b RP |
276 | NULL, |
277 | }; | |
278 | ||
be58e87e | 279 | int |
de43d7d0 SG |
280 | bug_wait (pid, status) |
281 | int pid; | |
67ac9759 | 282 | struct target_waitstatus *status; |
be58e87e | 283 | { |
c6f494e8 | 284 | int old_timeout = sr_get_timeout(); |
be58e87e RP |
285 | int old_immediate_quit = immediate_quit; |
286 | ||
67ac9759 JK |
287 | status->kind = TARGET_WAITKIND_EXITED; |
288 | status->value.integer = 0; | |
be58e87e | 289 | |
16f6ab6b RP |
290 | /* read off leftovers from resume so that the rest can be passed |
291 | back out as stdout. */ | |
292 | if (need_artificial_trap == 0) | |
5905161c | 293 | { |
c6f494e8 RP |
294 | sr_expect("Effective address: "); |
295 | (void) sr_get_hex_word(); | |
296 | sr_expect ("\r\n"); | |
5905161c | 297 | } |
be58e87e | 298 | |
c6f494e8 | 299 | sr_set_timeout(-1); /* Don't time out -- user program is running. */ |
be58e87e RP |
300 | immediate_quit = 1; /* Helps ability to QUIT */ |
301 | ||
c6f494e8 | 302 | switch (gr_multi_scan(wait_strings, need_artificial_trap == 0)) |
5905161c | 303 | { |
16f6ab6b | 304 | case 0: /* breakpoint case */ |
67ac9759 JK |
305 | status->kind = TARGET_WAITKIND_STOPPED; |
306 | status->value.sig = TARGET_SIGNAL_TRAP; | |
16f6ab6b | 307 | /* user output from the target can be discarded here. (?) */ |
c6f494e8 | 308 | gr_expect_prompt(); |
16f6ab6b RP |
309 | break; |
310 | ||
311 | case 1: /* bus error */ | |
67ac9759 JK |
312 | status->kind = TARGET_WAITKIND_STOPPED; |
313 | status->value.sig = TARGET_SIGNAL_BUS; | |
16f6ab6b | 314 | /* user output from the target can be discarded here. (?) */ |
c6f494e8 | 315 | gr_expect_prompt(); |
16f6ab6b RP |
316 | break; |
317 | ||
318 | case 2: /* normal case */ | |
c6f494e8 | 319 | case 3: |
16f6ab6b RP |
320 | if (need_artificial_trap != 0) |
321 | { | |
322 | /* stepping */ | |
67ac9759 JK |
323 | status->kind = TARGET_WAITKIND_STOPPED; |
324 | status->value.sig = TARGET_SIGNAL_TRAP; | |
16f6ab6b RP |
325 | need_artificial_trap--; |
326 | break; | |
327 | } | |
328 | else | |
329 | { | |
330 | /* exit case */ | |
67ac9759 JK |
331 | status->kind = TARGET_WAITKIND_EXITED; |
332 | status->value.integer = 0; | |
16f6ab6b RP |
333 | break; |
334 | } | |
be58e87e | 335 | |
16f6ab6b RP |
336 | case -1: /* trouble */ |
337 | default: | |
338 | fprintf_filtered (stderr, | |
339 | "Trouble reading target during wait\n"); | |
340 | break; | |
341 | } | |
5905161c | 342 | |
c6f494e8 | 343 | sr_set_timeout(old_timeout); |
5905161c RP |
344 | immediate_quit = old_immediate_quit; |
345 | return 0; | |
346 | } | |
347 | ||
348 | /* Return the name of register number REGNO | |
349 | in the form input and output by bug. | |
350 | ||
351 | Returns a pointer to a static buffer containing the answer. */ | |
352 | static char * | |
353 | get_reg_name (regno) | |
354 | int regno; | |
355 | { | |
356 | static char *rn[] = { | |
357 | "r00", "r01", "r02", "r03", "r04", "r05", "r06", "r07", | |
358 | "r08", "r09", "r10", "r11", "r12", "r13", "r14", "r15", | |
359 | "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23", | |
360 | "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31", | |
361 | ||
362 | /* these get confusing because we omit a few and switch some ordering around. */ | |
363 | ||
364 | "cr01", /* 32 = psr */ | |
365 | "fcr62", /* 33 = fpsr*/ | |
366 | "fcr63", /* 34 = fpcr */ | |
be58e87e RP |
367 | "ip", /* this is something of a cheat. */ |
368 | /* 35 = sxip */ | |
5905161c RP |
369 | "cr05", /* 36 = snip */ |
370 | "cr06", /* 37 = sfip */ | |
c6f494e8 RP |
371 | |
372 | "x00", "x01", "x02", "x03", "x04", "x05", "x06", "x07", | |
373 | "x08", "x09", "x10", "x11", "x12", "x13", "x14", "x15", | |
374 | "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23", | |
375 | "x24", "x25", "x26", "x27", "x28", "x29", "x30", "x31", | |
5905161c RP |
376 | }; |
377 | ||
378 | return rn[regno]; | |
379 | } | |
380 | ||
16f6ab6b | 381 | #if 0 /* not currently used */ |
be58e87e RP |
382 | /* Read from remote while the input matches STRING. Return zero on |
383 | success, -1 on failure. */ | |
384 | ||
385 | static int | |
386 | bug_scan (s) | |
387 | char *s; | |
388 | { | |
389 | int c; | |
390 | ||
391 | while (*s) | |
392 | { | |
c6f494e8 | 393 | c = sr_readchar(); |
be58e87e RP |
394 | if (c != *s++) |
395 | { | |
396 | fflush(stdout); | |
397 | printf("\nNext character is '%c' - %d and s is \"%s\".\n", c, c, --s); | |
398 | return(-1); | |
399 | } | |
400 | } | |
401 | ||
402 | return(0); | |
403 | } | |
16f6ab6b | 404 | #endif /* never */ |
be58e87e RP |
405 | |
406 | static int | |
407 | bug_srec_write_cr (s) | |
408 | char *s; | |
409 | { | |
410 | char *p = s; | |
411 | ||
412 | if (srec_echo_pace) | |
413 | for (p = s; *p; ++p) | |
414 | { | |
c6f494e8 | 415 | if (sr_get_debug() > 0) |
be58e87e RP |
416 | printf ("%c", *p); |
417 | ||
418 | do | |
c6f494e8 RP |
419 | SERIAL_WRITE(sr_get_desc(), p, 1); |
420 | while (sr_pollchar() != *p); | |
be58e87e RP |
421 | } |
422 | else | |
423 | { | |
c6f494e8 | 424 | sr_write_cr (s); |
be58e87e RP |
425 | /* return(bug_scan (s) || bug_scan ("\n")); */ |
426 | } | |
427 | ||
428 | return(0); | |
5905161c RP |
429 | } |
430 | ||
431 | /* Store register REGNO, or all if REGNO == -1. */ | |
432 | ||
433 | static void | |
434 | bug_fetch_register(regno) | |
435 | int regno; | |
436 | { | |
c6f494e8 | 437 | sr_check_open(); |
5905161c RP |
438 | |
439 | if (regno == -1) | |
440 | { | |
441 | int i; | |
442 | ||
443 | for (i = 0; i < NUM_REGS; ++i) | |
444 | bug_fetch_register(i); | |
445 | } | |
817ac7f8 RP |
446 | else if (target_is_m88110 && regno == SFIP_REGNUM) |
447 | { | |
448 | /* m88110 has no sfip. */ | |
449 | long l = 0; | |
450 | supply_register(regno, (char *) &l); | |
451 | } | |
c6f494e8 | 452 | else if (regno < XFP_REGNUM) |
5905161c | 453 | { |
f4f0d174 JK |
454 | char buffer[MAX_REGISTER_RAW_SIZE]; |
455 | ||
456 | sr_write ("rs ", 3); | |
457 | sr_write_cr (get_reg_name(regno)); | |
458 | sr_expect ("="); | |
459 | store_unsigned_integer (buffer, REGISTER_RAW_SIZE (regno), | |
460 | sr_get_hex_word()); | |
461 | gr_expect_prompt (); | |
462 | supply_register (regno, buffer); | |
5905161c | 463 | } |
c6f494e8 RP |
464 | else |
465 | { | |
466 | /* Float register so we need to parse a strange data format. */ | |
467 | long p; | |
9c41f6a6 | 468 | unsigned char fpreg_buf[10]; |
c6f494e8 RP |
469 | |
470 | sr_write("rs ", 3); | |
471 | sr_write(get_reg_name(regno), strlen(get_reg_name(regno))); | |
472 | sr_write_cr(";d"); | |
473 | sr_expect("rs"); | |
474 | sr_expect(get_reg_name(regno)); | |
475 | sr_expect(";d"); | |
476 | sr_expect("="); | |
477 | ||
478 | /* sign */ | |
479 | p = sr_get_hex_digit(1); | |
9c41f6a6 | 480 | fpreg_buf[0] = p << 7; |
c6f494e8 RP |
481 | |
482 | /* exponent */ | |
483 | sr_expect("_"); | |
484 | p = sr_get_hex_digit(1); | |
9c41f6a6 JK |
485 | fpreg_buf[0] += (p << 4); |
486 | fpreg_buf[0] += sr_get_hex_digit(1); | |
c6f494e8 | 487 | |
9c41f6a6 | 488 | fpreg_buf[1] = sr_get_hex_digit(1) << 4; |
c6f494e8 RP |
489 | |
490 | /* fraction */ | |
491 | sr_expect("_"); | |
9c41f6a6 | 492 | fpreg_buf[1] += sr_get_hex_digit(1); |
c6f494e8 | 493 | |
9c41f6a6 JK |
494 | fpreg_buf[2] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1); |
495 | fpreg_buf[3] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1); | |
496 | fpreg_buf[4] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1); | |
497 | fpreg_buf[5] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1); | |
498 | fpreg_buf[6] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1); | |
499 | fpreg_buf[7] = (sr_get_hex_digit(1) << 4) + sr_get_hex_digit(1); | |
500 | fpreg_buf[8] = 0; | |
501 | fpreg_buf[9] = 0; | |
c6f494e8 RP |
502 | |
503 | gr_expect_prompt(); | |
9c41f6a6 | 504 | supply_register(regno, fpreg_buf); |
c6f494e8 | 505 | } |
5905161c RP |
506 | |
507 | return; | |
508 | } | |
509 | ||
510 | /* Store register REGNO, or all if REGNO == -1. */ | |
511 | ||
512 | static void | |
513 | bug_store_register (regno) | |
514 | int regno; | |
515 | { | |
5905161c | 516 | char buffer[1024]; |
c6f494e8 | 517 | sr_check_open(); |
5905161c RP |
518 | |
519 | if (regno == -1) | |
520 | { | |
521 | int i; | |
522 | ||
523 | for (i = 0; i < NUM_REGS; ++i) | |
524 | bug_store_register(i); | |
525 | } | |
526 | else | |
527 | { | |
528 | char *regname; | |
529 | ||
c6f494e8 | 530 | regname = get_reg_name(regno); |
5905161c | 531 | |
817ac7f8 RP |
532 | if (target_is_m88110 && regno == SFIP_REGNUM) |
533 | return; | |
534 | else if (regno < XFP_REGNUM) | |
c6f494e8 RP |
535 | sprintf(buffer, "rs %s %08x", |
536 | regname, | |
537 | read_register(regno)); | |
538 | else | |
539 | { | |
9c41f6a6 JK |
540 | unsigned char *fpreg_buf = |
541 | (unsigned char *)®isters[REGISTER_BYTE(regno)]; | |
c6f494e8 | 542 | |
817ac7f8 | 543 | sprintf(buffer, "rs %s %1x_%02x%1x_%1x%02x%02x%02x%02x%02x%02x;d", |
c6f494e8 RP |
544 | regname, |
545 | /* sign */ | |
9c41f6a6 | 546 | (fpreg_buf[0] >> 7) & 0xf, |
c6f494e8 | 547 | /* exponent */ |
9c41f6a6 JK |
548 | fpreg_buf[0] & 0x7f, |
549 | (fpreg_buf[1] >> 8) & 0xf, | |
c6f494e8 | 550 | /* fraction */ |
9c41f6a6 JK |
551 | fpreg_buf[1] & 0xf, |
552 | fpreg_buf[2], | |
553 | fpreg_buf[3], | |
554 | fpreg_buf[4], | |
555 | fpreg_buf[5], | |
556 | fpreg_buf[6], | |
557 | fpreg_buf[7]); | |
c6f494e8 | 558 | } |
5905161c | 559 | |
c6f494e8 RP |
560 | sr_write_cr(buffer); |
561 | gr_expect_prompt(); | |
5905161c RP |
562 | } |
563 | ||
564 | return; | |
565 | } | |
566 | ||
5905161c | 567 | int |
c6f494e8 | 568 | bug_xfer_memory (memaddr, myaddr, len, write, target) |
5905161c RP |
569 | CORE_ADDR memaddr; |
570 | char *myaddr; | |
571 | int len; | |
572 | int write; | |
573 | struct target_ops *target; /* ignored */ | |
574 | { | |
575 | register int i; | |
576 | ||
577 | /* Round starting address down to longword boundary. */ | |
578 | register CORE_ADDR addr; | |
579 | ||
580 | /* Round ending address up; get number of longwords that makes. */ | |
581 | register int count; | |
582 | ||
583 | /* Allocate buffer of that many longwords. */ | |
584 | register int *buffer; | |
585 | ||
586 | addr = memaddr & -sizeof (int); | |
587 | count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int); | |
588 | ||
589 | buffer = (int *) alloca (count * sizeof (int)); | |
590 | ||
591 | if (write) | |
592 | { | |
593 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
594 | ||
595 | if (addr != memaddr || len < (int) sizeof (int)) | |
596 | { | |
597 | /* Need part of initial word -- fetch it. */ | |
c6f494e8 | 598 | buffer[0] = gr_fetch_word (addr); |
5905161c RP |
599 | } |
600 | ||
601 | if (count > 1) /* FIXME, avoid if even boundary */ | |
602 | { | |
603 | buffer[count - 1] | |
c6f494e8 | 604 | = gr_fetch_word (addr + (count - 1) * sizeof (int)); |
5905161c RP |
605 | } |
606 | ||
607 | /* Copy data to be written over corresponding part of buffer */ | |
608 | ||
ade40d31 | 609 | memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len); |
5905161c RP |
610 | |
611 | /* Write the entire buffer. */ | |
612 | ||
613 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
614 | { | |
615 | errno = 0; | |
c6f494e8 | 616 | gr_store_word (addr, buffer[i]); |
5905161c RP |
617 | if (errno) |
618 | { | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | } | |
624 | } | |
625 | else | |
626 | { | |
627 | /* Read all the longwords */ | |
628 | for (i = 0; i < count; i++, addr += sizeof (int)) | |
629 | { | |
630 | errno = 0; | |
c6f494e8 | 631 | buffer[i] = gr_fetch_word (addr); |
5905161c RP |
632 | if (errno) |
633 | { | |
634 | return 0; | |
635 | } | |
636 | QUIT; | |
637 | } | |
638 | ||
639 | /* Copy appropriate bytes out of the buffer. */ | |
ade40d31 | 640 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len); |
5905161c RP |
641 | } |
642 | ||
643 | return len; | |
644 | } | |
645 | ||
be58e87e RP |
646 | static void |
647 | start_load() | |
648 | { | |
649 | char *command; | |
650 | ||
651 | command = (srec_echo_pace ? "lo 0 ;x" : "lo 0"); | |
652 | ||
c6f494e8 RP |
653 | sr_write_cr (command); |
654 | sr_expect (command); | |
655 | sr_expect ("\r\n"); | |
be58e87e RP |
656 | bug_srec_write_cr ("S0030000FC"); |
657 | return; | |
658 | } | |
659 | ||
660 | /* This is an extremely vulnerable and fragile function. I've made | |
661 | considerable attempts to make this deterministic, but I've | |
662 | certainly forgotten something. The trouble is that S-records are | |
663 | only a partial file format, not a protocol. Worse, apparently the | |
664 | m88k bug monitor does not run in real time while receiving | |
665 | S-records. Hence, we must pay excruciating attention to when and | |
666 | where error messages are returned, and what has actually been sent. | |
5905161c | 667 | |
be58e87e RP |
668 | Each call represents a chunk of memory to be sent to the target. |
669 | We break that chunk into an S0 header record, some number of S3 | |
670 | data records each containing srec_bytes, and an S7 termination | |
671 | record. */ | |
672 | ||
16f6ab6b RP |
673 | static char *srecord_strings[] = { |
674 | "S-RECORD", | |
c6f494e8 | 675 | "-Bug>", |
16f6ab6b RP |
676 | NULL, |
677 | }; | |
678 | ||
be58e87e | 679 | static int |
c6f494e8 | 680 | bug_write_memory (memaddr, myaddr, len) |
5905161c RP |
681 | CORE_ADDR memaddr; |
682 | unsigned char *myaddr; | |
683 | int len; | |
684 | { | |
685 | int done; | |
5905161c | 686 | int checksum; |
be58e87e RP |
687 | int x; |
688 | int retries; | |
9c41f6a6 | 689 | char *buffer = alloca ((srec_bytes + 8) << 1); |
5905161c | 690 | |
be58e87e | 691 | retries = 0; |
5905161c | 692 | |
be58e87e | 693 | do |
5905161c | 694 | { |
be58e87e RP |
695 | done = 0; |
696 | ||
697 | if (retries > srec_max_retries) | |
698 | return(-1); | |
699 | ||
700 | if (retries > 0) | |
5905161c | 701 | { |
c6f494e8 | 702 | if (sr_get_debug() > 0) |
be58e87e RP |
703 | printf("\n<retrying...>\n"); |
704 | ||
c6f494e8 | 705 | /* This gr_expect_prompt call is extremely important. Without |
be58e87e RP |
706 | it, we will tend to resend our packet so fast that it |
707 | will arrive before the bug monitor is ready to receive | |
708 | it. This would lead to a very ugly resend loop. */ | |
709 | ||
c6f494e8 | 710 | gr_expect_prompt(); |
5905161c | 711 | } |
5905161c | 712 | |
be58e87e RP |
713 | start_load(); |
714 | ||
715 | while (done < len) | |
716 | { | |
717 | int thisgo; | |
718 | int idx; | |
719 | char *buf = buffer; | |
720 | CORE_ADDR address; | |
721 | ||
722 | checksum = 0; | |
723 | thisgo = len - done; | |
724 | if (thisgo > srec_bytes) | |
725 | thisgo = srec_bytes; | |
726 | ||
727 | address = memaddr + done; | |
728 | sprintf (buf, "S3%02X%08X", thisgo + 4 + 1, address); | |
729 | buf += 12; | |
730 | ||
731 | checksum += (thisgo + 4 + 1 | |
732 | + (address & 0xff) | |
733 | + ((address >> 8) & 0xff) | |
734 | + ((address >> 16) & 0xff) | |
735 | + ((address >> 24) & 0xff)); | |
736 | ||
737 | for (idx = 0; idx < thisgo; idx++) | |
738 | { | |
739 | sprintf (buf, "%02X", myaddr[idx + done]); | |
740 | checksum += myaddr[idx + done]; | |
741 | buf += 2; | |
742 | } | |
743 | ||
744 | if (srec_noise > 0) | |
745 | { | |
746 | /* FIXME-NOW: insert a deliberate error every now and then. | |
747 | This is intended for testing/debugging the error handling | |
748 | stuff. */ | |
749 | static int counter = 0; | |
750 | if (++counter > srec_noise) | |
751 | { | |
752 | counter = 0; | |
753 | ++checksum; | |
754 | } | |
755 | } | |
756 | ||
757 | sprintf(buf, "%02X", ~checksum & 0xff); | |
758 | bug_srec_write_cr (buffer); | |
759 | ||
760 | if (srec_sleep != 0) | |
761 | sleep(srec_sleep); | |
762 | ||
c6f494e8 | 763 | /* This pollchar is probably redundant to the gr_multi_scan |
be58e87e RP |
764 | below. Trouble is, we can't be sure when or where an |
765 | error message will appear. Apparently, when running at | |
766 | full speed from a typical sun4, error messages tend to | |
767 | appear to arrive only *after* the s7 record. */ | |
768 | ||
c6f494e8 | 769 | if ((x = sr_pollchar()) != 0) |
be58e87e | 770 | { |
c6f494e8 | 771 | if (sr_get_debug() > 0) |
be58e87e RP |
772 | printf("\n<retrying...>\n"); |
773 | ||
774 | ++retries; | |
775 | ||
776 | /* flush any remaining input and verify that we are back | |
777 | at the prompt level. */ | |
c6f494e8 | 778 | gr_expect_prompt(); |
be58e87e RP |
779 | /* start all over again. */ |
780 | start_load(); | |
781 | done = 0; | |
782 | continue; | |
783 | } | |
784 | ||
785 | done += thisgo; | |
786 | } | |
787 | ||
788 | bug_srec_write_cr("S7060000000000F9"); | |
789 | ++retries; | |
790 | ||
791 | /* Having finished the load, we need to figure out whether we | |
792 | had any errors. */ | |
c6f494e8 | 793 | } while (gr_multi_scan(srecord_strings, 0) == 0);; |
be58e87e RP |
794 | |
795 | return(0); | |
5905161c RP |
796 | } |
797 | ||
5905161c RP |
798 | /* Copy LEN bytes of data from debugger memory at MYADDR |
799 | to inferior's memory at MEMADDR. Returns errno value. | |
800 | * sb/sh instructions don't work on unaligned addresses, when TU=1. | |
801 | */ | |
802 | ||
803 | /* Read LEN bytes from inferior memory at MEMADDR. Put the result | |
804 | at debugger address MYADDR. Returns errno value. */ | |
be58e87e | 805 | static int |
c6f494e8 | 806 | bug_read_memory (memaddr, myaddr, len) |
5905161c | 807 | CORE_ADDR memaddr; |
755892d6 | 808 | unsigned char *myaddr; |
5905161c RP |
809 | int len; |
810 | { | |
811 | char request[100]; | |
812 | char *buffer; | |
813 | char *p; | |
814 | char type; | |
815 | char size; | |
816 | unsigned char c; | |
817 | unsigned int inaddr; | |
818 | unsigned int checksum; | |
819 | ||
820 | sprintf(request, "du 0 %x:&%d", memaddr, len); | |
c6f494e8 | 821 | sr_write_cr(request); |
5905161c RP |
822 | |
823 | p = buffer = alloca(len); | |
824 | ||
825 | /* scan up through the header */ | |
c6f494e8 | 826 | sr_expect("S0030000FC"); |
5905161c RP |
827 | |
828 | while (p < buffer + len) | |
829 | { | |
830 | /* scan off any white space. */ | |
c6f494e8 | 831 | while (sr_readchar() != 'S') ;; |
5905161c RP |
832 | |
833 | /* what kind of s-rec? */ | |
c6f494e8 | 834 | type = sr_readchar(); |
5905161c RP |
835 | |
836 | /* scan record size */ | |
c6f494e8 | 837 | sr_get_hex_byte(&size); |
5905161c RP |
838 | checksum = size; |
839 | --size; | |
840 | inaddr = 0; | |
841 | ||
842 | switch (type) | |
843 | { | |
844 | case '7': | |
845 | case '8': | |
846 | case '9': | |
847 | goto done; | |
848 | ||
849 | case '3': | |
c6f494e8 | 850 | sr_get_hex_byte(&c); |
5905161c RP |
851 | inaddr = (inaddr << 8) + c; |
852 | checksum += c; | |
853 | --size; | |
854 | /* intentional fall through */ | |
855 | case '2': | |
c6f494e8 | 856 | sr_get_hex_byte(&c); |
5905161c RP |
857 | inaddr = (inaddr << 8) + c; |
858 | checksum += c; | |
859 | --size; | |
860 | /* intentional fall through */ | |
861 | case '1': | |
c6f494e8 | 862 | sr_get_hex_byte(&c); |
5905161c RP |
863 | inaddr = (inaddr << 8) + c; |
864 | checksum += c; | |
865 | --size; | |
c6f494e8 | 866 | sr_get_hex_byte(&c); |
5905161c RP |
867 | inaddr = (inaddr << 8) + c; |
868 | checksum += c; | |
869 | --size; | |
870 | break; | |
871 | ||
872 | default: | |
873 | /* bonk */ | |
874 | error("reading s-records."); | |
875 | } | |
876 | ||
877 | if (inaddr < memaddr | |
878 | || (memaddr + len) < (inaddr + size)) | |
879 | error("srec out of memory range."); | |
880 | ||
881 | if (p != buffer + inaddr - memaddr) | |
882 | error("srec out of sequence."); | |
883 | ||
884 | for (; size; --size, ++p) | |
885 | { | |
c6f494e8 | 886 | sr_get_hex_byte(p); |
5905161c RP |
887 | checksum += *p; |
888 | } | |
889 | ||
c6f494e8 | 890 | sr_get_hex_byte(&c); |
5905161c RP |
891 | if (c != (~checksum & 0xff)) |
892 | error("bad s-rec checksum"); | |
893 | } | |
894 | ||
895 | done: | |
c6f494e8 | 896 | gr_expect_prompt(); |
5905161c RP |
897 | if (p != buffer + len) |
898 | return(1); | |
899 | ||
900 | memcpy(myaddr, buffer, len); | |
901 | return(0); | |
902 | } | |
903 | ||
5905161c RP |
904 | #define MAX_BREAKS 16 |
905 | static int num_brkpts = 0; | |
906 | static int | |
907 | bug_insert_breakpoint (addr, save) | |
908 | CORE_ADDR addr; | |
909 | char *save; /* Throw away, let bug save instructions */ | |
910 | { | |
c6f494e8 | 911 | sr_check_open (); |
5905161c RP |
912 | |
913 | if (num_brkpts < MAX_BREAKS) | |
914 | { | |
915 | char buffer[100]; | |
916 | ||
917 | num_brkpts++; | |
918 | sprintf (buffer, "br %x", addr); | |
c6f494e8 RP |
919 | sr_write_cr (buffer); |
920 | gr_expect_prompt (); | |
5905161c RP |
921 | return(0); |
922 | } | |
923 | else | |
924 | { | |
925 | fprintf_filtered (stderr, | |
926 | "Too many break points, break point not installed\n"); | |
927 | return(1); | |
928 | } | |
929 | ||
930 | } | |
931 | static int | |
932 | bug_remove_breakpoint (addr, save) | |
933 | CORE_ADDR addr; | |
934 | char *save; /* Throw away, let bug save instructions */ | |
935 | { | |
936 | if (num_brkpts > 0) | |
937 | { | |
938 | char buffer[100]; | |
939 | ||
940 | num_brkpts--; | |
941 | sprintf (buffer, "nobr %x", addr); | |
c6f494e8 RP |
942 | sr_write_cr (buffer); |
943 | gr_expect_prompt (); | |
5905161c RP |
944 | |
945 | } | |
946 | return (0); | |
947 | } | |
948 | ||
949 | /* Clear the bugs notion of what the break points are */ | |
950 | static int | |
951 | bug_clear_breakpoints () | |
952 | { | |
953 | ||
c6f494e8 | 954 | if (sr_is_open()) |
5905161c | 955 | { |
c6f494e8 RP |
956 | sr_write_cr ("nobr"); |
957 | sr_expect("nobr"); | |
958 | gr_expect_prompt (); | |
5905161c RP |
959 | } |
960 | num_brkpts = 0; | |
be58e87e | 961 | return(0); |
5905161c | 962 | } |
be58e87e | 963 | |
c719b714 JM |
964 | struct target_ops bug_ops ; |
965 | ||
966 | static void init_bug_ops(void) | |
5905161c | 967 | { |
c719b714 JM |
968 | bug_ops.to_shortname = "bug"; "Remote BUG monitor", |
969 | bug_ops.to_longname = "Use the mvme187 board running the BUG monitor connected by a serial line."; | |
970 | bug_ops.to_doc = " "; | |
971 | bug_ops.to_open = bug_open; | |
972 | bug_ops.to_close = gr_close; | |
973 | bug_ops.to_attach = 0; | |
974 | bug_ops.to_detach = gr_detach; | |
975 | bug_ops.to_resume = bug_resume; | |
976 | bug_ops.to_wait = bug_wait; | |
977 | bug_ops.to_fetch_registers = bug_fetch_register; | |
978 | bug_ops.to_store_registers = bug_store_register; | |
979 | bug_ops.to_prepare_to_store = gr_prepare_to_store; | |
980 | bug_ops.to_xfer_memory = bug_xfer_memory; | |
981 | bug_ops.to_files_info = gr_files_info; | |
982 | bug_ops.to_insert_breakpoint = bug_insert_breakpoint; | |
983 | bug_ops.to_remove_breakpoint = bug_remove_breakpoint; | |
984 | bug_ops.to_terminal_init = 0; | |
985 | bug_ops.to_terminal_inferior = 0; | |
986 | bug_ops.to_terminal_ours_for_output = 0; | |
987 | bug_ops.to_terminal_ours = 0; | |
988 | bug_ops.to_terminal_info = 0; | |
989 | bug_ops.to_kill = gr_kill; | |
990 | bug_ops.to_load = bug_load; | |
991 | bug_ops.to_lookup_symbol = 0; | |
992 | bug_ops.to_create_inferior = gr_create_inferior; | |
993 | bug_ops.to_mourn_inferior = gr_mourn; | |
994 | bug_ops.to_can_run = 0; | |
995 | bug_ops.to_notice_signals = 0; | |
996 | bug_ops.to_thread_alive = 0 ; | |
997 | bug_ops.to_stop = 0; | |
998 | bug_ops.to_stratum = process_stratum ; | |
999 | bug_ops.DONT_USE = 0; | |
1000 | bug_ops.to_has_all_memory = 1; | |
1001 | bug_ops.to_has_memory = 1; | |
1002 | bug_ops.to_has_stack = 1; | |
1003 | bug_ops.to_has_registers = 0; | |
1004 | bug_ops.to_has_execution = 0; | |
1005 | bug_ops.to_sections = 0 ; | |
1006 | bug_ops.to_sections_end = 0 ; | |
1007 | bug_ops.to_magic = OPS_MAGIC; /* Always the last thing */ | |
1008 | } /* init_bug_ops */ | |
5905161c RP |
1009 | |
1010 | void | |
1011 | _initialize_remote_bug () | |
1012 | { | |
c719b714 | 1013 | init_bug_ops() ; |
5905161c RP |
1014 | add_target (&bug_ops); |
1015 | ||
be58e87e RP |
1016 | add_show_from_set |
1017 | (add_set_cmd ("srec-bytes", class_support, var_uinteger, | |
1018 | (char *) &srec_bytes, | |
1019 | "\ | |
1020 | Set the number of bytes represented in each S-record.\n\ | |
1021 | This affects the communication protocol with the remote target.", | |
1022 | &setlist), | |
1023 | &showlist); | |
1024 | ||
1025 | add_show_from_set | |
1026 | (add_set_cmd ("srec-max-retries", class_support, var_uinteger, | |
1027 | (char *) &srec_max_retries, | |
1028 | "\ | |
1029 | Set the number of retries for shipping S-records.\n\ | |
1030 | This affects the communication protocol with the remote target.", | |
1031 | &setlist), | |
1032 | &showlist); | |
1033 | ||
e3a7e9d5 JK |
1034 | #if 0 |
1035 | /* This needs to set SREC_SIZE, not srec_frame which gets changed at the | |
1036 | end of a download. But do we need the option at all? */ | |
be58e87e RP |
1037 | add_show_from_set |
1038 | (add_set_cmd ("srec-frame", class_support, var_uinteger, | |
1039 | (char *) &srec_frame, | |
1040 | "\ | |
1041 | Set the number of bytes in an S-record frame.\n\ | |
1042 | This affects the communication protocol with the remote target.", | |
1043 | &setlist), | |
1044 | &showlist); | |
e3a7e9d5 | 1045 | #endif /* 0 */ |
be58e87e RP |
1046 | |
1047 | add_show_from_set | |
1048 | (add_set_cmd ("srec-noise", class_support, var_zinteger, | |
1049 | (char *) &srec_noise, | |
1050 | "\ | |
1051 | Set number of S-record to send before deliberately flubbing a checksum.\n\ | |
1052 | Zero means flub none at all. This affects the communication protocol\n\ | |
1053 | with the remote target.", | |
1054 | &setlist), | |
1055 | &showlist); | |
1056 | ||
1057 | add_show_from_set | |
1058 | (add_set_cmd ("srec-sleep", class_support, var_zinteger, | |
1059 | (char *) &srec_sleep, | |
1060 | "\ | |
1061 | Set number of seconds to sleep after an S-record for a possible error message to arrive.\n\ | |
1062 | This affects the communication protocol with the remote target.", | |
1063 | &setlist), | |
1064 | &showlist); | |
1065 | ||
1066 | add_show_from_set | |
1067 | (add_set_cmd ("srec-echo-pace", class_support, var_boolean, | |
1068 | (char *) &srec_echo_pace, | |
1069 | "\ | |
1070 | Set echo-verification.\n\ | |
1071 | When on, use verification by echo when downloading S-records. This is\n\ | |
1072 | much slower, but generally more reliable.", | |
1073 | &setlist), | |
1074 | &showlist); | |
5905161c | 1075 | } |