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