582ff1626ffa3085cc64d064e4d94860aeecc3e4
[deliverable/binutils-gdb.git] / gdb / go32-nat.c
1 /* Native debugging support for Intel x86 running DJGPP.
2 Copyright 1997, 1999, 2001 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 #include "i387-nat.h"
32
33 #include <stdio.h> /* required for __DJGPP_MINOR__ */
34 #include <stdlib.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <io.h>
39 #include <dpmi.h>
40 #include <debug/v2load.h>
41 #include <debug/dbgcom.h>
42 #if __DJGPP_MINOR__ > 2
43 #include <debug/redir.h>
44 #endif
45
46 #if __DJGPP_MINOR__ < 3
47 /* This code will be provided from DJGPP 2.03 on. Until then I code it
48 here */
49 typedef 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 }
58 NPXREG;
59
60 typedef 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 }
71 NPX;
72
73 static NPX npx;
74
75 static void save_npx (void); /* Save the FPU of the debugged program */
76 static void load_npx (void); /* Restore the FPU of the debugged program */
77
78 /* ------------------------------------------------------------------------- */
79 /* Store the contents of the NPX in the global variable `npx'. */
80 /* *INDENT-OFF* */
81
82 static void
83 save_npx (void)
84 {
85 asm ("inb $0xa0, %%al
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
93 1:
94 fnsave % 0
95 fwait "
96 : "=m" (npx)
97 : /* No input */
98 : "%eax");
99 }
100
101 /* *INDENT-ON* */
102
103
104
105
106
107 /* ------------------------------------------------------------------------- */
108 /* Reload the contents of the NPX from the global variable `npx'. */
109
110 static void
111 load_npx (void)
112 {
113 asm ("frstor %0":"=m" (npx));
114 }
115 /* ------------------------------------------------------------------------- */
116 /* Stubs for the missing redirection functions. */
117 typedef struct {
118 char *command;
119 int redirected;
120 } cmdline_t;
121
122 void redir_cmdline_delete (cmdline_t *ptr) {ptr->redirected = 0;}
123 int redir_cmdline_parse (const char *args, cmdline_t *ptr)
124 {
125 return -1;
126 }
127 int redir_to_child (cmdline_t *ptr)
128 {
129 return 1;
130 }
131 int redir_to_debugger (cmdline_t *ptr)
132 {
133 return 1;
134 }
135 int redir_debug_init (cmdline_t *ptr) { return 0; }
136 #endif /* __DJGPP_MINOR < 3 */
137
138 extern void _initialize_go32_nat (void);
139
140 typedef enum { wp_insert, wp_remove, wp_count } wp_op;
141
142 /* This holds the current reference counts for each debug register. */
143 static int dr_ref_count[4];
144
145 extern char **environ;
146
147 #define SOME_PID 42
148
149 static int prog_has_started = 0;
150 static void go32_open (char *name, int from_tty);
151 static void go32_close (int quitting);
152 static void go32_attach (char *args, int from_tty);
153 static void go32_detach (char *args, int from_tty);
154 static void go32_resume (int pid, int step, enum target_signal siggnal);
155 static int go32_wait (int pid, struct target_waitstatus *status);
156 static void go32_fetch_registers (int regno);
157 static void store_register (int regno);
158 static void go32_store_registers (int regno);
159 static void go32_prepare_to_store (void);
160 static int go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
161 int write,
162 struct mem_attrib *attrib,
163 struct target_ops *target);
164 static void go32_files_info (struct target_ops *target);
165 static void go32_stop (void);
166 static void go32_kill_inferior (void);
167 static void go32_create_inferior (char *exec_file, char *args, char **env);
168 static void cleanup_dregs (void);
169 static void go32_mourn_inferior (void);
170 static int go32_can_run (void);
171 static int go32_insert_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
172 int len, int rw);
173 static int go32_remove_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
174 int len, int rw);
175 static int go32_handle_nonaligned_watchpoint (wp_op what, CORE_ADDR waddr,
176 CORE_ADDR addr, int len, int rw);
177
178 static struct target_ops go32_ops;
179 static void go32_terminal_init (void);
180 static void go32_terminal_inferior (void);
181 static void go32_terminal_ours (void);
182
183 #define r_ofs(x) (offsetof(TSS,x))
184
185 static struct
186 {
187 size_t tss_ofs;
188 size_t size;
189 }
190 regno_mapping[] =
191 {
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},
216 /* The order of the next 7 registers must be consistent
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 */
226 };
227
228 static struct
229 {
230 int go32_sig;
231 enum target_signal gdb_sig;
232 }
233 sig_map[] =
234 {
235 {0, TARGET_SIGNAL_FPE},
236 {1, TARGET_SIGNAL_TRAP},
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. */
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}
262 };
263
264 static struct {
265 enum target_signal gdb_sig;
266 int djgpp_excepno;
267 } excepn_map[] = {
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 */
273 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
274 details. */
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}
282 };
283
284 static void
285 go32_open (char *name ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
286 {
287 printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
288 }
289
290 static void
291 go32_close (int quitting ATTRIBUTE_UNUSED)
292 {
293 }
294
295 static void
296 go32_attach (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
297 {
298 error ("\
299 You cannot attach to a running program on this platform.\n\
300 Use the `run' command to run DJGPP programs.");
301 }
302
303 static void
304 go32_detach (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
305 {
306 }
307
308 static int resume_is_step;
309 static int resume_signal = -1;
310
311 static void
312 go32_resume (int pid ATTRIBUTE_UNUSED, int step, enum target_signal siggnal)
313 {
314 int i;
315
316 resume_is_step = step;
317
318 if (siggnal != TARGET_SIGNAL_0 && siggnal != TARGET_SIGNAL_TRAP)
319 {
320 for (i = 0, resume_signal = -1;
321 excepn_map[i].gdb_sig != TARGET_SIGNAL_LAST; i++)
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 }
331 }
332
333 static char child_cwd[FILENAME_MAX];
334
335 static int
336 go32_wait (int pid ATTRIBUTE_UNUSED, struct target_waitstatus *status)
337 {
338 int i;
339 unsigned char saved_opcode;
340 unsigned long INT3_addr = 0;
341 int stepping_over_INT = 0;
342
343 a_tss.tss_eflags &= 0xfeff; /* reset the single-step flag (TF) */
344 if (resume_is_step)
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 }
382 else
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. */
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
402 chdir (child_cwd);
403
404 #if __DJGPP_MINOR__ < 3
405 load_npx ();
406 #endif
407 run_child ();
408 #if __DJGPP_MINOR__ < 3
409 save_npx ();
410 #endif
411
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
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 {
439 #if __DJGPP_MINOR__ < 3
440 if ((status->value.sig = sig_map[i].gdb_sig) !=
441 TARGET_SIGNAL_TRAP)
442 status->kind = TARGET_WAITKIND_SIGNALLED;
443 #else
444 status->value.sig = sig_map[i].gdb_sig;
445 #endif
446 break;
447 }
448 }
449 }
450 return SOME_PID;
451 }
452
453 static void
454 fetch_register (int regno)
455 {
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 }
464
465 static void
466 go32_fetch_registers (int regno)
467 {
468 if (regno >= 0)
469 fetch_register (regno);
470 else
471 {
472 for (regno = 0; regno < FP0_REGNUM; regno++)
473 fetch_register (regno);
474 i387_supply_fsave ((char *) &npx);
475 }
476 }
477
478 static void
479 store_register (int regno)
480 {
481 void *rp;
482 void *v = (void *) &registers[REGISTER_BYTE (regno)];
483
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);
489 else
490 internal_error (__FILE__, __LINE__,
491 "Invalid register no. %d in store_register.", regno);
492 }
493
494 static void
495 go32_store_registers (int regno)
496 {
497 unsigned r;
498
499 if (regno >= 0)
500 store_register (regno);
501 else
502 {
503 for (r = 0; r < FP0_REGNUM; r++)
504 store_register (r);
505 i387_fill_fsave ((char *) &npx, -1);
506 }
507 }
508
509 static void
510 go32_prepare_to_store (void)
511 {
512 }
513
514 static int
515 go32_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
516 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
517 struct target_ops *target ATTRIBUTE_UNUSED)
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
543 static cmdline_t child_cmd; /* parsed child's command line kept here */
544
545 static void
546 go32_files_info (struct target_ops *target ATTRIBUTE_UNUSED)
547 {
548 printf_unfiltered ("You are running a DJGPP V2 program.\n");
549 }
550
551 static void
552 go32_stop (void)
553 {
554 normal_stop ();
555 cleanup_client ();
556 inferior_pid = 0;
557 prog_has_started = 0;
558 }
559
560 static void
561 go32_kill_inferior (void)
562 {
563 redir_cmdline_delete (&child_cmd);
564 resume_signal = -1;
565 resume_is_step = 0;
566 unpush_target (&go32_ops);
567 }
568
569 static void
570 go32_create_inferior (char *exec_file, char *args, char **env)
571 {
572 jmp_buf start_state;
573 char *cmdline;
574 char **env_save = environ;
575
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
581 if (prog_has_started)
582 {
583 go32_stop ();
584 go32_kill_inferior ();
585 }
586 resume_signal = -1;
587 resume_is_step = 0;
588
589 /* Initialize child's cwd as empty to be initialized when starting
590 the child. */
591 *child_cwd = 0;
592
593 /* Init command line storage. */
594 if (redir_debug_init (&child_cmd) == -1)
595 internal_error (__FILE__, __LINE__,
596 "Cannot allocate redirection storage: not enough memory.\n");
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
607 child_cmd.command = xstrdup (args);
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);
625 #if __DJGPP_MINOR__ < 3
626 save_npx ();
627 #endif
628
629 inferior_pid = SOME_PID;
630 push_target (&go32_ops);
631 clear_proceed_status ();
632 insert_breakpoints ();
633 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
634 prog_has_started = 1;
635 }
636
637 static void
638 go32_mourn_inferior (void)
639 {
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 ();
648 go32_kill_inferior ();
649 generic_mourn_inferior ();
650 }
651
652 static int
653 go32_can_run (void)
654 {
655 return 1;
656 }
657
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
669 #define DR_RW_READWRITE 0x3
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) \
682 (!(CONTROL & (3 << (DR_ENABLE_SIZE * (index)))))
683
684 #define LOCAL_ENABLE_REG(index) \
685 (CONTROL |= (1 << (DR_LOCAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
686
687 #define GLOBAL_ENABLE_REG(index) \
688 (CONTROL |= (1 << (DR_GLOBAL_ENABLE_SHIFT + DR_ENABLE_SIZE * (index))))
689
690 #define DISABLE_REG(index) \
691 (CONTROL &= ~(3 << (DR_ENABLE_SIZE * (index))))
692
693 #define SET_LOCAL_EXACT() \
694 (CONTROL |= DR_LOCAL_SLOWDOWN)
695
696 #define SET_GLOBAL_EXACT() \
697 (CONTROL |= DR_GLOBAL_SLOWDOWN)
698
699 #define RESET_LOCAL_EXACT() \
700 (CONTROL &= ~(DR_LOCAL_SLOWDOWN))
701
702 #define RESET_GLOBAL_EXACT() \
703 (CONTROL &= ~(DR_GLOBAL_SLOWDOWN))
704
705 #define SET_BREAK(index,address) \
706 do {\
707 CONTROL &= ~(DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index)));\
708 D_REGS[index] = address;\
709 dr_ref_count[index]++;\
710 } while(0)
711
712 #define SET_WATCH(index,address,rw,len) \
713 do {\
714 SET_BREAK(index,address);\
715 CONTROL |= ((len)|(rw)) << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE * (index));\
716 } while (0)
717
718 #define IS_WATCH(index) \
719 (CONTROL & (DR_CONTROL_MASK << (DR_CONTROL_SHIFT + DR_CONTROL_SIZE*(index))))
720
721 #define WATCH_HIT(index) ((STATUS & (1 << (index))) && IS_WATCH(index))
722
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) \
729 do { \
730 if (!getenv ("GDB_SHOW_DR")) break; \
731 fprintf(stderr,"%08x %08x ",edi.dr[7],edi.dr[6]); \
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); \
737 } while (0)
738 #else
739 #define SHOW_DR(text,len) do {} while (0)
740 #endif
741
742 static void
743 cleanup_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
756 /* Insert a watchpoint. */
757
758 int
759 go32_insert_watchpoint (int pid ATTRIBUTE_UNUSED, CORE_ADDR addr,
760 int len, int rw)
761 {
762 int ret = go32_insert_aligned_watchpoint (addr, addr, len, rw);
763
764 SHOW_DR (insert_watch, len);
765 return ret;
766 }
767
768 static int
769 go32_insert_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
770 int len, int rw)
771 {
772 int i;
773 int read_write_bits, len_bits;
774
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
802 && DR_DEF (i) == (unsigned)(len_bits | read_write_bits))
803 {
804 dr_ref_count[i]++;
805 return 0;
806 }
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 }
815
816 /* No more debug registers! */
817 if (i > 3)
818 return -1;
819
820 if (len == 2)
821 {
822 if (addr % 2)
823 return go32_handle_nonaligned_watchpoint (wp_insert, waddr, addr,
824 len, rw);
825 }
826 else if (len == 4)
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);
834
835 SET_WATCH (i, addr, read_write_bits, len_bits);
836 LOCAL_ENABLE_REG (i);
837 SET_LOCAL_EXACT ();
838 SET_GLOBAL_EXACT ();
839 return 0;
840 }
841
842 static int
843 go32_handle_nonaligned_watchpoint (wp_op what, CORE_ADDR waddr, CORE_ADDR addr,
844 int len, int rw)
845 {
846 int align;
847 int size;
848 int rv = 0, status = 0;
849
850 static int size_try_array[4][4] =
851 {
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 */
856 };
857
858 while (len > 0)
859 {
860 align = addr % 4;
861 /* Four is the maximum length a 386 debug register can watch. */
862 size = size_try_array[len > 4 ? 3 : len - 1][align];
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
870 status = EINVAL;
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;
881 addr += size;
882 len -= size;
883 }
884 return rv;
885 }
886
887 /* Remove a watchpoint. */
888
889 int
890 go32_remove_watchpoint (int pid ATTRIBUTE_UNUSED, CORE_ADDR addr,
891 int len, int rw)
892 {
893 int ret = go32_remove_aligned_watchpoint (addr, addr, len, rw);
894
895 SHOW_DR (remove_watch, len);
896 return ret;
897 }
898
899 static int
900 go32_remove_aligned_watchpoint (CORE_ADDR waddr, CORE_ADDR addr,
901 int len, int rw)
902 {
903 int i;
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);
941
942 for (i = 0; i <= 3; i++)
943 {
944 if (!IS_REG_FREE (i) && D_REGS[i] == addr
945 && DR_DEF (i) == (unsigned)(len_bits | read_write_bits))
946 {
947 dr_ref_count[i]--;
948 if (dr_ref_count[i] == 0)
949 DISABLE_REG (i);
950 }
951 }
952 RESET_LOCAL_EXACT ();
953 RESET_GLOBAL_EXACT ();
954
955 return 0;
956 }
957
958 /* Can we use debug registers to watch a region whose address is ADDR
959 and whose length is LEN bytes? */
960
961 int
962 go32_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. */
973
974 CORE_ADDR
975 go32_stopped_by_watchpoint (int pid ATTRIBUTE_UNUSED, int data_watchpoint)
976 {
977 int i, ret = 0;
978 int status;
979
980 status = edi.dr[DR_STATUS];
981 SHOW_DR (stopped_by, 0);
982 for (i = 0; i <= 3; i++)
983 {
984 if (WATCH_HIT (i) && data_watchpoint)
985 {
986 SHOW_DR (WP_HIT, 0);
987 ret = D_REGS[i];
988 }
989 }
990
991 return ret;
992 }
993
994 /* Remove a breakpoint. */
995
996 int
997 go32_remove_hw_breakpoint (CORE_ADDR addr, void *shadow ATTRIBUTE_UNUSED)
998 {
999 int i;
1000 for (i = 0; i <= 3; i++)
1001 {
1002 if (!IS_REG_FREE (i) && D_REGS[i] == addr && DR_DEF (i) == 0)
1003 {
1004 dr_ref_count[i]--;
1005 if (dr_ref_count[i] == 0)
1006 DISABLE_REG (i);
1007 }
1008 }
1009 SHOW_DR (remove_hw, 0);
1010 return 0;
1011 }
1012
1013 int
1014 go32_insert_hw_breakpoint (CORE_ADDR addr, void *shadow ATTRIBUTE_UNUSED)
1015 {
1016 int i;
1017
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
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
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);
1045
1046 return i < 4 ? 0 : EBUSY;
1047 }
1048
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
1052 static int
1053 device_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
1083 static int inf_mode_valid = 0;
1084 static 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. */
1090 static int terminal_is_ours = 1;
1091
1092 static void
1093 go32_terminal_init (void)
1094 {
1095 inf_mode_valid = 0; /* reinitialize, in case they are restarting child */
1096 terminal_is_ours = 1;
1097 }
1098
1099 static void
1100 go32_terminal_info (char *args ATTRIBUTE_UNUSED, int from_tty ATTRIBUTE_UNUSED)
1101 {
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++)
1112 {
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);
1124 }
1125 }
1126 #endif
1127 }
1128
1129 static void
1130 go32_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 }
1148 }
1149
1150 static void
1151 go32_terminal_ours (void)
1152 {
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 }
1175 }
1176
1177 static void
1178 init_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;
1186 go32_ops.to_attach = go32_attach;
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;
1197 go32_ops.to_terminal_init = go32_terminal_init;
1198 go32_ops.to_terminal_inferior = go32_terminal_inferior;
1199 go32_ops.to_terminal_ours_for_output = go32_terminal_ours;
1200 go32_ops.to_terminal_ours = go32_terminal_ours;
1201 go32_ops.to_terminal_info = go32_terminal_info;
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;
1214
1215 /* Initialize child's cwd as empty to be initialized when starting
1216 the child. */
1217 *child_cwd = 0;
1218
1219 /* Initialize child's command line storage. */
1220 if (redir_debug_init (&child_cmd) == -1)
1221 internal_error (__FILE__, __LINE__,
1222 "Cannot allocate redirection storage: not enough memory.\n");
1223
1224 /* We are always processing GCC-compiled programs. */
1225 processing_gcc_compilation = 2;
1226 }
1227
1228 void
1229 _initialize_go32_nat (void)
1230 {
1231 init_go32_ops ();
1232 add_target (&go32_ops);
1233 }
1234
1235 pid_t
1236 tcgetpgrp (int fd)
1237 {
1238 if (isatty (fd))
1239 return SOME_PID;
1240 errno = ENOTTY;
1241 return -1;
1242 }
1243
1244 int
1245 tcsetpgrp (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 }
This page took 0.054404 seconds and 3 git commands to generate.