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