]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/go32-nat.c
Fix the year on the following lines:
[thirdparty/binutils-gdb.git] / gdb / go32-nat.c
CommitLineData
e49d4fa6 1/* Native debugging support for Intel x86 running DJGPP.
8e65ff28 2 Copyright 1997, 1999, 2001 Free Software Foundation, Inc.
e49d4fa6
SS
3 Written by Robert Hoehne.
4
c5aa993b 5 This file is part of GDB.
e49d4fa6 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
e49d4fa6 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
e49d4fa6 16
c5aa993b
JM
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
e49d4fa6
SS
21
22#include <fcntl.h>
23
24#include "defs.h"
e49d4fa6 25#include "inferior.h"
03f2053f 26#include "gdb_wait.h"
e49d4fa6
SS
27#include "gdbcore.h"
28#include "command.h"
29#include "floatformat.h"
0fff5247 30#include "buildsym.h"
89dea5aa 31#include "i387-nat.h"
e49d4fa6 32
c5aa993b 33#include <stdio.h> /* required for __DJGPP_MINOR__ */
e49d4fa6
SS
34#include <stdlib.h>
35#include <string.h>
53a5351d 36#include <errno.h>
c2c6d25f 37#include <unistd.h>
53a5351d
JM
38#include <io.h>
39#include <dpmi.h>
e49d4fa6
SS
40#include <debug/v2load.h>
41#include <debug/dbgcom.h>
53a5351d
JM
42#if __DJGPP_MINOR__ > 2
43#include <debug/redir.h>
44#endif
e49d4fa6 45
b83266a0
SS
46#if __DJGPP_MINOR__ < 3
47/* This code will be provided from DJGPP 2.03 on. Until then I code it
48 here */
c5aa993b
JM
49typedef struct
50 {
51 unsigned short sig0;
52 unsigned short sig1;
53 unsigned short sig2;
54 unsigned short sig3;
55 unsigned short exponent:15;
56 unsigned short sign:1;
57 }
58NPXREG;
59
60typedef struct
61 {
62 unsigned int control;
63 unsigned int status;
64 unsigned int tag;
65 unsigned int eip;
66 unsigned int cs;
67 unsigned int dataptr;
68 unsigned int datasel;
69 NPXREG reg[8];
70 }
71NPX;
b83266a0
SS
72
73static NPX npx;
74
c5aa993b
JM
75static void save_npx (void); /* Save the FPU of the debugged program */
76static void load_npx (void); /* Restore the FPU of the debugged program */
b83266a0
SS
77
78/* ------------------------------------------------------------------------- */
79/* Store the contents of the NPX in the global variable `npx'. */
c5aa993b 80/* *INDENT-OFF* */
b83266a0
SS
81
82static void
83save_npx (void)
84{
85 asm ("inb $0xa0, %%al
c5aa993b
JM
86 testb $0x20, %%al
87 jz 1f
88 xorb %% al, %%al
89 outb %% al, $0xf0
90 movb $0x20, %%al
91 outb %% al, $0xa0
92 outb %% al, $0x20
b83266a0 931:
c5aa993b
JM
94 fnsave % 0
95 fwait "
96: "=m" (npx)
97: /* No input */
98: "%eax");
b83266a0 99}
c5aa993b
JM
100
101/* *INDENT-ON* */
102
103
104
105
106
b83266a0
SS
107/* ------------------------------------------------------------------------- */
108/* Reload the contents of the NPX from the global variable `npx'. */
109
110static void
111load_npx (void)
112{
c5aa993b 113asm ("frstor %0":"=m" (npx));
b83266a0 114}
53a5351d
JM
115/* ------------------------------------------------------------------------- */
116/* Stubs for the missing redirection functions. */
117typedef struct {
118 char *command;
119 int redirected;
120} cmdline_t;
121
122void redir_cmdline_delete (cmdline_t *ptr) {ptr->redirected = 0;}
123int redir_cmdline_parse (const char *args, cmdline_t *ptr)
124{
125 return -1;
126}
127int redir_to_child (cmdline_t *ptr)
128{
129 return 1;
130}
131int redir_to_debugger (cmdline_t *ptr)
132{
133 return 1;
134}
135int redir_debug_init (cmdline_t *ptr) { return 0; }
b83266a0
SS
136#endif /* __DJGPP_MINOR < 3 */
137
e49d4fa6
SS
138extern void _initialize_go32_nat (void);
139
53a5351d
JM
140typedef enum { wp_insert, wp_remove, wp_count } wp_op;
141
142/* This holds the current reference counts for each debug register. */
143static int dr_ref_count[4];
144
e49d4fa6
SS
145extern char **environ;
146
147#define SOME_PID 42
148
e49d4fa6 149static int prog_has_started = 0;
c5aa993b
JM
150static void go32_open (char *name, int from_tty);
151static void go32_close (int quitting);
152static void go32_attach (char *args, int from_tty);
153static void go32_detach (char *args, int from_tty);
154static void go32_resume (int pid, int step, enum target_signal siggnal);
155static int go32_wait (int pid, struct target_waitstatus *status);
156static void go32_fetch_registers (int regno);
157static void store_register (int regno);
158static void go32_store_registers (int regno);
159static void go32_prepare_to_store (void);
160static int go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
a17b5c4e
EZ
161 int write,
162 struct mem_attrib *attrib,
163 struct target_ops *target);
c5aa993b
JM
164static void go32_files_info (struct target_ops *target);
165static void go32_stop (void);
166static void go32_kill_inferior (void);
167static void go32_create_inferior (char *exec_file, char *args, char **env);
53a5351d 168static void cleanup_dregs (void);
c5aa993b
JM
169static void go32_mourn_inferior (void);
170static int go32_can_run (void);
53a5351d
JM
171static int go32_insert_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
172 int len, int rw);
173static int go32_remove_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
174 int len, int rw);
175static int go32_handle_nonaligned_watchpoint (wp_op what, CORE_ADDR waddr,
176 CORE_ADDR addr, int len, int rw);
b83266a0
SS
177
178static struct target_ops go32_ops;
c5aa993b
JM
179static void go32_terminal_init (void);
180static void go32_terminal_inferior (void);
181static void go32_terminal_ours (void);
e49d4fa6 182
53a5351d 183#define r_ofs(x) (offsetof(TSS,x))
e49d4fa6
SS
184
185static struct
186{
53a5351d
JM
187 size_t tss_ofs;
188 size_t size;
e49d4fa6
SS
189}
190regno_mapping[] =
191{
0fff5247
EZ
192 {r_ofs (tss_eax), 4}, /* normal registers, from a_tss */
193 {r_ofs (tss_ecx), 4},
194 {r_ofs (tss_edx), 4},
195 {r_ofs (tss_ebx), 4},
196 {r_ofs (tss_esp), 4},
197 {r_ofs (tss_ebp), 4},
198 {r_ofs (tss_esi), 4},
199 {r_ofs (tss_edi), 4},
200 {r_ofs (tss_eip), 4},
201 {r_ofs (tss_eflags), 4},
202 {r_ofs (tss_cs), 2},
203 {r_ofs (tss_ss), 2},
204 {r_ofs (tss_ds), 2},
205 {r_ofs (tss_es), 2},
206 {r_ofs (tss_fs), 2},
207 {r_ofs (tss_gs), 2},
208 {0, 10}, /* 8 FP registers, from npx.reg[] */
209 {1, 10},
210 {2, 10},
211 {3, 10},
212 {4, 10},
213 {5, 10},
214 {6, 10},
215 {7, 10},
53a5351d 216 /* The order of the next 7 registers must be consistent
0fff5247
EZ
217 with their numbering in config/i386/tm-i386.h, which see. */
218 {0, 2}, /* control word, from npx */
219 {4, 2}, /* status word, from npx */
220 {8, 2}, /* tag word, from npx */
221 {16, 2}, /* last FP exception CS from npx */
222 {12, 4}, /* last FP exception EIP from npx */
223 {24, 2}, /* last FP exception operand selector from npx */
224 {20, 4}, /* last FP exception operand offset from npx */
225 {18, 2} /* last FP opcode from npx */
e49d4fa6
SS
226};
227
228static struct
229 {
230 int go32_sig;
0fff5247 231 enum target_signal gdb_sig;
e49d4fa6
SS
232 }
233sig_map[] =
234{
0fff5247
EZ
235 {0, TARGET_SIGNAL_FPE},
236 {1, TARGET_SIGNAL_TRAP},
53a5351d
JM
237 /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
238 but I think SIGBUS is better, since the NMI is usually activated
239 as a result of a memory parity check failure. */
0fff5247
EZ
240 {2, TARGET_SIGNAL_BUS},
241 {3, TARGET_SIGNAL_TRAP},
242 {4, TARGET_SIGNAL_FPE},
243 {5, TARGET_SIGNAL_SEGV},
244 {6, TARGET_SIGNAL_ILL},
245 {7, TARGET_SIGNAL_EMT}, /* no-coprocessor exception */
246 {8, TARGET_SIGNAL_SEGV},
247 {9, TARGET_SIGNAL_SEGV},
248 {10, TARGET_SIGNAL_BUS},
249 {11, TARGET_SIGNAL_SEGV},
250 {12, TARGET_SIGNAL_SEGV},
251 {13, TARGET_SIGNAL_SEGV},
252 {14, TARGET_SIGNAL_SEGV},
253 {16, TARGET_SIGNAL_FPE},
254 {17, TARGET_SIGNAL_BUS},
255 {31, TARGET_SIGNAL_ILL},
256 {0x1b, TARGET_SIGNAL_INT},
257 {0x75, TARGET_SIGNAL_FPE},
258 {0x78, TARGET_SIGNAL_ALRM},
259 {0x79, TARGET_SIGNAL_INT},
260 {0x7a, TARGET_SIGNAL_QUIT},
261 {-1, TARGET_SIGNAL_LAST}
e49d4fa6
SS
262};
263
53a5351d
JM
264static struct {
265 enum target_signal gdb_sig;
266 int djgpp_excepno;
267} excepn_map[] = {
0fff5247
EZ
268 {TARGET_SIGNAL_0, -1},
269 {TARGET_SIGNAL_ILL, 6}, /* Invalid Opcode */
270 {TARGET_SIGNAL_EMT, 7}, /* triggers SIGNOFP */
271 {TARGET_SIGNAL_SEGV, 13}, /* GPF */
272 {TARGET_SIGNAL_BUS, 17}, /* Alignment Check */
53a5351d
JM
273 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
274 details. */
0fff5247
EZ
275 {TARGET_SIGNAL_TERM, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
276 {TARGET_SIGNAL_FPE, 0x75},
277 {TARGET_SIGNAL_INT, 0x79},
278 {TARGET_SIGNAL_QUIT, 0x7a},
279 {TARGET_SIGNAL_ALRM, 0x78}, /* triggers SIGTIMR */
280 {TARGET_SIGNAL_PROF, 0x78},
281 {TARGET_SIGNAL_LAST, -1}
53a5351d
JM
282};
283
e49d4fa6 284static void
0fff5247 285go32_open (char *name ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
e49d4fa6 286{
53a5351d 287 printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
e49d4fa6
SS
288}
289
290static void
0fff5247 291go32_close (int quitting ATTRIBUTE_UNUSED)
e49d4fa6
SS
292{
293}
294
295static void
0fff5247 296go32_attach (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
e49d4fa6 297{
53a5351d
JM
298 error ("\
299You cannot attach to a running program on this platform.\n\
300Use the `run' command to run DJGPP programs.");
e49d4fa6
SS
301}
302
303static void
0fff5247 304go32_detach (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
e49d4fa6
SS
305{
306}
307
308static int resume_is_step;
53a5351d 309static int resume_signal = -1;
e49d4fa6
SS
310
311static void
0fff5247 312go32_resume (int pid ATTRIBUTE_UNUSED, int step, enum target_signal siggnal)
c5aa993b 313{
53a5351d
JM
314 int i;
315
c5aa993b 316 resume_is_step = step;
53a5351d
JM
317
318 if (siggnal != TARGET_SIGNAL_0 && siggnal != TARGET_SIGNAL_TRAP)
319 {
0fff5247
EZ
320 for (i = 0, resume_signal = -1;
321 excepn_map[i].gdb_sig != TARGET_SIGNAL_LAST; i++)
53a5351d
JM
322 if (excepn_map[i].gdb_sig == siggnal)
323 {
324 resume_signal = excepn_map[i].djgpp_excepno;
325 break;
326 }
327 if (resume_signal == -1)
328 printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
329 target_signal_to_name (siggnal));
330 }
c5aa993b 331}
e49d4fa6 332
53a5351d
JM
333static char child_cwd[FILENAME_MAX];
334
e49d4fa6 335static int
0fff5247 336go32_wait (int pid ATTRIBUTE_UNUSED, struct target_waitstatus *status)
e49d4fa6
SS
337{
338 int i;
53a5351d 339 unsigned char saved_opcode;
0fff5247 340 unsigned long INT3_addr = 0;
53a5351d 341 int stepping_over_INT = 0;
e49d4fa6 342
53a5351d 343 a_tss.tss_eflags &= 0xfeff; /* reset the single-step flag (TF) */
e49d4fa6 344 if (resume_is_step)
53a5351d
JM
345 {
346 /* If the next instruction is INT xx or INTO, we need to handle
347 them specially. Intel manuals say that these instructions
348 reset the single-step flag (a.k.a. TF). However, it seems
349 that, at least in the DPMI environment, and at least when
350 stepping over the DPMI interrupt 31h, the problem is having
351 TF set at all when INT 31h is executed: the debuggee either
352 crashes (and takes the system with it) or is killed by a
353 SIGTRAP.
354
355 So we need to emulate single-step mode: we put an INT3 opcode
356 right after the INT xx instruction, let the debuggee run
357 until it hits INT3 and stops, then restore the original
358 instruction which we overwrote with the INT3 opcode, and back
359 up the debuggee's EIP to that instruction. */
360 read_child (a_tss.tss_eip, &saved_opcode, 1);
361 if (saved_opcode == 0xCD || saved_opcode == 0xCE)
362 {
363 unsigned char INT3_opcode = 0xCC;
364
365 INT3_addr
366 = saved_opcode == 0xCD ? a_tss.tss_eip + 2 : a_tss.tss_eip + 1;
367 stepping_over_INT = 1;
368 read_child (INT3_addr, &saved_opcode, 1);
369 write_child (INT3_addr, &INT3_opcode, 1);
370 }
371 else
372 a_tss.tss_eflags |= 0x0100; /* normal instruction: set TF */
373 }
374
375 /* The special value FFFFh in tss_trap indicates to run_child that
376 tss_irqn holds a signal to be delivered to the debuggee. */
377 if (resume_signal <= -1)
378 {
379 a_tss.tss_trap = 0;
380 a_tss.tss_irqn = 0xff;
381 }
e49d4fa6 382 else
53a5351d
JM
383 {
384 a_tss.tss_trap = 0xffff; /* run_child looks for this */
385 a_tss.tss_irqn = resume_signal;
386 }
387
388 /* The child might change working directory behind our back. The
389 GDB users won't like the side effects of that when they work with
390 relative file names, and GDB might be confused by its current
391 directory not being in sync with the truth. So we always make a
392 point of changing back to where GDB thinks is its cwd, when we
393 return control to the debugger, but restore child's cwd before we
394 run it. */
3a45aed8
EZ
395 /* Initialize child_cwd, before the first call to run_child and not
396 in the initialization, so the child get also the changed directory
397 set with the gdb-command "cd ..." */
398 if (!*child_cwd)
399 /* Initialize child's cwd with the current one. */
400 getcwd (child_cwd, sizeof (child_cwd));
401
53a5351d 402 chdir (child_cwd);
e49d4fa6 403
b83266a0 404#if __DJGPP_MINOR__ < 3
53a5351d 405 load_npx ();
b83266a0 406#endif
e49d4fa6 407 run_child ();
b83266a0 408#if __DJGPP_MINOR__ < 3
53a5351d 409 save_npx ();
b83266a0 410#endif
e49d4fa6 411
53a5351d
JM
412 /* Did we step over an INT xx instruction? */
413 if (stepping_over_INT && a_tss.tss_eip == INT3_addr + 1)
414 {
415 /* Restore the original opcode. */
416 a_tss.tss_eip--; /* EIP points *after* the INT3 instruction */
417 write_child (a_tss.tss_eip, &saved_opcode, 1);
418 /* Simulate a TRAP exception. */
419 a_tss.tss_irqn = 1;
420 a_tss.tss_eflags |= 0x0100;
421 }
422
423 getcwd (child_cwd, sizeof (child_cwd)); /* in case it has changed */
424 chdir (current_directory);
425
e49d4fa6
SS
426 if (a_tss.tss_irqn == 0x21)
427 {
428 status->kind = TARGET_WAITKIND_EXITED;
429 status->value.integer = a_tss.tss_eax & 0xff;
430 }
431 else
432 {
433 status->value.sig = TARGET_SIGNAL_UNKNOWN;
434 status->kind = TARGET_WAITKIND_STOPPED;
435 for (i = 0; sig_map[i].go32_sig != -1; i++)
436 {
437 if (a_tss.tss_irqn == sig_map[i].go32_sig)
438 {
53a5351d 439#if __DJGPP_MINOR__ < 3
e49d4fa6
SS
440 if ((status->value.sig = sig_map[i].gdb_sig) !=
441 TARGET_SIGNAL_TRAP)
442 status->kind = TARGET_WAITKIND_SIGNALLED;
53a5351d
JM
443#else
444 status->value.sig = sig_map[i].gdb_sig;
445#endif
e49d4fa6
SS
446 break;
447 }
448 }
449 }
450 return SOME_PID;
451}
452
453static void
89dea5aa 454fetch_register (int regno)
e49d4fa6 455{
89dea5aa
EZ
456 if (regno < FP0_REGNUM)
457 supply_register (regno, (char *) &a_tss + regno_mapping[regno].tss_ofs);
458 else if (regno <= LAST_FPU_CTRL_REGNUM)
459 i387_supply_register (regno, (char *) &npx);
460 else
461 internal_error (__FILE__, __LINE__,
462 "Invalid register no. %d in fetch_register.", regno);
463}
e49d4fa6 464
89dea5aa
EZ
465static void
466go32_fetch_registers (int regno)
467{
468 if (regno >= 0)
469 fetch_register (regno);
470 else
e49d4fa6 471 {
89dea5aa
EZ
472 for (regno = 0; regno < FP0_REGNUM; regno++)
473 fetch_register (regno);
474 i387_supply_fsave ((char *) &npx);
e49d4fa6
SS
475 }
476}
477
478static void
479store_register (int regno)
480{
481 void *rp;
482 void *v = (void *) &registers[REGISTER_BYTE (regno)];
483
89dea5aa
EZ
484 if (regno < FP0_REGNUM)
485 memcpy ((char *) &a_tss + regno_mapping[regno].tss_ofs,
486 v, regno_mapping[regno].size);
487 else if (regno <= LAST_FPU_CTRL_REGNUM)
488 i387_fill_fsave ((char *)&npx, regno);
e49d4fa6 489 else
8e65ff28
AC
490 internal_error (__FILE__, __LINE__,
491 "Invalid register no. %d in store_register.", regno);
e49d4fa6
SS
492}
493
494static void
495go32_store_registers (int regno)
496{
0fff5247 497 unsigned r;
e49d4fa6
SS
498
499 if (regno >= 0)
500 store_register (regno);
501 else
502 {
89dea5aa 503 for (r = 0; r < FP0_REGNUM; r++)
e49d4fa6 504 store_register (r);
89dea5aa 505 i387_fill_fsave ((char *) &npx, -1);
e49d4fa6
SS
506 }
507}
508
509static void
510go32_prepare_to_store (void)
511{
512}
513
514static int
515go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
a17b5c4e 516 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
0fff5247 517 struct target_ops *target ATTRIBUTE_UNUSED)
e49d4fa6
SS
518{
519 if (write)
520 {
521 if (write_child (memaddr, myaddr, len))
522 {
523 return 0;
524 }
525 else
526 {
527 return len;
528 }
529 }
530 else
531 {
532 if (read_child (memaddr, myaddr, len))
533 {
534 return 0;
535 }
536 else
537 {
538 return len;
539 }
540 }
541}
542
53a5351d
JM
543static cmdline_t child_cmd; /* parsed child's command line kept here */
544
e49d4fa6 545static void
0fff5247 546go32_files_info (struct target_ops *target ATTRIBUTE_UNUSED)
e49d4fa6 547{
53a5351d 548 printf_unfiltered ("You are running a DJGPP V2 program.\n");
e49d4fa6
SS
549}
550
551static void
552go32_stop (void)
553{
554 normal_stop ();
555 cleanup_client ();
556 inferior_pid = 0;
557 prog_has_started = 0;
558}
559
560static void
561go32_kill_inferior (void)
562{
53a5351d
JM
563 redir_cmdline_delete (&child_cmd);
564 resume_signal = -1;
565 resume_is_step = 0;
e49d4fa6
SS
566 unpush_target (&go32_ops);
567}
568
569static void
570go32_create_inferior (char *exec_file, char *args, char **env)
571{
572 jmp_buf start_state;
573 char *cmdline;
574 char **env_save = environ;
575
0fff5247
EZ
576 /* If no exec file handed to us, get it from the exec-file command -- with
577 a good, common error message if none is specified. */
578 if (exec_file == 0)
579 exec_file = get_exec_file (1);
580
e49d4fa6
SS
581 if (prog_has_started)
582 {
b83266a0 583 go32_stop ();
e49d4fa6
SS
584 go32_kill_inferior ();
585 }
53a5351d
JM
586 resume_signal = -1;
587 resume_is_step = 0;
3a45aed8
EZ
588
589 /* Initialize child's cwd as empty to be initialized when starting
590 the child. */
591 *child_cwd = 0;
592
53a5351d
JM
593 /* Init command line storage. */
594 if (redir_debug_init (&child_cmd) == -1)
8e65ff28
AC
595 internal_error (__FILE__, __LINE__,
596 "Cannot allocate redirection storage: not enough memory.\n");
53a5351d
JM
597
598 /* Parse the command line and create redirections. */
599 if (strpbrk (args, "<>"))
600 {
601 if (redir_cmdline_parse (args, &child_cmd) == 0)
602 args = child_cmd.command;
603 else
604 error ("Syntax error in command line.");
605 }
606 else
c2d11a7d 607 child_cmd.command = xstrdup (args);
e49d4fa6
SS
608
609 cmdline = (char *) alloca (strlen (args) + 4);
610 cmdline[0] = strlen (args);
611 strcpy (cmdline + 1, args);
612 cmdline[strlen (args) + 1] = 13;
613
614 environ = env;
615
616 if (v2loadimage (exec_file, cmdline, start_state))
617 {
618 environ = env_save;
619 printf_unfiltered ("Load failed for image %s\n", exec_file);
620 exit (1);
621 }
622 environ = env_save;
623
624 edi_init (start_state);
53a5351d
JM
625#if __DJGPP_MINOR__ < 3
626 save_npx ();
627#endif
e49d4fa6
SS
628
629 inferior_pid = SOME_PID;
630 push_target (&go32_ops);
631 clear_proceed_status ();
632 insert_breakpoints ();
2acceee2 633 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
b83266a0 634 prog_has_started = 1;
e49d4fa6
SS
635}
636
637static void
638go32_mourn_inferior (void)
639{
53a5351d
JM
640 /* We need to make sure all the breakpoint enable bits in the DR7
641 register are reset when the inferior exits. Otherwise, if they
642 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
643 failure to set more watchpoints, and other calamities. It would
644 be nice if GDB itself would take care to remove all breakpoints
645 at all times, but it doesn't, probably under an assumption that
646 the OS cleans up when the debuggee exits. */
647 cleanup_dregs ();
e49d4fa6
SS
648 go32_kill_inferior ();
649 generic_mourn_inferior ();
650}
651
652static int
653go32_can_run (void)
654{
655 return 1;
656}
657
e49d4fa6
SS
658/* Hardware watchpoint support. */
659
660#define DR_STATUS 6
661#define DR_CONTROL 7
662#define DR_ENABLE_SIZE 2
663#define DR_LOCAL_ENABLE_SHIFT 0
664#define DR_GLOBAL_ENABLE_SHIFT 1
665#define DR_LOCAL_SLOWDOWN 0x100
666#define DR_GLOBAL_SLOWDOWN 0x200
667#define DR_CONTROL_SHIFT 16
668#define DR_CONTROL_SIZE 4
53a5351d 669#define DR_RW_READWRITE 0x3
e49d4fa6
SS
670#define DR_RW_WRITE 0x1
671#define DR_CONTROL_MASK 0xf
672#define DR_ENABLE_MASK 0x3
673#define DR_LEN_1 0x0
674#define DR_LEN_2 0x4
675#define DR_LEN_4 0xc
676
677#define D_REGS edi.dr
678#define CONTROL D_REGS[DR_CONTROL]
679#define STATUS D_REGS[DR_STATUS]
680
681#define IS_REG_FREE(index) \
53a5351d 682 (!(CONTROL & (3 << (DR_ENABLE_SIZE * (index)))))
e49d4fa6
SS
683
684#define LOCAL_ENABLE_REG(index) \
53a5351d 685 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
e49d4fa6
SS
686
687#define GLOBAL_ENABLE_REG(index) \
53a5351d 688 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
e49d4fa6
SS
689
690#define DISABLE_REG(index) \
53a5351d 691 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * (index))))
e49d4fa6
SS
692
693#define SET_LOCAL_EXACT() \
694 (CONTROL |= DR_LOCAL_SLOWDOWN)
695
696#define SET_GLOBAL_EXACT() \
697 (CONTROL |= DR_GLOBAL_SLOWDOWN)
698
53a5351d
JM
699#define RESET_LOCAL_EXACT() \
700 (CONTROL &= ~(DR_LOCAL_SLOWDOWN))
701
702#define RESET_GLOBAL_EXACT() \
703 (CONTROL &= ~(DR_GLOBAL_SLOWDOWN))
704
e49d4fa6
SS
705#define SET_BREAK(index,address) \
706 do {\
53a5351d 707 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index)));\
e49d4fa6 708 D_REGS[index] = address;\
c2c6d25f 709 dr_ref_count[index]++;\
e49d4fa6
SS
710 } while(0)
711
712#define SET_WATCH(index,address,rw,len) \
713 do {\
714 SET_BREAK(index,address);\
53a5351d 715 CONTROL |= ((len)|(rw)) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index));\
e49d4fa6
SS
716 } while (0)
717
53a5351d
JM
718#define IS_WATCH(index) \
719 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE*(index))))
720
c2c6d25f 721#define WATCH_HIT(index) ((STATUS & (1 << (index))) && IS_WATCH(index))
e49d4fa6 722
53a5351d
JM
723#define DR_DEF(index) \
724 ((CONTROL >> (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index))) & 0x0f)
725
726
727#if 0 /* use debugging macro */
728#define SHOW_DR(text,len) \
e49d4fa6 729do { \
53a5351d 730 if (!getenv ("GDB_SHOW_DR")) break; \
e49d4fa6 731 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
53a5351d
JM
732 fprintf(stderr,"%08x %d %08x %d ", \
733 edi.dr[0],dr_ref_count[0],edi.dr[1],dr_ref_count[1]); \
734 fprintf(stderr,"%08x %d %08x %d ", \
735 edi.dr[2],dr_ref_count[2],edi.dr[3],dr_ref_count[3]); \
736 fprintf(stderr,(len)?"(%s:%d)\n":"(%s)\n",#text,len); \
e49d4fa6
SS
737} while (0)
738#else
53a5351d 739#define SHOW_DR(text,len) do {} while (0)
e49d4fa6
SS
740#endif
741
53a5351d
JM
742static void
743cleanup_dregs (void)
744{
745 int i;
746
747 CONTROL = 0;
748 STATUS = 0;
749 for (i = 0; i < 4; i++)
750 {
751 D_REGS[i] = 0;
752 dr_ref_count[i] = 0;
753 }
754}
755
e49d4fa6
SS
756/* Insert a watchpoint. */
757
758int
0fff5247
EZ
759go32_insert_watchpoint (int pid ATTRIBUTE_UNUSED, CORE_ADDR addr,
760 int len, int rw)
e49d4fa6 761{
53a5351d 762 int ret = go32_insert_aligned_watchpoint (addr, addr, len, rw);
e49d4fa6 763
53a5351d 764 SHOW_DR (insert_watch, len);
e49d4fa6
SS
765 return ret;
766}
767
768static int
53a5351d 769go32_insert_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
e49d4fa6
SS
770 int len, int rw)
771{
772 int i;
773 int read_write_bits, len_bits;
774
53a5351d
JM
775 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
776 However, x86 doesn't support read-only data breakpoints. */
777 read_write_bits = rw ? DR_RW_READWRITE : DR_RW_WRITE;
778
779 switch (len)
780 {
781 case 4:
782 len_bits = DR_LEN_4;
783 break;
784 case 2:
785 len_bits = DR_LEN_2;
786 break;
787 case 1:
788 len_bits = DR_LEN_1;
789 break;
790 default:
791 /* The debug registers only have 2 bits for the length, so
792 so this value will always fail the loop below. */
793 len_bits = 0x10;
794 }
795
796 /* Look for an occupied debug register with the same address and the
797 same RW and LEN definitions. If we find one, we can use it for
798 this watchpoint as well (and save a register). */
799 for (i = 0; i < 4; i++)
800 {
801 if (!IS_REG_FREE (i) && D_REGS[i] == addr
0fff5247 802 && DR_DEF (i) == (unsigned)(len_bits | read_write_bits))
e49d4fa6 803 {
53a5351d
JM
804 dr_ref_count[i]++;
805 return 0;
e49d4fa6 806 }
53a5351d
JM
807 }
808
809 /* Look for a free debug register. */
810 for (i = 0; i <= 3; i++)
811 {
812 if (IS_REG_FREE (i))
813 break;
814 }
e49d4fa6
SS
815
816 /* No more debug registers! */
817 if (i > 3)
818 return -1;
819
53a5351d
JM
820 if (len == 2)
821 {
822 if (addr % 2)
823 return go32_handle_nonaligned_watchpoint (wp_insert, waddr, addr,
824 len, rw);
825 }
e49d4fa6 826 else if (len == 4)
53a5351d
JM
827 {
828 if (addr % 4)
829 return go32_handle_nonaligned_watchpoint (wp_insert, waddr, addr,
830 len, rw);
831 }
832 else if (len != 1)
833 return go32_handle_nonaligned_watchpoint (wp_insert, waddr, addr, len, rw);
e49d4fa6
SS
834
835 SET_WATCH (i, addr, read_write_bits, len_bits);
836 LOCAL_ENABLE_REG (i);
837 SET_LOCAL_EXACT ();
53a5351d
JM
838 SET_GLOBAL_EXACT ();
839 return 0;
e49d4fa6
SS
840}
841
842static int
53a5351d 843go32_handle_nonaligned_watchpoint (wp_op what, CORE_ADDR waddr, CORE_ADDR addr,
e49d4fa6
SS
844 int len, int rw)
845{
846 int align;
847 int size;
53a5351d 848 int rv = 0, status = 0;
e49d4fa6 849
184a103e 850 static int size_try_array[4][4] =
e49d4fa6 851 {
184a103e
EZ
852 { 1, 1, 1, 1 }, /* trying size one */
853 { 2, 1, 2, 1 }, /* trying size two */
854 { 2, 1, 2, 1 }, /* trying size three */
855 { 4, 1, 2, 1 } /* trying size four */
e49d4fa6
SS
856 };
857
858 while (len > 0)
859 {
860 align = addr % 4;
184a103e
EZ
861 /* Four is the maximum length a 386 debug register can watch. */
862 size = size_try_array[len > 4 ? 3 : len - 1][align];
53a5351d
JM
863 if (what == wp_insert)
864 status = go32_insert_aligned_watchpoint (waddr, addr, size, rw);
865 else if (what == wp_remove)
866 status = go32_remove_aligned_watchpoint (waddr, addr, size, rw);
867 else if (what == wp_count)
868 rv++;
869 else
d03cef9d 870 status = EINVAL;
53a5351d
JM
871 /* We keep the loop going even after a failure, because some of
872 the other aligned watchpoints might still succeed, e.g. if
873 they watch addresses that are already watched, and thus just
874 increment the reference counts of occupied debug registers.
875 If we break out of the loop too early, we could cause those
876 addresses watched by other watchpoints to be disabled when
877 GDB reacts to our failure to insert this watchpoint and tries
878 to remove it. */
879 if (status)
880 rv = status;
e49d4fa6
SS
881 addr += size;
882 len -= size;
883 }
884 return rv;
885}
886
887/* Remove a watchpoint. */
888
889int
0fff5247
EZ
890go32_remove_watchpoint (int pid ATTRIBUTE_UNUSED, CORE_ADDR addr,
891 int len, int rw)
53a5351d
JM
892{
893 int ret = go32_remove_aligned_watchpoint (addr, addr, len, rw);
894
895 SHOW_DR (remove_watch, len);
896 return ret;
897}
898
899static int
900go32_remove_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
901 int len, int rw)
e49d4fa6
SS
902{
903 int i;
53a5351d
JM
904 int read_write_bits, len_bits;
905
906 /* Values of rw: 0 - write, 1 - read, 2 - access (read and write).
907 However, x86 doesn't support read-only data breakpoints. */
908 read_write_bits = rw ? DR_RW_READWRITE : DR_RW_WRITE;
909
910 switch (len)
911 {
912 case 4:
913 len_bits = DR_LEN_4;
914 break;
915 case 2:
916 len_bits = DR_LEN_2;
917 break;
918 case 1:
919 len_bits = DR_LEN_1;
920 break;
921 default:
922 /* The debug registers only have 2 bits for the length, so
923 so this value will always fail the loop below. */
924 len_bits = 0x10;
925 }
926
927 if (len == 2)
928 {
929 if (addr % 2)
930 return go32_handle_nonaligned_watchpoint (wp_remove, waddr, addr,
931 len, rw);
932 }
933 else if (len == 4)
934 {
935 if (addr % 4)
936 return go32_handle_nonaligned_watchpoint (wp_remove, waddr, addr,
937 len, rw);
938 }
939 else if (len != 1)
940 return go32_handle_nonaligned_watchpoint (wp_remove, waddr, addr, len, rw);
e49d4fa6
SS
941
942 for (i = 0; i <= 3; i++)
943 {
53a5351d 944 if (!IS_REG_FREE (i) && D_REGS[i] == addr
0fff5247 945 && DR_DEF (i) == (unsigned)(len_bits | read_write_bits))
e49d4fa6 946 {
53a5351d
JM
947 dr_ref_count[i]--;
948 if (dr_ref_count[i] == 0)
949 DISABLE_REG (i);
e49d4fa6
SS
950 }
951 }
53a5351d
JM
952 RESET_LOCAL_EXACT ();
953 RESET_GLOBAL_EXACT ();
e49d4fa6
SS
954
955 return 0;
956}
957
53a5351d
JM
958/* Can we use debug registers to watch a region whose address is ADDR
959 and whose length is LEN bytes? */
960
961int
962go32_region_ok_for_watchpoint (CORE_ADDR addr, int len)
963{
964 /* Compute how many aligned watchpoints we would need to cover this
965 region. */
966 int nregs = go32_handle_nonaligned_watchpoint (wp_count, addr, addr, len, 0);
967
968 return nregs <= 4 ? 1 : 0;
969}
970
971/* Check if stopped by a data watchpoint. If so, return the address
972 whose access triggered the watchpoint. */
e49d4fa6
SS
973
974CORE_ADDR
0fff5247 975go32_stopped_by_watchpoint (int pid ATTRIBUTE_UNUSED, int data_watchpoint)
e49d4fa6
SS
976{
977 int i, ret = 0;
978 int status;
979
980 status = edi.dr[DR_STATUS];
53a5351d 981 SHOW_DR (stopped_by, 0);
e49d4fa6
SS
982 for (i = 0; i <= 3; i++)
983 {
53a5351d 984 if (WATCH_HIT (i) && data_watchpoint)
e49d4fa6 985 {
53a5351d 986 SHOW_DR (WP_HIT, 0);
e49d4fa6
SS
987 ret = D_REGS[i];
988 }
989 }
e49d4fa6
SS
990
991 return ret;
992}
993
994/* Remove a breakpoint. */
995
996int
0fff5247 997go32_remove_hw_breakpoint (CORE_ADDR addr, void *shadow ATTRIBUTE_UNUSED)
e49d4fa6
SS
998{
999 int i;
1000 for (i = 0; i <= 3; i++)
1001 {
53a5351d 1002 if (!IS_REG_FREE (i) && D_REGS[i] == addr && DR_DEF (i) == 0)
e49d4fa6 1003 {
53a5351d
JM
1004 dr_ref_count[i]--;
1005 if (dr_ref_count[i] == 0)
1006 DISABLE_REG (i);
e49d4fa6
SS
1007 }
1008 }
53a5351d 1009 SHOW_DR (remove_hw, 0);
e49d4fa6
SS
1010 return 0;
1011}
1012
1013int
0fff5247 1014go32_insert_hw_breakpoint (CORE_ADDR addr, void *shadow ATTRIBUTE_UNUSED)
e49d4fa6
SS
1015{
1016 int i;
e49d4fa6 1017
53a5351d
JM
1018 /* Look for an occupied debug register with the same address and the
1019 same RW and LEN definitions. If we find one, we can use it for
1020 this breakpoint as well (and save a register). */
1021 for (i = 0; i < 4; i++)
1022 {
1023 if (!IS_REG_FREE (i) && D_REGS[i] == addr && DR_DEF (i) == 0)
1024 {
1025 dr_ref_count[i]++;
1026 SHOW_DR (insert_hw, 0);
1027 return 0;
1028 }
1029 }
1030
e49d4fa6
SS
1031 /* Look for a free debug register. */
1032 for (i = 0; i <= 3; i++)
1033 {
1034 if (IS_REG_FREE (i))
1035 break;
1036 }
1037
53a5351d
JM
1038 /* No more debug registers? */
1039 if (i < 4)
1040 {
1041 SET_BREAK (i, addr);
1042 LOCAL_ENABLE_REG (i);
1043 }
1044 SHOW_DR (insert_hw, 0);
e49d4fa6 1045
d03cef9d 1046 return i < 4 ? 0 : EBUSY;
e49d4fa6
SS
1047}
1048
53a5351d
JM
1049/* Put the device open on handle FD into either raw or cooked
1050 mode, return 1 if it was in raw mode, zero otherwise. */
1051
1052static int
1053device_mode (int fd, int raw_p)
1054{
1055 int oldmode, newmode;
1056 __dpmi_regs regs;
1057
1058 regs.x.ax = 0x4400;
1059 regs.x.bx = fd;
1060 __dpmi_int (0x21, &regs);
1061 if (regs.x.flags & 1)
1062 return -1;
1063 newmode = oldmode = regs.x.dx;
1064
1065 if (raw_p)
1066 newmode |= 0x20;
1067 else
1068 newmode &= ~0x20;
1069
1070 if (oldmode & 0x80) /* Only for character dev */
1071 {
1072 regs.x.ax = 0x4401;
1073 regs.x.bx = fd;
1074 regs.x.dx = newmode & 0xff; /* Force upper byte zero, else it fails */
1075 __dpmi_int (0x21, &regs);
1076 if (regs.x.flags & 1)
1077 return -1;
1078 }
1079 return (oldmode & 0x20) == 0x20;
1080}
1081
1082
1083static int inf_mode_valid = 0;
1084static int inf_terminal_mode;
1085
1086/* This semaphore is needed because, amazingly enough, GDB calls
1087 target.to_terminal_ours more than once after the inferior stops.
1088 But we need the information from the first call only, since the
1089 second call will always see GDB's own cooked terminal. */
1090static int terminal_is_ours = 1;
1091
cce74817
JM
1092static void
1093go32_terminal_init (void)
1094{
53a5351d
JM
1095 inf_mode_valid = 0; /* reinitialize, in case they are restarting child */
1096 terminal_is_ours = 1;
cce74817
JM
1097}
1098
1099static void
0fff5247 1100go32_terminal_info (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
cce74817 1101{
53a5351d
JM
1102 printf_unfiltered ("Inferior's terminal is in %s mode.\n",
1103 !inf_mode_valid
1104 ? "default" : inf_terminal_mode ? "raw" : "cooked");
1105
1106#if __DJGPP_MINOR__ > 2
1107 if (child_cmd.redirection)
1108 {
1109 int i;
1110
1111 for (i = 0; i < DBG_HANDLES; i++)
c5aa993b 1112 {
53a5351d
JM
1113 if (child_cmd.redirection[i]->file_name)
1114 printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
1115 i, child_cmd.redirection[i]->file_name);
1116 else if (_get_dev_info (child_cmd.redirection[i]->inf_handle) == -1)
1117 printf_unfiltered
1118 ("\tFile handle %d appears to be closed by inferior.\n", i);
1119 /* Mask off the raw/cooked bit when comparing device info words. */
1120 else if ((_get_dev_info (child_cmd.redirection[i]->inf_handle) & 0xdf)
1121 != (_get_dev_info (i) & 0xdf))
1122 printf_unfiltered
1123 ("\tFile handle %d appears to be redirected by inferior.\n", i);
c5aa993b 1124 }
53a5351d
JM
1125 }
1126#endif
1127}
1128
1129static void
1130go32_terminal_inferior (void)
1131{
1132 /* Redirect standard handles as child wants them. */
1133 errno = 0;
1134 if (redir_to_child (&child_cmd) == -1)
1135 {
1136 redir_to_debugger (&child_cmd);
1137 error ("Cannot redirect standard handles for program: %s.",
1138 strerror (errno));
1139 }
1140 /* set the console device of the inferior to whatever mode
1141 (raw or cooked) we found it last time */
1142 if (terminal_is_ours)
1143 {
1144 if (inf_mode_valid)
1145 device_mode (0, inf_terminal_mode);
1146 terminal_is_ours = 0;
1147 }
cce74817
JM
1148}
1149
1150static void
1151go32_terminal_ours (void)
1152{
53a5351d
JM
1153 /* Switch to cooked mode on the gdb terminal and save the inferior
1154 terminal mode to be restored when it is resumed */
1155 if (!terminal_is_ours)
1156 {
1157 inf_terminal_mode = device_mode (0, 0);
1158 if (inf_terminal_mode != -1)
1159 inf_mode_valid = 1;
1160 else
1161 /* If device_mode returned -1, we don't know what happens with
1162 handle 0 anymore, so make the info invalid. */
1163 inf_mode_valid = 0;
1164 terminal_is_ours = 1;
1165
1166 /* Restore debugger's standard handles. */
1167 errno = 0;
1168 if (redir_to_debugger (&child_cmd) == -1)
1169 {
1170 redir_to_child (&child_cmd);
1171 error ("Cannot redirect standard handles for debugger: %s.",
1172 strerror (errno));
1173 }
1174 }
cce74817
JM
1175}
1176
e49d4fa6
SS
1177static void
1178init_go32_ops (void)
1179{
1180 go32_ops.to_shortname = "djgpp";
1181 go32_ops.to_longname = "djgpp target process";
1182 go32_ops.to_doc =
1183 "Program loaded by djgpp, when gdb is used as an external debugger";
1184 go32_ops.to_open = go32_open;
1185 go32_ops.to_close = go32_close;
53a5351d 1186 go32_ops.to_attach = go32_attach;
e49d4fa6
SS
1187 go32_ops.to_detach = go32_detach;
1188 go32_ops.to_resume = go32_resume;
1189 go32_ops.to_wait = go32_wait;
1190 go32_ops.to_fetch_registers = go32_fetch_registers;
1191 go32_ops.to_store_registers = go32_store_registers;
1192 go32_ops.to_prepare_to_store = go32_prepare_to_store;
1193 go32_ops.to_xfer_memory = go32_xfer_memory;
1194 go32_ops.to_files_info = go32_files_info;
1195 go32_ops.to_insert_breakpoint = memory_insert_breakpoint;
1196 go32_ops.to_remove_breakpoint = memory_remove_breakpoint;
cce74817
JM
1197 go32_ops.to_terminal_init = go32_terminal_init;
1198 go32_ops.to_terminal_inferior = go32_terminal_inferior;
53a5351d 1199 go32_ops.to_terminal_ours_for_output = go32_terminal_ours;
cce74817 1200 go32_ops.to_terminal_ours = go32_terminal_ours;
53a5351d 1201 go32_ops.to_terminal_info = go32_terminal_info;
e49d4fa6
SS
1202 go32_ops.to_kill = go32_kill_inferior;
1203 go32_ops.to_create_inferior = go32_create_inferior;
1204 go32_ops.to_mourn_inferior = go32_mourn_inferior;
1205 go32_ops.to_can_run = go32_can_run;
1206 go32_ops.to_stop = go32_stop;
1207 go32_ops.to_stratum = process_stratum;
1208 go32_ops.to_has_all_memory = 1;
1209 go32_ops.to_has_memory = 1;
1210 go32_ops.to_has_stack = 1;
1211 go32_ops.to_has_registers = 1;
1212 go32_ops.to_has_execution = 1;
1213 go32_ops.to_magic = OPS_MAGIC;
53a5351d 1214
3a45aed8
EZ
1215 /* Initialize child's cwd as empty to be initialized when starting
1216 the child. */
1217 *child_cwd = 0;
53a5351d
JM
1218
1219 /* Initialize child's command line storage. */
1220 if (redir_debug_init (&child_cmd) == -1)
8e65ff28
AC
1221 internal_error (__FILE__, __LINE__,
1222 "Cannot allocate redirection storage: not enough memory.\n");
0fff5247
EZ
1223
1224 /* We are always processing GCC-compiled programs. */
1225 processing_gcc_compilation = 2;
e49d4fa6
SS
1226}
1227
1228void
1229_initialize_go32_nat (void)
1230{
1231 init_go32_ops ();
1232 add_target (&go32_ops);
1233}
53a5351d
JM
1234
1235pid_t
1236tcgetpgrp (int fd)
1237{
1238 if (isatty (fd))
1239 return SOME_PID;
1240 errno = ENOTTY;
1241 return -1;
1242}
1243
1244int
1245tcsetpgrp (int fd, pid_t pgid)
1246{
1247 if (isatty (fd) && pgid == SOME_PID)
1248 return 0;
1249 errno = pgid == SOME_PID ? ENOTTY : ENOSYS;
1250 return -1;
1251}