1 /* Remote debugging interface to m32r and mon2000 ROM monitors for GDB,
4 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2004, 2005, 2007, 2008,
5 2009, 2010 Free Software Foundation, Inc.
7 Adapted by Michael Snyder of Cygnus Support.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 /* This module defines communication with the Renesas m32r monitor */
29 #include "exceptions.h"
35 #include "symfile.h" /* for generic load */
37 #include <time.h> /* for time_t */
38 #include "gdb_string.h"
39 #include "objfiles.h" /* for ALL_OBJFILES etc. */
45 * All this stuff just to get my host computer's IP address!
50 #include <sys/types.h>
51 #include <netdb.h> /* for hostent */
52 #include <netinet/in.h> /* for struct in_addr */
54 #include <arpa/inet.h> /* for inet_ntoa */
58 static char *board_addr; /* user-settable IP address for M32R-EVA */
59 static char *server_addr; /* user-settable IP address for gdb host */
60 static char *download_path; /* user-settable path for SREC files */
67 #define ACCL_REGNUM 22
68 #define ACCH_REGNUM 23
72 * Function: m32r_load_1 (helper function)
76 m32r_load_section (bfd *abfd, asection *s, void *obj)
78 unsigned int *data_count = obj;
79 if (s->flags & SEC_LOAD)
81 int addr_size = gdbarch_addr_bit (target_gdbarch) / 8;
82 bfd_size_type section_size = bfd_section_size (abfd, s);
83 bfd_vma section_base = bfd_section_lma (abfd, s);
84 unsigned int buffer, i;
86 *data_count += section_size;
88 printf_filtered ("Loading section %s, size 0x%lx lma ",
89 bfd_section_name (abfd, s),
90 (unsigned long) section_size);
91 fputs_filtered (paddress (target_gdbarch, section_base), gdb_stdout);
92 printf_filtered ("\n");
93 gdb_flush (gdb_stdout);
94 monitor_printf ("%s mw\r", phex_nz (section_base, addr_size));
95 for (i = 0; i < section_size; i += 4)
98 monitor_expect (" -> ", NULL, 0);
99 bfd_get_section_contents (abfd, s, (char *) &buffer, i, 4);
100 monitor_printf ("%x\n", buffer);
102 monitor_expect (" -> ", NULL, 0);
103 monitor_printf ("q\n");
104 monitor_expect_prompt (NULL, 0);
109 m32r_load_1 (void *dummy)
113 bfd_map_over_sections ((bfd *) dummy, m32r_load_section, &data_count);
118 * Function: m32r_load (an alternate way to load)
122 m32r_load (char *filename, int from_tty)
126 unsigned int i, data_count = 0;
127 struct timeval start_time, end_time;
129 if (filename == NULL || filename[0] == 0)
130 filename = get_exec_file (1);
132 abfd = bfd_openr (filename, 0);
134 error (_("Unable to open file %s."), filename);
135 if (bfd_check_format (abfd, bfd_object) == 0)
136 error (_("File is not an object file."));
137 gettimeofday (&start_time, NULL);
139 for (s = abfd->sections; s; s = s->next)
140 if (s->flags & SEC_LOAD)
142 bfd_size_type section_size = bfd_section_size (abfd, s);
143 bfd_vma section_base = bfd_section_vma (abfd, s);
146 data_count += section_size;
148 printf_filtered ("Loading section %s, size 0x%lx vma ",
149 bfd_section_name (abfd, s), section_size);
150 fputs_filtered (paddress (target_gdbarch, section_base), gdb_stdout);
151 printf_filtered ("\n");
152 gdb_flush (gdb_stdout);
153 monitor_printf ("%x mw\r", section_base);
154 for (i = 0; i < section_size; i += 4)
156 monitor_expect (" -> ", NULL, 0);
157 bfd_get_section_contents (abfd, s, (char *) &buffer, i, 4);
158 monitor_printf ("%x\n", buffer);
160 monitor_expect (" -> ", NULL, 0);
161 monitor_printf ("q\n");
162 monitor_expect_prompt (NULL, 0);
165 if (!(catch_errors (m32r_load_1, abfd, "Load aborted!\n", RETURN_MASK_ALL)))
167 monitor_printf ("q\n");
171 gettimeofday (&end_time, NULL);
172 printf_filtered ("Start address 0x%lx\n",
173 (unsigned long) bfd_get_start_address (abfd));
174 print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
177 /* Finally, make the PC point at the start address */
179 regcache_write_pc (get_current_regcache (),
180 bfd_get_start_address (exec_bfd));
182 inferior_ptid = null_ptid; /* No process now */
184 /* This is necessary because many things were based on the PC at the
185 time that we attached to the monitor, which is no longer valid
186 now that we have loaded new code (and just changed the PC).
187 Another way to do this might be to call normal_stop, except that
188 the stack may not be valid, and things would get horribly
191 clear_symtab_users ();
195 m32r_load_gen (char *filename, int from_tty)
197 generic_load (filename, from_tty);
200 static void m32r_open (char *args, int from_tty);
201 static void mon2000_open (char *args, int from_tty);
203 /* This array of registers needs to match the indexes used by GDB. The
204 whole reason this exists is because the various ROM monitors use
205 different names than GDB does, and don't support all the registers
206 either. So, typing "info reg sp" becomes an "A7". */
208 static char *m32r_regnames[] =
209 { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
210 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
211 "psw", "cbr", "spi", "spu", "bpc", "pc", "accl", "acch",
215 m32r_supply_register (struct regcache *regcache, char *regname,
216 int regnamelen, char *val, int vallen)
219 int num_regs = sizeof (m32r_regnames) / sizeof (m32r_regnames[0]);
220 struct gdbarch *gdbarch = get_regcache_arch (regcache);
222 for (regno = 0; regno < num_regs; regno++)
223 if (strncmp (regname, m32r_regnames[regno], regnamelen) == 0)
226 if (regno >= num_regs)
227 return; /* no match */
229 if (regno == ACCL_REGNUM)
230 { /* special handling for 64-bit acc reg */
231 monitor_supply_register (regcache, ACCH_REGNUM, val);
232 val = strchr (val, ':'); /* skip past ':' to get 2nd word */
234 monitor_supply_register (regcache, ACCL_REGNUM, val + 1);
238 monitor_supply_register (regcache, regno, val);
239 if (regno == PSW_REGNUM)
241 unsigned long psw = strtoul (val, NULL, 16);
242 char *zero = "00000000", *one = "00000001";
246 monitor_supply_register (regcache, SM_REGNUM, (psw & 0x80) ? one : zero);
249 /* Backup stack mode bit */
250 monitor_supply_register (regcache, BSM_REGNUM, (psw & 0x8000) ? one : zero);
253 /* Interrupt enable bit */
254 monitor_supply_register (regcache, IE_REGNUM, (psw & 0x40) ? one : zero);
257 /* Backup interrupt enable bit */
258 monitor_supply_register (regcache, BIE_REGNUM, (psw & 0x4000) ? one : zero);
261 /* Condition bit (carry etc.) */
262 monitor_supply_register (regcache, COND_REGNUM, (psw & 0x1) ? one : zero);
265 monitor_supply_register (regcache, CBR_REGNUM, (psw & 0x1) ? one : zero);
268 monitor_supply_register (regcache, BPC_REGNUM, zero); /* KLUDGE: (???????) */
271 monitor_supply_register (regcache, BCARRY_REGNUM, zero); /* KLUDGE: (??????) */
275 if (regno == SPI_REGNUM || regno == SPU_REGNUM)
276 { /* special handling for stack pointer (spu or spi) */
277 ULONGEST stackmode, psw;
278 regcache_cooked_read_unsigned (regcache, PSW_REGNUM, &psw);
279 stackmode = psw & 0x80;
281 if (regno == SPI_REGNUM && !stackmode) /* SP == SPI */
282 monitor_supply_register (regcache,
283 gdbarch_sp_regnum (gdbarch), val);
284 else if (regno == SPU_REGNUM && stackmode) /* SP == SPU */
285 monitor_supply_register (regcache,
286 gdbarch_sp_regnum (gdbarch), val);
291 /* m32r RevC board monitor */
293 static struct target_ops m32r_ops;
295 static char *m32r_inits[] = { "\r", NULL };
297 static struct monitor_ops m32r_cmds;
300 init_m32r_cmds (void)
302 m32r_cmds.flags = MO_CLR_BREAK_USES_ADDR | MO_REGISTER_VALUE_FIRST;
303 m32r_cmds.init = m32r_inits; /* Init strings */
304 m32r_cmds.cont = "go\r"; /* continue command */
305 m32r_cmds.step = "step\r"; /* single step */
306 m32r_cmds.stop = NULL; /* interrupt command */
307 m32r_cmds.set_break = "%x +bp\r"; /* set a breakpoint */
308 m32r_cmds.clr_break = "%x -bp\r"; /* clear a breakpoint */
309 m32r_cmds.clr_all_break = "bpoff\r"; /* clear all breakpoints */
310 m32r_cmds.fill = "%x %x %x fill\r"; /* fill (start length val) */
311 m32r_cmds.setmem.cmdb = "%x 1 %x fill\r"; /* setmem.cmdb (addr, value) */
312 m32r_cmds.setmem.cmdw = "%x 1 %x fillh\r"; /* setmem.cmdw (addr, value) */
313 m32r_cmds.setmem.cmdl = "%x 1 %x fillw\r"; /* setmem.cmdl (addr, value) */
314 m32r_cmds.setmem.cmdll = NULL; /* setmem.cmdll (addr, value) */
315 m32r_cmds.setmem.resp_delim = NULL; /* setmem.resp_delim */
316 m32r_cmds.setmem.term = NULL; /* setmem.term */
317 m32r_cmds.setmem.term_cmd = NULL; /* setmem.term_cmd */
318 m32r_cmds.getmem.cmdb = "%x %x dump\r"; /* getmem.cmdb (addr, len) */
319 m32r_cmds.getmem.cmdw = NULL; /* getmem.cmdw (addr, len) */
320 m32r_cmds.getmem.cmdl = NULL; /* getmem.cmdl (addr, len) */
321 m32r_cmds.getmem.cmdll = NULL; /* getmem.cmdll (addr, len) */
322 m32r_cmds.getmem.resp_delim = ": "; /* getmem.resp_delim */
323 m32r_cmds.getmem.term = NULL; /* getmem.term */
324 m32r_cmds.getmem.term_cmd = NULL; /* getmem.term_cmd */
325 m32r_cmds.setreg.cmd = "%x to %%%s\r"; /* setreg.cmd (name, value) */
326 m32r_cmds.setreg.resp_delim = NULL; /* setreg.resp_delim */
327 m32r_cmds.setreg.term = NULL; /* setreg.term */
328 m32r_cmds.setreg.term_cmd = NULL; /* setreg.term_cmd */
329 m32r_cmds.getreg.cmd = NULL; /* getreg.cmd (name) */
330 m32r_cmds.getreg.resp_delim = NULL; /* getreg.resp_delim */
331 m32r_cmds.getreg.term = NULL; /* getreg.term */
332 m32r_cmds.getreg.term_cmd = NULL; /* getreg.term_cmd */
333 m32r_cmds.dump_registers = ".reg\r"; /* dump_registers */
334 m32r_cmds.register_pattern = "\\(\\w+\\) += \\([0-9a-fA-F]+\\b\\)"; /* register_pattern */
335 m32r_cmds.supply_register = m32r_supply_register;
336 m32r_cmds.load_routine = NULL; /* load_routine (defaults to SRECs) */
337 m32r_cmds.load = NULL; /* download command */
338 m32r_cmds.loadresp = NULL; /* load response */
339 m32r_cmds.prompt = "ok "; /* monitor command prompt */
340 m32r_cmds.line_term = "\r"; /* end-of-line terminator */
341 m32r_cmds.cmd_end = NULL; /* optional command terminator */
342 m32r_cmds.target = &m32r_ops; /* target operations */
343 m32r_cmds.stopbits = SERIAL_1_STOPBITS; /* number of stop bits */
344 m32r_cmds.regnames = m32r_regnames; /* registers names */
345 m32r_cmds.magic = MONITOR_OPS_MAGIC; /* magic */
346 } /* init_m32r_cmds */
349 m32r_open (char *args, int from_tty)
351 monitor_open (args, &m32r_cmds, from_tty);
354 /* Mon2000 monitor (MSA2000 board) */
356 static struct target_ops mon2000_ops;
357 static struct monitor_ops mon2000_cmds;
360 init_mon2000_cmds (void)
362 mon2000_cmds.flags = MO_CLR_BREAK_USES_ADDR | MO_REGISTER_VALUE_FIRST;
363 mon2000_cmds.init = m32r_inits; /* Init strings */
364 mon2000_cmds.cont = "go\r"; /* continue command */
365 mon2000_cmds.step = "step\r"; /* single step */
366 mon2000_cmds.stop = NULL; /* interrupt command */
367 mon2000_cmds.set_break = "%x +bp\r"; /* set a breakpoint */
368 mon2000_cmds.clr_break = "%x -bp\r"; /* clear a breakpoint */
369 mon2000_cmds.clr_all_break = "bpoff\r"; /* clear all breakpoints */
370 mon2000_cmds.fill = "%x %x %x fill\r"; /* fill (start length val) */
371 mon2000_cmds.setmem.cmdb = "%x 1 %x fill\r"; /* setmem.cmdb (addr, value) */
372 mon2000_cmds.setmem.cmdw = "%x 1 %x fillh\r"; /* setmem.cmdw (addr, value) */
373 mon2000_cmds.setmem.cmdl = "%x 1 %x fillw\r"; /* setmem.cmdl (addr, value) */
374 mon2000_cmds.setmem.cmdll = NULL; /* setmem.cmdll (addr, value) */
375 mon2000_cmds.setmem.resp_delim = NULL; /* setmem.resp_delim */
376 mon2000_cmds.setmem.term = NULL; /* setmem.term */
377 mon2000_cmds.setmem.term_cmd = NULL; /* setmem.term_cmd */
378 mon2000_cmds.getmem.cmdb = "%x %x dump\r"; /* getmem.cmdb (addr, len) */
379 mon2000_cmds.getmem.cmdw = NULL; /* getmem.cmdw (addr, len) */
380 mon2000_cmds.getmem.cmdl = NULL; /* getmem.cmdl (addr, len) */
381 mon2000_cmds.getmem.cmdll = NULL; /* getmem.cmdll (addr, len) */
382 mon2000_cmds.getmem.resp_delim = ": "; /* getmem.resp_delim */
383 mon2000_cmds.getmem.term = NULL; /* getmem.term */
384 mon2000_cmds.getmem.term_cmd = NULL; /* getmem.term_cmd */
385 mon2000_cmds.setreg.cmd = "%x to %%%s\r"; /* setreg.cmd (name, value) */
386 mon2000_cmds.setreg.resp_delim = NULL; /* setreg.resp_delim */
387 mon2000_cmds.setreg.term = NULL; /* setreg.term */
388 mon2000_cmds.setreg.term_cmd = NULL; /* setreg.term_cmd */
389 mon2000_cmds.getreg.cmd = NULL; /* getreg.cmd (name) */
390 mon2000_cmds.getreg.resp_delim = NULL; /* getreg.resp_delim */
391 mon2000_cmds.getreg.term = NULL; /* getreg.term */
392 mon2000_cmds.getreg.term_cmd = NULL; /* getreg.term_cmd */
393 mon2000_cmds.dump_registers = ".reg\r"; /* dump_registers */
394 mon2000_cmds.register_pattern = "\\(\\w+\\) += \\([0-9a-fA-F]+\\b\\)"; /* register_pattern */
395 mon2000_cmds.supply_register = m32r_supply_register;
396 mon2000_cmds.load_routine = NULL; /* load_routine (defaults to SRECs) */
397 mon2000_cmds.load = NULL; /* download command */
398 mon2000_cmds.loadresp = NULL; /* load response */
399 mon2000_cmds.prompt = "Mon2000>"; /* monitor command prompt */
400 mon2000_cmds.line_term = "\r"; /* end-of-line terminator */
401 mon2000_cmds.cmd_end = NULL; /* optional command terminator */
402 mon2000_cmds.target = &mon2000_ops; /* target operations */
403 mon2000_cmds.stopbits = SERIAL_1_STOPBITS; /* number of stop bits */
404 mon2000_cmds.regnames = m32r_regnames; /* registers names */
405 mon2000_cmds.magic = MONITOR_OPS_MAGIC; /* magic */
406 } /* init_mon2000_cmds */
409 mon2000_open (char *args, int from_tty)
411 monitor_open (args, &mon2000_cmds, from_tty);
415 m32r_upload_command (char *args, int from_tty)
419 struct timeval start_time, end_time;
420 int resp_len, data_count = 0;
422 struct hostent *hostent;
423 struct in_addr inet_addr;
425 /* first check to see if there's an ethernet port! */
426 monitor_printf ("ust\r");
427 resp_len = monitor_expect_prompt (buf, sizeof (buf));
428 if (!strchr (buf, ':'))
429 error (_("No ethernet connection!"));
433 /* scan second colon in the output from the "ust" command */
434 char *myIPaddress = strchr (strchr (buf, ':') + 1, ':') + 1;
436 while (isspace (*myIPaddress))
439 if (!strncmp (myIPaddress, "0.0.", 4)) /* empty */
441 ("Please use 'set board-address' to set the M32R-EVA board's IP address.");
442 if (strchr (myIPaddress, '('))
443 *(strchr (myIPaddress, '(')) = '\0'; /* delete trailing junk */
444 board_addr = xstrdup (myIPaddress);
446 if (server_addr == 0)
450 /* Winsock initialization. */
451 if (WSAStartup (MAKEWORD (1, 1), &wd))
452 error (_("Couldn't initialize WINSOCK."));
456 gethostname (buf, sizeof (buf));
459 hostent = gethostbyname (buf);
463 memcpy (&inet_addr.s_addr, hostent->h_addr,
464 sizeof (inet_addr.s_addr));
465 server_addr = (char *) inet_ntoa (inet_addr);
467 server_addr = (char *) inet_ntoa (hostent->h_addr);
471 if (server_addr == 0) /* failed? */
473 ("Need to know gdb host computer's IP address (use 'set server-address')");
476 if (args == 0 || args[0] == 0) /* no args: upload the current file */
477 args = get_exec_file (1);
479 if (args[0] != '/' && download_path == 0)
481 if (current_directory)
482 download_path = xstrdup (current_directory);
485 ("Need to know default download path (use 'set download-path')");
488 gettimeofday (&start_time, NULL);
489 monitor_printf ("uhip %s\r", server_addr);
490 resp_len = monitor_expect_prompt (buf, sizeof (buf)); /* parse result? */
491 monitor_printf ("ulip %s\r", board_addr);
492 resp_len = monitor_expect_prompt (buf, sizeof (buf)); /* parse result? */
494 monitor_printf ("up %s\r", download_path); /* use default path */
496 monitor_printf ("up\r"); /* rooted filename/path */
497 resp_len = monitor_expect_prompt (buf, sizeof (buf)); /* parse result? */
499 if (strrchr (args, '.') && !strcmp (strrchr (args, '.'), ".srec"))
500 monitor_printf ("ul %s\r", args);
501 else /* add ".srec" suffix */
502 monitor_printf ("ul %s.srec\r", args);
503 resp_len = monitor_expect_prompt (buf, sizeof (buf)); /* parse result? */
505 if (buf[0] == 0 || strstr (buf, "complete") == 0)
507 ("Upload file not found: %s.srec\nCheck IP addresses and download path.",
510 printf_filtered (" -- Ethernet load complete.\n");
512 gettimeofday (&end_time, NULL);
513 abfd = bfd_openr (args, 0);
515 { /* Download is done -- print section statistics */
516 if (bfd_check_format (abfd, bfd_object) == 0)
518 printf_filtered ("File is not an object file\n");
520 for (s = abfd->sections; s; s = s->next)
521 if (s->flags & SEC_LOAD)
523 bfd_size_type section_size = bfd_section_size (abfd, s);
524 bfd_vma section_base = bfd_section_lma (abfd, s);
527 data_count += section_size;
529 printf_filtered ("Loading section %s, size 0x%lx lma ",
530 bfd_section_name (abfd, s),
531 (unsigned long) section_size);
532 fputs_filtered (paddress (target_gdbarch, section_base),
534 printf_filtered ("\n");
535 gdb_flush (gdb_stdout);
537 /* Finally, make the PC point at the start address */
538 regcache_write_pc (get_current_regcache (),
539 bfd_get_start_address (abfd));
540 printf_filtered ("Start address 0x%lx\n",
541 (unsigned long) bfd_get_start_address (abfd));
542 print_transfer_performance (gdb_stdout, data_count, 0, &start_time,
545 inferior_ptid = null_ptid; /* No process now */
547 /* This is necessary because many things were based on the PC at the
548 time that we attached to the monitor, which is no longer valid
549 now that we have loaded new code (and just changed the PC).
550 Another way to do this might be to call normal_stop, except that
551 the stack may not be valid, and things would get horribly
554 clear_symtab_users ();
557 /* Provide a prototype to silence -Wmissing-prototypes. */
558 extern initialize_file_ftype _initialize_m32r_rom;
561 _initialize_m32r_rom (void)
563 /* Initialize m32r RevC monitor target */
565 init_monitor_ops (&m32r_ops);
567 m32r_ops.to_shortname = "m32r";
568 m32r_ops.to_longname = "m32r monitor";
569 m32r_ops.to_load = m32r_load_gen; /* monitor lacks a download command */
570 m32r_ops.to_doc = "Debug via the m32r monitor.\n\
571 Specify the serial device it is connected to (e.g. /dev/ttya).";
572 m32r_ops.to_open = m32r_open;
573 add_target (&m32r_ops);
575 /* Initialize mon2000 monitor target */
576 init_mon2000_cmds ();
577 init_monitor_ops (&mon2000_ops);
579 mon2000_ops.to_shortname = "mon2000";
580 mon2000_ops.to_longname = "Mon2000 monitor";
581 mon2000_ops.to_load = m32r_load_gen; /* monitor lacks a download command */
582 mon2000_ops.to_doc = "Debug via the Mon2000 monitor.\n\
583 Specify the serial device it is connected to (e.g. /dev/ttya).";
584 mon2000_ops.to_open = mon2000_open;
585 add_target (&mon2000_ops);
587 add_setshow_string_cmd ("download-path", class_obscure, &download_path, _("\
588 Set the default path for downloadable SREC files."), _("\
589 Show the default path for downloadable SREC files."), _("\
590 Determines the default path for downloadable SREC files."),
592 NULL, /* FIXME: i18n: The default path for downloadable SREC files is %s. */
593 &setlist, &showlist);
595 add_setshow_string_cmd ("board-address", class_obscure, &board_addr, _("\
596 Set IP address for M32R-EVA target board."), _("\
597 Show IP address for M32R-EVA target board."), _("\
598 Determine the IP address for M32R-EVA target board."),
600 NULL, /* FIXME: i18n: IP address for M32R-EVA target board is %s. */
601 &setlist, &showlist);
603 add_setshow_string_cmd ("server-address", class_obscure, &server_addr, _("\
604 Set IP address for download server (GDB's host computer)."), _("\
605 Show IP address for download server (GDB's host computer)."), _("\
606 Determine the IP address for download server (GDB's host computer)."),
608 NULL, /* FIXME: i18n: IP address for download server (GDB's host computer) is %s. */
609 &setlist, &showlist);
611 add_com ("upload", class_obscure, m32r_upload_command, _("\
612 Upload the srec file via the monitor's Ethernet upload capability."));
614 add_com ("tload", class_obscure, m32r_load, _("test upload command."));