1 /* Native debugging support for Intel x86 running DJGPP.
2 Copyright (C) 1997-2024 Free Software Foundation, Inc.
3 Written by Robert Hoehne.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* To whomever it may concern, here's a general description of how
21 debugging in DJGPP works, and the special quirks GDB does to
24 When the DJGPP port of GDB is debugging a DJGPP program natively,
25 there aren't 2 separate processes, the debuggee and GDB itself, as
26 on other systems. (This is DOS, where there can only be one active
27 process at any given time, remember?) Instead, GDB and the
28 debuggee live in the same process. So when GDB calls
29 go32_create_inferior below, and that function calls edi_init from
30 the DJGPP debug support library libdbg.a, we load the debuggee's
31 executable file into GDB's address space, set it up for execution
32 as the stub loader (a short real-mode program prepended to each
33 DJGPP executable) normally would, and do a lot of preparations for
34 swapping between GDB's and debuggee's internal state, primarily wrt
35 the exception handlers. This swapping happens every time we resume
36 the debuggee or switch back to GDB's code, and it includes:
38 . swapping all the segment registers
39 . swapping the PSP (the Program Segment Prefix)
40 . swapping the signal handlers
41 . swapping the exception handlers
42 . swapping the FPU status
43 . swapping the 3 standard file handles (more about this below)
45 Then running the debuggee simply means longjmp into it where its PC
46 is and let it run until it stops for some reason. When it stops,
47 GDB catches the exception that stopped it and longjmp's back into
48 its own code. All the possible exit points of the debuggee are
49 watched; for example, the normal exit point is recognized because a
50 DOS program issues a special system call to exit. If one of those
51 exit points is hit, we mourn the inferior and clean up after it.
52 Cleaning up is very important, even if the process exits normally,
53 because otherwise we might leave behind traces of previous
54 execution, and in several cases GDB itself might be left hosed,
55 because all the exception handlers were not restored.
57 Swapping of the standard handles (in redir_to_child and
58 redir_to_debugger) is needed because, since both GDB and the
59 debuggee live in the same process, as far as the OS is concerned,
60 the share the same file table. This means that the standard
61 handles 0, 1, and 2 point to the same file table entries, and thus
62 are connected to the same devices. Therefore, if the debugger
63 redirects its standard output, the standard output of the debuggee
64 is also automagically redirected to the same file/device!
65 Similarly, if the debuggee redirects its stdout to a file, you
66 won't be able to see debugger's output (it will go to the same file
67 where the debuggee has its output); and if the debuggee closes its
68 standard input, you will lose the ability to talk to debugger!
70 For this reason, every time the debuggee is about to be resumed, we
71 call redir_to_child, which redirects the standard handles to where
72 the debuggee expects them to be. When the debuggee stops and GDB
73 regains control, we call redir_to_debugger, which redirects those 3
74 handles back to where GDB expects.
76 Note that only the first 3 handles are swapped, so if the debuggee
77 redirects or closes any other handles, GDB will not notice. In
78 particular, the exit code of a DJGPP program forcibly closes all
79 file handles beyond the first 3 ones, so when the debuggee exits,
80 GDB currently loses its stdaux and stdprn streams. Fortunately,
81 GDB does not use those as of this writing, and will never need
91 #include "gdbthread.h"
92 #include "gdbsupport/gdb_wait.h"
96 #include "floatformat.h"
97 #include "buildsym-legacy.h"
98 #include "i387-tdep.h"
99 #include "i386-tdep.h"
100 #include "nat/x86-cpuid.h"
102 #include "regcache.h"
104 #include "cli/cli-utils.h"
105 #include "inf-child.h"
109 #include <sys/utsname.h>
114 #include <sys/farptr.h>
115 #include <debug/v2load.h>
116 #include <debug/dbgcom.h>
117 #if __DJGPP_MINOR__ > 2
118 #include <debug/redir.h>
121 #include <langinfo.h>
123 #if __DJGPP_MINOR__ < 3
124 /* This code will be provided from DJGPP 2.03 on. Until then I code it
132 unsigned short exponent
:15;
133 unsigned short sign
:1;
139 unsigned int control
;
144 unsigned int dataptr
;
145 unsigned int datasel
;
152 static void save_npx (void); /* Save the FPU of the debugged program. */
153 static void load_npx (void); /* Restore the FPU of the debugged program. */
155 /* ------------------------------------------------------------------------- */
156 /* Store the contents of the NPX in the global variable `npx'. */
161 asm ("inb $0xa0, %%al \n\
162 testb $0x20, %%al \n\
179 /* ------------------------------------------------------------------------- */
180 /* Reload the contents of the NPX from the global variable `npx'. */
185 asm ("frstor %0":"=m" (npx
));
187 /* ------------------------------------------------------------------------- */
188 /* Stubs for the missing redirection functions. */
195 redir_cmdline_delete (cmdline_t
*ptr
)
201 redir_cmdline_parse (const char *args
, cmdline_t
*ptr
)
207 redir_to_child (cmdline_t
*ptr
)
213 redir_to_debugger (cmdline_t
*ptr
)
219 redir_debug_init (cmdline_t
*ptr
)
223 #endif /* __DJGPP_MINOR < 3 */
225 typedef enum { wp_insert
, wp_remove
, wp_count
} wp_op
;
227 /* This holds the current reference counts for each debug register. */
228 static int dr_ref_count
[4];
232 static int prog_has_started
= 0;
234 #define r_ofs(x) (offsetof(TSS,x))
243 {r_ofs (tss_eax
), 4}, /* normal registers, from a_tss */
244 {r_ofs (tss_ecx
), 4},
245 {r_ofs (tss_edx
), 4},
246 {r_ofs (tss_ebx
), 4},
247 {r_ofs (tss_esp
), 4},
248 {r_ofs (tss_ebp
), 4},
249 {r_ofs (tss_esi
), 4},
250 {r_ofs (tss_edi
), 4},
251 {r_ofs (tss_eip
), 4},
252 {r_ofs (tss_eflags
), 4},
259 {0, 10}, /* 8 FP registers, from npx.reg[] */
267 /* The order of the next 7 registers must be consistent
268 with their numbering in config/i386/tm-i386.h, which see. */
269 {0, 2}, /* control word, from npx */
270 {4, 2}, /* status word, from npx */
271 {8, 2}, /* tag word, from npx */
272 {16, 2}, /* last FP exception CS from npx */
273 {12, 4}, /* last FP exception EIP from npx */
274 {24, 2}, /* last FP exception operand selector from npx */
275 {20, 4}, /* last FP exception operand offset from npx */
276 {18, 2} /* last FP opcode from npx */
282 enum gdb_signal gdb_sig
;
287 {1, GDB_SIGNAL_TRAP
},
288 /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
289 but I think SIGBUS is better, since the NMI is usually activated
290 as a result of a memory parity check failure. */
292 {3, GDB_SIGNAL_TRAP
},
294 {5, GDB_SIGNAL_SEGV
},
296 {7, GDB_SIGNAL_EMT
}, /* no-coprocessor exception */
297 {8, GDB_SIGNAL_SEGV
},
298 {9, GDB_SIGNAL_SEGV
},
299 {10, GDB_SIGNAL_BUS
},
300 {11, GDB_SIGNAL_SEGV
},
301 {12, GDB_SIGNAL_SEGV
},
302 {13, GDB_SIGNAL_SEGV
},
303 {14, GDB_SIGNAL_SEGV
},
304 {16, GDB_SIGNAL_FPE
},
305 {17, GDB_SIGNAL_BUS
},
306 {31, GDB_SIGNAL_ILL
},
307 {0x1b, GDB_SIGNAL_INT
},
308 {0x75, GDB_SIGNAL_FPE
},
309 {0x78, GDB_SIGNAL_ALRM
},
310 {0x79, GDB_SIGNAL_INT
},
311 {0x7a, GDB_SIGNAL_QUIT
},
312 {-1, GDB_SIGNAL_LAST
}
316 enum gdb_signal gdb_sig
;
320 {GDB_SIGNAL_ILL
, 6}, /* Invalid Opcode */
321 {GDB_SIGNAL_EMT
, 7}, /* triggers SIGNOFP */
322 {GDB_SIGNAL_SEGV
, 13}, /* GPF */
323 {GDB_SIGNAL_BUS
, 17}, /* Alignment Check */
324 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
326 {GDB_SIGNAL_TERM
, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
327 {GDB_SIGNAL_FPE
, 0x75},
328 {GDB_SIGNAL_INT
, 0x79},
329 {GDB_SIGNAL_QUIT
, 0x7a},
330 {GDB_SIGNAL_ALRM
, 0x78}, /* triggers SIGTIMR */
331 {GDB_SIGNAL_PROF
, 0x78},
332 {GDB_SIGNAL_LAST
, -1}
335 /* The go32 target. */
337 struct go32_nat_target final
: public x86_nat_target
<inf_child_target
>
339 void attach (const char *, int) override
;
341 void resume (ptid_t
, int, enum gdb_signal
) override
;
343 ptid_t
wait (ptid_t
, struct target_waitstatus
*, target_wait_flags
) override
;
345 void fetch_registers (struct regcache
*, int) override
;
346 void store_registers (struct regcache
*, int) override
;
348 enum target_xfer_status
xfer_partial (enum target_object object
,
351 const gdb_byte
*writebuf
,
352 ULONGEST offset
, ULONGEST len
,
353 ULONGEST
*xfered_len
) override
;
355 void files_info () override
;
357 void terminal_init () override
;
359 void terminal_inferior () override
;
361 void terminal_ours_for_output () override
;
363 void terminal_ours () override
;
365 void terminal_info (const char *, int) override
;
367 void pass_ctrlc () override
;
369 void kill () override
;
371 void create_inferior (const char *, const std::string
&,
372 char **, int) override
;
374 void mourn_inferior () override
;
376 bool thread_alive (ptid_t ptid
) override
;
378 std::string
pid_to_str (ptid_t
) override
;
381 static go32_nat_target the_go32_nat_target
;
384 go32_nat_target::attach (const char *args
, int from_tty
)
387 You cannot attach to a running program on this platform.\n\
388 Use the `run' command to run DJGPP programs."));
391 static int resume_is_step
;
392 static int resume_signal
= -1;
395 go32_nat_target::resume (ptid_t ptid
, int step
, enum gdb_signal siggnal
)
399 resume_is_step
= step
;
401 if (siggnal
!= GDB_SIGNAL_0
&& siggnal
!= GDB_SIGNAL_TRAP
)
403 for (i
= 0, resume_signal
= -1;
404 excepn_map
[i
].gdb_sig
!= GDB_SIGNAL_LAST
; i
++)
405 if (excepn_map
[i
].gdb_sig
== siggnal
)
407 resume_signal
= excepn_map
[i
].djgpp_excepno
;
410 if (resume_signal
== -1)
411 printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
412 gdb_signal_to_name (siggnal
));
416 static char child_cwd
[FILENAME_MAX
];
419 go32_nat_target::wait (ptid_t ptid
, struct target_waitstatus
*status
,
420 target_wait_flags options
)
423 unsigned char saved_opcode
;
424 unsigned long INT3_addr
= 0;
425 int stepping_over_INT
= 0;
427 a_tss
.tss_eflags
&= 0xfeff; /* Reset the single-step flag (TF). */
430 /* If the next instruction is INT xx or INTO, we need to handle
431 them specially. Intel manuals say that these instructions
432 reset the single-step flag (a.k.a. TF). However, it seems
433 that, at least in the DPMI environment, and at least when
434 stepping over the DPMI interrupt 31h, the problem is having
435 TF set at all when INT 31h is executed: the debuggee either
436 crashes (and takes the system with it) or is killed by a
439 So we need to emulate single-step mode: we put an INT3 opcode
440 right after the INT xx instruction, let the debuggee run
441 until it hits INT3 and stops, then restore the original
442 instruction which we overwrote with the INT3 opcode, and back
443 up the debuggee's EIP to that instruction. */
444 read_child (a_tss
.tss_eip
, &saved_opcode
, 1);
445 if (saved_opcode
== 0xCD || saved_opcode
== 0xCE)
447 unsigned char INT3_opcode
= 0xCC;
450 = saved_opcode
== 0xCD ? a_tss
.tss_eip
+ 2 : a_tss
.tss_eip
+ 1;
451 stepping_over_INT
= 1;
452 read_child (INT3_addr
, &saved_opcode
, 1);
453 write_child (INT3_addr
, &INT3_opcode
, 1);
456 a_tss
.tss_eflags
|= 0x0100; /* normal instruction: set TF */
459 /* The special value FFFFh in tss_trap indicates to run_child that
460 tss_irqn holds a signal to be delivered to the debuggee. */
461 if (resume_signal
<= -1)
464 a_tss
.tss_irqn
= 0xff;
468 a_tss
.tss_trap
= 0xffff; /* run_child looks for this. */
469 a_tss
.tss_irqn
= resume_signal
;
472 /* The child might change working directory behind our back. The
473 GDB users won't like the side effects of that when they work with
474 relative file names, and GDB might be confused by its current
475 directory not being in sync with the truth. So we always make a
476 point of changing back to where GDB thinks is its cwd, when we
477 return control to the debugger, but restore child's cwd before we
479 /* Initialize child_cwd, before the first call to run_child and not
480 in the initialization, so the child get also the changed directory
481 set with the gdb-command "cd ..." */
483 /* Initialize child's cwd with the current one. */
484 getcwd (child_cwd
, sizeof (child_cwd
));
488 #if __DJGPP_MINOR__ < 3
492 #if __DJGPP_MINOR__ < 3
496 /* Did we step over an INT xx instruction? */
497 if (stepping_over_INT
&& a_tss
.tss_eip
== INT3_addr
+ 1)
499 /* Restore the original opcode. */
500 a_tss
.tss_eip
--; /* EIP points *after* the INT3 instruction. */
501 write_child (a_tss
.tss_eip
, &saved_opcode
, 1);
502 /* Simulate a TRAP exception. */
504 a_tss
.tss_eflags
|= 0x0100;
507 getcwd (child_cwd
, sizeof (child_cwd
)); /* in case it has changed */
508 if (current_directory
!= NULL
)
509 chdir (current_directory
);
511 if (a_tss
.tss_irqn
== 0x21)
512 status
->set_exited (a_tss
.tss_eax
& 0xff);
515 status
->set_stopped (GDB_SIGNAL_UNKNOWN
);
516 for (i
= 0; sig_map
[i
].go32_sig
!= -1; i
++)
518 if (a_tss
.tss_irqn
== sig_map
[i
].go32_sig
)
520 #if __DJGPP_MINOR__ < 3
521 status
->set_stopped (sig_map
[i
].gdb_sig
);
522 if (status
->sig () != GDB_SIGNAL_TRAP
)
523 status
->set_signalled (status
->sig ());
525 status
->set_stopped (sig_map
[i
].gdb_sig
);
531 return ptid_t (SOME_PID
);
535 fetch_register (struct regcache
*regcache
, int regno
)
537 struct gdbarch
*gdbarch
= regcache
->arch ();
538 if (regno
< gdbarch_fp0_regnum (gdbarch
))
539 regcache
->raw_supply (regno
,
540 (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
541 else if (i386_fp_regnum_p (gdbarch
, regno
) || i386_fpc_regnum_p (gdbarch
,
543 i387_supply_fsave (regcache
, regno
, &npx
);
545 internal_error (_("Invalid register no. %d in fetch_register."), regno
);
549 go32_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
552 fetch_register (regcache
, regno
);
556 regno
< gdbarch_fp0_regnum (regcache
->arch ());
558 fetch_register (regcache
, regno
);
559 i387_supply_fsave (regcache
, -1, &npx
);
564 store_register (const struct regcache
*regcache
, int regno
)
566 struct gdbarch
*gdbarch
= regcache
->arch ();
567 if (regno
< gdbarch_fp0_regnum (gdbarch
))
568 regcache
->raw_collect (regno
,
569 (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
570 else if (i386_fp_regnum_p (gdbarch
, regno
) || i386_fpc_regnum_p (gdbarch
,
572 i387_collect_fsave (regcache
, regno
, &npx
);
574 internal_error (_("Invalid register no. %d in store_register."), regno
);
578 go32_nat_target::store_registers (struct regcache
*regcache
, int regno
)
583 store_register (regcache
, regno
);
586 for (r
= 0; r
< gdbarch_fp0_regnum (regcache
->arch ()); r
++)
587 store_register (regcache
, r
);
588 i387_collect_fsave (regcache
, -1, &npx
);
592 /* Const-correct version of DJGPP's write_child, which unfortunately
593 takes a non-const buffer pointer. */
596 my_write_child (unsigned child_addr
, const void *buf
, unsigned len
)
598 static void *buffer
= NULL
;
599 static unsigned buffer_len
= 0;
602 if (buffer_len
< len
)
604 buffer
= xrealloc (buffer
, len
);
608 memcpy (buffer
, buf
, len
);
609 res
= write_child (child_addr
, buffer
, len
);
613 /* Helper for go32_xfer_partial that handles memory transfers.
614 Arguments are like target_xfer_partial. */
616 static enum target_xfer_status
617 go32_xfer_memory (gdb_byte
*readbuf
, const gdb_byte
*writebuf
,
618 ULONGEST memaddr
, ULONGEST len
, ULONGEST
*xfered_len
)
622 if (writebuf
!= NULL
)
623 res
= my_write_child (memaddr
, writebuf
, len
);
625 res
= read_child (memaddr
, readbuf
, len
);
627 /* read_child and write_child return zero on success, non-zero on
630 return TARGET_XFER_E_IO
;
633 return TARGET_XFER_OK
;
636 /* Target to_xfer_partial implementation. */
638 enum target_xfer_status
639 go32_nat_target::xfer_partial (enum target_object object
,
640 const char *annex
, gdb_byte
*readbuf
,
641 const gdb_byte
*writebuf
, ULONGEST offset
,
643 ULONGEST
*xfered_len
)
647 case TARGET_OBJECT_MEMORY
:
648 return go32_xfer_memory (readbuf
, writebuf
, offset
, len
, xfered_len
);
651 return this->beneath ()->xfer_partial (object
, annex
,
652 readbuf
, writebuf
, offset
, len
,
657 static cmdline_t child_cmd
; /* Parsed child's command line kept here. */
660 go32_nat_target::files_info ()
662 gdb_printf ("You are running a DJGPP V2 program.\n");
666 go32_nat_target::kill_inferior ()
672 go32_nat_target::create_inferior (const char *exec_file
,
673 const std::string
&allargs
,
674 char **env
, int from_tty
)
676 extern char **environ
;
679 char **env_save
= environ
;
681 struct inferior
*inf
;
683 const char *args
= allargs
.c_str ();
685 /* If no exec file handed to us, get it from the exec-file command -- with
686 a good, common error message if none is specified. */
688 exec_file
= get_exec_file (1);
693 /* Initialize child's cwd as empty to be initialized when starting
697 /* Init command line storage. */
698 if (redir_debug_init (&child_cmd
) == -1)
699 internal_error (_("Cannot allocate redirection storage: "
700 "not enough memory.\n"));
702 /* Parse the command line and create redirections. */
703 if (strpbrk (args
, "<>"))
705 if (redir_cmdline_parse (args
, &child_cmd
) == 0)
706 args
= child_cmd
.command
;
708 error (_("Syntax error in command line."));
711 child_cmd
.command
= xstrdup (args
);
713 cmdlen
= strlen (args
);
714 /* v2loadimage passes command lines via DOS memory, so it cannot
715 possibly handle commands longer than 1MB. */
716 if (cmdlen
> 1024*1024)
717 error (_("Command line too long."));
719 cmdline
= (char *) xmalloc (cmdlen
+ 4);
720 strcpy (cmdline
+ 1, args
);
721 /* If the command-line length fits into DOS 126-char limits, use the
722 DOS command tail format; otherwise, tell v2loadimage to pass it
723 through a buffer in conventional memory. */
726 cmdline
[0] = strlen (args
);
727 cmdline
[cmdlen
+ 1] = 13;
730 cmdline
[0] = 0xff; /* Signal v2loadimage it's a long command. */
734 result
= v2loadimage (exec_file
, cmdline
, start_state
);
740 error (_("Load failed for image %s"), exec_file
);
742 edi_init (start_state
);
743 #if __DJGPP_MINOR__ < 3
747 inf
= current_inferior ();
748 inferior_appeared (inf
, SOME_PID
);
750 if (!inf
->target_is_pushed (this))
751 inf
->push_target (this);
753 thread_info
*thr
= add_thread_silent (ptid_t (SOME_PID
));
754 switch_to_thread (thr
);
756 clear_proceed_status (0);
757 insert_breakpoints ();
758 prog_has_started
= 1;
762 go32_nat_target::mourn_inferior ()
764 redir_cmdline_delete (&child_cmd
);
770 /* We need to make sure all the breakpoint enable bits in the DR7
771 register are reset when the inferior exits. Otherwise, if they
772 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
773 failure to set more watchpoints, and other calamities. It would
774 be nice if GDB itself would take care to remove all breakpoints
775 at all times, but it doesn't, probably under an assumption that
776 the OS cleans up when the debuggee exits. */
777 x86_cleanup_dregs ();
779 prog_has_started
= 0;
781 generic_mourn_inferior ();
782 maybe_unpush_target ();
785 /* Hardware watchpoint support. */
787 #define D_REGS edi.dr
788 #define CONTROL D_REGS[7]
789 #define STATUS D_REGS[6]
791 /* Pass the address ADDR to the inferior in the I'th debug register.
792 Here we just store the address in D_REGS, the watchpoint will be
793 actually set up when go32_wait runs the debuggee. */
795 go32_set_dr (int i
, CORE_ADDR addr
)
798 internal_error (_("Invalid register %d in go32_set_dr.\n"), i
);
802 /* Pass the value VAL to the inferior in the DR7 debug control
803 register. Here we just store the address in D_REGS, the watchpoint
804 will be actually set up when go32_wait runs the debuggee. */
806 go32_set_dr7 (unsigned long val
)
811 /* Get the value of the DR6 debug status register from the inferior.
812 Here we just return the value stored in D_REGS, as we've got it
813 from the last go32_wait call. */
820 /* Get the value of the DR7 debug status register from the inferior.
821 Here we just return the value stored in D_REGS, as we've got it
822 from the last go32_wait call. */
830 /* Get the value of the DR debug register I from the inferior. Here
831 we just return the value stored in D_REGS, as we've got it from the
832 last go32_wait call. */
838 internal_error (_("Invalid register %d in go32_get_dr.\n"), i
);
842 /* Put the device open on handle FD into either raw or cooked
843 mode, return 1 if it was in raw mode, zero otherwise. */
846 device_mode (int fd
, int raw_p
)
848 int oldmode
, newmode
;
853 __dpmi_int (0x21, ®s
);
854 if (regs
.x
.flags
& 1)
856 newmode
= oldmode
= regs
.x
.dx
;
863 if (oldmode
& 0x80) /* Only for character dev. */
867 regs
.x
.dx
= newmode
& 0xff; /* Force upper byte zero, else it fails. */
868 __dpmi_int (0x21, ®s
);
869 if (regs
.x
.flags
& 1)
872 return (oldmode
& 0x20) == 0x20;
876 static int inf_mode_valid
= 0;
877 static int inf_terminal_mode
;
879 /* This semaphore is needed because, amazingly enough, GDB calls
880 target.to_terminal_ours more than once after the inferior stops.
881 But we need the information from the first call only, since the
882 second call will always see GDB's own cooked terminal. */
883 static int terminal_is_ours
= 1;
886 go32_nat_target::terminal_init ()
888 inf_mode_valid
= 0; /* Reinitialize, in case they are restarting child. */
889 terminal_is_ours
= 1;
893 go32_nat_target::terminal_info (const char *args
, int from_tty
)
895 gdb_printf ("Inferior's terminal is in %s mode.\n",
897 ? "default" : inf_terminal_mode
? "raw" : "cooked");
899 #if __DJGPP_MINOR__ > 2
900 if (child_cmd
.redirection
)
904 for (i
= 0; i
< DBG_HANDLES
; i
++)
906 if (child_cmd
.redirection
[i
]->file_name
)
907 gdb_printf ("\tFile handle %d is redirected to `%s'.\n",
908 i
, child_cmd
.redirection
[i
]->file_name
);
909 else if (_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) == -1)
911 ("\tFile handle %d appears to be closed by inferior.\n", i
);
912 /* Mask off the raw/cooked bit when comparing device info words. */
913 else if ((_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) & 0xdf)
914 != (_get_dev_info (i
) & 0xdf))
916 ("\tFile handle %d appears to be redirected by inferior.\n", i
);
923 go32_nat_target::terminal_inferior ()
925 /* Redirect standard handles as child wants them. */
927 if (redir_to_child (&child_cmd
) == -1)
929 redir_to_debugger (&child_cmd
);
930 error (_("Cannot redirect standard handles for program: %s."),
931 safe_strerror (errno
));
933 /* Set the console device of the inferior to whatever mode
934 (raw or cooked) we found it last time. */
935 if (terminal_is_ours
)
938 device_mode (0, inf_terminal_mode
);
939 terminal_is_ours
= 0;
944 go32_nat_target::terminal_ours ()
946 /* Switch to cooked mode on the gdb terminal and save the inferior
947 terminal mode to be restored when it is resumed. */
948 if (!terminal_is_ours
)
950 inf_terminal_mode
= device_mode (0, 0);
951 if (inf_terminal_mode
!= -1)
954 /* If device_mode returned -1, we don't know what happens with
955 handle 0 anymore, so make the info invalid. */
957 terminal_is_ours
= 1;
959 /* Restore debugger's standard handles. */
961 if (redir_to_debugger (&child_cmd
) == -1)
963 redir_to_child (&child_cmd
);
964 error (_("Cannot redirect standard handles for debugger: %s."),
965 safe_strerror (errno
));
971 go32_nat_target::pass_ctrlc ()
976 go32_nat_target::thread_alive (ptid_t ptid
)
978 return ptid
!= null_ptid
;
982 go32_nat_target::pid_to_str (ptid_t ptid
)
984 return normal_pid_to_str (ptid
);
987 /* Return the current DOS codepage number. */
994 __dpmi_int (0x21, ®s
);
995 if (!(regs
.x
.flags
& 1))
996 return regs
.x
.bx
& 0xffff;
998 return 437; /* default */
1001 /* Limited emulation of `nl_langinfo', for charset.c. */
1003 nl_langinfo (nl_item item
)
1011 /* 8 is enough for SHORT_MAX + "CP" + null. */
1013 int blen
= sizeof (buf
);
1014 int needed
= snprintf (buf
, blen
, "CP%d", dos_codepage ());
1016 if (needed
> blen
) /* Should never happen. */
1018 retval
= xstrdup (buf
);
1022 retval
= xstrdup ("");
1028 unsigned short windows_major
, windows_minor
;
1030 /* Compute the version Windows reports via Int 2Fh/AX=1600h. */
1032 go32_get_windows_version(void)
1037 __dpmi_int(0x2f, &r
);
1038 if (r
.h
.al
> 2 && r
.h
.al
!= 0x80 && r
.h
.al
!= 0xff
1039 && (r
.h
.al
> 3 || r
.h
.ah
> 0))
1041 windows_major
= r
.h
.al
;
1042 windows_minor
= r
.h
.ah
;
1045 windows_major
= 0xff; /* meaning no Windows */
1048 /* A subroutine of go32_sysinfo to display memory info. */
1050 print_mem (unsigned long datum
, const char *header
, int in_pages_p
)
1052 if (datum
!= 0xffffffffUL
)
1059 gdb_printf ("%lu KB", datum
>> 10);
1060 if (datum
> 1024 * 1024)
1061 gdb_printf (" (%lu MB)", datum
>> 20);
1064 gdb_printf ("%lu Bytes", datum
);
1069 /* Display assorted information about the underlying OS. */
1071 go32_sysinfo (const char *arg
, int from_tty
)
1073 static const char test_pattern
[] =
1074 "deadbeafdeadbeafdeadbeafdeadbeafdeadbeaf"
1075 "deadbeafdeadbeafdeadbeafdeadbeafdeadbeaf"
1076 "deadbeafdeadbeafdeadbeafdeadbeafdeadbeafdeadbeaf";
1078 char cpuid_vendor
[13];
1079 unsigned cpuid_max
= 0, cpuid_eax
, cpuid_ebx
, cpuid_ecx
, cpuid_edx
;
1080 unsigned true_dos_version
= _get_dos_version (1);
1081 unsigned advertized_dos_version
= ((unsigned int)_osmajor
<< 8) | _osminor
;
1083 char dpmi_vendor_info
[129];
1084 int dpmi_vendor_available
;
1085 __dpmi_version_ret dpmi_version_data
;
1087 __dpmi_free_mem_info mem_info
;
1090 cpuid_vendor
[0] = '\0';
1092 strcpy (u
.machine
, "Unknown x86");
1093 else if (u
.machine
[0] == 'i' && u
.machine
[1] > 4)
1095 /* CPUID with EAX = 0 returns the Vendor ID. */
1097 /* Ideally we would use x86_cpuid(), but it needs someone to run
1098 native tests first to make sure things actually work. They should.
1099 http://sourceware.org/ml/gdb-patches/2013-05/msg00164.html */
1100 unsigned int eax
, ebx
, ecx
, edx
;
1102 if (x86_cpuid (0, &eax
, &ebx
, &ecx
, &edx
))
1105 memcpy (&vendor
[0], &ebx
, 4);
1106 memcpy (&vendor
[4], &ecx
, 4);
1107 memcpy (&vendor
[8], &edx
, 4);
1108 cpuid_vendor
[12] = '\0';
1111 __asm__
__volatile__ ("xorl %%ebx, %%ebx;"
1112 "xorl %%ecx, %%ecx;"
1113 "xorl %%edx, %%edx;"
1120 : "=m" (cpuid_vendor
[0]),
1121 "=m" (cpuid_vendor
[4]),
1122 "=m" (cpuid_vendor
[8]),
1125 : "%eax", "%ebx", "%ecx", "%edx");
1126 cpuid_vendor
[12] = '\0';
1130 gdb_printf ("CPU Type.......................%s", u
.machine
);
1131 if (cpuid_vendor
[0])
1132 gdb_printf (" (%s)", cpuid_vendor
);
1135 /* CPUID with EAX = 1 returns processor signature and features. */
1138 static const char *brand_name
[] = {
1146 char cpu_string
[80];
1149 int intel_p
= strcmp (cpuid_vendor
, "GenuineIntel") == 0;
1150 int amd_p
= strcmp (cpuid_vendor
, "AuthenticAMD") == 0;
1151 int hygon_p
= strcmp (cpuid_vendor
, "HygonGenuine") == 0;
1152 unsigned cpu_family
, cpu_model
;
1155 /* See comment above about cpuid usage. */
1156 x86_cpuid (1, &cpuid_eax
, &cpuid_ebx
, NULL
, &cpuid_edx
);
1158 __asm__
__volatile__ ("movl $1, %%eax;"
1166 brand_idx
= cpuid_ebx
& 0xff;
1167 cpu_family
= (cpuid_eax
>> 8) & 0xf;
1168 cpu_model
= (cpuid_eax
>> 4) & 0xf;
1169 cpu_brand
[0] = '\0';
1173 && brand_idx
< sizeof(brand_name
)/sizeof(brand_name
[0])
1174 && *brand_name
[brand_idx
])
1175 strcpy (cpu_brand
, brand_name
[brand_idx
]);
1176 else if (cpu_family
== 5)
1178 if (((cpuid_eax
>> 12) & 3) == 0 && cpu_model
== 4)
1179 strcpy (cpu_brand
, " MMX");
1180 else if (cpu_model
> 1 && ((cpuid_eax
>> 12) & 3) == 1)
1181 strcpy (cpu_brand
, " OverDrive");
1182 else if (cpu_model
> 1 && ((cpuid_eax
>> 12) & 3) == 2)
1183 strcpy (cpu_brand
, " Dual");
1185 else if (cpu_family
== 6 && cpu_model
< 8)
1190 strcpy (cpu_brand
, " Pro");
1193 strcpy (cpu_brand
, " II");
1196 strcpy (cpu_brand
, " II Xeon");
1199 strcpy (cpu_brand
, " Celeron");
1202 strcpy (cpu_brand
, " III");
1212 strcpy (cpu_brand
, "486/5x86");
1221 strcpy (cpu_brand
, "-K5");
1225 strcpy (cpu_brand
, "-K6");
1228 strcpy (cpu_brand
, "-K6-2");
1231 strcpy (cpu_brand
, "-K6-III");
1241 strcpy (cpu_brand
, " Athlon");
1244 strcpy (cpu_brand
, " Duron");
1250 xsnprintf (cpu_string
, sizeof (cpu_string
), "%s%s Model %d Stepping %d",
1251 intel_p
? "Pentium" : (amd_p
? "AMD" : (hygon_p
? "Hygon" : "ix86")),
1252 cpu_brand
, cpu_model
, cpuid_eax
& 0xf);
1253 gdb_printf ("%*s%s\n", 31, "", cpu_string
);
1254 if (((cpuid_edx
& (6 | (0x0d << 23))) != 0)
1255 || ((cpuid_edx
& 1) == 0)
1256 || ((amd_p
|| hygon_p
) && (cpuid_edx
& (3 << 30)) != 0))
1258 gdb_puts ("CPU Features...................");
1259 /* We only list features which might be useful in the DPMI
1261 if ((cpuid_edx
& 1) == 0)
1262 gdb_puts ("No FPU "); /* It's unusual to not have an FPU. */
1263 if ((cpuid_edx
& (1 << 1)) != 0)
1265 if ((cpuid_edx
& (1 << 2)) != 0)
1267 if ((cpuid_edx
& (1 << 4)) != 0)
1269 if ((cpuid_edx
& (1 << 23)) != 0)
1271 if ((cpuid_edx
& (1 << 25)) != 0)
1273 if ((cpuid_edx
& (1 << 26)) != 0)
1275 if (amd_p
|| hygon_p
)
1277 if ((cpuid_edx
& (1 << 31)) != 0)
1278 gdb_puts ("3DNow! ");
1279 if ((cpuid_edx
& (1 << 30)) != 0)
1280 gdb_puts ("3DNow!Ext");
1286 gdb_printf ("DOS Version....................%s %s.%s",
1287 _os_flavor
, u
.release
, u
.version
);
1288 if (true_dos_version
!= advertized_dos_version
)
1289 gdb_printf (" (disguised as v%d.%d)", _osmajor
, _osminor
);
1292 go32_get_windows_version ();
1293 if (windows_major
!= 0xff)
1295 const char *windows_flavor
;
1297 gdb_printf ("Windows Version................%d.%02d (Windows ",
1298 windows_major
, windows_minor
);
1299 switch (windows_major
)
1302 windows_flavor
= "3.X";
1305 switch (windows_minor
)
1308 windows_flavor
= "95, 95A, or 95B";
1311 windows_flavor
= "95B OSR2.1 or 95C OSR2.5";
1314 windows_flavor
= "98 or 98 SE";
1317 windows_flavor
= "ME";
1320 windows_flavor
= "9X";
1325 windows_flavor
= "??";
1328 gdb_printf ("%s)\n", windows_flavor
);
1330 else if (true_dos_version
== 0x532 && advertized_dos_version
== 0x500)
1331 gdb_printf ("Windows Version................"
1332 "Windows NT family (W2K/XP/W2K3/Vista/W2K8)\n");
1334 /* On some versions of Windows, __dpmi_get_capabilities returns
1335 zero, but the buffer is not filled with info, so we fill the
1336 buffer with a known pattern and test for it afterwards. */
1337 memcpy (dpmi_vendor_info
, test_pattern
, sizeof(dpmi_vendor_info
));
1338 dpmi_vendor_available
=
1339 __dpmi_get_capabilities (&dpmi_flags
, dpmi_vendor_info
);
1340 if (dpmi_vendor_available
== 0
1341 && memcmp (dpmi_vendor_info
, test_pattern
,
1342 sizeof(dpmi_vendor_info
)) != 0)
1344 /* The DPMI spec says the vendor string should be ASCIIZ, but
1345 I don't trust the vendors to follow that... */
1346 if (!memchr (&dpmi_vendor_info
[2], 0, 126))
1347 dpmi_vendor_info
[128] = '\0';
1348 gdb_printf ("DPMI Host......................"
1349 "%s v%d.%d (capabilities: %#x)\n",
1350 &dpmi_vendor_info
[2],
1351 (unsigned)dpmi_vendor_info
[0],
1352 (unsigned)dpmi_vendor_info
[1],
1353 ((unsigned)dpmi_flags
& 0x7f));
1356 gdb_printf ("DPMI Host......................(Info not available)\n");
1357 __dpmi_get_version (&dpmi_version_data
);
1358 gdb_printf ("DPMI Version...................%d.%02d\n",
1359 dpmi_version_data
.major
, dpmi_version_data
.minor
);
1360 gdb_printf ("DPMI Info......................"
1361 "%s-bit DPMI, with%s Virtual Memory support\n",
1362 (dpmi_version_data
.flags
& 1) ? "32" : "16",
1363 (dpmi_version_data
.flags
& 4) ? "" : "out");
1364 gdb_printf ("%*sInterrupts reflected to %s mode\n", 31, "",
1365 (dpmi_version_data
.flags
& 2) ? "V86" : "Real");
1366 gdb_printf ("%*sProcessor type: i%d86\n", 31, "",
1367 dpmi_version_data
.cpu
);
1368 gdb_printf ("%*sPIC base interrupt: Master: %#x Slave: %#x\n", 31, "",
1369 dpmi_version_data
.master_pic
, dpmi_version_data
.slave_pic
);
1371 /* a_tss is only initialized when the debuggee is first run. */
1372 if (prog_has_started
)
1374 __asm__
__volatile__ ("pushfl ; popl %0" : "=g" (eflags
));
1375 gdb_printf ("Protection....................."
1376 "Ring %d (in %s), with%s I/O protection\n",
1377 a_tss
.tss_cs
& 3, (a_tss
.tss_cs
& 4) ? "LDT" : "GDT",
1378 (a_tss
.tss_cs
& 3) > ((eflags
>> 12) & 3) ? "" : "out");
1381 __dpmi_get_free_memory_information (&mem_info
);
1382 print_mem (mem_info
.total_number_of_physical_pages
,
1383 "DPMI Total Physical Memory.....", 1);
1384 print_mem (mem_info
.total_number_of_free_pages
,
1385 "DPMI Free Physical Memory......", 1);
1386 print_mem (mem_info
.size_of_paging_file_partition_in_pages
,
1387 "DPMI Swap Space................", 1);
1388 print_mem (mem_info
.linear_address_space_size_in_pages
,
1389 "DPMI Total Linear Address Size.", 1);
1390 print_mem (mem_info
.free_linear_address_space_in_pages
,
1391 "DPMI Free Linear Address Size..", 1);
1392 print_mem (mem_info
.largest_available_free_block_in_bytes
,
1393 "DPMI Largest Free Memory Block.", 0);
1397 __dpmi_int (0x21, ®s
);
1398 print_mem (regs
.x
.bx
<< 4, "Free DOS Memory................", 0);
1400 __dpmi_int (0x21, ®s
);
1401 if ((regs
.x
.flags
& 1) == 0)
1403 static const char *dos_hilo
[] = {
1404 "Low", "", "", "", "High", "", "", "", "High, then Low"
1406 static const char *dos_fit
[] = {
1407 "First", "Best", "Last"
1409 int hilo_idx
= (regs
.x
.ax
>> 4) & 0x0f;
1410 int fit_idx
= regs
.x
.ax
& 0x0f;
1416 gdb_printf ("DOS Memory Allocation..........%s memory, %s fit\n",
1417 dos_hilo
[hilo_idx
], dos_fit
[fit_idx
]);
1419 __dpmi_int (0x21, ®s
);
1420 if ((regs
.x
.flags
& 1) != 0)
1422 gdb_printf ("%*sUMBs %sin DOS memory chain\n", 31, "",
1423 regs
.h
.al
== 0 ? "not " : "");
1428 unsigned short limit0
;
1429 unsigned short base0
;
1430 unsigned char base1
;
1435 unsigned available
:1;
1438 unsigned page_granular
:1;
1439 unsigned char base2
;
1440 } __attribute__ ((packed
));
1443 unsigned short offset0
;
1444 unsigned short selector
;
1445 unsigned param_count
:5;
1450 unsigned short offset1
;
1451 } __attribute__ ((packed
));
1453 /* Read LEN bytes starting at logical address ADDR, and put the result
1454 into DEST. Return 1 if success, zero if not. */
1456 read_memory_region (unsigned long addr
, void *dest
, size_t len
)
1458 unsigned long dos_ds_limit
= __dpmi_get_segment_limit (_dos_ds
);
1461 /* For the low memory, we can simply use _dos_ds. */
1462 if (addr
<= dos_ds_limit
- len
)
1463 dosmemget (addr
, len
, dest
);
1466 /* For memory above 1MB we need to set up a special segment to
1467 be able to access that memory. */
1468 int sel
= __dpmi_allocate_ldt_descriptors (1);
1474 int access_rights
= __dpmi_get_descriptor_access_rights (sel
);
1475 size_t segment_limit
= len
- 1;
1477 /* Make sure the crucial bits in the descriptor access
1478 rights are set correctly. Some DPMI providers might barf
1479 if we set the segment limit to something that is not an
1480 integral multiple of 4KB pages if the granularity bit is
1481 not set to byte-granular, even though the DPMI spec says
1482 it's the host's responsibility to set that bit correctly. */
1483 if (len
> 1024 * 1024)
1485 access_rights
|= 0x8000;
1486 /* Page-granular segments should have the low 12 bits of
1488 segment_limit
|= 0xfff;
1491 access_rights
&= ~0x8000;
1493 if (__dpmi_set_segment_base_address (sel
, addr
) != -1
1494 && __dpmi_set_descriptor_access_rights (sel
, access_rights
) != -1
1495 && __dpmi_set_segment_limit (sel
, segment_limit
) != -1
1496 /* W2K silently fails to set the segment limit, leaving
1497 it at zero; this test avoids the resulting crash. */
1498 && __dpmi_get_segment_limit (sel
) >= segment_limit
)
1499 movedata (sel
, 0, _my_ds (), (unsigned)dest
, len
);
1503 __dpmi_free_ldt_descriptor (sel
);
1509 /* Get a segment descriptor stored at index IDX in the descriptor
1510 table whose base address is TABLE_BASE. Return the descriptor
1511 type, or -1 if failure. */
1513 get_descriptor (unsigned long table_base
, int idx
, void *descr
)
1515 unsigned long addr
= table_base
+ idx
* 8; /* 8 bytes per entry */
1517 if (read_memory_region (addr
, descr
, 8))
1518 return (int)((struct seg_descr
*)descr
)->stype
;
1523 unsigned short limit
__attribute__((packed
));
1524 unsigned long base
__attribute__((packed
));
1527 /* Display a segment descriptor stored at index IDX in a descriptor
1528 table whose type is TYPE and whose base address is BASE_ADDR. If
1529 FORCE is non-zero, display even invalid descriptors. */
1531 display_descriptor (unsigned type
, unsigned long base_addr
, int idx
, int force
)
1533 struct seg_descr descr
;
1534 struct gate_descr gate
;
1536 /* Get the descriptor from the table. */
1537 if (idx
== 0 && type
== 0)
1538 gdb_puts ("0x000: null descriptor\n");
1539 else if (get_descriptor (base_addr
, idx
, &descr
) != -1)
1541 /* For each type of descriptor table, this has a bit set if the
1542 corresponding type of selectors is valid in that table. */
1543 static unsigned allowed_descriptors
[] = {
1544 0xffffdafeL
, /* GDT */
1545 0x0000c0e0L
, /* IDT */
1546 0xffffdafaL
/* LDT */
1549 /* If the program hasn't started yet, assume the debuggee will
1550 have the same CPL as the debugger. */
1551 int cpl
= prog_has_started
? (a_tss
.tss_cs
& 3) : _my_cs () & 3;
1552 unsigned long limit
= (descr
.limit1
<< 16) | descr
.limit0
;
1555 && (allowed_descriptors
[type
] & (1 << descr
.stype
)) != 0)
1557 gdb_printf ("0x%03x: ",
1559 ? idx
: (idx
* 8) | (type
? (cpl
| 4) : 0));
1560 if (descr
.page_granular
)
1561 limit
= (limit
<< 12) | 0xfff; /* big segment: low 12 bit set */
1562 if (descr
.stype
== 1 || descr
.stype
== 2 || descr
.stype
== 3
1563 || descr
.stype
== 9 || descr
.stype
== 11
1564 || (descr
.stype
>= 16 && descr
.stype
< 32))
1565 gdb_printf ("base=0x%02x%02x%04x limit=0x%08lx",
1566 descr
.base2
, descr
.base1
, descr
.base0
, limit
);
1568 switch (descr
.stype
)
1572 gdb_printf (" 16-bit TSS (task %sactive)",
1573 descr
.stype
== 3 ? "" : "in");
1579 memcpy (&gate
, &descr
, sizeof gate
);
1580 gdb_printf ("selector=0x%04x offs=0x%04x%04x",
1581 gate
.selector
, gate
.offset1
, gate
.offset0
);
1582 gdb_printf (" 16-bit Call Gate (params=%d)",
1586 gdb_printf ("TSS selector=0x%04x", descr
.base0
);
1587 gdb_printf ("%*sTask Gate", 16, "");
1591 memcpy (&gate
, &descr
, sizeof gate
);
1592 gdb_printf ("selector=0x%04x offs=0x%04x%04x",
1593 gate
.selector
, gate
.offset1
, gate
.offset0
);
1594 gdb_printf (" 16-bit %s Gate",
1595 descr
.stype
== 6 ? "Interrupt" : "Trap");
1599 gdb_printf (" 32-bit TSS (task %sactive)",
1600 descr
.stype
== 3 ? "" : "in");
1603 memcpy (&gate
, &descr
, sizeof gate
);
1604 gdb_printf ("selector=0x%04x offs=0x%04x%04x",
1605 gate
.selector
, gate
.offset1
, gate
.offset0
);
1606 gdb_printf (" 32-bit Call Gate (params=%d)",
1611 memcpy (&gate
, &descr
, sizeof gate
);
1612 gdb_printf ("selector=0x%04x offs=0x%04x%04x",
1613 gate
.selector
, gate
.offset1
, gate
.offset0
);
1614 gdb_printf (" 32-bit %s Gate",
1615 descr
.stype
== 14 ? "Interrupt" : "Trap");
1617 case 16: /* data segments */
1625 gdb_printf (" %s-bit Data (%s Exp-%s%s)",
1626 descr
.bit32
? "32" : "16",
1628 ? "Read/Write," : "Read-Only, ",
1629 descr
.stype
& 4 ? "down" : "up",
1630 descr
.stype
& 1 ? "" : ", N.Acc");
1632 case 24: /* code segments */
1640 gdb_printf (" %s-bit Code (%s, %sConf%s)",
1641 descr
.bit32
? "32" : "16",
1642 descr
.stype
& 2 ? "Exec/Read" : "Exec-Only",
1643 descr
.stype
& 4 ? "" : "N.",
1644 descr
.stype
& 1 ? "" : ", N.Acc");
1647 gdb_printf ("Unknown type 0x%02x", descr
.stype
);
1654 gdb_printf ("0x%03x: ",
1656 ? idx
: (idx
* 8) | (type
? (cpl
| 4) : 0));
1658 gdb_puts ("Segment not present\n");
1660 gdb_printf ("Segment type 0x%02x is invalid in this table\n",
1665 gdb_printf ("0x%03x: Cannot read this descriptor\n", idx
);
1669 go32_sldt (const char *arg
, int from_tty
)
1671 struct dtr_reg gdtr
;
1672 unsigned short ldtr
= 0;
1674 struct seg_descr ldt_descr
;
1675 long ldt_entry
= -1L;
1676 int cpl
= (prog_has_started
? a_tss
.tss_cs
: _my_cs ()) & 3;
1680 arg
= skip_spaces (arg
);
1684 ldt_entry
= parse_and_eval_long (arg
);
1686 || (ldt_entry
& 4) == 0
1687 || (ldt_entry
& 3) != (cpl
& 3))
1688 error (_("Invalid LDT entry 0x%03lx."), (unsigned long)ldt_entry
);
1692 __asm__
__volatile__ ("sgdt %0" : "=m" (gdtr
) : /* no inputs */ );
1693 __asm__
__volatile__ ("sldt %0" : "=m" (ldtr
) : /* no inputs */ );
1696 gdb_puts ("There is no LDT.\n");
1697 /* LDT's entry in the GDT must have the type LDT, which is 2. */
1698 else if (get_descriptor (gdtr
.base
, ldt_idx
, &ldt_descr
) != 2)
1699 gdb_printf ("LDT is present (at %#x), but unreadable by GDB.\n",
1701 | (ldt_descr
.base1
<< 16)
1702 | (ldt_descr
.base2
<< 24));
1707 | (ldt_descr
.base1
<< 16)
1708 | (ldt_descr
.base2
<< 24);
1709 unsigned limit
= ldt_descr
.limit0
| (ldt_descr
.limit1
<< 16);
1712 if (ldt_descr
.page_granular
)
1713 /* Page-granular segments must have the low 12 bits of their
1715 limit
= (limit
<< 12) | 0xfff;
1716 /* LDT cannot have more than 8K 8-byte entries, i.e. more than
1721 max_entry
= (limit
+ 1) / 8;
1725 if (ldt_entry
> limit
)
1726 error (_("Invalid LDT entry %#lx: outside valid limits [0..%#x]"),
1727 (unsigned long)ldt_entry
, limit
);
1729 display_descriptor (ldt_descr
.stype
, base
, ldt_entry
/ 8, 1);
1735 for (i
= 0; i
< max_entry
; i
++)
1736 display_descriptor (ldt_descr
.stype
, base
, i
, 0);
1742 go32_sgdt (const char *arg
, int from_tty
)
1744 struct dtr_reg gdtr
;
1745 long gdt_entry
= -1L;
1750 arg
= skip_spaces (arg
);
1754 gdt_entry
= parse_and_eval_long (arg
);
1755 if (gdt_entry
< 0 || (gdt_entry
& 7) != 0)
1756 error (_("Invalid GDT entry 0x%03lx: "
1757 "not an integral multiple of 8."),
1758 (unsigned long)gdt_entry
);
1762 __asm__
__volatile__ ("sgdt %0" : "=m" (gdtr
) : /* no inputs */ );
1763 max_entry
= (gdtr
.limit
+ 1) / 8;
1767 if (gdt_entry
> gdtr
.limit
)
1768 error (_("Invalid GDT entry %#lx: outside valid limits [0..%#x]"),
1769 (unsigned long)gdt_entry
, gdtr
.limit
);
1771 display_descriptor (0, gdtr
.base
, gdt_entry
/ 8, 1);
1777 for (i
= 0; i
< max_entry
; i
++)
1778 display_descriptor (0, gdtr
.base
, i
, 0);
1783 go32_sidt (const char *arg
, int from_tty
)
1785 struct dtr_reg idtr
;
1786 long idt_entry
= -1L;
1791 arg
= skip_spaces (arg
);
1795 idt_entry
= parse_and_eval_long (arg
);
1797 error (_("Invalid (negative) IDT entry %ld."), idt_entry
);
1801 __asm__
__volatile__ ("sidt %0" : "=m" (idtr
) : /* no inputs */ );
1802 max_entry
= (idtr
.limit
+ 1) / 8;
1803 if (max_entry
> 0x100) /* No more than 256 entries. */
1808 if (idt_entry
> idtr
.limit
)
1809 error (_("Invalid IDT entry %#lx: outside valid limits [0..%#x]"),
1810 (unsigned long)idt_entry
, idtr
.limit
);
1812 display_descriptor (1, idtr
.base
, idt_entry
, 1);
1818 for (i
= 0; i
< max_entry
; i
++)
1819 display_descriptor (1, idtr
.base
, i
, 0);
1823 /* Cached linear address of the base of the page directory. For
1824 now, available only under CWSDPMI. Code based on ideas and
1825 suggestions from Charles Sandmann <sandmann@clio.rice.edu>. */
1826 static unsigned long pdbr
;
1828 static unsigned long
1833 unsigned long taskbase
, cr3
;
1834 struct dtr_reg gdtr
;
1836 if (pdbr
> 0 && pdbr
<= 0xfffff)
1839 /* Get the linear address of GDT and the Task Register. */
1840 __asm__
__volatile__ ("sgdt %0" : "=m" (gdtr
) : /* no inputs */ );
1841 __asm__
__volatile__ ("str %0" : "=m" (taskreg
) : /* no inputs */ );
1843 /* Task Register is a segment selector for the TSS of the current
1844 task. Therefore, it can be used as an index into the GDT to get
1845 at the segment descriptor for the TSS. To get the index, reset
1846 the low 3 bits of the selector (which give the CPL). Add 2 to the
1847 offset to point to the 3 low bytes of the base address. */
1848 offset
= gdtr
.base
+ (taskreg
& 0xfff8) + 2;
1851 /* CWSDPMI's task base is always under the 1MB mark. */
1852 if (offset
> 0xfffff)
1855 _farsetsel (_dos_ds
);
1856 taskbase
= _farnspeekl (offset
) & 0xffffffU
;
1857 taskbase
+= _farnspeekl (offset
+ 2) & 0xff000000U
;
1858 if (taskbase
> 0xfffff)
1861 /* CR3 (a.k.a. PDBR, the Page Directory Base Register) is stored at
1862 offset 1Ch in the TSS. */
1863 cr3
= _farnspeekl (taskbase
+ 0x1c) & ~0xfff;
1866 #if 0 /* Not fully supported yet. */
1867 /* The Page Directory is in UMBs. In that case, CWSDPMI puts
1868 the first Page Table right below the Page Directory. Thus,
1869 the first Page Table's entry for its own address and the Page
1870 Directory entry for that Page Table will hold the same
1871 physical address. The loop below searches the entire UMB
1872 range of addresses for such an occurrence. */
1873 unsigned long addr
, pte_idx
;
1875 for (addr
= 0xb0000, pte_idx
= 0xb0;
1877 addr
+= 0x1000, pte_idx
++)
1879 if (((_farnspeekl (addr
+ 4 * pte_idx
) & 0xfffff027) ==
1880 (_farnspeekl (addr
+ 0x1000) & 0xfffff027))
1881 && ((_farnspeekl (addr
+ 4 * pte_idx
+ 4) & 0xfffff000) == cr3
))
1883 cr3
= addr
+ 0x1000;
1896 /* Return the N'th Page Directory entry. */
1897 static unsigned long
1900 unsigned long pde
= 0;
1902 if (pdbr
&& n
>= 0 && n
< 1024)
1904 pde
= _farpeekl (_dos_ds
, pdbr
+ 4*n
);
1909 /* Return the N'th entry of the Page Table whose Page Directory entry
1911 static unsigned long
1912 get_pte (unsigned long pde
, int n
)
1914 unsigned long pte
= 0;
1916 /* pde & 0x80 tests the 4MB page bit. We don't support 4MB
1917 page tables, for now. */
1918 if ((pde
& 1) && !(pde
& 0x80) && n
>= 0 && n
< 1024)
1920 pde
&= ~0xfff; /* Clear non-address bits. */
1921 pte
= _farpeekl (_dos_ds
, pde
+ 4*n
);
1926 /* Display a Page Directory or Page Table entry. IS_DIR, if non-zero,
1927 says this is a Page Directory entry. If FORCE is non-zero, display
1928 the entry even if its Present flag is off. OFF is the offset of the
1929 address from the page's base address. */
1931 display_ptable_entry (unsigned long entry
, int is_dir
, int force
, unsigned off
)
1933 if ((entry
& 1) != 0)
1935 gdb_printf ("Base=0x%05lx000", entry
>> 12);
1936 if ((entry
& 0x100) && !is_dir
)
1937 gdb_puts (" Global");
1938 if ((entry
& 0x40) && !is_dir
)
1939 gdb_puts (" Dirty");
1940 gdb_printf (" %sAcc.", (entry
& 0x20) ? "" : "Not-");
1941 gdb_printf (" %sCached", (entry
& 0x10) ? "" : "Not-");
1942 gdb_printf (" Write-%s", (entry
& 8) ? "Thru" : "Back");
1943 gdb_printf (" %s", (entry
& 4) ? "Usr" : "Sup");
1944 gdb_printf (" Read-%s", (entry
& 2) ? "Write" : "Only");
1946 gdb_printf (" +0x%x", off
);
1950 gdb_printf ("Page%s not present or not supported; value=0x%lx.\n",
1951 is_dir
? " Table" : "", entry
>> 1);
1955 go32_pde (const char *arg
, int from_tty
)
1957 long pde_idx
= -1, i
;
1961 arg
= skip_spaces (arg
);
1965 pde_idx
= parse_and_eval_long (arg
);
1966 if (pde_idx
< 0 || pde_idx
>= 1024)
1967 error (_("Entry %ld is outside valid limits [0..1023]."), pde_idx
);
1973 gdb_puts ("Access to Page Directories is "
1974 "not supported on this system.\n");
1975 else if (pde_idx
>= 0)
1976 display_ptable_entry (get_pde (pde_idx
), 1, 1, 0);
1978 for (i
= 0; i
< 1024; i
++)
1979 display_ptable_entry (get_pde (i
), 1, 0, 0);
1982 /* A helper function to display entries in a Page Table pointed to by
1983 the N'th entry in the Page Directory. If FORCE is non-zero, say
1984 something even if the Page Table is not accessible. */
1986 display_page_table (long n
, int force
)
1988 unsigned long pde
= get_pde (n
);
1994 gdb_printf ("Page Table pointed to by "
1995 "Page Directory entry 0x%lx:\n", n
);
1996 for (i
= 0; i
< 1024; i
++)
1997 display_ptable_entry (get_pte (pde
, i
), 0, 0, 0);
2001 gdb_printf ("Page Table not present; value=0x%lx.\n", pde
>> 1);
2005 go32_pte (const char *arg
, int from_tty
)
2007 long pde_idx
= -1L, i
;
2011 arg
= skip_spaces (arg
);
2015 pde_idx
= parse_and_eval_long (arg
);
2016 if (pde_idx
< 0 || pde_idx
>= 1024)
2017 error (_("Entry %ld is outside valid limits [0..1023]."), pde_idx
);
2023 gdb_puts ("Access to Page Tables is not supported on this system.\n");
2024 else if (pde_idx
>= 0)
2025 display_page_table (pde_idx
, 1);
2027 for (i
= 0; i
< 1024; i
++)
2028 display_page_table (i
, 0);
2032 go32_pte_for_address (const char *arg
, int from_tty
)
2034 CORE_ADDR addr
= 0, i
;
2038 arg
= skip_spaces (arg
);
2041 addr
= parse_and_eval_address (arg
);
2044 error_no_arg (_("linear address"));
2048 gdb_puts ("Access to Page Tables is not supported on this system.\n");
2051 int pde_idx
= (addr
>> 22) & 0x3ff;
2052 int pte_idx
= (addr
>> 12) & 0x3ff;
2053 unsigned offs
= addr
& 0xfff;
2055 gdb_printf ("Page Table entry for address %s:\n",
2057 display_ptable_entry (get_pte (get_pde (pde_idx
), pte_idx
), 0, 1, offs
);
2061 static struct cmd_list_element
*info_dos_cmdlist
= NULL
;
2063 void _initialize_go32_nat ();
2065 _initialize_go32_nat ()
2067 x86_dr_low
.set_control
= go32_set_dr7
;
2068 x86_dr_low
.set_addr
= go32_set_dr
;
2069 x86_dr_low
.get_status
= go32_get_dr6
;
2070 x86_dr_low
.get_control
= go32_get_dr7
;
2071 x86_dr_low
.get_addr
= go32_get_dr
;
2072 x86_set_debug_register_length (4);
2074 add_inf_child_target (&the_go32_nat_target
);
2076 /* Initialize child's cwd as empty to be initialized when starting
2080 /* Initialize child's command line storage. */
2081 if (redir_debug_init (&child_cmd
) == -1)
2082 internal_error (_("Cannot allocate redirection storage: "
2083 "not enough memory.\n"));
2085 /* We are always processing GCC-compiled programs. */
2086 processing_gcc_compilation
= 2;
2088 add_basic_prefix_cmd ("dos", class_info
, _("\
2089 Print information specific to DJGPP (aka MS-DOS) debugging."),
2090 &info_dos_cmdlist
, 0, &infolist
);
2092 add_cmd ("sysinfo", class_info
, go32_sysinfo
, _("\
2093 Display information about the target system, including CPU, OS, DPMI, etc."),
2095 add_cmd ("ldt", class_info
, go32_sldt
, _("\
2096 Display entries in the LDT (Local Descriptor Table).\n\
2097 Entry number (an expression) as an argument means display only that entry."),
2099 add_cmd ("gdt", class_info
, go32_sgdt
, _("\
2100 Display entries in the GDT (Global Descriptor Table).\n\
2101 Entry number (an expression) as an argument means display only that entry."),
2103 add_cmd ("idt", class_info
, go32_sidt
, _("\
2104 Display entries in the IDT (Interrupt Descriptor Table).\n\
2105 Entry number (an expression) as an argument means display only that entry."),
2107 add_cmd ("pde", class_info
, go32_pde
, _("\
2108 Display entries in the Page Directory.\n\
2109 Entry number (an expression) as an argument means display only that entry."),
2111 add_cmd ("pte", class_info
, go32_pte
, _("\
2112 Display entries in Page Tables.\n\
2113 Entry number (an expression) as an argument means display only entries\n\
2114 from the Page Table pointed to by the specified Page Directory entry."),
2116 add_cmd ("address-pte", class_info
, go32_pte_for_address
, _("\
2117 Display a Page Table entry for a linear address.\n\
2118 The address argument must be a linear address, after adding to\n\
2119 it the base address of the appropriate segment.\n\
2120 The base address of variables and functions in the debuggee's data\n\
2121 or code segment is stored in the variable __djgpp_base_address,\n\
2122 so use `__djgpp_base_address + (char *)&var' as the argument.\n\
2123 For other segments, look up their base address in the output of\n\
2124 the `info dos ldt' command."),
2138 tcsetpgrp (int fd
, pid_t pgid
)
2140 if (isatty (fd
) && pgid
== SOME_PID
)
2142 errno
= pgid
== SOME_PID
? ENOTTY
: ENOSYS
;