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