Commit | Line | Data |
---|---|---|
dd3b648e | 1 | /* Remote debugging interface for AMD 29000 EBMON on IBM PC, for GDB. |
e17960fb | 2 | Copyright 1990, 1991, 1992 Free Software Foundation, Inc. |
dd3b648e RP |
3 | Contributed by Cygnus Support. Written by Jim Kingdon for Cygnus. |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
99a7de40 JG |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
dd3b648e RP |
11 | |
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
99a7de40 JG |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
dd3b648e RP |
20 | |
21 | /* This is like remote.c but is for an esoteric situation-- | |
d7d35f00 | 22 | having a a29k board in a PC hooked up to a unix machine with |
dd3b648e RP |
23 | a serial line, and running ctty com1 on the PC, through which |
24 | the unix machine can run ebmon. Not to mention that the PC | |
25 | has PC/NFS, so it can access the same executables that gdb can, | |
26 | over the net in real time. */ | |
27 | ||
7d9884b9 | 28 | #define TM_FILE_OVERRIDE |
dd3b648e | 29 | #include "defs.h" |
d747e0af | 30 | #include <string.h> |
d7d35f00 | 31 | #include "a29k/tm-a29k.h" |
76b2c3c8 | 32 | |
dd3b648e | 33 | #include "inferior.h" |
6b27ebe8 | 34 | #include "symfile.h" |
dd3b648e RP |
35 | #include "wait.h" |
36 | #include "value.h" | |
37 | #include <ctype.h> | |
38 | #include <fcntl.h> | |
39 | #include <signal.h> | |
40 | #include <errno.h> | |
41 | #include "terminal.h" | |
42 | #include "target.h" | |
76b2c3c8 | 43 | #include "gdbcore.h" |
dd3b648e | 44 | |
dd3b648e RP |
45 | extern struct target_ops eb_ops; /* Forward declaration */ |
46 | ||
19b66c0e JG |
47 | static void eb_close(); |
48 | ||
dd3b648e RP |
49 | #define LOG_FILE "eb.log" |
50 | #if defined (LOG_FILE) | |
51 | FILE *log_file; | |
52 | #endif | |
53 | ||
76b2c3c8 | 54 | static int timeout = 24; |
dd3b648e RP |
55 | |
56 | /* Descriptor for I/O to remote machine. Initialize it to -1 so that | |
57 | eb_open knows that we don't have a file open when the program | |
58 | starts. */ | |
59 | int eb_desc = -1; | |
60 | ||
61 | /* stream which is fdopen'd from eb_desc. Only valid when | |
62 | eb_desc != -1. */ | |
63 | FILE *eb_stream; | |
64 | ||
65 | /* Read a character from the remote system, doing all the fancy | |
66 | timeout stuff. */ | |
67 | static int | |
68 | readchar () | |
69 | { | |
70 | char buf; | |
71 | ||
72 | buf = '\0'; | |
73 | #ifdef HAVE_TERMIO | |
74 | /* termio does the timeout for us. */ | |
75 | read (eb_desc, &buf, 1); | |
76 | #else | |
77 | alarm (timeout); | |
78 | if (read (eb_desc, &buf, 1) < 0) | |
79 | { | |
80 | if (errno == EINTR) | |
81 | error ("Timeout reading from remote system."); | |
82 | else | |
83 | perror_with_name ("remote"); | |
84 | } | |
85 | alarm (0); | |
86 | #endif | |
87 | ||
88 | if (buf == '\0') | |
89 | error ("Timeout reading from remote system."); | |
90 | #if defined (LOG_FILE) | |
91 | putc (buf & 0x7f, log_file); | |
92 | #endif | |
93 | return buf & 0x7f; | |
94 | } | |
95 | ||
96 | /* Keep discarding input from the remote system, until STRING is found. | |
97 | Let the user break out immediately. */ | |
98 | static void | |
99 | expect (string) | |
100 | char *string; | |
101 | { | |
102 | char *p = string; | |
103 | ||
104 | immediate_quit = 1; | |
105 | while (1) | |
106 | { | |
107 | if (readchar() == *p) | |
108 | { | |
109 | p++; | |
110 | if (*p == '\0') | |
111 | { | |
112 | immediate_quit = 0; | |
113 | return; | |
114 | } | |
115 | } | |
116 | else | |
117 | p = string; | |
118 | } | |
119 | } | |
120 | ||
121 | /* Keep discarding input until we see the ebmon prompt. | |
122 | ||
123 | The convention for dealing with the prompt is that you | |
124 | o give your command | |
125 | o *then* wait for the prompt. | |
126 | ||
127 | Thus the last thing that a procedure does with the serial line | |
128 | will be an expect_prompt(). Exception: eb_resume does not | |
129 | wait for the prompt, because the terminal is being handed over | |
130 | to the inferior. However, the next thing which happens after that | |
131 | is a eb_wait which does wait for the prompt. | |
132 | Note that this includes abnormal exit, e.g. error(). This is | |
133 | necessary to prevent getting into states from which we can't | |
134 | recover. */ | |
135 | static void | |
136 | expect_prompt () | |
137 | { | |
138 | #if defined (LOG_FILE) | |
139 | /* This is a convenient place to do this. The idea is to do it often | |
140 | enough that we never lose much data if we terminate abnormally. */ | |
141 | fflush (log_file); | |
142 | #endif | |
143 | expect ("\n# "); | |
144 | } | |
145 | ||
146 | /* Get a hex digit from the remote system & return its value. | |
147 | If ignore_space is nonzero, ignore spaces (not newline, tab, etc). */ | |
148 | static int | |
149 | get_hex_digit (ignore_space) | |
150 | int ignore_space; | |
151 | { | |
152 | int ch; | |
153 | while (1) | |
154 | { | |
155 | ch = readchar (); | |
156 | if (ch >= '0' && ch <= '9') | |
157 | return ch - '0'; | |
158 | else if (ch >= 'A' && ch <= 'F') | |
159 | return ch - 'A' + 10; | |
160 | else if (ch >= 'a' && ch <= 'f') | |
161 | return ch - 'a' + 10; | |
162 | else if (ch == ' ' && ignore_space) | |
163 | ; | |
164 | else | |
165 | { | |
166 | expect_prompt (); | |
167 | error ("Invalid hex digit from remote system."); | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | /* Get a byte from eb_desc and put it in *BYT. Accept any number | |
173 | leading spaces. */ | |
174 | static void | |
175 | get_hex_byte (byt) | |
176 | char *byt; | |
177 | { | |
178 | int val; | |
179 | ||
180 | val = get_hex_digit (1) << 4; | |
181 | val |= get_hex_digit (0); | |
182 | *byt = val; | |
183 | } | |
184 | ||
185 | /* Get N 32-bit words from remote, each preceded by a space, | |
186 | and put them in registers starting at REGNO. */ | |
187 | static void | |
188 | get_hex_regs (n, regno) | |
189 | int n; | |
190 | int regno; | |
191 | { | |
192 | long val; | |
193 | int i; | |
194 | ||
195 | for (i = 0; i < n; i++) | |
196 | { | |
197 | int j; | |
198 | ||
199 | val = 0; | |
200 | for (j = 0; j < 8; j++) | |
201 | val = (val << 4) + get_hex_digit (j == 0); | |
202 | supply_register (regno++, &val); | |
203 | } | |
204 | } | |
205 | ||
206 | /* Called when SIGALRM signal sent due to alarm() timeout. */ | |
207 | #ifndef HAVE_TERMIO | |
208 | ||
209 | #ifndef __STDC__ | |
210 | #define volatile /**/ | |
211 | #endif | |
212 | volatile int n_alarms; | |
213 | ||
214 | void | |
215 | eb_timer () | |
216 | { | |
217 | #if 0 | |
218 | if (kiodebug) | |
219 | printf ("eb_timer called\n"); | |
220 | #endif | |
221 | n_alarms++; | |
222 | } | |
223 | #endif | |
224 | ||
225 | /* malloc'd name of the program on the remote system. */ | |
226 | static char *prog_name = NULL; | |
227 | ||
228 | /* Nonzero if we have loaded the file ("yc") and not yet issued a "gi" | |
229 | command. "gi" is supposed to happen exactly once for each "yc". */ | |
230 | static int need_gi = 0; | |
231 | ||
232 | /* Number of SIGTRAPs we need to simulate. That is, the next | |
233 | NEED_ARTIFICIAL_TRAP calls to eb_wait should just return | |
234 | SIGTRAP without actually waiting for anything. */ | |
235 | ||
236 | static int need_artificial_trap = 0; | |
237 | ||
238 | /* This is called not only when we first attach, but also when the | |
239 | user types "run" after having attached. */ | |
76b2c3c8 JG |
240 | static void |
241 | eb_create_inferior (execfile, args, env) | |
242 | char *execfile; | |
243 | char *args; | |
244 | char **env; | |
dd3b648e | 245 | { |
76b2c3c8 | 246 | int entry_pt; |
dd3b648e | 247 | |
76b2c3c8 JG |
248 | if (args && *args) |
249 | error ("Can't pass arguments to remote EBMON process"); | |
dd3b648e | 250 | |
76b2c3c8 JG |
251 | if (execfile == 0 || exec_bfd == 0) |
252 | error ("No exec file specified"); | |
dd3b648e | 253 | |
76b2c3c8 | 254 | entry_pt = (int) bfd_get_start_address (exec_bfd); |
dd3b648e | 255 | |
76b2c3c8 JG |
256 | #ifdef CREATE_INFERIOR_HOOK |
257 | CREATE_INFERIOR_HOOK (0); /* No process-ID */ | |
258 | #endif | |
259 | ||
260 | { | |
261 | /* OK, now read in the file. Y=read, C=COFF, D=no symbols | |
262 | 0=start address, %s=filename. */ | |
263 | ||
264 | fprintf (eb_stream, "YC D,0:%s", prog_name); | |
265 | ||
266 | if (args != NULL) | |
267 | fprintf(eb_stream, " %s", args); | |
268 | ||
269 | fprintf (eb_stream, "\n"); | |
270 | fflush (eb_stream); | |
271 | ||
272 | expect_prompt (); | |
273 | ||
274 | need_gi = 1; | |
275 | } | |
276 | ||
277 | /* The "process" (board) is already stopped awaiting our commands, and | |
278 | the program is already downloaded. We just set its PC and go. */ | |
279 | ||
280 | clear_proceed_status (); | |
281 | ||
282 | /* Tell wait_for_inferior that we've started a new process. */ | |
283 | init_wait_for_inferior (); | |
284 | ||
285 | /* Set up the "saved terminal modes" of the inferior | |
286 | based on what modes we are starting it with. */ | |
287 | target_terminal_init (); | |
dd3b648e | 288 | |
76b2c3c8 JG |
289 | /* Install inferior's terminal modes. */ |
290 | target_terminal_inferior (); | |
291 | ||
292 | /* insert_step_breakpoint (); FIXME, do we need this? */ | |
293 | proceed ((CORE_ADDR)entry_pt, -1, 0); /* Let 'er rip... */ | |
dd3b648e RP |
294 | } |
295 | ||
296 | /* Translate baud rates from integers to damn B_codes. Unix should | |
297 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
298 | ||
299 | #ifndef B19200 | |
300 | #define B19200 EXTA | |
301 | #endif | |
302 | #ifndef B38400 | |
303 | #define B38400 EXTB | |
304 | #endif | |
305 | ||
306 | struct {int rate, damn_b;} baudtab[] = { | |
307 | {0, B0}, | |
308 | {50, B50}, | |
309 | {75, B75}, | |
310 | {110, B110}, | |
311 | {134, B134}, | |
312 | {150, B150}, | |
313 | {200, B200}, | |
314 | {300, B300}, | |
315 | {600, B600}, | |
316 | {1200, B1200}, | |
317 | {1800, B1800}, | |
318 | {2400, B2400}, | |
319 | {4800, B4800}, | |
320 | {9600, B9600}, | |
321 | {19200, B19200}, | |
322 | {38400, B38400}, | |
323 | {-1, -1}, | |
324 | }; | |
325 | ||
326 | int damn_b (rate) | |
327 | int rate; | |
328 | { | |
329 | int i; | |
330 | ||
331 | for (i = 0; baudtab[i].rate != -1; i++) | |
332 | if (rate == baudtab[i].rate) return baudtab[i].damn_b; | |
333 | return B38400; /* Random */ | |
334 | } | |
335 | ||
336 | ||
337 | /* Open a connection to a remote debugger. | |
338 | NAME is the filename used for communication, then a space, | |
339 | then the name of the program as we should name it to EBMON. */ | |
340 | ||
341 | static int baudrate = 9600; | |
342 | static char *dev_name; | |
343 | void | |
344 | eb_open (name, from_tty) | |
345 | char *name; | |
346 | int from_tty; | |
347 | { | |
348 | TERMINAL sg; | |
349 | ||
350 | char *p; | |
351 | ||
f2fc6e7a JK |
352 | target_preopen (from_tty); |
353 | ||
dd3b648e RP |
354 | /* Find the first whitespace character, it separates dev_name from |
355 | prog_name. */ | |
356 | if (name == 0) | |
357 | goto erroid; | |
358 | ||
359 | for (p = name; | |
360 | *p != '\0' && !isspace (*p); p++) | |
361 | ; | |
362 | if (*p == '\0') | |
363 | erroid: | |
364 | error ("\ | |
365 | Please include the name of the device for the serial port,\n\ | |
366 | the baud rate, and the name of the program to run on the remote system."); | |
367 | dev_name = alloca (p - name + 1); | |
368 | strncpy (dev_name, name, p - name); | |
369 | dev_name[p - name] = '\0'; | |
370 | ||
371 | /* Skip over the whitespace after dev_name */ | |
372 | for (; isspace (*p); p++) | |
373 | /*EMPTY*/; | |
374 | ||
375 | if (1 != sscanf (p, "%d ", &baudrate)) | |
376 | goto erroid; | |
377 | ||
378 | /* Skip the number and then the spaces */ | |
379 | for (; isdigit (*p); p++) | |
380 | /*EMPTY*/; | |
381 | for (; isspace (*p); p++) | |
382 | /*EMPTY*/; | |
383 | ||
384 | if (prog_name != NULL) | |
385 | free (prog_name); | |
386 | prog_name = savestring (p, strlen (p)); | |
387 | ||
388 | eb_close (0); | |
389 | ||
390 | eb_desc = open (dev_name, O_RDWR); | |
391 | if (eb_desc < 0) | |
392 | perror_with_name (dev_name); | |
393 | ioctl (eb_desc, TIOCGETP, &sg); | |
394 | #ifdef HAVE_TERMIO | |
395 | sg.c_cc[VMIN] = 0; /* read with timeout. */ | |
396 | sg.c_cc[VTIME] = timeout * 10; | |
397 | sg.c_lflag &= ~(ICANON | ECHO); | |
398 | sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate); | |
399 | #else | |
400 | sg.sg_ispeed = damn_b (baudrate); | |
401 | sg.sg_ospeed = damn_b (baudrate); | |
402 | sg.sg_flags |= RAW | ANYP; | |
403 | sg.sg_flags &= ~ECHO; | |
404 | #endif | |
405 | ||
406 | ioctl (eb_desc, TIOCSETP, &sg); | |
407 | eb_stream = fdopen (eb_desc, "r+"); | |
408 | ||
409 | push_target (&eb_ops); | |
410 | if (from_tty) | |
411 | printf ("Remote %s debugging %s using %s\n", target_shortname, | |
412 | prog_name, dev_name); | |
413 | ||
414 | #ifndef HAVE_TERMIO | |
415 | #ifndef NO_SIGINTERRUPT | |
416 | /* Cause SIGALRM's to make reads fail with EINTR instead of resuming | |
417 | the read. */ | |
418 | if (siginterrupt (SIGALRM, 1) != 0) | |
419 | perror ("eb_open: error in siginterrupt"); | |
420 | #endif | |
421 | ||
422 | /* Set up read timeout timer. */ | |
423 | if ((void (*)) signal (SIGALRM, eb_timer) == (void (*)) -1) | |
424 | perror ("eb_open: error in signal"); | |
425 | #endif | |
426 | ||
427 | #if defined (LOG_FILE) | |
428 | log_file = fopen (LOG_FILE, "w"); | |
429 | if (log_file == NULL) | |
430 | perror_with_name (LOG_FILE); | |
431 | #endif | |
432 | ||
433 | /* Hello? Are you there? */ | |
434 | write (eb_desc, "\n", 1); | |
435 | ||
436 | expect_prompt (); | |
437 | } | |
438 | ||
439 | /* Close out all files and local state before this target loses control. */ | |
440 | ||
e36ca74a | 441 | static void |
dd3b648e RP |
442 | eb_close (quitting) |
443 | int quitting; | |
444 | { | |
445 | ||
446 | /* Due to a bug in Unix, fclose closes not only the stdio stream, | |
447 | but also the file descriptor. So we don't actually close | |
448 | eb_desc. */ | |
449 | if (eb_stream) | |
450 | fclose (eb_stream); /* This also closes eb_desc */ | |
451 | if (eb_desc >= 0) | |
452 | /* close (eb_desc); */ | |
453 | ||
454 | /* Do not try to close eb_desc again, later in the program. */ | |
455 | eb_stream = NULL; | |
456 | eb_desc = -1; | |
457 | ||
458 | #if defined (LOG_FILE) | |
76b2c3c8 JG |
459 | if (log_file) { |
460 | if (ferror (log_file)) | |
461 | printf ("Error writing log file.\n"); | |
462 | if (fclose (log_file) != 0) | |
463 | printf ("Error closing log file.\n"); | |
464 | } | |
dd3b648e RP |
465 | #endif |
466 | } | |
467 | ||
468 | /* Terminate the open connection to the remote debugger. | |
469 | Use this when you want to detach and do something else | |
470 | with your gdb. */ | |
471 | void | |
472 | eb_detach (from_tty) | |
473 | int from_tty; | |
474 | { | |
475 | pop_target(); /* calls eb_close to do the real work */ | |
476 | if (from_tty) | |
477 | printf ("Ending remote %s debugging\n", target_shortname); | |
478 | } | |
479 | ||
480 | /* Tell the remote machine to resume. */ | |
481 | ||
482 | void | |
483 | eb_resume (step, sig) | |
484 | int step, sig; | |
485 | { | |
486 | if (step) | |
487 | { | |
488 | write (eb_desc, "t 1,s\n", 6); | |
489 | /* Wait for the echo. */ | |
490 | expect ("t 1,s\r"); | |
491 | /* Then comes a line containing the instruction we stepped to. */ | |
492 | expect ("\n@"); | |
493 | /* Then we get the prompt. */ | |
494 | expect_prompt (); | |
495 | ||
496 | /* Force the next eb_wait to return a trap. Not doing anything | |
497 | about I/O from the target means that the user has to type | |
498 | "continue" to see any. This should be fixed. */ | |
499 | need_artificial_trap = 1; | |
500 | } | |
501 | else | |
502 | { | |
503 | if (need_gi) | |
504 | { | |
505 | need_gi = 0; | |
506 | write (eb_desc, "gi\n", 3); | |
507 | ||
508 | /* Swallow the echo of "gi". */ | |
509 | expect ("gi\r"); | |
510 | } | |
511 | else | |
512 | { | |
513 | write (eb_desc, "GR\n", 3); | |
514 | /* Swallow the echo. */ | |
515 | expect ("GR\r"); | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | /* Wait until the remote machine stops, then return, | |
521 | storing status in STATUS just as `wait' would. */ | |
522 | ||
523 | int | |
524 | eb_wait (status) | |
525 | WAITTYPE *status; | |
526 | { | |
527 | /* Strings to look for. '?' means match any single character. | |
528 | Note that with the algorithm we use, the initial character | |
529 | of the string cannot recur in the string, or we will not | |
530 | find some cases of the string in the input. */ | |
531 | ||
532 | static char bpt[] = "Invalid interrupt taken - #0x50 - "; | |
533 | /* It would be tempting to look for "\n[__exit + 0x8]\n" | |
534 | but that requires loading symbols with "yc i" and even if | |
535 | we did do that we don't know that the file has symbols. */ | |
536 | static char exitmsg[] = "\n@????????I JMPTI GR121,LR0"; | |
537 | char *bp = bpt; | |
538 | char *ep = exitmsg; | |
539 | ||
540 | /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars. */ | |
541 | char swallowed[50]; | |
542 | /* Current position in swallowed. */ | |
543 | char *swallowed_p = swallowed; | |
544 | ||
545 | int ch; | |
546 | int ch_handled; | |
547 | ||
548 | int old_timeout = timeout; | |
549 | ||
550 | WSETEXIT ((*status), 0); | |
551 | ||
552 | if (need_artificial_trap != 0) | |
553 | { | |
554 | WSETSTOP ((*status), SIGTRAP); | |
555 | need_artificial_trap--; | |
556 | return 0; | |
557 | } | |
558 | ||
559 | timeout = 0; /* Don't time out -- user program is running. */ | |
560 | while (1) | |
561 | { | |
562 | ch_handled = 0; | |
563 | ch = readchar (); | |
564 | if (ch == *bp) | |
565 | { | |
566 | bp++; | |
567 | if (*bp == '\0') | |
568 | break; | |
569 | ch_handled = 1; | |
570 | ||
571 | *swallowed_p++ = ch; | |
572 | } | |
573 | else | |
574 | bp = bpt; | |
575 | ||
576 | if (ch == *ep || *ep == '?') | |
577 | { | |
578 | ep++; | |
579 | if (*ep == '\0') | |
580 | break; | |
581 | ||
582 | if (!ch_handled) | |
583 | *swallowed_p++ = ch; | |
584 | ch_handled = 1; | |
585 | } | |
586 | else | |
587 | ep = exitmsg; | |
588 | ||
589 | if (!ch_handled) | |
590 | { | |
591 | char *p; | |
592 | ||
593 | /* Print out any characters which have been swallowed. */ | |
594 | for (p = swallowed; p < swallowed_p; ++p) | |
595 | putc (*p, stdout); | |
596 | swallowed_p = swallowed; | |
597 | ||
598 | putc (ch, stdout); | |
599 | } | |
600 | } | |
601 | expect_prompt (); | |
602 | if (*bp== '\0') | |
603 | WSETSTOP ((*status), SIGTRAP); | |
604 | else | |
605 | WSETEXIT ((*status), 0); | |
606 | timeout = old_timeout; | |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
611 | /* Return the name of register number REGNO | |
612 | in the form input and output by EBMON. | |
613 | ||
614 | Returns a pointer to a static buffer containing the answer. */ | |
615 | static char * | |
616 | get_reg_name (regno) | |
617 | int regno; | |
618 | { | |
619 | static char buf[80]; | |
620 | if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32) | |
621 | sprintf (buf, "GR%03d", regno - GR96_REGNUM + 96); | |
622 | else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128) | |
623 | sprintf (buf, "LR%03d", regno - LR0_REGNUM); | |
624 | else if (regno == Q_REGNUM) | |
625 | strcpy (buf, "SR131"); | |
626 | else if (regno >= BP_REGNUM && regno <= CR_REGNUM) | |
627 | sprintf (buf, "SR%03d", regno - BP_REGNUM + 133); | |
628 | else if (regno == ALU_REGNUM) | |
629 | strcpy (buf, "SR132"); | |
630 | else if (regno >= IPC_REGNUM && regno <= IPB_REGNUM) | |
631 | sprintf (buf, "SR%03d", regno - IPC_REGNUM + 128); | |
632 | else if (regno >= VAB_REGNUM && regno <= LRU_REGNUM) | |
633 | sprintf (buf, "SR%03d", regno - VAB_REGNUM); | |
634 | else if (regno == GR1_REGNUM) | |
635 | strcpy (buf, "GR001"); | |
636 | return buf; | |
637 | } | |
638 | ||
639 | /* Read the remote registers into the block REGS. */ | |
640 | ||
641 | static void | |
642 | eb_fetch_registers () | |
643 | { | |
644 | int reg_index; | |
645 | int regnum_index; | |
646 | char tempbuf[10]; | |
647 | int i; | |
648 | ||
649 | #if 0 | |
650 | /* This should not be necessary, because one is supposed to read the | |
651 | registers only when the inferior is stopped (at least with | |
652 | ptrace() and why not make it the same for remote?). */ | |
653 | /* ^A is the "normal character" used to make sure we are talking to EBMON | |
654 | and not to the program being debugged. */ | |
655 | write (eb_desc, "\001\n"); | |
656 | expect_prompt (); | |
657 | #endif | |
658 | ||
659 | write (eb_desc, "dw gr96,gr127\n", 14); | |
660 | for (reg_index = 96, regnum_index = GR96_REGNUM; | |
661 | reg_index < 128; | |
662 | reg_index += 4, regnum_index += 4) | |
663 | { | |
664 | sprintf (tempbuf, "GR%03d ", reg_index); | |
665 | expect (tempbuf); | |
666 | get_hex_regs (4, regnum_index); | |
667 | expect ("\n"); | |
668 | } | |
669 | ||
670 | for (i = 0; i < 128; i += 32) | |
671 | { | |
672 | /* The PC has a tendency to hang if we get these | |
673 | all in one fell swoop ("dw lr0,lr127"). */ | |
674 | sprintf (tempbuf, "dw lr%d\n", i); | |
675 | write (eb_desc, tempbuf, strlen (tempbuf)); | |
676 | for (reg_index = i, regnum_index = LR0_REGNUM + i; | |
677 | reg_index < i + 32; | |
678 | reg_index += 4, regnum_index += 4) | |
679 | { | |
680 | sprintf (tempbuf, "LR%03d ", reg_index); | |
681 | expect (tempbuf); | |
682 | get_hex_regs (4, regnum_index); | |
683 | expect ("\n"); | |
684 | } | |
685 | } | |
686 | ||
687 | write (eb_desc, "dw sr133,sr133\n", 15); | |
688 | expect ("SR133 "); | |
689 | get_hex_regs (1, BP_REGNUM); | |
690 | expect ("\n"); | |
691 | ||
692 | write (eb_desc, "dw sr134,sr134\n", 15); | |
693 | expect ("SR134 "); | |
694 | get_hex_regs (1, FC_REGNUM); | |
695 | expect ("\n"); | |
696 | ||
697 | write (eb_desc, "dw sr135,sr135\n", 15); | |
698 | expect ("SR135 "); | |
699 | get_hex_regs (1, CR_REGNUM); | |
700 | expect ("\n"); | |
701 | ||
702 | write (eb_desc, "dw sr131,sr131\n", 15); | |
703 | expect ("SR131 "); | |
704 | get_hex_regs (1, Q_REGNUM); | |
705 | expect ("\n"); | |
706 | ||
707 | write (eb_desc, "dw sr0,sr14\n", 12); | |
708 | for (reg_index = 0, regnum_index = VAB_REGNUM; | |
709 | regnum_index <= LRU_REGNUM; | |
710 | regnum_index += 4, reg_index += 4) | |
711 | { | |
712 | sprintf (tempbuf, "SR%03d ", reg_index); | |
713 | expect (tempbuf); | |
714 | get_hex_regs (reg_index == 12 ? 3 : 4, regnum_index); | |
715 | expect ("\n"); | |
716 | } | |
717 | ||
718 | /* There doesn't seem to be any way to get these. */ | |
719 | { | |
720 | int val = -1; | |
721 | supply_register (FPE_REGNUM, &val); | |
e95bfbf1 | 722 | supply_register (INTE_REGNUM, &val); |
dd3b648e RP |
723 | supply_register (FPS_REGNUM, &val); |
724 | supply_register (EXO_REGNUM, &val); | |
725 | } | |
726 | ||
727 | write (eb_desc, "dw gr1,gr1\n", 11); | |
728 | expect ("GR001 "); | |
729 | get_hex_regs (1, GR1_REGNUM); | |
730 | expect_prompt (); | |
731 | } | |
732 | ||
733 | /* Fetch register REGNO, or all registers if REGNO is -1. | |
734 | Returns errno value. */ | |
17f7e032 | 735 | void |
dd3b648e RP |
736 | eb_fetch_register (regno) |
737 | int regno; | |
738 | { | |
739 | if (regno == -1) | |
740 | eb_fetch_registers (); | |
741 | else | |
742 | { | |
743 | char *name = get_reg_name (regno); | |
744 | fprintf (eb_stream, "dw %s,%s\n", name, name); | |
745 | expect (name); | |
746 | expect (" "); | |
747 | get_hex_regs (1, regno); | |
748 | expect_prompt (); | |
749 | } | |
17f7e032 | 750 | return; |
dd3b648e RP |
751 | } |
752 | ||
753 | /* Store the remote registers from the contents of the block REGS. */ | |
754 | ||
755 | static void | |
756 | eb_store_registers () | |
757 | { | |
758 | int i, j; | |
759 | fprintf (eb_stream, "s gr1,%x\n", read_register (GR1_REGNUM)); | |
760 | expect_prompt (); | |
761 | ||
762 | for (j = 0; j < 32; j += 16) | |
763 | { | |
764 | fprintf (eb_stream, "s gr%d,", j + 96); | |
765 | for (i = 0; i < 15; ++i) | |
766 | fprintf (eb_stream, "%x,", read_register (GR96_REGNUM + j + i)); | |
767 | fprintf (eb_stream, "%x\n", read_register (GR96_REGNUM + j + 15)); | |
768 | expect_prompt (); | |
769 | } | |
770 | ||
771 | for (j = 0; j < 128; j += 16) | |
772 | { | |
773 | fprintf (eb_stream, "s lr%d,", j); | |
774 | for (i = 0; i < 15; ++i) | |
775 | fprintf (eb_stream, "%x,", read_register (LR0_REGNUM + j + i)); | |
776 | fprintf (eb_stream, "%x\n", read_register (LR0_REGNUM + j + 15)); | |
777 | expect_prompt (); | |
778 | } | |
779 | ||
780 | fprintf (eb_stream, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM), | |
781 | read_register (FC_REGNUM), read_register (CR_REGNUM)); | |
782 | expect_prompt (); | |
783 | fprintf (eb_stream, "s sr131,%x\n", read_register (Q_REGNUM)); | |
784 | expect_prompt (); | |
785 | fprintf (eb_stream, "s sr0,"); | |
786 | for (i = 0; i < 11; ++i) | |
787 | fprintf (eb_stream, "%x,", read_register (VAB_REGNUM + i)); | |
788 | fprintf (eb_stream, "%x\n", read_register (VAB_REGNUM + 11)); | |
789 | expect_prompt (); | |
790 | } | |
791 | ||
792 | /* Store register REGNO, or all if REGNO == 0. | |
793 | Return errno value. */ | |
e95bfbf1 | 794 | void |
dd3b648e RP |
795 | eb_store_register (regno) |
796 | int regno; | |
797 | { | |
798 | if (regno == -1) | |
799 | eb_store_registers (); | |
800 | else | |
801 | { | |
802 | char *name = get_reg_name (regno); | |
803 | fprintf (eb_stream, "s %s,%x\n", name, read_register (regno)); | |
804 | /* Setting GR1 changes the numbers of all the locals, so | |
805 | invalidate the register cache. Do this *after* calling | |
806 | read_register, because we want read_register to return the | |
807 | value that write_register has just stuffed into the registers | |
808 | array, not the value of the register fetched from the | |
809 | inferior. */ | |
810 | if (regno == GR1_REGNUM) | |
811 | registers_changed (); | |
812 | expect_prompt (); | |
813 | } | |
dd3b648e RP |
814 | } |
815 | ||
816 | /* Get ready to modify the registers array. On machines which store | |
817 | individual registers, this doesn't need to do anything. On machines | |
818 | which store all the registers in one fell swoop, this makes sure | |
819 | that registers contains all the registers from the program being | |
820 | debugged. */ | |
821 | ||
822 | void | |
823 | eb_prepare_to_store () | |
824 | { | |
825 | /* Do nothing, since we can store individual regs */ | |
826 | } | |
827 | ||
76b2c3c8 JG |
828 | |
829 | /* FIXME-someday! Merge these two. */ | |
dd3b648e | 830 | int |
8f1f2a72 | 831 | eb_xfer_inferior_memory (memaddr, myaddr, len, write, target) |
dd3b648e RP |
832 | CORE_ADDR memaddr; |
833 | char *myaddr; | |
834 | int len; | |
835 | int write; | |
8f1f2a72 | 836 | struct target_ops *target; /* ignored */ |
dd3b648e RP |
837 | { |
838 | if (write) | |
839 | return eb_write_inferior_memory (memaddr, myaddr, len); | |
840 | else | |
76b2c3c8 | 841 | return eb_read_inferior_memory (memaddr, myaddr, len); |
dd3b648e RP |
842 | } |
843 | ||
844 | void | |
845 | eb_files_info () | |
846 | { | |
847 | printf ("\tAttached to %s at %d baud and running program %s.\n", | |
848 | dev_name, baudrate, prog_name); | |
849 | } | |
850 | ||
851 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
76b2c3c8 | 852 | to inferior's memory at MEMADDR. Returns length moved. */ |
dd3b648e RP |
853 | int |
854 | eb_write_inferior_memory (memaddr, myaddr, len) | |
855 | CORE_ADDR memaddr; | |
856 | char *myaddr; | |
857 | int len; | |
858 | { | |
859 | int i; | |
860 | ||
861 | for (i = 0; i < len; i++) | |
862 | { | |
863 | if ((i % 16) == 0) | |
864 | fprintf (eb_stream, "sb %x,", memaddr + i); | |
865 | if ((i % 16) == 15 || i == len - 1) | |
866 | { | |
867 | fprintf (eb_stream, "%x\n", ((unsigned char *)myaddr)[i]); | |
868 | expect_prompt (); | |
869 | } | |
870 | else | |
871 | fprintf (eb_stream, "%x,", ((unsigned char *)myaddr)[i]); | |
872 | } | |
76b2c3c8 | 873 | return len; |
dd3b648e RP |
874 | } |
875 | ||
876 | /* Read LEN bytes from inferior memory at MEMADDR. Put the result | |
76b2c3c8 | 877 | at debugger address MYADDR. Returns length moved. */ |
dd3b648e RP |
878 | int |
879 | eb_read_inferior_memory(memaddr, myaddr, len) | |
880 | CORE_ADDR memaddr; | |
881 | char *myaddr; | |
882 | int len; | |
883 | { | |
884 | int i; | |
885 | ||
886 | /* Number of bytes read so far. */ | |
887 | int count; | |
888 | ||
889 | /* Starting address of this pass. */ | |
890 | unsigned long startaddr; | |
891 | ||
892 | /* Number of bytes to read in this pass. */ | |
893 | int len_this_pass; | |
894 | ||
895 | /* Note that this code works correctly if startaddr is just less | |
896 | than UINT_MAX (well, really CORE_ADDR_MAX if there was such a | |
897 | thing). That is, something like | |
898 | eb_read_bytes (CORE_ADDR_MAX - 4, foo, 4) | |
899 | works--it never adds len to memaddr and gets 0. */ | |
900 | /* However, something like | |
901 | eb_read_bytes (CORE_ADDR_MAX - 3, foo, 4) | |
902 | doesn't need to work. Detect it and give up if there's an attempt | |
903 | to do that. */ | |
76b2c3c8 JG |
904 | if (((memaddr - 1) + len) < memaddr) { |
905 | errno = EIO; | |
906 | return 0; | |
907 | } | |
dd3b648e RP |
908 | |
909 | startaddr = memaddr; | |
910 | count = 0; | |
911 | while (count < len) | |
912 | { | |
913 | len_this_pass = 16; | |
914 | if ((startaddr % 16) != 0) | |
915 | len_this_pass -= startaddr % 16; | |
916 | if (len_this_pass > (len - count)) | |
917 | len_this_pass = (len - count); | |
918 | ||
919 | fprintf (eb_stream, "db %x,%x\n", startaddr, | |
920 | (startaddr - 1) + len_this_pass); | |
921 | expect ("\n"); | |
922 | ||
923 | /* Look for 8 hex digits. */ | |
924 | i = 0; | |
925 | while (1) | |
926 | { | |
927 | if (isxdigit (readchar ())) | |
928 | ++i; | |
929 | else | |
930 | { | |
931 | expect_prompt (); | |
932 | error ("Hex digit expected from remote system."); | |
933 | } | |
934 | if (i >= 8) | |
935 | break; | |
936 | } | |
937 | ||
938 | expect (" "); | |
939 | ||
940 | for (i = 0; i < len_this_pass; i++) | |
941 | get_hex_byte (&myaddr[count++]); | |
942 | ||
943 | expect_prompt (); | |
944 | ||
945 | startaddr += len_this_pass; | |
946 | } | |
76b2c3c8 | 947 | return len; |
dd3b648e RP |
948 | } |
949 | ||
76b2c3c8 JG |
950 | static void |
951 | eb_kill (args, from_tty) | |
952 | char *args; | |
953 | int from_tty; | |
954 | { | |
955 | return; /* Ignore attempts to kill target system */ | |
956 | } | |
957 | ||
958 | /* Clean up when a program exits. | |
959 | ||
960 | The program actually lives on in the remote processor's RAM, and may be | |
961 | run again without a download. Don't leave it full of breakpoint | |
962 | instructions. */ | |
963 | ||
964 | void | |
965 | eb_mourn_inferior () | |
966 | { | |
967 | remove_breakpoints (); | |
71607f9d | 968 | unpush_target (&eb_ops); |
76b2c3c8 JG |
969 | generic_mourn_inferior (); /* Do all the proper things now */ |
970 | } | |
dd3b648e RP |
971 | /* Define the target subroutine names */ |
972 | ||
973 | struct target_ops eb_ops = { | |
974 | "amd-eb", "Remote serial AMD EBMON target", | |
f2fc6e7a JK |
975 | "Use a remote computer running EBMON connected by a serial line.\n\ |
976 | Arguments are the name of the device for the serial line,\n\ | |
977 | the speed to connect at in bits per second, and the filename of the\n\ | |
978 | executable as it exists on the remote computer. For example,\n\ | |
979 | target amd-eb /dev/ttya 9600 demo", | |
dd3b648e RP |
980 | eb_open, eb_close, |
981 | 0, eb_detach, eb_resume, eb_wait, | |
982 | eb_fetch_register, eb_store_register, | |
a03d4f8e | 983 | eb_prepare_to_store, |
dd3b648e RP |
984 | eb_xfer_inferior_memory, eb_files_info, |
985 | 0, 0, /* Breakpoints */ | |
986 | 0, 0, 0, 0, 0, /* Terminal handling */ | |
76b2c3c8 | 987 | eb_kill, |
6b27ebe8 | 988 | generic_load, /* load */ |
dd3b648e | 989 | 0, /* lookup_symbol */ |
76b2c3c8 JG |
990 | eb_create_inferior, |
991 | eb_mourn_inferior, | |
5ee4e16c | 992 | 0, /* can_run */ |
3950a34e | 993 | 0, /* notice_signals */ |
dd3b648e RP |
994 | process_stratum, 0, /* next */ |
995 | 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */ | |
8f1f2a72 | 996 | 0, 0, /* Section pointers */ |
dd3b648e RP |
997 | OPS_MAGIC, /* Always the last thing */ |
998 | }; | |
999 | ||
1000 | void | |
1001 | _initialize_remote_eb () | |
1002 | { | |
1003 | add_target (&eb_ops); | |
1004 | } |