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 (char *, int);
51 extern void stepi_command (char *, int);
53 extern void nexti_command (char *, int);
55 extern void continue_command (char *, int);
57 extern int (*ui_loop_hook
) (int);
59 /* Prototypes for local functions */
60 static int init_hidden_window (void);
62 static LRESULT CALLBACK
v850ice_wndproc (HWND
, UINT
, WPARAM
, LPARAM
);
64 static void v850ice_files_info (struct target_ops
*ignore
);
66 static int v850ice_xfer_memory (CORE_ADDR memaddr
, char *myaddr
,
67 int len
, int should_write
,
68 struct target_ops
*target
);
70 static void v850ice_prepare_to_store (void);
72 static void v850ice_fetch_registers (int regno
);
74 static void v850ice_resume (int pid
, int step
, enum target_signal siggnal
);
76 static void v850ice_open (char *name
, int from_tty
);
78 static void v850ice_close (int quitting
);
80 static void v850ice_stop (void);
82 static void v850ice_store_registers (int regno
);
84 static void v850ice_mourn (void);
86 static int v850ice_wait (int pid
, struct target_waitstatus
*status
);
88 static void v850ice_kill (void);
90 static void v850ice_detach (char *args
, int from_tty
);
92 static int v850ice_insert_breakpoint (CORE_ADDR
, char *);
94 static int v850ice_remove_breakpoint (CORE_ADDR
, char *);
96 static void v850ice_command (char *, int);
98 static int ice_disassemble (unsigned long, int, char *);
100 static int ice_lookup_addr (unsigned long *, char *, char *);
102 static int ice_lookup_symbol (unsigned long, char *);
104 static void ice_SimulateDisassemble (char *, int);
106 static void ice_SimulateAddrLookup (char *, int);
108 static void ice_Simulate_SymLookup (char *, int);
110 static void ice_fputs (const char *, struct ui_file
*);
112 static int ice_file (char *);
114 static int ice_cont (char *);
116 static int ice_stepi (char *);
118 static int ice_nexti (char *);
120 static void togdb_force_update (void);
122 static void view_source (CORE_ADDR
);
124 static void do_gdb (char *, char *, void (*func
) (char *, int), int);
128 static HWND hidden_hwnd
; /* HWND for messages */
130 long (__stdcall
* ExeAppReq
) (char *, long, char *, struct MessageIO
*);
132 long (__stdcall
* RegisterClient
) (HWND
);
134 long (__stdcall
* UnregisterClient
) (void);
136 extern Tcl_Interp
*gdbtk_interp
;
138 /* Globals local to this file only */
139 static int ice_open
= 0; /* Is ICE open? */
141 static char *v850_CB_Result
; /* special char array for saving 'callback' results */
143 static int SimulateCallback
; /* simulate a callback event */
145 #define MAX_BLOCK_SIZE 64*1024 /* Cannot transfer memory in blocks bigger
147 /* MDI/ICE Message IDs */
148 #define GSINGLESTEP 0x200 /* single-step target */
149 #define GRESUME 0x201 /* resume target */
150 #define GREADREG 0x202 /* read a register */
151 #define GWRITEREG 0x203 /* write a register */
152 #define GWRITEBLOCK 0x204 /* write a block of memory */
153 #define GREADBLOCK 0x205 /* read a block of memory */
154 #define GSETBREAK 0x206 /* set a breakpoint */
155 #define GREMOVEBREAK 0x207 /* remove a breakpoint */
156 #define GHALT 0x208 /* ??? */
157 #define GCHECKSTATUS 0x209 /* check status of ICE */
158 #define GMDIREPLY 0x210 /* Reply for previous query - NOT USED */
159 #define GDOWNLOAD 0x211 /* something for MDI */
160 #define GCOMMAND 0x212 /* execute command in ice */
161 #define GLOADFILENAME 0x213 /* retrieve load filename */
162 #define GWRITEMEM 0x214 /* write word, half-word, or byte */
164 /* GCHECKSTATUS return codes: */
165 #define ICE_Idle 0x00
166 #define ICE_Breakpoint 0x01 /* hit a breakpoint */
167 #define ICE_Stepped 0x02 /* have stepped */
168 #define ICE_Exception 0x03 /* have exception */
169 #define ICE_Halted 0x04 /* hit a user halt */
170 #define ICE_Exited 0x05 /* called exit */
171 #define ICE_Terminated 0x06 /* user terminated */
172 #define ICE_Running 0x07
173 #define ICE_Unknown 0x99
175 /* Windows messages */
176 #define WM_STATE_CHANGE WM_USER+101
177 #define WM_SYM_TO_ADDR WM_USER+102
178 #define WM_ADDR_TO_SYM WM_USER+103
179 #define WM_DISASSEMBLY WM_USER+104
180 #define WM_SOURCE WM_USER+105
182 /* STATE_CHANGE codes */
183 #define STATE_CHANGE_REGS 1 /* Register(s) changed */
184 #define STATE_CHANGE_LOAD 2 /* HW reset */
185 #define STATE_CHANGE_RESET 3 /* Load new file */
186 #define STATE_CHANGE_CONT 4 /* Run target */
187 #define STATE_CHANGE_STOP 5 /* Stop target */
188 #define STATE_CHANGE_STEPI 6 /* Stepi target */
189 #define STATE_CHANGE_NEXTI 7 /* Nexti target */
191 static struct target_ops v850ice_ops
; /* Forward decl */
193 /* This function creates a hidden window */
195 init_hidden_window (void)
199 if (hidden_hwnd
!= NULL
)
203 class.cbClsExtra
= 0;
204 class.cbWndExtra
= 0;
205 class.hInstance
= GetModuleHandle (0);
206 class.hbrBackground
= NULL
;
207 class.lpszMenuName
= NULL
;
208 class.lpszClassName
= "gdb_v850ice";
209 class.lpfnWndProc
= v850ice_wndproc
;
211 class.hCursor
= NULL
;
213 if (!RegisterClass (&class))
216 hidden_hwnd
= CreateWindow ("gdb_v850ice", "gdb_v850ice", WS_TILED
,
217 0, 0, 0, 0, NULL
, NULL
, class.hInstance
,
219 if (hidden_hwnd
== NULL
)
224 err
= GetLastError ();
225 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, err
,
227 printf_unfiltered ("Could not create window: %s", buf
);
235 This function is installed as the message handler for the hidden window
236 which QBox will use to communicate with gdb. It recognize and acts
237 on the following messages:
240 WM_ADDR_TO_SYM | Not implemented at NEC's request
242 WM_STATE_CHANGE - tells us that a state change has occured in the ICE
244 static LRESULT CALLBACK
245 v850ice_wndproc (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
247 LRESULT result
= FALSE
;
252 MessageBox (0, "Symbol resolution\nNot implemented", "GDB", MB_OK
);
255 MessageBox (0, "Address resolution\nNot implemented", "GDB", MB_OK
);
258 view_source ((CORE_ADDR
) lParam
);
260 case WM_STATE_CHANGE
:
263 case STATE_CHANGE_LOAD
:
265 struct MessageIO iob
;
271 /* Load in a new file... Need filename */
272 ExeAppReq ("GDB", GLOADFILENAME
, NULL
, &iob
);
273 if (!catch_errors ((catch_errors_ftype
*) ice_file
, iob
.buf
, "", RETURN_MASK_ALL
))
274 printf_unfiltered ("load errored\n");
277 case STATE_CHANGE_RESET
:
278 registers_changed ();
279 flush_cached_frames ();
280 togdb_force_update ();
283 case STATE_CHANGE_REGS
:
284 registers_changed ();
285 togdb_force_update ();
288 case STATE_CHANGE_CONT
:
289 if (!catch_errors ((catch_errors_ftype
*) ice_cont
, NULL
, "", RETURN_MASK_ALL
))
290 printf_unfiltered ("continue errored\n");
293 case STATE_CHANGE_STEPI
:
294 if (!catch_errors ((catch_errors_ftype
*) ice_stepi
, (PTR
) (int) lParam
, "",
296 printf_unfiltered ("stepi errored\n");
299 case STATE_CHANGE_NEXTI
:
300 if (!catch_errors ((catch_errors_ftype
*) ice_nexti
, (PTR
) (int) lParam
, "",
302 printf_unfiltered ("nexti errored\n");
309 return DefWindowProc (hwnd
, message
, wParam
, lParam
);
314 /* Code for opening a connection to the ICE. */
317 v850ice_open (char *name
, int from_tty
)
322 error ("Too many arguments.");
324 target_preopen (from_tty
);
326 unpush_target (&v850ice_ops
);
329 puts_filtered ("V850ice debugging\n");
331 push_target (&v850ice_ops
); /* Switch to using v850ice target now */
333 target_terminal_init ();
335 /* Initialize everything necessary to facilitate communication
336 between QBox, gdb, and the DLLs which control the ICE */
337 if (ExeAppReq
== NULL
)
339 handle
= LoadLibrary ("necmsg.dll");
341 error ("Cannot load necmsg.dll");
343 ExeAppReq
= (long (*) (char *, long, char *, struct MessageIO
*))
344 GetProcAddress (handle
, "ExeAppReq");
345 RegisterClient
= (long (*) (HWND
))
346 GetProcAddress (handle
, "RegisterClient");
347 UnregisterClient
= (long (*) (void))
348 GetProcAddress (handle
, "UnregisterClient");
350 if (ExeAppReq
== NULL
|| RegisterClient
== NULL
|| UnregisterClient
== NULL
)
351 error ("Could not find requisite functions in necmsg.dll.");
353 if (!init_hidden_window ())
354 error ("could not initialize message handling");
357 /* Tell the DLL we are here */
358 RegisterClient (hidden_hwnd
);
362 /* Without this, some commands which require an active target (such as kill)
363 won't work. This variable serves (at least) double duty as both the pid
364 of the target process (if it has such), and as a flag indicating that a
365 target is active. These functions should be split out into seperate
366 variables, especially since GDB will someday have a notion of debugging
367 several processes. */
368 inferior_pid
= 42000;
374 /* Clean up connection to a remote debugger. */
378 v850ice_close (int quitting
)
388 /* Stop the process on the ice. */
392 /* This is silly, but it works... */
393 v850ice_command ("stop", 0);
397 v850ice_detach (char *args
, int from_tty
)
400 error ("Argument given to \"detach\" when remotely debugging.");
404 puts_filtered ("Ending v850ice debugging.\n");
407 /* Tell the remote machine to resume. */
410 v850ice_resume (int pid
, int step
, enum target_signal siggnal
)
414 struct MessageIO iob
;
420 retval
= ExeAppReq ("GDB", GSINGLESTEP
, "step", &iob
);
422 retval
= ExeAppReq ("GDB", GRESUME
, "run", &iob
);
425 error ("ExeAppReq (step = %d) returned %d", step
, retval
);
428 /* Wait until the remote machine stops, then return,
429 storing status in STATUS just as `wait' would.
430 Returns "pid" (though it's not clear what, if anything, that
431 means in the case of this target). */
434 v850ice_wait (int pid
, struct target_waitstatus
*status
)
438 struct MessageIO iob
;
447 if (count
++ % 100000)
453 v850_status
= ExeAppReq ("GDB", GCHECKSTATUS
, NULL
, &iob
);
461 status
->kind
= TARGET_WAITKIND_STOPPED
;
462 status
->value
.sig
= TARGET_SIGNAL_TRAP
;
466 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
467 status
->value
.sig
= TARGET_SIGNAL_SEGV
;
471 status
->kind
= TARGET_WAITKIND_EXITED
;
472 status
->value
.integer
= 0;
476 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
477 status
->value
.sig
= TARGET_SIGNAL_KILL
;
490 convert_register (int regno
, char *buf
)
493 sprintf (buf
, "r%d", regno
);
494 else if (REGISTER_NAME (regno
)[0] == 's'
495 && REGISTER_NAME (regno
)[1] == 'r')
498 sprintf (buf
, "%s", REGISTER_NAME (regno
));
503 /* Read the remote registers into the block REGS. */
504 /* Note that the ICE returns register contents as ascii hex strings. We have
505 to convert that to an unsigned long, and then call store_unsigned_integer to
506 convert it to target byte-order if necessary. */
509 v850ice_fetch_registers (int 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 (int regno
)
552 unsigned long regval
;
554 struct MessageIO iob
;
560 for (regno
= 0; regno
< NUM_REGS
; regno
++)
561 v850ice_store_registers (regno
);
565 regval
= extract_unsigned_integer (®isters
[REGISTER_BYTE (regno
)],
566 REGISTER_RAW_SIZE (regno
));
567 strcpy (cmd
, "reg ");
568 if (!convert_register (regno
, &cmd
[4]))
570 sprintf (cmd
+ strlen (cmd
), "=0x%x", regval
);
572 retval
= ExeAppReq ("GDB", GWRITEREG
, cmd
, &iob
);
574 error ("2: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
577 /* Prepare to store registers. Nothing to do here, since the ICE can write one
578 register at a time. */
581 v850ice_prepare_to_store (void)
585 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
586 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
587 nonzero. TARGET is unused. Returns length of data written or read;
590 We can only read/write MAX_BLOCK_SIZE bytes at a time, though, or the DLL
594 v850ice_xfer_memory (CORE_ADDR memaddr
, char *myaddr
, int len
,
595 int should_write
, struct target_ops
*target
)
599 struct MessageIO iob
;
604 if (len
== 4 || len
== 2 || len
== 1)
618 value
|= (long) ((myaddr
[3] << 24) & 0xff000000);
619 value
|= (long) ((myaddr
[2] << 16) & 0x00ff0000);
620 value
|= (long) ((myaddr
[1] << 8) & 0x0000ff00);
621 value
|= (long) (myaddr
[0] & 0x000000ff);
625 value
|= (long) ((myaddr
[1] << 8) & 0xff00);
626 value
|= (long) (myaddr
[0] & 0x00ff);
630 value
|= (long) (myaddr
[0] & 0xff);
634 sprintf (cmd
, "memory %c c 0x%x=0x%x", c
, (int) memaddr
, value
);
635 retval
= ExeAppReq ("GDB", GWRITEMEM
, cmd
, &iob
);
644 iob
.size
= len
> MAX_BLOCK_SIZE
? MAX_BLOCK_SIZE
: len
;
646 sprintf (cmd
, "memory b c 0x%x=0x00 l=%d", (int) memaddr
, iob
.size
);
647 retval
= ExeAppReq ("GDB", GWRITEBLOCK
, cmd
, &iob
);
664 tmp
= alloca (len
+ 100);
666 memset (tmp
+ len
, 0xff, 100);
671 iob
.size
= len
> MAX_BLOCK_SIZE
? MAX_BLOCK_SIZE
: len
;
673 sprintf (cmd
, "memory b 0x%x l=%d", (int) memaddr
, iob
.size
);
674 retval
= ExeAppReq ("GDB", GREADBLOCK
, cmd
, &iob
);
686 for (i
= 0; i
< 100; i
++)
688 if (t
[sent
+ i
] != 0xff)
690 warning ("GREADBLOCK trashed bytes after transfer area.");
694 memcpy (myaddr
, t
, sent
);
699 error ("3: ExeAppReq returned %d: cmd = %s", retval
, cmd
);
705 v850ice_files_info (struct target_ops
*ignore
)
707 puts_filtered ("Debugging a target via the NEC V850 ICE.\n");
711 v850ice_insert_breakpoint (CORE_ADDR addr
, char *contents_cache
)
716 struct MessageIO iob
;
720 sprintf (cmd
, "%d, ", addr
);
722 retval
= ExeAppReq ("GDB", GSETBREAK
, cmd
, &iob
);
724 error ("ExeAppReq (GSETBREAK) returned %d: cmd = %s", retval
, cmd
);
730 v850ice_remove_breakpoint (CORE_ADDR addr
, char *contents_cache
)
735 struct MessageIO iob
;
740 sprintf (cmd
, "%d, ", addr
);
742 retval
= ExeAppReq ("GDB", GREMOVEBREAK
, cmd
, &iob
);
744 error ("ExeAppReq (GREMOVEBREAK) returned %d: cmd = %s", retval
, cmd
);
752 target_mourn_inferior ();
762 v850ice_load (char *filename
, int from_tty
)
764 struct MessageIO iob
;
769 generic_load (filename
, from_tty
);
770 ExeAppReq ("GDB", GDOWNLOAD
, filename
, &iob
);
778 target_detach (NULL
, 0);
781 printf_unfiltered ("\n");
791 /* Safegaurd against confusing the breakpoint routines... */
792 delete_command (NULL
, 0);
794 /* Must supress from_tty, otherwise we could start asking if the
795 user really wants to load a new symbol table, etc... */
796 printf_unfiltered ("Reading symbols from %s...", arg
);
797 exec_file_command (arg
, 0);
798 symbol_file_command (arg
, 0);
799 printf_unfiltered ("done\n");
801 /* exec_file_command will kill our target, so reinstall the ICE as
803 v850ice_open (NULL
, 0);
805 togdb_force_update ();
812 printf_filtered ("continue (ice)\n");
813 ReplyMessage ((LRESULT
) 1);
815 if (gdbtk_interp
== NULL
)
817 continue_command (NULL
, 1);
820 Tcl_Eval (gdbtk_interp
, "gdb_immediate continue");
826 do_gdb (char *cmd
, char *str
, void (*func
) (char *, int), int count
)
828 ReplyMessage ((LRESULT
) 1);
832 printf_unfiltered (str
);
834 if (gdbtk_interp
== NULL
)
839 Tcl_Eval (gdbtk_interp
, cmd
);
849 do_gdb ("gdb_immediate stepi", "stepi (ice)\n", stepi_command
, count
);
858 do_gdb ("gdb_immediate nexti", "nexti (ice)\n", nexti_command
, count
);
863 v850ice_command (char *arg
, int from_tty
)
865 struct MessageIO iob
;
870 ExeAppReq ("GDB", GCOMMAND
, arg
, &iob
);
874 togdb_force_update (void)
876 if (gdbtk_interp
!= NULL
)
877 Tcl_Eval (gdbtk_interp
, "gdbtk_update");
881 view_source (CORE_ADDR addr
)
885 if (gdbtk_interp
!= NULL
)
887 sprintf (c
, "catch {set src [lindex [ManagedWin::find SrcWin] 0]\n$src location BROWSE [gdb_loc *0x%x]}", addr
);
888 Tcl_Eval (gdbtk_interp
, c
);
892 /* Define the target subroutine names */
895 init_850ice_ops (void)
897 v850ice_ops
.to_shortname
= "ice";
898 v850ice_ops
.to_longname
= "NEC V850 ICE interface";
899 v850ice_ops
.to_doc
= "Debug a system controlled by a NEC 850 ICE.";
900 v850ice_ops
.to_open
= v850ice_open
;
901 v850ice_ops
.to_close
= v850ice_close
;
902 v850ice_ops
.to_attach
= NULL
;
903 v850ice_ops
.to_post_attach
= NULL
;
904 v850ice_ops
.to_require_attach
= NULL
;
905 v850ice_ops
.to_detach
= v850ice_detach
;
906 v850ice_ops
.to_require_detach
= NULL
;
907 v850ice_ops
.to_resume
= v850ice_resume
;
908 v850ice_ops
.to_wait
= v850ice_wait
;
909 v850ice_ops
.to_post_wait
= NULL
;
910 v850ice_ops
.to_fetch_registers
= v850ice_fetch_registers
;
911 v850ice_ops
.to_store_registers
= v850ice_store_registers
;
912 v850ice_ops
.to_prepare_to_store
= v850ice_prepare_to_store
;
913 v850ice_ops
.to_xfer_memory
= v850ice_xfer_memory
;
914 v850ice_ops
.to_files_info
= v850ice_files_info
;
915 v850ice_ops
.to_insert_breakpoint
= v850ice_insert_breakpoint
;
916 v850ice_ops
.to_remove_breakpoint
= v850ice_remove_breakpoint
;
917 v850ice_ops
.to_terminal_init
= NULL
;
918 v850ice_ops
.to_terminal_inferior
= NULL
;
919 v850ice_ops
.to_terminal_ours_for_output
= NULL
;
920 v850ice_ops
.to_terminal_ours
= NULL
;
921 v850ice_ops
.to_terminal_info
= NULL
;
922 v850ice_ops
.to_kill
= v850ice_kill
;
923 v850ice_ops
.to_load
= v850ice_load
;
924 v850ice_ops
.to_lookup_symbol
= NULL
;
925 v850ice_ops
.to_create_inferior
= NULL
;
926 v850ice_ops
.to_mourn_inferior
= v850ice_mourn
;
927 v850ice_ops
.to_can_run
= 0;
928 v850ice_ops
.to_notice_signals
= 0;
929 v850ice_ops
.to_thread_alive
= NULL
;
930 v850ice_ops
.to_stop
= v850ice_stop
;
931 v850ice_ops
.to_pid_to_exec_file
= NULL
;
932 v850ice_ops
.to_core_file_to_sym_file
= NULL
;
933 v850ice_ops
.to_stratum
= process_stratum
;
934 v850ice_ops
.DONT_USE
= NULL
;
935 v850ice_ops
.to_has_all_memory
= 1;
936 v850ice_ops
.to_has_memory
= 1;
937 v850ice_ops
.to_has_stack
= 1;
938 v850ice_ops
.to_has_registers
= 1;
939 v850ice_ops
.to_has_execution
= 1;
940 v850ice_ops
.to_sections
= NULL
;
941 v850ice_ops
.to_sections_end
= NULL
;
942 v850ice_ops
.to_magic
= OPS_MAGIC
;
946 _initialize_v850ice (void)
949 add_target (&v850ice_ops
);
951 add_com ("ice", class_obscure
, v850ice_command
,
952 "Send command to ICE");