- * Download a file specified in 'args', to the hms.
- */
-static void
-hms_load (args, fromtty)
- char *args;
- int fromtty;
-{
- bfd *abfd;
- asection *s;
- int n;
- char buffer[1024];
-
- check_open ();
-
- dcache_flush ();
- inferior_pid = 0;
- abfd = bfd_openr (args, gnutarget);
- if (!abfd)
- {
- printf_filtered ("Unable to open file %s\n", args);
- return;
- }
-
- if (bfd_check_format (abfd, bfd_object) == 0)
- {
- printf_filtered ("File is not an object file\n");
- return;
- }
-
- s = abfd->sections;
- while (s != (asection *) NULL)
- {
- if (s->flags & SEC_LOAD)
- {
- int i;
-
-#define DELTA 1024
- char *buffer = xmalloc (DELTA);
-
- printf_filtered ("%s\t: 0x%4x .. 0x%4x ", s->name, s->vma, s->vma + s->_raw_size);
- for (i = 0; i < s->_raw_size; i += DELTA)
- {
- int delta = DELTA;
-
- if (delta > s->_raw_size - i)
- delta = s->_raw_size - i;
-
- bfd_get_section_contents (abfd, s, buffer, i, delta);
- hms_write_inferior_memory (s->vma + i, buffer, delta);
- printf_filtered ("*");
- gdb_flush (gdb_stdout);
- }
- printf_filtered ("\n");
- free (buffer);
- }
- s = s->next;
- }
- sprintf (buffer, "r PC=%x", abfd->start_address);
- hms_write_cr (buffer);
- expect_prompt ();
- /* Turn off all breakpoints */
- hms_write_cr ("b -");
- expect_prompt ();
-}
-
-/* This is called not only when we first attach, but also when the
- user types "run" after having attached. */
-void
-hms_create_inferior (execfile, args, env)
- char *execfile;
- char *args;
- char **env;
-{
- int entry_pt;
- char buffer[100];
-
- if (args && *args)
- error ("Can't pass arguments to remote hms process.");
-
- if (execfile == 0 || exec_bfd == 0)
- error ("No exec file specified");
-
- entry_pt = (int) bfd_get_start_address (exec_bfd);
- check_open ();
-
- hms_kill (NULL, NULL);
- hms_clear_breakpoints ();
- init_wait_for_inferior ();
- hms_write_cr ("");
- expect_prompt ();
-
- insert_breakpoints (); /* Needed to get correct instruction in cache */
- proceed (entry_pt, TARGET_SIGNAL_DEFAULT, 0);
-}
-
-/* Open a connection to a remote debugger.
- NAME is the filename used for communication, then a space,
- then the baud rate.
- */
-
-static char *
-find_end_of_word (s)
- char *s;
-{
- while (*s && !isspace (*s))
- s++;
- return s;
-}
-
-static char *
-get_word (p)
- char **p;
-{
- char *s = *p;
- char *word;
- char *copy;
- size_t len;
-
- while (isspace (*s))
- s++;
-
- word = s;
-
- len = 0;
-
- while (*s && !isspace (*s))
- {
- s++;
- len++;
-
- }
- copy = xmalloc (len + 1);
- memcpy (copy, word, len);
- copy[len] = 0;
- *p = s;
- return copy;
-}
-
-static int baudrate = 9600;
-
-static int
-is_baudrate_right ()
-{
- int ok;
-
- /* Put this port into NORMAL mode, send the 'normal' character */
-
- hms_write ("\001", 1); /* Control A */
- hms_write ("\r\n", 2); /* Cr */
-
- while (1)
- {
- ok = SERIAL_READCHAR (desc, timeout);
- if (ok < 0)
- break;
- }
-
- hms_write ("r", 1);
-
- if (readchar_nofail () == 'r')
- return 1;
-
- /* Not the right baudrate, or the board's not on */
- return 0;
-}
-static void
-set_rate ()
-{
- if (!SERIAL_SETBAUDRATE (desc, baudrate))
- error ("Can't set baudrate");
-}
-
-
-static void
-hms_open (name, from_tty)
- char *name;
- int from_tty;
-{
- unsigned int prl;
- char *p;
-
- if (name == 0)
- {
- name = "";
- }
- if (is_open)
- hms_close (0);
- dev_name = strdup (name);
-
- if (!(desc = SERIAL_OPEN (dev_name)))
- perror_with_name ((char *) dev_name);
-
- SERIAL_RAW (desc);
- is_open = 1;
- push_target (&hms_ops);
- dcache_init ();
-
- /* Hello? Are you there? */
- SERIAL_WRITE (desc, "\r\n", 2);
- expect_prompt ();
-
- /* Clear any break points */
- hms_clear_breakpoints ();
-
- printf_filtered ("Connected to remote board running HMS monitor.\n");
- add_commands ();
- hms_drain ();
-}
-
-/* Close out all files and local state before this target loses control. */
-
-static void
-hms_close (quitting)
- int quitting;
-{
- /* Clear any break points */
- remove_commands ();
- hms_clear_breakpoints ();
- sleep (1); /* Let any output make it all the way back */
- if (is_open)
- {
- SERIAL_WRITE (desc, "R\r\n", 3);
- SERIAL_CLOSE (desc);
- }
- is_open = 0;
-}
-
-/* Terminate the open connection to the remote debugger. Use this
-when you want to detach and do something else with your gdb. */ void
-hms_detach (args, from_tty)
- char *args;
- int from_tty;
-{
- if (is_open)
- {
- hms_clear_breakpoints ();
- }
-
- pop_target (); /* calls hms_close to do the real work
-*/
- if (from_tty)
- printf_filtered ("Ending remote %s debugging\n",
- target_shortname);
-}
-
-/* Tell the remote machine to resume. */
-
-void
-hms_resume (pid, step, sig)
- int pid, step;
- enum target_signal
- sig;
-{
- dcache_flush ();
-
- if (step)
- {
- hms_write_cr ("s");
- expect ("Step>");
-
- /* Force the next hms_wait to return a trap. Not doing anything
- about I/O from the target means that the user has to type "continue"
- to see any. FIXME, this should be fixed. */
- need_artificial_trap = 1;
- }
- else
- {
- hms_write_cr ("g");
- expect ("g");
- }
-}
-
-/* Wait until the remote machine stops, then return, storing status in
-STATUS just as `wait' would. */
-
-int
-hms_wait (pid, status)
- int pid;
- struct target_waitstatus *status;
-{
- /* Strings to look for. '?' means match any single character. Note
- that with the algorithm we use, the initial character of the string
- cannot recur in the string, or we will not find some cases of the
- string in the input. */
-
- static char bpt[] = "At breakpoint:";
-
- /* It would be tempting to look for "\n[__exit + 0x8]\n" but that
- requires loading symbols with "yc i" and even if we did do that we
- don't know that the file has symbols. */
- static char exitmsg[] = "HMS>";
- char *bp = bpt;
- char *ep = exitmsg;
-
- /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.
- */
- char swallowed[50];
-
- /* Current position in swallowed. */
- char *swallowed_p = swallowed;
-
- int ch;
- int ch_handled;
- int old_timeout = timeout;
- int
- old_immediate_quit = immediate_quit;
- int swallowed_cr = 0;
-
- status->kind = TARGET_WAITKIND_EXITED;
- status->value.integer = 0;
-
- if (need_artificial_trap != 0)
- {
- status->kind =
- TARGET_WAITKIND_STOPPED;
- status->value.sig = TARGET_SIGNAL_TRAP;
- need_artificial_trap--;
- return 0;
- }
-
- timeout = 5; /* Don't time out for a while - user program is running.
- */
- immediate_quit = 1; /* Helps ability to QUIT */
- while (1)
- {
- QUIT; /* Let user quit and leave process running */
- ch_handled = 0;
- ch = readchar ();
- if (ch == *bp)
- {
- bp++;
- if (*bp == '\0')
- break;
- ch_handled = 1;
-
- *swallowed_p++ = ch;
- }
- else
- {
- bp = bpt;
- }
- if
- (ch == *ep || *ep == '?')
- {
- ep++;
- if (*ep == '\0')
- break;
-
- if (!ch_handled)
- *swallowed_p++ = ch;
- ch_handled =
- 1;
- }
- else
- {
- ep = exitmsg;
- }
-
- if (!ch_handled)
- {
- char *p;
-
- /* Print out any characters which have been swallowed. */
- for (p = swallowed; p < swallowed_p; ++p)
- putc_unfiltered (*p);
- swallowed_p = swallowed;
-
- if ((ch != '\r' && ch != '\n') || swallowed_cr > 10)
- {
- putc_unfiltered (ch);
- swallowed_cr = 10;
- }
- swallowed_cr++;
-
- }
- }
- if (*bp == '\0')
- {
- status->kind = TARGET_WAITKIND_STOPPED;
- status->value.sig = TARGET_SIGNAL_TRAP;
- expect_prompt ();
- }
- else
- {
- status->kind = TARGET_WAITKIND_EXITED;
- status->value.integer =
- TARGET_SIGNAL_STOP;
- }
-
- timeout = old_timeout;
- immediate_quit = old_immediate_quit;
- return
- 0;
-}
-
-/* Return the name of register number REGNO in the form input and
-output by hms.
-
- Returns a pointer to a static buffer containing the answer. */
-static char *
-get_reg_name (regno)
- int regno;
-{
- static char *rn[] =
- REGISTER_NAMES;
-
- return rn[regno];
-}
-
-/* Read the remote registers. */
-
-static int
-gethex (length, start, ok)
- unsigned int length;
- char *start;
- int *ok;
-{
- int result = 0;
-
- while (length--)
- {
- result <<= 4;
- if (*start >= 'a' && *start <= 'f')
- {
- result += *start - 'a' + 10;
- }
- else if (*start >= 'A' &&
- *start <= 'F')
- {
- result += *start - 'A' + 10;
- }
- else if
- (*start >= '0' && *start <= '9')
- {
- result += *start - '0';
- }
- else
- *ok = 0;
- start++;
-
- }
- return result;
-}
-static int
-timed_read (buf, n, timeout)
- char
- *buf;
-
-{
- int i;
- char c;
-
- i = 0;
- while (i < n)
- {
- c = readchar ();
-
- if (c == 0)
- return i;
- buf[i] = c;
- i++;
-
- }
- return i;
-}
-
-hms_write (a, l)
- char *a;
-{
- int i;
-
- SERIAL_WRITE (desc, a, l);
-
- if (!quiet)
- {
- printf_unfiltered ("<");
- for (i = 0; i < l; i++)
- {
- printf_unfiltered ("%c", a[i]);
- }
- printf_unfiltered (">");
- }
-}
-
-hms_write_cr (s)
- char *s;
-{
- hms_write (s, strlen (s));
- hms_write ("\r\n", 2);
-}
-
-#ifdef GDB_TARGET_IS_H8500
-
-/* H8/500 monitor reg dump looks like:
-
-HMS>r
-PC:8000 SR:070C .7NZ.. CP:00 DP:00 EP:00 TP:00 BR:00
-R0-R7: FF5A 0001 F4FE F500 0000 F528 F528 F4EE
-HMS>
-
-
-*/
-
-supply_val (n, size, ptr, segptr)
- int n;
- int size;
- char *ptr;
- char *segptr;
-{
- int ok;
- char raw[4];
- switch (size)
- {
- case 2:
- raw[0] = gethex (2, ptr, &ok);
- raw[1] = gethex (2, ptr + 2, &ok);
- supply_register (n, raw);
- break;
- case 1:
- raw[0] = gethex (2, ptr, &ok);
- supply_register (n, raw);
- break;
- case 4:
- {
- int v = gethex (4, ptr, &ok);
- v |= gethex (2, segptr, &ok) << 16;
- raw[0] = 0;
- raw[1] = (v >> 16) & 0xff;
- raw[2] = (v >> 8) & 0xff;
- raw[3] = (v >> 0) & 0xff;
- supply_register (n, raw);
- }
- }
-
-}
-static void
-hms_fetch_register (dummy)
- int dummy;
-{
-#define REGREPLY_SIZE 108
- char linebuf[REGREPLY_SIZE + 1];
- int i;
- int s;
- int gottok;
-
- LONGEST reg[NUM_REGS];
- check_open ();
-
- do
- {
-
- hms_write_cr ("r");
- expect ("r");
- s = timed_read (linebuf + 1, REGREPLY_SIZE, 1);
-
- linebuf[REGREPLY_SIZE] = 0;
- gottok = 0;
- if (linebuf[3] == 'P' &&
- linebuf[4] == 'C' &&
- linebuf[5] == ':' &&
- linebuf[105] == 'H' &&
- linebuf[106] == 'M' &&
- linebuf[107] == 'S')
- {
-
- /*
- 012
- r**
- -------1---------2---------3---------4---------5-----
- 345678901234567890123456789012345678901234567890123456
- PC:8000 SR:070C .7NZ.. CP:00 DP:00 EP:00 TP:00 BR:00**
- ---6---------7---------8---------9--------10----
- 789012345678901234567890123456789012345678901234
- R0-R7: FF5A 0001 F4FE F500 0000 F528 F528 F4EE**
-
- 56789
- HMS>
- */
- gottok = 1;
-
-
- supply_val (PC_REGNUM, 4, linebuf + 6, linebuf + 29);
-
- supply_val (CCR_REGNUM, 2, linebuf + 14);
- supply_val (SEG_C_REGNUM, 1, linebuf + 29);
- supply_val (SEG_D_REGNUM, 1, linebuf + 35);
- supply_val (SEG_E_REGNUM, 1, linebuf + 41);
- supply_val (SEG_T_REGNUM, 1, linebuf + 47);
- for (i = 0; i < 8; i++)
- {
- static int sr[8] =
- {35, 35, 35, 35,
- 41, 41, 47, 47};
-
- char raw[4];
- char *src = linebuf + 64 + 5 * i;
- char *segsrc = linebuf + sr[i];
- supply_val (R0_REGNUM + i, 2, src);
- supply_val (PR0_REGNUM + i, 4, src, segsrc);
- }
- }
- if (!gottok)
- {
- hms_write_cr ("");
- expect ("HMS>");
- }
- }
- while (!gottok);
-}
-#endif
-
-#ifdef GDB_TARGET_IS_H8300
-static void
-hms_fetch_register (dummy)
- int dummy;
-{
-#define REGREPLY_SIZE 79
- char linebuf[REGREPLY_SIZE + 1];
- int i;
- int s;
- int gottok;
-
- unsigned LONGEST reg[NUM_REGS];
-
- check_open ();
-
- do
- {
- hms_write_cr ("r");
-
- s = timed_read (linebuf, 1, 1);
-
- while (linebuf[0] != 'r')
- s = timed_read (linebuf, 1, 1);
-
- s = timed_read (linebuf + 1, REGREPLY_SIZE - 1, 1);
-
- linebuf[REGREPLY_SIZE] = 0;
- gottok = 0;
- if (linebuf[0] == 'r' &&
- linebuf[3] == 'P' &&
- linebuf[4] == 'C' &&
- linebuf[5] == '=' &&
- linebuf[75] == 'H' &&
- linebuf[76] == 'M' &&
- linebuf[77] == 'S')
- {
- /*
- PC=XXXX CCR=XX:XXXXXXXX R0-R7= XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX
- 5436789012345678901234567890123456789012345678901234567890123456789012
- 0 1 2 3 4 5 6
- */
- gottok = 1;
-
- reg[PC_REGNUM] = gethex (4, linebuf + 6, &gottok);
- reg[CCR_REGNUM] = gethex (2, linebuf + 15, &gottok);
- for (i = 0; i < 8; i++)
- {
- reg[i] = gethex (4, linebuf + 34 + 5 * i, &gottok);
- }
- }
- }
- while (!gottok);
- for (i = 0; i < NUM_REGS; i++)
- {
- char swapped[2];
-
- swapped[1] = reg[i];
- swapped[0] = (reg[i]) >> 8;
-
- supply_register (i, swapped);
- }
-}
-#endif
-/* Store register REGNO, or all if REGNO == -1.
- Return errno value. */
-static void
-hms_store_register (regno)
- int regno;
-{
- if (regno == -1)
- {
- for (regno = 0; regno < NUM_REGS; regno++)
- {
- hms_store_register (regno);
- }
- }
- else
- {
- char *name = get_reg_name (regno);
- char buffer[100];
- /* Some regs dont really exist */
- if (!(name[0] == 'p' && name[1] == 'r')
- && !(name[0] == 'c' && name[1] == 'y')
- && !(name[0] == 't' && name[1] == 'i')
- && !(name[0] == 'i' && name[1] == 'n'))
- {
- sprintf (buffer, "r %s=%x", name, read_register (regno));
- hms_write_cr (buffer);
- expect_prompt ();
- }
- }
-}
-
-
-/* Get ready to modify the registers array. On machines which store
- individual registers, this doesn't need to do anything. On machines
- which store all the registers in one fell swoop, this makes sure
- that registers contains all the registers from the program being
- debugged. */
-
-void
-hms_prepare_to_store ()
-{
- /* Do nothing, since we can store individual regs */
-}
-
-static CORE_ADDR
-translate_addr (addr)
- CORE_ADDR addr;
-{
-
- return (addr);
-
-}
-
-/* Read a word from remote address ADDR and return it.
- * This goes through the data cache.
- */
-int
-hms_fetch_word (addr)
- CORE_ADDR addr;
-{
- return dcache_fetch (addr);
-}
-
-/* Write a word WORD into remote address ADDR.
- This goes through the data cache. */
-
-void
-hms_store_word (addr, word)
- CORE_ADDR addr;
- int word;
-{
- dcache_poke (addr, word);
-}
-
-int
-hms_xfer_inferior_memory (memaddr, myaddr, len, write, target)
- CORE_ADDR memaddr;
- char *myaddr;
- int len;
- int write;
- struct target_ops *target; /* ignored */
-{
- register int i;
-
- /* Round starting address down to longword boundary. */
- register CORE_ADDR addr;
-
- /* Round ending address up; get number of longwords that makes. */
- register int count;
-
- /* Allocate buffer of that many longwords. */
- register int *buffer;
-
- memaddr &= 0xffff;
- addr = memaddr & -sizeof (int);
- count = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
-
- buffer = (int *) alloca (count * sizeof (int));
-
- if (write)
- {
- /* Fill start and end extra bytes of buffer with existing memory data. */
-
- if (addr != memaddr || len < (int) sizeof (int))
- {
- /* Need part of initial word -- fetch it. */
- buffer[0] = hms_fetch_word (addr);
- }
-
- if (count > 1) /* FIXME, avoid if even boundary */
- {
- buffer[count - 1]
- = hms_fetch_word (addr + (count - 1) * sizeof (int));
- }
-
- /* Copy data to be written over corresponding part of buffer */
-
- memcpy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
-
- /* Write the entire buffer. */
-
- for (i = 0; i < count; i++, addr += sizeof (int))
- {
- errno = 0;
- hms_store_word (addr, buffer[i]);
- if (errno)
- {
- return 0;
- }
-
- }
- }
- else
- {
- /* Read all the longwords */
- for (i = 0; i < count; i++, addr += sizeof (int))
- {
- errno = 0;
- buffer[i] = hms_fetch_word (addr);
- if (errno)
- {
- return 0;
- }
- QUIT;
- }
-
- /* Copy appropriate bytes out of the buffer. */
- memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
- }
-
- return len;
-}
-
-int
-hms_write_inferior_memory (memaddr, myaddr, len)
- CORE_ADDR memaddr;
- unsigned char *myaddr;
- int len;
-{
- bfd_vma addr;
- int done;
- int todo;
- char buffer[100];
- done = 0;
- hms_write_cr (".");
- expect_prompt ();
- while (done < len)
- {
- char *ptr = buffer;
- int thisgo;
- int idx;
-
- thisgo = len - done;
- if (thisgo > 20)
- thisgo = 20;
-
- sprintf (ptr, "M.B %4x =", memaddr + done);
- ptr += 10;
- for (idx = 0; idx < thisgo; idx++)
- {
- sprintf (ptr, "%2x ", myaddr[idx + done]);
- ptr += 3;
- }
- hms_write_cr (buffer);
- expect_prompt ();
- done += thisgo;
- }
-}
-
-void
-hms_files_info ()
-{
- char *file = "nothing";
-
- if (exec_bfd)
- file = bfd_get_filename (exec_bfd);
-
- if (exec_bfd)
-#ifdef __GO32__
- printf_filtered ("\tAttached to DOS asynctsr and running program %s\n", file);
-#else
- printf_filtered ("\tAttached to %s at %d baud and running program %s\n", dev_name, baudrate, file);
-#endif
- printf_filtered ("\ton an H8/300 processor.\n");
-}
-
-/* Copy LEN bytes of data from debugger memory at MYADDR
- to inferior's memory at MEMADDR. Returns errno value.
- * sb/sh instructions don't work on unaligned addresses, when TU=1.