summaryrefslogtreecommitdiff
path: root/overlay/mgdb/gdb/winnt-nat.c
blob: bf997c9379316ae47502ca9064dcbd13dd961bc0 (plain)
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
/***************************************************************/
/*  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_error(msg) \
	error("%s(): %s.",__FUNCTION__,msg)

#define winnt_perror(msg,pid) \
	error("%s(): %s (pid %d).",__FUNCTION__,msg,pid)

struct winnt_process {
	int	pfd;
	pid_t	pid;
	pid_t	syspid;
	pid_t	systid;
	pid_t	flags;
};

static size_t          pcnt;
static winnt_process * plist;
static char            outbuf[256];

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 int winnt_pfd_from_pid (pid_t pid)
{
	winnt_process * pdbg;
	winnt_process * pcap;

	for (pdbg=plist, pcap=&plist[pcnt]; pdbg<pcap; pdbg++)
		if (pdbg->pid == pid)
			return pdbg->pfd;

	return -1;
}

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 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);
}

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);

	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);

		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_abandon (struct target_ops * t, winnt_process * pidinfo)
{
	inferior_ptid = null_ptid;

	x86_cleanup_dregs();
	inf_child_maybe_unpush_target(t);

	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(t,pidinfo);
}

static ptid_t winnt_wait(
	struct target_ops * t,
	ptid_t ptid,
	struct target_waitstatus * waitstatus,
	int target_options)
{
	struct __dbg_event      dbgevent;
	struct pollfd           pollfdbuf[512];
	struct pollfd *         pfds;
	struct pollfd *         pfd;
	nfds_t                  nfds,i;

	if (pcnt*sizeof(*pfds) < sizeof(pollfdbuf))
		pfds = pollfdbuf;
	else if (!(pfds = (struct pollfd *)calloc(pcnt,sizeof(*pfds))))
		return null_ptid;

	nfds = (ptid.pid > 0)
		? winnt_poll_one_init(pfds,ptid.pid)
		: winnt_poll_init(pfds);

	if (poll(pfds,nfds,-1) < 0)
		return null_ptid;

	for (i=0, pfd=0; i<nfds && !pfd; i++)
		if (pfds[i].revents & POLLIN)
			pfd = &pfds[i];

	if (!pfd)
		return null_ptid;

	if (__dbg_event_query_one(pfd->fd,&dbgevent) < 0)
		return null_ptid;

	return ptid_build(
		dbgevent.cpid ? dbgevent.cpid : dbgevent.syspid,
		0,
		dbgevent.ctid ? dbgevent.ctid : dbgevent.systid);
}

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(t,pidinfo);
}

static void winnt_mourn_inferior (struct target_ops * t)
{
	/* update plist, etc. */
}

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:%d",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;

	x86_use_watchpoints(t);

	return t;
}



extern initialize_file_ftype _initialize_winnt_nat;

void
_initialize_winnt_nat(void)
{
	add_target(winnt_target_alloc());
}