forked from gbenson/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo32-nat.c
2146 lines (1913 loc) · 59.6 KB
/
go32-nat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Native debugging support for Intel x86 running DJGPP.
Copyright (C) 1997-2015 Free Software Foundation, Inc.
Written by Robert Hoehne.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* To whomever it may concern, here's a general description of how
debugging in DJGPP works, and the special quirks GDB does to
support that.
When the DJGPP port of GDB is debugging a DJGPP program natively,
there aren't 2 separate processes, the debuggee and GDB itself, as
on other systems. (This is DOS, where there can only be one active
process at any given time, remember?) Instead, GDB and the
debuggee live in the same process. So when GDB calls
go32_create_inferior below, and that function calls edi_init from
the DJGPP debug support library libdbg.a, we load the debuggee's
executable file into GDB's address space, set it up for execution
as the stub loader (a short real-mode program prepended to each
DJGPP executable) normally would, and do a lot of preparations for
swapping between GDB's and debuggee's internal state, primarily wrt
the exception handlers. This swapping happens every time we resume
the debuggee or switch back to GDB's code, and it includes:
. swapping all the segment registers
. swapping the PSP (the Program Segment Prefix)
. swapping the signal handlers
. swapping the exception handlers
. swapping the FPU status
. swapping the 3 standard file handles (more about this below)
Then running the debuggee simply means longjmp into it where its PC
is and let it run until it stops for some reason. When it stops,
GDB catches the exception that stopped it and longjmp's back into
its own code. All the possible exit points of the debuggee are
watched; for example, the normal exit point is recognized because a
DOS program issues a special system call to exit. If one of those
exit points is hit, we mourn the inferior and clean up after it.
Cleaning up is very important, even if the process exits normally,
because otherwise we might leave behind traces of previous
execution, and in several cases GDB itself might be left hosed,
because all the exception handlers were not restored.
Swapping of the standard handles (in redir_to_child and
redir_to_debugger) is needed because, since both GDB and the
debuggee live in the same process, as far as the OS is concerned,
the share the same file table. This means that the standard
handles 0, 1, and 2 point to the same file table entries, and thus
are connected to the same devices. Therefore, if the debugger
redirects its standard output, the standard output of the debuggee
is also automagically redirected to the same file/device!
Similarly, if the debuggee redirects its stdout to a file, you
won't be able to see debugger's output (it will go to the same file
where the debuggee has its output); and if the debuggee closes its
standard input, you will lose the ability to talk to debugger!
For this reason, every time the debuggee is about to be resumed, we
call redir_to_child, which redirects the standard handles to where
the debuggee expects them to be. When the debuggee stops and GDB
regains control, we call redir_to_debugger, which redirects those 3
handles back to where GDB expects.
Note that only the first 3 handles are swapped, so if the debuggee
redirects or closes any other handles, GDB will not notice. In
particular, the exit code of a DJGPP program forcibly closes all
file handles beyond the first 3 ones, so when the debuggee exits,
GDB currently loses its stdaux and stdprn streams. Fortunately,
GDB does not use those as of this writing, and will never need
to. */
#include "defs.h"
#include <fcntl.h>
#include "x86-nat.h"
#include "inferior.h"
#include "infrun.h"
#include "gdbthread.h"
#include "gdb_wait.h"
#include "gdbcore.h"
#include "command.h"
#include "gdbcmd.h"
#include "floatformat.h"
#include "buildsym.h"
#include "i387-tdep.h"
#include "i386-tdep.h"
#include "nat/x86-cpuid.h"
#include "value.h"
#include "regcache.h"
#include "top.h"
#include "cli/cli-utils.h"
#include "inf-child.h"
#include <ctype.h>
#include <unistd.h>
#include <sys/utsname.h>
#include <io.h>
#include <dos.h>
#include <dpmi.h>
#include <go32.h>
#include <sys/farptr.h>
#include <debug/v2load.h>
#include <debug/dbgcom.h>
#if __DJGPP_MINOR__ > 2
#include <debug/redir.h>
#endif
#include <langinfo.h>
#if __DJGPP_MINOR__ < 3
/* This code will be provided from DJGPP 2.03 on. Until then I code it
here. */
typedef struct
{
unsigned short sig0;
unsigned short sig1;
unsigned short sig2;
unsigned short sig3;
unsigned short exponent:15;
unsigned short sign:1;
}
NPXREG;
typedef struct
{
unsigned int control;
unsigned int status;
unsigned int tag;
unsigned int eip;
unsigned int cs;
unsigned int dataptr;
unsigned int datasel;
NPXREG reg[8];
}
NPX;
static NPX npx;
static void save_npx (void); /* Save the FPU of the debugged program. */
static void load_npx (void); /* Restore the FPU of the debugged program. */
/* ------------------------------------------------------------------------- */
/* Store the contents of the NPX in the global variable `npx'. */
/* *INDENT-OFF* */
static void
save_npx (void)
{
asm ("inb $0xa0, %%al \n\
testb $0x20, %%al \n\
jz 1f \n\
xorb %%al, %%al \n\
outb %%al, $0xf0 \n\
movb $0x20, %%al \n\
outb %%al, $0xa0 \n\
outb %%al, $0x20 \n\
1: \n\
fnsave %0 \n\
fwait "
: "=m" (npx)
: /* No input */
: "%eax");
}
/* *INDENT-ON* */
/* ------------------------------------------------------------------------- */
/* Reload the contents of the NPX from the global variable `npx'. */
static void
load_npx (void)
{
asm ("frstor %0":"=m" (npx));
}
/* ------------------------------------------------------------------------- */
/* Stubs for the missing redirection functions. */
typedef struct {
char *command;
int redirected;
} cmdline_t;
void
redir_cmdline_delete (cmdline_t *ptr)
{
ptr->redirected = 0;
}
int
redir_cmdline_parse (const char *args, cmdline_t *ptr)
{
return -1;
}
int
redir_to_child (cmdline_t *ptr)
{
return 1;
}
int
redir_to_debugger (cmdline_t *ptr)
{
return 1;
}
int
redir_debug_init (cmdline_t *ptr)
{
return 0;
}
#endif /* __DJGPP_MINOR < 3 */
typedef enum { wp_insert, wp_remove, wp_count } wp_op;
/* This holds the current reference counts for each debug register. */
static int dr_ref_count[4];
#define SOME_PID 42
static int prog_has_started = 0;
static void go32_mourn_inferior (struct target_ops *ops);
#define r_ofs(x) (offsetof(TSS,x))
static struct
{
size_t tss_ofs;
size_t size;
}
regno_mapping[] =
{
{r_ofs (tss_eax), 4}, /* normal registers, from a_tss */
{r_ofs (tss_ecx), 4},
{r_ofs (tss_edx), 4},
{r_ofs (tss_ebx), 4},
{r_ofs (tss_esp), 4},
{r_ofs (tss_ebp), 4},
{r_ofs (tss_esi), 4},
{r_ofs (tss_edi), 4},
{r_ofs (tss_eip), 4},
{r_ofs (tss_eflags), 4},
{r_ofs (tss_cs), 2},
{r_ofs (tss_ss), 2},
{r_ofs (tss_ds), 2},
{r_ofs (tss_es), 2},
{r_ofs (tss_fs), 2},
{r_ofs (tss_gs), 2},
{0, 10}, /* 8 FP registers, from npx.reg[] */
{1, 10},
{2, 10},
{3, 10},
{4, 10},
{5, 10},
{6, 10},
{7, 10},
/* The order of the next 7 registers must be consistent
with their numbering in config/i386/tm-i386.h, which see. */
{0, 2}, /* control word, from npx */
{4, 2}, /* status word, from npx */
{8, 2}, /* tag word, from npx */
{16, 2}, /* last FP exception CS from npx */
{12, 4}, /* last FP exception EIP from npx */
{24, 2}, /* last FP exception operand selector from npx */
{20, 4}, /* last FP exception operand offset from npx */
{18, 2} /* last FP opcode from npx */
};
static struct
{
int go32_sig;
enum gdb_signal gdb_sig;
}
sig_map[] =
{
{0, GDB_SIGNAL_FPE},
{1, GDB_SIGNAL_TRAP},
/* Exception 2 is triggered by the NMI. DJGPP handles it as SIGILL,
but I think SIGBUS is better, since the NMI is usually activated
as a result of a memory parity check failure. */
{2, GDB_SIGNAL_BUS},
{3, GDB_SIGNAL_TRAP},
{4, GDB_SIGNAL_FPE},
{5, GDB_SIGNAL_SEGV},
{6, GDB_SIGNAL_ILL},
{7, GDB_SIGNAL_EMT}, /* no-coprocessor exception */
{8, GDB_SIGNAL_SEGV},
{9, GDB_SIGNAL_SEGV},
{10, GDB_SIGNAL_BUS},
{11, GDB_SIGNAL_SEGV},
{12, GDB_SIGNAL_SEGV},
{13, GDB_SIGNAL_SEGV},
{14, GDB_SIGNAL_SEGV},
{16, GDB_SIGNAL_FPE},
{17, GDB_SIGNAL_BUS},
{31, GDB_SIGNAL_ILL},
{0x1b, GDB_SIGNAL_INT},
{0x75, GDB_SIGNAL_FPE},
{0x78, GDB_SIGNAL_ALRM},
{0x79, GDB_SIGNAL_INT},
{0x7a, GDB_SIGNAL_QUIT},
{-1, GDB_SIGNAL_LAST}
};
static struct {
enum gdb_signal gdb_sig;
int djgpp_excepno;
} excepn_map[] = {
{GDB_SIGNAL_0, -1},
{GDB_SIGNAL_ILL, 6}, /* Invalid Opcode */
{GDB_SIGNAL_EMT, 7}, /* triggers SIGNOFP */
{GDB_SIGNAL_SEGV, 13}, /* GPF */
{GDB_SIGNAL_BUS, 17}, /* Alignment Check */
/* The rest are fake exceptions, see dpmiexcp.c in djlsr*.zip for
details. */
{GDB_SIGNAL_TERM, 0x1b}, /* triggers Ctrl-Break type of SIGINT */
{GDB_SIGNAL_FPE, 0x75},
{GDB_SIGNAL_INT, 0x79},
{GDB_SIGNAL_QUIT, 0x7a},
{GDB_SIGNAL_ALRM, 0x78}, /* triggers SIGTIMR */
{GDB_SIGNAL_PROF, 0x78},
{GDB_SIGNAL_LAST, -1}
};
static void
go32_attach (struct target_ops *ops, const char *args, int from_tty)
{
error (_("\
You cannot attach to a running program on this platform.\n\
Use the `run' command to run DJGPP programs."));
}
static int resume_is_step;
static int resume_signal = -1;
static void
go32_resume (struct target_ops *ops,
ptid_t ptid, int step, enum gdb_signal siggnal)
{
int i;
resume_is_step = step;
if (siggnal != GDB_SIGNAL_0 && siggnal != GDB_SIGNAL_TRAP)
{
for (i = 0, resume_signal = -1;
excepn_map[i].gdb_sig != GDB_SIGNAL_LAST; i++)
if (excepn_map[i].gdb_sig == siggnal)
{
resume_signal = excepn_map[i].djgpp_excepno;
break;
}
if (resume_signal == -1)
printf_unfiltered ("Cannot deliver signal %s on this platform.\n",
gdb_signal_to_name (siggnal));
}
}
static char child_cwd[FILENAME_MAX];
static ptid_t
go32_wait (struct target_ops *ops,
ptid_t ptid, struct target_waitstatus *status, int options)
{
int i;
unsigned char saved_opcode;
unsigned long INT3_addr = 0;
int stepping_over_INT = 0;
a_tss.tss_eflags &= 0xfeff; /* Reset the single-step flag (TF). */
if (resume_is_step)
{
/* If the next instruction is INT xx or INTO, we need to handle
them specially. Intel manuals say that these instructions
reset the single-step flag (a.k.a. TF). However, it seems
that, at least in the DPMI environment, and at least when
stepping over the DPMI interrupt 31h, the problem is having
TF set at all when INT 31h is executed: the debuggee either
crashes (and takes the system with it) or is killed by a
SIGTRAP.
So we need to emulate single-step mode: we put an INT3 opcode
right after the INT xx instruction, let the debuggee run
until it hits INT3 and stops, then restore the original
instruction which we overwrote with the INT3 opcode, and back
up the debuggee's EIP to that instruction. */
read_child (a_tss.tss_eip, &saved_opcode, 1);
if (saved_opcode == 0xCD || saved_opcode == 0xCE)
{
unsigned char INT3_opcode = 0xCC;
INT3_addr
= saved_opcode == 0xCD ? a_tss.tss_eip + 2 : a_tss.tss_eip + 1;
stepping_over_INT = 1;
read_child (INT3_addr, &saved_opcode, 1);
write_child (INT3_addr, &INT3_opcode, 1);
}
else
a_tss.tss_eflags |= 0x0100; /* normal instruction: set TF */
}
/* The special value FFFFh in tss_trap indicates to run_child that
tss_irqn holds a signal to be delivered to the debuggee. */
if (resume_signal <= -1)
{
a_tss.tss_trap = 0;
a_tss.tss_irqn = 0xff;
}
else
{
a_tss.tss_trap = 0xffff; /* run_child looks for this. */
a_tss.tss_irqn = resume_signal;
}
/* The child might change working directory behind our back. The
GDB users won't like the side effects of that when they work with
relative file names, and GDB might be confused by its current
directory not being in sync with the truth. So we always make a
point of changing back to where GDB thinks is its cwd, when we
return control to the debugger, but restore child's cwd before we
run it. */
/* Initialize child_cwd, before the first call to run_child and not
in the initialization, so the child get also the changed directory
set with the gdb-command "cd ..." */
if (!*child_cwd)
/* Initialize child's cwd with the current one. */
getcwd (child_cwd, sizeof (child_cwd));
chdir (child_cwd);
#if __DJGPP_MINOR__ < 3
load_npx ();
#endif
run_child ();
#if __DJGPP_MINOR__ < 3
save_npx ();
#endif
/* Did we step over an INT xx instruction? */
if (stepping_over_INT && a_tss.tss_eip == INT3_addr + 1)
{
/* Restore the original opcode. */
a_tss.tss_eip--; /* EIP points *after* the INT3 instruction. */
write_child (a_tss.tss_eip, &saved_opcode, 1);
/* Simulate a TRAP exception. */
a_tss.tss_irqn = 1;
a_tss.tss_eflags |= 0x0100;
}
getcwd (child_cwd, sizeof (child_cwd)); /* in case it has changed */
chdir (current_directory);
if (a_tss.tss_irqn == 0x21)
{
status->kind = TARGET_WAITKIND_EXITED;
status->value.integer = a_tss.tss_eax & 0xff;
}
else
{
status->value.sig = GDB_SIGNAL_UNKNOWN;
status->kind = TARGET_WAITKIND_STOPPED;
for (i = 0; sig_map[i].go32_sig != -1; i++)
{
if (a_tss.tss_irqn == sig_map[i].go32_sig)
{
#if __DJGPP_MINOR__ < 3
if ((status->value.sig = sig_map[i].gdb_sig) !=
GDB_SIGNAL_TRAP)
status->kind = TARGET_WAITKIND_SIGNALLED;
#else
status->value.sig = sig_map[i].gdb_sig;
#endif
break;
}
}
}
return pid_to_ptid (SOME_PID);
}
static void
fetch_register (struct regcache *regcache, int regno)
{
struct gdbarch *gdbarch = get_regcache_arch (regcache);
if (regno < gdbarch_fp0_regnum (gdbarch))
regcache_raw_supply (regcache, regno,
(char *) &a_tss + regno_mapping[regno].tss_ofs);
else if (i386_fp_regnum_p (gdbarch, regno) || i386_fpc_regnum_p (gdbarch,
regno))
i387_supply_fsave (regcache, regno, &npx);
else
internal_error (__FILE__, __LINE__,
_("Invalid register no. %d in fetch_register."), regno);
}
static void
go32_fetch_registers (struct target_ops *ops,
struct regcache *regcache, int regno)
{
if (regno >= 0)
fetch_register (regcache, regno);
else
{
for (regno = 0;
regno < gdbarch_fp0_regnum (get_regcache_arch (regcache));
regno++)
fetch_register (regcache, regno);
i387_supply_fsave (regcache, -1, &npx);
}
}
static void
store_register (const struct regcache *regcache, int regno)
{
struct gdbarch *gdbarch = get_regcache_arch (regcache);
if (regno < gdbarch_fp0_regnum (gdbarch))
regcache_raw_collect (regcache, regno,
(char *) &a_tss + regno_mapping[regno].tss_ofs);
else if (i386_fp_regnum_p (gdbarch, regno) || i386_fpc_regnum_p (gdbarch,
regno))
i387_collect_fsave (regcache, regno, &npx);
else
internal_error (__FILE__, __LINE__,
_("Invalid register no. %d in store_register."), regno);
}
static void
go32_store_registers (struct target_ops *ops,
struct regcache *regcache, int regno)
{
unsigned r;
if (regno >= 0)
store_register (regcache, regno);
else
{
for (r = 0; r < gdbarch_fp0_regnum (get_regcache_arch (regcache)); r++)
store_register (regcache, r);
i387_collect_fsave (regcache, -1, &npx);
}
}
/* Const-correct version of DJGPP's write_child, which unfortunately
takes a non-const buffer pointer. */
static int
my_write_child (unsigned child_addr, const void *buf, unsigned len)
{
static void *buffer = NULL;
static unsigned buffer_len = 0;
int res;
if (buffer_len < len)
{
buffer = xrealloc (buffer, len);
buffer_len = len;
}
memcpy (buffer, buf, len);
res = write_child (child_addr, buffer, len);
return res;
}
/* Helper for go32_xfer_partial that handles memory transfers.
Arguments are like target_xfer_partial. */
static enum target_xfer_status
go32_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
{
int res;
if (writebuf != NULL)
res = my_write_child (memaddr, writebuf, len);
else
res = read_child (memaddr, readbuf, len);
/* read_child and write_child return zero on success, non-zero on
failure. */
if (res != 0)
return TARGET_XFER_E_IO;
*xfered_len = len;
return TARGET_XFER_OK;
}
/* Target to_xfer_partial implementation. */
static enum target_xfer_status
go32_xfer_partial (struct target_ops *ops, enum target_object object,
const char *annex, gdb_byte *readbuf,
const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
ULONGEST *xfered_len)
{
switch (object)
{
case TARGET_OBJECT_MEMORY:
return go32_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
default:
return ops->beneath->to_xfer_partial (ops->beneath, object, annex,
readbuf, writebuf, offset, len,
xfered_len);
}
}
static cmdline_t child_cmd; /* Parsed child's command line kept here. */
static void
go32_files_info (struct target_ops *target)
{
printf_unfiltered ("You are running a DJGPP V2 program.\n");
}
static void
go32_kill_inferior (struct target_ops *ops)
{
go32_mourn_inferior (ops);
}
static void
go32_create_inferior (struct target_ops *ops, char *exec_file,
char *args, char **env, int from_tty)
{
extern char **environ;
jmp_buf start_state;
char *cmdline;
char **env_save = environ;
size_t cmdlen;
struct inferior *inf;
int result;
/* If no exec file handed to us, get it from the exec-file command -- with
a good, common error message if none is specified. */
if (exec_file == 0)
exec_file = get_exec_file (1);
resume_signal = -1;
resume_is_step = 0;
/* Initialize child's cwd as empty to be initialized when starting
the child. */
*child_cwd = 0;
/* Init command line storage. */
if (redir_debug_init (&child_cmd) == -1)
internal_error (__FILE__, __LINE__,
_("Cannot allocate redirection storage: "
"not enough memory.\n"));
/* Parse the command line and create redirections. */
if (strpbrk (args, "<>"))
{
if (redir_cmdline_parse (args, &child_cmd) == 0)
args = child_cmd.command;
else
error (_("Syntax error in command line."));
}
else
child_cmd.command = xstrdup (args);
cmdlen = strlen (args);
/* v2loadimage passes command lines via DOS memory, so it cannot
possibly handle commands longer than 1MB. */
if (cmdlen > 1024*1024)
error (_("Command line too long."));
cmdline = xmalloc (cmdlen + 4);
strcpy (cmdline + 1, args);
/* If the command-line length fits into DOS 126-char limits, use the
DOS command tail format; otherwise, tell v2loadimage to pass it
through a buffer in conventional memory. */
if (cmdlen < 127)
{
cmdline[0] = strlen (args);
cmdline[cmdlen + 1] = 13;
}
else
cmdline[0] = 0xff; /* Signal v2loadimage it's a long command. */
environ = env;
result = v2loadimage (exec_file, cmdline, start_state);
environ = env_save;
xfree (cmdline);
if (result != 0)
error (_("Load failed for image %s"), exec_file);
edi_init (start_state);
#if __DJGPP_MINOR__ < 3
save_npx ();
#endif
inferior_ptid = pid_to_ptid (SOME_PID);
inf = current_inferior ();
inferior_appeared (inf, SOME_PID);
if (!target_is_pushed (ops))
push_target (ops);
add_thread_silent (inferior_ptid);
clear_proceed_status (0);
insert_breakpoints ();
prog_has_started = 1;
}
static void
go32_mourn_inferior (struct target_ops *ops)
{
ptid_t ptid;
redir_cmdline_delete (&child_cmd);
resume_signal = -1;
resume_is_step = 0;
cleanup_client ();
/* We need to make sure all the breakpoint enable bits in the DR7
register are reset when the inferior exits. Otherwise, if they
rerun the inferior, the uncleared bits may cause random SIGTRAPs,
failure to set more watchpoints, and other calamities. It would
be nice if GDB itself would take care to remove all breakpoints
at all times, but it doesn't, probably under an assumption that
the OS cleans up when the debuggee exits. */
x86_cleanup_dregs ();
ptid = inferior_ptid;
inferior_ptid = null_ptid;
delete_thread_silent (ptid);
prog_has_started = 0;
generic_mourn_inferior ();
inf_child_maybe_unpush_target (ops);
}
/* Hardware watchpoint support. */
#define D_REGS edi.dr
#define CONTROL D_REGS[7]
#define STATUS D_REGS[6]
/* Pass the address ADDR to the inferior in the I'th debug register.
Here we just store the address in D_REGS, the watchpoint will be
actually set up when go32_wait runs the debuggee. */
static void
go32_set_dr (int i, CORE_ADDR addr)
{
if (i < 0 || i > 3)
internal_error (__FILE__, __LINE__,
_("Invalid register %d in go32_set_dr.\n"), i);
D_REGS[i] = addr;
}
/* Pass the value VAL to the inferior in the DR7 debug control
register. Here we just store the address in D_REGS, the watchpoint
will be actually set up when go32_wait runs the debuggee. */
static void
go32_set_dr7 (unsigned long val)
{
CONTROL = val;
}
/* Get the value of the DR6 debug status register from the inferior.
Here we just return the value stored in D_REGS, as we've got it
from the last go32_wait call. */
static unsigned long
go32_get_dr6 (void)
{
return STATUS;
}
/* Get the value of the DR7 debug status register from the inferior.
Here we just return the value stored in D_REGS, as we've got it
from the last go32_wait call. */
static unsigned long
go32_get_dr7 (void)
{
return CONTROL;
}
/* Get the value of the DR debug register I from the inferior. Here
we just return the value stored in D_REGS, as we've got it from the
last go32_wait call. */
static CORE_ADDR
go32_get_dr (int i)
{
if (i < 0 || i > 3)
internal_error (__FILE__, __LINE__,
_("Invalid register %d in go32_get_dr.\n"), i);
return D_REGS[i];
}
/* Put the device open on handle FD into either raw or cooked
mode, return 1 if it was in raw mode, zero otherwise. */
static int
device_mode (int fd, int raw_p)
{
int oldmode, newmode;
__dpmi_regs regs;
regs.x.ax = 0x4400;
regs.x.bx = fd;
__dpmi_int (0x21, ®s);
if (regs.x.flags & 1)
return -1;
newmode = oldmode = regs.x.dx;
if (raw_p)
newmode |= 0x20;
else
newmode &= ~0x20;
if (oldmode & 0x80) /* Only for character dev. */
{
regs.x.ax = 0x4401;
regs.x.bx = fd;
regs.x.dx = newmode & 0xff; /* Force upper byte zero, else it fails. */
__dpmi_int (0x21, ®s);
if (regs.x.flags & 1)
return -1;
}
return (oldmode & 0x20) == 0x20;
}
static int inf_mode_valid = 0;
static int inf_terminal_mode;
/* This semaphore is needed because, amazingly enough, GDB calls
target.to_terminal_ours more than once after the inferior stops.
But we need the information from the first call only, since the
second call will always see GDB's own cooked terminal. */
static int terminal_is_ours = 1;
static void
go32_terminal_init (struct target_ops *self)
{
inf_mode_valid = 0; /* Reinitialize, in case they are restarting child. */
terminal_is_ours = 1;
}
static void
go32_terminal_info (struct target_ops *self, const char *args, int from_tty)
{
printf_unfiltered ("Inferior's terminal is in %s mode.\n",
!inf_mode_valid
? "default" : inf_terminal_mode ? "raw" : "cooked");
#if __DJGPP_MINOR__ > 2
if (child_cmd.redirection)
{
int i;
for (i = 0; i < DBG_HANDLES; i++)
{
if (child_cmd.redirection[i]->file_name)
printf_unfiltered ("\tFile handle %d is redirected to `%s'.\n",
i, child_cmd.redirection[i]->file_name);
else if (_get_dev_info (child_cmd.redirection[i]->inf_handle) == -1)
printf_unfiltered
("\tFile handle %d appears to be closed by inferior.\n", i);
/* Mask off the raw/cooked bit when comparing device info words. */
else if ((_get_dev_info (child_cmd.redirection[i]->inf_handle) & 0xdf)
!= (_get_dev_info (i) & 0xdf))
printf_unfiltered
("\tFile handle %d appears to be redirected by inferior.\n", i);
}
}
#endif
}
static void
go32_terminal_inferior (struct target_ops *self)
{
/* Redirect standard handles as child wants them. */
errno = 0;
if (redir_to_child (&child_cmd) == -1)
{
redir_to_debugger (&child_cmd);
error (_("Cannot redirect standard handles for program: %s."),
safe_strerror (errno));
}
/* Set the console device of the inferior to whatever mode
(raw or cooked) we found it last time. */
if (terminal_is_ours)
{
if (inf_mode_valid)
device_mode (0, inf_terminal_mode);
terminal_is_ours = 0;
}
}
static void
go32_terminal_ours (struct target_ops *self)
{
/* Switch to cooked mode on the gdb terminal and save the inferior
terminal mode to be restored when it is resumed. */
if (!terminal_is_ours)
{
inf_terminal_mode = device_mode (0, 0);
if (inf_terminal_mode != -1)
inf_mode_valid = 1;
else
/* If device_mode returned -1, we don't know what happens with
handle 0 anymore, so make the info invalid. */
inf_mode_valid = 0;
terminal_is_ours = 1;
/* Restore debugger's standard handles. */
errno = 0;
if (redir_to_debugger (&child_cmd) == -1)
{
redir_to_child (&child_cmd);
error (_("Cannot redirect standard handles for debugger: %s."),
safe_strerror (errno));
}
}
}
static int
go32_thread_alive (struct target_ops *ops, ptid_t ptid)
{
return !ptid_equal (inferior_ptid, null_ptid);
}
static char *
go32_pid_to_str (struct target_ops *ops, ptid_t ptid)
{
return normal_pid_to_str (ptid);
}
/* Create a go32 target. */
static struct target_ops *
go32_target (void)
{
struct target_ops *t = inf_child_target ();
t->to_attach = go32_attach;
t->to_resume = go32_resume;
t->to_wait = go32_wait;
t->to_fetch_registers = go32_fetch_registers;
t->to_store_registers = go32_store_registers;
t->to_xfer_partial = go32_xfer_partial;
t->to_files_info = go32_files_info;
t->to_terminal_init = go32_terminal_init;
t->to_terminal_inferior = go32_terminal_inferior;
t->to_terminal_ours_for_output = go32_terminal_ours;
t->to_terminal_ours = go32_terminal_ours;
t->to_terminal_info = go32_terminal_info;
t->to_kill = go32_kill_inferior;
t->to_create_inferior = go32_create_inferior;
t->to_mourn_inferior = go32_mourn_inferior;
t->to_thread_alive = go32_thread_alive;
t->to_pid_to_str = go32_pid_to_str;
return t;
}
/* Return the current DOS codepage number. */
static int
dos_codepage (void)
{
__dpmi_regs regs;
regs.x.ax = 0x6601;
__dpmi_int (0x21, ®s);
if (!(regs.x.flags & 1))
return regs.x.bx & 0xffff;
else
return 437; /* default */
}
/* Limited emulation of `nl_langinfo', for charset.c. */
char *
nl_langinfo (nl_item item)
{
char *retval;
switch (item)
{
case CODESET: