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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
|
/***************************************************************/
/* mgdb: midipix-specific bits for gdb */
/* Copyright (C) 2019 Z. Gilboa */
/* Released under GPLv2 and GPLv3; see COPYING.MGDB. */
/***************************************************************/
#include "defs.h"
#include "osabi.h"
#include "config.h"
#include "target.h"
#include "main.h"
#include "utils.h"
#include "inferior.h"
#include "gdbcore.h"
#include "gdbthread.h"
#include "inf-child.h"
#include "xml-support.h"
#include "winnt-nat.h"
#include "x86-nat.h"
#include <poll.h>
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <pthread.h>
#include <sys/cmd.h>
#include <sys/debug.h>
#include <perk/perk.h>
#define WINNT_OUTBUF_SIZE 65536
#define X86_DR_RTM_MASK (1 << 16)
#define X86_EFLAGS_TRACE (1 << 8)
#define winnt_error(msg) \
error("%s(): %s.",__FUNCTION__,msg)
#define winnt_perror(msg,pid) \
error("%s(): %s (pid %d).",__FUNCTION__,msg,pid)
#define WINNT_W32_EXCEPTION_MASK ((0xe0 << 24) | ('m' << 16) | ('s' << 8) | ('c' << 0))
#define WINNT_ZED_EXCEPTION_MASK ((0x2d << 24) | ('Z' << 16) | ('E' << 8) | ('D' << 0))
#define WINNT_GCC_EXCEPTION_MASK ((0x20 << 24) | ('G' << 16) | ('C' << 8) | ('C' << 0))
#define WINNT_MTN_EXCEPTION_MASK ((0x40 << 24) | ('m' << 16) | (0x13 << 8) | (0x88 << 0))
static size_t pcnt;
static winnt_process * plist;
static char * outbuf;
static struct pe_driver_ctx * pe_dctx;
static struct winnt_exception_filter winnt_exception_filters[] = {
{WINNT_W32_EXCEPTION_MASK, WINNT_EXCEPTION_FIRST_CHANCE, 0},
{WINNT_ZED_EXCEPTION_MASK, WINNT_EXCEPTION_FIRST_CHANCE, 0},
{WINNT_GCC_EXCEPTION_MASK, WINNT_EXCEPTION_FIRST_CHANCE, 0},
{WINNT_MTN_EXCEPTION_MASK, WINNT_EXCEPTION_FIRST_CHANCE, 0},
{0, WINNT_EXCEPTION_FIRST_CHANCE, "sysfer.dll"},
{0, 0, 0},
};
static winnt_process * winnt_plist_expand (void)
{
struct winnt_process * pnew;
struct winnt_process * pold;
size_t idx;
if (!(pnew = (struct winnt_process *)calloc(pcnt+32, sizeof(*pnew))))
return 0;
memcpy(
pnew,plist,
pcnt*sizeof(*plist));
pold = plist;
plist = pnew;
pcnt += 32;
free(pold);
return pnew;
}
static int winnt_plist_add (pid_t pid, int pfd)
{
struct winnt_process * pdbg;
struct winnt_process * pcap;
for (pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++) {
if (!pdbg->pid) {
pdbg->pid = pid;
pdbg->pfd = pfd;
return 0;
}
}
return winnt_plist_expand()
? winnt_plist_add(pid,pfd)
: -1;
}
static void winnt_free_modules(struct winnt_process * pidinfo)
{
struct __dbg_module_info * module;
struct __dbg_module_info * modules_base;
struct __dbg_module_info * modules_cap;
if (!pidinfo->modules)
return;
modules_base = pidinfo->modules;
modules_cap = &modules_base[pidinfo->nmodules];
for (module=modules_base; module<modules_cap; module++) {
if (module->module_name)
free(module->module_name);
if (module->module_ctx)
pe_free_unit_ctx((pe_unit_ctx *)module->module_ctx);
}
if (pidinfo->solibs)
free(pidinfo->solibs);
free(pidinfo->modules);
pidinfo->modules = 0;
pidinfo->nmodules = 0;
pidinfo->solibs = 0;
}
static void winnt_free_threads(struct winnt_process * pidinfo)
{
struct winnt_thread * thread;
struct winnt_thread * next;
next = pidinfo->threads;
for (thread=next; thread; thread=next) {
next = thread->next;
free(thread);
}
}
static void winnt_plist_remove (pid_t pid)
{
struct winnt_process * pdbg;
struct winnt_process * pcap;
for (pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++) {
if (pdbg->pid == pid) {
close(pdbg->pfd);
winnt_free_modules(pdbg);
winnt_free_threads(pdbg);
memset(pdbg,0,sizeof(*pdbg));
return;
}
}
}
static struct winnt_process * winnt_process_record(pid_t pid)
{
winnt_process * pdbg;
winnt_process * pcap;
if (!pid)
return 0;
for (pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++)
if (pdbg->pid == pid)
return pdbg;
return 0;
}
static ptid_t winnt_get_ada_task_ptid (struct target_ops * t, long lwp, long tid)
{
ptid_t ptid;
(void)t;
ptid.pid = inferior_ptid.pid;
ptid.lwp = 0;
ptid.tid = lwp;
return ptid;
}
static char * winnt_pid_to_exec_file (struct target_ops * t, int pid)
{
ssize_t ret;
struct winnt_process * pidinfo;
if (!(pidinfo = winnt_process_record(pid)))
winnt_perror("internal error: record not found",pid);
ret = __dbg_info_get(
pidinfo->pfd,0,
__DBG_INFO_IMAGE_RPATH,
outbuf,WINNT_OUTBUF_SIZE);
if (ret < 0)
ret = __dbg_info_get(
pidinfo->pfd,0,
__DBG_INFO_REPORTED_IMAGE_RPATH,
outbuf,WINNT_OUTBUF_SIZE);
if (ret < 0)
outbuf[0] = 0;
return outbuf;
}
static void winnt_get_modules(struct winnt_process * pidinfo)
{
void * addr;
char * xmlstr;
char * mark;
char * text;
struct pe_unit_ctx * uctx;
struct pe_meta_sec_hdr * sechdr;
struct __dbg_module_info * module;
struct __dbg_module_info * modules;
struct __dbg_module_info * modules_cap;
ssize_t nbytes;
size_t nmodules;
size_t buflen;
int secidx;
winnt_free_modules(pidinfo);
if (!(addr = calloc(2048,sizeof(*modules))))
return;
nbytes = __dbg_info_get(
pidinfo->pfd,0,
__DBG_INFO_CACHED_MODULE_LIST,
addr,2048*sizeof(*modules));
if (nbytes < 0) {
free(addr);
return;
};
nmodules = nbytes / sizeof(*modules);
modules = (struct __dbg_module_info *)calloc(nmodules,sizeof(*modules));
if (!modules) {
free(addr);
return;
}
memcpy(modules,addr,nmodules*sizeof(*modules));
free(addr);
buflen = 64;
module = modules;
modules_cap = &modules[nmodules];
for (; module<modules_cap; ) {
nbytes = __dbg_info_get(
pidinfo->pfd,module->module_key,
__DBG_INFO_MODULE_RPATH,
outbuf,WINNT_OUTBUF_SIZE);
if (nbytes > 0) {
module->module_name = strdup(outbuf);
xmlstr = xml_escape_text(outbuf);
if (xmlstr) {
buflen += strlen(xmlstr) + 128;
xfree(xmlstr);
}
pe_get_unit_ctx(
pe_dctx,
module->module_name,
(pe_unit_ctx **)&module->module_ctx);
}
module++;
}
module = modules;
pidinfo->modules = modules;
pidinfo->nmodules = nmodules;
if (!(pidinfo->solibs = (char *)calloc(1,buflen)))
return;
mark = pidinfo->solibs;
mark += sprintf(mark,"<library-list>\n");
for (; module<modules_cap; ) {
uctx = 0;
text = 0;
if (module->module_name) {
if ((xmlstr = xml_escape_text(module->module_name))) {
mark += sprintf(mark,"<library name=");
mark += sprintf(mark,"\"%s\">",xmlstr);
xfree(xmlstr);
}
/* text */
if ((uctx = (pe_unit_ctx *)module->module_ctx)) {
secidx = pe_get_named_section_index(
uctx->meta,".text");
if (secidx >= 0) {
sechdr = &uctx->meta->m_sectbl[secidx];
text = (char *)module->module_base;
text += sechdr->sh_virtual_addr;
}
}
mark += sprintf(mark,"<segment address=");
mark += sprintf(mark,"\"%p\"/>",(void *)text);
mark += sprintf(mark,"</library>");
}
module++;
}
mark += sprintf(mark,"</library-list>\n");
}
static void winnt_mourn_inferior (struct target_ops * t)
{
if (inferior_ptid.pid)
winnt_plist_remove(inferior_ptid.pid);
x86_cleanup_dregs();
inf_child_maybe_unpush_target(t);
inf_child_mourn_inferior(t);
}
static void winnt_abandon (winnt_process * pidinfo)
{
inferior_ptid = null_ptid;
winnt_plist_remove(pidinfo->pid);
}
ssize_t winnt_xfer_solibs(
struct winnt_process * pidinfo,
gdb_byte * readbuf,
ULONGEST offset,
ULONGEST len)
{
size_t buflen;
if (!pidinfo->solibs)
return -1;
buflen = strlen(pidinfo->solibs);
if (offset >= buflen)
return 0;
if (len > (buflen - offset))
len = buflen - offset;
memcpy(readbuf,pidinfo->solibs + offset,len);
return len;
}
static enum target_xfer_status winnt_xfer_partial(
struct target_ops * t,
enum target_object object,
const char * annex,
gdb_byte * readbuf,
const gdb_byte * writebuf,
ULONGEST offset,
ULONGEST len,
ULONGEST * nxfered)
{
ssize_t ret;
struct winnt_process * pidinfo;
pidinfo = winnt_process_record(inferior_ptid.pid);
if (readbuf && writebuf)
winnt_error("internal error: both readbuf and writebuf are non-null.");
if (!readbuf && !writebuf)
winnt_error("internal error: both readbuf and writebuf are null.");
if (!nxfered)
winnt_error("internal error: nxfered is null.");
switch (object) {
case TARGET_OBJECT_MEMORY:
if (!pidinfo)
return TARGET_XFER_E_IO;
ret = (readbuf)
? __dbg_vm_read(pidinfo->pfd,readbuf,len,offset)
: __dbg_vm_write(pidinfo->pfd,writebuf,len,offset);
break;
case TARGET_OBJECT_LIBRARIES:
if (!pidinfo)
return TARGET_XFER_E_IO;
ret = winnt_xfer_solibs(pidinfo,readbuf,offset,len);
break;
default:
ret = t->beneath->to_xfer_partial(
t,object,annex,
readbuf,writebuf,
offset,len,nxfered);
break;
}
if (ret == 0)
return TARGET_XFER_EOF;
else if (ret < 0)
return TARGET_XFER_E_IO;
*nxfered = ret;
return TARGET_XFER_OK;
}
static int winnt_thread_alive (struct target_ops * t, ptid_t ptid)
{
(void)t;
(void)ptid;
return true;
}
static void winnt_interrupt (struct target_ops * t, ptid_t ptid)
{
(void)t;
(void)ptid;
}
static void winnt_files_info (struct target_ops * t)
{
(void)t;
}
static nfds_t winnt_poll_one_init (struct pollfd * pfds, pid_t pid)
{
winnt_process * pdbg;
winnt_process * pcap;
for (pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++) {
if (pdbg->pid == pid) {
pfds->fd = pdbg->pfd;
pfds->events = POLLIN;
return 1;
}
}
return 0;
}
static nfds_t winnt_poll_init (struct pollfd * pfds)
{
winnt_process * pdbg;
winnt_process * pcap;
struct pollfd * pfd;
for (pfd=pfds, pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++) {
if (pdbg->pid) {
pfd->fd = pdbg->pfd;
pfd->events = POLLIN;
pfd++;
}
}
return pfd - pfds;
}
static void winnt_close (struct target_ops * t)
{
}
static void winnt_xclose (struct target_ops * t)
{
}
static void winnt_set_thread_context_state(struct winnt_thread * thread, int state)
{
thread->regctx.uc_context_flags = state;
}
static void winnt_add_thread(int pfd, ptid_t ptid)
{
pid_t pid;
struct winnt_process * pidinfo;
struct winnt_thread * thread;
mcontext_t * tctx;
pid = ptid_get_pid(ptid);
pidinfo = winnt_process_record(pid);
if (!(thread = (struct winnt_thread *)calloc(1,sizeof(*thread))))
winnt_perror("could not allocate thread record.",pid);
if (__dbg_regs_fetch(pfd,ptid.tid,&thread->regctx) < 0)
winnt_perror("could not fetch registers for thread.",pid);
tctx = &thread->regctx;
tctx->uc_dr0 = pidinfo->dr_cache[0];
tctx->uc_dr1 = pidinfo->dr_cache[1];
tctx->uc_dr2 = pidinfo->dr_cache[2];
tctx->uc_dr3 = pidinfo->dr_cache[3];
tctx->uc_dr6 = X86_DR_RTM_MASK;
tctx->uc_dr7 = pidinfo->dr_cache[7];
winnt_set_thread_context_state(
thread,
WINNT_THREAD_CONTEXT_READY);
if (__dbg_regs_store(pfd,ptid.tid,tctx) < 0)
winnt_perror("could not store registers for thread.",pid);
thread->next = pidinfo->threads;
pidinfo->threads = thread;
thread->process = pidinfo;
thread->tid = ptid.tid;
add_thread(ptid);
}
static struct winnt_thread * winnt_get_thread(struct winnt_process * pidinfo, pid_t tid)
{
struct winnt_thread * thread;
for (thread=pidinfo->threads; thread; thread=thread->next)
if (thread->tid == tid)
return thread;
return 0;
}
static struct winnt_thread * winnt_get_thread_from_ptid(ptid_t ptid)
{
struct winnt_process * pidinfo;
if ((pidinfo = winnt_process_record(ptid.pid)))
return winnt_get_thread(pidinfo,ptid.tid);
return 0;
}
static void winnt_delete_thread(struct __dbg_event * event)
{
ptid_t ptid;
struct winnt_process * process;
struct winnt_thread * thread;
struct winnt_thread * prev;
ptid.pid = event->syspid;
ptid.tid = event->systid;
ptid.lwp = 0;
delete_thread(ptid);
if (!(thread = winnt_get_thread_from_ptid(ptid)))
return;
process = thread->process;
if (thread == process->threads) {
process->threads = thread->next;
} else {
prev = process->threads;
for (; prev->next != thread; )
prev = prev->next;
prev->next = thread->next;
}
if (process->threads)
if (inferior_ptid.pid == ptid.pid)
if (inferior_ptid.tid == ptid.tid)
inferior_ptid.tid = process->threads->tid;
free(thread);
}
static void winnt_respond (int pfd, struct __dbg_event * event, int response)
{
ptid_t ptid;
event->eresponse = response;
switch (event->evttype) {
case __DBG_STATE_CREATE_PROCESS:
ptid = ptid_build(event->syspid,0,event->systid);
winnt_add_thread(pfd,ptid);
break;
case __DBG_STATE_CREATE_THREAD:
ptid = ptid_build(event->syspid,0,event->systid);
winnt_add_thread(pfd,ptid);
break;
case __DBG_STATE_EXIT_THREAD:
winnt_delete_thread(event);
break;
case __DBG_STATE_EXIT_PROCESS:
target_terminal_ours();
break;
default:
break;
}
if (__dbg_event_respond(pfd,event) < 0)
winnt_perror("failed to respond to debug event",pfd);
event->syspid = 0;
event->systid = 0;
}
static void winnt_prepare (struct target_ops * t, pid_t pid, int pfd, int attached)
{
int ret;
struct inferior * cinf;
struct __dbg_event * event;
struct winnt_process * pidinfo;
if (!target_is_pushed(t))
push_target(t);
disable_breakpoints_in_shlibs();
clear_proceed_status(0);
init_wait_for_inferior();
if (!(cinf = current_inferior()))
winnt_error("failed to obtain current inferior");
if (!(pidinfo = winnt_process_record(pid)))
winnt_perror("internal error: record not found",pid);
event = &pidinfo->event;
inferior_ptid = pid_to_ptid (pid);
cinf->attach_flag = attached;
inferior_appeared(cinf,pid);
init_thread_list();
target_terminal_init();
target_terminal_inferior();
while (1) {
do {
ret = __dbg_event_acquire(pfd,event);
} while ((ret < 0) && (errno == EINTR));
if (ret < 0)
winnt_perror("failed to acquire preliminary debug event",pid);
if ((event->evttype == __DBG_STATE_CREATE_PROCESS) && !attached)
if (__dbg_resume_thread(pfd,event->systid) < 0)
winnt_perror("failed to resume first thread",pid);
inferior_ptid.pid = event->syspid;
inferior_ptid.tid = event->systid;
switch (event->evttype) {
case __DBG_STATE_EXCEPTION:
case __DBG_STATE_BREAKPOINT:
case __DBG_STATE_SINGLE_STEP:
pidinfo->syspid = event->syspid;
pidinfo->systid = event->systid;
target_terminal_ours();
return;
case __DBG_STATE_EXIT_PROCESS:
winnt_respond(pfd,event,__DBG_RESPONSE_CONTINUE);
winnt_mourn_inferior(t);
case __DBG_STATE_CREATE_PROCESS:
case __DBG_STATE_CREATE_THREAD:
case __DBG_STATE_IDLE:
case __DBG_STATE_REPLY_PENDING:
case __DBG_STATE_DLL_LOAD:
case __DBG_STATE_DLL_UNLOAD:
case __DBG_STATE_EXIT_THREAD:
winnt_respond(pfd,event,__DBG_RESPONSE_CONTINUE);
}
}
}
static void winnt_fetch_registers (
struct target_ops * t,
struct regcache * rcache,
int regnum)
{
pid_t tid;
struct winnt_thread * thread;
tid = inferior_ptid.tid;
if (!(thread = winnt_get_thread_from_ptid(inferior_ptid)))
winnt_perror("internal error: thread record not found",tid);
if (sizeof(uintptr_t) == 8)
amd64_winnt_fetch_registers(
rcache,regnum,
thread);
else
i386_winnt_fetch_registers(
rcache,regnum,
thread);
}
static void winnt_store_registers (
struct target_ops * t,
struct regcache * rcache,
int regnum)
{
pid_t tid;
struct winnt_thread * thread;
tid = inferior_ptid.tid;
if (!(thread = winnt_get_thread_from_ptid(inferior_ptid)))
winnt_perror("internal error: thread record not found",tid);
if (sizeof(uintptr_t) == 8)
amd64_winnt_store_registers(
rcache,regnum,
thread);
else
i386_winnt_store_registers(
rcache,regnum,
thread);
}
static void winnt_attach (struct target_ops * t, const char * args, int from_tty)
{
pid_t pid;
int pfd;
if ((pid = parse_pid_to_attach(args)) < 0) {
winnt_error ("cannot parse pid to attach");
}
if ((pfd = __dbg_attach(pid)) < 0) {
winnt_perror ("cannot attach to process",pid);
}
if (__dbg_rbreak(pfd) < 0) {
__dbg_detach(pfd);
close(pfd);
winnt_perror ("could not issue a breakpoint in process",pid);
}
if (winnt_plist_add(pid,pfd) < 0) {
__dbg_detach(pfd);
close(pfd);
winnt_error ("could not expand debuggee list");
}
winnt_prepare(t,pid,pfd,from_tty);
winnt_get_modules(winnt_process_record(pid));
}
static void winnt_detach (struct target_ops * t, const char * args, int from_tty)
{
pid_t pid;
winnt_process * pidinfo;
if ((pid = ptid_get_pid(inferior_ptid)) < 0)
winnt_error ("cannot determine pid to detach");
if (!(pidinfo = winnt_process_record(pid)))
winnt_perror ("debuggee record does not exist",pid);
if (__dbg_detach(pidinfo->pfd) < 0)
winnt_perror ("could not detach from process",pid);
if (close(pidinfo->pfd) < 0)
winnt_perror ("failed to close process file descriptor",pid);
detach_inferior(pidinfo->syspid);
winnt_abandon(pidinfo);
winnt_mourn_inferior(t);
exec_file_clear(from_tty);
}
static int winnt_exception_filter_match_code(
struct __dbg_event * event,
struct winnt_exception_filter * filter)
{
if (!filter->exception_code)
return 1;
if (!event->exception_record)
return 0;
if (filter->exception_code != event->exception_record->exception_code)
return 0;
if (filter->exception_flags & WINNT_EXCEPTION_FIRST_CHANCE)
if (event->exception_priority)
return 1;
if (filter->exception_flags & WINNT_EXCEPTION_SECOND_CHANCE)
if (!event->exception_priority)
return 1;
return 0;
}
static int winnt_exception_filter_match_module(
struct __dbg_event * event,
struct winnt_exception_filter * filter,
struct winnt_process* pidinfo)
{
struct __dbg_module_info * module;
struct __dbg_module_info * module_cap;
struct winnt_thread * thread;
struct pe_unit_ctx * uctx;
struct pe_meta_sec_hdr * sechdr;
int secidx;
char * addr;
char * mark;
char * text;
char * text_cap;
thread = winnt_get_thread(pidinfo,event->systid);
addr = (char *)thread->regctx.uc_rip;
module_cap = &pidinfo->modules[pidinfo->nmodules];
for (module=pidinfo->modules; module<module_cap; module++) {
if ((uctx = (struct pe_unit_ctx *)module->module_ctx)) {
secidx = pe_get_named_section_index(
uctx->meta,".text");
if (secidx >= 0) {
mark = 0;
sechdr = &uctx->meta->m_sectbl[secidx];
text = (char *)module->module_base;
text += sechdr->sh_virtual_addr;
text_cap = text + sechdr->sh_virtual_size;
if ((addr >= text) && (addr < text_cap))
mark = strrchr(module->module_name,'/');
if (mark && !filter->exception_module)
return 1;
if (mark && filter->exception_module)
if (!(strcmp(++mark,filter->exception_module)))
return 1;
}
}
}
return 0;
}
static int winnt_exception_filter_match(struct __dbg_event * event)
{
struct winnt_process * pidinfo;
struct winnt_exception_filter * filter = winnt_exception_filters;
switch (event->evttype) {
case __DBG_STATE_EXCEPTION:
case __DBG_STATE_BREAKPOINT:
case __DBG_STATE_SINGLE_STEP:
break;
default:
return 0;
}
if (!(pidinfo = winnt_process_record(event->syspid)))
return 0;
for (; filter->exception_flags; filter++)
if (winnt_exception_filter_match_code(event,filter))
if (winnt_exception_filter_match_module(event,filter,pidinfo))
return 1;
return 0;
}
static enum gdb_signal winnt_dbg_signal(struct __dbg_event * event)
{
switch (event->exception_record->exception_code) {
case WINNT_STATUS_ACCESS_VIOLATION:
return GDB_SIGNAL_SEGV;
default:
return GDB_SIGNAL_UNKNOWN;
}
}
static struct __dbg_event * winnt_wait_event(ptid_t ptid)
{
int ret;
struct __dbg_event event;
struct winnt_process * pdbg;
struct winnt_process * pcap;
struct winnt_process * pidinfo;
struct winnt_thread * thread;
struct pollfd pollfdbuf[512];
struct pollfd * pfds;
struct pollfd * pfd;
nfds_t nfds,i;
if (ptid.pid < 0)
for (pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++)
if (pdbg->event.systid)
return &pdbg->event;
if (ptid.pid > 0) {
if (!(pidinfo = winnt_process_record(event.syspid)))
winnt_perror("internal error: record not found",event.syspid);
if (pidinfo->event.systid)
return &pidinfo->event;
}
if (pcnt*sizeof(*pfds) < sizeof(pollfdbuf))
pfds = pollfdbuf;
else if (!(pfds = (struct pollfd *)calloc(pcnt,sizeof(*pfds))))
return 0;
nfds = (ptid.pid > 0)
? winnt_poll_one_init(pfds,ptid.pid)
: winnt_poll_init(pfds);
do {
ret = poll(pfds,nfds,-1);
} while ((ret < 0) && (errno == EINTR));
for (i=0, pfd=0; i<nfds && !pfd; i++)
if (pfds[i].revents & POLLIN)
pfd = &pfds[i];
if (!pfd)
return 0;
if (__dbg_event_acquire(pfd->fd,&event) < 0)
return 0;
if (!(pidinfo = winnt_process_record(event.syspid)))
winnt_perror("internal error: record not found",event.syspid);
memcpy(&pidinfo->event,&event,sizeof(event));
for (thread=pidinfo->threads; thread; thread=thread->next) {
__dbg_regs_fetch(
pidinfo->pfd,
thread->tid,
&thread->regctx);
winnt_set_thread_context_state(
thread,
WINNT_THREAD_CONTEXT_READY);
}
return &pidinfo->event;
}
static ptid_t winnt_wait(
struct target_ops * t,
ptid_t ptid,
struct target_waitstatus * waitstatus,
int target_options)
{
int pfd;
struct __dbg_event * event;
struct winnt_process * process;
int fskip;
int eresponse;
target_terminal_ours();
do {
if (!(event = winnt_wait_event(ptid)))
return null_ptid;
fskip = winnt_exception_filter_match(event)
|| (event->evtattr & __DBG_EVENT_ATTR_INTERNAL_BREAKPOINT);
if (fskip) {
if (!(process = winnt_process_record(event->syspid)))
return null_ptid;
if (event->evtattr & __DBG_EVENT_ATTR_INTERNAL_BREAKPOINT)
eresponse = __DBG_RESPONSE_CONTINUE;
else if (event->exception_priority)
eresponse = __DBG_RESPONSE_EXCEPTION_NOT_HANDLED;
else
eresponse = __DBG_RESPONSE_CONTINUE;
winnt_respond(process->pfd,event,eresponse);
}
} while (fskip);
if (!(process = winnt_process_record(event->syspid)))
return null_ptid;
pfd = process->pfd;
inferior_ptid.pid = event->syspid;
inferior_ptid.tid = event->systid;
switch (event->evttype) {
case __DBG_STATE_EXCEPTION:
waitstatus->kind = TARGET_WAITKIND_STOPPED;
waitstatus->value.sig = winnt_dbg_signal(event);
break;
case __DBG_STATE_BREAKPOINT:
case __DBG_STATE_SINGLE_STEP:
waitstatus->kind = TARGET_WAITKIND_STOPPED;
waitstatus->value.sig = GDB_SIGNAL_TRAP;
break;
case __DBG_STATE_CREATE_THREAD:
waitstatus->kind = TARGET_WAITKIND_THREAD_CREATED;
waitstatus->value.integer = event->systid;
winnt_respond(pfd,event,__DBG_RESPONSE_CONTINUE);
break;
case __DBG_STATE_EXIT_THREAD:
waitstatus->kind = TARGET_WAITKIND_THREAD_EXITED;
waitstatus->value.integer = event->systid;
winnt_respond(pfd,event,__DBG_RESPONSE_CONTINUE);
break;
case __DBG_STATE_CREATE_PROCESS:
waitstatus->kind = TARGET_WAITKIND_SPURIOUS;
waitstatus->value.integer = 0;
break;
case __DBG_STATE_EXIT_PROCESS:
waitstatus->kind = TARGET_WAITKIND_EXITED;
waitstatus->value.integer = *event->process_exit_code;
winnt_respond(pfd,event,__DBG_RESPONSE_CONTINUE);
break;
case __DBG_STATE_DLL_LOAD:
waitstatus->kind = TARGET_WAITKIND_LOADED;
waitstatus->value.integer = 0;
winnt_get_modules(process);
break;
case __DBG_STATE_DLL_UNLOAD:
waitstatus->kind = TARGET_WAITKIND_LOADED;
waitstatus->value.integer = 0;
winnt_get_modules(process);
break;
case __DBG_STATE_IDLE:
case __DBG_STATE_REPLY_PENDING:
waitstatus->kind = TARGET_WAITKIND_SPURIOUS;
waitstatus->value.integer = 0;
break;
}
return inferior_ptid;
}
static void winnt_resume_one(ptid_t ptid, int step, enum gdb_signal sig)
{
pid_t tid;
struct winnt_process * process;
struct winnt_thread * thread;
mcontext_t * tctx;
if ((tid = ptid.tid) <= 0)
if ((process = winnt_process_record(ptid.pid)))
tid = process->systid;
if (!(thread = winnt_get_thread_from_ptid(ptid)))
winnt_perror("internal error: thread record not found",tid);
process = thread->process;
tctx = &thread->regctx;
if (process->dr_state) {
tctx->uc_dr0 = process->dr_cache[0];
tctx->uc_dr1 = process->dr_cache[1];
tctx->uc_dr2 = process->dr_cache[2];
tctx->uc_dr3 = process->dr_cache[3];
tctx->uc_dr6 = X86_DR_RTM_MASK;
tctx->uc_dr7 = process->dr_cache[7];
}
if (step && !(tctx->uc_eflags & X86_EFLAGS_TRACE)) {
tctx->uc_eflags |= X86_EFLAGS_TRACE;
winnt_set_thread_context_state(
thread,
WINNT_THREAD_CONTEXT_DIRTY);
}
if (process->dr_state || tctx->uc_context_flags)
__dbg_regs_store(process->pfd,tid,tctx);
if (tid == process->event.systid) {
process->event.eresponse = __DBG_RESPONSE_CONTINUE;
__dbg_event_respond(
process->pfd,
&process->event);
while (__dbg_resume_thread(process->pfd,tid) > 1)
(void)0;
process->event.syspid = 0;
process->event.systid = 0;
}
}
static void winnt_resume_process(pid_t pid, int step, enum gdb_signal sig)
{
struct winnt_process * pdbg;
struct winnt_thread * thread;
ptid_t ptid;
if (!(pdbg = winnt_process_record(pid)))
return;
for (thread=pdbg->threads; thread; thread=thread->next) {
ptid.pid = pdbg->syspid;
ptid.tid = thread->tid;
ptid.lwp = 0;
(thread->tid == inferior_ptid.tid)
? winnt_resume_one(ptid,step,sig)
: winnt_resume_one(ptid,0,sig);
}
}
static void winnt_resume_all(int step, enum gdb_signal sig)
{
struct winnt_process * pdbg;
struct winnt_process * pcap;
struct winnt_thread * thread;
ptid_t ptid;
for (pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++) {
if (pdbg->pfd && (thread = pdbg->threads)) {
for (; thread; ) {
ptid.pid = pdbg->syspid;
ptid.tid = thread->tid;
ptid.lwp = 0;
(thread->tid == inferior_ptid.tid)
? winnt_resume_one(ptid,step,sig)
: winnt_resume_one(ptid,0,sig);
thread = thread->next;
}
}
}
}
static void winnt_resume (
struct target_ops * t,
ptid_t ptid, int step,
enum gdb_signal sig)
{
ptid_equal(ptid,minus_one_ptid)
? winnt_resume_all(step,sig)
: ptid.tid
? winnt_resume_one(ptid,step,sig)
: winnt_resume_process(ptid.pid,step,sig);
}
static void winnt_kill (struct target_ops * t)
{
struct inferior * cinf;
struct winnt_process * pidinfo;
if (!(cinf = current_inferior()))
winnt_error("failed to obtain current inferior");
if (!(pidinfo = winnt_process_record(cinf->pid)))
winnt_perror("internal error: record not found",cinf->pid);
if (__dbg_kill(pidinfo->pfd) < 0)
winnt_perror("failed to kill current inferior",cinf->pid);
winnt_abandon(pidinfo);
winnt_mourn_inferior(t);
}
static void winnt_create_inferior(
struct target_ops * t,
char * exec_file,
char * args,
char ** envp,
int from_tty)
{
int pfd;
pid_t pid;
size_t arglen;
char * argbuf;
char ** argv;
/* validate */
if (!exec_file)
winnt_error("Executable not set; use `target exec'.");
/* assume no folded white space, add final null termination */
arglen = strlen(args);
arglen++;
if (!(argbuf = (char *)calloc(2*arglen,1)))
winnt_error("Failed to allocate argument string buffer.");
/* extra pointer for exec_file (argv[0]) */
arglen++;
if (!(argv = (char **)calloc(arglen,sizeof(char *))))
winnt_error("Failed to allocate argument vector buffer.");
arglen--;
/* argv */
argv[0] = exec_file;
if (__cmd_args_to_argv(args,argbuf,2*arglen,&argv[1],arglen) < 0)
winnt_error("Failed to parse command-line arguments.");
/* spawn */
if ((pfd = __dbg_spawn(exec_file,argv,envp,0)) < 0)
winnt_error("Failed to spawn executable.");
/* syspid */
if ((pid = __dbg_query_syspid(pfd)) < 0) {
__dbg_kill(pfd);
close(pfd);
winnt_error("Failed to obtain syspid (should never happen).");
}
/* debuggee list */
if (winnt_plist_add(pid,pfd) < 0) {
__dbg_kill(pfd);
close(pfd);
winnt_error ("could not expand debuggee list");
}
/* init loop */
winnt_prepare(t,pid,pfd,0);
winnt_get_modules(winnt_process_record(pid));
}
static char * winnt_pid_to_str (struct target_ops * t, ptid_t ptid)
{
if (ptid.tid)
sprintf(outbuf,"Thread %d:%ld",ptid.pid,ptid.tid);
else
sprintf(outbuf,"Process %d",ptid.pid);
return outbuf;
}
static int winnt_supports_multi_process(struct target_ops * t)
{
return 1;
}
static int winnt_perk_init(void)
{
char * argv[2];
if (pe_dctx)
return 0;
argv[0] = (char *)get_gdb_program_name();
argv[1] = 0;
return pe_get_driver_ctx(argv,0,0,0,&pe_dctx);
}
#define WINNT_DR_HANDLER(handler) \
winnt_dr_ ## handler
#define WINNT_SET_DR_LOW_HANDLER(handler) \
x86_dr_low.handler = WINNT_DR_HANDLER(handler)
static void winnt_dr_set_addr(int idx, uintptr_t any)
{
pid_t pid;
struct winnt_process * pidinfo;
pid = ptid_get_pid(inferior_ptid);
pidinfo = winnt_process_record(pid);
pidinfo->dr_cache[idx] = any;
pidinfo->dr_state = WINNT_DR_STATE_DIRTY;
}
static uintptr_t winnt_dr_get_addr(int idx)
{
pid_t pid;
struct winnt_process * pidinfo;
pid = ptid_get_pid(inferior_ptid);
pidinfo = winnt_process_record(pid);
return pidinfo->dr_cache[idx];
}
static void winnt_dr_set_control(unsigned long ctrl)
{
winnt_dr_set_addr(7,ctrl);
}
static unsigned long winnt_dr_get_control(void)
{
return winnt_dr_get_addr(7);
}
static unsigned long winnt_dr_get_status(void)
{
return winnt_dr_get_addr(6);
}
static void winnt_init_dr_low(void)
{
WINNT_SET_DR_LOW_HANDLER(set_control);
WINNT_SET_DR_LOW_HANDLER(get_control);
WINNT_SET_DR_LOW_HANDLER(get_status);
WINNT_SET_DR_LOW_HANDLER(set_addr);
WINNT_SET_DR_LOW_HANDLER(get_addr);
x86_set_debug_register_length(sizeof(uintptr_t));
}
static target_ops * winnt_target_alloc (void)
{
target_ops * t = inf_child_target();
t->to_attach_no_wait = 1;
t->to_has_thread_control = 1;
t->to_close = winnt_close;
t->to_xclose = winnt_xclose;
t->to_attach = winnt_attach;
t->to_detach = winnt_detach;
t->to_wait = winnt_wait;
t->to_resume = winnt_resume;
t->to_kill = winnt_kill;
t->to_mourn_inferior = winnt_mourn_inferior;
t->to_create_inferior = winnt_create_inferior;
t->to_pid_to_str = winnt_pid_to_str;
t->to_pid_to_exec_file = winnt_pid_to_exec_file;
t->to_fetch_registers = winnt_fetch_registers;
t->to_store_registers = winnt_store_registers;
t->to_xfer_partial = winnt_xfer_partial;
t->to_files_info = winnt_files_info;
t->to_thread_alive = winnt_thread_alive;
t->to_interrupt = winnt_interrupt;
t->to_get_ada_task_ptid = winnt_get_ada_task_ptid;
t->to_supports_multi_process = winnt_supports_multi_process;
x86_use_watchpoints(t);
winnt_init_dr_low();
if (winnt_perk_init() < 0)
return 0;
return ((outbuf = (char *)calloc(1,WINNT_OUTBUF_SIZE)))
? t : 0;
}
extern initialize_file_ftype _initialize_winnt_nat;
extern void amd64_winnt_init_reg_offsets(void);
void
_initialize_winnt_nat(void)
{
add_target(winnt_target_alloc());
amd64_winnt_init_reg_offsets();
}
|