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