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