| 1 | /* ICE interface for the NEC V850 for GDB, the GNU debugger. |
| 2 | Copyright 1996, 2000 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 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. |
| 10 | |
| 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. |
| 15 | |
| 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. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "gdb_string.h" |
| 23 | #include "frame.h" |
| 24 | #include "symtab.h" |
| 25 | #include "inferior.h" |
| 26 | #include "breakpoint.h" |
| 27 | #include "symfile.h" |
| 28 | #include "target.h" |
| 29 | #include "objfiles.h" |
| 30 | #include "gdbcore.h" |
| 31 | #include "value.h" |
| 32 | #include "command.h" |
| 33 | |
| 34 | #include <tcl.h> |
| 35 | #include <windows.h> |
| 36 | #include <winuser.h> /* for WM_USER */ |
| 37 | |
| 38 | extern unsigned long int strtoul (const char *nptr, char **endptr, |
| 39 | int base); |
| 40 | |
| 41 | /* Local data definitions */ |
| 42 | struct MessageIO |
| 43 | { |
| 44 | int size; /* length of input or output in bytes */ |
| 45 | char *buf; /* buffer having the input/output information */ |
| 46 | }; |
| 47 | |
| 48 | /* Prototypes for functions located in other files */ |
| 49 | extern void break_command (char *, int); |
| 50 | |
| 51 | extern void stepi_command (char *, int); |
| 52 | |
| 53 | extern void nexti_command (char *, int); |
| 54 | |
| 55 | extern void continue_command (char *, int); |
| 56 | |
| 57 | extern int (*ui_loop_hook) (int); |
| 58 | |
| 59 | /* Prototypes for local functions */ |
| 60 | static int init_hidden_window (void); |
| 61 | |
| 62 | static LRESULT CALLBACK v850ice_wndproc (HWND, UINT, WPARAM, LPARAM); |
| 63 | |
| 64 | static void v850ice_files_info (struct target_ops *ignore); |
| 65 | |
| 66 | static int v850ice_xfer_memory (CORE_ADDR memaddr, char *myaddr, |
| 67 | int len, int should_write, |
| 68 | struct target_ops *target); |
| 69 | |
| 70 | static void v850ice_prepare_to_store (void); |
| 71 | |
| 72 | static void v850ice_fetch_registers (int regno); |
| 73 | |
| 74 | static void v850ice_resume (int pid, int step, enum target_signal siggnal); |
| 75 | |
| 76 | static void v850ice_open (char *name, int from_tty); |
| 77 | |
| 78 | static void v850ice_close (int quitting); |
| 79 | |
| 80 | static void v850ice_stop (void); |
| 81 | |
| 82 | static void v850ice_store_registers (int regno); |
| 83 | |
| 84 | static void v850ice_mourn (void); |
| 85 | |
| 86 | static int v850ice_wait (int pid, struct target_waitstatus *status); |
| 87 | |
| 88 | static void v850ice_kill (void); |
| 89 | |
| 90 | static void v850ice_detach (char *args, int from_tty); |
| 91 | |
| 92 | static int v850ice_insert_breakpoint (CORE_ADDR, char *); |
| 93 | |
| 94 | static int v850ice_remove_breakpoint (CORE_ADDR, char *); |
| 95 | |
| 96 | static void v850ice_command (char *, int); |
| 97 | |
| 98 | static int ice_disassemble (unsigned long, int, char *); |
| 99 | |
| 100 | static int ice_lookup_addr (unsigned long *, char *, char *); |
| 101 | |
| 102 | static int ice_lookup_symbol (unsigned long, char *); |
| 103 | |
| 104 | static void ice_SimulateDisassemble (char *, int); |
| 105 | |
| 106 | static void ice_SimulateAddrLookup (char *, int); |
| 107 | |
| 108 | static void ice_Simulate_SymLookup (char *, int); |
| 109 | |
| 110 | static void ice_fputs (const char *, struct ui_file *); |
| 111 | |
| 112 | static int ice_file (char *); |
| 113 | |
| 114 | static int ice_cont (char *); |
| 115 | |
| 116 | static int ice_stepi (char *); |
| 117 | |
| 118 | static int ice_nexti (char *); |
| 119 | |
| 120 | static void togdb_force_update (void); |
| 121 | |
| 122 | static void view_source (CORE_ADDR); |
| 123 | |
| 124 | static void do_gdb (char *, char *, void (*func) (char *, int), int); |
| 125 | |
| 126 | |
| 127 | /* Globals */ |
| 128 | static HWND hidden_hwnd; /* HWND for messages */ |
| 129 | |
| 130 | long (__stdcall * ExeAppReq) (char *, long, char *, struct MessageIO *); |
| 131 | |
| 132 | long (__stdcall * RegisterClient) (HWND); |
| 133 | |
| 134 | long (__stdcall * UnregisterClient) (void); |
| 135 | |
| 136 | extern Tcl_Interp *gdbtk_interp; |
| 137 | |
| 138 | /* Globals local to this file only */ |
| 139 | static int ice_open = 0; /* Is ICE open? */ |
| 140 | |
| 141 | static char *v850_CB_Result; /* special char array for saving 'callback' results */ |
| 142 | |
| 143 | static int SimulateCallback; /* simulate a callback event */ |
| 144 | |
| 145 | #define MAX_BLOCK_SIZE 64*1024 /* Cannot transfer memory in blocks bigger |
| 146 | than this */ |
| 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 */ |
| 163 | |
| 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 |
| 174 | |
| 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 |
| 181 | |
| 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 */ |
| 190 | |
| 191 | static struct target_ops v850ice_ops; /* Forward decl */ |
| 192 | |
| 193 | /* This function creates a hidden window */ |
| 194 | static int |
| 195 | init_hidden_window (void) |
| 196 | { |
| 197 | WNDCLASS class; |
| 198 | |
| 199 | if (hidden_hwnd != NULL) |
| 200 | return 1; |
| 201 | |
| 202 | class.style = 0; |
| 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; |
| 210 | class.hIcon = NULL; |
| 211 | class.hCursor = NULL; |
| 212 | |
| 213 | if (!RegisterClass (&class)) |
| 214 | return 0; |
| 215 | |
| 216 | hidden_hwnd = CreateWindow ("gdb_v850ice", "gdb_v850ice", WS_TILED, |
| 217 | 0, 0, 0, 0, NULL, NULL, class.hInstance, |
| 218 | NULL); |
| 219 | if (hidden_hwnd == NULL) |
| 220 | { |
| 221 | char buf[200]; |
| 222 | DWORD err; |
| 223 | |
| 224 | err = GetLastError (); |
| 225 | FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, |
| 226 | 0, buf, 200, NULL); |
| 227 | printf_unfiltered ("Could not create window: %s", buf); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | /* |
| 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: |
| 238 | |
| 239 | WM_SYM_TO_ADDR \ |
| 240 | WM_ADDR_TO_SYM | Not implemented at NEC's request |
| 241 | WM_DISASSEMBLY / |
| 242 | WM_STATE_CHANGE - tells us that a state change has occured in the ICE |
| 243 | */ |
| 244 | static LRESULT CALLBACK |
| 245 | v850ice_wndproc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 246 | { |
| 247 | LRESULT result = FALSE; |
| 248 | |
| 249 | switch (message) |
| 250 | { |
| 251 | case WM_SYM_TO_ADDR: |
| 252 | MessageBox (0, "Symbol resolution\nNot implemented", "GDB", MB_OK); |
| 253 | break; |
| 254 | case WM_ADDR_TO_SYM: |
| 255 | MessageBox (0, "Address resolution\nNot implemented", "GDB", MB_OK); |
| 256 | break; |
| 257 | case WM_SOURCE: |
| 258 | view_source ((CORE_ADDR) lParam); |
| 259 | break; |
| 260 | case WM_STATE_CHANGE: |
| 261 | switch (wParam) |
| 262 | { |
| 263 | case STATE_CHANGE_LOAD: |
| 264 | { |
| 265 | struct MessageIO iob; |
| 266 | char buf[128]; |
| 267 | |
| 268 | iob.buf = buf; |
| 269 | iob.size = 128; |
| 270 | |
| 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"); |
| 275 | } |
| 276 | break; |
| 277 | case STATE_CHANGE_RESET: |
| 278 | registers_changed (); |
| 279 | flush_cached_frames (); |
| 280 | togdb_force_update (); |
| 281 | result = TRUE; |
| 282 | break; |
| 283 | case STATE_CHANGE_REGS: |
| 284 | registers_changed (); |
| 285 | togdb_force_update (); |
| 286 | result = TRUE; |
| 287 | break; |
| 288 | case STATE_CHANGE_CONT: |
| 289 | if (!catch_errors ((catch_errors_ftype *) ice_cont, NULL, "", RETURN_MASK_ALL)) |
| 290 | printf_unfiltered ("continue errored\n"); |
| 291 | result = TRUE; |
| 292 | break; |
| 293 | case STATE_CHANGE_STEPI: |
| 294 | if (!catch_errors ((catch_errors_ftype *) ice_stepi, (PTR) (int) lParam, "", |
| 295 | RETURN_MASK_ALL)) |
| 296 | printf_unfiltered ("stepi errored\n"); |
| 297 | result = TRUE; |
| 298 | break; |
| 299 | case STATE_CHANGE_NEXTI: |
| 300 | if (!catch_errors ((catch_errors_ftype *) ice_nexti, (PTR) (int) lParam, "", |
| 301 | RETURN_MASK_ALL)) |
| 302 | printf_unfiltered ("nexti errored\n"); |
| 303 | result = TRUE; |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | if (result == FALSE) |
| 309 | return DefWindowProc (hwnd, message, wParam, lParam); |
| 310 | |
| 311 | return FALSE; |
| 312 | } |
| 313 | |
| 314 | /* Code for opening a connection to the ICE. */ |
| 315 | |
| 316 | static void |
| 317 | v850ice_open (char *name, int from_tty) |
| 318 | { |
| 319 | HINSTANCE handle; |
| 320 | |
| 321 | if (name) |
| 322 | error ("Too many arguments."); |
| 323 | |
| 324 | target_preopen (from_tty); |
| 325 | |
| 326 | unpush_target (&v850ice_ops); |
| 327 | |
| 328 | if (from_tty) |
| 329 | puts_filtered ("V850ice debugging\n"); |
| 330 | |
| 331 | push_target (&v850ice_ops); /* Switch to using v850ice target now */ |
| 332 | |
| 333 | target_terminal_init (); |
| 334 | |
| 335 | /* Initialize everything necessary to facilitate communication |
| 336 | between QBox, gdb, and the DLLs which control the ICE */ |
| 337 | if (ExeAppReq == NULL) |
| 338 | { |
| 339 | handle = LoadLibrary ("necmsg.dll"); |
| 340 | if (handle == NULL) |
| 341 | error ("Cannot load necmsg.dll"); |
| 342 | |
| 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"); |
| 349 | |
| 350 | if (ExeAppReq == NULL || RegisterClient == NULL || UnregisterClient == NULL) |
| 351 | error ("Could not find requisite functions in necmsg.dll."); |
| 352 | |
| 353 | if (!init_hidden_window ()) |
| 354 | error ("could not initialize message handling"); |
| 355 | } |
| 356 | |
| 357 | /* Tell the DLL we are here */ |
| 358 | RegisterClient (hidden_hwnd); |
| 359 | |
| 360 | ice_open = 1; |
| 361 | |
| 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; |
| 369 | |
| 370 | start_remote (); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | /* Clean up connection to a remote debugger. */ |
| 375 | |
| 376 | /* ARGSUSED */ |
| 377 | static void |
| 378 | v850ice_close (int quitting) |
| 379 | { |
| 380 | if (ice_open) |
| 381 | { |
| 382 | UnregisterClient (); |
| 383 | ice_open = 0; |
| 384 | inferior_pid = 0; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* Stop the process on the ice. */ |
| 389 | static void |
| 390 | v850ice_stop (void) |
| 391 | { |
| 392 | /* This is silly, but it works... */ |
| 393 | v850ice_command ("stop", 0); |
| 394 | } |
| 395 | |
| 396 | static void |
| 397 | v850ice_detach (char *args, int from_tty) |
| 398 | { |
| 399 | if (args) |
| 400 | error ("Argument given to \"detach\" when remotely debugging."); |
| 401 | |
| 402 | pop_target (); |
| 403 | if (from_tty) |
| 404 | puts_filtered ("Ending v850ice debugging.\n"); |
| 405 | } |
| 406 | |
| 407 | /* Tell the remote machine to resume. */ |
| 408 | |
| 409 | static void |
| 410 | v850ice_resume (int pid, int step, enum target_signal siggnal) |
| 411 | { |
| 412 | long retval; |
| 413 | char buf[256]; |
| 414 | struct MessageIO iob; |
| 415 | |
| 416 | iob.size = 0; |
| 417 | iob.buf = buf; |
| 418 | |
| 419 | if (step) |
| 420 | retval = ExeAppReq ("GDB", GSINGLESTEP, "step", &iob); |
| 421 | else |
| 422 | retval = ExeAppReq ("GDB", GRESUME, "run", &iob); |
| 423 | |
| 424 | if (retval) |
| 425 | error ("ExeAppReq (step = %d) returned %d", step, retval); |
| 426 | } |
| 427 | |
| 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). */ |
| 432 | |
| 433 | static int |
| 434 | v850ice_wait (int pid, struct target_waitstatus *status) |
| 435 | { |
| 436 | long v850_status; |
| 437 | char buf[256]; |
| 438 | struct MessageIO iob; |
| 439 | int done = 0; |
| 440 | int count = 0; |
| 441 | |
| 442 | iob.size = 0; |
| 443 | iob.buf = buf; |
| 444 | |
| 445 | do |
| 446 | { |
| 447 | if (count++ % 100000) |
| 448 | { |
| 449 | ui_loop_hook (0); |
| 450 | count = 0; |
| 451 | } |
| 452 | |
| 453 | v850_status = ExeAppReq ("GDB", GCHECKSTATUS, NULL, &iob); |
| 454 | |
| 455 | switch (v850_status) |
| 456 | { |
| 457 | case ICE_Idle: |
| 458 | case ICE_Breakpoint: |
| 459 | case ICE_Stepped: |
| 460 | case ICE_Halted: |
| 461 | status->kind = TARGET_WAITKIND_STOPPED; |
| 462 | status->value.sig = TARGET_SIGNAL_TRAP; |
| 463 | done = 1; |
| 464 | break; |
| 465 | case ICE_Exception: |
| 466 | status->kind = TARGET_WAITKIND_SIGNALLED; |
| 467 | status->value.sig = TARGET_SIGNAL_SEGV; |
| 468 | done = 1; |
| 469 | break; |
| 470 | case ICE_Exited: |
| 471 | status->kind = TARGET_WAITKIND_EXITED; |
| 472 | status->value.integer = 0; |
| 473 | done = 1; |
| 474 | break; |
| 475 | case ICE_Terminated: |
| 476 | status->kind = TARGET_WAITKIND_SIGNALLED; |
| 477 | status->value.sig = TARGET_SIGNAL_KILL; |
| 478 | done = 1; |
| 479 | break; |
| 480 | default: |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | while (!done); |
| 485 | |
| 486 | return inferior_pid; |
| 487 | } |
| 488 | |
| 489 | static int |
| 490 | convert_register (int regno, char *buf) |
| 491 | { |
| 492 | if (regno <= 31) |
| 493 | sprintf (buf, "r%d", regno); |
| 494 | else if (REGISTER_NAME (regno)[0] == 's' |
| 495 | && REGISTER_NAME (regno)[1] == 'r') |
| 496 | return 0; |
| 497 | else |
| 498 | sprintf (buf, "%s", REGISTER_NAME (regno)); |
| 499 | |
| 500 | return 1; |
| 501 | } |
| 502 | |
| 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. */ |
| 507 | |
| 508 | static void |
| 509 | v850ice_fetch_registers (int regno) |
| 510 | { |
| 511 | long retval; |
| 512 | char cmd[100]; |
| 513 | char val[100]; |
| 514 | struct MessageIO iob; |
| 515 | unsigned long regval; |
| 516 | char *p; |
| 517 | |
| 518 | if (regno == -1) |
| 519 | { |
| 520 | for (regno = 0; regno < NUM_REGS; regno++) |
| 521 | v850ice_fetch_registers (regno); |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | strcpy (cmd, "reg "); |
| 526 | if (!convert_register (regno, &cmd[4])) |
| 527 | return; |
| 528 | |
| 529 | iob.size = sizeof val; |
| 530 | iob.buf = val; |
| 531 | retval = ExeAppReq ("GDB", GREADREG, cmd, &iob); |
| 532 | if (retval) |
| 533 | error ("1: ExeAppReq returned %d: cmd = %s", retval, cmd); |
| 534 | |
| 535 | regval = strtoul (val, NULL, 16); |
| 536 | if (regval == 0 && p == val) |
| 537 | error ("v850ice_fetch_registers (%d): bad value from ICE: %s.", |
| 538 | regno, val); |
| 539 | |
| 540 | store_unsigned_integer (val, REGISTER_RAW_SIZE (regno), regval); |
| 541 | supply_register (regno, val); |
| 542 | } |
| 543 | |
| 544 | /* Store register REGNO, or all registers if REGNO == -1, from the contents |
| 545 | of REGISTERS. */ |
| 546 | |
| 547 | static void |
| 548 | v850ice_store_registers (int regno) |
| 549 | { |
| 550 | long retval; |
| 551 | char cmd[100]; |
| 552 | unsigned long regval; |
| 553 | char buf[256]; |
| 554 | struct MessageIO iob; |
| 555 | iob.size = 0; |
| 556 | iob.buf = buf; |
| 557 | |
| 558 | if (regno == -1) |
| 559 | { |
| 560 | for (regno = 0; regno < NUM_REGS; regno++) |
| 561 | v850ice_store_registers (regno); |
| 562 | return; |
| 563 | } |
| 564 | |
| 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])) |
| 569 | return; |
| 570 | sprintf (cmd + strlen (cmd), "=0x%x", regval); |
| 571 | |
| 572 | retval = ExeAppReq ("GDB", GWRITEREG, cmd, &iob); |
| 573 | if (retval) |
| 574 | error ("2: ExeAppReq returned %d: cmd = %s", retval, cmd); |
| 575 | } |
| 576 | |
| 577 | /* Prepare to store registers. Nothing to do here, since the ICE can write one |
| 578 | register at a time. */ |
| 579 | |
| 580 | static void |
| 581 | v850ice_prepare_to_store (void) |
| 582 | { |
| 583 | } |
| 584 | |
| 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. Returns length of data written or read; 0 for error. |
| 588 | |
| 589 | We can only read/write MAX_BLOCK_SIZE bytes at a time, though, or the DLL |
| 590 | dies */ |
| 591 | /* ARGSUSED */ |
| 592 | static int |
| 593 | v850ice_xfer_memory (memaddr, myaddr, len, should_write, target) |
| 594 | CORE_ADDR memaddr; |
| 595 | char *myaddr; |
| 596 | int len; |
| 597 | int should_write; |
| 598 | struct target_ops *target; /* ignored */ |
| 599 | { |
| 600 | long retval; |
| 601 | char cmd[100]; |
| 602 | struct MessageIO iob; |
| 603 | int sent; |
| 604 | |
| 605 | if (should_write) |
| 606 | { |
| 607 | if (len == 4 || len == 2 || len == 1) |
| 608 | { |
| 609 | long value = 0; |
| 610 | char buf[256]; |
| 611 | char c; |
| 612 | |
| 613 | iob.size = 0; |
| 614 | iob.buf = buf; |
| 615 | |
| 616 | sent = 0; |
| 617 | switch (len) |
| 618 | { |
| 619 | case 4: |
| 620 | c = 'w'; |
| 621 | value |= (long) ((myaddr[3] << 24) & 0xff000000); |
| 622 | value |= (long) ((myaddr[2] << 16) & 0x00ff0000); |
| 623 | value |= (long) ((myaddr[1] << 8) & 0x0000ff00); |
| 624 | value |= (long) (myaddr[0] & 0x000000ff); |
| 625 | break; |
| 626 | case 2: |
| 627 | c = 'h'; |
| 628 | value |= (long) ((myaddr[1] << 8) & 0xff00); |
| 629 | value |= (long) (myaddr[0] & 0x00ff); |
| 630 | break; |
| 631 | case 1: |
| 632 | c = 'b'; |
| 633 | value |= (long) (myaddr[0] & 0xff); |
| 634 | break; |
| 635 | } |
| 636 | |
| 637 | sprintf (cmd, "memory %c c 0x%x=0x%x", c, (int) memaddr, value); |
| 638 | retval = ExeAppReq ("GDB", GWRITEMEM, cmd, &iob); |
| 639 | if (retval == 0) |
| 640 | sent = len; |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | sent = 0; |
| 645 | do |
| 646 | { |
| 647 | iob.size = len > MAX_BLOCK_SIZE ? MAX_BLOCK_SIZE : len; |
| 648 | iob.buf = myaddr; |
| 649 | sprintf (cmd, "memory b c 0x%x=0x00 l=%d", (int) memaddr, iob.size); |
| 650 | retval = ExeAppReq ("GDB", GWRITEBLOCK, cmd, &iob); |
| 651 | if (retval != 0) |
| 652 | break; |
| 653 | len -= iob.size; |
| 654 | memaddr += iob.size; |
| 655 | myaddr += iob.size; |
| 656 | sent += iob.size; |
| 657 | } |
| 658 | while (len > 0); |
| 659 | } |
| 660 | } |
| 661 | else |
| 662 | { |
| 663 | unsigned char *tmp; |
| 664 | unsigned char *t; |
| 665 | int i; |
| 666 | |
| 667 | tmp = alloca (len + 100); |
| 668 | t = tmp; |
| 669 | memset (tmp + len, 0xff, 100); |
| 670 | |
| 671 | sent = 0; |
| 672 | do |
| 673 | { |
| 674 | iob.size = len > MAX_BLOCK_SIZE ? MAX_BLOCK_SIZE : len; |
| 675 | iob.buf = tmp; |
| 676 | sprintf (cmd, "memory b 0x%x l=%d", (int) memaddr, iob.size); |
| 677 | retval = ExeAppReq ("GDB", GREADBLOCK, cmd, &iob); |
| 678 | if (retval != 0) |
| 679 | break; |
| 680 | len -= iob.size; |
| 681 | memaddr += iob.size; |
| 682 | sent += iob.size; |
| 683 | tmp += iob.size; |
| 684 | } |
| 685 | while (len > 0); |
| 686 | |
| 687 | if (retval == 0) |
| 688 | { |
| 689 | for (i = 0; i < 100; i++) |
| 690 | { |
| 691 | if (t[sent + i] != 0xff) |
| 692 | { |
| 693 | warning ("GREADBLOCK trashed bytes after transfer area."); |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | memcpy (myaddr, t, sent); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | if (retval != 0) |
| 702 | error ("3: ExeAppReq returned %d: cmd = %s", retval, cmd); |
| 703 | |
| 704 | return sent; |
| 705 | } |
| 706 | |
| 707 | static void |
| 708 | v850ice_files_info (struct target_ops *ignore) |
| 709 | { |
| 710 | puts_filtered ("Debugging a target via the NEC V850 ICE.\n"); |
| 711 | } |
| 712 | |
| 713 | static int |
| 714 | v850ice_insert_breakpoint (CORE_ADDR addr, char *contents_cache) |
| 715 | { |
| 716 | long retval; |
| 717 | char cmd[100]; |
| 718 | char buf[256]; |
| 719 | struct MessageIO iob; |
| 720 | |
| 721 | iob.size = 0; |
| 722 | iob.buf = buf; |
| 723 | sprintf (cmd, "%d, ", addr); |
| 724 | |
| 725 | retval = ExeAppReq ("GDB", GSETBREAK, cmd, &iob); |
| 726 | if (retval) |
| 727 | error ("ExeAppReq (GSETBREAK) returned %d: cmd = %s", retval, cmd); |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | v850ice_remove_breakpoint (CORE_ADDR addr, char *contents_cache) |
| 734 | { |
| 735 | long retval; |
| 736 | char cmd[100]; |
| 737 | char buf[256]; |
| 738 | struct MessageIO iob; |
| 739 | |
| 740 | iob.size = 0; |
| 741 | iob.buf = buf; |
| 742 | |
| 743 | sprintf (cmd, "%d, ", addr); |
| 744 | |
| 745 | retval = ExeAppReq ("GDB", GREMOVEBREAK, cmd, &iob); |
| 746 | if (retval) |
| 747 | error ("ExeAppReq (GREMOVEBREAK) returned %d: cmd = %s", retval, cmd); |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | static void |
| 753 | v850ice_kill (void) |
| 754 | { |
| 755 | target_mourn_inferior (); |
| 756 | inferior_pid = 0; |
| 757 | } |
| 758 | |
| 759 | static void |
| 760 | v850ice_mourn (void) |
| 761 | { |
| 762 | } |
| 763 | |
| 764 | static void |
| 765 | v850ice_load (char *filename, int from_tty) |
| 766 | { |
| 767 | struct MessageIO iob; |
| 768 | char buf[256]; |
| 769 | |
| 770 | iob.size = 0; |
| 771 | iob.buf = buf; |
| 772 | generic_load (filename, from_tty); |
| 773 | ExeAppReq ("GDB", GDOWNLOAD, filename, &iob); |
| 774 | } |
| 775 | |
| 776 | static int |
| 777 | ice_file (char *arg) |
| 778 | { |
| 779 | char *s; |
| 780 | |
| 781 | target_detach (NULL, 0); |
| 782 | pop_target (); |
| 783 | |
| 784 | printf_unfiltered ("\n"); |
| 785 | |
| 786 | s = arg; |
| 787 | while (*s != '\0') |
| 788 | { |
| 789 | if (*s == '\\') |
| 790 | *s = '/'; |
| 791 | s++; |
| 792 | } |
| 793 | |
| 794 | /* Safegaurd against confusing the breakpoint routines... */ |
| 795 | delete_command (NULL, 0); |
| 796 | |
| 797 | /* Must supress from_tty, otherwise we could start asking if the |
| 798 | user really wants to load a new symbol table, etc... */ |
| 799 | printf_unfiltered ("Reading symbols from %s...", arg); |
| 800 | exec_file_command (arg, 0); |
| 801 | symbol_file_command (arg, 0); |
| 802 | printf_unfiltered ("done\n"); |
| 803 | |
| 804 | /* exec_file_command will kill our target, so reinstall the ICE as |
| 805 | the target. */ |
| 806 | v850ice_open (NULL, 0); |
| 807 | |
| 808 | togdb_force_update (); |
| 809 | return 1; |
| 810 | } |
| 811 | |
| 812 | static int |
| 813 | ice_cont (char *c) |
| 814 | { |
| 815 | printf_filtered ("continue (ice)\n"); |
| 816 | ReplyMessage ((LRESULT) 1); |
| 817 | |
| 818 | if (gdbtk_interp == NULL) |
| 819 | { |
| 820 | continue_command (NULL, 1); |
| 821 | } |
| 822 | else |
| 823 | Tcl_Eval (gdbtk_interp, "gdb_immediate continue"); |
| 824 | |
| 825 | return 1; |
| 826 | } |
| 827 | |
| 828 | static void |
| 829 | do_gdb (cmd, str, func, count) |
| 830 | char *cmd; |
| 831 | char *str; |
| 832 | void (*func) (char *, int); |
| 833 | int count; |
| 834 | { |
| 835 | ReplyMessage ((LRESULT) 1); |
| 836 | |
| 837 | while (count--) |
| 838 | { |
| 839 | printf_unfiltered (str); |
| 840 | |
| 841 | if (gdbtk_interp == NULL) |
| 842 | { |
| 843 | func (NULL, 0); |
| 844 | } |
| 845 | else |
| 846 | Tcl_Eval (gdbtk_interp, cmd); |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | |
| 851 | static int |
| 852 | ice_stepi (char *c) |
| 853 | { |
| 854 | int count = (int) c; |
| 855 | |
| 856 | do_gdb ("gdb_immediate stepi", "stepi (ice)\n", stepi_command, count); |
| 857 | return 1; |
| 858 | } |
| 859 | |
| 860 | static int |
| 861 | ice_nexti (char *c) |
| 862 | { |
| 863 | int count = (int) c; |
| 864 | |
| 865 | do_gdb ("gdb_immediate nexti", "nexti (ice)\n", nexti_command, count); |
| 866 | return 1; |
| 867 | } |
| 868 | |
| 869 | static void |
| 870 | v850ice_command (char *arg, int from_tty) |
| 871 | { |
| 872 | struct MessageIO iob; |
| 873 | char buf[256]; |
| 874 | |
| 875 | iob.buf = buf; |
| 876 | iob.size = 0; |
| 877 | ExeAppReq ("GDB", GCOMMAND, arg, &iob); |
| 878 | } |
| 879 | |
| 880 | static void |
| 881 | togdb_force_update (void) |
| 882 | { |
| 883 | if (gdbtk_interp != NULL) |
| 884 | Tcl_Eval (gdbtk_interp, "gdbtk_update"); |
| 885 | } |
| 886 | |
| 887 | static void |
| 888 | view_source (CORE_ADDR addr) |
| 889 | { |
| 890 | char c[256]; |
| 891 | |
| 892 | if (gdbtk_interp != NULL) |
| 893 | { |
| 894 | sprintf (c, "catch {set src [lindex [ManagedWin::find SrcWin] 0]\n$src location BROWSE [gdb_loc *0x%x]}", addr); |
| 895 | Tcl_Eval (gdbtk_interp, c); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /* Define the target subroutine names */ |
| 900 | |
| 901 | static void |
| 902 | init_850ice_ops (void) |
| 903 | { |
| 904 | v850ice_ops.to_shortname = "ice"; |
| 905 | v850ice_ops.to_longname = "NEC V850 ICE interface"; |
| 906 | v850ice_ops.to_doc = "Debug a system controlled by a NEC 850 ICE."; |
| 907 | v850ice_ops.to_open = v850ice_open; |
| 908 | v850ice_ops.to_close = v850ice_close; |
| 909 | v850ice_ops.to_attach = NULL; |
| 910 | v850ice_ops.to_post_attach = NULL; |
| 911 | v850ice_ops.to_require_attach = NULL; |
| 912 | v850ice_ops.to_detach = v850ice_detach; |
| 913 | v850ice_ops.to_require_detach = NULL; |
| 914 | v850ice_ops.to_resume = v850ice_resume; |
| 915 | v850ice_ops.to_wait = v850ice_wait; |
| 916 | v850ice_ops.to_post_wait = NULL; |
| 917 | v850ice_ops.to_fetch_registers = v850ice_fetch_registers; |
| 918 | v850ice_ops.to_store_registers = v850ice_store_registers; |
| 919 | v850ice_ops.to_prepare_to_store = v850ice_prepare_to_store; |
| 920 | v850ice_ops.to_xfer_memory = v850ice_xfer_memory; |
| 921 | v850ice_ops.to_files_info = v850ice_files_info; |
| 922 | v850ice_ops.to_insert_breakpoint = v850ice_insert_breakpoint; |
| 923 | v850ice_ops.to_remove_breakpoint = v850ice_remove_breakpoint; |
| 924 | v850ice_ops.to_terminal_init = NULL; |
| 925 | v850ice_ops.to_terminal_inferior = NULL; |
| 926 | v850ice_ops.to_terminal_ours_for_output = NULL; |
| 927 | v850ice_ops.to_terminal_ours = NULL; |
| 928 | v850ice_ops.to_terminal_info = NULL; |
| 929 | v850ice_ops.to_kill = v850ice_kill; |
| 930 | v850ice_ops.to_load = v850ice_load; |
| 931 | v850ice_ops.to_lookup_symbol = NULL; |
| 932 | v850ice_ops.to_create_inferior = NULL; |
| 933 | v850ice_ops.to_mourn_inferior = v850ice_mourn; |
| 934 | v850ice_ops.to_can_run = 0; |
| 935 | v850ice_ops.to_notice_signals = 0; |
| 936 | v850ice_ops.to_thread_alive = NULL; |
| 937 | v850ice_ops.to_stop = v850ice_stop; |
| 938 | v850ice_ops.to_pid_to_exec_file = NULL; |
| 939 | v850ice_ops.to_core_file_to_sym_file = NULL; |
| 940 | v850ice_ops.to_stratum = process_stratum; |
| 941 | v850ice_ops.DONT_USE = NULL; |
| 942 | v850ice_ops.to_has_all_memory = 1; |
| 943 | v850ice_ops.to_has_memory = 1; |
| 944 | v850ice_ops.to_has_stack = 1; |
| 945 | v850ice_ops.to_has_registers = 1; |
| 946 | v850ice_ops.to_has_execution = 1; |
| 947 | v850ice_ops.to_sections = NULL; |
| 948 | v850ice_ops.to_sections_end = NULL; |
| 949 | v850ice_ops.to_magic = OPS_MAGIC; |
| 950 | } |
| 951 | |
| 952 | void |
| 953 | _initialize_v850ice (void) |
| 954 | { |
| 955 | init_850ice_ops (); |
| 956 | add_target (&v850ice_ops); |
| 957 | |
| 958 | add_com ("ice", class_obscure, v850ice_command, |
| 959 | "Send command to ICE"); |
| 960 | } |