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
|
/***************************************************************/
/* 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 "utils.h"
#include "inferior.h"
#include "gdbthread.h"
#include "inf-child.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>
#define WINNT_OUTBUF_SIZE 65536
#define winnt_error(msg) \
error("%s(): %s.",__FUNCTION__,msg)
#define winnt_perror(msg,pid) \
error("%s(): %s (pid %d).",__FUNCTION__,msg,pid)
struct regcache;
struct winnt_process {
int pfd;
pid_t pid;
pid_t syspid;
pid_t systid;
pid_t flags;
struct __dbg_event event;
};
static size_t pcnt;
static winnt_process * plist;
static char * outbuf;
extern void amd64_winnt_fetch_registers(
int pfd, struct regcache * rcache,
int regnum, pid_t tid);
extern void i386_winnt_fetch_registers(
int pfd, struct regcache * rcache,
int regnum, pid_t tid);
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_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) {
pdbg->pid = 0;
return;
}
}
}
static struct winnt_process * winnt_process_record(pid_t pid)
{
winnt_process * pdbg;
winnt_process * pcap;
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)
{
(void)t;
(void)pid;
outbuf[0] = 0;
return outbuf;
}
static void winnt_fetch_registers (
struct target_ops * t,
struct regcache * rcache,
int regnum)
{
pid_t pid;
pid_t tid;
struct winnt_process * pidinfo;
pid = inferior_ptid.pid;
tid = inferior_ptid.tid;
if (!(pidinfo = winnt_process_record(pid)))
winnt_perror("internal error: record not found",pid);
if (sizeof(uintptr_t) == 8)
amd64_winnt_fetch_registers(
pidinfo->pfd,
rcache,regnum,
tid);
else
i386_winnt_fetch_registers(
pidinfo->pfd,
rcache,regnum,
tid);
}
static void winnt_store_registers (
struct target_ops * t,
struct regcache * rcache,
int regnum)
{
(void)t;
(void)rcache;
(void)regnum;
}
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)
{
(void)t;
(void)object;
(void)annex;
(void)offset;
(void)len;
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.");
return TARGET_XFER_E_IO;
}
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_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,0);
add_thread(ptid);
ptid = ptid_build(event->syspid,0,event->systid);
add_thread(ptid);
break;
case __DBG_STATE_CREATE_THREAD:
ptid = ptid_build(event->syspid,0,event->systid);
add_thread(ptid);
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);
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 == EAGAIN));
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_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:
case __DBG_STATE_EXIT_PROCESS:
winnt_respond(pfd,event,__DBG_RESPONSE_CONTINUE);
}
}
}
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);
}
static void winnt_mourn_inferior (struct target_ops * t)
{
x86_cleanup_dregs();
inf_child_maybe_unpush_target(t);
inf_child_mourn_inferior(t);
delete_inferior(current_inferior());
}
static void winnt_abandon (winnt_process * pidinfo)
{
inferior_ptid = null_ptid;
winnt_plist_remove(pidinfo->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);
}
static struct __dbg_event * winnt_wait_event(ptid_t ptid)
{
struct __dbg_event event;
struct winnt_process * pdbg;
struct winnt_process * pcap;
struct winnt_process * pidinfo;
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);
if (poll(pfds,nfds,-1) < 0)
return 0;
for (i=0, pfd=0; i<nfds && !pfd; i++)
if (pfds[i].revents & POLLIN)
pfd = &pfds[i];
if (!pfd)
return 0;
if (__dbg_event_query_one(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));
return &pidinfo->event;
}
static ptid_t winnt_wait(
struct target_ops * t,
ptid_t ptid,
struct target_waitstatus * waitstatus,
int target_options)
{
struct __dbg_event * event;
if (!(event = winnt_wait_event(ptid)))
return null_ptid;
switch (event->evttype) {
case __DBG_STATE_EXCEPTION:
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_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:
case __DBG_STATE_EXIT_PROCESS:
waitstatus->kind = TARGET_WAITKIND_SPURIOUS;
waitstatus->value.integer = 0;
break;
}
inferior_ptid.pid = event->syspid;
inferior_ptid.tid = event->systid;
target_terminal_ours();
return inferior_ptid;
}
static void winnt_resume (
struct target_ops * t,
ptid_t ptid, int step,
enum gdb_signal 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(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,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);
}
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 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;
x86_use_watchpoints(t);
return ((outbuf = (char *)calloc(1,WINNT_OUTBUF_SIZE)))
? t : 0;
}
extern initialize_file_ftype _initialize_winnt_nat;
void
_initialize_winnt_nat(void)
{
add_target(winnt_target_alloc());
}
|