* gdb.java/jmain.exp: Handle demangled names with and without method
[deliverable/binutils-gdb.git] / gdb / m68klinux-nat.c
CommitLineData
a4b6fc86
AC
1/* Motorola m68k native support for GNU/Linux.
2
6aba47ca 3 Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
10d6c8cd 4 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
26#include "language.h"
27#include "gdbcore.h"
32eeb91a 28#include "gdb_string.h"
4e052eda 29#include "regcache.h"
10d6c8cd
DJ
30#include "target.h"
31#include "linux-nat.h"
c906108c 32
32eeb91a
AS
33#include "m68k-tdep.h"
34
c906108c
SS
35#include <sys/param.h>
36#include <sys/dir.h>
37#include <signal.h>
0280a90a 38#include <sys/ptrace.h>
c906108c
SS
39#include <sys/user.h>
40#include <sys/ioctl.h>
41#include <fcntl.h>
42#include <sys/procfs.h>
43
0280a90a
AS
44#ifdef HAVE_SYS_REG_H
45#include <sys/reg.h>
46#endif
47
c906108c
SS
48#include <sys/file.h>
49#include "gdb_stat.h"
50
51#include "floatformat.h"
52
53#include "target.h"
c906108c 54\f
77949794 55/* This table must line up with REGISTER_NAME in "m68k-tdep.c". */
c5aa993b 56static const int regmap[] =
c906108c
SS
57{
58 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
59 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
60 PT_SR, PT_PC,
61 /* PT_FP0, ..., PT_FP7 */
62 21, 24, 27, 30, 33, 36, 39, 42,
63 /* PT_FPCR, PT_FPSR, PT_FPIAR */
64 45, 46, 47
65};
66
0280a90a
AS
67/* Which ptrace request retrieves which registers?
68 These apply to the corresponding SET requests as well. */
69#define NUM_GREGS (18)
70#define MAX_NUM_REGS (NUM_GREGS + 11)
71
72int
73getregs_supplies (int regno)
74{
75 return 0 <= regno && regno < NUM_GREGS;
76}
77
78int
79getfpregs_supplies (int regno)
80{
32eeb91a 81 return FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
0280a90a
AS
82}
83
84/* Does the current host support the GETREGS request? */
85int have_ptrace_getregs =
86#ifdef HAVE_PTRACE_GETREGS
87 1
88#else
89 0
90#endif
91;
92
93\f
94
0280a90a
AS
95/* Fetching registers directly from the U area, one at a time. */
96
97/* FIXME: This duplicates code from `inptrace.c'. The problem is that we
98 define FETCH_INFERIOR_REGISTERS since we want to use our own versions
99 of {fetch,store}_inferior_registers that use the GETREGS request. This
100 means that the code in `infptrace.c' is #ifdef'd out. But we need to
101 fall back on that code when GDB is running on top of a kernel that
102 doesn't support the GETREGS request. */
103
104#ifndef PT_READ_U
105#define PT_READ_U PTRACE_PEEKUSR
106#endif
107#ifndef PT_WRITE_U
108#define PT_WRITE_U PTRACE_POKEUSR
109#endif
110
0280a90a
AS
111/* Fetch one register. */
112
113static void
114fetch_register (int regno)
115{
116 /* This isn't really an address. But ptrace thinks of it as one. */
117 CORE_ADDR regaddr;
118 char mess[128]; /* For messages */
52f0bd74 119 int i;
123a958e 120 char buf[MAX_REGISTER_SIZE];
0280a90a
AS
121 int tid;
122
123 if (CANNOT_FETCH_REGISTER (regno))
124 {
8de307e0 125 memset (buf, '\0', register_size (current_gdbarch, regno)); /* Supply zeroes */
23a6d369 126 regcache_raw_supply (current_regcache, regno, buf);
0280a90a
AS
127 return;
128 }
129
130 /* Overload thread id onto process id */
8de307e0
AS
131 tid = TIDGET (inferior_ptid);
132 if (tid == 0)
0280a90a
AS
133 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
134
de732108 135 regaddr = 4 * regmap[regno];
8de307e0 136 for (i = 0; i < register_size (current_gdbarch, regno);
5f402660 137 i += sizeof (PTRACE_TYPE_RET))
0280a90a
AS
138 {
139 errno = 0;
5f402660
UW
140 *(PTRACE_TYPE_RET *) &buf[i] = ptrace (PT_READ_U, tid,
141 (PTRACE_TYPE_ARG3) regaddr, 0);
142 regaddr += sizeof (PTRACE_TYPE_RET);
0280a90a
AS
143 if (errno != 0)
144 {
145 sprintf (mess, "reading register %s (#%d)",
146 REGISTER_NAME (regno), regno);
147 perror_with_name (mess);
148 }
149 }
23a6d369 150 regcache_raw_supply (current_regcache, regno, buf);
0280a90a
AS
151}
152
153/* Fetch register values from the inferior.
154 If REGNO is negative, do this for all registers.
155 Otherwise, REGNO specifies which register (so we can save time). */
c906108c 156
10d6c8cd 157static void
0280a90a
AS
158old_fetch_inferior_registers (int regno)
159{
160 if (regno >= 0)
161 {
162 fetch_register (regno);
163 }
164 else
165 {
166 for (regno = 0; regno < NUM_REGS; regno++)
167 {
168 fetch_register (regno);
169 }
170 }
171}
172
173/* Store one register. */
174
175static void
176store_register (int regno)
177{
178 /* This isn't really an address. But ptrace thinks of it as one. */
179 CORE_ADDR regaddr;
180 char mess[128]; /* For messages */
52f0bd74 181 int i;
0280a90a 182 int tid;
d9d9c31f 183 char buf[MAX_REGISTER_SIZE];
0280a90a
AS
184
185 if (CANNOT_STORE_REGISTER (regno))
186 {
187 return;
188 }
189
190 /* Overload thread id onto process id */
8de307e0
AS
191 tid = TIDGET (inferior_ptid);
192 if (tid == 0)
0280a90a
AS
193 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
194
de732108 195 regaddr = 4 * regmap[regno];
9852326a
AS
196
197 /* Put the contents of regno into a local buffer */
822c9732 198 regcache_raw_collect (current_regcache, regno, buf);
9852326a
AS
199
200 /* Store the local buffer into the inferior a chunk at the time. */
8de307e0 201 for (i = 0; i < register_size (current_gdbarch, regno);
5f402660 202 i += sizeof (PTRACE_TYPE_RET))
0280a90a
AS
203 {
204 errno = 0;
5f402660
UW
205 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) regaddr,
206 *(PTRACE_TYPE_RET *) (buf + i));
207 regaddr += sizeof (PTRACE_TYPE_RET);
0280a90a
AS
208 if (errno != 0)
209 {
210 sprintf (mess, "writing register %s (#%d)",
211 REGISTER_NAME (regno), regno);
212 perror_with_name (mess);
213 }
214 }
215}
216
217/* Store our register values back into the inferior.
218 If REGNO is negative, do this for all registers.
219 Otherwise, REGNO specifies which register (so we can save time). */
220
10d6c8cd 221static void
0280a90a
AS
222old_store_inferior_registers (int regno)
223{
224 if (regno >= 0)
225 {
226 store_register (regno);
227 }
228 else
229 {
230 for (regno = 0; regno < NUM_REGS; regno++)
231 {
232 store_register (regno);
233 }
234 }
235}
236\f
f175af98
DJ
237/* Given a pointer to a general register set in /proc format
238 (elf_gregset_t *), unpack the register contents and supply
239 them as gdb's idea of the current register values. */
c906108c
SS
240
241
242/* Note both m68k-tdep.c and m68klinux-nat.c contain definitions
243 for supply_gregset and supply_fpregset. The definitions
244 in m68k-tdep.c are valid if USE_PROC_FS is defined. Otherwise,
245 the definitions in m68klinux-nat.c will be used. This is a
246 bit of a hack. The supply_* routines do not belong in
247 *_tdep.c files. But, there are several lynx ports that currently
c5aa993b 248 depend on these definitions. */
c906108c
SS
249
250#ifndef USE_PROC_FS
251
c60c0f5f
MS
252/* Prototypes for supply_gregset etc. */
253#include "gregset.h"
254
c906108c 255void
f175af98 256supply_gregset (elf_gregset_t *gregsetp)
c906108c 257{
0280a90a 258 elf_greg_t *regp = (elf_greg_t *) gregsetp;
c906108c
SS
259 int regi;
260
32eeb91a 261 for (regi = M68K_D0_REGNUM; regi <= SP_REGNUM; regi++)
23a6d369
AC
262 regcache_raw_supply (current_regcache, regi, (char *) &regp[regmap[regi]]);
263 regcache_raw_supply (current_regcache, PS_REGNUM, (char *) &regp[PT_SR]);
264 regcache_raw_supply (current_regcache, PC_REGNUM, (char *) &regp[PT_PC]);
0280a90a
AS
265}
266
267/* Fill register REGNO (if it is a general-purpose register) in
268 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
269 do this for all registers. */
270void
271fill_gregset (elf_gregset_t *gregsetp, int regno)
272{
273 elf_greg_t *regp = (elf_greg_t *) gregsetp;
274 int i;
275
276 for (i = 0; i < NUM_GREGS; i++)
8de307e0 277 if (regno == -1 || regno == i)
822c9732 278 regcache_raw_collect (current_regcache, i, regp + regmap[i]);
0280a90a
AS
279}
280
281#ifdef HAVE_PTRACE_GETREGS
282
283/* Fetch all general-purpose registers from process/thread TID and
284 store their values in GDB's register array. */
285
286static void
287fetch_regs (int tid)
288{
289 elf_gregset_t regs;
290
291 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
292 {
293 if (errno == EIO)
294 {
295 /* The kernel we're running on doesn't support the GETREGS
296 request. Reset `have_ptrace_getregs'. */
297 have_ptrace_getregs = 0;
298 return;
299 }
300
e2e0b3e5 301 perror_with_name (_("Couldn't get registers"));
0280a90a
AS
302 }
303
304 supply_gregset (&regs);
c906108c
SS
305}
306
0280a90a
AS
307/* Store all valid general-purpose registers in GDB's register array
308 into the process/thread specified by TID. */
309
310static void
311store_regs (int tid, int regno)
312{
313 elf_gregset_t regs;
314
315 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 316 perror_with_name (_("Couldn't get registers"));
0280a90a
AS
317
318 fill_gregset (&regs, regno);
319
320 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
e2e0b3e5 321 perror_with_name (_("Couldn't write registers"));
0280a90a
AS
322}
323
324#else
325
326static void fetch_regs (int tid) {}
327static void store_regs (int tid, int regno) {}
328
329#endif
330
331\f
332/* Transfering floating-point registers between GDB, inferiors and cores. */
333
334/* What is the address of fpN within the floating-point register set F? */
335#define FPREG_ADDR(f, n) ((char *) &(f)->fpregs[(n) * 3])
336
337/* Fill GDB's register array with the floating-point register values in
338 *FPREGSETP. */
c906108c 339
c5aa993b 340void
f175af98 341supply_fpregset (elf_fpregset_t *fpregsetp)
c906108c
SS
342{
343 int regi;
344
32eeb91a 345 for (regi = FP0_REGNUM; regi < FP0_REGNUM + 8; regi++)
23a6d369
AC
346 regcache_raw_supply (current_regcache, regi,
347 FPREG_ADDR (fpregsetp, regi - FP0_REGNUM));
348 regcache_raw_supply (current_regcache, M68K_FPC_REGNUM,
349 (char *) &fpregsetp->fpcntl[0]);
350 regcache_raw_supply (current_regcache, M68K_FPS_REGNUM,
351 (char *) &fpregsetp->fpcntl[1]);
352 regcache_raw_supply (current_regcache, M68K_FPI_REGNUM,
353 (char *) &fpregsetp->fpcntl[2]);
c906108c
SS
354}
355
0280a90a
AS
356/* Fill register REGNO (if it is a floating-point register) in
357 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
358 do this for all registers. */
359
360void
361fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
362{
363 int i;
364
365 /* Fill in the floating-point registers. */
366 for (i = FP0_REGNUM; i < FP0_REGNUM + 8; i++)
367 if (regno == -1 || regno == i)
822c9732
AC
368 regcache_raw_collect (current_regcache, i,
369 FPREG_ADDR (fpregsetp, i - FP0_REGNUM));
0280a90a
AS
370
371 /* Fill in the floating-point control registers. */
32eeb91a 372 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
0280a90a 373 if (regno == -1 || regno == i)
822c9732
AC
374 regcache_raw_collect (current_regcache, i,
375 (char *) &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
0280a90a
AS
376}
377
378#ifdef HAVE_PTRACE_GETREGS
379
380/* Fetch all floating-point registers from process/thread TID and store
381 thier values in GDB's register array. */
382
383static void
384fetch_fpregs (int tid)
385{
386 elf_fpregset_t fpregs;
387
388 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 389 perror_with_name (_("Couldn't get floating point status"));
0280a90a
AS
390
391 supply_fpregset (&fpregs);
392}
393
394/* Store all valid floating-point registers in GDB's register array
395 into the process/thread specified by TID. */
396
397static void
398store_fpregs (int tid, int regno)
399{
400 elf_fpregset_t fpregs;
401
402 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 403 perror_with_name (_("Couldn't get floating point status"));
0280a90a
AS
404
405 fill_fpregset (&fpregs, regno);
406
407 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
e2e0b3e5 408 perror_with_name (_("Couldn't write floating point status"));
0280a90a
AS
409}
410
411#else
412
413static void fetch_fpregs (int tid) {}
414static void store_fpregs (int tid, int regno) {}
415
416#endif
417
c906108c 418#endif
0280a90a
AS
419\f
420/* Transferring arbitrary registers between GDB and inferior. */
421
422/* Fetch register REGNO from the child process. If REGNO is -1, do
423 this for all registers (including the floating point and SSE
424 registers). */
425
10d6c8cd
DJ
426static void
427m68k_linux_fetch_inferior_registers (int regno)
0280a90a
AS
428{
429 int tid;
430
431 /* Use the old method of peeking around in `struct user' if the
432 GETREGS request isn't available. */
433 if (! have_ptrace_getregs)
434 {
435 old_fetch_inferior_registers (regno);
436 return;
437 }
438
a4b6fc86 439 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
440 tid = TIDGET (inferior_ptid);
441 if (tid == 0)
0280a90a 442 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f175af98 443
0280a90a
AS
444 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
445 transfers more registers in one system call, and we'll cache the
446 results. But remember that fetch_fpxregs can fail, and return
447 zero. */
448 if (regno == -1)
449 {
450 fetch_regs (tid);
451
452 /* The call above might reset `have_ptrace_getregs'. */
453 if (! have_ptrace_getregs)
454 {
455 old_fetch_inferior_registers (-1);
456 return;
457 }
458
459 fetch_fpregs (tid);
460 return;
461 }
462
463 if (getregs_supplies (regno))
464 {
465 fetch_regs (tid);
466 return;
467 }
468
469 if (getfpregs_supplies (regno))
470 {
471 fetch_fpregs (tid);
472 return;
473 }
474
475 internal_error (__FILE__, __LINE__,
e2e0b3e5 476 _("Got request for bad register number %d."), regno);
0280a90a
AS
477}
478
479/* Store register REGNO back into the child process. If REGNO is -1,
480 do this for all registers (including the floating point and SSE
481 registers). */
10d6c8cd
DJ
482static void
483m68k_linux_store_inferior_registers (int regno)
0280a90a
AS
484{
485 int tid;
486
487 /* Use the old method of poking around in `struct user' if the
488 SETREGS request isn't available. */
489 if (! have_ptrace_getregs)
490 {
491 old_store_inferior_registers (regno);
492 return;
493 }
494
a4b6fc86 495 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
496 tid = TIDGET (inferior_ptid);
497 if (tid == 0)
0280a90a
AS
498 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
499
500 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
501 transfers more registers in one system call. But remember that
502 store_fpregs can fail, and return zero. */
503 if (regno == -1)
504 {
505 store_regs (tid, regno);
506 store_fpregs (tid, regno);
507 return;
508 }
509
510 if (getregs_supplies (regno))
511 {
512 store_regs (tid, regno);
513 return;
514 }
515
516 if (getfpregs_supplies (regno))
517 {
518 store_fpregs (tid, regno);
519 return;
520 }
521
522 internal_error (__FILE__, __LINE__,
e2e0b3e5 523 _("Got request to store bad register number %d."), regno);
0280a90a 524}
f175af98
DJ
525\f
526/* Interpreting register set info found in core files. */
527
528/* Provide registers to GDB from a core file.
529
530 (We can't use the generic version of this function in
531 core-regset.c, because we need to use elf_gregset_t instead of
532 gregset_t.)
533
534 CORE_REG_SECT points to an array of bytes, which are the contents
535 of a `note' from a core file which BFD thinks might contain
536 register contents. CORE_REG_SIZE is its size.
537
538 WHICH says which register set corelow suspects this is:
539 0 --- the general-purpose register set, in elf_gregset_t format
540 2 --- the floating-point register set, in elf_fpregset_t format
541
a4b6fc86 542 REG_ADDR isn't used on GNU/Linux. */
f175af98
DJ
543
544static void
545fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
546 int which, CORE_ADDR reg_addr)
547{
548 elf_gregset_t gregset;
549 elf_fpregset_t fpregset;
550
551 switch (which)
552 {
553 case 0:
554 if (core_reg_size != sizeof (gregset))
8a3fe4f8 555 warning (_("Wrong size gregset in core file."));
f175af98
DJ
556 else
557 {
558 memcpy (&gregset, core_reg_sect, sizeof (gregset));
559 supply_gregset (&gregset);
560 }
561 break;
562
563 case 2:
564 if (core_reg_size != sizeof (fpregset))
8a3fe4f8 565 warning (_("Wrong size fpregset in core file."));
f175af98
DJ
566 else
567 {
568 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
569 supply_fpregset (&fpregset);
570 }
571 break;
572
573 default:
574 /* We've covered all the kinds of registers we know about here,
575 so this must be something we wouldn't know what to do with
576 anyway. Just ignore it. */
577 break;
578 }
579}
c906108c 580\f
c5aa993b 581
a4b6fc86
AC
582/* Register that we are able to handle GNU/Linux ELF core file
583 formats. */
f175af98
DJ
584
585static struct core_fns linux_elf_core_fns =
586{
587 bfd_target_elf_flavour, /* core_flavour */
588 default_check_format, /* check_format */
589 default_core_sniffer, /* core_sniffer */
590 fetch_core_registers, /* core_read_registers */
591 NULL /* next */
592};
593
10d6c8cd
DJ
594void _initialize_m68k_linux_nat (void);
595
f175af98 596void
5ae5f592 597_initialize_m68k_linux_nat (void)
f175af98 598{
10d6c8cd
DJ
599 struct target_ops *t;
600
601 /* Fill in the generic GNU/Linux methods. */
602 t = linux_target ();
603
604 /* Add our register access methods. */
605 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
606 t->to_store_registers = m68k_linux_store_inferior_registers;
607
608 /* Register the target. */
f973ed9c 609 linux_nat_add_target (t);
10d6c8cd 610
00e32a35 611 deprecated_add_core_fns (&linux_elf_core_fns);
f175af98 612}
This page took 0.574085 seconds and 4 git commands to generate.