1 /* ICE interface for the NEC V850 for GDB, the GNU debugger.
2 Copyright 1996, 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 Pessublic 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, Boston, MA 02111-1307, USA. */
21 #include "gdb_string.h"
25 #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 HINSTANCE Tk_GetHINSTANCE
PARAMS ((void));
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
PARAMS ((const char *, GDB_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));
124 static HWND hidden_hwnd
; /* HWND for messages */
126 long (__stdcall
*ExeAppReq
) PARAMS ((char *, long, char *, struct MessageIO
*));
128 long (__stdcall
*RegisterClient
) PARAMS ((HWND
));
130 long (__stdcall
*UnregisterClient
) PARAMS ((void));
132 extern Tcl_Interp
*gdbtk_interp
;
134 /* Globals local to this file only */
135 static int ice_open
= 0; /* Is ICE open? */
137 static char * v850_CB_Result
; /* special char array for saving 'callback' results */
139 static int SimulateCallback
; /* simulate a callback event */
141 #define MAX_BLOCK_SIZE 64*1024 /* Cannot transfer memory in blocks bigger
143 /* MDI/ICE Message IDs */
144 #define GSINGLESTEP 0x200 /* single-step target */
145 #define GRESUME 0x201 /* resume target */
146 #define GREADREG 0x202 /* read a register */
147 #define GWRITEREG 0x203 /* write a register */
148 #define GWRITEBLOCK 0x204 /* write a block of memory */
149 #define GREADBLOCK 0x205 /* read a block of memory */
150 #define GSETBREAK 0x206 /* set a breakpoint */
151 #define GREMOVEBREAK 0x207 /* remove a breakpoint */
152 #define GHALT 0x208 /* ??? */
153 #define GCHECKSTATUS 0x209 /* check status of ICE */
154 #define GMDIREPLY 0x210 /* Reply for previous query - NOT USED */
155 #define GDOWNLOAD 0x211 /* something for MDI */
156 #define GCOMMAND 0x212 /* execute command in ice */
157 #define GLOADFILENAME 0x213 /* retrieve load filename */
158 #define GWRITEMEM 0x214 /* write word, half-word, or byte */
160 /* GCHECKSTATUS return codes: */
161 #define ICE_Idle 0x00
162 #define ICE_Breakpoint 0x01 /* hit a breakpoint */
163 #define ICE_Stepped 0x02 /* have stepped */
164 #define ICE_Exception 0x03 /* have exception */
165 #define ICE_Halted 0x04 /* hit a user halt */
166 #define ICE_Exited 0x05 /* called exit */
167 #define ICE_Terminated 0x06 /* user terminated */
168 #define ICE_Running 0x07
169 #define ICE_Unknown 0x99
171 /* Windows messages */
172 #define WM_STATE_CHANGE WM_USER+101
173 #define WM_SYM_TO_ADDR WM_USER+102
174 #define WM_ADDR_TO_SYM WM_USER+103
175 #define WM_DISASSEMBLY WM_USER+104
177 /* STATE_CHANGE codes */
178 #define STATE_CHANGE_REGS 1 /* Register(s) changed */
179 #define STATE_CHANGE_LOAD 2 /* HW reset */
180 #define STATE_CHANGE_RESET 3 /* Load new file */
181 #define STATE_CHANGE_CONT 4 /* Run target */
182 #define STATE_CHANGE_STOP 5 /* Stop target */
183 #define STATE_CHANGE_STEPI 6 /* Stepi target */
184 #define STATE_CHANGE_NEXTI 7 /* Nexti target */
186 static struct target_ops v850ice_ops
; /* Forward decl */
188 /* This function creates a hidden window */
190 init_hidden_window ()
194 if (hidden_hwnd
!= NULL
)
198 class.cbClsExtra
= 0;
199 class.cbWndExtra
= 0;
200 class.hInstance
= Tk_GetHINSTANCE();
201 class.hbrBackground
= NULL
;
202 class.lpszMenuName
= NULL
;
203 class.lpszClassName
= "gdbtk_v850ice";
204 class.lpfnWndProc
= v850ice_wndproc
;
206 class.hCursor
= NULL
;
208 if (! RegisterClass (&class))
211 hidden_hwnd
= CreateWindow ("gdbtk_v850ice", "gdbtk_v850ice", WS_TILED
,
212 0, 0, 0, 0, NULL
, NULL
, class.hInstance
,
214 if (hidden_hwnd
== NULL
)
219 err
= GetLastError ();
220 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, err
,
222 printf_unfiltered ("Could not create window: %s", buf
);
230 This function is installed as the message handler for the hidden window
231 which QBox will use to communicate with gdbtk. It recognize and acts
232 on the following messages:
235 WM_ADDR_TO_SYM | Not implemented at NEC's request
237 WM_STATE_CHANGE - tells us that a state change has occured in the ICE
239 static LRESULT CALLBACK
240 v850ice_wndproc (hwnd
, message
, wParam
, lParam
)
246 LRESULT result
= FALSE
;
251 MessageBox (0, "Symbol resolution\nNot implemented", "GDB", MB_OK
);
254 MessageBox (0, "Address resolution\nNot implemented", "GDB", MB_OK
);
256 case WM_STATE_CHANGE
:
259 case STATE_CHANGE_LOAD
:
261 struct MessageIO iob
;
267 /* Load in a new file... Need filename */
268 ExeAppReq ("GDB", GLOADFILENAME
, NULL
, &iob
);
269 if (!catch_errors (ice_file
, iob
.buf
, "", RETURN_MASK_ALL
))
270 printf_unfiltered ("load errored\n");
273 case STATE_CHANGE_RESET
:
274 registers_changed ();
275 flush_cached_frames ();
276 togdb_force_update ();
279 case STATE_CHANGE_REGS
:
280 registers_changed ();
281 togdb_force_update ();
284 case STATE_CHANGE_CONT
:
285 if (!catch_errors (ice_cont
, NULL
, "", RETURN_MASK_ALL
))
286 printf_unfiltered ("continue errored\n");
289 case STATE_CHANGE_STEPI
:
290 if (!catch_errors (ice_stepi
, (PTR
)(int) lParam
, "",
292 printf_unfiltered ("stepi errored\n");
295 case STATE_CHANGE_NEXTI
:
296 if (!catch_errors (ice_nexti
, (PTR
)(int) lParam
, "",
298 printf_unfiltered ("nexti errored\n");
305 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
310 /* Code for opening a connection to the ICE. */
313 v850ice_open (name
, from_tty
)
320 error ("Too many arguments.");
322 target_preopen (from_tty
);
324 unpush_target (&v850ice_ops
);
327 puts_filtered ("V850ice debugging\n");
329 push_target (&v850ice_ops
); /* Switch to using v850ice target now */
331 target_terminal_init ();
333 /* Initialize everything necessary to facilitate communication
334 between QBox, gdbtk, and the DLLs which control the ICE */
335 if (ExeAppReq
== NULL
)
337 handle
= LoadLibrary ("necmsg.dll");
339 error ("Cannot load necmsg.dll");
341 ExeAppReq
= (long (*) PARAMS ((char *, long, char *, struct MessageIO
*)))
342 GetProcAddress (handle
, "ExeAppReq");
343 RegisterClient
= (long (*) PARAMS ((HWND
)))
344 GetProcAddress (handle
, "RegisterClient");
345 UnregisterClient
= (long (*) PARAMS ((void)))
346 GetProcAddress (handle
, "UnregisterClient");
348 if (ExeAppReq
== NULL
|| RegisterClient
== NULL
|| UnregisterClient
== NULL
)
349 error ("Could not find requisite functions in necmsg.dll.");
351 if (init_hidden_window () != TCL_OK
)
352 error ("could not initialize message handling");
355 /* Tell the DLL we are here */
356 RegisterClient (hidden_hwnd
);
360 /* Without this, some commands which require an active target (such as kill)
361 won't work. This variable serves (at least) double duty as both the pid
362 of the target process (if it has such), and as a flag indicating that a
363 target is active. These functions should be split out into seperate
364 variables, especially since GDB will someday have a notion of debugging
365 several processes. */
366 inferior_pid
= 42000;
372 /* Clean up connection to a remote debugger. */
376 v850ice_close (quitting
)
387 /* Stop the process on the ice. */
391 /* This is silly, but it works... */
392 v850ice_command ("stop", 0);
396 v850ice_detach (args
, from_tty
)
401 error ("Argument given to \"detach\" when remotely debugging.");
405 puts_filtered ("Ending v850ice debugging.\n");
408 /* Tell the remote machine to resume. */
411 v850ice_resume (pid
, step
, siggnal
)
413 enum target_signal siggnal
;
417 struct MessageIO iob
;
423 retval
= ExeAppReq ("GDB", GSINGLESTEP
, "step", &iob
);
425 retval
= ExeAppReq ("GDB", GRESUME
, "run", &iob
);
428 error ("ExeAppReq (step = %d) returned %d", step
, retval
);
431 /* Wait until the remote machine stops, then return,
432 storing status in STATUS just as `wait' would.
433 Returns "pid" (though it's not clear what, if anything, that
434 means in the case of this target). */
437 v850ice_wait (pid
, status
)
439 struct target_waitstatus
*status
;
443 struct MessageIO iob
;
450 v850_status
= ExeAppReq ("GDB", GCHECKSTATUS
, NULL
, &iob
);
458 status
->kind
= TARGET_WAITKIND_STOPPED
;
459 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
463 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
464 status
->value
.sig
= TARGET_SIGNAL_SEGV
;
468 status
->kind
= TARGET_WAITKIND_EXITED
;
469 status
->value
.integer
= 0;
473 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
474 status
->value
.sig
= TARGET_SIGNAL_KILL
;
487 convert_register (regno
, buf
)
492 sprintf (buf
, "r%d", regno
);
493 else if (reg_names
[regno
][0] == 's'
494 && reg_names
[regno
][1] == 'r')
497 sprintf (buf
, "%s", reg_names
[regno
]);
502 /* Read the remote registers into the block REGS. */
503 /* Note that the ICE returns register contents as ascii hex strings. We have
504 to convert that to an unsigned long, and then call store_unsigned_integer to
505 convert it to target byte-order if necessary. */
508 v850ice_fetch_registers (regno
)
514 struct MessageIO iob
;
515 unsigned long regval
;
520 for (regno
= 0; regno
< NUM_REGS
; regno
++)
521 v850ice_fetch_registers (regno
);
525 strcpy (cmd
, "reg ");
526 if (!convert_register (regno
, &cmd
[4]))
529 iob
.size
= sizeof val
;
531 retval
= ExeAppReq ("GDB", GREADREG
, cmd
, &iob
);
533 error ("1: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
535 regval
= strtoul (val
, NULL
, 16);
536 if (regval
== 0 && p
== val
)
537 error ("v850ice_fetch_registers (%d): bad value from ICE: %s.",
540 store_unsigned_integer (val
, REGISTER_RAW_SIZE (regno
), regval
);
541 supply_register (regno
, val
);
544 /* Store register REGNO, or all registers if REGNO == -1, from the contents
548 v850ice_store_registers (regno
)
553 unsigned long regval
;
555 struct MessageIO iob
;
561 for (regno
= 0; regno
< NUM_REGS
; regno
++)
562 v850ice_store_registers (regno
);
566 regval
= extract_unsigned_integer (®isters
[REGISTER_BYTE (regno
)],
567 REGISTER_RAW_SIZE (regno
));
568 strcpy (cmd
, "reg ");
569 if (!convert_register (regno
, &cmd
[4]))
571 sprintf (cmd
+ strlen (cmd
), "=0x%x", regval
);
573 retval
= ExeAppReq ("GDB", GWRITEREG
, cmd
, &iob
);
575 error ("2: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
578 /* Prepare to store registers. Nothing to do here, since the ICE can write one
579 register at a time. */
582 v850ice_prepare_to_store ()
586 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
587 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
588 nonzero. Returns length of data written or read; 0 for error.
590 We can only read/write MAX_BLOCK_SIZE bytes at a time, though, or the DLL
594 v850ice_xfer_memory (memaddr
, myaddr
, len
, should_write
, target
)
599 struct target_ops
*target
; /* ignored */
603 struct MessageIO iob
;
608 if (len
== 4 || len
== 2 || len
== 1)
622 value
|= (long) (myaddr
[3] << 24) & 0xff000000;
623 value
|= (long) (myaddr
[2] << 16) & 0x00ff0000;
624 value
|= (long) (myaddr
[1] << 8) & 0x0000ff00;
625 value
|= (long) myaddr
[0] & 0x000000ff;
628 value
|= (long) myaddr
[1] << 8 & 0xff00;
629 value
|= (long) myaddr
[0] & 0x00ff;
633 value
|= (long) myaddr
[0] & 0xff;
637 sprintf (cmd
, "memory %c c 0x%x=0x%x", c
, (int) memaddr
, value
);
638 retval
= ExeAppReq ("GDB", GWRITEMEM
, cmd
, &iob
);
647 iob
.size
= len
> MAX_BLOCK_SIZE
? MAX_BLOCK_SIZE
: len
;
649 sprintf (cmd
, "memory b c 0x%x=0x00 l=%d", (int)memaddr
, iob
.size
);
650 retval
= ExeAppReq ("GDB", GWRITEBLOCK
, cmd
, &iob
);
667 tmp
= alloca (len
+ 100);
669 memset (tmp
+ len
, 0xff, 100);
674 iob
.size
= len
> MAX_BLOCK_SIZE
? MAX_BLOCK_SIZE
: len
;
676 sprintf (cmd
, "memory b 0x%x l=%d", (int)memaddr
, iob
.size
);
677 retval
= ExeAppReq ("GDB", GREADBLOCK
, cmd
, &iob
);
689 for (i
= 0; i
< 100; i
++)
691 if (t
[sent
+ i
] != 0xff)
693 warning ("GREADBLOCK trashed bytes after transfer area.");
697 memcpy (myaddr
, t
, sent
);
702 error ("3: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
708 v850ice_files_info (ignore
)
709 struct target_ops
*ignore
;
711 puts_filtered ("Debugging a target via the NEC V850 ICE.\n");
715 v850ice_insert_breakpoint (addr
, contents_cache
)
717 char *contents_cache
;
722 struct MessageIO iob
;
726 sprintf (cmd
, "%d, ", addr
);
728 retval
= ExeAppReq ("GDB", GSETBREAK
, cmd
, &iob
);
730 error ("ExeAppReq (GSETBREAK) returned %d: cmd = %s", retval
, cmd
);
736 v850ice_remove_breakpoint (addr
, contents_cache
)
738 char *contents_cache
;
743 struct MessageIO iob
;
748 sprintf (cmd
, "%d, ", addr
);
750 retval
= ExeAppReq ("GDB", GREMOVEBREAK
, cmd
, &iob
);
752 error ("ExeAppReq (GREMOVEBREAK) returned %d: cmd = %s", retval
, cmd
);
760 target_mourn_inferior ();
770 v850ice_load (filename
, from_tty
)
774 struct MessageIO iob
;
779 generic_load(filename
, from_tty
);
780 ExeAppReq ("GDB", GDOWNLOAD
, filename
, &iob
);
789 target_detach (NULL
, 0);
792 printf_unfiltered ("\n");
802 /* Safegaurd against confusing the breakpoint routines... */
803 delete_command(NULL
, 0);
805 /* Must supress from_tty, otherwise we could start asking if the
806 user really wants to load a new symbol table, etc... */
807 printf_unfiltered ("Reading symbols from %s...", arg
);
808 exec_file_command (arg
, 0);
809 symbol_file_command (arg
, 0);
810 printf_unfiltered ("done\n");
812 /* exec_file_command will kill our target, so reinstall the ICE as
814 v850ice_open (NULL
, 0);
816 togdb_force_update ();
824 printf_filtered ("continue (ice)");
825 Tcl_Eval (gdbtk_interp
, "gdb_immediate continue");
833 char string
[50] = "\0";
836 sprintf (string
, "gdb_immediate stepi %d", count
);
837 printf_unfiltered ("stepi (ice)\n");
838 Tcl_Eval (gdbtk_interp
, string
);
839 ReplyMessage ((LRESULT
) 1);
847 char string
[50] = "\0";
850 sprintf (string
, "gdb_immediate nexti %d", count
);
851 printf_unfiltered ("nexti (ice)\n");
852 Tcl_Eval (gdbtk_interp
, string
);
857 v850ice_command (arg
, from_tty
)
861 struct MessageIO iob
;
866 ExeAppReq ("GDB", GCOMMAND
, arg
, &iob
);
870 togdb_force_update (void)
872 Tcl_Eval (gdbtk_interp
, "gdbtk_update");
875 /* Define the target subroutine names */
877 static void init_850ice_ops(void)
879 v850ice_ops
.to_shortname
= "ice";
880 v850ice_ops
.to_longname
= "NEC V850 ICE interface";
881 v850ice_ops
.to_doc
= "Debug a system controlled by a NEC 850 ICE.";
882 v850ice_ops
.to_open
= v850ice_open
;
883 v850ice_ops
.to_close
= v850ice_close
;
884 v850ice_ops
.to_attach
= NULL
;
885 v850ice_ops
.to_detach
= v850ice_detach
;
886 v850ice_ops
.to_resume
= v850ice_resume
;
887 v850ice_ops
.to_wait
= v850ice_wait
;
888 v850ice_ops
.to_fetch_registers
= v850ice_fetch_registers
;
889 v850ice_ops
.to_store_registers
= v850ice_store_registers
;
890 v850ice_ops
.to_prepare_to_store
= v850ice_prepare_to_store
;
891 v850ice_ops
.to_xfer_memory
= v850ice_xfer_memory
;
892 v850ice_ops
.to_files_info
= v850ice_files_info
;
893 v850ice_ops
.to_insert_breakpoint
= v850ice_insert_breakpoint
;
894 v850ice_ops
.to_remove_breakpoint
= v850ice_remove_breakpoint
;
895 v850ice_ops
.to_terminal_init
= NULL
;
896 v850ice_ops
.to_terminal_inferior
= NULL
;
897 v850ice_ops
.to_terminal_ours_for_output
= NULL
;
898 v850ice_ops
.to_terminal_ours
= NULL
;
899 v850ice_ops
.to_terminal_info
= NULL
;
900 v850ice_ops
.to_kill
= v850ice_kill
;
901 v850ice_ops
.to_load
= v850ice_load
;
902 v850ice_ops
.to_lookup_symbol
= NULL
;
903 v850ice_ops
.to_create_inferior
= NULL
;
904 v850ice_ops
.to_mourn_inferior
= v850ice_mourn
;
905 v850ice_ops
.to_can_run
= 0;
906 v850ice_ops
.to_notice_signals
= 0;
907 v850ice_ops
.to_thread_alive
= NULL
;
908 v850ice_ops
.to_stop
= v850ice_stop
;
909 v850ice_ops
.to_stratum
= process_stratum
;
910 v850ice_ops
.DONT_USE
= NULL
;
911 v850ice_ops
.to_has_all_memory
= 1;
912 v850ice_ops
.to_has_memory
= 1;
913 v850ice_ops
.to_has_stack
= 1;
914 v850ice_ops
.to_has_registers
= 1;
915 v850ice_ops
.to_has_execution
= 1;
916 v850ice_ops
.to_sections
= NULL
;
917 v850ice_ops
.to_sections_end
= NULL
;
918 v850ice_ops
.to_magic
= OPS_MAGIC
;
922 _initialize_v850ice ()
925 add_target (&v850ice_ops
);
927 add_com ("ice", class_obscure
, v850ice_command
,
928 "Send command to ICE");