1 /* ICE interface for the NEC V850 for GDB, the GNU debugger.
2 Copyright 1996, 2000 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 #include "gdb_string.h"
26 #include "breakpoint.h"
36 #include <winuser.h> /* for WM_USER */
38 extern unsigned long int strtoul (const char *nptr, char **endptr,
41 /* Local data definitions */
44 int size; /* length of input or output in bytes */
45 char *buf; /* buffer having the input/output information */
48 /* Prototypes for functions located in other files */
49 extern void break_command PARAMS ((char *, int));
51 extern void stepi_command PARAMS ((char *, int));
53 extern void nexti_command PARAMS ((char *, int));
55 extern void continue_command PARAMS ((char *, int));
57 extern int (*ui_loop_hook) PARAMS ((int));
59 /* Prototypes for local functions */
60 static int init_hidden_window PARAMS ((void));
62 static LRESULT CALLBACK v850ice_wndproc PARAMS ((HWND, UINT, WPARAM, LPARAM));
64 static void v850ice_files_info PARAMS ((struct target_ops * ignore));
66 static int v850ice_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
67 int len, int should_write,
68 struct target_ops * target));
70 static void v850ice_prepare_to_store PARAMS ((void));
72 static void v850ice_fetch_registers PARAMS ((int regno));
74 static void v850ice_resume PARAMS ((int pid, int step,
75 enum target_signal siggnal));
77 static void v850ice_open PARAMS ((char *name, int from_tty));
79 static void v850ice_close PARAMS ((int quitting));
81 static void v850ice_stop PARAMS ((void));
83 static void v850ice_store_registers PARAMS ((int regno));
85 static void v850ice_mourn PARAMS ((void));
87 static int v850ice_wait PARAMS ((int pid, struct target_waitstatus * status));
89 static void v850ice_kill PARAMS ((void));
91 static void v850ice_detach PARAMS ((char *args, int from_tty));
93 static int v850ice_insert_breakpoint PARAMS ((CORE_ADDR, char *));
95 static int v850ice_remove_breakpoint PARAMS ((CORE_ADDR, char *));
97 static void v850ice_command PARAMS ((char *, int));
99 static int ice_disassemble PARAMS ((unsigned long, int, char *));
101 static int ice_lookup_addr PARAMS ((unsigned long *, char *, char *));
103 static int ice_lookup_symbol PARAMS ((unsigned long, char *));
105 static void ice_SimulateDisassemble PARAMS ((char *, int));
107 static void ice_SimulateAddrLookup PARAMS ((char *, int));
109 static void ice_Simulate_SymLookup PARAMS ((char *, int));
111 static void ice_fputs (const char *, struct ui_file *);
113 static int ice_file PARAMS ((char *));
115 static int ice_cont PARAMS ((char *));
117 static int ice_stepi PARAMS ((char *));
119 static int ice_nexti PARAMS ((char *));
121 static void togdb_force_update PARAMS ((void));
123 static void view_source PARAMS ((CORE_ADDR));
125 static void do_gdb (char *, char *, void (*func) PARAMS ((char *, int)), int);
129 static HWND hidden_hwnd; /* HWND for messages */
131 long (__stdcall * ExeAppReq) PARAMS ((char *, long, char *, struct MessageIO *));
133 long (__stdcall * RegisterClient) PARAMS ((HWND));
135 long (__stdcall * UnregisterClient) PARAMS ((void));
137 extern Tcl_Interp *gdbtk_interp;
139 /* Globals local to this file only */
140 static int ice_open = 0; /* Is ICE open? */
142 static char *v850_CB_Result; /* special char array for saving 'callback' results */
144 static int SimulateCallback; /* simulate a callback event */
146 #define MAX_BLOCK_SIZE 64*1024 /* Cannot transfer memory in blocks bigger
148 /* MDI/ICE Message IDs */
149 #define GSINGLESTEP 0x200 /* single-step target */
150 #define GRESUME 0x201 /* resume target */
151 #define GREADREG 0x202 /* read a register */
152 #define GWRITEREG 0x203 /* write a register */
153 #define GWRITEBLOCK 0x204 /* write a block of memory */
154 #define GREADBLOCK 0x205 /* read a block of memory */
155 #define GSETBREAK 0x206 /* set a breakpoint */
156 #define GREMOVEBREAK 0x207 /* remove a breakpoint */
157 #define GHALT 0x208 /* ??? */
158 #define GCHECKSTATUS 0x209 /* check status of ICE */
159 #define GMDIREPLY 0x210 /* Reply for previous query - NOT USED */
160 #define GDOWNLOAD 0x211 /* something for MDI */
161 #define GCOMMAND 0x212 /* execute command in ice */
162 #define GLOADFILENAME 0x213 /* retrieve load filename */
163 #define GWRITEMEM 0x214 /* write word, half-word, or byte */
165 /* GCHECKSTATUS return codes: */
166 #define ICE_Idle 0x00
167 #define ICE_Breakpoint 0x01 /* hit a breakpoint */
168 #define ICE_Stepped 0x02 /* have stepped */
169 #define ICE_Exception 0x03 /* have exception */
170 #define ICE_Halted 0x04 /* hit a user halt */
171 #define ICE_Exited 0x05 /* called exit */
172 #define ICE_Terminated 0x06 /* user terminated */
173 #define ICE_Running 0x07
174 #define ICE_Unknown 0x99
176 /* Windows messages */
177 #define WM_STATE_CHANGE WM_USER+101
178 #define WM_SYM_TO_ADDR WM_USER+102
179 #define WM_ADDR_TO_SYM WM_USER+103
180 #define WM_DISASSEMBLY WM_USER+104
181 #define WM_SOURCE WM_USER+105
183 /* STATE_CHANGE codes */
184 #define STATE_CHANGE_REGS 1 /* Register(s) changed */
185 #define STATE_CHANGE_LOAD 2 /* HW reset */
186 #define STATE_CHANGE_RESET 3 /* Load new file */
187 #define STATE_CHANGE_CONT 4 /* Run target */
188 #define STATE_CHANGE_STOP 5 /* Stop target */
189 #define STATE_CHANGE_STEPI 6 /* Stepi target */
190 #define STATE_CHANGE_NEXTI 7 /* Nexti target */
192 static struct target_ops v850ice_ops; /* Forward decl */
194 /* This function creates a hidden window */
196 init_hidden_window ()
200 if (hidden_hwnd != NULL)
204 class.cbClsExtra = 0;
205 class.cbWndExtra = 0;
206 class.hInstance = GetModuleHandle (0);
207 class.hbrBackground = NULL;
208 class.lpszMenuName = NULL;
209 class.lpszClassName = "gdb_v850ice";
210 class.lpfnWndProc = v850ice_wndproc;
212 class.hCursor = NULL;
214 if (!RegisterClass (&class))
217 hidden_hwnd = CreateWindow ("gdb_v850ice", "gdb_v850ice", WS_TILED,
218 0, 0, 0, 0, NULL, NULL, class.hInstance,
220 if (hidden_hwnd == NULL)
225 err = GetLastError ();
226 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
228 printf_unfiltered ("Could not create window: %s", buf);
236 This function is installed as the message handler for the hidden window
237 which QBox will use to communicate with gdb. It recognize and acts
238 on the following messages:
241 WM_ADDR_TO_SYM | Not implemented at NEC's request
243 WM_STATE_CHANGE - tells us that a state change has occured in the ICE
245 static LRESULT CALLBACK
246 v850ice_wndproc (hwnd, message, wParam, lParam)
252 LRESULT result = FALSE;
257 MessageBox (0, "Symbol resolution\nNot implemented", "GDB", MB_OK);
260 MessageBox (0, "Address resolution\nNot implemented", "GDB", MB_OK);
263 view_source ((CORE_ADDR) lParam);
265 case WM_STATE_CHANGE:
268 case STATE_CHANGE_LOAD:
270 struct MessageIO iob;
276 /* Load in a new file... Need filename */
277 ExeAppReq ("GDB", GLOADFILENAME, NULL, &iob);
278 if (!catch_errors ((catch_errors_ftype *) ice_file, iob.buf, "", RETURN_MASK_ALL))
279 printf_unfiltered ("load errored\n");
282 case STATE_CHANGE_RESET:
283 registers_changed ();
284 flush_cached_frames ();
285 togdb_force_update ();
288 case STATE_CHANGE_REGS:
289 registers_changed ();
290 togdb_force_update ();
293 case STATE_CHANGE_CONT:
294 if (!catch_errors ((catch_errors_ftype *) ice_cont, NULL, "", RETURN_MASK_ALL))
295 printf_unfiltered ("continue errored\n");
298 case STATE_CHANGE_STEPI:
299 if (!catch_errors ((catch_errors_ftype *) ice_stepi, (PTR) (int) lParam, "",
301 printf_unfiltered ("stepi errored\n");
304 case STATE_CHANGE_NEXTI:
305 if (!catch_errors ((catch_errors_ftype *) ice_nexti, (PTR) (int) lParam, "",
307 printf_unfiltered ("nexti errored\n");
314 return DefWindowProc (hwnd, message, wParam, lParam);
319 /* Code for opening a connection to the ICE. */
322 v850ice_open (name, from_tty)
329 error ("Too many arguments.");
331 target_preopen (from_tty);
333 unpush_target (&v850ice_ops);
336 puts_filtered ("V850ice debugging\n");
338 push_target (&v850ice_ops); /* Switch to using v850ice target now */
340 target_terminal_init ();
342 /* Initialize everything necessary to facilitate communication
343 between QBox, gdb, and the DLLs which control the ICE */
344 if (ExeAppReq == NULL)
346 handle = LoadLibrary ("necmsg.dll");
348 error ("Cannot load necmsg.dll");
350 ExeAppReq = (long (*)PARAMS ((char *, long, char *, struct MessageIO *)))
351 GetProcAddress (handle, "ExeAppReq");
352 RegisterClient = (long (*)PARAMS ((HWND)))
353 GetProcAddress (handle, "RegisterClient");
354 UnregisterClient = (long (*)PARAMS ((void)))
355 GetProcAddress (handle, "UnregisterClient");
357 if (ExeAppReq == NULL || RegisterClient == NULL || UnregisterClient == NULL)
358 error ("Could not find requisite functions in necmsg.dll.");
360 if (!init_hidden_window ())
361 error ("could not initialize message handling");
364 /* Tell the DLL we are here */
365 RegisterClient (hidden_hwnd);
369 /* Without this, some commands which require an active target (such as kill)
370 won't work. This variable serves (at least) double duty as both the pid
371 of the target process (if it has such), and as a flag indicating that a
372 target is active. These functions should be split out into seperate
373 variables, especially since GDB will someday have a notion of debugging
374 several processes. */
375 inferior_pid = 42000;
381 /* Clean up connection to a remote debugger. */
385 v850ice_close (quitting)
396 /* Stop the process on the ice. */
400 /* This is silly, but it works... */
401 v850ice_command ("stop", 0);
405 v850ice_detach (args, from_tty)
410 error ("Argument given to \"detach\" when remotely debugging.");
414 puts_filtered ("Ending v850ice debugging.\n");
417 /* Tell the remote machine to resume. */
420 v850ice_resume (pid, step, siggnal)
422 enum target_signal siggnal;
426 struct MessageIO iob;
432 retval = ExeAppReq ("GDB", GSINGLESTEP, "step", &iob);
434 retval = ExeAppReq ("GDB", GRESUME, "run", &iob);
437 error ("ExeAppReq (step = %d) returned %d", step, retval);
440 /* Wait until the remote machine stops, then return,
441 storing status in STATUS just as `wait' would.
442 Returns "pid" (though it's not clear what, if anything, that
443 means in the case of this target). */
446 v850ice_wait (pid, status)
448 struct target_waitstatus *status;
452 struct MessageIO iob;
461 if (count++ % 100000)
467 v850_status = ExeAppReq ("GDB", GCHECKSTATUS, NULL, &iob);
475 status->kind = TARGET_WAITKIND_STOPPED;
476 status->value.sig = TARGET_SIGNAL_TRAP;
480 status->kind = TARGET_WAITKIND_SIGNALLED;
481 status->value.sig = TARGET_SIGNAL_SEGV;
485 status->kind = TARGET_WAITKIND_EXITED;
486 status->value.integer = 0;
490 status->kind = TARGET_WAITKIND_SIGNALLED;
491 status->value.sig = TARGET_SIGNAL_KILL;
504 convert_register (regno, buf)
509 sprintf (buf, "r%d", regno);
510 else if (REGISTER_NAME (regno)[0] == 's'
511 && REGISTER_NAME (regno)[1] == 'r')
514 sprintf (buf, "%s", REGISTER_NAME (regno));
519 /* Read the remote registers into the block REGS. */
520 /* Note that the ICE returns register contents as ascii hex strings. We have
521 to convert that to an unsigned long, and then call store_unsigned_integer to
522 convert it to target byte-order if necessary. */
525 v850ice_fetch_registers (regno)
531 struct MessageIO iob;
532 unsigned long regval;
537 for (regno = 0; regno < NUM_REGS; regno++)
538 v850ice_fetch_registers (regno);
542 strcpy (cmd, "reg ");
543 if (!convert_register (regno, &cmd[4]))
546 iob.size = sizeof val;
548 retval = ExeAppReq ("GDB", GREADREG, cmd, &iob);
550 error ("1: ExeAppReq returned %d: cmd = %s", retval, cmd);
552 regval = strtoul (val, NULL, 16);
553 if (regval == 0 && p == val)
554 error ("v850ice_fetch_registers (%d): bad value from ICE: %s.",
557 store_unsigned_integer (val, REGISTER_RAW_SIZE (regno), regval);
558 supply_register (regno, val);
561 /* Store register REGNO, or all registers if REGNO == -1, from the contents
565 v850ice_store_registers (regno)
570 unsigned long regval;
572 struct MessageIO iob;
578 for (regno = 0; regno < NUM_REGS; regno++)
579 v850ice_store_registers (regno);
583 regval = extract_unsigned_integer (®isters[REGISTER_BYTE (regno)],
584 REGISTER_RAW_SIZE (regno));
585 strcpy (cmd, "reg ");
586 if (!convert_register (regno, &cmd[4]))
588 sprintf (cmd + strlen (cmd), "=0x%x", regval);
590 retval = ExeAppReq ("GDB", GWRITEREG, cmd, &iob);
592 error ("2: ExeAppReq returned %d: cmd = %s", retval, cmd);
595 /* Prepare to store registers. Nothing to do here, since the ICE can write one
596 register at a time. */
599 v850ice_prepare_to_store ()
603 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
604 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
605 nonzero. Returns length of data written or read; 0 for error.
607 We can only read/write MAX_BLOCK_SIZE bytes at a time, though, or the DLL
611 v850ice_xfer_memory (memaddr, myaddr, len, should_write, target)
616 struct target_ops *target; /* ignored */
620 struct MessageIO iob;
625 if (len == 4 || len == 2 || len == 1)
639 value |= (long) ((myaddr[3] << 24) & 0xff000000);
640 value |= (long) ((myaddr[2] << 16) & 0x00ff0000);
641 value |= (long) ((myaddr[1] << 8) & 0x0000ff00);
642 value |= (long) (myaddr[0] & 0x000000ff);
646 value |= (long) ((myaddr[1] << 8) & 0xff00);
647 value |= (long) (myaddr[0] & 0x00ff);
651 value |= (long) (myaddr[0] & 0xff);
655 sprintf (cmd, "memory %c c 0x%x=0x%x", c, (int) memaddr, value);
656 retval = ExeAppReq ("GDB", GWRITEMEM, cmd, &iob);
665 iob.size = len > MAX_BLOCK_SIZE ? MAX_BLOCK_SIZE : len;
667 sprintf (cmd, "memory b c 0x%x=0x00 l=%d", (int) memaddr, iob.size);
668 retval = ExeAppReq ("GDB", GWRITEBLOCK, cmd, &iob);
685 tmp = alloca (len + 100);
687 memset (tmp + len, 0xff, 100);
692 iob.size = len > MAX_BLOCK_SIZE ? MAX_BLOCK_SIZE : len;
694 sprintf (cmd, "memory b 0x%x l=%d", (int) memaddr, iob.size);
695 retval = ExeAppReq ("GDB", GREADBLOCK, cmd, &iob);
707 for (i = 0; i < 100; i++)
709 if (t[sent + i] != 0xff)
711 warning ("GREADBLOCK trashed bytes after transfer area.");
715 memcpy (myaddr, t, sent);
720 error ("3: ExeAppReq returned %d: cmd = %s", retval, cmd);
726 v850ice_files_info (ignore)
727 struct target_ops *ignore;
729 puts_filtered ("Debugging a target via the NEC V850 ICE.\n");
733 v850ice_insert_breakpoint (addr, contents_cache)
735 char *contents_cache;
740 struct MessageIO iob;
744 sprintf (cmd, "%d, ", addr);
746 retval = ExeAppReq ("GDB", GSETBREAK, cmd, &iob);
748 error ("ExeAppReq (GSETBREAK) returned %d: cmd = %s", retval, cmd);
754 v850ice_remove_breakpoint (addr, contents_cache)
756 char *contents_cache;
761 struct MessageIO iob;
766 sprintf (cmd, "%d, ", addr);
768 retval = ExeAppReq ("GDB", GREMOVEBREAK, cmd, &iob);
770 error ("ExeAppReq (GREMOVEBREAK) returned %d: cmd = %s", retval, cmd);
778 target_mourn_inferior ();
788 v850ice_load (filename, from_tty)
792 struct MessageIO iob;
797 generic_load (filename, from_tty);
798 ExeAppReq ("GDB", GDOWNLOAD, filename, &iob);
807 target_detach (NULL, 0);
810 printf_unfiltered ("\n");
820 /* Safegaurd against confusing the breakpoint routines... */
821 delete_command (NULL, 0);
823 /* Must supress from_tty, otherwise we could start asking if the
824 user really wants to load a new symbol table, etc... */
825 printf_unfiltered ("Reading symbols from %s...", arg);
826 exec_file_command (arg, 0);
827 symbol_file_command (arg, 0);
828 printf_unfiltered ("done\n");
830 /* exec_file_command will kill our target, so reinstall the ICE as
832 v850ice_open (NULL, 0);
834 togdb_force_update ();
842 printf_filtered ("continue (ice)\n");
843 ReplyMessage ((LRESULT) 1);
845 if (gdbtk_interp == NULL)
847 continue_command (NULL, 1);
850 Tcl_Eval (gdbtk_interp, "gdb_immediate continue");
856 do_gdb (cmd, str, func, count)
859 void (*func) PARAMS ((char *, int));
862 ReplyMessage ((LRESULT) 1);
866 printf_unfiltered (str);
868 if (gdbtk_interp == NULL)
873 Tcl_Eval (gdbtk_interp, cmd);
884 do_gdb ("gdb_immediate stepi", "stepi (ice)\n", stepi_command, count);
894 do_gdb ("gdb_immediate nexti", "nexti (ice)\n", nexti_command, count);
899 v850ice_command (arg, from_tty)
903 struct MessageIO iob;
908 ExeAppReq ("GDB", GCOMMAND, arg, &iob);
912 togdb_force_update (void)
914 if (gdbtk_interp != NULL)
915 Tcl_Eval (gdbtk_interp, "gdbtk_update");
924 if (gdbtk_interp != NULL)
926 sprintf (c, "catch {set src [lindex [ManagedWin::find SrcWin] 0]\n$src location BROWSE [gdb_loc *0x%x]}", addr);
927 Tcl_Eval (gdbtk_interp, c);
931 /* Define the target subroutine names */
934 init_850ice_ops (void)
936 v850ice_ops.to_shortname = "ice";
937 v850ice_ops.to_longname = "NEC V850 ICE interface";
938 v850ice_ops.to_doc = "Debug a system controlled by a NEC 850 ICE.";
939 v850ice_ops.to_open = v850ice_open;
940 v850ice_ops.to_close = v850ice_close;
941 v850ice_ops.to_attach = NULL;
942 v850ice_ops.to_post_attach = NULL;
943 v850ice_ops.to_require_attach = NULL;
944 v850ice_ops.to_detach = v850ice_detach;
945 v850ice_ops.to_require_detach = NULL;
946 v850ice_ops.to_resume = v850ice_resume;
947 v850ice_ops.to_wait = v850ice_wait;
948 v850ice_ops.to_post_wait = NULL;
949 v850ice_ops.to_fetch_registers = v850ice_fetch_registers;
950 v850ice_ops.to_store_registers = v850ice_store_registers;
951 v850ice_ops.to_prepare_to_store = v850ice_prepare_to_store;
952 v850ice_ops.to_xfer_memory = v850ice_xfer_memory;
953 v850ice_ops.to_files_info = v850ice_files_info;
954 v850ice_ops.to_insert_breakpoint = v850ice_insert_breakpoint;
955 v850ice_ops.to_remove_breakpoint = v850ice_remove_breakpoint;
956 v850ice_ops.to_terminal_init = NULL;
957 v850ice_ops.to_terminal_inferior = NULL;
958 v850ice_ops.to_terminal_ours_for_output = NULL;
959 v850ice_ops.to_terminal_ours = NULL;
960 v850ice_ops.to_terminal_info = NULL;
961 v850ice_ops.to_kill = v850ice_kill;
962 v850ice_ops.to_load = v850ice_load;
963 v850ice_ops.to_lookup_symbol = NULL;
964 v850ice_ops.to_create_inferior = NULL;
965 v850ice_ops.to_mourn_inferior = v850ice_mourn;
966 v850ice_ops.to_can_run = 0;
967 v850ice_ops.to_notice_signals = 0;
968 v850ice_ops.to_thread_alive = NULL;
969 v850ice_ops.to_stop = v850ice_stop;
970 v850ice_ops.to_pid_to_exec_file = NULL;
971 v850ice_ops.to_core_file_to_sym_file = NULL;
972 v850ice_ops.to_stratum = process_stratum;
973 v850ice_ops.DONT_USE = NULL;
974 v850ice_ops.to_has_all_memory = 1;
975 v850ice_ops.to_has_memory = 1;
976 v850ice_ops.to_has_stack = 1;
977 v850ice_ops.to_has_registers = 1;
978 v850ice_ops.to_has_execution = 1;
979 v850ice_ops.to_sections = NULL;
980 v850ice_ops.to_sections_end = NULL;
981 v850ice_ops.to_magic = OPS_MAGIC;
985 _initialize_v850ice ()
988 add_target (&v850ice_ops);
990 add_com ("ice", class_obscure, v850ice_command,
991 "Send command to ICE");