1 /* ICE interface for the NEC V850 for GDB, the GNU debugger.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "gdb_string.h"
27 #include "breakpoint.h"
38 #include <winuser.h> /* for WM_USER */
40 extern unsigned long int strtoul (const char *nptr
, char **endptr
,
43 /* Local data definitions */
46 int size
; /* length of input or output in bytes */
47 char *buf
; /* buffer having the input/output information */
50 /* Prototypes for functions located in other files */
51 extern void break_command (char *, int);
53 extern void stepi_command (char *, int);
55 extern void nexti_command (char *, int);
57 extern void continue_command (char *, int);
59 extern int (*ui_loop_hook
) (int);
61 /* Prototypes for local functions */
62 static int init_hidden_window (void);
64 static LRESULT CALLBACK
v850ice_wndproc (HWND
, UINT
, WPARAM
, LPARAM
);
66 static void v850ice_files_info (struct target_ops
*ignore
);
68 static int v850ice_xfer_memory (CORE_ADDR memaddr
, char *myaddr
,
69 int len
, int should_write
,
70 struct target_ops
*target
);
72 static void v850ice_prepare_to_store (void);
74 static void v850ice_fetch_registers (int regno
);
76 static void v850ice_resume (int pid
, int step
, enum target_signal siggnal
);
78 static void v850ice_open (char *name
, int from_tty
);
80 static void v850ice_close (int quitting
);
82 static void v850ice_stop (void);
84 static void v850ice_store_registers (int regno
);
86 static void v850ice_mourn (void);
88 static int v850ice_wait (int pid
, struct target_waitstatus
*status
);
90 static void v850ice_kill (void);
92 static void v850ice_detach (char *args
, int from_tty
);
94 static int v850ice_insert_breakpoint (CORE_ADDR
, char *);
96 static int v850ice_remove_breakpoint (CORE_ADDR
, char *);
98 static void v850ice_command (char *, int);
100 static int ice_disassemble (unsigned long, int, char *);
102 static int ice_lookup_addr (unsigned long *, char *, char *);
104 static int ice_lookup_symbol (unsigned long, char *);
106 static void ice_SimulateDisassemble (char *, int);
108 static void ice_SimulateAddrLookup (char *, int);
110 static void ice_Simulate_SymLookup (char *, int);
112 static void ice_fputs (const char *, struct ui_file
*);
114 static int ice_file (char *);
116 static int ice_cont (char *);
118 static int ice_stepi (char *);
120 static int ice_nexti (char *);
122 static void togdb_force_update (void);
124 static void view_source (CORE_ADDR
);
126 static void do_gdb (char *, char *, void (*func
) (char *, int), int);
130 static HWND hidden_hwnd
; /* HWND for messages */
132 long (__stdcall
* ExeAppReq
) (char *, long, char *, struct MessageIO
*);
134 long (__stdcall
* RegisterClient
) (HWND
);
136 long (__stdcall
* UnregisterClient
) (void);
138 extern Tcl_Interp
*gdbtk_interp
;
140 /* Globals local to this file only */
141 static int ice_open
= 0; /* Is ICE open? */
143 static char *v850_CB_Result
; /* special char array for saving 'callback' results */
145 static int SimulateCallback
; /* simulate a callback event */
147 #define MAX_BLOCK_SIZE 64*1024 /* Cannot transfer memory in blocks bigger
149 /* MDI/ICE Message IDs */
150 #define GSINGLESTEP 0x200 /* single-step target */
151 #define GRESUME 0x201 /* resume target */
152 #define GREADREG 0x202 /* read a register */
153 #define GWRITEREG 0x203 /* write a register */
154 #define GWRITEBLOCK 0x204 /* write a block of memory */
155 #define GREADBLOCK 0x205 /* read a block of memory */
156 #define GSETBREAK 0x206 /* set a breakpoint */
157 #define GREMOVEBREAK 0x207 /* remove a breakpoint */
158 #define GHALT 0x208 /* ??? */
159 #define GCHECKSTATUS 0x209 /* check status of ICE */
160 #define GMDIREPLY 0x210 /* Reply for previous query - NOT USED */
161 #define GDOWNLOAD 0x211 /* something for MDI */
162 #define GCOMMAND 0x212 /* execute command in ice */
163 #define GLOADFILENAME 0x213 /* retrieve load filename */
164 #define GWRITEMEM 0x214 /* write word, half-word, or byte */
166 /* GCHECKSTATUS return codes: */
167 #define ICE_Idle 0x00
168 #define ICE_Breakpoint 0x01 /* hit a breakpoint */
169 #define ICE_Stepped 0x02 /* have stepped */
170 #define ICE_Exception 0x03 /* have exception */
171 #define ICE_Halted 0x04 /* hit a user halt */
172 #define ICE_Exited 0x05 /* called exit */
173 #define ICE_Terminated 0x06 /* user terminated */
174 #define ICE_Running 0x07
175 #define ICE_Unknown 0x99
177 /* Windows messages */
178 #define WM_STATE_CHANGE WM_USER+101
179 #define WM_SYM_TO_ADDR WM_USER+102
180 #define WM_ADDR_TO_SYM WM_USER+103
181 #define WM_DISASSEMBLY WM_USER+104
182 #define WM_SOURCE WM_USER+105
184 /* STATE_CHANGE codes */
185 #define STATE_CHANGE_REGS 1 /* Register(s) changed */
186 #define STATE_CHANGE_LOAD 2 /* HW reset */
187 #define STATE_CHANGE_RESET 3 /* Load new file */
188 #define STATE_CHANGE_CONT 4 /* Run target */
189 #define STATE_CHANGE_STOP 5 /* Stop target */
190 #define STATE_CHANGE_STEPI 6 /* Stepi target */
191 #define STATE_CHANGE_NEXTI 7 /* Nexti target */
193 static struct target_ops v850ice_ops
; /* Forward decl */
195 /* This function creates a hidden window */
197 init_hidden_window (void)
201 if (hidden_hwnd
!= NULL
)
205 class.cbClsExtra
= 0;
206 class.cbWndExtra
= 0;
207 class.hInstance
= GetModuleHandle (0);
208 class.hbrBackground
= NULL
;
209 class.lpszMenuName
= NULL
;
210 class.lpszClassName
= "gdb_v850ice";
211 class.lpfnWndProc
= v850ice_wndproc
;
213 class.hCursor
= NULL
;
215 if (!RegisterClass (&class))
218 hidden_hwnd
= CreateWindow ("gdb_v850ice", "gdb_v850ice", WS_TILED
,
219 0, 0, 0, 0, NULL
, NULL
, class.hInstance
,
221 if (hidden_hwnd
== NULL
)
226 err
= GetLastError ();
227 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, err
,
229 printf_unfiltered ("Could not create window: %s", buf
);
237 This function is installed as the message handler for the hidden window
238 which QBox will use to communicate with gdb. It recognize and acts
239 on the following messages:
242 WM_ADDR_TO_SYM | Not implemented at NEC's request
244 WM_STATE_CHANGE - tells us that a state change has occured in the ICE
246 static LRESULT CALLBACK
247 v850ice_wndproc (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
249 LRESULT result
= FALSE
;
254 MessageBox (0, "Symbol resolution\nNot implemented", "GDB", MB_OK
);
257 MessageBox (0, "Address resolution\nNot implemented", "GDB", MB_OK
);
260 view_source ((CORE_ADDR
) lParam
);
262 case WM_STATE_CHANGE
:
265 case STATE_CHANGE_LOAD
:
267 struct MessageIO iob
;
273 /* Load in a new file... Need filename */
274 ExeAppReq ("GDB", GLOADFILENAME
, NULL
, &iob
);
275 if (!catch_errors ((catch_errors_ftype
*) ice_file
, iob
.buf
, "", RETURN_MASK_ALL
))
276 printf_unfiltered ("load errored\n");
279 case STATE_CHANGE_RESET
:
280 registers_changed ();
281 flush_cached_frames ();
282 togdb_force_update ();
285 case STATE_CHANGE_REGS
:
286 registers_changed ();
287 togdb_force_update ();
290 case STATE_CHANGE_CONT
:
291 if (!catch_errors ((catch_errors_ftype
*) ice_cont
, NULL
, "", RETURN_MASK_ALL
))
292 printf_unfiltered ("continue errored\n");
295 case STATE_CHANGE_STEPI
:
296 if (!catch_errors ((catch_errors_ftype
*) ice_stepi
, (PTR
) (int) lParam
, "",
298 printf_unfiltered ("stepi errored\n");
301 case STATE_CHANGE_NEXTI
:
302 if (!catch_errors ((catch_errors_ftype
*) ice_nexti
, (PTR
) (int) lParam
, "",
304 printf_unfiltered ("nexti errored\n");
311 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
316 /* Code for opening a connection to the ICE. */
319 v850ice_open (char *name
, int from_tty
)
324 error ("Too many arguments.");
326 target_preopen (from_tty
);
328 unpush_target (&v850ice_ops
);
331 puts_filtered ("V850ice debugging\n");
333 push_target (&v850ice_ops
); /* Switch to using v850ice target now */
335 target_terminal_init ();
337 /* Initialize everything necessary to facilitate communication
338 between QBox, gdb, and the DLLs which control the ICE */
339 if (ExeAppReq
== NULL
)
341 handle
= LoadLibrary ("necmsg.dll");
343 error ("Cannot load necmsg.dll");
345 ExeAppReq
= (long (*) (char *, long, char *, struct MessageIO
*))
346 GetProcAddress (handle
, "ExeAppReq");
347 RegisterClient
= (long (*) (HWND
))
348 GetProcAddress (handle
, "RegisterClient");
349 UnregisterClient
= (long (*) (void))
350 GetProcAddress (handle
, "UnregisterClient");
352 if (ExeAppReq
== NULL
|| RegisterClient
== NULL
|| UnregisterClient
== NULL
)
353 error ("Could not find requisite functions in necmsg.dll.");
355 if (!init_hidden_window ())
356 error ("could not initialize message handling");
359 /* Tell the DLL we are here */
360 RegisterClient (hidden_hwnd
);
364 /* Without this, some commands which require an active target (such as kill)
365 won't work. This variable serves (at least) double duty as both the pid
366 of the target process (if it has such), and as a flag indicating that a
367 target is active. These functions should be split out into seperate
368 variables, especially since GDB will someday have a notion of debugging
369 several processes. */
370 inferior_pid
= 42000;
376 /* Clean up connection to a remote debugger. */
380 v850ice_close (int quitting
)
390 /* Stop the process on the ice. */
394 /* This is silly, but it works... */
395 v850ice_command ("stop", 0);
399 v850ice_detach (char *args
, int from_tty
)
402 error ("Argument given to \"detach\" when remotely debugging.");
406 puts_filtered ("Ending v850ice debugging.\n");
409 /* Tell the remote machine to resume. */
412 v850ice_resume (int pid
, int step
, enum target_signal siggnal
)
416 struct MessageIO iob
;
422 retval
= ExeAppReq ("GDB", GSINGLESTEP
, "step", &iob
);
424 retval
= ExeAppReq ("GDB", GRESUME
, "run", &iob
);
427 error ("ExeAppReq (step = %d) returned %d", step
, retval
);
430 /* Wait until the remote machine stops, then return,
431 storing status in STATUS just as `wait' would.
432 Returns "pid" (though it's not clear what, if anything, that
433 means in the case of this target). */
436 v850ice_wait (int pid
, struct target_waitstatus
*status
)
440 struct MessageIO iob
;
449 if (count
++ % 100000)
455 v850_status
= ExeAppReq ("GDB", GCHECKSTATUS
, NULL
, &iob
);
463 status
->kind
= TARGET_WAITKIND_STOPPED
;
464 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
468 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
469 status
->value
.sig
= TARGET_SIGNAL_SEGV
;
473 status
->kind
= TARGET_WAITKIND_EXITED
;
474 status
->value
.integer
= 0;
478 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
479 status
->value
.sig
= TARGET_SIGNAL_KILL
;
492 convert_register (int regno
, char *buf
)
495 sprintf (buf
, "r%d", regno
);
496 else if (REGISTER_NAME (regno
)[0] == 's'
497 && REGISTER_NAME (regno
)[1] == 'r')
500 sprintf (buf
, "%s", REGISTER_NAME (regno
));
505 /* Read the remote registers into the block REGS. */
506 /* Note that the ICE returns register contents as ascii hex strings. We have
507 to convert that to an unsigned long, and then call store_unsigned_integer to
508 convert it to target byte-order if necessary. */
511 v850ice_fetch_registers (int regno
)
516 struct MessageIO iob
;
517 unsigned long regval
;
522 for (regno
= 0; regno
< NUM_REGS
; regno
++)
523 v850ice_fetch_registers (regno
);
527 strcpy (cmd
, "reg ");
528 if (!convert_register (regno
, &cmd
[4]))
531 iob
.size
= sizeof val
;
533 retval
= ExeAppReq ("GDB", GREADREG
, cmd
, &iob
);
535 error ("1: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
537 regval
= strtoul (val
, NULL
, 16);
538 if (regval
== 0 && p
== val
)
539 error ("v850ice_fetch_registers (%d): bad value from ICE: %s.",
542 store_unsigned_integer (val
, REGISTER_RAW_SIZE (regno
), regval
);
543 supply_register (regno
, val
);
546 /* Store register REGNO, or all registers if REGNO == -1, from the contents
550 v850ice_store_registers (int regno
)
554 unsigned long regval
;
556 struct MessageIO iob
;
562 for (regno
= 0; regno
< NUM_REGS
; regno
++)
563 v850ice_store_registers (regno
);
567 regval
= extract_unsigned_integer (®isters
[REGISTER_BYTE (regno
)],
568 REGISTER_RAW_SIZE (regno
));
569 strcpy (cmd
, "reg ");
570 if (!convert_register (regno
, &cmd
[4]))
572 sprintf (cmd
+ strlen (cmd
), "=0x%x", regval
);
574 retval
= ExeAppReq ("GDB", GWRITEREG
, cmd
, &iob
);
576 error ("2: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
579 /* Prepare to store registers. Nothing to do here, since the ICE can write one
580 register at a time. */
583 v850ice_prepare_to_store (void)
587 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
588 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
589 nonzero. TARGET is unused. Returns length of data written or read;
592 We can only read/write MAX_BLOCK_SIZE bytes at a time, though, or the DLL
596 v850ice_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
,
597 int should_write
, struct target_ops
*target
)
601 struct MessageIO iob
;
606 if (len
== 4 || len
== 2 || len
== 1)
620 value
|= (long) ((myaddr
[3] << 24) & 0xff000000);
621 value
|= (long) ((myaddr
[2] << 16) & 0x00ff0000);
622 value
|= (long) ((myaddr
[1] << 8) & 0x0000ff00);
623 value
|= (long) (myaddr
[0] & 0x000000ff);
627 value
|= (long) ((myaddr
[1] << 8) & 0xff00);
628 value
|= (long) (myaddr
[0] & 0x00ff);
632 value
|= (long) (myaddr
[0] & 0xff);
636 sprintf (cmd
, "memory %c c 0x%x=0x%x", c
, (int) memaddr
, value
);
637 retval
= ExeAppReq ("GDB", GWRITEMEM
, cmd
, &iob
);
646 iob
.size
= len
> MAX_BLOCK_SIZE
? MAX_BLOCK_SIZE
: len
;
648 sprintf (cmd
, "memory b c 0x%x=0x00 l=%d", (int) memaddr
, iob
.size
);
649 retval
= ExeAppReq ("GDB", GWRITEBLOCK
, cmd
, &iob
);
666 tmp
= alloca (len
+ 100);
668 memset (tmp
+ len
, 0xff, 100);
673 iob
.size
= len
> MAX_BLOCK_SIZE
? MAX_BLOCK_SIZE
: len
;
675 sprintf (cmd
, "memory b 0x%x l=%d", (int) memaddr
, iob
.size
);
676 retval
= ExeAppReq ("GDB", GREADBLOCK
, cmd
, &iob
);
688 for (i
= 0; i
< 100; i
++)
690 if (t
[sent
+ i
] != 0xff)
692 warning ("GREADBLOCK trashed bytes after transfer area.");
696 memcpy (myaddr
, t
, sent
);
701 error ("3: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
707 v850ice_files_info (struct target_ops
*ignore
)
709 puts_filtered ("Debugging a target via the NEC V850 ICE.\n");
713 v850ice_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
718 struct MessageIO iob
;
722 sprintf (cmd
, "%d, ", addr
);
724 retval
= ExeAppReq ("GDB", GSETBREAK
, cmd
, &iob
);
726 error ("ExeAppReq (GSETBREAK) returned %d: cmd = %s", retval
, cmd
);
732 v850ice_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
737 struct MessageIO iob
;
742 sprintf (cmd
, "%d, ", addr
);
744 retval
= ExeAppReq ("GDB", GREMOVEBREAK
, cmd
, &iob
);
746 error ("ExeAppReq (GREMOVEBREAK) returned %d: cmd = %s", retval
, cmd
);
754 target_mourn_inferior ();
764 v850ice_load (char *filename
, int from_tty
)
766 struct MessageIO iob
;
771 generic_load (filename
, from_tty
);
772 ExeAppReq ("GDB", GDOWNLOAD
, filename
, &iob
);
780 target_detach (NULL
, 0);
783 printf_unfiltered ("\n");
793 /* Safegaurd against confusing the breakpoint routines... */
794 delete_command (NULL
, 0);
796 /* Must supress from_tty, otherwise we could start asking if the
797 user really wants to load a new symbol table, etc... */
798 printf_unfiltered ("Reading symbols from %s...", arg
);
800 symbol_file_add_main (arg
, 0);
801 printf_unfiltered ("done\n");
803 /* exec_open will kill our target, so reinstall the ICE as
805 v850ice_open (NULL
, 0);
807 togdb_force_update ();
814 printf_filtered ("continue (ice)\n");
815 ReplyMessage ((LRESULT
) 1);
817 if (gdbtk_interp
== NULL
)
819 continue_command (NULL
, 1);
822 Tcl_Eval (gdbtk_interp
, "gdb_immediate continue");
828 do_gdb (char *cmd
, char *str
, void (*func
) (char *, int), int count
)
830 ReplyMessage ((LRESULT
) 1);
834 printf_unfiltered (str
);
836 if (gdbtk_interp
== NULL
)
841 Tcl_Eval (gdbtk_interp
, cmd
);
851 do_gdb ("gdb_immediate stepi", "stepi (ice)\n", stepi_command
, count
);
860 do_gdb ("gdb_immediate nexti", "nexti (ice)\n", nexti_command
, count
);
865 v850ice_command (char *arg
, int from_tty
)
867 struct MessageIO iob
;
872 ExeAppReq ("GDB", GCOMMAND
, arg
, &iob
);
876 togdb_force_update (void)
878 if (gdbtk_interp
!= NULL
)
879 Tcl_Eval (gdbtk_interp
, "gdbtk_update");
883 view_source (CORE_ADDR addr
)
887 if (gdbtk_interp
!= NULL
)
889 sprintf (c
, "catch {set src [lindex [ManagedWin::find SrcWin] 0]\n$src location BROWSE [gdb_loc *0x%x]}", addr
);
890 Tcl_Eval (gdbtk_interp
, c
);
894 /* Define the target subroutine names */
897 init_850ice_ops (void)
899 v850ice_ops
.to_shortname
= "ice";
900 v850ice_ops
.to_longname
= "NEC V850 ICE interface";
901 v850ice_ops
.to_doc
= "Debug a system controlled by a NEC 850 ICE.";
902 v850ice_ops
.to_open
= v850ice_open
;
903 v850ice_ops
.to_close
= v850ice_close
;
904 v850ice_ops
.to_attach
= NULL
;
905 v850ice_ops
.to_post_attach
= NULL
;
906 v850ice_ops
.to_require_attach
= NULL
;
907 v850ice_ops
.to_detach
= v850ice_detach
;
908 v850ice_ops
.to_require_detach
= NULL
;
909 v850ice_ops
.to_resume
= v850ice_resume
;
910 v850ice_ops
.to_wait
= v850ice_wait
;
911 v850ice_ops
.to_post_wait
= NULL
;
912 v850ice_ops
.to_fetch_registers
= v850ice_fetch_registers
;
913 v850ice_ops
.to_store_registers
= v850ice_store_registers
;
914 v850ice_ops
.to_prepare_to_store
= v850ice_prepare_to_store
;
915 v850ice_ops
.to_xfer_memory
= v850ice_xfer_memory
;
916 v850ice_ops
.to_files_info
= v850ice_files_info
;
917 v850ice_ops
.to_insert_breakpoint
= v850ice_insert_breakpoint
;
918 v850ice_ops
.to_remove_breakpoint
= v850ice_remove_breakpoint
;
919 v850ice_ops
.to_terminal_init
= NULL
;
920 v850ice_ops
.to_terminal_inferior
= NULL
;
921 v850ice_ops
.to_terminal_ours_for_output
= NULL
;
922 v850ice_ops
.to_terminal_ours
= NULL
;
923 v850ice_ops
.to_terminal_info
= NULL
;
924 v850ice_ops
.to_kill
= v850ice_kill
;
925 v850ice_ops
.to_load
= v850ice_load
;
926 v850ice_ops
.to_lookup_symbol
= NULL
;
927 v850ice_ops
.to_create_inferior
= NULL
;
928 v850ice_ops
.to_mourn_inferior
= v850ice_mourn
;
929 v850ice_ops
.to_can_run
= 0;
930 v850ice_ops
.to_notice_signals
= 0;
931 v850ice_ops
.to_thread_alive
= NULL
;
932 v850ice_ops
.to_stop
= v850ice_stop
;
933 v850ice_ops
.to_pid_to_exec_file
= NULL
;
934 v850ice_ops
.to_core_file_to_sym_file
= NULL
;
935 v850ice_ops
.to_stratum
= process_stratum
;
936 v850ice_ops
.DONT_USE
= NULL
;
937 v850ice_ops
.to_has_all_memory
= 1;
938 v850ice_ops
.to_has_memory
= 1;
939 v850ice_ops
.to_has_stack
= 1;
940 v850ice_ops
.to_has_registers
= 1;
941 v850ice_ops
.to_has_execution
= 1;
942 v850ice_ops
.to_sections
= NULL
;
943 v850ice_ops
.to_sections_end
= NULL
;
944 v850ice_ops
.to_magic
= OPS_MAGIC
;
948 _initialize_v850ice (void)
951 add_target (&v850ice_ops
);
953 add_com ("ice", class_obscure
, v850ice_command
,
954 "Send command to ICE");