Commit | Line | Data |
---|---|---|
51d6a954 | 1 | /* Remote debugging interface for boot monitors, for GDB. |
431b7d5f | 2 | Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc. |
51d6a954 RS |
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, | |
431b7d5f | 29 | which in turn talks to the target board. */ |
51d6a954 RS |
30 | |
31 | #include "defs.h" | |
32 | #include "gdbcore.h" | |
33 | #include "target.h" | |
34 | #include "wait.h" | |
85c613aa C |
35 | #ifdef __STDC__ |
36 | #include <stdarg.h> | |
37 | #else | |
51d6a954 | 38 | #include <varargs.h> |
85c613aa | 39 | #endif |
51d6a954 RS |
40 | #include <signal.h> |
41 | #include <string.h> | |
42 | #include <sys/types.h> | |
43 | #include "command.h" | |
44 | #include "serial.h" | |
45 | #include "monitor.h" | |
1265e2d8 SG |
46 | #include "gdbcmd.h" |
47 | #include "inferior.h" | |
a706069f | 48 | #include "regex.h" |
51d6a954 | 49 | |
eba08643 C |
50 | static int readchar PARAMS ((int timeout)); |
51 | ||
8f078234 | 52 | static void monitor_command PARAMS ((char *args, int fromtty)); |
774e5d7f | 53 | static void monitor_load_srec PARAMS ((char *args)); |
431b7d5f | 54 | |
431b7d5f SS |
55 | static int monitor_make_srec PARAMS ((char *buffer, int type, |
56 | CORE_ADDR memaddr, | |
57 | unsigned char *myaddr, int len)); | |
58 | ||
1265e2d8 SG |
59 | static void monitor_fetch_register PARAMS ((int regno)); |
60 | static void monitor_store_register PARAMS ((int regno)); | |
61 | ||
a706069f SG |
62 | static void monitor_close PARAMS ((int quitting)); |
63 | static void monitor_detach PARAMS ((char *args, int from_tty)); | |
64 | static void monitor_resume PARAMS ((int pid, int step, enum target_signal sig)); | |
eba08643 C |
65 | static void monitor_interrupt PARAMS ((int signo)); |
66 | static void monitor_interrupt_twice PARAMS ((int signo)); | |
67 | static void monitor_interrupt_query PARAMS ((void)); | |
68 | static void monitor_wait_cleanup PARAMS ((int old_timeout)); | |
69 | ||
a706069f SG |
70 | static int monitor_wait PARAMS ((int pid, struct target_waitstatus *status)); |
71 | static void monitor_fetch_registers PARAMS ((int regno)); | |
72 | static void monitor_store_registers PARAMS ((int regno)); | |
73 | static void monitor_prepare_to_store PARAMS ((void)); | |
74 | static int monitor_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len, int write, struct target_ops *target)); | |
75 | static void monitor_files_info PARAMS ((struct target_ops *ops)); | |
76 | static int monitor_insert_breakpoint PARAMS ((CORE_ADDR addr, char *shadow)); | |
77 | static int monitor_remove_breakpoint PARAMS ((CORE_ADDR addr, char *shadow)); | |
78 | static void monitor_kill PARAMS ((void)); | |
79 | static void monitor_load PARAMS ((char *file, int from_tty)); | |
80 | static void monitor_mourn_inferior PARAMS ((void)); | |
81 | static void monitor_stop PARAMS ((void)); | |
82 | ||
8f078234 | 83 | static int from_hex PARAMS ((int a)); |
1265e2d8 | 84 | static unsigned long get_hex_word PARAMS ((void)); |
06b8f5e4 | 85 | |
8f078234 | 86 | static struct monitor_ops *current_monitor; |
431b7d5f | 87 | |
774e5d7f | 88 | static int hashmark; /* flag set by "set hash" */ |
51d6a954 | 89 | |
1b552670 | 90 | static int timeout = 30; |
431b7d5f | 91 | |
eba08643 C |
92 | static void (*ofunc)(); /* Old SIGINT signal handler */ |
93 | ||
431b7d5f SS |
94 | /* Descriptor for I/O to remote machine. Initialize it to NULL so |
95 | that monitor_open knows that we don't have a file open when the | |
96 | program starts. */ | |
97 | ||
1265e2d8 | 98 | static serial_t monitor_desc = NULL; |
431b7d5f | 99 | |
a706069f SG |
100 | /* Pointer to regexp pattern matching data */ |
101 | ||
102 | static struct re_pattern_buffer register_pattern; | |
103 | ||
104 | /* Element 0 points to start of register name, and element 1 points to the | |
105 | start of the register value. */ | |
106 | ||
107 | static struct re_registers register_strings; | |
108 | ||
109 | static char fastmap[256]; | |
110 | ||
111 | static int dump_reg_flag; /* Non-zero means do a dump_registers cmd when | |
112 | monitor_wait wakes up. */ | |
113 | ||
eba08643 C |
114 | /* monitor_printf_noecho -- Send data to monitor, but don't expect an echo. |
115 | Works just like printf. */ | |
116 | ||
117 | void | |
85c613aa C |
118 | #ifdef __STDC__ |
119 | monitor_printf_noecho (char *pattern, ...) | |
120 | #else | |
eba08643 C |
121 | monitor_printf_noecho (va_alist) |
122 | va_dcl | |
85c613aa | 123 | #endif |
eba08643 C |
124 | { |
125 | va_list args; | |
eba08643 C |
126 | char sndbuf[2000]; |
127 | int len; | |
128 | ||
85c613aa C |
129 | #if __STDC__ |
130 | va_start (args, pattern); | |
131 | #else | |
132 | char *pattern; | |
eba08643 | 133 | va_start (args); |
eba08643 | 134 | pattern = va_arg (args, char *); |
85c613aa | 135 | #endif |
eba08643 C |
136 | |
137 | vsprintf (sndbuf, pattern, args); | |
138 | ||
139 | if (remote_debug > 0) | |
140 | fputs_unfiltered (sndbuf, gdb_stderr); | |
141 | ||
142 | len = strlen (sndbuf); | |
143 | ||
144 | if (len + 1 > sizeof sndbuf) | |
145 | abort (); | |
146 | ||
147 | if (SERIAL_WRITE(monitor_desc, sndbuf, len)) | |
148 | fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno)); | |
149 | } | |
150 | ||
151 | /* monitor_printf -- Send data to monitor and check the echo. Works just like | |
152 | printf. */ | |
431b7d5f | 153 | |
774e5d7f | 154 | void |
85c613aa C |
155 | #ifdef __STDC__ |
156 | monitor_printf (char *pattern, ...) | |
157 | #else | |
774e5d7f | 158 | monitor_printf (va_alist) |
51d6a954 | 159 | va_dcl |
85c613aa | 160 | #endif |
51d6a954 RS |
161 | { |
162 | va_list args; | |
eba08643 | 163 | char sndbuf[2000]; |
774e5d7f | 164 | int len; |
eba08643 | 165 | int i, c; |
51d6a954 | 166 | |
85c613aa C |
167 | #ifdef __STDC__ |
168 | va_start (args, pattern); | |
169 | #else | |
170 | char *pattern; | |
431b7d5f | 171 | va_start (args); |
431b7d5f | 172 | pattern = va_arg (args, char *); |
85c613aa | 173 | #endif |
51d6a954 | 174 | |
eba08643 | 175 | vsprintf (sndbuf, pattern, args); |
51d6a954 | 176 | |
1265e2d8 | 177 | if (remote_debug > 0) |
eba08643 | 178 | fputs_unfiltered (sndbuf, gdb_stderr); |
51d6a954 | 179 | |
eba08643 | 180 | len = strlen (sndbuf); |
431b7d5f | 181 | |
eba08643 | 182 | if (len + 1 > sizeof sndbuf) |
774e5d7f | 183 | abort (); |
431b7d5f | 184 | |
eba08643 | 185 | if (SERIAL_WRITE(monitor_desc, sndbuf, len)) |
774e5d7f | 186 | fprintf_unfiltered (stderr, "SERIAL_WRITE failed: %s\n", safe_strerror (errno)); |
eba08643 C |
187 | |
188 | for (i = 0; i < len; i++) | |
189 | { | |
190 | trycr: | |
191 | c = readchar (timeout); | |
192 | ||
193 | if (c != sndbuf[i]) | |
194 | { | |
195 | #if 0 | |
196 | if (sndbuf[i] == '\r' | |
197 | && c == '\n') | |
198 | goto trycr; | |
199 | #endif | |
200 | error ("monitor_printf: Bad echo. Sent: \"%s\", Got: \"%.*s%c\".", | |
201 | sndbuf, i, sndbuf, c); | |
202 | } | |
203 | } | |
e6fa5bd6 RS |
204 | } |
205 | ||
431b7d5f SS |
206 | /* Read a character from the remote system, doing all the fancy |
207 | timeout stuff. */ | |
208 | ||
51d6a954 | 209 | static int |
431b7d5f | 210 | readchar (timeout) |
51d6a954 RS |
211 | int timeout; |
212 | { | |
213 | int c; | |
214 | ||
431b7d5f | 215 | c = SERIAL_READCHAR (monitor_desc, timeout); |
51d6a954 | 216 | |
1265e2d8 | 217 | if (remote_debug > 0) |
a706069f | 218 | fputc_unfiltered (c, gdb_stderr); |
51d6a954 RS |
219 | |
220 | if (c >= 0) | |
221 | return c & 0x7f; | |
222 | ||
431b7d5f | 223 | if (c == SERIAL_TIMEOUT) |
a706069f SG |
224 | error ("Timeout reading from remote system."); |
225 | ||
431b7d5f | 226 | perror_with_name ("remote-monitor"); |
51d6a954 RS |
227 | } |
228 | ||
1265e2d8 | 229 | /* Scan input from the remote system, until STRING is found. If BUF is non- |
a706069f SG |
230 | zero, then collect input until we have collected either STRING or BUFLEN-1 |
231 | chars. In either case we terminate BUF with a 0. If input overflows BUF | |
232 | because STRING can't be found, return -1, else return number of chars in BUF | |
233 | (minus the terminating NUL). Note that in the non-overflow case, STRING | |
234 | will be at the end of BUF. */ | |
431b7d5f | 235 | |
774e5d7f SS |
236 | int |
237 | monitor_expect (string, buf, buflen) | |
51d6a954 | 238 | char *string; |
1265e2d8 SG |
239 | char *buf; |
240 | int buflen; | |
51d6a954 RS |
241 | { |
242 | char *p = string; | |
1265e2d8 | 243 | int obuflen = buflen; |
51d6a954 RS |
244 | int c; |
245 | ||
51d6a954 | 246 | immediate_quit = 1; |
431b7d5f SS |
247 | while (1) |
248 | { | |
1265e2d8 | 249 | if (buf) |
431b7d5f | 250 | { |
a706069f | 251 | if (buflen < 2) |
431b7d5f | 252 | { |
a706069f | 253 | *buf = '\000'; |
431b7d5f | 254 | immediate_quit = 0; |
1265e2d8 | 255 | return -1; |
431b7d5f | 256 | } |
1265e2d8 SG |
257 | |
258 | c = readchar (timeout); | |
259 | *buf++ = c; | |
260 | buflen--; | |
431b7d5f SS |
261 | } |
262 | else | |
1265e2d8 SG |
263 | c = readchar (timeout); |
264 | ||
265 | if (c == *p++) | |
431b7d5f | 266 | { |
1265e2d8 | 267 | if (*p == '\0') |
431b7d5f | 268 | { |
1265e2d8 SG |
269 | immediate_quit = 0; |
270 | ||
a706069f SG |
271 | if (buf) |
272 | { | |
273 | *buf++ = '\000'; | |
274 | return obuflen - buflen; | |
275 | } | |
276 | else | |
277 | return 0; | |
431b7d5f | 278 | } |
1265e2d8 SG |
279 | } |
280 | else | |
281 | { | |
431b7d5f | 282 | p = string; |
1265e2d8 SG |
283 | if (c == *p) |
284 | p++; | |
431b7d5f | 285 | } |
51d6a954 | 286 | } |
51d6a954 RS |
287 | } |
288 | ||
289 | /* Keep discarding input until we see the MONITOR prompt. | |
290 | ||
291 | The convention for dealing with the prompt is that you | |
292 | o give your command | |
293 | o *then* wait for the prompt. | |
294 | ||
295 | Thus the last thing that a procedure does with the serial line | |
774e5d7f | 296 | will be an monitor_expect_prompt(). Exception: monitor_resume does not |
51d6a954 RS |
297 | wait for the prompt, because the terminal is being handed over |
298 | to the inferior. However, the next thing which happens after that | |
299 | is a monitor_wait which does wait for the prompt. | |
300 | Note that this includes abnormal exit, e.g. error(). This is | |
301 | necessary to prevent getting into states from which we can't | |
302 | recover. */ | |
431b7d5f | 303 | |
774e5d7f SS |
304 | int |
305 | monitor_expect_prompt (buf, buflen) | |
1265e2d8 SG |
306 | char *buf; |
307 | int buflen; | |
51d6a954 | 308 | { |
774e5d7f | 309 | return monitor_expect (PROMPT, buf, buflen); |
51d6a954 RS |
310 | } |
311 | ||
431b7d5f SS |
312 | /* Get N 32-bit words from remote, each preceded by a space, and put |
313 | them in registers starting at REGNO. */ | |
314 | ||
1265e2d8 | 315 | static unsigned long |
51d6a954 RS |
316 | get_hex_word () |
317 | { | |
1265e2d8 | 318 | unsigned long val; |
51d6a954 | 319 | int i; |
1265e2d8 | 320 | int ch; |
51d6a954 | 321 | |
1265e2d8 SG |
322 | do |
323 | ch = readchar (timeout); | |
324 | while (isspace(ch)); | |
cf51c601 | 325 | |
1265e2d8 SG |
326 | val = from_hex (ch); |
327 | ||
328 | for (i = 7; i >= 1; i--) | |
431b7d5f | 329 | { |
1265e2d8 SG |
330 | ch = readchar (timeout); |
331 | if (!isxdigit (ch)) | |
332 | break; | |
333 | val = (val << 4) | from_hex (ch); | |
431b7d5f | 334 | } |
51d6a954 RS |
335 | |
336 | return val; | |
337 | } | |
338 | ||
431b7d5f SS |
339 | /* Open a connection to a remote debugger. NAME is the filename used |
340 | for communication. */ | |
341 | ||
1265e2d8 | 342 | static char *dev_name; |
8f078234 | 343 | static struct target_ops *targ_ops; |
51d6a954 RS |
344 | |
345 | void | |
1265e2d8 | 346 | monitor_open (args, mon_ops, from_tty) |
51d6a954 | 347 | char *args; |
1265e2d8 | 348 | struct monitor_ops *mon_ops; |
51d6a954 RS |
349 | int from_tty; |
350 | { | |
1265e2d8 SG |
351 | char *name; |
352 | int i; | |
a706069f SG |
353 | char **p; |
354 | ||
355 | if (mon_ops->magic != MONITOR_OPS_MAGIC) | |
356 | error ("Magic number of monitor_ops struct wrong."); | |
1265e2d8 SG |
357 | |
358 | targ_ops = mon_ops->target; | |
359 | name = targ_ops->to_shortname; | |
51d6a954 | 360 | |
1265e2d8 | 361 | if (!args) |
51d6a954 RS |
362 | error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\ |
363 | `target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name); | |
364 | ||
1265e2d8 SG |
365 | target_preopen (from_tty); |
366 | ||
a706069f SG |
367 | /* Setup pattern for register dump */ |
368 | ||
369 | if (mon_ops->register_pattern) | |
370 | { | |
371 | int tmp; | |
372 | char *val; | |
373 | ||
374 | register_pattern.fastmap = fastmap; | |
375 | tmp = re_set_syntax (RE_SYNTAX_EMACS); | |
376 | val = re_compile_pattern (mon_ops->register_pattern, | |
377 | strlen (mon_ops->register_pattern), | |
378 | ®ister_pattern); | |
379 | re_set_syntax (tmp); | |
380 | if (val) | |
381 | error ("Can't compiler register pattern string: %s!", val); | |
382 | re_compile_fastmap (®ister_pattern); | |
383 | } | |
384 | ||
1265e2d8 SG |
385 | unpush_target (targ_ops); |
386 | ||
387 | if (dev_name) | |
388 | free (dev_name); | |
389 | dev_name = strsave (args); | |
51d6a954 | 390 | |
431b7d5f | 391 | monitor_desc = SERIAL_OPEN (dev_name); |
51d6a954 | 392 | |
1265e2d8 | 393 | if (!monitor_desc) |
431b7d5f SS |
394 | perror_with_name (dev_name); |
395 | ||
396 | if (baud_rate != -1) | |
397 | { | |
398 | if (SERIAL_SETBAUDRATE (monitor_desc, baud_rate)) | |
399 | { | |
400 | SERIAL_CLOSE (monitor_desc); | |
1265e2d8 | 401 | perror_with_name (dev_name); |
431b7d5f | 402 | } |
51d6a954 | 403 | } |
7a1330f7 | 404 | |
431b7d5f | 405 | SERIAL_RAW (monitor_desc); |
51d6a954 | 406 | |
1265e2d8 SG |
407 | SERIAL_FLUSH_INPUT (monitor_desc); |
408 | ||
cf51c601 | 409 | /* some systems only work with 2 stop bits */ |
1265e2d8 SG |
410 | |
411 | SERIAL_SETSTOPBITS (monitor_desc, mon_ops->stopbits); | |
412 | ||
413 | current_monitor = mon_ops; | |
51d6a954 | 414 | |
a706069f SG |
415 | /* See if we can wake up the monitor. First, try sending a stop sequence, |
416 | then send the init strings. Last, remove all breakpoints. */ | |
417 | ||
eba08643 C |
418 | if (current_monitor->stop) |
419 | { | |
420 | monitor_stop (); | |
421 | monitor_expect_prompt (NULL, 0); | |
422 | } | |
431b7d5f | 423 | |
1265e2d8 | 424 | /* wake up the monitor and see if it's alive */ |
a706069f SG |
425 | for (p = mon_ops->init; *p != NULL; p++) |
426 | { | |
774e5d7f SS |
427 | monitor_printf (*p); |
428 | monitor_expect_prompt (NULL, 0); | |
a706069f SG |
429 | } |
430 | ||
eba08643 C |
431 | SERIAL_FLUSH_INPUT (monitor_desc); |
432 | ||
a706069f | 433 | /* Remove all breakpoints */ |
431b7d5f | 434 | |
a706069f SG |
435 | if (mon_ops->clr_all_break) |
436 | { | |
774e5d7f SS |
437 | monitor_printf (mon_ops->clr_all_break); |
438 | monitor_expect_prompt (NULL, 0); | |
a706069f | 439 | } |
1265e2d8 | 440 | |
51d6a954 | 441 | if (from_tty) |
1265e2d8 SG |
442 | printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name); |
443 | ||
444 | push_target (targ_ops); | |
445 | ||
446 | inferior_pid = 42000; /* Make run command think we are busy... */ | |
447 | ||
eba08643 C |
448 | /* Give monitor_wait something to read */ |
449 | ||
450 | monitor_printf (current_monitor->line_term); | |
1265e2d8 SG |
451 | |
452 | start_remote (); | |
51d6a954 RS |
453 | } |
454 | ||
431b7d5f SS |
455 | /* Close out all files and local state before this target loses |
456 | control. */ | |
51d6a954 | 457 | |
a706069f | 458 | static void |
51d6a954 RS |
459 | monitor_close (quitting) |
460 | int quitting; | |
461 | { | |
1265e2d8 SG |
462 | if (monitor_desc) |
463 | SERIAL_CLOSE (monitor_desc); | |
51d6a954 | 464 | monitor_desc = NULL; |
51d6a954 RS |
465 | } |
466 | ||
431b7d5f SS |
467 | /* Terminate the open connection to the remote debugger. Use this |
468 | when you want to detach and do something else with your gdb. */ | |
469 | ||
a706069f | 470 | static void |
1265e2d8 SG |
471 | monitor_detach (args, from_tty) |
472 | char *args; | |
51d6a954 RS |
473 | int from_tty; |
474 | { | |
431b7d5f | 475 | pop_target (); /* calls monitor_close to do the real work */ |
51d6a954 | 476 | if (from_tty) |
1265e2d8 | 477 | printf_unfiltered ("Ending remote %s debugging\n", target_shortname); |
51d6a954 | 478 | } |
7804e5bc | 479 | |
a706069f SG |
480 | /* Convert VALSTR into the target byte-ordered value of REGNO and store it. */ |
481 | ||
482 | char * | |
483 | monitor_supply_register (regno, valstr) | |
484 | int regno; | |
485 | char *valstr; | |
486 | { | |
487 | unsigned LONGEST val; | |
488 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; | |
489 | char *p; | |
490 | ||
491 | val = strtoul (valstr, &p, 16); | |
492 | ||
493 | if (val == 0 && valstr == p) | |
494 | error ("monitor_supply_register (%d): bad value from monitor: %s.", | |
495 | regno, valstr); | |
496 | ||
497 | /* supply register stores in target byte order, so swap here */ | |
498 | ||
499 | store_unsigned_integer (regbuf, REGISTER_RAW_SIZE (regno), val); | |
500 | ||
501 | supply_register (regno, regbuf); | |
502 | ||
503 | return p; | |
504 | } | |
505 | ||
431b7d5f SS |
506 | /* Tell the remote machine to resume. */ |
507 | ||
a706069f | 508 | static void |
51d6a954 RS |
509 | monitor_resume (pid, step, sig) |
510 | int pid, step; | |
511 | enum target_signal sig; | |
512 | { | |
431b7d5f | 513 | if (step) |
774e5d7f | 514 | monitor_printf (STEP_CMD); |
431b7d5f | 515 | else |
a706069f | 516 | { |
774e5d7f | 517 | monitor_printf (CONT_CMD); |
a706069f SG |
518 | if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT) |
519 | dump_reg_flag = 1; | |
520 | } | |
521 | } | |
522 | ||
523 | /* Parse the output of a register dump command. A monitor specific regexp is | |
524 | used to extract individual register descriptions of the form REG=VAL. Each | |
525 | description is split up into a name and a value string which are passed down | |
526 | to monitor specific code. */ | |
527 | ||
528 | static char * | |
529 | parse_register_dump (buf, len) | |
530 | char *buf; | |
531 | int len; | |
532 | { | |
533 | while (1) | |
534 | { | |
535 | int regnamelen, vallen; | |
536 | char *regname, *val; | |
537 | ||
538 | if (re_search (®ister_pattern, buf, len, 0, len, | |
539 | ®ister_strings) == -1) | |
540 | break; | |
541 | ||
542 | regnamelen = register_strings.end[1] - register_strings.start[1]; | |
543 | regname = buf + register_strings.start[1]; | |
544 | vallen = register_strings.end[2] - register_strings.start[2]; | |
545 | val = buf + register_strings.start[2]; | |
546 | ||
547 | current_monitor->supply_register (regname, regnamelen, val, vallen); | |
548 | ||
549 | buf += register_strings.end[0]; | |
550 | len -= register_strings.end[0]; | |
551 | } | |
51d6a954 RS |
552 | } |
553 | ||
eba08643 C |
554 | /* Send ^C to target to halt it. Target will respond, and send us a |
555 | packet. */ | |
556 | ||
557 | static void | |
558 | monitor_interrupt (signo) | |
559 | int signo; | |
560 | { | |
561 | /* If this doesn't work, try more severe steps. */ | |
562 | signal (signo, monitor_interrupt_twice); | |
563 | ||
564 | if (remote_debug) | |
565 | printf_unfiltered ("monitor_interrupt called\n"); | |
566 | ||
567 | target_stop (); | |
568 | } | |
569 | ||
570 | /* The user typed ^C twice. */ | |
571 | ||
572 | static void | |
573 | monitor_interrupt_twice (signo) | |
574 | int signo; | |
575 | { | |
576 | signal (signo, ofunc); | |
577 | ||
578 | monitor_interrupt_query (); | |
579 | ||
580 | signal (signo, monitor_interrupt); | |
581 | } | |
582 | ||
583 | /* Ask the user what to do when an interrupt is received. */ | |
584 | ||
585 | static void | |
586 | monitor_interrupt_query () | |
587 | { | |
588 | target_terminal_ours (); | |
589 | ||
590 | if (query ("Interrupted while waiting for the program.\n\ | |
591 | Give up (and stop debugging it)? ")) | |
592 | { | |
593 | target_mourn_inferior (); | |
594 | return_to_top_level (RETURN_QUIT); | |
595 | } | |
596 | ||
597 | target_terminal_inferior (); | |
598 | } | |
599 | ||
600 | static void | |
601 | monitor_wait_cleanup (old_timeout) | |
602 | int old_timeout; | |
603 | { | |
604 | timeout = old_timeout; | |
605 | signal (SIGINT, ofunc); | |
606 | } | |
607 | ||
431b7d5f SS |
608 | /* Wait until the remote machine stops, then return, storing status in |
609 | status just as `wait' would. */ | |
610 | ||
a706069f | 611 | static int |
51d6a954 RS |
612 | monitor_wait (pid, status) |
613 | int pid; | |
614 | struct target_waitstatus *status; | |
615 | { | |
616 | int old_timeout = timeout; | |
774e5d7f | 617 | char buf[1024]; |
a706069f | 618 | int resp_len; |
eba08643 | 619 | struct cleanup *old_chain; |
51d6a954 RS |
620 | |
621 | status->kind = TARGET_WAITKIND_EXITED; | |
622 | status->value.integer = 0; | |
623 | ||
eba08643 C |
624 | old_chain = make_cleanup (monitor_wait_cleanup, old_timeout); |
625 | ||
1265e2d8 | 626 | timeout = -1; /* Don't time out -- user program is running. */ |
51d6a954 | 627 | |
eba08643 C |
628 | ofunc = (void (*)()) signal (SIGINT, monitor_interrupt); |
629 | ||
a706069f SG |
630 | do |
631 | { | |
774e5d7f | 632 | resp_len = monitor_expect_prompt (buf, sizeof (buf)); |
51d6a954 | 633 | |
a706069f SG |
634 | if (resp_len <= 0) |
635 | fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf); | |
636 | } | |
637 | while (resp_len < 0); | |
51d6a954 | 638 | |
eba08643 C |
639 | signal (SIGINT, ofunc); |
640 | ||
51d6a954 RS |
641 | timeout = old_timeout; |
642 | ||
a706069f SG |
643 | if (dump_reg_flag && current_monitor->dump_registers) |
644 | { | |
645 | dump_reg_flag = 0; | |
646 | ||
774e5d7f SS |
647 | monitor_printf (current_monitor->dump_registers); |
648 | resp_len = monitor_expect_prompt (buf, sizeof (buf)); | |
a706069f SG |
649 | } |
650 | ||
651 | if (current_monitor->register_pattern) | |
652 | parse_register_dump (buf, resp_len); | |
653 | ||
654 | status->kind = TARGET_WAITKIND_STOPPED; | |
655 | status->value.sig = TARGET_SIGNAL_TRAP; | |
656 | ||
eba08643 C |
657 | discard_cleanups (old_chain); |
658 | ||
1265e2d8 | 659 | return inferior_pid; |
51d6a954 RS |
660 | } |
661 | ||
1265e2d8 SG |
662 | /* Fetch register REGNO, or all registers if REGNO is -1. Returns |
663 | errno value. */ | |
51d6a954 | 664 | |
1265e2d8 SG |
665 | static void |
666 | monitor_fetch_register (regno) | |
667 | int regno; | |
668 | { | |
1265e2d8 | 669 | char *name; |
774e5d7f | 670 | static char zerobuf[MAX_REGISTER_RAW_SIZE] = {0}; |
eba08643 C |
671 | char regbuf[MAX_REGISTER_RAW_SIZE * 2 + 1]; |
672 | int i; | |
51d6a954 | 673 | |
8f078234 | 674 | name = REGNAMES (regno); |
f1ca4cbc | 675 | |
1265e2d8 | 676 | if (!name) |
774e5d7f SS |
677 | { |
678 | supply_register (regno, zerobuf); | |
679 | return; | |
680 | } | |
51d6a954 | 681 | |
1265e2d8 | 682 | /* send the register examine command */ |
431b7d5f | 683 | |
774e5d7f | 684 | monitor_printf (current_monitor->getreg.cmd, name); |
1b552670 | 685 | |
eba08643 C |
686 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for |
687 | the register value. Otherwise, we just start searching from the start of | |
688 | the buf. */ | |
689 | ||
690 | if (current_monitor->getreg.resp_delim) | |
691 | monitor_expect (current_monitor->getreg.resp_delim, NULL, 0); | |
692 | ||
693 | /* Now, read the appropriate number of hex digits for this register, skipping | |
694 | spaces. */ | |
695 | ||
696 | for (i = 0; i < REGISTER_RAW_SIZE (regno) * 2; i++) | |
697 | { | |
698 | int c; | |
699 | ||
700 | while (1) | |
701 | { | |
702 | c = readchar (timeout); | |
703 | if (isxdigit (c)) | |
704 | break; | |
705 | if (c == ' ') | |
706 | continue; | |
707 | ||
708 | error ("monitor_fetch_register (%d): bad response from monitor: %.*s%c.", | |
709 | regno, i, regbuf, c); | |
710 | } | |
711 | ||
712 | regbuf[i] = c; | |
713 | } | |
714 | ||
715 | regbuf[i] = '\000'; /* terminate the number */ | |
716 | ||
1265e2d8 SG |
717 | /* If TERM is present, we wait for that to show up. Also, (if TERM is |
718 | present), we will send TERM_CMD if that is present. In any case, we collect | |
719 | all of the output into buf, and then wait for the normal prompt. */ | |
1b552670 | 720 | |
1265e2d8 | 721 | if (current_monitor->getreg.term) |
431b7d5f | 722 | { |
eba08643 | 723 | monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */ |
1265e2d8 SG |
724 | |
725 | if (current_monitor->getreg.term_cmd) | |
431b7d5f | 726 | { |
eba08643 | 727 | monitor_printf (current_monitor->getreg.term_cmd); |
774e5d7f | 728 | monitor_expect_prompt (NULL, 0); |
431b7d5f SS |
729 | } |
730 | } | |
731 | else | |
eba08643 | 732 | monitor_expect_prompt (NULL, 0); /* get response */ |
51d6a954 | 733 | |
eba08643 | 734 | monitor_supply_register (regno, regbuf); |
51d6a954 RS |
735 | } |
736 | ||
1265e2d8 | 737 | /* Read the remote registers into the block regs. */ |
431b7d5f | 738 | |
a706069f | 739 | static void |
1265e2d8 SG |
740 | monitor_fetch_registers (regno) |
741 | int regno; | |
51d6a954 | 742 | { |
1265e2d8 | 743 | if (regno >= 0) |
431b7d5f | 744 | { |
1265e2d8 SG |
745 | monitor_fetch_register (regno); |
746 | return; | |
1b552670 | 747 | } |
51d6a954 | 748 | |
1265e2d8 SG |
749 | for (regno = 0; regno < NUM_REGS; regno++) |
750 | monitor_fetch_register (regno); | |
51d6a954 RS |
751 | } |
752 | ||
431b7d5f SS |
753 | /* Store register REGNO, or all if REGNO == 0. Return errno value. */ |
754 | ||
1265e2d8 | 755 | static void |
51d6a954 RS |
756 | monitor_store_register (regno) |
757 | int regno; | |
758 | { | |
759 | char *name; | |
1265e2d8 | 760 | unsigned LONGEST val; |
f1ca4cbc | 761 | |
8f078234 | 762 | name = REGNAMES (regno); |
1265e2d8 SG |
763 | if (!name) |
764 | return; | |
51d6a954 | 765 | |
1265e2d8 | 766 | val = read_register (regno); |
51d6a954 | 767 | |
1265e2d8 SG |
768 | /* send the register deposit command */ |
769 | ||
774e5d7f | 770 | monitor_printf (current_monitor->setreg.cmd, name, val); |
1265e2d8 SG |
771 | |
772 | /* It's possible that there are actually some monitors out there that will | |
773 | prompt you when you set a register. In that case, you may need to add some | |
774 | code here to deal with TERM and TERM_CMD (see monitor_fetch_register to get | |
775 | an idea of what's needed...) */ | |
776 | ||
774e5d7f | 777 | monitor_expect_prompt (NULL, 0); |
1265e2d8 SG |
778 | } |
779 | ||
780 | /* Store the remote registers. */ | |
781 | ||
a706069f | 782 | static void |
1265e2d8 SG |
783 | monitor_store_registers (regno) |
784 | int regno; | |
785 | { | |
786 | if (regno >= 0) | |
431b7d5f | 787 | { |
1265e2d8 SG |
788 | monitor_store_register (regno); |
789 | return; | |
51d6a954 | 790 | } |
431b7d5f | 791 | |
1265e2d8 SG |
792 | for (regno = 0; regno < NUM_REGS; regno++) |
793 | monitor_store_register (regno); | |
51d6a954 RS |
794 | } |
795 | ||
796 | /* Get ready to modify the registers array. On machines which store | |
797 | individual registers, this doesn't need to do anything. On machines | |
798 | which store all the registers in one fell swoop, this makes sure | |
799 | that registers contains all the registers from the program being | |
800 | debugged. */ | |
801 | ||
a706069f | 802 | static void |
51d6a954 RS |
803 | monitor_prepare_to_store () |
804 | { | |
805 | /* Do nothing, since we can store individual regs */ | |
806 | } | |
807 | ||
a706069f SG |
808 | static void |
809 | monitor_files_info (ops) | |
810 | struct target_ops *ops; | |
51d6a954 | 811 | { |
1265e2d8 | 812 | printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate); |
51d6a954 RS |
813 | } |
814 | ||
1265e2d8 SG |
815 | static int |
816 | monitor_write_memory (memaddr, myaddr, len) | |
51d6a954 RS |
817 | CORE_ADDR memaddr; |
818 | unsigned char *myaddr; | |
819 | int len; | |
820 | { | |
a706069f SG |
821 | unsigned LONGEST val; |
822 | char *cmd; | |
823 | int i; | |
1b552670 | 824 | |
a706069f | 825 | /* Use memory fill command for leading 0 bytes. */ |
1b552670 | 826 | |
a706069f SG |
827 | if (current_monitor->fill) |
828 | { | |
829 | for (i = 0; i < len; i++) | |
830 | if (myaddr[i] != 0) | |
831 | break; | |
832 | ||
833 | if (i > 4) /* More than 4 zeros is worth doing */ | |
834 | { | |
835 | if (current_monitor->flags & MO_FILL_USES_ADDR) | |
774e5d7f | 836 | monitor_printf (current_monitor->fill, memaddr, memaddr + i, 0); |
a706069f | 837 | else |
774e5d7f | 838 | monitor_printf (current_monitor->fill, memaddr, i, 0); |
a706069f | 839 | |
774e5d7f | 840 | monitor_expect_prompt (NULL, 0); |
a706069f SG |
841 | |
842 | return i; | |
843 | } | |
844 | } | |
845 | ||
846 | if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll) | |
847 | { | |
848 | len = 8; | |
849 | cmd = current_monitor->setmem.cmdll; | |
850 | } | |
851 | else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl) | |
852 | { | |
853 | len = 4; | |
854 | cmd = current_monitor->setmem.cmdl; | |
855 | } | |
856 | else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw) | |
857 | { | |
858 | len = 2; | |
859 | cmd = current_monitor->setmem.cmdw; | |
860 | } | |
861 | else | |
862 | { | |
863 | len = 1; | |
864 | cmd = current_monitor->setmem.cmdb; | |
865 | } | |
866 | ||
867 | val = extract_unsigned_integer (myaddr, len); | |
868 | ||
774e5d7f | 869 | monitor_printf (cmd, memaddr, val); |
1265e2d8 | 870 | |
774e5d7f | 871 | monitor_expect_prompt (NULL, 0); |
1265e2d8 | 872 | |
a706069f | 873 | return len; |
51d6a954 RS |
874 | } |
875 | ||
eba08643 C |
876 | /* This is an alternate form of monitor_read_memory which is used for monitors |
877 | which can only read a single byte/word/etc. at a time. */ | |
878 | ||
879 | static int | |
880 | monitor_read_memory_single (memaddr, myaddr, len) | |
881 | CORE_ADDR memaddr; | |
882 | unsigned char *myaddr; | |
883 | int len; | |
884 | { | |
885 | unsigned LONGEST val; | |
886 | char membuf[sizeof(LONGEST) * 2 + 1]; | |
887 | char *p; | |
888 | char *cmd; | |
889 | int i; | |
890 | ||
891 | if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll) | |
892 | { | |
893 | len = 8; | |
894 | cmd = current_monitor->getmem.cmdll; | |
895 | } | |
896 | else if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl) | |
897 | { | |
898 | len = 4; | |
899 | cmd = current_monitor->getmem.cmdl; | |
900 | } | |
901 | else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw) | |
902 | { | |
903 | len = 2; | |
904 | cmd = current_monitor->getmem.cmdw; | |
905 | } | |
906 | else | |
907 | { | |
908 | len = 1; | |
909 | cmd = current_monitor->getmem.cmdb; | |
910 | } | |
911 | ||
912 | /* Send the examine command. */ | |
913 | ||
914 | monitor_printf (cmd, memaddr); | |
915 | ||
916 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for | |
917 | the register value. Otherwise, we just start searching from the start of | |
918 | the buf. */ | |
919 | ||
920 | if (current_monitor->getmem.resp_delim) | |
921 | monitor_expect (current_monitor->getmem.resp_delim, NULL, 0); | |
922 | ||
923 | /* Now, read the appropriate number of hex digits for this loc, skipping | |
924 | spaces. */ | |
925 | ||
926 | for (i = 0; i < len * 2; i++) | |
927 | { | |
928 | int c; | |
929 | ||
930 | while (1) | |
931 | { | |
932 | c = readchar (timeout); | |
933 | if (isxdigit (c)) | |
934 | break; | |
935 | if (c == ' ') | |
936 | continue; | |
937 | ||
938 | error ("monitor_read_memory_single (0x%x): bad response from monitor: %.*s%c.", | |
939 | memaddr, i, membuf, c); | |
940 | } | |
941 | ||
942 | membuf[i] = c; | |
943 | } | |
944 | ||
945 | membuf[i] = '\000'; /* terminate the number */ | |
946 | ||
947 | /* If TERM is present, we wait for that to show up. Also, (if TERM is | |
948 | present), we will send TERM_CMD if that is present. In any case, we collect | |
949 | all of the output into buf, and then wait for the normal prompt. */ | |
950 | ||
951 | if (current_monitor->getmem.term) | |
952 | { | |
953 | monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */ | |
954 | ||
955 | if (current_monitor->getmem.term_cmd) | |
956 | { | |
957 | monitor_printf (current_monitor->getmem.term_cmd); | |
958 | monitor_expect_prompt (NULL, 0); | |
959 | } | |
960 | } | |
961 | else | |
962 | monitor_expect_prompt (NULL, 0); /* get response */ | |
963 | ||
964 | p = membuf; | |
965 | val = strtoul (membuf, &p, 16); | |
966 | ||
967 | if (val == 0 && membuf == p) | |
968 | error ("monitor_read_memory_single (0x%x): bad value from monitor: %s.", | |
969 | memaddr, membuf); | |
970 | ||
971 | /* supply register stores in target byte order, so swap here */ | |
972 | ||
973 | store_unsigned_integer (myaddr, len, val); | |
974 | ||
975 | return len; | |
976 | } | |
977 | ||
1265e2d8 SG |
978 | /* Copy LEN bytes of data from debugger memory at MYADDR to inferior's memory |
979 | at MEMADDR. Returns length moved. Currently, we only do one byte at a | |
980 | time. */ | |
431b7d5f | 981 | |
1265e2d8 SG |
982 | static int |
983 | monitor_read_memory (memaddr, myaddr, len) | |
51d6a954 RS |
984 | CORE_ADDR memaddr; |
985 | char *myaddr; | |
986 | int len; | |
987 | { | |
1265e2d8 SG |
988 | unsigned LONGEST val; |
989 | unsigned char regbuf[MAX_REGISTER_RAW_SIZE]; | |
774e5d7f | 990 | char buf[512]; |
1265e2d8 SG |
991 | char *p, *p1; |
992 | char *name; | |
993 | int resp_len; | |
a706069f SG |
994 | int i; |
995 | ||
eba08643 C |
996 | if (current_monitor->flags & MO_GETMEM_READ_SINGLE) |
997 | return monitor_read_memory_single (memaddr, myaddr, len); | |
998 | ||
a706069f | 999 | len = min (len, 16); |
1265e2d8 | 1000 | |
774e5d7f SS |
1001 | /* See if xfer would cross a 16 byte boundary. If so, clip it. */ |
1002 | if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0) | |
1003 | len = ((memaddr + len) & ~0xf) - memaddr; | |
1004 | ||
1265e2d8 SG |
1005 | /* send the memory examine command */ |
1006 | ||
774e5d7f SS |
1007 | if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE) |
1008 | monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len - 1); | |
1009 | else | |
1010 | monitor_printf (current_monitor->getmem.cmdb, memaddr, len); | |
1265e2d8 SG |
1011 | |
1012 | /* If TERM is present, we wait for that to show up. Also, (if TERM is | |
1013 | present), we will send TERM_CMD if that is present. In any case, we collect | |
1014 | all of the output into buf, and then wait for the normal prompt. */ | |
1015 | ||
1016 | if (current_monitor->getmem.term) | |
431b7d5f | 1017 | { |
774e5d7f | 1018 | resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */ |
1265e2d8 SG |
1019 | |
1020 | if (resp_len <= 0) | |
8f078234 SG |
1021 | error ("monitor_read_memory (0x%x): excessive response from monitor: %.*s.", |
1022 | memaddr, resp_len, buf); | |
1265e2d8 SG |
1023 | |
1024 | if (current_monitor->getmem.term_cmd) | |
431b7d5f | 1025 | { |
1265e2d8 SG |
1026 | SERIAL_WRITE (monitor_desc, current_monitor->getmem.term_cmd, |
1027 | strlen (current_monitor->getmem.term_cmd)); | |
774e5d7f | 1028 | monitor_expect_prompt (NULL, 0); |
1b552670 | 1029 | } |
51d6a954 | 1030 | } |
1265e2d8 | 1031 | else |
774e5d7f SS |
1032 | resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */ |
1033 | ||
1034 | p = buf; | |
1035 | ||
1265e2d8 | 1036 | /* If RESP_DELIM is specified, we search for that as a leading delimiter for |
a706069f SG |
1037 | the values. Otherwise, we just start searching from the start of the buf. |
1038 | */ | |
1265e2d8 SG |
1039 | |
1040 | if (current_monitor->getmem.resp_delim) | |
1041 | { | |
774e5d7f | 1042 | p = strstr (p, current_monitor->getmem.resp_delim); |
1265e2d8 | 1043 | if (!p) |
8f078234 SG |
1044 | error ("monitor_read_memory (0x%x): bad response from monitor: %.*s.", |
1045 | memaddr, resp_len, buf); | |
1265e2d8 SG |
1046 | p += strlen (current_monitor->getmem.resp_delim); |
1047 | } | |
1265e2d8 | 1048 | |
a706069f SG |
1049 | for (i = len; i > 0; i--) |
1050 | { | |
774e5d7f SS |
1051 | /* Skip non-hex chars, but bomb on end of string and newlines */ |
1052 | ||
1053 | while (1) | |
1054 | { | |
1055 | if (isxdigit (*p)) | |
1056 | break; | |
1057 | if (*p == '\000' || *p == '\n' || *p == '\r') | |
1058 | error ("monitor_read_memory (0x%x): badly terminated response from monitor: %.*s", memaddr, resp_len, buf); | |
1059 | p++; | |
1060 | } | |
1061 | ||
a706069f | 1062 | val = strtoul (p, &p1, 16); |
1265e2d8 | 1063 | |
a706069f SG |
1064 | if (val == 0 && p == p1) |
1065 | error ("monitor_read_memory (0x%x): bad value from monitor: %.*s.", memaddr, | |
1066 | resp_len, buf); | |
1265e2d8 | 1067 | |
a706069f | 1068 | *myaddr++ = val; |
774e5d7f SS |
1069 | |
1070 | if (i == 1) | |
1071 | break; | |
1072 | ||
1073 | p = p1; | |
a706069f | 1074 | } |
1265e2d8 | 1075 | |
a706069f | 1076 | return len; |
51d6a954 RS |
1077 | } |
1078 | ||
a706069f | 1079 | static int |
1265e2d8 | 1080 | monitor_xfer_memory (memaddr, myaddr, len, write, target) |
51d6a954 RS |
1081 | CORE_ADDR memaddr; |
1082 | char *myaddr; | |
1083 | int len; | |
1084 | int write; | |
1085 | struct target_ops *target; /* ignored */ | |
1086 | { | |
1087 | if (write) | |
1265e2d8 | 1088 | return monitor_write_memory (memaddr, myaddr, len); |
51d6a954 | 1089 | else |
1265e2d8 | 1090 | return monitor_read_memory (memaddr, myaddr, len); |
51d6a954 RS |
1091 | } |
1092 | ||
a706069f SG |
1093 | static void |
1094 | monitor_kill () | |
51d6a954 RS |
1095 | { |
1096 | return; /* ignore attempts to kill target system */ | |
1097 | } | |
1098 | ||
a706069f SG |
1099 | /* All we actually do is set the PC to the start address of exec_bfd, and start |
1100 | the program at that point. */ | |
1101 | ||
1102 | static void | |
1103 | monitor_create_inferior (exec_file, args, env) | |
1104 | char *exec_file; | |
1105 | char *args; | |
1106 | char **env; | |
1107 | { | |
1108 | if (args && (*args != '\000')) | |
1109 | error ("Args are not supported by the monitor."); | |
1110 | ||
1111 | clear_proceed_status (); | |
1112 | proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0); | |
1113 | } | |
1114 | ||
51d6a954 RS |
1115 | /* Clean up when a program exits. |
1116 | The program actually lives on in the remote processor's RAM, and may be | |
1117 | run again without a download. Don't leave it full of breakpoint | |
1118 | instructions. */ | |
1119 | ||
a706069f | 1120 | static void |
51d6a954 RS |
1121 | monitor_mourn_inferior () |
1122 | { | |
8f078234 | 1123 | unpush_target (targ_ops); |
51d6a954 RS |
1124 | generic_mourn_inferior (); /* Do all the proper things now */ |
1125 | } | |
1126 | ||
1265e2d8 | 1127 | #define NUM_MONITOR_BREAKPOINTS 8 |
51d6a954 | 1128 | |
1265e2d8 | 1129 | static CORE_ADDR breakaddr[NUM_MONITOR_BREAKPOINTS] = {0}; |
51d6a954 | 1130 | |
431b7d5f SS |
1131 | /* Tell the monitor to add a breakpoint. */ |
1132 | ||
a706069f | 1133 | static int |
51d6a954 RS |
1134 | monitor_insert_breakpoint (addr, shadow) |
1135 | CORE_ADDR addr; | |
1136 | char *shadow; | |
1137 | { | |
1138 | int i; | |
1265e2d8 | 1139 | static unsigned char break_insn[] = BREAKPOINT; |
51d6a954 | 1140 | |
1265e2d8 | 1141 | for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++) |
431b7d5f SS |
1142 | { |
1143 | if (breakaddr[i] == 0) | |
1144 | { | |
1145 | breakaddr[i] = addr; | |
1265e2d8 | 1146 | monitor_read_memory (addr, shadow, sizeof (break_insn)); |
774e5d7f SS |
1147 | monitor_printf (SET_BREAK_CMD, addr); |
1148 | monitor_expect_prompt (NULL, 0); | |
431b7d5f SS |
1149 | return 0; |
1150 | } | |
f1ca4cbc | 1151 | } |
f1ca4cbc | 1152 | |
1265e2d8 | 1153 | error ("Too many breakpoints (> %d) for monitor.", NUM_MONITOR_BREAKPOINTS); |
51d6a954 RS |
1154 | } |
1155 | ||
431b7d5f SS |
1156 | /* Tell the monitor to remove a breakpoint. */ |
1157 | ||
a706069f | 1158 | static int |
51d6a954 RS |
1159 | monitor_remove_breakpoint (addr, shadow) |
1160 | CORE_ADDR addr; | |
1161 | char *shadow; | |
1162 | { | |
1163 | int i; | |
1164 | ||
1265e2d8 | 1165 | for (i = 0; i < NUM_MONITOR_BREAKPOINTS; i++) |
431b7d5f SS |
1166 | { |
1167 | if (breakaddr[i] == addr) | |
1168 | { | |
1169 | breakaddr[i] = 0; | |
1170 | /* some monitors remove breakpoints based on the address */ | |
a706069f | 1171 | if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR) |
774e5d7f | 1172 | monitor_printf (CLR_BREAK_CMD, addr); |
431b7d5f | 1173 | else |
774e5d7f SS |
1174 | monitor_printf (CLR_BREAK_CMD, i); |
1175 | monitor_expect_prompt (NULL, 0); | |
431b7d5f SS |
1176 | return 0; |
1177 | } | |
7804e5bc | 1178 | } |
1265e2d8 | 1179 | fprintf_unfiltered (stderr, "Can't find breakpoint associated with 0x%x\n", addr); |
51d6a954 RS |
1180 | return 1; |
1181 | } | |
1182 | ||
774e5d7f | 1183 | /* monitor_load -- download a file. */ |
431b7d5f | 1184 | |
a706069f SG |
1185 | static void |
1186 | monitor_load (file, from_tty) | |
21ed3dcd | 1187 | char *file; |
a706069f | 1188 | int from_tty; |
21ed3dcd | 1189 | { |
774e5d7f SS |
1190 | if (current_monitor->load_routine) |
1191 | current_monitor->load_routine (monitor_desc, file, hashmark); | |
1192 | else | |
1193 | monitor_load_srec (file); | |
21ed3dcd | 1194 | |
a706069f | 1195 | /* Finally, make the PC point at the start address */ |
431b7d5f | 1196 | |
a706069f | 1197 | write_pc (bfd_get_start_address (exec_bfd)); |
431b7d5f | 1198 | |
a706069f | 1199 | inferior_pid = 0; /* No process now */ |
51d6a954 | 1200 | |
a706069f SG |
1201 | /* This is necessary because many things were based on the PC at the time that |
1202 | we attached to the monitor, which is no longer valid now that we have loaded | |
1203 | new code (and just changed the PC). Another way to do this might be to call | |
1204 | normal_stop, except that the stack may not be valid, and things would get | |
1205 | horribly confused... */ | |
51d6a954 | 1206 | |
a706069f SG |
1207 | clear_symtab_users (); |
1208 | } | |
1209 | ||
1210 | static void | |
1211 | monitor_stop () | |
1212 | { | |
eba08643 C |
1213 | if (current_monitor->stop) |
1214 | monitor_printf_noecho (current_monitor->stop); | |
51d6a954 RS |
1215 | } |
1216 | ||
431b7d5f SS |
1217 | /* Put a command string, in args, out to MONITOR. Output from MONITOR |
1218 | is placed on the users terminal until the prompt is seen. FIXME: We | |
1219 | read the characters ourseleves here cause of a nasty echo. */ | |
1220 | ||
8f078234 | 1221 | static void |
a706069f | 1222 | monitor_command (args, from_tty) |
431b7d5f | 1223 | char *args; |
a706069f | 1224 | int from_tty; |
51d6a954 | 1225 | { |
7804e5bc | 1226 | char *p; |
a706069f SG |
1227 | int resp_len; |
1228 | char buf[1000]; | |
431b7d5f | 1229 | |
51d6a954 | 1230 | if (monitor_desc == NULL) |
431b7d5f | 1231 | error ("monitor target not open."); |
7804e5bc | 1232 | |
774e5d7f SS |
1233 | p = PROMPT; |
1234 | ||
431b7d5f SS |
1235 | /* Send the command. Note that if no args were supplied, then we're |
1236 | just sending the monitor a newline, which is sometimes useful. */ | |
1237 | ||
774e5d7f | 1238 | monitor_printf ("%s\r", (args ? args : "")); |
7804e5bc | 1239 | |
774e5d7f | 1240 | resp_len = monitor_expect_prompt (buf, sizeof buf); |
a706069f SG |
1241 | |
1242 | fputs_unfiltered (buf, gdb_stdout); /* Output the response */ | |
51d6a954 RS |
1243 | } |
1244 | ||
774e5d7f | 1245 | /* Download a binary file by converting it to S records. */ |
431b7d5f | 1246 | |
21ed3dcd | 1247 | static void |
774e5d7f | 1248 | monitor_load_srec (args) |
21ed3dcd | 1249 | char *args; |
21ed3dcd RS |
1250 | { |
1251 | bfd *abfd; | |
1252 | asection *s; | |
431b7d5f | 1253 | char *buffer, srec[1024]; |
06b8f5e4 | 1254 | int i; |
eba08643 | 1255 | int srec_frame = 32; |
774e5d7f | 1256 | int reclen; |
21ed3dcd | 1257 | |
a706069f SG |
1258 | buffer = alloca (srec_frame * 2 + 256); |
1259 | ||
21ed3dcd | 1260 | abfd = bfd_openr (args, 0); |
431b7d5f SS |
1261 | if (!abfd) |
1262 | { | |
1263 | printf_filtered ("Unable to open file %s\n", args); | |
1264 | return; | |
1265 | } | |
21ed3dcd | 1266 | |
431b7d5f SS |
1267 | if (bfd_check_format (abfd, bfd_object) == 0) |
1268 | { | |
1269 | printf_filtered ("File is not an object file\n"); | |
1270 | return; | |
1271 | } | |
21ed3dcd | 1272 | |
774e5d7f | 1273 | monitor_printf (LOAD_CMD); /* tell the monitor to load */ |
a706069f | 1274 | if (current_monitor->loadresp) |
774e5d7f | 1275 | monitor_expect (current_monitor->loadresp, NULL, 0); |
a706069f | 1276 | |
a706069f SG |
1277 | for (s = abfd->sections; s; s = s->next) |
1278 | if (s->flags & SEC_LOAD) | |
1279 | { | |
1280 | printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, | |
1281 | s->vma + s->_raw_size); | |
1282 | gdb_flush (gdb_stdout); | |
774e5d7f | 1283 | |
a706069f SG |
1284 | for (i = 0; i < s->_raw_size; i += srec_frame) |
1285 | { | |
1286 | int numbytes; | |
1287 | ||
1288 | numbytes = min (srec_frame, s->_raw_size - i); | |
1289 | ||
1290 | bfd_get_section_contents (abfd, s, buffer, i, numbytes); | |
a706069f | 1291 | |
774e5d7f SS |
1292 | reclen = monitor_make_srec (srec, 3, s->vma + i, buffer, numbytes); |
1293 | ||
eba08643 | 1294 | monitor_printf_noecho ("%.*s\r", reclen, srec); |
a706069f SG |
1295 | |
1296 | if (hashmark) | |
1297 | { | |
1298 | putchar_unfiltered ('#'); | |
1299 | gdb_flush (gdb_stdout); | |
1300 | } | |
1301 | } /* Per-packet (or S-record) loop */ | |
774e5d7f | 1302 | |
a706069f SG |
1303 | putchar_unfiltered ('\n'); |
1304 | } /* Loadable sections */ | |
1305 | ||
1306 | if (hashmark) | |
1307 | putchar_unfiltered ('\n'); | |
06b8f5e4 | 1308 | |
431b7d5f SS |
1309 | /* Write a type 7 terminator record. no data for a type 7, and there |
1310 | is no data, so len is 0. */ | |
1311 | ||
774e5d7f | 1312 | reclen = monitor_make_srec (srec, 7, abfd->start_address, NULL, 0); |
06b8f5e4 | 1313 | |
eba08643 | 1314 | monitor_printf_noecho ("%.*s\r", reclen, srec); |
21ed3dcd | 1315 | |
eba08643 | 1316 | monitor_printf_noecho ("\r\r"); /* Some monitors need these to wake up */ |
431b7d5f | 1317 | |
774e5d7f | 1318 | monitor_expect_prompt (NULL, 0); |
eba08643 C |
1319 | |
1320 | SERIAL_FLUSH_INPUT (monitor_desc); | |
06b8f5e4 | 1321 | } |
21ed3dcd | 1322 | |
06b8f5e4 RS |
1323 | /* |
1324 | * monitor_make_srec -- make an srecord. This writes each line, one at a | |
1325 | * time, each with it's own header and trailer line. | |
1326 | * An srecord looks like this: | |
1327 | * | |
1328 | * byte count-+ address | |
1329 | * start ---+ | | data +- checksum | |
1330 | * | | | | | |
1331 | * S01000006F6B692D746573742E73726563E4 | |
1332 | * S315000448600000000000000000FC00005900000000E9 | |
1333 | * S31A0004000023C1400037DE00F023604000377B009020825000348D | |
1334 | * S30B0004485A0000000000004E | |
1335 | * S70500040000F6 | |
1336 | * | |
1337 | * S<type><length><address><data><checksum> | |
1338 | * | |
1339 | * Where | |
1340 | * - length | |
1341 | * is the number of bytes following upto the checksum. Note that | |
1342 | * this is not the number of chars following, since it takes two | |
1343 | * chars to represent a byte. | |
1344 | * - type | |
1345 | * is one of: | |
1346 | * 0) header record | |
1347 | * 1) two byte address data record | |
1348 | * 2) three byte address data record | |
1349 | * 3) four byte address data record | |
1350 | * 7) four byte address termination record | |
1351 | * 8) three byte address termination record | |
1352 | * 9) two byte address termination record | |
1353 | * | |
1354 | * - address | |
1355 | * is the start address of the data following, or in the case of | |
1356 | * a termination record, the start address of the image | |
1357 | * - data | |
1358 | * is the data. | |
1359 | * - checksum | |
1360 | * is the sum of all the raw byte data in the record, from the length | |
1361 | * upwards, modulo 256 and subtracted from 255. | |
774e5d7f SS |
1362 | * |
1363 | * This routine returns the length of the S-record. | |
1364 | * | |
06b8f5e4 | 1365 | */ |
431b7d5f SS |
1366 | |
1367 | static int | |
06b8f5e4 RS |
1368 | monitor_make_srec (buffer, type, memaddr, myaddr, len) |
1369 | char *buffer; | |
1370 | int type; | |
21ed3dcd RS |
1371 | CORE_ADDR memaddr; |
1372 | unsigned char *myaddr; | |
1373 | int len; | |
1374 | { | |
774e5d7f | 1375 | unsigned char checksum; |
06b8f5e4 RS |
1376 | int i; |
1377 | char *buf; | |
774e5d7f | 1378 | static char hextab[] = "0123456789ABCDEF"; |
21ed3dcd | 1379 | |
06b8f5e4 | 1380 | buf = buffer; |
1265e2d8 | 1381 | |
06b8f5e4 RS |
1382 | checksum = 0; |
1383 | ||
431b7d5f SS |
1384 | /* Create the header for the srec. 4 is the number of bytes in the address, |
1385 | and 1 is the number of bytes in the count. */ | |
1386 | ||
06b8f5e4 RS |
1387 | sprintf (buf, "S%d%02X%08X", type, len + 4 + 1, memaddr); |
1388 | buf += 12; | |
1389 | ||
774e5d7f SS |
1390 | /* Note that the checksum is calculated on the raw data, not the hexified |
1391 | data. It includes the length, address and the data portions of the | |
1392 | packet. */ | |
1393 | ||
1394 | checksum += (len + 4 + 1 /* Packet length */ | |
1395 | + (memaddr & 0xff) /* Address... */ | |
06b8f5e4 RS |
1396 | + ((memaddr >> 8) & 0xff) |
1397 | + ((memaddr >> 16) & 0xff) | |
1398 | + ((memaddr >> 24) & 0xff)); | |
1399 | ||
431b7d5f SS |
1400 | /* build the srecord */ |
1401 | for (i = 0; i < len; i++) | |
1402 | { | |
774e5d7f SS |
1403 | *buf++ = hextab [myaddr[i] >> 4]; |
1404 | *buf++ = hextab [myaddr[i] & 0xf]; | |
431b7d5f | 1405 | checksum += myaddr[i]; |
431b7d5f | 1406 | } |
21ed3dcd | 1407 | |
774e5d7f | 1408 | checksum = ~checksum; |
431b7d5f | 1409 | |
774e5d7f SS |
1410 | *buf++ = hextab[checksum >> 4]; |
1411 | *buf++ = hextab[checksum & 0xf]; | |
06b8f5e4 | 1412 | |
774e5d7f | 1413 | return buf - buffer; |
1b552670 RS |
1414 | } |
1415 | ||
1416 | /* Convert hex digit A to a number. */ | |
431b7d5f | 1417 | |
1b552670 RS |
1418 | static int |
1419 | from_hex (a) | |
1420 | int a; | |
1421 | { | |
1b552670 RS |
1422 | if (a >= '0' && a <= '9') |
1423 | return a - '0'; | |
1424 | if (a >= 'a' && a <= 'f') | |
1425 | return a - 'a' + 10; | |
1426 | if (a >= 'A' && a <= 'F') | |
1427 | return a - 'A' + 10; | |
1b552670 | 1428 | |
1265e2d8 | 1429 | error ("Reply contains invalid hex digit 0x%x", a); |
1b552670 RS |
1430 | } |
1431 | ||
774e5d7f SS |
1432 | static struct target_ops monitor_ops = |
1433 | { | |
a706069f SG |
1434 | NULL, /* to_shortname */ |
1435 | NULL, /* to_longname */ | |
1436 | NULL, /* to_doc */ | |
1437 | NULL, /* to_open */ | |
1438 | monitor_close, /* to_close */ | |
1439 | NULL, /* to_attach */ | |
1440 | monitor_detach, /* to_detach */ | |
1441 | monitor_resume, /* to_resume */ | |
1442 | monitor_wait, /* to_wait */ | |
1443 | monitor_fetch_registers, /* to_fetch_registers */ | |
1444 | monitor_store_registers, /* to_store_registers */ | |
1445 | monitor_prepare_to_store, /* to_prepare_to_store */ | |
1446 | monitor_xfer_memory, /* to_xfer_memory */ | |
1447 | monitor_files_info, /* to_files_info */ | |
1448 | monitor_insert_breakpoint, /* to_insert_breakpoint */ | |
1449 | monitor_remove_breakpoint, /* to_remove_breakpoint */ | |
1450 | 0, /* to_terminal_init */ | |
1451 | 0, /* to_terminal_inferior */ | |
1452 | 0, /* to_terminal_ours_for_output */ | |
1453 | 0, /* to_terminal_ours */ | |
1454 | 0, /* to_terminal_info */ | |
1455 | monitor_kill, /* to_kill */ | |
1456 | monitor_load, /* to_load */ | |
1457 | 0, /* to_lookup_symbol */ | |
1458 | monitor_create_inferior, /* to_create_inferior */ | |
1459 | monitor_mourn_inferior, /* to_mourn_inferior */ | |
1460 | 0, /* to_can_run */ | |
1461 | 0, /* to_notice_signals */ | |
1462 | monitor_stop, /* to_stop */ | |
1463 | process_stratum, /* to_stratum */ | |
1464 | 0, /* to_next */ | |
1465 | 1, /* to_has_all_memory */ | |
1466 | 1, /* to_has_memory */ | |
1467 | 1, /* to_has_stack */ | |
1468 | 1, /* to_has_registers */ | |
1469 | 1, /* to_has_execution */ | |
1470 | 0, /* sections */ | |
1471 | 0, /* sections_end */ | |
1472 | OPS_MAGIC /* to_magic */ | |
1473 | }; | |
1474 | ||
1475 | /* Init the target_ops structure pointed at by OPS */ | |
1476 | ||
1477 | void | |
1478 | init_monitor_ops (ops) | |
1479 | struct target_ops *ops; | |
1480 | { | |
1481 | memcpy (ops, &monitor_ops, sizeof monitor_ops); | |
1482 | } | |
1483 | ||
431b7d5f SS |
1484 | /* Define additional commands that are usually only used by monitors. */ |
1485 | ||
51d6a954 RS |
1486 | void |
1487 | _initialize_remote_monitors () | |
1488 | { | |
51d6a954 RS |
1489 | add_show_from_set (add_set_cmd ("hash", no_class, var_boolean, |
1490 | (char *)&hashmark, | |
1491 | "Set display of activity while downloading a file.\n\ | |
1265e2d8 | 1492 | When enabled, a hashmark \'#\' is displayed.", |
51d6a954 RS |
1493 | &setlist), |
1494 | &showlist); | |
1495 | ||
51d6a954 RS |
1496 | add_com ("monitor", class_obscure, monitor_command, |
1497 | "Send a command to the debug monitor."); | |
1498 | } |