1130812Smarcel/* Remote debugging interface for boot monitors, for GDB. 2130812Smarcel 3130812Smarcel Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 4130812Smarcel 1999, 2000, 2001, 2002 Free Software Foundation, Inc. 5130812Smarcel 6130812Smarcel Contributed by Cygnus Support. Written by Rob Savoye for Cygnus. 7130812Smarcel Resurrected from the ashes by Stu Grossman. 8130812Smarcel 9130812Smarcel This file is part of GDB. 10130812Smarcel 11130812Smarcel This program is free software; you can redistribute it and/or modify 12130812Smarcel it under the terms of the GNU General Public License as published by 13130812Smarcel the Free Software Foundation; either version 2 of the License, or 14130812Smarcel (at your option) any later version. 15130812Smarcel 16130812Smarcel This program is distributed in the hope that it will be useful, 17130812Smarcel but WITHOUT ANY WARRANTY; without even the implied warranty of 18130812Smarcel MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19130812Smarcel GNU General Public License for more details. 20130812Smarcel 21130812Smarcel You should have received a copy of the GNU General Public License 22130812Smarcel along with this program; if not, write to the Free Software 23130812Smarcel Foundation, Inc., 59 Temple Place - Suite 330, 24130812Smarcel Boston, MA 02111-1307, USA. */ 25130812Smarcel 26130812Smarcel/* This file was derived from various remote-* modules. It is a collection 27130812Smarcel of generic support functions so GDB can talk directly to a ROM based 28130812Smarcel monitor. This saves use from having to hack an exception based handler 29130812Smarcel into existence, and makes for quick porting. 30130812Smarcel 31130812Smarcel This module talks to a debug monitor called 'MONITOR', which 32130812Smarcel We communicate with MONITOR via either a direct serial line, or a TCP 33130812Smarcel (or possibly TELNET) stream to a terminal multiplexor, 34130812Smarcel which in turn talks to the target board. */ 35130812Smarcel 36130812Smarcel/* FIXME 32x64: This code assumes that registers and addresses are at 37130812Smarcel most 32 bits long. If they can be larger, you will need to declare 38130812Smarcel values as LONGEST and use %llx or some such to print values when 39130812Smarcel building commands to send to the monitor. Since we don't know of 40130812Smarcel any actual 64-bit targets with ROM monitors that use this code, 41130812Smarcel it's not an issue right now. -sts 4/18/96 */ 42130812Smarcel 43130812Smarcel#include "defs.h" 44130812Smarcel#include "gdbcore.h" 45130812Smarcel#include "target.h" 46130812Smarcel#include <signal.h> 47130812Smarcel#include <ctype.h> 48130812Smarcel#include "gdb_string.h" 49130812Smarcel#include <sys/types.h> 50130812Smarcel#include "command.h" 51130812Smarcel#include "serial.h" 52130812Smarcel#include "monitor.h" 53130812Smarcel#include "gdbcmd.h" 54130812Smarcel#include "inferior.h" 55130812Smarcel#include "gdb_regex.h" 56130812Smarcel#include "srec.h" 57130812Smarcel#include "regcache.h" 58130812Smarcel 59130812Smarcelstatic char *dev_name; 60130812Smarcelstatic struct target_ops *targ_ops; 61130812Smarcel 62130812Smarcelstatic void monitor_vsprintf (char *sndbuf, char *pattern, va_list args); 63130812Smarcel 64130812Smarcelstatic int readchar (int timeout); 65130812Smarcel 66130812Smarcelstatic void monitor_fetch_register (int regno); 67130812Smarcelstatic void monitor_store_register (int regno); 68130812Smarcel 69130812Smarcelstatic void monitor_printable_string (char *newstr, char *oldstr, int len); 70130812Smarcelstatic void monitor_error (char *function, char *message, CORE_ADDR memaddr, int len, char *string, int final_char); 71130812Smarcelstatic void monitor_detach (char *args, int from_tty); 72130812Smarcelstatic void monitor_resume (ptid_t ptid, int step, enum target_signal sig); 73130812Smarcelstatic void monitor_interrupt (int signo); 74130812Smarcelstatic void monitor_interrupt_twice (int signo); 75130812Smarcelstatic void monitor_interrupt_query (void); 76130812Smarcelstatic void monitor_wait_cleanup (void *old_timeout); 77130812Smarcel 78130812Smarcelstatic ptid_t monitor_wait (ptid_t ptid, struct target_waitstatus *status); 79130812Smarcelstatic void monitor_fetch_registers (int regno); 80130812Smarcelstatic void monitor_store_registers (int regno); 81130812Smarcelstatic void monitor_prepare_to_store (void); 82130812Smarcelstatic int monitor_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, 83130812Smarcel int write, 84130812Smarcel struct mem_attrib *attrib, 85130812Smarcel struct target_ops *target); 86130812Smarcelstatic void monitor_files_info (struct target_ops *ops); 87130812Smarcelstatic int monitor_insert_breakpoint (CORE_ADDR addr, char *shadow); 88130812Smarcelstatic int monitor_remove_breakpoint (CORE_ADDR addr, char *shadow); 89130812Smarcelstatic void monitor_kill (void); 90130812Smarcelstatic void monitor_load (char *file, int from_tty); 91130812Smarcelstatic void monitor_mourn_inferior (void); 92130812Smarcelstatic void monitor_stop (void); 93130812Smarcel 94130812Smarcelstatic int monitor_read_memory (CORE_ADDR addr, char *myaddr, int len); 95130812Smarcelstatic int monitor_write_memory (CORE_ADDR addr, char *myaddr, int len); 96130812Smarcelstatic int monitor_write_memory_bytes (CORE_ADDR addr, char *myaddr, int len); 97130812Smarcelstatic int monitor_write_memory_block (CORE_ADDR memaddr, 98130812Smarcel char *myaddr, int len); 99130812Smarcelstatic int monitor_expect_regexp (struct re_pattern_buffer *pat, 100130812Smarcel char *buf, int buflen); 101130812Smarcelstatic void monitor_dump_regs (void); 102130812Smarcel#if 0 103130812Smarcelstatic int from_hex (int a); 104130812Smarcelstatic unsigned long get_hex_word (void); 105130812Smarcel#endif 106130812Smarcelstatic void parse_register_dump (char *, int); 107130812Smarcel 108130812Smarcelstatic struct monitor_ops *current_monitor; 109130812Smarcel 110130812Smarcelstatic int hashmark; /* flag set by "set hash" */ 111130812Smarcel 112130812Smarcelstatic int timeout = 30; 113130812Smarcel 114130812Smarcelstatic int in_monitor_wait = 0; /* Non-zero means we are in monitor_wait() */ 115130812Smarcel 116130812Smarcelstatic void (*ofunc) (); /* Old SIGINT signal handler */ 117130812Smarcel 118130812Smarcelstatic CORE_ADDR *breakaddr; 119130812Smarcel 120130812Smarcel/* Descriptor for I/O to remote machine. Initialize it to NULL so 121130812Smarcel that monitor_open knows that we don't have a file open when the 122130812Smarcel program starts. */ 123130812Smarcel 124130812Smarcelstatic struct serial *monitor_desc = NULL; 125130812Smarcel 126130812Smarcel/* Pointer to regexp pattern matching data */ 127130812Smarcel 128130812Smarcelstatic struct re_pattern_buffer register_pattern; 129130812Smarcelstatic char register_fastmap[256]; 130130812Smarcel 131130812Smarcelstatic struct re_pattern_buffer getmem_resp_delim_pattern; 132130812Smarcelstatic char getmem_resp_delim_fastmap[256]; 133130812Smarcel 134130812Smarcelstatic struct re_pattern_buffer setmem_resp_delim_pattern; 135130812Smarcelstatic char setmem_resp_delim_fastmap[256]; 136130812Smarcel 137130812Smarcelstatic struct re_pattern_buffer setreg_resp_delim_pattern; 138130812Smarcelstatic char setreg_resp_delim_fastmap[256]; 139130812Smarcel 140130812Smarcelstatic int dump_reg_flag; /* Non-zero means do a dump_registers cmd when 141130812Smarcel monitor_wait wakes up. */ 142130812Smarcel 143130812Smarcelstatic int first_time = 0; /* is this the first time we're executing after 144130812Smarcel gaving created the child proccess? */ 145130812Smarcel 146130812Smarcel#define TARGET_BUF_SIZE 2048 147130812Smarcel 148130812Smarcel/* Monitor specific debugging information. Typically only useful to 149130812Smarcel the developer of a new monitor interface. */ 150130812Smarcel 151130812Smarcelstatic void monitor_debug (const char *fmt, ...) ATTR_FORMAT(printf, 1, 2); 152130812Smarcel 153130812Smarcelstatic int monitor_debug_p = 0; 154130812Smarcel 155130812Smarcel/* NOTE: This file alternates between monitor_debug_p and remote_debug 156130812Smarcel when determining if debug information is printed. Perhaphs this 157130812Smarcel could be simplified. */ 158130812Smarcel 159130812Smarcelstatic void 160130812Smarcelmonitor_debug (const char *fmt, ...) 161130812Smarcel{ 162130812Smarcel if (monitor_debug_p) 163130812Smarcel { 164130812Smarcel va_list args; 165130812Smarcel va_start (args, fmt); 166130812Smarcel vfprintf_filtered (gdb_stdlog, fmt, args); 167130812Smarcel va_end (args); 168130812Smarcel } 169130812Smarcel} 170130812Smarcel 171130812Smarcel 172130812Smarcel/* Convert a string into a printable representation, Return # byte in 173130812Smarcel the new string. When LEN is >0 it specifies the size of the 174130812Smarcel string. Otherwize strlen(oldstr) is used. */ 175130812Smarcel 176130812Smarcelstatic void 177130812Smarcelmonitor_printable_string (char *newstr, char *oldstr, int len) 178130812Smarcel{ 179130812Smarcel int ch; 180130812Smarcel int i; 181130812Smarcel 182130812Smarcel if (len <= 0) 183130812Smarcel len = strlen (oldstr); 184130812Smarcel 185130812Smarcel for (i = 0; i < len; i++) 186130812Smarcel { 187130812Smarcel ch = oldstr[i]; 188130812Smarcel switch (ch) 189130812Smarcel { 190130812Smarcel default: 191130812Smarcel if (isprint (ch)) 192130812Smarcel *newstr++ = ch; 193130812Smarcel 194130812Smarcel else 195130812Smarcel { 196130812Smarcel sprintf (newstr, "\\x%02x", ch & 0xff); 197130812Smarcel newstr += 4; 198130812Smarcel } 199130812Smarcel break; 200130812Smarcel 201130812Smarcel case '\\': 202130812Smarcel *newstr++ = '\\'; 203130812Smarcel *newstr++ = '\\'; 204130812Smarcel break; 205130812Smarcel case '\b': 206130812Smarcel *newstr++ = '\\'; 207130812Smarcel *newstr++ = 'b'; 208130812Smarcel break; 209130812Smarcel case '\f': 210130812Smarcel *newstr++ = '\\'; 211130812Smarcel *newstr++ = 't'; 212130812Smarcel break; 213130812Smarcel case '\n': 214130812Smarcel *newstr++ = '\\'; 215130812Smarcel *newstr++ = 'n'; 216130812Smarcel break; 217130812Smarcel case '\r': 218130812Smarcel *newstr++ = '\\'; 219130812Smarcel *newstr++ = 'r'; 220130812Smarcel break; 221130812Smarcel case '\t': 222130812Smarcel *newstr++ = '\\'; 223130812Smarcel *newstr++ = 't'; 224130812Smarcel break; 225130812Smarcel case '\v': 226130812Smarcel *newstr++ = '\\'; 227130812Smarcel *newstr++ = 'v'; 228130812Smarcel break; 229130812Smarcel } 230130812Smarcel } 231130812Smarcel 232130812Smarcel *newstr++ = '\0'; 233130812Smarcel} 234130812Smarcel 235130812Smarcel/* Print monitor errors with a string, converting the string to printable 236130812Smarcel representation. */ 237130812Smarcel 238130812Smarcelstatic void 239130812Smarcelmonitor_error (char *function, char *message, 240130812Smarcel CORE_ADDR memaddr, int len, char *string, int final_char) 241130812Smarcel{ 242130812Smarcel int real_len = (len == 0 && string != (char *) 0) ? strlen (string) : len; 243130812Smarcel char *safe_string = alloca ((real_len * 4) + 1); 244130812Smarcel monitor_printable_string (safe_string, string, real_len); 245130812Smarcel 246130812Smarcel if (final_char) 247130812Smarcel error ("%s (0x%s): %s: %s%c", function, paddr_nz (memaddr), message, safe_string, final_char); 248130812Smarcel else 249130812Smarcel error ("%s (0x%s): %s: %s", function, paddr_nz (memaddr), message, safe_string); 250130812Smarcel} 251130812Smarcel 252130812Smarcel/* Convert hex digit A to a number. */ 253130812Smarcel 254130812Smarcelstatic int 255130812Smarcelfromhex (int a) 256130812Smarcel{ 257130812Smarcel if (a >= '0' && a <= '9') 258130812Smarcel return a - '0'; 259130812Smarcel else if (a >= 'a' && a <= 'f') 260130812Smarcel return a - 'a' + 10; 261130812Smarcel else if (a >= 'A' && a <= 'F') 262130812Smarcel return a - 'A' + 10; 263130812Smarcel else 264130812Smarcel error ("Invalid hex digit %d", a); 265130812Smarcel} 266130812Smarcel 267130812Smarcel/* monitor_vsprintf - similar to vsprintf but handles 64-bit addresses 268130812Smarcel 269130812Smarcel This function exists to get around the problem that many host platforms 270130812Smarcel don't have a printf that can print 64-bit addresses. The %A format 271130812Smarcel specification is recognized as a special case, and causes the argument 272130812Smarcel to be printed as a 64-bit hexadecimal address. 273130812Smarcel 274130812Smarcel Only format specifiers of the form "[0-9]*[a-z]" are recognized. 275130812Smarcel If it is a '%s' format, the argument is a string; otherwise the 276130812Smarcel argument is assumed to be a long integer. 277130812Smarcel 278130812Smarcel %% is also turned into a single %. 279130812Smarcel */ 280130812Smarcel 281130812Smarcelstatic void 282130812Smarcelmonitor_vsprintf (char *sndbuf, char *pattern, va_list args) 283130812Smarcel{ 284130812Smarcel char format[10]; 285130812Smarcel char fmt; 286130812Smarcel char *p; 287130812Smarcel int i; 288130812Smarcel long arg_int; 289130812Smarcel CORE_ADDR arg_addr; 290130812Smarcel char *arg_string; 291130812Smarcel 292130812Smarcel for (p = pattern; *p; p++) 293130812Smarcel { 294130812Smarcel if (*p == '%') 295130812Smarcel { 296130812Smarcel /* Copy the format specifier to a separate buffer. */ 297130812Smarcel format[0] = *p++; 298130812Smarcel for (i = 1; *p >= '0' && *p <= '9' && i < (int) sizeof (format) - 2; 299130812Smarcel i++, p++) 300130812Smarcel format[i] = *p; 301130812Smarcel format[i] = fmt = *p; 302130812Smarcel format[i + 1] = '\0'; 303130812Smarcel 304130812Smarcel /* Fetch the next argument and print it. */ 305130812Smarcel switch (fmt) 306130812Smarcel { 307130812Smarcel case '%': 308130812Smarcel strcpy (sndbuf, "%"); 309130812Smarcel break; 310130812Smarcel case 'A': 311130812Smarcel arg_addr = va_arg (args, CORE_ADDR); 312130812Smarcel strcpy (sndbuf, paddr_nz (arg_addr)); 313130812Smarcel break; 314130812Smarcel case 's': 315130812Smarcel arg_string = va_arg (args, char *); 316130812Smarcel sprintf (sndbuf, format, arg_string); 317130812Smarcel break; 318130812Smarcel default: 319130812Smarcel arg_int = va_arg (args, long); 320130812Smarcel sprintf (sndbuf, format, arg_int); 321130812Smarcel break; 322130812Smarcel } 323130812Smarcel sndbuf += strlen (sndbuf); 324130812Smarcel } 325130812Smarcel else 326130812Smarcel *sndbuf++ = *p; 327130812Smarcel } 328130812Smarcel *sndbuf = '\0'; 329130812Smarcel} 330130812Smarcel 331130812Smarcel 332130812Smarcel/* monitor_printf_noecho -- Send data to monitor, but don't expect an echo. 333130812Smarcel Works just like printf. */ 334130812Smarcel 335130812Smarcelvoid 336130812Smarcelmonitor_printf_noecho (char *pattern,...) 337130812Smarcel{ 338130812Smarcel va_list args; 339130812Smarcel char sndbuf[2000]; 340130812Smarcel int len; 341130812Smarcel 342130812Smarcel va_start (args, pattern); 343130812Smarcel 344130812Smarcel monitor_vsprintf (sndbuf, pattern, args); 345130812Smarcel 346130812Smarcel len = strlen (sndbuf); 347130812Smarcel if (len + 1 > sizeof sndbuf) 348130812Smarcel internal_error (__FILE__, __LINE__, "failed internal consistency check"); 349130812Smarcel 350130812Smarcel if (monitor_debug_p) 351130812Smarcel { 352130812Smarcel char *safe_string = (char *) alloca ((strlen (sndbuf) * 4) + 1); 353130812Smarcel monitor_printable_string (safe_string, sndbuf, 0); 354130812Smarcel fprintf_unfiltered (gdb_stdlog, "sent[%s]\n", safe_string); 355130812Smarcel } 356130812Smarcel 357130812Smarcel monitor_write (sndbuf, len); 358130812Smarcel} 359130812Smarcel 360130812Smarcel/* monitor_printf -- Send data to monitor and check the echo. Works just like 361130812Smarcel printf. */ 362130812Smarcel 363130812Smarcelvoid 364130812Smarcelmonitor_printf (char *pattern,...) 365130812Smarcel{ 366130812Smarcel va_list args; 367130812Smarcel char sndbuf[2000]; 368130812Smarcel int len; 369130812Smarcel 370130812Smarcel va_start (args, pattern); 371130812Smarcel 372130812Smarcel monitor_vsprintf (sndbuf, pattern, args); 373130812Smarcel 374130812Smarcel len = strlen (sndbuf); 375130812Smarcel if (len + 1 > sizeof sndbuf) 376130812Smarcel internal_error (__FILE__, __LINE__, "failed internal consistency check"); 377130812Smarcel 378130812Smarcel if (monitor_debug_p) 379130812Smarcel { 380130812Smarcel char *safe_string = (char *) alloca ((len * 4) + 1); 381130812Smarcel monitor_printable_string (safe_string, sndbuf, 0); 382130812Smarcel fprintf_unfiltered (gdb_stdlog, "sent[%s]\n", safe_string); 383130812Smarcel } 384130812Smarcel 385130812Smarcel monitor_write (sndbuf, len); 386130812Smarcel 387130812Smarcel /* We used to expect that the next immediate output was the characters we 388130812Smarcel just output, but sometimes some extra junk appeared before the characters 389130812Smarcel we expected, like an extra prompt, or a portmaster sending telnet negotiations. 390130812Smarcel So, just start searching for what we sent, and skip anything unknown. */ 391130812Smarcel monitor_debug ("ExpectEcho\n"); 392130812Smarcel monitor_expect (sndbuf, (char *) 0, 0); 393130812Smarcel} 394130812Smarcel 395130812Smarcel 396130812Smarcel/* Write characters to the remote system. */ 397130812Smarcel 398130812Smarcelvoid 399130812Smarcelmonitor_write (char *buf, int buflen) 400130812Smarcel{ 401130812Smarcel if (serial_write (monitor_desc, buf, buflen)) 402130812Smarcel fprintf_unfiltered (gdb_stderr, "serial_write failed: %s\n", 403130812Smarcel safe_strerror (errno)); 404130812Smarcel} 405130812Smarcel 406130812Smarcel 407130812Smarcel/* Read a binary character from the remote system, doing all the fancy 408130812Smarcel timeout stuff, but without interpreting the character in any way, 409130812Smarcel and without printing remote debug information. */ 410130812Smarcel 411130812Smarcelint 412130812Smarcelmonitor_readchar (void) 413130812Smarcel{ 414130812Smarcel int c; 415130812Smarcel int looping; 416130812Smarcel 417130812Smarcel do 418130812Smarcel { 419130812Smarcel looping = 0; 420130812Smarcel c = serial_readchar (monitor_desc, timeout); 421130812Smarcel 422130812Smarcel if (c >= 0) 423130812Smarcel c &= 0xff; /* don't lose bit 7 */ 424130812Smarcel } 425130812Smarcel while (looping); 426130812Smarcel 427130812Smarcel if (c >= 0) 428130812Smarcel return c; 429130812Smarcel 430130812Smarcel if (c == SERIAL_TIMEOUT) 431130812Smarcel error ("Timeout reading from remote system."); 432130812Smarcel 433130812Smarcel perror_with_name ("remote-monitor"); 434130812Smarcel} 435130812Smarcel 436130812Smarcel 437130812Smarcel/* Read a character from the remote system, doing all the fancy 438130812Smarcel timeout stuff. */ 439130812Smarcel 440130812Smarcelstatic int 441130812Smarcelreadchar (int timeout) 442130812Smarcel{ 443130812Smarcel int c; 444130812Smarcel static enum 445130812Smarcel { 446130812Smarcel last_random, last_nl, last_cr, last_crnl 447130812Smarcel } 448130812Smarcel state = last_random; 449130812Smarcel int looping; 450130812Smarcel 451130812Smarcel do 452130812Smarcel { 453130812Smarcel looping = 0; 454130812Smarcel c = serial_readchar (monitor_desc, timeout); 455130812Smarcel 456130812Smarcel if (c >= 0) 457130812Smarcel { 458130812Smarcel c &= 0x7f; 459130812Smarcel /* This seems to interfere with proper function of the 460130812Smarcel input stream */ 461130812Smarcel if (monitor_debug_p || remote_debug) 462130812Smarcel { 463130812Smarcel char buf[2]; 464130812Smarcel buf[0] = c; 465130812Smarcel buf[1] = '\0'; 466130812Smarcel puts_debug ("read -->", buf, "<--"); 467130812Smarcel } 468130812Smarcel 469130812Smarcel } 470130812Smarcel 471130812Smarcel /* Canonicialize \n\r combinations into one \r */ 472130812Smarcel if ((current_monitor->flags & MO_HANDLE_NL) != 0) 473130812Smarcel { 474130812Smarcel if ((c == '\r' && state == last_nl) 475130812Smarcel || (c == '\n' && state == last_cr)) 476130812Smarcel { 477130812Smarcel state = last_crnl; 478130812Smarcel looping = 1; 479130812Smarcel } 480130812Smarcel else if (c == '\r') 481130812Smarcel state = last_cr; 482130812Smarcel else if (c != '\n') 483130812Smarcel state = last_random; 484130812Smarcel else 485130812Smarcel { 486130812Smarcel state = last_nl; 487130812Smarcel c = '\r'; 488130812Smarcel } 489130812Smarcel } 490130812Smarcel } 491130812Smarcel while (looping); 492130812Smarcel 493130812Smarcel if (c >= 0) 494130812Smarcel return c; 495130812Smarcel 496130812Smarcel if (c == SERIAL_TIMEOUT) 497130812Smarcel#if 0 498130812Smarcel /* I fail to see how detaching here can be useful */ 499130812Smarcel if (in_monitor_wait) /* Watchdog went off */ 500130812Smarcel { 501130812Smarcel target_mourn_inferior (); 502130812Smarcel error ("GDB serial timeout has expired. Target detached.\n"); 503130812Smarcel } 504130812Smarcel else 505130812Smarcel#endif 506130812Smarcel error ("Timeout reading from remote system."); 507130812Smarcel 508130812Smarcel perror_with_name ("remote-monitor"); 509130812Smarcel} 510130812Smarcel 511130812Smarcel/* Scan input from the remote system, until STRING is found. If BUF is non- 512130812Smarcel zero, then collect input until we have collected either STRING or BUFLEN-1 513130812Smarcel chars. In either case we terminate BUF with a 0. If input overflows BUF 514130812Smarcel because STRING can't be found, return -1, else return number of chars in BUF 515130812Smarcel (minus the terminating NUL). Note that in the non-overflow case, STRING 516130812Smarcel will be at the end of BUF. */ 517130812Smarcel 518130812Smarcelint 519130812Smarcelmonitor_expect (char *string, char *buf, int buflen) 520130812Smarcel{ 521130812Smarcel char *p = string; 522130812Smarcel int obuflen = buflen; 523130812Smarcel int c; 524130812Smarcel 525130812Smarcel if (monitor_debug_p) 526130812Smarcel { 527130812Smarcel char *safe_string = (char *) alloca ((strlen (string) * 4) + 1); 528130812Smarcel monitor_printable_string (safe_string, string, 0); 529130812Smarcel fprintf_unfiltered (gdb_stdlog, "MON Expecting '%s'\n", safe_string); 530130812Smarcel } 531130812Smarcel 532130812Smarcel immediate_quit++; 533130812Smarcel while (1) 534130812Smarcel { 535130812Smarcel if (buf) 536130812Smarcel { 537130812Smarcel if (buflen < 2) 538130812Smarcel { 539130812Smarcel *buf = '\000'; 540130812Smarcel immediate_quit--; 541130812Smarcel return -1; 542130812Smarcel } 543130812Smarcel 544130812Smarcel c = readchar (timeout); 545130812Smarcel if (c == '\000') 546130812Smarcel continue; 547130812Smarcel *buf++ = c; 548130812Smarcel buflen--; 549130812Smarcel } 550130812Smarcel else 551130812Smarcel c = readchar (timeout); 552130812Smarcel 553130812Smarcel /* Don't expect any ^C sent to be echoed */ 554130812Smarcel 555130812Smarcel if (*p == '\003' || c == *p) 556130812Smarcel { 557130812Smarcel p++; 558130812Smarcel if (*p == '\0') 559130812Smarcel { 560130812Smarcel immediate_quit--; 561130812Smarcel 562130812Smarcel if (buf) 563130812Smarcel { 564130812Smarcel *buf++ = '\000'; 565130812Smarcel return obuflen - buflen; 566130812Smarcel } 567130812Smarcel else 568130812Smarcel return 0; 569130812Smarcel } 570130812Smarcel } 571130812Smarcel else 572130812Smarcel { 573130812Smarcel /* We got a character that doesn't match the string. We need to 574130812Smarcel back up p, but how far? If we're looking for "..howdy" and the 575130812Smarcel monitor sends "...howdy"? There's certainly a match in there, 576130812Smarcel but when we receive the third ".", we won't find it if we just 577130812Smarcel restart the matching at the beginning of the string. 578130812Smarcel 579130812Smarcel This is a Boyer-Moore kind of situation. We want to reset P to 580130812Smarcel the end of the longest prefix of STRING that is a suffix of 581130812Smarcel what we've read so far. In the example above, that would be 582130812Smarcel ".." --- the longest prefix of "..howdy" that is a suffix of 583130812Smarcel "...". This longest prefix could be the empty string, if C 584130812Smarcel is nowhere to be found in STRING. 585130812Smarcel 586130812Smarcel If this longest prefix is not the empty string, it must contain 587130812Smarcel C, so let's search from the end of STRING for instances of C, 588130812Smarcel and see if the portion of STRING before that is a suffix of 589130812Smarcel what we read before C. Actually, we can search backwards from 590130812Smarcel p, since we know no prefix can be longer than that. 591130812Smarcel 592130812Smarcel Note that we can use STRING itself, along with C, as a record 593130812Smarcel of what we've received so far. :) */ 594130812Smarcel int i; 595130812Smarcel 596130812Smarcel for (i = (p - string) - 1; i >= 0; i--) 597130812Smarcel if (string[i] == c) 598130812Smarcel { 599130812Smarcel /* Is this prefix a suffix of what we've read so far? 600130812Smarcel In other words, does 601130812Smarcel string[0 .. i-1] == string[p - i, p - 1]? */ 602130812Smarcel if (! memcmp (string, p - i, i)) 603130812Smarcel { 604130812Smarcel p = string + i + 1; 605130812Smarcel break; 606130812Smarcel } 607130812Smarcel } 608130812Smarcel if (i < 0) 609130812Smarcel p = string; 610130812Smarcel } 611130812Smarcel } 612130812Smarcel} 613130812Smarcel 614130812Smarcel/* Search for a regexp. */ 615130812Smarcel 616130812Smarcelstatic int 617130812Smarcelmonitor_expect_regexp (struct re_pattern_buffer *pat, char *buf, int buflen) 618130812Smarcel{ 619130812Smarcel char *mybuf; 620130812Smarcel char *p; 621130812Smarcel monitor_debug ("MON Expecting regexp\n"); 622130812Smarcel if (buf) 623130812Smarcel mybuf = buf; 624130812Smarcel else 625130812Smarcel { 626130812Smarcel mybuf = alloca (TARGET_BUF_SIZE); 627130812Smarcel buflen = TARGET_BUF_SIZE; 628130812Smarcel } 629130812Smarcel 630130812Smarcel p = mybuf; 631130812Smarcel while (1) 632130812Smarcel { 633130812Smarcel int retval; 634130812Smarcel 635130812Smarcel if (p - mybuf >= buflen) 636130812Smarcel { /* Buffer about to overflow */ 637130812Smarcel 638130812Smarcel/* On overflow, we copy the upper half of the buffer to the lower half. Not 639130812Smarcel great, but it usually works... */ 640130812Smarcel 641130812Smarcel memcpy (mybuf, mybuf + buflen / 2, buflen / 2); 642130812Smarcel p = mybuf + buflen / 2; 643130812Smarcel } 644130812Smarcel 645130812Smarcel *p++ = readchar (timeout); 646130812Smarcel 647130812Smarcel retval = re_search (pat, mybuf, p - mybuf, 0, p - mybuf, NULL); 648130812Smarcel if (retval >= 0) 649130812Smarcel return 1; 650130812Smarcel } 651130812Smarcel} 652130812Smarcel 653130812Smarcel/* Keep discarding input until we see the MONITOR prompt. 654130812Smarcel 655130812Smarcel The convention for dealing with the prompt is that you 656130812Smarcel o give your command 657130812Smarcel o *then* wait for the prompt. 658130812Smarcel 659130812Smarcel Thus the last thing that a procedure does with the serial line will 660130812Smarcel be an monitor_expect_prompt(). Exception: monitor_resume does not 661130812Smarcel wait for the prompt, because the terminal is being handed over to 662130812Smarcel the inferior. However, the next thing which happens after that is 663130812Smarcel a monitor_wait which does wait for the prompt. Note that this 664130812Smarcel includes abnormal exit, e.g. error(). This is necessary to prevent 665130812Smarcel getting into states from which we can't recover. */ 666130812Smarcel 667130812Smarcelint 668130812Smarcelmonitor_expect_prompt (char *buf, int buflen) 669130812Smarcel{ 670130812Smarcel monitor_debug ("MON Expecting prompt\n"); 671130812Smarcel return monitor_expect (current_monitor->prompt, buf, buflen); 672130812Smarcel} 673130812Smarcel 674130812Smarcel/* Get N 32-bit words from remote, each preceded by a space, and put 675130812Smarcel them in registers starting at REGNO. */ 676130812Smarcel 677130812Smarcel#if 0 678130812Smarcelstatic unsigned long 679130812Smarcelget_hex_word (void) 680130812Smarcel{ 681130812Smarcel unsigned long val; 682130812Smarcel int i; 683130812Smarcel int ch; 684130812Smarcel 685130812Smarcel do 686130812Smarcel ch = readchar (timeout); 687130812Smarcel while (isspace (ch)); 688130812Smarcel 689130812Smarcel val = from_hex (ch); 690130812Smarcel 691130812Smarcel for (i = 7; i >= 1; i--) 692130812Smarcel { 693130812Smarcel ch = readchar (timeout); 694130812Smarcel if (!isxdigit (ch)) 695130812Smarcel break; 696130812Smarcel val = (val << 4) | from_hex (ch); 697130812Smarcel } 698130812Smarcel 699130812Smarcel return val; 700130812Smarcel} 701130812Smarcel#endif 702130812Smarcel 703130812Smarcelstatic void 704130812Smarcelcompile_pattern (char *pattern, struct re_pattern_buffer *compiled_pattern, 705130812Smarcel char *fastmap) 706130812Smarcel{ 707130812Smarcel int tmp; 708130812Smarcel const char *val; 709130812Smarcel 710130812Smarcel compiled_pattern->fastmap = fastmap; 711130812Smarcel 712130812Smarcel tmp = re_set_syntax (RE_SYNTAX_EMACS); 713130812Smarcel val = re_compile_pattern (pattern, 714130812Smarcel strlen (pattern), 715130812Smarcel compiled_pattern); 716130812Smarcel re_set_syntax (tmp); 717130812Smarcel 718130812Smarcel if (val) 719130812Smarcel error ("compile_pattern: Can't compile pattern string `%s': %s!", pattern, val); 720130812Smarcel 721130812Smarcel if (fastmap) 722130812Smarcel re_compile_fastmap (compiled_pattern); 723130812Smarcel} 724130812Smarcel 725130812Smarcel/* Open a connection to a remote debugger. NAME is the filename used 726130812Smarcel for communication. */ 727130812Smarcel 728130812Smarcelvoid 729130812Smarcelmonitor_open (char *args, struct monitor_ops *mon_ops, int from_tty) 730130812Smarcel{ 731130812Smarcel char *name; 732130812Smarcel char **p; 733130812Smarcel 734130812Smarcel if (mon_ops->magic != MONITOR_OPS_MAGIC) 735130812Smarcel error ("Magic number of monitor_ops struct wrong."); 736130812Smarcel 737130812Smarcel targ_ops = mon_ops->target; 738130812Smarcel name = targ_ops->to_shortname; 739130812Smarcel 740130812Smarcel if (!args) 741130812Smarcel error ("Use `target %s DEVICE-NAME' to use a serial port, or \n\ 742130812Smarcel`target %s HOST-NAME:PORT-NUMBER' to use a network connection.", name, name); 743130812Smarcel 744130812Smarcel target_preopen (from_tty); 745130812Smarcel 746130812Smarcel /* Setup pattern for register dump */ 747130812Smarcel 748130812Smarcel if (mon_ops->register_pattern) 749130812Smarcel compile_pattern (mon_ops->register_pattern, ®ister_pattern, 750130812Smarcel register_fastmap); 751130812Smarcel 752130812Smarcel if (mon_ops->getmem.resp_delim) 753130812Smarcel compile_pattern (mon_ops->getmem.resp_delim, &getmem_resp_delim_pattern, 754130812Smarcel getmem_resp_delim_fastmap); 755130812Smarcel 756130812Smarcel if (mon_ops->setmem.resp_delim) 757130812Smarcel compile_pattern (mon_ops->setmem.resp_delim, &setmem_resp_delim_pattern, 758130812Smarcel setmem_resp_delim_fastmap); 759130812Smarcel 760130812Smarcel if (mon_ops->setreg.resp_delim) 761130812Smarcel compile_pattern (mon_ops->setreg.resp_delim, &setreg_resp_delim_pattern, 762130812Smarcel setreg_resp_delim_fastmap); 763130812Smarcel 764130812Smarcel unpush_target (targ_ops); 765130812Smarcel 766130812Smarcel if (dev_name) 767130812Smarcel xfree (dev_name); 768130812Smarcel dev_name = xstrdup (args); 769130812Smarcel 770130812Smarcel monitor_desc = serial_open (dev_name); 771130812Smarcel 772130812Smarcel if (!monitor_desc) 773130812Smarcel perror_with_name (dev_name); 774130812Smarcel 775130812Smarcel if (baud_rate != -1) 776130812Smarcel { 777130812Smarcel if (serial_setbaudrate (monitor_desc, baud_rate)) 778130812Smarcel { 779130812Smarcel serial_close (monitor_desc); 780130812Smarcel perror_with_name (dev_name); 781130812Smarcel } 782130812Smarcel } 783130812Smarcel 784130812Smarcel serial_raw (monitor_desc); 785130812Smarcel 786130812Smarcel serial_flush_input (monitor_desc); 787130812Smarcel 788130812Smarcel /* some systems only work with 2 stop bits */ 789130812Smarcel 790130812Smarcel serial_setstopbits (monitor_desc, mon_ops->stopbits); 791130812Smarcel 792130812Smarcel current_monitor = mon_ops; 793130812Smarcel 794130812Smarcel /* See if we can wake up the monitor. First, try sending a stop sequence, 795130812Smarcel then send the init strings. Last, remove all breakpoints. */ 796130812Smarcel 797130812Smarcel if (current_monitor->stop) 798130812Smarcel { 799130812Smarcel monitor_stop (); 800130812Smarcel if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0) 801130812Smarcel { 802130812Smarcel monitor_debug ("EXP Open echo\n"); 803130812Smarcel monitor_expect_prompt (NULL, 0); 804130812Smarcel } 805130812Smarcel } 806130812Smarcel 807130812Smarcel /* wake up the monitor and see if it's alive */ 808130812Smarcel for (p = mon_ops->init; *p != NULL; p++) 809130812Smarcel { 810130812Smarcel /* Some of the characters we send may not be echoed, 811130812Smarcel but we hope to get a prompt at the end of it all. */ 812130812Smarcel 813130812Smarcel if ((current_monitor->flags & MO_NO_ECHO_ON_OPEN) == 0) 814130812Smarcel monitor_printf (*p); 815130812Smarcel else 816130812Smarcel monitor_printf_noecho (*p); 817130812Smarcel monitor_expect_prompt (NULL, 0); 818130812Smarcel } 819130812Smarcel 820130812Smarcel serial_flush_input (monitor_desc); 821130812Smarcel 822130812Smarcel /* Alloc breakpoints */ 823130812Smarcel if (mon_ops->set_break != NULL) 824130812Smarcel { 825130812Smarcel if (mon_ops->num_breakpoints == 0) 826130812Smarcel mon_ops->num_breakpoints = 8; 827130812Smarcel 828130812Smarcel breakaddr = (CORE_ADDR *) xmalloc (mon_ops->num_breakpoints * sizeof (CORE_ADDR)); 829130812Smarcel memset (breakaddr, 0, mon_ops->num_breakpoints * sizeof (CORE_ADDR)); 830130812Smarcel } 831130812Smarcel 832130812Smarcel /* Remove all breakpoints */ 833130812Smarcel 834130812Smarcel if (mon_ops->clr_all_break) 835130812Smarcel { 836130812Smarcel monitor_printf (mon_ops->clr_all_break); 837130812Smarcel monitor_expect_prompt (NULL, 0); 838130812Smarcel } 839130812Smarcel 840130812Smarcel if (from_tty) 841130812Smarcel printf_unfiltered ("Remote target %s connected to %s\n", name, dev_name); 842130812Smarcel 843130812Smarcel push_target (targ_ops); 844130812Smarcel 845130812Smarcel inferior_ptid = pid_to_ptid (42000); /* Make run command think we are busy... */ 846130812Smarcel 847130812Smarcel /* Give monitor_wait something to read */ 848130812Smarcel 849130812Smarcel monitor_printf (current_monitor->line_term); 850130812Smarcel 851130812Smarcel start_remote (); 852130812Smarcel} 853130812Smarcel 854130812Smarcel/* Close out all files and local state before this target loses 855130812Smarcel control. */ 856130812Smarcel 857130812Smarcelvoid 858130812Smarcelmonitor_close (int quitting) 859130812Smarcel{ 860130812Smarcel if (monitor_desc) 861130812Smarcel serial_close (monitor_desc); 862130812Smarcel 863130812Smarcel /* Free breakpoint memory */ 864130812Smarcel if (breakaddr != NULL) 865130812Smarcel { 866130812Smarcel xfree (breakaddr); 867130812Smarcel breakaddr = NULL; 868130812Smarcel } 869130812Smarcel 870130812Smarcel monitor_desc = NULL; 871130812Smarcel} 872130812Smarcel 873130812Smarcel/* Terminate the open connection to the remote debugger. Use this 874130812Smarcel when you want to detach and do something else with your gdb. */ 875130812Smarcel 876130812Smarcelstatic void 877130812Smarcelmonitor_detach (char *args, int from_tty) 878130812Smarcel{ 879130812Smarcel pop_target (); /* calls monitor_close to do the real work */ 880130812Smarcel if (from_tty) 881130812Smarcel printf_unfiltered ("Ending remote %s debugging\n", target_shortname); 882130812Smarcel} 883130812Smarcel 884130812Smarcel/* Convert VALSTR into the target byte-ordered value of REGNO and store it. */ 885130812Smarcel 886130812Smarcelchar * 887130812Smarcelmonitor_supply_register (int regno, char *valstr) 888130812Smarcel{ 889130812Smarcel ULONGEST val; 890130812Smarcel unsigned char regbuf[MAX_REGISTER_SIZE]; 891130812Smarcel char *p; 892130812Smarcel 893130812Smarcel val = 0; 894130812Smarcel p = valstr; 895130812Smarcel while (p && *p != '\0') 896130812Smarcel { 897130812Smarcel if (*p == '\r' || *p == '\n') 898130812Smarcel { 899130812Smarcel while (*p != '\0') 900130812Smarcel p++; 901130812Smarcel break; 902130812Smarcel } 903130812Smarcel if (isspace (*p)) 904130812Smarcel { 905130812Smarcel p++; 906130812Smarcel continue; 907130812Smarcel } 908130812Smarcel if (!isxdigit (*p) && *p != 'x') 909130812Smarcel { 910130812Smarcel break; 911130812Smarcel } 912130812Smarcel 913130812Smarcel val <<= 4; 914130812Smarcel val += fromhex (*p++); 915130812Smarcel } 916130812Smarcel monitor_debug ("Supplying Register %d %s\n", regno, valstr); 917130812Smarcel 918130812Smarcel if (val == 0 && valstr == p) 919130812Smarcel error ("monitor_supply_register (%d): bad value from monitor: %s.", 920130812Smarcel regno, valstr); 921130812Smarcel 922130812Smarcel /* supply register stores in target byte order, so swap here */ 923130812Smarcel 924130812Smarcel store_unsigned_integer (regbuf, DEPRECATED_REGISTER_RAW_SIZE (regno), val); 925130812Smarcel 926130812Smarcel supply_register (regno, regbuf); 927130812Smarcel 928130812Smarcel return p; 929130812Smarcel} 930130812Smarcel 931130812Smarcel/* Tell the remote machine to resume. */ 932130812Smarcel 933130812Smarcelstatic void 934130812Smarcelmonitor_resume (ptid_t ptid, int step, enum target_signal sig) 935130812Smarcel{ 936130812Smarcel /* Some monitors require a different command when starting a program */ 937130812Smarcel monitor_debug ("MON resume\n"); 938130812Smarcel if (current_monitor->flags & MO_RUN_FIRST_TIME && first_time == 1) 939130812Smarcel { 940130812Smarcel first_time = 0; 941130812Smarcel monitor_printf ("run\r"); 942130812Smarcel if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT) 943130812Smarcel dump_reg_flag = 1; 944130812Smarcel return; 945130812Smarcel } 946130812Smarcel if (step) 947130812Smarcel monitor_printf (current_monitor->step); 948130812Smarcel else 949130812Smarcel { 950130812Smarcel if (current_monitor->continue_hook) 951130812Smarcel (*current_monitor->continue_hook) (); 952130812Smarcel else 953130812Smarcel monitor_printf (current_monitor->cont); 954130812Smarcel if (current_monitor->flags & MO_NEED_REGDUMP_AFTER_CONT) 955130812Smarcel dump_reg_flag = 1; 956130812Smarcel } 957130812Smarcel} 958130812Smarcel 959130812Smarcel/* Parse the output of a register dump command. A monitor specific 960130812Smarcel regexp is used to extract individual register descriptions of the 961130812Smarcel form REG=VAL. Each description is split up into a name and a value 962130812Smarcel string which are passed down to monitor specific code. */ 963130812Smarcel 964130812Smarcelstatic void 965130812Smarcelparse_register_dump (char *buf, int len) 966130812Smarcel{ 967130812Smarcel monitor_debug ("MON Parsing register dump\n"); 968130812Smarcel while (1) 969130812Smarcel { 970130812Smarcel int regnamelen, vallen; 971130812Smarcel char *regname, *val; 972130812Smarcel /* Element 0 points to start of register name, and element 1 973130812Smarcel points to the start of the register value. */ 974130812Smarcel struct re_registers register_strings; 975130812Smarcel 976130812Smarcel memset (®ister_strings, 0, sizeof (struct re_registers)); 977130812Smarcel 978130812Smarcel if (re_search (®ister_pattern, buf, len, 0, len, 979130812Smarcel ®ister_strings) == -1) 980130812Smarcel break; 981130812Smarcel 982130812Smarcel regnamelen = register_strings.end[1] - register_strings.start[1]; 983130812Smarcel regname = buf + register_strings.start[1]; 984130812Smarcel vallen = register_strings.end[2] - register_strings.start[2]; 985130812Smarcel val = buf + register_strings.start[2]; 986130812Smarcel 987130812Smarcel current_monitor->supply_register (regname, regnamelen, val, vallen); 988130812Smarcel 989130812Smarcel buf += register_strings.end[0]; 990130812Smarcel len -= register_strings.end[0]; 991130812Smarcel } 992130812Smarcel} 993130812Smarcel 994130812Smarcel/* Send ^C to target to halt it. Target will respond, and send us a 995130812Smarcel packet. */ 996130812Smarcel 997130812Smarcelstatic void 998130812Smarcelmonitor_interrupt (int signo) 999130812Smarcel{ 1000130812Smarcel /* If this doesn't work, try more severe steps. */ 1001130812Smarcel signal (signo, monitor_interrupt_twice); 1002130812Smarcel 1003130812Smarcel if (monitor_debug_p || remote_debug) 1004130812Smarcel fprintf_unfiltered (gdb_stdlog, "monitor_interrupt called\n"); 1005130812Smarcel 1006130812Smarcel target_stop (); 1007130812Smarcel} 1008130812Smarcel 1009130812Smarcel/* The user typed ^C twice. */ 1010130812Smarcel 1011130812Smarcelstatic void 1012130812Smarcelmonitor_interrupt_twice (int signo) 1013130812Smarcel{ 1014130812Smarcel signal (signo, ofunc); 1015130812Smarcel 1016130812Smarcel monitor_interrupt_query (); 1017130812Smarcel 1018130812Smarcel signal (signo, monitor_interrupt); 1019130812Smarcel} 1020130812Smarcel 1021130812Smarcel/* Ask the user what to do when an interrupt is received. */ 1022130812Smarcel 1023130812Smarcelstatic void 1024130812Smarcelmonitor_interrupt_query (void) 1025130812Smarcel{ 1026130812Smarcel target_terminal_ours (); 1027130812Smarcel 1028130812Smarcel if (query ("Interrupted while waiting for the program.\n\ 1029130812SmarcelGive up (and stop debugging it)? ")) 1030130812Smarcel { 1031130812Smarcel target_mourn_inferior (); 1032130812Smarcel throw_exception (RETURN_QUIT); 1033130812Smarcel } 1034130812Smarcel 1035130812Smarcel target_terminal_inferior (); 1036130812Smarcel} 1037130812Smarcel 1038130812Smarcelstatic void 1039130812Smarcelmonitor_wait_cleanup (void *old_timeout) 1040130812Smarcel{ 1041130812Smarcel timeout = *(int *) old_timeout; 1042130812Smarcel signal (SIGINT, ofunc); 1043130812Smarcel in_monitor_wait = 0; 1044130812Smarcel} 1045130812Smarcel 1046130812Smarcel 1047130812Smarcel 1048130812Smarcelstatic void 1049130812Smarcelmonitor_wait_filter (char *buf, 1050130812Smarcel int bufmax, 1051130812Smarcel int *ext_resp_len, 1052130812Smarcel struct target_waitstatus *status) 1053130812Smarcel{ 1054130812Smarcel int resp_len; 1055130812Smarcel do 1056130812Smarcel { 1057130812Smarcel resp_len = monitor_expect_prompt (buf, bufmax); 1058130812Smarcel *ext_resp_len = resp_len; 1059130812Smarcel 1060130812Smarcel if (resp_len <= 0) 1061130812Smarcel fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf); 1062130812Smarcel } 1063130812Smarcel while (resp_len < 0); 1064130812Smarcel 1065130812Smarcel /* Print any output characters that were preceded by ^O. */ 1066130812Smarcel /* FIXME - This would be great as a user settabgle flag */ 1067130812Smarcel if (monitor_debug_p || remote_debug 1068130812Smarcel || current_monitor->flags & MO_PRINT_PROGRAM_OUTPUT) 1069130812Smarcel { 1070130812Smarcel int i; 1071130812Smarcel 1072130812Smarcel for (i = 0; i < resp_len - 1; i++) 1073130812Smarcel if (buf[i] == 0x0f) 1074130812Smarcel putchar_unfiltered (buf[++i]); 1075130812Smarcel } 1076130812Smarcel} 1077130812Smarcel 1078130812Smarcel 1079130812Smarcel 1080130812Smarcel/* Wait until the remote machine stops, then return, storing status in 1081130812Smarcel status just as `wait' would. */ 1082130812Smarcel 1083130812Smarcelstatic ptid_t 1084130812Smarcelmonitor_wait (ptid_t ptid, struct target_waitstatus *status) 1085130812Smarcel{ 1086130812Smarcel int old_timeout = timeout; 1087130812Smarcel char buf[TARGET_BUF_SIZE]; 1088130812Smarcel int resp_len; 1089130812Smarcel struct cleanup *old_chain; 1090130812Smarcel 1091130812Smarcel status->kind = TARGET_WAITKIND_EXITED; 1092130812Smarcel status->value.integer = 0; 1093130812Smarcel 1094130812Smarcel old_chain = make_cleanup (monitor_wait_cleanup, &old_timeout); 1095130812Smarcel monitor_debug ("MON wait\n"); 1096130812Smarcel 1097130812Smarcel#if 0 1098130812Smarcel /* This is somthing other than a maintenance command */ 1099130812Smarcel in_monitor_wait = 1; 1100130812Smarcel timeout = watchdog > 0 ? watchdog : -1; 1101130812Smarcel#else 1102130812Smarcel timeout = -1; /* Don't time out -- user program is running. */ 1103130812Smarcel#endif 1104130812Smarcel 1105130812Smarcel ofunc = (void (*)()) signal (SIGINT, monitor_interrupt); 1106130812Smarcel 1107130812Smarcel if (current_monitor->wait_filter) 1108130812Smarcel (*current_monitor->wait_filter) (buf, sizeof (buf), &resp_len, status); 1109130812Smarcel else 1110130812Smarcel monitor_wait_filter (buf, sizeof (buf), &resp_len, status); 1111130812Smarcel 1112130812Smarcel#if 0 /* Transferred to monitor wait filter */ 1113130812Smarcel do 1114130812Smarcel { 1115130812Smarcel resp_len = monitor_expect_prompt (buf, sizeof (buf)); 1116130812Smarcel 1117130812Smarcel if (resp_len <= 0) 1118130812Smarcel fprintf_unfiltered (gdb_stderr, "monitor_wait: excessive response from monitor: %s.", buf); 1119130812Smarcel } 1120130812Smarcel while (resp_len < 0); 1121130812Smarcel 1122130812Smarcel /* Print any output characters that were preceded by ^O. */ 1123130812Smarcel /* FIXME - This would be great as a user settabgle flag */ 1124130812Smarcel if (monitor_debug_p || remote_debug 1125130812Smarcel || current_monitor->flags & MO_PRINT_PROGRAM_OUTPUT) 1126130812Smarcel { 1127130812Smarcel int i; 1128130812Smarcel 1129130812Smarcel for (i = 0; i < resp_len - 1; i++) 1130130812Smarcel if (buf[i] == 0x0f) 1131130812Smarcel putchar_unfiltered (buf[++i]); 1132130812Smarcel } 1133130812Smarcel#endif 1134130812Smarcel 1135130812Smarcel signal (SIGINT, ofunc); 1136130812Smarcel 1137130812Smarcel timeout = old_timeout; 1138130812Smarcel#if 0 1139130812Smarcel if (dump_reg_flag && current_monitor->dump_registers) 1140130812Smarcel { 1141130812Smarcel dump_reg_flag = 0; 1142130812Smarcel monitor_printf (current_monitor->dump_registers); 1143130812Smarcel resp_len = monitor_expect_prompt (buf, sizeof (buf)); 1144130812Smarcel } 1145130812Smarcel 1146130812Smarcel if (current_monitor->register_pattern) 1147130812Smarcel parse_register_dump (buf, resp_len); 1148130812Smarcel#else 1149130812Smarcel monitor_debug ("Wait fetching registers after stop\n"); 1150130812Smarcel monitor_dump_regs (); 1151130812Smarcel#endif 1152130812Smarcel 1153130812Smarcel status->kind = TARGET_WAITKIND_STOPPED; 1154130812Smarcel status->value.sig = TARGET_SIGNAL_TRAP; 1155130812Smarcel 1156130812Smarcel discard_cleanups (old_chain); 1157130812Smarcel 1158130812Smarcel in_monitor_wait = 0; 1159130812Smarcel 1160130812Smarcel return inferior_ptid; 1161130812Smarcel} 1162130812Smarcel 1163130812Smarcel/* Fetch register REGNO, or all registers if REGNO is -1. Returns 1164130812Smarcel errno value. */ 1165130812Smarcel 1166130812Smarcelstatic void 1167130812Smarcelmonitor_fetch_register (int regno) 1168130812Smarcel{ 1169130812Smarcel const char *name; 1170130812Smarcel char *zerobuf; 1171130812Smarcel char *regbuf; 1172130812Smarcel int i; 1173130812Smarcel 1174130812Smarcel regbuf = alloca (MAX_REGISTER_SIZE * 2 + 1); 1175130812Smarcel zerobuf = alloca (MAX_REGISTER_SIZE); 1176130812Smarcel memset (zerobuf, 0, MAX_REGISTER_SIZE); 1177130812Smarcel 1178130812Smarcel if (current_monitor->regname != NULL) 1179130812Smarcel name = current_monitor->regname (regno); 1180130812Smarcel else 1181130812Smarcel name = current_monitor->regnames[regno]; 1182130812Smarcel monitor_debug ("MON fetchreg %d '%s'\n", regno, name ? name : "(null name)"); 1183130812Smarcel 1184130812Smarcel if (!name || (*name == '\0')) 1185130812Smarcel { 1186130812Smarcel monitor_debug ("No register known for %d\n", regno); 1187130812Smarcel supply_register (regno, zerobuf); 1188130812Smarcel return; 1189130812Smarcel } 1190130812Smarcel 1191130812Smarcel /* send the register examine command */ 1192130812Smarcel 1193130812Smarcel monitor_printf (current_monitor->getreg.cmd, name); 1194130812Smarcel 1195130812Smarcel /* If RESP_DELIM is specified, we search for that as a leading 1196130812Smarcel delimiter for the register value. Otherwise, we just start 1197130812Smarcel searching from the start of the buf. */ 1198130812Smarcel 1199130812Smarcel if (current_monitor->getreg.resp_delim) 1200130812Smarcel { 1201130812Smarcel monitor_debug ("EXP getreg.resp_delim\n"); 1202130812Smarcel monitor_expect (current_monitor->getreg.resp_delim, NULL, 0); 1203130812Smarcel /* Handle case of first 32 registers listed in pairs. */ 1204130812Smarcel if (current_monitor->flags & MO_32_REGS_PAIRED 1205130812Smarcel && (regno & 1) != 0 && regno < 32) 1206130812Smarcel { 1207130812Smarcel monitor_debug ("EXP getreg.resp_delim\n"); 1208130812Smarcel monitor_expect (current_monitor->getreg.resp_delim, NULL, 0); 1209130812Smarcel } 1210130812Smarcel } 1211130812Smarcel 1212130812Smarcel /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set */ 1213130812Smarcel if (current_monitor->flags & MO_HEX_PREFIX) 1214130812Smarcel { 1215130812Smarcel int c; 1216130812Smarcel c = readchar (timeout); 1217130812Smarcel while (c == ' ') 1218130812Smarcel c = readchar (timeout); 1219130812Smarcel if ((c == '0') && ((c = readchar (timeout)) == 'x')) 1220130812Smarcel ; 1221130812Smarcel else 1222130812Smarcel error ("Bad value returned from monitor while fetching register %x.", 1223130812Smarcel regno); 1224130812Smarcel } 1225130812Smarcel 1226130812Smarcel /* Read upto the maximum number of hex digits for this register, skipping 1227130812Smarcel spaces, but stop reading if something else is seen. Some monitors 1228130812Smarcel like to drop leading zeros. */ 1229130812Smarcel 1230130812Smarcel for (i = 0; i < DEPRECATED_REGISTER_RAW_SIZE (regno) * 2; i++) 1231130812Smarcel { 1232130812Smarcel int c; 1233130812Smarcel c = readchar (timeout); 1234130812Smarcel while (c == ' ') 1235130812Smarcel c = readchar (timeout); 1236130812Smarcel 1237130812Smarcel if (!isxdigit (c)) 1238130812Smarcel break; 1239130812Smarcel 1240130812Smarcel regbuf[i] = c; 1241130812Smarcel } 1242130812Smarcel 1243130812Smarcel regbuf[i] = '\000'; /* terminate the number */ 1244130812Smarcel monitor_debug ("REGVAL '%s'\n", regbuf); 1245130812Smarcel 1246130812Smarcel /* If TERM is present, we wait for that to show up. Also, (if TERM 1247130812Smarcel is present), we will send TERM_CMD if that is present. In any 1248130812Smarcel case, we collect all of the output into buf, and then wait for 1249130812Smarcel the normal prompt. */ 1250130812Smarcel 1251130812Smarcel if (current_monitor->getreg.term) 1252130812Smarcel { 1253130812Smarcel monitor_debug ("EXP getreg.term\n"); 1254130812Smarcel monitor_expect (current_monitor->getreg.term, NULL, 0); /* get response */ 1255130812Smarcel } 1256130812Smarcel 1257130812Smarcel if (current_monitor->getreg.term_cmd) 1258130812Smarcel { 1259130812Smarcel monitor_debug ("EMIT getreg.term.cmd\n"); 1260130812Smarcel monitor_printf (current_monitor->getreg.term_cmd); 1261130812Smarcel } 1262130812Smarcel if (!current_monitor->getreg.term || /* Already expected or */ 1263130812Smarcel current_monitor->getreg.term_cmd) /* ack expected */ 1264130812Smarcel monitor_expect_prompt (NULL, 0); /* get response */ 1265130812Smarcel 1266130812Smarcel monitor_supply_register (regno, regbuf); 1267130812Smarcel} 1268130812Smarcel 1269130812Smarcel/* Sometimes, it takes several commands to dump the registers */ 1270130812Smarcel/* This is a primitive for use by variations of monitor interfaces in 1271130812Smarcel case they need to compose the operation. 1272130812Smarcel */ 1273130812Smarcelint 1274130812Smarcelmonitor_dump_reg_block (char *block_cmd) 1275130812Smarcel{ 1276130812Smarcel char buf[TARGET_BUF_SIZE]; 1277130812Smarcel int resp_len; 1278130812Smarcel monitor_printf (block_cmd); 1279130812Smarcel resp_len = monitor_expect_prompt (buf, sizeof (buf)); 1280130812Smarcel parse_register_dump (buf, resp_len); 1281130812Smarcel return 1; 1282130812Smarcel} 1283130812Smarcel 1284130812Smarcel 1285130812Smarcel/* Read the remote registers into the block regs. */ 1286130812Smarcel/* Call the specific function if it has been provided */ 1287130812Smarcel 1288130812Smarcelstatic void 1289130812Smarcelmonitor_dump_regs (void) 1290130812Smarcel{ 1291130812Smarcel char buf[TARGET_BUF_SIZE]; 1292130812Smarcel int resp_len; 1293130812Smarcel if (current_monitor->dumpregs) 1294130812Smarcel (*(current_monitor->dumpregs)) (); /* call supplied function */ 1295130812Smarcel else if (current_monitor->dump_registers) /* default version */ 1296130812Smarcel { 1297130812Smarcel monitor_printf (current_monitor->dump_registers); 1298130812Smarcel resp_len = monitor_expect_prompt (buf, sizeof (buf)); 1299130812Smarcel parse_register_dump (buf, resp_len); 1300130812Smarcel } 1301130812Smarcel else 1302130812Smarcel internal_error (__FILE__, __LINE__, "failed internal consistency check"); /* Need some way to read registers */ 1303130812Smarcel} 1304130812Smarcel 1305130812Smarcelstatic void 1306130812Smarcelmonitor_fetch_registers (int regno) 1307130812Smarcel{ 1308130812Smarcel monitor_debug ("MON fetchregs\n"); 1309130812Smarcel if (current_monitor->getreg.cmd) 1310130812Smarcel { 1311130812Smarcel if (regno >= 0) 1312130812Smarcel { 1313130812Smarcel monitor_fetch_register (regno); 1314130812Smarcel return; 1315130812Smarcel } 1316130812Smarcel 1317130812Smarcel for (regno = 0; regno < NUM_REGS; regno++) 1318130812Smarcel monitor_fetch_register (regno); 1319130812Smarcel } 1320130812Smarcel else 1321130812Smarcel { 1322130812Smarcel monitor_dump_regs (); 1323130812Smarcel } 1324130812Smarcel} 1325130812Smarcel 1326130812Smarcel/* Store register REGNO, or all if REGNO == 0. Return errno value. */ 1327130812Smarcel 1328130812Smarcelstatic void 1329130812Smarcelmonitor_store_register (int regno) 1330130812Smarcel{ 1331130812Smarcel const char *name; 1332130812Smarcel ULONGEST val; 1333130812Smarcel 1334130812Smarcel if (current_monitor->regname != NULL) 1335130812Smarcel name = current_monitor->regname (regno); 1336130812Smarcel else 1337130812Smarcel name = current_monitor->regnames[regno]; 1338130812Smarcel 1339130812Smarcel if (!name || (*name == '\0')) 1340130812Smarcel { 1341130812Smarcel monitor_debug ("MON Cannot store unknown register\n"); 1342130812Smarcel return; 1343130812Smarcel } 1344130812Smarcel 1345130812Smarcel val = read_register (regno); 1346130812Smarcel monitor_debug ("MON storeg %d %s\n", regno, 1347130812Smarcel phex (val, DEPRECATED_REGISTER_RAW_SIZE (regno))); 1348130812Smarcel 1349130812Smarcel /* send the register deposit command */ 1350130812Smarcel 1351130812Smarcel if (current_monitor->flags & MO_REGISTER_VALUE_FIRST) 1352130812Smarcel monitor_printf (current_monitor->setreg.cmd, val, name); 1353130812Smarcel else if (current_monitor->flags & MO_SETREG_INTERACTIVE) 1354130812Smarcel monitor_printf (current_monitor->setreg.cmd, name); 1355130812Smarcel else 1356130812Smarcel monitor_printf (current_monitor->setreg.cmd, name, val); 1357130812Smarcel 1358130812Smarcel if (current_monitor->setreg.resp_delim) 1359130812Smarcel { 1360130812Smarcel monitor_debug ("EXP setreg.resp_delim\n"); 1361130812Smarcel monitor_expect_regexp (&setreg_resp_delim_pattern, NULL, 0); 1362130812Smarcel if (current_monitor->flags & MO_SETREG_INTERACTIVE) 1363130812Smarcel monitor_printf ("%s\r", paddr_nz (val)); 1364130812Smarcel } 1365130812Smarcel if (current_monitor->setreg.term) 1366130812Smarcel { 1367130812Smarcel monitor_debug ("EXP setreg.term\n"); 1368130812Smarcel monitor_expect (current_monitor->setreg.term, NULL, 0); 1369130812Smarcel if (current_monitor->flags & MO_SETREG_INTERACTIVE) 1370130812Smarcel monitor_printf ("%s\r", paddr_nz (val)); 1371130812Smarcel monitor_expect_prompt (NULL, 0); 1372130812Smarcel } 1373130812Smarcel else 1374130812Smarcel monitor_expect_prompt (NULL, 0); 1375130812Smarcel if (current_monitor->setreg.term_cmd) /* Mode exit required */ 1376130812Smarcel { 1377130812Smarcel monitor_debug ("EXP setreg_termcmd\n"); 1378130812Smarcel monitor_printf ("%s", current_monitor->setreg.term_cmd); 1379130812Smarcel monitor_expect_prompt (NULL, 0); 1380130812Smarcel } 1381130812Smarcel} /* monitor_store_register */ 1382130812Smarcel 1383130812Smarcel/* Store the remote registers. */ 1384130812Smarcel 1385130812Smarcelstatic void 1386130812Smarcelmonitor_store_registers (int regno) 1387130812Smarcel{ 1388130812Smarcel if (regno >= 0) 1389130812Smarcel { 1390130812Smarcel monitor_store_register (regno); 1391130812Smarcel return; 1392130812Smarcel } 1393130812Smarcel 1394130812Smarcel for (regno = 0; regno < NUM_REGS; regno++) 1395130812Smarcel monitor_store_register (regno); 1396130812Smarcel} 1397130812Smarcel 1398130812Smarcel/* Get ready to modify the registers array. On machines which store 1399130812Smarcel individual registers, this doesn't need to do anything. On machines 1400130812Smarcel which store all the registers in one fell swoop, this makes sure 1401130812Smarcel that registers contains all the registers from the program being 1402130812Smarcel debugged. */ 1403130812Smarcel 1404130812Smarcelstatic void 1405130812Smarcelmonitor_prepare_to_store (void) 1406130812Smarcel{ 1407130812Smarcel /* Do nothing, since we can store individual regs */ 1408130812Smarcel} 1409130812Smarcel 1410130812Smarcelstatic void 1411130812Smarcelmonitor_files_info (struct target_ops *ops) 1412130812Smarcel{ 1413130812Smarcel printf_unfiltered ("\tAttached to %s at %d baud.\n", dev_name, baud_rate); 1414130812Smarcel} 1415130812Smarcel 1416130812Smarcelstatic int 1417130812Smarcelmonitor_write_memory (CORE_ADDR memaddr, char *myaddr, int len) 1418130812Smarcel{ 1419130812Smarcel unsigned int val, hostval; 1420130812Smarcel char *cmd; 1421130812Smarcel int i; 1422130812Smarcel 1423130812Smarcel monitor_debug ("MON write %d %s\n", len, paddr (memaddr)); 1424130812Smarcel 1425130812Smarcel if (current_monitor->flags & MO_ADDR_BITS_REMOVE) 1426130812Smarcel memaddr = ADDR_BITS_REMOVE (memaddr); 1427130812Smarcel 1428130812Smarcel /* Use memory fill command for leading 0 bytes. */ 1429130812Smarcel 1430130812Smarcel if (current_monitor->fill) 1431130812Smarcel { 1432130812Smarcel for (i = 0; i < len; i++) 1433130812Smarcel if (myaddr[i] != 0) 1434130812Smarcel break; 1435130812Smarcel 1436130812Smarcel if (i > 4) /* More than 4 zeros is worth doing */ 1437130812Smarcel { 1438130812Smarcel monitor_debug ("MON FILL %d\n", i); 1439130812Smarcel if (current_monitor->flags & MO_FILL_USES_ADDR) 1440130812Smarcel monitor_printf (current_monitor->fill, memaddr, (memaddr + i) - 1, 0); 1441130812Smarcel else 1442130812Smarcel monitor_printf (current_monitor->fill, memaddr, i, 0); 1443130812Smarcel 1444130812Smarcel monitor_expect_prompt (NULL, 0); 1445130812Smarcel 1446130812Smarcel return i; 1447130812Smarcel } 1448130812Smarcel } 1449130812Smarcel 1450130812Smarcel#if 0 1451130812Smarcel /* Can't actually use long longs if VAL is an int (nice idea, though). */ 1452130812Smarcel if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->setmem.cmdll) 1453130812Smarcel { 1454130812Smarcel len = 8; 1455130812Smarcel cmd = current_monitor->setmem.cmdll; 1456130812Smarcel } 1457130812Smarcel else 1458130812Smarcel#endif 1459130812Smarcel if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->setmem.cmdl) 1460130812Smarcel { 1461130812Smarcel len = 4; 1462130812Smarcel cmd = current_monitor->setmem.cmdl; 1463130812Smarcel } 1464130812Smarcel else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->setmem.cmdw) 1465130812Smarcel { 1466130812Smarcel len = 2; 1467130812Smarcel cmd = current_monitor->setmem.cmdw; 1468130812Smarcel } 1469130812Smarcel else 1470130812Smarcel { 1471130812Smarcel len = 1; 1472130812Smarcel cmd = current_monitor->setmem.cmdb; 1473130812Smarcel } 1474130812Smarcel 1475130812Smarcel val = extract_unsigned_integer (myaddr, len); 1476130812Smarcel 1477130812Smarcel if (len == 4) 1478130812Smarcel { 1479130812Smarcel hostval = *(unsigned int *) myaddr; 1480130812Smarcel monitor_debug ("Hostval(%08x) val(%08x)\n", hostval, val); 1481130812Smarcel } 1482130812Smarcel 1483130812Smarcel 1484130812Smarcel if (current_monitor->flags & MO_NO_ECHO_ON_SETMEM) 1485130812Smarcel monitor_printf_noecho (cmd, memaddr, val); 1486130812Smarcel else if (current_monitor->flags & MO_SETMEM_INTERACTIVE) 1487130812Smarcel { 1488130812Smarcel 1489130812Smarcel monitor_printf_noecho (cmd, memaddr); 1490130812Smarcel 1491130812Smarcel if (current_monitor->setmem.resp_delim) 1492130812Smarcel { 1493130812Smarcel monitor_debug ("EXP setmem.resp_delim"); 1494130812Smarcel monitor_expect_regexp (&setmem_resp_delim_pattern, NULL, 0); 1495130812Smarcel monitor_printf ("%x\r", val); 1496130812Smarcel } 1497130812Smarcel if (current_monitor->setmem.term) 1498130812Smarcel { 1499130812Smarcel monitor_debug ("EXP setmem.term"); 1500130812Smarcel monitor_expect (current_monitor->setmem.term, NULL, 0); 1501130812Smarcel monitor_printf ("%x\r", val); 1502130812Smarcel } 1503130812Smarcel if (current_monitor->setmem.term_cmd) 1504130812Smarcel { /* Emit this to get out of the memory editing state */ 1505130812Smarcel monitor_printf ("%s", current_monitor->setmem.term_cmd); 1506130812Smarcel /* Drop through to expecting a prompt */ 1507130812Smarcel } 1508130812Smarcel } 1509130812Smarcel else 1510130812Smarcel monitor_printf (cmd, memaddr, val); 1511130812Smarcel 1512130812Smarcel monitor_expect_prompt (NULL, 0); 1513130812Smarcel 1514130812Smarcel return len; 1515130812Smarcel} 1516130812Smarcel 1517130812Smarcel 1518130812Smarcelstatic int 1519130812Smarcelmonitor_write_memory_bytes (CORE_ADDR memaddr, char *myaddr, int len) 1520130812Smarcel{ 1521130812Smarcel unsigned char val; 1522130812Smarcel int written = 0; 1523130812Smarcel if (len == 0) 1524130812Smarcel return 0; 1525130812Smarcel /* Enter the sub mode */ 1526130812Smarcel monitor_printf (current_monitor->setmem.cmdb, memaddr); 1527130812Smarcel monitor_expect_prompt (NULL, 0); 1528130812Smarcel while (len) 1529130812Smarcel { 1530130812Smarcel val = *myaddr; 1531130812Smarcel monitor_printf ("%x\r", val); 1532130812Smarcel myaddr++; 1533130812Smarcel memaddr++; 1534130812Smarcel written++; 1535130812Smarcel /* If we wanted to, here we could validate the address */ 1536130812Smarcel monitor_expect_prompt (NULL, 0); 1537130812Smarcel len--; 1538130812Smarcel } 1539130812Smarcel /* Now exit the sub mode */ 1540130812Smarcel monitor_printf (current_monitor->getreg.term_cmd); 1541130812Smarcel monitor_expect_prompt (NULL, 0); 1542130812Smarcel return written; 1543130812Smarcel} 1544130812Smarcel 1545130812Smarcel 1546130812Smarcelstatic void 1547130812Smarcellonglongendswap (unsigned char *a) 1548130812Smarcel{ 1549130812Smarcel int i, j; 1550130812Smarcel unsigned char x; 1551130812Smarcel i = 0; 1552130812Smarcel j = 7; 1553130812Smarcel while (i < 4) 1554130812Smarcel { 1555130812Smarcel x = *(a + i); 1556130812Smarcel *(a + i) = *(a + j); 1557130812Smarcel *(a + j) = x; 1558130812Smarcel i++, j--; 1559130812Smarcel } 1560130812Smarcel} 1561130812Smarcel/* Format 32 chars of long long value, advance the pointer */ 1562130812Smarcelstatic char *hexlate = "0123456789abcdef"; 1563130812Smarcelstatic char * 1564130812Smarcellonglong_hexchars (unsigned long long value, 1565130812Smarcel char *outbuff) 1566130812Smarcel{ 1567130812Smarcel if (value == 0) 1568130812Smarcel { 1569130812Smarcel *outbuff++ = '0'; 1570130812Smarcel return outbuff; 1571130812Smarcel } 1572130812Smarcel else 1573130812Smarcel { 1574130812Smarcel static unsigned char disbuf[8]; /* disassembly buffer */ 1575130812Smarcel unsigned char *scan, *limit; /* loop controls */ 1576130812Smarcel unsigned char c, nib; 1577130812Smarcel int leadzero = 1; 1578130812Smarcel scan = disbuf; 1579130812Smarcel limit = scan + 8; 1580130812Smarcel { 1581130812Smarcel unsigned long long *dp; 1582130812Smarcel dp = (unsigned long long *) scan; 1583130812Smarcel *dp = value; 1584130812Smarcel } 1585130812Smarcel longlongendswap (disbuf); /* FIXME: ONly on big endian hosts */ 1586130812Smarcel while (scan < limit) 1587130812Smarcel { 1588130812Smarcel c = *scan++; /* a byte of our long long value */ 1589130812Smarcel if (leadzero) 1590130812Smarcel { 1591130812Smarcel if (c == 0) 1592130812Smarcel continue; 1593130812Smarcel else 1594130812Smarcel leadzero = 0; /* henceforth we print even zeroes */ 1595130812Smarcel } 1596130812Smarcel nib = c >> 4; /* high nibble bits */ 1597130812Smarcel *outbuff++ = hexlate[nib]; 1598130812Smarcel nib = c & 0x0f; /* low nibble bits */ 1599130812Smarcel *outbuff++ = hexlate[nib]; 1600130812Smarcel } 1601130812Smarcel return outbuff; 1602130812Smarcel } 1603130812Smarcel} /* longlong_hexchars */ 1604130812Smarcel 1605130812Smarcel 1606130812Smarcel 1607130812Smarcel/* I am only going to call this when writing virtual byte streams. 1608130812Smarcel Which possably entails endian conversions 1609130812Smarcel */ 1610130812Smarcelstatic int 1611130812Smarcelmonitor_write_memory_longlongs (CORE_ADDR memaddr, char *myaddr, int len) 1612130812Smarcel{ 1613130812Smarcel static char hexstage[20]; /* At least 16 digits required, plus null */ 1614130812Smarcel char *endstring; 1615130812Smarcel long long *llptr; 1616130812Smarcel long long value; 1617130812Smarcel int written = 0; 1618130812Smarcel llptr = (unsigned long long *) myaddr; 1619130812Smarcel if (len == 0) 1620130812Smarcel return 0; 1621130812Smarcel monitor_printf (current_monitor->setmem.cmdll, memaddr); 1622130812Smarcel monitor_expect_prompt (NULL, 0); 1623130812Smarcel while (len >= 8) 1624130812Smarcel { 1625130812Smarcel value = *llptr; 1626130812Smarcel endstring = longlong_hexchars (*llptr, hexstage); 1627130812Smarcel *endstring = '\0'; /* NUll terminate for printf */ 1628130812Smarcel monitor_printf ("%s\r", hexstage); 1629130812Smarcel llptr++; 1630130812Smarcel memaddr += 8; 1631130812Smarcel written += 8; 1632130812Smarcel /* If we wanted to, here we could validate the address */ 1633130812Smarcel monitor_expect_prompt (NULL, 0); 1634130812Smarcel len -= 8; 1635130812Smarcel } 1636130812Smarcel /* Now exit the sub mode */ 1637130812Smarcel monitor_printf (current_monitor->getreg.term_cmd); 1638130812Smarcel monitor_expect_prompt (NULL, 0); 1639130812Smarcel return written; 1640130812Smarcel} /* */ 1641130812Smarcel 1642130812Smarcel 1643130812Smarcel 1644130812Smarcel/* ----- MONITOR_WRITE_MEMORY_BLOCK ---------------------------- */ 1645130812Smarcel/* This is for the large blocks of memory which may occur in downloading. 1646130812Smarcel And for monitors which use interactive entry, 1647130812Smarcel And for monitors which do not have other downloading methods. 1648130812Smarcel Without this, we will end up calling monitor_write_memory many times 1649130812Smarcel and do the entry and exit of the sub mode many times 1650130812Smarcel This currently assumes... 1651130812Smarcel MO_SETMEM_INTERACTIVE 1652130812Smarcel ! MO_NO_ECHO_ON_SETMEM 1653130812Smarcel To use this, the you have to patch the monitor_cmds block with 1654130812Smarcel this function. Otherwise, its not tuned up for use by all 1655130812Smarcel monitor variations. 1656130812Smarcel */ 1657130812Smarcel 1658130812Smarcelstatic int 1659130812Smarcelmonitor_write_memory_block (CORE_ADDR memaddr, char *myaddr, int len) 1660130812Smarcel{ 1661130812Smarcel int written; 1662130812Smarcel written = 0; 1663130812Smarcel /* FIXME: This would be a good place to put the zero test */ 1664130812Smarcel#if 1 1665130812Smarcel if ((len > 8) && (((len & 0x07)) == 0) && current_monitor->setmem.cmdll) 1666130812Smarcel { 1667130812Smarcel return monitor_write_memory_longlongs (memaddr, myaddr, len); 1668130812Smarcel } 1669130812Smarcel#endif 1670130812Smarcel written = monitor_write_memory_bytes (memaddr, myaddr, len); 1671130812Smarcel return written; 1672130812Smarcel} 1673130812Smarcel 1674130812Smarcel/* This is an alternate form of monitor_read_memory which is used for monitors 1675130812Smarcel which can only read a single byte/word/etc. at a time. */ 1676130812Smarcel 1677130812Smarcelstatic int 1678130812Smarcelmonitor_read_memory_single (CORE_ADDR memaddr, char *myaddr, int len) 1679130812Smarcel{ 1680130812Smarcel unsigned int val; 1681130812Smarcel char membuf[sizeof (int) * 2 + 1]; 1682130812Smarcel char *p; 1683130812Smarcel char *cmd; 1684130812Smarcel 1685130812Smarcel monitor_debug ("MON read single\n"); 1686130812Smarcel#if 0 1687130812Smarcel /* Can't actually use long longs (nice idea, though). In fact, the 1688130812Smarcel call to strtoul below will fail if it tries to convert a value 1689130812Smarcel that's too big to fit in a long. */ 1690130812Smarcel if ((memaddr & 0x7) == 0 && len >= 8 && current_monitor->getmem.cmdll) 1691130812Smarcel { 1692130812Smarcel len = 8; 1693130812Smarcel cmd = current_monitor->getmem.cmdll; 1694130812Smarcel } 1695130812Smarcel else 1696130812Smarcel#endif 1697130812Smarcel if ((memaddr & 0x3) == 0 && len >= 4 && current_monitor->getmem.cmdl) 1698130812Smarcel { 1699130812Smarcel len = 4; 1700130812Smarcel cmd = current_monitor->getmem.cmdl; 1701130812Smarcel } 1702130812Smarcel else if ((memaddr & 0x1) == 0 && len >= 2 && current_monitor->getmem.cmdw) 1703130812Smarcel { 1704130812Smarcel len = 2; 1705130812Smarcel cmd = current_monitor->getmem.cmdw; 1706130812Smarcel } 1707130812Smarcel else 1708130812Smarcel { 1709130812Smarcel len = 1; 1710130812Smarcel cmd = current_monitor->getmem.cmdb; 1711130812Smarcel } 1712130812Smarcel 1713130812Smarcel /* Send the examine command. */ 1714130812Smarcel 1715130812Smarcel monitor_printf (cmd, memaddr); 1716130812Smarcel 1717130812Smarcel /* If RESP_DELIM is specified, we search for that as a leading 1718130812Smarcel delimiter for the memory value. Otherwise, we just start 1719130812Smarcel searching from the start of the buf. */ 1720130812Smarcel 1721130812Smarcel if (current_monitor->getmem.resp_delim) 1722130812Smarcel { 1723130812Smarcel monitor_debug ("EXP getmem.resp_delim\n"); 1724130812Smarcel monitor_expect_regexp (&getmem_resp_delim_pattern, NULL, 0); 1725130812Smarcel } 1726130812Smarcel 1727130812Smarcel /* Now, read the appropriate number of hex digits for this loc, 1728130812Smarcel skipping spaces. */ 1729130812Smarcel 1730130812Smarcel /* Skip leading spaces and "0x" if MO_HEX_PREFIX flag is set. */ 1731130812Smarcel if (current_monitor->flags & MO_HEX_PREFIX) 1732130812Smarcel { 1733130812Smarcel int c; 1734130812Smarcel 1735130812Smarcel c = readchar (timeout); 1736130812Smarcel while (c == ' ') 1737130812Smarcel c = readchar (timeout); 1738130812Smarcel if ((c == '0') && ((c = readchar (timeout)) == 'x')) 1739130812Smarcel ; 1740130812Smarcel else 1741130812Smarcel monitor_error ("monitor_read_memory_single", 1742130812Smarcel "bad response from monitor", 1743130812Smarcel memaddr, 0, NULL, 0); 1744130812Smarcel } 1745130812Smarcel 1746130812Smarcel { 1747130812Smarcel int i; 1748130812Smarcel for (i = 0; i < len * 2; i++) 1749130812Smarcel { 1750130812Smarcel int c; 1751130812Smarcel 1752130812Smarcel while (1) 1753130812Smarcel { 1754130812Smarcel c = readchar (timeout); 1755130812Smarcel if (isxdigit (c)) 1756130812Smarcel break; 1757130812Smarcel if (c == ' ') 1758130812Smarcel continue; 1759130812Smarcel 1760130812Smarcel monitor_error ("monitor_read_memory_single", 1761130812Smarcel "bad response from monitor", 1762130812Smarcel memaddr, i, membuf, 0); 1763130812Smarcel } 1764130812Smarcel membuf[i] = c; 1765130812Smarcel } 1766130812Smarcel membuf[i] = '\000'; /* terminate the number */ 1767130812Smarcel } 1768130812Smarcel 1769130812Smarcel/* If TERM is present, we wait for that to show up. Also, (if TERM is 1770130812Smarcel present), we will send TERM_CMD if that is present. In any case, we collect 1771130812Smarcel all of the output into buf, and then wait for the normal prompt. */ 1772130812Smarcel 1773130812Smarcel if (current_monitor->getmem.term) 1774130812Smarcel { 1775130812Smarcel monitor_expect (current_monitor->getmem.term, NULL, 0); /* get response */ 1776130812Smarcel 1777130812Smarcel if (current_monitor->getmem.term_cmd) 1778130812Smarcel { 1779130812Smarcel monitor_printf (current_monitor->getmem.term_cmd); 1780130812Smarcel monitor_expect_prompt (NULL, 0); 1781130812Smarcel } 1782130812Smarcel } 1783130812Smarcel else 1784130812Smarcel monitor_expect_prompt (NULL, 0); /* get response */ 1785130812Smarcel 1786130812Smarcel p = membuf; 1787130812Smarcel val = strtoul (membuf, &p, 16); 1788130812Smarcel 1789130812Smarcel if (val == 0 && membuf == p) 1790130812Smarcel monitor_error ("monitor_read_memory_single", 1791130812Smarcel "bad value from monitor", 1792130812Smarcel memaddr, 0, membuf, 0); 1793130812Smarcel 1794130812Smarcel /* supply register stores in target byte order, so swap here */ 1795130812Smarcel 1796130812Smarcel store_unsigned_integer (myaddr, len, val); 1797130812Smarcel 1798130812Smarcel return len; 1799130812Smarcel} 1800130812Smarcel 1801130812Smarcel/* Copy LEN bytes of data from debugger memory at MYADDR to inferior's 1802130812Smarcel memory at MEMADDR. Returns length moved. Currently, we do no more 1803130812Smarcel than 16 bytes at a time. */ 1804130812Smarcel 1805130812Smarcelstatic int 1806130812Smarcelmonitor_read_memory (CORE_ADDR memaddr, char *myaddr, int len) 1807130812Smarcel{ 1808130812Smarcel unsigned int val; 1809130812Smarcel char buf[512]; 1810130812Smarcel char *p, *p1; 1811130812Smarcel int resp_len; 1812130812Smarcel int i; 1813130812Smarcel CORE_ADDR dumpaddr; 1814130812Smarcel 1815130812Smarcel if (len <= 0) 1816130812Smarcel { 1817130812Smarcel monitor_debug ("Zero length call to monitor_read_memory\n"); 1818130812Smarcel return 0; 1819130812Smarcel } 1820130812Smarcel 1821130812Smarcel monitor_debug ("MON read block ta(%s) ha(%lx) %d\n", 1822130812Smarcel paddr_nz (memaddr), (long) myaddr, len); 1823130812Smarcel 1824130812Smarcel if (current_monitor->flags & MO_ADDR_BITS_REMOVE) 1825130812Smarcel memaddr = ADDR_BITS_REMOVE (memaddr); 1826130812Smarcel 1827130812Smarcel if (current_monitor->flags & MO_GETMEM_READ_SINGLE) 1828130812Smarcel return monitor_read_memory_single (memaddr, myaddr, len); 1829130812Smarcel 1830130812Smarcel len = min (len, 16); 1831130812Smarcel 1832130812Smarcel /* Some dumpers align the first data with the preceeding 16 1833130812Smarcel byte boundary. Some print blanks and start at the 1834130812Smarcel requested boundary. EXACT_DUMPADDR 1835130812Smarcel */ 1836130812Smarcel 1837130812Smarcel dumpaddr = (current_monitor->flags & MO_EXACT_DUMPADDR) 1838130812Smarcel ? memaddr : memaddr & ~0x0f; 1839130812Smarcel 1840130812Smarcel /* See if xfer would cross a 16 byte boundary. If so, clip it. */ 1841130812Smarcel if (((memaddr ^ (memaddr + len - 1)) & ~0xf) != 0) 1842130812Smarcel len = ((memaddr + len) & ~0xf) - memaddr; 1843130812Smarcel 1844130812Smarcel /* send the memory examine command */ 1845130812Smarcel 1846130812Smarcel if (current_monitor->flags & MO_GETMEM_NEEDS_RANGE) 1847130812Smarcel monitor_printf (current_monitor->getmem.cmdb, memaddr, memaddr + len); 1848130812Smarcel else if (current_monitor->flags & MO_GETMEM_16_BOUNDARY) 1849130812Smarcel monitor_printf (current_monitor->getmem.cmdb, dumpaddr); 1850130812Smarcel else 1851130812Smarcel monitor_printf (current_monitor->getmem.cmdb, memaddr, len); 1852130812Smarcel 1853130812Smarcel /* If TERM is present, we wait for that to show up. Also, (if TERM 1854130812Smarcel is present), we will send TERM_CMD if that is present. In any 1855130812Smarcel case, we collect all of the output into buf, and then wait for 1856130812Smarcel the normal prompt. */ 1857130812Smarcel 1858130812Smarcel if (current_monitor->getmem.term) 1859130812Smarcel { 1860130812Smarcel resp_len = monitor_expect (current_monitor->getmem.term, buf, sizeof buf); /* get response */ 1861130812Smarcel 1862130812Smarcel if (resp_len <= 0) 1863130812Smarcel monitor_error ("monitor_read_memory", 1864130812Smarcel "excessive response from monitor", 1865130812Smarcel memaddr, resp_len, buf, 0); 1866130812Smarcel 1867130812Smarcel if (current_monitor->getmem.term_cmd) 1868130812Smarcel { 1869130812Smarcel serial_write (monitor_desc, current_monitor->getmem.term_cmd, 1870130812Smarcel strlen (current_monitor->getmem.term_cmd)); 1871130812Smarcel monitor_expect_prompt (NULL, 0); 1872130812Smarcel } 1873130812Smarcel } 1874130812Smarcel else 1875130812Smarcel resp_len = monitor_expect_prompt (buf, sizeof buf); /* get response */ 1876130812Smarcel 1877130812Smarcel p = buf; 1878130812Smarcel 1879130812Smarcel /* If RESP_DELIM is specified, we search for that as a leading 1880130812Smarcel delimiter for the values. Otherwise, we just start searching 1881130812Smarcel from the start of the buf. */ 1882130812Smarcel 1883130812Smarcel if (current_monitor->getmem.resp_delim) 1884130812Smarcel { 1885130812Smarcel int retval, tmp; 1886130812Smarcel struct re_registers resp_strings; 1887130812Smarcel monitor_debug ("MON getmem.resp_delim %s\n", current_monitor->getmem.resp_delim); 1888130812Smarcel 1889130812Smarcel memset (&resp_strings, 0, sizeof (struct re_registers)); 1890130812Smarcel tmp = strlen (p); 1891130812Smarcel retval = re_search (&getmem_resp_delim_pattern, p, tmp, 0, tmp, 1892130812Smarcel &resp_strings); 1893130812Smarcel 1894130812Smarcel if (retval < 0) 1895130812Smarcel monitor_error ("monitor_read_memory", 1896130812Smarcel "bad response from monitor", 1897130812Smarcel memaddr, resp_len, buf, 0); 1898130812Smarcel 1899130812Smarcel p += resp_strings.end[0]; 1900130812Smarcel#if 0 1901130812Smarcel p = strstr (p, current_monitor->getmem.resp_delim); 1902130812Smarcel if (!p) 1903130812Smarcel monitor_error ("monitor_read_memory", 1904130812Smarcel "bad response from monitor", 1905130812Smarcel memaddr, resp_len, buf, 0); 1906130812Smarcel p += strlen (current_monitor->getmem.resp_delim); 1907130812Smarcel#endif 1908130812Smarcel } 1909130812Smarcel monitor_debug ("MON scanning %d ,%lx '%s'\n", len, (long) p, p); 1910130812Smarcel if (current_monitor->flags & MO_GETMEM_16_BOUNDARY) 1911130812Smarcel { 1912130812Smarcel char c; 1913130812Smarcel int fetched = 0; 1914130812Smarcel i = len; 1915130812Smarcel c = *p; 1916130812Smarcel 1917130812Smarcel 1918130812Smarcel while (!(c == '\000' || c == '\n' || c == '\r') && i > 0) 1919130812Smarcel { 1920130812Smarcel if (isxdigit (c)) 1921130812Smarcel { 1922130812Smarcel if ((dumpaddr >= memaddr) && (i > 0)) 1923130812Smarcel { 1924130812Smarcel val = fromhex (c) * 16 + fromhex (*(p + 1)); 1925130812Smarcel *myaddr++ = val; 1926130812Smarcel if (monitor_debug_p || remote_debug) 1927130812Smarcel fprintf_unfiltered (gdb_stdlog, "[%02x]", val); 1928130812Smarcel --i; 1929130812Smarcel fetched++; 1930130812Smarcel } 1931130812Smarcel ++dumpaddr; 1932130812Smarcel ++p; 1933130812Smarcel } 1934130812Smarcel ++p; /* skip a blank or other non hex char */ 1935130812Smarcel c = *p; 1936130812Smarcel } 1937130812Smarcel if (fetched == 0) 1938130812Smarcel error ("Failed to read via monitor"); 1939130812Smarcel if (monitor_debug_p || remote_debug) 1940130812Smarcel fprintf_unfiltered (gdb_stdlog, "\n"); 1941130812Smarcel return fetched; /* Return the number of bytes actually read */ 1942130812Smarcel } 1943130812Smarcel monitor_debug ("MON scanning bytes\n"); 1944130812Smarcel 1945130812Smarcel for (i = len; i > 0; i--) 1946130812Smarcel { 1947130812Smarcel /* Skip non-hex chars, but bomb on end of string and newlines */ 1948130812Smarcel 1949130812Smarcel while (1) 1950130812Smarcel { 1951130812Smarcel if (isxdigit (*p)) 1952130812Smarcel break; 1953130812Smarcel 1954130812Smarcel if (*p == '\000' || *p == '\n' || *p == '\r') 1955130812Smarcel monitor_error ("monitor_read_memory", 1956130812Smarcel "badly terminated response from monitor", 1957130812Smarcel memaddr, resp_len, buf, 0); 1958130812Smarcel p++; 1959130812Smarcel } 1960130812Smarcel 1961130812Smarcel val = strtoul (p, &p1, 16); 1962130812Smarcel 1963130812Smarcel if (val == 0 && p == p1) 1964130812Smarcel monitor_error ("monitor_read_memory", 1965130812Smarcel "bad value from monitor", 1966130812Smarcel memaddr, resp_len, buf, 0); 1967130812Smarcel 1968130812Smarcel *myaddr++ = val; 1969130812Smarcel 1970130812Smarcel if (i == 1) 1971130812Smarcel break; 1972130812Smarcel 1973130812Smarcel p = p1; 1974130812Smarcel } 1975130812Smarcel 1976130812Smarcel return len; 1977130812Smarcel} 1978130812Smarcel 1979130812Smarcel/* Transfer LEN bytes between target address MEMADDR and GDB address 1980130812Smarcel MYADDR. Returns 0 for success, errno code for failure. TARGET is 1981130812Smarcel unused. */ 1982130812Smarcel 1983130812Smarcelstatic int 1984130812Smarcelmonitor_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, 1985130812Smarcel struct mem_attrib *attrib, struct target_ops *target) 1986130812Smarcel{ 1987130812Smarcel int res; 1988130812Smarcel 1989130812Smarcel if (write) 1990130812Smarcel { 1991130812Smarcel if (current_monitor->flags & MO_HAS_BLOCKWRITES) 1992130812Smarcel res = monitor_write_memory_block(memaddr, myaddr, len); 1993130812Smarcel else 1994130812Smarcel res = monitor_write_memory(memaddr, myaddr, len); 1995130812Smarcel } 1996130812Smarcel else 1997130812Smarcel { 1998130812Smarcel res = monitor_read_memory(memaddr, myaddr, len); 1999130812Smarcel } 2000130812Smarcel 2001130812Smarcel return res; 2002130812Smarcel} 2003130812Smarcel 2004130812Smarcelstatic void 2005130812Smarcelmonitor_kill (void) 2006130812Smarcel{ 2007130812Smarcel return; /* ignore attempts to kill target system */ 2008130812Smarcel} 2009130812Smarcel 2010130812Smarcel/* All we actually do is set the PC to the start address of exec_bfd, and start 2011130812Smarcel the program at that point. */ 2012130812Smarcel 2013130812Smarcelstatic void 2014130812Smarcelmonitor_create_inferior (char *exec_file, char *args, char **env) 2015130812Smarcel{ 2016130812Smarcel if (args && (*args != '\000')) 2017130812Smarcel error ("Args are not supported by the monitor."); 2018130812Smarcel 2019130812Smarcel first_time = 1; 2020130812Smarcel clear_proceed_status (); 2021130812Smarcel proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0); 2022130812Smarcel} 2023130812Smarcel 2024130812Smarcel/* Clean up when a program exits. 2025130812Smarcel The program actually lives on in the remote processor's RAM, and may be 2026130812Smarcel run again without a download. Don't leave it full of breakpoint 2027130812Smarcel instructions. */ 2028130812Smarcel 2029130812Smarcelstatic void 2030130812Smarcelmonitor_mourn_inferior (void) 2031130812Smarcel{ 2032130812Smarcel unpush_target (targ_ops); 2033130812Smarcel generic_mourn_inferior (); /* Do all the proper things now */ 2034130812Smarcel} 2035130812Smarcel 2036130812Smarcel/* Tell the monitor to add a breakpoint. */ 2037130812Smarcel 2038130812Smarcelstatic int 2039130812Smarcelmonitor_insert_breakpoint (CORE_ADDR addr, char *shadow) 2040130812Smarcel{ 2041130812Smarcel int i; 2042130812Smarcel const unsigned char *bp; 2043130812Smarcel int bplen; 2044130812Smarcel 2045130812Smarcel monitor_debug ("MON inst bkpt %s\n", paddr (addr)); 2046130812Smarcel if (current_monitor->set_break == NULL) 2047130812Smarcel error ("No set_break defined for this monitor"); 2048130812Smarcel 2049130812Smarcel if (current_monitor->flags & MO_ADDR_BITS_REMOVE) 2050130812Smarcel addr = ADDR_BITS_REMOVE (addr); 2051130812Smarcel 2052130812Smarcel /* Determine appropriate breakpoint size for this address. */ 2053130812Smarcel bp = gdbarch_breakpoint_from_pc (current_gdbarch, &addr, &bplen); 2054130812Smarcel 2055130812Smarcel for (i = 0; i < current_monitor->num_breakpoints; i++) 2056130812Smarcel { 2057130812Smarcel if (breakaddr[i] == 0) 2058130812Smarcel { 2059130812Smarcel breakaddr[i] = addr; 2060130812Smarcel monitor_read_memory (addr, shadow, bplen); 2061130812Smarcel monitor_printf (current_monitor->set_break, addr); 2062130812Smarcel monitor_expect_prompt (NULL, 0); 2063130812Smarcel return 0; 2064130812Smarcel } 2065130812Smarcel } 2066130812Smarcel 2067130812Smarcel error ("Too many breakpoints (> %d) for monitor.", current_monitor->num_breakpoints); 2068130812Smarcel} 2069130812Smarcel 2070130812Smarcel/* Tell the monitor to remove a breakpoint. */ 2071130812Smarcel 2072130812Smarcelstatic int 2073130812Smarcelmonitor_remove_breakpoint (CORE_ADDR addr, char *shadow) 2074130812Smarcel{ 2075130812Smarcel int i; 2076130812Smarcel 2077130812Smarcel monitor_debug ("MON rmbkpt %s\n", paddr (addr)); 2078130812Smarcel if (current_monitor->clr_break == NULL) 2079130812Smarcel error ("No clr_break defined for this monitor"); 2080130812Smarcel 2081130812Smarcel if (current_monitor->flags & MO_ADDR_BITS_REMOVE) 2082130812Smarcel addr = ADDR_BITS_REMOVE (addr); 2083130812Smarcel 2084130812Smarcel for (i = 0; i < current_monitor->num_breakpoints; i++) 2085130812Smarcel { 2086130812Smarcel if (breakaddr[i] == addr) 2087130812Smarcel { 2088130812Smarcel breakaddr[i] = 0; 2089130812Smarcel /* some monitors remove breakpoints based on the address */ 2090130812Smarcel if (current_monitor->flags & MO_CLR_BREAK_USES_ADDR) 2091130812Smarcel monitor_printf (current_monitor->clr_break, addr); 2092130812Smarcel else if (current_monitor->flags & MO_CLR_BREAK_1_BASED) 2093130812Smarcel monitor_printf (current_monitor->clr_break, i + 1); 2094130812Smarcel else 2095130812Smarcel monitor_printf (current_monitor->clr_break, i); 2096130812Smarcel monitor_expect_prompt (NULL, 0); 2097130812Smarcel return 0; 2098130812Smarcel } 2099130812Smarcel } 2100130812Smarcel fprintf_unfiltered (gdb_stderr, 2101130812Smarcel "Can't find breakpoint associated with 0x%s\n", 2102130812Smarcel paddr_nz (addr)); 2103130812Smarcel return 1; 2104130812Smarcel} 2105130812Smarcel 2106130812Smarcel/* monitor_wait_srec_ack -- wait for the target to send an acknowledgement for 2107130812Smarcel an S-record. Return non-zero if the ACK is received properly. */ 2108130812Smarcel 2109130812Smarcelstatic int 2110130812Smarcelmonitor_wait_srec_ack (void) 2111130812Smarcel{ 2112130812Smarcel int ch; 2113130812Smarcel 2114130812Smarcel if (current_monitor->flags & MO_SREC_ACK_PLUS) 2115130812Smarcel { 2116130812Smarcel return (readchar (timeout) == '+'); 2117130812Smarcel } 2118130812Smarcel else if (current_monitor->flags & MO_SREC_ACK_ROTATE) 2119130812Smarcel { 2120130812Smarcel /* Eat two backspaces, a "rotating" char (|/-\), and a space. */ 2121130812Smarcel if ((ch = readchar (1)) < 0) 2122130812Smarcel return 0; 2123130812Smarcel if ((ch = readchar (1)) < 0) 2124130812Smarcel return 0; 2125130812Smarcel if ((ch = readchar (1)) < 0) 2126130812Smarcel return 0; 2127130812Smarcel if ((ch = readchar (1)) < 0) 2128130812Smarcel return 0; 2129130812Smarcel } 2130130812Smarcel return 1; 2131130812Smarcel} 2132130812Smarcel 2133130812Smarcel/* monitor_load -- download a file. */ 2134130812Smarcel 2135130812Smarcelstatic void 2136130812Smarcelmonitor_load (char *file, int from_tty) 2137130812Smarcel{ 2138130812Smarcel monitor_debug ("MON load\n"); 2139130812Smarcel 2140130812Smarcel if (current_monitor->load_routine) 2141130812Smarcel current_monitor->load_routine (monitor_desc, file, hashmark); 2142130812Smarcel else 2143130812Smarcel { /* The default is ascii S-records */ 2144130812Smarcel int n; 2145130812Smarcel unsigned long load_offset; 2146130812Smarcel char buf[128]; 2147130812Smarcel 2148130812Smarcel /* enable user to specify address for downloading as 2nd arg to load */ 2149130812Smarcel n = sscanf (file, "%s 0x%lx", buf, &load_offset); 2150130812Smarcel if (n > 1) 2151130812Smarcel file = buf; 2152130812Smarcel else 2153130812Smarcel load_offset = 0; 2154130812Smarcel 2155130812Smarcel monitor_printf (current_monitor->load); 2156130812Smarcel if (current_monitor->loadresp) 2157130812Smarcel monitor_expect (current_monitor->loadresp, NULL, 0); 2158130812Smarcel 2159130812Smarcel load_srec (monitor_desc, file, (bfd_vma) load_offset, 2160130812Smarcel 32, SREC_ALL, hashmark, 2161130812Smarcel current_monitor->flags & MO_SREC_ACK ? 2162130812Smarcel monitor_wait_srec_ack : NULL); 2163130812Smarcel 2164130812Smarcel monitor_expect_prompt (NULL, 0); 2165130812Smarcel } 2166130812Smarcel 2167130812Smarcel /* Finally, make the PC point at the start address */ 2168130812Smarcel if (exec_bfd) 2169130812Smarcel write_pc (bfd_get_start_address (exec_bfd)); 2170130812Smarcel 2171130812Smarcel /* There used to be code here which would clear inferior_ptid and 2172130812Smarcel call clear_symtab_users. None of that should be necessary: 2173130812Smarcel monitor targets should behave like remote protocol targets, and 2174130812Smarcel since generic_load does none of those things, this function 2175130812Smarcel shouldn't either. 2176130812Smarcel 2177130812Smarcel Furthermore, clearing inferior_ptid is *incorrect*. After doing 2178130812Smarcel a load, we still have a valid connection to the monitor, with a 2179130812Smarcel live processor state to fiddle with. The user can type 2180130812Smarcel `continue' or `jump *start' and make the program run. If they do 2181130812Smarcel these things, however, GDB will be talking to a running program 2182130812Smarcel while inferior_ptid is null_ptid; this makes things like 2183130812Smarcel reinit_frame_cache very confused. */ 2184130812Smarcel} 2185130812Smarcel 2186130812Smarcelstatic void 2187130812Smarcelmonitor_stop (void) 2188130812Smarcel{ 2189130812Smarcel monitor_debug ("MON stop\n"); 2190130812Smarcel if ((current_monitor->flags & MO_SEND_BREAK_ON_STOP) != 0) 2191130812Smarcel serial_send_break (monitor_desc); 2192130812Smarcel if (current_monitor->stop) 2193130812Smarcel monitor_printf_noecho (current_monitor->stop); 2194130812Smarcel} 2195130812Smarcel 2196130812Smarcel/* Put a COMMAND string out to MONITOR. Output from MONITOR is placed 2197130812Smarcel in OUTPUT until the prompt is seen. FIXME: We read the characters 2198130812Smarcel ourseleves here cause of a nasty echo. */ 2199130812Smarcel 2200130812Smarcelstatic void 2201130812Smarcelmonitor_rcmd (char *command, 2202130812Smarcel struct ui_file *outbuf) 2203130812Smarcel{ 2204130812Smarcel char *p; 2205130812Smarcel int resp_len; 2206130812Smarcel char buf[1000]; 2207130812Smarcel 2208130812Smarcel if (monitor_desc == NULL) 2209130812Smarcel error ("monitor target not open."); 2210130812Smarcel 2211130812Smarcel p = current_monitor->prompt; 2212130812Smarcel 2213130812Smarcel /* Send the command. Note that if no args were supplied, then we're 2214130812Smarcel just sending the monitor a newline, which is sometimes useful. */ 2215130812Smarcel 2216130812Smarcel monitor_printf ("%s\r", (command ? command : "")); 2217130812Smarcel 2218130812Smarcel resp_len = monitor_expect_prompt (buf, sizeof buf); 2219130812Smarcel 2220130812Smarcel fputs_unfiltered (buf, outbuf); /* Output the response */ 2221130812Smarcel} 2222130812Smarcel 2223130812Smarcel/* Convert hex digit A to a number. */ 2224130812Smarcel 2225130812Smarcel#if 0 2226130812Smarcelstatic int 2227130812Smarcelfrom_hex (int a) 2228130812Smarcel{ 2229130812Smarcel if (a >= '0' && a <= '9') 2230130812Smarcel return a - '0'; 2231130812Smarcel if (a >= 'a' && a <= 'f') 2232130812Smarcel return a - 'a' + 10; 2233130812Smarcel if (a >= 'A' && a <= 'F') 2234130812Smarcel return a - 'A' + 10; 2235130812Smarcel 2236130812Smarcel error ("Reply contains invalid hex digit 0x%x", a); 2237130812Smarcel} 2238130812Smarcel#endif 2239130812Smarcel 2240130812Smarcelchar * 2241130812Smarcelmonitor_get_dev_name (void) 2242130812Smarcel{ 2243130812Smarcel return dev_name; 2244130812Smarcel} 2245130812Smarcel 2246130812Smarcelstatic struct target_ops monitor_ops; 2247130812Smarcel 2248130812Smarcelstatic void 2249130812Smarcelinit_base_monitor_ops (void) 2250130812Smarcel{ 2251130812Smarcel monitor_ops.to_close = monitor_close; 2252130812Smarcel monitor_ops.to_detach = monitor_detach; 2253130812Smarcel monitor_ops.to_resume = monitor_resume; 2254130812Smarcel monitor_ops.to_wait = monitor_wait; 2255130812Smarcel monitor_ops.to_fetch_registers = monitor_fetch_registers; 2256130812Smarcel monitor_ops.to_store_registers = monitor_store_registers; 2257130812Smarcel monitor_ops.to_prepare_to_store = monitor_prepare_to_store; 2258130812Smarcel monitor_ops.to_xfer_memory = monitor_xfer_memory; 2259130812Smarcel monitor_ops.to_files_info = monitor_files_info; 2260130812Smarcel monitor_ops.to_insert_breakpoint = monitor_insert_breakpoint; 2261130812Smarcel monitor_ops.to_remove_breakpoint = monitor_remove_breakpoint; 2262130812Smarcel monitor_ops.to_kill = monitor_kill; 2263130812Smarcel monitor_ops.to_load = monitor_load; 2264130812Smarcel monitor_ops.to_create_inferior = monitor_create_inferior; 2265130812Smarcel monitor_ops.to_mourn_inferior = monitor_mourn_inferior; 2266130812Smarcel monitor_ops.to_stop = monitor_stop; 2267130812Smarcel monitor_ops.to_rcmd = monitor_rcmd; 2268130812Smarcel monitor_ops.to_stratum = process_stratum; 2269130812Smarcel monitor_ops.to_has_all_memory = 1; 2270130812Smarcel monitor_ops.to_has_memory = 1; 2271130812Smarcel monitor_ops.to_has_stack = 1; 2272130812Smarcel monitor_ops.to_has_registers = 1; 2273130812Smarcel monitor_ops.to_has_execution = 1; 2274130812Smarcel monitor_ops.to_magic = OPS_MAGIC; 2275130812Smarcel} /* init_base_monitor_ops */ 2276130812Smarcel 2277130812Smarcel/* Init the target_ops structure pointed at by OPS */ 2278130812Smarcel 2279130812Smarcelvoid 2280130812Smarcelinit_monitor_ops (struct target_ops *ops) 2281130812Smarcel{ 2282130812Smarcel if (monitor_ops.to_magic != OPS_MAGIC) 2283130812Smarcel init_base_monitor_ops (); 2284130812Smarcel 2285130812Smarcel memcpy (ops, &monitor_ops, sizeof monitor_ops); 2286130812Smarcel} 2287130812Smarcel 2288130812Smarcel/* Define additional commands that are usually only used by monitors. */ 2289130812Smarcel 2290130812Smarcelextern initialize_file_ftype _initialize_remote_monitors; /* -Wmissing-prototypes */ 2291130812Smarcel 2292130812Smarcelvoid 2293130812Smarcel_initialize_remote_monitors (void) 2294130812Smarcel{ 2295130812Smarcel init_base_monitor_ops (); 2296130812Smarcel add_show_from_set (add_set_cmd ("hash", no_class, var_boolean, 2297130812Smarcel (char *) &hashmark, 2298130812Smarcel "Set display of activity while downloading a file.\n\ 2299130812SmarcelWhen enabled, a hashmark \'#\' is displayed.", 2300130812Smarcel &setlist), 2301130812Smarcel &showlist); 2302130812Smarcel 2303130812Smarcel add_show_from_set 2304130812Smarcel (add_set_cmd ("monitor", no_class, var_zinteger, 2305130812Smarcel (char *) &monitor_debug_p, 2306130812Smarcel "Set debugging of remote monitor communication.\n\ 2307130812SmarcelWhen enabled, communication between GDB and the remote monitor\n\ 2308130812Smarcelis displayed.", &setdebuglist), 2309130812Smarcel &showdebuglist); 2310130812Smarcel} 2311