Commit | Line | Data |
---|---|---|
51d6a954 RS |
1 | /* Remote debugging interface for boot monitors, for GDB. |
2 | Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc. | |
3 | Contributed by Cygnus Support. Written by Rob Savoye 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 | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
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 | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | /* This file was derived from various remote-* modules. It is a collection | |
22 | of generic support functions so GDB can talk directly to a ROM based | |
23 | monitor. This saves use from having to hack an exception based handler | |
24 | into existance, and makes for quick porting. | |
25 | ||
26 | This module talks to a debug monitor called 'MONITOR', which | |
27 | We communicate with MONITOR via either a direct serial line, or a TCP | |
28 | (or possibly TELNET) stream to a terminal multiplexor, | |
29 | which in turn talks to the target board. | |
30 | */ | |
31 | ||
32 | #include "defs.h" | |
33 | #include "gdbcore.h" | |
34 | #include "target.h" | |
35 | #include "wait.h" | |
36 | #include <varargs.h> | |
37 | #include <signal.h> | |
38 | #include <string.h> | |
39 | #include <sys/types.h> | |
40 | #include "command.h" | |
41 | #include "serial.h" | |
42 | #include "monitor.h" | |
43 | #include "remote-utils.h" | |
44 | ||
45 | #ifdef HAVE_TERMIO | |
46 | # define TERMINAL struct termios | |
47 | #else | |
48 | # define TERMINAL struct sgttyb | |
49 | #endif | |
50 | ||
51 | struct monitor_ops *current_monitor; | |
52 | extern struct cmd_list_element *setlist; | |
53 | extern struct cmd_list_element *unsetlist; | |
54 | struct cmd_list_element *showlist; | |
55 | ||
56 | static int hashmark; /* flag set by "set hash" */ | |
57 | ||
58 | /* FIXME: Replace with sr_get_debug (). */ | |
59 | #define LOG_FILE "monitor.log" | |
60 | #if defined (LOG_FILE) | |
61 | FILE *log_file; | |
62 | #endif | |
63 | ||
64 | static int timeout = 24; | |
65 | ||
66 | /* Descriptor for I/O to remote machine. Initialize it to NULL so that | |
67 | monitor_open knows that we don't have a file open when the program starts. | |
68 | */ | |
69 | static serial_t monitor_desc = NULL; | |
70 | ||
71 | /* sets the download protocol, choices are srec, generic, boot */ | |
72 | char *loadtype; | |
73 | static char *loadtype_str; | |
74 | static void set_loadtype_command(); | |
75 | ||
76 | /* | |
77 | * set_loadtype_command -- set the type for downloading. Check to make | |
78 | * sure you have a support protocol for this target. | |
79 | */ | |
80 | static void | |
81 | set_loadtype_command (ignore, from_tty, c) | |
82 | char *ignore; | |
83 | int from_tty; | |
84 | struct cmd_list_element *c; | |
85 | { | |
86 | #if 0 | |
87 | char *type; | |
88 | if (strcmp (LOADTYPES, "")) { | |
89 | error ("No loadtype set"); | |
90 | return; | |
91 | } | |
92 | ||
93 | type = strtok(LOADTYPES, ","); | |
94 | if (STREQ (type, (*(char **) c->var))) { | |
95 | loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var)); | |
96 | return; | |
97 | } | |
98 | ||
99 | while (type = strtok (NULL, ",") != (char *)NULL) | |
100 | if (STREQ (type, (*(char **) c->var))) | |
101 | loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var)); | |
102 | #endif | |
103 | loadtype_str = savestring (*(char **) c->var, strlen (*(char **) c->var)); | |
104 | } | |
105 | ||
106 | /* | |
107 | * printf_monitor -- send data to monitor. Works just like printf. | |
108 | */ | |
109 | static void | |
110 | printf_monitor(va_alist) | |
111 | va_dcl | |
112 | { | |
113 | va_list args; | |
114 | char *pattern; | |
115 | char buf[200]; | |
116 | int i; | |
117 | ||
118 | va_start(args); | |
119 | ||
120 | pattern = va_arg(args, char *); | |
121 | ||
122 | vsprintf(buf, pattern, args); | |
123 | ||
e6fa5bd6 | 124 | debuglogs (1, "printf_monitor(), Sending: \"%s\".", buf); |
51d6a954 RS |
125 | |
126 | if (SERIAL_WRITE(monitor_desc, buf, strlen(buf))) | |
127 | fprintf(stderr, "SERIAL_WRITE failed: %s\n", safe_strerror(errno)); | |
128 | } | |
129 | ||
e6fa5bd6 RS |
130 | /* |
131 | * debuglogs -- deal with debugging info to multiple sources. This takes | |
132 | * two real args, the first one is the level to be compared against | |
133 | * the sr_get_debug() value, the second arg is a printf buffer and args | |
134 | * to be formatted and printed. A CR is added after each string is printed. | |
135 | */ | |
136 | static void | |
137 | debuglogs(va_alist) | |
138 | va_dcl | |
139 | { | |
140 | va_list args; | |
141 | char *pattern, *p; | |
142 | char buf[200]; | |
143 | char newbuf[300]; | |
144 | int level, i; | |
145 | ||
146 | va_start(args); | |
147 | ||
148 | level = va_arg(args, int); /* get the debug level */ | |
149 | if ((level <0) || (level > 100)) { | |
150 | error ("Bad argument passed to debuglogs()"); | |
151 | return; | |
152 | } | |
153 | ||
154 | pattern = va_arg(args, char *); /* get the printf style pattern */ | |
155 | ||
156 | vsprintf(buf, pattern, args); /* format the string */ | |
157 | ||
158 | /* convert some characters so it'll look right in the log */ | |
159 | p = newbuf; | |
160 | for (i=0 ; buf[i] != '\0'; i++) { | |
161 | switch (buf[i]) { | |
162 | case '\n': /* newlines */ | |
163 | *p++ = '\\'; | |
164 | *p++ = 'n'; | |
165 | continue; | |
166 | case '\r': /* carriage returns */ | |
167 | *p++ = '\\'; | |
168 | *p++ = 'r'; | |
169 | continue; | |
170 | case '\033': /* escape */ | |
171 | *p++ = '\\'; | |
172 | *p++ = 'e'; | |
173 | continue; | |
174 | case '\t': /* tab */ | |
175 | *p++ = '\\'; | |
176 | *p++ = 't'; | |
177 | continue; | |
178 | case '\b': /* backspace */ | |
179 | *p++ = '\\'; | |
180 | *p++ = 'b'; | |
181 | continue; | |
182 | default: /* no change */ | |
183 | *p++ = buf[i]; | |
184 | } | |
185 | ||
186 | if (buf[i] < 26) { /* modify control characters */ | |
187 | *p++ = '^'; | |
188 | *p++ = buf[i] + 'A'; | |
189 | continue; | |
190 | } | |
191 | } | |
192 | *p = '\0'; /* terminate the string */ | |
193 | ||
194 | if (sr_get_debug() > level) | |
195 | puts (newbuf); | |
196 | ||
197 | #ifdef LOG_FILE /* write to the monitor log */ | |
198 | if (log_file != 0x0) { | |
199 | fputs (newbuf, log_file); | |
200 | fputc ('\n', log_file); | |
201 | fflush (log_file); | |
202 | } | |
203 | #endif | |
204 | } | |
205 | ||
7804e5bc RS |
206 | /* readchar -- read a character from the remote system, doing all the fancy |
207 | * timeout stuff. | |
208 | */ | |
51d6a954 RS |
209 | static int |
210 | readchar(timeout) | |
211 | int timeout; | |
212 | { | |
213 | int c; | |
214 | ||
215 | c = SERIAL_READCHAR(monitor_desc, timeout); | |
216 | ||
e6fa5bd6 | 217 | if (sr_get_debug() > 4) |
51d6a954 RS |
218 | putchar(c & 0x7f); |
219 | ||
220 | #ifdef LOG_FILE | |
221 | if (isascii (c)) | |
222 | putc(c & 0x7f, log_file); | |
223 | #endif | |
224 | ||
225 | if (c >= 0) | |
226 | return c & 0x7f; | |
227 | ||
7804e5bc RS |
228 | if (c == SERIAL_TIMEOUT) { |
229 | if (timeout == 0) | |
230 | return c; /* Polls shouldn't generate timeout errors */ | |
231 | error("Timeout reading from remote system."); | |
7a1330f7 RS |
232 | #ifdef LOG_FILE |
233 | fputc ("ERROR: Timeout reading from remote system", log_file); | |
234 | #endif | |
7804e5bc | 235 | } |
51d6a954 RS |
236 | perror_with_name("remote-monitor"); |
237 | } | |
238 | ||
7804e5bc RS |
239 | /* |
240 | * expect -- scan input from the remote system, until STRING is found. | |
241 | * If DISCARD is non-zero, then discard non-matching input, else print | |
242 | * it out. Let the user break out immediately. | |
243 | */ | |
51d6a954 RS |
244 | static void |
245 | expect (string, discard) | |
246 | char *string; | |
247 | int discard; | |
248 | { | |
249 | char *p = string; | |
250 | int c; | |
251 | ||
252 | ||
e6fa5bd6 | 253 | debuglogs (1, "Expecting \"%s\".", string); |
51d6a954 RS |
254 | |
255 | immediate_quit = 1; | |
256 | while (1) { | |
257 | c = readchar(timeout); | |
258 | if (!isascii (c)) | |
259 | continue; | |
260 | if (c == *p++) { | |
261 | if (*p == '\0') { | |
262 | immediate_quit = 0; | |
7a1330f7 | 263 | debuglogs (4, "Matched"); |
51d6a954 RS |
264 | return; |
265 | } | |
266 | } else { | |
267 | if (!discard) { | |
268 | fwrite(string, 1, (p - 1) - string, stdout); | |
269 | putchar((char)c); | |
270 | fflush(stdout); | |
271 | } | |
272 | p = string; | |
273 | } | |
274 | } | |
275 | } | |
276 | ||
277 | /* Keep discarding input until we see the MONITOR prompt. | |
278 | ||
279 | The convention for dealing with the prompt is that you | |
280 | o give your command | |
281 | o *then* wait for the prompt. | |
282 | ||
283 | Thus the last thing that a procedure does with the serial line | |
284 | will be an expect_prompt(). Exception: monitor_resume does not | |
285 | wait for the prompt, because the terminal is being handed over | |
286 | to the inferior. However, the next thing which happens after that | |
287 | is a monitor_wait which does wait for the prompt. | |
288 | Note that this includes abnormal exit, e.g. error(). This is | |
289 | necessary to prevent getting into states from which we can't | |
290 | recover. */ | |
291 | static void | |
292 | expect_prompt(discard) | |
293 | int discard; | |
294 | { | |
295 | #if defined (LOG_FILE) | |
296 | /* This is a convenient place to do this. The idea is to do it often | |
297 | enough that we never lose much data if we terminate abnormally. */ | |
298 | fflush(log_file); | |
299 | #endif | |
300 | expect (PROMPT, discard); | |
301 | } | |
302 | ||
303 | /* | |
304 | * junk -- ignore junk characters. Returns a 1 if junk, 0 otherwise | |
305 | */ | |
306 | static int | |
307 | junk(ch) | |
308 | char ch; | |
309 | { | |
310 | switch (ch) { | |
311 | case ' ': | |
312 | case '-': | |
313 | case '\t': | |
314 | case '\r': | |
315 | case '\n': | |
316 | if (sr_get_debug() > 5) | |
e6fa5bd6 | 317 | debuglogs (5, "Ignoring \'%c\'.", ch); |
51d6a954 RS |
318 | return 1; |
319 | default: | |
320 | if (sr_get_debug() > 5) | |
e6fa5bd6 | 321 | debuglogs (5, "Accepting \'%c\'.", ch); |
51d6a954 RS |
322 | return 0; |
323 | } | |
324 | } | |
325 | ||
326 | /* | |
327 | * get_hex_digit -- Get a hex digit from the remote system & return its value. | |
328 | * If ignore is nonzero, ignore spaces, newline & tabs. | |
329 | */ | |
330 | static int | |
331 | get_hex_digit(ignore) | |
332 | int ignore; | |
333 | { | |
334 | static int ch; | |
335 | while (1) { | |
336 | ch = readchar(timeout); | |
337 | if (junk(ch)) | |
338 | continue; | |
339 | if (sr_get_debug() > 4) | |
e6fa5bd6 | 340 | debuglogs (4, "get_hex_digit() got a 0x%x(%c)", ch, ch); |
51d6a954 RS |
341 | |
342 | if (ch >= '0' && ch <= '9') | |
343 | return ch - '0'; | |
344 | else if (ch >= 'A' && ch <= 'F') | |
345 | return ch - 'A' + 10; | |
346 | else if (ch >= 'a' && ch <= 'f') | |
347 | return ch - 'a' + 10; | |
348 | else if (ch == ' ' && ignore) | |
349 | ; | |
350 | else { | |
351 | expect_prompt(1); | |
352 | error("Invalid hex digit from remote system."); | |
353 | } | |
354 | } | |
355 | } | |
356 | ||
357 | /* get_hex_byte -- Get a byte from monitor and put it in *BYT. | |
358 | * Accept any number leading spaces. | |
359 | */ | |
360 | static void | |
361 | get_hex_byte (byt) | |
362 | char *byt; | |
363 | { | |
364 | int val; | |
365 | ||
366 | val = get_hex_digit (1) << 4; | |
e6fa5bd6 | 367 | debuglogs (4, "get_hex_digit() -- Read first nibble 0x%x", val); |
51d6a954 RS |
368 | |
369 | val |= get_hex_digit (0); | |
e6fa5bd6 | 370 | debuglogs (4, "get_hex_digit() -- Read second nibble 0x%x", val); |
51d6a954 RS |
371 | *byt = val; |
372 | ||
e6fa5bd6 | 373 | debuglogs (4, "get_hex_digit() -- Read a 0x%x", val); |
51d6a954 RS |
374 | } |
375 | ||
376 | /* | |
377 | * get_hex_word -- Get N 32-bit words from remote, each preceded by a space, | |
378 | * and put them in registers starting at REGNO. | |
379 | */ | |
380 | static int | |
381 | get_hex_word () | |
382 | { | |
383 | long val; | |
384 | int i; | |
385 | ||
386 | val = 0; | |
387 | for (i = 0; i < 8; i++) | |
388 | val = (val << 4) + get_hex_digit (i == 0); | |
389 | ||
7a1330f7 | 390 | debuglogs (4, "get_hex_word() got a 0x%x.", val); |
51d6a954 RS |
391 | |
392 | return val; | |
393 | } | |
394 | ||
395 | /* This is called not only when we first attach, but also when the | |
396 | user types "run" after having attached. */ | |
397 | void | |
398 | monitor_create_inferior (execfile, args, env) | |
399 | char *execfile; | |
400 | char *args; | |
401 | char **env; | |
402 | { | |
403 | int entry_pt; | |
404 | ||
405 | if (args && *args) | |
406 | error("Can't pass arguments to remote MONITOR process"); | |
407 | ||
408 | if (execfile == 0 || exec_bfd == 0) | |
409 | error("No exec file specified"); | |
410 | ||
411 | entry_pt = (int) bfd_get_start_address (exec_bfd); | |
412 | ||
7a1330f7 | 413 | debuglogs (1, "create_inferior(exexfile=%s, args=%s, env=%s)", execfile, args, env); |
51d6a954 RS |
414 | |
415 | /* The "process" (board) is already stopped awaiting our commands, and | |
416 | the program is already downloaded. We just set its PC and go. */ | |
417 | ||
418 | clear_proceed_status (); | |
419 | ||
420 | /* Tell wait_for_inferior that we've started a new process. */ | |
421 | init_wait_for_inferior (); | |
422 | ||
423 | /* Set up the "saved terminal modes" of the inferior | |
424 | based on what modes we are starting it with. */ | |
425 | target_terminal_init (); | |
426 | ||
427 | /* Install inferior's terminal modes. */ | |
428 | target_terminal_inferior (); | |
429 | ||
430 | /* insert_step_breakpoint (); FIXME, do we need this? */ | |
431 | ||
432 | /* Let 'er rip... */ | |
433 | proceed ((CORE_ADDR)entry_pt, TARGET_SIGNAL_DEFAULT, 0); | |
434 | } | |
435 | ||
7804e5bc RS |
436 | /* |
437 | * monitor_open -- open a connection to a remote debugger. | |
438 | * NAME is the filename used for communication. | |
439 | */ | |
51d6a954 RS |
440 | static int baudrate = 9600; |
441 | static char dev_name[100]; | |
442 | ||
443 | void | |
444 | monitor_open(args, name, from_tty) | |
445 | char *args; | |
446 | char *name; | |
447 | int from_tty; | |
448 | { | |
449 | ||
450 | if (args == NULL) | |
451 | error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\ | |
452 | `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name); | |
453 | ||
454 | /* if (is_open) */ | |
455 | monitor_close(0); | |
456 | ||
457 | strcpy(dev_name, args); | |
458 | monitor_desc = SERIAL_OPEN(dev_name); | |
459 | ||
460 | if (monitor_desc == NULL) | |
461 | perror_with_name(dev_name); | |
462 | ||
7a1330f7 RS |
463 | if (baud_rate != -1) { |
464 | if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) { | |
465 | SERIAL_CLOSE (monitor_desc); | |
466 | perror_with_name (name); | |
51d6a954 | 467 | } |
7a1330f7 RS |
468 | } |
469 | ||
51d6a954 RS |
470 | SERIAL_RAW(monitor_desc); |
471 | ||
472 | #if defined (LOG_FILE) | |
473 | log_file = fopen (LOG_FILE, "w"); | |
474 | if (log_file == NULL) | |
475 | perror_with_name (LOG_FILE); | |
476 | #endif | |
477 | ||
478 | /* wake up the monitor and see if it's alive */ | |
479 | printf_monitor(INIT_CMD); | |
480 | expect_prompt(1); /* See if we get a prompt */ | |
481 | ||
482 | /* try again to be sure */ | |
483 | printf_monitor(INIT_CMD); | |
484 | expect_prompt(1); /* See if we get a prompt */ | |
485 | ||
486 | if (from_tty) | |
487 | printf("Remote target %s connected to %s\n", TARGET_NAME, dev_name); | |
51d6a954 RS |
488 | } |
489 | ||
490 | /* | |
7804e5bc RS |
491 | * monitor_close -- Close out all files and local state before this |
492 | * target loses control. | |
51d6a954 RS |
493 | */ |
494 | ||
495 | void | |
496 | monitor_close (quitting) | |
497 | int quitting; | |
498 | { | |
499 | SERIAL_CLOSE(monitor_desc); | |
500 | monitor_desc = NULL; | |
501 | ||
e6fa5bd6 | 502 | debuglogs (1, "monitor_close (quitting=%d)", quitting); |
7804e5bc | 503 | |
51d6a954 RS |
504 | #if defined (LOG_FILE) |
505 | if (log_file) { | |
506 | if (ferror(log_file)) | |
507 | fprintf(stderr, "Error writing log file.\n"); | |
508 | if (fclose(log_file) != 0) | |
509 | fprintf(stderr, "Error closing log file.\n"); | |
510 | } | |
511 | #endif | |
512 | } | |
513 | ||
7804e5bc RS |
514 | /* |
515 | * monitor_detach -- terminate the open connection to the remote | |
516 | * debugger. Use this when you want to detach and do something | |
517 | * else with your gdb. | |
518 | */ | |
51d6a954 RS |
519 | void |
520 | monitor_detach (from_tty) | |
521 | int from_tty; | |
522 | { | |
e6fa5bd6 | 523 | |
7a1330f7 | 524 | debuglogs (1, "monitor_detach ()"); |
7804e5bc | 525 | |
51d6a954 RS |
526 | pop_target(); /* calls monitor_close to do the real work */ |
527 | if (from_tty) | |
528 | printf ("Ending remote %s debugging\n", target_shortname); | |
529 | } | |
7804e5bc RS |
530 | |
531 | /* | |
532 | * monitor_attach -- attach GDB to the target. | |
533 | */ | |
534 | void | |
535 | monitor_attach (args, from_tty) | |
536 | char *args; | |
537 | int from_tty; | |
538 | { | |
539 | if (from_tty) | |
540 | printf ("Starting remote %s debugging\n", target_shortname); | |
51d6a954 | 541 | |
7a1330f7 | 542 | debuglogs (1, "monitor_attach (args=%s)", args); |
7804e5bc | 543 | |
7804e5bc RS |
544 | printf_monitor (GO_CMD); |
545 | /* swallow the echo. */ | |
546 | expect (GO_CMD, 1); | |
547 | } | |
548 | ||
51d6a954 | 549 | /* |
7804e5bc | 550 | * monitor_resume -- Tell the remote machine to resume. |
51d6a954 RS |
551 | */ |
552 | void | |
553 | monitor_resume (pid, step, sig) | |
554 | int pid, step; | |
555 | enum target_signal sig; | |
556 | { | |
7a1330f7 | 557 | debuglogs (1, "monitor_resume (step=%d, sig=%d)", step, sig); |
7804e5bc RS |
558 | |
559 | if (step) { | |
560 | printf_monitor (STEP_CMD); | |
7804e5bc RS |
561 | } else { |
562 | printf_monitor (CONT_CMD); | |
7804e5bc | 563 | } |
51d6a954 RS |
564 | } |
565 | ||
566 | /* | |
7a1330f7 | 567 | * monitor_wait -- Wait until the remote machine stops, then return, |
51d6a954 RS |
568 | * storing status in status just as `wait' would. |
569 | */ | |
51d6a954 RS |
570 | int |
571 | monitor_wait (pid, status) | |
572 | int pid; | |
573 | struct target_waitstatus *status; | |
574 | { | |
575 | int old_timeout = timeout; | |
51d6a954 | 576 | |
7a1330f7 | 577 | debuglogs(1, "monitor_wait (), printing extraneous text."); |
e6fa5bd6 | 578 | |
51d6a954 RS |
579 | status->kind = TARGET_WAITKIND_EXITED; |
580 | status->value.integer = 0; | |
581 | ||
582 | timeout = 0; /* Don't time out -- user program is running. */ | |
583 | ||
584 | expect_prompt(0); /* Wait for prompt, outputting extraneous text */ | |
7a1330f7 | 585 | debuglogs (4, "monitor_wait(), got the prompt."); |
51d6a954 RS |
586 | |
587 | status->kind = TARGET_WAITKIND_STOPPED; | |
588 | status->value.sig = TARGET_SIGNAL_TRAP; | |
589 | ||
590 | timeout = old_timeout; | |
591 | ||
592 | return 0; | |
593 | } | |
594 | ||
595 | /* Return the name of register number regno in the form input and output by | |
596 | monitor. Currently, register_names just happens to contain exactly what | |
597 | monitor wants. Lets take advantage of that just as long as possible! */ | |
598 | ||
599 | static char * | |
600 | get_reg_name (regno) | |
601 | int regno; | |
602 | { | |
603 | static char buf[50]; | |
604 | const char *p; | |
605 | char *b; | |
606 | ||
607 | b = buf; | |
608 | ||
609 | if (regno < 0) | |
610 | return (""); | |
611 | ||
612 | for (p = REGNAMES(regno); *p; p++) | |
613 | *b++ = tolower(*p); | |
614 | ||
615 | *b = '\000'; | |
616 | ||
e6fa5bd6 | 617 | debuglogs (5, "Got name \"%s\" from regno #%d.", buf, regno); |
f1ca4cbc | 618 | |
51d6a954 RS |
619 | return buf; |
620 | } | |
621 | ||
e6fa5bd6 RS |
622 | /* |
623 | * monitor_fetch_registers -- read the remote registers into the | |
624 | * block regs. | |
625 | */ | |
51d6a954 RS |
626 | void |
627 | monitor_fetch_registers () | |
628 | { | |
629 | int regno; | |
630 | ||
631 | /* yeah yeah, i know this is horribly inefficient. but it isn't done | |
632 | very often... i'll clean it up later. */ | |
633 | ||
634 | for (regno = 0; regno <= PC_REGNUM; regno++) | |
635 | monitor_fetch_register(regno); | |
636 | } | |
637 | ||
638 | /* | |
f1ca4cbc RS |
639 | * monitor_fetch_register -- fetch register REGNO, or all registers if REGNO |
640 | * is -1. Returns errno value. | |
51d6a954 RS |
641 | */ |
642 | void | |
643 | monitor_fetch_register (regno) | |
644 | int regno; | |
645 | { | |
646 | int val, j; | |
647 | ||
7a1330f7 | 648 | debuglogs (1, "monitor_fetch_register (reg=%s)", get_reg_name (regno)); |
51d6a954 RS |
649 | |
650 | if (regno < 0) { | |
651 | monitor_fetch_registers (); | |
652 | } else { | |
653 | char *name = get_reg_name (regno); | |
654 | if (STREQ(name, "")) | |
655 | return; | |
656 | printf_monitor (ROMCMD(GET_REG), name); /* send the command */ | |
657 | expect (name, 1); /* then strip the leading garbage */ | |
658 | if (*ROMDELIM(GET_REG) != 0) { /* if there's a delimiter */ | |
659 | expect (ROMDELIM(GET_REG), 1); | |
660 | } | |
661 | ||
662 | val = get_hex_word(); /* get the value, ignore junk */ | |
663 | supply_register (regno, (char *) &val); | |
664 | ||
665 | if (*ROMDELIM(GET_REG) != 0) { | |
f1ca4cbc | 666 | /*** expect (ROMRES(GET_REG)); ***/ |
51d6a954 RS |
667 | printf_monitor (CMD_END); |
668 | } | |
669 | expect_prompt (1); | |
670 | } | |
671 | return; | |
672 | } | |
673 | ||
674 | /* Store the remote registers from the contents of the block REGS. */ | |
675 | ||
676 | void | |
677 | monitor_store_registers () | |
678 | { | |
679 | int regno; | |
680 | ||
e6fa5bd6 RS |
681 | debuglogs (1, "monitor_store_registers()"); |
682 | ||
51d6a954 RS |
683 | for (regno = 0; regno <= PC_REGNUM; regno++) |
684 | monitor_store_register(regno); | |
685 | ||
686 | registers_changed (); | |
687 | } | |
688 | ||
f1ca4cbc RS |
689 | /* |
690 | * monitor_store_register -- store register REGNO, or all if REGNO == 0. | |
691 | * return errno value. | |
692 | */ | |
51d6a954 RS |
693 | void |
694 | monitor_store_register (regno) | |
695 | int regno; | |
696 | { | |
697 | char *name; | |
f1ca4cbc RS |
698 | int i; |
699 | ||
700 | i = read_register(regno); | |
51d6a954 | 701 | |
e6fa5bd6 | 702 | debuglogs (1, "monitor_store_register (regno=%d)", regno); |
51d6a954 RS |
703 | |
704 | if (regno < 0) | |
705 | monitor_store_registers (); | |
706 | else { | |
e6fa5bd6 | 707 | debuglogs (3, "Setting register %s to 0x%x", get_reg_name (regno), read_register (regno)); |
51d6a954 RS |
708 | |
709 | name = get_reg_name (regno); | |
710 | if (STREQ(name, "")) | |
711 | return; | |
f1ca4cbc RS |
712 | printf_monitor (ROMCMD(SET_REG), name, read_register(regno)); |
713 | expect (name, 1); /* strip the leading garbage */ | |
51d6a954 RS |
714 | if (*ROMDELIM(SET_REG) != 0) { /* if there's a delimiter */ |
715 | expect (ROMDELIM(SET_REG), 1); | |
f1ca4cbc RS |
716 | get_hex_word(1); |
717 | printf_monitor ("%d%s\n", i, CMD_END); | |
51d6a954 RS |
718 | } |
719 | expect_prompt (1); | |
720 | } | |
721 | return; | |
722 | ||
723 | #if 0 | |
724 | printf_monitor (SET_REG, get_reg_name (regno), | |
725 | read_register (regno)); | |
726 | expect_prompt (1); | |
727 | } | |
728 | #endif | |
729 | } | |
730 | ||
731 | /* Get ready to modify the registers array. On machines which store | |
732 | individual registers, this doesn't need to do anything. On machines | |
733 | which store all the registers in one fell swoop, this makes sure | |
734 | that registers contains all the registers from the program being | |
735 | debugged. */ | |
736 | ||
737 | void | |
738 | monitor_prepare_to_store () | |
739 | { | |
740 | /* Do nothing, since we can store individual regs */ | |
741 | } | |
742 | ||
743 | void | |
744 | monitor_files_info () | |
745 | { | |
746 | printf ("\tAttached to %s at %d baud.\n", | |
747 | dev_name, baudrate); | |
748 | } | |
749 | ||
f1ca4cbc RS |
750 | /* |
751 | * monitor_write_inferior_memory -- Copy LEN bytes of data from debugger | |
752 | * memory at MYADDR to inferior's memory at MEMADDR. Returns length moved. | |
753 | */ | |
51d6a954 RS |
754 | int |
755 | monitor_write_inferior_memory (memaddr, myaddr, len) | |
756 | CORE_ADDR memaddr; | |
757 | unsigned char *myaddr; | |
758 | int len; | |
759 | { | |
760 | int i; | |
761 | char buf[10]; | |
762 | ||
e6fa5bd6 | 763 | debuglogs (1, "monitor_write_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len); |
f1ca4cbc RS |
764 | |
765 | for (i = 0; i < len; i++) { | |
766 | printf_monitor (ROMCMD(SET_MEM), memaddr + i, myaddr[i] ); | |
767 | if (*ROMDELIM(SET_MEM) != 0) { /* if there's a delimiter */ | |
768 | expect (ROMDELIM(SET_MEM), 1); | |
51d6a954 RS |
769 | expect (CMD_DELIM); |
770 | printf_monitor ("%x", myaddr[i]); | |
51d6a954 | 771 | } |
f1ca4cbc RS |
772 | /*** printf_monitor ("%x", myaddr[i]); ***/ |
773 | if (sr_get_debug() > 1) | |
774 | printf ("\nSet 0x%x to 0x%x\n", memaddr + i, myaddr[i]); | |
775 | if (*ROMDELIM(SET_MEM) != 0) { | |
776 | expect (CMD_DELIM); | |
777 | printf_monitor (CMD_END); | |
778 | } | |
779 | expect_prompt (1); | |
780 | } | |
51d6a954 RS |
781 | return len; |
782 | } | |
783 | ||
784 | /* | |
785 | * monitor_read_inferior_memory -- read LEN bytes from inferior memory | |
786 | * at MEMADDR. Put the result at debugger address MYADDR. Returns | |
787 | * length moved. | |
788 | */ | |
789 | int | |
790 | monitor_read_inferior_memory(memaddr, myaddr, len) | |
791 | CORE_ADDR memaddr; | |
792 | char *myaddr; | |
793 | int len; | |
794 | { | |
795 | int i, j; | |
796 | char buf[20]; | |
797 | ||
798 | /* Number of bytes read so far. */ | |
799 | int count; | |
800 | ||
801 | /* Starting address of this pass. */ | |
802 | unsigned long startaddr; | |
803 | ||
804 | /* Number of bytes to read in this pass. */ | |
805 | int len_this_pass; | |
806 | ||
e6fa5bd6 | 807 | debuglogs (1, "monitor_read_inferior_memory (memaddr=0x%x, myaddr=0x%x, len=%d)", memaddr, myaddr, len); |
51d6a954 RS |
808 | |
809 | /* Note that this code works correctly if startaddr is just less | |
810 | than UINT_MAX (well, really CORE_ADDR_MAX if there was such a | |
811 | thing). That is, something like | |
812 | monitor_read_bytes (CORE_ADDR_MAX - 4, foo, 4) | |
813 | works--it never adds len To memaddr and gets 0. */ | |
814 | /* However, something like | |
815 | monitor_read_bytes (CORE_ADDR_MAX - 3, foo, 4) | |
816 | doesn't need to work. Detect it and give up if there's an attempt | |
817 | to do that. */ | |
818 | if (((memaddr - 1) + len) < memaddr) { | |
819 | errno = EIO; | |
820 | return 0; | |
821 | } | |
822 | ||
823 | startaddr = memaddr; | |
824 | count = 0; | |
825 | while (count < len) { | |
826 | len_this_pass = 16; | |
827 | if ((startaddr % 16) != 0) | |
828 | len_this_pass -= startaddr % 16; | |
829 | if (len_this_pass > (len - count)) | |
830 | len_this_pass = (len - count); | |
e6fa5bd6 RS |
831 | |
832 | debuglogs (3, "Display %d bytes at %x", len_this_pass, startaddr); | |
51d6a954 RS |
833 | |
834 | for (i = 0; i < len_this_pass; i++) { | |
835 | printf_monitor (ROMCMD(GET_MEM), startaddr, startaddr); | |
836 | sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr); | |
51d6a954 RS |
837 | if (*ROMDELIM(GET_MEM) != 0) { /* if there's a delimiter */ |
838 | expect (ROMDELIM(GET_MEM), 1); | |
839 | } else { | |
840 | sprintf (buf, ROMCMD(GET_MEM), startaddr, startaddr); | |
841 | expect (buf,1); /* get the command echo */ | |
842 | get_hex_word(1); /* strip away the address */ | |
843 | } | |
844 | get_hex_byte (&myaddr[count++]); /* get the value at this address */ | |
845 | ||
846 | if (*ROMDELIM(GET_MEM) != 0) { | |
847 | printf_monitor (CMD_END); | |
848 | } | |
849 | expect_prompt (1); | |
850 | startaddr += 1; | |
851 | } | |
852 | } | |
853 | return len; | |
854 | } | |
855 | ||
856 | /* FIXME-someday! merge these two. */ | |
857 | int | |
858 | monitor_xfer_inferior_memory (memaddr, myaddr, len, write, target) | |
859 | CORE_ADDR memaddr; | |
860 | char *myaddr; | |
861 | int len; | |
862 | int write; | |
863 | struct target_ops *target; /* ignored */ | |
864 | { | |
865 | if (write) | |
866 | return monitor_write_inferior_memory (memaddr, myaddr, len); | |
867 | else | |
868 | return monitor_read_inferior_memory (memaddr, myaddr, len); | |
869 | } | |
870 | ||
871 | void | |
872 | monitor_kill (args, from_tty) | |
873 | char *args; | |
874 | int from_tty; | |
875 | { | |
876 | return; /* ignore attempts to kill target system */ | |
877 | } | |
878 | ||
879 | /* Clean up when a program exits. | |
880 | The program actually lives on in the remote processor's RAM, and may be | |
881 | run again without a download. Don't leave it full of breakpoint | |
882 | instructions. */ | |
883 | ||
884 | void | |
885 | monitor_mourn_inferior () | |
886 | { | |
887 | remove_breakpoints (); | |
888 | generic_mourn_inferior (); /* Do all the proper things now */ | |
889 | } | |
890 | ||
891 | #define MAX_MONITOR_BREAKPOINTS 16 | |
892 | ||
893 | extern int memory_breakpoint_size; | |
894 | static CORE_ADDR breakaddr[MAX_MONITOR_BREAKPOINTS] = {0}; | |
895 | ||
896 | /* | |
897 | * monitor_insert_breakpoint -- add a breakpoint | |
898 | */ | |
899 | int | |
900 | monitor_insert_breakpoint (addr, shadow) | |
901 | CORE_ADDR addr; | |
902 | char *shadow; | |
903 | { | |
904 | int i; | |
905 | ||
e6fa5bd6 | 906 | debuglogs (1, "monitor_insert_breakpoint() addr = 0x%x", addr); |
f1ca4cbc RS |
907 | |
908 | for (i = 0; i <= MAX_MONITOR_BREAKPOINTS; i++) { | |
909 | if (breakaddr[i] == 0) { | |
910 | breakaddr[i] = addr; | |
911 | if (sr_get_debug() > 4) | |
912 | printf ("Breakpoint at %x\n", addr); | |
913 | monitor_read_inferior_memory(addr, shadow, memory_breakpoint_size); | |
914 | printf_monitor(SET_BREAK_CMD, addr); | |
915 | expect_prompt(1); | |
916 | return 0; | |
917 | } | |
918 | } | |
919 | ||
51d6a954 RS |
920 | fprintf(stderr, "Too many breakpoints (> 16) for monitor\n"); |
921 | return 1; | |
922 | } | |
923 | ||
924 | /* | |
925 | * _remove_breakpoint -- Tell the monitor to remove a breakpoint | |
926 | */ | |
927 | int | |
928 | monitor_remove_breakpoint (addr, shadow) | |
929 | CORE_ADDR addr; | |
930 | char *shadow; | |
931 | { | |
932 | int i; | |
933 | ||
e6fa5bd6 | 934 | debuglogs (1, "monitor_remove_breakpoint() addr = 0x%x", addr); |
7804e5bc RS |
935 | |
936 | for (i = 0; i < MAX_MONITOR_BREAKPOINTS; i++) { | |
937 | if (breakaddr[i] == addr) { | |
938 | breakaddr[i] = 0; | |
939 | /* some monitors remove breakpoints based on the address */ | |
940 | if (CLR_BREAK_ADDR) | |
941 | printf_monitor(CLR_BREAK_CMD, addr); | |
942 | else | |
943 | printf_monitor(CLR_BREAK_CMD, i); | |
944 | expect_prompt(1); | |
945 | return 0; | |
946 | } | |
947 | } | |
51d6a954 RS |
948 | fprintf(stderr, "Can't find breakpoint associated with 0x%x\n", addr); |
949 | return 1; | |
950 | } | |
951 | ||
952 | /* Load a file. This is usually an srecord, which is ascii. No | |
953 | protocol, just sent line by line. */ | |
954 | ||
955 | #define DOWNLOAD_LINE_SIZE 100 | |
956 | void | |
957 | monitor_load (arg) | |
958 | char *arg; | |
959 | { | |
960 | FILE *download; | |
961 | char buf[DOWNLOAD_LINE_SIZE]; | |
962 | int i, bytes_read; | |
963 | ||
964 | if (sr_get_debug()) | |
965 | printf ("Loading %s to monitor\n", arg); | |
966 | ||
967 | download = fopen (arg, "r"); | |
968 | if (download == NULL) | |
969 | { | |
970 | error (sprintf (buf, "%s Does not exist", arg)); | |
971 | return; | |
972 | } | |
973 | ||
974 | printf_monitor (LOAD_CMD); | |
975 | /* expect ("Waiting for S-records from host... ", 1); */ | |
976 | ||
977 | while (!feof (download)) | |
978 | { | |
979 | bytes_read = fread (buf, sizeof (char), DOWNLOAD_LINE_SIZE, download); | |
980 | if (hashmark) | |
981 | { | |
982 | putchar ('.'); | |
983 | fflush (stdout); | |
984 | } | |
985 | ||
986 | if (SERIAL_WRITE(monitor_desc, buf, bytes_read)) { | |
987 | fprintf(stderr, "SERIAL_WRITE failed: (while downloading) %s\n", safe_strerror(errno)); | |
988 | break; | |
989 | } | |
990 | i = 0; | |
991 | while (i++ <=200000) {} ; /* Ugly HACK, probably needs flow control */ | |
992 | if (bytes_read < DOWNLOAD_LINE_SIZE) | |
993 | { | |
994 | if (!feof (download)) | |
995 | error ("Only read %d bytes\n", bytes_read); | |
996 | break; | |
997 | } | |
998 | } | |
999 | ||
1000 | if (hashmark) | |
1001 | { | |
1002 | putchar ('\n'); | |
1003 | } | |
1004 | if (!feof (download)) | |
1005 | error ("Never got EOF while downloading"); | |
1006 | fclose (download); | |
1007 | } | |
1008 | ||
7804e5bc RS |
1009 | /* |
1010 | * monitor_command -- put a command string, in args, out to MONITOR. | |
1011 | * Output from MONITOR is placed on the users terminal until the | |
1012 | * prompt is seen. FIXME: We read the charcters ourseleves here | |
1013 | * cause of a nasty echo. | |
1014 | */ | |
51d6a954 RS |
1015 | void |
1016 | monitor_command (args, fromtty) | |
1017 | char *args; | |
1018 | int fromtty; | |
1019 | { | |
7804e5bc RS |
1020 | |
1021 | char *p; | |
1022 | char c, cp; | |
1023 | p = PROMPT; | |
1024 | ||
e6fa5bd6 RS |
1025 | debuglogs (1, "monitor_command (args=%s)", args); |
1026 | ||
51d6a954 RS |
1027 | if (monitor_desc == NULL) |
1028 | error("monitor target not open."); | |
7804e5bc | 1029 | |
51d6a954 RS |
1030 | if (!args) |
1031 | error("Missing command."); | |
1032 | ||
7804e5bc RS |
1033 | printf_monitor ("%s\n", args); |
1034 | ||
51d6a954 RS |
1035 | expect_prompt(0); |
1036 | } | |
1037 | ||
1038 | /* | |
1039 | * _initialize_remote_monitors -- setup a few addtitional commands that | |
1040 | * are usually only used by monitors. | |
1041 | */ | |
1042 | void | |
1043 | _initialize_remote_monitors () | |
1044 | { | |
1045 | struct cmd_list_element *c; | |
1046 | ||
1047 | /* this sets the type of download protocol */ | |
1048 | c = add_set_cmd ("loadtype", no_class, var_string, (char *)&loadtype_str, | |
1049 | "Set the type of the remote load protocol.\n", &setlist); | |
1050 | c->function.sfunc = set_loadtype_command; | |
1051 | add_show_from_set (c, &showlist); | |
1052 | loadtype_str = savestring ("generic", 8); | |
1053 | ||
1054 | add_show_from_set (add_set_cmd ("hash", no_class, var_boolean, | |
1055 | (char *)&hashmark, | |
1056 | "Set display of activity while downloading a file.\n\ | |
1057 | When enabled, a period \'.\' is displayed.", | |
1058 | &setlist), | |
1059 | &showlist); | |
1060 | ||
1061 | /* generic monitor command */ | |
1062 | add_com ("monitor", class_obscure, monitor_command, | |
1063 | "Send a command to the debug monitor."); | |
1064 | } |