| 1 | /* Target-vector operations for controlling Windows CE child processes, for GDB. |
| 2 | Copyright 1999, 2000, 2001 Free Software Foundation, Inc. |
| 3 | Contributed by Cygnus Solutions, A Red Hat Company. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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. |
| 21 | */ |
| 22 | |
| 23 | /* by Christopher Faylor (cgf@cygnus.com) */ |
| 24 | |
| 25 | /* We assume we're being built with and will be used for cygwin. */ |
| 26 | |
| 27 | #ifdef SHx |
| 28 | #undef SH4 |
| 29 | #define SH4 /* Just to get all of the CONTEXT defines. */ |
| 30 | #endif |
| 31 | |
| 32 | #include "defs.h" |
| 33 | #include "frame.h" /* required by inferior.h */ |
| 34 | #include "inferior.h" |
| 35 | #include "target.h" |
| 36 | #include "gdbcore.h" |
| 37 | #include "command.h" |
| 38 | #include <signal.h> |
| 39 | #include <sys/types.h> |
| 40 | #include <fcntl.h> |
| 41 | #include <stdlib.h> |
| 42 | |
| 43 | #include <windows.h> |
| 44 | #include <rapi.h> |
| 45 | #include <netdb.h> |
| 46 | #include <cygwin/in.h> |
| 47 | #include <cygwin/socket.h> |
| 48 | |
| 49 | #include "buildsym.h" |
| 50 | #include "symfile.h" |
| 51 | #include "objfiles.h" |
| 52 | #include "gdb_string.h" |
| 53 | #include "gdbthread.h" |
| 54 | #include "gdbcmd.h" |
| 55 | #include <sys/param.h> |
| 56 | #include "wince-stub.h" |
| 57 | #include <time.h> |
| 58 | |
| 59 | /* The ui's event loop. */ |
| 60 | extern int (*ui_loop_hook) (int signo); |
| 61 | |
| 62 | /* If we're not using the old Cygwin header file set, define the |
| 63 | following which never should have been in the generic Win32 API |
| 64 | headers in the first place since they were our own invention... */ |
| 65 | #ifndef _GNU_H_WINDOWS_H |
| 66 | #define FLAG_TRACE_BIT 0x100 |
| 67 | #ifdef CONTEXT_FLOATING_POINT |
| 68 | #define CONTEXT_DEBUGGER0 (CONTEXT_FULL | CONTEXT_FLOATING_POINT) |
| 69 | #else |
| 70 | #define CONTEXT_DEBUGGER0 (CONTEXT_FULL) |
| 71 | #endif |
| 72 | #endif |
| 73 | |
| 74 | #ifdef SH4 |
| 75 | #define CONTEXT_DEBUGGER ((CONTEXT_DEBUGGER0 & ~(CONTEXT_SH4 | CONTEXT_FLOATING_POINT)) | CONTEXT_SH3) |
| 76 | #else |
| 77 | #define CONTEXT_DEBUGGER CONTEXT_DEBUGGER0 |
| 78 | #endif |
| 79 | /* The string sent by cygwin when it processes a signal. |
| 80 | FIXME: This should be in a cygwin include file. */ |
| 81 | #define CYGWIN_SIGNAL_STRING "cygwin: signal" |
| 82 | |
| 83 | #define CHECK(x) check (x, __FILE__,__LINE__) |
| 84 | #define DEBUG_EXEC(x) if (debug_exec) printf x |
| 85 | #define DEBUG_EVENTS(x) if (debug_events) printf x |
| 86 | #define DEBUG_MEM(x) if (debug_memory) printf x |
| 87 | #define DEBUG_EXCEPT(x) if (debug_exceptions) printf x |
| 88 | |
| 89 | static int connection_initialized = 0; /* True if we've initialized a RAPI session. */ |
| 90 | |
| 91 | /* The directory where the stub and executable files are uploaded. */ |
| 92 | static const char *remote_directory = "\\gdb"; |
| 93 | |
| 94 | /* The types automatic upload available. */ |
| 95 | static enum |
| 96 | { |
| 97 | UPLOAD_ALWAYS = 0, |
| 98 | UPLOAD_NEWER = 1, |
| 99 | UPLOAD_NEVER = 2 |
| 100 | } |
| 101 | upload_when = UPLOAD_NEWER; |
| 102 | |
| 103 | /* Valid options for 'set remoteupload'. Note that options |
| 104 | must track upload_when enum. */ |
| 105 | static struct opts |
| 106 | { |
| 107 | const char *name; |
| 108 | int abbrev; |
| 109 | } |
| 110 | upload_options[3] = |
| 111 | { |
| 112 | { |
| 113 | "always", 1 |
| 114 | } |
| 115 | , |
| 116 | { |
| 117 | "newer", 3 |
| 118 | } |
| 119 | , |
| 120 | { |
| 121 | "never", 3 |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | static char *remote_upload = NULL; /* Set by set remoteupload */ |
| 126 | static int remote_add_host = 0; |
| 127 | |
| 128 | /* Forward declaration */ |
| 129 | extern struct target_ops child_ops; |
| 130 | |
| 131 | static int win32_child_thread_alive (int); |
| 132 | void child_kill_inferior (void); |
| 133 | |
| 134 | static int last_sig = 0; /* Set if a signal was received from the |
| 135 | debugged process */ |
| 136 | |
| 137 | /* Thread information structure used to track information that is |
| 138 | not available in gdb's thread structure. */ |
| 139 | typedef struct thread_info_struct |
| 140 | { |
| 141 | struct thread_info_struct *next; |
| 142 | DWORD id; |
| 143 | HANDLE h; |
| 144 | char *name; |
| 145 | int suspend_count; |
| 146 | int stepped; /* True if stepped. */ |
| 147 | CORE_ADDR step_pc; |
| 148 | unsigned long step_prev; |
| 149 | CONTEXT context; |
| 150 | } |
| 151 | thread_info; |
| 152 | |
| 153 | static thread_info thread_head = |
| 154 | {NULL}; |
| 155 | static thread_info * thread_rec (DWORD id, int get_context); |
| 156 | |
| 157 | /* The process and thread handles for the above context. */ |
| 158 | |
| 159 | static DEBUG_EVENT current_event; /* The current debug event from |
| 160 | WaitForDebugEvent */ |
| 161 | static HANDLE current_process_handle; /* Currently executing process */ |
| 162 | static thread_info *current_thread; /* Info on currently selected thread */ |
| 163 | static thread_info *this_thread; /* Info on thread returned by wait_for_debug_event */ |
| 164 | static DWORD main_thread_id; /* Thread ID of the main thread */ |
| 165 | |
| 166 | /* Counts of things. */ |
| 167 | static int exception_count = 0; |
| 168 | static int event_count = 0; |
| 169 | |
| 170 | /* User options. */ |
| 171 | static int debug_exec = 0; /* show execution */ |
| 172 | static int debug_events = 0; /* show events from kernel */ |
| 173 | static int debug_memory = 0; /* show target memory accesses */ |
| 174 | static int debug_exceptions = 0; /* show target exceptions */ |
| 175 | |
| 176 | /* An array of offset mappings into a Win32 Context structure. |
| 177 | This is a one-to-one mapping which is indexed by gdb's register |
| 178 | numbers. It retrieves an offset into the context structure where |
| 179 | the 4 byte register is located. |
| 180 | An offset value of -1 indicates that Win32 does not provide this |
| 181 | register in it's CONTEXT structure. regptr will return zero for this |
| 182 | register. |
| 183 | |
| 184 | This is used by the regptr function. */ |
| 185 | #define context_offset(x) ((int)&(((PCONTEXT)NULL)->x)) |
| 186 | static const int mappings[NUM_REGS + 1] = |
| 187 | { |
| 188 | #ifdef __i386__ |
| 189 | context_offset (Eax), |
| 190 | context_offset (Ecx), |
| 191 | context_offset (Edx), |
| 192 | context_offset (Ebx), |
| 193 | context_offset (Esp), |
| 194 | context_offset (Ebp), |
| 195 | context_offset (Esi), |
| 196 | context_offset (Edi), |
| 197 | context_offset (Eip), |
| 198 | context_offset (EFlags), |
| 199 | context_offset (SegCs), |
| 200 | context_offset (SegSs), |
| 201 | context_offset (SegDs), |
| 202 | context_offset (SegEs), |
| 203 | context_offset (SegFs), |
| 204 | context_offset (SegGs), |
| 205 | context_offset (FloatSave.RegisterArea[0 * 10]), |
| 206 | context_offset (FloatSave.RegisterArea[1 * 10]), |
| 207 | context_offset (FloatSave.RegisterArea[2 * 10]), |
| 208 | context_offset (FloatSave.RegisterArea[3 * 10]), |
| 209 | context_offset (FloatSave.RegisterArea[4 * 10]), |
| 210 | context_offset (FloatSave.RegisterArea[5 * 10]), |
| 211 | context_offset (FloatSave.RegisterArea[6 * 10]), |
| 212 | context_offset (FloatSave.RegisterArea[7 * 10]), |
| 213 | #elif defined(SHx) |
| 214 | context_offset (R0), |
| 215 | context_offset (R1), |
| 216 | context_offset (R2), |
| 217 | context_offset (R3), |
| 218 | context_offset (R4), |
| 219 | context_offset (R5), |
| 220 | context_offset (R6), |
| 221 | context_offset (R7), |
| 222 | context_offset (R8), |
| 223 | context_offset (R9), |
| 224 | context_offset (R10), |
| 225 | context_offset (R11), |
| 226 | context_offset (R12), |
| 227 | context_offset (R13), |
| 228 | context_offset (R14), |
| 229 | context_offset (R15), |
| 230 | context_offset (Fir), |
| 231 | context_offset (PR), /* Procedure Register */ |
| 232 | context_offset (GBR), /* Global Base Register */ |
| 233 | context_offset (MACH), /* Accumulate */ |
| 234 | context_offset (MACL), /* Multiply */ |
| 235 | context_offset (Psr), |
| 236 | context_offset (Fpul), |
| 237 | context_offset (Fpscr), |
| 238 | context_offset (FRegs[0]), |
| 239 | context_offset (FRegs[1]), |
| 240 | context_offset (FRegs[2]), |
| 241 | context_offset (FRegs[3]), |
| 242 | context_offset (FRegs[4]), |
| 243 | context_offset (FRegs[5]), |
| 244 | context_offset (FRegs[6]), |
| 245 | context_offset (FRegs[7]), |
| 246 | context_offset (FRegs[8]), |
| 247 | context_offset (FRegs[9]), |
| 248 | context_offset (FRegs[10]), |
| 249 | context_offset (FRegs[11]), |
| 250 | context_offset (FRegs[12]), |
| 251 | context_offset (FRegs[13]), |
| 252 | context_offset (FRegs[14]), |
| 253 | context_offset (FRegs[15]), |
| 254 | context_offset (xFRegs[0]), |
| 255 | context_offset (xFRegs[1]), |
| 256 | context_offset (xFRegs[2]), |
| 257 | context_offset (xFRegs[3]), |
| 258 | context_offset (xFRegs[4]), |
| 259 | context_offset (xFRegs[5]), |
| 260 | context_offset (xFRegs[6]), |
| 261 | context_offset (xFRegs[7]), |
| 262 | context_offset (xFRegs[8]), |
| 263 | context_offset (xFRegs[9]), |
| 264 | context_offset (xFRegs[10]), |
| 265 | context_offset (xFRegs[11]), |
| 266 | context_offset (xFRegs[12]), |
| 267 | context_offset (xFRegs[13]), |
| 268 | context_offset (xFRegs[14]), |
| 269 | context_offset (xFRegs[15]), |
| 270 | #elif defined(MIPS) |
| 271 | context_offset (IntZero), |
| 272 | context_offset (IntAt), |
| 273 | context_offset (IntV0), |
| 274 | context_offset (IntV1), |
| 275 | context_offset (IntA0), |
| 276 | context_offset (IntA1), |
| 277 | context_offset (IntA2), |
| 278 | context_offset (IntA3), |
| 279 | context_offset (IntT0), |
| 280 | context_offset (IntT1), |
| 281 | context_offset (IntT2), |
| 282 | context_offset (IntT3), |
| 283 | context_offset (IntT4), |
| 284 | context_offset (IntT5), |
| 285 | context_offset (IntT6), |
| 286 | context_offset (IntT7), |
| 287 | context_offset (IntS0), |
| 288 | context_offset (IntS1), |
| 289 | context_offset (IntS2), |
| 290 | context_offset (IntS3), |
| 291 | context_offset (IntS4), |
| 292 | context_offset (IntS5), |
| 293 | context_offset (IntS6), |
| 294 | context_offset (IntS7), |
| 295 | context_offset (IntT8), |
| 296 | context_offset (IntT9), |
| 297 | context_offset (IntK0), |
| 298 | context_offset (IntK1), |
| 299 | context_offset (IntGp), |
| 300 | context_offset (IntSp), |
| 301 | context_offset (IntS8), |
| 302 | context_offset (IntRa), |
| 303 | context_offset (Psr), |
| 304 | context_offset (IntLo), |
| 305 | context_offset (IntHi), |
| 306 | -1, /* bad */ |
| 307 | -1, /* cause */ |
| 308 | context_offset (Fir), |
| 309 | context_offset (FltF0), |
| 310 | context_offset (FltF1), |
| 311 | context_offset (FltF2), |
| 312 | context_offset (FltF3), |
| 313 | context_offset (FltF4), |
| 314 | context_offset (FltF5), |
| 315 | context_offset (FltF6), |
| 316 | context_offset (FltF7), |
| 317 | context_offset (FltF8), |
| 318 | context_offset (FltF9), |
| 319 | context_offset (FltF10), |
| 320 | context_offset (FltF11), |
| 321 | context_offset (FltF12), |
| 322 | context_offset (FltF13), |
| 323 | context_offset (FltF14), |
| 324 | context_offset (FltF15), |
| 325 | context_offset (FltF16), |
| 326 | context_offset (FltF17), |
| 327 | context_offset (FltF18), |
| 328 | context_offset (FltF19), |
| 329 | context_offset (FltF20), |
| 330 | context_offset (FltF21), |
| 331 | context_offset (FltF22), |
| 332 | context_offset (FltF23), |
| 333 | context_offset (FltF24), |
| 334 | context_offset (FltF25), |
| 335 | context_offset (FltF26), |
| 336 | context_offset (FltF27), |
| 337 | context_offset (FltF28), |
| 338 | context_offset (FltF29), |
| 339 | context_offset (FltF30), |
| 340 | context_offset (FltF31), |
| 341 | context_offset (Fsr), |
| 342 | context_offset (Fir), |
| 343 | -1, /* fp */ |
| 344 | #elif defined(ARM) |
| 345 | context_offset (R0), |
| 346 | context_offset (R1), |
| 347 | context_offset (R2), |
| 348 | context_offset (R3), |
| 349 | context_offset (R4), |
| 350 | context_offset (R5), |
| 351 | context_offset (R6), |
| 352 | context_offset (R7), |
| 353 | context_offset (R8), |
| 354 | context_offset (R9), |
| 355 | context_offset (R10), |
| 356 | context_offset (R11), |
| 357 | context_offset (R12), |
| 358 | context_offset (Sp), |
| 359 | context_offset (Lr), |
| 360 | context_offset (Pc), |
| 361 | -1, |
| 362 | -1, |
| 363 | -1, |
| 364 | -1, |
| 365 | -1, |
| 366 | -1, |
| 367 | -1, |
| 368 | -1, |
| 369 | -1, |
| 370 | context_offset (Psr), |
| 371 | #endif |
| 372 | -1 |
| 373 | }; |
| 374 | |
| 375 | /* Return a pointer into a CONTEXT field indexed by gdb register number. |
| 376 | Return a pointer to an address pointing to zero if there is no |
| 377 | corresponding CONTEXT field for the given register number. |
| 378 | */ |
| 379 | static ULONG * |
| 380 | regptr (LPCONTEXT c, int r) |
| 381 | { |
| 382 | static ULONG zero = 0; |
| 383 | ULONG *p; |
| 384 | if (mappings[r] < 0) |
| 385 | p = &zero; |
| 386 | else |
| 387 | p = (ULONG *) (((char *) c) + mappings[r]); |
| 388 | return p; |
| 389 | } |
| 390 | |
| 391 | /******************** Beginning of stub interface ********************/ |
| 392 | |
| 393 | /* Stub interface description: |
| 394 | |
| 395 | The Windows CE stub implements a crude RPC. The hand-held device |
| 396 | connects to gdb using port 7000. gdb and the stub then communicate |
| 397 | using packets where: |
| 398 | |
| 399 | byte 0: command id (e.g. Create Process) |
| 400 | |
| 401 | byte 1-4: DWORD |
| 402 | |
| 403 | byte 1-2: WORD |
| 404 | |
| 405 | byte 1-2: length |
| 406 | byte 3-n: arbitrary memory. |
| 407 | |
| 408 | The interface is deterministic, i.e., if the stub expects a DWORD then |
| 409 | the gdb server should send a DWORD. |
| 410 | */ |
| 411 | |
| 412 | /* Note: In the functions below, the `huh' parameter is a string passed from the |
| 413 | function containing a descriptive string concerning the current operation. |
| 414 | This is used for error reporting. |
| 415 | |
| 416 | The 'what' parameter is a command id as found in wince-stub.h. |
| 417 | |
| 418 | Hopefully, the rest of the parameters are self-explanatory. |
| 419 | */ |
| 420 | |
| 421 | static int s; /* communication socket */ |
| 422 | |
| 423 | /* v-style interface for handling varying argyment list error messages. |
| 424 | Displays the error message in a dialog box and exits when user clicks |
| 425 | on OK. */ |
| 426 | static void |
| 427 | vstub_error (LPCSTR fmt, va_list * args) |
| 428 | { |
| 429 | char buf[4096]; |
| 430 | vsprintf (buf, fmt, args); |
| 431 | s = -1; |
| 432 | error ("%s", buf); |
| 433 | } |
| 434 | |
| 435 | /* The standard way to display an error message and exit. */ |
| 436 | static void |
| 437 | stub_error (LPCSTR fmt,...) |
| 438 | { |
| 439 | va_list args; |
| 440 | va_start (args, fmt); |
| 441 | vstub_error (fmt, args); |
| 442 | } |
| 443 | |
| 444 | /* Standard "oh well" can't communicate error. Someday this might attempt |
| 445 | synchronization. */ |
| 446 | static void |
| 447 | attempt_resync (LPCSTR huh, int s) |
| 448 | { |
| 449 | stub_error ("lost synchronization with target attempting %s", huh); |
| 450 | } |
| 451 | |
| 452 | /* Read arbitrary stuff from a socket. */ |
| 453 | static int |
| 454 | sockread (LPCSTR huh, int s, void *str, size_t n) |
| 455 | { |
| 456 | for (;;) |
| 457 | { |
| 458 | if (recv (s, str, n, 0) == n) |
| 459 | return n; |
| 460 | attempt_resync (huh, s); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* Write arbitrary stuff to a socket. */ |
| 465 | static int |
| 466 | sockwrite (LPCSTR huh, const void *str, size_t n) |
| 467 | { |
| 468 | for (;;) |
| 469 | { |
| 470 | if (send (s, str, n, 0) == n) |
| 471 | return n; |
| 472 | attempt_resync (huh, s); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /* Output an id/dword to the host */ |
| 477 | static void |
| 478 | putdword (LPCSTR huh, gdb_wince_id what, DWORD n) |
| 479 | { |
| 480 | if (sockwrite (huh, &what, sizeof (what)) != sizeof (what)) |
| 481 | stub_error ("error writing record id to host for %s", huh); |
| 482 | if (sockwrite (huh, &n, sizeof (n)) != sizeof (n)) |
| 483 | stub_error ("error writing %s to host.", huh); |
| 484 | } |
| 485 | |
| 486 | /* Output an id/word to the host */ |
| 487 | static void |
| 488 | putword (LPCSTR huh, gdb_wince_id what, WORD n) |
| 489 | { |
| 490 | if (sockwrite (huh, &what, sizeof (what)) != sizeof (what)) |
| 491 | stub_error ("error writing record id to host for %s", huh); |
| 492 | if (sockwrite (huh, &n, sizeof (n)) != sizeof (n)) |
| 493 | stub_error ("error writing %s host.", huh); |
| 494 | } |
| 495 | |
| 496 | /* Convenience define for outputting a "gdb_wince_len" type. */ |
| 497 | #define putlen(huh, what, n) putword((huh), (what), (gdb_wince_len) (n)) |
| 498 | |
| 499 | /* Put an arbitrary block of memory to the gdb host. This comes in |
| 500 | two chunks an id/dword representing the length and the stream of memory |
| 501 | itself. */ |
| 502 | static void |
| 503 | putmemory (LPCSTR huh, gdb_wince_id what, const void *mem, gdb_wince_len len) |
| 504 | { |
| 505 | putlen (huh, what, len); |
| 506 | if (((short) len > 0) && sockwrite (huh, mem, len) != len) |
| 507 | stub_error ("error writing %s to host.", huh); |
| 508 | } |
| 509 | |
| 510 | /* Output the result of an operation to the host. If res != 0, sends a block of |
| 511 | memory starting at mem of len bytes. If res == 0, sends -GetLastError () and |
| 512 | avoids sending the mem. */ |
| 513 | static DWORD |
| 514 | getdword (LPCSTR huh, gdb_wince_id what_this) |
| 515 | { |
| 516 | DWORD n; |
| 517 | gdb_wince_id what; |
| 518 | do |
| 519 | if (sockread (huh, s, &what, sizeof (what)) != sizeof (what)) |
| 520 | stub_error ("error getting record type from host - %s.", huh); |
| 521 | while (what_this != what); |
| 522 | |
| 523 | if (sockread (huh, s, &n, sizeof (n)) != sizeof (n)) |
| 524 | stub_error ("error getting %s from host.", huh); |
| 525 | |
| 526 | return n; |
| 527 | } |
| 528 | |
| 529 | /* Get a an ID (possibly) and a WORD from the host gdb. |
| 530 | Don't bother with the id if the main loop has already |
| 531 | read it. */ |
| 532 | static WORD |
| 533 | getword (LPCSTR huh, gdb_wince_id what_this) |
| 534 | { |
| 535 | WORD n; |
| 536 | gdb_wince_id what; |
| 537 | do |
| 538 | if (sockread (huh, s, &what, sizeof (what)) != sizeof (what)) |
| 539 | stub_error ("error getting record type from host - %s.", huh); |
| 540 | while (what_this != what); |
| 541 | |
| 542 | if (sockread (huh, s, &n, sizeof (n)) != sizeof (n)) |
| 543 | stub_error ("error getting %s from host.", huh); |
| 544 | |
| 545 | return n; |
| 546 | } |
| 547 | |
| 548 | /* Handy defines for getting/putting various types of values. */ |
| 549 | #define gethandle(huh, what) (HANDLE) getdword ((huh), (what)) |
| 550 | #define getpvoid(huh, what) (LPVOID) getdword ((huh), (what)) |
| 551 | #define getlen(huh, what) (gdb_wince_len) getword ((huh), (what)) |
| 552 | #define puthandle(huh, what, h) putdword ((huh), (what), (DWORD) (h)) |
| 553 | #define putpvoid(huh, what, p) putdword ((huh), (what), (DWORD) (p)) |
| 554 | |
| 555 | /* Retrieve the result of an operation from the stub. If nbytes < 0) then nbytes |
| 556 | is actually an error and nothing else follows. Use SetLastError to remember this. |
| 557 | if nbytes > 0, retrieve a block of *nbytes into buf. |
| 558 | */ |
| 559 | int |
| 560 | getresult (LPCSTR huh, gdb_wince_id what, LPVOID buf, gdb_wince_len * nbytes) |
| 561 | { |
| 562 | gdb_wince_len dummy; |
| 563 | if (nbytes == NULL) |
| 564 | nbytes = &dummy; |
| 565 | |
| 566 | *nbytes = getlen (huh, what); |
| 567 | |
| 568 | if ((short) *nbytes < 0) |
| 569 | { |
| 570 | SetLastError (-(short) *nbytes); |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | if ((gdb_wince_len) sockread (huh, s, buf, *nbytes) != *nbytes) |
| 575 | stub_error ("couldn't read information from wince stub - %s", huh); |
| 576 | |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | /* Convert "narrow" string to "wide". Manipulates a buffer ring of 8 |
| 581 | buffers which hold the translated string. This is an arbitrary limit |
| 582 | but it is approximately double the current needs of this module. |
| 583 | */ |
| 584 | LPWSTR |
| 585 | towide (const char *s, gdb_wince_len * out_len) |
| 586 | { |
| 587 | static int n = -1; |
| 588 | static LPWSTR outs[8] = |
| 589 | {NULL /*, NULL, etc. */ }; |
| 590 | gdb_wince_len dummy; |
| 591 | |
| 592 | if (!out_len) |
| 593 | out_len = &dummy; |
| 594 | |
| 595 | /* First determine the length required to hold the converted string. */ |
| 596 | *out_len = sizeof (WCHAR) * MultiByteToWideChar (CP_ACP, 0, s, -1, NULL, 0); |
| 597 | if (!*out_len) |
| 598 | return NULL; /* The conversion failed */ |
| 599 | |
| 600 | if (++n >= (sizeof (outs) / sizeof (outs[0]))) |
| 601 | n = 0; /* wrap */ |
| 602 | |
| 603 | /* Allocate space for the converted string, reusing any previously allocated |
| 604 | space, if applicable. Note that if outs[n] is NULL, realloc will act as |
| 605 | a malloc (under cygwin, at least). |
| 606 | */ |
| 607 | outs[n] = (LPWSTR) realloc (outs[n], *out_len); |
| 608 | memset (outs[n], 0, *out_len); |
| 609 | (void) MultiByteToWideChar (CP_ACP, 0, s, -1, outs[n], *out_len); |
| 610 | return outs[n]; |
| 611 | } |
| 612 | |
| 613 | /******************** Emulation routines start here. ******************** |
| 614 | |
| 615 | The functions below are modelled after their Win32 counterparts. They are named |
| 616 | similarly to Win32 and take exactly the same arguments except where otherwise noted. |
| 617 | They communicate with the stub on the hand-held device by sending their arguments |
| 618 | over the socket and waiting for results from the socket. |
| 619 | |
| 620 | There is one universal change. In cases where a length is expected to be returned |
| 621 | in a DWORD, we use a gdb_wince_len type instead. Currently this is an unsigned short |
| 622 | which is smaller than the standard Win32 DWORD. This is done to minimize unnecessary |
| 623 | traffic since the connection to Windows CE can be slow. To change this, modify the |
| 624 | typedef in wince-stub.h and change the putlen/getlen macros in this file and in |
| 625 | the stub. |
| 626 | */ |
| 627 | |
| 628 | static int |
| 629 | create_process (LPSTR exec_file, LPSTR args, DWORD flags, PROCESS_INFORMATION * pi) |
| 630 | { |
| 631 | gdb_wince_len len; |
| 632 | LPWSTR buf; |
| 633 | |
| 634 | buf = towide (exec_file, &len); |
| 635 | putmemory ("CreateProcess exec_file", GDB_CREATEPROCESS, buf, len); |
| 636 | buf = towide (args, &len); |
| 637 | putmemory ("CreateProcess args", GDB_CREATEPROCESS, buf, len); |
| 638 | putdword ("CreateProcess flags", GDB_CREATEPROCESS, flags); |
| 639 | return getresult ("CreateProcess result", GDB_CREATEPROCESS, pi, NULL); |
| 640 | } |
| 641 | |
| 642 | /* Emulate TerminateProcess. Don't bother with the second argument since CE |
| 643 | ignores it. |
| 644 | */ |
| 645 | static int |
| 646 | terminate_process (HANDLE h) |
| 647 | { |
| 648 | gdb_wince_result res; |
| 649 | if (s < 0) |
| 650 | return 1; |
| 651 | puthandle ("TerminateProcess handle", GDB_TERMINATEPROCESS, h); |
| 652 | return getresult ("TerminateProcess result", GDB_TERMINATEPROCESS, &res, NULL); |
| 653 | } |
| 654 | |
| 655 | static int |
| 656 | wait_for_debug_event (DEBUG_EVENT * ev, DWORD ms) |
| 657 | { |
| 658 | if (s < 0) |
| 659 | return 1; |
| 660 | putdword ("WaitForDebugEvent ms", GDB_WAITFORDEBUGEVENT, ms); |
| 661 | return getresult ("WaitForDebugEvent event", GDB_WAITFORDEBUGEVENT, ev, NULL); |
| 662 | } |
| 663 | |
| 664 | static int |
| 665 | get_thread_context (HANDLE h, CONTEXT * c) |
| 666 | { |
| 667 | if (s < 0) |
| 668 | return 1; |
| 669 | puthandle ("GetThreadContext handle", GDB_GETTHREADCONTEXT, h); |
| 670 | putdword ("GetThreadContext flags", GDB_GETTHREADCONTEXT, c->ContextFlags); |
| 671 | return getresult ("GetThreadContext context", GDB_GETTHREADCONTEXT, c, NULL); |
| 672 | } |
| 673 | |
| 674 | static int |
| 675 | set_thread_context (HANDLE h, CONTEXT * c) |
| 676 | { |
| 677 | gdb_wince_result res; |
| 678 | if (s < 0) |
| 679 | return 1; |
| 680 | puthandle ("SetThreadContext handle", GDB_SETTHREADCONTEXT, h); |
| 681 | putmemory ("SetThreadContext context", GDB_SETTHREADCONTEXT, c, sizeof (*c)); |
| 682 | return getresult ("SetThreadContext context", GDB_SETTHREADCONTEXT, &res, NULL); |
| 683 | } |
| 684 | |
| 685 | static int |
| 686 | read_process_memory (HANDLE h, LPCVOID where, LPVOID buf, gdb_wince_len len, gdb_wince_len * nbytes) |
| 687 | { |
| 688 | if (s < 0) |
| 689 | return 1; |
| 690 | puthandle ("ReadProcessMemory handle", GDB_READPROCESSMEMORY, h); |
| 691 | putpvoid ("ReadProcessMemory location", GDB_READPROCESSMEMORY, where); |
| 692 | putlen ("ReadProcessMemory size", GDB_READPROCESSMEMORY, len); |
| 693 | |
| 694 | return getresult ("ReadProcessMemory buf", GDB_READPROCESSMEMORY, buf, nbytes); |
| 695 | } |
| 696 | |
| 697 | static int |
| 698 | write_process_memory (HANDLE h, LPCVOID where, LPCVOID buf, gdb_wince_len len, gdb_wince_len * nbytes) |
| 699 | { |
| 700 | if (s < 0) |
| 701 | return 1; |
| 702 | puthandle ("WriteProcessMemory handle", GDB_WRITEPROCESSMEMORY, h); |
| 703 | putpvoid ("WriteProcessMemory location", GDB_WRITEPROCESSMEMORY, where); |
| 704 | putmemory ("WriteProcProcessMemory buf", GDB_WRITEPROCESSMEMORY, buf, len); |
| 705 | |
| 706 | return getresult ("WriteProcessMemory result", GDB_WRITEPROCESSMEMORY, nbytes, NULL); |
| 707 | } |
| 708 | |
| 709 | static int |
| 710 | remote_read_bytes (CORE_ADDR memaddr, char *myaddr, int len) |
| 711 | { |
| 712 | gdb_wince_len nbytes; |
| 713 | if (!read_process_memory (current_process_handle, (LPCVOID) memaddr, |
| 714 | (LPVOID) myaddr, len, &nbytes)) |
| 715 | return -1; |
| 716 | return nbytes; |
| 717 | } |
| 718 | |
| 719 | static int |
| 720 | remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len) |
| 721 | { |
| 722 | gdb_wince_len nbytes; |
| 723 | if (!write_process_memory (current_process_handle, (LPCVOID) memaddr, |
| 724 | (LPCVOID) myaddr, len, &nbytes)) |
| 725 | return -1; |
| 726 | return nbytes; |
| 727 | } |
| 728 | |
| 729 | /* This is not a standard Win32 function. It instructs the stub to return TRUE |
| 730 | if the thread referenced by HANDLE h is alive. |
| 731 | */ |
| 732 | static int |
| 733 | thread_alive (HANDLE h) |
| 734 | { |
| 735 | gdb_wince_result res; |
| 736 | if (s < 0) |
| 737 | return 1; |
| 738 | puthandle ("ThreadAlive handle", GDB_THREADALIVE, h); |
| 739 | return getresult ("ThreadAlive result", GDB_THREADALIVE, &res, NULL); |
| 740 | } |
| 741 | |
| 742 | static int |
| 743 | suspend_thread (HANDLE h) |
| 744 | { |
| 745 | if (s < 0) |
| 746 | return 1; |
| 747 | puthandle ("SuspendThread handle", GDB_SUSPENDTHREAD, h); |
| 748 | return (int) getdword ("SuspendThread result", GDB_SUSPENDTHREAD); |
| 749 | } |
| 750 | |
| 751 | static int |
| 752 | resume_thread (HANDLE h) |
| 753 | { |
| 754 | if (s < 0) |
| 755 | return 1; |
| 756 | puthandle ("ResumeThread handle", GDB_RESUMETHREAD, h); |
| 757 | return (int) getdword ("SuspendThread result", GDB_RESUMETHREAD); |
| 758 | } |
| 759 | |
| 760 | static int |
| 761 | continue_debug_event (DWORD pid, DWORD tid, DWORD status) |
| 762 | { |
| 763 | gdb_wince_result res; |
| 764 | if (s < 0) |
| 765 | return 0; |
| 766 | putdword ("ContinueDebugEvent pid", GDB_CONTINUEDEBUGEVENT, pid); |
| 767 | putdword ("ContinueDebugEvent tid", GDB_CONTINUEDEBUGEVENT, tid); |
| 768 | putdword ("ContinueDebugEvent status", GDB_CONTINUEDEBUGEVENT, status); |
| 769 | return getresult ("ContinueDebugEvent result", GDB_CONTINUEDEBUGEVENT, &res, NULL); |
| 770 | } |
| 771 | |
| 772 | static int |
| 773 | close_handle (HANDLE h) |
| 774 | { |
| 775 | gdb_wince_result res; |
| 776 | if (s < 0) |
| 777 | return 1; |
| 778 | puthandle ("CloseHandle handle", GDB_CLOSEHANDLE, h); |
| 779 | return (int) getresult ("CloseHandle result", GDB_CLOSEHANDLE, &res, NULL); |
| 780 | } |
| 781 | |
| 782 | /* This is not a standard Win32 interface. This function tells the stub |
| 783 | to terminate. |
| 784 | */ |
| 785 | static void |
| 786 | stop_stub (void) |
| 787 | { |
| 788 | if (s < 0) |
| 789 | return; |
| 790 | (void) putdword ("Stopping gdb stub", GDB_STOPSTUB, 0); |
| 791 | s = -1; |
| 792 | } |
| 793 | |
| 794 | /******************** End of emulation routines. ********************/ |
| 795 | /******************** End of stub interface ********************/ |
| 796 | |
| 797 | #define check_for_step(a, x) (x) |
| 798 | |
| 799 | #ifdef MIPS |
| 800 | static void |
| 801 | undoSStep (thread_info * th) |
| 802 | { |
| 803 | if (th->stepped) |
| 804 | { |
| 805 | memory_remove_breakpoint (th->step_pc, (void *) &th->step_prev); |
| 806 | th->stepped = 0; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | void |
| 811 | wince_software_single_step (unsigned int ignore, int insert_breakpoints_p) |
| 812 | { |
| 813 | unsigned long pc; |
| 814 | thread_info *th = current_thread; /* Info on currently selected thread */ |
| 815 | CORE_ADDR mips_next_pc (CORE_ADDR pc); |
| 816 | |
| 817 | if (!insert_breakpoints_p) |
| 818 | { |
| 819 | undoSStep (th); |
| 820 | return; |
| 821 | } |
| 822 | |
| 823 | th->stepped = 1; |
| 824 | pc = read_register (PC_REGNUM); |
| 825 | th->step_pc = mips_next_pc (pc); |
| 826 | th->step_prev = 0; |
| 827 | memory_insert_breakpoint (th->step_pc, (void *) &th->step_prev); |
| 828 | return; |
| 829 | } |
| 830 | #elif SHx |
| 831 | /* Hitachi SH architecture instruction encoding masks */ |
| 832 | |
| 833 | #define COND_BR_MASK 0xff00 |
| 834 | #define UCOND_DBR_MASK 0xe000 |
| 835 | #define UCOND_RBR_MASK 0xf0df |
| 836 | #define TRAPA_MASK 0xff00 |
| 837 | |
| 838 | #define COND_DISP 0x00ff |
| 839 | #define UCOND_DISP 0x0fff |
| 840 | #define UCOND_REG 0x0f00 |
| 841 | |
| 842 | /* Hitachi SH instruction opcodes */ |
| 843 | |
| 844 | #define BF_INSTR 0x8b00 |
| 845 | #define BT_INSTR 0x8900 |
| 846 | #define BRA_INSTR 0xa000 |
| 847 | #define BSR_INSTR 0xb000 |
| 848 | #define JMP_INSTR 0x402b |
| 849 | #define JSR_INSTR 0x400b |
| 850 | #define RTS_INSTR 0x000b |
| 851 | #define RTE_INSTR 0x002b |
| 852 | #define TRAPA_INSTR 0xc300 |
| 853 | #define SSTEP_INSTR 0xc3ff |
| 854 | |
| 855 | |
| 856 | #define T_BIT_MASK 0x0001 |
| 857 | |
| 858 | static CORE_ADDR |
| 859 | sh_get_next_pc (CONTEXT *c) |
| 860 | { |
| 861 | short *instrMem; |
| 862 | int displacement; |
| 863 | int reg; |
| 864 | unsigned short opcode; |
| 865 | |
| 866 | instrMem = (short *) c->Fir; |
| 867 | |
| 868 | opcode = read_memory_integer ((CORE_ADDR) c->Fir, sizeof (opcode)); |
| 869 | |
| 870 | if ((opcode & COND_BR_MASK) == BT_INSTR) |
| 871 | { |
| 872 | if (c->Psr & T_BIT_MASK) |
| 873 | { |
| 874 | displacement = (opcode & COND_DISP) << 1; |
| 875 | if (displacement & 0x80) |
| 876 | displacement |= 0xffffff00; |
| 877 | /* |
| 878 | * Remember PC points to second instr. |
| 879 | * after PC of branch ... so add 4 |
| 880 | */ |
| 881 | instrMem = (short *) (c->Fir + displacement + 4); |
| 882 | } |
| 883 | else |
| 884 | instrMem += 1; |
| 885 | } |
| 886 | else if ((opcode & COND_BR_MASK) == BF_INSTR) |
| 887 | { |
| 888 | if (c->Psr & T_BIT_MASK) |
| 889 | instrMem += 1; |
| 890 | else |
| 891 | { |
| 892 | displacement = (opcode & COND_DISP) << 1; |
| 893 | if (displacement & 0x80) |
| 894 | displacement |= 0xffffff00; |
| 895 | /* |
| 896 | * Remember PC points to second instr. |
| 897 | * after PC of branch ... so add 4 |
| 898 | */ |
| 899 | instrMem = (short *) (c->Fir + displacement + 4); |
| 900 | } |
| 901 | } |
| 902 | else if ((opcode & UCOND_DBR_MASK) == BRA_INSTR) |
| 903 | { |
| 904 | displacement = (opcode & UCOND_DISP) << 1; |
| 905 | if (displacement & 0x0800) |
| 906 | displacement |= 0xfffff000; |
| 907 | |
| 908 | /* |
| 909 | * Remember PC points to second instr. |
| 910 | * after PC of branch ... so add 4 |
| 911 | */ |
| 912 | instrMem = (short *) (c->Fir + displacement + 4); |
| 913 | } |
| 914 | else if ((opcode & UCOND_RBR_MASK) == JSR_INSTR) |
| 915 | { |
| 916 | reg = (char) ((opcode & UCOND_REG) >> 8); |
| 917 | |
| 918 | instrMem = (short *) *regptr (c, reg); |
| 919 | } |
| 920 | else if (opcode == RTS_INSTR) |
| 921 | instrMem = (short *) c->PR; |
| 922 | else if (opcode == RTE_INSTR) |
| 923 | instrMem = (short *) *regptr (c, 15); |
| 924 | else if ((opcode & TRAPA_MASK) == TRAPA_INSTR) |
| 925 | instrMem = (short *) ((opcode & ~TRAPA_MASK) << 2); |
| 926 | else |
| 927 | instrMem += 1; |
| 928 | |
| 929 | return (CORE_ADDR) instrMem; |
| 930 | } |
| 931 | /* Single step (in a painstaking fashion) by inspecting the current |
| 932 | instruction and setting a breakpoint on the "next" instruction |
| 933 | which would be executed. This code hails from sh-stub.c. |
| 934 | */ |
| 935 | static void |
| 936 | undoSStep (thread_info * th) |
| 937 | { |
| 938 | if (th->stepped) |
| 939 | { |
| 940 | memory_remove_breakpoint (th->step_pc, (void *) &th->step_prev); |
| 941 | th->stepped = 0; |
| 942 | } |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | /* Single step (in a painstaking fashion) by inspecting the current |
| 947 | instruction and setting a breakpoint on the "next" instruction |
| 948 | which would be executed. This code hails from sh-stub.c. |
| 949 | */ |
| 950 | void |
| 951 | wince_software_single_step (unsigned int ignore, int insert_breakpoints_p) |
| 952 | { |
| 953 | thread_info *th = current_thread; /* Info on currently selected thread */ |
| 954 | |
| 955 | if (!insert_breakpoints_p) |
| 956 | { |
| 957 | undoSStep (th); |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | th->stepped = 1; |
| 962 | th->step_pc = sh_get_next_pc (&th->context); |
| 963 | th->step_prev = 0; |
| 964 | memory_insert_breakpoint (th->step_pc, (void *) &th->step_prev); |
| 965 | return; |
| 966 | } |
| 967 | #elif defined (ARM) |
| 968 | #undef check_for_step |
| 969 | |
| 970 | static enum target_signal |
| 971 | check_for_step (DEBUG_EVENT *ev, enum target_signal x) |
| 972 | { |
| 973 | thread_info *th = thread_rec (ev->dwThreadId, 1); |
| 974 | |
| 975 | if (th->stepped && |
| 976 | th->step_pc == (CORE_ADDR) ev->u.Exception.ExceptionRecord.ExceptionAddress) |
| 977 | return TARGET_SIGNAL_TRAP; |
| 978 | else |
| 979 | return x; |
| 980 | } |
| 981 | |
| 982 | /* Single step (in a painstaking fashion) by inspecting the current |
| 983 | instruction and setting a breakpoint on the "next" instruction |
| 984 | which would be executed. This code hails from sh-stub.c. |
| 985 | */ |
| 986 | static void |
| 987 | undoSStep (thread_info * th) |
| 988 | { |
| 989 | if (th->stepped) |
| 990 | { |
| 991 | memory_remove_breakpoint (th->step_pc, (void *) &th->step_prev); |
| 992 | th->stepped = 0; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void |
| 997 | wince_software_single_step (unsigned int ignore, int insert_breakpoints_p) |
| 998 | { |
| 999 | unsigned long pc; |
| 1000 | thread_info *th = current_thread; /* Info on currently selected thread */ |
| 1001 | CORE_ADDR mips_next_pc (CORE_ADDR pc); |
| 1002 | |
| 1003 | if (!insert_breakpoints_p) |
| 1004 | { |
| 1005 | undoSStep (th); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | th->stepped = 1; |
| 1010 | pc = read_register (PC_REGNUM); |
| 1011 | th->step_pc = arm_get_next_pc (pc); |
| 1012 | th->step_prev = 0; |
| 1013 | memory_insert_breakpoint (th->step_pc, (void *) &th->step_prev); |
| 1014 | return; |
| 1015 | } |
| 1016 | #endif |
| 1017 | |
| 1018 | /* Find a thread record given a thread id. |
| 1019 | If get_context then also retrieve the context for this |
| 1020 | thread. */ |
| 1021 | static thread_info * |
| 1022 | thread_rec (DWORD id, int get_context) |
| 1023 | { |
| 1024 | thread_info *th; |
| 1025 | |
| 1026 | for (th = &thread_head; (th = th->next) != NULL;) |
| 1027 | if (th->id == id) |
| 1028 | { |
| 1029 | if (!th->suspend_count && get_context) |
| 1030 | { |
| 1031 | if (get_context > 0 && th != this_thread) |
| 1032 | th->suspend_count = suspend_thread (th->h) + 1; |
| 1033 | else if (get_context < 0) |
| 1034 | th->suspend_count = -1; |
| 1035 | |
| 1036 | th->context.ContextFlags = CONTEXT_DEBUGGER; |
| 1037 | get_thread_context (th->h, &th->context); |
| 1038 | } |
| 1039 | return th; |
| 1040 | } |
| 1041 | |
| 1042 | return NULL; |
| 1043 | } |
| 1044 | |
| 1045 | /* Add a thread to the thread list */ |
| 1046 | static thread_info * |
| 1047 | child_add_thread (DWORD id, HANDLE h) |
| 1048 | { |
| 1049 | thread_info *th; |
| 1050 | |
| 1051 | if ((th = thread_rec (id, FALSE))) |
| 1052 | return th; |
| 1053 | |
| 1054 | th = (thread_info *) xmalloc (sizeof (*th)); |
| 1055 | memset (th, 0, sizeof (*th)); |
| 1056 | th->id = id; |
| 1057 | th->h = h; |
| 1058 | th->next = thread_head.next; |
| 1059 | thread_head.next = th; |
| 1060 | add_thread (id); |
| 1061 | return th; |
| 1062 | } |
| 1063 | |
| 1064 | /* Clear out any old thread list and reintialize it to a |
| 1065 | pristine state. */ |
| 1066 | static void |
| 1067 | child_init_thread_list (void) |
| 1068 | { |
| 1069 | thread_info *th = &thread_head; |
| 1070 | |
| 1071 | DEBUG_EVENTS (("gdb: child_init_thread_list\n")); |
| 1072 | init_thread_list (); |
| 1073 | while (th->next != NULL) |
| 1074 | { |
| 1075 | thread_info *here = th->next; |
| 1076 | th->next = here->next; |
| 1077 | (void) close_handle (here->h); |
| 1078 | xfree (here); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | /* Delete a thread from the list of threads */ |
| 1083 | static void |
| 1084 | child_delete_thread (DWORD id) |
| 1085 | { |
| 1086 | thread_info *th; |
| 1087 | |
| 1088 | if (info_verbose) |
| 1089 | printf_unfiltered ("[Deleting %s]\n", target_pid_to_str (id)); |
| 1090 | delete_thread (id); |
| 1091 | |
| 1092 | for (th = &thread_head; |
| 1093 | th->next != NULL && th->next->id != id; |
| 1094 | th = th->next) |
| 1095 | continue; |
| 1096 | |
| 1097 | if (th->next != NULL) |
| 1098 | { |
| 1099 | thread_info *here = th->next; |
| 1100 | th->next = here->next; |
| 1101 | close_handle (here->h); |
| 1102 | xfree (here); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | static void |
| 1107 | check (BOOL ok, const char *file, int line) |
| 1108 | { |
| 1109 | if (!ok) |
| 1110 | printf_filtered ("error return %s:%d was %d\n", file, line, GetLastError ()); |
| 1111 | } |
| 1112 | |
| 1113 | static void |
| 1114 | do_child_fetch_inferior_registers (int r) |
| 1115 | { |
| 1116 | if (r >= 0) |
| 1117 | { |
| 1118 | supply_register (r, (char *) regptr (¤t_thread->context, r)); |
| 1119 | } |
| 1120 | else |
| 1121 | { |
| 1122 | for (r = 0; r < NUM_REGS; r++) |
| 1123 | do_child_fetch_inferior_registers (r); |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | static void |
| 1128 | child_fetch_inferior_registers (int r) |
| 1129 | { |
| 1130 | current_thread = thread_rec (inferior_pid, TRUE); |
| 1131 | do_child_fetch_inferior_registers (r); |
| 1132 | } |
| 1133 | |
| 1134 | static void |
| 1135 | do_child_store_inferior_registers (int r) |
| 1136 | { |
| 1137 | if (r >= 0) |
| 1138 | read_register_gen (r, ((char *) ¤t_thread->context) + mappings[r]); |
| 1139 | else |
| 1140 | { |
| 1141 | for (r = 0; r < NUM_REGS; r++) |
| 1142 | do_child_store_inferior_registers (r); |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /* Store a new register value into the current thread context */ |
| 1147 | static void |
| 1148 | child_store_inferior_registers (int r) |
| 1149 | { |
| 1150 | current_thread = thread_rec (inferior_pid, TRUE); |
| 1151 | do_child_store_inferior_registers (r); |
| 1152 | } |
| 1153 | |
| 1154 | /* Wait for child to do something. Return pid of child, or -1 in case |
| 1155 | of error; store status through argument pointer OURSTATUS. */ |
| 1156 | |
| 1157 | static int |
| 1158 | handle_load_dll (PTR dummy) |
| 1159 | { |
| 1160 | LOAD_DLL_DEBUG_INFO *event = ¤t_event.u.LoadDll; |
| 1161 | char dll_buf[MAX_PATH + 1]; |
| 1162 | char *p, *bufp, *imgp, *dll_name, *dll_basename; |
| 1163 | int len; |
| 1164 | |
| 1165 | dll_buf[0] = dll_buf[sizeof (dll_buf) - 1] = '\0'; |
| 1166 | if (!event->lpImageName) |
| 1167 | return 1; |
| 1168 | |
| 1169 | len = 0; |
| 1170 | for (bufp = dll_buf, imgp = event->lpImageName; |
| 1171 | bufp < dll_buf + sizeof (dll_buf); |
| 1172 | bufp += 16, imgp += 16) |
| 1173 | { |
| 1174 | gdb_wince_len nbytes = 0; |
| 1175 | (void) read_process_memory (current_process_handle, |
| 1176 | imgp, bufp, 16, &nbytes); |
| 1177 | |
| 1178 | if (!nbytes && bufp == dll_buf) |
| 1179 | return 1; /* couldn't read it */ |
| 1180 | for (p = bufp; p < bufp + nbytes; p++) |
| 1181 | { |
| 1182 | len++; |
| 1183 | if (*p == '\0') |
| 1184 | goto out; |
| 1185 | if (event->fUnicode) |
| 1186 | p++; |
| 1187 | } |
| 1188 | if (!nbytes) |
| 1189 | break; |
| 1190 | } |
| 1191 | |
| 1192 | out: |
| 1193 | if (!len) |
| 1194 | return 1; |
| 1195 | #if 0 |
| 1196 | dll_buf[len] = '\0'; |
| 1197 | #endif |
| 1198 | dll_name = alloca (len); |
| 1199 | |
| 1200 | if (!dll_name) |
| 1201 | return 1; |
| 1202 | |
| 1203 | if (!event->fUnicode) |
| 1204 | memcpy (dll_name, dll_buf, len); |
| 1205 | else |
| 1206 | WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) dll_buf, len, |
| 1207 | dll_name, len, 0, 0); |
| 1208 | |
| 1209 | while ((p = strchr (dll_name, '\\'))) |
| 1210 | *p = '/'; |
| 1211 | |
| 1212 | /* FIXME!! It would be nice to define one symbol which pointed to the |
| 1213 | front of the dll if we can't find any symbols. */ |
| 1214 | |
| 1215 | if (!(dll_basename = strrchr (dll_name, '/'))) |
| 1216 | dll_basename = dll_name; |
| 1217 | else |
| 1218 | dll_basename++; |
| 1219 | |
| 1220 | /* The symbols in a dll are offset by 0x1000, which is the |
| 1221 | the offset from 0 of the first byte in an image - because |
| 1222 | of the file header and the section alignment. |
| 1223 | |
| 1224 | FIXME: Is this the real reason that we need the 0x1000 ? */ |
| 1225 | |
| 1226 | printf_unfiltered ("%x:%s", event->lpBaseOfDll, dll_name); |
| 1227 | printf_unfiltered ("\n"); |
| 1228 | |
| 1229 | return 1; |
| 1230 | } |
| 1231 | |
| 1232 | /* Handle DEBUG_STRING output from child process. */ |
| 1233 | static void |
| 1234 | handle_output_debug_string (struct target_waitstatus *ourstatus) |
| 1235 | { |
| 1236 | char p[256]; |
| 1237 | char s[255]; |
| 1238 | char *q; |
| 1239 | gdb_wince_len nbytes_read; |
| 1240 | gdb_wince_len nbytes = current_event.u.DebugString.nDebugStringLength; |
| 1241 | |
| 1242 | if (nbytes > 255) |
| 1243 | nbytes = 255; |
| 1244 | |
| 1245 | memset (p, 0, sizeof (p)); |
| 1246 | if (!read_process_memory (current_process_handle, |
| 1247 | current_event.u.DebugString.lpDebugStringData, |
| 1248 | &p, nbytes, &nbytes_read) |
| 1249 | || !*p) |
| 1250 | return; |
| 1251 | |
| 1252 | memset (s, 0, sizeof (s)); |
| 1253 | WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) p, (int) nbytes_read, s, |
| 1254 | sizeof (s) - 1, NULL, NULL); |
| 1255 | q = strchr (s, '\n'); |
| 1256 | if (q != NULL) |
| 1257 | { |
| 1258 | *q = '\0'; |
| 1259 | if (*--q = '\r') |
| 1260 | *q = '\0'; |
| 1261 | } |
| 1262 | |
| 1263 | warning (s); |
| 1264 | |
| 1265 | return; |
| 1266 | } |
| 1267 | |
| 1268 | /* Handle target exceptions. */ |
| 1269 | static int |
| 1270 | handle_exception (struct target_waitstatus *ourstatus) |
| 1271 | { |
| 1272 | #if 0 |
| 1273 | if (current_event.u.Exception.dwFirstChance) |
| 1274 | return 0; |
| 1275 | #endif |
| 1276 | |
| 1277 | ourstatus->kind = TARGET_WAITKIND_STOPPED; |
| 1278 | |
| 1279 | switch (current_event.u.Exception.ExceptionRecord.ExceptionCode) |
| 1280 | { |
| 1281 | case EXCEPTION_ACCESS_VIOLATION: |
| 1282 | DEBUG_EXCEPT (("gdb: Target exception ACCESS_VIOLATION at 0x%08x\n", |
| 1283 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
| 1284 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; |
| 1285 | break; |
| 1286 | case STATUS_STACK_OVERFLOW: |
| 1287 | DEBUG_EXCEPT (("gdb: Target exception STACK_OVERFLOW at 0x%08x\n", |
| 1288 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
| 1289 | ourstatus->value.sig = TARGET_SIGNAL_SEGV; |
| 1290 | break; |
| 1291 | case EXCEPTION_BREAKPOINT: |
| 1292 | DEBUG_EXCEPT (("gdb: Target exception BREAKPOINT at 0x%08x\n", |
| 1293 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
| 1294 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
| 1295 | break; |
| 1296 | case DBG_CONTROL_C: |
| 1297 | DEBUG_EXCEPT (("gdb: Target exception CONTROL_C at 0x%08x\n", |
| 1298 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
| 1299 | ourstatus->value.sig = TARGET_SIGNAL_INT; |
| 1300 | /* User typed CTRL-C. Continue with this status */ |
| 1301 | last_sig = SIGINT; /* FIXME - should check pass state */ |
| 1302 | break; |
| 1303 | case EXCEPTION_SINGLE_STEP: |
| 1304 | DEBUG_EXCEPT (("gdb: Target exception SINGLE_STEP at 0x%08x\n", |
| 1305 | (unsigned) current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
| 1306 | ourstatus->value.sig = TARGET_SIGNAL_TRAP; |
| 1307 | break; |
| 1308 | case EXCEPTION_ILLEGAL_INSTRUCTION: |
| 1309 | DEBUG_EXCEPT (("gdb: Target exception SINGLE_ILL at 0x%08x\n", |
| 1310 | current_event.u.Exception.ExceptionRecord.ExceptionAddress)); |
| 1311 | ourstatus->value.sig = check_for_step (¤t_event, TARGET_SIGNAL_ILL); |
| 1312 | break; |
| 1313 | default: |
| 1314 | /* This may be a structured exception handling exception. In |
| 1315 | that case, we want to let the program try to handle it, and |
| 1316 | only break if we see the exception a second time. */ |
| 1317 | |
| 1318 | printf_unfiltered ("gdb: unknown target exception 0x%08x at 0x%08x\n", |
| 1319 | current_event.u.Exception.ExceptionRecord.ExceptionCode, |
| 1320 | current_event.u.Exception.ExceptionRecord.ExceptionAddress); |
| 1321 | ourstatus->value.sig = TARGET_SIGNAL_UNKNOWN; |
| 1322 | break; |
| 1323 | } |
| 1324 | exception_count++; |
| 1325 | return 1; |
| 1326 | } |
| 1327 | |
| 1328 | /* Resume all artificially suspended threads if we are continuing |
| 1329 | execution */ |
| 1330 | static BOOL |
| 1331 | child_continue (DWORD continue_status, int id) |
| 1332 | { |
| 1333 | int i; |
| 1334 | thread_info *th; |
| 1335 | BOOL res; |
| 1336 | |
| 1337 | DEBUG_EVENTS (("ContinueDebugEvent (cpid=%d, ctid=%d, DBG_CONTINUE);\n", |
| 1338 | (unsigned) current_event.dwProcessId, (unsigned) current_event.dwThreadId)); |
| 1339 | res = continue_debug_event (current_event.dwProcessId, |
| 1340 | current_event.dwThreadId, |
| 1341 | continue_status); |
| 1342 | if (res) |
| 1343 | for (th = &thread_head; (th = th->next) != NULL;) |
| 1344 | if (((id == -1) || (id == th->id)) && th->suspend_count) |
| 1345 | { |
| 1346 | for (i = 0; i < th->suspend_count; i++) |
| 1347 | (void) resume_thread (th->h); |
| 1348 | th->suspend_count = 0; |
| 1349 | } |
| 1350 | |
| 1351 | return res; |
| 1352 | } |
| 1353 | |
| 1354 | /* Get the next event from the child. Return 1 if the event requires |
| 1355 | handling by WFI (or whatever). |
| 1356 | */ |
| 1357 | static int |
| 1358 | get_child_debug_event (int pid, struct target_waitstatus *ourstatus, |
| 1359 | DWORD target_event_code, int *retval) |
| 1360 | { |
| 1361 | int breakout = 0; |
| 1362 | BOOL debug_event; |
| 1363 | DWORD continue_status, event_code; |
| 1364 | thread_info *th = NULL; |
| 1365 | static thread_info dummy_thread_info; |
| 1366 | |
| 1367 | if (!(debug_event = wait_for_debug_event (¤t_event, 1000))) |
| 1368 | { |
| 1369 | *retval = 0; |
| 1370 | goto out; |
| 1371 | } |
| 1372 | |
| 1373 | event_count++; |
| 1374 | continue_status = DBG_CONTINUE; |
| 1375 | *retval = 0; |
| 1376 | |
| 1377 | event_code = current_event.dwDebugEventCode; |
| 1378 | breakout = event_code == target_event_code; |
| 1379 | |
| 1380 | switch (event_code) |
| 1381 | { |
| 1382 | case CREATE_THREAD_DEBUG_EVENT: |
| 1383 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%x code=%s)\n", |
| 1384 | (unsigned) current_event.dwProcessId, |
| 1385 | (unsigned) current_event.dwThreadId, |
| 1386 | "CREATE_THREAD_DEBUG_EVENT")); |
| 1387 | /* Record the existence of this thread */ |
| 1388 | th = child_add_thread (current_event.dwThreadId, |
| 1389 | current_event.u.CreateThread.hThread); |
| 1390 | if (info_verbose) |
| 1391 | printf_unfiltered ("[New %s]\n", |
| 1392 | target_pid_to_str (current_event.dwThreadId)); |
| 1393 | break; |
| 1394 | |
| 1395 | case EXIT_THREAD_DEBUG_EVENT: |
| 1396 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", |
| 1397 | (unsigned) current_event.dwProcessId, |
| 1398 | (unsigned) current_event.dwThreadId, |
| 1399 | "EXIT_THREAD_DEBUG_EVENT")); |
| 1400 | child_delete_thread (current_event.dwThreadId); |
| 1401 | th = &dummy_thread_info; |
| 1402 | break; |
| 1403 | |
| 1404 | case CREATE_PROCESS_DEBUG_EVENT: |
| 1405 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", |
| 1406 | (unsigned) current_event.dwProcessId, |
| 1407 | (unsigned) current_event.dwThreadId, |
| 1408 | "CREATE_PROCESS_DEBUG_EVENT")); |
| 1409 | current_process_handle = current_event.u.CreateProcessInfo.hProcess; |
| 1410 | |
| 1411 | main_thread_id = inferior_pid = current_event.dwThreadId; |
| 1412 | /* Add the main thread */ |
| 1413 | th = child_add_thread (inferior_pid, |
| 1414 | current_event.u.CreateProcessInfo.hThread); |
| 1415 | break; |
| 1416 | |
| 1417 | case EXIT_PROCESS_DEBUG_EVENT: |
| 1418 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", |
| 1419 | (unsigned) current_event.dwProcessId, |
| 1420 | (unsigned) current_event.dwThreadId, |
| 1421 | "EXIT_PROCESS_DEBUG_EVENT")); |
| 1422 | ourstatus->kind = TARGET_WAITKIND_EXITED; |
| 1423 | ourstatus->value.integer = current_event.u.ExitProcess.dwExitCode; |
| 1424 | close_handle (current_process_handle); |
| 1425 | *retval = current_event.dwProcessId; |
| 1426 | breakout = 1; |
| 1427 | break; |
| 1428 | |
| 1429 | case LOAD_DLL_DEBUG_EVENT: |
| 1430 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", |
| 1431 | (unsigned) current_event.dwProcessId, |
| 1432 | (unsigned) current_event.dwThreadId, |
| 1433 | "LOAD_DLL_DEBUG_EVENT")); |
| 1434 | catch_errors (handle_load_dll, NULL, (char *) "", RETURN_MASK_ALL); |
| 1435 | registers_changed (); /* mark all regs invalid */ |
| 1436 | break; |
| 1437 | |
| 1438 | case UNLOAD_DLL_DEBUG_EVENT: |
| 1439 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", |
| 1440 | (unsigned) current_event.dwProcessId, |
| 1441 | (unsigned) current_event.dwThreadId, |
| 1442 | "UNLOAD_DLL_DEBUG_EVENT")); |
| 1443 | break; /* FIXME: don't know what to do here */ |
| 1444 | |
| 1445 | case EXCEPTION_DEBUG_EVENT: |
| 1446 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", |
| 1447 | (unsigned) current_event.dwProcessId, |
| 1448 | (unsigned) current_event.dwThreadId, |
| 1449 | "EXCEPTION_DEBUG_EVENT")); |
| 1450 | if (handle_exception (ourstatus)) |
| 1451 | *retval = current_event.dwThreadId; |
| 1452 | else |
| 1453 | { |
| 1454 | continue_status = DBG_EXCEPTION_NOT_HANDLED; |
| 1455 | breakout = 0; |
| 1456 | } |
| 1457 | break; |
| 1458 | |
| 1459 | case OUTPUT_DEBUG_STRING_EVENT: /* message from the kernel */ |
| 1460 | DEBUG_EVENTS (("gdb: kernel event for pid=%d tid=%d code=%s)\n", |
| 1461 | (unsigned) current_event.dwProcessId, |
| 1462 | (unsigned) current_event.dwThreadId, |
| 1463 | "OUTPUT_DEBUG_STRING_EVENT")); |
| 1464 | handle_output_debug_string ( ourstatus); |
| 1465 | break; |
| 1466 | default: |
| 1467 | printf_unfiltered ("gdb: kernel event for pid=%d tid=%d\n", |
| 1468 | current_event.dwProcessId, |
| 1469 | current_event.dwThreadId); |
| 1470 | printf_unfiltered (" unknown event code %d\n", |
| 1471 | current_event.dwDebugEventCode); |
| 1472 | break; |
| 1473 | } |
| 1474 | |
| 1475 | if (breakout) |
| 1476 | this_thread = current_thread = th ?: thread_rec (current_event.dwThreadId, TRUE); |
| 1477 | else |
| 1478 | CHECK (child_continue (continue_status, -1)); |
| 1479 | |
| 1480 | out: |
| 1481 | return breakout; |
| 1482 | } |
| 1483 | |
| 1484 | /* Wait for interesting events to occur in the target process. */ |
| 1485 | static int |
| 1486 | child_wait (int pid, struct target_waitstatus *ourstatus) |
| 1487 | { |
| 1488 | DWORD event_code; |
| 1489 | int retval; |
| 1490 | |
| 1491 | /* We loop when we get a non-standard exception rather than return |
| 1492 | with a SPURIOUS because resume can try and step or modify things, |
| 1493 | which needs a current_thread->h. But some of these exceptions mark |
| 1494 | the birth or death of threads, which mean that the current thread |
| 1495 | isn't necessarily what you think it is. */ |
| 1496 | |
| 1497 | while (1) |
| 1498 | if (get_child_debug_event (pid, ourstatus, EXCEPTION_DEBUG_EVENT, &retval)) |
| 1499 | return retval; |
| 1500 | else |
| 1501 | { |
| 1502 | int detach = 0; |
| 1503 | |
| 1504 | if (ui_loop_hook != NULL) |
| 1505 | detach = ui_loop_hook (0); |
| 1506 | |
| 1507 | if (detach) |
| 1508 | child_kill_inferior (); |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | /* Print status information about what we're accessing. */ |
| 1513 | |
| 1514 | static void |
| 1515 | child_files_info (struct target_ops *ignore) |
| 1516 | { |
| 1517 | printf_unfiltered ("\tUsing the running image of child %s.\n", |
| 1518 | target_pid_to_str (inferior_pid)); |
| 1519 | } |
| 1520 | |
| 1521 | /* ARGSUSED */ |
| 1522 | static void |
| 1523 | child_open (char *arg, int from_tty) |
| 1524 | { |
| 1525 | error ("Use the \"run\" command to start a child process."); |
| 1526 | } |
| 1527 | |
| 1528 | #define FACTOR (0x19db1ded53ea710LL) |
| 1529 | #define NSPERSEC 10000000 |
| 1530 | |
| 1531 | /* Convert a Win32 time to "UNIX" format. */ |
| 1532 | long |
| 1533 | to_time_t (FILETIME * ptr) |
| 1534 | { |
| 1535 | /* A file time is the number of 100ns since jan 1 1601 |
| 1536 | stuffed into two long words. |
| 1537 | A time_t is the number of seconds since jan 1 1970. */ |
| 1538 | |
| 1539 | long rem; |
| 1540 | long long x = ((long long) ptr->dwHighDateTime << 32) + ((unsigned) ptr->dwLowDateTime); |
| 1541 | x -= FACTOR; /* number of 100ns between 1601 and 1970 */ |
| 1542 | rem = x % ((long long) NSPERSEC); |
| 1543 | rem += (NSPERSEC / 2); |
| 1544 | x /= (long long) NSPERSEC; /* number of 100ns in a second */ |
| 1545 | x += (long long) (rem / NSPERSEC); |
| 1546 | return x; |
| 1547 | } |
| 1548 | |
| 1549 | /* Upload a file to the remote device depending on the user's |
| 1550 | 'set remoteupload' specification. */ |
| 1551 | char * |
| 1552 | upload_to_device (const char *to, const char *from) |
| 1553 | { |
| 1554 | HANDLE h; |
| 1555 | const char *dir = remote_directory ?: "\\gdb"; |
| 1556 | int len; |
| 1557 | static char *remotefile = NULL; |
| 1558 | LPWSTR wstr; |
| 1559 | char *p; |
| 1560 | DWORD err; |
| 1561 | const char *in_to = to; |
| 1562 | FILETIME crtime, actime, wrtime; |
| 1563 | time_t utime; |
| 1564 | struct stat st; |
| 1565 | int fd; |
| 1566 | |
| 1567 | /* Look for a path separator and only use trailing part. */ |
| 1568 | while ((p = strpbrk (to, "/\\")) != NULL) |
| 1569 | to = p + 1; |
| 1570 | |
| 1571 | if (!*to) |
| 1572 | error ("no filename found to upload - %s.", in_to); |
| 1573 | |
| 1574 | len = strlen (dir) + strlen (to) + 2; |
| 1575 | remotefile = (char *) realloc (remotefile, len); |
| 1576 | strcpy (remotefile, dir); |
| 1577 | strcat (remotefile, "\\"); |
| 1578 | strcat (remotefile, to); |
| 1579 | |
| 1580 | if (upload_when == UPLOAD_NEVER) |
| 1581 | return remotefile; /* Don't bother uploading. */ |
| 1582 | |
| 1583 | /* Open the source. */ |
| 1584 | if ((fd = openp (getenv ("PATH"), TRUE, (char *) from, O_RDONLY, 0, NULL)) < 0) |
| 1585 | error ("couldn't open %s", from); |
| 1586 | |
| 1587 | /* Get the time for later comparison. */ |
| 1588 | if (fstat (fd, &st)) |
| 1589 | st.st_mtime = (time_t) - 1; |
| 1590 | |
| 1591 | /* Always attempt to create the directory on the remote system. */ |
| 1592 | wstr = towide (dir, NULL); |
| 1593 | (void) CeCreateDirectory (wstr, NULL); |
| 1594 | |
| 1595 | /* Attempt to open the remote file, creating it if it doesn't exist. */ |
| 1596 | wstr = towide (remotefile, NULL); |
| 1597 | h = CeCreateFile (wstr, GENERIC_READ | GENERIC_WRITE, 0, NULL, |
| 1598 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 1599 | |
| 1600 | /* Some kind of problem? */ |
| 1601 | err = CeGetLastError (); |
| 1602 | if (h == NULL || h == INVALID_HANDLE_VALUE) |
| 1603 | error ("error opening file \"%s\". Windows error %d.", |
| 1604 | remotefile, err); |
| 1605 | |
| 1606 | CeGetFileTime (h, &crtime, &actime, &wrtime); |
| 1607 | utime = to_time_t (&wrtime); |
| 1608 | #if 0 |
| 1609 | if (utime < st.st_mtime) |
| 1610 | { |
| 1611 | char buf[80]; |
| 1612 | strcpy (buf, ctime(&utime)); |
| 1613 | printf ("%s < %s\n", buf, ctime(&st.st_mtime)); |
| 1614 | } |
| 1615 | #endif |
| 1616 | /* See if we need to upload the file. */ |
| 1617 | if (upload_when == UPLOAD_ALWAYS || |
| 1618 | err != ERROR_ALREADY_EXISTS || |
| 1619 | !CeGetFileTime (h, &crtime, &actime, &wrtime) || |
| 1620 | to_time_t (&wrtime) < st.st_mtime) |
| 1621 | { |
| 1622 | DWORD nbytes; |
| 1623 | char buf[4096]; |
| 1624 | int n; |
| 1625 | |
| 1626 | /* Upload the file. */ |
| 1627 | while ((n = read (fd, buf, sizeof (buf))) > 0) |
| 1628 | if (!CeWriteFile (h, buf, (DWORD) n, &nbytes, NULL)) |
| 1629 | error ("error writing to remote device - %d.", |
| 1630 | CeGetLastError ()); |
| 1631 | } |
| 1632 | |
| 1633 | close (fd); |
| 1634 | if (!CeCloseHandle (h)) |
| 1635 | error ("error closing remote file - %d.", CeGetLastError ()); |
| 1636 | |
| 1637 | return remotefile; |
| 1638 | } |
| 1639 | |
| 1640 | /* Initialize the connection to the remote device. */ |
| 1641 | static void |
| 1642 | wince_initialize (void) |
| 1643 | { |
| 1644 | int tmp; |
| 1645 | char args[256]; |
| 1646 | char *hostname; |
| 1647 | struct sockaddr_in sin; |
| 1648 | char *stub_file_name; |
| 1649 | int s0; |
| 1650 | PROCESS_INFORMATION pi; |
| 1651 | |
| 1652 | if (!connection_initialized) |
| 1653 | switch (CeRapiInit ()) |
| 1654 | { |
| 1655 | case 0: |
| 1656 | connection_initialized = 1; |
| 1657 | break; |
| 1658 | default: |
| 1659 | CeRapiUninit (); |
| 1660 | error ("Can't initialize connection to remote device.\n"); |
| 1661 | break; |
| 1662 | } |
| 1663 | |
| 1664 | /* Upload the stub to the handheld device. */ |
| 1665 | stub_file_name = upload_to_device ("wince-stub.exe", WINCE_STUB); |
| 1666 | strcpy (args, stub_file_name); |
| 1667 | |
| 1668 | if (remote_add_host) |
| 1669 | { |
| 1670 | strcat (args, " "); |
| 1671 | hostname = strchr (args, '\0'); |
| 1672 | if (gethostname (hostname, sizeof (args) - strlen (args))) |
| 1673 | error ("couldn't get hostname of this system."); |
| 1674 | } |
| 1675 | |
| 1676 | /* Get a socket. */ |
| 1677 | if ((s0 = socket (AF_INET, SOCK_STREAM, 0)) < 0) |
| 1678 | stub_error ("Couldn't connect to host system."); |
| 1679 | |
| 1680 | /* Allow rapid reuse of the port. */ |
| 1681 | tmp = 1; |
| 1682 | (void) setsockopt (s0, SOL_SOCKET, SO_REUSEADDR, (char *) &tmp, sizeof (tmp)); |
| 1683 | |
| 1684 | |
| 1685 | /* Set up the information for connecting to the host gdb process. */ |
| 1686 | memset (&sin, 0, sizeof (sin)); |
| 1687 | sin.sin_family = AF_INET; |
| 1688 | sin.sin_port = htons (7000); /* FIXME: This should be configurable */ |
| 1689 | |
| 1690 | if (bind (s0, (struct sockaddr *) &sin, sizeof (sin))) |
| 1691 | error ("couldn't bind socket"); |
| 1692 | |
| 1693 | if (listen (s0, 1)) |
| 1694 | error ("Couldn't open socket for listening.\n"); |
| 1695 | |
| 1696 | /* Start up the stub on the remote device. */ |
| 1697 | if (!CeCreateProcess (towide (stub_file_name, NULL), towide (args, NULL), |
| 1698 | NULL, NULL, 0, 0, NULL, NULL, NULL, &pi)) |
| 1699 | error ("Unable to start remote stub '%s'. Windows CE error %d.", |
| 1700 | stub_file_name, CeGetLastError ()); |
| 1701 | |
| 1702 | /* Wait for a connection */ |
| 1703 | |
| 1704 | if ((s = accept (s0, NULL, NULL)) < 0) |
| 1705 | error ("couldn't set up server for connection."); |
| 1706 | |
| 1707 | close (s0); |
| 1708 | } |
| 1709 | |
| 1710 | /* Start an inferior win32 child process and sets inferior_pid to its pid. |
| 1711 | EXEC_FILE is the file to run. |
| 1712 | ALLARGS is a string containing the arguments to the program. |
| 1713 | ENV is the environment vector to pass. Errors reported with error(). */ |
| 1714 | static void |
| 1715 | child_create_inferior (char *exec_file, char *args, char **env) |
| 1716 | { |
| 1717 | PROCESS_INFORMATION pi; |
| 1718 | struct target_waitstatus dummy; |
| 1719 | int ret; |
| 1720 | DWORD flags, event_code; |
| 1721 | char *exec_and_args; |
| 1722 | |
| 1723 | if (!exec_file) |
| 1724 | error ("No executable specified, use `target exec'.\n"); |
| 1725 | |
| 1726 | flags = DEBUG_PROCESS; |
| 1727 | |
| 1728 | wince_initialize (); /* Make sure we've got a connection. */ |
| 1729 | |
| 1730 | exec_file = upload_to_device (exec_file, exec_file); |
| 1731 | |
| 1732 | while (*args == ' ') |
| 1733 | args++; |
| 1734 | |
| 1735 | /* Allocate space for "command<sp>args" */ |
| 1736 | if (*args == '\0') |
| 1737 | { |
| 1738 | exec_and_args = alloca (strlen (exec_file) + 1); |
| 1739 | strcpy (exec_and_args, exec_file); |
| 1740 | } |
| 1741 | else |
| 1742 | { |
| 1743 | exec_and_args = alloca (strlen (exec_file + strlen (args) + 2)); |
| 1744 | sprintf (exec_and_args, "%s %s", exec_file, args); |
| 1745 | } |
| 1746 | |
| 1747 | memset (&pi, 0, sizeof (pi)); |
| 1748 | /* Execute the process */ |
| 1749 | if (!create_process (exec_file, exec_and_args, flags, &pi)) |
| 1750 | error ("Error creating process %s, (error %d)\n", exec_file, GetLastError ()); |
| 1751 | |
| 1752 | exception_count = 0; |
| 1753 | event_count = 0; |
| 1754 | |
| 1755 | current_process_handle = pi.hProcess; |
| 1756 | current_event.dwProcessId = pi.dwProcessId; |
| 1757 | memset (¤t_event, 0, sizeof (current_event)); |
| 1758 | inferior_pid = current_event.dwThreadId = pi.dwThreadId; |
| 1759 | push_target (&child_ops); |
| 1760 | child_init_thread_list (); |
| 1761 | child_add_thread (pi.dwThreadId, pi.hThread); |
| 1762 | init_wait_for_inferior (); |
| 1763 | clear_proceed_status (); |
| 1764 | target_terminal_init (); |
| 1765 | target_terminal_inferior (); |
| 1766 | |
| 1767 | /* Run until process and threads are loaded */ |
| 1768 | while (!get_child_debug_event (inferior_pid, &dummy, |
| 1769 | CREATE_PROCESS_DEBUG_EVENT, &ret)) |
| 1770 | continue; |
| 1771 | |
| 1772 | proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0); |
| 1773 | } |
| 1774 | |
| 1775 | /* Chile has gone bye-bye. */ |
| 1776 | static void |
| 1777 | child_mourn_inferior (void) |
| 1778 | { |
| 1779 | (void) child_continue (DBG_CONTINUE, -1); |
| 1780 | unpush_target (&child_ops); |
| 1781 | stop_stub (); |
| 1782 | CeRapiUninit (); |
| 1783 | connection_initialized = 0; |
| 1784 | generic_mourn_inferior (); |
| 1785 | } |
| 1786 | |
| 1787 | /* Move memory from child to/from gdb. */ |
| 1788 | int |
| 1789 | child_xfer_memory (CORE_ADDR memaddr, char *our, int len, |
| 1790 | int write, struct target_ops *target) |
| 1791 | { |
| 1792 | if (len <= 0) |
| 1793 | return 0; |
| 1794 | |
| 1795 | if (write) |
| 1796 | res = remote_write_bytes (memaddr, our, len); |
| 1797 | else |
| 1798 | res = remote_read_bytes (memaddr, our, len); |
| 1799 | |
| 1800 | return res; |
| 1801 | } |
| 1802 | |
| 1803 | /* Terminate the process and wait for child to tell us it has completed. */ |
| 1804 | void |
| 1805 | child_kill_inferior (void) |
| 1806 | { |
| 1807 | CHECK (terminate_process (current_process_handle)); |
| 1808 | |
| 1809 | for (;;) |
| 1810 | { |
| 1811 | if (!child_continue (DBG_CONTINUE, -1)) |
| 1812 | break; |
| 1813 | if (!wait_for_debug_event (¤t_event, INFINITE)) |
| 1814 | break; |
| 1815 | if (current_event.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) |
| 1816 | break; |
| 1817 | } |
| 1818 | |
| 1819 | CHECK (close_handle (current_process_handle)); |
| 1820 | close_handle (current_thread->h); |
| 1821 | target_mourn_inferior (); /* or just child_mourn_inferior? */ |
| 1822 | } |
| 1823 | |
| 1824 | /* Resume the child after an exception. */ |
| 1825 | void |
| 1826 | child_resume (int pid, int step, enum target_signal sig) |
| 1827 | { |
| 1828 | thread_info *th; |
| 1829 | DWORD continue_status = last_sig > 0 && last_sig < NSIG ? |
| 1830 | DBG_EXCEPTION_NOT_HANDLED : DBG_CONTINUE; |
| 1831 | |
| 1832 | DEBUG_EXEC (("gdb: child_resume (pid=%d, step=%d, sig=%d);\n", |
| 1833 | pid, step, sig)); |
| 1834 | |
| 1835 | /* Get context for currently selected thread */ |
| 1836 | th = thread_rec (current_event.dwThreadId, FALSE); |
| 1837 | |
| 1838 | if (th->context.ContextFlags) |
| 1839 | { |
| 1840 | CHECK (set_thread_context (th->h, &th->context)); |
| 1841 | th->context.ContextFlags = 0; |
| 1842 | } |
| 1843 | |
| 1844 | /* Allow continuing with the same signal that interrupted us. |
| 1845 | Otherwise complain. */ |
| 1846 | if (sig && sig != last_sig) |
| 1847 | fprintf_unfiltered (gdb_stderr, "Can't send signals to the child. signal %d\n", sig); |
| 1848 | |
| 1849 | last_sig = 0; |
| 1850 | child_continue (continue_status, pid); |
| 1851 | } |
| 1852 | |
| 1853 | static void |
| 1854 | child_prepare_to_store (void) |
| 1855 | { |
| 1856 | /* Do nothing, since we can store individual regs */ |
| 1857 | } |
| 1858 | |
| 1859 | static int |
| 1860 | child_can_run (void) |
| 1861 | { |
| 1862 | return 1; |
| 1863 | } |
| 1864 | |
| 1865 | static void |
| 1866 | child_close (void) |
| 1867 | { |
| 1868 | DEBUG_EVENTS (("gdb: child_close, inferior_pid=%d\n", inferior_pid)); |
| 1869 | } |
| 1870 | |
| 1871 | /* Explicitly upload file to remotedir */ |
| 1872 | |
| 1873 | static void |
| 1874 | child_load (char *file, int from_tty) |
| 1875 | { |
| 1876 | upload_to_device (file, file); |
| 1877 | } |
| 1878 | |
| 1879 | struct target_ops child_ops; |
| 1880 | |
| 1881 | static void |
| 1882 | init_child_ops (void) |
| 1883 | { |
| 1884 | memset (&child_ops, 0, sizeof (child_ops)); |
| 1885 | child_ops.to_shortname = (char *) "child"; |
| 1886 | child_ops.to_longname = (char *) "Windows CE process"; |
| 1887 | child_ops.to_doc = (char *) "Windows CE process (started by the \"run\" command)."; |
| 1888 | child_ops.to_open = child_open; |
| 1889 | child_ops.to_close = child_close; |
| 1890 | child_ops.to_resume = child_resume; |
| 1891 | child_ops.to_wait = child_wait; |
| 1892 | child_ops.to_fetch_registers = child_fetch_inferior_registers; |
| 1893 | child_ops.to_store_registers = child_store_inferior_registers; |
| 1894 | child_ops.to_prepare_to_store = child_prepare_to_store; |
| 1895 | child_ops.to_xfer_memory = child_xfer_memory; |
| 1896 | child_ops.to_files_info = child_files_info; |
| 1897 | child_ops.to_insert_breakpoint = memory_insert_breakpoint; |
| 1898 | child_ops.to_remove_breakpoint = memory_remove_breakpoint; |
| 1899 | child_ops.to_terminal_init = terminal_init_inferior; |
| 1900 | child_ops.to_terminal_inferior = terminal_inferior; |
| 1901 | child_ops.to_terminal_ours_for_output = terminal_ours_for_output; |
| 1902 | child_ops.to_terminal_ours = terminal_ours; |
| 1903 | child_ops.to_terminal_info = child_terminal_info; |
| 1904 | child_ops.to_kill = child_kill_inferior; |
| 1905 | child_ops.to_load = child_load; |
| 1906 | child_ops.to_create_inferior = child_create_inferior; |
| 1907 | child_ops.to_mourn_inferior = child_mourn_inferior; |
| 1908 | child_ops.to_can_run = child_can_run; |
| 1909 | child_ops.to_thread_alive = win32_child_thread_alive; |
| 1910 | child_ops.to_stratum = process_stratum; |
| 1911 | child_ops.to_has_all_memory = 1; |
| 1912 | child_ops.to_has_memory = 1; |
| 1913 | child_ops.to_has_stack = 1; |
| 1914 | child_ops.to_has_registers = 1; |
| 1915 | child_ops.to_has_execution = 1; |
| 1916 | child_ops.to_sections = 0; |
| 1917 | child_ops.to_sections_end = 0; |
| 1918 | child_ops.to_magic = OPS_MAGIC; |
| 1919 | } |
| 1920 | |
| 1921 | |
| 1922 | /* Handle 'set remoteupload' parameter. */ |
| 1923 | |
| 1924 | #define replace_upload(what) \ |
| 1925 | upload_when = what; \ |
| 1926 | remote_upload = realloc (remote_upload, strlen (upload_options[upload_when].name) + 1); \ |
| 1927 | strcpy (remote_upload, upload_options[upload_when].name); |
| 1928 | |
| 1929 | static void |
| 1930 | set_upload_type (char *ignore, int from_tty) |
| 1931 | { |
| 1932 | int i, len; |
| 1933 | char *bad_option; |
| 1934 | |
| 1935 | if (!remote_upload || !remote_upload[0]) |
| 1936 | { |
| 1937 | replace_upload (UPLOAD_NEWER); |
| 1938 | if (from_tty) |
| 1939 | printf_unfiltered ("Upload upload_options are: always, newer, never.\n"); |
| 1940 | return; |
| 1941 | } |
| 1942 | |
| 1943 | len = strlen (remote_upload); |
| 1944 | for (i = 0; i < (sizeof (upload_options) / sizeof (upload_options[0])); i++) |
| 1945 | if (len >= upload_options[i].abbrev && |
| 1946 | strncasecmp (remote_upload, upload_options[i].name, len) == 0) |
| 1947 | { |
| 1948 | replace_upload (i); |
| 1949 | return; |
| 1950 | } |
| 1951 | |
| 1952 | bad_option = remote_upload; |
| 1953 | replace_upload (UPLOAD_NEWER); |
| 1954 | error ("Unknown upload type: %s.", bad_option); |
| 1955 | } |
| 1956 | |
| 1957 | void |
| 1958 | _initialize_inftarg (void) |
| 1959 | { |
| 1960 | struct cmd_list_element *set; |
| 1961 | init_child_ops (); |
| 1962 | |
| 1963 | add_show_from_set |
| 1964 | (add_set_cmd ((char *) "remotedirectory", no_class, |
| 1965 | var_string_noescape, (char *) &remote_directory, |
| 1966 | (char *) "Set directory for remote upload.\n", |
| 1967 | &setlist), |
| 1968 | &showlist); |
| 1969 | remote_directory = xstrdup (remote_directory); |
| 1970 | |
| 1971 | set = add_set_cmd ((char *) "remoteupload", no_class, |
| 1972 | var_string_noescape, (char *) &remote_upload, |
| 1973 | (char *) "Set how to upload executables to remote device.\n", |
| 1974 | &setlist); |
| 1975 | add_show_from_set (set, &showlist); |
| 1976 | set->function.cfunc = set_upload_type; |
| 1977 | set_upload_type (NULL, 0); |
| 1978 | |
| 1979 | add_show_from_set |
| 1980 | (add_set_cmd ((char *) "debugexec", class_support, var_boolean, |
| 1981 | (char *) &debug_exec, |
| 1982 | (char *) "Set whether to display execution in child process.", |
| 1983 | &setlist), |
| 1984 | &showlist); |
| 1985 | |
| 1986 | add_show_from_set |
| 1987 | (add_set_cmd ((char *) "remoteaddhost", class_support, var_boolean, |
| 1988 | (char *) &remote_add_host, |
| 1989 | (char *) "Set whether to add this host to remote stub arguments for\n |
| 1990 | debugging over a network.", &setlist), |
| 1991 | &showlist); |
| 1992 | |
| 1993 | add_show_from_set |
| 1994 | (add_set_cmd ((char *) "debugevents", class_support, var_boolean, |
| 1995 | (char *) &debug_events, |
| 1996 | (char *) "Set whether to display kernel events in child process.", |
| 1997 | &setlist), |
| 1998 | &showlist); |
| 1999 | |
| 2000 | add_show_from_set |
| 2001 | (add_set_cmd ((char *) "debugmemory", class_support, var_boolean, |
| 2002 | (char *) &debug_memory, |
| 2003 | (char *) "Set whether to display memory accesses in child process.", |
| 2004 | &setlist), |
| 2005 | &showlist); |
| 2006 | |
| 2007 | add_show_from_set |
| 2008 | (add_set_cmd ((char *) "debugexceptions", class_support, var_boolean, |
| 2009 | (char *) &debug_exceptions, |
| 2010 | (char *) "Set whether to display kernel exceptions in child process.", |
| 2011 | &setlist), |
| 2012 | &showlist); |
| 2013 | |
| 2014 | add_target (&child_ops); |
| 2015 | } |
| 2016 | |
| 2017 | /* Determine if the thread referenced by "pid" is alive |
| 2018 | by "polling" it. If WaitForSingleObject returns WAIT_OBJECT_0 |
| 2019 | it means that the pid has died. Otherwise it is assumed to be alive. */ |
| 2020 | static int |
| 2021 | win32_child_thread_alive (int pid) |
| 2022 | { |
| 2023 | return thread_alive (thread_rec (pid, FALSE)->h); |
| 2024 | } |
| 2025 | |
| 2026 | /* Convert pid to printable format. */ |
| 2027 | char * |
| 2028 | cygwin_pid_to_str (int pid) |
| 2029 | { |
| 2030 | static char buf[80]; |
| 2031 | if (pid == current_event.dwProcessId) |
| 2032 | sprintf (buf, "process %d", pid); |
| 2033 | else |
| 2034 | sprintf (buf, "thread %d.0x%x", (unsigned) current_event.dwProcessId, pid); |
| 2035 | return buf; |
| 2036 | } |