Commit | Line | Data |
---|---|---|
53e95fcf JS |
1 | /* Native-dependent code for Linux/x86-64. |
2 | Copyright 2001 | |
3 | Free Software Foundation, Inc. | |
4 | Contributed by Jiri Smid, SuSE Labs. | |
5 | ||
6 | This file is part of GDB. | |
7 | ||
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 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
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. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
20 | Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | #include "defs.h" | |
24 | #include "inferior.h" | |
25 | #include "gdbcore.h" | |
26 | #include "regcache.h" | |
27 | #include "i387-nat.h" | |
28 | #include "gdb_assert.h" | |
29 | #include "x86-64-tdep.h" | |
30 | ||
31 | #include <sys/ptrace.h> | |
32 | #include <sys/debugreg.h> | |
33 | #include <sys/syscall.h> | |
34 | #include <sys/procfs.h> | |
35 | ||
36 | static unsigned long | |
37 | x86_64_linux_dr_get (int regnum) | |
38 | { | |
39 | int tid; | |
40 | unsigned long value; | |
41 | ||
42 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with | |
43 | multi-threaded processes here. For now, pretend there is just | |
44 | one thread. */ | |
45 | tid = PIDGET (inferior_ptid); | |
46 | ||
47 | /* FIXME: kettenis/2001-03-27: Calling perror_with_name if the | |
48 | ptrace call fails breaks debugging remote targets. The correct | |
49 | way to fix this is to add the hardware breakpoint and watchpoint | |
50 | stuff to the target vectore. For now, just return zero if the | |
51 | ptrace call fails. */ | |
52 | errno = 0; | |
53 | value = ptrace (PT_READ_U, tid, | |
54 | offsetof (struct user, u_debugreg[regnum]), 0); | |
55 | if (errno != 0) | |
56 | #if 0 | |
57 | perror_with_name ("Couldn't read debug register"); | |
58 | #else | |
59 | return 0; | |
60 | #endif | |
61 | ||
62 | return value; | |
63 | } | |
64 | ||
65 | static void | |
66 | x86_64_linux_dr_set (int regnum, unsigned long value) | |
67 | { | |
68 | int tid; | |
69 | ||
70 | /* FIXME: kettenis/2001-01-29: It's not clear what we should do with | |
71 | multi-threaded processes here. For now, pretend there is just | |
72 | one thread. */ | |
73 | tid = PIDGET (inferior_ptid); | |
74 | ||
75 | errno = 0; | |
76 | ptrace (PT_WRITE_U, tid, | |
77 | offsetof (struct user, u_debugreg[regnum]), value); | |
78 | if (errno != 0) | |
79 | perror_with_name ("Couldn't write debug register"); | |
80 | } | |
81 | ||
82 | void | |
83 | x86_64_linux_dr_set_control (unsigned long control) | |
84 | { | |
85 | x86_64_linux_dr_set (DR_CONTROL, control); | |
86 | } | |
87 | ||
88 | void | |
89 | x86_64_linux_dr_set_addr (int regnum, CORE_ADDR addr) | |
90 | { | |
91 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); | |
92 | ||
93 | x86_64_linux_dr_set (DR_FIRSTADDR + regnum, addr); | |
94 | } | |
95 | ||
96 | void | |
97 | x86_64_linux_dr_reset_addr (int regnum) | |
98 | { | |
99 | gdb_assert (regnum >= 0 && regnum <= DR_LASTADDR - DR_FIRSTADDR); | |
100 | ||
101 | x86_64_linux_dr_set (DR_FIRSTADDR + regnum, 0L); | |
102 | } | |
103 | ||
104 | unsigned long | |
105 | x86_64_linux_dr_get_status (void) | |
106 | { | |
107 | return x86_64_linux_dr_get (DR_STATUS); | |
108 | } | |
109 | \f | |
110 | ||
111 | /* The register sets used in Linux ELF core-dumps are identical to the | |
112 | register sets used by `ptrace'. */ | |
113 | ||
114 | #define GETREGS_SUPPLIES(regno) \ | |
115 | (0 <= (regno) && (regno) <= 17) | |
116 | #define GETFPREGS_SUPPLIES(regno) \ | |
117 | (FP0_REGNUM <= (regno) && (regno) <= MXCSR_REGNUM) | |
118 | ||
119 | #define PTRACE_XFER_TYPE unsigned long | |
120 | \f | |
121 | ||
122 | /* Transfering the general-purpose registers between GDB, inferiors | |
123 | and core files. */ | |
124 | ||
125 | /* Fill GDB's register array with the general-purpose register values | |
126 | in *GREGSETP. */ | |
127 | ||
128 | void | |
129 | supply_gregset (elf_gregset_t * gregsetp) | |
130 | { | |
131 | elf_greg_t *regp = (elf_greg_t *) gregsetp; | |
132 | int i; | |
133 | ||
134 | for (i = 0; i < X86_64_NUM_GREGS; i++) | |
135 | supply_register (i, (char *) (regp + x86_64_regmap[i])); | |
136 | } | |
137 | ||
138 | /* Fill register REGNO (if it is a general-purpose register) in | |
139 | *GREGSETPS with the value in GDB's register array. If REGNO is -1, | |
140 | do this for all registers. */ | |
141 | ||
142 | void | |
143 | fill_gregset (elf_gregset_t * gregsetp, int regno) | |
144 | { | |
145 | elf_greg_t *regp = (elf_greg_t *) gregsetp; | |
146 | int i; | |
147 | ||
148 | for (i = 0; i < X86_64_NUM_GREGS; i++) | |
149 | if ((regno == -1 || regno == i)) | |
150 | *(regp + x86_64_regmap[i]) = | |
151 | *(elf_greg_t *) & registers[REGISTER_BYTE (i)]; | |
152 | } | |
153 | ||
154 | /* Fetch all general-purpose registers from process/thread TID and | |
155 | store their values in GDB's register array. */ | |
156 | ||
157 | static void | |
158 | fetch_regs (int tid) | |
159 | { | |
160 | elf_gregset_t regs; | |
161 | ||
162 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
163 | perror_with_name ("Couldn't get registers"); | |
164 | ||
165 | supply_gregset (®s); | |
166 | } | |
167 | ||
168 | /* Store all valid general-purpose registers in GDB's register array | |
169 | into the process/thread specified by TID. */ | |
170 | ||
171 | static void | |
172 | store_regs (int tid, int regno) | |
173 | { | |
174 | elf_gregset_t regs; | |
175 | ||
176 | if (ptrace (PTRACE_GETREGS, tid, 0, (long) ®s) < 0) | |
177 | perror_with_name ("Couldn't get registers"); | |
178 | ||
179 | fill_gregset (®s, regno); | |
180 | ||
181 | if (ptrace (PTRACE_SETREGS, tid, 0, (long) ®s) < 0) | |
182 | perror_with_name ("Couldn't write registers"); | |
183 | } | |
184 | \f | |
185 | ||
186 | /* Transfering floating-point registers between GDB, inferiors and cores. */ | |
187 | ||
188 | /* Fill GDB's register array with the floating-point register values in | |
189 | *FPREGSETP. */ | |
190 | ||
191 | void | |
192 | supply_fpregset (elf_fpregset_t * fpregsetp) | |
193 | { | |
194 | i387_supply_fxsave ((char *) fpregsetp); | |
195 | } | |
196 | ||
197 | /* Fill register REGNO (if it is a floating-point register) in | |
198 | *FPREGSETP with the value in GDB's register array. If REGNO is -1, | |
199 | do this for all registers. */ | |
200 | ||
201 | void | |
202 | fill_fpregset (elf_fpregset_t * fpregsetp, int regno) | |
203 | { | |
204 | i387_fill_fxsave ((char *) fpregsetp, regno); | |
205 | } | |
206 | ||
207 | /* Fetch all floating-point registers from process/thread TID and store | |
208 | thier values in GDB's register array. */ | |
209 | ||
210 | static void | |
211 | fetch_fpregs (int tid) | |
212 | { | |
213 | elf_fpregset_t fpregs; | |
214 | ||
215 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
216 | perror_with_name ("Couldn't get floating point status"); | |
217 | ||
218 | supply_fpregset (&fpregs); | |
219 | } | |
220 | ||
221 | /* Store all valid floating-point registers in GDB's register array | |
222 | into the process/thread specified by TID. */ | |
223 | ||
224 | static void | |
225 | store_fpregs (int tid, int regno) | |
226 | { | |
227 | elf_fpregset_t fpregs; | |
228 | ||
229 | if (ptrace (PTRACE_GETFPREGS, tid, 0, (long) &fpregs) < 0) | |
230 | perror_with_name ("Couldn't get floating point status"); | |
231 | ||
232 | fill_fpregset (&fpregs, regno); | |
233 | ||
234 | if (ptrace (PTRACE_SETFPREGS, tid, 0, (long) &fpregs) < 0) | |
235 | perror_with_name ("Couldn't write floating point status"); | |
236 | } | |
237 | \f | |
238 | ||
239 | /* Transferring arbitrary registers between GDB and inferior. */ | |
240 | ||
241 | /* Fetch register REGNO from the child process. If REGNO is -1, do | |
242 | this for all registers (including the floating point and SSE | |
243 | registers). */ | |
244 | ||
245 | void | |
246 | fetch_inferior_registers (int regno) | |
247 | { | |
248 | int tid; | |
249 | ||
250 | /* Linux LWP ID's are process ID's. */ | |
251 | if ((tid = TIDGET (inferior_ptid)) == 0) | |
252 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
253 | ||
254 | if (regno == -1) | |
255 | { | |
256 | fetch_regs (tid); | |
257 | fetch_fpregs (tid); | |
258 | return; | |
259 | } | |
260 | ||
261 | if (GETREGS_SUPPLIES (regno)) | |
262 | { | |
263 | fetch_regs (tid); | |
264 | return; | |
265 | } | |
266 | ||
267 | if (GETFPREGS_SUPPLIES (regno)) | |
268 | { | |
269 | fetch_fpregs (tid); | |
270 | return; | |
271 | } | |
272 | ||
273 | internal_error (__FILE__, __LINE__, | |
274 | "Got request for bad register number %d.", regno); | |
275 | } | |
276 | ||
277 | /* Store register REGNO back into the child process. If REGNO is -1, | |
278 | do this for all registers (including the floating point and SSE | |
279 | registers). */ | |
280 | void | |
281 | store_inferior_registers (int regno) | |
282 | { | |
283 | int tid; | |
284 | ||
285 | /* Linux LWP ID's are process ID's. */ | |
286 | if ((tid = TIDGET (inferior_ptid)) == 0) | |
287 | tid = PIDGET (inferior_ptid); /* Not a threaded program. */ | |
288 | ||
289 | if (regno == -1) | |
290 | { | |
291 | store_regs (tid, regno); | |
292 | store_fpregs (tid, regno); | |
293 | return; | |
294 | } | |
295 | ||
296 | if (GETREGS_SUPPLIES (regno)) | |
297 | { | |
298 | store_regs (tid, regno); | |
299 | return; | |
300 | } | |
301 | ||
302 | if (GETFPREGS_SUPPLIES (regno)) | |
303 | { | |
304 | store_fpregs (tid, regno); | |
305 | return; | |
306 | } | |
307 | ||
308 | internal_error (__FILE__, __LINE__, | |
309 | "Got request to store bad register number %d.", regno); | |
310 | } | |
311 | \f | |
312 | ||
313 | static const unsigned char linux_syscall[] = { 0x0f, 0x05 }; | |
314 | ||
315 | #define LINUX_SYSCALL_LEN (sizeof linux_syscall) | |
316 | ||
317 | /* The system call number is stored in the %rax register. */ | |
318 | #define LINUX_SYSCALL_REGNUM 0 /* %rax */ | |
319 | ||
320 | /* We are specifically interested in the sigreturn and rt_sigreturn | |
321 | system calls. */ | |
322 | ||
323 | #ifndef SYS_sigreturn | |
324 | #define SYS_sigreturn __NR_sigreturn | |
325 | #endif | |
326 | #ifndef SYS_rt_sigreturn | |
327 | #define SYS_rt_sigreturn __NR_rt_sigreturn | |
328 | #endif | |
329 | ||
330 | /* Offset to saved processor flags, from <asm/sigcontext.h>. */ | |
331 | #define LINUX_SIGCONTEXT_EFLAGS_OFFSET (152) | |
332 | /* Offset to saved processor registers from <asm/ucontext.h> */ | |
333 | #define LINUX_UCONTEXT_SIGCONTEXT_OFFSET (36) | |
334 | ||
335 | /* Resume execution of the inferior process. | |
336 | If STEP is nonzero, single-step it. | |
337 | If SIGNAL is nonzero, give it that signal. */ | |
338 | ||
339 | void | |
340 | child_resume (ptid_t ptid, int step, enum target_signal signal) | |
341 | { | |
342 | int pid = PIDGET (ptid); | |
343 | int request = PTRACE_CONT; | |
344 | ||
345 | if (pid == -1) | |
346 | /* Resume all threads. */ | |
347 | /* I think this only gets used in the non-threaded case, where "resume | |
348 | all threads" and "resume inferior_ptid" are the same. */ | |
349 | pid = PIDGET (inferior_ptid); | |
350 | ||
351 | if (step) | |
352 | { | |
353 | CORE_ADDR pc = read_pc_pid (pid_to_ptid (pid)); | |
354 | unsigned char buf[LINUX_SYSCALL_LEN]; | |
355 | ||
356 | request = PTRACE_SINGLESTEP; | |
357 | ||
358 | /* Returning from a signal trampoline is done by calling a | |
359 | special system call (sigreturn or rt_sigreturn, see | |
360 | i386-linux-tdep.c for more information). This system call | |
361 | restores the registers that were saved when the signal was | |
362 | raised, including %eflags. That means that single-stepping | |
363 | won't work. Instead, we'll have to modify the signal context | |
364 | that's about to be restored, and set the trace flag there. */ | |
365 | ||
366 | /* First check if PC is at a system call. */ | |
367 | if (read_memory_nobpt (pc, (char *) buf, LINUX_SYSCALL_LEN) == 0 | |
368 | && memcmp (buf, linux_syscall, LINUX_SYSCALL_LEN) == 0) | |
369 | { | |
370 | int syscall = | |
371 | read_register_pid (LINUX_SYSCALL_REGNUM, pid_to_ptid (pid)); | |
372 | ||
373 | /* Then check the system call number. */ | |
374 | if (syscall == SYS_rt_sigreturn) | |
375 | { | |
376 | CORE_ADDR sp = read_register (SP_REGNUM); | |
377 | CORE_ADDR addr = sp; | |
378 | unsigned long int eflags; | |
379 | ||
380 | addr += | |
381 | sizeof (struct siginfo) + LINUX_UCONTEXT_SIGCONTEXT_OFFSET; | |
382 | ||
383 | /* Set the trace flag in the context that's about to be | |
384 | restored. */ | |
385 | addr += LINUX_SIGCONTEXT_EFLAGS_OFFSET; | |
386 | read_memory (addr, (char *) &eflags, 8); | |
387 | eflags |= 0x0100; | |
388 | write_memory (addr, (char *) &eflags, 8); | |
389 | } | |
390 | } | |
391 | } | |
392 | ||
393 | if (ptrace (request, pid, 0, target_signal_to_host (signal)) == -1) | |
394 | perror_with_name ("ptrace"); | |
395 | } | |
396 | \f | |
397 | ||
398 | /* Copy LEN bytes to or from inferior's memory starting at MEMADDR | |
399 | to debugger memory starting at MYADDR. Copy to inferior if | |
400 | WRITE is nonzero. TARGET is ignored. | |
401 | ||
402 | Returns the length copied, which is either the LEN argument or zero. | |
403 | This xfer function does not do partial moves, since child_ops | |
404 | doesn't allow memory operations to cross below us in the target stack | |
405 | anyway. */ | |
406 | ||
407 | int | |
408 | child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write, | |
409 | struct mem_attrib *attrib ATTRIBUTE_UNUSED, | |
410 | struct target_ops *target) | |
411 | { | |
412 | register int i; | |
413 | /* Round starting address down to longword boundary. */ | |
414 | register CORE_ADDR addr = memaddr & -sizeof (PTRACE_XFER_TYPE); | |
415 | /* Round ending address up; get number of longwords that makes. */ | |
416 | register int count | |
417 | = (((memaddr + len) - addr) + sizeof (PTRACE_XFER_TYPE) - 1) | |
418 | / sizeof (PTRACE_XFER_TYPE); | |
419 | /* Allocate buffer of that many longwords. */ | |
420 | register PTRACE_XFER_TYPE *buffer | |
421 | = (PTRACE_XFER_TYPE *) alloca (count * sizeof (PTRACE_XFER_TYPE)); | |
422 | ||
423 | if (write) | |
424 | { | |
425 | /* Fill start and end extra bytes of buffer with existing memory data. */ | |
426 | if (addr != memaddr || len < (int) sizeof (PTRACE_XFER_TYPE)) | |
427 | { | |
428 | /* Need part of initial word -- fetch it. */ | |
429 | ptrace (PT_READ_I, PIDGET (inferior_ptid), | |
430 | (PTRACE_ARG3_TYPE) addr, buffer); | |
431 | } | |
432 | ||
433 | if (count > 1) /* FIXME, avoid if even boundary */ | |
434 | { | |
435 | ptrace (PT_READ_I, PIDGET (inferior_ptid), | |
436 | ((PTRACE_ARG3_TYPE) | |
437 | (addr + (count - 1) * sizeof (PTRACE_XFER_TYPE))), | |
438 | buffer + count - 1); | |
439 | } | |
440 | ||
441 | /* Copy data to be written over corresponding part of buffer */ | |
442 | ||
443 | memcpy ((char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), | |
444 | myaddr, len); | |
445 | ||
446 | /* Write the entire buffer. */ | |
447 | ||
448 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
449 | { | |
450 | errno = 0; | |
451 | ptrace (PT_WRITE_D, PIDGET (inferior_ptid), | |
452 | (PTRACE_ARG3_TYPE) addr, buffer[i]); | |
453 | if (errno) | |
454 | { | |
455 | /* Using the appropriate one (I or D) is necessary for | |
456 | Gould NP1, at least. */ | |
457 | errno = 0; | |
458 | ptrace (PT_WRITE_I, PIDGET (inferior_ptid), | |
459 | (PTRACE_ARG3_TYPE) addr, buffer[i]); | |
460 | } | |
461 | if (errno) | |
462 | return 0; | |
463 | } | |
464 | #ifdef CLEAR_INSN_CACHE | |
465 | CLEAR_INSN_CACHE (); | |
466 | #endif | |
467 | } | |
468 | else | |
469 | { | |
470 | /* Read all the longwords */ | |
471 | for (i = 0; i < count; i++, addr += sizeof (PTRACE_XFER_TYPE)) | |
472 | { | |
473 | errno = 0; | |
474 | ptrace (PT_READ_I, PIDGET (inferior_ptid), | |
475 | (PTRACE_ARG3_TYPE) addr, buffer + i); | |
476 | if (errno) | |
477 | return 0; | |
478 | } | |
479 | ||
480 | /* Copy appropriate bytes out of the buffer. */ | |
481 | memcpy (myaddr, | |
482 | (char *) buffer + (memaddr & (sizeof (PTRACE_XFER_TYPE) - 1)), | |
483 | len); | |
484 | } | |
485 | return len; | |
486 | } | |
487 | ||
488 | /* Interpreting register set info found in core files. */ | |
489 | ||
490 | /* Provide registers to GDB from a core file. | |
491 | ||
492 | CORE_REG_SECT points to an array of bytes, which are the contents | |
493 | of a `note' from a core file which BFD thinks might contain | |
494 | register contents. CORE_REG_SIZE is its size. | |
495 | ||
496 | WHICH says which register set corelow suspects this is: | |
497 | 0 --- the general-purpose register set, in elf_gregset_t format | |
498 | 2 --- the floating-point register set, in elf_fpregset_t format | |
499 | ||
500 | REG_ADDR isn't used on Linux. */ | |
501 | ||
502 | static void | |
503 | fetch_core_registers (char *core_reg_sect, unsigned core_reg_size, | |
504 | int which, CORE_ADDR reg_addr) | |
505 | { | |
506 | elf_gregset_t gregset; | |
507 | elf_fpregset_t fpregset; | |
508 | switch (which) | |
509 | { | |
510 | case 0: | |
511 | if (core_reg_size != sizeof (gregset)) | |
512 | warning ("Wrong size gregset in core file."); | |
513 | else | |
514 | { | |
515 | memcpy (&gregset, core_reg_sect, sizeof (gregset)); | |
516 | supply_gregset (&gregset); | |
517 | } | |
518 | break; | |
519 | ||
520 | case 2: | |
521 | if (core_reg_size != sizeof (fpregset)) | |
522 | warning ("Wrong size fpregset in core file."); | |
523 | else | |
524 | { | |
525 | memcpy (&fpregset, core_reg_sect, sizeof (fpregset)); | |
526 | supply_fpregset (&fpregset); | |
527 | } | |
528 | break; | |
529 | ||
530 | default: | |
531 | /* We've covered all the kinds of registers we know about here, | |
532 | so this must be something we wouldn't know what to do with | |
533 | anyway. Just ignore it. */ | |
534 | break; | |
535 | } | |
536 | } | |
537 | ||
538 | /* Register that we are able to handle Linux ELF core file formats. */ | |
539 | ||
540 | static struct core_fns linux_elf_core_fns = { | |
541 | bfd_target_elf_flavour, /* core_flavour */ | |
542 | default_check_format, /* check_format */ | |
543 | default_core_sniffer, /* core_sniffer */ | |
544 | fetch_core_registers, /* core_read_registers */ | |
545 | NULL /* next */ | |
546 | }; | |
547 | \f | |
548 | ||
549 | #if !defined (offsetof) | |
550 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
551 | #endif | |
552 | ||
553 | /* Record the value of the debug control register. */ | |
554 | static long debug_control_mirror; | |
555 | ||
556 | /* Record which address associates with which register. */ | |
557 | static CORE_ADDR address_lookup[DR_LASTADDR - DR_FIRSTADDR + 1]; | |
558 | ||
559 | void | |
560 | _initialize_x86_64_linux_nat (void) | |
561 | { | |
562 | add_core_fns (&linux_elf_core_fns); | |
563 | } |