This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows-nat.c
3279 lines (2856 loc) · 93.1 KB
/
windows-nat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Target-vector operations for controlling windows child processes, for GDB.
Copyright (C) 1995-2024 Free Software Foundation, Inc.
Contributed by Cygnus Solutions, A Red Hat Company.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* Originally by Steve Chamberlain, sac@cygnus.com */
#include "defs.h"
#include "frame.h"
#include "inferior.h"
#include "infrun.h"
#include "target.h"
#include "gdbcore.h"
#include "command.h"
#include "completer.h"
#include "regcache.h"
#include "top.h"
#include <signal.h>
#include <sys/types.h>
#include <fcntl.h>
#include <windows.h>
#include <imagehlp.h>
#ifdef __CYGWIN__
#include <wchar.h>
#include <sys/cygwin.h>
#include <cygwin/version.h>
#endif
#include <algorithm>
#include <vector>
#include <queue>
#include "filenames.h"
#include "symfile.h"
#include "objfiles.h"
#include "gdb_bfd.h"
#include "gdbsupport/gdb_obstack.h"
#include "gdbthread.h"
#include "gdbcmd.h"
#include <unistd.h>
#include "exec.h"
#include "solist.h"
#include "solib.h"
#include "xml-support.h"
#include "inttypes.h"
#include "i386-tdep.h"
#include "i387-tdep.h"
#include "windows-tdep.h"
#include "windows-nat.h"
#include "x86-nat.h"
#include "complaints.h"
#include "inf-child.h"
#include "gdbsupport/gdb_tilde_expand.h"
#include "gdbsupport/pathstuff.h"
#include "gdbsupport/gdb_wait.h"
#include "nat/windows-nat.h"
#include "gdbsupport/symbol.h"
#include "ser-event.h"
#include "inf-loop.h"
using namespace windows_nat;
/* Maintain a linked list of "so" information. */
struct windows_solib
{
LPVOID load_addr = 0;
CORE_ADDR text_offset = 0;
/* Original name. */
std::string original_name;
/* Expanded form of the name. */
std::string name;
};
struct windows_per_inferior : public windows_process_info
{
windows_thread_info *thread_rec (ptid_t ptid,
thread_disposition_type disposition) override;
int handle_output_debug_string (struct target_waitstatus *ourstatus) override;
void handle_load_dll (const char *dll_name, LPVOID base) override;
void handle_unload_dll () override;
bool handle_access_violation (const EXCEPTION_RECORD *rec) override;
int have_saved_context = 0; /* True if we've saved context from a
cygwin signal. */
uintptr_t dr[8] {};
int windows_initialization_done = 0;
std::vector<std::unique_ptr<windows_thread_info>> thread_list;
/* Counts of things. */
int saw_create = 0;
int open_process_used = 0;
#ifdef __x86_64__
void *wow64_dbgbreak = nullptr;
#endif
/* This vector maps GDB's idea of a register's number into an offset
in the windows exception context vector.
It also contains the bit mask needed to load the register in question.
The contents of this table can only be computed by the units
that provide CPU-specific support for Windows native debugging.
One day we could read a reg, we could inspect the context we
already have loaded, if it doesn't have the bit set that we need,
we read that set of registers in using GetThreadContext. If the
context already contains what we need, we just unpack it. Then to
write a register, first we have to ensure that the context contains
the other regs of the group, and then we copy the info in and set
out bit. */
const int *mappings = nullptr;
/* The function to use in order to determine whether a register is
a segment register or not. */
segment_register_p_ftype *segment_register_p = nullptr;
std::vector<windows_solib> solibs;
#ifdef __CYGWIN__
CONTEXT saved_context {}; /* Contains the saved context from a
cygwin signal. */
/* The starting and ending address of the cygwin1.dll text segment. */
CORE_ADDR cygwin_load_start = 0;
CORE_ADDR cygwin_load_end = 0;
#endif /* __CYGWIN__ */
};
/* The current process. */
static windows_per_inferior windows_process;
#undef STARTUPINFO
#ifndef __CYGWIN__
# define __PMAX (MAX_PATH + 1)
# define STARTUPINFO STARTUPINFOA
#else
# define __PMAX PATH_MAX
# define STARTUPINFO STARTUPINFOW
#endif
/* If we're not using the old Cygwin header file set, define the
following which never should have been in the generic Win32 API
headers in the first place since they were our own invention... */
#ifndef _GNU_H_WINDOWS_H
enum
{
FLAG_TRACE_BIT = 0x100,
};
#endif
#ifndef CONTEXT_EXTENDED_REGISTERS
/* This macro is only defined on ia32. It only makes sense on this target,
so define it as zero if not already defined. */
#define CONTEXT_EXTENDED_REGISTERS 0
#endif
#define CONTEXT_DEBUGGER_DR CONTEXT_FULL | CONTEXT_FLOATING_POINT \
| CONTEXT_SEGMENTS | CONTEXT_DEBUG_REGISTERS \
| CONTEXT_EXTENDED_REGISTERS
#define DR6_CLEAR_VALUE 0xffff0ff0
/* The string sent by cygwin when it processes a signal.
FIXME: This should be in a cygwin include file. */
#ifndef _CYGWIN_SIGNAL_STRING
#define _CYGWIN_SIGNAL_STRING "cYgSiGw00f"
#endif
#define CHECK(x) check (x, __FILE__,__LINE__)
#define DEBUG_EXEC(fmt, ...) \
debug_prefixed_printf_cond (debug_exec, "windows exec", fmt, ## __VA_ARGS__)
#define DEBUG_EVENTS(fmt, ...) \
debug_prefixed_printf_cond (debug_events, "windows events", fmt, \
## __VA_ARGS__)
#define DEBUG_MEM(fmt, ...) \
debug_prefixed_printf_cond (debug_memory, "windows mem", fmt, \
## __VA_ARGS__)
#define DEBUG_EXCEPT(fmt, ...) \
debug_prefixed_printf_cond (debug_exceptions, "windows except", fmt, \
## __VA_ARGS__)
static void cygwin_set_dr (int i, CORE_ADDR addr);
static void cygwin_set_dr7 (unsigned long val);
static CORE_ADDR cygwin_get_dr (int i);
static unsigned long cygwin_get_dr6 (void);
static unsigned long cygwin_get_dr7 (void);
/* User options. */
static bool new_console = false;
#ifdef __CYGWIN__
static bool cygwin_exceptions = false;
#endif
static bool new_group = true;
static bool debug_exec = false; /* show execution */
static bool debug_events = false; /* show events from kernel */
static bool debug_memory = false; /* show target memory accesses */
static bool debug_exceptions = false; /* show target exceptions */
static bool useshell = false; /* use shell for subprocesses */
/* See windows_nat_target::resume to understand why this is commented
out. */
#if 0
/* This vector maps the target's idea of an exception (extracted
from the DEBUG_EVENT structure) to GDB's idea. */
struct xlate_exception
{
DWORD them;
enum gdb_signal us;
};
static const struct xlate_exception xlate[] =
{
{EXCEPTION_ACCESS_VIOLATION, GDB_SIGNAL_SEGV},
{STATUS_STACK_OVERFLOW, GDB_SIGNAL_SEGV},
{EXCEPTION_BREAKPOINT, GDB_SIGNAL_TRAP},
{DBG_CONTROL_C, GDB_SIGNAL_INT},
{EXCEPTION_SINGLE_STEP, GDB_SIGNAL_TRAP},
{STATUS_FLOAT_DIVIDE_BY_ZERO, GDB_SIGNAL_FPE}
};
#endif /* 0 */
struct windows_nat_target final : public x86_nat_target<inf_child_target>
{
windows_nat_target ();
void close () override;
void attach (const char *, int) override;
bool attach_no_wait () override
{ return true; }
void detach (inferior *, int) override;
void resume (ptid_t, int , enum gdb_signal) override;
ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
void fetch_registers (struct regcache *, int) override;
void store_registers (struct regcache *, int) override;
bool stopped_by_sw_breakpoint () override
{
windows_thread_info *th
= windows_process.thread_rec (inferior_ptid, DONT_INVALIDATE_CONTEXT);
return th->stopped_at_software_breakpoint;
}
bool supports_stopped_by_sw_breakpoint () override
{
return true;
}
enum target_xfer_status xfer_partial (enum target_object object,
const char *annex,
gdb_byte *readbuf,
const gdb_byte *writebuf,
ULONGEST offset, ULONGEST len,
ULONGEST *xfered_len) override;
void files_info () override;
void kill () override;
void create_inferior (const char *, const std::string &,
char **, int) override;
void mourn_inferior () override;
bool thread_alive (ptid_t ptid) override;
std::string pid_to_str (ptid_t) override;
void interrupt () override;
void pass_ctrlc () override;
const char *pid_to_exec_file (int pid) override;
ptid_t get_ada_task_ptid (long lwp, ULONGEST thread) override;
bool get_tib_address (ptid_t ptid, CORE_ADDR *addr) override;
const char *thread_name (struct thread_info *) override;
ptid_t get_windows_debug_event (int pid, struct target_waitstatus *ourstatus,
target_wait_flags options);
void do_initial_windows_stuff (DWORD pid, bool attaching);
bool supports_disable_randomization () override
{
return disable_randomization_available ();
}
bool can_async_p () override
{
return true;
}
bool is_async_p () override
{
return m_is_async;
}
void async (bool enable) override;
int async_wait_fd () override
{
return serial_event_fd (m_wait_event);
}
private:
windows_thread_info *add_thread (ptid_t ptid, HANDLE h, void *tlb,
bool main_thread_p);
void delete_thread (ptid_t ptid, DWORD exit_code, bool main_thread_p);
DWORD fake_create_process ();
BOOL windows_continue (DWORD continue_status, int id, int killed,
bool last_call = false);
/* Helper function to start process_thread. */
static DWORD WINAPI process_thread_starter (LPVOID self);
/* This function implements the background thread that starts
inferiors and waits for events. */
void process_thread ();
/* Push FUNC onto the queue of requests for process_thread, and wait
until it has been called. On Windows, certain debugging
functions can only be called by the thread that started (or
attached to) the inferior. These are all done in the worker
thread, via calls to this method. If FUNC returns true,
process_thread will wait for debug events when FUNC returns. */
void do_synchronously (gdb::function_view<bool ()> func);
/* This waits for a debug event, dispatching to the worker thread as
needed. */
void wait_for_debug_event_main_thread (DEBUG_EVENT *event);
/* Queue used to send requests to process_thread. This is
implicitly locked. */
std::queue<gdb::function_view<bool ()>> m_queue;
/* Event used to signal process_thread that an item has been
pushed. */
HANDLE m_pushed_event;
/* Event used by process_thread to indicate that it has processed a
single function call. */
HANDLE m_response_event;
/* Serial event used to communicate wait event availability to the
main loop. */
serial_event *m_wait_event;
/* The last debug event, when M_WAIT_EVENT has been set. */
DEBUG_EVENT m_last_debug_event {};
/* True if a debug event is pending. */
std::atomic<bool> m_debug_event_pending { false };
/* True if currently in async mode. */
bool m_is_async = false;
};
static void
check (BOOL ok, const char *file, int line)
{
if (!ok)
{
unsigned err = (unsigned) GetLastError ();
gdb_printf ("error return %s:%d was %u: %s\n", file, line,
err, strwinerror (err));
}
}
windows_nat_target::windows_nat_target ()
: m_pushed_event (CreateEvent (nullptr, false, false, nullptr)),
m_response_event (CreateEvent (nullptr, false, false, nullptr)),
m_wait_event (make_serial_event ())
{
HANDLE bg_thread = CreateThread (nullptr, 64 * 1024,
process_thread_starter, this, 0, nullptr);
CloseHandle (bg_thread);
}
void
windows_nat_target::async (bool enable)
{
if (enable == is_async_p ())
return;
if (enable)
add_file_handler (async_wait_fd (),
[] (int, gdb_client_data)
{
inferior_event_handler (INF_REG_EVENT);
},
nullptr, "windows_nat_target");
else
delete_file_handler (async_wait_fd ());
m_is_async = enable;
}
/* A wrapper for WaitForSingleObject that issues a warning if
something unusual happens. */
static void
wait_for_single (HANDLE handle, DWORD howlong)
{
while (true)
{
DWORD r = WaitForSingleObject (handle, howlong);
if (r == WAIT_OBJECT_0)
return;
if (r == WAIT_FAILED)
{
unsigned err = (unsigned) GetLastError ();
warning ("WaitForSingleObject failed (code %u): %s",
err, strwinerror (err));
}
else
warning ("unexpected result from WaitForSingleObject: %u",
(unsigned) r);
}
}
DWORD WINAPI
windows_nat_target::process_thread_starter (LPVOID self)
{
((windows_nat_target *) self)->process_thread ();
return 0;
}
void
windows_nat_target::process_thread ()
{
while (true)
{
wait_for_single (m_pushed_event, INFINITE);
gdb::function_view<bool ()> func = std::move (m_queue.front ());
m_queue.pop ();
bool should_wait = func ();
SetEvent (m_response_event);
if (should_wait)
{
if (!m_debug_event_pending)
{
wait_for_debug_event (&m_last_debug_event, INFINITE);
m_debug_event_pending = true;
}
serial_event_set (m_wait_event);
}
}
}
void
windows_nat_target::do_synchronously (gdb::function_view<bool ()> func)
{
m_queue.emplace (std::move (func));
SetEvent (m_pushed_event);
wait_for_single (m_response_event, INFINITE);
}
void
windows_nat_target::wait_for_debug_event_main_thread (DEBUG_EVENT *event)
{
do_synchronously ([&] ()
{
if (m_debug_event_pending)
{
*event = m_last_debug_event;
m_debug_event_pending = false;
serial_event_clear (m_wait_event);
}
else
wait_for_debug_event (event, INFINITE);
return false;
});
}
/* See nat/windows-nat.h. */
windows_thread_info *
windows_per_inferior::thread_rec
(ptid_t ptid, thread_disposition_type disposition)
{
for (auto &th : thread_list)
if (th->tid == ptid.lwp ())
{
if (!th->suspended)
{
switch (disposition)
{
case DONT_INVALIDATE_CONTEXT:
/* Nothing. */
break;
case INVALIDATE_CONTEXT:
if (ptid.lwp () != current_event.dwThreadId)
th->suspend ();
th->reload_context = true;
break;
case DONT_SUSPEND:
th->reload_context = true;
th->suspended = -1;
break;
}
}
return th.get ();
}
return NULL;
}
/* Add a thread to the thread list.
PTID is the ptid of the thread to be added.
H is its Windows handle.
TLB is its thread local base.
MAIN_THREAD_P should be true if the thread to be added is
the main thread, false otherwise. */
windows_thread_info *
windows_nat_target::add_thread (ptid_t ptid, HANDLE h, void *tlb,
bool main_thread_p)
{
windows_thread_info *th;
gdb_assert (ptid.lwp () != 0);
if ((th = windows_process.thread_rec (ptid, DONT_INVALIDATE_CONTEXT)))
return th;
CORE_ADDR base = (CORE_ADDR) (uintptr_t) tlb;
#ifdef __x86_64__
/* For WOW64 processes, this is actually the pointer to the 64bit TIB,
and the 32bit TIB is exactly 2 pages after it. */
if (windows_process.wow64_process)
base += 0x2000;
#endif
th = new windows_thread_info (ptid.lwp (), h, base);
windows_process.thread_list.emplace_back (th);
/* Add this new thread to the list of threads.
To be consistent with what's done on other platforms, we add
the main thread silently (in reality, this thread is really
more of a process to the user than a thread). */
if (main_thread_p)
add_thread_silent (this, ptid);
else
::add_thread (this, ptid);
/* It's simplest to always set this and update the debug
registers. */
th->debug_registers_changed = true;
return th;
}
/* Clear out any old thread list and reinitialize it to a
pristine state. */
static void
windows_init_thread_list (void)
{
DEBUG_EVENTS ("called");
windows_process.thread_list.clear ();
}
/* Delete a thread from the list of threads.
PTID is the ptid of the thread to be deleted.
EXIT_CODE is the thread's exit code.
MAIN_THREAD_P should be true if the thread to be deleted is
the main thread, false otherwise. */
void
windows_nat_target::delete_thread (ptid_t ptid, DWORD exit_code,
bool main_thread_p)
{
DWORD id;
gdb_assert (ptid.lwp () != 0);
id = ptid.lwp ();
/* Note that no notification was printed when the main thread was
created, and thus, unless in verbose mode, we should be symmetrical,
and avoid an exit notification for the main thread here as well. */
bool silent = (main_thread_p && !info_verbose);
thread_info *to_del = this->find_thread (ptid);
delete_thread_with_exit_code (to_del, exit_code, silent);
auto iter = std::find_if (windows_process.thread_list.begin (),
windows_process.thread_list.end (),
[=] (std::unique_ptr<windows_thread_info> &th)
{
return th->tid == id;
});
if (iter != windows_process.thread_list.end ())
windows_process.thread_list.erase (iter);
}
/* Fetches register number R from the given windows_thread_info,
and supplies its value to the given regcache.
This function assumes that R is non-negative. A failed assertion
is raised if that is not true.
This function assumes that TH->RELOAD_CONTEXT is not set, meaning
that the windows_thread_info has an up-to-date context. A failed
assertion is raised if that assumption is violated. */
static void
windows_fetch_one_register (struct regcache *regcache,
windows_thread_info *th, int r)
{
gdb_assert (r >= 0);
gdb_assert (!th->reload_context);
char *context_ptr = (char *) &th->context;
#ifdef __x86_64__
if (windows_process.wow64_process)
context_ptr = (char *) &th->wow64_context;
#endif
char *context_offset = context_ptr + windows_process.mappings[r];
struct gdbarch *gdbarch = regcache->arch ();
i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
gdb_assert (!gdbarch_read_pc_p (gdbarch));
gdb_assert (gdbarch_pc_regnum (gdbarch) >= 0);
gdb_assert (!gdbarch_write_pc_p (gdbarch));
/* GDB treats some registers as 32-bit, where they are in fact only
16 bits long. These cases must be handled specially to avoid
reading extraneous bits from the context. */
if (r == I387_FISEG_REGNUM (tdep) || windows_process.segment_register_p (r))
{
gdb_byte bytes[4] = {};
memcpy (bytes, context_offset, 2);
regcache->raw_supply (r, bytes);
}
else if (r == I387_FOP_REGNUM (tdep))
{
long l = (*((long *) context_offset) >> 16) & ((1 << 11) - 1);
regcache->raw_supply (r, &l);
}
else
{
if (th->stopped_at_software_breakpoint
&& !th->pc_adjusted
&& r == gdbarch_pc_regnum (gdbarch))
{
int size = register_size (gdbarch, r);
if (size == 4)
{
uint32_t value;
memcpy (&value, context_offset, size);
value -= gdbarch_decr_pc_after_break (gdbarch);
memcpy (context_offset, &value, size);
}
else
{
gdb_assert (size == 8);
uint64_t value;
memcpy (&value, context_offset, size);
value -= gdbarch_decr_pc_after_break (gdbarch);
memcpy (context_offset, &value, size);
}
/* Make sure we only rewrite the PC a single time. */
th->pc_adjusted = true;
}
regcache->raw_supply (r, context_offset);
}
}
void
windows_nat_target::fetch_registers (struct regcache *regcache, int r)
{
windows_thread_info *th
= windows_process.thread_rec (regcache->ptid (), INVALIDATE_CONTEXT);
/* Check if TH exists. Windows sometimes uses a non-existent
thread id in its events. */
if (th == NULL)
return;
if (th->reload_context)
{
#ifdef __CYGWIN__
if (windows_process.have_saved_context)
{
/* Lie about where the program actually is stopped since
cygwin has informed us that we should consider the signal
to have occurred at another location which is stored in
"saved_context. */
memcpy (&th->context, &windows_process.saved_context,
__COPY_CONTEXT_SIZE);
windows_process.have_saved_context = 0;
}
else
#endif
#ifdef __x86_64__
if (windows_process.wow64_process)
{
th->wow64_context.ContextFlags = CONTEXT_DEBUGGER_DR;
CHECK (Wow64GetThreadContext (th->h, &th->wow64_context));
/* Copy dr values from that thread.
But only if there were not modified since last stop.
PR gdb/2388 */
if (!th->debug_registers_changed)
{
windows_process.dr[0] = th->wow64_context.Dr0;
windows_process.dr[1] = th->wow64_context.Dr1;
windows_process.dr[2] = th->wow64_context.Dr2;
windows_process.dr[3] = th->wow64_context.Dr3;
windows_process.dr[6] = th->wow64_context.Dr6;
windows_process.dr[7] = th->wow64_context.Dr7;
}
}
else
#endif
{
th->context.ContextFlags = CONTEXT_DEBUGGER_DR;
CHECK (GetThreadContext (th->h, &th->context));
/* Copy dr values from that thread.
But only if there were not modified since last stop.
PR gdb/2388 */
if (!th->debug_registers_changed)
{
windows_process.dr[0] = th->context.Dr0;
windows_process.dr[1] = th->context.Dr1;
windows_process.dr[2] = th->context.Dr2;
windows_process.dr[3] = th->context.Dr3;
windows_process.dr[6] = th->context.Dr6;
windows_process.dr[7] = th->context.Dr7;
}
}
th->reload_context = false;
}
if (r < 0)
for (r = 0; r < gdbarch_num_regs (regcache->arch()); r++)
windows_fetch_one_register (regcache, th, r);
else
windows_fetch_one_register (regcache, th, r);
}
/* Collect the register number R from the given regcache, and store
its value into the corresponding area of the given thread's context.
This function assumes that R is non-negative. A failed assertion
assertion is raised if that is not true. */
static void
windows_store_one_register (const struct regcache *regcache,
windows_thread_info *th, int r)
{
gdb_assert (r >= 0);
char *context_ptr = (char *) &th->context;
#ifdef __x86_64__
if (windows_process.wow64_process)
context_ptr = (char *) &th->wow64_context;
#endif
struct gdbarch *gdbarch = regcache->arch ();
i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
/* GDB treats some registers as 32-bit, where they are in fact only
16 bits long. These cases must be handled specially to avoid
overwriting other registers in the context. */
if (r == I387_FISEG_REGNUM (tdep) || windows_process.segment_register_p (r))
{
gdb_byte bytes[4];
regcache->raw_collect (r, bytes);
memcpy (context_ptr + windows_process.mappings[r], bytes, 2);
}
else if (r == I387_FOP_REGNUM (tdep))
{
gdb_byte bytes[4];
regcache->raw_collect (r, bytes);
/* The value of FOP occupies the top two bytes in the context,
so write the two low-order bytes from the cache into the
appropriate spot. */
memcpy (context_ptr + windows_process.mappings[r] + 2, bytes, 2);
}
else
regcache->raw_collect (r, context_ptr + windows_process.mappings[r]);
}
/* Store a new register value into the context of the thread tied to
REGCACHE. */
void
windows_nat_target::store_registers (struct regcache *regcache, int r)
{
windows_thread_info *th
= windows_process.thread_rec (regcache->ptid (), INVALIDATE_CONTEXT);
/* Check if TH exists. Windows sometimes uses a non-existent
thread id in its events. */
if (th == NULL)
return;
if (r < 0)
for (r = 0; r < gdbarch_num_regs (regcache->arch ()); r++)
windows_store_one_register (regcache, th, r);
else
windows_store_one_register (regcache, th, r);
}
/* See nat/windows-nat.h. */
static windows_solib *
windows_make_so (const char *name, LPVOID load_addr)
{
#ifndef __CYGWIN__
char *p;
char buf[__PMAX];
char cwd[__PMAX];
WIN32_FIND_DATA w32_fd;
HANDLE h = FindFirstFile(name, &w32_fd);
if (h == INVALID_HANDLE_VALUE)
strcpy (buf, name);
else
{
FindClose (h);
strcpy (buf, name);
if (GetCurrentDirectory (MAX_PATH + 1, cwd))
{
p = strrchr (buf, '\\');
if (p)
p[1] = '\0';
SetCurrentDirectory (buf);
GetFullPathName (w32_fd.cFileName, MAX_PATH, buf, &p);
SetCurrentDirectory (cwd);
}
}
if (strcasecmp (buf, "ntdll.dll") == 0)
{
GetSystemDirectory (buf, sizeof (buf));
strcat (buf, "\\ntdll.dll");
}
#else
wchar_t buf[__PMAX];
buf[0] = 0;
if (access (name, F_OK) != 0)
{
if (strcasecmp (name, "ntdll.dll") == 0)
{
GetSystemDirectoryW (buf, sizeof (buf) / sizeof (wchar_t));
wcscat (buf, L"\\ntdll.dll");
}
}
#endif
windows_solib *so = &windows_process.solibs.emplace_back ();
so->load_addr = load_addr;
so->original_name = name;
#ifndef __CYGWIN__
so->name = buf;
#else
if (buf[0])
{
char cname[SO_NAME_MAX_PATH_SIZE];
cygwin_conv_path (CCP_WIN_W_TO_POSIX, buf, cname,
SO_NAME_MAX_PATH_SIZE);
so->name = cname;
}
else
{
char *rname = realpath (name, NULL);
if (rname && strlen (rname) < SO_NAME_MAX_PATH_SIZE)
{
so->name = rname;
free (rname);
}
else
{
warning (_("dll path for \"%s\" too long or inaccessible"), name);
so->name = so->original_name;
}
}
/* Record cygwin1.dll .text start/end. */
size_t len = sizeof ("/cygwin1.dll") - 1;
if (so->name.size () >= len
&& strcasecmp (so->name.c_str () + so->name.size () - len,
"/cygwin1.dll") == 0)
{
asection *text = NULL;
gdb_bfd_ref_ptr abfd (gdb_bfd_open (so->name.c_str(), "pei-i386"));
if (abfd == NULL)
return so;
if (bfd_check_format (abfd.get (), bfd_object))
text = bfd_get_section_by_name (abfd.get (), ".text");
if (!text)
return so;
/* The symbols in a dll are offset by 0x1000, which is the
offset from 0 of the first byte in an image - because of the
file header and the section alignment. */
windows_process.cygwin_load_start = (CORE_ADDR) (uintptr_t) ((char *)
load_addr + 0x1000);
windows_process.cygwin_load_end = windows_process.cygwin_load_start +
bfd_section_size (text);
}
#endif
return so;
}
/* See nat/windows-nat.h. */
void
windows_per_inferior::handle_load_dll (const char *dll_name, LPVOID base)
{
windows_solib *solib = windows_make_so (dll_name, base);
DEBUG_EVENTS ("Loading dll \"%s\" at %s.", solib->name.c_str (),
host_address_to_string (solib->load_addr));
}
/* See nat/windows-nat.h. */
void
windows_per_inferior::handle_unload_dll ()
{
LPVOID lpBaseOfDll = current_event.u.UnloadDll.lpBaseOfDll;
auto iter = std::remove_if (windows_process.solibs.begin (),
windows_process.solibs.end (),
[&] (windows_solib &lib)
{
if (lib.load_addr == lpBaseOfDll)
{
DEBUG_EVENTS ("Unloading dll \"%s\".", lib.name.c_str ());
return true;
}
return false;
});
if (iter != windows_process.solibs.end ())
{
windows_process.solibs.erase (iter, windows_process.solibs.end ());
return;
}
/* We did not find any DLL that was previously loaded at this address,
so register a complaint. We do not report an error, because we have
observed that this may be happening under some circumstances. For
instance, running 32bit applications on x64 Windows causes us to receive
4 mysterious UNLOAD_DLL_DEBUG_EVENTs during the startup phase (these
events are apparently caused by the WOW layer, the interface between
32bit and 64bit worlds). */
complaint (_("dll starting at %s not found."),
host_address_to_string (lpBaseOfDll));
}
/* Clear list of loaded DLLs. */
static void
windows_clear_solib (void)
{
windows_process.solibs.clear ();
}