More LynxOS support
[binutils-gdb.git] / gdb / i386ly-nat.c
1 /* Native-dependent code for Intel 386 running LynxOS.
2 Copyright 1988, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "target.h"
24
25 #include <sys/ptrace.h>
26 #include "/usr/include/sys/wait.h"
27
28 /* these values indicate the offset of the named register in the econtext
29 structure */
30
31 #define EAX 10
32 #define ECX 9
33 #define EDX 8
34 #define EBX 7
35 #define ESP 16
36 #define EBP 5
37 #define ESI 4
38 #define EDI 3
39 #define EIP 13
40 #define EFL 15
41 #define CS 14
42 #define SS 17
43 #define DS 2
44 #define ES 1
45
46 /* Currently these are not being used. So set them to 0 */
47
48 #define FS 0
49 #define GS 0
50
51 /* this table must line up with REGISTER_NAMES in m-i386.h */
52 static unsigned int regmap[] =
53 {
54 EAX, ECX, EDX, EBX,
55 ESP, EBP, ESI, EDI,
56 EIP, EFL, CS, SS,
57 DS, ES, FS, GS,
58 };
59
60 /* Return the address in the core dump or inferior of register REGNO.
61 BLOCKEND is the address of the econtext structure */
62
63 static unsigned int
64 register_addr (regno, blockend)
65 int regno, blockend;
66 {
67 if (regno < 0 || regno >= NUM_REGS)
68 error ("Invalid register number %d.", regno);
69
70 return (blockend + regmap[regno] * sizeof (long));
71 }
72
73 /* Fetch one register. */
74
75 static void
76 fetch_register (regno, offset, bpid)
77 int regno, bpid;
78 unsigned int offset;
79 {
80 unsigned int regaddr;
81 char buf[MAX_REGISTER_RAW_SIZE];
82 char mess[128]; /* For messages */
83 int i;
84
85 regaddr = register_addr (regno, offset);
86 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
87 {
88 errno = 0;
89 *(int *) &buf[i] = ptrace (PTRACE_PEEKTHREAD, bpid,
90 (PTRACE_ARG3_TYPE) regaddr, 0);
91 regaddr += sizeof (int);
92 if (errno != 0)
93 {
94 sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
95 perror_with_name (mess);
96 }
97 }
98 supply_register (regno, buf);
99 }
100
101 /* Store our register values back into the inferior.
102 If REGNO is -1, do this for all registers.
103 Otherwise, REGNO specifies which register (so we can save time). */
104
105 static void
106 store_register (regno, offset, bpid)
107 int regno, bpid;
108 unsigned int offset;
109 {
110 unsigned int regaddr;
111 char mess[128];
112 extern char registers[];
113 int i;
114
115 regaddr = register_addr (regno, offset);
116 for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
117 {
118 errno = 0;
119 ptrace (PTRACE_POKEUSER, bpid, (PTRACE_ARG3_TYPE) regaddr,
120 *(int *) &registers[REGISTER_BYTE (regno) + i]);
121 if (errno != 0)
122 {
123 sprintf (mess, "writing register number %d(%d)", regno, i);
124 perror_with_name (mess);
125 }
126 regaddr += sizeof(int);
127 }
128 }
129
130 /* return an offset for use with register_addr() */
131
132 static unsigned int
133 fetch_offset (pid)
134 int pid;
135 {
136 struct st_entry s;
137 unsigned int specpage_off, offset = (char *) &s.ecp - (char *) &s;
138
139 errno = 0;
140 specpage_off = ptrace (PTRACE_THREADUSER, pid, (PTRACE_ARG3_TYPE) 0, 0);
141 if (errno != 0)
142 perror_with_name ("ptrace");
143 errno = 0;
144 offset = ptrace (PTRACE_PEEKTHREAD, pid, (PTRACE_ARG3_TYPE) offset, 0)
145 - specpage_off;
146 if (errno != 0)
147 perror_with_name ("ptrace");
148 return offset;
149 }
150
151 /* Fetch all registers, or just one, from the child process. */
152
153 void
154 fetch_inferior_registers (regno)
155 int regno;
156 {
157 unsigned int offset = fetch_offset (inferior_pid);
158
159 if (regno == -1)
160 {
161 for (regno = 0; regno < NUM_REGS; regno++)
162 fetch_register (regno, offset, inferior_pid);
163 }
164 else
165 fetch_register (regno, offset, inferior_pid);
166 }
167
168 /* Store all registers, or just one, to the child process. */
169
170 void
171 store_inferior_registers (regno)
172 int regno;
173 {
174 unsigned int offset = fetch_offset (inferior_pid);
175
176 if (regno == -1)
177 {
178 for (regno = 0; regno < NUM_REGS; regno++)
179 store_register (regno, offset, inferior_pid);
180 }
181 else
182 store_register (regno, offset, inferior_pid);
183 }
184
185 /* Wait for child to do something. Return pid of child, or -1 in case
186 of error; store status through argument pointer STATUS. */
187
188 int
189 child_wait (pid, status)
190 int pid;
191 int *status;
192 {
193 int save_errno;
194 int thread;
195
196 while (1)
197 {
198 int sig;
199
200 if (attach_flag)
201 set_sigint_trap(); /* Causes SIGINT to be passed on to the
202 attached process. */
203 pid = wait (status);
204 save_errno = errno;
205
206 if (attach_flag)
207 clear_sigint_trap();
208
209 if (pid == -1)
210 {
211 if (save_errno == EINTR)
212 continue;
213 fprintf (stderr, "Child process unexpectedly missing: %s.\n",
214 safe_strerror (save_errno));
215 *status = 42; /* Claim it exited with signal 42 */
216 return -1;
217 }
218
219 if (pid != PIDGET (inferior_pid)) /* Some other process?!? */
220 continue;
221
222 /* thread = WIFTID (*status);*/
223 thread = *status >> 16;
224
225 /* Initial thread value can only be acquired via wait, so we have to
226 resort to this hack. */
227
228 if (TIDGET (inferior_pid) == 0)
229 {
230 inferior_pid = BUILDPID (inferior_pid, thread);
231 add_thread (inferior_pid);
232 }
233
234 pid = BUILDPID (pid, thread);
235
236 return pid;
237 }
238 }
239
240 /* Convert a Lynx process ID to a string. Returns the string in a static
241 buffer. */
242
243 char *
244 i386lynx_pid_to_str (pid)
245 int pid;
246 {
247 static char buf[40];
248
249 sprintf (buf, "process %d thread %d", PIDGET (pid), TIDGET (pid));
250
251 return buf;
252 }
253
254 /* Extract the register values out of the core file and store
255 them where `read_register' will find them.
256
257 CORE_REG_SECT points to the register values themselves, read into memory.
258 CORE_REG_SIZE is the size of that area.
259 WHICH says which set of registers we are handling (0 = int, 2 = float
260 on machines where they are discontiguous).
261 REG_ADDR is the offset from u.u_ar0 to the register values relative to
262 core_reg_sect. This is used with old-fashioned core files to
263 locate the registers in a large upage-plus-stack ".reg" section.
264 Original upage address X is at location core_reg_sect+x+reg_addr.
265 */
266
267 void
268 fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
269 char *core_reg_sect;
270 unsigned core_reg_size;
271 int which;
272 unsigned reg_addr;
273 {
274 struct st_entry s;
275 unsigned int regno, addr;
276
277 for (regno = 0; regno < NUM_REGS; regno++)
278 {
279 addr = register_addr (regno, (char *) &s.ec - (char *) &s);
280 supply_register (regno, core_reg_sect + addr);
281 }
282 }