[PATCH] Windows CE support update.

Pedro Alves
2006-06-19T12:54:18Z

[-- Attachment #1: Type: text/plain, Size: 14833 bytes --]

Arghh, I very sorry to have posted this to the wrong mailing list
before. I am sorry for generating even more traffic in sending once
again a 35K attachment. Bash me at will. I deserve it.

---

Hi all,

I was fixing a few things here and there in gdb/wince.c and
gdb/wince-stub.c, until I noticed that most of it
is duplicated from gdb/win32-nat.c. I then merged the WinCE support into
win32-nat.c.
The result is that Windows CE support code got almost cut in half, and
we lose no features. In fact, we gain some.
The ChangeLog is big, but it is mostly about renaming, moving and
deleting stuff.

I've declared a vector (win32_target_iface_t) holding pointers into
implementations of the needed functions to communicate with the stub.
I've done it this way, because I thought of adding support for remote
debugging of desktop Windows apps using the same stub as Windows CE.
In the end I never got to it, but should be easy to do. IMHO, this is
better than duplicating all of the debugging support in a target remote
stub,
as done in gdbproxy from Wine.
I then define macros for all the win32 calls in this vector, so for
example, a call for CloseHandle turns into a DEBUG_CloseHandle.

I have introduced a few defines, that I don't know the right place for
them. They currently live on the win32.h file. They are
the HAVE_CREATE_NEW_PROCESS_GROUP, HAVE_SELECTORS,
HAVE_DEBUG_PRIVILEGE_SUPPORT.
They depend on target architecture and target OS, so maybe they should
be in the respective config/$arch/*.h files.

Also, I only tested this on arm-wince-pe. I don't have access to any
MIPS or SH Windows CE machine. I don't think they are
supported by MS anymore, but I am not sure. I am not against deprecating
those.

Any chance of this going into mainline?

Cheers,
Pedro Alves

---
ChangeLog

2006-06-19 Pedro Alves <pedro_alves@portugalmail.pt>

* win32.h: New file.

* win32-nat.c: Merge common code from wince.c.
(win32_target_iface): Instantiate.
(CHECK, DEBUG_EXEC, DEBUG_EVENTS, DEBUG_MEM, DEBUG_EXCEPT):
Guard against dangling else.
(new_group): Conditionally compile.
(pe_bfd_names_type, pe_bfd_names_list, PE_BFD_NAMES_LIST_COUNT,
pe_bfd_names, pe_bfd_names_init): Select arch name to pass
to bfd support.
(mappings): Add SH, MIPS, and ARM context offsets.
(thread_rec): Call DEBUG_SuspendThread instead of SuspendThread.
(win32_add_thread): Conditionally compile debug registers support.
Call DEBUG_GetThreadContext instead of GetThreadContext.
Call DEBUG_SetThreadContext instead of SetThreadContext.
(win32_init_thread_list): Call DEBUG_CloseHandle instead of
CloseHandle.
(win32_delete_thread): Ditto.
(do_win32_fetch_inferior_registers): Call DEBUG_GetThreadContext
instead of GetThreadContext.
Not all archs have CONTEXT_DEBUG_REGISTERS or
CONTEXT_EXTENDED_REGISTERS,
conditionally compile. Guard i386 specific code.
(psapi_get_dll_name): Don't compile for Windows CE.
Replace LoadLibrary call with LoadLibraryA call.
(solib_symbols_add): Pass bfd_openr the pei and pe archive names
resolved by pe_bfd_names_init.
Don't try to bfd_close NULL pointer.
(register_loaded_dll): Support for Windows CE using solib.c machinery.
(get_image_name): Pass in buffer to write to, instead of
allocating it on the stack.
In Windows CE the address pointer contains the image name itself.
Call DEBUG_ReadProcessMemory instead of ReadProcessMemory.
Try reading process memory in chunks for efficiency.
(handle_load_dll): Don't call psapi_get_dll_name when compiling for
Windows CE.
Pass buffer to get_image_name.
(handle_output_debug_string): target_read_string doesn't support
unicode.
Reimplement with ReadProcessMemory. Filter out last endline in
Windows CE case.
(display_selector, display_selectors): Only compile when the
architecture has selectors.
(DEBUG_EXCEPTION_SIMPLE): Guard against dangling else.
(win32_continue): Call DEBUG_ContinueDebugEvent instead of
ContinueDebugEvent.
Call DEBUG_ResumeThread instead of ResumeThread.
Call DEBUG_SetThreadContext instead of SetThreadContext.
Conditionally compile debug registers support.
(win32_resume): FLAG_TRACE_BIT is i386 specific.
Use SOFTWARE_SINGLE_STEP on other architectures.
Conditionally compile debug registers support.
Call DEBUG_SetThreadContext instead of SetThreadContext. Fix typo.
(get_win32_debug_event): Call DEBUG_WaitForDebugEvent instead of
WaitForDebugEvent.
Call DEBUG_CloseHandle instead of CloseHandle. Progress report
on Windows CE.
Windows CE doesn't send the initial breakpoint like desktop
versions of Windows do.
Add it explicitly. Handle RIP_EVENT.
(do_initial_win32_stuff): Conditionally compile debug registers
support. Remove manually inserted initial breakpoint in
Windows CE case.
(has_detach_ability): Windows CE has no detach ability.
(set_process_privilege): Conditionally compile. No support
in Windows CE.
(win32_attach): Don't call set_process_privilege when there is
no support for it.
Call DEBUG_DebugActiveProcess instead of DebugActiveProcess.
Fix typo.
Windows CE has no detach ability.
(win32_detach): Windows CE has no detach ability, don't even try it.
(win32_pid_to_exec_file): Not supported in Windows CE.
(win32_create_inferior): Upload Windows CE stub and connect to it.
Upload the executable.
Don't convert the path for Windows CE. Conditionally compile support
for CREATE_NEW_PROCESS_GROUP.
Call DEBUG_CreateProcessA instead of CreateProcess.
Pass in executable name. Windows CE needs it,
and desktop Windows accepts it.
Don't close the newly created handles on Windows CE.
(win32_mourn_inferior): Disconnect from stub.
Conditionally compile debug registers support.
(win32_stop): Unimplemented on Windows CE (yet).
(win32_xfer_memory): Call DEBUG_WriteProcessMemory,
DEBUG_FlushInstructionCache and DEBUG_ReadProcessMemory
instead of WriteProcessMemory, FlushInstructionCache and
ReadProcessMemory.
(win32_kill_inferior): Call DEBUG_TerminateProcess,
DEBUG_WaitForDebugEvent and DEBUG_CloseHandle instead of
TerminateProcess, WaitForDebugEvent and CloseHandle.
For Windows CE case, dont't check TerminateProcess result,
as that always gives an ACCESS_DENIED error, and
don't wait for target to terminate.
(dll_code_sections_add): Pass bfd_openr the pei archive name
resolved by pe_bfd_names_init.
(init_win32_ops): to_longname and to_doc get "Windows CE" on Windows
CE.
No support for to_pid_to_exec_file on Windows CE.
(_init_win32_nat_iface): New function.
(_initialize_win32_nat): Use stub interface or native interface
depending on compiling for Windows CE or not.
Conditionally compile support for CREATE_NEW_PROCESS_GROUP.
Only compile selectors support on targets that support it.
(cygwin_set_dr, cygwin_set_dr7, cygwin_get_dr6): Conditionally
compile based on debug registers support.
(win32_win32_thread_alive): Call DEBUG_WaitForSingleObject
instead of WaitForSingleObject.

* wince.c: Remove code duplicate fro win32-nat.c
(CHECK, DEBUG_EXEC, DEBUG_EVENTS, DEBUG_MEM, DEBUG_EXCEPT): Remove.
(wcslen, CeSetFileTime): Declare.
(werror): New function.
(DEFAULT_UPLOAD_DIR): New define.
(DEBUG_COM, DEBUG_WINCE): New macros.
(win32_child_thread_alive, child_kill_inferior, last_sig,
thread_info_struct, thread_head, thread_rec, current_event,
current_process_handle, current_thread, this_thread, main_thread_id,
exception_count, event_count, debug_exec, debug_events,
debug_memory,
debug_exceptions, mappings, regptr, getword, putlen, getlen): Remove.
(s): Initialize to invalid socket state.
(vstub_error, stub_error): Tag with ATTR_NORETURN.
(sockread): Handle EINTR. Return 1 on success.
(sockwrite): Return 1 on success.
(putdword, putword, getdword, getresult): Update sockread and
sockwrite calls to new semantics.
(putmemory, getresult): len is now long.
(REMOTE_CreateProcessA): New function.
(REMOTE_OpenProcess): New function.
(REMOTE_TerminateProcess): Rename from terminate_process.
Return FALSE when there is no connection.
(REMOTE_WaitForDebugEvent): Rename from wait_for_debug_event.
Return FALSE when there is no connection.
(REMOTE_GetThreadContext): Rename from get_thread_context.
Return FALSE when there is no connection. Make sure we have
the right context.
(REMOTE_SetThreadContext): Rename from set_thread_context.
Return FALSE when there is no connection.
(REMOTE_ReadProcessMemory): Rename from read_process_memory.
Return FALSE when there is no connection.
(REMOTE_WriteProcessMemory): Rename from write_process_memory.
Return FALSE when there is no connection.
(REMOTE_FlushInstructionCache): New function.
(remote_write_bytes, thread_alive): Remove.
(REMOTE_WaitForSingleObject): New function.
(REMOTE_SuspendThread): Rename from suspend_thread.
(REMOTE_ResumeThread): Rename from resume_thread.
(REMOTE_ContinueDebugEvent): Rename from continue_debug_event.
Return FALSE when there is no connection.
(REMOTE_CloseHandle): Rename from close_handle. Return FALSE when
there is no connection. Short-circuit NULL case.
(check_for_step, undoSStep, wince_software_single_step,
sh_get_next_pc): Remove.
(child_add_thread, child_init_thread_list, child_delete_thread,
check, do_child_fetch_inferior_registers,
child_fetch_inferior_registers,
do_child_store_inferior_registers, child_store_inferior_registers,
handle_load_dll, handle_output_debug_string, handle_exception,
child_continue, get_child_debug_event, child_wait,
child_files_info, child_open): Remove.

(REMOTE_FileChecksum): New function.
(_init_win32_wce_iface): New function.
(FACTOR, NSPERSEC, time_t_to_filetime, to_time_t):
Update from a recent cygwin.
(upload_to_device): Don't rely only on timestamps to decide
if file needs uploading, but do a simple checksum compare too.
Enables the debugging of older executables than already on device.
Don't open for writing when reading the timestamps.
Enables more than one stub running. Issue progress report.
Synchronize timestamps between host and target.
(wince_stub_connect): Rename from wince_initialize. Don't bother
connecting when upload_when is UPLOAD_NEVER. Disable Nagle algorithm.
(child_create_inferior, child_kill_inferior, child_resume,
child_prepare_to_store, child_can_run, child_close,
child_load, init_child_ops): Remove.
(replace_upload): Guard against dangling else.
(set_upload_type): Update to new declaration.
(_initialize_wince): Remove commands already handled by win32-nat.c.
Add "debugcommunication", "debugwince" commands.
(win32_child_thread_alive, cygwin_pid_to_str): Remove.

* wince-stub.h (win_func): Sort alphanumerically. Define GDB_FIRST
as the first element. Remove GDB_SINGLESTEP.
(gdb_wince_len): Define as unsigned long.
(gdb_wince_result): Define as unsigned long.
(putlen): Define.
(getlen): Define.

* wince-stub.c (wcschr): Protoize.
(in_addr_t): Define.
(REALLOC): Remove.
(STUB_LOG): New macro.
(vstub_log): New function.
(stub_log): New function.
(sockread, sockwrite): Return 1 on success.
(getdword, getword, getmemory, putdword, putword): Adapt to new
sockread and sockwrite semantics.
(getlen, putlen): Remove.
(puthandle): New macro.
(putmemory): gdb_wince_len is now 32 bits. Adapt.
(curproc): Remove comment, no longer true.
(create_process): Try to detect suspicious cases.
(terminate_process): Fully simulate TerminateProcess call.
(debug_active_process): New function.
(stepped): Remove.
(flat_single_step): Remove.
(wait_for_debug_event): Only handle events for the process we
are debugging, not it's children.
(open_process): New function.
(get_thread_context): Workaround the fact that w32api headers
don't define the whole CONTEXT structure yet.
(write_process_memory): Remove unneeded casts.
(flush_instruction_cache): New function.
(thread_alive): Remove.
(wait_for_single_object): New function.
(file_checksum): New function.
(stop_requested): Record here a stub stop request.
(stop_stub): New function.
(msg_handler_map): Map stub command to handler function.
(HANDLER_COUNT): New macro.
(error_there_are_handlers_missing,
error_there_are_too_many_handlers): New compile time asserts.
(dispatch): Use msg_handler_map instead of switch over every
possible message. Close stub when stop_requested is detected.
(WinMain): Add some logging. Workaround the fact that gethostbyname
function cannot resolve IP address strings passed to it.
Disable Nagle algorithm.

* config/arm/tm-wince.h (SOFTWARE_SINGLE_STEP_P): Remove.
(SOFTWARE_SINGLE_STEP): Remove.
(wince_software_single_step): Remove.
(ARM_BE_BREAKPOINT, ARM_LE_BREAKPOINT): Define the correct
values for Windows CE.

* config/arm/wince.mt (TDEPFILES): Also compile win32-nat.c and
corelow.c.
(MT_CFLAGS): Define __arm__. Break long line into multiple lines.

* solib.c: Set errno to ENOENT, so we can use perror_with_name
correctly.
(solib_map_sections): When the so is not found, flag it.
On subsequent searches, avoid researching, and repeating the
same error message.
(solib_read_symbols): Only call symbol_add_stub, when solib_open
reports the so is found. Otherwise, flag it.
(read_symbols_stub_arg): New struct.
(read_symbols_stub): New function.
sharedlibrary_command: Always pass in a valid string to solib_add.
(solib_add): Only consider a non-empty string a valid pattern.
Try reading in symbols from all sos when there is an empty
pattern (called from sharedlibrary_command). If there isn't a pattern,
only try reading sos we know exist (gdb->not_found is not set).
Call solib_read symbols wrapped in catch_errors,
so we don't stop on first error.

* solist.h (so_list): Add not_found cache flag.

[-- Attachment #2: gdb-wince.diff.gz --]
[-- Type: application/gzip, Size: 35728 bytes --]