1 /* Native debugging support for Intel x86 running DJGPP.
2 Copyright (C) 1997, 1999, 2000, 2001, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Written by Robert Hoehne.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdbthread.h"
30 #include "floatformat.h"
32 #include "i387-tdep.h"
33 #include "i386-tdep.h"
36 #include "gdb_string.h"
39 #include <stdio.h> /* might be required for __DJGPP_MINOR__ */
44 #include <sys/utsname.h>
49 #include <sys/farptr.h>
50 #include <debug/v2load.h>
51 #include <debug/dbgcom.h>
52 #if __DJGPP_MINOR__ > 2
53 #include <debug/redir.h>
56 #if __DJGPP_MINOR__ < 3
57 /* This code will be provided from DJGPP 2.03 on. Until then I code it
65 unsigned short exponent
:15;
66 unsigned short sign
:1;
85 static void save_npx (void); /* Save the FPU of the debugged program */
86 static void load_npx (void); /* Restore the FPU of the debugged program */
88 /* ------------------------------------------------------------------------- */
89 /* Store the contents of the NPX in the global variable `npx'. */
95 asm ("inb $0xa0, %%al \n\
114 /* ------------------------------------------------------------------------- */
115 /* Reload the contents of the NPX from the global variable `npx'. */
120 asm ("frstor %0":"=m" (npx
));
122 /* ------------------------------------------------------------------------- */
123 /* Stubs for the missing redirection functions. */
130 redir_cmdline_delete (cmdline_t
*ptr
)
136 redir_cmdline_parse (const char *args
, cmdline_t
*ptr
)
142 redir_to_child (cmdline_t
*ptr
)
148 redir_to_debugger (cmdline_t
*ptr
)
154 redir_debug_init (cmdline_t
*ptr
)
158 #endif /* __DJGPP_MINOR < 3 */
160 typedef enum { wp_insert
, wp_remove
, wp_count
} wp_op
;
162 /* This holds the current reference counts for each debug register. */
163 static int dr_ref_count
[4];
167 static int prog_has_started
= 0;
168 static void go32_open (char *name
, int from_tty
);
169 static void go32_close (int quitting
);
170 static void go32_attach (char *args
, int from_tty
);
171 static void go32_detach (char *args
, int from_tty
);
172 static void go32_resume (struct target_ops
*ops
,
173 ptid_t ptid
, int step
,
174 enum target_signal siggnal
);
175 static void go32_fetch_registers (struct target_ops
*ops
,
176 struct regcache
*, int regno
);
177 static void store_register (const struct regcache
*, int regno
);
178 static void go32_store_registers (struct target_ops
*ops
,
179 struct regcache
*, int regno
);
180 static void go32_prepare_to_store (struct regcache
*);
181 static int go32_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
,
183 struct mem_attrib
*attrib
,
184 struct target_ops
*target
);
185 static void go32_files_info (struct target_ops
*target
);
186 static void go32_stop (ptid_t
);
187 static void go32_kill_inferior (void);
188 static void go32_create_inferior (struct target_ops
*ops
, char *exec_file
,
189 char *args
, char **env
, int from_tty
);
190 static void go32_mourn_inferior (struct target_ops
*ops
);
191 static int go32_can_run (void);
193 static struct target_ops go32_ops
;
194 static void go32_terminal_init (void);
195 static void go32_terminal_inferior (void);
196 static void go32_terminal_ours (void);
198 #define r_ofs(x) (offsetof(TSS,x))
207 {r_ofs (tss_eax
), 4}, /* normal registers, from a_tss */
208 {r_ofs (tss_ecx
), 4},
209 {r_ofs (tss_edx
), 4},
210 {r_ofs (tss_ebx
), 4},
211 {r_ofs (tss_esp
), 4},
212 {r_ofs (tss_ebp
), 4},
213 {r_ofs (tss_esi
), 4},
214 {r_ofs (tss_edi
), 4},
215 {r_ofs (tss_eip
), 4},
216 {r_ofs (tss_eflags
), 4},
223 {0, 10}, /* 8 FP registers, from npx.reg[] */
231 /* The order of the next 7 registers must be consistent
232 with their numbering in config/i386/tm-i386.h, which see. */
233 {0, 2}, /* control word, from npx */
234 {4, 2}, /* status word, from npx */
235 {8, 2}, /* tag word, from npx */
236 {16, 2}, /* last FP exception CS from npx */
237 {12, 4}, /* last FP exception EIP from npx */
238 {24, 2}, /* last FP exception operand selector from npx */
239 {20, 4}, /* last FP exception operand offset from npx */
240 {18, 2} /* last FP opcode from npx */
246 enum target_signal gdb_sig
;
250 {0, TARGET_SIGNAL_FPE
},
251 {1, TARGET_SIGNAL_TRAP
},
252 /* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
253 but I think SIGBUS is better, since the NMI is usually activated
254 as a result of a memory parity check failure. */
255 {2, TARGET_SIGNAL_BUS
},
256 {3, TARGET_SIGNAL_TRAP
},
257 {4, TARGET_SIGNAL_FPE
},
258 {5, TARGET_SIGNAL_SEGV
},
259 {6, TARGET_SIGNAL_ILL
},
260 {7, TARGET_SIGNAL_EMT
}, /* no-coprocessor exception */
261 {8, TARGET_SIGNAL_SEGV
},
262 {9, TARGET_SIGNAL_SEGV
},
263 {10, TARGET_SIGNAL_BUS
},
264 {11, TARGET_SIGNAL_SEGV
},
265 {12, TARGET_SIGNAL_SEGV
},
266 {13, TARGET_SIGNAL_SEGV
},
267 {14, TARGET_SIGNAL_SEGV
},
268 {16, TARGET_SIGNAL_FPE
},
269 {17, TARGET_SIGNAL_BUS
},
270 {31, TARGET_SIGNAL_ILL
},
271 {0x1b, TARGET_SIGNAL_INT
},
272 {0x75, TARGET_SIGNAL_FPE
},
273 {0x78, TARGET_SIGNAL_ALRM
},
274 {0x79, TARGET_SIGNAL_INT
},
275 {0x7a, TARGET_SIGNAL_QUIT
},
276 {-1, TARGET_SIGNAL_LAST
}
280 enum target_signal gdb_sig
;
283 {TARGET_SIGNAL_0
, -1},
284 {TARGET_SIGNAL_ILL
, 6}, /* Invalid Opcode */
285 {TARGET_SIGNAL_EMT
, 7}, /* triggers SIGNOFP */
286 {TARGET_SIGNAL_SEGV
, 13}, /* GPF */
287 {TARGET_SIGNAL_BUS
, 17}, /* Alignment Check */
288 /* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
290 {TARGET_SIGNAL_TERM
, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
291 {TARGET_SIGNAL_FPE
, 0x75},
292 {TARGET_SIGNAL_INT
, 0x79},
293 {TARGET_SIGNAL_QUIT
, 0x7a},
294 {TARGET_SIGNAL_ALRM
, 0x78}, /* triggers SIGTIMR */
295 {TARGET_SIGNAL_PROF
, 0x78},
296 {TARGET_SIGNAL_LAST
, -1}
300 go32_open (char *name
, int from_tty
)
302 printf_unfiltered ("Done. Use the \"run\" command to run the program.\n");
306 go32_close (int quitting
)
311 go32_attach (struct target_ops
*ops
, char *args
, int from_tty
)
314 You cannot attach to a running program on this platform.\n\
315 Use the `run' command to run DJGPP programs."));
319 go32_detach (struct target_ops
*ops
, char *args
, int from_tty
)
323 static int resume_is_step
;
324 static int resume_signal
= -1;
327 go32_resume (struct target_ops
*ops
,
328 ptid_t ptid
, int step
, enum target_signal siggnal
)
332 resume_is_step
= step
;
334 if (siggnal
!= TARGET_SIGNAL_0
&& siggnal
!= TARGET_SIGNAL_TRAP
)
336 for (i
= 0, resume_signal
= -1;
337 excepn_map
[i
].gdb_sig
!= TARGET_SIGNAL_LAST
; i
++)
338 if (excepn_map
[i
].gdb_sig
== siggnal
)
340 resume_signal
= excepn_map
[i
].djgpp_excepno
;
343 if (resume_signal
== -1)
344 printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
345 target_signal_to_name (siggnal
));
349 static char child_cwd
[FILENAME_MAX
];
352 go32_wait (struct target_ops
*ops
,
353 ptid_t ptid
, struct target_waitstatus
*status
)
356 unsigned char saved_opcode
;
357 unsigned long INT3_addr
= 0;
358 int stepping_over_INT
= 0;
360 a_tss
.tss_eflags
&= 0xfeff; /* reset the single-step flag (TF) */
363 /* If the next instruction is INT xx or INTO, we need to handle
364 them specially. Intel manuals say that these instructions
365 reset the single-step flag (a.k.a. TF). However, it seems
366 that, at least in the DPMI environment, and at least when
367 stepping over the DPMI interrupt 31h, the problem is having
368 TF set at all when INT 31h is executed: the debuggee either
369 crashes (and takes the system with it) or is killed by a
372 So we need to emulate single-step mode: we put an INT3 opcode
373 right after the INT xx instruction, let the debuggee run
374 until it hits INT3 and stops, then restore the original
375 instruction which we overwrote with the INT3 opcode, and back
376 up the debuggee's EIP to that instruction. */
377 read_child (a_tss
.tss_eip
, &saved_opcode
, 1);
378 if (saved_opcode
== 0xCD || saved_opcode
== 0xCE)
380 unsigned char INT3_opcode
= 0xCC;
383 = saved_opcode
== 0xCD ? a_tss
.tss_eip
+ 2 : a_tss
.tss_eip
+ 1;
384 stepping_over_INT
= 1;
385 read_child (INT3_addr
, &saved_opcode
, 1);
386 write_child (INT3_addr
, &INT3_opcode
, 1);
389 a_tss
.tss_eflags
|= 0x0100; /* normal instruction: set TF */
392 /* The special value FFFFh in tss_trap indicates to run_child that
393 tss_irqn holds a signal to be delivered to the debuggee. */
394 if (resume_signal
<= -1)
397 a_tss
.tss_irqn
= 0xff;
401 a_tss
.tss_trap
= 0xffff; /* run_child looks for this */
402 a_tss
.tss_irqn
= resume_signal
;
405 /* The child might change working directory behind our back. The
406 GDB users won't like the side effects of that when they work with
407 relative file names, and GDB might be confused by its current
408 directory not being in sync with the truth. So we always make a
409 point of changing back to where GDB thinks is its cwd, when we
410 return control to the debugger, but restore child's cwd before we
412 /* Initialize child_cwd, before the first call to run_child and not
413 in the initialization, so the child get also the changed directory
414 set with the gdb-command "cd ..." */
416 /* Initialize child's cwd with the current one. */
417 getcwd (child_cwd
, sizeof (child_cwd
));
421 #if __DJGPP_MINOR__ < 3
425 #if __DJGPP_MINOR__ < 3
429 /* Did we step over an INT xx instruction? */
430 if (stepping_over_INT
&& a_tss
.tss_eip
== INT3_addr
+ 1)
432 /* Restore the original opcode. */
433 a_tss
.tss_eip
--; /* EIP points *after* the INT3 instruction */
434 write_child (a_tss
.tss_eip
, &saved_opcode
, 1);
435 /* Simulate a TRAP exception. */
437 a_tss
.tss_eflags
|= 0x0100;
440 getcwd (child_cwd
, sizeof (child_cwd
)); /* in case it has changed */
441 chdir (current_directory
);
443 if (a_tss
.tss_irqn
== 0x21)
445 status
->kind
= TARGET_WAITKIND_EXITED
;
446 status
->value
.integer
= a_tss
.tss_eax
& 0xff;
450 status
->value
.sig
= TARGET_SIGNAL_UNKNOWN
;
451 status
->kind
= TARGET_WAITKIND_STOPPED
;
452 for (i
= 0; sig_map
[i
].go32_sig
!= -1; i
++)
454 if (a_tss
.tss_irqn
== sig_map
[i
].go32_sig
)
456 #if __DJGPP_MINOR__ < 3
457 if ((status
->value
.sig
= sig_map
[i
].gdb_sig
) !=
459 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
461 status
->value
.sig
= sig_map
[i
].gdb_sig
;
467 return pid_to_ptid (SOME_PID
);
471 fetch_register (struct regcache
*regcache
, int regno
)
473 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
474 if (regno
< gdbarch_fp0_regnum (gdbarch
))
475 regcache_raw_supply (regcache
, regno
,
476 (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
477 else if (i386_fp_regnum_p (gdbarch
, regno
) || i386_fpc_regnum_p (gdbarch
, regno
))
478 i387_supply_fsave (regcache
, regno
, &npx
);
480 internal_error (__FILE__
, __LINE__
,
481 _("Invalid register no. %d in fetch_register."), regno
);
485 go32_fetch_registers (struct target_ops
*ops
,
486 struct regcache
*regcache
, int regno
)
489 fetch_register (regcache
, regno
);
493 regno
< gdbarch_fp0_regnum (get_regcache_arch (regcache
));
495 fetch_register (regcache
, regno
);
496 i387_supply_fsave (regcache
, -1, &npx
);
501 store_register (const struct regcache
*regcache
, int regno
)
503 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
504 if (regno
< gdbarch_fp0_regnum (gdbarch
))
505 regcache_raw_collect (regcache
, regno
,
506 (char *) &a_tss
+ regno_mapping
[regno
].tss_ofs
);
507 else if (i386_fp_regnum_p (gdbarch
, regno
) || i386_fpc_regnum_p (gdbarch
, regno
))
508 i387_collect_fsave (regcache
, regno
, &npx
);
510 internal_error (__FILE__
, __LINE__
,
511 _("Invalid register no. %d in store_register."), regno
);
515 go32_store_registers (struct target_ops
*ops
,
516 struct regcache
*regcache
, int regno
)
521 store_register (regcache
, regno
);
524 for (r
= 0; r
< gdbarch_fp0_regnum (get_regcache_arch (regcache
)); r
++)
525 store_register (regcache
, r
);
526 i387_collect_fsave (regcache
, -1, &npx
);
531 go32_prepare_to_store (struct regcache
*regcache
)
536 go32_xfer_memory (CORE_ADDR memaddr
, gdb_byte
*myaddr
, int len
, int write
,
537 struct mem_attrib
*attrib
, struct target_ops
*target
)
541 if (write_child (memaddr
, myaddr
, len
))
552 if (read_child (memaddr
, myaddr
, len
))
563 static cmdline_t child_cmd
; /* parsed child's command line kept here */
566 go32_files_info (struct target_ops
*target
)
568 printf_unfiltered ("You are running a DJGPP V2 program.\n");
572 go32_stop (ptid_t ptid
)
576 ptid
= inferior_ptid
;
577 inferior_ptid
= null_ptid
;
578 delete_thread_silent (ptid
);
579 prog_has_started
= 0;
583 go32_kill_inferior (void)
585 redir_cmdline_delete (&child_cmd
);
588 if (!ptid_equal (inferior_ptid
, null_ptid
))
589 delete_thread_silent (inferior_ptid
);
590 unpush_target (&go32_ops
);
594 go32_create_inferior (char *exec_file
, char *args
, char **env
, int from_tty
)
596 extern char **environ
;
599 char **env_save
= environ
;
602 /* If no exec file handed to us, get it from the exec-file command -- with
603 a good, common error message if none is specified. */
605 exec_file
= get_exec_file (1);
607 if (prog_has_started
)
609 go32_stop (inferior_ptid
);
610 go32_kill_inferior ();
615 /* Initialize child's cwd as empty to be initialized when starting
619 /* Init command line storage. */
620 if (redir_debug_init (&child_cmd
) == -1)
621 internal_error (__FILE__
, __LINE__
,
622 _("Cannot allocate redirection storage: not enough memory.\n"));
624 /* Parse the command line and create redirections. */
625 if (strpbrk (args
, "<>"))
627 if (redir_cmdline_parse (args
, &child_cmd
) == 0)
628 args
= child_cmd
.command
;
630 error (_("Syntax error in command line."));
633 child_cmd
.command
= xstrdup (args
);
635 cmdlen
= strlen (args
);
636 /* v2loadimage passes command lines via DOS memory, so it cannot
637 possibly handle commands longer than 1MB. */
638 if (cmdlen
> 1024*1024)
639 error (_("Command line too long."));
641 cmdline
= xmalloc (cmdlen
+ 4);
642 strcpy (cmdline
+ 1, args
);
643 /* If the command-line length fits into DOS 126-char limits, use the
644 DOS command tail format; otherwise, tell v2loadimage to pass it
645 through a buffer in conventional memory. */
648 cmdline
[0] = strlen (args
);
649 cmdline
[cmdlen
+ 1] = 13;
652 cmdline
[0] = 0xff; /* signal v2loadimage it's a long command */
656 if (v2loadimage (exec_file
, cmdline
, start_state
))
659 printf_unfiltered ("Load failed for image %s\n", exec_file
);
665 edi_init (start_state
);
666 #if __DJGPP_MINOR__ < 3
670 inferior_ptid
= pid_to_ptid (SOME_PID
);
671 add_inferior_silent (SOME_PID
);
673 push_target (&go32_ops
);
675 add_thread_silent (inferior_ptid
);
677 clear_proceed_status ();
678 insert_breakpoints ();
679 prog_has_started
= 1;
683 go32_mourn_inferior (struct target_ops
*ops
)
685 /* We need to make sure all the breakpoint enable bits in the DR7
686 register are reset when the inferior exits. Otherwise, if they
687 rerun the inferior, the uncleared bits may cause random SIGTRAPs,
688 failure to set more watchpoints, and other calamities. It would
689 be nice if GDB itself would take care to remove all breakpoints
690 at all times, but it doesn't, probably under an assumption that
691 the OS cleans up when the debuggee exits. */
692 i386_cleanup_dregs ();
693 go32_kill_inferior ();
694 generic_mourn_inferior ();
703 /* Hardware watchpoint support. */
705 #define D_REGS edi.dr
706 #define CONTROL D_REGS[7]
707 #define STATUS D_REGS[6]
709 /* Pass the address ADDR to the inferior in the I'th debug register.
710 Here we just store the address in D_REGS, the watchpoint will be
711 actually set up when go32_wait runs the debuggee. */
713 go32_set_dr (int i
, CORE_ADDR addr
)
716 internal_error (__FILE__
, __LINE__
,
717 _("Invalid register %d in go32_set_dr.\n"), i
);
721 /* Pass the value VAL to the inferior in the DR7 debug control
722 register. Here we just store the address in D_REGS, the watchpoint
723 will be actually set up when go32_wait runs the debuggee. */
725 go32_set_dr7 (unsigned val
)
730 /* Get the value of the DR6 debug status register from the inferior.
731 Here we just return the value stored in D_REGS, as we've got it
732 from the last go32_wait call. */
739 /* Put the device open on handle FD into either raw or cooked
740 mode, return 1 if it was in raw mode, zero otherwise. */
743 device_mode (int fd
, int raw_p
)
745 int oldmode
, newmode
;
750 __dpmi_int (0x21, ®s
);
751 if (regs
.x
.flags
& 1)
753 newmode
= oldmode
= regs
.x
.dx
;
760 if (oldmode
& 0x80) /* Only for character dev */
764 regs
.x
.dx
= newmode
& 0xff; /* Force upper byte zero, else it fails */
765 __dpmi_int (0x21, ®s
);
766 if (regs
.x
.flags
& 1)
769 return (oldmode
& 0x20) == 0x20;
773 static int inf_mode_valid
= 0;
774 static int inf_terminal_mode
;
776 /* This semaphore is needed because, amazingly enough, GDB calls
777 target.to_terminal_ours more than once after the inferior stops.
778 But we need the information from the first call only, since the
779 second call will always see GDB's own cooked terminal. */
780 static int terminal_is_ours
= 1;
783 go32_terminal_init (void)
785 inf_mode_valid
= 0; /* reinitialize, in case they are restarting child */
786 terminal_is_ours
= 1;
790 go32_terminal_info (char *args
, int from_tty
)
792 printf_unfiltered ("Inferior's terminal is in %s mode.\n",
794 ? "default" : inf_terminal_mode
? "raw" : "cooked");
796 #if __DJGPP_MINOR__ > 2
797 if (child_cmd
.redirection
)
801 for (i
= 0; i
< DBG_HANDLES
; i
++)
803 if (child_cmd
.redirection
[i
]->file_name
)
804 printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
805 i
, child_cmd
.redirection
[i
]->file_name
);
806 else if (_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) == -1)
808 ("\tFile handle %d appears to be closed by inferior.\n", i
);
809 /* Mask off the raw/cooked bit when comparing device info words. */
810 else if ((_get_dev_info (child_cmd
.redirection
[i
]->inf_handle
) & 0xdf)
811 != (_get_dev_info (i
) & 0xdf))
813 ("\tFile handle %d appears to be redirected by inferior.\n", i
);
820 go32_terminal_inferior (void)
822 /* Redirect standard handles as child wants them. */
824 if (redir_to_child (&child_cmd
) == -1)
826 redir_to_debugger (&child_cmd
);
827 error (_("Cannot redirect standard handles for program: %s."),
828 safe_strerror (errno
));
830 /* set the console device of the inferior to whatever mode
831 (raw or cooked) we found it last time */
832 if (terminal_is_ours
)
835 device_mode (0, inf_terminal_mode
);
836 terminal_is_ours
= 0;
841 go32_terminal_ours (void)
843 /* Switch to cooked mode on the gdb terminal and save the inferior
844 terminal mode to be restored when it is resumed */
845 if (!terminal_is_ours
)
847 inf_terminal_mode
= device_mode (0, 0);
848 if (inf_terminal_mode
!= -1)
851 /* If device_mode returned -1, we don't know what happens with
852 handle 0 anymore, so make the info invalid. */
854 terminal_is_ours
= 1;
856 /* Restore debugger's standard handles. */
858 if (redir_to_debugger (&child_cmd
) == -1)
860 redir_to_child (&child_cmd
);
861 error (_("Cannot redirect standard handles for debugger: %s."),
862 safe_strerror (errno
));
868 go32_thread_alive (struct target_ops
*ops
, ptid_t ptid
)
874 go32_pid_to_str (struct target_ops
*ops
, ptid_t ptid
)
877 xsnprintf (buf
, sizeof buf
, "Thread <main>");
884 go32_ops
.to_shortname
= "djgpp";
885 go32_ops
.to_longname
= "djgpp target process";
887 "Program loaded by djgpp, when gdb is used as an external debugger";
888 go32_ops
.to_open
= go32_open
;
889 go32_ops
.to_close
= go32_close
;
890 go32_ops
.to_attach
= go32_attach
;
891 go32_ops
.to_detach
= go32_detach
;
892 go32_ops
.to_resume
= go32_resume
;
893 go32_ops
.to_wait
= go32_wait
;
894 go32_ops
.to_fetch_registers
= go32_fetch_registers
;
895 go32_ops
.to_store_registers
= go32_store_registers
;
896 go32_ops
.to_prepare_to_store
= go32_prepare_to_store
;
897 go32_ops
.deprecated_xfer_memory
= go32_xfer_memory
;
898 go32_ops
.to_files_info
= go32_files_info
;
899 go32_ops
.to_insert_breakpoint
= memory_insert_breakpoint
;
900 go32_ops
.to_remove_breakpoint
= memory_remove_breakpoint
;
901 go32_ops
.to_terminal_init
= go32_terminal_init
;
902 go32_ops
.to_terminal_inferior
= go32_terminal_inferior
;
903 go32_ops
.to_terminal_ours_for_output
= go32_terminal_ours
;
904 go32_ops
.to_terminal_ours
= go32_terminal_ours
;
905 go32_ops
.to_terminal_info
= go32_terminal_info
;
906 go32_ops
.to_kill
= go32_kill_inferior
;
907 go32_ops
.to_create_inferior
= go32_create_inferior
;
908 go32_ops
.to_mourn_inferior
= go32_mourn_inferior
;
909 go32_ops
.to_can_run
= go32_can_run
;
910 go32_ops
.to_stop
= go32_stop
;
911 go32_ops
.to_thread_alive
= go32_thread_alive
;
912 go32_ops
.to_pid_to_str
= go32_pid_to_str
;
913 go32_ops
.to_stratum
= process_stratum
;
914 go32_ops
.to_has_all_memory
= 1;
915 go32_ops
.to_has_memory
= 1;
916 go32_ops
.to_has_stack
= 1;
917 go32_ops
.to_has_registers
= 1;
918 go32_ops
.to_has_execution
= 1;
920 i386_use_watchpoints (&go32_ops
);
922 go32_ops
.to_magic
= OPS_MAGIC
;
924 /* Initialize child's cwd as empty to be initialized when starting
928 /* Initialize child's command line storage. */
929 if (redir_debug_init (&child_cmd
) == -1)
930 internal_error (__FILE__
, __LINE__
,
931 _("Cannot allocate redirection storage: not enough memory.\n"));
933 /* We are always processing GCC-compiled programs. */
934 processing_gcc_compilation
= 2;
936 /* Override the default name of the GDB init file. */
937 strcpy (gdbinit
, "gdb.ini");
940 unsigned short windows_major
, windows_minor
;
942 /* Compute the version Windows reports via Int 2Fh/AX=1600h. */
944 go32_get_windows_version(void)
949 __dpmi_int(0x2f, &r
);
950 if (r
.h
.al
> 2 && r
.h
.al
!= 0x80 && r
.h
.al
!= 0xff
951 && (r
.h
.al
> 3 || r
.h
.ah
> 0))
953 windows_major
= r
.h
.al
;
954 windows_minor
= r
.h
.ah
;
957 windows_major
= 0xff; /* meaning no Windows */
960 /* A subroutine of go32_sysinfo to display memory info. */
962 print_mem (unsigned long datum
, const char *header
, int in_pages_p
)
964 if (datum
!= 0xffffffffUL
)
968 puts_filtered (header
);
971 printf_filtered ("%lu KB", datum
>> 10);
972 if (datum
> 1024 * 1024)
973 printf_filtered (" (%lu MB)", datum
>> 20);
976 printf_filtered ("%lu Bytes", datum
);
977 puts_filtered ("\n");
981 /* Display assorted information about the underlying OS. */
983 go32_sysinfo (char *arg
, int from_tty
)
986 char cpuid_vendor
[13];
987 unsigned cpuid_max
= 0, cpuid_eax
, cpuid_ebx
, cpuid_ecx
, cpuid_edx
;
988 unsigned true_dos_version
= _get_dos_version (1);
989 unsigned advertized_dos_version
= ((unsigned int)_osmajor
<< 8) | _osminor
;
991 char dpmi_vendor_info
[129];
992 int dpmi_vendor_available
=
993 __dpmi_get_capabilities (&dpmi_flags
, dpmi_vendor_info
);
994 __dpmi_version_ret dpmi_version_data
;
996 __dpmi_free_mem_info mem_info
;
999 cpuid_vendor
[0] = '\0';
1001 strcpy (u
.machine
, "Unknown x86");
1002 else if (u
.machine
[0] == 'i' && u
.machine
[1] > 4)
1004 /* CPUID with EAX = 0 returns the Vendor ID. */
1005 __asm__
__volatile__ ("xorl %%ebx, %%ebx;"
1006 "xorl %%ecx, %%ecx;"
1007 "xorl %%edx, %%edx;"
1014 : "=m" (cpuid_vendor
[0]),
1015 "=m" (cpuid_vendor
[4]),
1016 "=m" (cpuid_vendor
[8]),
1019 : "%eax", "%ebx", "%ecx", "%edx");
1020 cpuid_vendor
[12] = '\0';
1023 printf_filtered ("CPU Type.......................%s", u
.machine
);
1024 if (cpuid_vendor
[0])
1025 printf_filtered (" (%s)", cpuid_vendor
);
1026 puts_filtered ("\n");
1028 /* CPUID with EAX = 1 returns processor signature and features. */
1031 static char *brand_name
[] = {
1039 char cpu_string
[80];
1042 int intel_p
= strcmp (cpuid_vendor
, "GenuineIntel") == 0;
1043 int amd_p
= strcmp (cpuid_vendor
, "AuthenticAMD") == 0;
1044 unsigned cpu_family
, cpu_model
;
1046 __asm__
__volatile__ ("movl $1, %%eax;"
1053 brand_idx
= cpuid_ebx
& 0xff;
1054 cpu_family
= (cpuid_eax
>> 8) & 0xf;
1055 cpu_model
= (cpuid_eax
>> 4) & 0xf;
1056 cpu_brand
[0] = '\0';
1060 && brand_idx
< sizeof(brand_name
)/sizeof(brand_name
[0])
1061 && *brand_name
[brand_idx
])
1062 strcpy (cpu_brand
, brand_name
[brand_idx
]);
1063 else if (cpu_family
== 5)
1065 if (((cpuid_eax
>> 12) & 3) == 0 && cpu_model
== 4)
1066 strcpy (cpu_brand
, " MMX");
1067 else if (cpu_model
> 1 && ((cpuid_eax
>> 12) & 3) == 1)
1068 strcpy (cpu_brand
, " OverDrive");
1069 else if (cpu_model
> 1 && ((cpuid_eax
>> 12) & 3) == 2)
1070 strcpy (cpu_brand
, " Dual");
1072 else if (cpu_family
== 6 && cpu_model
< 8)
1077 strcpy (cpu_brand
, " Pro");
1080 strcpy (cpu_brand
, " II");
1083 strcpy (cpu_brand
, " II Xeon");
1086 strcpy (cpu_brand
, " Celeron");
1089 strcpy (cpu_brand
, " III");
1099 strcpy (cpu_brand
, "486/5x86");
1108 strcpy (cpu_brand
, "-K5");
1112 strcpy (cpu_brand
, "-K6");
1115 strcpy (cpu_brand
, "-K6-2");
1118 strcpy (cpu_brand
, "-K6-III");
1128 strcpy (cpu_brand
, " Athlon");
1131 strcpy (cpu_brand
, " Duron");
1137 sprintf (cpu_string
, "%s%s Model %d Stepping %d",
1138 intel_p
? "Pentium" : (amd_p
? "AMD" : "ix86"),
1139 cpu_brand
, cpu_model
, cpuid_eax
& 0xf);
1140 printfi_filtered (31, "%s\n", cpu_string
);
1141 if (((cpuid_edx
& (6 | (0x0d << 23))) != 0)
1142 || ((cpuid_edx
& 1) == 0)
1143 || (amd_p
&& (cpuid_edx
& (3 << 30)) != 0))
1145 puts_filtered ("CPU Features...................");
1146 /* We only list features which might be useful in the DPMI
1148 if ((cpuid_edx
& 1) == 0)
1149 puts_filtered ("No FPU "); /* it's unusual to not have an FPU */
1150 if ((cpuid_edx
& (1 << 1)) != 0)
1151 puts_filtered ("VME ");
1152 if ((cpuid_edx
& (1 << 2)) != 0)
1153 puts_filtered ("DE ");
1154 if ((cpuid_edx
& (1 << 4)) != 0)
1155 puts_filtered ("TSC ");
1156 if ((cpuid_edx
& (1 << 23)) != 0)
1157 puts_filtered ("MMX ");
1158 if ((cpuid_edx
& (1 << 25)) != 0)
1159 puts_filtered ("SSE ");
1160 if ((cpuid_edx
& (1 << 26)) != 0)
1161 puts_filtered ("SSE2 ");
1164 if ((cpuid_edx
& (1 << 31)) != 0)
1165 puts_filtered ("3DNow! ");
1166 if ((cpuid_edx
& (1 << 30)) != 0)
1167 puts_filtered ("3DNow!Ext");
1169 puts_filtered ("\n");
1172 puts_filtered ("\n");
1173 printf_filtered ("DOS Version....................%s %s.%s",
1174 _os_flavor
, u
.release
, u
.version
);
1175 if (true_dos_version
!= advertized_dos_version
)
1176 printf_filtered (" (disguised as v%d.%d)", _osmajor
, _osminor
);
1177 puts_filtered ("\n");
1179 go32_get_windows_version ();
1180 if (windows_major
!= 0xff)
1182 const char *windows_flavor
;
1184 printf_filtered ("Windows Version................%d.%02d (Windows ",
1185 windows_major
, windows_minor
);
1186 switch (windows_major
)
1189 windows_flavor
= "3.X";
1192 switch (windows_minor
)
1195 windows_flavor
= "95, 95A, or 95B";
1198 windows_flavor
= "95B OSR2.1 or 95C OSR2.5";
1201 windows_flavor
= "98 or 98 SE";
1204 windows_flavor
= "ME";
1207 windows_flavor
= "9X";
1212 windows_flavor
= "??";
1215 printf_filtered ("%s)\n", windows_flavor
);
1217 else if (true_dos_version
== 0x532 && advertized_dos_version
== 0x500)
1218 printf_filtered ("Windows Version................Windows NT or Windows 2000\n");
1219 puts_filtered ("\n");
1220 if (dpmi_vendor_available
== 0)
1222 /* The DPMI spec says the vendor string should be ASCIIZ, but
1223 I don't trust the vendors to follow that... */
1224 if (!memchr (&dpmi_vendor_info
[2], 0, 126))
1225 dpmi_vendor_info
[128] = '\0';
1226 printf_filtered ("DPMI Host......................%s v%d.%d (capabilities: %#x)\n",
1227 &dpmi_vendor_info
[2],
1228 (unsigned)dpmi_vendor_info
[0],
1229 (unsigned)dpmi_vendor_info
[1],
1230 ((unsigned)dpmi_flags
& 0x7f));
1232 __dpmi_get_version (&dpmi_version_data
);
1233 printf_filtered ("DPMI Version...................%d.%02d\n",
1234 dpmi_version_data
.major
, dpmi_version_data
.minor
);
1235 printf_filtered ("DPMI Info......................%s-bit DPMI, with%s Virtual Memory support\n",
1236 (dpmi_version_data
.flags
& 1) ? "32" : "16",
1237 (dpmi_version_data
.flags
& 4) ? "" : "out");
1238 printfi_filtered (31, "Interrupts reflected to %s mode\n",
1239 (dpmi_version_data
.flags
& 2) ? "V86" : "Real");
1240 printfi_filtered (31, "Processor type: i%d86\n",
1241 dpmi_version_data
.cpu
);
1242 printfi_filtered (31, "PIC base interrupt: Master: %#x Slave: %#x\n",
1243 dpmi_version_data
.master_pic
, dpmi_version_data
.slave_pic
);
1245 /* a_tss is only initialized when the debuggee is first run. */
1246 if (prog_has_started
)
1248 __asm__
__volatile__ ("pushfl ; popl %0" : "=g" (eflags
));
1249 printf_filtered ("Protection.....................Ring %d (in %s), with%s I/O protection\n",
1250 a_tss
.tss_cs
& 3, (a_tss
.tss_cs
& 4) ? "LDT" : "GDT",
1251 (a_tss
.tss_cs
& 3) > ((eflags
>> 12) & 3) ? "" : "out");
1253 puts_filtered ("\n");
1254 __dpmi_get_free_memory_information (&mem_info
);
1255 print_mem (mem_info
.total_number_of_physical_pages
,
1256 "DPMI Total Physical Memory.....", 1);
1257 print_mem (mem_info
.total_number_of_free_pages
,
1258 "DPMI Free Physical Memory......", 1);
1259 print_mem (mem_info
.size_of_paging_file_partition_in_pages
,
1260 "DPMI Swap Space................", 1);
1261 print_mem (mem_info
.linear_address_space_size_in_pages
,
1262 "DPMI Total Linear Address Size.", 1);
1263 print_mem (mem_info
.free_linear_address_space_in_pages
,
1264 "DPMI Free Linear Address Size..", 1);
1265 print_mem (mem_info
.largest_available_free_block_in_bytes
,
1266 "DPMI Largest Free Memory Block.", 0);
1270 __dpmi_int (0x21, ®s
);
1271 print_mem (regs
.x
.bx
<< 4, "Free DOS Memory................", 0);
1273 __dpmi_int (0x21, ®s
);
1274 if ((regs
.x
.flags
& 1) == 0)
1276 static const char *dos_hilo
[] = {
1277 "Low", "", "", "", "High", "", "", "", "High, then Low"
1279 static const char *dos_fit
[] = {
1280 "First", "Best", "Last"
1282 int hilo_idx
= (regs
.x
.ax
>> 4) & 0x0f;
1283 int fit_idx
= regs
.x
.ax
& 0x0f;
1289 printf_filtered ("DOS Memory Allocation..........%s memory, %s fit\n",
1290 dos_hilo
[hilo_idx
], dos_fit
[fit_idx
]);
1292 __dpmi_int (0x21, ®s
);
1293 if ((regs
.x
.flags
& 1) != 0)
1295 printfi_filtered (31, "UMBs %sin DOS memory chain\n",
1296 regs
.h
.al
== 0 ? "not " : "");
1301 unsigned short limit0
;
1302 unsigned short base0
;
1303 unsigned char base1
;
1308 unsigned available
:1;
1311 unsigned page_granular
:1;
1312 unsigned char base2
;
1313 } __attribute__ ((packed
));
1316 unsigned short offset0
;
1317 unsigned short selector
;
1318 unsigned param_count
:5;
1323 unsigned short offset1
;
1324 } __attribute__ ((packed
));
1326 /* Read LEN bytes starting at logical address ADDR, and put the result
1327 into DEST. Return 1 if success, zero if not. */
1329 read_memory_region (unsigned long addr
, void *dest
, size_t len
)
1331 unsigned long dos_ds_limit
= __dpmi_get_segment_limit (_dos_ds
);
1334 /* For the low memory, we can simply use _dos_ds. */
1335 if (addr
<= dos_ds_limit
- len
)
1336 dosmemget (addr
, len
, dest
);
1339 /* For memory above 1MB we need to set up a special segment to
1340 be able to access that memory. */
1341 int sel
= __dpmi_allocate_ldt_descriptors (1);
1347 int access_rights
= __dpmi_get_descriptor_access_rights (sel
);
1348 size_t segment_limit
= len
- 1;
1350 /* Make sure the crucial bits in the descriptor access
1351 rights are set correctly. Some DPMI providers might barf
1352 if we set the segment limit to something that is not an
1353 integral multiple of 4KB pages if the granularity bit is
1354 not set to byte-granular, even though the DPMI spec says
1355 it's the host's responsibility to set that bit correctly. */
1356 if (len
> 1024 * 1024)
1358 access_rights
|= 0x8000;
1359 /* Page-granular segments should have the low 12 bits of
1361 segment_limit
|= 0xfff;
1364 access_rights
&= ~0x8000;
1366 if (__dpmi_set_segment_base_address (sel
, addr
) != -1
1367 && __dpmi_set_descriptor_access_rights (sel
, access_rights
) != -1
1368 && __dpmi_set_segment_limit (sel
, segment_limit
) != -1
1369 /* W2K silently fails to set the segment limit, leaving
1370 it at zero; this test avoids the resulting crash. */
1371 && __dpmi_get_segment_limit (sel
) >= segment_limit
)
1372 movedata (sel
, 0, _my_ds (), (unsigned)dest
, len
);
1376 __dpmi_free_ldt_descriptor (sel
);
1382 /* Get a segment descriptor stored at index IDX in the descriptor
1383 table whose base address is TABLE_BASE. Return the descriptor
1384 type, or -1 if failure. */
1386 get_descriptor (unsigned long table_base
, int idx
, void *descr
)
1388 unsigned long addr
= table_base
+ idx
* 8; /* 8 bytes per entry */
1390 if (read_memory_region (addr
, descr
, 8))
1391 return (int)((struct seg_descr
*)descr
)->stype
;
1396 unsigned short limit
__attribute__((packed
));
1397 unsigned long base
__attribute__((packed
));
1400 /* Display a segment descriptor stored at index IDX in a descriptor
1401 table whose type is TYPE and whose base address is BASE_ADDR. If
1402 FORCE is non-zero, display even invalid descriptors. */
1404 display_descriptor (unsigned type
, unsigned long base_addr
, int idx
, int force
)
1406 struct seg_descr descr
;
1407 struct gate_descr gate
;
1409 /* Get the descriptor from the table. */
1410 if (idx
== 0 && type
== 0)
1411 puts_filtered ("0x000: null descriptor\n");
1412 else if (get_descriptor (base_addr
, idx
, &descr
) != -1)
1414 /* For each type of descriptor table, this has a bit set if the
1415 corresponding type of selectors is valid in that table. */
1416 static unsigned allowed_descriptors
[] = {
1417 0xffffdafeL
, /* GDT */
1418 0x0000c0e0L
, /* IDT */
1419 0xffffdafaL
/* LDT */
1422 /* If the program hasn't started yet, assume the debuggee will
1423 have the same CPL as the debugger. */
1424 int cpl
= prog_has_started
? (a_tss
.tss_cs
& 3) : _my_cs () & 3;
1425 unsigned long limit
= (descr
.limit1
<< 16) | descr
.limit0
;
1428 && (allowed_descriptors
[type
] & (1 << descr
.stype
)) != 0)
1430 printf_filtered ("0x%03x: ",
1432 ? idx
: (idx
* 8) | (type
? (cpl
| 4) : 0));
1433 if (descr
.page_granular
)
1434 limit
= (limit
<< 12) | 0xfff; /* big segment: low 12 bit set */
1435 if (descr
.stype
== 1 || descr
.stype
== 2 || descr
.stype
== 3
1436 || descr
.stype
== 9 || descr
.stype
== 11
1437 || (descr
.stype
>= 16 && descr
.stype
< 32))
1438 printf_filtered ("base=0x%02x%02x%04x limit=0x%08lx",
1439 descr
.base2
, descr
.base1
, descr
.base0
, limit
);
1441 switch (descr
.stype
)
1445 printf_filtered (" 16-bit TSS (task %sactive)",
1446 descr
.stype
== 3 ? "" : "in");
1449 puts_filtered (" LDT");
1452 memcpy (&gate
, &descr
, sizeof gate
);
1453 printf_filtered ("selector=0x%04x offs=0x%04x%04x",
1454 gate
.selector
, gate
.offset1
, gate
.offset0
);
1455 printf_filtered (" 16-bit Call Gate (params=%d)",
1459 printf_filtered ("TSS selector=0x%04x", descr
.base0
);
1460 printfi_filtered (16, "Task Gate");
1464 memcpy (&gate
, &descr
, sizeof gate
);
1465 printf_filtered ("selector=0x%04x offs=0x%04x%04x",
1466 gate
.selector
, gate
.offset1
, gate
.offset0
);
1467 printf_filtered (" 16-bit %s Gate",
1468 descr
.stype
== 6 ? "Interrupt" : "Trap");
1472 printf_filtered (" 32-bit TSS (task %sactive)",
1473 descr
.stype
== 3 ? "" : "in");
1476 memcpy (&gate
, &descr
, sizeof gate
);
1477 printf_filtered ("selector=0x%04x offs=0x%04x%04x",
1478 gate
.selector
, gate
.offset1
, gate
.offset0
);
1479 printf_filtered (" 32-bit Call Gate (params=%d)",
1484 memcpy (&gate
, &descr
, sizeof gate
);
1485 printf_filtered ("selector=0x%04x offs=0x%04x%04x",
1486 gate
.selector
, gate
.offset1
, gate
.offset0
);
1487 printf_filtered (" 32-bit %s Gate",
1488 descr
.stype
== 14 ? "Interrupt" : "Trap");
1490 case 16: /* data segments */
1498 printf_filtered (" %s-bit Data (%s Exp-%s%s)",
1499 descr
.bit32
? "32" : "16",
1500 descr
.stype
& 2 ? "Read/Write," : "Read-Only, ",
1501 descr
.stype
& 4 ? "down" : "up",
1502 descr
.stype
& 1 ? "" : ", N.Acc");
1504 case 24: /* code segments */
1512 printf_filtered (" %s-bit Code (%s, %sConf%s)",
1513 descr
.bit32
? "32" : "16",
1514 descr
.stype
& 2 ? "Exec/Read" : "Exec-Only",
1515 descr
.stype
& 4 ? "" : "N.",
1516 descr
.stype
& 1 ? "" : ", N.Acc");
1519 printf_filtered ("Unknown type 0x%02x", descr
.stype
);
1522 puts_filtered ("\n");
1526 printf_filtered ("0x%03x: ",
1528 ? idx
: (idx
* 8) | (type
? (cpl
| 4) : 0));
1530 puts_filtered ("Segment not present\n");
1532 printf_filtered ("Segment type 0x%02x is invalid in this table\n",
1537 printf_filtered ("0x%03x: Cannot read this descriptor\n", idx
);
1541 go32_sldt (char *arg
, int from_tty
)
1543 struct dtr_reg gdtr
;
1544 unsigned short ldtr
= 0;
1546 struct seg_descr ldt_descr
;
1547 long ldt_entry
= -1L;
1548 int cpl
= (prog_has_started
? a_tss
.tss_cs
: _my_cs ()) & 3;
1552 while (*arg
&& isspace(*arg
))
1557 ldt_entry
= parse_and_eval_long (arg
);
1559 || (ldt_entry
& 4) == 0
1560 || (ldt_entry
& 3) != (cpl
& 3))
1561 error (_("Invalid LDT entry 0x%03lx."), (unsigned long)ldt_entry
);
1565 __asm__
__volatile__ ("sgdt %0" : "=m" (gdtr
) : /* no inputs */ );
1566 __asm__
__volatile__ ("sldt %0" : "=m" (ldtr
) : /* no inputs */ );
1569 puts_filtered ("There is no LDT.\n");
1570 /* LDT's entry in the GDT must have the type LDT, which is 2. */
1571 else if (get_descriptor (gdtr
.base
, ldt_idx
, &ldt_descr
) != 2)
1572 printf_filtered ("LDT is present (at %#x), but unreadable by GDB.\n",
1574 | (ldt_descr
.base1
<< 16)
1575 | (ldt_descr
.base2
<< 24));
1580 | (ldt_descr
.base1
<< 16)
1581 | (ldt_descr
.base2
<< 24);
1582 unsigned limit
= ldt_descr
.limit0
| (ldt_descr
.limit1
<< 16);
1585 if (ldt_descr
.page_granular
)
1586 /* Page-granular segments must have the low 12 bits of their
1588 limit
= (limit
<< 12) | 0xfff;
1589 /* LDT cannot have more than 8K 8-byte entries, i.e. more than
1594 max_entry
= (limit
+ 1) / 8;
1598 if (ldt_entry
> limit
)
1599 error (_("Invalid LDT entry %#lx: outside valid limits [0..%#x]"),
1600 (unsigned long)ldt_entry
, limit
);
1602 display_descriptor (ldt_descr
.stype
, base
, ldt_entry
/ 8, 1);
1608 for (i
= 0; i
< max_entry
; i
++)
1609 display_descriptor (ldt_descr
.stype
, base
, i
, 0);
1615 go32_sgdt (char *arg
, int from_tty
)
1617 struct dtr_reg gdtr
;
1618 long gdt_entry
= -1L;
1623 while (*arg
&& isspace(*arg
))
1628 gdt_entry
= parse_and_eval_long (arg
);
1629 if (gdt_entry
< 0 || (gdt_entry
& 7) != 0)
1630 error (_("Invalid GDT entry 0x%03lx: not an integral multiple of 8."),
1631 (unsigned long)gdt_entry
);
1635 __asm__
__volatile__ ("sgdt %0" : "=m" (gdtr
) : /* no inputs */ );
1636 max_entry
= (gdtr
.limit
+ 1) / 8;
1640 if (gdt_entry
> gdtr
.limit
)
1641 error (_("Invalid GDT entry %#lx: outside valid limits [0..%#x]"),
1642 (unsigned long)gdt_entry
, gdtr
.limit
);
1644 display_descriptor (0, gdtr
.base
, gdt_entry
/ 8, 1);
1650 for (i
= 0; i
< max_entry
; i
++)
1651 display_descriptor (0, gdtr
.base
, i
, 0);
1656 go32_sidt (char *arg
, int from_tty
)
1658 struct dtr_reg idtr
;
1659 long idt_entry
= -1L;
1664 while (*arg
&& isspace(*arg
))
1669 idt_entry
= parse_and_eval_long (arg
);
1671 error (_("Invalid (negative) IDT entry %ld."), idt_entry
);
1675 __asm__
__volatile__ ("sidt %0" : "=m" (idtr
) : /* no inputs */ );
1676 max_entry
= (idtr
.limit
+ 1) / 8;
1677 if (max_entry
> 0x100) /* no more than 256 entries */
1682 if (idt_entry
> idtr
.limit
)
1683 error (_("Invalid IDT entry %#lx: outside valid limits [0..%#x]"),
1684 (unsigned long)idt_entry
, idtr
.limit
);
1686 display_descriptor (1, idtr
.base
, idt_entry
, 1);
1692 for (i
= 0; i
< max_entry
; i
++)
1693 display_descriptor (1, idtr
.base
, i
, 0);
1697 /* Cached linear address of the base of the page directory. For
1698 now, available only under CWSDPMI. Code based on ideas and
1699 suggestions from Charles Sandmann <sandmann@clio.rice.edu>. */
1700 static unsigned long pdbr
;
1702 static unsigned long
1707 unsigned long taskbase
, cr3
;
1708 struct dtr_reg gdtr
;
1710 if (pdbr
> 0 && pdbr
<= 0xfffff)
1713 /* Get the linear address of GDT and the Task Register. */
1714 __asm__
__volatile__ ("sgdt %0" : "=m" (gdtr
) : /* no inputs */ );
1715 __asm__
__volatile__ ("str %0" : "=m" (taskreg
) : /* no inputs */ );
1717 /* Task Register is a segment selector for the TSS of the current
1718 task. Therefore, it can be used as an index into the GDT to get
1719 at the segment descriptor for the TSS. To get the index, reset
1720 the low 3 bits of the selector (which give the CPL). Add 2 to the
1721 offset to point to the 3 low bytes of the base address. */
1722 offset
= gdtr
.base
+ (taskreg
& 0xfff8) + 2;
1725 /* CWSDPMI's task base is always under the 1MB mark. */
1726 if (offset
> 0xfffff)
1729 _farsetsel (_dos_ds
);
1730 taskbase
= _farnspeekl (offset
) & 0xffffffU
;
1731 taskbase
+= _farnspeekl (offset
+ 2) & 0xff000000U
;
1732 if (taskbase
> 0xfffff)
1735 /* CR3 (a.k.a. PDBR, the Page Directory Base Register) is stored at
1736 offset 1Ch in the TSS. */
1737 cr3
= _farnspeekl (taskbase
+ 0x1c) & ~0xfff;
1740 #if 0 /* not fullly supported yet */
1741 /* The Page Directory is in UMBs. In that case, CWSDPMI puts
1742 the first Page Table right below the Page Directory. Thus,
1743 the first Page Table's entry for its own address and the Page
1744 Directory entry for that Page Table will hold the same
1745 physical address. The loop below searches the entire UMB
1746 range of addresses for such an occurence. */
1747 unsigned long addr
, pte_idx
;
1749 for (addr
= 0xb0000, pte_idx
= 0xb0;
1751 addr
+= 0x1000, pte_idx
++)
1753 if (((_farnspeekl (addr
+ 4 * pte_idx
) & 0xfffff027) ==
1754 (_farnspeekl (addr
+ 0x1000) & 0xfffff027))
1755 && ((_farnspeekl (addr
+ 4 * pte_idx
+ 4) & 0xfffff000) == cr3
))
1757 cr3
= addr
+ 0x1000;
1770 /* Return the N'th Page Directory entry. */
1771 static unsigned long
1774 unsigned long pde
= 0;
1776 if (pdbr
&& n
>= 0 && n
< 1024)
1778 pde
= _farpeekl (_dos_ds
, pdbr
+ 4*n
);
1783 /* Return the N'th entry of the Page Table whose Page Directory entry
1785 static unsigned long
1786 get_pte (unsigned long pde
, int n
)
1788 unsigned long pte
= 0;
1790 /* pde & 0x80 tests the 4MB page bit. We don't support 4MB
1791 page tables, for now. */
1792 if ((pde
& 1) && !(pde
& 0x80) && n
>= 0 && n
< 1024)
1794 pde
&= ~0xfff; /* clear non-address bits */
1795 pte
= _farpeekl (_dos_ds
, pde
+ 4*n
);
1800 /* Display a Page Directory or Page Table entry. IS_DIR, if non-zero,
1801 says this is a Page Directory entry. If FORCE is non-zero, display
1802 the entry even if its Present flag is off. OFF is the offset of the
1803 address from the page's base address. */
1805 display_ptable_entry (unsigned long entry
, int is_dir
, int force
, unsigned off
)
1807 if ((entry
& 1) != 0)
1809 printf_filtered ("Base=0x%05lx000", entry
>> 12);
1810 if ((entry
& 0x100) && !is_dir
)
1811 puts_filtered (" Global");
1812 if ((entry
& 0x40) && !is_dir
)
1813 puts_filtered (" Dirty");
1814 printf_filtered (" %sAcc.", (entry
& 0x20) ? "" : "Not-");
1815 printf_filtered (" %sCached", (entry
& 0x10) ? "" : "Not-");
1816 printf_filtered (" Write-%s", (entry
& 8) ? "Thru" : "Back");
1817 printf_filtered (" %s", (entry
& 4) ? "Usr" : "Sup");
1818 printf_filtered (" Read-%s", (entry
& 2) ? "Write" : "Only");
1820 printf_filtered (" +0x%x", off
);
1821 puts_filtered ("\n");
1824 printf_filtered ("Page%s not present or not supported; value=0x%lx.\n",
1825 is_dir
? " Table" : "", entry
>> 1);
1829 go32_pde (char *arg
, int from_tty
)
1831 long pde_idx
= -1, i
;
1835 while (*arg
&& isspace(*arg
))
1840 pde_idx
= parse_and_eval_long (arg
);
1841 if (pde_idx
< 0 || pde_idx
>= 1024)
1842 error (_("Entry %ld is outside valid limits [0..1023]."), pde_idx
);
1848 puts_filtered ("Access to Page Directories is not supported on this system.\n");
1849 else if (pde_idx
>= 0)
1850 display_ptable_entry (get_pde (pde_idx
), 1, 1, 0);
1852 for (i
= 0; i
< 1024; i
++)
1853 display_ptable_entry (get_pde (i
), 1, 0, 0);
1856 /* A helper function to display entries in a Page Table pointed to by
1857 the N'th entry in the Page Directory. If FORCE is non-zero, say
1858 something even if the Page Table is not accessible. */
1860 display_page_table (long n
, int force
)
1862 unsigned long pde
= get_pde (n
);
1868 printf_filtered ("Page Table pointed to by Page Directory entry 0x%lx:\n", n
);
1869 for (i
= 0; i
< 1024; i
++)
1870 display_ptable_entry (get_pte (pde
, i
), 0, 0, 0);
1871 puts_filtered ("\n");
1874 printf_filtered ("Page Table not present; value=0x%lx.\n", pde
>> 1);
1878 go32_pte (char *arg
, int from_tty
)
1880 long pde_idx
= -1L, i
;
1884 while (*arg
&& isspace(*arg
))
1889 pde_idx
= parse_and_eval_long (arg
);
1890 if (pde_idx
< 0 || pde_idx
>= 1024)
1891 error (_("Entry %ld is outside valid limits [0..1023]."), pde_idx
);
1897 puts_filtered ("Access to Page Tables is not supported on this system.\n");
1898 else if (pde_idx
>= 0)
1899 display_page_table (pde_idx
, 1);
1901 for (i
= 0; i
< 1024; i
++)
1902 display_page_table (i
, 0);
1906 go32_pte_for_address (char *arg
, int from_tty
)
1908 CORE_ADDR addr
= 0, i
;
1912 while (*arg
&& isspace(*arg
))
1916 addr
= parse_and_eval_address (arg
);
1919 error_no_arg (_("linear address"));
1923 puts_filtered ("Access to Page Tables is not supported on this system.\n");
1926 int pde_idx
= (addr
>> 22) & 0x3ff;
1927 int pte_idx
= (addr
>> 12) & 0x3ff;
1928 unsigned offs
= addr
& 0xfff;
1930 printf_filtered ("Page Table entry for address 0x%llx:\n",
1931 (unsigned long long)addr
);
1932 display_ptable_entry (get_pte (get_pde (pde_idx
), pte_idx
), 0, 1, offs
);
1936 static struct cmd_list_element
*info_dos_cmdlist
= NULL
;
1939 go32_info_dos_command (char *args
, int from_tty
)
1941 help_list (info_dos_cmdlist
, "info dos ", class_info
, gdb_stdout
);
1945 _initialize_go32_nat (void)
1948 add_target (&go32_ops
);
1950 add_prefix_cmd ("dos", class_info
, go32_info_dos_command
, _("\
1951 Print information specific to DJGPP (aka MS-DOS) debugging."),
1952 &info_dos_cmdlist
, "info dos ", 0, &infolist
);
1954 add_cmd ("sysinfo", class_info
, go32_sysinfo
, _("\
1955 Display information about the target system, including CPU, OS, DPMI, etc."),
1957 add_cmd ("ldt", class_info
, go32_sldt
, _("\
1958 Display entries in the LDT (Local Descriptor Table).\n\
1959 Entry number (an expression) as an argument means display only that entry."),
1961 add_cmd ("gdt", class_info
, go32_sgdt
, _("\
1962 Display entries in the GDT (Global Descriptor Table).\n\
1963 Entry number (an expression) as an argument means display only that entry."),
1965 add_cmd ("idt", class_info
, go32_sidt
, _("\
1966 Display entries in the IDT (Interrupt Descriptor Table).\n\
1967 Entry number (an expression) as an argument means display only that entry."),
1969 add_cmd ("pde", class_info
, go32_pde
, _("\
1970 Display entries in the Page Directory.\n\
1971 Entry number (an expression) as an argument means display only that entry."),
1973 add_cmd ("pte", class_info
, go32_pte
, _("\
1974 Display entries in Page Tables.\n\
1975 Entry number (an expression) as an argument means display only entries\n\
1976 from the Page Table pointed to by the specified Page Directory entry."),
1978 add_cmd ("address-pte", class_info
, go32_pte_for_address
, _("\
1979 Display a Page Table entry for a linear address.\n\
1980 The address argument must be a linear address, after adding to\n\
1981 it the base address of the appropriate segment.\n\
1982 The base address of variables and functions in the debuggee's data\n\
1983 or code segment is stored in the variable __djgpp_base_address,\n\
1984 so use `__djgpp_base_address + (char *)&var' as the argument.\n\
1985 For other segments, look up their base address in the output of\n\
1986 the `info dos ldt' command."),
2000 tcsetpgrp (int fd
, pid_t pgid
)
2002 if (isatty (fd
) && pgid
== SOME_PID
)
2004 errno
= pgid
== SOME_PID
? ENOTTY
: ENOSYS
;