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