Commit | Line | Data |
---|---|---|
da6d8c04 DJ |
1 | /* Low level interface to ptrace, for the remote server for GDB. |
2 | Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002 | |
3 | Free Software Foundation, Inc. | |
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., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | #include "server.h" | |
58caa3dc | 23 | #include "linux-low.h" |
da6d8c04 | 24 | |
58caa3dc | 25 | #include <sys/wait.h> |
da6d8c04 DJ |
26 | #include <stdio.h> |
27 | #include <sys/param.h> | |
28 | #include <sys/dir.h> | |
29 | #include <sys/ptrace.h> | |
30 | #include <sys/user.h> | |
31 | #include <signal.h> | |
32 | #include <sys/ioctl.h> | |
33 | #include <fcntl.h> | |
d07c63e7 | 34 | #include <string.h> |
0a30fbc4 DJ |
35 | #include <stdlib.h> |
36 | #include <unistd.h> | |
da6d8c04 | 37 | |
d844cde6 | 38 | #define PTRACE_ARG3_TYPE long |
c6ecbae5 | 39 | #define PTRACE_XFER_TYPE long |
da6d8c04 | 40 | |
58caa3dc DJ |
41 | #ifdef HAVE_LINUX_REGSETS |
42 | static int use_regsets_p = 1; | |
43 | #endif | |
44 | ||
da6d8c04 | 45 | extern int errno; |
c6ecbae5 DJ |
46 | |
47 | #ifdef HAVE_LINUX_USRREGS | |
0a30fbc4 DJ |
48 | extern int num_regs; |
49 | extern int regmap[]; | |
c6ecbae5 | 50 | #endif |
da6d8c04 DJ |
51 | |
52 | /* Start an inferior process and returns its pid. | |
53 | ALLARGS is a vector of program-name and args. */ | |
54 | ||
55 | int | |
56 | create_inferior (char *program, char **allargs) | |
57 | { | |
58 | int pid; | |
59 | ||
60 | pid = fork (); | |
61 | if (pid < 0) | |
62 | perror_with_name ("fork"); | |
63 | ||
64 | if (pid == 0) | |
65 | { | |
66 | ptrace (PTRACE_TRACEME, 0, 0, 0); | |
67 | ||
68 | execv (program, allargs); | |
69 | ||
70 | fprintf (stderr, "Cannot exec %s: %s.\n", program, | |
d07c63e7 | 71 | strerror (errno)); |
da6d8c04 DJ |
72 | fflush (stderr); |
73 | _exit (0177); | |
74 | } | |
75 | ||
76 | return pid; | |
77 | } | |
78 | ||
79 | /* Attach to an inferior process. */ | |
80 | ||
81 | int | |
82 | myattach (int pid) | |
83 | { | |
84 | if (ptrace (PTRACE_ATTACH, pid, 0, 0) != 0) | |
85 | { | |
86 | fprintf (stderr, "Cannot attach to process %d: %s (%d)\n", pid, | |
87 | errno < sys_nerr ? sys_errlist[errno] : "unknown error", | |
88 | errno); | |
89 | fflush (stderr); | |
90 | _exit (0177); | |
91 | } | |
92 | ||
93 | return 0; | |
94 | } | |
95 | ||
96 | /* Kill the inferior process. Make us have no inferior. */ | |
97 | ||
98 | void | |
99 | kill_inferior (void) | |
100 | { | |
101 | if (inferior_pid == 0) | |
102 | return; | |
103 | ptrace (PTRACE_KILL, inferior_pid, 0, 0); | |
104 | wait (0); | |
da6d8c04 DJ |
105 | } |
106 | ||
107 | /* Return nonzero if the given thread is still alive. */ | |
108 | int | |
109 | mythread_alive (int pid) | |
110 | { | |
111 | return 1; | |
112 | } | |
113 | ||
114 | /* Wait for process, returns status */ | |
115 | ||
116 | unsigned char | |
117 | mywait (char *status) | |
118 | { | |
119 | int pid; | |
e5f1222d | 120 | int w; |
da6d8c04 DJ |
121 | |
122 | enable_async_io (); | |
e5f1222d | 123 | pid = waitpid (inferior_pid, &w, 0); |
da6d8c04 DJ |
124 | disable_async_io (); |
125 | if (pid != inferior_pid) | |
126 | perror_with_name ("wait"); | |
127 | ||
128 | if (WIFEXITED (w)) | |
129 | { | |
130 | fprintf (stderr, "\nChild exited with retcode = %x \n", WEXITSTATUS (w)); | |
131 | *status = 'W'; | |
132 | return ((unsigned char) WEXITSTATUS (w)); | |
133 | } | |
134 | else if (!WIFSTOPPED (w)) | |
135 | { | |
136 | fprintf (stderr, "\nChild terminated with signal = %x \n", WTERMSIG (w)); | |
137 | *status = 'X'; | |
138 | return ((unsigned char) WTERMSIG (w)); | |
139 | } | |
140 | ||
141 | fetch_inferior_registers (0); | |
142 | ||
143 | *status = 'T'; | |
144 | return ((unsigned char) WSTOPSIG (w)); | |
145 | } | |
146 | ||
147 | /* Resume execution of the inferior process. | |
148 | If STEP is nonzero, single-step it. | |
149 | If SIGNAL is nonzero, give it that signal. */ | |
150 | ||
151 | void | |
152 | myresume (int step, int signal) | |
153 | { | |
154 | errno = 0; | |
155 | ptrace (step ? PTRACE_SINGLESTEP : PTRACE_CONT, inferior_pid, 1, signal); | |
156 | if (errno) | |
157 | perror_with_name ("ptrace"); | |
158 | } | |
159 | ||
c6ecbae5 DJ |
160 | |
161 | #ifdef HAVE_LINUX_USRREGS | |
162 | ||
0a30fbc4 | 163 | #define REGISTER_RAW_SIZE(regno) register_size((regno)) |
da6d8c04 DJ |
164 | |
165 | int | |
0a30fbc4 | 166 | register_addr (int regnum) |
da6d8c04 DJ |
167 | { |
168 | int addr; | |
169 | ||
0a30fbc4 | 170 | if (regnum < 0 || regnum >= num_regs) |
da6d8c04 DJ |
171 | error ("Invalid register number %d.", regnum); |
172 | ||
0a30fbc4 | 173 | addr = regmap[regnum]; |
da6d8c04 DJ |
174 | if (addr == -1) |
175 | addr = 0; | |
176 | ||
177 | return addr; | |
178 | } | |
179 | ||
58caa3dc | 180 | /* Fetch one register. */ |
da6d8c04 DJ |
181 | static void |
182 | fetch_register (int regno) | |
183 | { | |
184 | CORE_ADDR regaddr; | |
185 | register int i; | |
186 | ||
0a30fbc4 DJ |
187 | if (regno >= num_regs) |
188 | return; | |
189 | if (cannot_fetch_register (regno)) | |
190 | return; | |
da6d8c04 | 191 | |
0a30fbc4 DJ |
192 | regaddr = register_addr (regno); |
193 | if (regaddr == -1) | |
194 | return; | |
da6d8c04 DJ |
195 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) |
196 | { | |
197 | errno = 0; | |
0a30fbc4 | 198 | *(PTRACE_XFER_TYPE *) (register_data (regno) + i) = |
da6d8c04 DJ |
199 | ptrace (PTRACE_PEEKUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, 0); |
200 | regaddr += sizeof (PTRACE_XFER_TYPE); | |
201 | if (errno != 0) | |
202 | { | |
203 | /* Warning, not error, in case we are attached; sometimes the | |
204 | kernel doesn't let us at the registers. */ | |
205 | char *err = strerror (errno); | |
206 | char *msg = alloca (strlen (err) + 128); | |
207 | sprintf (msg, "reading register %d: %s", regno, err); | |
208 | error (msg); | |
209 | goto error_exit; | |
210 | } | |
211 | } | |
212 | error_exit:; | |
213 | } | |
214 | ||
215 | /* Fetch all registers, or just one, from the child process. */ | |
58caa3dc DJ |
216 | static void |
217 | usr_fetch_inferior_registers (int regno) | |
da6d8c04 DJ |
218 | { |
219 | if (regno == -1 || regno == 0) | |
0a30fbc4 | 220 | for (regno = 0; regno < num_regs; regno++) |
da6d8c04 DJ |
221 | fetch_register (regno); |
222 | else | |
223 | fetch_register (regno); | |
224 | } | |
225 | ||
226 | /* Store our register values back into the inferior. | |
227 | If REGNO is -1, do this for all registers. | |
228 | Otherwise, REGNO specifies which register (so we can save time). */ | |
58caa3dc DJ |
229 | static void |
230 | usr_store_inferior_registers (int regno) | |
da6d8c04 DJ |
231 | { |
232 | CORE_ADDR regaddr; | |
233 | int i; | |
da6d8c04 DJ |
234 | |
235 | if (regno >= 0) | |
236 | { | |
0a30fbc4 DJ |
237 | if (regno >= num_regs) |
238 | return; | |
239 | ||
240 | if (cannot_store_register (regno)) | |
241 | return; | |
242 | ||
243 | regaddr = register_addr (regno); | |
244 | if (regaddr == -1) | |
da6d8c04 | 245 | return; |
da6d8c04 | 246 | errno = 0; |
c6ecbae5 | 247 | for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (PTRACE_XFER_TYPE)) |
da6d8c04 | 248 | { |
0a30fbc4 DJ |
249 | errno = 0; |
250 | ptrace (PTRACE_POKEUSER, inferior_pid, (PTRACE_ARG3_TYPE) regaddr, | |
251 | *(int *) (register_data (regno) + i)); | |
da6d8c04 DJ |
252 | if (errno != 0) |
253 | { | |
0a30fbc4 DJ |
254 | /* Warning, not error, in case we are attached; sometimes the |
255 | kernel doesn't let us at the registers. */ | |
256 | char *err = strerror (errno); | |
257 | char *msg = alloca (strlen (err) + 128); | |
258 | sprintf (msg, "writing register %d: %s", | |
259 | regno, err); | |
260 | error (msg); | |
261 | return; | |
da6d8c04 | 262 | } |
0a30fbc4 | 263 | regaddr += sizeof (int); |
da6d8c04 | 264 | } |
da6d8c04 DJ |
265 | } |
266 | else | |
0a30fbc4 | 267 | for (regno = 0; regno < num_regs; regno++) |
da6d8c04 DJ |
268 | store_inferior_registers (regno); |
269 | } | |
58caa3dc DJ |
270 | #endif /* HAVE_LINUX_USRREGS */ |
271 | ||
272 | ||
273 | ||
274 | #ifdef HAVE_LINUX_REGSETS | |
275 | ||
276 | static int | |
277 | regsets_fetch_inferior_registers (void) | |
278 | { | |
279 | struct regset_info *regset; | |
280 | ||
281 | regset = target_regsets; | |
282 | ||
283 | while (regset->size >= 0) | |
284 | { | |
285 | void *buf; | |
286 | int res; | |
287 | ||
288 | if (regset->size == 0) | |
289 | { | |
290 | regset ++; | |
291 | continue; | |
292 | } | |
293 | ||
294 | buf = malloc (regset->size); | |
295 | res = ptrace (regset->get_request, inferior_pid, 0, (int) buf); | |
296 | if (res < 0) | |
297 | { | |
298 | if (errno == EIO) | |
299 | { | |
300 | /* If we get EIO on the first regset, do not try regsets again. | |
301 | If we get EIO on a later regset, disable that regset. */ | |
302 | if (regset == target_regsets) | |
303 | { | |
304 | use_regsets_p = 0; | |
305 | return -1; | |
306 | } | |
307 | else | |
308 | { | |
309 | regset->size = 0; | |
310 | continue; | |
311 | } | |
312 | } | |
313 | else | |
314 | { | |
315 | perror ("Warning: ptrace(regsets_fetch_inferior_registers)"); | |
316 | } | |
317 | } | |
318 | regset->store_function (buf); | |
319 | regset ++; | |
320 | } | |
321 | } | |
322 | ||
323 | static int | |
324 | regsets_store_inferior_registers (void) | |
325 | { | |
326 | struct regset_info *regset; | |
327 | ||
328 | regset = target_regsets; | |
329 | ||
330 | while (regset->size >= 0) | |
331 | { | |
332 | void *buf; | |
333 | int res; | |
334 | ||
335 | if (regset->size == 0) | |
336 | { | |
337 | regset ++; | |
338 | continue; | |
339 | } | |
340 | ||
341 | buf = malloc (regset->size); | |
342 | regset->fill_function (buf); | |
343 | res = ptrace (regset->set_request, inferior_pid, 0, (int) buf); | |
344 | if (res < 0) | |
345 | { | |
346 | if (errno == EIO) | |
347 | { | |
348 | /* If we get EIO on the first regset, do not try regsets again. | |
349 | If we get EIO on a later regset, disable that regset. */ | |
350 | if (regset == target_regsets) | |
351 | { | |
352 | use_regsets_p = 0; | |
353 | return -1; | |
354 | } | |
355 | else | |
356 | { | |
357 | regset->size = 0; | |
358 | continue; | |
359 | } | |
360 | } | |
361 | else | |
362 | { | |
363 | perror ("Warning: ptrace(regsets_fetch_inferior_registers)"); | |
364 | } | |
365 | } | |
366 | regset ++; | |
367 | } | |
368 | } | |
369 | ||
370 | #endif /* HAVE_LINUX_REGSETS */ | |
371 | ||
372 | ||
373 | void | |
374 | fetch_inferior_registers (int regno) | |
375 | { | |
376 | #ifdef HAVE_LINUX_REGSETS | |
377 | if (use_regsets_p) | |
378 | { | |
379 | if (regsets_fetch_inferior_registers () == 0) | |
380 | return; | |
381 | } | |
382 | #endif | |
383 | #ifdef HAVE_LINUX_USRREGS | |
384 | usr_fetch_inferior_registers (regno); | |
385 | #endif | |
386 | } | |
387 | ||
388 | void | |
389 | store_inferior_registers (int regno) | |
390 | { | |
391 | #ifdef HAVE_LINUX_REGSETS | |
392 | if (use_regsets_p) | |
393 | { | |
394 | if (regsets_store_inferior_registers () == 0) | |
395 | return; | |
396 | } | |
397 | #endif | |
398 | #ifdef HAVE_LINUX_USRREGS | |
399 | usr_store_inferior_registers (regno); | |
400 | #endif | |
401 | } | |
402 | ||
da6d8c04 | 403 | |
da6d8c04 DJ |
404 | /* Copy LEN bytes from inferior's memory starting at MEMADDR |
405 | to debugger memory starting at MYADDR. */ | |
406 | ||
407 | void | |
408 | read_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) | |
409 | { | |
410 | register int i; | |
411 | /* Round starting address down to longword boundary. */ | |
412 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
413 | /* Round ending address up; get number of longwords that makes. */ | |
414 | register int count | |
415 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) | |
416 | / sizeof (PTRACE_XFER_TYPE); | |
417 | /* Allocate buffer of that many longwords. */ | |
418 | register PTRACE_XFER_TYPE *buffer | |
419 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
420 | ||
421 | /* Read all the longwords */ | |
422 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
423 | { | |
d844cde6 | 424 | buffer[i] = ptrace (PTRACE_PEEKTEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, 0); |
da6d8c04 DJ |
425 | } |
426 | ||
427 | /* Copy appropriate bytes out of the buffer. */ | |
428 | memcpy (myaddr, (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), len); | |
429 | } | |
430 | ||
431 | /* Copy LEN bytes of data from debugger memory at MYADDR | |
432 | to inferior's memory at MEMADDR. | |
433 | On failure (cannot write the inferior) | |
434 | returns the value of errno. */ | |
435 | ||
436 | int | |
437 | write_inferior_memory (CORE_ADDR memaddr, char *myaddr, int len) | |
438 | { | |
439 | register int i; | |
440 | /* Round starting address down to longword boundary. */ | |
441 | register CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (PTRACE_XFER_TYPE); | |
442 | /* Round ending address up; get number of longwords that makes. */ | |
443 | register int count | |
444 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) / sizeof (PTRACE_XFER_TYPE); | |
445 | /* Allocate buffer of that many longwords. */ | |
446 | register PTRACE_XFER_TYPE *buffer = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
447 | extern int errno; | |
448 | ||
449 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
450 | ||
d844cde6 DJ |
451 | buffer[0] = ptrace (PTRACE_PEEKTEXT, inferior_pid, |
452 | (PTRACE_ARG3_TYPE) addr, 0); | |
da6d8c04 DJ |
453 | |
454 | if (count > 1) | |
455 | { | |
456 | buffer[count - 1] | |
457 | = ptrace (PTRACE_PEEKTEXT, inferior_pid, | |
d844cde6 DJ |
458 | (PTRACE_ARG3_TYPE) (addr + (count - 1) |
459 | * sizeof (PTRACE_XFER_TYPE)), | |
460 | 0); | |
da6d8c04 DJ |
461 | } |
462 | ||
463 | /* Copy data to be written over corresponding part of buffer */ | |
464 | ||
465 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), myaddr, len); | |
466 | ||
467 | /* Write the entire buffer. */ | |
468 | ||
469 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
470 | { | |
471 | errno = 0; | |
d844cde6 | 472 | ptrace (PTRACE_POKETEXT, inferior_pid, (PTRACE_ARG3_TYPE) addr, buffer[i]); |
da6d8c04 DJ |
473 | if (errno) |
474 | return errno; | |
475 | } | |
476 | ||
477 | return 0; | |
478 | } | |
479 | \f | |
480 | void | |
481 | initialize_low (void) | |
482 | { | |
0a30fbc4 | 483 | init_registers (); |
da6d8c04 | 484 | } |